blob: 6fb6bd8495e7026884b4417d4bfbe3d737fe5efe [file] [log] [blame]
Daniel Mack10494dc2009-05-18 16:10:39 -07001/*
2 * Touch Screen driver for EETI's I2C connected touch screen panels
3 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
4 *
5 * See EETI's software guide for the protocol specification:
6 * http://home.eeti.com.tw/web20/eg/guide.htm
7 *
8 * Based on migor_ts.c
9 * Copyright (c) 2008 Magnus Damm
10 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
11 *
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.
16 *
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.
21 *
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
25 */
26
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>
Daniel Mack25a70e32009-08-12 00:50:09 -070035#include <linux/input/eeti_ts.h>
Daniel Mack10494dc2009-05-18 16:10:39 -070036
37static int flip_x;
38module_param(flip_x, bool, 0644);
39MODULE_PARM_DESC(flip_x, "flip x coordinate");
40
41static int flip_y;
42module_param(flip_y, bool, 0644);
43MODULE_PARM_DESC(flip_y, "flip y coordinate");
44
45struct eeti_ts_priv {
46 struct i2c_client *client;
47 struct input_dev *input;
48 struct work_struct work;
49 struct mutex mutex;
Daniel Mack25a70e32009-08-12 00:50:09 -070050 int irq, irq_active_high;
Daniel Mack10494dc2009-05-18 16:10:39 -070051};
52
53#define EETI_TS_BITDEPTH (11)
54#define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
55
56#define REPORT_BIT_PRESSED (1 << 0)
57#define REPORT_BIT_AD0 (1 << 1)
58#define REPORT_BIT_AD1 (1 << 2)
59#define REPORT_BIT_HAS_PRESSURE (1 << 6)
60#define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
61
Daniel Mack25a70e32009-08-12 00:50:09 -070062static inline int eeti_ts_irq_active(struct eeti_ts_priv *priv)
63{
64 return gpio_get_value(irq_to_gpio(priv->irq)) == priv->irq_active_high;
65}
66
Daniel Mack10494dc2009-05-18 16:10:39 -070067static void eeti_ts_read(struct work_struct *work)
68{
69 char buf[6];
70 unsigned int x, y, res, pressed, to = 100;
71 struct eeti_ts_priv *priv =
72 container_of(work, struct eeti_ts_priv, work);
73
74 mutex_lock(&priv->mutex);
75
Daniel Mack25a70e32009-08-12 00:50:09 -070076 while (eeti_ts_irq_active(priv) && --to)
Daniel Mack10494dc2009-05-18 16:10:39 -070077 i2c_master_recv(priv->client, buf, sizeof(buf));
78
79 if (!to) {
80 dev_err(&priv->client->dev,
81 "unable to clear IRQ - line stuck?\n");
82 goto out;
83 }
84
85 /* drop non-report packets */
86 if (!(buf[0] & 0x80))
87 goto out;
88
89 pressed = buf[0] & REPORT_BIT_PRESSED;
90 res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
91 x = buf[2] | (buf[1] << 8);
92 y = buf[4] | (buf[3] << 8);
93
94 /* fix the range to 11 bits */
95 x >>= res - EETI_TS_BITDEPTH;
96 y >>= res - EETI_TS_BITDEPTH;
97
98 if (flip_x)
99 x = EETI_MAXVAL - x;
100
101 if (flip_y)
102 y = EETI_MAXVAL - y;
103
104 if (buf[0] & REPORT_BIT_HAS_PRESSURE)
105 input_report_abs(priv->input, ABS_PRESSURE, buf[5]);
106
107 input_report_abs(priv->input, ABS_X, x);
108 input_report_abs(priv->input, ABS_Y, y);
109 input_report_key(priv->input, BTN_TOUCH, !!pressed);
110 input_sync(priv->input);
111
112out:
113 mutex_unlock(&priv->mutex);
114}
115
116static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
117{
118 struct eeti_ts_priv *priv = dev_id;
119
120 /* postpone I2C transactions as we are atomic */
121 schedule_work(&priv->work);
122
123 return IRQ_HANDLED;
124}
125
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700126static void eeti_ts_start(struct eeti_ts_priv *priv)
Daniel Mack10494dc2009-05-18 16:10:39 -0700127{
Daniel Mack10494dc2009-05-18 16:10:39 -0700128 enable_irq(priv->irq);
129
130 /* Read the events once to arm the IRQ */
131 eeti_ts_read(&priv->work);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700132}
133
134static void eeti_ts_stop(struct eeti_ts_priv *priv)
135{
136 disable_irq(priv->irq);
137 cancel_work_sync(&priv->work);
138}
139
140static int eeti_ts_open(struct input_dev *dev)
141{
142 struct eeti_ts_priv *priv = input_get_drvdata(dev);
143
144 eeti_ts_start(priv);
Daniel Mack10494dc2009-05-18 16:10:39 -0700145
146 return 0;
147}
148
149static void eeti_ts_close(struct input_dev *dev)
150{
151 struct eeti_ts_priv *priv = input_get_drvdata(dev);
152
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700153 eeti_ts_stop(priv);
Daniel Mack10494dc2009-05-18 16:10:39 -0700154}
155
156static int __devinit eeti_ts_probe(struct i2c_client *client,
157 const struct i2c_device_id *idp)
158{
Daniel Mack25a70e32009-08-12 00:50:09 -0700159 struct eeti_ts_platform_data *pdata;
Daniel Mack10494dc2009-05-18 16:10:39 -0700160 struct eeti_ts_priv *priv;
161 struct input_dev *input;
Daniel Mack25a70e32009-08-12 00:50:09 -0700162 unsigned int irq_flags;
Daniel Mack10494dc2009-05-18 16:10:39 -0700163 int err = -ENOMEM;
164
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700165 /*
166 * In contrast to what's described in the datasheet, there seems
Daniel Mack10494dc2009-05-18 16:10:39 -0700167 * to be no way of probing the presence of that device using I2C
168 * commands. So we need to blindly believe it is there, and wait
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700169 * for interrupts to occur.
170 */
Daniel Mack10494dc2009-05-18 16:10:39 -0700171
172 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
173 if (!priv) {
174 dev_err(&client->dev, "failed to allocate driver data\n");
175 goto err0;
176 }
177
178 mutex_init(&priv->mutex);
179 input = input_allocate_device();
180
181 if (!input) {
182 dev_err(&client->dev, "Failed to allocate input device.\n");
183 goto err1;
184 }
185
186 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
187 input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
188
189 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
190 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
191 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
192
193 input->name = client->name;
194 input->id.bustype = BUS_I2C;
195 input->dev.parent = &client->dev;
196 input->open = eeti_ts_open;
197 input->close = eeti_ts_close;
198
199 priv->client = client;
200 priv->input = input;
201 priv->irq = client->irq;
202
Daniel Mack25a70e32009-08-12 00:50:09 -0700203 pdata = client->dev.platform_data;
204
205 if (pdata)
206 priv->irq_active_high = pdata->irq_active_high;
207
208 irq_flags = priv->irq_active_high ?
209 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
210
Daniel Mack10494dc2009-05-18 16:10:39 -0700211 INIT_WORK(&priv->work, eeti_ts_read);
212 i2c_set_clientdata(client, priv);
213 input_set_drvdata(input, priv);
214
215 err = input_register_device(input);
216 if (err)
217 goto err1;
218
Daniel Mack25a70e32009-08-12 00:50:09 -0700219 err = request_irq(priv->irq, eeti_ts_isr, irq_flags,
Daniel Mack10494dc2009-05-18 16:10:39 -0700220 client->name, priv);
221 if (err) {
222 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
223 goto err2;
224 }
225
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700226 /*
227 * Disable the device for now. It will be enabled once the
228 * input device is opened.
229 */
230 eeti_ts_stop(priv);
Daniel Mack10494dc2009-05-18 16:10:39 -0700231
232 device_init_wakeup(&client->dev, 0);
233 return 0;
234
235err2:
236 input_unregister_device(input);
237 input = NULL; /* so we dont try to free it below */
238err1:
239 input_free_device(input);
240 i2c_set_clientdata(client, NULL);
241 kfree(priv);
242err0:
243 return err;
244}
245
246static int __devexit eeti_ts_remove(struct i2c_client *client)
247{
248 struct eeti_ts_priv *priv = i2c_get_clientdata(client);
249
250 free_irq(priv->irq, priv);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700251 /*
252 * eeti_ts_stop() leaves IRQ disabled. We need to re-enable it
253 * so that device still works if we reload the driver.
254 */
255 enable_irq(priv->irq);
256
Daniel Mack10494dc2009-05-18 16:10:39 -0700257 input_unregister_device(priv->input);
258 i2c_set_clientdata(client, NULL);
259 kfree(priv);
260
261 return 0;
262}
263
264#ifdef CONFIG_PM
265static int eeti_ts_suspend(struct i2c_client *client, pm_message_t mesg)
266{
267 struct eeti_ts_priv *priv = i2c_get_clientdata(client);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700268 struct input_dev *input_dev = priv->input;
269
270 mutex_lock(&input_dev->mutex);
271
272 if (input_dev->users)
273 eeti_ts_stop(priv);
274
275 mutex_unlock(&input_dev->mutex);
Daniel Mack10494dc2009-05-18 16:10:39 -0700276
277 if (device_may_wakeup(&client->dev))
278 enable_irq_wake(priv->irq);
279
280 return 0;
281}
282
283static int eeti_ts_resume(struct i2c_client *client)
284{
285 struct eeti_ts_priv *priv = i2c_get_clientdata(client);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700286 struct input_dev *input_dev = priv->input;
Daniel Mack10494dc2009-05-18 16:10:39 -0700287
288 if (device_may_wakeup(&client->dev))
289 disable_irq_wake(priv->irq);
290
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700291 mutex_lock(&input_dev->mutex);
292
293 if (input_dev->users)
294 eeti_ts_start(priv);
295
296 mutex_unlock(&input_dev->mutex);
297
Daniel Mack10494dc2009-05-18 16:10:39 -0700298 return 0;
299}
300#else
301#define eeti_ts_suspend NULL
302#define eeti_ts_resume NULL
303#endif
304
305static const struct i2c_device_id eeti_ts_id[] = {
306 { "eeti_ts", 0 },
307 { }
308};
309MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
310
311static struct i2c_driver eeti_ts_driver = {
312 .driver = {
313 .name = "eeti_ts",
314 },
315 .probe = eeti_ts_probe,
316 .remove = __devexit_p(eeti_ts_remove),
317 .suspend = eeti_ts_suspend,
318 .resume = eeti_ts_resume,
319 .id_table = eeti_ts_id,
320};
321
322static int __init eeti_ts_init(void)
323{
324 return i2c_add_driver(&eeti_ts_driver);
325}
326
327static void __exit eeti_ts_exit(void)
328{
329 i2c_del_driver(&eeti_ts_driver);
330}
331
332MODULE_DESCRIPTION("EETI Touchscreen driver");
333MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
334MODULE_LICENSE("GPL");
335
336module_init(eeti_ts_init);
337module_exit(eeti_ts_exit);
338