| 1 | |
| 2 | |
| 3 | * Raspberry Pi firmware based touchscreen driver
|
| 4 | *
|
| 5 | * Copyright (C) 2015, 2017 Raspberry Pi
|
| 6 | * Copyright (C) 2018 Nicolas Saenz Julienne <[email protected]>
|
| 7 |
|
| 8 |
|
| 9 | #include <linux/io.h>
|
| 10 | #include <linux/of.h>
|
| 11 | #include <linux/slab.h>
|
| 12 | #include <linux/device.h>
|
| 13 | #include <linux/module.h>
|
| 14 | #include <linux/bitops.h>
|
| 15 | #include <linux/dma-mapping.h>
|
| 16 | #include <linux/platform_device.h>
|
| 17 | #include <linux/input.h>
|
| 18 | #include <linux/input/mt.h>
|
| 19 | #include <linux/input/touchscreen.h>
|
| 20 | #include <soc/bcm2835/raspberrypi-firmware.h>
|
| 21 |
|
| 22 | #define RPI_TS_DEFAULT_WIDTH 800
|
| 23 | #define RPI_TS_DEFAULT_HEIGHT 480
|
| 24 |
|
| 25 | #define RPI_TS_MAX_SUPPORTED_POINTS 10
|
| 26 |
|
| 27 | #define RPI_TS_FTS_TOUCH_DOWN 0
|
| 28 | #define RPI_TS_FTS_TOUCH_CONTACT 2
|
| 29 |
|
| 30 | #define RPI_TS_POLL_INTERVAL 17
|
| 31 |
|
| 32 | #define RPI_TS_NPOINTS_REG_INVALIDATE 99
|
| 33 |
|
| 34 | struct rpi_ts {
|
| 35 | struct platform_device *pdev;
|
| 36 | struct input_dev *input;
|
| 37 | struct touchscreen_properties prop;
|
| 38 |
|
| 39 | void __iomem *fw_regs_va;
|
| 40 | dma_addr_t fw_regs_phys;
|
| 41 |
|
| 42 | int known_ids;
|
| 43 | };
|
| 44 |
|
| 45 | struct rpi_ts_regs {
|
| 46 | u8 device_mode;
|
| 47 | u8 gesture_id;
|
| 48 | u8 num_points;
|
| 49 | struct rpi_ts_touch {
|
| 50 | u8 xh;
|
| 51 | u8 xl;
|
| 52 | u8 yh;
|
| 53 | u8 yl;
|
| 54 | u8 pressure;
|
| 55 | u8 area;
|
| 56 | } point[RPI_TS_MAX_SUPPORTED_POINTS];
|
| 57 | };
|
| 58 |
|
| 59 | static void rpi_ts_poll(struct input_dev *input)
|
| 60 | {
|
| 61 | struct rpi_ts *ts = input_get_drvdata(input);
|
| 62 | struct rpi_ts_regs regs;
|
| 63 | int modified_ids = 0;
|
| 64 | long released_ids;
|
| 65 | int event_type;
|
| 66 | int touchid;
|
| 67 | int x, y;
|
| 68 | int i;
|
| 69 |
|
| 70 | memcpy_fromio(®s, ts->fw_regs_va, sizeof(regs));
|
| 71 | |
| 72 | * We poll the memory based register copy of the touchscreen chip using
|
| 73 | * the number of points register to know whether the copy has been
|
| 74 | * updated (we write 99 to the memory copy, the GPU will write between
|
| 75 | * 0 - 10 points)
|
| 76 |
|
| 77 | iowrite8(RPI_TS_NPOINTS_REG_INVALIDATE,
|
| 78 | ts->fw_regs_va + offsetof(struct rpi_ts_regs, num_points));
|
| 79 |
|
| 80 | if (regs.num_points == RPI_TS_NPOINTS_REG_INVALIDATE ||
|
| 81 | (regs.num_points == 0 && ts->known_ids == 0))
|
| 82 | return;
|
| 83 |
|
| 84 | for (i = 0; i < regs.num_points; i++) {
|
| 85 | x = (((int)regs.point[i].xh & 0xf) << 8) + regs.point[i].xl;
|
| 86 | y = (((int)regs.point[i].yh & 0xf) << 8) + regs.point[i].yl;
|
| 87 | touchid = (regs.point[i].yh >> 4) & 0xf;
|
| 88 | event_type = (regs.point[i].xh >> 6) & 0x03;
|
| 89 |
|
| 90 | modified_ids |= BIT(touchid);
|
| 91 |
|
| 92 | if (event_type == RPI_TS_FTS_TOUCH_DOWN ||
|
| 93 | event_type == RPI_TS_FTS_TOUCH_CONTACT) {
|
| 94 | input_mt_slot(input, touchid);
|
| 95 | input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
|
| 96 | touchscreen_report_pos(input, &ts->prop, x, y, true);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | released_ids = ts->known_ids & ~modified_ids;
|
| 101 | for_each_set_bit(i, &released_ids, RPI_TS_MAX_SUPPORTED_POINTS) {
|
| 102 | input_mt_slot(input, i);
|
| 103 | input_mt_report_slot_inactive(input);
|
| 104 | modified_ids &= ~(BIT(i));
|
| 105 | }
|
| 106 | ts->known_ids = modified_ids;
|
| 107 |
|
| 108 | input_mt_sync_frame(input);
|
| 109 | input_sync(input);
|
| 110 | }
|
| 111 |
|
| 112 | static void rpi_ts_dma_cleanup(void *data)
|
| 113 | {
|
| 114 | struct rpi_ts *ts = data;
|
| 115 | struct device *dev = &ts->pdev->dev;
|
| 116 |
|
| 117 | dma_free_coherent(dev, PAGE_SIZE, ts->fw_regs_va, ts->fw_regs_phys);
|
| 118 | }
|
| 119 |
|
| 120 | static int rpi_ts_probe(struct platform_device *pdev)
|
| 121 | {
|
| 122 | struct device *dev = &pdev->dev;
|
| 123 | struct device_node *np = dev->of_node;
|
| 124 | struct input_dev *input;
|
| 125 | struct rpi_firmware *fw;
|
| 126 | struct rpi_ts *ts;
|
| 127 | u32 touchbuf;
|
| 128 | int error;
|
| 129 |
|
| 130 | struct device_node *fw_node __free(device_node) = of_get_parent(np);
|
| 131 | if (!fw_node) {
|
| 132 | dev_err(dev, "Missing firmware node\n");
|
| 133 | return -ENOENT;
|
| 134 | }
|
| 135 |
|
| 136 | fw = devm_rpi_firmware_get(&pdev->dev, fw_node);
|
| 137 | if (!fw)
|
| 138 | return -EPROBE_DEFER;
|
| 139 |
|
| 140 | ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
|
| 141 | if (!ts)
|
| 142 | return -ENOMEM;
|
| 143 | ts->pdev = pdev;
|
| 144 |
|
| 145 | ts->fw_regs_va = dma_alloc_coherent(dev, PAGE_SIZE, &ts->fw_regs_phys,
|
| 146 | GFP_KERNEL);
|
| 147 | if (!ts->fw_regs_va) {
|
| 148 | dev_err(dev, "failed to dma_alloc_coherent\n");
|
| 149 | return -ENOMEM;
|
| 150 | }
|
| 151 |
|
| 152 | error = devm_add_action_or_reset(dev, rpi_ts_dma_cleanup, ts);
|
| 153 | if (error) {
|
| 154 | dev_err(dev, "failed to devm_add_action_or_reset, %d\n", error);
|
| 155 | return error;
|
| 156 | }
|
| 157 |
|
| 158 | touchbuf = (u32)ts->fw_regs_phys;
|
| 159 | error = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
|
| 160 | &touchbuf, sizeof(touchbuf));
|
| 161 | if (error || touchbuf != 0) {
|
| 162 | dev_warn(dev, "Failed to set touchbuf, %d\n", error);
|
| 163 | return error;
|
| 164 | }
|
| 165 |
|
| 166 | input = devm_input_allocate_device(dev);
|
| 167 | if (!input) {
|
| 168 | dev_err(dev, "Failed to allocate input device\n");
|
| 169 | return -ENOMEM;
|
| 170 | }
|
| 171 |
|
| 172 | ts->input = input;
|
| 173 | input_set_drvdata(input, ts);
|
| 174 |
|
| 175 | input->name = "raspberrypi-ts";
|
| 176 | input->id.bustype = BUS_HOST;
|
| 177 |
|
| 178 | input_set_abs_params(input, ABS_MT_POSITION_X, 0,
|
| 179 | RPI_TS_DEFAULT_WIDTH, 0, 0);
|
| 180 | input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
|
| 181 | RPI_TS_DEFAULT_HEIGHT, 0, 0);
|
| 182 | touchscreen_parse_properties(input, true, &ts->prop);
|
| 183 |
|
| 184 | error = input_mt_init_slots(input, RPI_TS_MAX_SUPPORTED_POINTS,
|
| 185 | INPUT_MT_DIRECT);
|
| 186 | if (error) {
|
| 187 | dev_err(dev, "could not init mt slots, %d\n", error);
|
| 188 | return error;
|
| 189 | }
|
| 190 |
|
| 191 | error = input_setup_polling(input, rpi_ts_poll);
|
| 192 | if (error) {
|
| 193 | dev_err(dev, "could not set up polling mode, %d\n", error);
|
| 194 | return error;
|
| 195 | }
|
| 196 |
|
| 197 | input_set_poll_interval(input, RPI_TS_POLL_INTERVAL);
|
| 198 |
|
| 199 | error = input_register_device(input);
|
| 200 | if (error) {
|
| 201 | dev_err(dev, "could not register input device, %d\n", error);
|
| 202 | return error;
|
| 203 | }
|
| 204 |
|
| 205 | return 0;
|
| 206 | }
|
| 207 |
|
| 208 | static const struct of_device_id rpi_ts_match[] = {
|
| 209 | { .compatible = "raspberrypi,firmware-ts", },
|
| 210 | {},
|
| 211 | };
|
| 212 | MODULE_DEVICE_TABLE(of, rpi_ts_match);
|
| 213 |
|
| 214 | static struct platform_driver rpi_ts_driver = {
|
| 215 | .driver = {
|
| 216 | .name = "raspberrypi-ts",
|
| 217 | .of_match_table = rpi_ts_match,
|
| 218 | },
|
| 219 | .probe = rpi_ts_probe,
|
| 220 | };
|
| 221 | module_platform_driver(rpi_ts_driver);
|
| 222 |
|
| 223 | MODULE_AUTHOR("Gordon Hollingworth");
|
| 224 | MODULE_AUTHOR("Nicolas Saenz Julienne <[email protected]>");
|
| 225 | MODULE_DESCRIPTION("Raspberry Pi firmware based touchscreen driver");
|
| 226 | MODULE_LICENSE("GPL v2");
|