033039e5cb96f94231b95767027c2c466f6b458e
[oweals/openwrt.git] /
1 From 99aaee7d1fde86bd2119d84e90435d89d6d3af07 Mon Sep 17 00:00:00 2001
2 From: Ahmet Inan <inan@distec.de>
3 Date: Sat, 14 Oct 2017 10:10:53 -0700
4 Subject: [PATCH 162/454] Input: add I2C attached EETI EXC3000 multi touch
5  driver
6
7 commit 7e577a17f2eefeef32f1106ebf91e7cd143ba654 upstream.
8 beware: code adapted to use the old timer API.
9
10 The 3000 series have a new protocol which allows to report up to 5 points
11 in a single 66 byte frame. One must always read in 66 byte frames.
12 To support up to 10 points, two consecutive frames need to be read:
13 The first frame says how many points until sync.
14 The second frame must say zero points or both frames must be discarded.
15
16 To be able to work with the higher 400KHz I2C bus rate, one must
17 successfully send a special package prior _each_ read or the controller
18 will refuse to cooperate.
19
20 This is a minimal implementation based on egalax_i2c.c (which can be found
21 on the internet) and egalax_ts.c but without the vendor interface and no
22 power management support.
23
24 Signed-off-by: Ahmet Inan <inan@distec.de>
25 Acked-by: Rob Herring <robh@kernel.org>
26 Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
27 ---
28  .../bindings/input/touchscreen/exc3000.txt    |  27 +++
29  drivers/input/touchscreen/Kconfig             |  10 +
30  drivers/input/touchscreen/Makefile            |   1 +
31  drivers/input/touchscreen/exc3000.c           | 223 ++++++++++++++++++
32  4 files changed, 261 insertions(+)
33  create mode 100644 Documentation/devicetree/bindings/input/touchscreen/exc3000.txt
34  create mode 100644 drivers/input/touchscreen/exc3000.c
35
36 --- /dev/null
37 +++ b/Documentation/devicetree/bindings/input/touchscreen/exc3000.txt
38 @@ -0,0 +1,27 @@
39 +* EETI EXC3000 Multiple Touch Controller
40 +
41 +Required properties:
42 +- compatible: must be "eeti,exc3000"
43 +- reg: i2c slave address
44 +- interrupt-parent: the phandle for the interrupt controller
45 +- interrupts: touch controller interrupt
46 +- touchscreen-size-x: See touchscreen.txt
47 +- touchscreen-size-y: See touchscreen.txt
48 +
49 +Optional properties:
50 +- touchscreen-inverted-x: See touchscreen.txt
51 +- touchscreen-inverted-y: See touchscreen.txt
52 +- touchscreen-swapped-x-y: See touchscreen.txt
53 +
54 +Example:
55 +
56 +       touchscreen@2a {
57 +               compatible = "eeti,exc3000";
58 +               reg = <0x2a>;
59 +               interrupt-parent = <&gpio1>;
60 +               interrupts = <9 IRQ_TYPE_LEVEL_LOW>;
61 +               touchscreen-size-x = <4096>;
62 +               touchscreen-size-y = <4096>;
63 +               touchscreen-inverted-x;
64 +               touchscreen-swapped-x-y;
65 +       };
66 --- a/drivers/input/touchscreen/Kconfig
67 +++ b/drivers/input/touchscreen/Kconfig
68 @@ -316,6 +316,16 @@ config TOUCHSCREEN_EGALAX_SERIAL
69           To compile this driver as a module, choose M here: the
70           module will be called egalax_ts_serial.
71  
72 +config TOUCHSCREEN_EXC3000
73 +       tristate "EETI EXC3000 multi-touch panel support"
74 +       depends on I2C
75 +       help
76 +         Say Y here to enable support for I2C connected EETI
77 +         EXC3000 multi-touch panels.
78 +
79 +         To compile this driver as a module, choose M here: the
80 +         module will be called exc3000.
81 +
82  config TOUCHSCREEN_FUJITSU
83         tristate "Fujitsu serial touchscreen"
84         select SERIO
85 --- a/drivers/input/touchscreen/Makefile
86 +++ b/drivers/input/touchscreen/Makefile
87 @@ -39,6 +39,7 @@ obj-$(CONFIG_TOUCHSCREEN_ELAN)                += elant
88  obj-$(CONFIG_TOUCHSCREEN_ELO)          += elo.o
89  obj-$(CONFIG_TOUCHSCREEN_EGALAX)       += egalax_ts.o
90  obj-$(CONFIG_TOUCHSCREEN_EGALAX_SERIAL)        += egalax_ts_serial.o
91 +obj-$(CONFIG_TOUCHSCREEN_EXC3000)      += exc3000.o
92  obj-$(CONFIG_TOUCHSCREEN_FUJITSU)      += fujitsu_ts.o
93  obj-$(CONFIG_TOUCHSCREEN_GOODIX)       += goodix.o
94  obj-$(CONFIG_TOUCHSCREEN_ILI210X)      += ili210x.o
95 --- /dev/null
96 +++ b/drivers/input/touchscreen/exc3000.c
97 @@ -0,0 +1,223 @@
98 +/*
99 + * Driver for I2C connected EETI EXC3000 multiple touch controller
100 + *
101 + * Copyright (C) 2017 Ahmet Inan <inan@distec.de>
102 + *
103 + * minimal implementation based on egalax_ts.c and egalax_i2c.c
104 + *
105 + * This program is free software; you can redistribute it and/or modify
106 + * it under the terms of the GNU General Public License version 2 as
107 + * published by the Free Software Foundation.
108 + */
109 +
110 +#include <linux/bitops.h>
111 +#include <linux/device.h>
112 +#include <linux/i2c.h>
113 +#include <linux/input.h>
114 +#include <linux/input/mt.h>
115 +#include <linux/input/touchscreen.h>
116 +#include <linux/interrupt.h>
117 +#include <linux/module.h>
118 +#include <linux/of.h>
119 +#include <linux/timer.h>
120 +#include <asm/unaligned.h>
121 +
122 +#define EXC3000_NUM_SLOTS              10
123 +#define EXC3000_SLOTS_PER_FRAME                5
124 +#define EXC3000_LEN_FRAME              66
125 +#define EXC3000_LEN_POINT              10
126 +#define EXC3000_MT_EVENT               6
127 +#define EXC3000_TIMEOUT_MS             100
128 +
129 +struct exc3000_data {
130 +       struct i2c_client *client;
131 +       struct input_dev *input;
132 +       struct touchscreen_properties prop;
133 +       struct timer_list timer;
134 +       u8 buf[2 * EXC3000_LEN_FRAME];
135 +};
136 +
137 +static void exc3000_report_slots(struct input_dev *input,
138 +                                struct touchscreen_properties *prop,
139 +                                const u8 *buf, int num)
140 +{
141 +       for (; num--; buf += EXC3000_LEN_POINT) {
142 +               if (buf[0] & BIT(0)) {
143 +                       input_mt_slot(input, buf[1]);
144 +                       input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
145 +                       touchscreen_report_pos(input, prop,
146 +                                              get_unaligned_le16(buf + 2),
147 +                                              get_unaligned_le16(buf + 4),
148 +                                              true);
149 +               }
150 +       }
151 +}
152 +
153 +static void exc3000_timer(unsigned long d)
154 +{
155 +       struct exc3000_data *data = (void *)d;
156 +
157 +       input_mt_sync_frame(data->input);
158 +       input_sync(data->input);
159 +}
160 +
161 +static int exc3000_read_frame(struct i2c_client *client, u8 *buf)
162 +{
163 +       int ret;
164 +
165 +       ret = i2c_master_send(client, "'", 2);
166 +       if (ret < 0)
167 +               return ret;
168 +
169 +       if (ret != 2)
170 +               return -EIO;
171 +
172 +       ret = i2c_master_recv(client, buf, EXC3000_LEN_FRAME);
173 +       if (ret < 0)
174 +               return ret;
175 +
176 +       if (ret != EXC3000_LEN_FRAME)
177 +               return -EIO;
178 +
179 +       if (get_unaligned_le16(buf) != EXC3000_LEN_FRAME ||
180 +                       buf[2] != EXC3000_MT_EVENT)
181 +               return -EINVAL;
182 +
183 +       return 0;
184 +}
185 +
186 +static int exc3000_read_data(struct i2c_client *client,
187 +                            u8 *buf, int *n_slots)
188 +{
189 +       int error;
190 +
191 +       error = exc3000_read_frame(client, buf);
192 +       if (error)
193 +               return error;
194 +
195 +       *n_slots = buf[3];
196 +       if (!*n_slots || *n_slots > EXC3000_NUM_SLOTS)
197 +               return -EINVAL;
198 +
199 +       if (*n_slots > EXC3000_SLOTS_PER_FRAME) {
200 +               /* Read 2nd frame to get the rest of the contacts. */
201 +               error = exc3000_read_frame(client, buf + EXC3000_LEN_FRAME);
202 +               if (error)
203 +                       return error;
204 +
205 +               /* 2nd chunk must have number of contacts set to 0. */
206 +               if (buf[EXC3000_LEN_FRAME + 3] != 0)
207 +                       return -EINVAL;
208 +       }
209 +
210 +       return 0;
211 +}
212 +
213 +static irqreturn_t exc3000_interrupt(int irq, void *dev_id)
214 +{
215 +       struct exc3000_data *data = dev_id;
216 +       struct input_dev *input = data->input;
217 +       u8 *buf = data->buf;
218 +       int slots, total_slots;
219 +       int error;
220 +
221 +       error = exc3000_read_data(data->client, buf, &total_slots);
222 +       if (error) {
223 +               /* Schedule a timer to release "stuck" contacts */
224 +               mod_timer(&data->timer,
225 +                         jiffies + msecs_to_jiffies(EXC3000_TIMEOUT_MS));
226 +               goto out;
227 +       }
228 +
229 +       /*
230 +        * We read full state successfully, no contacts will be "stuck".
231 +        */
232 +       del_timer_sync(&data->timer);
233 +
234 +       while (total_slots > 0) {
235 +               slots = min(total_slots, EXC3000_SLOTS_PER_FRAME);
236 +               exc3000_report_slots(input, &data->prop, buf + 4, slots);
237 +               total_slots -= slots;
238 +               buf += EXC3000_LEN_FRAME;
239 +       }
240 +
241 +       input_mt_sync_frame(input);
242 +       input_sync(input);
243 +
244 +out:
245 +       return IRQ_HANDLED;
246 +}
247 +
248 +static int exc3000_probe(struct i2c_client *client,
249 +                        const struct i2c_device_id *id)
250 +{
251 +       struct exc3000_data *data;
252 +       struct input_dev *input;
253 +       int error;
254 +
255 +       data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
256 +       if (!data)
257 +               return -ENOMEM;
258 +
259 +       data->client = client;
260 +       setup_timer(&data->timer, exc3000_timer, (unsigned long)data);
261 +
262 +       input = devm_input_allocate_device(&client->dev);
263 +       if (!input)
264 +               return -ENOMEM;
265 +
266 +       data->input = input;
267 +
268 +       input->name = "EETI EXC3000 Touch Screen";
269 +       input->id.bustype = BUS_I2C;
270 +
271 +       input_set_abs_params(input, ABS_MT_POSITION_X, 0, 4095, 0, 0);
272 +       input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 4095, 0, 0);
273 +       touchscreen_parse_properties(input, true, &data->prop);
274 +
275 +       error = input_mt_init_slots(input, EXC3000_NUM_SLOTS,
276 +                                   INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
277 +       if (error)
278 +               return error;
279 +
280 +       error = input_register_device(input);
281 +       if (error)
282 +               return error;
283 +
284 +       error = devm_request_threaded_irq(&client->dev, client->irq,
285 +                                         NULL, exc3000_interrupt, IRQF_ONESHOT,
286 +                                         client->name, data);
287 +       if (error)
288 +               return error;
289 +
290 +       return 0;
291 +}
292 +
293 +static const struct i2c_device_id exc3000_id[] = {
294 +       { "exc3000", 0 },
295 +       { }
296 +};
297 +MODULE_DEVICE_TABLE(i2c, exc3000_id);
298 +
299 +#ifdef CONFIG_OF
300 +static const struct of_device_id exc3000_of_match[] = {
301 +       { .compatible = "eeti,exc3000" },
302 +       { }
303 +};
304 +MODULE_DEVICE_TABLE(of, exc3000_of_match);
305 +#endif
306 +
307 +static struct i2c_driver exc3000_driver = {
308 +       .driver = {
309 +               .name   = "exc3000",
310 +               .of_match_table = of_match_ptr(exc3000_of_match),
311 +       },
312 +       .id_table       = exc3000_id,
313 +       .probe          = exc3000_probe,
314 +};
315 +
316 +module_i2c_driver(exc3000_driver);
317 +
318 +MODULE_AUTHOR("Ahmet Inan <inan@distec.de>");
319 +MODULE_DESCRIPTION("I2C connected EETI EXC3000 multiple touch controller driver");
320 +MODULE_LICENSE("GPL v2");