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/kernel.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/slab.h>
17 #include <linux/input.h>
18 #include <linux/jiffies.h>
19 #include <linux/i2c.h>
20 #include <linux/mutex.h>
21 #include <linux/delay.h>
22 #include <linux/gpio.h>
23 #include <linux/input/auo-pixcir-ts.h>
25 #include <linux/of_gpio.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)
72 #define AUO_PIXCIR_INT_MODE_MASK 0x03
76 * active: scan speed 60Hz
77 * sleep: scan speed 10Hz can be auto-activated, wakeup on 1st touch
78 * deep sleep: scan speed 1Hz can only be entered or left manually.
80 #define AUO_PIXCIR_POWER_ACTIVE 0x00
81 #define AUO_PIXCIR_POWER_SLEEP 0x01
82 #define AUO_PIXCIR_POWER_DEEP_SLEEP 0x02
83 #define AUO_PIXCIR_POWER_MASK 0x03
85 #define AUO_PIXCIR_POWER_ALLOW_SLEEP (1 << 2)
86 #define AUO_PIXCIR_POWER_IDLE_TIME(ms) ((ms & 0xf) << 4)
88 #define AUO_PIXCIR_CALIBRATE 0x03
90 #define AUO_PIXCIR_EEPROM_CALIB_X_LEN 62
91 #define AUO_PIXCIR_EEPROM_CALIB_Y_LEN 36
93 #define AUO_PIXCIR_RAW_DATA_X_LEN 18
94 #define AUO_PIXCIR_RAW_DATA_Y_LEN 11
96 #define AUO_PIXCIR_STRENGTH_ENABLE (1 << 0)
98 /* Touchscreen absolute values */
99 #define AUO_PIXCIR_REPORT_POINTS 2
100 #define AUO_PIXCIR_MAX_AREA 0xff
101 #define AUO_PIXCIR_PENUP_TIMEOUT_MS 10
103 struct auo_pixcir_ts
{
104 struct i2c_client
*client
;
105 struct input_dev
*input
;
106 const struct auo_pixcir_ts_platdata
*pdata
;
109 /* special handling for touch_indicate interupt mode */
112 wait_queue_head_t wait
;
124 static int auo_pixcir_collect_data(struct auo_pixcir_ts
*ts
,
125 struct auo_point_t
*point
)
127 struct i2c_client
*client
= ts
->client
;
128 const struct auo_pixcir_ts_platdata
*pdata
= ts
->pdata
;
129 uint8_t raw_coord
[8];
133 /* touch coordinates */
134 ret
= i2c_smbus_read_i2c_block_data(client
, AUO_PIXCIR_REG_X1_LSB
,
137 dev_err(&client
->dev
, "failed to read coordinate, %d\n", ret
);
142 ret
= i2c_smbus_read_i2c_block_data(client
, AUO_PIXCIR_REG_TOUCHAREA_X1
,
145 dev_err(&client
->dev
, "could not read touch area, %d\n", ret
);
149 for (i
= 0; i
< AUO_PIXCIR_REPORT_POINTS
; i
++) {
151 raw_coord
[4 * i
+ 1] << 8 | raw_coord
[4 * i
];
153 raw_coord
[4 * i
+ 3] << 8 | raw_coord
[4 * i
+ 2];
155 if (point
[i
].coord_x
> pdata
->x_max
||
156 point
[i
].coord_y
> pdata
->y_max
) {
157 dev_warn(&client
->dev
, "coordinates (%d,%d) invalid\n",
158 point
[i
].coord_x
, point
[i
].coord_y
);
159 point
[i
].coord_x
= point
[i
].coord_y
= 0;
162 /* determine touch major, minor and orientation */
163 point
[i
].area_major
= max(raw_area
[2 * i
], raw_area
[2 * i
+ 1]);
164 point
[i
].area_minor
= min(raw_area
[2 * i
], raw_area
[2 * i
+ 1]);
165 point
[i
].orientation
= raw_area
[2 * i
] > raw_area
[2 * i
+ 1];
171 static irqreturn_t
auo_pixcir_interrupt(int irq
, void *dev_id
)
173 struct auo_pixcir_ts
*ts
= dev_id
;
174 const struct auo_pixcir_ts_platdata
*pdata
= ts
->pdata
;
175 struct auo_point_t point
[AUO_PIXCIR_REPORT_POINTS
];
181 while (!ts
->stopped
) {
183 /* check for up event in touch touch_ind_mode */
184 if (ts
->touch_ind_mode
) {
185 if (gpio_get_value(pdata
->gpio_int
) == 0) {
186 input_mt_sync(ts
->input
);
187 input_report_key(ts
->input
, BTN_TOUCH
, 0);
188 input_sync(ts
->input
);
193 ret
= auo_pixcir_collect_data(ts
, point
);
195 /* we want to loop only in touch_ind_mode */
196 if (!ts
->touch_ind_mode
)
199 wait_event_timeout(ts
->wait
, ts
->stopped
,
200 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS
));
204 for (i
= 0; i
< AUO_PIXCIR_REPORT_POINTS
; i
++) {
205 if (point
[i
].coord_x
> 0 || point
[i
].coord_y
> 0) {
206 input_report_abs(ts
->input
, ABS_MT_POSITION_X
,
208 input_report_abs(ts
->input
, ABS_MT_POSITION_Y
,
210 input_report_abs(ts
->input
, ABS_MT_TOUCH_MAJOR
,
211 point
[i
].area_major
);
212 input_report_abs(ts
->input
, ABS_MT_TOUCH_MINOR
,
213 point
[i
].area_minor
);
214 input_report_abs(ts
->input
, ABS_MT_ORIENTATION
,
215 point
[i
].orientation
);
216 input_mt_sync(ts
->input
);
218 /* use first finger as source for singletouch */
222 /* number of touch points could also be queried
223 * via i2c but would require an additional call
229 input_report_key(ts
->input
, BTN_TOUCH
, fingers
> 0);
232 input_report_abs(ts
->input
, ABS_X
, point
[abs
].coord_x
);
233 input_report_abs(ts
->input
, ABS_Y
, point
[abs
].coord_y
);
236 input_sync(ts
->input
);
238 /* we want to loop only in touch_ind_mode */
239 if (!ts
->touch_ind_mode
)
242 wait_event_timeout(ts
->wait
, ts
->stopped
,
243 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS
));
250 * Set the power mode of the device.
252 * - AUO_PIXCIR_POWER_ACTIVE
253 * - AUO_PIXCIR_POWER_SLEEP - automatically left on first touch
254 * - AUO_PIXCIR_POWER_DEEP_SLEEP
256 static int auo_pixcir_power_mode(struct auo_pixcir_ts
*ts
, int mode
)
258 struct i2c_client
*client
= ts
->client
;
261 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_POWER_MODE
);
263 dev_err(&client
->dev
, "unable to read reg %Xh, %d\n",
264 AUO_PIXCIR_REG_POWER_MODE
, ret
);
268 ret
&= ~AUO_PIXCIR_POWER_MASK
;
271 ret
= i2c_smbus_write_byte_data(client
, AUO_PIXCIR_REG_POWER_MODE
, ret
);
273 dev_err(&client
->dev
, "unable to write reg %Xh, %d\n",
274 AUO_PIXCIR_REG_POWER_MODE
, ret
);
281 static int auo_pixcir_int_config(struct auo_pixcir_ts
*ts
,
284 struct i2c_client
*client
= ts
->client
;
285 const struct auo_pixcir_ts_platdata
*pdata
= ts
->pdata
;
288 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
);
290 dev_err(&client
->dev
, "unable to read reg %Xh, %d\n",
291 AUO_PIXCIR_REG_INT_SETTING
, ret
);
295 ret
&= ~AUO_PIXCIR_INT_MODE_MASK
;
297 ret
|= AUO_PIXCIR_INT_POL_HIGH
; /* always use high for interrupts */
299 ret
= i2c_smbus_write_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
,
302 dev_err(&client
->dev
, "unable to write reg %Xh, %d\n",
303 AUO_PIXCIR_REG_INT_SETTING
, ret
);
307 ts
->touch_ind_mode
= pdata
->int_setting
== AUO_PIXCIR_INT_TOUCH_IND
;
312 /* control the generation of interrupts on the device side */
313 static int auo_pixcir_int_toggle(struct auo_pixcir_ts
*ts
, bool enable
)
315 struct i2c_client
*client
= ts
->client
;
318 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
);
320 dev_err(&client
->dev
, "unable to read reg %Xh, %d\n",
321 AUO_PIXCIR_REG_INT_SETTING
, ret
);
326 ret
|= AUO_PIXCIR_INT_ENABLE
;
328 ret
&= ~AUO_PIXCIR_INT_ENABLE
;
330 ret
= i2c_smbus_write_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
,
333 dev_err(&client
->dev
, "unable to write reg %Xh, %d\n",
334 AUO_PIXCIR_REG_INT_SETTING
, ret
);
341 static int auo_pixcir_start(struct auo_pixcir_ts
*ts
)
343 struct i2c_client
*client
= ts
->client
;
346 ret
= auo_pixcir_power_mode(ts
, AUO_PIXCIR_POWER_ACTIVE
);
348 dev_err(&client
->dev
, "could not set power mode, %d\n",
355 enable_irq(client
->irq
);
357 ret
= auo_pixcir_int_toggle(ts
, 1);
359 dev_err(&client
->dev
, "could not enable interrupt, %d\n",
361 disable_irq(client
->irq
);
368 static int auo_pixcir_stop(struct auo_pixcir_ts
*ts
)
370 struct i2c_client
*client
= ts
->client
;
373 ret
= auo_pixcir_int_toggle(ts
, 0);
375 dev_err(&client
->dev
, "could not disable interrupt, %d\n",
380 /* disable receiving of interrupts */
381 disable_irq(client
->irq
);
386 return auo_pixcir_power_mode(ts
, AUO_PIXCIR_POWER_DEEP_SLEEP
);
389 static int auo_pixcir_input_open(struct input_dev
*dev
)
391 struct auo_pixcir_ts
*ts
= input_get_drvdata(dev
);
393 return auo_pixcir_start(ts
);
396 static void auo_pixcir_input_close(struct input_dev
*dev
)
398 struct auo_pixcir_ts
*ts
= input_get_drvdata(dev
);
403 static int __maybe_unused
auo_pixcir_suspend(struct device
*dev
)
405 struct i2c_client
*client
= to_i2c_client(dev
);
406 struct auo_pixcir_ts
*ts
= i2c_get_clientdata(client
);
407 struct input_dev
*input
= ts
->input
;
410 mutex_lock(&input
->mutex
);
412 /* when configured as wakeup source, device should always wake system
413 * therefore start device if necessary
415 if (device_may_wakeup(&client
->dev
)) {
416 /* need to start device if not open, to be wakeup source */
417 if (!input_device_enabled(input
)) {
418 ret
= auo_pixcir_start(ts
);
423 enable_irq_wake(client
->irq
);
424 ret
= auo_pixcir_power_mode(ts
, AUO_PIXCIR_POWER_SLEEP
);
425 } else if (input_device_enabled(input
)) {
426 ret
= auo_pixcir_stop(ts
);
430 mutex_unlock(&input
->mutex
);
435 static int __maybe_unused
auo_pixcir_resume(struct device
*dev
)
437 struct i2c_client
*client
= to_i2c_client(dev
);
438 struct auo_pixcir_ts
*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 disable_irq_wake(client
->irq
);
447 /* need to stop device if it was not open on suspend */
448 if (!input_device_enabled(input
)) {
449 ret
= auo_pixcir_stop(ts
);
454 /* device wakes automatically from SLEEP */
455 } else if (input_device_enabled(input
)) {
456 ret
= auo_pixcir_start(ts
);
460 mutex_unlock(&input
->mutex
);
465 static SIMPLE_DEV_PM_OPS(auo_pixcir_pm_ops
,
466 auo_pixcir_suspend
, auo_pixcir_resume
);
469 static struct auo_pixcir_ts_platdata
*auo_pixcir_parse_dt(struct device
*dev
)
471 struct auo_pixcir_ts_platdata
*pdata
;
472 struct device_node
*np
= dev
->of_node
;
475 return ERR_PTR(-ENOENT
);
477 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
479 return ERR_PTR(-ENOMEM
);
481 pdata
->gpio_int
= of_get_gpio(np
, 0);
482 if (!gpio_is_valid(pdata
->gpio_int
)) {
483 dev_err(dev
, "failed to get interrupt gpio\n");
484 return ERR_PTR(-EINVAL
);
487 pdata
->gpio_rst
= of_get_gpio(np
, 1);
488 if (!gpio_is_valid(pdata
->gpio_rst
)) {
489 dev_err(dev
, "failed to get reset gpio\n");
490 return ERR_PTR(-EINVAL
);
493 if (of_property_read_u32(np
, "x-size", &pdata
->x_max
)) {
494 dev_err(dev
, "failed to get x-size property\n");
495 return ERR_PTR(-EINVAL
);
498 if (of_property_read_u32(np
, "y-size", &pdata
->y_max
)) {
499 dev_err(dev
, "failed to get y-size property\n");
500 return ERR_PTR(-EINVAL
);
503 /* default to asserting the interrupt when the screen is touched */
504 pdata
->int_setting
= AUO_PIXCIR_INT_TOUCH_IND
;
509 static struct auo_pixcir_ts_platdata
*auo_pixcir_parse_dt(struct device
*dev
)
511 return ERR_PTR(-EINVAL
);
515 static void auo_pixcir_reset(void *data
)
517 struct auo_pixcir_ts
*ts
= data
;
519 gpio_set_value(ts
->pdata
->gpio_rst
, 0);
522 static int auo_pixcir_probe(struct i2c_client
*client
,
523 const struct i2c_device_id
*id
)
525 const struct auo_pixcir_ts_platdata
*pdata
;
526 struct auo_pixcir_ts
*ts
;
527 struct input_dev
*input_dev
;
531 pdata
= dev_get_platdata(&client
->dev
);
533 pdata
= auo_pixcir_parse_dt(&client
->dev
);
535 return PTR_ERR(pdata
);
538 ts
= devm_kzalloc(&client
->dev
,
539 sizeof(struct auo_pixcir_ts
), GFP_KERNEL
);
543 input_dev
= devm_input_allocate_device(&client
->dev
);
545 dev_err(&client
->dev
, "could not allocate input device\n");
551 ts
->input
= input_dev
;
552 ts
->touch_ind_mode
= 0;
554 init_waitqueue_head(&ts
->wait
);
556 snprintf(ts
->phys
, sizeof(ts
->phys
),
557 "%s/input0", dev_name(&client
->dev
));
559 input_dev
->name
= "AUO-Pixcir touchscreen";
560 input_dev
->phys
= ts
->phys
;
561 input_dev
->id
.bustype
= BUS_I2C
;
563 input_dev
->open
= auo_pixcir_input_open
;
564 input_dev
->close
= auo_pixcir_input_close
;
566 __set_bit(EV_ABS
, input_dev
->evbit
);
567 __set_bit(EV_KEY
, input_dev
->evbit
);
569 __set_bit(BTN_TOUCH
, input_dev
->keybit
);
571 /* For single touch */
572 input_set_abs_params(input_dev
, ABS_X
, 0, pdata
->x_max
, 0, 0);
573 input_set_abs_params(input_dev
, ABS_Y
, 0, pdata
->y_max
, 0, 0);
575 /* For multi touch */
576 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0,
578 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0,
580 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
, 0,
581 AUO_PIXCIR_MAX_AREA
, 0, 0);
582 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MINOR
, 0,
583 AUO_PIXCIR_MAX_AREA
, 0, 0);
584 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
, 0, 1, 0, 0);
586 input_set_drvdata(ts
->input
, ts
);
588 error
= devm_gpio_request_one(&client
->dev
, pdata
->gpio_int
,
589 GPIOF_DIR_IN
, "auo_pixcir_ts_int");
591 dev_err(&client
->dev
, "request of gpio %d failed, %d\n",
592 pdata
->gpio_int
, error
);
596 error
= devm_gpio_request_one(&client
->dev
, pdata
->gpio_rst
,
597 GPIOF_DIR_OUT
| GPIOF_INIT_HIGH
,
598 "auo_pixcir_ts_rst");
600 dev_err(&client
->dev
, "request of gpio %d failed, %d\n",
601 pdata
->gpio_rst
, error
);
605 error
= devm_add_action_or_reset(&client
->dev
, auo_pixcir_reset
, ts
);
607 dev_err(&client
->dev
, "failed to register reset action, %d\n",
614 version
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_VERSION
);
620 dev_info(&client
->dev
, "firmware version 0x%X\n", version
);
622 error
= auo_pixcir_int_config(ts
, pdata
->int_setting
);
626 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
627 NULL
, auo_pixcir_interrupt
,
628 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
629 input_dev
->name
, ts
);
631 dev_err(&client
->dev
, "irq %d requested failed, %d\n",
636 /* stop device and put it into deep sleep until it is opened */
637 error
= auo_pixcir_stop(ts
);
641 error
= input_register_device(input_dev
);
643 dev_err(&client
->dev
, "could not register input device, %d\n",
648 i2c_set_clientdata(client
, ts
);
653 static const struct i2c_device_id auo_pixcir_idtable
[] = {
654 { "auo_pixcir_ts", 0 },
657 MODULE_DEVICE_TABLE(i2c
, auo_pixcir_idtable
);
660 static const struct of_device_id auo_pixcir_ts_dt_idtable
[] = {
661 { .compatible
= "auo,auo_pixcir_ts" },
664 MODULE_DEVICE_TABLE(of
, auo_pixcir_ts_dt_idtable
);
667 static struct i2c_driver auo_pixcir_driver
= {
669 .name
= "auo_pixcir_ts",
670 .pm
= &auo_pixcir_pm_ops
,
671 .of_match_table
= of_match_ptr(auo_pixcir_ts_dt_idtable
),
673 .probe
= auo_pixcir_probe
,
674 .id_table
= auo_pixcir_idtable
,
677 module_i2c_driver(auo_pixcir_driver
);
679 MODULE_DESCRIPTION("AUO-PIXCIR touchscreen driver");
680 MODULE_LICENSE("GPL v2");
681 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");