1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for AUO in-cell touchscreens
5 * Copyright (c) 2011 Heiko Stuebner <heiko@sntech.de>
7 * loosely based on auo_touch.c from Dell Streak vendor-kernel
9 * Copyright (c) 2008 QUALCOMM Incorporated.
10 * Copyright (c) 2008 QUALCOMM USA, INC.
13 #include <linux/err.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/slab.h>
18 #include <linux/input.h>
19 #include <linux/jiffies.h>
20 #include <linux/i2c.h>
21 #include <linux/mutex.h>
22 #include <linux/delay.h>
23 #include <linux/gpio/consumer.h>
25 #include <linux/property.h>
28 * Coordinate calculation:
29 * X1 = X1_LSB + X1_MSB*256
30 * Y1 = Y1_LSB + Y1_MSB*256
31 * X2 = X2_LSB + X2_MSB*256
32 * Y2 = Y2_LSB + Y2_MSB*256
34 #define AUO_PIXCIR_REG_X1_LSB 0x00
35 #define AUO_PIXCIR_REG_X1_MSB 0x01
36 #define AUO_PIXCIR_REG_Y1_LSB 0x02
37 #define AUO_PIXCIR_REG_Y1_MSB 0x03
38 #define AUO_PIXCIR_REG_X2_LSB 0x04
39 #define AUO_PIXCIR_REG_X2_MSB 0x05
40 #define AUO_PIXCIR_REG_Y2_LSB 0x06
41 #define AUO_PIXCIR_REG_Y2_MSB 0x07
43 #define AUO_PIXCIR_REG_STRENGTH 0x0d
44 #define AUO_PIXCIR_REG_STRENGTH_X1_LSB 0x0e
45 #define AUO_PIXCIR_REG_STRENGTH_X1_MSB 0x0f
47 #define AUO_PIXCIR_REG_RAW_DATA_X 0x2b
48 #define AUO_PIXCIR_REG_RAW_DATA_Y 0x4f
50 #define AUO_PIXCIR_REG_X_SENSITIVITY 0x6f
51 #define AUO_PIXCIR_REG_Y_SENSITIVITY 0x70
52 #define AUO_PIXCIR_REG_INT_SETTING 0x71
53 #define AUO_PIXCIR_REG_INT_WIDTH 0x72
54 #define AUO_PIXCIR_REG_POWER_MODE 0x73
56 #define AUO_PIXCIR_REG_VERSION 0x77
57 #define AUO_PIXCIR_REG_CALIBRATE 0x78
59 #define AUO_PIXCIR_REG_TOUCHAREA_X1 0x1e
60 #define AUO_PIXCIR_REG_TOUCHAREA_Y1 0x1f
61 #define AUO_PIXCIR_REG_TOUCHAREA_X2 0x20
62 #define AUO_PIXCIR_REG_TOUCHAREA_Y2 0x21
64 #define AUO_PIXCIR_REG_EEPROM_CALIB_X 0x42
65 #define AUO_PIXCIR_REG_EEPROM_CALIB_Y 0xad
67 #define AUO_PIXCIR_INT_TPNUM_MASK 0xe0
68 #define AUO_PIXCIR_INT_TPNUM_SHIFT 5
69 #define AUO_PIXCIR_INT_RELEASE (1 << 4)
70 #define AUO_PIXCIR_INT_ENABLE (1 << 3)
71 #define AUO_PIXCIR_INT_POL_HIGH (1 << 2)
75 * periodical: interrupt is asserted periodicaly
76 * compare coordinates: interrupt is asserted when coordinates change
77 * indicate touch: interrupt is asserted during touch
79 #define AUO_PIXCIR_INT_PERIODICAL 0x00
80 #define AUO_PIXCIR_INT_COMP_COORD 0x01
81 #define AUO_PIXCIR_INT_TOUCH_IND 0x02
82 #define AUO_PIXCIR_INT_MODE_MASK 0x03
86 * active: scan speed 60Hz
87 * sleep: scan speed 10Hz can be auto-activated, wakeup on 1st touch
88 * deep sleep: scan speed 1Hz can only be entered or left manually.
90 #define AUO_PIXCIR_POWER_ACTIVE 0x00
91 #define AUO_PIXCIR_POWER_SLEEP 0x01
92 #define AUO_PIXCIR_POWER_DEEP_SLEEP 0x02
93 #define AUO_PIXCIR_POWER_MASK 0x03
95 #define AUO_PIXCIR_POWER_ALLOW_SLEEP (1 << 2)
96 #define AUO_PIXCIR_POWER_IDLE_TIME(ms) ((ms & 0xf) << 4)
98 #define AUO_PIXCIR_CALIBRATE 0x03
100 #define AUO_PIXCIR_EEPROM_CALIB_X_LEN 62
101 #define AUO_PIXCIR_EEPROM_CALIB_Y_LEN 36
103 #define AUO_PIXCIR_RAW_DATA_X_LEN 18
104 #define AUO_PIXCIR_RAW_DATA_Y_LEN 11
106 #define AUO_PIXCIR_STRENGTH_ENABLE (1 << 0)
108 /* Touchscreen absolute values */
109 #define AUO_PIXCIR_REPORT_POINTS 2
110 #define AUO_PIXCIR_MAX_AREA 0xff
111 #define AUO_PIXCIR_PENUP_TIMEOUT_MS 10
113 struct auo_pixcir_ts
{
114 struct i2c_client
*client
;
115 struct input_dev
*input
;
116 struct gpio_desc
*gpio_int
;
117 struct gpio_desc
*gpio_rst
;
123 /* special handling for touch_indicate interrupt mode */
126 wait_queue_head_t wait
;
138 static int auo_pixcir_collect_data(struct auo_pixcir_ts
*ts
,
139 struct auo_point_t
*point
)
141 struct i2c_client
*client
= ts
->client
;
142 uint8_t raw_coord
[8];
146 /* touch coordinates */
147 ret
= i2c_smbus_read_i2c_block_data(client
, AUO_PIXCIR_REG_X1_LSB
,
150 dev_err(&client
->dev
, "failed to read coordinate, %d\n", ret
);
155 ret
= i2c_smbus_read_i2c_block_data(client
, AUO_PIXCIR_REG_TOUCHAREA_X1
,
158 dev_err(&client
->dev
, "could not read touch area, %d\n", ret
);
162 for (i
= 0; i
< AUO_PIXCIR_REPORT_POINTS
; i
++) {
164 raw_coord
[4 * i
+ 1] << 8 | raw_coord
[4 * i
];
166 raw_coord
[4 * i
+ 3] << 8 | raw_coord
[4 * i
+ 2];
168 if (point
[i
].coord_x
> ts
->x_max
||
169 point
[i
].coord_y
> ts
->y_max
) {
170 dev_warn(&client
->dev
, "coordinates (%d,%d) invalid\n",
171 point
[i
].coord_x
, point
[i
].coord_y
);
172 point
[i
].coord_x
= point
[i
].coord_y
= 0;
175 /* determine touch major, minor and orientation */
176 point
[i
].area_major
= max(raw_area
[2 * i
], raw_area
[2 * i
+ 1]);
177 point
[i
].area_minor
= min(raw_area
[2 * i
], raw_area
[2 * i
+ 1]);
178 point
[i
].orientation
= raw_area
[2 * i
] > raw_area
[2 * i
+ 1];
184 static irqreturn_t
auo_pixcir_interrupt(int irq
, void *dev_id
)
186 struct auo_pixcir_ts
*ts
= dev_id
;
187 struct auo_point_t point
[AUO_PIXCIR_REPORT_POINTS
];
193 while (!ts
->stopped
) {
195 /* check for up event in touch touch_ind_mode */
196 if (ts
->touch_ind_mode
) {
197 if (gpiod_get_value_cansleep(ts
->gpio_int
) == 0) {
198 input_mt_sync(ts
->input
);
199 input_report_key(ts
->input
, BTN_TOUCH
, 0);
200 input_sync(ts
->input
);
205 ret
= auo_pixcir_collect_data(ts
, point
);
207 /* we want to loop only in touch_ind_mode */
208 if (!ts
->touch_ind_mode
)
211 wait_event_timeout(ts
->wait
, ts
->stopped
,
212 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS
));
216 for (i
= 0; i
< AUO_PIXCIR_REPORT_POINTS
; i
++) {
217 if (point
[i
].coord_x
> 0 || point
[i
].coord_y
> 0) {
218 input_report_abs(ts
->input
, ABS_MT_POSITION_X
,
220 input_report_abs(ts
->input
, ABS_MT_POSITION_Y
,
222 input_report_abs(ts
->input
, ABS_MT_TOUCH_MAJOR
,
223 point
[i
].area_major
);
224 input_report_abs(ts
->input
, ABS_MT_TOUCH_MINOR
,
225 point
[i
].area_minor
);
226 input_report_abs(ts
->input
, ABS_MT_ORIENTATION
,
227 point
[i
].orientation
);
228 input_mt_sync(ts
->input
);
230 /* use first finger as source for singletouch */
234 /* number of touch points could also be queried
235 * via i2c but would require an additional call
241 input_report_key(ts
->input
, BTN_TOUCH
, fingers
> 0);
244 input_report_abs(ts
->input
, ABS_X
, point
[abs
].coord_x
);
245 input_report_abs(ts
->input
, ABS_Y
, point
[abs
].coord_y
);
248 input_sync(ts
->input
);
250 /* we want to loop only in touch_ind_mode */
251 if (!ts
->touch_ind_mode
)
254 wait_event_timeout(ts
->wait
, ts
->stopped
,
255 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS
));
262 * Set the power mode of the device.
264 * - AUO_PIXCIR_POWER_ACTIVE
265 * - AUO_PIXCIR_POWER_SLEEP - automatically left on first touch
266 * - AUO_PIXCIR_POWER_DEEP_SLEEP
268 static int auo_pixcir_power_mode(struct auo_pixcir_ts
*ts
, int mode
)
270 struct i2c_client
*client
= ts
->client
;
273 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_POWER_MODE
);
275 dev_err(&client
->dev
, "unable to read reg %Xh, %d\n",
276 AUO_PIXCIR_REG_POWER_MODE
, ret
);
280 ret
&= ~AUO_PIXCIR_POWER_MASK
;
283 ret
= i2c_smbus_write_byte_data(client
, AUO_PIXCIR_REG_POWER_MODE
, ret
);
285 dev_err(&client
->dev
, "unable to write reg %Xh, %d\n",
286 AUO_PIXCIR_REG_POWER_MODE
, ret
);
293 static int auo_pixcir_int_config(struct auo_pixcir_ts
*ts
, int int_setting
)
295 struct i2c_client
*client
= ts
->client
;
298 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
);
300 dev_err(&client
->dev
, "unable to read reg %Xh, %d\n",
301 AUO_PIXCIR_REG_INT_SETTING
, ret
);
305 ret
&= ~AUO_PIXCIR_INT_MODE_MASK
;
307 ret
|= AUO_PIXCIR_INT_POL_HIGH
; /* always use high for interrupts */
309 ret
= i2c_smbus_write_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
,
312 dev_err(&client
->dev
, "unable to write reg %Xh, %d\n",
313 AUO_PIXCIR_REG_INT_SETTING
, ret
);
317 ts
->touch_ind_mode
= int_setting
== AUO_PIXCIR_INT_TOUCH_IND
;
322 /* control the generation of interrupts on the device side */
323 static int auo_pixcir_int_toggle(struct auo_pixcir_ts
*ts
, bool enable
)
325 struct i2c_client
*client
= ts
->client
;
328 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
);
330 dev_err(&client
->dev
, "unable to read reg %Xh, %d\n",
331 AUO_PIXCIR_REG_INT_SETTING
, ret
);
336 ret
|= AUO_PIXCIR_INT_ENABLE
;
338 ret
&= ~AUO_PIXCIR_INT_ENABLE
;
340 ret
= i2c_smbus_write_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
,
343 dev_err(&client
->dev
, "unable to write reg %Xh, %d\n",
344 AUO_PIXCIR_REG_INT_SETTING
, ret
);
351 static int auo_pixcir_start(struct auo_pixcir_ts
*ts
)
353 struct i2c_client
*client
= ts
->client
;
356 ret
= auo_pixcir_power_mode(ts
, AUO_PIXCIR_POWER_ACTIVE
);
358 dev_err(&client
->dev
, "could not set power mode, %d\n",
365 enable_irq(client
->irq
);
367 ret
= auo_pixcir_int_toggle(ts
, 1);
369 dev_err(&client
->dev
, "could not enable interrupt, %d\n",
371 disable_irq(client
->irq
);
378 static int auo_pixcir_stop(struct auo_pixcir_ts
*ts
)
380 struct i2c_client
*client
= ts
->client
;
383 ret
= auo_pixcir_int_toggle(ts
, 0);
385 dev_err(&client
->dev
, "could not disable interrupt, %d\n",
390 /* disable receiving of interrupts */
391 disable_irq(client
->irq
);
396 return auo_pixcir_power_mode(ts
, AUO_PIXCIR_POWER_DEEP_SLEEP
);
399 static int auo_pixcir_input_open(struct input_dev
*dev
)
401 struct auo_pixcir_ts
*ts
= input_get_drvdata(dev
);
403 return auo_pixcir_start(ts
);
406 static void auo_pixcir_input_close(struct input_dev
*dev
)
408 struct auo_pixcir_ts
*ts
= input_get_drvdata(dev
);
413 static int auo_pixcir_suspend(struct device
*dev
)
415 struct i2c_client
*client
= to_i2c_client(dev
);
416 struct auo_pixcir_ts
*ts
= i2c_get_clientdata(client
);
417 struct input_dev
*input
= ts
->input
;
420 mutex_lock(&input
->mutex
);
422 /* when configured as wakeup source, device should always wake system
423 * therefore start device if necessary
425 if (device_may_wakeup(&client
->dev
)) {
426 /* need to start device if not open, to be wakeup source */
427 if (!input_device_enabled(input
)) {
428 ret
= auo_pixcir_start(ts
);
433 enable_irq_wake(client
->irq
);
434 ret
= auo_pixcir_power_mode(ts
, AUO_PIXCIR_POWER_SLEEP
);
435 } else if (input_device_enabled(input
)) {
436 ret
= auo_pixcir_stop(ts
);
440 mutex_unlock(&input
->mutex
);
445 static int auo_pixcir_resume(struct device
*dev
)
447 struct i2c_client
*client
= to_i2c_client(dev
);
448 struct auo_pixcir_ts
*ts
= i2c_get_clientdata(client
);
449 struct input_dev
*input
= ts
->input
;
452 mutex_lock(&input
->mutex
);
454 if (device_may_wakeup(&client
->dev
)) {
455 disable_irq_wake(client
->irq
);
457 /* need to stop device if it was not open on suspend */
458 if (!input_device_enabled(input
)) {
459 ret
= auo_pixcir_stop(ts
);
464 /* device wakes automatically from SLEEP */
465 } else if (input_device_enabled(input
)) {
466 ret
= auo_pixcir_start(ts
);
470 mutex_unlock(&input
->mutex
);
475 static DEFINE_SIMPLE_DEV_PM_OPS(auo_pixcir_pm_ops
,
476 auo_pixcir_suspend
, auo_pixcir_resume
);
478 static void auo_pixcir_reset(void *data
)
480 struct auo_pixcir_ts
*ts
= data
;
482 gpiod_set_value_cansleep(ts
->gpio_rst
, 1);
485 static int auo_pixcir_probe(struct i2c_client
*client
)
487 struct auo_pixcir_ts
*ts
;
488 struct input_dev
*input_dev
;
492 ts
= devm_kzalloc(&client
->dev
, sizeof(*ts
), GFP_KERNEL
);
496 input_dev
= devm_input_allocate_device(&client
->dev
);
498 dev_err(&client
->dev
, "could not allocate input device\n");
503 ts
->input
= input_dev
;
504 ts
->touch_ind_mode
= 0;
506 init_waitqueue_head(&ts
->wait
);
508 snprintf(ts
->phys
, sizeof(ts
->phys
),
509 "%s/input0", dev_name(&client
->dev
));
511 if (device_property_read_u32(&client
->dev
, "x-size", &ts
->x_max
)) {
512 dev_err(&client
->dev
, "failed to get x-size property\n");
516 if (device_property_read_u32(&client
->dev
, "y-size", &ts
->y_max
)) {
517 dev_err(&client
->dev
, "failed to get y-size property\n");
521 input_dev
->name
= "AUO-Pixcir touchscreen";
522 input_dev
->phys
= ts
->phys
;
523 input_dev
->id
.bustype
= BUS_I2C
;
525 input_dev
->open
= auo_pixcir_input_open
;
526 input_dev
->close
= auo_pixcir_input_close
;
528 __set_bit(EV_ABS
, input_dev
->evbit
);
529 __set_bit(EV_KEY
, input_dev
->evbit
);
531 __set_bit(BTN_TOUCH
, input_dev
->keybit
);
533 /* For single touch */
534 input_set_abs_params(input_dev
, ABS_X
, 0, ts
->x_max
, 0, 0);
535 input_set_abs_params(input_dev
, ABS_Y
, 0, ts
->y_max
, 0, 0);
537 /* For multi touch */
538 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0, ts
->x_max
, 0, 0);
539 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0, ts
->y_max
, 0, 0);
540 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
541 0, AUO_PIXCIR_MAX_AREA
, 0, 0);
542 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MINOR
,
543 0, AUO_PIXCIR_MAX_AREA
, 0, 0);
544 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
, 0, 1, 0, 0);
546 input_set_drvdata(ts
->input
, ts
);
548 ts
->gpio_int
= devm_gpiod_get_index(&client
->dev
, NULL
, 0, GPIOD_IN
);
549 error
= PTR_ERR_OR_ZERO(ts
->gpio_int
);
551 dev_err(&client
->dev
,
552 "request of int gpio failed: %d\n", error
);
556 gpiod_set_consumer_name(ts
->gpio_int
, "auo_pixcir_ts_int");
558 /* Take the chip out of reset */
559 ts
->gpio_rst
= devm_gpiod_get_index(&client
->dev
, NULL
, 1,
561 error
= PTR_ERR_OR_ZERO(ts
->gpio_rst
);
563 dev_err(&client
->dev
,
564 "request of reset gpio failed: %d\n", error
);
568 gpiod_set_consumer_name(ts
->gpio_rst
, "auo_pixcir_ts_rst");
570 error
= devm_add_action_or_reset(&client
->dev
, auo_pixcir_reset
, ts
);
572 dev_err(&client
->dev
, "failed to register reset action, %d\n",
579 version
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_VERSION
);
585 dev_info(&client
->dev
, "firmware version 0x%X\n", version
);
587 /* default to asserting the interrupt when the screen is touched */
588 error
= auo_pixcir_int_config(ts
, AUO_PIXCIR_INT_TOUCH_IND
);
592 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
593 NULL
, auo_pixcir_interrupt
,
595 input_dev
->name
, ts
);
597 dev_err(&client
->dev
, "irq %d requested failed, %d\n",
602 /* stop device and put it into deep sleep until it is opened */
603 error
= auo_pixcir_stop(ts
);
607 error
= input_register_device(input_dev
);
609 dev_err(&client
->dev
, "could not register input device, %d\n",
614 i2c_set_clientdata(client
, ts
);
619 static const struct i2c_device_id auo_pixcir_idtable
[] = {
623 MODULE_DEVICE_TABLE(i2c
, auo_pixcir_idtable
);
626 static const struct of_device_id auo_pixcir_ts_dt_idtable
[] = {
627 { .compatible
= "auo,auo_pixcir_ts" },
630 MODULE_DEVICE_TABLE(of
, auo_pixcir_ts_dt_idtable
);
633 static struct i2c_driver auo_pixcir_driver
= {
635 .name
= "auo_pixcir_ts",
636 .pm
= pm_sleep_ptr(&auo_pixcir_pm_ops
),
637 .of_match_table
= of_match_ptr(auo_pixcir_ts_dt_idtable
),
639 .probe
= auo_pixcir_probe
,
640 .id_table
= auo_pixcir_idtable
,
643 module_i2c_driver(auo_pixcir_driver
);
645 MODULE_DESCRIPTION("AUO-PIXCIR touchscreen driver");
646 MODULE_LICENSE("GPL v2");
647 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");