Input: pixcir_i2c_ts - add RESET gpio
[cascardo/linux.git] / drivers / input / touchscreen / pixcir_i2c_ts.c
1 /*
2  * Driver for Pixcir I2C touchscreen controllers.
3  *
4  * Copyright (C) 2010-2011 Pixcir, Inc.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  */
19
20 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24 #include <linux/i2c.h>
25 #include <linux/input.h>
26 #include <linux/input/mt.h>
27 #include <linux/gpio.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include <linux/platform_data/pixcir_i2c_ts.h>
32
33 #define PIXCIR_MAX_SLOTS       5 /* Max fingers supported by driver */
34
35 struct pixcir_i2c_ts_data {
36         struct i2c_client *client;
37         struct input_dev *input;
38         struct gpio_desc *gpio_attb;
39         struct gpio_desc *gpio_reset;
40         const struct pixcir_ts_platform_data *pdata;
41         bool running;
42         int max_fingers;        /* Max fingers supported in this instance */
43 };
44
45 struct pixcir_touch {
46         int x;
47         int y;
48         int id;
49 };
50
51 struct pixcir_report_data {
52         int num_touches;
53         struct pixcir_touch touches[PIXCIR_MAX_SLOTS];
54 };
55
56 static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
57                             struct pixcir_report_data *report)
58 {
59         u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
60         u8 wrbuf[1] = { 0 };
61         u8 *bufptr;
62         u8 touch;
63         int ret, i;
64         int readsize;
65         const struct pixcir_i2c_chip_data *chip = &tsdata->pdata->chip;
66
67         memset(report, 0, sizeof(struct pixcir_report_data));
68
69         i = chip->has_hw_ids ? 1 : 0;
70         readsize = 2 + tsdata->max_fingers * (4 + i);
71         if (readsize > sizeof(rdbuf))
72                 readsize = sizeof(rdbuf);
73
74         ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
75         if (ret != sizeof(wrbuf)) {
76                 dev_err(&tsdata->client->dev,
77                         "%s: i2c_master_send failed(), ret=%d\n",
78                         __func__, ret);
79                 return;
80         }
81
82         ret = i2c_master_recv(tsdata->client, rdbuf, readsize);
83         if (ret != sizeof(rdbuf)) {
84                 dev_err(&tsdata->client->dev,
85                         "%s: i2c_master_recv failed(), ret=%d\n",
86                         __func__, ret);
87                 return;
88         }
89
90         touch = rdbuf[0] & 0x7;
91         if (touch > tsdata->max_fingers)
92                 touch = tsdata->max_fingers;
93
94         report->num_touches = touch;
95         bufptr = &rdbuf[2];
96
97         for (i = 0; i < touch; i++) {
98                 report->touches[i].x = (bufptr[1] << 8) | bufptr[0];
99                 report->touches[i].y = (bufptr[3] << 8) | bufptr[2];
100
101                 if (chip->has_hw_ids) {
102                         report->touches[i].id = bufptr[4];
103                         bufptr = bufptr + 5;
104                 } else {
105                         bufptr = bufptr + 4;
106                 }
107         }
108 }
109
110 static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts,
111                              struct pixcir_report_data *report)
112 {
113         struct input_mt_pos pos[PIXCIR_MAX_SLOTS];
114         int slots[PIXCIR_MAX_SLOTS];
115         struct pixcir_touch *touch;
116         int n, i, slot;
117         struct device *dev = &ts->client->dev;
118         const struct pixcir_i2c_chip_data *chip = &ts->pdata->chip;
119
120         n = report->num_touches;
121         if (n > PIXCIR_MAX_SLOTS)
122                 n = PIXCIR_MAX_SLOTS;
123
124         if (!chip->has_hw_ids) {
125                 for (i = 0; i < n; i++) {
126                         touch = &report->touches[i];
127                         pos[i].x = touch->x;
128                         pos[i].y = touch->y;
129                 }
130
131                 input_mt_assign_slots(ts->input, slots, pos, n, 0);
132         }
133
134         for (i = 0; i < n; i++) {
135                 touch = &report->touches[i];
136
137                 if (chip->has_hw_ids) {
138                         slot = input_mt_get_slot_by_key(ts->input, touch->id);
139                         if (slot < 0) {
140                                 dev_dbg(dev, "no free slot for id 0x%x\n",
141                                         touch->id);
142                                 continue;
143                         }
144                 } else {
145                         slot = slots[i];
146                 }
147
148                 input_mt_slot(ts->input, slot);
149                 input_mt_report_slot_state(ts->input,
150                                            MT_TOOL_FINGER, true);
151
152                 input_event(ts->input, EV_ABS, ABS_MT_POSITION_X, touch->x);
153                 input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y, touch->y);
154
155                 dev_dbg(dev, "%d: slot %d, x %d, y %d\n",
156                         i, slot, touch->x, touch->y);
157         }
158
159         input_mt_sync_frame(ts->input);
160         input_sync(ts->input);
161 }
162
163 static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
164 {
165         struct pixcir_i2c_ts_data *tsdata = dev_id;
166         struct pixcir_report_data report;
167
168         while (tsdata->running) {
169                 /* parse packet */
170                 pixcir_ts_parse(tsdata, &report);
171
172                 /* report it */
173                 pixcir_ts_report(tsdata, &report);
174
175                 if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
176                         if (report.num_touches) {
177                                 /*
178                                  * Last report with no finger up?
179                                  * Do it now then.
180                                  */
181                                 input_mt_sync_frame(tsdata->input);
182                                 input_sync(tsdata->input);
183                         }
184                         break;
185                 }
186
187                 msleep(20);
188         }
189
190         return IRQ_HANDLED;
191 }
192
193 static void pixcir_reset(struct pixcir_i2c_ts_data *tsdata)
194 {
195         if (!IS_ERR_OR_NULL(tsdata->gpio_reset)) {
196                 gpiod_set_value_cansleep(tsdata->gpio_reset, 1);
197                 ndelay(100);    /* datasheet section 1.2.3 says 80ns min. */
198                 gpiod_set_value_cansleep(tsdata->gpio_reset, 0);
199                 /* wait for controller ready. 100ms guess. */
200                 msleep(100);
201         }
202 }
203
204 static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
205                                  enum pixcir_power_mode mode)
206 {
207         struct device *dev = &ts->client->dev;
208         int ret;
209
210         ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
211         if (ret < 0) {
212                 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
213                         __func__, PIXCIR_REG_POWER_MODE, ret);
214                 return ret;
215         }
216
217         ret &= ~PIXCIR_POWER_MODE_MASK;
218         ret |= mode;
219
220         /* Always AUTO_IDLE */
221         ret |= PIXCIR_POWER_ALLOW_IDLE;
222
223         ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
224         if (ret < 0) {
225                 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
226                         __func__, PIXCIR_REG_POWER_MODE, ret);
227                 return ret;
228         }
229
230         return 0;
231 }
232
233 /*
234  * Set the interrupt mode for the device i.e. ATTB line behaviour
235  *
236  * @polarity : 1 for active high, 0 for active low.
237  */
238 static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
239                                enum pixcir_int_mode mode, bool polarity)
240 {
241         struct device *dev = &ts->client->dev;
242         int ret;
243
244         ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
245         if (ret < 0) {
246                 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
247                         __func__, PIXCIR_REG_INT_MODE, ret);
248                 return ret;
249         }
250
251         ret &= ~PIXCIR_INT_MODE_MASK;
252         ret |= mode;
253
254         if (polarity)
255                 ret |= PIXCIR_INT_POL_HIGH;
256         else
257                 ret &= ~PIXCIR_INT_POL_HIGH;
258
259         ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
260         if (ret < 0) {
261                 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
262                         __func__, PIXCIR_REG_INT_MODE, ret);
263                 return ret;
264         }
265
266         return 0;
267 }
268
269 /*
270  * Enable/disable interrupt generation
271  */
272 static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
273 {
274         struct device *dev = &ts->client->dev;
275         int ret;
276
277         ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
278         if (ret < 0) {
279                 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
280                         __func__, PIXCIR_REG_INT_MODE, ret);
281                 return ret;
282         }
283
284         if (enable)
285                 ret |= PIXCIR_INT_ENABLE;
286         else
287                 ret &= ~PIXCIR_INT_ENABLE;
288
289         ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
290         if (ret < 0) {
291                 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
292                         __func__, PIXCIR_REG_INT_MODE, ret);
293                 return ret;
294         }
295
296         return 0;
297 }
298
299 static int pixcir_start(struct pixcir_i2c_ts_data *ts)
300 {
301         struct device *dev = &ts->client->dev;
302         int error;
303
304         /* LEVEL_TOUCH interrupt with active low polarity */
305         error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
306         if (error) {
307                 dev_err(dev, "Failed to set interrupt mode: %d\n", error);
308                 return error;
309         }
310
311         ts->running = true;
312         mb();   /* Update status before IRQ can fire */
313
314         /* enable interrupt generation */
315         error = pixcir_int_enable(ts, true);
316         if (error) {
317                 dev_err(dev, "Failed to enable interrupt generation: %d\n",
318                         error);
319                 return error;
320         }
321
322         return 0;
323 }
324
325 static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
326 {
327         int error;
328
329         /* Disable interrupt generation */
330         error = pixcir_int_enable(ts, false);
331         if (error) {
332                 dev_err(&ts->client->dev,
333                         "Failed to disable interrupt generation: %d\n",
334                         error);
335                 return error;
336         }
337
338         /* Exit ISR if running, no more report parsing */
339         ts->running = false;
340         mb();   /* update status before we synchronize irq */
341
342         /* Wait till running ISR is complete */
343         synchronize_irq(ts->client->irq);
344
345         return 0;
346 }
347
348 static int pixcir_input_open(struct input_dev *dev)
349 {
350         struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
351
352         return pixcir_start(ts);
353 }
354
355 static void pixcir_input_close(struct input_dev *dev)
356 {
357         struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
358
359         pixcir_stop(ts);
360 }
361
362 static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev)
363 {
364         struct i2c_client *client = to_i2c_client(dev);
365         struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
366         struct input_dev *input = ts->input;
367         int ret = 0;
368
369         mutex_lock(&input->mutex);
370
371         if (device_may_wakeup(&client->dev)) {
372                 if (!input->users) {
373                         ret = pixcir_start(ts);
374                         if (ret) {
375                                 dev_err(dev, "Failed to start\n");
376                                 goto unlock;
377                         }
378                 }
379
380                 enable_irq_wake(client->irq);
381         } else if (input->users) {
382                 ret = pixcir_stop(ts);
383         }
384
385 unlock:
386         mutex_unlock(&input->mutex);
387
388         return ret;
389 }
390
391 static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
392 {
393         struct i2c_client *client = to_i2c_client(dev);
394         struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
395         struct input_dev *input = ts->input;
396         int ret = 0;
397
398         mutex_lock(&input->mutex);
399
400         if (device_may_wakeup(&client->dev)) {
401                 disable_irq_wake(client->irq);
402
403                 if (!input->users) {
404                         ret = pixcir_stop(ts);
405                         if (ret) {
406                                 dev_err(dev, "Failed to stop\n");
407                                 goto unlock;
408                         }
409                 }
410         } else if (input->users) {
411                 ret = pixcir_start(ts);
412         }
413
414 unlock:
415         mutex_unlock(&input->mutex);
416
417         return ret;
418 }
419
420 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
421                          pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
422
423 #ifdef CONFIG_OF
424 static const struct of_device_id pixcir_of_match[];
425
426 static struct pixcir_ts_platform_data *pixcir_parse_dt(struct device *dev)
427 {
428         struct pixcir_ts_platform_data *pdata;
429         struct device_node *np = dev->of_node;
430         const struct of_device_id *match;
431
432         match = of_match_device(of_match_ptr(pixcir_of_match), dev);
433         if (!match)
434                 return ERR_PTR(-EINVAL);
435
436         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
437         if (!pdata)
438                 return ERR_PTR(-ENOMEM);
439
440         pdata->chip = *(const struct pixcir_i2c_chip_data *)match->data;
441
442         if (of_property_read_u32(np, "touchscreen-size-x", &pdata->x_max)) {
443                 dev_err(dev, "Failed to get touchscreen-size-x property\n");
444                 return ERR_PTR(-EINVAL);
445         }
446         pdata->x_max -= 1;
447
448         if (of_property_read_u32(np, "touchscreen-size-y", &pdata->y_max)) {
449                 dev_err(dev, "Failed to get touchscreen-size-y property\n");
450                 return ERR_PTR(-EINVAL);
451         }
452         pdata->y_max -= 1;
453
454         dev_dbg(dev, "%s: x %d, y %d\n", __func__,
455                 pdata->x_max + 1, pdata->y_max + 1);
456
457         return pdata;
458 }
459 #else
460 static struct pixcir_ts_platform_data *pixcir_parse_dt(struct device *dev)
461 {
462         return ERR_PTR(-EINVAL);
463 }
464 #endif
465
466 static int pixcir_i2c_ts_probe(struct i2c_client *client,
467                                          const struct i2c_device_id *id)
468 {
469         const struct pixcir_ts_platform_data *pdata =
470                         dev_get_platdata(&client->dev);
471         struct device *dev = &client->dev;
472         struct device_node *np = dev->of_node;
473         struct pixcir_i2c_ts_data *tsdata;
474         struct input_dev *input;
475         int error;
476
477         if (np && !pdata) {
478                 pdata = pixcir_parse_dt(dev);
479                 if (IS_ERR(pdata))
480                         return PTR_ERR(pdata);
481         }
482
483         if (!pdata) {
484                 dev_err(&client->dev, "platform data not defined\n");
485                 return -EINVAL;
486         }
487
488         if (!pdata->chip.max_fingers) {
489                 dev_err(dev, "Invalid max_fingers in pdata\n");
490                 return -EINVAL;
491         }
492
493         tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
494         if (!tsdata)
495                 return -ENOMEM;
496
497         input = devm_input_allocate_device(dev);
498         if (!input) {
499                 dev_err(dev, "Failed to allocate input device\n");
500                 return -ENOMEM;
501         }
502
503         tsdata->client = client;
504         tsdata->input = input;
505         tsdata->pdata = pdata;
506
507         input->name = client->name;
508         input->id.bustype = BUS_I2C;
509         input->open = pixcir_input_open;
510         input->close = pixcir_input_close;
511         input->dev.parent = &client->dev;
512
513         __set_bit(EV_KEY, input->evbit);
514         __set_bit(EV_ABS, input->evbit);
515         __set_bit(BTN_TOUCH, input->keybit);
516         input_set_abs_params(input, ABS_X, 0, pdata->x_max, 0, 0);
517         input_set_abs_params(input, ABS_Y, 0, pdata->y_max, 0, 0);
518         input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
519         input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
520
521         tsdata->max_fingers = tsdata->pdata->chip.max_fingers;
522         if (tsdata->max_fingers > PIXCIR_MAX_SLOTS) {
523                 tsdata->max_fingers = PIXCIR_MAX_SLOTS;
524                 dev_info(dev, "Limiting maximum fingers to %d\n",
525                          tsdata->max_fingers);
526         }
527
528         error = input_mt_init_slots(input, tsdata->max_fingers,
529                                     INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
530         if (error) {
531                 dev_err(dev, "Error initializing Multi-Touch slots\n");
532                 return error;
533         }
534
535         input_set_drvdata(input, tsdata);
536
537         tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN);
538         if (IS_ERR(tsdata->gpio_attb)) {
539                 error = PTR_ERR(tsdata->gpio_attb);
540                 dev_err(dev, "Failed to request ATTB gpio: %d\n", error);
541                 return error;
542         }
543
544         tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
545                                                      GPIOD_OUT_LOW);
546         if (IS_ERR(tsdata->gpio_reset)) {
547                 error = PTR_ERR(tsdata->gpio_reset);
548                 dev_err(dev, "Failed to request RESET gpio: %d\n", error);
549                 return error;
550         }
551
552         error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
553                                           IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
554                                           client->name, tsdata);
555         if (error) {
556                 dev_err(dev, "failed to request irq %d\n", client->irq);
557                 return error;
558         }
559
560         pixcir_reset(tsdata);
561
562         /* Always be in IDLE mode to save power, device supports auto wake */
563         error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
564         if (error) {
565                 dev_err(dev, "Failed to set IDLE mode\n");
566                 return error;
567         }
568
569         /* Stop device till opened */
570         error = pixcir_stop(tsdata);
571         if (error)
572                 return error;
573
574         error = input_register_device(input);
575         if (error)
576                 return error;
577
578         i2c_set_clientdata(client, tsdata);
579         device_init_wakeup(&client->dev, 1);
580
581         return 0;
582 }
583
584 static int pixcir_i2c_ts_remove(struct i2c_client *client)
585 {
586         device_init_wakeup(&client->dev, 0);
587
588         return 0;
589 }
590
591 static const struct i2c_device_id pixcir_i2c_ts_id[] = {
592         { "pixcir_ts", 0 },
593         { "pixcir_tangoc", 0 },
594         { }
595 };
596 MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
597
598 #ifdef CONFIG_OF
599 static const struct pixcir_i2c_chip_data pixcir_ts_data = {
600         .max_fingers = 2,
601         /* no hw id support */
602 };
603
604 static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
605         .max_fingers = 5,
606         .has_hw_ids = true,
607 };
608
609 static const struct of_device_id pixcir_of_match[] = {
610         { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data },
611         { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data },
612         { }
613 };
614 MODULE_DEVICE_TABLE(of, pixcir_of_match);
615 #endif
616
617 static struct i2c_driver pixcir_i2c_ts_driver = {
618         .driver = {
619                 .owner  = THIS_MODULE,
620                 .name   = "pixcir_ts",
621                 .pm     = &pixcir_dev_pm_ops,
622                 .of_match_table = of_match_ptr(pixcir_of_match),
623         },
624         .probe          = pixcir_i2c_ts_probe,
625         .remove         = pixcir_i2c_ts_remove,
626         .id_table       = pixcir_i2c_ts_id,
627 };
628
629 module_i2c_driver(pixcir_i2c_ts_driver);
630
631 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
632 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
633 MODULE_LICENSE("GPL");