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