1 From 8137201823bd2ed1b3951220f29fdc9555c3c6de 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
6 Fix driver detection failure Check that the buffer response is non-zero meaning the touchscreen was detected
8 rpi-ft5406: Use firmware API
10 RPI-FT5406: Enable aarch64 support through explicit iomem interface
12 Signed-off-by: Gerhard de Clercq <gerharddeclercq@outlook.com>
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
20 --- a/drivers/input/touchscreen/Kconfig
21 +++ b/drivers/input/touchscreen/Kconfig
22 @@ -696,6 +696,13 @@ config TOUCHSCREEN_EDT_FT5X06
23 To compile this driver as a module, choose M here: the
24 module will be called edt-ft5x06.
26 +config TOUCHSCREEN_RPI_FT5406
27 + tristate "Raspberry Pi FT5406 driver"
28 + depends on RASPBERRYPI_FIRMWARE
30 + Say Y here to enable the Raspberry Pi memory based FT5406 device
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 @@ -33,6 +33,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
47 +++ b/drivers/input/touchscreen/rpi-ft5406.c
50 + * Driver for memory based ft5406 touchscreen
52 + * Copyright (C) 2015 Raspberry Pi
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.
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>
73 +#include <linux/dma-mapping.h>
74 +#include <soc/bcm2835/raspberrypi-firmware.h>
76 +#define MAXIMUM_SUPPORTED_POINTS 10
78 + uint8_t device_mode;
81 + struct ft5406_touch {
88 + } point[MAXIMUM_SUPPORTED_POINTS];
91 +#define SCREEN_WIDTH 800
92 +#define SCREEN_HEIGHT 480
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;
102 +/* Thread to poll for touchscreen events
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
109 +static int ft5406_thread(void *arg)
111 + struct ft5406 *ts = (struct ft5406 *) arg;
112 + struct ft5406_regs regs;
115 + while(!kthread_should_stop())
118 + msleep_interruptible(17);
119 + memcpy_fromio(®s, 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)))
126 + int modified_ids = 0, released_ids;
127 + for(i = 0; i < regs.num_points; i++)
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;
133 + modified_ids |= 1 << touchid;
135 + if(!((1 << touchid) & known_ids))
136 + dev_dbg(&ts->pdev->dev, "x = %d, y = %d, touchid = %d\n", x, y, touchid);
138 + input_mt_slot(ts->input_dev, touchid);
139 + input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 1);
141 + input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
142 + input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
146 + released_ids = known_ids & ~modified_ids;
147 + for(i = 0; released_ids && i < MAXIMUM_SUPPORTED_POINTS; i++)
149 + if(released_ids & (1<<i))
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);
157 + known_ids = modified_ids;
159 + input_mt_report_pointer_emulation(ts->input_dev, true);
160 + input_sync(ts->input_dev);
168 +static int ft5406_probe(struct platform_device *pdev)
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;
178 + dev_info(dev, "Probing device\n");
180 + fw_node = of_parse_phandle(np, "firmware", 0);
182 + dev_err(dev, "Missing firmware node\n");
186 + fw = rpi_firmware_get(fw_node);
188 + return -EPROBE_DEFER;
190 + ts = devm_kzalloc(dev, sizeof(struct ft5406), GFP_KERNEL);
192 + dev_err(dev, "Failed to allocate memory\n");
196 + ts->input_dev = input_allocate_device();
197 + if (!ts->input_dev) {
198 + dev_err(dev, "Failed to allocate input device\n");
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);
210 + touchbuf = (u32)ts->bus_addr;
211 + err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
212 + &touchbuf, sizeof(touchbuf));
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);
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);
224 + err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF,
225 + &touchbuf, sizeof(touchbuf));
227 + dev_err(dev, "Failed to get touch buffer\n");
232 + dev_err(dev, "Touchscreen not detected\n");
237 + dev_dbg(dev, "Got TS buffer 0x%x\n", touchbuf);
239 + // mmap the physical memory
240 + touchbuf &= ~0xc0000000;
241 + ts->ts_base = ioremap(touchbuf, sizeof(struct ft5406_regs));
242 + if (ts->ts_base == NULL)
244 + dev_err(dev, "Failed to map physical address\n");
249 + platform_set_drvdata(pdev, ts);
252 + ts->input_dev->name = "FT5406 memory based driver";
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);
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);
263 + input_mt_init_slots(ts->input_dev, MAXIMUM_SUPPORTED_POINTS, INPUT_MT_DIRECT);
265 + input_set_drvdata(ts->input_dev, ts);
267 + err = input_register_device(ts->input_dev);
269 + dev_err(dev, "could not register input device, %d\n",
274 + // create thread to poll the touch events
275 + ts->thread = kthread_run(ft5406_thread, ts, "ft5406");
276 + if(ts->thread == NULL)
278 + dev_err(dev, "Failed to create kernel thread");
286 + if (ts->bus_addr) {
287 + dma_free_coherent(dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);
289 + ts->ts_base = NULL;
290 + } else if (ts->ts_base) {
291 + iounmap(ts->ts_base);
292 + ts->ts_base = NULL;
294 + if (ts->input_dev) {
295 + input_unregister_device(ts->input_dev);
296 + ts->input_dev = NULL;
301 +static int ft5406_remove(struct platform_device *pdev)
303 + struct device *dev = &pdev->dev;
304 + struct ft5406 *ts = (struct ft5406 *) platform_get_drvdata(pdev);
306 + dev_info(dev, "Removing rpi-ft5406\n");
308 + kthread_stop(ts->thread);
311 + dma_free_coherent(dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);
312 + else if (ts->ts_base)
313 + iounmap(ts->ts_base);
315 + input_unregister_device(ts->input_dev);
320 +static const struct of_device_id ft5406_match[] = {
321 + { .compatible = "rpi,rpi-ft5406", },
324 +MODULE_DEVICE_TABLE(of, ft5406_match);
326 +static struct platform_driver ft5406_driver = {
328 + .name = "rpi-ft5406",
329 + .owner = THIS_MODULE,
330 + .of_match_table = ft5406_match,
332 + .probe = ft5406_probe,
333 + .remove = ft5406_remove,
336 +module_platform_driver(ft5406_driver);
338 +MODULE_AUTHOR("Gordon Hollingworth");
339 +MODULE_DESCRIPTION("Touchscreen driver for memory based FT5406");
340 +MODULE_LICENSE("GPL");