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>
14 #include <linux/kernel.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
21 /* polling interval in ms */
22 #define POLL_INTERVAL 3
24 #define DEBOUNCE_COUNT 1
26 /* sensor values are 12-bit wide */
27 #define MAX_12BIT ((1 << 12) - 1)
29 #define PENDOWN_MASK 0x1
35 struct input_dev *input;
40 struct regmap *regmap;
48 static int ts4800_ts_open(struct input_dev *input_dev)
50 struct ts4800_ts *ts = input_get_drvdata(input_dev);
54 ts->debounce = DEBOUNCE_COUNT;
56 error = regmap_update_bits(ts->regmap, ts->reg, ts->bit, ts->bit);
58 dev_warn(ts->dev, "Failed to enable touchscreen: %d\n", error);
65 static void ts4800_ts_close(struct input_dev *input_dev)
67 struct ts4800_ts *ts = input_get_drvdata(input_dev);
70 ret = regmap_update_bits(ts->regmap, ts->reg, ts->bit, 0);
72 dev_warn(ts->dev, "Failed to disable touchscreen\n");
76 static void ts4800_ts_poll(struct input_dev *input_dev)
78 struct ts4800_ts *ts = input_get_drvdata(input_dev);
79 u16 last_x = readw(ts->base + X_OFFSET);
80 u16 last_y = readw(ts->base + Y_OFFSET);
81 bool pendown = last_x & PENDOWN_MASK;
90 input_report_key(input_dev, BTN_TOUCH, 1);
94 last_x = ((~last_x) >> 4) & MAX_12BIT;
95 last_y = ((~last_y) >> 4) & MAX_12BIT;
97 input_report_abs(input_dev, ABS_X, last_x);
98 input_report_abs(input_dev, ABS_Y, last_y);
99 input_sync(input_dev);
100 } else if (ts->pendown) {
102 ts->debounce = DEBOUNCE_COUNT;
103 input_report_key(input_dev, BTN_TOUCH, 0);
104 input_sync(input_dev);
108 static int ts4800_parse_dt(struct platform_device *pdev,
109 struct ts4800_ts *ts)
111 struct device *dev = &pdev->dev;
112 struct device_node *np = dev->of_node;
116 struct device_node *syscon_np __free(device_node) =
117 of_parse_phandle(np, "syscon", 0);
119 dev_err(dev, "no syscon property\n");
123 ts->regmap = syscon_node_to_regmap(syscon_np);
124 if (IS_ERR(ts->regmap)) {
125 dev_err(dev, "cannot get parent's regmap\n");
126 return PTR_ERR(ts->regmap);
129 error = of_property_read_u32_index(np, "syscon", 1, ®);
131 dev_err(dev, "no offset in syscon\n");
137 error = of_property_read_u32_index(np, "syscon", 2, &bit);
139 dev_err(dev, "no bit in syscon\n");
148 static int ts4800_ts_probe(struct platform_device *pdev)
150 struct input_dev *input_dev;
151 struct ts4800_ts *ts;
154 ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
158 error = ts4800_parse_dt(pdev, ts);
162 ts->base = devm_platform_ioremap_resource(pdev, 0);
163 if (IS_ERR(ts->base))
164 return PTR_ERR(ts->base);
166 input_dev = devm_input_allocate_device(&pdev->dev);
170 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&pdev->dev));
171 ts->input = input_dev;
172 ts->dev = &pdev->dev;
174 input_set_drvdata(input_dev, ts);
176 input_dev->name = "TS-4800 Touchscreen";
177 input_dev->phys = ts->phys;
179 input_dev->open = ts4800_ts_open;
180 input_dev->close = ts4800_ts_close;
182 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
183 input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
184 input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
186 error = input_setup_polling(input_dev, ts4800_ts_poll);
188 dev_err(&pdev->dev, "Unable to set up polling: %d\n", error);
192 input_set_poll_interval(input_dev, POLL_INTERVAL);
194 error = input_register_device(input_dev);
197 "Unable to register input device: %d\n", error);
204 static const struct of_device_id ts4800_ts_of_match[] = {
205 { .compatible = "technologic,ts4800-ts", },
208 MODULE_DEVICE_TABLE(of, ts4800_ts_of_match);
210 static struct platform_driver ts4800_ts_driver = {
213 .of_match_table = ts4800_ts_of_match,
215 .probe = ts4800_ts_probe,
217 module_platform_driver(ts4800_ts_driver);
220 MODULE_DESCRIPTION("TS-4800 Touchscreen Driver");
221 MODULE_LICENSE("GPL v2");
222 MODULE_ALIAS("platform:ts4800_ts");