brcm2708: switch to linux 4.4 and update patches
[openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0205-Input-add-support-for-FocalTech-FT6236-touchscreen-c.patch
1 From 75a6299867667a481a12f3c833322e39ff4fb089 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= <noralf@tronnes.org>
3 Date: Fri, 2 Oct 2015 11:30:11 -0700
4 Subject: [PATCH 205/222] Input: add support for FocalTech FT6236 touchscreen
5  controller
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 This adds support for the FT6x06 and the FT6x36 family of capacitive touch
11 panel controllers, in particular the FT6236.
12
13 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
14 Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
15 ---
16  .../input/touchscreen/focaltech-ft6236.txt         |  35 +++
17  .../devicetree/bindings/vendor-prefixes.txt        |   1 +
18  drivers/input/touchscreen/Kconfig                  |  13 +
19  drivers/input/touchscreen/Makefile                 |   1 +
20  drivers/input/touchscreen/ft6236.c                 | 327 +++++++++++++++++++++
21  5 files changed, 377 insertions(+)
22  create mode 100644 Documentation/devicetree/bindings/input/touchscreen/focaltech-ft6236.txt
23  create mode 100644 drivers/input/touchscreen/ft6236.c
24
25 --- /dev/null
26 +++ b/Documentation/devicetree/bindings/input/touchscreen/focaltech-ft6236.txt
27 @@ -0,0 +1,35 @@
28 +* FocalTech FT6236 I2C touchscreen controller
29 +
30 +Required properties:
31 + - compatible            : "focaltech,ft6236"
32 + - reg                   : I2C slave address of the chip (0x38)
33 + - interrupt-parent      : a phandle pointing to the interrupt controller
34 +                           serving the interrupt for this chip
35 + - interrupts            : interrupt specification for the touch controller
36 +                           interrupt
37 + - reset-gpios           : GPIO specification for the RSTN input
38 + - touchscreen-size-x    : horizontal resolution of touchscreen (in pixels)
39 + - touchscreen-size-y    : vertical resolution of touchscreen (in pixels)
40 +
41 +Optional properties:
42 + - touchscreen-fuzz-x    : horizontal noise value of the absolute input
43 +                           device (in pixels)
44 + - touchscreen-fuzz-y    : vertical noise value of the absolute input
45 +                           device (in pixels)
46 + - touchscreen-inverted-x : X axis is inverted (boolean)
47 + - touchscreen-inverted-y : Y axis is inverted (boolean)
48 + - touchscreen-swapped-x-y: X and Y axis are swapped (boolean)
49 +                           Swapping is done after inverting the axis
50 +
51 +Example:
52 +
53 +       ft6x06@38 {
54 +               compatible = "focaltech,ft6236";
55 +               reg = <0x38>;
56 +               interrupt-parent = <&gpio>;
57 +               interrupts = <23 2>;
58 +               touchscreen-size-x = <320>;
59 +               touchscreen-size-y = <480>;
60 +               touchscreen-inverted-x;
61 +               touchscreen-swapped-x-y;
62 +       };
63 --- a/Documentation/devicetree/bindings/vendor-prefixes.txt
64 +++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
65 @@ -76,6 +76,7 @@ everspin      Everspin Technologies, Inc.
66  excito Excito
67  fcs    Fairchild Semiconductor
68  firefly        Firefly
69 +focaltech      FocalTech Systems Co.,Ltd
70  fsl    Freescale Semiconductor
71  GEFanuc        GE Fanuc Intelligent Platforms Embedded Systems, Inc.
72  gef    GE Fanuc Intelligent Platforms Embedded Systems, Inc.
73 --- a/drivers/input/touchscreen/Kconfig
74 +++ b/drivers/input/touchscreen/Kconfig
75 @@ -295,6 +295,19 @@ config TOUCHSCREEN_EGALAX
76           To compile this driver as a module, choose M here: the
77           module will be called egalax_ts.
78  
79 +config TOUCHSCREEN_FT6236
80 +       tristate "FT6236 I2C touchscreen"
81 +       depends on I2C
82 +       depends on GPIOLIB || COMPILE_TEST
83 +       help
84 +         Say Y here to enable support for the I2C connected FT6x06 and
85 +         FT6x36 family of capacitive touchscreen drivers.
86 +
87 +         If unsure, say N.
88 +
89 +         To compile this driver as a module, choose M here: the
90 +         module will be called ft6236.
91 +
92  config TOUCHSCREEN_FUJITSU
93         tristate "Fujitsu serial touchscreen"
94         select SERIO
95 --- a/drivers/input/touchscreen/Makefile
96 +++ b/drivers/input/touchscreen/Makefile
97 @@ -36,6 +36,7 @@ obj-$(CONFIG_TOUCHSCREEN_EETI)                += eeti_
98  obj-$(CONFIG_TOUCHSCREEN_ELAN)         += elants_i2c.o
99  obj-$(CONFIG_TOUCHSCREEN_ELO)          += elo.o
100  obj-$(CONFIG_TOUCHSCREEN_EGALAX)       += egalax_ts.o
101 +obj-$(CONFIG_TOUCHSCREEN_FT6236)       += ft6236.o
102  obj-$(CONFIG_TOUCHSCREEN_FUJITSU)      += fujitsu_ts.o
103  obj-$(CONFIG_TOUCHSCREEN_GOODIX)       += goodix.o
104  obj-$(CONFIG_TOUCHSCREEN_ILI210X)      += ili210x.o
105 --- /dev/null
106 +++ b/drivers/input/touchscreen/ft6236.c
107 @@ -0,0 +1,327 @@
108 +/*
109 + * FocalTech FT6236 TouchScreen driver.
110 + *
111 + * Copyright (c) 2010  Focal tech Ltd.
112 + *
113 + * This software is licensed under the terms of the GNU General Public
114 + * License version 2, as published by the Free Software Foundation, and
115 + * may be copied, distributed, and modified under those terms.
116 + *
117 + * This program is distributed in the hope that it will be useful,
118 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
119 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
120 + * GNU General Public License for more details.
121 + */
122 +
123 +#include <linux/delay.h>
124 +#include <linux/gpio/consumer.h>
125 +#include <linux/i2c.h>
126 +#include <linux/input.h>
127 +#include <linux/input/mt.h>
128 +#include <linux/interrupt.h>
129 +#include <linux/module.h>
130 +#include <linux/property.h>
131 +
132 +#define FT6236_MAX_TOUCH_POINTS                2
133 +
134 +#define FT6236_REG_TH_GROUP            0x80
135 +#define FT6236_REG_PERIODACTIVE                0x88
136 +#define FT6236_REG_LIB_VER_H           0xa1
137 +#define FT6236_REG_LIB_VER_L           0xa2
138 +#define FT6236_REG_CIPHER              0xa3
139 +#define FT6236_REG_FIRMID              0xa6
140 +#define FT6236_REG_FOCALTECH_ID                0xa8
141 +#define FT6236_REG_RELEASE_CODE_ID     0xaf
142 +
143 +#define FT6236_EVENT_PRESS_DOWN                0
144 +#define FT6236_EVENT_LIFT_UP           1
145 +#define FT6236_EVENT_CONTACT           2
146 +#define FT6236_EVENT_NO_EVENT          3
147 +
148 +struct ft6236_data {
149 +       struct i2c_client *client;
150 +       struct input_dev *input;
151 +       struct gpio_desc *reset_gpio;
152 +       u32 max_x;
153 +       u32 max_y;
154 +       bool invert_x;
155 +       bool invert_y;
156 +       bool swap_xy;
157 +};
158 +
159 +/*
160 + * This struct is a touchpoint as stored in hardware.  Note that the id,
161 + * as well as the event, are stored in the upper nybble of the hi byte.
162 + */
163 +struct ft6236_touchpoint {
164 +       union {
165 +               u8 xhi;
166 +               u8 event;
167 +       };
168 +       u8 xlo;
169 +       union {
170 +               u8 yhi;
171 +               u8 id;
172 +       };
173 +       u8 ylo;
174 +       u8 weight;
175 +       u8 misc;
176 +} __packed;
177 +
178 +/* This packet represents the register map as read from offset 0 */
179 +struct ft6236_packet {
180 +       u8 dev_mode;
181 +       u8 gest_id;
182 +       u8 touches;
183 +       struct ft6236_touchpoint points[FT6236_MAX_TOUCH_POINTS];
184 +} __packed;
185 +
186 +static int ft6236_read(struct i2c_client *client, u8 reg, u8 len, void *data)
187 +{
188 +       int error;
189 +
190 +       error = i2c_smbus_read_i2c_block_data(client, reg, len, data);
191 +       if (error < 0)
192 +               return error;
193 +
194 +       if (error != len)
195 +               return -EIO;
196 +
197 +       return 0;
198 +}
199 +
200 +static irqreturn_t ft6236_interrupt(int irq, void *dev_id)
201 +{
202 +       struct ft6236_data *ft6236 = dev_id;
203 +       struct device *dev = &ft6236->client->dev;
204 +       struct input_dev *input = ft6236->input;
205 +       struct ft6236_packet buf;
206 +       u8 touches;
207 +       int i, error;
208 +
209 +       error = ft6236_read(ft6236->client, 0, sizeof(buf), &buf);
210 +       if (error) {
211 +               dev_err(dev, "read touchdata failed %d\n", error);
212 +               return IRQ_HANDLED;
213 +       }
214 +
215 +       touches = buf.touches & 0xf;
216 +       if (touches > FT6236_MAX_TOUCH_POINTS) {
217 +               dev_dbg(dev,
218 +                       "%d touch points reported, only %d are supported\n",
219 +                       touches, FT6236_MAX_TOUCH_POINTS);
220 +               touches = FT6236_MAX_TOUCH_POINTS;
221 +       }
222 +
223 +       for (i = 0; i < touches; i++) {
224 +               struct ft6236_touchpoint *point = &buf.points[i];
225 +               u16 x = ((point->xhi & 0xf) << 8) | buf.points[i].xlo;
226 +               u16 y = ((point->yhi & 0xf) << 8) | buf.points[i].ylo;
227 +               u8 event = point->event >> 6;
228 +               u8 id = point->id >> 4;
229 +               bool act = (event == FT6236_EVENT_PRESS_DOWN ||
230 +                           event == FT6236_EVENT_CONTACT);
231 +
232 +               input_mt_slot(input, id);
233 +               input_mt_report_slot_state(input, MT_TOOL_FINGER, act);
234 +               if (!act)
235 +                       continue;
236 +
237 +               if (ft6236->invert_x)
238 +                       x = ft6236->max_x - x;
239 +
240 +               if (ft6236->invert_y)
241 +                       y = ft6236->max_y - y;
242 +
243 +               if (ft6236->swap_xy) {
244 +                       input_report_abs(input, ABS_MT_POSITION_X, y);
245 +                       input_report_abs(input, ABS_MT_POSITION_Y, x);
246 +               } else {
247 +                       input_report_abs(input, ABS_MT_POSITION_X, x);
248 +                       input_report_abs(input, ABS_MT_POSITION_Y, y);
249 +               }
250 +       }
251 +
252 +       input_mt_sync_frame(input);
253 +       input_sync(input);
254 +
255 +       return IRQ_HANDLED;
256 +}
257 +
258 +static u8 ft6236_debug_read_byte(struct ft6236_data *ft6236, u8 reg)
259 +{
260 +       struct i2c_client *client = ft6236->client;
261 +       u8 val = 0;
262 +       int error;
263 +
264 +       error = ft6236_read(client, reg, 1, &val);
265 +       if (error)
266 +               dev_dbg(&client->dev,
267 +                       "error reading register 0x%02x: %d\n", reg, error);
268 +
269 +       return val;
270 +}
271 +
272 +static void ft6236_debug_info(struct ft6236_data *ft6236)
273 +{
274 +       struct device *dev = &ft6236->client->dev;
275 +
276 +       dev_dbg(dev, "Touch threshold is %d\n",
277 +               ft6236_debug_read_byte(ft6236, FT6236_REG_TH_GROUP) * 4);
278 +       dev_dbg(dev, "Report rate is %dHz\n",
279 +               ft6236_debug_read_byte(ft6236, FT6236_REG_PERIODACTIVE) * 10);
280 +       dev_dbg(dev, "Firmware library version 0x%02x%02x\n",
281 +               ft6236_debug_read_byte(ft6236, FT6236_REG_LIB_VER_H),
282 +               ft6236_debug_read_byte(ft6236, FT6236_REG_LIB_VER_L));
283 +       dev_dbg(dev, "Firmware version 0x%02x\n",
284 +               ft6236_debug_read_byte(ft6236, FT6236_REG_FIRMID));
285 +       dev_dbg(dev, "Chip vendor ID 0x%02x\n",
286 +               ft6236_debug_read_byte(ft6236, FT6236_REG_CIPHER));
287 +       dev_dbg(dev, "CTPM vendor ID 0x%02x\n",
288 +               ft6236_debug_read_byte(ft6236, FT6236_REG_FOCALTECH_ID));
289 +       dev_dbg(dev, "Release code version 0x%02x\n",
290 +               ft6236_debug_read_byte(ft6236, FT6236_REG_RELEASE_CODE_ID));
291 +}
292 +
293 +static void ft6236_reset(struct ft6236_data *ft6236)
294 +{
295 +       if (!ft6236->reset_gpio)
296 +               return;
297 +
298 +       gpiod_set_value_cansleep(ft6236->reset_gpio, 1);
299 +       usleep_range(5000, 20000);
300 +       gpiod_set_value_cansleep(ft6236->reset_gpio, 0);
301 +       msleep(300);
302 +}
303 +
304 +static int ft6236_probe(struct i2c_client *client,
305 +                       const struct i2c_device_id *id)
306 +{
307 +       struct device *dev = &client->dev;
308 +       struct ft6236_data *ft6236;
309 +       struct input_dev *input;
310 +       u32 fuzz_x = 0, fuzz_y = 0;
311 +       u8 val;
312 +       int error;
313 +
314 +       if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
315 +               return -ENXIO;
316 +
317 +       if (!client->irq) {
318 +               dev_err(dev, "irq is missing\n");
319 +               return -EINVAL;
320 +       }
321 +
322 +       ft6236 = devm_kzalloc(dev, sizeof(*ft6236), GFP_KERNEL);
323 +       if (!ft6236)
324 +               return -ENOMEM;
325 +
326 +       ft6236->client = client;
327 +       ft6236->reset_gpio = devm_gpiod_get_optional(dev, "reset",
328 +                                                    GPIOD_OUT_LOW);
329 +       if (IS_ERR(ft6236->reset_gpio)) {
330 +               error = PTR_ERR(ft6236->reset_gpio);
331 +               if (error != -EPROBE_DEFER)
332 +                       dev_err(dev, "error getting reset gpio: %d\n", error);
333 +               return error;
334 +       }
335 +
336 +       ft6236_reset(ft6236);
337 +
338 +       /* verify that the controller is present */
339 +       error = ft6236_read(client, 0x00, 1, &val);
340 +       if (error) {
341 +               dev_err(dev, "failed to read from controller: %d\n", error);
342 +               return error;
343 +       }
344 +
345 +       ft6236_debug_info(ft6236);
346 +
347 +       input = devm_input_allocate_device(dev);
348 +       if (!input)
349 +               return -ENOMEM;
350 +
351 +       ft6236->input = input;
352 +       input->name = client->name;
353 +       input->id.bustype = BUS_I2C;
354 +
355 +       if (device_property_read_u32(dev, "touchscreen-size-x",
356 +                                    &ft6236->max_x) ||
357 +           device_property_read_u32(dev, "touchscreen-size-y",
358 +                                    &ft6236->max_y)) {
359 +               dev_err(dev, "touchscreen-size-x and/or -y missing\n");
360 +               return -EINVAL;
361 +       }
362 +
363 +       device_property_read_u32(dev, "touchscreen-fuzz-x", &fuzz_x);
364 +       device_property_read_u32(dev, "touchscreen-fuzz-y", &fuzz_y);
365 +       ft6236->invert_x = device_property_read_bool(dev,
366 +                                                    "touchscreen-inverted-x");
367 +       ft6236->invert_y = device_property_read_bool(dev,
368 +                                                    "touchscreen-inverted-y");
369 +       ft6236->swap_xy = device_property_read_bool(dev,
370 +                                                   "touchscreen-swapped-x-y");
371 +
372 +       if (ft6236->swap_xy) {
373 +               input_set_abs_params(input, ABS_MT_POSITION_X, 0,
374 +                                    ft6236->max_y, fuzz_y, 0);
375 +               input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
376 +                                    ft6236->max_x, fuzz_x, 0);
377 +       } else {
378 +               input_set_abs_params(input, ABS_MT_POSITION_X, 0,
379 +                                    ft6236->max_x, fuzz_x, 0);
380 +               input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
381 +                                    ft6236->max_y, fuzz_y, 0);
382 +       }
383 +
384 +       error = input_mt_init_slots(input, FT6236_MAX_TOUCH_POINTS,
385 +                                   INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
386 +       if (error)
387 +               return error;
388 +
389 +       error = devm_request_threaded_irq(dev, client->irq, NULL,
390 +                                         ft6236_interrupt, IRQF_ONESHOT,
391 +                                         client->name, ft6236);
392 +       if (error) {
393 +               dev_err(dev, "request irq %d failed: %d\n", client->irq, error);
394 +               return error;
395 +       }
396 +
397 +       error = input_register_device(input);
398 +       if (error) {
399 +               dev_err(dev, "failed to register input device: %d\n", error);
400 +               return error;
401 +       }
402 +
403 +       return 0;
404 +}
405 +
406 +#ifdef CONFIG_OF
407 +static const struct of_device_id ft6236_of_match[] = {
408 +       { .compatible = "focaltech,ft6236", },
409 +       { }
410 +};
411 +MODULE_DEVICE_TABLE(of, ft6236_of_match);
412 +#endif
413 +
414 +static const struct i2c_device_id ft6236_id[] = {
415 +       { "ft6236", },
416 +       { }
417 +};
418 +MODULE_DEVICE_TABLE(i2c, ft6236_id);
419 +
420 +static struct i2c_driver ft6236_driver = {
421 +       .driver = {
422 +               .name = "ft6236",
423 +               .owner = THIS_MODULE,
424 +               .of_match_table = of_match_ptr(ft6236_of_match),
425 +       },
426 +       .probe = ft6236_probe,
427 +       .id_table = ft6236_id,
428 +};
429 +module_i2c_driver(ft6236_driver);
430 +
431 +MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
432 +MODULE_AUTHOR("Noralf Trønnes <noralf@tronnes.org>");
433 +MODULE_DESCRIPTION("FocalTech FT6236 TouchScreen driver");
434 +MODULE_LICENSE("GPL");