Skip to content

Commit 94bbfc4

Browse files
ghollingworthpopcornmix
authored andcommitted
rpi-ft5406: Add touchscreen driver for pi LCD display
1 parent f865598 commit 94bbfc4

File tree

4 files changed

+267
-0
lines changed

4 files changed

+267
-0
lines changed

drivers/input/touchscreen/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,13 @@ config TOUCHSCREEN_EDT_FT5X06
557557
To compile this driver as a module, choose M here: the
558558
module will be called edt-ft5x06.
559559

560+
config TOUCHSCREEN_RPI_FT5406
561+
tristate "Raspberry Pi FT5406 driver"
562+
depends on ARCH_BCM2708 || ARCH_BCM2709
563+
help
564+
Say Y here to enable the Raspberry Pi memory based FT5406 device
565+
566+
560567
config TOUCHSCREEN_MIGOR
561568
tristate "Renesas MIGO-R touchscreen"
562569
depends on SH_MIGOR && I2C

drivers/input/touchscreen/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ obj-$(CONFIG_TOUCHSCREEN_DA9034) += da9034-ts.o
2828
obj-$(CONFIG_TOUCHSCREEN_DA9052) += da9052_tsi.o
2929
obj-$(CONFIG_TOUCHSCREEN_DYNAPRO) += dynapro.o
3030
obj-$(CONFIG_TOUCHSCREEN_EDT_FT5X06) += edt-ft5x06.o
31+
obj-$(CONFIG_TOUCHSCREEN_RPI_FT5406) += rpi-ft5406.o
3132
obj-$(CONFIG_TOUCHSCREEN_HAMPSHIRE) += hampshire.o
3233
obj-$(CONFIG_TOUCHSCREEN_GUNZE) += gunze.o
3334
obj-$(CONFIG_TOUCHSCREEN_EETI) += eeti_ts.o
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
/*
2+
* Driver for memory based ft5406 touchscreen
3+
*
4+
* Copyright (C) 2015 Raspberry Pi
5+
*
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License version 2 as
9+
* published by the Free Software Foundation.
10+
*/
11+
12+
13+
#include <linux/module.h>
14+
#include <linux/interrupt.h>
15+
#include <linux/input.h>
16+
#include <linux/irq.h>
17+
#include <linux/delay.h>
18+
#include <linux/slab.h>
19+
#include <linux/bitops.h>
20+
#include <linux/input/mt.h>
21+
#include <linux/kthread.h>
22+
#include <linux/platform_device.h>
23+
#include <asm/io.h>
24+
#include <linux/platform_data/mailbox-bcm2708.h>
25+
26+
#define MAXIMUM_SUPPORTED_POINTS 10
27+
struct ft5406_regs {
28+
uint8_t device_mode;
29+
uint8_t gesture_id;
30+
uint8_t num_points;
31+
struct ft5406_touch {
32+
uint8_t xh;
33+
uint8_t xl;
34+
uint8_t yh;
35+
uint8_t yl;
36+
uint8_t res1;
37+
uint8_t res2;
38+
} point[MAXIMUM_SUPPORTED_POINTS];
39+
};
40+
41+
#define SCREEN_WIDTH 800
42+
#define SCREEN_HEIGHT 480
43+
44+
struct ft5406 {
45+
struct platform_device * pdev;
46+
struct input_dev * input_dev;
47+
void __iomem * ts_base;
48+
struct ft5406_regs * regs;
49+
struct task_struct * thread;
50+
};
51+
52+
53+
/* tag part of the message */
54+
struct vc_msg_tag {
55+
uint32_t tag_id; /* the message id */
56+
uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
57+
uint32_t data_size; /* amount of data being sent or received */
58+
uint32_t val; /* data buffer */
59+
};
60+
61+
/* message structure to be sent to videocore */
62+
struct vc_msg {
63+
uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
64+
uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
65+
struct vc_msg_tag tag; /* the tag structure above to make */
66+
uint32_t end_tag; /* an end identifier, should be set to NULL */
67+
};
68+
69+
/* Thread to poll for touchscreen events
70+
*
71+
* This thread polls the memory based register copy of the ft5406 registers
72+
* using the number of points register to know whether the copy has been
73+
* updated (we write 99 to the memory copy, the GPU will write between
74+
* 0 - 10 points)
75+
*/
76+
static int ft5406_thread(void *arg)
77+
{
78+
struct ft5406 *ts = (struct ft5406 *) arg;
79+
struct ft5406_regs regs;
80+
int known_ids = 0;
81+
82+
while(!kthread_should_stop())
83+
{
84+
// 60fps polling
85+
msleep(17);
86+
memcpy_fromio(&regs, ts->regs, sizeof(*ts->regs));
87+
writel(99, &ts->regs->num_points);
88+
// Do not output if theres no new information (num_points is 99)
89+
// or we have no touch points and don't need to release any
90+
if(!(regs.num_points == 99 || (regs.num_points == 0 && known_ids == 0)))
91+
{
92+
int i;
93+
int modified_ids = 0, released_ids;
94+
for(i = 0; i < regs.num_points; i++)
95+
{
96+
int x = (((int) regs.point[i].xh & 0xf) << 8) + regs.point[i].xl;
97+
int y = (((int) regs.point[i].yh & 0xf) << 8) + regs.point[i].yl;
98+
int touchid = (regs.point[i].yh >> 4) & 0xf;
99+
100+
modified_ids |= 1 << touchid;
101+
102+
if(!((1 << touchid) & known_ids))
103+
dev_dbg(&ts->pdev->dev, "x = %d, y = %d, touchid = %d\n", x, y, touchid);
104+
105+
input_mt_slot(ts->input_dev, touchid);
106+
input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 1);
107+
108+
input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
109+
input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
110+
111+
}
112+
113+
released_ids = known_ids & ~modified_ids;
114+
for(i = 0; released_ids && i < MAXIMUM_SUPPORTED_POINTS; i++)
115+
{
116+
if(released_ids & (1<<i))
117+
{
118+
dev_dbg(&ts->pdev->dev, "Released %d, known = %x modified = %x\n", i, known_ids, modified_ids);
119+
input_mt_slot(ts->input_dev, i);
120+
input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 0);
121+
modified_ids &= ~(1 << i);
122+
}
123+
}
124+
known_ids = modified_ids;
125+
126+
input_mt_report_pointer_emulation(ts->input_dev, true);
127+
input_sync(ts->input_dev);
128+
}
129+
130+
}
131+
132+
return 0;
133+
}
134+
135+
static int ft5406_probe(struct platform_device *pdev)
136+
{
137+
int ret;
138+
struct input_dev * input_dev = input_allocate_device();
139+
struct vc_msg request;
140+
struct ft5406 * ts;
141+
142+
dev_info(&pdev->dev, "Probing device\n");
143+
144+
ts = kzalloc(sizeof(struct ft5406), GFP_KERNEL);
145+
146+
if (!ts || !input_dev) {
147+
ret = -ENOMEM;
148+
dev_err(&pdev->dev, "Failed to allocate memory\n");
149+
return ret;
150+
}
151+
ts->input_dev = input_dev;
152+
platform_set_drvdata(pdev, ts);
153+
ts->pdev = pdev;
154+
155+
input_dev->name = "FT5406 memory based driver";
156+
157+
__set_bit(EV_KEY, input_dev->evbit);
158+
__set_bit(EV_SYN, input_dev->evbit);
159+
__set_bit(EV_ABS, input_dev->evbit);
160+
161+
input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
162+
SCREEN_WIDTH, 0, 0);
163+
input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
164+
SCREEN_HEIGHT, 0, 0);
165+
166+
input_mt_init_slots(input_dev, MAXIMUM_SUPPORTED_POINTS, INPUT_MT_DIRECT);
167+
168+
input_set_drvdata(input_dev, ts);
169+
170+
ret = input_register_device(input_dev);
171+
if (ret) {
172+
dev_err(&pdev->dev, "could not register input device, %d\n",
173+
ret);
174+
return ret;
175+
}
176+
177+
memset(&request, 0, sizeof request);
178+
179+
request.msg_size = sizeof request;
180+
request.request_code = VCMSG_PROCESS_REQUEST;
181+
request.tag.tag_id = VCMSG_GET_TOUCHBUF;
182+
request.tag.buffer_size = 4;
183+
request.tag.data_size = 4;
184+
185+
bcm_mailbox_property(&request, sizeof(request));
186+
187+
if(request.request_code == VCMSG_REQUEST_SUCCESSFUL)
188+
{
189+
dev_dbg(&pdev->dev, "Got TS buffer 0x%x\n", request.tag.val);
190+
}
191+
else
192+
{
193+
input_unregister_device(input_dev);
194+
kzfree(ts);
195+
return -1;
196+
}
197+
198+
// mmap the physical memory
199+
request.tag.val &= ~0xc0000000;
200+
ts->ts_base = ioremap(request.tag.val, sizeof(*ts->regs));
201+
if(ts->ts_base == NULL)
202+
{
203+
dev_err(&pdev->dev, "Failed to map physical address\n");
204+
input_unregister_device(input_dev);
205+
kzfree(ts);
206+
return -1;
207+
}
208+
209+
ts->regs = (struct ft5406_regs *) ts->ts_base;
210+
211+
// create thread to poll the touch events
212+
ts->thread = kthread_run(ft5406_thread, ts, "ft5406");
213+
if(ts->thread == NULL)
214+
{
215+
dev_err(&pdev->dev, "Failed to create kernel thread");
216+
iounmap(ts->ts_base);
217+
input_unregister_device(input_dev);
218+
kzfree(ts);
219+
}
220+
221+
return 0;
222+
}
223+
224+
static int ft5406_remove(struct platform_device *pdev)
225+
{
226+
struct ft5406 *ts = (struct ft5406 *) platform_get_drvdata(pdev);
227+
228+
dev_info(&pdev->dev, "Removing rpi-ft5406\n");
229+
230+
kthread_stop(ts->thread);
231+
iounmap(ts->ts_base);
232+
input_unregister_device(ts->input_dev);
233+
kzfree(ts);
234+
235+
return 0;
236+
}
237+
238+
static const struct of_device_id ft5406_match[] = {
239+
{ .compatible = "rpi,rpi-ft5406", },
240+
{},
241+
};
242+
MODULE_DEVICE_TABLE(of, ft5406_match);
243+
244+
static struct platform_driver ft5406_driver = {
245+
.driver = {
246+
.name = "rpi-ft5406",
247+
.owner = THIS_MODULE,
248+
.of_match_table = ft5406_match,
249+
},
250+
.probe = ft5406_probe,
251+
.remove = ft5406_remove,
252+
};
253+
254+
module_platform_driver(ft5406_driver);
255+
256+
MODULE_AUTHOR("Gordon Hollingworth");
257+
MODULE_DESCRIPTION("Touchscreen driver for memory based FT5406");
258+
MODULE_LICENSE("GPL");

include/linux/platform_data/mailbox-bcm2708.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ enum {
115115
VCMSG_SET_TRANSFORM = 0x0004800d,
116116
VCMSG_TST_VSYNC = 0x0004400e,
117117
VCMSG_SET_VSYNC = 0x0004800e,
118+
VCMSG_GET_TOUCHBUF = 0x0004000f,
118119
VCMSG_SET_CURSOR_INFO = 0x00008010,
119120
VCMSG_SET_CURSOR_STATE = 0x00008011,
120121
};

0 commit comments

Comments
 (0)