blob: 59e81b00f244c1852eca936493538e52d4eb3085 [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>
Vipul Kumar Samar037db522012-11-22 23:42:12 -080020#include <linux/of.h>
Luotao Fuf94add32010-07-02 14:10:29 +020021#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>
27
28#include <linux/mfd/stmpe.h>
29
30/* Register layouts and functionalities are identical on all stmpexxx variants
31 * with touchscreen controller
32 */
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
44
45#define OP_MOD_XYZ 0
46
47#define STMPE_TSC_CTRL_TSC_EN (1<<0)
48
49#define STMPE_FIFO_STA_RESET (1<<0)
50
51#define STMPE_IRQ_TOUCH_DET 0
52
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)
63
64#define STMPE_TS_NAME "stmpe-ts"
65#define XY_MASK 0xfff
66
67struct stmpe_touch {
68 struct stmpe *stmpe;
69 struct input_dev *idev;
70 struct delayed_work work;
71 struct device *dev;
72 u8 sample_time;
73 u8 mod_12b;
74 u8 ref_sel;
75 u8 adc_freq;
76 u8 ave_ctrl;
77 u8 touch_det_delay;
78 u8 settling;
79 u8 fraction_z;
80 u8 i_drive;
81};
82
83static int __stmpe_reset_fifo(struct stmpe *stmpe)
84{
85 int ret;
86
87 ret = stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
88 STMPE_FIFO_STA_RESET, STMPE_FIFO_STA_RESET);
89 if (ret)
90 return ret;
91
92 return stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
93 STMPE_FIFO_STA_RESET, 0);
94}
95
96static void stmpe_work(struct work_struct *work)
97{
98 int int_sta;
99 u32 timeout = 40;
100
101 struct stmpe_touch *ts =
102 container_of(work, struct stmpe_touch, work.work);
103
104 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
105
106 /*
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.
112 */
113 while ((int_sta & (1 << STMPE_IRQ_TOUCH_DET)) && (timeout > 0)) {
114 timeout--;
115 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
116 udelay(100);
117 }
118
119 /* reset the FIFO before we report release event */
120 __stmpe_reset_fifo(ts->stmpe);
121
122 input_report_abs(ts->idev, ABS_PRESSURE, 0);
Vipul Kumar Samar7488b1b2012-12-16 23:10:56 -0800123 input_report_key(ts->idev, BTN_TOUCH, 0);
Luotao Fuf94add32010-07-02 14:10:29 +0200124 input_sync(ts->idev);
125}
126
127static irqreturn_t stmpe_ts_handler(int irq, void *data)
128{
129 u8 data_set[4];
130 int x, y, z;
131 struct stmpe_touch *ts = data;
132
133 /*
134 * Cancel scheduled polling for release if we have new value
135 * available. Wait if the polling is already running.
136 */
137 cancel_delayed_work_sync(&ts->work);
138
139 /*
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
144 */
145 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
146 STMPE_TSC_CTRL_TSC_EN, 0);
147
148 stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
149
150 x = (data_set[0] << 4) | (data_set[1] >> 4);
151 y = ((data_set[1] & 0xf) << 8) | data_set[2];
152 z = data_set[3];
153
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);
Vipul Kumar Samar7488b1b2012-12-16 23:10:56 -0800157 input_report_key(ts->idev, BTN_TOUCH, 1);
Luotao Fuf94add32010-07-02 14:10:29 +0200158 input_sync(ts->idev);
159
160 /* flush the FIFO after we have read out our values. */
161 __stmpe_reset_fifo(ts->stmpe);
162
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);
166
167 /* start polling for touch_det to detect release */
168 schedule_delayed_work(&ts->work, HZ / 50);
169
170 return IRQ_HANDLED;
171}
172
Bill Pemberton5298cc42012-11-23 21:38:25 -0800173static int stmpe_init_hw(struct stmpe_touch *ts)
Luotao Fuf94add32010-07-02 14:10:29 +0200174{
175 int ret;
176 u8 adc_ctrl1, adc_ctrl1_mask, tsc_cfg, tsc_cfg_mask;
177 struct stmpe *stmpe = ts->stmpe;
178 struct device *dev = ts->dev;
179
180 ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
181 if (ret) {
182 dev_err(dev, "Could not enable clock for ADC and TS\n");
183 return ret;
184 }
185
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);
189
190 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL1,
191 adc_ctrl1_mask, adc_ctrl1);
192 if (ret) {
193 dev_err(dev, "Could not setup ADC\n");
194 return ret;
195 }
196
197 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL2,
198 ADC_FREQ(0xff), ADC_FREQ(ts->adc_freq));
199 if (ret) {
200 dev_err(dev, "Could not setup ADC\n");
201 return ret;
202 }
203
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);
207
208 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
209 if (ret) {
210 dev_err(dev, "Could not config touch\n");
211 return ret;
212 }
213
214 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
215 FRACTION_Z(0xff), FRACTION_Z(ts->fraction_z));
216 if (ret) {
217 dev_err(dev, "Could not config touch\n");
218 return ret;
219 }
220
221 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
222 I_DRIVE(0xff), I_DRIVE(ts->i_drive));
223 if (ret) {
224 dev_err(dev, "Could not config touch\n");
225 return ret;
226 }
227
228 /* set FIFO to 1 for single point reading */
229 ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
230 if (ret) {
231 dev_err(dev, "Could not set FIFO\n");
232 return ret;
233 }
234
235 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
236 OP_MODE(0xff), OP_MODE(OP_MOD_XYZ));
237 if (ret) {
238 dev_err(dev, "Could not set mode\n");
239 return ret;
240 }
241
242 return 0;
243}
244
245static int stmpe_ts_open(struct input_dev *dev)
246{
247 struct stmpe_touch *ts = input_get_drvdata(dev);
248 int ret = 0;
249
250 ret = __stmpe_reset_fifo(ts->stmpe);
251 if (ret)
252 return ret;
253
254 return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
255 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
256}
257
258static void stmpe_ts_close(struct input_dev *dev)
259{
260 struct stmpe_touch *ts = input_get_drvdata(dev);
261
262 cancel_delayed_work_sync(&ts->work);
263
264 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
265 STMPE_TSC_CTRL_TSC_EN, 0);
266}
267
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800268static void stmpe_ts_get_platform_info(struct platform_device *pdev,
269 struct stmpe_touch *ts)
Luotao Fuf94add32010-07-02 14:10:29 +0200270{
271 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800272 struct device_node *np = pdev->dev.of_node;
273 struct stmpe_ts_platform_data *ts_pdata = NULL;
274
275 ts->stmpe = stmpe;
276
277 if (stmpe->pdata && stmpe->pdata->ts) {
278 ts_pdata = stmpe->pdata->ts;
279
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;
289 } else if (np) {
290 u32 val;
291
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))
295 ts->mod_12b = val;
296 if (!of_property_read_u32(np, "st,ref-sel", &val))
297 ts->ref_sel = val;
298 if (!of_property_read_u32(np, "st,adc-freq", &val))
299 ts->adc_freq = val;
300 if (!of_property_read_u32(np, "st,ave-ctrl", &val))
301 ts->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))
305 ts->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))
309 ts->i_drive = val;
310 }
311}
312
Bill Pemberton5298cc42012-11-23 21:38:25 -0800313static int stmpe_input_probe(struct platform_device *pdev)
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800314{
Luotao Fuf94add32010-07-02 14:10:29 +0200315 struct stmpe_touch *ts;
316 struct input_dev *idev;
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800317 int error;
Kulikov Vasiliy3faeb352010-07-15 22:43:54 +0400318 int ts_irq;
Luotao Fuf94add32010-07-02 14:10:29 +0200319
320 ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
321 if (ts_irq < 0)
322 return ts_irq;
323
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800324 ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
325 if (!ts)
326 return -ENOMEM;
Luotao Fuf94add32010-07-02 14:10:29 +0200327
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800328 idev = devm_input_allocate_device(&pdev->dev);
329 if (!idev)
330 return -ENOMEM;
Luotao Fuf94add32010-07-02 14:10:29 +0200331
332 platform_set_drvdata(pdev, ts);
Luotao Fuf94add32010-07-02 14:10:29 +0200333 ts->idev = idev;
334 ts->dev = &pdev->dev;
335
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800336 stmpe_ts_get_platform_info(pdev, ts);
Luotao Fuf94add32010-07-02 14:10:29 +0200337
338 INIT_DELAYED_WORK(&ts->work, stmpe_work);
339
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800340 error = devm_request_threaded_irq(&pdev->dev, ts_irq,
341 NULL, stmpe_ts_handler,
342 IRQF_ONESHOT, STMPE_TS_NAME, ts);
343 if (error) {
Luotao Fuf94add32010-07-02 14:10:29 +0200344 dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800345 return error;
Luotao Fuf94add32010-07-02 14:10:29 +0200346 }
347
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800348 error = stmpe_init_hw(ts);
349 if (error)
350 return error;
Luotao Fuf94add32010-07-02 14:10:29 +0200351
352 idev->name = STMPE_TS_NAME;
Vipul Kumar Samarb8d52e22012-11-10 00:08:20 -0800353 idev->phys = STMPE_TS_NAME"/input0";
Luotao Fuf94add32010-07-02 14:10:29 +0200354 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);
357
358 idev->open = stmpe_ts_open;
359 idev->close = stmpe_ts_close;
360
361 input_set_drvdata(idev, ts);
362
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);
366
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800367 error = input_register_device(idev);
368 if (error) {
Luotao Fuf94add32010-07-02 14:10:29 +0200369 dev_err(&pdev->dev, "Could not register input device\n");
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800370 return error;
Luotao Fuf94add32010-07-02 14:10:29 +0200371 }
372
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800373 return 0;
Luotao Fuf94add32010-07-02 14:10:29 +0200374}
375
Bill Pembertone2619cf2012-11-23 21:50:47 -0800376static int stmpe_ts_remove(struct platform_device *pdev)
Luotao Fuf94add32010-07-02 14:10:29 +0200377{
378 struct stmpe_touch *ts = platform_get_drvdata(pdev);
Luotao Fuf94add32010-07-02 14:10:29 +0200379
380 stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
381
Luotao Fuf94add32010-07-02 14:10:29 +0200382 return 0;
383}
384
385static struct platform_driver stmpe_ts_driver = {
386 .driver = {
387 .name = STMPE_TS_NAME,
388 .owner = THIS_MODULE,
389 },
390 .probe = stmpe_input_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800391 .remove = stmpe_ts_remove,
Luotao Fuf94add32010-07-02 14:10:29 +0200392};
JJ Dingcdcc96e2011-11-29 11:14:13 -0800393module_platform_driver(stmpe_ts_driver);
Luotao Fuf94add32010-07-02 14:10:29 +0200394
395MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
396MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
397MODULE_LICENSE("GPL");
398MODULE_ALIAS("platform:" STMPE_TS_NAME);