brcm2708: add linux 4.1 support
[openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0069-rpi-ft5406-Add-touchscreen-driver-for-pi-LCD-display.patch
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
5
6 ---
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
13
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.
19  
20 +config TOUCHSCREEN_RPI_FT5406
21 +       tristate "Raspberry Pi FT5406 driver"
22 +       depends on ARCH_BCM2708 || ARCH_BCM2709
23 +       help
24 +         Say Y here to enable the Raspberry Pi memory based FT5406 device
25 +
26 +
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
40 --- /dev/null
41 +++ b/drivers/input/touchscreen/rpi-ft5406.c
42 @@ -0,0 +1,258 @@
43 +/*
44 + * Driver for memory based ft5406 touchscreen
45 + *
46 + * Copyright (C) 2015 Raspberry Pi
47 + *
48 + *
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.
52 + */
53 +
54 +
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>
65 +#include <asm/io.h>
66 +#include <linux/platform_data/mailbox-bcm2708.h>
67 +
68 +#define MAXIMUM_SUPPORTED_POINTS 10
69 +struct ft5406_regs {
70 +       uint8_t device_mode;
71 +       uint8_t gesture_id;
72 +       uint8_t num_points;
73 +       struct ft5406_touch {
74 +               uint8_t xh;
75 +               uint8_t xl;
76 +               uint8_t yh;
77 +               uint8_t yl;
78 +               uint8_t res1;
79 +               uint8_t res2;
80 +       } point[MAXIMUM_SUPPORTED_POINTS];
81 +};
82 +
83 +#define SCREEN_WIDTH  800
84 +#define SCREEN_HEIGHT 480
85 +
86 +struct ft5406 {
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;
92 +};
93 +
94 +
95 +/* tag part of the message */
96 +struct vc_msg_tag {
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 */
101 +};
102 +
103 +/* message structure to be sent to videocore */
104 +struct vc_msg {
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 */
109 +};
110 +
111 +/* Thread to poll for touchscreen events
112 + * 
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 
116 + * 0 - 10 points)
117 + */
118 +static int ft5406_thread(void *arg)
119 +{
120 +       struct ft5406 *ts = (struct ft5406 *) arg;
121 +       struct ft5406_regs regs;
122 +       int known_ids = 0;
123 +       
124 +       while(!kthread_should_stop())
125 +       {
126 +               // 60fps polling
127 +               msleep(17);
128 +               memcpy_fromio(&regs, 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)))
133 +               {
134 +                       int i;
135 +                       int modified_ids = 0, released_ids;
136 +                       for(i = 0; i < regs.num_points; i++)
137 +                       {
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;
141 +                               
142 +                               modified_ids |= 1 << touchid;
143 +
144 +                               if(!((1 << touchid) & known_ids))
145 +                                       dev_dbg(&ts->pdev->dev, "x = %d, y = %d, touchid = %d\n", x, y, touchid);
146 +                               
147 +                               input_mt_slot(ts->input_dev, touchid);
148 +                               input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 1);
149 +
150 +                               input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
151 +                               input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
152 +
153 +                       }
154 +
155 +                       released_ids = known_ids & ~modified_ids;
156 +                       for(i = 0; released_ids && i < MAXIMUM_SUPPORTED_POINTS; i++)
157 +                       {
158 +                               if(released_ids & (1<<i))
159 +                               {
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);
164 +                               }
165 +                       }
166 +                       known_ids = modified_ids;
167 +                       
168 +                       input_mt_report_pointer_emulation(ts->input_dev, true);
169 +                       input_sync(ts->input_dev);
170 +               }
171 +                       
172 +       }
173 +       
174 +       return 0;
175 +}
176 +
177 +static int ft5406_probe(struct platform_device *pdev)
178 +{
179 +       int ret;
180 +       struct input_dev * input_dev = input_allocate_device();
181 +       struct vc_msg request;
182 +       struct ft5406 * ts;
183 +       
184 +       dev_info(&pdev->dev, "Probing device\n");
185 +       
186 +       ts = kzalloc(sizeof(struct ft5406), GFP_KERNEL);
187 +
188 +       if (!ts || !input_dev) {
189 +               ret = -ENOMEM;
190 +               dev_err(&pdev->dev, "Failed to allocate memory\n");
191 +               return ret;
192 +       }
193 +       ts->input_dev = input_dev;
194 +       platform_set_drvdata(pdev, ts);
195 +       ts->pdev = pdev;
196 +       
197 +       input_dev->name = "FT5406 memory based driver";
198 +       
199 +       __set_bit(EV_KEY, input_dev->evbit);
200 +       __set_bit(EV_SYN, input_dev->evbit);
201 +       __set_bit(EV_ABS, input_dev->evbit);
202 +
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);
207 +
208 +       input_mt_init_slots(input_dev, MAXIMUM_SUPPORTED_POINTS, INPUT_MT_DIRECT);
209 +
210 +       input_set_drvdata(input_dev, ts);
211 +       
212 +       ret = input_register_device(input_dev);
213 +       if (ret) {
214 +               dev_err(&pdev->dev, "could not register input device, %d\n",
215 +                       ret);
216 +               return ret;
217 +       }
218 +       
219 +       memset(&request, 0, sizeof request);
220 +
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;
226 +       
227 +       bcm_mailbox_property(&request, sizeof(request));
228 +       
229 +       if(request.request_code == VCMSG_REQUEST_SUCCESSFUL)
230 +       {
231 +               dev_dbg(&pdev->dev, "Got TS buffer 0x%x\n", request.tag.val);
232 +       }
233 +       else
234 +       {
235 +               input_unregister_device(input_dev);
236 +               kzfree(ts);
237 +               return -1;
238 +       }
239 +       
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)
244 +       {
245 +               dev_err(&pdev->dev, "Failed to map physical address\n");
246 +               input_unregister_device(input_dev);
247 +               kzfree(ts);
248 +               return -1;      
249 +       }
250 +       
251 +       ts->regs = (struct ft5406_regs *) ts->ts_base;
252 +
253 +       // create thread to poll the touch events
254 +       ts->thread = kthread_run(ft5406_thread, ts, "ft5406");
255 +       if(ts->thread == NULL)
256 +       {
257 +               dev_err(&pdev->dev, "Failed to create kernel thread");
258 +               iounmap(ts->ts_base);
259 +               input_unregister_device(input_dev);
260 +               kzfree(ts);
261 +       }
262 +
263 +       return 0;
264 +}
265 +
266 +static int ft5406_remove(struct platform_device *pdev)
267 +{
268 +       struct ft5406 *ts = (struct ft5406 *) platform_get_drvdata(pdev);
269 +       
270 +       dev_info(&pdev->dev, "Removing rpi-ft5406\n");
271 +       
272 +       kthread_stop(ts->thread);
273 +       iounmap(ts->ts_base);
274 +       input_unregister_device(ts->input_dev);
275 +       kzfree(ts);
276 +       
277 +       return 0;
278 +}
279 +
280 +static const struct of_device_id ft5406_match[] = {
281 +       { .compatible = "rpi,rpi-ft5406", },
282 +       {},
283 +};
284 +MODULE_DEVICE_TABLE(of, ft5406_match);
285 +
286 +static struct platform_driver ft5406_driver = {
287 +       .driver = {
288 +               .name   = "rpi-ft5406",
289 +               .owner  = THIS_MODULE,
290 +               .of_match_table = ft5406_match,
291 +       },
292 +       .probe          = ft5406_probe,
293 +       .remove         = ft5406_remove,
294 +};
295 +
296 +module_platform_driver(ft5406_driver);
297 +
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,
310  };