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