2 * STMicroelectronics STMPE811 Touchscreen Driver
4 * (C) 2010 Luotao Fu <l.fu@pengutronix.de>
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.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/interrupt.h>
18 #include <linux/device.h>
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>
27 #include <linux/mfd/stmpe.h>
29 /* Register layouts and functionalities are identical on all stmpexxx variants
30 * with touchscreen controller
32 #define STMPE_REG_INT_STA 0x0B
33 #define STMPE_REG_ADC_CTRL1 0x20
34 #define STMPE_REG_ADC_CTRL2 0x21
35 #define STMPE_REG_TSC_CTRL 0x40
36 #define STMPE_REG_TSC_CFG 0x41
37 #define STMPE_REG_FIFO_TH 0x4A
38 #define STMPE_REG_FIFO_STA 0x4B
39 #define STMPE_REG_FIFO_SIZE 0x4C
40 #define STMPE_REG_TSC_DATA_XYZ 0x52
41 #define STMPE_REG_TSC_FRACTION_Z 0x56
42 #define STMPE_REG_TSC_I_DRIVE 0x58
46 #define STMPE_TSC_CTRL_TSC_EN (1<<0)
48 #define STMPE_FIFO_STA_RESET (1<<0)
50 #define STMPE_IRQ_TOUCH_DET 0
52 #define SAMPLE_TIME(x) ((x & 0xf) << 4)
53 #define MOD_12B(x) ((x & 0x1) << 3)
54 #define REF_SEL(x) ((x & 0x1) << 1)
55 #define ADC_FREQ(x) (x & 0x3)
56 #define AVE_CTRL(x) ((x & 0x3) << 6)
57 #define DET_DELAY(x) ((x & 0x7) << 3)
58 #define SETTLING(x) (x & 0x7)
59 #define FRACTION_Z(x) (x & 0x7)
60 #define I_DRIVE(x) (x & 0x1)
61 #define OP_MODE(x) ((x & 0x7) << 1)
63 #define STMPE_TS_NAME "stmpe-ts"
68 struct input_dev
*idev
;
69 struct delayed_work work
;
82 static int __stmpe_reset_fifo(struct stmpe
*stmpe
)
86 ret
= stmpe_set_bits(stmpe
, STMPE_REG_FIFO_STA
,
87 STMPE_FIFO_STA_RESET
, STMPE_FIFO_STA_RESET
);
91 return stmpe_set_bits(stmpe
, STMPE_REG_FIFO_STA
,
92 STMPE_FIFO_STA_RESET
, 0);
95 static void stmpe_work(struct work_struct
*work
)
100 struct stmpe_touch
*ts
=
101 container_of(work
, struct stmpe_touch
, work
.work
);
103 int_sta
= stmpe_reg_read(ts
->stmpe
, STMPE_REG_INT_STA
);
106 * touch_det sometimes get desasserted or just get stuck. This appears
107 * to be a silicon bug, We still have to clearify this with the
108 * manufacture. As a workaround We release the key anyway if the
109 * touch_det keeps coming in after 4ms, while the FIFO contains no value
110 * during the whole time.
112 while ((int_sta
& (1 << STMPE_IRQ_TOUCH_DET
)) && (timeout
> 0)) {
114 int_sta
= stmpe_reg_read(ts
->stmpe
, STMPE_REG_INT_STA
);
118 /* reset the FIFO before we report release event */
119 __stmpe_reset_fifo(ts
->stmpe
);
121 input_report_abs(ts
->idev
, ABS_PRESSURE
, 0);
122 input_report_key(ts
->idev
, BTN_TOUCH
, 0);
123 input_sync(ts
->idev
);
126 static irqreturn_t
stmpe_ts_handler(int irq
, void *data
)
130 struct stmpe_touch
*ts
= data
;
133 * Cancel scheduled polling for release if we have new value
134 * available. Wait if the polling is already running.
136 cancel_delayed_work_sync(&ts
->work
);
139 * The FIFO sometimes just crashes and stops generating interrupts. This
140 * appears to be a silicon bug. We still have to clearify this with
141 * the manufacture. As a workaround we disable the TSC while we are
142 * collecting data and flush the FIFO after reading
144 stmpe_set_bits(ts
->stmpe
, STMPE_REG_TSC_CTRL
,
145 STMPE_TSC_CTRL_TSC_EN
, 0);
147 stmpe_block_read(ts
->stmpe
, STMPE_REG_TSC_DATA_XYZ
, 4, data_set
);
149 x
= (data_set
[0] << 4) | (data_set
[1] >> 4);
150 y
= ((data_set
[1] & 0xf) << 8) | data_set
[2];
153 input_report_abs(ts
->idev
, ABS_X
, x
);
154 input_report_abs(ts
->idev
, ABS_Y
, y
);
155 input_report_abs(ts
->idev
, ABS_PRESSURE
, z
);
156 input_report_key(ts
->idev
, BTN_TOUCH
, 1);
157 input_sync(ts
->idev
);
159 /* flush the FIFO after we have read out our values. */
160 __stmpe_reset_fifo(ts
->stmpe
);
162 /* reenable the tsc */
163 stmpe_set_bits(ts
->stmpe
, STMPE_REG_TSC_CTRL
,
164 STMPE_TSC_CTRL_TSC_EN
, STMPE_TSC_CTRL_TSC_EN
);
166 /* start polling for touch_det to detect release */
167 schedule_delayed_work(&ts
->work
, msecs_to_jiffies(50));
172 static int stmpe_init_hw(struct stmpe_touch
*ts
)
175 u8 adc_ctrl1
, adc_ctrl1_mask
, tsc_cfg
, tsc_cfg_mask
;
176 struct stmpe
*stmpe
= ts
->stmpe
;
177 struct device
*dev
= ts
->dev
;
179 ret
= stmpe_enable(stmpe
, STMPE_BLOCK_TOUCHSCREEN
| STMPE_BLOCK_ADC
);
181 dev_err(dev
, "Could not enable clock for ADC and TS\n");
185 adc_ctrl1
= SAMPLE_TIME(ts
->sample_time
) | MOD_12B(ts
->mod_12b
) |
186 REF_SEL(ts
->ref_sel
);
187 adc_ctrl1_mask
= SAMPLE_TIME(0xff) | MOD_12B(0xff) | REF_SEL(0xff);
189 ret
= stmpe_set_bits(stmpe
, STMPE_REG_ADC_CTRL1
,
190 adc_ctrl1_mask
, adc_ctrl1
);
192 dev_err(dev
, "Could not setup ADC\n");
196 ret
= stmpe_set_bits(stmpe
, STMPE_REG_ADC_CTRL2
,
197 ADC_FREQ(0xff), ADC_FREQ(ts
->adc_freq
));
199 dev_err(dev
, "Could not setup ADC\n");
203 tsc_cfg
= AVE_CTRL(ts
->ave_ctrl
) | DET_DELAY(ts
->touch_det_delay
) |
204 SETTLING(ts
->settling
);
205 tsc_cfg_mask
= AVE_CTRL(0xff) | DET_DELAY(0xff) | SETTLING(0xff);
207 ret
= stmpe_set_bits(stmpe
, STMPE_REG_TSC_CFG
, tsc_cfg_mask
, tsc_cfg
);
209 dev_err(dev
, "Could not config touch\n");
213 ret
= stmpe_set_bits(stmpe
, STMPE_REG_TSC_FRACTION_Z
,
214 FRACTION_Z(0xff), FRACTION_Z(ts
->fraction_z
));
216 dev_err(dev
, "Could not config touch\n");
220 ret
= stmpe_set_bits(stmpe
, STMPE_REG_TSC_I_DRIVE
,
221 I_DRIVE(0xff), I_DRIVE(ts
->i_drive
));
223 dev_err(dev
, "Could not config touch\n");
227 /* set FIFO to 1 for single point reading */
228 ret
= stmpe_reg_write(stmpe
, STMPE_REG_FIFO_TH
, 1);
230 dev_err(dev
, "Could not set FIFO\n");
234 ret
= stmpe_set_bits(stmpe
, STMPE_REG_TSC_CTRL
,
235 OP_MODE(0xff), OP_MODE(OP_MOD_XYZ
));
237 dev_err(dev
, "Could not set mode\n");
244 static int stmpe_ts_open(struct input_dev
*dev
)
246 struct stmpe_touch
*ts
= input_get_drvdata(dev
);
249 ret
= __stmpe_reset_fifo(ts
->stmpe
);
253 return stmpe_set_bits(ts
->stmpe
, STMPE_REG_TSC_CTRL
,
254 STMPE_TSC_CTRL_TSC_EN
, STMPE_TSC_CTRL_TSC_EN
);
257 static void stmpe_ts_close(struct input_dev
*dev
)
259 struct stmpe_touch
*ts
= input_get_drvdata(dev
);
261 cancel_delayed_work_sync(&ts
->work
);
263 stmpe_set_bits(ts
->stmpe
, STMPE_REG_TSC_CTRL
,
264 STMPE_TSC_CTRL_TSC_EN
, 0);
267 static void stmpe_ts_get_platform_info(struct platform_device
*pdev
,
268 struct stmpe_touch
*ts
)
270 struct device_node
*np
= pdev
->dev
.of_node
;
274 if (!of_property_read_u32(np
, "st,sample-time", &val
))
275 ts
->sample_time
= val
;
276 if (!of_property_read_u32(np
, "st,mod-12b", &val
))
278 if (!of_property_read_u32(np
, "st,ref-sel", &val
))
280 if (!of_property_read_u32(np
, "st,adc-freq", &val
))
282 if (!of_property_read_u32(np
, "st,ave-ctrl", &val
))
284 if (!of_property_read_u32(np
, "st,touch-det-delay", &val
))
285 ts
->touch_det_delay
= val
;
286 if (!of_property_read_u32(np
, "st,settling", &val
))
288 if (!of_property_read_u32(np
, "st,fraction-z", &val
))
289 ts
->fraction_z
= val
;
290 if (!of_property_read_u32(np
, "st,i-drive", &val
))
295 static int stmpe_input_probe(struct platform_device
*pdev
)
297 struct stmpe
*stmpe
= dev_get_drvdata(pdev
->dev
.parent
);
298 struct stmpe_touch
*ts
;
299 struct input_dev
*idev
;
303 ts_irq
= platform_get_irq_byname(pdev
, "FIFO_TH");
307 ts
= devm_kzalloc(&pdev
->dev
, sizeof(*ts
), GFP_KERNEL
);
311 idev
= devm_input_allocate_device(&pdev
->dev
);
315 platform_set_drvdata(pdev
, ts
);
318 ts
->dev
= &pdev
->dev
;
320 stmpe_ts_get_platform_info(pdev
, ts
);
322 INIT_DELAYED_WORK(&ts
->work
, stmpe_work
);
324 error
= devm_request_threaded_irq(&pdev
->dev
, ts_irq
,
325 NULL
, stmpe_ts_handler
,
326 IRQF_ONESHOT
, STMPE_TS_NAME
, ts
);
328 dev_err(&pdev
->dev
, "Failed to request IRQ %d\n", ts_irq
);
332 error
= stmpe_init_hw(ts
);
336 idev
->name
= STMPE_TS_NAME
;
337 idev
->phys
= STMPE_TS_NAME
"/input0";
338 idev
->id
.bustype
= BUS_I2C
;
340 idev
->open
= stmpe_ts_open
;
341 idev
->close
= stmpe_ts_close
;
343 input_set_drvdata(idev
, ts
);
345 input_set_capability(idev
, EV_KEY
, BTN_TOUCH
);
346 input_set_abs_params(idev
, ABS_X
, 0, XY_MASK
, 0, 0);
347 input_set_abs_params(idev
, ABS_Y
, 0, XY_MASK
, 0, 0);
348 input_set_abs_params(idev
, ABS_PRESSURE
, 0x0, 0xff, 0, 0);
350 error
= input_register_device(idev
);
352 dev_err(&pdev
->dev
, "Could not register input device\n");
359 static int stmpe_ts_remove(struct platform_device
*pdev
)
361 struct stmpe_touch
*ts
= platform_get_drvdata(pdev
);
363 stmpe_disable(ts
->stmpe
, STMPE_BLOCK_TOUCHSCREEN
);
368 static struct platform_driver stmpe_ts_driver
= {
370 .name
= STMPE_TS_NAME
,
372 .probe
= stmpe_input_probe
,
373 .remove
= stmpe_ts_remove
,
375 module_platform_driver(stmpe_ts_driver
);
377 static const struct of_device_id stmpe_ts_ids
[] = {
378 { .compatible
= "st,stmpe-ts", },
381 MODULE_DEVICE_TABLE(of
, stmpe_ts_ids
);
383 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
384 MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
385 MODULE_LICENSE("GPL");