2 * STMicroelectronics STMPE811 Touchscreen Driver
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/init.h>
19 #include <linux/device.h>
21 #include <linux/platform_device.h>
22 #include <linux/input.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25 #include <linux/i2c.h>
26 #include <linux/workqueue.h>
28 #include <linux/mfd/stmpe.h>
30 /* Register layouts and functionalities are identical on all stmpexxx variants
31 * with touchscreen controller
33 #define STMPE_REG_INT_STA 0x0B
34 #define STMPE_REG_ADC_CTRL1 0x20
35 #define STMPE_REG_ADC_CTRL2 0x21
36 #define STMPE_REG_TSC_CTRL 0x40
37 #define STMPE_REG_TSC_CFG 0x41
38 #define STMPE_REG_FIFO_TH 0x4A
39 #define STMPE_REG_FIFO_STA 0x4B
40 #define STMPE_REG_FIFO_SIZE 0x4C
41 #define STMPE_REG_TSC_DATA_XYZ 0x52
42 #define STMPE_REG_TSC_FRACTION_Z 0x56
43 #define STMPE_REG_TSC_I_DRIVE 0x58
47 #define STMPE_TSC_CTRL_TSC_EN (1<<0)
49 #define STMPE_FIFO_STA_RESET (1<<0)
51 #define STMPE_IRQ_TOUCH_DET 0
53 #define SAMPLE_TIME(x) ((x & 0xf) << 4)
54 #define MOD_12B(x) ((x & 0x1) << 3)
55 #define REF_SEL(x) ((x & 0x1) << 1)
56 #define ADC_FREQ(x) (x & 0x3)
57 #define AVE_CTRL(x) ((x & 0x3) << 6)
58 #define DET_DELAY(x) ((x & 0x7) << 3)
59 #define SETTLING(x) (x & 0x7)
60 #define FRACTION_Z(x) (x & 0x7)
61 #define I_DRIVE(x) (x & 0x1)
62 #define OP_MODE(x) ((x & 0x7) << 1)
64 #define STMPE_TS_NAME "stmpe-ts"
69 struct input_dev *idev;
70 struct delayed_work work;
83 static int __stmpe_reset_fifo(struct stmpe *stmpe)
87 ret = stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
88 STMPE_FIFO_STA_RESET, STMPE_FIFO_STA_RESET);
92 return stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
93 STMPE_FIFO_STA_RESET, 0);
96 static void stmpe_work(struct work_struct *work)
101 struct stmpe_touch *ts =
102 container_of(work, struct stmpe_touch, work.work);
104 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
107 * touch_det sometimes get desasserted or just get stuck. This appears
108 * to be a silicon bug, We still have to clearify this with the
109 * manufacture. As a workaround We release the key anyway if the
110 * touch_det keeps coming in after 4ms, while the FIFO contains no value
111 * during the whole time.
113 while ((int_sta & (1 << STMPE_IRQ_TOUCH_DET)) && (timeout > 0)) {
115 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
119 /* reset the FIFO before we report release event */
120 __stmpe_reset_fifo(ts->stmpe);
122 input_report_abs(ts->idev, ABS_PRESSURE, 0);
123 input_report_key(ts->idev, BTN_TOUCH, 0);
124 input_sync(ts->idev);
127 static irqreturn_t stmpe_ts_handler(int irq, void *data)
131 struct stmpe_touch *ts = data;
134 * Cancel scheduled polling for release if we have new value
135 * available. Wait if the polling is already running.
137 cancel_delayed_work_sync(&ts->work);
140 * The FIFO sometimes just crashes and stops generating interrupts. This
141 * appears to be a silicon bug. We still have to clearify this with
142 * the manufacture. As a workaround we disable the TSC while we are
143 * collecting data and flush the FIFO after reading
145 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
146 STMPE_TSC_CTRL_TSC_EN, 0);
148 stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
150 x = (data_set[0] << 4) | (data_set[1] >> 4);
151 y = ((data_set[1] & 0xf) << 8) | data_set[2];
154 input_report_abs(ts->idev, ABS_X, x);
155 input_report_abs(ts->idev, ABS_Y, y);
156 input_report_abs(ts->idev, ABS_PRESSURE, z);
157 input_report_key(ts->idev, BTN_TOUCH, 1);
158 input_sync(ts->idev);
160 /* flush the FIFO after we have read out our values. */
161 __stmpe_reset_fifo(ts->stmpe);
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);
167 /* start polling for touch_det to detect release */
168 schedule_delayed_work(&ts->work, HZ / 50);
173 static int stmpe_init_hw(struct stmpe_touch *ts)
176 u8 adc_ctrl1, adc_ctrl1_mask, tsc_cfg, tsc_cfg_mask;
177 struct stmpe *stmpe = ts->stmpe;
178 struct device *dev = ts->dev;
180 ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
182 dev_err(dev, "Could not enable clock for ADC and TS\n");
186 adc_ctrl1 = SAMPLE_TIME(ts->sample_time) | MOD_12B(ts->mod_12b) |
187 REF_SEL(ts->ref_sel);
188 adc_ctrl1_mask = SAMPLE_TIME(0xff) | MOD_12B(0xff) | REF_SEL(0xff);
190 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL1,
191 adc_ctrl1_mask, adc_ctrl1);
193 dev_err(dev, "Could not setup ADC\n");
197 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL2,
198 ADC_FREQ(0xff), ADC_FREQ(ts->adc_freq));
200 dev_err(dev, "Could not setup ADC\n");
204 tsc_cfg = AVE_CTRL(ts->ave_ctrl) | DET_DELAY(ts->touch_det_delay) |
205 SETTLING(ts->settling);
206 tsc_cfg_mask = AVE_CTRL(0xff) | DET_DELAY(0xff) | SETTLING(0xff);
208 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
210 dev_err(dev, "Could not config touch\n");
214 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
215 FRACTION_Z(0xff), FRACTION_Z(ts->fraction_z));
217 dev_err(dev, "Could not config touch\n");
221 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
222 I_DRIVE(0xff), I_DRIVE(ts->i_drive));
224 dev_err(dev, "Could not config touch\n");
228 /* set FIFO to 1 for single point reading */
229 ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
231 dev_err(dev, "Could not set FIFO\n");
235 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
236 OP_MODE(0xff), OP_MODE(OP_MOD_XYZ));
238 dev_err(dev, "Could not set mode\n");
245 static int stmpe_ts_open(struct input_dev *dev)
247 struct stmpe_touch *ts = input_get_drvdata(dev);
250 ret = __stmpe_reset_fifo(ts->stmpe);
254 return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
255 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
258 static void stmpe_ts_close(struct input_dev *dev)
260 struct stmpe_touch *ts = input_get_drvdata(dev);
262 cancel_delayed_work_sync(&ts->work);
264 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
265 STMPE_TSC_CTRL_TSC_EN, 0);
268 static void stmpe_ts_get_platform_info(struct platform_device *pdev,
269 struct stmpe_touch *ts)
271 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
272 struct device_node *np = pdev->dev.of_node;
273 struct stmpe_ts_platform_data *ts_pdata = NULL;
277 if (stmpe->pdata && stmpe->pdata->ts) {
278 ts_pdata = stmpe->pdata->ts;
280 ts->sample_time = ts_pdata->sample_time;
281 ts->mod_12b = ts_pdata->mod_12b;
282 ts->ref_sel = ts_pdata->ref_sel;
283 ts->adc_freq = ts_pdata->adc_freq;
284 ts->ave_ctrl = ts_pdata->ave_ctrl;
285 ts->touch_det_delay = ts_pdata->touch_det_delay;
286 ts->settling = ts_pdata->settling;
287 ts->fraction_z = ts_pdata->fraction_z;
288 ts->i_drive = ts_pdata->i_drive;
292 if (!of_property_read_u32(np, "st,sample-time", &val))
293 ts->sample_time = val;
294 if (!of_property_read_u32(np, "st,mod-12b", &val))
296 if (!of_property_read_u32(np, "st,ref-sel", &val))
298 if (!of_property_read_u32(np, "st,adc-freq", &val))
300 if (!of_property_read_u32(np, "st,ave-ctrl", &val))
302 if (!of_property_read_u32(np, "st,touch-det-delay", &val))
303 ts->touch_det_delay = val;
304 if (!of_property_read_u32(np, "st,settling", &val))
306 if (!of_property_read_u32(np, "st,fraction-z", &val))
307 ts->fraction_z = val;
308 if (!of_property_read_u32(np, "st,i-drive", &val))
313 static int stmpe_input_probe(struct platform_device *pdev)
315 struct stmpe_touch *ts;
316 struct input_dev *idev;
320 ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
324 ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
328 idev = devm_input_allocate_device(&pdev->dev);
332 platform_set_drvdata(pdev, ts);
334 ts->dev = &pdev->dev;
336 stmpe_ts_get_platform_info(pdev, ts);
338 INIT_DELAYED_WORK(&ts->work, stmpe_work);
340 error = devm_request_threaded_irq(&pdev->dev, ts_irq,
341 NULL, stmpe_ts_handler,
342 IRQF_ONESHOT, STMPE_TS_NAME, ts);
344 dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
348 error = stmpe_init_hw(ts);
352 idev->name = STMPE_TS_NAME;
353 idev->phys = STMPE_TS_NAME"/input0";
354 idev->id.bustype = BUS_I2C;
355 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
356 idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
358 idev->open = stmpe_ts_open;
359 idev->close = stmpe_ts_close;
361 input_set_drvdata(idev, ts);
363 input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
364 input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
365 input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
367 error = input_register_device(idev);
369 dev_err(&pdev->dev, "Could not register input device\n");
376 static int stmpe_ts_remove(struct platform_device *pdev)
378 struct stmpe_touch *ts = platform_get_drvdata(pdev);
380 stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
385 static struct platform_driver stmpe_ts_driver = {
387 .name = STMPE_TS_NAME,
388 .owner = THIS_MODULE,
390 .probe = stmpe_input_probe,
391 .remove = stmpe_ts_remove,
393 module_platform_driver(stmpe_ts_driver);
396 MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
397 MODULE_LICENSE("GPL");
398 MODULE_ALIAS("platform:" STMPE_TS_NAME);