1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Pixcir I2C touchscreen controllers.
5 * Copyright (C) 2010-2011 Pixcir, Inc.
8 #include <linux/delay.h>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/input.h>
14 #include <linux/input/mt.h>
15 #include <linux/input/touchscreen.h>
16 #include <linux/gpio.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_data/pixcir_i2c_ts.h>
20 #include <asm/unaligned.h>
22 #define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
24 struct pixcir_i2c_ts_data
{
25 struct i2c_client
*client
;
26 struct input_dev
*input
;
27 struct gpio_desc
*gpio_attb
;
28 struct gpio_desc
*gpio_reset
;
29 struct gpio_desc
*gpio_enable
;
30 struct gpio_desc
*gpio_wake
;
31 const struct pixcir_i2c_chip_data
*chip
;
32 struct touchscreen_properties prop
;
33 int max_fingers
; /* Max fingers supported in this instance */
37 struct pixcir_report_data
{
39 struct input_mt_pos pos
[PIXCIR_MAX_SLOTS
];
40 int ids
[PIXCIR_MAX_SLOTS
];
43 static void pixcir_ts_parse(struct pixcir_i2c_ts_data
*tsdata
,
44 struct pixcir_report_data
*report
)
46 u8 rdbuf
[2 + PIXCIR_MAX_SLOTS
* 5];
52 const struct pixcir_i2c_chip_data
*chip
= tsdata
->chip
;
54 memset(report
, 0, sizeof(struct pixcir_report_data
));
56 i
= chip
->has_hw_ids
? 1 : 0;
57 readsize
= 2 + tsdata
->max_fingers
* (4 + i
);
58 if (readsize
> sizeof(rdbuf
))
59 readsize
= sizeof(rdbuf
);
61 ret
= i2c_master_send(tsdata
->client
, wrbuf
, sizeof(wrbuf
));
62 if (ret
!= sizeof(wrbuf
)) {
63 dev_err(&tsdata
->client
->dev
,
64 "%s: i2c_master_send failed(), ret=%d\n",
69 ret
= i2c_master_recv(tsdata
->client
, rdbuf
, readsize
);
70 if (ret
!= readsize
) {
71 dev_err(&tsdata
->client
->dev
,
72 "%s: i2c_master_recv failed(), ret=%d\n",
77 touch
= rdbuf
[0] & 0x7;
78 if (touch
> tsdata
->max_fingers
)
79 touch
= tsdata
->max_fingers
;
81 report
->num_touches
= touch
;
84 for (i
= 0; i
< touch
; i
++) {
85 touchscreen_set_mt_pos(&report
->pos
[i
], &tsdata
->prop
,
86 get_unaligned_le16(bufptr
),
87 get_unaligned_le16(bufptr
+ 2));
88 if (chip
->has_hw_ids
) {
89 report
->ids
[i
] = bufptr
[4];
97 static void pixcir_ts_report(struct pixcir_i2c_ts_data
*ts
,
98 struct pixcir_report_data
*report
)
100 int slots
[PIXCIR_MAX_SLOTS
];
102 struct device
*dev
= &ts
->client
->dev
;
103 const struct pixcir_i2c_chip_data
*chip
= ts
->chip
;
105 n
= report
->num_touches
;
106 if (n
> PIXCIR_MAX_SLOTS
)
107 n
= PIXCIR_MAX_SLOTS
;
109 if (!ts
->chip
->has_hw_ids
)
110 input_mt_assign_slots(ts
->input
, slots
, report
->pos
, n
, 0);
112 for (i
= 0; i
< n
; i
++) {
113 if (chip
->has_hw_ids
) {
114 slot
= input_mt_get_slot_by_key(ts
->input
,
117 dev_dbg(dev
, "no free slot for id 0x%x\n",
125 input_mt_slot(ts
->input
, slot
);
126 input_mt_report_slot_state(ts
->input
, MT_TOOL_FINGER
, true);
128 input_report_abs(ts
->input
, ABS_MT_POSITION_X
,
130 input_report_abs(ts
->input
, ABS_MT_POSITION_Y
,
133 dev_dbg(dev
, "%d: slot %d, x %d, y %d\n",
134 i
, slot
, report
->pos
[i
].x
, report
->pos
[i
].y
);
137 input_mt_sync_frame(ts
->input
);
138 input_sync(ts
->input
);
141 static irqreturn_t
pixcir_ts_isr(int irq
, void *dev_id
)
143 struct pixcir_i2c_ts_data
*tsdata
= dev_id
;
144 struct pixcir_report_data report
;
146 while (tsdata
->running
) {
148 pixcir_ts_parse(tsdata
, &report
);
151 pixcir_ts_report(tsdata
, &report
);
153 if (gpiod_get_value_cansleep(tsdata
->gpio_attb
)) {
154 if (report
.num_touches
) {
156 * Last report with no finger up?
159 input_mt_sync_frame(tsdata
->input
);
160 input_sync(tsdata
->input
);
171 static void pixcir_reset(struct pixcir_i2c_ts_data
*tsdata
)
173 if (!IS_ERR_OR_NULL(tsdata
->gpio_reset
)) {
174 gpiod_set_value_cansleep(tsdata
->gpio_reset
, 1);
175 ndelay(100); /* datasheet section 1.2.3 says 80ns min. */
176 gpiod_set_value_cansleep(tsdata
->gpio_reset
, 0);
177 /* wait for controller ready. 100ms guess. */
182 static int pixcir_set_power_mode(struct pixcir_i2c_ts_data
*ts
,
183 enum pixcir_power_mode mode
)
185 struct device
*dev
= &ts
->client
->dev
;
188 if (mode
== PIXCIR_POWER_ACTIVE
|| mode
== PIXCIR_POWER_IDLE
) {
190 gpiod_set_value_cansleep(ts
->gpio_wake
, 1);
193 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
);
195 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
196 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
200 ret
&= ~PIXCIR_POWER_MODE_MASK
;
203 /* Always AUTO_IDLE */
204 ret
|= PIXCIR_POWER_ALLOW_IDLE
;
206 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
, ret
);
208 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
209 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
213 if (mode
== PIXCIR_POWER_HALT
) {
215 gpiod_set_value_cansleep(ts
->gpio_wake
, 0);
222 * Set the interrupt mode for the device i.e. ATTB line behaviour
224 * @polarity : 1 for active high, 0 for active low.
226 static int pixcir_set_int_mode(struct pixcir_i2c_ts_data
*ts
,
227 enum pixcir_int_mode mode
, bool polarity
)
229 struct device
*dev
= &ts
->client
->dev
;
232 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
234 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
235 __func__
, PIXCIR_REG_INT_MODE
, ret
);
239 ret
&= ~PIXCIR_INT_MODE_MASK
;
243 ret
|= PIXCIR_INT_POL_HIGH
;
245 ret
&= ~PIXCIR_INT_POL_HIGH
;
247 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
249 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
250 __func__
, PIXCIR_REG_INT_MODE
, ret
);
258 * Enable/disable interrupt generation
260 static int pixcir_int_enable(struct pixcir_i2c_ts_data
*ts
, bool enable
)
262 struct device
*dev
= &ts
->client
->dev
;
265 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
267 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
268 __func__
, PIXCIR_REG_INT_MODE
, ret
);
273 ret
|= PIXCIR_INT_ENABLE
;
275 ret
&= ~PIXCIR_INT_ENABLE
;
277 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
279 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
280 __func__
, PIXCIR_REG_INT_MODE
, ret
);
287 static int pixcir_start(struct pixcir_i2c_ts_data
*ts
)
289 struct device
*dev
= &ts
->client
->dev
;
292 if (ts
->gpio_enable
) {
293 gpiod_set_value_cansleep(ts
->gpio_enable
, 1);
297 /* LEVEL_TOUCH interrupt with active low polarity */
298 error
= pixcir_set_int_mode(ts
, PIXCIR_INT_LEVEL_TOUCH
, 0);
300 dev_err(dev
, "Failed to set interrupt mode: %d\n", error
);
305 mb(); /* Update status before IRQ can fire */
307 /* enable interrupt generation */
308 error
= pixcir_int_enable(ts
, true);
310 dev_err(dev
, "Failed to enable interrupt generation: %d\n",
318 static int pixcir_stop(struct pixcir_i2c_ts_data
*ts
)
322 /* Disable interrupt generation */
323 error
= pixcir_int_enable(ts
, false);
325 dev_err(&ts
->client
->dev
,
326 "Failed to disable interrupt generation: %d\n",
331 /* Exit ISR if running, no more report parsing */
333 mb(); /* update status before we synchronize irq */
335 /* Wait till running ISR is complete */
336 synchronize_irq(ts
->client
->irq
);
339 gpiod_set_value_cansleep(ts
->gpio_enable
, 0);
344 static int pixcir_input_open(struct input_dev
*dev
)
346 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
348 return pixcir_start(ts
);
351 static void pixcir_input_close(struct input_dev
*dev
)
353 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
358 static int __maybe_unused
pixcir_i2c_ts_suspend(struct device
*dev
)
360 struct i2c_client
*client
= to_i2c_client(dev
);
361 struct pixcir_i2c_ts_data
*ts
= i2c_get_clientdata(client
);
362 struct input_dev
*input
= ts
->input
;
365 mutex_lock(&input
->mutex
);
367 if (device_may_wakeup(&client
->dev
)) {
369 ret
= pixcir_start(ts
);
371 dev_err(dev
, "Failed to start\n");
375 } else if (input
->users
) {
376 ret
= pixcir_stop(ts
);
380 mutex_unlock(&input
->mutex
);
385 static int __maybe_unused
pixcir_i2c_ts_resume(struct device
*dev
)
387 struct i2c_client
*client
= to_i2c_client(dev
);
388 struct pixcir_i2c_ts_data
*ts
= i2c_get_clientdata(client
);
389 struct input_dev
*input
= ts
->input
;
392 mutex_lock(&input
->mutex
);
394 if (device_may_wakeup(&client
->dev
)) {
396 ret
= pixcir_stop(ts
);
398 dev_err(dev
, "Failed to stop\n");
402 } else if (input
->users
) {
403 ret
= pixcir_start(ts
);
407 mutex_unlock(&input
->mutex
);
412 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops
,
413 pixcir_i2c_ts_suspend
, pixcir_i2c_ts_resume
);
416 static const struct of_device_id pixcir_of_match
[];
418 static int pixcir_parse_dt(struct device
*dev
,
419 struct pixcir_i2c_ts_data
*tsdata
)
421 tsdata
->chip
= of_device_get_match_data(dev
);
428 static int pixcir_parse_dt(struct device
*dev
,
429 struct pixcir_i2c_ts_data
*tsdata
)
435 static int pixcir_i2c_ts_probe(struct i2c_client
*client
,
436 const struct i2c_device_id
*id
)
438 const struct pixcir_ts_platform_data
*pdata
=
439 dev_get_platdata(&client
->dev
);
440 struct device
*dev
= &client
->dev
;
441 struct pixcir_i2c_ts_data
*tsdata
;
442 struct input_dev
*input
;
445 tsdata
= devm_kzalloc(dev
, sizeof(*tsdata
), GFP_KERNEL
);
450 tsdata
->chip
= &pdata
->chip
;
451 } else if (dev
->of_node
) {
452 error
= pixcir_parse_dt(dev
, tsdata
);
456 dev_err(dev
, "platform data not defined\n");
460 if (!tsdata
->chip
->max_fingers
) {
461 dev_err(dev
, "Invalid max_fingers in chip data\n");
465 input
= devm_input_allocate_device(dev
);
467 dev_err(dev
, "Failed to allocate input device\n");
471 tsdata
->client
= client
;
472 tsdata
->input
= input
;
474 input
->name
= client
->name
;
475 input
->id
.bustype
= BUS_I2C
;
476 input
->open
= pixcir_input_open
;
477 input
->close
= pixcir_input_close
;
478 input
->dev
.parent
= dev
;
481 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, pdata
->x_max
, 0, 0);
482 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, pdata
->y_max
, 0, 0);
484 input_set_capability(input
, EV_ABS
, ABS_MT_POSITION_X
);
485 input_set_capability(input
, EV_ABS
, ABS_MT_POSITION_Y
);
486 touchscreen_parse_properties(input
, true, &tsdata
->prop
);
487 if (!input_abs_get_max(input
, ABS_MT_POSITION_X
) ||
488 !input_abs_get_max(input
, ABS_MT_POSITION_Y
)) {
489 dev_err(dev
, "Touchscreen size is not specified\n");
494 tsdata
->max_fingers
= tsdata
->chip
->max_fingers
;
495 if (tsdata
->max_fingers
> PIXCIR_MAX_SLOTS
) {
496 tsdata
->max_fingers
= PIXCIR_MAX_SLOTS
;
497 dev_info(dev
, "Limiting maximum fingers to %d\n",
498 tsdata
->max_fingers
);
501 error
= input_mt_init_slots(input
, tsdata
->max_fingers
,
502 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
504 dev_err(dev
, "Error initializing Multi-Touch slots\n");
508 input_set_drvdata(input
, tsdata
);
510 tsdata
->gpio_attb
= devm_gpiod_get(dev
, "attb", GPIOD_IN
);
511 if (IS_ERR(tsdata
->gpio_attb
)) {
512 error
= PTR_ERR(tsdata
->gpio_attb
);
513 dev_err(dev
, "Failed to request ATTB gpio: %d\n", error
);
517 tsdata
->gpio_reset
= devm_gpiod_get_optional(dev
, "reset",
519 if (IS_ERR(tsdata
->gpio_reset
)) {
520 error
= PTR_ERR(tsdata
->gpio_reset
);
521 dev_err(dev
, "Failed to request RESET gpio: %d\n", error
);
525 tsdata
->gpio_wake
= devm_gpiod_get_optional(dev
, "wake",
527 if (IS_ERR(tsdata
->gpio_wake
)) {
528 error
= PTR_ERR(tsdata
->gpio_wake
);
529 if (error
!= -EPROBE_DEFER
)
530 dev_err(dev
, "Failed to get wake gpio: %d\n", error
);
534 tsdata
->gpio_enable
= devm_gpiod_get_optional(dev
, "enable",
536 if (IS_ERR(tsdata
->gpio_enable
)) {
537 error
= PTR_ERR(tsdata
->gpio_enable
);
538 if (error
!= -EPROBE_DEFER
)
539 dev_err(dev
, "Failed to get enable gpio: %d\n", error
);
543 if (tsdata
->gpio_enable
)
546 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, pixcir_ts_isr
,
547 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
548 client
->name
, tsdata
);
550 dev_err(dev
, "failed to request irq %d\n", client
->irq
);
554 pixcir_reset(tsdata
);
556 /* Always be in IDLE mode to save power, device supports auto wake */
557 error
= pixcir_set_power_mode(tsdata
, PIXCIR_POWER_IDLE
);
559 dev_err(dev
, "Failed to set IDLE mode\n");
563 /* Stop device till opened */
564 error
= pixcir_stop(tsdata
);
568 error
= input_register_device(input
);
572 i2c_set_clientdata(client
, tsdata
);
577 static const struct i2c_device_id pixcir_i2c_ts_id
[] = {
579 { "pixcir_tangoc", 0 },
582 MODULE_DEVICE_TABLE(i2c
, pixcir_i2c_ts_id
);
585 static const struct pixcir_i2c_chip_data pixcir_ts_data
= {
587 /* no hw id support */
590 static const struct pixcir_i2c_chip_data pixcir_tangoc_data
= {
595 static const struct of_device_id pixcir_of_match
[] = {
596 { .compatible
= "pixcir,pixcir_ts", .data
= &pixcir_ts_data
},
597 { .compatible
= "pixcir,pixcir_tangoc", .data
= &pixcir_tangoc_data
},
600 MODULE_DEVICE_TABLE(of
, pixcir_of_match
);
603 static struct i2c_driver pixcir_i2c_ts_driver
= {
606 .pm
= &pixcir_dev_pm_ops
,
607 .of_match_table
= of_match_ptr(pixcir_of_match
),
609 .probe
= pixcir_i2c_ts_probe
,
610 .id_table
= pixcir_i2c_ts_id
,
613 module_i2c_driver(pixcir_i2c_ts_driver
);
615 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
616 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
617 MODULE_LICENSE("GPL");