]> Pileus Git - ~andy/linux/blob - drivers/input/touchscreen/tsc2007.c
88b150a0b8eecafe6066004c975bdf0198de585c
[~andy/linux] / drivers / input / touchscreen / tsc2007.c
1 /*
2  * drivers/input/touchscreen/tsc2007.c
3  *
4  * Copyright (c) 2008 MtekVision Co., Ltd.
5  *      Kwangwoo Lee <kwlee@mtekvision.com>
6  *
7  * Using code from:
8  *  - ads7846.c
9  *      Copyright (c) 2005 David Brownell
10  *      Copyright (c) 2006 Nokia Corporation
11  *  - corgi_ts.c
12  *      Copyright (C) 2004-2005 Richard Purdie
13  *  - omap_ts.[hc], ads7846.h, ts_osk.c
14  *      Copyright (C) 2002 MontaVista Software
15  *      Copyright (C) 2004 Texas Instruments
16  *      Copyright (C) 2005 Dirk Behme
17  *
18  *  This program is free software; you can redistribute it and/or modify
19  *  it under the terms of the GNU General Public License version 2 as
20  *  published by the Free Software Foundation.
21  */
22
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/input.h>
26 #include <linux/interrupt.h>
27 #include <linux/i2c.h>
28 #include <linux/i2c/tsc2007.h>
29 #include <linux/of_device.h>
30 #include <linux/of.h>
31 #include <linux/of_gpio.h>
32
33 #define TSC2007_MEASURE_TEMP0           (0x0 << 4)
34 #define TSC2007_MEASURE_AUX             (0x2 << 4)
35 #define TSC2007_MEASURE_TEMP1           (0x4 << 4)
36 #define TSC2007_ACTIVATE_XN             (0x8 << 4)
37 #define TSC2007_ACTIVATE_YN             (0x9 << 4)
38 #define TSC2007_ACTIVATE_YP_XN          (0xa << 4)
39 #define TSC2007_SETUP                   (0xb << 4)
40 #define TSC2007_MEASURE_X               (0xc << 4)
41 #define TSC2007_MEASURE_Y               (0xd << 4)
42 #define TSC2007_MEASURE_Z1              (0xe << 4)
43 #define TSC2007_MEASURE_Z2              (0xf << 4)
44
45 #define TSC2007_POWER_OFF_IRQ_EN        (0x0 << 2)
46 #define TSC2007_ADC_ON_IRQ_DIS0         (0x1 << 2)
47 #define TSC2007_ADC_OFF_IRQ_EN          (0x2 << 2)
48 #define TSC2007_ADC_ON_IRQ_DIS1         (0x3 << 2)
49
50 #define TSC2007_12BIT                   (0x0 << 1)
51 #define TSC2007_8BIT                    (0x1 << 1)
52
53 #define MAX_12BIT                       ((1 << 12) - 1)
54
55 #define ADC_ON_12BIT    (TSC2007_12BIT | TSC2007_ADC_ON_IRQ_DIS0)
56
57 #define READ_Y          (ADC_ON_12BIT | TSC2007_MEASURE_Y)
58 #define READ_Z1         (ADC_ON_12BIT | TSC2007_MEASURE_Z1)
59 #define READ_Z2         (ADC_ON_12BIT | TSC2007_MEASURE_Z2)
60 #define READ_X          (ADC_ON_12BIT | TSC2007_MEASURE_X)
61 #define PWRDOWN         (TSC2007_12BIT | TSC2007_POWER_OFF_IRQ_EN)
62
63 struct ts_event {
64         u16     x;
65         u16     y;
66         u16     z1, z2;
67 };
68
69 struct tsc2007 {
70         struct input_dev        *input;
71         char                    phys[32];
72
73         struct i2c_client       *client;
74
75         u16                     model;
76         u16                     x_plate_ohms;
77         u16                     max_rt;
78         unsigned long           poll_delay;
79         unsigned long           poll_period;
80         int                     fuzzx;
81         int                     fuzzy;
82         int                     fuzzz;
83
84         unsigned                gpio;
85         int                     irq;
86
87         wait_queue_head_t       wait;
88         bool                    stopped;
89
90         int                     (*get_pendown_state)(struct device *);
91         void                    (*clear_penirq)(void);
92 };
93
94 static inline int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd)
95 {
96         s32 data;
97         u16 val;
98
99         data = i2c_smbus_read_word_data(tsc->client, cmd);
100         if (data < 0) {
101                 dev_err(&tsc->client->dev, "i2c io error: %d\n", data);
102                 return data;
103         }
104
105         /* The protocol and raw data format from i2c interface:
106          * S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
107          * Where DataLow has [D11-D4], DataHigh has [D3-D0 << 4 | Dummy 4bit].
108          */
109         val = swab16(data) >> 4;
110
111         dev_dbg(&tsc->client->dev, "data: 0x%x, val: 0x%x\n", data, val);
112
113         return val;
114 }
115
116 static void tsc2007_read_values(struct tsc2007 *tsc, struct ts_event *tc)
117 {
118         /* y- still on; turn on only y+ (and ADC) */
119         tc->y = tsc2007_xfer(tsc, READ_Y);
120
121         /* turn y- off, x+ on, then leave in lowpower */
122         tc->x = tsc2007_xfer(tsc, READ_X);
123
124         /* turn y+ off, x- on; we'll use formula #1 */
125         tc->z1 = tsc2007_xfer(tsc, READ_Z1);
126         tc->z2 = tsc2007_xfer(tsc, READ_Z2);
127
128         /* Prepare for next touch reading - power down ADC, enable PENIRQ */
129         tsc2007_xfer(tsc, PWRDOWN);
130 }
131
132 static u32 tsc2007_calculate_pressure(struct tsc2007 *tsc, struct ts_event *tc)
133 {
134         u32 rt = 0;
135
136         /* range filtering */
137         if (tc->x == MAX_12BIT)
138                 tc->x = 0;
139
140         if (likely(tc->x && tc->z1)) {
141                 /* compute touch pressure resistance using equation #1 */
142                 rt = tc->z2 - tc->z1;
143                 rt *= tc->x;
144                 rt *= tsc->x_plate_ohms;
145                 rt /= tc->z1;
146                 rt = (rt + 2047) >> 12;
147         }
148
149         return rt;
150 }
151
152 static bool tsc2007_is_pen_down(struct tsc2007 *ts)
153 {
154         /*
155          * NOTE: We can't rely on the pressure to determine the pen down
156          * state, even though this controller has a pressure sensor.
157          * The pressure value can fluctuate for quite a while after
158          * lifting the pen and in some cases may not even settle at the
159          * expected value.
160          *
161          * The only safe way to check for the pen up condition is in the
162          * work function by reading the pen signal state (it's a GPIO
163          * and IRQ). Unfortunately such callback is not always available,
164          * in that case we assume that the pen is down and expect caller
165          * to fall back on the pressure reading.
166          */
167
168         if (!ts->get_pendown_state)
169                 return true;
170
171         return ts->get_pendown_state(&ts->client->dev);
172 }
173
174 static irqreturn_t tsc2007_soft_irq(int irq, void *handle)
175 {
176         struct tsc2007 *ts = handle;
177         struct input_dev *input = ts->input;
178         struct ts_event tc;
179         u32 rt;
180
181         while (!ts->stopped && tsc2007_is_pen_down(ts)) {
182
183                 /* pen is down, continue with the measurement */
184                 tsc2007_read_values(ts, &tc);
185
186                 rt = tsc2007_calculate_pressure(ts, &tc);
187
188                 if (!rt && !ts->get_pendown_state) {
189                         /*
190                          * If pressure reported is 0 and we don't have
191                          * callback to check pendown state, we have to
192                          * assume that pen was lifted up.
193                          */
194                         break;
195                 }
196
197                 if (rt <= ts->max_rt) {
198                         dev_dbg(&ts->client->dev,
199                                 "DOWN point(%4d,%4d), pressure (%4u)\n",
200                                 tc.x, tc.y, rt);
201
202                         input_report_key(input, BTN_TOUCH, 1);
203                         input_report_abs(input, ABS_X, tc.x);
204                         input_report_abs(input, ABS_Y, tc.y);
205                         input_report_abs(input, ABS_PRESSURE, rt);
206
207                         input_sync(input);
208
209                 } else {
210                         /*
211                          * Sample found inconsistent by debouncing or pressure is
212                          * beyond the maximum. Don't report it to user space,
213                          * repeat at least once more the measurement.
214                          */
215                         dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
216                 }
217
218                 wait_event_timeout(ts->wait, ts->stopped,
219                                    msecs_to_jiffies(ts->poll_period));
220         }
221
222         dev_dbg(&ts->client->dev, "UP\n");
223
224         input_report_key(input, BTN_TOUCH, 0);
225         input_report_abs(input, ABS_PRESSURE, 0);
226         input_sync(input);
227
228         if (ts->clear_penirq)
229                 ts->clear_penirq();
230
231         return IRQ_HANDLED;
232 }
233
234 static irqreturn_t tsc2007_hard_irq(int irq, void *handle)
235 {
236         struct tsc2007 *ts = handle;
237
238         if (tsc2007_is_pen_down(ts))
239                 return IRQ_WAKE_THREAD;
240
241         if (ts->clear_penirq)
242                 ts->clear_penirq();
243
244         return IRQ_HANDLED;
245 }
246
247 static void tsc2007_stop(struct tsc2007 *ts)
248 {
249         ts->stopped = true;
250         mb();
251         wake_up(&ts->wait);
252
253         disable_irq(ts->irq);
254 }
255
256 static int tsc2007_open(struct input_dev *input_dev)
257 {
258         struct tsc2007 *ts = input_get_drvdata(input_dev);
259         int err;
260
261         ts->stopped = false;
262         mb();
263
264         enable_irq(ts->irq);
265
266         /* Prepare for touch readings - power down ADC and enable PENIRQ */
267         err = tsc2007_xfer(ts, PWRDOWN);
268         if (err < 0) {
269                 tsc2007_stop(ts);
270                 return err;
271         }
272
273         return 0;
274 }
275
276 static void tsc2007_close(struct input_dev *input_dev)
277 {
278         struct tsc2007 *ts = input_get_drvdata(input_dev);
279
280         tsc2007_stop(ts);
281 }
282
283 #ifdef CONFIG_OF
284 static int tsc2007_get_pendown_state_gpio(struct device *dev)
285 {
286         struct i2c_client *client = to_i2c_client(dev);
287         struct tsc2007 *ts = i2c_get_clientdata(client);
288
289         return !gpio_get_value(ts->gpio);
290 }
291
292 static int tsc2007_probe_dt(struct i2c_client *client, struct tsc2007 *ts)
293 {
294         struct device_node *np = client->dev.of_node;
295         u32 val32;
296         u64 val64;
297
298         if (!np) {
299                 dev_err(&client->dev, "missing device tree data\n");
300                 return -EINVAL;
301         }
302
303         if (!of_property_read_u32(np, "ti,max-rt", &val32))
304                 ts->max_rt = val32;
305         else
306                 ts->max_rt = MAX_12BIT;
307
308         if (!of_property_read_u32(np, "ti,fuzzx", &val32))
309                 ts->fuzzx = val32;
310
311         if (!of_property_read_u32(np, "ti,fuzzy", &val32))
312                 ts->fuzzy = val32;
313
314         if (!of_property_read_u32(np, "ti,fuzzz", &val32))
315                 ts->fuzzz = val32;
316
317         if (!of_property_read_u64(np, "ti,poll-period", &val64))
318                 ts->poll_period = val64;
319         else
320                 ts->poll_period = 1;
321
322         if (!of_property_read_u32(np, "ti,x-plate-ohms", &val32)) {
323                 ts->x_plate_ohms = val32;
324         } else {
325                 dev_err(&client->dev, "missing ti,x-plate-ohms devicetree property.");
326                 return -EINVAL;
327         }
328
329         ts->gpio = of_get_gpio(np, 0);
330         if (gpio_is_valid(ts->gpio))
331                 ts->get_pendown_state = tsc2007_get_pendown_state_gpio;
332         else
333                 dev_warn(&client->dev,
334                          "GPIO not specified in DT (of_get_gpio returned %d)\n",
335                          ts->gpio);
336
337         return 0;
338 }
339 #else
340 static int tsc2007_probe_dt(struct i2c_client *client, struct tsc2007 *ts)
341 {
342         dev_err(&client->dev, "platform data is required!\n");
343         return -EINVAL;
344 }
345 #endif
346
347 static int tsc2007_probe_pdev(struct i2c_client *client, struct tsc2007 *ts,
348                               const struct tsc2007_platform_data *pdata,
349                               const struct i2c_device_id *id)
350 {
351         ts->model             = pdata->model;
352         ts->x_plate_ohms      = pdata->x_plate_ohms;
353         ts->max_rt            = pdata->max_rt ? : MAX_12BIT;
354         ts->poll_delay        = pdata->poll_delay ? : 1;
355         ts->poll_period       = pdata->poll_period ? : 1;
356         ts->get_pendown_state = pdata->get_pendown_state;
357         ts->clear_penirq      = pdata->clear_penirq;
358         ts->fuzzx             = pdata->fuzzx;
359         ts->fuzzy             = pdata->fuzzy;
360         ts->fuzzz             = pdata->fuzzz;
361
362         if (pdata->x_plate_ohms == 0) {
363                 dev_err(&client->dev, "x_plate_ohms is not set up in platform data");
364                 return -EINVAL;
365         }
366
367         return 0;
368 }
369
370 static int tsc2007_probe(struct i2c_client *client,
371                          const struct i2c_device_id *id)
372 {
373         const struct tsc2007_platform_data *pdata = dev_get_platdata(&client->dev);
374         struct tsc2007 *ts;
375         struct input_dev *input_dev;
376         int err;
377
378         if (!i2c_check_functionality(client->adapter,
379                                      I2C_FUNC_SMBUS_READ_WORD_DATA))
380                 return -EIO;
381
382         ts = devm_kzalloc(&client->dev, sizeof(struct tsc2007), GFP_KERNEL);
383         if (!ts)
384                 return -ENOMEM;
385
386         if (pdata)
387                 err = tsc2007_probe_pdev(client, ts, pdata, id);
388         else
389                 err = tsc2007_probe_dt(client, ts);
390         if (err)
391                 return err;
392
393         input_dev = input_allocate_device();
394         if (!input_dev) {
395                 err = -ENOMEM;
396                 goto err_free_input;
397         };
398
399         i2c_set_clientdata(client, ts);
400
401         ts->client = client;
402         ts->irq = client->irq;
403         ts->input = input_dev;
404         init_waitqueue_head(&ts->wait);
405
406         snprintf(ts->phys, sizeof(ts->phys),
407                  "%s/input0", dev_name(&client->dev));
408
409         input_dev->name = "TSC2007 Touchscreen";
410         input_dev->phys = ts->phys;
411         input_dev->id.bustype = BUS_I2C;
412
413         input_dev->open = tsc2007_open;
414         input_dev->close = tsc2007_close;
415
416         input_set_drvdata(input_dev, ts);
417
418         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
419         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
420
421         input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, ts->fuzzx, 0);
422         input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, ts->fuzzy, 0);
423         input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
424                              ts->fuzzz, 0);
425
426         if (pdata && pdata->init_platform_hw)
427                 pdata->init_platform_hw();
428
429         err = request_threaded_irq(ts->irq, tsc2007_hard_irq, tsc2007_soft_irq,
430                                    IRQF_ONESHOT, client->dev.driver->name, ts);
431         if (err < 0) {
432                 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
433                 goto err_free_input;
434         }
435
436         tsc2007_stop(ts);
437
438         err = input_register_device(input_dev);
439         if (err)
440                 goto err_free_irq;
441
442         return 0;
443
444  err_free_irq:
445         free_irq(ts->irq, ts);
446         if (pdata && pdata->exit_platform_hw)
447                 pdata->exit_platform_hw();
448  err_free_input:
449         input_free_device(input_dev);
450         return err;
451 }
452
453 static int tsc2007_remove(struct i2c_client *client)
454 {
455         const struct tsc2007_platform_data *pdata = dev_get_platdata(&client->dev);
456         struct tsc2007 *ts = i2c_get_clientdata(client);
457
458         free_irq(ts->irq, ts);
459
460         if (pdata && pdata->exit_platform_hw)
461                 pdata->exit_platform_hw();
462
463         input_unregister_device(ts->input);
464         kfree(ts);
465
466         return 0;
467 }
468
469 static const struct i2c_device_id tsc2007_idtable[] = {
470         { "tsc2007", 0 },
471         { }
472 };
473
474 MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
475
476 #ifdef CONFIG_OF
477 static const struct of_device_id tsc2007_of_match[] = {
478         { .compatible = "ti,tsc2007" },
479         { /* sentinel */ }
480 };
481 MODULE_DEVICE_TABLE(of, tsc2007_of_match);
482 #endif
483
484 static struct i2c_driver tsc2007_driver = {
485         .driver = {
486                 .owner  = THIS_MODULE,
487                 .name   = "tsc2007",
488                 .of_match_table = of_match_ptr(tsc2007_of_match),
489         },
490         .id_table       = tsc2007_idtable,
491         .probe          = tsc2007_probe,
492         .remove         = tsc2007_remove,
493 };
494
495 module_i2c_driver(tsc2007_driver);
496
497 MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
498 MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
499 MODULE_LICENSE("GPL");