GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / input / touchscreen / stmpe-ts.c
blob9f494f2677ad101c45ed165e32bae57ef215a23a
1 /* STMicroelectronics STMPE811 Touchscreen Driver
3 * (C) 2010 Luotao Fu <l.fu@pengutronix.de>
4 * All rights reserved.
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.
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>
26 #include <linux/mfd/stmpe.h>
28 /* Register layouts and functionalities are identical on all stmpexxx variants
29 * with touchscreen controller
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
43 #define OP_MOD_XYZ 0
45 #define STMPE_TSC_CTRL_TSC_EN (1<<0)
47 #define STMPE_FIFO_STA_RESET (1<<0)
49 #define STMPE_IRQ_TOUCH_DET 0
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)
62 #define STMPE_TS_NAME "stmpe-ts"
63 #define XY_MASK 0xfff
65 struct 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;
81 static int __stmpe_reset_fifo(struct stmpe *stmpe)
83 int ret;
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;
90 return stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
91 STMPE_FIFO_STA_RESET, 0);
94 static void stmpe_work(struct work_struct *work)
96 int int_sta;
97 u32 timeout = 40;
99 struct stmpe_touch *ts =
100 container_of(work, struct stmpe_touch, work.work);
102 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
104 while ((int_sta & (1 << STMPE_IRQ_TOUCH_DET)) && (timeout > 0)) {
105 timeout--;
106 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
107 udelay(100);
110 /* reset the FIFO before we report release event */
111 __stmpe_reset_fifo(ts->stmpe);
113 input_report_abs(ts->idev, ABS_PRESSURE, 0);
114 input_sync(ts->idev);
117 static irqreturn_t stmpe_ts_handler(int irq, void *data)
119 u8 data_set[4];
120 int x, y, z;
121 struct stmpe_touch *ts = data;
124 * Cancel scheduled polling for release if we have new value
125 * available. Wait if the polling is already running.
127 cancel_delayed_work_sync(&ts->work);
129 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
130 STMPE_TSC_CTRL_TSC_EN, 0);
132 stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
134 x = (data_set[0] << 4) | (data_set[1] >> 4);
135 y = ((data_set[1] & 0xf) << 8) | data_set[2];
136 z = data_set[3];
138 input_report_abs(ts->idev, ABS_X, x);
139 input_report_abs(ts->idev, ABS_Y, y);
140 input_report_abs(ts->idev, ABS_PRESSURE, z);
141 input_sync(ts->idev);
143 /* flush the FIFO after we have read out our values. */
144 __stmpe_reset_fifo(ts->stmpe);
146 /* reenable the tsc */
147 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
148 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
150 /* start polling for touch_det to detect release */
151 schedule_delayed_work(&ts->work, HZ / 50);
153 return IRQ_HANDLED;
156 static int __devinit stmpe_init_hw(struct stmpe_touch *ts)
158 int ret;
159 u8 adc_ctrl1, adc_ctrl1_mask, tsc_cfg, tsc_cfg_mask;
160 struct stmpe *stmpe = ts->stmpe;
161 struct device *dev = ts->dev;
163 ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
164 if (ret) {
165 dev_err(dev, "Could not enable clock for ADC and TS\n");
166 return ret;
169 adc_ctrl1 = SAMPLE_TIME(ts->sample_time) | MOD_12B(ts->mod_12b) |
170 REF_SEL(ts->ref_sel);
171 adc_ctrl1_mask = SAMPLE_TIME(0xff) | MOD_12B(0xff) | REF_SEL(0xff);
173 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL1,
174 adc_ctrl1_mask, adc_ctrl1);
175 if (ret) {
176 dev_err(dev, "Could not setup ADC\n");
177 return ret;
180 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL2,
181 ADC_FREQ(0xff), ADC_FREQ(ts->adc_freq));
182 if (ret) {
183 dev_err(dev, "Could not setup ADC\n");
184 return ret;
187 tsc_cfg = AVE_CTRL(ts->ave_ctrl) | DET_DELAY(ts->touch_det_delay) |
188 SETTLING(ts->settling);
189 tsc_cfg_mask = AVE_CTRL(0xff) | DET_DELAY(0xff) | SETTLING(0xff);
191 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
192 if (ret) {
193 dev_err(dev, "Could not config touch\n");
194 return ret;
197 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
198 FRACTION_Z(0xff), FRACTION_Z(ts->fraction_z));
199 if (ret) {
200 dev_err(dev, "Could not config touch\n");
201 return ret;
204 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
205 I_DRIVE(0xff), I_DRIVE(ts->i_drive));
206 if (ret) {
207 dev_err(dev, "Could not config touch\n");
208 return ret;
211 /* set FIFO to 1 for single point reading */
212 ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
213 if (ret) {
214 dev_err(dev, "Could not set FIFO\n");
215 return ret;
218 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
219 OP_MODE(0xff), OP_MODE(OP_MOD_XYZ));
220 if (ret) {
221 dev_err(dev, "Could not set mode\n");
222 return ret;
225 return 0;
228 static int stmpe_ts_open(struct input_dev *dev)
230 struct stmpe_touch *ts = input_get_drvdata(dev);
231 int ret = 0;
233 ret = __stmpe_reset_fifo(ts->stmpe);
234 if (ret)
235 return ret;
237 return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
238 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
241 static void stmpe_ts_close(struct input_dev *dev)
243 struct stmpe_touch *ts = input_get_drvdata(dev);
245 cancel_delayed_work_sync(&ts->work);
247 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
248 STMPE_TSC_CTRL_TSC_EN, 0);
251 static int __devinit stmpe_input_probe(struct platform_device *pdev)
253 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
254 struct stmpe_platform_data *pdata = stmpe->pdata;
255 struct stmpe_touch *ts;
256 struct input_dev *idev;
257 struct stmpe_ts_platform_data *ts_pdata = NULL;
258 int ret = 0;
259 int ts_irq;
261 ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
262 if (ts_irq < 0)
263 return ts_irq;
265 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
266 if (!ts)
267 goto err_out;
269 idev = input_allocate_device();
270 if (!idev)
271 goto err_free_ts;
273 platform_set_drvdata(pdev, ts);
274 ts->stmpe = stmpe;
275 ts->idev = idev;
276 ts->dev = &pdev->dev;
278 if (pdata)
279 ts_pdata = pdata->ts;
281 if (ts_pdata) {
282 ts->sample_time = ts_pdata->sample_time;
283 ts->mod_12b = ts_pdata->mod_12b;
284 ts->ref_sel = ts_pdata->ref_sel;
285 ts->adc_freq = ts_pdata->adc_freq;
286 ts->ave_ctrl = ts_pdata->ave_ctrl;
287 ts->touch_det_delay = ts_pdata->touch_det_delay;
288 ts->settling = ts_pdata->settling;
289 ts->fraction_z = ts_pdata->fraction_z;
290 ts->i_drive = ts_pdata->i_drive;
293 INIT_DELAYED_WORK(&ts->work, stmpe_work);
295 ret = request_threaded_irq(ts_irq, NULL, stmpe_ts_handler,
296 IRQF_ONESHOT, STMPE_TS_NAME, ts);
297 if (ret) {
298 dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
299 goto err_free_input;
302 ret = stmpe_init_hw(ts);
303 if (ret)
304 goto err_free_irq;
306 idev->name = STMPE_TS_NAME;
307 idev->id.bustype = BUS_I2C;
308 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
309 idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
311 idev->open = stmpe_ts_open;
312 idev->close = stmpe_ts_close;
314 input_set_drvdata(idev, ts);
316 input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
317 input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
318 input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
320 ret = input_register_device(idev);
321 if (ret) {
322 dev_err(&pdev->dev, "Could not register input device\n");
323 goto err_free_irq;
326 return ret;
328 err_free_irq:
329 free_irq(ts_irq, ts);
330 err_free_input:
331 input_free_device(idev);
332 platform_set_drvdata(pdev, NULL);
333 err_free_ts:
334 kfree(ts);
335 err_out:
336 return ret;
339 static int __devexit stmpe_ts_remove(struct platform_device *pdev)
341 struct stmpe_touch *ts = platform_get_drvdata(pdev);
342 unsigned int ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
344 stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
346 free_irq(ts_irq, ts);
348 platform_set_drvdata(pdev, NULL);
350 input_unregister_device(ts->idev);
351 input_free_device(ts->idev);
353 kfree(ts);
355 return 0;
358 static struct platform_driver stmpe_ts_driver = {
359 .driver = {
360 .name = STMPE_TS_NAME,
361 .owner = THIS_MODULE,
363 .probe = stmpe_input_probe,
364 .remove = __devexit_p(stmpe_ts_remove),
367 static int __init stmpe_ts_init(void)
369 return platform_driver_register(&stmpe_ts_driver);
372 module_init(stmpe_ts_init);
374 static void __exit stmpe_ts_exit(void)
376 platform_driver_unregister(&stmpe_ts_driver);
379 module_exit(stmpe_ts_exit);
381 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
382 MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
383 MODULE_LICENSE("GPL");
384 MODULE_ALIAS("platform:" STMPE_TS_NAME);