|
| 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 <soc/bcm2835/raspberrypi-firmware.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 | +/* Thread to poll for touchscreen events |
| 53 | + * |
| 54 | + * This thread polls the memory based register copy of the ft5406 registers |
| 55 | + * using the number of points register to know whether the copy has been |
| 56 | + * updated (we write 99 to the memory copy, the GPU will write between |
| 57 | + * 0 - 10 points) |
| 58 | + */ |
| 59 | +static int ft5406_thread(void *arg) |
| 60 | +{ |
| 61 | + struct ft5406 *ts = (struct ft5406 *) arg; |
| 62 | + struct ft5406_regs regs; |
| 63 | + int known_ids = 0; |
| 64 | + |
| 65 | + while(!kthread_should_stop()) |
| 66 | + { |
| 67 | + // 60fps polling |
| 68 | + msleep_interruptible(17); |
| 69 | + memcpy_fromio(®s, ts->regs, sizeof(*ts->regs)); |
| 70 | + writel(99, &ts->regs->num_points); |
| 71 | + // Do not output if theres no new information (num_points is 99) |
| 72 | + // or we have no touch points and don't need to release any |
| 73 | + if(!(regs.num_points == 99 || (regs.num_points == 0 && known_ids == 0))) |
| 74 | + { |
| 75 | + int i; |
| 76 | + int modified_ids = 0, released_ids; |
| 77 | + for(i = 0; i < regs.num_points; i++) |
| 78 | + { |
| 79 | + int x = (((int) regs.point[i].xh & 0xf) << 8) + regs.point[i].xl; |
| 80 | + int y = (((int) regs.point[i].yh & 0xf) << 8) + regs.point[i].yl; |
| 81 | + int touchid = (regs.point[i].yh >> 4) & 0xf; |
| 82 | + |
| 83 | + modified_ids |= 1 << touchid; |
| 84 | + |
| 85 | + if(!((1 << touchid) & known_ids)) |
| 86 | + dev_dbg(&ts->pdev->dev, "x = %d, y = %d, touchid = %d\n", x, y, touchid); |
| 87 | + |
| 88 | + input_mt_slot(ts->input_dev, touchid); |
| 89 | + input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 1); |
| 90 | + |
| 91 | + input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x); |
| 92 | + input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + released_ids = known_ids & ~modified_ids; |
| 97 | + for(i = 0; released_ids && i < MAXIMUM_SUPPORTED_POINTS; i++) |
| 98 | + { |
| 99 | + if(released_ids & (1<<i)) |
| 100 | + { |
| 101 | + dev_dbg(&ts->pdev->dev, "Released %d, known = %x modified = %x\n", i, known_ids, modified_ids); |
| 102 | + input_mt_slot(ts->input_dev, i); |
| 103 | + input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 0); |
| 104 | + modified_ids &= ~(1 << i); |
| 105 | + } |
| 106 | + } |
| 107 | + known_ids = modified_ids; |
| 108 | + |
| 109 | + input_mt_report_pointer_emulation(ts->input_dev, true); |
| 110 | + input_sync(ts->input_dev); |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +static int ft5406_probe(struct platform_device *pdev) |
| 119 | +{ |
| 120 | + int ret; |
| 121 | + struct input_dev * input_dev = input_allocate_device(); |
| 122 | + struct ft5406 * ts; |
| 123 | + struct device_node *fw_node; |
| 124 | + struct rpi_firmware *fw; |
| 125 | + u32 touchbuf; |
| 126 | + |
| 127 | + dev_info(&pdev->dev, "Probing device\n"); |
| 128 | + |
| 129 | + fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0); |
| 130 | + if (!fw_node) { |
| 131 | + dev_err(&pdev->dev, "Missing firmware node\n"); |
| 132 | + return -ENOENT; |
| 133 | + } |
| 134 | + |
| 135 | + fw = rpi_firmware_get(fw_node); |
| 136 | + if (!fw) |
| 137 | + return -EPROBE_DEFER; |
| 138 | + |
| 139 | + ret = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF, |
| 140 | + &touchbuf, sizeof(touchbuf)); |
| 141 | + if (ret) { |
| 142 | + dev_err(&pdev->dev, "Failed to get touch buffer\n"); |
| 143 | + return ret; |
| 144 | + } |
| 145 | + |
| 146 | + if (!touchbuf) { |
| 147 | + dev_err(&pdev->dev, "Touchscreen not detected\n"); |
| 148 | + return -ENODEV; |
| 149 | + } |
| 150 | + |
| 151 | + dev_dbg(&pdev->dev, "Got TS buffer 0x%x\n", touchbuf); |
| 152 | + |
| 153 | + ts = kzalloc(sizeof(struct ft5406), GFP_KERNEL); |
| 154 | + |
| 155 | + if (!ts || !input_dev) { |
| 156 | + ret = -ENOMEM; |
| 157 | + dev_err(&pdev->dev, "Failed to allocate memory\n"); |
| 158 | + return ret; |
| 159 | + } |
| 160 | + ts->input_dev = input_dev; |
| 161 | + platform_set_drvdata(pdev, ts); |
| 162 | + ts->pdev = pdev; |
| 163 | + |
| 164 | + input_dev->name = "FT5406 memory based driver"; |
| 165 | + |
| 166 | + __set_bit(EV_KEY, input_dev->evbit); |
| 167 | + __set_bit(EV_SYN, input_dev->evbit); |
| 168 | + __set_bit(EV_ABS, input_dev->evbit); |
| 169 | + |
| 170 | + input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, |
| 171 | + SCREEN_WIDTH, 0, 0); |
| 172 | + input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, |
| 173 | + SCREEN_HEIGHT, 0, 0); |
| 174 | + |
| 175 | + input_mt_init_slots(input_dev, MAXIMUM_SUPPORTED_POINTS, INPUT_MT_DIRECT); |
| 176 | + |
| 177 | + input_set_drvdata(input_dev, ts); |
| 178 | + |
| 179 | + ret = input_register_device(input_dev); |
| 180 | + if (ret) { |
| 181 | + dev_err(&pdev->dev, "could not register input device, %d\n", |
| 182 | + ret); |
| 183 | + return ret; |
| 184 | + } |
| 185 | + |
| 186 | + // mmap the physical memory |
| 187 | + touchbuf &= ~0xc0000000; |
| 188 | + ts->ts_base = ioremap(touchbuf, sizeof(*ts->regs)); |
| 189 | + if(ts->ts_base == NULL) |
| 190 | + { |
| 191 | + dev_err(&pdev->dev, "Failed to map physical address\n"); |
| 192 | + input_unregister_device(input_dev); |
| 193 | + kzfree(ts); |
| 194 | + return -ENOMEM; |
| 195 | + } |
| 196 | + |
| 197 | + ts->regs = (struct ft5406_regs *) ts->ts_base; |
| 198 | + |
| 199 | + // create thread to poll the touch events |
| 200 | + ts->thread = kthread_run(ft5406_thread, ts, "ft5406"); |
| 201 | + if(ts->thread == NULL) |
| 202 | + { |
| 203 | + dev_err(&pdev->dev, "Failed to create kernel thread"); |
| 204 | + iounmap(ts->ts_base); |
| 205 | + input_unregister_device(input_dev); |
| 206 | + kzfree(ts); |
| 207 | + } |
| 208 | + |
| 209 | + return 0; |
| 210 | +} |
| 211 | + |
| 212 | +static int ft5406_remove(struct platform_device *pdev) |
| 213 | +{ |
| 214 | + struct ft5406 *ts = (struct ft5406 *) platform_get_drvdata(pdev); |
| 215 | + |
| 216 | + dev_info(&pdev->dev, "Removing rpi-ft5406\n"); |
| 217 | + |
| 218 | + kthread_stop(ts->thread); |
| 219 | + iounmap(ts->ts_base); |
| 220 | + input_unregister_device(ts->input_dev); |
| 221 | + kzfree(ts); |
| 222 | + |
| 223 | + return 0; |
| 224 | +} |
| 225 | + |
| 226 | +static const struct of_device_id ft5406_match[] = { |
| 227 | + { .compatible = "rpi,rpi-ft5406", }, |
| 228 | + {}, |
| 229 | +}; |
| 230 | +MODULE_DEVICE_TABLE(of, ft5406_match); |
| 231 | + |
| 232 | +static struct platform_driver ft5406_driver = { |
| 233 | + .driver = { |
| 234 | + .name = "rpi-ft5406", |
| 235 | + .owner = THIS_MODULE, |
| 236 | + .of_match_table = ft5406_match, |
| 237 | + }, |
| 238 | + .probe = ft5406_probe, |
| 239 | + .remove = ft5406_remove, |
| 240 | +}; |
| 241 | + |
| 242 | +module_platform_driver(ft5406_driver); |
| 243 | + |
| 244 | +MODULE_AUTHOR("Gordon Hollingworth"); |
| 245 | +MODULE_DESCRIPTION("Touchscreen driver for memory based FT5406"); |
| 246 | +MODULE_LICENSE("GPL"); |
0 commit comments