2 * Driver for Pixcir I2C touchscreen controllers.
4 * Copyright (C) 2010-2011 Pixcir, Inc.
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.
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.
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/slab.h>
20 #include <linux/i2c.h>
21 #include <linux/input.h>
22 #include <linux/input/mt.h>
23 #include <linux/input/touchscreen.h>
24 #include <linux/gpio.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_data/pixcir_i2c_ts.h>
28 #include <asm/unaligned.h>
30 #define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
32 struct pixcir_i2c_ts_data
{
33 struct i2c_client
*client
;
34 struct input_dev
*input
;
35 struct gpio_desc
*gpio_attb
;
36 struct gpio_desc
*gpio_reset
;
37 struct gpio_desc
*gpio_enable
;
38 struct gpio_desc
*gpio_wake
;
39 const struct pixcir_i2c_chip_data
*chip
;
40 struct touchscreen_properties prop
;
41 int max_fingers
; /* Max fingers supported in this instance */
45 struct pixcir_report_data
{
47 struct input_mt_pos pos
[PIXCIR_MAX_SLOTS
];
48 int ids
[PIXCIR_MAX_SLOTS
];
51 static void pixcir_ts_parse(struct pixcir_i2c_ts_data
*tsdata
,
52 struct pixcir_report_data
*report
)
54 u8 rdbuf
[2 + PIXCIR_MAX_SLOTS
* 5];
60 const struct pixcir_i2c_chip_data
*chip
= tsdata
->chip
;
62 memset(report
, 0, sizeof(struct pixcir_report_data
));
64 i
= chip
->has_hw_ids
? 1 : 0;
65 readsize
= 2 + tsdata
->max_fingers
* (4 + i
);
66 if (readsize
> sizeof(rdbuf
))
67 readsize
= sizeof(rdbuf
);
69 ret
= i2c_master_send(tsdata
->client
, wrbuf
, sizeof(wrbuf
));
70 if (ret
!= sizeof(wrbuf
)) {
71 dev_err(&tsdata
->client
->dev
,
72 "%s: i2c_master_send failed(), ret=%d\n",
77 ret
= i2c_master_recv(tsdata
->client
, rdbuf
, readsize
);
78 if (ret
!= readsize
) {
79 dev_err(&tsdata
->client
->dev
,
80 "%s: i2c_master_recv failed(), ret=%d\n",
85 touch
= rdbuf
[0] & 0x7;
86 if (touch
> tsdata
->max_fingers
)
87 touch
= tsdata
->max_fingers
;
89 report
->num_touches
= touch
;
92 for (i
= 0; i
< touch
; i
++) {
93 touchscreen_set_mt_pos(&report
->pos
[i
], &tsdata
->prop
,
94 get_unaligned_le16(bufptr
),
95 get_unaligned_le16(bufptr
+ 2));
96 if (chip
->has_hw_ids
) {
97 report
->ids
[i
] = bufptr
[4];
105 static void pixcir_ts_report(struct pixcir_i2c_ts_data
*ts
,
106 struct pixcir_report_data
*report
)
108 int slots
[PIXCIR_MAX_SLOTS
];
110 struct device
*dev
= &ts
->client
->dev
;
111 const struct pixcir_i2c_chip_data
*chip
= ts
->chip
;
113 n
= report
->num_touches
;
114 if (n
> PIXCIR_MAX_SLOTS
)
115 n
= PIXCIR_MAX_SLOTS
;
117 if (!ts
->chip
->has_hw_ids
)
118 input_mt_assign_slots(ts
->input
, slots
, report
->pos
, n
, 0);
120 for (i
= 0; i
< n
; i
++) {
121 if (chip
->has_hw_ids
) {
122 slot
= input_mt_get_slot_by_key(ts
->input
,
125 dev_dbg(dev
, "no free slot for id 0x%x\n",
133 input_mt_slot(ts
->input
, slot
);
134 input_mt_report_slot_state(ts
->input
, MT_TOOL_FINGER
, true);
136 input_report_abs(ts
->input
, ABS_MT_POSITION_X
,
138 input_report_abs(ts
->input
, ABS_MT_POSITION_Y
,
141 dev_dbg(dev
, "%d: slot %d, x %d, y %d\n",
142 i
, slot
, report
->pos
[i
].x
, report
->pos
[i
].y
);
145 input_mt_sync_frame(ts
->input
);
146 input_sync(ts
->input
);
149 static irqreturn_t
pixcir_ts_isr(int irq
, void *dev_id
)
151 struct pixcir_i2c_ts_data
*tsdata
= dev_id
;
152 struct pixcir_report_data report
;
154 while (tsdata
->running
) {
156 pixcir_ts_parse(tsdata
, &report
);
159 pixcir_ts_report(tsdata
, &report
);
161 if (gpiod_get_value_cansleep(tsdata
->gpio_attb
)) {
162 if (report
.num_touches
) {
164 * Last report with no finger up?
167 input_mt_sync_frame(tsdata
->input
);
168 input_sync(tsdata
->input
);
179 static void pixcir_reset(struct pixcir_i2c_ts_data
*tsdata
)
181 if (!IS_ERR_OR_NULL(tsdata
->gpio_reset
)) {
182 gpiod_set_value_cansleep(tsdata
->gpio_reset
, 1);
183 ndelay(100); /* datasheet section 1.2.3 says 80ns min. */
184 gpiod_set_value_cansleep(tsdata
->gpio_reset
, 0);
185 /* wait for controller ready. 100ms guess. */
190 static int pixcir_set_power_mode(struct pixcir_i2c_ts_data
*ts
,
191 enum pixcir_power_mode mode
)
193 struct device
*dev
= &ts
->client
->dev
;
196 if (mode
== PIXCIR_POWER_ACTIVE
|| mode
== PIXCIR_POWER_IDLE
) {
198 gpiod_set_value_cansleep(ts
->gpio_wake
, 1);
201 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
);
203 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
204 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
208 ret
&= ~PIXCIR_POWER_MODE_MASK
;
211 /* Always AUTO_IDLE */
212 ret
|= PIXCIR_POWER_ALLOW_IDLE
;
214 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
, ret
);
216 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
217 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
221 if (mode
== PIXCIR_POWER_HALT
) {
223 gpiod_set_value_cansleep(ts
->gpio_wake
, 0);
230 * Set the interrupt mode for the device i.e. ATTB line behaviour
232 * @polarity : 1 for active high, 0 for active low.
234 static int pixcir_set_int_mode(struct pixcir_i2c_ts_data
*ts
,
235 enum pixcir_int_mode mode
, bool polarity
)
237 struct device
*dev
= &ts
->client
->dev
;
240 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
242 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
243 __func__
, PIXCIR_REG_INT_MODE
, ret
);
247 ret
&= ~PIXCIR_INT_MODE_MASK
;
251 ret
|= PIXCIR_INT_POL_HIGH
;
253 ret
&= ~PIXCIR_INT_POL_HIGH
;
255 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
257 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
258 __func__
, PIXCIR_REG_INT_MODE
, ret
);
266 * Enable/disable interrupt generation
268 static int pixcir_int_enable(struct pixcir_i2c_ts_data
*ts
, bool enable
)
270 struct device
*dev
= &ts
->client
->dev
;
273 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
275 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
276 __func__
, PIXCIR_REG_INT_MODE
, ret
);
281 ret
|= PIXCIR_INT_ENABLE
;
283 ret
&= ~PIXCIR_INT_ENABLE
;
285 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
287 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
288 __func__
, PIXCIR_REG_INT_MODE
, ret
);
295 static int pixcir_start(struct pixcir_i2c_ts_data
*ts
)
297 struct device
*dev
= &ts
->client
->dev
;
300 if (ts
->gpio_enable
) {
301 gpiod_set_value_cansleep(ts
->gpio_enable
, 1);
305 /* LEVEL_TOUCH interrupt with active low polarity */
306 error
= pixcir_set_int_mode(ts
, PIXCIR_INT_LEVEL_TOUCH
, 0);
308 dev_err(dev
, "Failed to set interrupt mode: %d\n", error
);
313 mb(); /* Update status before IRQ can fire */
315 /* enable interrupt generation */
316 error
= pixcir_int_enable(ts
, true);
318 dev_err(dev
, "Failed to enable interrupt generation: %d\n",
326 static int pixcir_stop(struct pixcir_i2c_ts_data
*ts
)
330 /* Disable interrupt generation */
331 error
= pixcir_int_enable(ts
, false);
333 dev_err(&ts
->client
->dev
,
334 "Failed to disable interrupt generation: %d\n",
339 /* Exit ISR if running, no more report parsing */
341 mb(); /* update status before we synchronize irq */
343 /* Wait till running ISR is complete */
344 synchronize_irq(ts
->client
->irq
);
347 gpiod_set_value_cansleep(ts
->gpio_enable
, 0);
352 static int pixcir_input_open(struct input_dev
*dev
)
354 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
356 return pixcir_start(ts
);
359 static void pixcir_input_close(struct input_dev
*dev
)
361 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
366 static int __maybe_unused
pixcir_i2c_ts_suspend(struct device
*dev
)
368 struct i2c_client
*client
= to_i2c_client(dev
);
369 struct pixcir_i2c_ts_data
*ts
= i2c_get_clientdata(client
);
370 struct input_dev
*input
= ts
->input
;
373 mutex_lock(&input
->mutex
);
375 if (device_may_wakeup(&client
->dev
)) {
377 ret
= pixcir_start(ts
);
379 dev_err(dev
, "Failed to start\n");
383 } else if (input
->users
) {
384 ret
= pixcir_stop(ts
);
388 mutex_unlock(&input
->mutex
);
393 static int __maybe_unused
pixcir_i2c_ts_resume(struct device
*dev
)
395 struct i2c_client
*client
= to_i2c_client(dev
);
396 struct pixcir_i2c_ts_data
*ts
= i2c_get_clientdata(client
);
397 struct input_dev
*input
= ts
->input
;
400 mutex_lock(&input
->mutex
);
402 if (device_may_wakeup(&client
->dev
)) {
404 ret
= pixcir_stop(ts
);
406 dev_err(dev
, "Failed to stop\n");
410 } else if (input
->users
) {
411 ret
= pixcir_start(ts
);
415 mutex_unlock(&input
->mutex
);
420 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops
,
421 pixcir_i2c_ts_suspend
, pixcir_i2c_ts_resume
);
424 static const struct of_device_id pixcir_of_match
[];
426 static int pixcir_parse_dt(struct device
*dev
,
427 struct pixcir_i2c_ts_data
*tsdata
)
429 tsdata
->chip
= of_device_get_match_data(dev
);
436 static int pixcir_parse_dt(struct device
*dev
,
437 struct pixcir_i2c_ts_data
*tsdata
)
443 static int pixcir_i2c_ts_probe(struct i2c_client
*client
,
444 const struct i2c_device_id
*id
)
446 const struct pixcir_ts_platform_data
*pdata
=
447 dev_get_platdata(&client
->dev
);
448 struct device
*dev
= &client
->dev
;
449 struct pixcir_i2c_ts_data
*tsdata
;
450 struct input_dev
*input
;
453 tsdata
= devm_kzalloc(dev
, sizeof(*tsdata
), GFP_KERNEL
);
458 tsdata
->chip
= &pdata
->chip
;
459 } else if (dev
->of_node
) {
460 error
= pixcir_parse_dt(dev
, tsdata
);
464 dev_err(dev
, "platform data not defined\n");
468 if (!tsdata
->chip
->max_fingers
) {
469 dev_err(dev
, "Invalid max_fingers in chip data\n");
473 input
= devm_input_allocate_device(dev
);
475 dev_err(dev
, "Failed to allocate input device\n");
479 tsdata
->client
= client
;
480 tsdata
->input
= input
;
482 input
->name
= client
->name
;
483 input
->id
.bustype
= BUS_I2C
;
484 input
->open
= pixcir_input_open
;
485 input
->close
= pixcir_input_close
;
486 input
->dev
.parent
= dev
;
489 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, pdata
->x_max
, 0, 0);
490 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, pdata
->y_max
, 0, 0);
492 input_set_capability(input
, EV_ABS
, ABS_MT_POSITION_X
);
493 input_set_capability(input
, EV_ABS
, ABS_MT_POSITION_Y
);
494 touchscreen_parse_properties(input
, true, &tsdata
->prop
);
495 if (!input_abs_get_max(input
, ABS_MT_POSITION_X
) ||
496 !input_abs_get_max(input
, ABS_MT_POSITION_Y
)) {
497 dev_err(dev
, "Touchscreen size is not specified\n");
502 tsdata
->max_fingers
= tsdata
->chip
->max_fingers
;
503 if (tsdata
->max_fingers
> PIXCIR_MAX_SLOTS
) {
504 tsdata
->max_fingers
= PIXCIR_MAX_SLOTS
;
505 dev_info(dev
, "Limiting maximum fingers to %d\n",
506 tsdata
->max_fingers
);
509 error
= input_mt_init_slots(input
, tsdata
->max_fingers
,
510 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
512 dev_err(dev
, "Error initializing Multi-Touch slots\n");
516 input_set_drvdata(input
, tsdata
);
518 tsdata
->gpio_attb
= devm_gpiod_get(dev
, "attb", GPIOD_IN
);
519 if (IS_ERR(tsdata
->gpio_attb
)) {
520 error
= PTR_ERR(tsdata
->gpio_attb
);
521 dev_err(dev
, "Failed to request ATTB gpio: %d\n", error
);
525 tsdata
->gpio_reset
= devm_gpiod_get_optional(dev
, "reset",
527 if (IS_ERR(tsdata
->gpio_reset
)) {
528 error
= PTR_ERR(tsdata
->gpio_reset
);
529 dev_err(dev
, "Failed to request RESET gpio: %d\n", error
);
533 tsdata
->gpio_wake
= devm_gpiod_get_optional(dev
, "wake",
535 if (IS_ERR(tsdata
->gpio_wake
)) {
536 error
= PTR_ERR(tsdata
->gpio_wake
);
537 if (error
!= -EPROBE_DEFER
)
538 dev_err(dev
, "Failed to get wake gpio: %d\n", error
);
542 tsdata
->gpio_enable
= devm_gpiod_get_optional(dev
, "enable",
544 if (IS_ERR(tsdata
->gpio_enable
)) {
545 error
= PTR_ERR(tsdata
->gpio_enable
);
546 if (error
!= -EPROBE_DEFER
)
547 dev_err(dev
, "Failed to get enable gpio: %d\n", error
);
551 if (tsdata
->gpio_enable
)
554 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, pixcir_ts_isr
,
555 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
556 client
->name
, tsdata
);
558 dev_err(dev
, "failed to request irq %d\n", client
->irq
);
562 pixcir_reset(tsdata
);
564 /* Always be in IDLE mode to save power, device supports auto wake */
565 error
= pixcir_set_power_mode(tsdata
, PIXCIR_POWER_IDLE
);
567 dev_err(dev
, "Failed to set IDLE mode\n");
571 /* Stop device till opened */
572 error
= pixcir_stop(tsdata
);
576 error
= input_register_device(input
);
580 i2c_set_clientdata(client
, tsdata
);
585 static const struct i2c_device_id pixcir_i2c_ts_id
[] = {
587 { "pixcir_tangoc", 0 },
590 MODULE_DEVICE_TABLE(i2c
, pixcir_i2c_ts_id
);
593 static const struct pixcir_i2c_chip_data pixcir_ts_data
= {
595 /* no hw id support */
598 static const struct pixcir_i2c_chip_data pixcir_tangoc_data
= {
603 static const struct of_device_id pixcir_of_match
[] = {
604 { .compatible
= "pixcir,pixcir_ts", .data
= &pixcir_ts_data
},
605 { .compatible
= "pixcir,pixcir_tangoc", .data
= &pixcir_tangoc_data
},
608 MODULE_DEVICE_TABLE(of
, pixcir_of_match
);
611 static struct i2c_driver pixcir_i2c_ts_driver
= {
614 .pm
= &pixcir_dev_pm_ops
,
615 .of_match_table
= of_match_ptr(pixcir_of_match
),
617 .probe
= pixcir_i2c_ts_probe
,
618 .id_table
= pixcir_i2c_ts_id
,
621 module_i2c_driver(pixcir_i2c_ts_driver
);
623 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
624 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
625 MODULE_LICENSE("GPL");