1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * STMicroelectronics STMPE811 Touchscreen Driver
5 * (C) 2010 Luotao Fu <l.fu@pengutronix.de>
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>
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>
23 #include <linux/mfd/stmpe.h>
25 /* Register layouts and functionalities are identical on all stmpexxx variants
26 * with touchscreen controller
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
40 #define STMPE_TSC_CTRL_TSC_EN (1<<0)
42 #define STMPE_FIFO_STA_RESET (1<<0)
44 #define STMPE_IRQ_TOUCH_DET 0
46 #define STMPE_TS_NAME "stmpe-ts"
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 * @prop: Touchscreen properties
56 * @ave_ctrl: Sample average control
57 * (0 -> 1 sample, 1 -> 2 samples, 2 -> 4 samples, 3 -> 8 samples)
58 * @touch_det_delay: Touch detect interrupt delay
59 * (0 -> 10 us, 1 -> 50 us, 2 -> 100 us, 3 -> 500 us,
60 * 4-> 1 ms, 5 -> 5 ms, 6 -> 10 ms, 7 -> 50 ms)
62 * @settling: Panel driver settling time
63 * (0 -> 10 us, 1 -> 100 us, 2 -> 500 us, 3 -> 1 ms,
64 * 4 -> 5 ms, 5 -> 10 ms, 6 for 50 ms, 7 -> 100 ms)
66 * @fraction_z: Length of the fractional part in z
67 * (fraction_z ([0..7]) = Count of the fractional part)
69 * @i_drive: current limit value of the touchscreen drivers
70 * (0 -> 20 mA typical 35 mA max, 1 -> 50 mA typical 80 mA max)
74 struct input_dev
*idev
;
75 struct delayed_work work
;
77 struct touchscreen_properties prop
;
85 static int __stmpe_reset_fifo(struct stmpe
*stmpe
)
89 ret
= stmpe_set_bits(stmpe
, STMPE_REG_FIFO_STA
,
90 STMPE_FIFO_STA_RESET
, STMPE_FIFO_STA_RESET
);
94 return stmpe_set_bits(stmpe
, STMPE_REG_FIFO_STA
,
95 STMPE_FIFO_STA_RESET
, 0);
98 static void stmpe_work(struct work_struct
*work
)
103 struct stmpe_touch
*ts
=
104 container_of(work
, struct stmpe_touch
, work
.work
);
106 int_sta
= stmpe_reg_read(ts
->stmpe
, STMPE_REG_INT_STA
);
109 * touch_det sometimes get desasserted or just get stuck. This appears
110 * to be a silicon bug, We still have to clearify this with the
111 * manufacture. As a workaround We release the key anyway if the
112 * touch_det keeps coming in after 4ms, while the FIFO contains no value
113 * during the whole time.
115 while ((int_sta
& (1 << STMPE_IRQ_TOUCH_DET
)) && (timeout
> 0)) {
117 int_sta
= stmpe_reg_read(ts
->stmpe
, STMPE_REG_INT_STA
);
121 /* reset the FIFO before we report release event */
122 __stmpe_reset_fifo(ts
->stmpe
);
124 input_report_abs(ts
->idev
, ABS_PRESSURE
, 0);
125 input_report_key(ts
->idev
, BTN_TOUCH
, 0);
126 input_sync(ts
->idev
);
129 static irqreturn_t
stmpe_ts_handler(int irq
, void *data
)
133 struct stmpe_touch
*ts
= data
;
136 * Cancel scheduled polling for release if we have new value
137 * available. Wait if the polling is already running.
139 cancel_delayed_work_sync(&ts
->work
);
142 * The FIFO sometimes just crashes and stops generating interrupts. This
143 * appears to be a silicon bug. We still have to clearify this with
144 * the manufacture. As a workaround we disable the TSC while we are
145 * collecting data and flush the FIFO after reading
147 stmpe_set_bits(ts
->stmpe
, STMPE_REG_TSC_CTRL
,
148 STMPE_TSC_CTRL_TSC_EN
, 0);
150 stmpe_block_read(ts
->stmpe
, STMPE_REG_TSC_DATA_XYZ
, 4, data_set
);
152 x
= (data_set
[0] << 4) | (data_set
[1] >> 4);
153 y
= ((data_set
[1] & 0xf) << 8) | data_set
[2];
156 touchscreen_report_pos(ts
->idev
, &ts
->prop
, x
, y
, false);
157 input_report_abs(ts
->idev
, ABS_PRESSURE
, z
);
158 input_report_key(ts
->idev
, BTN_TOUCH
, 1);
159 input_sync(ts
->idev
);
161 /* flush the FIFO after we have read out our values. */
162 __stmpe_reset_fifo(ts
->stmpe
);
164 /* reenable the tsc */
165 stmpe_set_bits(ts
->stmpe
, STMPE_REG_TSC_CTRL
,
166 STMPE_TSC_CTRL_TSC_EN
, STMPE_TSC_CTRL_TSC_EN
);
168 /* start polling for touch_det to detect release */
169 schedule_delayed_work(&ts
->work
, msecs_to_jiffies(50));
174 static int stmpe_init_hw(struct stmpe_touch
*ts
)
177 u8 tsc_cfg
, tsc_cfg_mask
;
178 struct stmpe
*stmpe
= ts
->stmpe
;
179 struct device
*dev
= ts
->dev
;
181 ret
= stmpe_enable(stmpe
, STMPE_BLOCK_TOUCHSCREEN
| STMPE_BLOCK_ADC
);
183 dev_err(dev
, "Could not enable clock for ADC and TS\n");
187 ret
= stmpe811_adc_common_init(stmpe
);
189 stmpe_disable(stmpe
, STMPE_BLOCK_TOUCHSCREEN
| STMPE_BLOCK_ADC
);
193 tsc_cfg
= STMPE_AVE_CTRL(ts
->ave_ctrl
) |
194 STMPE_DET_DELAY(ts
->touch_det_delay
) |
195 STMPE_SETTLING(ts
->settling
);
196 tsc_cfg_mask
= STMPE_AVE_CTRL(0xff) | STMPE_DET_DELAY(0xff) |
197 STMPE_SETTLING(0xff);
199 ret
= stmpe_set_bits(stmpe
, STMPE_REG_TSC_CFG
, tsc_cfg_mask
, tsc_cfg
);
201 dev_err(dev
, "Could not config touch\n");
205 ret
= stmpe_set_bits(stmpe
, STMPE_REG_TSC_FRACTION_Z
,
206 STMPE_FRACTION_Z(0xff), STMPE_FRACTION_Z(ts
->fraction_z
));
208 dev_err(dev
, "Could not config touch\n");
212 ret
= stmpe_set_bits(stmpe
, STMPE_REG_TSC_I_DRIVE
,
213 STMPE_I_DRIVE(0xff), STMPE_I_DRIVE(ts
->i_drive
));
215 dev_err(dev
, "Could not config touch\n");
219 /* set FIFO to 1 for single point reading */
220 ret
= stmpe_reg_write(stmpe
, STMPE_REG_FIFO_TH
, 1);
222 dev_err(dev
, "Could not set FIFO\n");
226 ret
= stmpe_set_bits(stmpe
, STMPE_REG_TSC_CTRL
,
227 STMPE_OP_MODE(0xff), STMPE_OP_MODE(OP_MOD_XYZ
));
229 dev_err(dev
, "Could not set mode\n");
236 static int stmpe_ts_open(struct input_dev
*dev
)
238 struct stmpe_touch
*ts
= input_get_drvdata(dev
);
241 ret
= __stmpe_reset_fifo(ts
->stmpe
);
245 return stmpe_set_bits(ts
->stmpe
, STMPE_REG_TSC_CTRL
,
246 STMPE_TSC_CTRL_TSC_EN
, STMPE_TSC_CTRL_TSC_EN
);
249 static void stmpe_ts_close(struct input_dev
*dev
)
251 struct stmpe_touch
*ts
= input_get_drvdata(dev
);
253 cancel_delayed_work_sync(&ts
->work
);
255 stmpe_set_bits(ts
->stmpe
, STMPE_REG_TSC_CTRL
,
256 STMPE_TSC_CTRL_TSC_EN
, 0);
259 static void stmpe_ts_get_platform_info(struct platform_device
*pdev
,
260 struct stmpe_touch
*ts
)
262 struct device_node
*np
= pdev
->dev
.of_node
;
266 if (!of_property_read_u32(np
, "st,sample-time", &val
))
267 ts
->stmpe
->sample_time
= val
;
268 if (!of_property_read_u32(np
, "st,mod-12b", &val
))
269 ts
->stmpe
->mod_12b
= val
;
270 if (!of_property_read_u32(np
, "st,ref-sel", &val
))
271 ts
->stmpe
->ref_sel
= val
;
272 if (!of_property_read_u32(np
, "st,adc-freq", &val
))
273 ts
->stmpe
->adc_freq
= val
;
274 if (!of_property_read_u32(np
, "st,ave-ctrl", &val
))
276 if (!of_property_read_u32(np
, "st,touch-det-delay", &val
))
277 ts
->touch_det_delay
= val
;
278 if (!of_property_read_u32(np
, "st,settling", &val
))
280 if (!of_property_read_u32(np
, "st,fraction-z", &val
))
281 ts
->fraction_z
= val
;
282 if (!of_property_read_u32(np
, "st,i-drive", &val
))
287 static int stmpe_input_probe(struct platform_device
*pdev
)
289 struct stmpe
*stmpe
= dev_get_drvdata(pdev
->dev
.parent
);
290 struct stmpe_touch
*ts
;
291 struct input_dev
*idev
;
295 ts_irq
= platform_get_irq_byname(pdev
, "FIFO_TH");
299 ts
= devm_kzalloc(&pdev
->dev
, sizeof(*ts
), GFP_KERNEL
);
303 idev
= devm_input_allocate_device(&pdev
->dev
);
307 platform_set_drvdata(pdev
, ts
);
310 ts
->dev
= &pdev
->dev
;
312 stmpe_ts_get_platform_info(pdev
, ts
);
314 INIT_DELAYED_WORK(&ts
->work
, stmpe_work
);
316 error
= devm_request_threaded_irq(&pdev
->dev
, ts_irq
,
317 NULL
, stmpe_ts_handler
,
318 IRQF_ONESHOT
, STMPE_TS_NAME
, ts
);
320 dev_err(&pdev
->dev
, "Failed to request IRQ %d\n", ts_irq
);
324 error
= stmpe_init_hw(ts
);
328 idev
->name
= STMPE_TS_NAME
;
329 idev
->phys
= STMPE_TS_NAME
"/input0";
330 idev
->id
.bustype
= BUS_I2C
;
332 idev
->open
= stmpe_ts_open
;
333 idev
->close
= stmpe_ts_close
;
335 input_set_drvdata(idev
, ts
);
337 input_set_capability(idev
, EV_KEY
, BTN_TOUCH
);
338 input_set_abs_params(idev
, ABS_X
, 0, XY_MASK
, 0, 0);
339 input_set_abs_params(idev
, ABS_Y
, 0, XY_MASK
, 0, 0);
340 input_set_abs_params(idev
, ABS_PRESSURE
, 0x0, 0xff, 0, 0);
342 touchscreen_parse_properties(idev
, false, &ts
->prop
);
344 error
= input_register_device(idev
);
346 dev_err(&pdev
->dev
, "Could not register input device\n");
353 static void stmpe_ts_remove(struct platform_device
*pdev
)
355 struct stmpe_touch
*ts
= platform_get_drvdata(pdev
);
357 stmpe_disable(ts
->stmpe
, STMPE_BLOCK_TOUCHSCREEN
);
360 static struct platform_driver stmpe_ts_driver
= {
362 .name
= STMPE_TS_NAME
,
364 .probe
= stmpe_input_probe
,
365 .remove_new
= stmpe_ts_remove
,
367 module_platform_driver(stmpe_ts_driver
);
369 static const struct of_device_id stmpe_ts_ids
[] = {
370 { .compatible
= "st,stmpe-ts", },
373 MODULE_DEVICE_TABLE(of
, stmpe_ts_ids
);
375 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
376 MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
377 MODULE_LICENSE("GPL");