blob: b93a28b955fda327ac0248ccac00678acc5a2ed5 [file] [log] [blame]
Hans de Goede6decea72014-05-14 11:20:45 -07001/*
2 * Allwinner sunxi resistive touchscreen controller driver
3 *
4 * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
5 *
Hans de Goedef09f98d2014-05-14 11:22:09 -07006 * The hwmon parts are based on work by Corentin LABBE which is:
7 * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
8 *
Hans de Goede6decea72014-05-14 11:20:45 -07009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20/*
21 * The sun4i-ts controller is capable of detecting a second touch, but when a
22 * second touch is present then the accuracy becomes so bad the reported touch
23 * location is not useable.
24 *
25 * The original android driver contains some complicated heuristics using the
26 * aprox. distance between the 2 touches to see if the user is making a pinch
27 * open / close movement, and then reports emulated multi-touch events around
28 * the last touch coordinate (as the dual-touch coordinates are worthless).
29 *
30 * These kinds of heuristics are just asking for trouble (and don't belong
31 * in the kernel). So this driver offers straight forward, reliable single
32 * touch functionality only.
33 */
34
35#include <linux/err.h>
Hans de Goedef09f98d2014-05-14 11:22:09 -070036#include <linux/hwmon.h>
Chen-Yu Tsai22369712015-01-15 10:26:32 -080037#include <linux/thermal.h>
Hans de Goede6decea72014-05-14 11:20:45 -070038#include <linux/init.h>
39#include <linux/input.h>
40#include <linux/interrupt.h>
41#include <linux/io.h>
42#include <linux/module.h>
43#include <linux/of_platform.h>
44#include <linux/platform_device.h>
45#include <linux/slab.h>
46
47#define TP_CTRL0 0x00
48#define TP_CTRL1 0x04
49#define TP_CTRL2 0x08
50#define TP_CTRL3 0x0c
51#define TP_INT_FIFOC 0x10
52#define TP_INT_FIFOS 0x14
53#define TP_TPR 0x18
54#define TP_CDAT 0x1c
55#define TEMP_DATA 0x20
56#define TP_DATA 0x24
57
58/* TP_CTRL0 bits */
59#define ADC_FIRST_DLY(x) ((x) << 24) /* 8 bits */
60#define ADC_FIRST_DLY_MODE(x) ((x) << 23)
61#define ADC_CLK_SEL(x) ((x) << 22)
62#define ADC_CLK_DIV(x) ((x) << 20) /* 3 bits */
63#define FS_DIV(x) ((x) << 16) /* 4 bits */
64#define T_ACQ(x) ((x) << 0) /* 16 bits */
65
66/* TP_CTRL1 bits */
67#define STYLUS_UP_DEBOUN(x) ((x) << 12) /* 8 bits */
68#define STYLUS_UP_DEBOUN_EN(x) ((x) << 9)
69#define TOUCH_PAN_CALI_EN(x) ((x) << 6)
70#define TP_DUAL_EN(x) ((x) << 5)
71#define TP_MODE_EN(x) ((x) << 4)
72#define TP_ADC_SELECT(x) ((x) << 3)
73#define ADC_CHAN_SELECT(x) ((x) << 0) /* 3 bits */
74
Chen-Yu Tsai43c0e222015-01-26 23:57:00 -080075/* on sun6i, bits 3~6 are left shifted by 1 to 4~7 */
76#define SUN6I_TP_MODE_EN(x) ((x) << 5)
77
Hans de Goede6decea72014-05-14 11:20:45 -070078/* TP_CTRL2 bits */
79#define TP_SENSITIVE_ADJUST(x) ((x) << 28) /* 4 bits */
80#define TP_MODE_SELECT(x) ((x) << 26) /* 2 bits */
81#define PRE_MEA_EN(x) ((x) << 24)
82#define PRE_MEA_THRE_CNT(x) ((x) << 0) /* 24 bits */
83
84/* TP_CTRL3 bits */
85#define FILTER_EN(x) ((x) << 2)
86#define FILTER_TYPE(x) ((x) << 0) /* 2 bits */
87
88/* TP_INT_FIFOC irq and fifo mask / control bits */
89#define TEMP_IRQ_EN(x) ((x) << 18)
90#define OVERRUN_IRQ_EN(x) ((x) << 17)
91#define DATA_IRQ_EN(x) ((x) << 16)
92#define TP_DATA_XY_CHANGE(x) ((x) << 13)
93#define FIFO_TRIG(x) ((x) << 8) /* 5 bits */
94#define DATA_DRQ_EN(x) ((x) << 7)
95#define FIFO_FLUSH(x) ((x) << 4)
96#define TP_UP_IRQ_EN(x) ((x) << 1)
97#define TP_DOWN_IRQ_EN(x) ((x) << 0)
98
99/* TP_INT_FIFOS irq and fifo status bits */
100#define TEMP_DATA_PENDING BIT(18)
101#define FIFO_OVERRUN_PENDING BIT(17)
102#define FIFO_DATA_PENDING BIT(16)
103#define TP_IDLE_FLG BIT(2)
104#define TP_UP_PENDING BIT(1)
105#define TP_DOWN_PENDING BIT(0)
106
107/* TP_TPR bits */
108#define TEMP_ENABLE(x) ((x) << 16)
109#define TEMP_PERIOD(x) ((x) << 0) /* t = x * 256 * 16 / clkin */
110
111struct sun4i_ts_data {
112 struct device *dev;
113 struct input_dev *input;
Chen-Yu Tsai22369712015-01-15 10:26:32 -0800114 struct thermal_zone_device *tz;
Hans de Goede6decea72014-05-14 11:20:45 -0700115 void __iomem *base;
116 unsigned int irq;
117 bool ignore_fifo_data;
Hans de Goedef09f98d2014-05-14 11:22:09 -0700118 int temp_data;
Chen-Yu Tsai43c0e222015-01-26 23:57:00 -0800119 int temp_offset;
120 int temp_step;
Hans de Goede6decea72014-05-14 11:20:45 -0700121};
122
Hans de Goedef09f98d2014-05-14 11:22:09 -0700123static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
Hans de Goede6decea72014-05-14 11:20:45 -0700124{
Hans de Goedef09f98d2014-05-14 11:22:09 -0700125 u32 x, y;
Hans de Goede6decea72014-05-14 11:20:45 -0700126
127 if (reg_val & FIFO_DATA_PENDING) {
128 x = readl(ts->base + TP_DATA);
129 y = readl(ts->base + TP_DATA);
130 /* The 1st location reported after an up event is unreliable */
131 if (!ts->ignore_fifo_data) {
132 input_report_abs(ts->input, ABS_X, x);
133 input_report_abs(ts->input, ABS_Y, y);
134 /*
135 * The hardware has a separate down status bit, but
136 * that gets set before we get the first location,
137 * resulting in reporting a click on the old location.
138 */
139 input_report_key(ts->input, BTN_TOUCH, 1);
140 input_sync(ts->input);
141 } else {
142 ts->ignore_fifo_data = false;
143 }
144 }
145
146 if (reg_val & TP_UP_PENDING) {
147 ts->ignore_fifo_data = true;
148 input_report_key(ts->input, BTN_TOUCH, 0);
149 input_sync(ts->input);
150 }
Hans de Goedef09f98d2014-05-14 11:22:09 -0700151}
152
153static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
154{
155 struct sun4i_ts_data *ts = dev_id;
156 u32 reg_val;
157
158 reg_val = readl(ts->base + TP_INT_FIFOS);
159
160 if (reg_val & TEMP_DATA_PENDING)
161 ts->temp_data = readl(ts->base + TEMP_DATA);
162
163 if (ts->input)
164 sun4i_ts_irq_handle_input(ts, reg_val);
Hans de Goede6decea72014-05-14 11:20:45 -0700165
166 writel(reg_val, ts->base + TP_INT_FIFOS);
167
168 return IRQ_HANDLED;
169}
170
171static int sun4i_ts_open(struct input_dev *dev)
172{
173 struct sun4i_ts_data *ts = input_get_drvdata(dev);
174
Hans de Goedef09f98d2014-05-14 11:22:09 -0700175 /* Flush, set trig level to 1, enable temp, data and up irqs */
176 writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
177 TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
Hans de Goede6decea72014-05-14 11:20:45 -0700178
179 return 0;
180}
181
182static void sun4i_ts_close(struct input_dev *dev)
183{
184 struct sun4i_ts_data *ts = input_get_drvdata(dev);
185
Hans de Goedef09f98d2014-05-14 11:22:09 -0700186 /* Deactivate all input IRQs */
187 writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
Hans de Goede6decea72014-05-14 11:20:45 -0700188}
189
Chen-Yu Tsai22369712015-01-15 10:26:32 -0800190static int sun4i_get_temp(const struct sun4i_ts_data *ts, long *temp)
Hans de Goedef09f98d2014-05-14 11:22:09 -0700191{
Hans de Goedef09f98d2014-05-14 11:22:09 -0700192 /* No temp_data until the first irq */
193 if (ts->temp_data == -1)
194 return -EAGAIN;
195
Chen-Yu Tsai43c0e222015-01-26 23:57:00 -0800196 *temp = (ts->temp_data - ts->temp_offset) * ts->temp_step;
Chen-Yu Tsai22369712015-01-15 10:26:32 -0800197
198 return 0;
199}
200
201static int sun4i_get_tz_temp(void *data, long *temp)
202{
203 return sun4i_get_temp(data, temp);
204}
205
206static struct thermal_zone_of_device_ops sun4i_ts_tz_ops = {
207 .get_temp = sun4i_get_tz_temp,
208};
209
210static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
211 char *buf)
212{
213 struct sun4i_ts_data *ts = dev_get_drvdata(dev);
214 long temp;
215 int error;
216
217 error = sun4i_get_temp(ts, &temp);
218 if (error)
219 return error;
220
221 return sprintf(buf, "%ld\n", temp);
Hans de Goedef09f98d2014-05-14 11:22:09 -0700222}
223
224static ssize_t show_temp_label(struct device *dev,
225 struct device_attribute *devattr, char *buf)
226{
227 return sprintf(buf, "SoC temperature\n");
228}
229
230static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
231static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
232
233static struct attribute *sun4i_ts_attrs[] = {
234 &dev_attr_temp1_input.attr,
235 &dev_attr_temp1_label.attr,
236 NULL
237};
238ATTRIBUTE_GROUPS(sun4i_ts);
239
Hans de Goede6decea72014-05-14 11:20:45 -0700240static int sun4i_ts_probe(struct platform_device *pdev)
241{
242 struct sun4i_ts_data *ts;
243 struct device *dev = &pdev->dev;
Hans de Goedef09f98d2014-05-14 11:22:09 -0700244 struct device_node *np = dev->of_node;
245 struct device *hwmon;
Hans de Goede6decea72014-05-14 11:20:45 -0700246 int error;
Chen-Yu Tsai43c0e222015-01-26 23:57:00 -0800247 u32 reg;
Hans de Goedef09f98d2014-05-14 11:22:09 -0700248 bool ts_attached;
Hans de Goede6decea72014-05-14 11:20:45 -0700249
250 ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
251 if (!ts)
252 return -ENOMEM;
253
254 ts->dev = dev;
255 ts->ignore_fifo_data = true;
Hans de Goedef09f98d2014-05-14 11:22:09 -0700256 ts->temp_data = -1;
Chen-Yu Tsai43c0e222015-01-26 23:57:00 -0800257 if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts")) {
258 /* Allwinner SDK has temperature = -271 + (value / 6) (C) */
259 ts->temp_offset = 1626;
260 ts->temp_step = 167;
261 } else {
262 /*
263 * The user manuals do not contain the formula for calculating
264 * the temperature. The formula used here is from the AXP209,
265 * which is designed by X-Powers, an affiliate of Allwinner:
266 *
267 * temperature = -144.7 + (value * 0.1)
268 *
269 * Allwinner does not have any documentation whatsoever for
270 * this hardware. Moreover, it is claimed that the sensor
271 * is inaccurate and cannot work properly.
272 */
273 ts->temp_offset = 1447;
274 ts->temp_step = 100;
275 }
Hans de Goede6decea72014-05-14 11:20:45 -0700276
Hans de Goedef09f98d2014-05-14 11:22:09 -0700277 ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
278 if (ts_attached) {
279 ts->input = devm_input_allocate_device(dev);
280 if (!ts->input)
281 return -ENOMEM;
Hans de Goede6decea72014-05-14 11:20:45 -0700282
Hans de Goedef09f98d2014-05-14 11:22:09 -0700283 ts->input->name = pdev->name;
284 ts->input->phys = "sun4i_ts/input0";
285 ts->input->open = sun4i_ts_open;
286 ts->input->close = sun4i_ts_close;
287 ts->input->id.bustype = BUS_HOST;
288 ts->input->id.vendor = 0x0001;
289 ts->input->id.product = 0x0001;
290 ts->input->id.version = 0x0100;
291 ts->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
292 __set_bit(BTN_TOUCH, ts->input->keybit);
293 input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
294 input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
295 input_set_drvdata(ts->input, ts);
296 }
Hans de Goede6decea72014-05-14 11:20:45 -0700297
298 ts->base = devm_ioremap_resource(dev,
299 platform_get_resource(pdev, IORESOURCE_MEM, 0));
300 if (IS_ERR(ts->base))
301 return PTR_ERR(ts->base);
302
303 ts->irq = platform_get_irq(pdev, 0);
304 error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
305 if (error)
306 return error;
307
308 /*
309 * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
310 * t_acq = clkin / (16 * 64)
311 */
312 writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
313 ts->base + TP_CTRL0);
314
315 /*
316 * sensitive_adjust = 15 : max, which is not all that sensitive,
317 * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
318 */
319 writel(TP_SENSITIVE_ADJUST(15) | TP_MODE_SELECT(0),
320 ts->base + TP_CTRL2);
321
322 /* Enable median filter, type 1 : 5/3 */
323 writel(FILTER_EN(1) | FILTER_TYPE(1), ts->base + TP_CTRL3);
324
325 /* Enable temperature measurement, period 1953 (2 seconds) */
326 writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
327
328 /*
329 * Set stylus up debounce to aprox 10 ms, enable debounce, and
330 * finally enable tp mode.
331 */
Chen-Yu Tsai43c0e222015-01-26 23:57:00 -0800332 reg = STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1);
333 if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts"))
334 reg |= TP_MODE_EN(1);
335 else
336 reg |= SUN6I_TP_MODE_EN(1);
337 writel(reg, ts->base + TP_CTRL1);
Hans de Goede6decea72014-05-14 11:20:45 -0700338
Chen-Yu Tsai22369712015-01-15 10:26:32 -0800339 /*
340 * The thermal core does not register hwmon devices for DT-based
341 * thermal zone sensors, such as this one.
342 */
Hans de Goedef09f98d2014-05-14 11:22:09 -0700343 hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
344 ts, sun4i_ts_groups);
345 if (IS_ERR(hwmon))
346 return PTR_ERR(hwmon);
347
Chen-Yu Tsai22369712015-01-15 10:26:32 -0800348 ts->tz = thermal_zone_of_sensor_register(ts->dev, 0, ts,
349 &sun4i_ts_tz_ops);
350 if (IS_ERR(ts->tz))
351 ts->tz = NULL;
352
Hans de Goedef09f98d2014-05-14 11:22:09 -0700353 writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
354
355 if (ts_attached) {
356 error = input_register_device(ts->input);
357 if (error) {
358 writel(0, ts->base + TP_INT_FIFOC);
Chen-Yu Tsai22369712015-01-15 10:26:32 -0800359 thermal_zone_of_sensor_unregister(ts->dev, ts->tz);
Hans de Goedef09f98d2014-05-14 11:22:09 -0700360 return error;
361 }
362 }
Hans de Goede6decea72014-05-14 11:20:45 -0700363
364 platform_set_drvdata(pdev, ts);
365 return 0;
366}
367
Hans de Goedef09f98d2014-05-14 11:22:09 -0700368static int sun4i_ts_remove(struct platform_device *pdev)
369{
370 struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
371
372 /* Explicit unregister to avoid open/close changing the imask later */
373 if (ts->input)
374 input_unregister_device(ts->input);
375
Chen-Yu Tsai22369712015-01-15 10:26:32 -0800376 thermal_zone_of_sensor_unregister(ts->dev, ts->tz);
377
Hans de Goedef09f98d2014-05-14 11:22:09 -0700378 /* Deactivate all IRQs */
379 writel(0, ts->base + TP_INT_FIFOC);
380
381 return 0;
382}
383
Hans de Goede6decea72014-05-14 11:20:45 -0700384static const struct of_device_id sun4i_ts_of_match[] = {
385 { .compatible = "allwinner,sun4i-a10-ts", },
Chen-Yu Tsai43c0e222015-01-26 23:57:00 -0800386 { .compatible = "allwinner,sun6i-a31-ts", },
Hans de Goede6decea72014-05-14 11:20:45 -0700387 { /* sentinel */ }
388};
389MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
390
391static struct platform_driver sun4i_ts_driver = {
392 .driver = {
Hans de Goede6decea72014-05-14 11:20:45 -0700393 .name = "sun4i-ts",
394 .of_match_table = of_match_ptr(sun4i_ts_of_match),
395 },
396 .probe = sun4i_ts_probe,
Hans de Goedef09f98d2014-05-14 11:22:09 -0700397 .remove = sun4i_ts_remove,
Hans de Goede6decea72014-05-14 11:20:45 -0700398};
399
400module_platform_driver(sun4i_ts_driver);
401
402MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
403MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
404MODULE_LICENSE("GPL");