blob: 7d9aadcef154b054f1c607daae245605fd7f8640 [file] [log] [blame]
Viresh Kumar2bd942f2012-11-09 23:56:59 -08001/*
2 * STMicroelectronics STMPE811 Touchscreen Driver
Luotao Fuf94add32010-07-02 14:10:29 +02003 *
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/init.h>
19#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>
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_sync(ts->idev);
123}
124
125static irqreturn_t stmpe_ts_handler(int irq, void *data)
126{
127 u8 data_set[4];
128 int x, y, z;
129 struct stmpe_touch *ts = data;
130
131 /*
132 * Cancel scheduled polling for release if we have new value
133 * available. Wait if the polling is already running.
134 */
135 cancel_delayed_work_sync(&ts->work);
136
137 /*
138 * The FIFO sometimes just crashes and stops generating interrupts. This
139 * appears to be a silicon bug. We still have to clearify this with
140 * the manufacture. As a workaround we disable the TSC while we are
141 * collecting data and flush the FIFO after reading
142 */
143 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
144 STMPE_TSC_CTRL_TSC_EN, 0);
145
146 stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
147
148 x = (data_set[0] << 4) | (data_set[1] >> 4);
149 y = ((data_set[1] & 0xf) << 8) | data_set[2];
150 z = data_set[3];
151
152 input_report_abs(ts->idev, ABS_X, x);
153 input_report_abs(ts->idev, ABS_Y, y);
154 input_report_abs(ts->idev, ABS_PRESSURE, z);
155 input_sync(ts->idev);
156
157 /* flush the FIFO after we have read out our values. */
158 __stmpe_reset_fifo(ts->stmpe);
159
160 /* reenable the tsc */
161 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
162 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
163
164 /* start polling for touch_det to detect release */
165 schedule_delayed_work(&ts->work, HZ / 50);
166
167 return IRQ_HANDLED;
168}
169
170static int __devinit stmpe_init_hw(struct stmpe_touch *ts)
171{
172 int ret;
173 u8 adc_ctrl1, adc_ctrl1_mask, tsc_cfg, tsc_cfg_mask;
174 struct stmpe *stmpe = ts->stmpe;
175 struct device *dev = ts->dev;
176
177 ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
178 if (ret) {
179 dev_err(dev, "Could not enable clock for ADC and TS\n");
180 return ret;
181 }
182
183 adc_ctrl1 = SAMPLE_TIME(ts->sample_time) | MOD_12B(ts->mod_12b) |
184 REF_SEL(ts->ref_sel);
185 adc_ctrl1_mask = SAMPLE_TIME(0xff) | MOD_12B(0xff) | REF_SEL(0xff);
186
187 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL1,
188 adc_ctrl1_mask, adc_ctrl1);
189 if (ret) {
190 dev_err(dev, "Could not setup ADC\n");
191 return ret;
192 }
193
194 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL2,
195 ADC_FREQ(0xff), ADC_FREQ(ts->adc_freq));
196 if (ret) {
197 dev_err(dev, "Could not setup ADC\n");
198 return ret;
199 }
200
201 tsc_cfg = AVE_CTRL(ts->ave_ctrl) | DET_DELAY(ts->touch_det_delay) |
202 SETTLING(ts->settling);
203 tsc_cfg_mask = AVE_CTRL(0xff) | DET_DELAY(0xff) | SETTLING(0xff);
204
205 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
206 if (ret) {
207 dev_err(dev, "Could not config touch\n");
208 return ret;
209 }
210
211 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
212 FRACTION_Z(0xff), FRACTION_Z(ts->fraction_z));
213 if (ret) {
214 dev_err(dev, "Could not config touch\n");
215 return ret;
216 }
217
218 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
219 I_DRIVE(0xff), I_DRIVE(ts->i_drive));
220 if (ret) {
221 dev_err(dev, "Could not config touch\n");
222 return ret;
223 }
224
225 /* set FIFO to 1 for single point reading */
226 ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
227 if (ret) {
228 dev_err(dev, "Could not set FIFO\n");
229 return ret;
230 }
231
232 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
233 OP_MODE(0xff), OP_MODE(OP_MOD_XYZ));
234 if (ret) {
235 dev_err(dev, "Could not set mode\n");
236 return ret;
237 }
238
239 return 0;
240}
241
242static int stmpe_ts_open(struct input_dev *dev)
243{
244 struct stmpe_touch *ts = input_get_drvdata(dev);
245 int ret = 0;
246
247 ret = __stmpe_reset_fifo(ts->stmpe);
248 if (ret)
249 return ret;
250
251 return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
252 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
253}
254
255static void stmpe_ts_close(struct input_dev *dev)
256{
257 struct stmpe_touch *ts = input_get_drvdata(dev);
258
259 cancel_delayed_work_sync(&ts->work);
260
261 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
262 STMPE_TSC_CTRL_TSC_EN, 0);
263}
264
265static int __devinit stmpe_input_probe(struct platform_device *pdev)
266{
267 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800268 const struct stmpe_platform_data *pdata = stmpe->pdata;
269 const struct stmpe_ts_platform_data *ts_pdata = NULL;
Luotao Fuf94add32010-07-02 14:10:29 +0200270 struct stmpe_touch *ts;
271 struct input_dev *idev;
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800272 int error;
Kulikov Vasiliy3faeb352010-07-15 22:43:54 +0400273 int ts_irq;
Luotao Fuf94add32010-07-02 14:10:29 +0200274
275 ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
276 if (ts_irq < 0)
277 return ts_irq;
278
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800279 ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
280 if (!ts)
281 return -ENOMEM;
Luotao Fuf94add32010-07-02 14:10:29 +0200282
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800283 idev = devm_input_allocate_device(&pdev->dev);
284 if (!idev)
285 return -ENOMEM;
Luotao Fuf94add32010-07-02 14:10:29 +0200286
287 platform_set_drvdata(pdev, ts);
288 ts->stmpe = stmpe;
289 ts->idev = idev;
290 ts->dev = &pdev->dev;
291
292 if (pdata)
293 ts_pdata = pdata->ts;
294
295 if (ts_pdata) {
296 ts->sample_time = ts_pdata->sample_time;
297 ts->mod_12b = ts_pdata->mod_12b;
298 ts->ref_sel = ts_pdata->ref_sel;
299 ts->adc_freq = ts_pdata->adc_freq;
300 ts->ave_ctrl = ts_pdata->ave_ctrl;
301 ts->touch_det_delay = ts_pdata->touch_det_delay;
302 ts->settling = ts_pdata->settling;
303 ts->fraction_z = ts_pdata->fraction_z;
304 ts->i_drive = ts_pdata->i_drive;
305 }
306
307 INIT_DELAYED_WORK(&ts->work, stmpe_work);
308
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800309 error = devm_request_threaded_irq(&pdev->dev, ts_irq,
310 NULL, stmpe_ts_handler,
311 IRQF_ONESHOT, STMPE_TS_NAME, ts);
312 if (error) {
Luotao Fuf94add32010-07-02 14:10:29 +0200313 dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800314 return error;
Luotao Fuf94add32010-07-02 14:10:29 +0200315 }
316
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800317 error = stmpe_init_hw(ts);
318 if (error)
319 return error;
Luotao Fuf94add32010-07-02 14:10:29 +0200320
321 idev->name = STMPE_TS_NAME;
322 idev->id.bustype = BUS_I2C;
323 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
324 idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
325
326 idev->open = stmpe_ts_open;
327 idev->close = stmpe_ts_close;
328
329 input_set_drvdata(idev, ts);
330
331 input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
332 input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
333 input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
334
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800335 error = input_register_device(idev);
336 if (error) {
Luotao Fuf94add32010-07-02 14:10:29 +0200337 dev_err(&pdev->dev, "Could not register input device\n");
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800338 return error;
Luotao Fuf94add32010-07-02 14:10:29 +0200339 }
340
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800341 return 0;
Luotao Fuf94add32010-07-02 14:10:29 +0200342}
343
344static int __devexit stmpe_ts_remove(struct platform_device *pdev)
345{
346 struct stmpe_touch *ts = platform_get_drvdata(pdev);
Luotao Fuf94add32010-07-02 14:10:29 +0200347
348 stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
349
Luotao Fuf94add32010-07-02 14:10:29 +0200350 return 0;
351}
352
353static struct platform_driver stmpe_ts_driver = {
354 .driver = {
355 .name = STMPE_TS_NAME,
356 .owner = THIS_MODULE,
357 },
358 .probe = stmpe_input_probe,
359 .remove = __devexit_p(stmpe_ts_remove),
360};
JJ Dingcdcc96e2011-11-29 11:14:13 -0800361module_platform_driver(stmpe_ts_driver);
Luotao Fuf94add32010-07-02 14:10:29 +0200362
363MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
364MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
365MODULE_LICENSE("GPL");
366MODULE_ALIAS("platform:" STMPE_TS_NAME);