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