]>
Commit | Line | Data |
---|---|---|
10494dce DM |
1 | /* |
2 | * Touch Screen driver for EETI's I2C connected touch screen panels | |
fd8135b6 | 3 | * Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org> |
10494dce DM |
4 | * |
5 | * See EETI's software guide for the protocol specification: | |
fd8135b6 | 6 | * http://home.eeti.com.tw/documentation.html |
10494dce DM |
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> | |
10494dce DM |
28 | #include <linux/kernel.h> |
29 | #include <linux/input.h> | |
a114cbd0 | 30 | #include <linux/input/touchscreen.h> |
10494dce DM |
31 | #include <linux/interrupt.h> |
32 | #include <linux/i2c.h> | |
33 | #include <linux/timer.h> | |
d99caa47 | 34 | #include <linux/gpio/consumer.h> |
e32d7f1b | 35 | #include <linux/of.h> |
5a0e3ad6 | 36 | #include <linux/slab.h> |
e9f66cdb | 37 | #include <asm/unaligned.h> |
10494dce | 38 | |
720bebdf | 39 | struct eeti_ts { |
10494dce DM |
40 | struct i2c_client *client; |
41 | struct input_dev *input; | |
d99caa47 | 42 | struct gpio_desc *attn_gpio; |
a114cbd0 | 43 | struct touchscreen_properties props; |
0378008a | 44 | bool running; |
10494dce DM |
45 | }; |
46 | ||
47 | #define EETI_TS_BITDEPTH (11) | |
48 | #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1) | |
49 | ||
272c03bb DT |
50 | #define REPORT_BIT_PRESSED BIT(0) |
51 | #define REPORT_BIT_AD0 BIT(1) | |
52 | #define REPORT_BIT_AD1 BIT(2) | |
53 | #define REPORT_BIT_HAS_PRESSURE BIT(6) | |
10494dce DM |
54 | #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH) |
55 | ||
0378008a | 56 | static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf) |
10494dce | 57 | { |
0378008a DT |
58 | unsigned int res; |
59 | u16 x, y; | |
10494dce | 60 | |
10494dce | 61 | res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1)); |
e9f66cdb DT |
62 | |
63 | x = get_unaligned_be16(&buf[1]); | |
64 | y = get_unaligned_be16(&buf[3]); | |
10494dce DM |
65 | |
66 | /* fix the range to 11 bits */ | |
67 | x >>= res - EETI_TS_BITDEPTH; | |
68 | y >>= res - EETI_TS_BITDEPTH; | |
69 | ||
10494dce | 70 | if (buf[0] & REPORT_BIT_HAS_PRESSURE) |
720bebdf | 71 | input_report_abs(eeti->input, ABS_PRESSURE, buf[5]); |
10494dce | 72 | |
a114cbd0 | 73 | touchscreen_report_pos(eeti->input, &eeti->props, x, y, false); |
0378008a | 74 | input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED); |
720bebdf | 75 | input_sync(eeti->input); |
10494dce DM |
76 | } |
77 | ||
78 | static irqreturn_t eeti_ts_isr(int irq, void *dev_id) | |
79 | { | |
720bebdf | 80 | struct eeti_ts *eeti = dev_id; |
0378008a DT |
81 | int len; |
82 | int error; | |
83 | char buf[6]; | |
10494dce | 84 | |
0378008a DT |
85 | do { |
86 | len = i2c_master_recv(eeti->client, buf, sizeof(buf)); | |
87 | if (len != sizeof(buf)) { | |
88 | error = len < 0 ? len : -EIO; | |
89 | dev_err(&eeti->client->dev, | |
90 | "failed to read touchscreen data: %d\n", | |
91 | error); | |
92 | break; | |
93 | } | |
94 | ||
95 | if (buf[0] & 0x80) { | |
96 | /* Motion packet */ | |
97 | eeti_ts_report_event(eeti, buf); | |
98 | } | |
d99caa47 DT |
99 | } while (eeti->running && |
100 | eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio)); | |
10494dce DM |
101 | |
102 | return IRQ_HANDLED; | |
103 | } | |
104 | ||
720bebdf | 105 | static void eeti_ts_start(struct eeti_ts *eeti) |
10494dce | 106 | { |
0378008a DT |
107 | eeti->running = true; |
108 | wmb(); | |
2d388499 | 109 | enable_irq(eeti->client->irq); |
7fbef0d1 DM |
110 | } |
111 | ||
720bebdf | 112 | static void eeti_ts_stop(struct eeti_ts *eeti) |
7fbef0d1 | 113 | { |
0378008a DT |
114 | eeti->running = false; |
115 | wmb(); | |
2d388499 | 116 | disable_irq(eeti->client->irq); |
7fbef0d1 DM |
117 | } |
118 | ||
119 | static int eeti_ts_open(struct input_dev *dev) | |
120 | { | |
720bebdf | 121 | struct eeti_ts *eeti = input_get_drvdata(dev); |
7fbef0d1 | 122 | |
720bebdf | 123 | eeti_ts_start(eeti); |
10494dce DM |
124 | |
125 | return 0; | |
126 | } | |
127 | ||
128 | static void eeti_ts_close(struct input_dev *dev) | |
129 | { | |
720bebdf | 130 | struct eeti_ts *eeti = input_get_drvdata(dev); |
10494dce | 131 | |
720bebdf | 132 | eeti_ts_stop(eeti); |
10494dce DM |
133 | } |
134 | ||
5298cc4c | 135 | static int eeti_ts_probe(struct i2c_client *client, |
6f9fab69 | 136 | const struct i2c_device_id *idp) |
10494dce | 137 | { |
6f9fab69 | 138 | struct device *dev = &client->dev; |
720bebdf | 139 | struct eeti_ts *eeti; |
10494dce | 140 | struct input_dev *input; |
6f9fab69 | 141 | int error; |
10494dce | 142 | |
7fbef0d1 DM |
143 | /* |
144 | * In contrast to what's described in the datasheet, there seems | |
10494dce DM |
145 | * to be no way of probing the presence of that device using I2C |
146 | * commands. So we need to blindly believe it is there, and wait | |
7fbef0d1 DM |
147 | * for interrupts to occur. |
148 | */ | |
10494dce | 149 | |
6f9fab69 | 150 | eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL); |
720bebdf | 151 | if (!eeti) { |
6f9fab69 | 152 | dev_err(dev, "failed to allocate driver data\n"); |
ad03ae44 | 153 | return -ENOMEM; |
10494dce DM |
154 | } |
155 | ||
6f9fab69 | 156 | input = devm_input_allocate_device(dev); |
10494dce | 157 | if (!input) { |
6f9fab69 DT |
158 | dev_err(dev, "Failed to allocate input device.\n"); |
159 | return -ENOMEM; | |
10494dce DM |
160 | } |
161 | ||
42e02a6a | 162 | input_set_capability(input, EV_KEY, BTN_TOUCH); |
10494dce DM |
163 | |
164 | input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0); | |
165 | input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0); | |
166 | input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0); | |
167 | ||
a114cbd0 DM |
168 | touchscreen_parse_properties(input, false, &eeti->props); |
169 | ||
10494dce DM |
170 | input->name = client->name; |
171 | input->id.bustype = BUS_I2C; | |
10494dce DM |
172 | input->open = eeti_ts_open; |
173 | input->close = eeti_ts_close; | |
174 | ||
720bebdf DT |
175 | eeti->client = client; |
176 | eeti->input = input; | |
25a70e38 | 177 | |
d99caa47 DT |
178 | eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN); |
179 | if (IS_ERR(eeti->attn_gpio)) | |
180 | return PTR_ERR(eeti->attn_gpio); | |
25a70e38 | 181 | |
720bebdf DT |
182 | i2c_set_clientdata(client, eeti); |
183 | input_set_drvdata(input, eeti); | |
10494dce | 184 | |
0378008a DT |
185 | error = devm_request_threaded_irq(dev, client->irq, |
186 | NULL, eeti_ts_isr, | |
d422be5f | 187 | IRQF_ONESHOT, |
0378008a | 188 | client->name, eeti); |
6f9fab69 DT |
189 | if (error) { |
190 | dev_err(dev, "Unable to request touchscreen IRQ: %d\n", | |
191 | error); | |
192 | return error; | |
10494dce DM |
193 | } |
194 | ||
7fbef0d1 DM |
195 | /* |
196 | * Disable the device for now. It will be enabled once the | |
197 | * input device is opened. | |
198 | */ | |
720bebdf | 199 | eeti_ts_stop(eeti); |
10494dce | 200 | |
0378008a DT |
201 | error = input_register_device(input); |
202 | if (error) | |
203 | return error; | |
204 | ||
10494dce DM |
205 | return 0; |
206 | } | |
207 | ||
02b6a58b | 208 | static int __maybe_unused eeti_ts_suspend(struct device *dev) |
10494dce | 209 | { |
85012fff | 210 | struct i2c_client *client = to_i2c_client(dev); |
720bebdf DT |
211 | struct eeti_ts *eeti = i2c_get_clientdata(client); |
212 | struct input_dev *input_dev = eeti->input; | |
7fbef0d1 DM |
213 | |
214 | mutex_lock(&input_dev->mutex); | |
215 | ||
216 | if (input_dev->users) | |
720bebdf | 217 | eeti_ts_stop(eeti); |
7fbef0d1 DM |
218 | |
219 | mutex_unlock(&input_dev->mutex); | |
10494dce DM |
220 | |
221 | if (device_may_wakeup(&client->dev)) | |
2d388499 | 222 | enable_irq_wake(client->irq); |
10494dce DM |
223 | |
224 | return 0; | |
225 | } | |
226 | ||
02b6a58b | 227 | static int __maybe_unused eeti_ts_resume(struct device *dev) |
10494dce | 228 | { |
85012fff | 229 | struct i2c_client *client = to_i2c_client(dev); |
720bebdf DT |
230 | struct eeti_ts *eeti = i2c_get_clientdata(client); |
231 | struct input_dev *input_dev = eeti->input; | |
10494dce DM |
232 | |
233 | if (device_may_wakeup(&client->dev)) | |
2d388499 | 234 | disable_irq_wake(client->irq); |
10494dce | 235 | |
7fbef0d1 DM |
236 | mutex_lock(&input_dev->mutex); |
237 | ||
238 | if (input_dev->users) | |
720bebdf | 239 | eeti_ts_start(eeti); |
7fbef0d1 DM |
240 | |
241 | mutex_unlock(&input_dev->mutex); | |
242 | ||
10494dce DM |
243 | return 0; |
244 | } | |
85012fff MB |
245 | |
246 | static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume); | |
10494dce DM |
247 | |
248 | static const struct i2c_device_id eeti_ts_id[] = { | |
249 | { "eeti_ts", 0 }, | |
250 | { } | |
251 | }; | |
252 | MODULE_DEVICE_TABLE(i2c, eeti_ts_id); | |
253 | ||
e32d7f1b DM |
254 | #ifdef CONFIG_OF |
255 | static const struct of_device_id of_eeti_ts_match[] = { | |
256 | { .compatible = "eeti,exc3000-i2c", }, | |
257 | { } | |
258 | }; | |
259 | #endif | |
260 | ||
10494dce DM |
261 | static struct i2c_driver eeti_ts_driver = { |
262 | .driver = { | |
263 | .name = "eeti_ts", | |
85012fff | 264 | .pm = &eeti_ts_pm, |
e32d7f1b | 265 | .of_match_table = of_match_ptr(of_eeti_ts_match), |
10494dce DM |
266 | }, |
267 | .probe = eeti_ts_probe, | |
10494dce DM |
268 | .id_table = eeti_ts_id, |
269 | }; | |
270 | ||
1b92c1cf | 271 | module_i2c_driver(eeti_ts_driver); |
10494dce DM |
272 | |
273 | MODULE_DESCRIPTION("EETI Touchscreen driver"); | |
fd8135b6 | 274 | MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>"); |
10494dce | 275 | MODULE_LICENSE("GPL"); |