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/consumer.h>
35 #include <linux/slab.h>
36 #include <asm/unaligned.h>
39 module_param(flip_x
, bool, 0644);
40 MODULE_PARM_DESC(flip_x
, "flip x coordinate");
43 module_param(flip_y
, bool, 0644);
44 MODULE_PARM_DESC(flip_y
, "flip y coordinate");
47 struct i2c_client
*client
;
48 struct input_dev
*input
;
49 struct gpio_desc
*attn_gpio
;
53 #define EETI_TS_BITDEPTH (11)
54 #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
56 #define REPORT_BIT_PRESSED BIT(0)
57 #define REPORT_BIT_AD0 BIT(1)
58 #define REPORT_BIT_AD1 BIT(2)
59 #define REPORT_BIT_HAS_PRESSURE BIT(6)
60 #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
62 static void eeti_ts_report_event(struct eeti_ts
*eeti
, u8
*buf
)
67 res
= REPORT_RES_BITS(buf
[0] & (REPORT_BIT_AD0
| REPORT_BIT_AD1
));
69 x
= get_unaligned_be16(&buf
[1]);
70 y
= get_unaligned_be16(&buf
[3]);
72 /* fix the range to 11 bits */
73 x
>>= res
- EETI_TS_BITDEPTH
;
74 y
>>= res
- EETI_TS_BITDEPTH
;
82 if (buf
[0] & REPORT_BIT_HAS_PRESSURE
)
83 input_report_abs(eeti
->input
, ABS_PRESSURE
, buf
[5]);
85 input_report_abs(eeti
->input
, ABS_X
, x
);
86 input_report_abs(eeti
->input
, ABS_Y
, y
);
87 input_report_key(eeti
->input
, BTN_TOUCH
, buf
[0] & REPORT_BIT_PRESSED
);
88 input_sync(eeti
->input
);
91 static irqreturn_t
eeti_ts_isr(int irq
, void *dev_id
)
93 struct eeti_ts
*eeti
= dev_id
;
99 len
= i2c_master_recv(eeti
->client
, buf
, sizeof(buf
));
100 if (len
!= sizeof(buf
)) {
101 error
= len
< 0 ? len
: -EIO
;
102 dev_err(&eeti
->client
->dev
,
103 "failed to read touchscreen data: %d\n",
110 eeti_ts_report_event(eeti
, buf
);
112 } while (eeti
->running
&&
113 eeti
->attn_gpio
&& gpiod_get_value_cansleep(eeti
->attn_gpio
));
118 static void eeti_ts_start(struct eeti_ts
*eeti
)
120 eeti
->running
= true;
122 enable_irq(eeti
->client
->irq
);
125 static void eeti_ts_stop(struct eeti_ts
*eeti
)
127 eeti
->running
= false;
129 disable_irq(eeti
->client
->irq
);
132 static int eeti_ts_open(struct input_dev
*dev
)
134 struct eeti_ts
*eeti
= input_get_drvdata(dev
);
141 static void eeti_ts_close(struct input_dev
*dev
)
143 struct eeti_ts
*eeti
= input_get_drvdata(dev
);
148 static int eeti_ts_probe(struct i2c_client
*client
,
149 const struct i2c_device_id
*idp
)
151 struct device
*dev
= &client
->dev
;
152 struct eeti_ts
*eeti
;
153 struct input_dev
*input
;
157 * In contrast to what's described in the datasheet, there seems
158 * to be no way of probing the presence of that device using I2C
159 * commands. So we need to blindly believe it is there, and wait
160 * for interrupts to occur.
163 eeti
= devm_kzalloc(dev
, sizeof(*eeti
), GFP_KERNEL
);
165 dev_err(dev
, "failed to allocate driver data\n");
169 input
= devm_input_allocate_device(dev
);
171 dev_err(dev
, "Failed to allocate input device.\n");
175 input_set_capability(input
, EV_KEY
, 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
->open
= eeti_ts_open
;
184 input
->close
= eeti_ts_close
;
186 eeti
->client
= client
;
189 eeti
->attn_gpio
= devm_gpiod_get_optional(dev
, "attn", GPIOD_IN
);
190 if (IS_ERR(eeti
->attn_gpio
))
191 return PTR_ERR(eeti
->attn_gpio
);
193 i2c_set_clientdata(client
, eeti
);
194 input_set_drvdata(input
, eeti
);
196 error
= devm_request_threaded_irq(dev
, client
->irq
,
201 dev_err(dev
, "Unable to request touchscreen IRQ: %d\n",
207 * Disable the device for now. It will be enabled once the
208 * input device is opened.
212 error
= input_register_device(input
);
219 static int __maybe_unused
eeti_ts_suspend(struct device
*dev
)
221 struct i2c_client
*client
= to_i2c_client(dev
);
222 struct eeti_ts
*eeti
= i2c_get_clientdata(client
);
223 struct input_dev
*input_dev
= eeti
->input
;
225 mutex_lock(&input_dev
->mutex
);
227 if (input_dev
->users
)
230 mutex_unlock(&input_dev
->mutex
);
232 if (device_may_wakeup(&client
->dev
))
233 enable_irq_wake(client
->irq
);
238 static int __maybe_unused
eeti_ts_resume(struct device
*dev
)
240 struct i2c_client
*client
= to_i2c_client(dev
);
241 struct eeti_ts
*eeti
= i2c_get_clientdata(client
);
242 struct input_dev
*input_dev
= eeti
->input
;
244 if (device_may_wakeup(&client
->dev
))
245 disable_irq_wake(client
->irq
);
247 mutex_lock(&input_dev
->mutex
);
249 if (input_dev
->users
)
252 mutex_unlock(&input_dev
->mutex
);
257 static SIMPLE_DEV_PM_OPS(eeti_ts_pm
, eeti_ts_suspend
, eeti_ts_resume
);
259 static const struct i2c_device_id eeti_ts_id
[] = {
263 MODULE_DEVICE_TABLE(i2c
, eeti_ts_id
);
265 static struct i2c_driver eeti_ts_driver
= {
270 .probe
= eeti_ts_probe
,
271 .id_table
= eeti_ts_id
,
274 module_i2c_driver(eeti_ts_driver
);
276 MODULE_DESCRIPTION("EETI Touchscreen driver");
277 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
278 MODULE_LICENSE("GPL");