1 From 156ce0fa550bbcb8e63eb60d11ac05cc28c14775 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 069/121] rpi-ft5406: Add touchscreen driver for pi LCD display
7 drivers/input/touchscreen/Kconfig | 7 +
8 drivers/input/touchscreen/Makefile | 1 +
9 drivers/input/touchscreen/rpi-ft5406.c | 258 ++++++++++++++++++++++++++
10 include/linux/platform_data/mailbox-bcm2708.h | 1 +
11 4 files changed, 267 insertions(+)
12 create mode 100644 drivers/input/touchscreen/rpi-ft5406.c
14 --- a/drivers/input/touchscreen/Kconfig
15 +++ b/drivers/input/touchscreen/Kconfig
16 @@ -583,6 +583,13 @@ config TOUCHSCREEN_EDT_FT5X06
17 To compile this driver as a module, choose M here: the
18 module will be called edt-ft5x06.
20 +config TOUCHSCREEN_RPI_FT5406
21 + tristate "Raspberry Pi FT5406 driver"
22 + depends on ARCH_BCM2708 || ARCH_BCM2709
24 + Say Y here to enable the Raspberry Pi memory based FT5406 device
27 config TOUCHSCREEN_MIGOR
28 tristate "Renesas MIGO-R touchscreen"
29 depends on SH_MIGOR && I2C
30 --- a/drivers/input/touchscreen/Makefile
31 +++ b/drivers/input/touchscreen/Makefile
32 @@ -29,6 +29,7 @@ obj-$(CONFIG_TOUCHSCREEN_DA9034) += da90
33 obj-$(CONFIG_TOUCHSCREEN_DA9052) += da9052_tsi.o
34 obj-$(CONFIG_TOUCHSCREEN_DYNAPRO) += dynapro.o
35 obj-$(CONFIG_TOUCHSCREEN_EDT_FT5X06) += edt-ft5x06.o
36 +obj-$(CONFIG_TOUCHSCREEN_RPI_FT5406) += rpi-ft5406.o
37 obj-$(CONFIG_TOUCHSCREEN_HAMPSHIRE) += hampshire.o
38 obj-$(CONFIG_TOUCHSCREEN_GUNZE) += gunze.o
39 obj-$(CONFIG_TOUCHSCREEN_EETI) += eeti_ts.o
41 +++ b/drivers/input/touchscreen/rpi-ft5406.c
44 + * Driver for memory based ft5406 touchscreen
46 + * Copyright (C) 2015 Raspberry Pi
49 + * This program is free software; you can redistribute it and/or modify
50 + * it under the terms of the GNU General Public License version 2 as
51 + * published by the Free Software Foundation.
55 +#include <linux/module.h>
56 +#include <linux/interrupt.h>
57 +#include <linux/input.h>
58 +#include <linux/irq.h>
59 +#include <linux/delay.h>
60 +#include <linux/slab.h>
61 +#include <linux/bitops.h>
62 +#include <linux/input/mt.h>
63 +#include <linux/kthread.h>
64 +#include <linux/platform_device.h>
66 +#include <linux/platform_data/mailbox-bcm2708.h>
68 +#define MAXIMUM_SUPPORTED_POINTS 10
70 + uint8_t device_mode;
73 + struct ft5406_touch {
80 + } point[MAXIMUM_SUPPORTED_POINTS];
83 +#define SCREEN_WIDTH 800
84 +#define SCREEN_HEIGHT 480
87 + struct platform_device * pdev;
88 + struct input_dev * input_dev;
89 + void __iomem * ts_base;
90 + struct ft5406_regs * regs;
91 + struct task_struct * thread;
95 +/* tag part of the message */
97 + uint32_t tag_id; /* the message id */
98 + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
99 + uint32_t data_size; /* amount of data being sent or received */
100 + uint32_t val; /* data buffer */
103 +/* message structure to be sent to videocore */
105 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
106 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
107 + struct vc_msg_tag tag; /* the tag structure above to make */
108 + uint32_t end_tag; /* an end identifier, should be set to NULL */
111 +/* Thread to poll for touchscreen events
113 + * This thread polls the memory based register copy of the ft5406 registers
114 + * using the number of points register to know whether the copy has been
115 + * updated (we write 99 to the memory copy, the GPU will write between
118 +static int ft5406_thread(void *arg)
120 + struct ft5406 *ts = (struct ft5406 *) arg;
121 + struct ft5406_regs regs;
124 + while(!kthread_should_stop())
128 + memcpy_fromio(®s, ts->regs, sizeof(*ts->regs));
129 + writel(99, &ts->regs->num_points);
130 + // Do not output if theres no new information (num_points is 99)
131 + // or we have no touch points and don't need to release any
132 + if(!(regs.num_points == 99 || (regs.num_points == 0 && known_ids == 0)))
135 + int modified_ids = 0, released_ids;
136 + for(i = 0; i < regs.num_points; i++)
138 + int x = (((int) regs.point[i].xh & 0xf) << 8) + regs.point[i].xl;
139 + int y = (((int) regs.point[i].yh & 0xf) << 8) + regs.point[i].yl;
140 + int touchid = (regs.point[i].yh >> 4) & 0xf;
142 + modified_ids |= 1 << touchid;
144 + if(!((1 << touchid) & known_ids))
145 + dev_dbg(&ts->pdev->dev, "x = %d, y = %d, touchid = %d\n", x, y, touchid);
147 + input_mt_slot(ts->input_dev, touchid);
148 + input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 1);
150 + input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
151 + input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
155 + released_ids = known_ids & ~modified_ids;
156 + for(i = 0; released_ids && i < MAXIMUM_SUPPORTED_POINTS; i++)
158 + if(released_ids & (1<<i))
160 + dev_dbg(&ts->pdev->dev, "Released %d, known = %x modified = %x\n", i, known_ids, modified_ids);
161 + input_mt_slot(ts->input_dev, i);
162 + input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 0);
163 + modified_ids &= ~(1 << i);
166 + known_ids = modified_ids;
168 + input_mt_report_pointer_emulation(ts->input_dev, true);
169 + input_sync(ts->input_dev);
177 +static int ft5406_probe(struct platform_device *pdev)
180 + struct input_dev * input_dev = input_allocate_device();
181 + struct vc_msg request;
182 + struct ft5406 * ts;
184 + dev_info(&pdev->dev, "Probing device\n");
186 + ts = kzalloc(sizeof(struct ft5406), GFP_KERNEL);
188 + if (!ts || !input_dev) {
190 + dev_err(&pdev->dev, "Failed to allocate memory\n");
193 + ts->input_dev = input_dev;
194 + platform_set_drvdata(pdev, ts);
197 + input_dev->name = "FT5406 memory based driver";
199 + __set_bit(EV_KEY, input_dev->evbit);
200 + __set_bit(EV_SYN, input_dev->evbit);
201 + __set_bit(EV_ABS, input_dev->evbit);
203 + input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
204 + SCREEN_WIDTH, 0, 0);
205 + input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
206 + SCREEN_HEIGHT, 0, 0);
208 + input_mt_init_slots(input_dev, MAXIMUM_SUPPORTED_POINTS, INPUT_MT_DIRECT);
210 + input_set_drvdata(input_dev, ts);
212 + ret = input_register_device(input_dev);
214 + dev_err(&pdev->dev, "could not register input device, %d\n",
219 + memset(&request, 0, sizeof request);
221 + request.msg_size = sizeof request;
222 + request.request_code = VCMSG_PROCESS_REQUEST;
223 + request.tag.tag_id = VCMSG_GET_TOUCHBUF;
224 + request.tag.buffer_size = 4;
225 + request.tag.data_size = 4;
227 + bcm_mailbox_property(&request, sizeof(request));
229 + if(request.request_code == VCMSG_REQUEST_SUCCESSFUL)
231 + dev_dbg(&pdev->dev, "Got TS buffer 0x%x\n", request.tag.val);
235 + input_unregister_device(input_dev);
240 + // mmap the physical memory
241 + request.tag.val &= ~0xc0000000;
242 + ts->ts_base = ioremap(request.tag.val, sizeof(*ts->regs));
243 + if(ts->ts_base == NULL)
245 + dev_err(&pdev->dev, "Failed to map physical address\n");
246 + input_unregister_device(input_dev);
251 + ts->regs = (struct ft5406_regs *) ts->ts_base;
253 + // create thread to poll the touch events
254 + ts->thread = kthread_run(ft5406_thread, ts, "ft5406");
255 + if(ts->thread == NULL)
257 + dev_err(&pdev->dev, "Failed to create kernel thread");
258 + iounmap(ts->ts_base);
259 + input_unregister_device(input_dev);
266 +static int ft5406_remove(struct platform_device *pdev)
268 + struct ft5406 *ts = (struct ft5406 *) platform_get_drvdata(pdev);
270 + dev_info(&pdev->dev, "Removing rpi-ft5406\n");
272 + kthread_stop(ts->thread);
273 + iounmap(ts->ts_base);
274 + input_unregister_device(ts->input_dev);
280 +static const struct of_device_id ft5406_match[] = {
281 + { .compatible = "rpi,rpi-ft5406", },
284 +MODULE_DEVICE_TABLE(of, ft5406_match);
286 +static struct platform_driver ft5406_driver = {
288 + .name = "rpi-ft5406",
289 + .owner = THIS_MODULE,
290 + .of_match_table = ft5406_match,
292 + .probe = ft5406_probe,
293 + .remove = ft5406_remove,
296 +module_platform_driver(ft5406_driver);
298 +MODULE_AUTHOR("Gordon Hollingworth");
299 +MODULE_DESCRIPTION("Touchscreen driver for memory based FT5406");
300 +MODULE_LICENSE("GPL");
301 --- a/include/linux/platform_data/mailbox-bcm2708.h
302 +++ b/include/linux/platform_data/mailbox-bcm2708.h
303 @@ -115,6 +115,7 @@ enum {
304 VCMSG_SET_TRANSFORM = 0x0004800d,
305 VCMSG_TST_VSYNC = 0x0004400e,
306 VCMSG_SET_VSYNC = 0x0004800e,
307 + VCMSG_GET_TOUCHBUF = 0x0004000f,
308 VCMSG_SET_CURSOR_INFO = 0x00008010,
309 VCMSG_SET_CURSOR_STATE = 0x00008011,