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>
38 module_param(flip_x
, bool, 0644);
39 MODULE_PARM_DESC(flip_x
, "flip x coordinate");
42 module_param(flip_y
, bool, 0644);
43 MODULE_PARM_DESC(flip_y
, "flip y coordinate");
46 struct i2c_client
*client
;
47 struct input_dev
*input
;
48 struct work_struct work
;
50 int irq
, irq_active_high
;
53 #define EETI_TS_BITDEPTH (11)
54 #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
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)
62 static inline int eeti_ts_irq_active(struct eeti_ts_priv
*priv
)
64 return gpio_get_value(irq_to_gpio(priv
->irq
)) == priv
->irq_active_high
;
67 static void eeti_ts_read(struct work_struct
*work
)
70 unsigned int x
, y
, res
, pressed
, to
= 100;
71 struct eeti_ts_priv
*priv
=
72 container_of(work
, struct eeti_ts_priv
, work
);
74 mutex_lock(&priv
->mutex
);
76 while (eeti_ts_irq_active(priv
) && --to
)
77 i2c_master_recv(priv
->client
, buf
, sizeof(buf
));
80 dev_err(&priv
->client
->dev
,
81 "unable to clear IRQ - line stuck?\n");
85 /* drop non-report packets */
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);
94 /* fix the range to 11 bits */
95 x
>>= res
- EETI_TS_BITDEPTH
;
96 y
>>= res
- EETI_TS_BITDEPTH
;
104 if (buf
[0] & REPORT_BIT_HAS_PRESSURE
)
105 input_report_abs(priv
->input
, ABS_PRESSURE
, buf
[5]);
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
);
113 mutex_unlock(&priv
->mutex
);
116 static irqreturn_t
eeti_ts_isr(int irq
, void *dev_id
)
118 struct eeti_ts_priv
*priv
= dev_id
;
120 /* postpone I2C transactions as we are atomic */
121 schedule_work(&priv
->work
);
126 static int eeti_ts_open(struct input_dev
*dev
)
128 struct eeti_ts_priv
*priv
= input_get_drvdata(dev
);
130 enable_irq(priv
->irq
);
132 /* Read the events once to arm the IRQ */
133 eeti_ts_read(&priv
->work
);
138 static void eeti_ts_close(struct input_dev
*dev
)
140 struct eeti_ts_priv
*priv
= input_get_drvdata(dev
);
142 disable_irq(priv
->irq
);
143 cancel_work_sync(&priv
->work
);
146 static int __devinit
eeti_ts_probe(struct i2c_client
*client
,
147 const struct i2c_device_id
*idp
)
149 struct eeti_ts_platform_data
*pdata
;
150 struct eeti_ts_priv
*priv
;
151 struct input_dev
*input
;
152 unsigned int irq_flags
;
155 /* In contrast to what's described in the datasheet, there seems
156 * to be no way of probing the presence of that device using I2C
157 * commands. So we need to blindly believe it is there, and wait
158 * for interrupts to occur. */
160 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
162 dev_err(&client
->dev
, "failed to allocate driver data\n");
166 mutex_init(&priv
->mutex
);
167 input
= input_allocate_device();
170 dev_err(&client
->dev
, "Failed to allocate input device.\n");
174 input
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
175 input
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
177 input_set_abs_params(input
, ABS_X
, 0, EETI_MAXVAL
, 0, 0);
178 input_set_abs_params(input
, ABS_Y
, 0, EETI_MAXVAL
, 0, 0);
179 input_set_abs_params(input
, ABS_PRESSURE
, 0, 0xff, 0, 0);
181 input
->name
= client
->name
;
182 input
->id
.bustype
= BUS_I2C
;
183 input
->dev
.parent
= &client
->dev
;
184 input
->open
= eeti_ts_open
;
185 input
->close
= eeti_ts_close
;
187 priv
->client
= client
;
189 priv
->irq
= client
->irq
;
191 pdata
= client
->dev
.platform_data
;
194 priv
->irq_active_high
= pdata
->irq_active_high
;
196 irq_flags
= priv
->irq_active_high
?
197 IRQF_TRIGGER_RISING
: IRQF_TRIGGER_FALLING
;
199 INIT_WORK(&priv
->work
, eeti_ts_read
);
200 i2c_set_clientdata(client
, priv
);
201 input_set_drvdata(input
, priv
);
203 err
= input_register_device(input
);
207 err
= request_irq(priv
->irq
, eeti_ts_isr
, irq_flags
,
210 dev_err(&client
->dev
, "Unable to request touchscreen IRQ.\n");
214 /* Disable the irq for now. It will be enabled once the input device
216 disable_irq(priv
->irq
);
218 device_init_wakeup(&client
->dev
, 0);
222 input_unregister_device(input
);
223 input
= NULL
; /* so we dont try to free it below */
225 input_free_device(input
);
226 i2c_set_clientdata(client
, NULL
);
232 static int __devexit
eeti_ts_remove(struct i2c_client
*client
)
234 struct eeti_ts_priv
*priv
= i2c_get_clientdata(client
);
236 free_irq(priv
->irq
, priv
);
237 input_unregister_device(priv
->input
);
238 i2c_set_clientdata(client
, NULL
);
245 static int eeti_ts_suspend(struct i2c_client
*client
, pm_message_t mesg
)
247 struct eeti_ts_priv
*priv
= i2c_get_clientdata(client
);
249 if (device_may_wakeup(&client
->dev
))
250 enable_irq_wake(priv
->irq
);
255 static int eeti_ts_resume(struct i2c_client
*client
)
257 struct eeti_ts_priv
*priv
= i2c_get_clientdata(client
);
259 if (device_may_wakeup(&client
->dev
))
260 disable_irq_wake(priv
->irq
);
265 #define eeti_ts_suspend NULL
266 #define eeti_ts_resume NULL
269 static const struct i2c_device_id eeti_ts_id
[] = {
273 MODULE_DEVICE_TABLE(i2c
, eeti_ts_id
);
275 static struct i2c_driver eeti_ts_driver
= {
279 .probe
= eeti_ts_probe
,
280 .remove
= __devexit_p(eeti_ts_remove
),
281 .suspend
= eeti_ts_suspend
,
282 .resume
= eeti_ts_resume
,
283 .id_table
= eeti_ts_id
,
286 static int __init
eeti_ts_init(void)
288 return i2c_add_driver(&eeti_ts_driver
);
291 static void __exit
eeti_ts_exit(void)
293 i2c_del_driver(&eeti_ts_driver
);
296 MODULE_DESCRIPTION("EETI Touchscreen driver");
297 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
298 MODULE_LICENSE("GPL");
300 module_init(eeti_ts_init
);
301 module_exit(eeti_ts_exit
);