799a384db52ed1b03c7aed8cf67515b123d447b2
[openwrt/staging/blogic.git] /
1 From 0a9850b65c2ad0183684790cb6fefb2f59eca573 Mon Sep 17 00:00:00 2001
2 From: Gordon Hollingworth <gordon@raspberrypi.org>
3 Date: Tue, 12 May 2015 14:47:56 +0100
4 Subject: [PATCH 060/454] rpi-ft5406: Add touchscreen driver for pi LCD display
5
6 Fix driver detection failure Check that the buffer response is non-zero meaning the touchscreen was detected
7
8 rpi-ft5406: Use firmware API
9
10 RPI-FT5406: Enable aarch64 support through explicit iomem interface
11
12 Signed-off-by: Gerhard de Clercq <gerharddeclercq@outlook.com>
13 ---
14 drivers/input/touchscreen/Kconfig | 7 +
15 drivers/input/touchscreen/Makefile | 1 +
16 drivers/input/touchscreen/rpi-ft5406.c | 292 +++++++++++++++++++++++++
17 3 files changed, 300 insertions(+)
18 create mode 100644 drivers/input/touchscreen/rpi-ft5406.c
19
20 --- a/drivers/input/touchscreen/Kconfig
21 +++ b/drivers/input/touchscreen/Kconfig
22 @@ -628,6 +628,13 @@ config TOUCHSCREEN_EDT_FT5X06
23 To compile this driver as a module, choose M here: the
24 module will be called edt-ft5x06.
25
26 +config TOUCHSCREEN_RPI_FT5406
27 + tristate "Raspberry Pi FT5406 driver"
28 + depends on RASPBERRYPI_FIRMWARE
29 + help
30 + Say Y here to enable the Raspberry Pi memory based FT5406 device
31 +
32 +
33 config TOUCHSCREEN_MIGOR
34 tristate "Renesas MIGO-R touchscreen"
35 depends on (SH_MIGOR || COMPILE_TEST) && I2C
36 --- a/drivers/input/touchscreen/Makefile
37 +++ b/drivers/input/touchscreen/Makefile
38 @@ -30,6 +30,7 @@ obj-$(CONFIG_TOUCHSCREEN_DA9034) += da90
39 obj-$(CONFIG_TOUCHSCREEN_DA9052) += da9052_tsi.o
40 obj-$(CONFIG_TOUCHSCREEN_DYNAPRO) += dynapro.o
41 obj-$(CONFIG_TOUCHSCREEN_EDT_FT5X06) += edt-ft5x06.o
42 +obj-$(CONFIG_TOUCHSCREEN_RPI_FT5406) += rpi-ft5406.o
43 obj-$(CONFIG_TOUCHSCREEN_HAMPSHIRE) += hampshire.o
44 obj-$(CONFIG_TOUCHSCREEN_GUNZE) += gunze.o
45 obj-$(CONFIG_TOUCHSCREEN_EETI) += eeti_ts.o
46 --- /dev/null
47 +++ b/drivers/input/touchscreen/rpi-ft5406.c
48 @@ -0,0 +1,292 @@
49 +/*
50 + * Driver for memory based ft5406 touchscreen
51 + *
52 + * Copyright (C) 2015 Raspberry Pi
53 + *
54 + *
55 + * This program is free software; you can redistribute it and/or modify
56 + * it under the terms of the GNU General Public License version 2 as
57 + * published by the Free Software Foundation.
58 + */
59 +
60 +
61 +#include <linux/module.h>
62 +#include <linux/interrupt.h>
63 +#include <linux/input.h>
64 +#include <linux/irq.h>
65 +#include <linux/delay.h>
66 +#include <linux/slab.h>
67 +#include <linux/bitops.h>
68 +#include <linux/input/mt.h>
69 +#include <linux/kthread.h>
70 +#include <linux/platform_device.h>
71 +#include <linux/stddef.h>
72 +#include <asm/io.h>
73 +#include <linux/dma-mapping.h>
74 +#include <soc/bcm2835/raspberrypi-firmware.h>
75 +
76 +#define MAXIMUM_SUPPORTED_POINTS 10
77 +struct ft5406_regs {
78 + uint8_t device_mode;
79 + uint8_t gesture_id;
80 + uint8_t num_points;
81 + struct ft5406_touch {
82 + uint8_t xh;
83 + uint8_t xl;
84 + uint8_t yh;
85 + uint8_t yl;
86 + uint8_t res1;
87 + uint8_t res2;
88 + } point[MAXIMUM_SUPPORTED_POINTS];
89 +};
90 +
91 +#define SCREEN_WIDTH 800
92 +#define SCREEN_HEIGHT 480
93 +
94 +struct ft5406 {
95 + struct platform_device * pdev;
96 + struct input_dev * input_dev;
97 + void __iomem * ts_base;
98 + dma_addr_t bus_addr;
99 + struct task_struct * thread;
100 +};
101 +
102 +/* Thread to poll for touchscreen events
103 + *
104 + * This thread polls the memory based register copy of the ft5406 registers
105 + * using the number of points register to know whether the copy has been
106 + * updated (we write 99 to the memory copy, the GPU will write between
107 + * 0 - 10 points)
108 + */
109 +static int ft5406_thread(void *arg)
110 +{
111 + struct ft5406 *ts = (struct ft5406 *) arg;
112 + struct ft5406_regs regs;
113 + int known_ids = 0;
114 +
115 + while(!kthread_should_stop())
116 + {
117 + // 60fps polling
118 + msleep_interruptible(17);
119 + memcpy_fromio(&regs, ts->ts_base, sizeof(struct ft5406_regs));
120 + iowrite8(99, ts->ts_base + offsetof(struct ft5406_regs, num_points));
121 + // Do not output if theres no new information (num_points is 99)
122 + // or we have no touch points and don't need to release any
123 + if(!(regs.num_points == 99 || (regs.num_points == 0 && known_ids == 0)))
124 + {
125 + int i;
126 + int modified_ids = 0, released_ids;
127 + for(i = 0; i < regs.num_points; i++)
128 + {
129 + int x = (((int) regs.point[i].xh & 0xf) << 8) + regs.point[i].xl;
130 + int y = (((int) regs.point[i].yh & 0xf) << 8) + regs.point[i].yl;
131 + int touchid = (regs.point[i].yh >> 4) & 0xf;
132 +
133 + modified_ids |= 1 << touchid;
134 +
135 + if(!((1 << touchid) & known_ids))
136 + dev_dbg(&ts->pdev->dev, "x = %d, y = %d, touchid = %d\n", x, y, touchid);
137 +
138 + input_mt_slot(ts->input_dev, touchid);
139 + input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 1);
140 +
141 + input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
142 + input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
143 +
144 + }
145 +
146 + released_ids = known_ids & ~modified_ids;
147 + for(i = 0; released_ids && i < MAXIMUM_SUPPORTED_POINTS; i++)
148 + {
149 + if(released_ids & (1<<i))
150 + {
151 + dev_dbg(&ts->pdev->dev, "Released %d, known = %x modified = %x\n", i, known_ids, modified_ids);
152 + input_mt_slot(ts->input_dev, i);
153 + input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 0);
154 + modified_ids &= ~(1 << i);
155 + }
156 + }
157 + known_ids = modified_ids;
158 +
159 + input_mt_report_pointer_emulation(ts->input_dev, true);
160 + input_sync(ts->input_dev);
161 + }
162 +
163 + }
164 +
165 + return 0;
166 +}
167 +
168 +static int ft5406_probe(struct platform_device *pdev)
169 +{
170 + int err = 0;
171 + struct device *dev = &pdev->dev;
172 + struct device_node *np = dev->of_node;
173 + struct ft5406 * ts;
174 + struct device_node *fw_node;
175 + struct rpi_firmware *fw;
176 + u32 touchbuf;
177 +
178 + dev_info(dev, "Probing device\n");
179 +
180 + fw_node = of_parse_phandle(np, "firmware", 0);
181 + if (!fw_node) {
182 + dev_err(dev, "Missing firmware node\n");
183 + return -ENOENT;
184 + }
185 +
186 + fw = rpi_firmware_get(fw_node);
187 + if (!fw)
188 + return -EPROBE_DEFER;
189 +
190 + ts = devm_kzalloc(dev, sizeof(struct ft5406), GFP_KERNEL);
191 + if (!ts) {
192 + dev_err(dev, "Failed to allocate memory\n");
193 + return -ENOMEM;
194 + }
195 +
196 + ts->input_dev = input_allocate_device();
197 + if (!ts->input_dev) {
198 + dev_err(dev, "Failed to allocate input device\n");
199 + return -ENOMEM;
200 + }
201 +
202 + ts->ts_base = dma_zalloc_coherent(dev, PAGE_SIZE, &ts->bus_addr, GFP_KERNEL);
203 + if (!ts->ts_base) {
204 + pr_err("[%s]: failed to dma_alloc_coherent(%ld)\n",
205 + __func__, PAGE_SIZE);
206 + err = -ENOMEM;
207 + goto out;
208 + }
209 +
210 + touchbuf = (u32)ts->bus_addr;
211 + err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
212 + &touchbuf, sizeof(touchbuf));
213 +
214 + if (err || touchbuf != 0) {
215 + dev_warn(dev, "Failed to set touchbuf, trying to get err:%x\n", err);
216 + dma_free_coherent(dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);
217 + ts->ts_base = 0;
218 + ts->bus_addr = 0;
219 + }
220 +
221 + if (!ts->ts_base) {
222 + dev_warn(dev, "set failed, trying get (err:%d touchbuf:%x virt:%p bus:%x)\n", err, touchbuf, ts->ts_base, ts->bus_addr);
223 +
224 + err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF,
225 + &touchbuf, sizeof(touchbuf));
226 + if (err) {
227 + dev_err(dev, "Failed to get touch buffer\n");
228 + goto out;
229 + }
230 +
231 + if (!touchbuf) {
232 + dev_err(dev, "Touchscreen not detected\n");
233 + err = -ENODEV;
234 + goto out;
235 + }
236 +
237 + dev_dbg(dev, "Got TS buffer 0x%x\n", touchbuf);
238 +
239 + // mmap the physical memory
240 + touchbuf &= ~0xc0000000;
241 + ts->ts_base = ioremap(touchbuf, sizeof(struct ft5406_regs));
242 + if (ts->ts_base == NULL)
243 + {
244 + dev_err(dev, "Failed to map physical address\n");
245 + err = -ENOMEM;
246 + goto out;
247 + }
248 + }
249 + platform_set_drvdata(pdev, ts);
250 + ts->pdev = pdev;
251 +
252 + ts->input_dev->name = "FT5406 memory based driver";
253 +
254 + __set_bit(EV_KEY, ts->input_dev->evbit);
255 + __set_bit(EV_SYN, ts->input_dev->evbit);
256 + __set_bit(EV_ABS, ts->input_dev->evbit);
257 +
258 + input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0,
259 + SCREEN_WIDTH, 0, 0);
260 + input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0,
261 + SCREEN_HEIGHT, 0, 0);
262 +
263 + input_mt_init_slots(ts->input_dev, MAXIMUM_SUPPORTED_POINTS, INPUT_MT_DIRECT);
264 +
265 + input_set_drvdata(ts->input_dev, ts);
266 +
267 + err = input_register_device(ts->input_dev);
268 + if (err) {
269 + dev_err(dev, "could not register input device, %d\n",
270 + err);
271 + goto out;
272 + }
273 +
274 + // create thread to poll the touch events
275 + ts->thread = kthread_run(ft5406_thread, ts, "ft5406");
276 + if(ts->thread == NULL)
277 + {
278 + dev_err(dev, "Failed to create kernel thread");
279 + err = -ENOMEM;
280 + goto out;
281 + }
282 +
283 + return 0;
284 +
285 +out:
286 + if (ts->bus_addr) {
287 + dma_free_coherent(dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);
288 + ts->bus_addr = 0;
289 + ts->ts_base = NULL;
290 + } else if (ts->ts_base) {
291 + iounmap(ts->ts_base);
292 + ts->ts_base = NULL;
293 + }
294 + if (ts->input_dev) {
295 + input_unregister_device(ts->input_dev);
296 + ts->input_dev = NULL;
297 + }
298 + return err;
299 +}
300 +
301 +static int ft5406_remove(struct platform_device *pdev)
302 +{
303 + struct device *dev = &pdev->dev;
304 + struct ft5406 *ts = (struct ft5406 *) platform_get_drvdata(pdev);
305 +
306 + dev_info(dev, "Removing rpi-ft5406\n");
307 +
308 + kthread_stop(ts->thread);
309 +
310 + if (ts->bus_addr)
311 + dma_free_coherent(dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);
312 + else if (ts->ts_base)
313 + iounmap(ts->ts_base);
314 + if (ts->input_dev)
315 + input_unregister_device(ts->input_dev);
316 +
317 + return 0;
318 +}
319 +
320 +static const struct of_device_id ft5406_match[] = {
321 + { .compatible = "rpi,rpi-ft5406", },
322 + {},
323 +};
324 +MODULE_DEVICE_TABLE(of, ft5406_match);
325 +
326 +static struct platform_driver ft5406_driver = {
327 + .driver = {
328 + .name = "rpi-ft5406",
329 + .owner = THIS_MODULE,
330 + .of_match_table = ft5406_match,
331 + },
332 + .probe = ft5406_probe,
333 + .remove = ft5406_remove,
334 +};
335 +
336 +module_platform_driver(ft5406_driver);
337 +
338 +MODULE_AUTHOR("Gordon Hollingworth");
339 +MODULE_DESCRIPTION("Touchscreen driver for memory based FT5406");
340 +MODULE_LICENSE("GPL");