Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Touch Screen driver for EETI's I2C connected touch screen panels
4 * Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org>
5 *
6 * See EETI's software guide for the protocol specification:
7 * http://home.eeti.com.tw/documentation.html
8 *
9 * Based on migor_ts.c
10 * Copyright (c) 2008 Magnus Damm
11 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/input.h>
17#include <linux/input/touchscreen.h>
18#include <linux/interrupt.h>
19#include <linux/i2c.h>
20#include <linux/timer.h>
21#include <linux/gpio/consumer.h>
22#include <linux/of.h>
23#include <linux/slab.h>
24#include <linux/unaligned.h>
25
26struct eeti_ts {
27 struct i2c_client *client;
28 struct input_dev *input;
29 struct gpio_desc *attn_gpio;
30 struct touchscreen_properties props;
31 struct mutex mutex;
32 bool running;
33};
34
35#define EETI_TS_BITDEPTH (11)
36#define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
37
38#define REPORT_BIT_PRESSED BIT(0)
39#define REPORT_BIT_AD0 BIT(1)
40#define REPORT_BIT_AD1 BIT(2)
41#define REPORT_BIT_HAS_PRESSURE BIT(6)
42#define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
43
44static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
45{
46 unsigned int res;
47 u16 x, y;
48
49 res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
50
51 x = get_unaligned_be16(&buf[1]);
52 y = get_unaligned_be16(&buf[3]);
53
54 /* fix the range to 11 bits */
55 x >>= res - EETI_TS_BITDEPTH;
56 y >>= res - EETI_TS_BITDEPTH;
57
58 if (buf[0] & REPORT_BIT_HAS_PRESSURE)
59 input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
60
61 touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
62 input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
63 input_sync(eeti->input);
64}
65
66static int eeti_ts_read(struct eeti_ts *eeti)
67{
68 int len, error;
69 char buf[6];
70
71 len = i2c_master_recv(eeti->client, buf, sizeof(buf));
72 if (len != sizeof(buf)) {
73 error = len < 0 ? len : -EIO;
74 dev_err(&eeti->client->dev,
75 "failed to read touchscreen data: %d\n",
76 error);
77 return error;
78 }
79
80 /* Motion packet */
81 if (buf[0] & 0x80)
82 eeti_ts_report_event(eeti, buf);
83
84 return 0;
85}
86
87static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
88{
89 struct eeti_ts *eeti = dev_id;
90 int error;
91
92 guard(mutex)(&eeti->mutex);
93
94 do {
95 /*
96 * If we have attention GPIO, trust it. Otherwise we'll read
97 * once and exit. We assume that in this case we are using
98 * level triggered interrupt and it will get raised again
99 * if/when there is more data.
100 */
101 if (eeti->attn_gpio &&
102 !gpiod_get_value_cansleep(eeti->attn_gpio)) {
103 break;
104 }
105
106 error = eeti_ts_read(eeti);
107 if (error)
108 break;
109
110 } while (eeti->running && eeti->attn_gpio);
111
112 return IRQ_HANDLED;
113}
114
115static void eeti_ts_start(struct eeti_ts *eeti)
116{
117 guard(mutex)(&eeti->mutex);
118
119 eeti->running = true;
120 enable_irq(eeti->client->irq);
121
122 /*
123 * Kick the controller in case we are using edge interrupt and
124 * we missed our edge while interrupt was disabled. We expect
125 * the attention GPIO to be wired in this case.
126 */
127 if (eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio))
128 eeti_ts_read(eeti);
129}
130
131static void eeti_ts_stop(struct eeti_ts *eeti)
132{
133 /*
134 * Not locking here, just setting a flag and expect that the
135 * interrupt thread will notice the flag eventually.
136 */
137 eeti->running = false;
138 wmb();
139 disable_irq(eeti->client->irq);
140}
141
142static int eeti_ts_open(struct input_dev *dev)
143{
144 struct eeti_ts *eeti = input_get_drvdata(dev);
145
146 eeti_ts_start(eeti);
147
148 return 0;
149}
150
151static void eeti_ts_close(struct input_dev *dev)
152{
153 struct eeti_ts *eeti = input_get_drvdata(dev);
154
155 eeti_ts_stop(eeti);
156}
157
158static int eeti_ts_probe(struct i2c_client *client)
159{
160 struct device *dev = &client->dev;
161 struct eeti_ts *eeti;
162 struct input_dev *input;
163 int error;
164
165 /*
166 * In contrast to what's described in the datasheet, there seems
167 * to be no way of probing the presence of that device using I2C
168 * commands. So we need to blindly believe it is there, and wait
169 * for interrupts to occur.
170 */
171
172 eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
173 if (!eeti) {
174 dev_err(dev, "failed to allocate driver data\n");
175 return -ENOMEM;
176 }
177
178 mutex_init(&eeti->mutex);
179
180 input = devm_input_allocate_device(dev);
181 if (!input) {
182 dev_err(dev, "Failed to allocate input device.\n");
183 return -ENOMEM;
184 }
185
186 input_set_capability(input, EV_KEY, BTN_TOUCH);
187
188 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
189 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
190 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
191
192 touchscreen_parse_properties(input, false, &eeti->props);
193
194 input->name = client->name;
195 input->id.bustype = BUS_I2C;
196 input->open = eeti_ts_open;
197 input->close = eeti_ts_close;
198
199 eeti->client = client;
200 eeti->input = input;
201
202 eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
203 if (IS_ERR(eeti->attn_gpio))
204 return PTR_ERR(eeti->attn_gpio);
205
206 i2c_set_clientdata(client, eeti);
207 input_set_drvdata(input, eeti);
208
209 error = devm_request_threaded_irq(dev, client->irq,
210 NULL, eeti_ts_isr,
211 IRQF_ONESHOT,
212 client->name, eeti);
213 if (error) {
214 dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
215 error);
216 return error;
217 }
218
219 /*
220 * Disable the device for now. It will be enabled once the
221 * input device is opened.
222 */
223 eeti_ts_stop(eeti);
224
225 error = input_register_device(input);
226 if (error)
227 return error;
228
229 return 0;
230}
231
232static int eeti_ts_suspend(struct device *dev)
233{
234 struct i2c_client *client = to_i2c_client(dev);
235 struct eeti_ts *eeti = i2c_get_clientdata(client);
236 struct input_dev *input_dev = eeti->input;
237
238 scoped_guard(mutex, &input_dev->mutex) {
239 if (input_device_enabled(input_dev))
240 eeti_ts_stop(eeti);
241 }
242
243 if (device_may_wakeup(&client->dev))
244 enable_irq_wake(client->irq);
245
246 return 0;
247}
248
249static int eeti_ts_resume(struct device *dev)
250{
251 struct i2c_client *client = to_i2c_client(dev);
252 struct eeti_ts *eeti = i2c_get_clientdata(client);
253 struct input_dev *input_dev = eeti->input;
254
255 if (device_may_wakeup(&client->dev))
256 disable_irq_wake(client->irq);
257
258 scoped_guard(mutex, &input_dev->mutex) {
259 if (input_device_enabled(input_dev))
260 eeti_ts_start(eeti);
261 }
262
263 return 0;
264}
265
266static DEFINE_SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
267
268static const struct i2c_device_id eeti_ts_id[] = {
269 { "eeti_ts" },
270 { }
271};
272MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
273
274#ifdef CONFIG_OF
275static const struct of_device_id of_eeti_ts_match[] = {
276 { .compatible = "eeti,exc3000-i2c", },
277 { }
278};
279#endif
280
281static struct i2c_driver eeti_ts_driver = {
282 .driver = {
283 .name = "eeti_ts",
284 .pm = pm_sleep_ptr(&eeti_ts_pm),
285 .of_match_table = of_match_ptr(of_eeti_ts_match),
286 },
287 .probe = eeti_ts_probe,
288 .id_table = eeti_ts_id,
289};
290
291module_i2c_driver(eeti_ts_driver);
292
293MODULE_DESCRIPTION("EETI Touchscreen driver");
294MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
295MODULE_LICENSE("GPL");