blob: 3ab92222a52552fa8463270198ff0b1f6d640c62 [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>
35
36static int flip_x;
37module_param(flip_x, bool, 0644);
38MODULE_PARM_DESC(flip_x, "flip x coordinate");
39
40static int flip_y;
41module_param(flip_y, bool, 0644);
42MODULE_PARM_DESC(flip_y, "flip y coordinate");
43
44struct eeti_ts_priv {
45 struct i2c_client *client;
46 struct input_dev *input;
47 struct work_struct work;
48 struct mutex mutex;
49 int irq;
50};
51
52#define EETI_TS_BITDEPTH (11)
53#define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
54
55#define REPORT_BIT_PRESSED (1 << 0)
56#define REPORT_BIT_AD0 (1 << 1)
57#define REPORT_BIT_AD1 (1 << 2)
58#define REPORT_BIT_HAS_PRESSURE (1 << 6)
59#define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
60
61static void eeti_ts_read(struct work_struct *work)
62{
63 char buf[6];
64 unsigned int x, y, res, pressed, to = 100;
65 struct eeti_ts_priv *priv =
66 container_of(work, struct eeti_ts_priv, work);
67
68 mutex_lock(&priv->mutex);
69
70 while (!gpio_get_value(irq_to_gpio(priv->irq)) && --to)
71 i2c_master_recv(priv->client, buf, sizeof(buf));
72
73 if (!to) {
74 dev_err(&priv->client->dev,
75 "unable to clear IRQ - line stuck?\n");
76 goto out;
77 }
78
79 /* drop non-report packets */
80 if (!(buf[0] & 0x80))
81 goto out;
82
83 pressed = buf[0] & REPORT_BIT_PRESSED;
84 res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
85 x = buf[2] | (buf[1] << 8);
86 y = buf[4] | (buf[3] << 8);
87
88 /* fix the range to 11 bits */
89 x >>= res - EETI_TS_BITDEPTH;
90 y >>= res - EETI_TS_BITDEPTH;
91
92 if (flip_x)
93 x = EETI_MAXVAL - x;
94
95 if (flip_y)
96 y = EETI_MAXVAL - y;
97
98 if (buf[0] & REPORT_BIT_HAS_PRESSURE)
99 input_report_abs(priv->input, ABS_PRESSURE, buf[5]);
100
101 input_report_abs(priv->input, ABS_X, x);
102 input_report_abs(priv->input, ABS_Y, y);
103 input_report_key(priv->input, BTN_TOUCH, !!pressed);
104 input_sync(priv->input);
105
106out:
107 mutex_unlock(&priv->mutex);
108}
109
110static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
111{
112 struct eeti_ts_priv *priv = dev_id;
113
114 /* postpone I2C transactions as we are atomic */
115 schedule_work(&priv->work);
116
117 return IRQ_HANDLED;
118}
119
120static int eeti_ts_open(struct input_dev *dev)
121{
122 struct eeti_ts_priv *priv = input_get_drvdata(dev);
123
124 enable_irq(priv->irq);
125
126 /* Read the events once to arm the IRQ */
127 eeti_ts_read(&priv->work);
128
129 return 0;
130}
131
132static void eeti_ts_close(struct input_dev *dev)
133{
134 struct eeti_ts_priv *priv = input_get_drvdata(dev);
135
136 disable_irq(priv->irq);
137 cancel_work_sync(&priv->work);
138}
139
140static int __devinit eeti_ts_probe(struct i2c_client *client,
141 const struct i2c_device_id *idp)
142{
143 struct eeti_ts_priv *priv;
144 struct input_dev *input;
145 int err = -ENOMEM;
146
147 /* In contrast to what's described in the datasheet, there seems
148 * to be no way of probing the presence of that device using I2C
149 * commands. So we need to blindly believe it is there, and wait
150 * for interrupts to occur. */
151
152 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
153 if (!priv) {
154 dev_err(&client->dev, "failed to allocate driver data\n");
155 goto err0;
156 }
157
158 mutex_init(&priv->mutex);
159 input = input_allocate_device();
160
161 if (!input) {
162 dev_err(&client->dev, "Failed to allocate input device.\n");
163 goto err1;
164 }
165
166 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
167 input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
168
169 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
170 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
171 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
172
173 input->name = client->name;
174 input->id.bustype = BUS_I2C;
175 input->dev.parent = &client->dev;
176 input->open = eeti_ts_open;
177 input->close = eeti_ts_close;
178
179 priv->client = client;
180 priv->input = input;
181 priv->irq = client->irq;
182
183 INIT_WORK(&priv->work, eeti_ts_read);
184 i2c_set_clientdata(client, priv);
185 input_set_drvdata(input, priv);
186
187 err = input_register_device(input);
188 if (err)
189 goto err1;
190
191 err = request_irq(priv->irq, eeti_ts_isr, IRQF_TRIGGER_FALLING,
192 client->name, priv);
193 if (err) {
194 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
195 goto err2;
196 }
197
198 /* Disable the irq for now. It will be enabled once the input device
199 * is opened. */
200 disable_irq(priv->irq);
201
202 device_init_wakeup(&client->dev, 0);
203 return 0;
204
205err2:
206 input_unregister_device(input);
207 input = NULL; /* so we dont try to free it below */
208err1:
209 input_free_device(input);
210 i2c_set_clientdata(client, NULL);
211 kfree(priv);
212err0:
213 return err;
214}
215
216static int __devexit eeti_ts_remove(struct i2c_client *client)
217{
218 struct eeti_ts_priv *priv = i2c_get_clientdata(client);
219
220 free_irq(priv->irq, priv);
221 input_unregister_device(priv->input);
222 i2c_set_clientdata(client, NULL);
223 kfree(priv);
224
225 return 0;
226}
227
228#ifdef CONFIG_PM
229static int eeti_ts_suspend(struct i2c_client *client, pm_message_t mesg)
230{
231 struct eeti_ts_priv *priv = i2c_get_clientdata(client);
232
233 if (device_may_wakeup(&client->dev))
234 enable_irq_wake(priv->irq);
235
236 return 0;
237}
238
239static int eeti_ts_resume(struct i2c_client *client)
240{
241 struct eeti_ts_priv *priv = i2c_get_clientdata(client);
242
243 if (device_may_wakeup(&client->dev))
244 disable_irq_wake(priv->irq);
245
246 return 0;
247}
248#else
249#define eeti_ts_suspend NULL
250#define eeti_ts_resume NULL
251#endif
252
253static const struct i2c_device_id eeti_ts_id[] = {
254 { "eeti_ts", 0 },
255 { }
256};
257MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
258
259static struct i2c_driver eeti_ts_driver = {
260 .driver = {
261 .name = "eeti_ts",
262 },
263 .probe = eeti_ts_probe,
264 .remove = __devexit_p(eeti_ts_remove),
265 .suspend = eeti_ts_suspend,
266 .resume = eeti_ts_resume,
267 .id_table = eeti_ts_id,
268};
269
270static int __init eeti_ts_init(void)
271{
272 return i2c_add_driver(&eeti_ts_driver);
273}
274
275static void __exit eeti_ts_exit(void)
276{
277 i2c_del_driver(&eeti_ts_driver);
278}
279
280MODULE_DESCRIPTION("EETI Touchscreen driver");
281MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
282MODULE_LICENSE("GPL");
283
284module_init(eeti_ts_init);
285module_exit(eeti_ts_exit);
286