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