Input: pixcir_i2c_ts - simplify input device initialization
[deliverable/linux.git] / drivers / input / touchscreen / pixcir_i2c_ts.c
CommitLineData
36a281e2
JB
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>
62e65b7e 26#include <linux/input/mt.h>
0dfc8d41 27#include <linux/gpio.h>
cb4a5f06 28#include <linux/gpio/consumer.h>
a4054596 29#include <linux/of.h>
a4054596 30#include <linux/of_device.h>
28a74c05 31#include <linux/platform_data/pixcir_i2c_ts.h>
36a281e2 32
36874c7e 33#define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
62e65b7e 34
36a281e2
JB
35struct pixcir_i2c_ts_data {
36 struct i2c_client *client;
37 struct input_dev *input;
cb4a5f06 38 struct gpio_desc *gpio_attb;
40929167 39 struct gpio_desc *gpio_reset;
36874c7e 40 const struct pixcir_ts_platform_data *pdata;
3b36fbb0 41 bool running;
36874c7e 42 int max_fingers; /* Max fingers supported in this instance */
36a281e2
JB
43};
44
62e65b7e
RQ
45struct pixcir_touch {
46 int x;
47 int y;
36874c7e 48 int id;
62e65b7e
RQ
49};
50
51struct pixcir_report_data {
52 int num_touches;
53 struct pixcir_touch touches[PIXCIR_MAX_SLOTS];
54};
55
56static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
57 struct pixcir_report_data *report)
36a281e2 58{
36874c7e
RQ
59 u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
60 u8 wrbuf[1] = { 0 };
62e65b7e 61 u8 *bufptr;
36a281e2 62 u8 touch;
62e65b7e 63 int ret, i;
36874c7e
RQ
64 int readsize;
65 const struct pixcir_i2c_chip_data *chip = &tsdata->pdata->chip;
62e65b7e
RQ
66
67 memset(report, 0, sizeof(struct pixcir_report_data));
36a281e2 68
36874c7e
RQ
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
36a281e2
JB
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
36874c7e 82 ret = i2c_master_recv(tsdata->client, rdbuf, readsize);
36a281e2
JB
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
62e65b7e 90 touch = rdbuf[0] & 0x7;
36874c7e
RQ
91 if (touch > tsdata->max_fingers)
92 touch = tsdata->max_fingers;
62e65b7e
RQ
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
36874c7e
RQ
101 if (chip->has_hw_ids) {
102 report->touches[i].id = bufptr[4];
103 bufptr = bufptr + 5;
104 } else {
105 bufptr = bufptr + 4;
106 }
36a281e2 107 }
62e65b7e
RQ
108}
109
110static 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;
36874c7e 118 const struct pixcir_i2c_chip_data *chip = &ts->pdata->chip;
62e65b7e
RQ
119
120 n = report->num_touches;
121 if (n > PIXCIR_MAX_SLOTS)
122 n = PIXCIR_MAX_SLOTS;
36a281e2 123
36874c7e
RQ
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 }
62e65b7e 130
448c7f38 131 input_mt_assign_slots(ts->input, slots, pos, n, 0);
36874c7e 132 }
62e65b7e
RQ
133
134 for (i = 0; i < n; i++) {
135 touch = &report->touches[i];
36874c7e
RQ
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 }
62e65b7e
RQ
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);
36a281e2
JB
161}
162
163static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
164{
165 struct pixcir_i2c_ts_data *tsdata = dev_id;
62e65b7e 166 struct pixcir_report_data report;
36a281e2 167
3b36fbb0 168 while (tsdata->running) {
62e65b7e
RQ
169 /* parse packet */
170 pixcir_ts_parse(tsdata, &report);
171
172 /* report it */
173 pixcir_ts_report(tsdata, &report);
174
127520ca 175 if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
62e65b7e
RQ
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 }
36a281e2 184 break;
62e65b7e 185 }
36a281e2
JB
186
187 msleep(20);
188 }
189
190 return IRQ_HANDLED;
191}
192
40929167
RQ
193static 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
3b36fbb0
RQ
204static 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 */
238static 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 */
272static 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
299static 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
325static 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
348static 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
355static 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
02b6a58b 362static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev)
36a281e2
JB
363{
364 struct i2c_client *client = to_i2c_client(dev);
7cdcb8d1
RQ
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 }
36a281e2 379
36a281e2 380 enable_irq_wake(client->irq);
7cdcb8d1
RQ
381 } else if (input->users) {
382 ret = pixcir_stop(ts);
383 }
36a281e2 384
7cdcb8d1
RQ
385unlock:
386 mutex_unlock(&input->mutex);
387
388 return ret;
36a281e2
JB
389}
390
02b6a58b 391static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
36a281e2
JB
392{
393 struct i2c_client *client = to_i2c_client(dev);
7cdcb8d1
RQ
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);
36a281e2 399
7cdcb8d1 400 if (device_may_wakeup(&client->dev)) {
36a281e2
JB
401 disable_irq_wake(client->irq);
402
7cdcb8d1
RQ
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
414unlock:
415 mutex_unlock(&input->mutex);
416
417 return ret;
36a281e2 418}
36a281e2
JB
419
420static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
421 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
422
a4054596
RQ
423#ifdef CONFIG_OF
424static const struct of_device_id pixcir_of_match[];
425
426static 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
a4054596
RQ
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
cb4a5f06
DT
454 dev_dbg(dev, "%s: x %d, y %d\n", __func__,
455 pdata->x_max + 1, pdata->y_max + 1);
a4054596
RQ
456
457 return pdata;
458}
459#else
460static struct pixcir_ts_platform_data *pixcir_parse_dt(struct device *dev)
461{
462 return ERR_PTR(-EINVAL);
463}
464#endif
465
5298cc4c 466static int pixcir_i2c_ts_probe(struct i2c_client *client,
36a281e2
JB
467 const struct i2c_device_id *id)
468{
c838cb3d
JH
469 const struct pixcir_ts_platform_data *pdata =
470 dev_get_platdata(&client->dev);
e9d4718d 471 struct device *dev = &client->dev;
a4054596 472 struct device_node *np = dev->of_node;
36a281e2
JB
473 struct pixcir_i2c_ts_data *tsdata;
474 struct input_dev *input;
475 int error;
476
a4054596
RQ
477 if (np && !pdata) {
478 pdata = pixcir_parse_dt(dev);
479 if (IS_ERR(pdata))
480 return PTR_ERR(pdata);
481 }
482
36a281e2
JB
483 if (!pdata) {
484 dev_err(&client->dev, "platform data not defined\n");
485 return -EINVAL;
486 }
487
36874c7e
RQ
488 if (!pdata->chip.max_fingers) {
489 dev_err(dev, "Invalid max_fingers in pdata\n");
490 return -EINVAL;
491 }
492
e9d4718d
RQ
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;
36a281e2
JB
501 }
502
503 tsdata->client = client;
504 tsdata->input = input;
36874c7e 505 tsdata->pdata = pdata;
36a281e2
JB
506
507 input->name = client->name;
508 input->id.bustype = BUS_I2C;
3b36fbb0
RQ
509 input->open = pixcir_input_open;
510 input->close = pixcir_input_close;
36a281e2
JB
511 input->dev.parent = &client->dev;
512
36a281e2
JB
513 input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
514 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
515
36874c7e
RQ
516 tsdata->max_fingers = tsdata->pdata->chip.max_fingers;
517 if (tsdata->max_fingers > PIXCIR_MAX_SLOTS) {
518 tsdata->max_fingers = PIXCIR_MAX_SLOTS;
519 dev_info(dev, "Limiting maximum fingers to %d\n",
520 tsdata->max_fingers);
521 }
522
523 error = input_mt_init_slots(input, tsdata->max_fingers,
62e65b7e
RQ
524 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
525 if (error) {
526 dev_err(dev, "Error initializing Multi-Touch slots\n");
527 return error;
528 }
529
36a281e2
JB
530 input_set_drvdata(input, tsdata);
531
cb4a5f06
DT
532 tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN);
533 if (IS_ERR(tsdata->gpio_attb)) {
534 error = PTR_ERR(tsdata->gpio_attb);
535 dev_err(dev, "Failed to request ATTB gpio: %d\n", error);
0dfc8d41
RQ
536 return error;
537 }
538
40929167
RQ
539 tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
540 GPIOD_OUT_LOW);
541 if (IS_ERR(tsdata->gpio_reset)) {
542 error = PTR_ERR(tsdata->gpio_reset);
543 dev_err(dev, "Failed to request RESET gpio: %d\n", error);
544 return error;
545 }
546
e9d4718d
RQ
547 error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
548 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
549 client->name, tsdata);
36a281e2 550 if (error) {
e9d4718d
RQ
551 dev_err(dev, "failed to request irq %d\n", client->irq);
552 return error;
36a281e2
JB
553 }
554
40929167
RQ
555 pixcir_reset(tsdata);
556
3b36fbb0
RQ
557 /* Always be in IDLE mode to save power, device supports auto wake */
558 error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
559 if (error) {
560 dev_err(dev, "Failed to set IDLE mode\n");
561 return error;
562 }
563
564 /* Stop device till opened */
565 error = pixcir_stop(tsdata);
566 if (error)
567 return error;
568
36a281e2
JB
569 error = input_register_device(input);
570 if (error)
e9d4718d 571 return error;
36a281e2 572
7cdcb8d1 573 i2c_set_clientdata(client, tsdata);
36a281e2
JB
574 device_init_wakeup(&client->dev, 1);
575
576 return 0;
36a281e2
JB
577}
578
e2619cf7 579static int pixcir_i2c_ts_remove(struct i2c_client *client)
36a281e2 580{
36a281e2
JB
581 device_init_wakeup(&client->dev, 0);
582
36a281e2
JB
583 return 0;
584}
585
586static const struct i2c_device_id pixcir_i2c_ts_id[] = {
587 { "pixcir_ts", 0 },
a4054596 588 { "pixcir_tangoc", 0 },
36a281e2
JB
589 { }
590};
591MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
592
a4054596
RQ
593#ifdef CONFIG_OF
594static const struct pixcir_i2c_chip_data pixcir_ts_data = {
595 .max_fingers = 2,
596 /* no hw id support */
597};
598
599static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
600 .max_fingers = 5,
601 .has_hw_ids = true,
602};
603
604static const struct of_device_id pixcir_of_match[] = {
605 { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data },
606 { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data },
607 { }
608};
609MODULE_DEVICE_TABLE(of, pixcir_of_match);
610#endif
611
36a281e2
JB
612static struct i2c_driver pixcir_i2c_ts_driver = {
613 .driver = {
614 .owner = THIS_MODULE,
615 .name = "pixcir_ts",
616 .pm = &pixcir_dev_pm_ops,
a4054596 617 .of_match_table = of_match_ptr(pixcir_of_match),
36a281e2
JB
618 },
619 .probe = pixcir_i2c_ts_probe,
1cb0aa88 620 .remove = pixcir_i2c_ts_remove,
36a281e2
JB
621 .id_table = pixcir_i2c_ts_id,
622};
623
4a533835 624module_i2c_driver(pixcir_i2c_ts_driver);
36a281e2
JB
625
626MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
627MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
628MODULE_LICENSE("GPL");
This page took 0.086172 seconds and 5 git commands to generate.