]> Pileus Git - ~andy/linux/blob - drivers/input/touchscreen/tps6507x-ts.c
d3c2967f86fbe52c8add2a5c072e93f3be01a27a
[~andy/linux] / drivers / input / touchscreen / tps6507x-ts.c
1 /*
2  * Touchscreen driver for the tps6507x chip.
3  *
4  * Copyright (c) 2009 RidgeRun (todd.fischer@ridgerun.com)
5  *
6  * Credits:
7  *
8  *    Using code from tsc2007, MtekVision Co., Ltd.
9  *
10  * For licencing details see kernel-base/COPYING
11  *
12  * TPS65070, TPS65073, TPS650731, and TPS650732 support
13  * 10 bit touch screen interface.
14  */
15
16 #include <linux/module.h>
17 #include <linux/workqueue.h>
18 #include <linux/slab.h>
19 #include <linux/input.h>
20 #include <linux/platform_device.h>
21 #include <linux/mfd/tps6507x.h>
22 #include <linux/input/tps6507x-ts.h>
23 #include <linux/delay.h>
24
25 #define TSC_DEFAULT_POLL_PERIOD 30 /* ms */
26 #define TPS_DEFAULT_MIN_PRESSURE 0x30
27 #define MAX_10BIT ((1 << 10) - 1)
28
29 #define TPS6507X_ADCONFIG_CONVERT_TS (TPS6507X_ADCONFIG_AD_ENABLE | \
30                                          TPS6507X_ADCONFIG_START_CONVERSION | \
31                                          TPS6507X_ADCONFIG_INPUT_REAL_TSC)
32 #define TPS6507X_ADCONFIG_POWER_DOWN_TS (TPS6507X_ADCONFIG_INPUT_REAL_TSC)
33
34 struct ts_event {
35         u16     x;
36         u16     y;
37         u16     pressure;
38 };
39
40 struct tps6507x_ts {
41         struct input_dev        *input_dev;
42         struct device           *dev;
43         char                    phys[32];
44         struct delayed_work     work;
45         struct ts_event         tc;
46         struct tps6507x_dev     *mfd;
47         u16                     min_pressure;
48         unsigned long           poll_period;    /* ms */
49         bool                    pendown;
50 };
51
52 static int tps6507x_read_u8(struct tps6507x_ts *tsc, u8 reg, u8 *data)
53 {
54         int err;
55
56         err = tsc->mfd->read_dev(tsc->mfd, reg, 1, data);
57
58         if (err)
59                 return err;
60
61         return 0;
62 }
63
64 static int tps6507x_write_u8(struct tps6507x_ts *tsc, u8 reg, u8 data)
65 {
66         return tsc->mfd->write_dev(tsc->mfd, reg, 1, &data);
67 }
68
69 static s32 tps6507x_adc_conversion(struct tps6507x_ts *tsc,
70                                    u8 tsc_mode, u16 *value)
71 {
72         s32 ret;
73         u8 adc_status;
74         u8 result;
75
76         /* Route input signal to A/D converter */
77
78         ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE, tsc_mode);
79         if (ret) {
80                 dev_err(tsc->dev, "TSC mode read failed\n");
81                 goto err;
82         }
83
84         /* Start A/D conversion */
85
86         ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
87                                 TPS6507X_ADCONFIG_CONVERT_TS);
88         if (ret) {
89                 dev_err(tsc->dev, "ADC config write failed\n");
90                 return ret;
91         }
92
93         do {
94                 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADCONFIG,
95                                        &adc_status);
96                 if (ret) {
97                         dev_err(tsc->dev, "ADC config read failed\n");
98                         goto err;
99                 }
100         } while (adc_status & TPS6507X_ADCONFIG_START_CONVERSION);
101
102         ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_2, &result);
103         if (ret) {
104                 dev_err(tsc->dev, "ADC result 2 read failed\n");
105                 goto err;
106         }
107
108         *value = (result & TPS6507X_REG_ADRESULT_2_MASK) << 8;
109
110         ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_1, &result);
111         if (ret) {
112                 dev_err(tsc->dev, "ADC result 1 read failed\n");
113                 goto err;
114         }
115
116         *value |= result;
117
118         dev_dbg(tsc->dev, "TSC channel %d = 0x%X\n", tsc_mode, *value);
119
120 err:
121         return ret;
122 }
123
124 /* Need to call tps6507x_adc_standby() after using A/D converter for the
125  * touch screen interrupt to work properly.
126  */
127
128 static s32 tps6507x_adc_standby(struct tps6507x_ts *tsc)
129 {
130         s32 ret;
131         s32 loops = 0;
132         u8 val;
133
134         ret = tps6507x_write_u8(tsc,  TPS6507X_REG_ADCONFIG,
135                                 TPS6507X_ADCONFIG_INPUT_TSC);
136         if (ret)
137                 return ret;
138
139         ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE,
140                                 TPS6507X_TSCMODE_STANDBY);
141         if (ret)
142                 return ret;
143
144         ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
145         if (ret)
146                 return ret;
147
148         while (val & TPS6507X_REG_TSC_INT) {
149                 mdelay(10);
150                 ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
151                 if (ret)
152                         return ret;
153                 loops++;
154         }
155
156         return ret;
157 }
158
159 static void tps6507x_ts_handler(struct work_struct *work)
160 {
161         struct tps6507x_ts *tsc =  container_of(work,
162                                 struct tps6507x_ts, work.work);
163         struct input_dev *input_dev = tsc->input_dev;
164         bool pendown;
165         int schd;
166         s32 ret;
167
168         ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_PRESSURE,
169                                       &tsc->tc.pressure);
170         if (ret)
171                 goto done;
172
173         pendown = tsc->tc.pressure > tsc->min_pressure;
174
175         if (unlikely(!pendown && tsc->pendown)) {
176                 dev_dbg(tsc->dev, "UP\n");
177                 input_report_key(input_dev, BTN_TOUCH, 0);
178                 input_report_abs(input_dev, ABS_PRESSURE, 0);
179                 input_sync(input_dev);
180                 tsc->pendown = false;
181         }
182
183         if (pendown) {
184
185                 if (!tsc->pendown) {
186                         dev_dbg(tsc->dev, "DOWN\n");
187                         input_report_key(input_dev, BTN_TOUCH, 1);
188                 } else
189                         dev_dbg(tsc->dev, "still down\n");
190
191                 ret =  tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_X_POSITION,
192                                                &tsc->tc.x);
193                 if (ret)
194                         goto done;
195
196                 ret =  tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_Y_POSITION,
197                                                &tsc->tc.y);
198                 if (ret)
199                         goto done;
200
201                 input_report_abs(input_dev, ABS_X, tsc->tc.x);
202                 input_report_abs(input_dev, ABS_Y, tsc->tc.y);
203                 input_report_abs(input_dev, ABS_PRESSURE, tsc->tc.pressure);
204                 input_sync(input_dev);
205                 tsc->pendown = true;
206         }
207
208 done:
209         /* always poll if not using interrupts */
210         schd = schedule_delayed_work(&tsc->work,
211                                 msecs_to_jiffies(tsc->poll_period));
212         if (!schd)
213                 dev_err(tsc->dev, "re-schedule failed");
214
215         ret = tps6507x_adc_standby(tsc);
216 }
217
218 static int tps6507x_ts_probe(struct platform_device *pdev)
219 {
220         int error;
221         struct tps6507x_ts *tsc;
222         struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
223         struct touchscreen_init_data *init_data;
224         struct input_dev *input_dev;
225         struct tps6507x_board *tps_board;
226         int schd;
227
228         /**
229          * tps_board points to pmic related constants
230          * coming from the board-evm file.
231          */
232
233         tps_board = (struct tps6507x_board *)tps6507x_dev->dev->platform_data;
234
235         if (!tps_board) {
236                 dev_err(tps6507x_dev->dev,
237                         "Could not find tps6507x platform data\n");
238                 return -EIO;
239         }
240
241         /**
242          * init_data points to array of regulator_init structures
243          * coming from the board-evm file.
244          */
245
246         init_data = tps_board->tps6507x_ts_init_data;
247
248         tsc = kzalloc(sizeof(struct tps6507x_ts), GFP_KERNEL);
249         if (!tsc) {
250                 dev_err(tps6507x_dev->dev, "failed to allocate driver data\n");
251                 error = -ENOMEM;
252                 goto err0;
253         }
254
255         tps6507x_dev->ts = tsc;
256         tsc->mfd = tps6507x_dev;
257         tsc->dev = tps6507x_dev->dev;
258         input_dev = input_allocate_device();
259         if (!input_dev) {
260                 dev_err(tsc->dev, "Failed to allocate input device.\n");
261                 error = -ENOMEM;
262                 goto err1;
263         }
264
265         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
266         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
267
268         input_set_abs_params(input_dev, ABS_X, 0, MAX_10BIT, 0, 0);
269         input_set_abs_params(input_dev, ABS_Y, 0, MAX_10BIT, 0, 0);
270         input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_10BIT, 0, 0);
271
272         input_dev->name = "TPS6507x Touchscreen";
273         input_dev->id.bustype = BUS_I2C;
274         input_dev->dev.parent = tsc->dev;
275
276         snprintf(tsc->phys, sizeof(tsc->phys),
277                  "%s/input0", dev_name(tsc->dev));
278         input_dev->phys = tsc->phys;
279
280         dev_dbg(tsc->dev, "device: %s\n", input_dev->phys);
281
282         input_set_drvdata(input_dev, tsc);
283
284         tsc->input_dev = input_dev;
285
286         INIT_DELAYED_WORK(&tsc->work, tps6507x_ts_handler);
287
288         if (init_data) {
289                 tsc->poll_period = init_data->poll_period;
290                 tsc->min_pressure = init_data->min_pressure;
291                 input_dev->id.vendor = init_data->vendor;
292                 input_dev->id.product = init_data->product;
293                 input_dev->id.version = init_data->version;
294         } else {
295                 tsc->poll_period = TSC_DEFAULT_POLL_PERIOD;
296                 tsc->min_pressure = TPS_DEFAULT_MIN_PRESSURE;
297         }
298
299         error = tps6507x_adc_standby(tsc);
300         if (error)
301                 goto err2;
302
303         error = input_register_device(input_dev);
304         if (error)
305                 goto err2;
306
307         schd = schedule_delayed_work(&tsc->work,
308                                      msecs_to_jiffies(tsc->poll_period));
309
310         if (!schd) {
311                 dev_err(tsc->dev, "schedule failed");
312                 goto err2;
313          }
314         platform_set_drvdata(pdev, tps6507x_dev);
315
316         return 0;
317
318 err2:
319         cancel_delayed_work_sync(&tsc->work);
320         input_free_device(input_dev);
321 err1:
322         kfree(tsc);
323         tps6507x_dev->ts = NULL;
324 err0:
325         return error;
326 }
327
328 static int tps6507x_ts_remove(struct platform_device *pdev)
329 {
330         struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
331         struct tps6507x_ts *tsc = tps6507x_dev->ts;
332         struct input_dev *input_dev = tsc->input_dev;
333
334         cancel_delayed_work_sync(&tsc->work);
335
336         input_unregister_device(input_dev);
337
338         tps6507x_dev->ts = NULL;
339         kfree(tsc);
340
341         return 0;
342 }
343
344 static struct platform_driver tps6507x_ts_driver = {
345         .driver = {
346                 .name = "tps6507x-ts",
347                 .owner = THIS_MODULE,
348         },
349         .probe = tps6507x_ts_probe,
350         .remove = tps6507x_ts_remove,
351 };
352 module_platform_driver(tps6507x_ts_driver);
353
354 MODULE_AUTHOR("Todd Fischer <todd.fischer@ridgerun.com>");
355 MODULE_DESCRIPTION("TPS6507x - TouchScreen driver");
356 MODULE_LICENSE("GPL v2");
357 MODULE_ALIAS("platform:tps6507x-ts");