Loading...
1/*
2 * Allwinner sunxi resistive touchscreen controller driver
3 *
4 * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
5 *
6 * The hwmon parts are based on work by Corentin LABBE which is:
7 * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
8 *
9 * 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 * s.a. A20 User Manual "1.15 TP" (Documentation/arm/sunxi/README)
35 * (looks like the description in the A20 User Manual v1.3 is better
36 * than the one in the A10 User Manual v.1.5)
37 */
38
39#include <linux/err.h>
40#include <linux/hwmon.h>
41#include <linux/thermal.h>
42#include <linux/init.h>
43#include <linux/input.h>
44#include <linux/interrupt.h>
45#include <linux/io.h>
46#include <linux/module.h>
47#include <linux/of_platform.h>
48#include <linux/platform_device.h>
49#include <linux/slab.h>
50
51#define TP_CTRL0 0x00
52#define TP_CTRL1 0x04
53#define TP_CTRL2 0x08
54#define TP_CTRL3 0x0c
55#define TP_INT_FIFOC 0x10
56#define TP_INT_FIFOS 0x14
57#define TP_TPR 0x18
58#define TP_CDAT 0x1c
59#define TEMP_DATA 0x20
60#define TP_DATA 0x24
61
62/* TP_CTRL0 bits */
63#define ADC_FIRST_DLY(x) ((x) << 24) /* 8 bits */
64#define ADC_FIRST_DLY_MODE(x) ((x) << 23)
65#define ADC_CLK_SEL(x) ((x) << 22)
66#define ADC_CLK_DIV(x) ((x) << 20) /* 3 bits */
67#define FS_DIV(x) ((x) << 16) /* 4 bits */
68#define T_ACQ(x) ((x) << 0) /* 16 bits */
69
70/* TP_CTRL1 bits */
71#define STYLUS_UP_DEBOUN(x) ((x) << 12) /* 8 bits */
72#define STYLUS_UP_DEBOUN_EN(x) ((x) << 9)
73#define TOUCH_PAN_CALI_EN(x) ((x) << 6)
74#define TP_DUAL_EN(x) ((x) << 5)
75#define TP_MODE_EN(x) ((x) << 4)
76#define TP_ADC_SELECT(x) ((x) << 3)
77#define ADC_CHAN_SELECT(x) ((x) << 0) /* 3 bits */
78
79/* on sun6i, bits 3~6 are left shifted by 1 to 4~7 */
80#define SUN6I_TP_MODE_EN(x) ((x) << 5)
81
82/* TP_CTRL2 bits */
83#define TP_SENSITIVE_ADJUST(x) ((x) << 28) /* 4 bits */
84#define TP_MODE_SELECT(x) ((x) << 26) /* 2 bits */
85#define PRE_MEA_EN(x) ((x) << 24)
86#define PRE_MEA_THRE_CNT(x) ((x) << 0) /* 24 bits */
87
88/* TP_CTRL3 bits */
89#define FILTER_EN(x) ((x) << 2)
90#define FILTER_TYPE(x) ((x) << 0) /* 2 bits */
91
92/* TP_INT_FIFOC irq and fifo mask / control bits */
93#define TEMP_IRQ_EN(x) ((x) << 18)
94#define OVERRUN_IRQ_EN(x) ((x) << 17)
95#define DATA_IRQ_EN(x) ((x) << 16)
96#define TP_DATA_XY_CHANGE(x) ((x) << 13)
97#define FIFO_TRIG(x) ((x) << 8) /* 5 bits */
98#define DATA_DRQ_EN(x) ((x) << 7)
99#define FIFO_FLUSH(x) ((x) << 4)
100#define TP_UP_IRQ_EN(x) ((x) << 1)
101#define TP_DOWN_IRQ_EN(x) ((x) << 0)
102
103/* TP_INT_FIFOS irq and fifo status bits */
104#define TEMP_DATA_PENDING BIT(18)
105#define FIFO_OVERRUN_PENDING BIT(17)
106#define FIFO_DATA_PENDING BIT(16)
107#define TP_IDLE_FLG BIT(2)
108#define TP_UP_PENDING BIT(1)
109#define TP_DOWN_PENDING BIT(0)
110
111/* TP_TPR bits */
112#define TEMP_ENABLE(x) ((x) << 16)
113#define TEMP_PERIOD(x) ((x) << 0) /* t = x * 256 * 16 / clkin */
114
115struct sun4i_ts_data {
116 struct device *dev;
117 struct input_dev *input;
118 struct thermal_zone_device *tz;
119 void __iomem *base;
120 unsigned int irq;
121 bool ignore_fifo_data;
122 int temp_data;
123 int temp_offset;
124 int temp_step;
125};
126
127static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
128{
129 u32 x, y;
130
131 if (reg_val & FIFO_DATA_PENDING) {
132 x = readl(ts->base + TP_DATA);
133 y = readl(ts->base + TP_DATA);
134 /* The 1st location reported after an up event is unreliable */
135 if (!ts->ignore_fifo_data) {
136 input_report_abs(ts->input, ABS_X, x);
137 input_report_abs(ts->input, ABS_Y, y);
138 /*
139 * The hardware has a separate down status bit, but
140 * that gets set before we get the first location,
141 * resulting in reporting a click on the old location.
142 */
143 input_report_key(ts->input, BTN_TOUCH, 1);
144 input_sync(ts->input);
145 } else {
146 ts->ignore_fifo_data = false;
147 }
148 }
149
150 if (reg_val & TP_UP_PENDING) {
151 ts->ignore_fifo_data = true;
152 input_report_key(ts->input, BTN_TOUCH, 0);
153 input_sync(ts->input);
154 }
155}
156
157static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
158{
159 struct sun4i_ts_data *ts = dev_id;
160 u32 reg_val;
161
162 reg_val = readl(ts->base + TP_INT_FIFOS);
163
164 if (reg_val & TEMP_DATA_PENDING)
165 ts->temp_data = readl(ts->base + TEMP_DATA);
166
167 if (ts->input)
168 sun4i_ts_irq_handle_input(ts, reg_val);
169
170 writel(reg_val, ts->base + TP_INT_FIFOS);
171
172 return IRQ_HANDLED;
173}
174
175static int sun4i_ts_open(struct input_dev *dev)
176{
177 struct sun4i_ts_data *ts = input_get_drvdata(dev);
178
179 /* Flush, set trig level to 1, enable temp, data and up irqs */
180 writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
181 TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
182
183 return 0;
184}
185
186static void sun4i_ts_close(struct input_dev *dev)
187{
188 struct sun4i_ts_data *ts = input_get_drvdata(dev);
189
190 /* Deactivate all input IRQs */
191 writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
192}
193
194static int sun4i_get_temp(const struct sun4i_ts_data *ts, int *temp)
195{
196 /* No temp_data until the first irq */
197 if (ts->temp_data == -1)
198 return -EAGAIN;
199
200 *temp = ts->temp_data * ts->temp_step - ts->temp_offset;
201
202 return 0;
203}
204
205static int sun4i_get_tz_temp(void *data, int *temp)
206{
207 return sun4i_get_temp(data, temp);
208}
209
210static struct thermal_zone_of_device_ops sun4i_ts_tz_ops = {
211 .get_temp = sun4i_get_tz_temp,
212};
213
214static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
215 char *buf)
216{
217 struct sun4i_ts_data *ts = dev_get_drvdata(dev);
218 int temp;
219 int error;
220
221 error = sun4i_get_temp(ts, &temp);
222 if (error)
223 return error;
224
225 return sprintf(buf, "%d\n", temp);
226}
227
228static ssize_t show_temp_label(struct device *dev,
229 struct device_attribute *devattr, char *buf)
230{
231 return sprintf(buf, "SoC temperature\n");
232}
233
234static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
235static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
236
237static struct attribute *sun4i_ts_attrs[] = {
238 &dev_attr_temp1_input.attr,
239 &dev_attr_temp1_label.attr,
240 NULL
241};
242ATTRIBUTE_GROUPS(sun4i_ts);
243
244static int sun4i_ts_probe(struct platform_device *pdev)
245{
246 struct sun4i_ts_data *ts;
247 struct device *dev = &pdev->dev;
248 struct device_node *np = dev->of_node;
249 struct device *hwmon;
250 int error;
251 u32 reg;
252 bool ts_attached;
253 u32 tp_sensitive_adjust = 15;
254 u32 filter_type = 1;
255
256 ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
257 if (!ts)
258 return -ENOMEM;
259
260 ts->dev = dev;
261 ts->ignore_fifo_data = true;
262 ts->temp_data = -1;
263 if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts")) {
264 /* Allwinner SDK has temperature (C) = (value / 6) - 271 */
265 ts->temp_offset = 271000;
266 ts->temp_step = 167;
267 } else if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts")) {
268 /*
269 * The A10 temperature sensor has quite a wide spread, these
270 * parameters are based on the averaging of the calibration
271 * results of 4 completely different boards, with a spread of
272 * temp_step from 0.096 - 0.170 and temp_offset from 176 - 331.
273 */
274 ts->temp_offset = 257000;
275 ts->temp_step = 133;
276 } else {
277 /*
278 * The user manuals do not contain the formula for calculating
279 * the temperature. The formula used here is from the AXP209,
280 * which is designed by X-Powers, an affiliate of Allwinner:
281 *
282 * temperature (C) = (value * 0.1) - 144.7
283 *
284 * Allwinner does not have any documentation whatsoever for
285 * this hardware. Moreover, it is claimed that the sensor
286 * is inaccurate and cannot work properly.
287 */
288 ts->temp_offset = 144700;
289 ts->temp_step = 100;
290 }
291
292 ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
293 if (ts_attached) {
294 ts->input = devm_input_allocate_device(dev);
295 if (!ts->input)
296 return -ENOMEM;
297
298 ts->input->name = pdev->name;
299 ts->input->phys = "sun4i_ts/input0";
300 ts->input->open = sun4i_ts_open;
301 ts->input->close = sun4i_ts_close;
302 ts->input->id.bustype = BUS_HOST;
303 ts->input->id.vendor = 0x0001;
304 ts->input->id.product = 0x0001;
305 ts->input->id.version = 0x0100;
306 ts->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
307 __set_bit(BTN_TOUCH, ts->input->keybit);
308 input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
309 input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
310 input_set_drvdata(ts->input, ts);
311 }
312
313 ts->base = devm_ioremap_resource(dev,
314 platform_get_resource(pdev, IORESOURCE_MEM, 0));
315 if (IS_ERR(ts->base))
316 return PTR_ERR(ts->base);
317
318 ts->irq = platform_get_irq(pdev, 0);
319 error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
320 if (error)
321 return error;
322
323 /*
324 * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
325 * t_acq = clkin / (16 * 64)
326 */
327 writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
328 ts->base + TP_CTRL0);
329
330 /*
331 * tp_sensitive_adjust is an optional property
332 * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
333 */
334 of_property_read_u32(np, "allwinner,tp-sensitive-adjust",
335 &tp_sensitive_adjust);
336 writel(TP_SENSITIVE_ADJUST(tp_sensitive_adjust) | TP_MODE_SELECT(0),
337 ts->base + TP_CTRL2);
338
339 /*
340 * Enable median and averaging filter, optional property for
341 * filter type.
342 */
343 of_property_read_u32(np, "allwinner,filter-type", &filter_type);
344 writel(FILTER_EN(1) | FILTER_TYPE(filter_type), ts->base + TP_CTRL3);
345
346 /* Enable temperature measurement, period 1953 (2 seconds) */
347 writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
348
349 /*
350 * Set stylus up debounce to aprox 10 ms, enable debounce, and
351 * finally enable tp mode.
352 */
353 reg = STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1);
354 if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts"))
355 reg |= SUN6I_TP_MODE_EN(1);
356 else
357 reg |= TP_MODE_EN(1);
358 writel(reg, ts->base + TP_CTRL1);
359
360 /*
361 * The thermal core does not register hwmon devices for DT-based
362 * thermal zone sensors, such as this one.
363 */
364 hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
365 ts, sun4i_ts_groups);
366 if (IS_ERR(hwmon))
367 return PTR_ERR(hwmon);
368
369 ts->tz = thermal_zone_of_sensor_register(ts->dev, 0, ts,
370 &sun4i_ts_tz_ops);
371 if (IS_ERR(ts->tz))
372 ts->tz = NULL;
373
374 writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
375
376 if (ts_attached) {
377 error = input_register_device(ts->input);
378 if (error) {
379 writel(0, ts->base + TP_INT_FIFOC);
380 thermal_zone_of_sensor_unregister(ts->dev, ts->tz);
381 return error;
382 }
383 }
384
385 platform_set_drvdata(pdev, ts);
386 return 0;
387}
388
389static int sun4i_ts_remove(struct platform_device *pdev)
390{
391 struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
392
393 /* Explicit unregister to avoid open/close changing the imask later */
394 if (ts->input)
395 input_unregister_device(ts->input);
396
397 thermal_zone_of_sensor_unregister(ts->dev, ts->tz);
398
399 /* Deactivate all IRQs */
400 writel(0, ts->base + TP_INT_FIFOC);
401
402 return 0;
403}
404
405static const struct of_device_id sun4i_ts_of_match[] = {
406 { .compatible = "allwinner,sun4i-a10-ts", },
407 { .compatible = "allwinner,sun5i-a13-ts", },
408 { .compatible = "allwinner,sun6i-a31-ts", },
409 { /* sentinel */ }
410};
411MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
412
413static struct platform_driver sun4i_ts_driver = {
414 .driver = {
415 .name = "sun4i-ts",
416 .of_match_table = of_match_ptr(sun4i_ts_of_match),
417 },
418 .probe = sun4i_ts_probe,
419 .remove = sun4i_ts_remove,
420};
421
422module_platform_driver(sun4i_ts_driver);
423
424MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
425MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
426MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Allwinner sunxi resistive touchscreen controller driver
4 *
5 * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
6 *
7 * The hwmon parts are based on work by Corentin LABBE which is:
8 * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
9 */
10
11/*
12 * The sun4i-ts controller is capable of detecting a second touch, but when a
13 * second touch is present then the accuracy becomes so bad the reported touch
14 * location is not useable.
15 *
16 * The original android driver contains some complicated heuristics using the
17 * aprox. distance between the 2 touches to see if the user is making a pinch
18 * open / close movement, and then reports emulated multi-touch events around
19 * the last touch coordinate (as the dual-touch coordinates are worthless).
20 *
21 * These kinds of heuristics are just asking for trouble (and don't belong
22 * in the kernel). So this driver offers straight forward, reliable single
23 * touch functionality only.
24 *
25 * s.a. A20 User Manual "1.15 TP" (Documentation/arm/sunxi.rst)
26 * (looks like the description in the A20 User Manual v1.3 is better
27 * than the one in the A10 User Manual v.1.5)
28 */
29
30#include <linux/err.h>
31#include <linux/hwmon.h>
32#include <linux/thermal.h>
33#include <linux/init.h>
34#include <linux/input.h>
35#include <linux/interrupt.h>
36#include <linux/io.h>
37#include <linux/module.h>
38#include <linux/of_platform.h>
39#include <linux/platform_device.h>
40#include <linux/slab.h>
41
42#define TP_CTRL0 0x00
43#define TP_CTRL1 0x04
44#define TP_CTRL2 0x08
45#define TP_CTRL3 0x0c
46#define TP_INT_FIFOC 0x10
47#define TP_INT_FIFOS 0x14
48#define TP_TPR 0x18
49#define TP_CDAT 0x1c
50#define TEMP_DATA 0x20
51#define TP_DATA 0x24
52
53/* TP_CTRL0 bits */
54#define ADC_FIRST_DLY(x) ((x) << 24) /* 8 bits */
55#define ADC_FIRST_DLY_MODE(x) ((x) << 23)
56#define ADC_CLK_SEL(x) ((x) << 22)
57#define ADC_CLK_DIV(x) ((x) << 20) /* 3 bits */
58#define FS_DIV(x) ((x) << 16) /* 4 bits */
59#define T_ACQ(x) ((x) << 0) /* 16 bits */
60
61/* TP_CTRL1 bits */
62#define STYLUS_UP_DEBOUN(x) ((x) << 12) /* 8 bits */
63#define STYLUS_UP_DEBOUN_EN(x) ((x) << 9)
64#define TOUCH_PAN_CALI_EN(x) ((x) << 6)
65#define TP_DUAL_EN(x) ((x) << 5)
66#define TP_MODE_EN(x) ((x) << 4)
67#define TP_ADC_SELECT(x) ((x) << 3)
68#define ADC_CHAN_SELECT(x) ((x) << 0) /* 3 bits */
69
70/* on sun6i, bits 3~6 are left shifted by 1 to 4~7 */
71#define SUN6I_TP_MODE_EN(x) ((x) << 5)
72
73/* TP_CTRL2 bits */
74#define TP_SENSITIVE_ADJUST(x) ((x) << 28) /* 4 bits */
75#define TP_MODE_SELECT(x) ((x) << 26) /* 2 bits */
76#define PRE_MEA_EN(x) ((x) << 24)
77#define PRE_MEA_THRE_CNT(x) ((x) << 0) /* 24 bits */
78
79/* TP_CTRL3 bits */
80#define FILTER_EN(x) ((x) << 2)
81#define FILTER_TYPE(x) ((x) << 0) /* 2 bits */
82
83/* TP_INT_FIFOC irq and fifo mask / control bits */
84#define TEMP_IRQ_EN(x) ((x) << 18)
85#define OVERRUN_IRQ_EN(x) ((x) << 17)
86#define DATA_IRQ_EN(x) ((x) << 16)
87#define TP_DATA_XY_CHANGE(x) ((x) << 13)
88#define FIFO_TRIG(x) ((x) << 8) /* 5 bits */
89#define DATA_DRQ_EN(x) ((x) << 7)
90#define FIFO_FLUSH(x) ((x) << 4)
91#define TP_UP_IRQ_EN(x) ((x) << 1)
92#define TP_DOWN_IRQ_EN(x) ((x) << 0)
93
94/* TP_INT_FIFOS irq and fifo status bits */
95#define TEMP_DATA_PENDING BIT(18)
96#define FIFO_OVERRUN_PENDING BIT(17)
97#define FIFO_DATA_PENDING BIT(16)
98#define TP_IDLE_FLG BIT(2)
99#define TP_UP_PENDING BIT(1)
100#define TP_DOWN_PENDING BIT(0)
101
102/* TP_TPR bits */
103#define TEMP_ENABLE(x) ((x) << 16)
104#define TEMP_PERIOD(x) ((x) << 0) /* t = x * 256 * 16 / clkin */
105
106struct sun4i_ts_data {
107 struct device *dev;
108 struct input_dev *input;
109 void __iomem *base;
110 unsigned int irq;
111 bool ignore_fifo_data;
112 int temp_data;
113 int temp_offset;
114 int temp_step;
115};
116
117static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
118{
119 u32 x, y;
120
121 if (reg_val & FIFO_DATA_PENDING) {
122 x = readl(ts->base + TP_DATA);
123 y = readl(ts->base + TP_DATA);
124 /* The 1st location reported after an up event is unreliable */
125 if (!ts->ignore_fifo_data) {
126 input_report_abs(ts->input, ABS_X, x);
127 input_report_abs(ts->input, ABS_Y, y);
128 /*
129 * The hardware has a separate down status bit, but
130 * that gets set before we get the first location,
131 * resulting in reporting a click on the old location.
132 */
133 input_report_key(ts->input, BTN_TOUCH, 1);
134 input_sync(ts->input);
135 } else {
136 ts->ignore_fifo_data = false;
137 }
138 }
139
140 if (reg_val & TP_UP_PENDING) {
141 ts->ignore_fifo_data = true;
142 input_report_key(ts->input, BTN_TOUCH, 0);
143 input_sync(ts->input);
144 }
145}
146
147static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
148{
149 struct sun4i_ts_data *ts = dev_id;
150 u32 reg_val;
151
152 reg_val = readl(ts->base + TP_INT_FIFOS);
153
154 if (reg_val & TEMP_DATA_PENDING)
155 ts->temp_data = readl(ts->base + TEMP_DATA);
156
157 if (ts->input)
158 sun4i_ts_irq_handle_input(ts, reg_val);
159
160 writel(reg_val, ts->base + TP_INT_FIFOS);
161
162 return IRQ_HANDLED;
163}
164
165static int sun4i_ts_open(struct input_dev *dev)
166{
167 struct sun4i_ts_data *ts = input_get_drvdata(dev);
168
169 /* Flush, set trig level to 1, enable temp, data and up irqs */
170 writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
171 TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
172
173 return 0;
174}
175
176static void sun4i_ts_close(struct input_dev *dev)
177{
178 struct sun4i_ts_data *ts = input_get_drvdata(dev);
179
180 /* Deactivate all input IRQs */
181 writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
182}
183
184static int sun4i_get_temp(const struct sun4i_ts_data *ts, int *temp)
185{
186 /* No temp_data until the first irq */
187 if (ts->temp_data == -1)
188 return -EAGAIN;
189
190 *temp = ts->temp_data * ts->temp_step - ts->temp_offset;
191
192 return 0;
193}
194
195static int sun4i_get_tz_temp(void *data, int *temp)
196{
197 return sun4i_get_temp(data, temp);
198}
199
200static const struct thermal_zone_of_device_ops sun4i_ts_tz_ops = {
201 .get_temp = sun4i_get_tz_temp,
202};
203
204static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
205 char *buf)
206{
207 struct sun4i_ts_data *ts = dev_get_drvdata(dev);
208 int temp;
209 int error;
210
211 error = sun4i_get_temp(ts, &temp);
212 if (error)
213 return error;
214
215 return sprintf(buf, "%d\n", temp);
216}
217
218static ssize_t show_temp_label(struct device *dev,
219 struct device_attribute *devattr, char *buf)
220{
221 return sprintf(buf, "SoC temperature\n");
222}
223
224static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
225static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
226
227static struct attribute *sun4i_ts_attrs[] = {
228 &dev_attr_temp1_input.attr,
229 &dev_attr_temp1_label.attr,
230 NULL
231};
232ATTRIBUTE_GROUPS(sun4i_ts);
233
234static int sun4i_ts_probe(struct platform_device *pdev)
235{
236 struct sun4i_ts_data *ts;
237 struct device *dev = &pdev->dev;
238 struct device_node *np = dev->of_node;
239 struct device *hwmon;
240 int error;
241 u32 reg;
242 bool ts_attached;
243 u32 tp_sensitive_adjust = 15;
244 u32 filter_type = 1;
245
246 ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
247 if (!ts)
248 return -ENOMEM;
249
250 ts->dev = dev;
251 ts->ignore_fifo_data = true;
252 ts->temp_data = -1;
253 if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts")) {
254 /* Allwinner SDK has temperature (C) = (value / 6) - 271 */
255 ts->temp_offset = 271000;
256 ts->temp_step = 167;
257 } else if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts")) {
258 /*
259 * The A10 temperature sensor has quite a wide spread, these
260 * parameters are based on the averaging of the calibration
261 * results of 4 completely different boards, with a spread of
262 * temp_step from 0.096 - 0.170 and temp_offset from 176 - 331.
263 */
264 ts->temp_offset = 257000;
265 ts->temp_step = 133;
266 } else {
267 /*
268 * The user manuals do not contain the formula for calculating
269 * the temperature. The formula used here is from the AXP209,
270 * which is designed by X-Powers, an affiliate of Allwinner:
271 *
272 * temperature (C) = (value * 0.1) - 144.7
273 *
274 * Allwinner does not have any documentation whatsoever for
275 * this hardware. Moreover, it is claimed that the sensor
276 * is inaccurate and cannot work properly.
277 */
278 ts->temp_offset = 144700;
279 ts->temp_step = 100;
280 }
281
282 ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
283 if (ts_attached) {
284 ts->input = devm_input_allocate_device(dev);
285 if (!ts->input)
286 return -ENOMEM;
287
288 ts->input->name = pdev->name;
289 ts->input->phys = "sun4i_ts/input0";
290 ts->input->open = sun4i_ts_open;
291 ts->input->close = sun4i_ts_close;
292 ts->input->id.bustype = BUS_HOST;
293 ts->input->id.vendor = 0x0001;
294 ts->input->id.product = 0x0001;
295 ts->input->id.version = 0x0100;
296 ts->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
297 __set_bit(BTN_TOUCH, ts->input->keybit);
298 input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
299 input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
300 input_set_drvdata(ts->input, ts);
301 }
302
303 ts->base = devm_platform_ioremap_resource(pdev, 0);
304 if (IS_ERR(ts->base))
305 return PTR_ERR(ts->base);
306
307 ts->irq = platform_get_irq(pdev, 0);
308 error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
309 if (error)
310 return error;
311
312 /*
313 * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
314 * t_acq = clkin / (16 * 64)
315 */
316 writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
317 ts->base + TP_CTRL0);
318
319 /*
320 * tp_sensitive_adjust is an optional property
321 * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
322 */
323 of_property_read_u32(np, "allwinner,tp-sensitive-adjust",
324 &tp_sensitive_adjust);
325 writel(TP_SENSITIVE_ADJUST(tp_sensitive_adjust) | TP_MODE_SELECT(0),
326 ts->base + TP_CTRL2);
327
328 /*
329 * Enable median and averaging filter, optional property for
330 * filter type.
331 */
332 of_property_read_u32(np, "allwinner,filter-type", &filter_type);
333 writel(FILTER_EN(1) | FILTER_TYPE(filter_type), ts->base + TP_CTRL3);
334
335 /* Enable temperature measurement, period 1953 (2 seconds) */
336 writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
337
338 /*
339 * Set stylus up debounce to aprox 10 ms, enable debounce, and
340 * finally enable tp mode.
341 */
342 reg = STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1);
343 if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts"))
344 reg |= SUN6I_TP_MODE_EN(1);
345 else
346 reg |= TP_MODE_EN(1);
347 writel(reg, ts->base + TP_CTRL1);
348
349 /*
350 * The thermal core does not register hwmon devices for DT-based
351 * thermal zone sensors, such as this one.
352 */
353 hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
354 ts, sun4i_ts_groups);
355 if (IS_ERR(hwmon))
356 return PTR_ERR(hwmon);
357
358 devm_thermal_zone_of_sensor_register(ts->dev, 0, ts, &sun4i_ts_tz_ops);
359
360 writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
361
362 if (ts_attached) {
363 error = input_register_device(ts->input);
364 if (error) {
365 writel(0, ts->base + TP_INT_FIFOC);
366 return error;
367 }
368 }
369
370 platform_set_drvdata(pdev, ts);
371 return 0;
372}
373
374static int sun4i_ts_remove(struct platform_device *pdev)
375{
376 struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
377
378 /* Explicit unregister to avoid open/close changing the imask later */
379 if (ts->input)
380 input_unregister_device(ts->input);
381
382 /* Deactivate all IRQs */
383 writel(0, ts->base + TP_INT_FIFOC);
384
385 return 0;
386}
387
388static const struct of_device_id sun4i_ts_of_match[] = {
389 { .compatible = "allwinner,sun4i-a10-ts", },
390 { .compatible = "allwinner,sun5i-a13-ts", },
391 { .compatible = "allwinner,sun6i-a31-ts", },
392 { /* sentinel */ }
393};
394MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
395
396static struct platform_driver sun4i_ts_driver = {
397 .driver = {
398 .name = "sun4i-ts",
399 .of_match_table = of_match_ptr(sun4i_ts_of_match),
400 },
401 .probe = sun4i_ts_probe,
402 .remove = sun4i_ts_remove,
403};
404
405module_platform_driver(sun4i_ts_driver);
406
407MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
408MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
409MODULE_LICENSE("GPL");