2 * Touch Screen driver for EETI's I2C connected touch screen panels
3 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
5 * See EETI's software guide for the protocol specification:
6 * http://home.eeti.com.tw/web20/eg/guide.htm
9 * Copyright (c) 2008 Magnus Damm
10 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
12 * This file is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This file is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/kernel.h>
30 #include <linux/input.h>
31 #include <linux/interrupt.h>
32 #include <linux/i2c.h>
33 #include <linux/timer.h>
34 #include <linux/gpio.h>
35 #include <linux/input/eeti_ts.h>
36 #include <linux/slab.h>
37 #include <asm/unaligned.h>
40 module_param(flip_x, bool, 0644);
41 MODULE_PARM_DESC(flip_x, "flip x coordinate");
44 module_param(flip_y, bool, 0644);
45 MODULE_PARM_DESC(flip_y, "flip y coordinate");
48 struct i2c_client *client;
49 struct input_dev *input;
50 struct work_struct work;
52 int irq_gpio, irq, irq_active_high;
55 #define EETI_TS_BITDEPTH (11)
56 #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
58 #define REPORT_BIT_PRESSED BIT(0)
59 #define REPORT_BIT_AD0 BIT(1)
60 #define REPORT_BIT_AD1 BIT(2)
61 #define REPORT_BIT_HAS_PRESSURE BIT(6)
62 #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
64 static inline int eeti_ts_irq_active(struct eeti_ts *eeti)
66 return gpio_get_value(eeti->irq_gpio) == eeti->irq_active_high;
69 static void eeti_ts_read(struct work_struct *work)
72 unsigned int x, y, res, pressed, to = 100;
73 struct eeti_ts *eeti =
74 container_of(work, struct eeti_ts, work);
76 mutex_lock(&eeti->mutex);
78 while (eeti_ts_irq_active(eeti) && --to)
79 i2c_master_recv(eeti->client, buf, sizeof(buf));
82 dev_err(&eeti->client->dev,
83 "unable to clear IRQ - line stuck?\n");
87 /* drop non-report packets */
91 pressed = buf[0] & REPORT_BIT_PRESSED;
92 res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
94 x = get_unaligned_be16(&buf[1]);
95 y = get_unaligned_be16(&buf[3]);
97 /* fix the range to 11 bits */
98 x >>= res - EETI_TS_BITDEPTH;
99 y >>= res - EETI_TS_BITDEPTH;
107 if (buf[0] & REPORT_BIT_HAS_PRESSURE)
108 input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
110 input_report_abs(eeti->input, ABS_X, x);
111 input_report_abs(eeti->input, ABS_Y, y);
112 input_report_key(eeti->input, BTN_TOUCH, !!pressed);
113 input_sync(eeti->input);
116 mutex_unlock(&eeti->mutex);
119 static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
121 struct eeti_ts *eeti = dev_id;
123 /* postpone I2C transactions as we are atomic */
124 schedule_work(&eeti->work);
129 static void eeti_ts_start(struct eeti_ts *eeti)
131 enable_irq(eeti->irq);
133 /* Read the events once to arm the IRQ */
134 eeti_ts_read(&eeti->work);
137 static void eeti_ts_stop(struct eeti_ts *eeti)
139 disable_irq(eeti->irq);
140 cancel_work_sync(&eeti->work);
143 static int eeti_ts_open(struct input_dev *dev)
145 struct eeti_ts *eeti = input_get_drvdata(dev);
152 static void eeti_ts_close(struct input_dev *dev)
154 struct eeti_ts *eeti = input_get_drvdata(dev);
159 static int eeti_ts_probe(struct i2c_client *client,
160 const struct i2c_device_id *idp)
162 struct eeti_ts_platform_data *pdata = dev_get_platdata(&client->dev);
163 struct eeti_ts *eeti;
164 struct input_dev *input;
165 unsigned int irq_flags;
169 * In contrast to what's described in the datasheet, there seems
170 * to be no way of probing the presence of that device using I2C
171 * commands. So we need to blindly believe it is there, and wait
172 * for interrupts to occur.
175 eeti = kzalloc(sizeof(*eeti), GFP_KERNEL);
177 dev_err(&client->dev, "failed to allocate driver data\n");
181 mutex_init(&eeti->mutex);
183 input = input_allocate_device();
185 dev_err(&client->dev, "Failed to allocate input device.\n");
189 input_set_capability(input, EV_KEY, BTN_TOUCH);
191 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
192 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
193 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
195 input->name = client->name;
196 input->id.bustype = BUS_I2C;
197 input->dev.parent = &client->dev;
198 input->open = eeti_ts_open;
199 input->close = eeti_ts_close;
201 eeti->client = client;
203 eeti->irq_gpio = pdata->irq_gpio;
204 eeti->irq = gpio_to_irq(pdata->irq_gpio);
206 err = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
210 eeti->irq_active_high = pdata->irq_active_high;
212 irq_flags = eeti->irq_active_high ?
213 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
215 INIT_WORK(&eeti->work, eeti_ts_read);
216 i2c_set_clientdata(client, eeti);
217 input_set_drvdata(input, eeti);
219 err = input_register_device(input);
223 err = request_irq(eeti->irq, eeti_ts_isr, irq_flags,
226 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
231 * Disable the device for now. It will be enabled once the
232 * input device is opened.
239 input_unregister_device(input);
240 input = NULL; /* so we dont try to free it below */
242 gpio_free(pdata->irq_gpio);
244 input_free_device(input);
249 static int eeti_ts_remove(struct i2c_client *client)
251 struct eeti_ts *eeti = i2c_get_clientdata(client);
253 free_irq(eeti->irq, eeti);
255 * eeti_ts_stop() leaves IRQ disabled. We need to re-enable it
256 * so that device still works if we reload the driver.
258 enable_irq(eeti->irq);
260 input_unregister_device(eeti->input);
266 static int __maybe_unused eeti_ts_suspend(struct device *dev)
268 struct i2c_client *client = to_i2c_client(dev);
269 struct eeti_ts *eeti = i2c_get_clientdata(client);
270 struct input_dev *input_dev = eeti->input;
272 mutex_lock(&input_dev->mutex);
274 if (input_dev->users)
277 mutex_unlock(&input_dev->mutex);
279 if (device_may_wakeup(&client->dev))
280 enable_irq_wake(eeti->irq);
285 static int __maybe_unused eeti_ts_resume(struct device *dev)
287 struct i2c_client *client = to_i2c_client(dev);
288 struct eeti_ts *eeti = i2c_get_clientdata(client);
289 struct input_dev *input_dev = eeti->input;
291 if (device_may_wakeup(&client->dev))
292 disable_irq_wake(eeti->irq);
294 mutex_lock(&input_dev->mutex);
296 if (input_dev->users)
299 mutex_unlock(&input_dev->mutex);
304 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
306 static const struct i2c_device_id eeti_ts_id[] = {
310 MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
312 static struct i2c_driver eeti_ts_driver = {
317 .probe = eeti_ts_probe,
318 .remove = eeti_ts_remove,
319 .id_table = eeti_ts_id,
322 module_i2c_driver(eeti_ts_driver);
324 MODULE_DESCRIPTION("EETI Touchscreen driver");
325 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
326 MODULE_LICENSE("GPL");