0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/io.h>
0010 #include <linux/of.h>
0011 #include <linux/slab.h>
0012 #include <linux/device.h>
0013 #include <linux/module.h>
0014 #include <linux/bitops.h>
0015 #include <linux/dma-mapping.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/input.h>
0018 #include <linux/input/mt.h>
0019 #include <linux/input/touchscreen.h>
0020 #include <soc/bcm2835/raspberrypi-firmware.h>
0021
0022 #define RPI_TS_DEFAULT_WIDTH 800
0023 #define RPI_TS_DEFAULT_HEIGHT 480
0024
0025 #define RPI_TS_MAX_SUPPORTED_POINTS 10
0026
0027 #define RPI_TS_FTS_TOUCH_DOWN 0
0028 #define RPI_TS_FTS_TOUCH_CONTACT 2
0029
0030 #define RPI_TS_POLL_INTERVAL 17
0031
0032 #define RPI_TS_NPOINTS_REG_INVALIDATE 99
0033
0034 struct rpi_ts {
0035 struct platform_device *pdev;
0036 struct input_dev *input;
0037 struct touchscreen_properties prop;
0038
0039 void __iomem *fw_regs_va;
0040 dma_addr_t fw_regs_phys;
0041
0042 int known_ids;
0043 };
0044
0045 struct rpi_ts_regs {
0046 u8 device_mode;
0047 u8 gesture_id;
0048 u8 num_points;
0049 struct rpi_ts_touch {
0050 u8 xh;
0051 u8 xl;
0052 u8 yh;
0053 u8 yl;
0054 u8 pressure;
0055 u8 area;
0056 } point[RPI_TS_MAX_SUPPORTED_POINTS];
0057 };
0058
0059 static void rpi_ts_poll(struct input_dev *input)
0060 {
0061 struct rpi_ts *ts = input_get_drvdata(input);
0062 struct rpi_ts_regs regs;
0063 int modified_ids = 0;
0064 long released_ids;
0065 int event_type;
0066 int touchid;
0067 int x, y;
0068 int i;
0069
0070 memcpy_fromio(®s, ts->fw_regs_va, sizeof(regs));
0071
0072
0073
0074
0075
0076
0077 iowrite8(RPI_TS_NPOINTS_REG_INVALIDATE,
0078 ts->fw_regs_va + offsetof(struct rpi_ts_regs, num_points));
0079
0080 if (regs.num_points == RPI_TS_NPOINTS_REG_INVALIDATE ||
0081 (regs.num_points == 0 && ts->known_ids == 0))
0082 return;
0083
0084 for (i = 0; i < regs.num_points; i++) {
0085 x = (((int)regs.point[i].xh & 0xf) << 8) + regs.point[i].xl;
0086 y = (((int)regs.point[i].yh & 0xf) << 8) + regs.point[i].yl;
0087 touchid = (regs.point[i].yh >> 4) & 0xf;
0088 event_type = (regs.point[i].xh >> 6) & 0x03;
0089
0090 modified_ids |= BIT(touchid);
0091
0092 if (event_type == RPI_TS_FTS_TOUCH_DOWN ||
0093 event_type == RPI_TS_FTS_TOUCH_CONTACT) {
0094 input_mt_slot(input, touchid);
0095 input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
0096 touchscreen_report_pos(input, &ts->prop, x, y, true);
0097 }
0098 }
0099
0100 released_ids = ts->known_ids & ~modified_ids;
0101 for_each_set_bit(i, &released_ids, RPI_TS_MAX_SUPPORTED_POINTS) {
0102 input_mt_slot(input, i);
0103 input_mt_report_slot_inactive(input);
0104 modified_ids &= ~(BIT(i));
0105 }
0106 ts->known_ids = modified_ids;
0107
0108 input_mt_sync_frame(input);
0109 input_sync(input);
0110 }
0111
0112 static void rpi_ts_dma_cleanup(void *data)
0113 {
0114 struct rpi_ts *ts = data;
0115 struct device *dev = &ts->pdev->dev;
0116
0117 dma_free_coherent(dev, PAGE_SIZE, ts->fw_regs_va, ts->fw_regs_phys);
0118 }
0119
0120 static int rpi_ts_probe(struct platform_device *pdev)
0121 {
0122 struct device *dev = &pdev->dev;
0123 struct device_node *np = dev->of_node;
0124 struct input_dev *input;
0125 struct device_node *fw_node;
0126 struct rpi_firmware *fw;
0127 struct rpi_ts *ts;
0128 u32 touchbuf;
0129 int error;
0130
0131 fw_node = of_get_parent(np);
0132 if (!fw_node) {
0133 dev_err(dev, "Missing firmware node\n");
0134 return -ENOENT;
0135 }
0136
0137 fw = rpi_firmware_get(fw_node);
0138 of_node_put(fw_node);
0139 if (!fw)
0140 return -EPROBE_DEFER;
0141
0142 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
0143 if (!ts)
0144 return -ENOMEM;
0145 ts->pdev = pdev;
0146
0147 ts->fw_regs_va = dma_alloc_coherent(dev, PAGE_SIZE, &ts->fw_regs_phys,
0148 GFP_KERNEL);
0149 if (!ts->fw_regs_va) {
0150 dev_err(dev, "failed to dma_alloc_coherent\n");
0151 return -ENOMEM;
0152 }
0153
0154 error = devm_add_action_or_reset(dev, rpi_ts_dma_cleanup, ts);
0155 if (error) {
0156 dev_err(dev, "failed to devm_add_action_or_reset, %d\n", error);
0157 return error;
0158 }
0159
0160 touchbuf = (u32)ts->fw_regs_phys;
0161 error = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
0162 &touchbuf, sizeof(touchbuf));
0163 rpi_firmware_put(fw);
0164 if (error || touchbuf != 0) {
0165 dev_warn(dev, "Failed to set touchbuf, %d\n", error);
0166 return error;
0167 }
0168
0169 input = devm_input_allocate_device(dev);
0170 if (!input) {
0171 dev_err(dev, "Failed to allocate input device\n");
0172 return -ENOMEM;
0173 }
0174
0175 ts->input = input;
0176 input_set_drvdata(input, ts);
0177
0178 input->name = "raspberrypi-ts";
0179 input->id.bustype = BUS_HOST;
0180
0181 input_set_abs_params(input, ABS_MT_POSITION_X, 0,
0182 RPI_TS_DEFAULT_WIDTH, 0, 0);
0183 input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
0184 RPI_TS_DEFAULT_HEIGHT, 0, 0);
0185 touchscreen_parse_properties(input, true, &ts->prop);
0186
0187 error = input_mt_init_slots(input, RPI_TS_MAX_SUPPORTED_POINTS,
0188 INPUT_MT_DIRECT);
0189 if (error) {
0190 dev_err(dev, "could not init mt slots, %d\n", error);
0191 return error;
0192 }
0193
0194 error = input_setup_polling(input, rpi_ts_poll);
0195 if (error) {
0196 dev_err(dev, "could not set up polling mode, %d\n", error);
0197 return error;
0198 }
0199
0200 input_set_poll_interval(input, RPI_TS_POLL_INTERVAL);
0201
0202 error = input_register_device(input);
0203 if (error) {
0204 dev_err(dev, "could not register input device, %d\n", error);
0205 return error;
0206 }
0207
0208 return 0;
0209 }
0210
0211 static const struct of_device_id rpi_ts_match[] = {
0212 { .compatible = "raspberrypi,firmware-ts", },
0213 {},
0214 };
0215 MODULE_DEVICE_TABLE(of, rpi_ts_match);
0216
0217 static struct platform_driver rpi_ts_driver = {
0218 .driver = {
0219 .name = "raspberrypi-ts",
0220 .of_match_table = rpi_ts_match,
0221 },
0222 .probe = rpi_ts_probe,
0223 };
0224 module_platform_driver(rpi_ts_driver);
0225
0226 MODULE_AUTHOR("Gordon Hollingworth");
0227 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
0228 MODULE_DESCRIPTION("Raspberry Pi firmware based touchscreen driver");
0229 MODULE_LICENSE("GPL v2");