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