2 * Touchscreen driver for the TS-4800 board
4 * Copyright (c) 2015 - Savoir-faire Linux
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
11 #include <linux/bitops.h>
12 #include <linux/input.h>
13 #include <linux/input-polldev.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
22 /* polling interval in ms */
23 #define POLL_INTERVAL 3
25 #define DEBOUNCE_COUNT 1
27 /* sensor values are 12-bit wide */
28 #define MAX_12BIT ((1 << 12) - 1)
30 #define PENDOWN_MASK 0x1
36 struct input_polled_dev *poll_dev;
41 struct regmap *regmap;
49 static void ts4800_ts_open(struct input_polled_dev *dev)
51 struct ts4800_ts *ts = dev->private;
55 ts->debounce = DEBOUNCE_COUNT;
57 ret = regmap_update_bits(ts->regmap, ts->reg, ts->bit, ts->bit);
59 dev_warn(ts->dev, "Failed to enable touchscreen\n");
62 static void ts4800_ts_close(struct input_polled_dev *dev)
64 struct ts4800_ts *ts = dev->private;
67 ret = regmap_update_bits(ts->regmap, ts->reg, ts->bit, 0);
69 dev_warn(ts->dev, "Failed to disable touchscreen\n");
73 static void ts4800_ts_poll(struct input_polled_dev *dev)
75 struct input_dev *input_dev = dev->input;
76 struct ts4800_ts *ts = dev->private;
77 u16 last_x = readw(ts->base + X_OFFSET);
78 u16 last_y = readw(ts->base + Y_OFFSET);
79 bool pendown = last_x & PENDOWN_MASK;
88 input_report_key(input_dev, BTN_TOUCH, 1);
92 last_x = ((~last_x) >> 4) & MAX_12BIT;
93 last_y = ((~last_y) >> 4) & MAX_12BIT;
95 input_report_abs(input_dev, ABS_X, last_x);
96 input_report_abs(input_dev, ABS_Y, last_y);
97 input_sync(input_dev);
98 } else if (ts->pendown) {
100 ts->debounce = DEBOUNCE_COUNT;
101 input_report_key(input_dev, BTN_TOUCH, 0);
102 input_sync(input_dev);
106 static int ts4800_parse_dt(struct platform_device *pdev,
107 struct ts4800_ts *ts)
109 struct device *dev = &pdev->dev;
110 struct device_node *np = dev->of_node;
111 struct device_node *syscon_np;
115 syscon_np = of_parse_phandle(np, "syscon", 0);
117 dev_err(dev, "no syscon property\n");
121 ts->regmap = syscon_node_to_regmap(syscon_np);
122 of_node_put(syscon_np);
123 if (IS_ERR(ts->regmap)) {
124 dev_err(dev, "cannot get parent's regmap\n");
125 return PTR_ERR(ts->regmap);
128 error = of_property_read_u32_index(np, "syscon", 1, ®);
130 dev_err(dev, "no offset in syscon\n");
136 error = of_property_read_u32_index(np, "syscon", 2, &bit);
138 dev_err(dev, "no bit in syscon\n");
147 static int ts4800_ts_probe(struct platform_device *pdev)
149 struct input_polled_dev *poll_dev;
150 struct ts4800_ts *ts;
151 struct resource *res;
154 ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
158 error = ts4800_parse_dt(pdev, ts);
162 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
163 ts->base = devm_ioremap_resource(&pdev->dev, res);
164 if (IS_ERR(ts->base))
165 return PTR_ERR(ts->base);
167 poll_dev = devm_input_allocate_polled_device(&pdev->dev);
171 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&pdev->dev));
172 ts->poll_dev = poll_dev;
173 ts->dev = &pdev->dev;
175 poll_dev->private = ts;
176 poll_dev->poll_interval = POLL_INTERVAL;
177 poll_dev->open = ts4800_ts_open;
178 poll_dev->close = ts4800_ts_close;
179 poll_dev->poll = ts4800_ts_poll;
181 poll_dev->input->name = "TS-4800 Touchscreen";
182 poll_dev->input->phys = ts->phys;
184 input_set_capability(poll_dev->input, EV_KEY, BTN_TOUCH);
185 input_set_abs_params(poll_dev->input, ABS_X, 0, MAX_12BIT, 0, 0);
186 input_set_abs_params(poll_dev->input, ABS_Y, 0, MAX_12BIT, 0, 0);
188 error = input_register_polled_device(poll_dev);
191 "Unabled to register polled input device (%d)\n",
199 static const struct of_device_id ts4800_ts_of_match[] = {
200 { .compatible = "technologic,ts4800-ts", },
203 MODULE_DEVICE_TABLE(of, ts4800_ts_of_match);
205 static struct platform_driver ts4800_ts_driver = {
208 .of_match_table = ts4800_ts_of_match,
210 .probe = ts4800_ts_probe,
212 module_platform_driver(ts4800_ts_driver);
214 MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>");
215 MODULE_DESCRIPTION("TS-4800 Touchscreen Driver");
216 MODULE_LICENSE("GPL v2");
217 MODULE_ALIAS("platform:ts4800_ts");