sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / input / touchscreen / sun4i-ts.c
blob2ba8260249548c29e5f1d95a4aff0f7726062aa1
1 /*
2 * Allwinner sunxi resistive touchscreen controller driver
4 * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
6 * The hwmon parts are based on work by Corentin LABBE which is:
7 * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
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.
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.
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.
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).
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.
35 #include <linux/err.h>
36 #include <linux/hwmon.h>
37 #include <linux/init.h>
38 #include <linux/input.h>
39 #include <linux/interrupt.h>
40 #include <linux/io.h>
41 #include <linux/module.h>
42 #include <linux/of_platform.h>
43 #include <linux/platform_device.h>
44 #include <linux/slab.h>
46 #define TP_CTRL0 0x00
47 #define TP_CTRL1 0x04
48 #define TP_CTRL2 0x08
49 #define TP_CTRL3 0x0c
50 #define TP_INT_FIFOC 0x10
51 #define TP_INT_FIFOS 0x14
52 #define TP_TPR 0x18
53 #define TP_CDAT 0x1c
54 #define TEMP_DATA 0x20
55 #define TP_DATA 0x24
57 /* TP_CTRL0 bits */
58 #define ADC_FIRST_DLY(x) ((x) << 24) /* 8 bits */
59 #define ADC_FIRST_DLY_MODE(x) ((x) << 23)
60 #define ADC_CLK_SEL(x) ((x) << 22)
61 #define ADC_CLK_DIV(x) ((x) << 20) /* 3 bits */
62 #define FS_DIV(x) ((x) << 16) /* 4 bits */
63 #define T_ACQ(x) ((x) << 0) /* 16 bits */
65 /* TP_CTRL1 bits */
66 #define STYLUS_UP_DEBOUN(x) ((x) << 12) /* 8 bits */
67 #define STYLUS_UP_DEBOUN_EN(x) ((x) << 9)
68 #define TOUCH_PAN_CALI_EN(x) ((x) << 6)
69 #define TP_DUAL_EN(x) ((x) << 5)
70 #define TP_MODE_EN(x) ((x) << 4)
71 #define TP_ADC_SELECT(x) ((x) << 3)
72 #define ADC_CHAN_SELECT(x) ((x) << 0) /* 3 bits */
74 /* TP_CTRL2 bits */
75 #define TP_SENSITIVE_ADJUST(x) ((x) << 28) /* 4 bits */
76 #define TP_MODE_SELECT(x) ((x) << 26) /* 2 bits */
77 #define PRE_MEA_EN(x) ((x) << 24)
78 #define PRE_MEA_THRE_CNT(x) ((x) << 0) /* 24 bits */
80 /* TP_CTRL3 bits */
81 #define FILTER_EN(x) ((x) << 2)
82 #define FILTER_TYPE(x) ((x) << 0) /* 2 bits */
84 /* TP_INT_FIFOC irq and fifo mask / control bits */
85 #define TEMP_IRQ_EN(x) ((x) << 18)
86 #define OVERRUN_IRQ_EN(x) ((x) << 17)
87 #define DATA_IRQ_EN(x) ((x) << 16)
88 #define TP_DATA_XY_CHANGE(x) ((x) << 13)
89 #define FIFO_TRIG(x) ((x) << 8) /* 5 bits */
90 #define DATA_DRQ_EN(x) ((x) << 7)
91 #define FIFO_FLUSH(x) ((x) << 4)
92 #define TP_UP_IRQ_EN(x) ((x) << 1)
93 #define TP_DOWN_IRQ_EN(x) ((x) << 0)
95 /* TP_INT_FIFOS irq and fifo status bits */
96 #define TEMP_DATA_PENDING BIT(18)
97 #define FIFO_OVERRUN_PENDING BIT(17)
98 #define FIFO_DATA_PENDING BIT(16)
99 #define TP_IDLE_FLG BIT(2)
100 #define TP_UP_PENDING BIT(1)
101 #define TP_DOWN_PENDING BIT(0)
103 /* TP_TPR bits */
104 #define TEMP_ENABLE(x) ((x) << 16)
105 #define TEMP_PERIOD(x) ((x) << 0) /* t = x * 256 * 16 / clkin */
107 struct sun4i_ts_data {
108 struct device *dev;
109 struct input_dev *input;
110 void __iomem *base;
111 unsigned int irq;
112 bool ignore_fifo_data;
113 int temp_data;
116 static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
118 u32 x, y;
120 if (reg_val & FIFO_DATA_PENDING) {
121 x = readl(ts->base + TP_DATA);
122 y = readl(ts->base + TP_DATA);
123 /* The 1st location reported after an up event is unreliable */
124 if (!ts->ignore_fifo_data) {
125 input_report_abs(ts->input, ABS_X, x);
126 input_report_abs(ts->input, ABS_Y, y);
128 * The hardware has a separate down status bit, but
129 * that gets set before we get the first location,
130 * resulting in reporting a click on the old location.
132 input_report_key(ts->input, BTN_TOUCH, 1);
133 input_sync(ts->input);
134 } else {
135 ts->ignore_fifo_data = false;
139 if (reg_val & TP_UP_PENDING) {
140 ts->ignore_fifo_data = true;
141 input_report_key(ts->input, BTN_TOUCH, 0);
142 input_sync(ts->input);
146 static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
148 struct sun4i_ts_data *ts = dev_id;
149 u32 reg_val;
151 reg_val = readl(ts->base + TP_INT_FIFOS);
153 if (reg_val & TEMP_DATA_PENDING)
154 ts->temp_data = readl(ts->base + TEMP_DATA);
156 if (ts->input)
157 sun4i_ts_irq_handle_input(ts, reg_val);
159 writel(reg_val, ts->base + TP_INT_FIFOS);
161 return IRQ_HANDLED;
164 static int sun4i_ts_open(struct input_dev *dev)
166 struct sun4i_ts_data *ts = input_get_drvdata(dev);
168 /* Flush, set trig level to 1, enable temp, data and up irqs */
169 writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
170 TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
172 return 0;
175 static void sun4i_ts_close(struct input_dev *dev)
177 struct sun4i_ts_data *ts = input_get_drvdata(dev);
179 /* Deactivate all input IRQs */
180 writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
183 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
184 char *buf)
186 struct sun4i_ts_data *ts = dev_get_drvdata(dev);
188 /* No temp_data until the first irq */
189 if (ts->temp_data == -1)
190 return -EAGAIN;
192 return sprintf(buf, "%d\n", (ts->temp_data - 1447) * 100);
195 static ssize_t show_temp_label(struct device *dev,
196 struct device_attribute *devattr, char *buf)
198 return sprintf(buf, "SoC temperature\n");
201 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
202 static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
204 static struct attribute *sun4i_ts_attrs[] = {
205 &dev_attr_temp1_input.attr,
206 &dev_attr_temp1_label.attr,
207 NULL
209 ATTRIBUTE_GROUPS(sun4i_ts);
211 static int sun4i_ts_probe(struct platform_device *pdev)
213 struct sun4i_ts_data *ts;
214 struct device *dev = &pdev->dev;
215 struct device_node *np = dev->of_node;
216 struct device *hwmon;
217 int error;
218 bool ts_attached;
220 ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
221 if (!ts)
222 return -ENOMEM;
224 ts->dev = dev;
225 ts->ignore_fifo_data = true;
226 ts->temp_data = -1;
228 ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
229 if (ts_attached) {
230 ts->input = devm_input_allocate_device(dev);
231 if (!ts->input)
232 return -ENOMEM;
234 ts->input->name = pdev->name;
235 ts->input->phys = "sun4i_ts/input0";
236 ts->input->open = sun4i_ts_open;
237 ts->input->close = sun4i_ts_close;
238 ts->input->id.bustype = BUS_HOST;
239 ts->input->id.vendor = 0x0001;
240 ts->input->id.product = 0x0001;
241 ts->input->id.version = 0x0100;
242 ts->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
243 __set_bit(BTN_TOUCH, ts->input->keybit);
244 input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
245 input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
246 input_set_drvdata(ts->input, ts);
249 ts->base = devm_ioremap_resource(dev,
250 platform_get_resource(pdev, IORESOURCE_MEM, 0));
251 if (IS_ERR(ts->base))
252 return PTR_ERR(ts->base);
254 ts->irq = platform_get_irq(pdev, 0);
255 error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
256 if (error)
257 return error;
260 * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
261 * t_acq = clkin / (16 * 64)
263 writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
264 ts->base + TP_CTRL0);
267 * sensitive_adjust = 15 : max, which is not all that sensitive,
268 * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
270 writel(TP_SENSITIVE_ADJUST(15) | TP_MODE_SELECT(0),
271 ts->base + TP_CTRL2);
273 /* Enable median filter, type 1 : 5/3 */
274 writel(FILTER_EN(1) | FILTER_TYPE(1), ts->base + TP_CTRL3);
276 /* Enable temperature measurement, period 1953 (2 seconds) */
277 writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
280 * Set stylus up debounce to aprox 10 ms, enable debounce, and
281 * finally enable tp mode.
283 writel(STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1) | TP_MODE_EN(1),
284 ts->base + TP_CTRL1);
286 hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
287 ts, sun4i_ts_groups);
288 if (IS_ERR(hwmon))
289 return PTR_ERR(hwmon);
291 writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
293 if (ts_attached) {
294 error = input_register_device(ts->input);
295 if (error) {
296 writel(0, ts->base + TP_INT_FIFOC);
297 return error;
301 platform_set_drvdata(pdev, ts);
302 return 0;
305 static int sun4i_ts_remove(struct platform_device *pdev)
307 struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
309 /* Explicit unregister to avoid open/close changing the imask later */
310 if (ts->input)
311 input_unregister_device(ts->input);
313 /* Deactivate all IRQs */
314 writel(0, ts->base + TP_INT_FIFOC);
316 return 0;
319 static const struct of_device_id sun4i_ts_of_match[] = {
320 { .compatible = "allwinner,sun4i-a10-ts", },
321 { /* sentinel */ }
323 MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
325 static struct platform_driver sun4i_ts_driver = {
326 .driver = {
327 .owner = THIS_MODULE,
328 .name = "sun4i-ts",
329 .of_match_table = of_match_ptr(sun4i_ts_of_match),
331 .probe = sun4i_ts_probe,
332 .remove = sun4i_ts_remove,
335 module_platform_driver(sun4i_ts_driver);
337 MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
338 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
339 MODULE_LICENSE("GPL");