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