kernel: bump 4.14 to 4.14.93
[openwrt/staging/chunkeey.git] / target / linux / brcm2708 / patches-4.14 / 950-0133-Tidy-up-of-the-ft5406-driver-to-use-DT-2189.patch
1 From f87b44ad810fbb74b50df0df051b468bd30f4900 Mon Sep 17 00:00:00 2001
2 From: James Hughes <JamesH65@users.noreply.github.com>
3 Date: Thu, 16 Nov 2017 15:56:17 +0000
4 Subject: [PATCH 133/454] Tidy up of the ft5406 driver to use DT (#2189)
5
6 Driver was using a fixed resolution, this commit
7 adds touchscreen size, and coordinate flip and swap
8 features via device tree overlays.
9
10 Adds overrides so the VC4 can adjust the DT parameters
11 appropriately; there is a newer version of the VC4 side
12 driver that can now set up the appropriate DT values
13 if required.
14
15 Signed-off-by: James Hughes <james.hughes@raspberrypi.org>
16 ---
17 drivers/input/touchscreen/rpi-ft5406.c | 218 ++++++++++++++++---------
18 1 file changed, 145 insertions(+), 73 deletions(-)
19
20 --- a/drivers/input/touchscreen/rpi-ft5406.c
21 +++ b/drivers/input/touchscreen/rpi-ft5406.c
22 @@ -1,7 +1,7 @@
23 /*
24 * Driver for memory based ft5406 touchscreen
25 *
26 - * Copyright (C) 2015 Raspberry Pi
27 + * Copyright (C) 2015, 2017 Raspberry Pi
28 *
29 *
30 * This program is free software; you can redistribute it and/or modify
31 @@ -9,7 +9,6 @@
32 * published by the Free Software Foundation.
33 */
34
35 -
36 #include <linux/module.h>
37 #include <linux/interrupt.h>
38 #include <linux/input.h>
39 @@ -21,11 +20,15 @@
40 #include <linux/kthread.h>
41 #include <linux/platform_device.h>
42 #include <linux/stddef.h>
43 -#include <asm/io.h>
44 +#include <linux/io.h>
45 #include <linux/dma-mapping.h>
46 #include <soc/bcm2835/raspberrypi-firmware.h>
47
48 #define MAXIMUM_SUPPORTED_POINTS 10
49 +#define FTS_TOUCH_DOWN 0
50 +#define FTS_TOUCH_UP 1
51 +#define FTS_TOUCH_CONTACT 2
52 +
53 struct ft5406_regs {
54 uint8_t device_mode;
55 uint8_t gesture_id;
56 @@ -35,85 +38,125 @@ struct ft5406_regs {
57 uint8_t xl;
58 uint8_t yh;
59 uint8_t yl;
60 - uint8_t res1;
61 - uint8_t res2;
62 + uint8_t pressure; /* Not supported */
63 + uint8_t area; /* Not supported */
64 } point[MAXIMUM_SUPPORTED_POINTS];
65 };
66
67 -#define SCREEN_WIDTH 800
68 -#define SCREEN_HEIGHT 480
69 +/* These are defaults if the DT entries are missing */
70 +#define DEFAULT_SCREEN_WIDTH 800
71 +#define DEFAULT_SCREEN_HEIGHT 480
72
73 struct ft5406 {
74 - struct platform_device * pdev;
75 - struct input_dev * input_dev;
76 - void __iomem * ts_base;
77 - dma_addr_t bus_addr;
78 - struct task_struct * thread;
79 + struct platform_device *pdev;
80 + struct input_dev *input_dev;
81 + void __iomem *ts_base;
82 + dma_addr_t bus_addr;
83 + struct task_struct *thread;
84 +
85 + uint16_t max_x;
86 + uint16_t max_y;
87 + uint8_t hflip;
88 + uint8_t vflip;
89 + uint8_t xyswap;
90 };
91
92 /* Thread to poll for touchscreen events
93 - *
94 + *
95 * This thread polls the memory based register copy of the ft5406 registers
96 * using the number of points register to know whether the copy has been
97 - * updated (we write 99 to the memory copy, the GPU will write between
98 + * updated (we write 99 to the memory copy, the GPU will write between
99 * 0 - 10 points)
100 */
101 +#define ID_TO_BIT(a) (1 << a)
102 +
103 static int ft5406_thread(void *arg)
104 {
105 struct ft5406 *ts = (struct ft5406 *) arg;
106 struct ft5406_regs regs;
107 int known_ids = 0;
108 -
109 - while(!kthread_should_stop())
110 - {
111 - // 60fps polling
112 +
113 + while (!kthread_should_stop()) {
114 + /* 60fps polling */
115 msleep_interruptible(17);
116 memcpy_fromio(&regs, ts->ts_base, sizeof(struct ft5406_regs));
117 - iowrite8(99, ts->ts_base + offsetof(struct ft5406_regs, num_points));
118 - // Do not output if theres no new information (num_points is 99)
119 - // or we have no touch points and don't need to release any
120 - if(!(regs.num_points == 99 || (regs.num_points == 0 && known_ids == 0)))
121 - {
122 + iowrite8(99,
123 + ts->ts_base +
124 + offsetof(struct ft5406_regs, num_points));
125 +
126 + /*
127 + * Do not output if theres no new information (num_points is 99)
128 + * or we have no touch points and don't need to release any
129 + */
130 + if (!(regs.num_points == 99 ||
131 + (regs.num_points == 0 && known_ids == 0))) {
132 int i;
133 int modified_ids = 0, released_ids;
134 - for(i = 0; i < regs.num_points; i++)
135 - {
136 - int x = (((int) regs.point[i].xh & 0xf) << 8) + regs.point[i].xl;
137 - int y = (((int) regs.point[i].yh & 0xf) << 8) + regs.point[i].yl;
138 - int touchid = (regs.point[i].yh >> 4) & 0xf;
139 -
140 - modified_ids |= 1 << touchid;
141
142 - if(!((1 << touchid) & known_ids))
143 - dev_dbg(&ts->pdev->dev, "x = %d, y = %d, touchid = %d\n", x, y, touchid);
144 -
145 - input_mt_slot(ts->input_dev, touchid);
146 - input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 1);
147 + for (i = 0; i < regs.num_points; i++) {
148 + int x = (((int) regs.point[i].xh & 0xf) << 8) +
149 + regs.point[i].xl;
150 + int y = (((int) regs.point[i].yh & 0xf) << 8) +
151 + regs.point[i].yl;
152 + int touchid = (regs.point[i].yh >> 4) & 0xf;
153 + int event_type = (regs.point[i].xh >> 6) & 0x03;
154
155 - input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
156 - input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
157 + modified_ids |= ID_TO_BIT(touchid);
158
159 + if (event_type == FTS_TOUCH_DOWN ||
160 + event_type == FTS_TOUCH_CONTACT) {
161 + if (ts->hflip)
162 + x = ts->max_x - 1 - x;
163 +
164 + if (ts->vflip)
165 + y = ts->max_y - 1 - y;
166 +
167 + if (ts->xyswap)
168 + swap(x, y);
169 +
170 + if (!((ID_TO_BIT(touchid)) & known_ids))
171 + dev_dbg(&ts->pdev->dev,
172 + "x = %d, y = %d, press = %d, touchid = %d\n",
173 + x, y,
174 + regs.point[i].pressure,
175 + touchid);
176 +
177 + input_mt_slot(ts->input_dev, touchid);
178 + input_mt_report_slot_state(
179 + ts->input_dev,
180 + MT_TOOL_FINGER,
181 + 1);
182 +
183 + input_report_abs(ts->input_dev,
184 + ABS_MT_POSITION_X, x);
185 + input_report_abs(ts->input_dev,
186 + ABS_MT_POSITION_Y, y);
187 + }
188 }
189
190 released_ids = known_ids & ~modified_ids;
191 - for(i = 0; released_ids && i < MAXIMUM_SUPPORTED_POINTS; i++)
192 - {
193 - if(released_ids & (1<<i))
194 - {
195 - dev_dbg(&ts->pdev->dev, "Released %d, known = %x modified = %x\n", i, known_ids, modified_ids);
196 + for (i = 0;
197 + released_ids && i < MAXIMUM_SUPPORTED_POINTS;
198 + i++) {
199 + if (released_ids & (1<<i)) {
200 + dev_dbg(&ts->pdev->dev,
201 + "Released %d, known = %x, modified = %x\n",
202 + i, known_ids, modified_ids);
203 input_mt_slot(ts->input_dev, i);
204 - input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 0);
205 - modified_ids &= ~(1 << i);
206 + input_mt_report_slot_state(
207 + ts->input_dev,
208 + MT_TOOL_FINGER,
209 + 0);
210 + modified_ids &= ~(ID_TO_BIT(i));
211 }
212 }
213 known_ids = modified_ids;
214 -
215 +
216 input_mt_report_pointer_emulation(ts->input_dev, true);
217 input_sync(ts->input_dev);
218 }
219 -
220 }
221 -
222 +
223 return 0;
224 }
225
226 @@ -122,13 +165,14 @@ static int ft5406_probe(struct platform_
227 int err = 0;
228 struct device *dev = &pdev->dev;
229 struct device_node *np = dev->of_node;
230 - struct ft5406 * ts;
231 + struct ft5406 *ts;
232 struct device_node *fw_node;
233 struct rpi_firmware *fw;
234 u32 touchbuf;
235 -
236 + u32 val;
237 +
238 dev_info(dev, "Probing device\n");
239 -
240 +
241 fw_node = of_parse_phandle(np, "firmware", 0);
242 if (!fw_node) {
243 dev_err(dev, "Missing firmware node\n");
244 @@ -151,7 +195,8 @@ static int ft5406_probe(struct platform_
245 return -ENOMEM;
246 }
247
248 - ts->ts_base = dma_zalloc_coherent(dev, PAGE_SIZE, &ts->bus_addr, GFP_KERNEL);
249 + ts->ts_base = dma_zalloc_coherent(dev, PAGE_SIZE, &ts->bus_addr,
250 + GFP_KERNEL);
251 if (!ts->ts_base) {
252 pr_err("[%s]: failed to dma_alloc_coherent(%ld)\n",
253 __func__, PAGE_SIZE);
254 @@ -164,17 +209,22 @@ static int ft5406_probe(struct platform_
255 &touchbuf, sizeof(touchbuf));
256
257 if (err || touchbuf != 0) {
258 - dev_warn(dev, "Failed to set touchbuf, trying to get err:%x\n", err);
259 + dev_warn(dev, "Failed to set touchbuf, trying to get err:%x\n",
260 + err);
261 dma_free_coherent(dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);
262 ts->ts_base = 0;
263 ts->bus_addr = 0;
264 }
265
266 if (!ts->ts_base) {
267 - dev_warn(dev, "set failed, trying get (err:%d touchbuf:%x virt:%p bus:%x)\n", err, touchbuf, ts->ts_base, ts->bus_addr);
268 -
269 - err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF,
270 - &touchbuf, sizeof(touchbuf));
271 + dev_warn(dev,
272 + "set failed, trying get (err:%d touchbuf:%x virt:%p bus:%x)\n",
273 + err, touchbuf, ts->ts_base, ts->bus_addr);
274 +
275 + err = rpi_firmware_property(
276 + fw,
277 + RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF,
278 + &touchbuf, sizeof(touchbuf));
279 if (err) {
280 dev_err(dev, "Failed to get touch buffer\n");
281 goto out;
282 @@ -188,11 +238,10 @@ static int ft5406_probe(struct platform_
283
284 dev_dbg(dev, "Got TS buffer 0x%x\n", touchbuf);
285
286 - // mmap the physical memory
287 + /* mmap the physical memory */
288 touchbuf &= ~0xc0000000;
289 ts->ts_base = ioremap(touchbuf, sizeof(struct ft5406_regs));
290 - if (ts->ts_base == NULL)
291 - {
292 + if (ts->ts_base == NULL) {
293 dev_err(dev, "Failed to map physical address\n");
294 err = -ENOMEM;
295 goto out;
296 @@ -200,22 +249,46 @@ static int ft5406_probe(struct platform_
297 }
298 platform_set_drvdata(pdev, ts);
299 ts->pdev = pdev;
300 -
301 +
302 ts->input_dev->name = "FT5406 memory based driver";
303 -
304 +
305 + if (of_property_read_u32(np, "touchscreen-size-x", &val) >= 0)
306 + ts->max_x = val;
307 + else
308 + ts->max_x = DEFAULT_SCREEN_WIDTH;
309 +
310 + if (of_property_read_u32(np, "touchscreen-size-y", &val) >= 0)
311 + ts->max_y = val;
312 + else
313 + ts->max_y = DEFAULT_SCREEN_HEIGHT;
314 +
315 + if (of_property_read_u32(np, "touchscreen-inverted-x", &val) >= 0)
316 + ts->hflip = val;
317 +
318 + if (of_property_read_u32(np, "touchscreen-inverted-y", &val) >= 0)
319 + ts->vflip = val;
320 +
321 + if (of_property_read_u32(np, "touchscreen-swapped-x-y", &val) >= 0)
322 + ts->xyswap = val;
323 +
324 + dev_dbg(dev,
325 + "Touchscreen parameters (%d,%d), hflip=%d, vflip=%d, xyswap=%d",
326 + ts->max_x, ts->max_y, ts->hflip, ts->vflip, ts->xyswap);
327 +
328 __set_bit(EV_KEY, ts->input_dev->evbit);
329 __set_bit(EV_SYN, ts->input_dev->evbit);
330 __set_bit(EV_ABS, ts->input_dev->evbit);
331
332 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0,
333 - SCREEN_WIDTH, 0, 0);
334 + ts->xyswap ? ts->max_y : ts->max_x, 0, 0);
335 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0,
336 - SCREEN_HEIGHT, 0, 0);
337 + ts->xyswap ? ts->max_x : ts->max_y, 0, 0);
338
339 - input_mt_init_slots(ts->input_dev, MAXIMUM_SUPPORTED_POINTS, INPUT_MT_DIRECT);
340 + input_mt_init_slots(ts->input_dev,
341 + MAXIMUM_SUPPORTED_POINTS, INPUT_MT_DIRECT);
342
343 input_set_drvdata(ts->input_dev, ts);
344 -
345 +
346 err = input_register_device(ts->input_dev);
347 if (err) {
348 dev_err(dev, "could not register input device, %d\n",
349 @@ -223,10 +296,9 @@ static int ft5406_probe(struct platform_
350 goto out;
351 }
352
353 - // create thread to poll the touch events
354 + /* create thread that polls the touch events */
355 ts->thread = kthread_run(ft5406_thread, ts, "ft5406");
356 - if(ts->thread == NULL)
357 - {
358 + if (ts->thread == NULL) {
359 dev_err(dev, "Failed to create kernel thread");
360 err = -ENOMEM;
361 goto out;
362 @@ -254,9 +326,9 @@ static int ft5406_remove(struct platform
363 {
364 struct device *dev = &pdev->dev;
365 struct ft5406 *ts = (struct ft5406 *) platform_get_drvdata(pdev);
366 -
367 +
368 dev_info(dev, "Removing rpi-ft5406\n");
369 -
370 +
371 kthread_stop(ts->thread);
372
373 if (ts->bus_addr)
374 @@ -265,7 +337,7 @@ static int ft5406_remove(struct platform
375 iounmap(ts->ts_base);
376 if (ts->input_dev)
377 input_unregister_device(ts->input_dev);
378 -
379 +
380 return 0;
381 }
382