]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/input/touchscreen/stmpe-ts.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 151
[mirror_ubuntu-focal-kernel.git] / drivers / input / touchscreen / stmpe-ts.c
CommitLineData
2bd942f9
VK
1/*
2 * STMicroelectronics STMPE811 Touchscreen Driver
f94add3b
LF
3 *
4 * (C) 2010 Luotao Fu <l.fu@pengutronix.de>
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/sched.h>
17#include <linux/interrupt.h>
f94add3b 18#include <linux/device.h>
037db524 19#include <linux/of.h>
f94add3b
LF
20#include <linux/platform_device.h>
21#include <linux/input.h>
22#include <linux/slab.h>
23#include <linux/delay.h>
24#include <linux/i2c.h>
25#include <linux/workqueue.h>
26
27#include <linux/mfd/stmpe.h>
28
29/* Register layouts and functionalities are identical on all stmpexxx variants
30 * with touchscreen controller
31 */
32#define STMPE_REG_INT_STA 0x0B
f94add3b
LF
33#define STMPE_REG_TSC_CTRL 0x40
34#define STMPE_REG_TSC_CFG 0x41
35#define STMPE_REG_FIFO_TH 0x4A
36#define STMPE_REG_FIFO_STA 0x4B
37#define STMPE_REG_FIFO_SIZE 0x4C
38#define STMPE_REG_TSC_DATA_XYZ 0x52
39#define STMPE_REG_TSC_FRACTION_Z 0x56
40#define STMPE_REG_TSC_I_DRIVE 0x58
41
42#define OP_MOD_XYZ 0
43
44#define STMPE_TSC_CTRL_TSC_EN (1<<0)
45
46#define STMPE_FIFO_STA_RESET (1<<0)
47
48#define STMPE_IRQ_TOUCH_DET 0
49
f94add3b
LF
50#define STMPE_TS_NAME "stmpe-ts"
51#define XY_MASK 0xfff
52
564a68de
LW
53/**
54 * struct stmpe_touch - stmpe811 touch screen controller state
55 * @stmpe: pointer back to STMPE MFD container
56 * @idev: registered input device
57 * @work: a work item used to scan the device
58 * @dev: a pointer back to the MFD cell struct device*
564a68de
LW
59 * @ave_ctrl: Sample average control
60 * (0 -> 1 sample, 1 -> 2 samples, 2 -> 4 samples, 3 -> 8 samples)
61 * @touch_det_delay: Touch detect interrupt delay
62 * (0 -> 10 us, 1 -> 50 us, 2 -> 100 us, 3 -> 500 us,
63 * 4-> 1 ms, 5 -> 5 ms, 6 -> 10 ms, 7 -> 50 ms)
64 * recommended is 3
65 * @settling: Panel driver settling time
66 * (0 -> 10 us, 1 -> 100 us, 2 -> 500 us, 3 -> 1 ms,
67 * 4 -> 5 ms, 5 -> 10 ms, 6 for 50 ms, 7 -> 100 ms)
68 * recommended is 2
69 * @fraction_z: Length of the fractional part in z
70 * (fraction_z ([0..7]) = Count of the fractional part)
71 * recommended is 7
72 * @i_drive: current limit value of the touchscreen drivers
73 * (0 -> 20 mA typical 35 mA max, 1 -> 50 mA typical 80 mA max)
74 */
f94add3b
LF
75struct stmpe_touch {
76 struct stmpe *stmpe;
77 struct input_dev *idev;
78 struct delayed_work work;
79 struct device *dev;
f94add3b
LF
80 u8 ave_ctrl;
81 u8 touch_det_delay;
82 u8 settling;
83 u8 fraction_z;
84 u8 i_drive;
85};
86
87static int __stmpe_reset_fifo(struct stmpe *stmpe)
88{
89 int ret;
90
91 ret = stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
92 STMPE_FIFO_STA_RESET, STMPE_FIFO_STA_RESET);
93 if (ret)
94 return ret;
95
96 return stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
97 STMPE_FIFO_STA_RESET, 0);
98}
99
100static void stmpe_work(struct work_struct *work)
101{
102 int int_sta;
103 u32 timeout = 40;
104
105 struct stmpe_touch *ts =
106 container_of(work, struct stmpe_touch, work.work);
107
108 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
109
110 /*
111 * touch_det sometimes get desasserted or just get stuck. This appears
112 * to be a silicon bug, We still have to clearify this with the
113 * manufacture. As a workaround We release the key anyway if the
114 * touch_det keeps coming in after 4ms, while the FIFO contains no value
115 * during the whole time.
116 */
117 while ((int_sta & (1 << STMPE_IRQ_TOUCH_DET)) && (timeout > 0)) {
118 timeout--;
119 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
120 udelay(100);
121 }
122
123 /* reset the FIFO before we report release event */
124 __stmpe_reset_fifo(ts->stmpe);
125
126 input_report_abs(ts->idev, ABS_PRESSURE, 0);
7488b1b9 127 input_report_key(ts->idev, BTN_TOUCH, 0);
f94add3b
LF
128 input_sync(ts->idev);
129}
130
131static irqreturn_t stmpe_ts_handler(int irq, void *data)
132{
133 u8 data_set[4];
134 int x, y, z;
135 struct stmpe_touch *ts = data;
136
137 /*
138 * Cancel scheduled polling for release if we have new value
139 * available. Wait if the polling is already running.
140 */
141 cancel_delayed_work_sync(&ts->work);
142
143 /*
144 * The FIFO sometimes just crashes and stops generating interrupts. This
145 * appears to be a silicon bug. We still have to clearify this with
146 * the manufacture. As a workaround we disable the TSC while we are
147 * collecting data and flush the FIFO after reading
148 */
149 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
150 STMPE_TSC_CTRL_TSC_EN, 0);
151
152 stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
153
154 x = (data_set[0] << 4) | (data_set[1] >> 4);
155 y = ((data_set[1] & 0xf) << 8) | data_set[2];
156 z = data_set[3];
157
158 input_report_abs(ts->idev, ABS_X, x);
159 input_report_abs(ts->idev, ABS_Y, y);
160 input_report_abs(ts->idev, ABS_PRESSURE, z);
7488b1b9 161 input_report_key(ts->idev, BTN_TOUCH, 1);
f94add3b
LF
162 input_sync(ts->idev);
163
164 /* flush the FIFO after we have read out our values. */
165 __stmpe_reset_fifo(ts->stmpe);
166
167 /* reenable the tsc */
168 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
169 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
170
171 /* start polling for touch_det to detect release */
9ceeb59c 172 schedule_delayed_work(&ts->work, msecs_to_jiffies(50));
f94add3b
LF
173
174 return IRQ_HANDLED;
175}
176
5298cc4c 177static int stmpe_init_hw(struct stmpe_touch *ts)
f94add3b
LF
178{
179 int ret;
88f29d0f 180 u8 tsc_cfg, tsc_cfg_mask;
f94add3b
LF
181 struct stmpe *stmpe = ts->stmpe;
182 struct device *dev = ts->dev;
183
184 ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
185 if (ret) {
186 dev_err(dev, "Could not enable clock for ADC and TS\n");
187 return ret;
188 }
189
88f29d0f 190 ret = stmpe811_adc_common_init(stmpe);
f94add3b 191 if (ret) {
88f29d0f 192 stmpe_disable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
f94add3b
LF
193 return ret;
194 }
195
063755ab
PS
196 tsc_cfg = STMPE_AVE_CTRL(ts->ave_ctrl) |
197 STMPE_DET_DELAY(ts->touch_det_delay) |
198 STMPE_SETTLING(ts->settling);
199 tsc_cfg_mask = STMPE_AVE_CTRL(0xff) | STMPE_DET_DELAY(0xff) |
200 STMPE_SETTLING(0xff);
f94add3b
LF
201
202 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
203 if (ret) {
204 dev_err(dev, "Could not config touch\n");
205 return ret;
206 }
207
208 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
063755ab 209 STMPE_FRACTION_Z(0xff), STMPE_FRACTION_Z(ts->fraction_z));
f94add3b
LF
210 if (ret) {
211 dev_err(dev, "Could not config touch\n");
212 return ret;
213 }
214
215 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
063755ab 216 STMPE_I_DRIVE(0xff), STMPE_I_DRIVE(ts->i_drive));
f94add3b
LF
217 if (ret) {
218 dev_err(dev, "Could not config touch\n");
219 return ret;
220 }
221
222 /* set FIFO to 1 for single point reading */
223 ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
224 if (ret) {
225 dev_err(dev, "Could not set FIFO\n");
226 return ret;
227 }
228
229 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
063755ab 230 STMPE_OP_MODE(0xff), STMPE_OP_MODE(OP_MOD_XYZ));
f94add3b
LF
231 if (ret) {
232 dev_err(dev, "Could not set mode\n");
233 return ret;
234 }
235
236 return 0;
237}
238
239static int stmpe_ts_open(struct input_dev *dev)
240{
241 struct stmpe_touch *ts = input_get_drvdata(dev);
242 int ret = 0;
243
244 ret = __stmpe_reset_fifo(ts->stmpe);
245 if (ret)
246 return ret;
247
248 return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
249 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
250}
251
252static void stmpe_ts_close(struct input_dev *dev)
253{
254 struct stmpe_touch *ts = input_get_drvdata(dev);
255
256 cancel_delayed_work_sync(&ts->work);
257
258 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
259 STMPE_TSC_CTRL_TSC_EN, 0);
260}
261
037db524
VKS
262static void stmpe_ts_get_platform_info(struct platform_device *pdev,
263 struct stmpe_touch *ts)
f94add3b 264{
037db524 265 struct device_node *np = pdev->dev.of_node;
e4b88e19 266 u32 val;
037db524 267
e4b88e19 268 if (np) {
037db524 269 if (!of_property_read_u32(np, "st,sample-time", &val))
88f29d0f 270 ts->stmpe->sample_time = val;
037db524 271 if (!of_property_read_u32(np, "st,mod-12b", &val))
88f29d0f 272 ts->stmpe->mod_12b = val;
037db524 273 if (!of_property_read_u32(np, "st,ref-sel", &val))
88f29d0f 274 ts->stmpe->ref_sel = val;
037db524 275 if (!of_property_read_u32(np, "st,adc-freq", &val))
88f29d0f 276 ts->stmpe->adc_freq = val;
037db524
VKS
277 if (!of_property_read_u32(np, "st,ave-ctrl", &val))
278 ts->ave_ctrl = val;
279 if (!of_property_read_u32(np, "st,touch-det-delay", &val))
280 ts->touch_det_delay = val;
281 if (!of_property_read_u32(np, "st,settling", &val))
282 ts->settling = val;
283 if (!of_property_read_u32(np, "st,fraction-z", &val))
284 ts->fraction_z = val;
285 if (!of_property_read_u32(np, "st,i-drive", &val))
286 ts->i_drive = val;
287 }
288}
289
5298cc4c 290static int stmpe_input_probe(struct platform_device *pdev)
037db524 291{
e4b88e19 292 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
f94add3b
LF
293 struct stmpe_touch *ts;
294 struct input_dev *idev;
2bd942f9 295 int error;
3faeb35c 296 int ts_irq;
f94add3b
LF
297
298 ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
299 if (ts_irq < 0)
300 return ts_irq;
301
2bd942f9
VK
302 ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
303 if (!ts)
304 return -ENOMEM;
f94add3b 305
2bd942f9
VK
306 idev = devm_input_allocate_device(&pdev->dev);
307 if (!idev)
308 return -ENOMEM;
f94add3b
LF
309
310 platform_set_drvdata(pdev, ts);
e4b88e19 311 ts->stmpe = stmpe;
f94add3b
LF
312 ts->idev = idev;
313 ts->dev = &pdev->dev;
314
037db524 315 stmpe_ts_get_platform_info(pdev, ts);
f94add3b
LF
316
317 INIT_DELAYED_WORK(&ts->work, stmpe_work);
318
2bd942f9
VK
319 error = devm_request_threaded_irq(&pdev->dev, ts_irq,
320 NULL, stmpe_ts_handler,
321 IRQF_ONESHOT, STMPE_TS_NAME, ts);
322 if (error) {
f94add3b 323 dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
2bd942f9 324 return error;
f94add3b
LF
325 }
326
2bd942f9
VK
327 error = stmpe_init_hw(ts);
328 if (error)
329 return error;
f94add3b
LF
330
331 idev->name = STMPE_TS_NAME;
b8d52e2b 332 idev->phys = STMPE_TS_NAME"/input0";
f94add3b 333 idev->id.bustype = BUS_I2C;
f94add3b
LF
334
335 idev->open = stmpe_ts_open;
336 idev->close = stmpe_ts_close;
337
338 input_set_drvdata(idev, ts);
339
e4b88e19 340 input_set_capability(idev, EV_KEY, BTN_TOUCH);
f94add3b
LF
341 input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
342 input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
343 input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
344
2bd942f9
VK
345 error = input_register_device(idev);
346 if (error) {
f94add3b 347 dev_err(&pdev->dev, "Could not register input device\n");
2bd942f9 348 return error;
f94add3b
LF
349 }
350
2bd942f9 351 return 0;
f94add3b
LF
352}
353
e2619cf7 354static int stmpe_ts_remove(struct platform_device *pdev)
f94add3b
LF
355{
356 struct stmpe_touch *ts = platform_get_drvdata(pdev);
f94add3b
LF
357
358 stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
359
f94add3b
LF
360 return 0;
361}
362
363static struct platform_driver stmpe_ts_driver = {
364 .driver = {
f91a3f08
DT
365 .name = STMPE_TS_NAME,
366 },
f94add3b 367 .probe = stmpe_input_probe,
1cb0aa88 368 .remove = stmpe_ts_remove,
f94add3b 369};
cdcc96e2 370module_platform_driver(stmpe_ts_driver);
f94add3b 371
f91a3f08
DT
372static const struct of_device_id stmpe_ts_ids[] = {
373 { .compatible = "st,stmpe-ts", },
374 { },
375};
376MODULE_DEVICE_TABLE(of, stmpe_ts_ids);
f91a3f08 377
f94add3b
LF
378MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
379MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
380MODULE_LICENSE("GPL");