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