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/unaligned.h>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/input.h>
13 #include <linux/input/mt.h>
14 #include <linux/input/touchscreen.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
18 #include <linux/slab.h>
20 #define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
25 #define PIXCIR_REG_POWER_MODE 51
26 #define PIXCIR_REG_INT_MODE 52
30 * active: max scan speed
31 * idle: lower scan speed with automatic transition to active on touch
32 * halt: datasheet says sleep but this is more like halt as the chip
33 * clocks are cut and it can only be brought out of this mode
34 * using the RESET pin.
36 enum pixcir_power_mode
{
42 #define PIXCIR_POWER_MODE_MASK 0x03
43 #define PIXCIR_POWER_ALLOW_IDLE (1UL << 2)
47 * periodical: interrupt is asserted periodicaly
48 * diff coordinates: interrupt is asserted when coordinates change
49 * level on touch: interrupt level asserted during touch
50 * pulse on touch: interrupt pulse asserted during touch
53 enum pixcir_int_mode
{
54 PIXCIR_INT_PERIODICAL
,
55 PIXCIR_INT_DIFF_COORD
,
56 PIXCIR_INT_LEVEL_TOUCH
,
57 PIXCIR_INT_PULSE_TOUCH
,
60 #define PIXCIR_INT_MODE_MASK 0x03
61 #define PIXCIR_INT_ENABLE (1UL << 3)
62 #define PIXCIR_INT_POL_HIGH (1UL << 2)
65 * struct pixcir_i2c_chip_data - chip related data
66 * @max_fingers: Max number of fingers reported simultaneously by h/w
67 * @has_hw_ids: Hardware supports finger tracking IDs
70 struct pixcir_i2c_chip_data
{
75 struct pixcir_i2c_ts_data
{
76 struct i2c_client
*client
;
77 struct input_dev
*input
;
78 struct gpio_desc
*gpio_attb
;
79 struct gpio_desc
*gpio_reset
;
80 struct gpio_desc
*gpio_enable
;
81 struct gpio_desc
*gpio_wake
;
82 const struct pixcir_i2c_chip_data
*chip
;
83 struct touchscreen_properties prop
;
87 struct pixcir_report_data
{
89 struct input_mt_pos pos
[PIXCIR_MAX_SLOTS
];
90 int ids
[PIXCIR_MAX_SLOTS
];
93 static void pixcir_ts_parse(struct pixcir_i2c_ts_data
*tsdata
,
94 struct pixcir_report_data
*report
)
96 u8 rdbuf
[2 + PIXCIR_MAX_SLOTS
* 5];
102 const struct pixcir_i2c_chip_data
*chip
= tsdata
->chip
;
104 memset(report
, 0, sizeof(struct pixcir_report_data
));
106 i
= chip
->has_hw_ids
? 1 : 0;
107 readsize
= 2 + tsdata
->chip
->max_fingers
* (4 + i
);
108 if (readsize
> sizeof(rdbuf
))
109 readsize
= sizeof(rdbuf
);
111 ret
= i2c_master_send(tsdata
->client
, wrbuf
, sizeof(wrbuf
));
112 if (ret
!= sizeof(wrbuf
)) {
113 dev_err(&tsdata
->client
->dev
,
114 "%s: i2c_master_send failed(), ret=%d\n",
119 ret
= i2c_master_recv(tsdata
->client
, rdbuf
, readsize
);
120 if (ret
!= readsize
) {
121 dev_err(&tsdata
->client
->dev
,
122 "%s: i2c_master_recv failed(), ret=%d\n",
127 touch
= rdbuf
[0] & 0x7;
128 if (touch
> tsdata
->chip
->max_fingers
)
129 touch
= tsdata
->chip
->max_fingers
;
131 report
->num_touches
= touch
;
134 for (i
= 0; i
< touch
; i
++) {
135 touchscreen_set_mt_pos(&report
->pos
[i
], &tsdata
->prop
,
136 get_unaligned_le16(bufptr
),
137 get_unaligned_le16(bufptr
+ 2));
138 if (chip
->has_hw_ids
) {
139 report
->ids
[i
] = bufptr
[4];
147 static void pixcir_ts_report(struct pixcir_i2c_ts_data
*ts
,
148 struct pixcir_report_data
*report
)
150 int slots
[PIXCIR_MAX_SLOTS
];
152 struct device
*dev
= &ts
->client
->dev
;
153 const struct pixcir_i2c_chip_data
*chip
= ts
->chip
;
155 n
= report
->num_touches
;
156 if (n
> PIXCIR_MAX_SLOTS
)
157 n
= PIXCIR_MAX_SLOTS
;
159 if (!ts
->chip
->has_hw_ids
)
160 input_mt_assign_slots(ts
->input
, slots
, report
->pos
, n
, 0);
162 for (i
= 0; i
< n
; i
++) {
163 if (chip
->has_hw_ids
) {
164 slot
= input_mt_get_slot_by_key(ts
->input
,
167 dev_dbg(dev
, "no free slot for id 0x%x\n",
175 input_mt_slot(ts
->input
, slot
);
176 input_mt_report_slot_state(ts
->input
, MT_TOOL_FINGER
, true);
178 input_report_abs(ts
->input
, ABS_MT_POSITION_X
,
180 input_report_abs(ts
->input
, ABS_MT_POSITION_Y
,
183 dev_dbg(dev
, "%d: slot %d, x %d, y %d\n",
184 i
, slot
, report
->pos
[i
].x
, report
->pos
[i
].y
);
187 input_mt_sync_frame(ts
->input
);
188 input_sync(ts
->input
);
191 static irqreturn_t
pixcir_ts_isr(int irq
, void *dev_id
)
193 struct pixcir_i2c_ts_data
*tsdata
= dev_id
;
194 struct pixcir_report_data report
;
196 while (tsdata
->running
) {
198 pixcir_ts_parse(tsdata
, &report
);
201 pixcir_ts_report(tsdata
, &report
);
203 if (gpiod_get_value_cansleep(tsdata
->gpio_attb
)) {
204 if (report
.num_touches
) {
206 * Last report with no finger up?
209 input_mt_sync_frame(tsdata
->input
);
210 input_sync(tsdata
->input
);
221 static void pixcir_reset(struct pixcir_i2c_ts_data
*tsdata
)
223 if (!IS_ERR_OR_NULL(tsdata
->gpio_reset
)) {
224 gpiod_set_value_cansleep(tsdata
->gpio_reset
, 1);
225 ndelay(100); /* datasheet section 1.2.3 says 80ns min. */
226 gpiod_set_value_cansleep(tsdata
->gpio_reset
, 0);
227 /* wait for controller ready. 100ms guess. */
232 static int pixcir_set_power_mode(struct pixcir_i2c_ts_data
*ts
,
233 enum pixcir_power_mode mode
)
235 struct device
*dev
= &ts
->client
->dev
;
238 if (mode
== PIXCIR_POWER_ACTIVE
|| mode
== PIXCIR_POWER_IDLE
) {
240 gpiod_set_value_cansleep(ts
->gpio_wake
, 1);
243 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
);
245 dev_err(dev
, "%s: can't read reg %d : %d\n",
246 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
250 ret
&= ~PIXCIR_POWER_MODE_MASK
;
253 /* Always AUTO_IDLE */
254 ret
|= PIXCIR_POWER_ALLOW_IDLE
;
256 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
, ret
);
258 dev_err(dev
, "%s: can't write reg %d : %d\n",
259 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
263 if (mode
== PIXCIR_POWER_HALT
) {
265 gpiod_set_value_cansleep(ts
->gpio_wake
, 0);
272 * Set the interrupt mode for the device i.e. ATTB line behaviour
274 * @polarity : 1 for active high, 0 for active low.
276 static int pixcir_set_int_mode(struct pixcir_i2c_ts_data
*ts
,
277 enum pixcir_int_mode mode
, bool polarity
)
279 struct device
*dev
= &ts
->client
->dev
;
282 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
284 dev_err(dev
, "%s: can't read reg %d : %d\n",
285 __func__
, PIXCIR_REG_INT_MODE
, ret
);
289 ret
&= ~PIXCIR_INT_MODE_MASK
;
293 ret
|= PIXCIR_INT_POL_HIGH
;
295 ret
&= ~PIXCIR_INT_POL_HIGH
;
297 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
299 dev_err(dev
, "%s: can't write reg %d : %d\n",
300 __func__
, PIXCIR_REG_INT_MODE
, ret
);
308 * Enable/disable interrupt generation
310 static int pixcir_int_enable(struct pixcir_i2c_ts_data
*ts
, bool enable
)
312 struct device
*dev
= &ts
->client
->dev
;
315 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
317 dev_err(dev
, "%s: can't read reg %d : %d\n",
318 __func__
, PIXCIR_REG_INT_MODE
, ret
);
323 ret
|= PIXCIR_INT_ENABLE
;
325 ret
&= ~PIXCIR_INT_ENABLE
;
327 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
329 dev_err(dev
, "%s: can't write reg %d : %d\n",
330 __func__
, PIXCIR_REG_INT_MODE
, ret
);
337 static int pixcir_start(struct pixcir_i2c_ts_data
*ts
)
339 struct device
*dev
= &ts
->client
->dev
;
342 if (ts
->gpio_enable
) {
343 gpiod_set_value_cansleep(ts
->gpio_enable
, 1);
347 /* LEVEL_TOUCH interrupt with active low polarity */
348 error
= pixcir_set_int_mode(ts
, PIXCIR_INT_LEVEL_TOUCH
, 0);
350 dev_err(dev
, "Failed to set interrupt mode: %d\n", error
);
355 mb(); /* Update status before IRQ can fire */
357 /* enable interrupt generation */
358 error
= pixcir_int_enable(ts
, true);
360 dev_err(dev
, "Failed to enable interrupt generation: %d\n",
368 static int pixcir_stop(struct pixcir_i2c_ts_data
*ts
)
372 /* Disable interrupt generation */
373 error
= pixcir_int_enable(ts
, false);
375 dev_err(&ts
->client
->dev
,
376 "Failed to disable interrupt generation: %d\n",
381 /* Exit ISR if running, no more report parsing */
383 mb(); /* update status before we synchronize irq */
385 /* Wait till running ISR is complete */
386 synchronize_irq(ts
->client
->irq
);
389 gpiod_set_value_cansleep(ts
->gpio_enable
, 0);
394 static int pixcir_input_open(struct input_dev
*dev
)
396 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
398 return pixcir_start(ts
);
401 static void pixcir_input_close(struct input_dev
*dev
)
403 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
408 static int pixcir_i2c_ts_suspend(struct device
*dev
)
410 struct i2c_client
*client
= to_i2c_client(dev
);
411 struct pixcir_i2c_ts_data
*ts
= i2c_get_clientdata(client
);
412 struct input_dev
*input
= ts
->input
;
415 mutex_lock(&input
->mutex
);
417 if (device_may_wakeup(&client
->dev
)) {
418 if (!input_device_enabled(input
)) {
419 ret
= pixcir_start(ts
);
421 dev_err(dev
, "Failed to start\n");
425 } else if (input_device_enabled(input
)) {
426 ret
= pixcir_stop(ts
);
430 mutex_unlock(&input
->mutex
);
435 static int pixcir_i2c_ts_resume(struct device
*dev
)
437 struct i2c_client
*client
= to_i2c_client(dev
);
438 struct pixcir_i2c_ts_data
*ts
= i2c_get_clientdata(client
);
439 struct input_dev
*input
= ts
->input
;
442 mutex_lock(&input
->mutex
);
444 if (device_may_wakeup(&client
->dev
)) {
445 if (!input_device_enabled(input
)) {
446 ret
= pixcir_stop(ts
);
448 dev_err(dev
, "Failed to stop\n");
452 } else if (input_device_enabled(input
)) {
453 ret
= pixcir_start(ts
);
457 mutex_unlock(&input
->mutex
);
462 static DEFINE_SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops
,
463 pixcir_i2c_ts_suspend
, pixcir_i2c_ts_resume
);
465 static int pixcir_i2c_ts_probe(struct i2c_client
*client
)
467 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
468 struct device
*dev
= &client
->dev
;
469 struct pixcir_i2c_ts_data
*tsdata
;
470 struct input_dev
*input
;
473 tsdata
= devm_kzalloc(dev
, sizeof(*tsdata
), GFP_KERNEL
);
477 tsdata
->chip
= device_get_match_data(dev
);
478 if (!tsdata
->chip
&& id
)
479 tsdata
->chip
= (const void *)id
->driver_data
;
481 dev_err(dev
, "can't locate chip data\n");
485 input
= devm_input_allocate_device(dev
);
487 dev_err(dev
, "Failed to allocate input device\n");
491 tsdata
->client
= client
;
492 tsdata
->input
= input
;
494 input
->name
= client
->name
;
495 input
->id
.bustype
= BUS_I2C
;
496 input
->open
= pixcir_input_open
;
497 input
->close
= pixcir_input_close
;
499 input_set_capability(input
, EV_ABS
, ABS_MT_POSITION_X
);
500 input_set_capability(input
, EV_ABS
, ABS_MT_POSITION_Y
);
501 touchscreen_parse_properties(input
, true, &tsdata
->prop
);
502 if (!input_abs_get_max(input
, ABS_MT_POSITION_X
) ||
503 !input_abs_get_max(input
, ABS_MT_POSITION_Y
)) {
504 dev_err(dev
, "Touchscreen size is not specified\n");
508 error
= input_mt_init_slots(input
, tsdata
->chip
->max_fingers
,
509 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
511 dev_err(dev
, "Error initializing Multi-Touch slots\n");
515 input_set_drvdata(input
, tsdata
);
517 tsdata
->gpio_attb
= devm_gpiod_get(dev
, "attb", GPIOD_IN
);
518 if (IS_ERR(tsdata
->gpio_attb
))
519 return dev_err_probe(dev
, PTR_ERR(tsdata
->gpio_attb
),
520 "Failed to request ATTB gpio\n");
522 tsdata
->gpio_reset
= devm_gpiod_get_optional(dev
, "reset",
524 if (IS_ERR(tsdata
->gpio_reset
))
525 return dev_err_probe(dev
, PTR_ERR(tsdata
->gpio_reset
),
526 "Failed to request RESET gpio\n");
528 tsdata
->gpio_wake
= devm_gpiod_get_optional(dev
, "wake",
530 if (IS_ERR(tsdata
->gpio_wake
))
531 return dev_err_probe(dev
, PTR_ERR(tsdata
->gpio_wake
),
532 "Failed to get wake gpio\n");
534 tsdata
->gpio_enable
= devm_gpiod_get_optional(dev
, "enable",
536 if (IS_ERR(tsdata
->gpio_enable
))
537 return dev_err_probe(dev
, PTR_ERR(tsdata
->gpio_enable
),
538 "Failed to get enable gpio\n");
540 if (tsdata
->gpio_enable
)
543 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, pixcir_ts_isr
,
544 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
545 client
->name
, tsdata
);
547 dev_err(dev
, "failed to request irq %d\n", client
->irq
);
551 pixcir_reset(tsdata
);
553 /* Always be in IDLE mode to save power, device supports auto wake */
554 error
= pixcir_set_power_mode(tsdata
, PIXCIR_POWER_IDLE
);
556 dev_err(dev
, "Failed to set IDLE mode\n");
560 /* Stop device till opened */
561 error
= pixcir_stop(tsdata
);
565 error
= input_register_device(input
);
569 i2c_set_clientdata(client
, tsdata
);
574 static const struct pixcir_i2c_chip_data pixcir_ts_data
= {
576 /* no hw id support */
579 static const struct pixcir_i2c_chip_data pixcir_tangoc_data
= {
584 static const struct i2c_device_id pixcir_i2c_ts_id
[] = {
585 { "pixcir_ts", (unsigned long) &pixcir_ts_data
},
586 { "pixcir_tangoc", (unsigned long) &pixcir_tangoc_data
},
589 MODULE_DEVICE_TABLE(i2c
, pixcir_i2c_ts_id
);
592 static const struct of_device_id pixcir_of_match
[] = {
593 { .compatible
= "pixcir,pixcir_ts", .data
= &pixcir_ts_data
},
594 { .compatible
= "pixcir,pixcir_tangoc", .data
= &pixcir_tangoc_data
},
597 MODULE_DEVICE_TABLE(of
, pixcir_of_match
);
600 static struct i2c_driver pixcir_i2c_ts_driver
= {
603 .pm
= pm_sleep_ptr(&pixcir_dev_pm_ops
),
604 .of_match_table
= of_match_ptr(pixcir_of_match
),
606 .probe
= pixcir_i2c_ts_probe
,
607 .id_table
= pixcir_i2c_ts_id
,
610 module_i2c_driver(pixcir_i2c_ts_driver
);
612 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
613 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
614 MODULE_LICENSE("GPL");