blob: 55255a940072e7a0fb488670d984fd0c1df1de0e [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Daniel Mack10494dc2009-05-18 16:10:39 -070037
Rusty Russell90ab5ee2012-01-13 09:32:20 +103038static bool flip_x;
Daniel Mack10494dc2009-05-18 16:10:39 -070039module_param(flip_x, bool, 0644);
40MODULE_PARM_DESC(flip_x, "flip x coordinate");
41
Rusty Russell90ab5ee2012-01-13 09:32:20 +103042static bool flip_y;
Daniel Mack10494dc2009-05-18 16:10:39 -070043module_param(flip_y, bool, 0644);
44MODULE_PARM_DESC(flip_y, "flip y coordinate");
45
46struct eeti_ts_priv {
47 struct i2c_client *client;
48 struct input_dev *input;
49 struct work_struct work;
50 struct mutex mutex;
Arnd Bergmann4eef6cb2012-04-30 16:21:37 +000051 int irq_gpio, irq, irq_active_high;
Daniel Mack10494dc2009-05-18 16:10:39 -070052};
53
54#define EETI_TS_BITDEPTH (11)
55#define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
56
57#define REPORT_BIT_PRESSED (1 << 0)
58#define REPORT_BIT_AD0 (1 << 1)
59#define REPORT_BIT_AD1 (1 << 2)
60#define REPORT_BIT_HAS_PRESSURE (1 << 6)
61#define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
62
Daniel Mack25a70e32009-08-12 00:50:09 -070063static inline int eeti_ts_irq_active(struct eeti_ts_priv *priv)
64{
Arnd Bergmann4eef6cb2012-04-30 16:21:37 +000065 return gpio_get_value(priv->irq_gpio) == priv->irq_active_high;
Daniel Mack25a70e32009-08-12 00:50:09 -070066}
67
Daniel Mack10494dc2009-05-18 16:10:39 -070068static void eeti_ts_read(struct work_struct *work)
69{
70 char buf[6];
71 unsigned int x, y, res, pressed, to = 100;
72 struct eeti_ts_priv *priv =
73 container_of(work, struct eeti_ts_priv, work);
74
75 mutex_lock(&priv->mutex);
76
Daniel Mack25a70e32009-08-12 00:50:09 -070077 while (eeti_ts_irq_active(priv) && --to)
Daniel Mack10494dc2009-05-18 16:10:39 -070078 i2c_master_recv(priv->client, buf, sizeof(buf));
79
80 if (!to) {
81 dev_err(&priv->client->dev,
82 "unable to clear IRQ - line stuck?\n");
83 goto out;
84 }
85
86 /* drop non-report packets */
87 if (!(buf[0] & 0x80))
88 goto out;
89
90 pressed = buf[0] & REPORT_BIT_PRESSED;
91 res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
92 x = buf[2] | (buf[1] << 8);
93 y = buf[4] | (buf[3] << 8);
94
95 /* fix the range to 11 bits */
96 x >>= res - EETI_TS_BITDEPTH;
97 y >>= res - EETI_TS_BITDEPTH;
98
99 if (flip_x)
100 x = EETI_MAXVAL - x;
101
102 if (flip_y)
103 y = EETI_MAXVAL - y;
104
105 if (buf[0] & REPORT_BIT_HAS_PRESSURE)
106 input_report_abs(priv->input, ABS_PRESSURE, buf[5]);
107
108 input_report_abs(priv->input, ABS_X, x);
109 input_report_abs(priv->input, ABS_Y, y);
110 input_report_key(priv->input, BTN_TOUCH, !!pressed);
111 input_sync(priv->input);
112
113out:
114 mutex_unlock(&priv->mutex);
115}
116
117static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
118{
119 struct eeti_ts_priv *priv = dev_id;
120
121 /* postpone I2C transactions as we are atomic */
122 schedule_work(&priv->work);
123
124 return IRQ_HANDLED;
125}
126
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700127static void eeti_ts_start(struct eeti_ts_priv *priv)
Daniel Mack10494dc2009-05-18 16:10:39 -0700128{
Daniel Mack10494dc2009-05-18 16:10:39 -0700129 enable_irq(priv->irq);
130
131 /* Read the events once to arm the IRQ */
132 eeti_ts_read(&priv->work);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700133}
134
135static void eeti_ts_stop(struct eeti_ts_priv *priv)
136{
137 disable_irq(priv->irq);
138 cancel_work_sync(&priv->work);
139}
140
141static int eeti_ts_open(struct input_dev *dev)
142{
143 struct eeti_ts_priv *priv = input_get_drvdata(dev);
144
145 eeti_ts_start(priv);
Daniel Mack10494dc2009-05-18 16:10:39 -0700146
147 return 0;
148}
149
150static void eeti_ts_close(struct input_dev *dev)
151{
152 struct eeti_ts_priv *priv = input_get_drvdata(dev);
153
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700154 eeti_ts_stop(priv);
Daniel Mack10494dc2009-05-18 16:10:39 -0700155}
156
Bill Pemberton5298cc42012-11-23 21:38:25 -0800157static int eeti_ts_probe(struct i2c_client *client,
Daniel Mack10494dc2009-05-18 16:10:39 -0700158 const struct i2c_device_id *idp)
159{
Arnd Bergmann4eef6cb2012-04-30 16:21:37 +0000160 struct eeti_ts_platform_data *pdata = client->dev.platform_data;
Daniel Mack10494dc2009-05-18 16:10:39 -0700161 struct eeti_ts_priv *priv;
162 struct input_dev *input;
Daniel Mack25a70e32009-08-12 00:50:09 -0700163 unsigned int irq_flags;
Daniel Mack10494dc2009-05-18 16:10:39 -0700164 int err = -ENOMEM;
165
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700166 /*
167 * In contrast to what's described in the datasheet, there seems
Daniel Mack10494dc2009-05-18 16:10:39 -0700168 * to be no way of probing the presence of that device using I2C
169 * commands. So we need to blindly believe it is there, and wait
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700170 * for interrupts to occur.
171 */
Daniel Mack10494dc2009-05-18 16:10:39 -0700172
173 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
174 if (!priv) {
175 dev_err(&client->dev, "failed to allocate driver data\n");
176 goto err0;
177 }
178
179 mutex_init(&priv->mutex);
180 input = input_allocate_device();
181
182 if (!input) {
183 dev_err(&client->dev, "Failed to allocate input device.\n");
184 goto err1;
185 }
186
187 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
188 input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
189
190 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
191 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
192 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
193
194 input->name = client->name;
195 input->id.bustype = BUS_I2C;
196 input->dev.parent = &client->dev;
197 input->open = eeti_ts_open;
198 input->close = eeti_ts_close;
199
200 priv->client = client;
201 priv->input = input;
Arnd Bergmann4eef6cb2012-04-30 16:21:37 +0000202 priv->irq_gpio = pdata->irq_gpio;
203 priv->irq = gpio_to_irq(pdata->irq_gpio);
Daniel Mack10494dc2009-05-18 16:10:39 -0700204
Arnd Bergmann4eef6cb2012-04-30 16:21:37 +0000205 err = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
206 if (err < 0)
207 goto err1;
Daniel Mack25a70e32009-08-12 00:50:09 -0700208
209 if (pdata)
210 priv->irq_active_high = pdata->irq_active_high;
211
212 irq_flags = priv->irq_active_high ?
213 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
214
Daniel Mack10494dc2009-05-18 16:10:39 -0700215 INIT_WORK(&priv->work, eeti_ts_read);
216 i2c_set_clientdata(client, priv);
217 input_set_drvdata(input, priv);
218
219 err = input_register_device(input);
220 if (err)
Arnd Bergmann4eef6cb2012-04-30 16:21:37 +0000221 goto err2;
Daniel Mack10494dc2009-05-18 16:10:39 -0700222
Daniel Mack25a70e32009-08-12 00:50:09 -0700223 err = request_irq(priv->irq, eeti_ts_isr, irq_flags,
Daniel Mack10494dc2009-05-18 16:10:39 -0700224 client->name, priv);
225 if (err) {
226 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
Arnd Bergmann4eef6cb2012-04-30 16:21:37 +0000227 goto err3;
Daniel Mack10494dc2009-05-18 16:10:39 -0700228 }
229
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700230 /*
231 * Disable the device for now. It will be enabled once the
232 * input device is opened.
233 */
234 eeti_ts_stop(priv);
Daniel Mack10494dc2009-05-18 16:10:39 -0700235
236 device_init_wakeup(&client->dev, 0);
237 return 0;
238
Arnd Bergmann4eef6cb2012-04-30 16:21:37 +0000239err3:
Daniel Mack10494dc2009-05-18 16:10:39 -0700240 input_unregister_device(input);
241 input = NULL; /* so we dont try to free it below */
Arnd Bergmann4eef6cb2012-04-30 16:21:37 +0000242err2:
243 gpio_free(pdata->irq_gpio);
Daniel Mack10494dc2009-05-18 16:10:39 -0700244err1:
245 input_free_device(input);
Daniel Mack10494dc2009-05-18 16:10:39 -0700246 kfree(priv);
247err0:
248 return err;
249}
250
Bill Pembertone2619cf2012-11-23 21:50:47 -0800251static int eeti_ts_remove(struct i2c_client *client)
Daniel Mack10494dc2009-05-18 16:10:39 -0700252{
253 struct eeti_ts_priv *priv = i2c_get_clientdata(client);
254
255 free_irq(priv->irq, priv);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700256 /*
257 * eeti_ts_stop() leaves IRQ disabled. We need to re-enable it
258 * so that device still works if we reload the driver.
259 */
260 enable_irq(priv->irq);
261
Daniel Mack10494dc2009-05-18 16:10:39 -0700262 input_unregister_device(priv->input);
Daniel Mack10494dc2009-05-18 16:10:39 -0700263 kfree(priv);
264
265 return 0;
266}
267
268#ifdef CONFIG_PM
Mark Brown85012ff2011-01-06 23:01:02 -0800269static int eeti_ts_suspend(struct device *dev)
Daniel Mack10494dc2009-05-18 16:10:39 -0700270{
Mark Brown85012ff2011-01-06 23:01:02 -0800271 struct i2c_client *client = to_i2c_client(dev);
Daniel Mack10494dc2009-05-18 16:10:39 -0700272 struct eeti_ts_priv *priv = i2c_get_clientdata(client);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700273 struct input_dev *input_dev = priv->input;
274
275 mutex_lock(&input_dev->mutex);
276
277 if (input_dev->users)
278 eeti_ts_stop(priv);
279
280 mutex_unlock(&input_dev->mutex);
Daniel Mack10494dc2009-05-18 16:10:39 -0700281
282 if (device_may_wakeup(&client->dev))
283 enable_irq_wake(priv->irq);
284
285 return 0;
286}
287
Mark Brown85012ff2011-01-06 23:01:02 -0800288static int eeti_ts_resume(struct device *dev)
Daniel Mack10494dc2009-05-18 16:10:39 -0700289{
Mark Brown85012ff2011-01-06 23:01:02 -0800290 struct i2c_client *client = to_i2c_client(dev);
Daniel Mack10494dc2009-05-18 16:10:39 -0700291 struct eeti_ts_priv *priv = i2c_get_clientdata(client);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700292 struct input_dev *input_dev = priv->input;
Daniel Mack10494dc2009-05-18 16:10:39 -0700293
294 if (device_may_wakeup(&client->dev))
295 disable_irq_wake(priv->irq);
296
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700297 mutex_lock(&input_dev->mutex);
298
299 if (input_dev->users)
300 eeti_ts_start(priv);
301
302 mutex_unlock(&input_dev->mutex);
303
Daniel Mack10494dc2009-05-18 16:10:39 -0700304 return 0;
305}
Mark Brown85012ff2011-01-06 23:01:02 -0800306
307static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
Daniel Mack10494dc2009-05-18 16:10:39 -0700308#endif
309
310static const struct i2c_device_id eeti_ts_id[] = {
311 { "eeti_ts", 0 },
312 { }
313};
314MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
315
316static struct i2c_driver eeti_ts_driver = {
317 .driver = {
318 .name = "eeti_ts",
Mark Brown85012ff2011-01-06 23:01:02 -0800319#ifdef CONFIG_PM
320 .pm = &eeti_ts_pm,
321#endif
Daniel Mack10494dc2009-05-18 16:10:39 -0700322 },
323 .probe = eeti_ts_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800324 .remove = eeti_ts_remove,
Daniel Mack10494dc2009-05-18 16:10:39 -0700325 .id_table = eeti_ts_id,
326};
327
Axel Lin1b92c1c2012-03-16 23:05:41 -0700328module_i2c_driver(eeti_ts_driver);
Daniel Mack10494dc2009-05-18 16:10:39 -0700329
330MODULE_DESCRIPTION("EETI Touchscreen driver");
331MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
332MODULE_LICENSE("GPL");