^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Touchscreen driver for the TS-4800 board
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2015 - Savoir-faire Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This file is licensed under the terms of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * License version 2. This program is licensed "as is" without any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * warranty of any kind, whether express or implied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mfd/syscon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* polling interval in ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define POLL_INTERVAL 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define DEBOUNCE_COUNT 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* sensor values are 12-bit wide */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define MAX_12BIT ((1 << 12) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define PENDOWN_MASK 0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define X_OFFSET 0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define Y_OFFSET 0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct ts4800_ts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) char phys[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned int bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) bool pendown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int debounce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int ts4800_ts_open(struct input_dev *input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct ts4800_ts *ts = input_get_drvdata(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ts->pendown = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ts->debounce = DEBOUNCE_COUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) error = regmap_update_bits(ts->regmap, ts->reg, ts->bit, ts->bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) dev_warn(ts->dev, "Failed to enable touchscreen: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static void ts4800_ts_close(struct input_dev *input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct ts4800_ts *ts = input_get_drvdata(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ret = regmap_update_bits(ts->regmap, ts->reg, ts->bit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) dev_warn(ts->dev, "Failed to disable touchscreen\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static void ts4800_ts_poll(struct input_dev *input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct ts4800_ts *ts = input_get_drvdata(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) u16 last_x = readw(ts->base + X_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) u16 last_y = readw(ts->base + Y_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) bool pendown = last_x & PENDOWN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (pendown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (ts->debounce) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ts->debounce--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!ts->pendown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) input_report_key(input_dev, BTN_TOUCH, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ts->pendown = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) last_x = ((~last_x) >> 4) & MAX_12BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) last_y = ((~last_y) >> 4) & MAX_12BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) input_report_abs(input_dev, ABS_X, last_x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) input_report_abs(input_dev, ABS_Y, last_y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) input_sync(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) } else if (ts->pendown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) ts->pendown = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) ts->debounce = DEBOUNCE_COUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) input_report_key(input_dev, BTN_TOUCH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) input_sync(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int ts4800_parse_dt(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct ts4800_ts *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct device_node *syscon_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) u32 reg, bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) syscon_np = of_parse_phandle(np, "syscon", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!syscon_np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) dev_err(dev, "no syscon property\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ts->regmap = syscon_node_to_regmap(syscon_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) of_node_put(syscon_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (IS_ERR(ts->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) dev_err(dev, "cannot get parent's regmap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return PTR_ERR(ts->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) error = of_property_read_u32_index(np, "syscon", 1, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) dev_err(dev, "no offset in syscon\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ts->reg = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) error = of_property_read_u32_index(np, "syscon", 2, &bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) dev_err(dev, "no bit in syscon\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ts->bit = BIT(bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int ts4800_ts_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct ts4800_ts *ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) error = ts4800_parse_dt(pdev, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ts->base = devm_platform_ioremap_resource(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (IS_ERR(ts->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return PTR_ERR(ts->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) input_dev = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (!input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&pdev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ts->input = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ts->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) input_set_drvdata(input_dev, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) input_dev->name = "TS-4800 Touchscreen";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) input_dev->phys = ts->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) input_dev->open = ts4800_ts_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) input_dev->close = ts4800_ts_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) error = input_setup_polling(input_dev, ts4800_ts_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dev_err(&pdev->dev, "Unable to set up polling: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) input_set_poll_interval(input_dev, POLL_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) error = input_register_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) "Unable to register input device: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static const struct of_device_id ts4800_ts_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) { .compatible = "technologic,ts4800-ts", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) MODULE_DEVICE_TABLE(of, ts4800_ts_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static struct platform_driver ts4800_ts_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) .name = "ts4800-ts",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) .of_match_table = ts4800_ts_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) .probe = ts4800_ts_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) module_platform_driver(ts4800_ts_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) MODULE_DESCRIPTION("TS-4800 Touchscreen Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) MODULE_ALIAS("platform:ts4800_ts");