1 From 4d326f3b4817e2a29d4ae69239c1ec2ef4a321a1 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 105/725] Tidy up of the ft5406 driver to use DT (#2189)
6 Driver was using a fixed resolution, this commit
7 adds touchscreen size, and coordinate flip and swap
8 features via device tree overlays.
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
15 Signed-off-by: James Hughes <james.hughes@raspberrypi.org>
17 drivers/input/touchscreen/rpi-ft5406.c | 218 ++++++++++++++++---------
18 1 file changed, 145 insertions(+), 73 deletions(-)
20 --- a/drivers/input/touchscreen/rpi-ft5406.c
21 +++ b/drivers/input/touchscreen/rpi-ft5406.c
24 * Driver for memory based ft5406 touchscreen
26 - * Copyright (C) 2015 Raspberry Pi
27 + * Copyright (C) 2015, 2017 Raspberry Pi
30 * This program is free software; you can redistribute it and/or modify
32 * published by the Free Software Foundation.
36 #include <linux/module.h>
37 #include <linux/interrupt.h>
38 #include <linux/input.h>
40 #include <linux/kthread.h>
41 #include <linux/platform_device.h>
42 #include <linux/stddef.h>
44 +#include <linux/io.h>
45 #include <linux/dma-mapping.h>
46 #include <soc/bcm2835/raspberrypi-firmware.h>
48 #define MAXIMUM_SUPPORTED_POINTS 10
49 +#define FTS_TOUCH_DOWN 0
50 +#define FTS_TOUCH_UP 1
51 +#define FTS_TOUCH_CONTACT 2
56 @@ -35,85 +38,125 @@ struct ft5406_regs {
62 + uint8_t pressure; /* Not supported */
63 + uint8_t area; /* Not supported */
64 } point[MAXIMUM_SUPPORTED_POINTS];
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
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;
92 /* Thread to poll for touchscreen events
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
101 +#define ID_TO_BIT(a) (1 << a)
103 static int ft5406_thread(void *arg)
105 struct ft5406 *ts = (struct ft5406 *) arg;
106 struct ft5406_regs regs;
109 - while(!kthread_should_stop())
113 + while (!kthread_should_stop()) {
114 + /* 60fps polling */
115 msleep_interruptible(17);
116 memcpy_fromio(®s, 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)))
124 + offsetof(struct ft5406_regs, num_points));
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
130 + if (!(regs.num_points == 99 ||
131 + (regs.num_points == 0 && known_ids == 0))) {
133 int modified_ids = 0, released_ids;
134 - for(i = 0; i < regs.num_points; i++)
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;
140 - modified_ids |= 1 << touchid;
142 - if(!((1 << touchid) & known_ids))
143 - dev_dbg(&ts->pdev->dev, "x = %d, y = %d, touchid = %d\n", x, y, touchid);
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) +
150 + int y = (((int) regs.point[i].yh & 0xf) << 8) +
152 + int touchid = (regs.point[i].yh >> 4) & 0xf;
153 + int event_type = (regs.point[i].xh >> 6) & 0x03;
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);
159 + if (event_type == FTS_TOUCH_DOWN ||
160 + event_type == FTS_TOUCH_CONTACT) {
162 + x = ts->max_x - 1 - x;
165 + y = ts->max_y - 1 - y;
170 + if (!((ID_TO_BIT(touchid)) & known_ids))
171 + dev_dbg(&ts->pdev->dev,
172 + "x = %d, y = %d, press = %d, touchid = %d\n",
174 + regs.point[i].pressure,
177 + input_mt_slot(ts->input_dev, touchid);
178 + input_mt_report_slot_state(
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);
190 released_ids = known_ids & ~modified_ids;
191 - for(i = 0; released_ids && i < MAXIMUM_SUPPORTED_POINTS; i++)
193 - if(released_ids & (1<<i))
195 - dev_dbg(&ts->pdev->dev, "Released %d, known = %x modified = %x\n", i, known_ids, modified_ids);
197 + released_ids && i < MAXIMUM_SUPPORTED_POINTS;
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(
210 + modified_ids &= ~(ID_TO_BIT(i));
213 known_ids = modified_ids;
216 input_mt_report_pointer_emulation(ts->input_dev, true);
217 input_sync(ts->input_dev);
226 @@ -122,13 +165,14 @@ static int ft5406_probe(struct platform_
228 struct device *dev = &pdev->dev;
229 struct device_node *np = dev->of_node;
230 - struct ft5406 * ts;
232 struct device_node *fw_node;
233 struct rpi_firmware *fw;
238 dev_info(dev, "Probing device\n");
241 fw_node = of_parse_phandle(np, "firmware", 0);
243 dev_err(dev, "Missing firmware node\n");
244 @@ -151,7 +195,8 @@ static int ft5406_probe(struct platform_
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,
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));
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",
261 dma_free_coherent(dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);
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);
269 - err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF,
270 - &touchbuf, sizeof(touchbuf));
272 + "set failed, trying get (err:%d touchbuf:%x virt:%p bus:%x)\n",
273 + err, touchbuf, ts->ts_base, ts->bus_addr);
275 + err = rpi_firmware_property(
277 + RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF,
278 + &touchbuf, sizeof(touchbuf));
280 dev_err(dev, "Failed to get touch buffer\n");
282 @@ -188,11 +238,10 @@ static int ft5406_probe(struct platform_
284 dev_dbg(dev, "Got TS buffer 0x%x\n", touchbuf);
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)
292 + if (ts->ts_base == NULL) {
293 dev_err(dev, "Failed to map physical address\n");
296 @@ -200,22 +249,46 @@ static int ft5406_probe(struct platform_
298 platform_set_drvdata(pdev, ts);
302 ts->input_dev->name = "FT5406 memory based driver";
305 + if (of_property_read_u32(np, "touchscreen-size-x", &val) >= 0)
308 + ts->max_x = DEFAULT_SCREEN_WIDTH;
310 + if (of_property_read_u32(np, "touchscreen-size-y", &val) >= 0)
313 + ts->max_y = DEFAULT_SCREEN_HEIGHT;
315 + if (of_property_read_u32(np, "touchscreen-inverted-x", &val) >= 0)
318 + if (of_property_read_u32(np, "touchscreen-inverted-y", &val) >= 0)
321 + if (of_property_read_u32(np, "touchscreen-swapped-x-y", &val) >= 0)
325 + "Touchscreen parameters (%d,%d), hflip=%d, vflip=%d, xyswap=%d",
326 + ts->max_x, ts->max_y, ts->hflip, ts->vflip, ts->xyswap);
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);
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);
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);
343 input_set_drvdata(ts->input_dev, ts);
346 err = input_register_device(ts->input_dev);
348 dev_err(dev, "could not register input device, %d\n",
349 @@ -223,10 +296,9 @@ static int ft5406_probe(struct platform_
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)
358 + if (ts->thread == NULL) {
359 dev_err(dev, "Failed to create kernel thread");
362 @@ -254,9 +326,9 @@ static int ft5406_remove(struct platform
364 struct device *dev = &pdev->dev;
365 struct ft5406 *ts = (struct ft5406 *) platform_get_drvdata(pdev);
368 dev_info(dev, "Removing rpi-ft5406\n");
371 kthread_stop(ts->thread);
374 @@ -265,7 +337,7 @@ static int ft5406_remove(struct platform
375 iounmap(ts->ts_base);
377 input_unregister_device(ts->input_dev);