2 * Driver for AUO in-cell touchscreens
4 * Copyright (c) 2011 Heiko Stuebner <heiko@sntech.de>
6 * loosely based on auo_touch.c from Dell Streak vendor-kernel
8 * Copyright (c) 2008 QUALCOMM Incorporated.
9 * Copyright (c) 2008 QUALCOMM USA, INC.
12 * This software is licensed under the terms of the GNU General Public
13 * License version 2, as published by the Free Software Foundation, and
14 * may be copied, distributed, and modified under those terms.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
27 #include <linux/input.h>
28 #include <linux/jiffies.h>
29 #include <linux/i2c.h>
30 #include <linux/mutex.h>
31 #include <linux/delay.h>
32 #include <linux/gpio.h>
33 #include <linux/input/auo-pixcir-ts.h>
35 #include <linux/of_gpio.h>
38 * Coordinate calculation:
39 * X1 = X1_LSB + X1_MSB*256
40 * Y1 = Y1_LSB + Y1_MSB*256
41 * X2 = X2_LSB + X2_MSB*256
42 * Y2 = Y2_LSB + Y2_MSB*256
44 #define AUO_PIXCIR_REG_X1_LSB 0x00
45 #define AUO_PIXCIR_REG_X1_MSB 0x01
46 #define AUO_PIXCIR_REG_Y1_LSB 0x02
47 #define AUO_PIXCIR_REG_Y1_MSB 0x03
48 #define AUO_PIXCIR_REG_X2_LSB 0x04
49 #define AUO_PIXCIR_REG_X2_MSB 0x05
50 #define AUO_PIXCIR_REG_Y2_LSB 0x06
51 #define AUO_PIXCIR_REG_Y2_MSB 0x07
53 #define AUO_PIXCIR_REG_STRENGTH 0x0d
54 #define AUO_PIXCIR_REG_STRENGTH_X1_LSB 0x0e
55 #define AUO_PIXCIR_REG_STRENGTH_X1_MSB 0x0f
57 #define AUO_PIXCIR_REG_RAW_DATA_X 0x2b
58 #define AUO_PIXCIR_REG_RAW_DATA_Y 0x4f
60 #define AUO_PIXCIR_REG_X_SENSITIVITY 0x6f
61 #define AUO_PIXCIR_REG_Y_SENSITIVITY 0x70
62 #define AUO_PIXCIR_REG_INT_SETTING 0x71
63 #define AUO_PIXCIR_REG_INT_WIDTH 0x72
64 #define AUO_PIXCIR_REG_POWER_MODE 0x73
66 #define AUO_PIXCIR_REG_VERSION 0x77
67 #define AUO_PIXCIR_REG_CALIBRATE 0x78
69 #define AUO_PIXCIR_REG_TOUCHAREA_X1 0x1e
70 #define AUO_PIXCIR_REG_TOUCHAREA_Y1 0x1f
71 #define AUO_PIXCIR_REG_TOUCHAREA_X2 0x20
72 #define AUO_PIXCIR_REG_TOUCHAREA_Y2 0x21
74 #define AUO_PIXCIR_REG_EEPROM_CALIB_X 0x42
75 #define AUO_PIXCIR_REG_EEPROM_CALIB_Y 0xad
77 #define AUO_PIXCIR_INT_TPNUM_MASK 0xe0
78 #define AUO_PIXCIR_INT_TPNUM_SHIFT 5
79 #define AUO_PIXCIR_INT_RELEASE (1 << 4)
80 #define AUO_PIXCIR_INT_ENABLE (1 << 3)
81 #define AUO_PIXCIR_INT_POL_HIGH (1 << 2)
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 const struct auo_pixcir_ts_platdata
*pdata
;
119 /* special handling for touch_indicate interupt mode */
122 wait_queue_head_t wait
;
134 static int auo_pixcir_collect_data(struct auo_pixcir_ts
*ts
,
135 struct auo_point_t
*point
)
137 struct i2c_client
*client
= ts
->client
;
138 const struct auo_pixcir_ts_platdata
*pdata
= ts
->pdata
;
139 uint8_t raw_coord
[8];
143 /* touch coordinates */
144 ret
= i2c_smbus_read_i2c_block_data(client
, AUO_PIXCIR_REG_X1_LSB
,
147 dev_err(&client
->dev
, "failed to read coordinate, %d\n", ret
);
152 ret
= i2c_smbus_read_i2c_block_data(client
, AUO_PIXCIR_REG_TOUCHAREA_X1
,
155 dev_err(&client
->dev
, "could not read touch area, %d\n", ret
);
159 for (i
= 0; i
< AUO_PIXCIR_REPORT_POINTS
; i
++) {
161 raw_coord
[4 * i
+ 1] << 8 | raw_coord
[4 * i
];
163 raw_coord
[4 * i
+ 3] << 8 | raw_coord
[4 * i
+ 2];
165 if (point
[i
].coord_x
> pdata
->x_max
||
166 point
[i
].coord_y
> pdata
->y_max
) {
167 dev_warn(&client
->dev
, "coordinates (%d,%d) invalid\n",
168 point
[i
].coord_x
, point
[i
].coord_y
);
169 point
[i
].coord_x
= point
[i
].coord_y
= 0;
172 /* determine touch major, minor and orientation */
173 point
[i
].area_major
= max(raw_area
[2 * i
], raw_area
[2 * i
+ 1]);
174 point
[i
].area_minor
= min(raw_area
[2 * i
], raw_area
[2 * i
+ 1]);
175 point
[i
].orientation
= raw_area
[2 * i
] > raw_area
[2 * i
+ 1];
181 static irqreturn_t
auo_pixcir_interrupt(int irq
, void *dev_id
)
183 struct auo_pixcir_ts
*ts
= dev_id
;
184 const struct auo_pixcir_ts_platdata
*pdata
= ts
->pdata
;
185 struct auo_point_t point
[AUO_PIXCIR_REPORT_POINTS
];
191 while (!ts
->stopped
) {
193 /* check for up event in touch touch_ind_mode */
194 if (ts
->touch_ind_mode
) {
195 if (gpio_get_value(pdata
->gpio_int
) == 0) {
196 input_mt_sync(ts
->input
);
197 input_report_key(ts
->input
, BTN_TOUCH
, 0);
198 input_sync(ts
->input
);
203 ret
= auo_pixcir_collect_data(ts
, point
);
205 /* we want to loop only in touch_ind_mode */
206 if (!ts
->touch_ind_mode
)
209 wait_event_timeout(ts
->wait
, ts
->stopped
,
210 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS
));
214 for (i
= 0; i
< AUO_PIXCIR_REPORT_POINTS
; i
++) {
215 if (point
[i
].coord_x
> 0 || point
[i
].coord_y
> 0) {
216 input_report_abs(ts
->input
, ABS_MT_POSITION_X
,
218 input_report_abs(ts
->input
, ABS_MT_POSITION_Y
,
220 input_report_abs(ts
->input
, ABS_MT_TOUCH_MAJOR
,
221 point
[i
].area_major
);
222 input_report_abs(ts
->input
, ABS_MT_TOUCH_MINOR
,
223 point
[i
].area_minor
);
224 input_report_abs(ts
->input
, ABS_MT_ORIENTATION
,
225 point
[i
].orientation
);
226 input_mt_sync(ts
->input
);
228 /* use first finger as source for singletouch */
232 /* number of touch points could also be queried
233 * via i2c but would require an additional call
239 input_report_key(ts
->input
, BTN_TOUCH
, fingers
> 0);
242 input_report_abs(ts
->input
, ABS_X
, point
[abs
].coord_x
);
243 input_report_abs(ts
->input
, ABS_Y
, point
[abs
].coord_y
);
246 input_sync(ts
->input
);
248 /* we want to loop only in touch_ind_mode */
249 if (!ts
->touch_ind_mode
)
252 wait_event_timeout(ts
->wait
, ts
->stopped
,
253 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS
));
260 * Set the power mode of the device.
262 * - AUO_PIXCIR_POWER_ACTIVE
263 * - AUO_PIXCIR_POWER_SLEEP - automatically left on first touch
264 * - AUO_PIXCIR_POWER_DEEP_SLEEP
266 static int auo_pixcir_power_mode(struct auo_pixcir_ts
*ts
, int mode
)
268 struct i2c_client
*client
= ts
->client
;
271 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_POWER_MODE
);
273 dev_err(&client
->dev
, "unable to read reg %Xh, %d\n",
274 AUO_PIXCIR_REG_POWER_MODE
, ret
);
278 ret
&= ~AUO_PIXCIR_POWER_MASK
;
281 ret
= i2c_smbus_write_byte_data(client
, AUO_PIXCIR_REG_POWER_MODE
, ret
);
283 dev_err(&client
->dev
, "unable to write reg %Xh, %d\n",
284 AUO_PIXCIR_REG_POWER_MODE
, ret
);
291 static int auo_pixcir_int_config(struct auo_pixcir_ts
*ts
,
294 struct i2c_client
*client
= ts
->client
;
295 const struct auo_pixcir_ts_platdata
*pdata
= ts
->pdata
;
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
= pdata
->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
);
404 ret
= auo_pixcir_start(ts
);
411 static void auo_pixcir_input_close(struct input_dev
*dev
)
413 struct auo_pixcir_ts
*ts
= input_get_drvdata(dev
);
420 static int __maybe_unused
auo_pixcir_suspend(struct device
*dev
)
422 struct i2c_client
*client
= to_i2c_client(dev
);
423 struct auo_pixcir_ts
*ts
= i2c_get_clientdata(client
);
424 struct input_dev
*input
= ts
->input
;
427 mutex_lock(&input
->mutex
);
429 /* when configured as wakeup source, device should always wake system
430 * therefore start device if necessary
432 if (device_may_wakeup(&client
->dev
)) {
433 /* need to start device if not open, to be wakeup source */
435 ret
= auo_pixcir_start(ts
);
440 enable_irq_wake(client
->irq
);
441 ret
= auo_pixcir_power_mode(ts
, AUO_PIXCIR_POWER_SLEEP
);
442 } else if (input
->users
) {
443 ret
= auo_pixcir_stop(ts
);
447 mutex_unlock(&input
->mutex
);
452 static int __maybe_unused
auo_pixcir_resume(struct device
*dev
)
454 struct i2c_client
*client
= to_i2c_client(dev
);
455 struct auo_pixcir_ts
*ts
= i2c_get_clientdata(client
);
456 struct input_dev
*input
= ts
->input
;
459 mutex_lock(&input
->mutex
);
461 if (device_may_wakeup(&client
->dev
)) {
462 disable_irq_wake(client
->irq
);
464 /* need to stop device if it was not open on suspend */
466 ret
= auo_pixcir_stop(ts
);
471 /* device wakes automatically from SLEEP */
472 } else if (input
->users
) {
473 ret
= auo_pixcir_start(ts
);
477 mutex_unlock(&input
->mutex
);
482 static SIMPLE_DEV_PM_OPS(auo_pixcir_pm_ops
,
483 auo_pixcir_suspend
, auo_pixcir_resume
);
486 static struct auo_pixcir_ts_platdata
*auo_pixcir_parse_dt(struct device
*dev
)
488 struct auo_pixcir_ts_platdata
*pdata
;
489 struct device_node
*np
= dev
->of_node
;
492 return ERR_PTR(-ENOENT
);
494 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
496 dev_err(dev
, "failed to allocate platform data\n");
497 return ERR_PTR(-ENOMEM
);
500 pdata
->gpio_int
= of_get_gpio(np
, 0);
501 if (!gpio_is_valid(pdata
->gpio_int
)) {
502 dev_err(dev
, "failed to get interrupt gpio\n");
503 return ERR_PTR(-EINVAL
);
506 pdata
->gpio_rst
= of_get_gpio(np
, 1);
507 if (!gpio_is_valid(pdata
->gpio_rst
)) {
508 dev_err(dev
, "failed to get reset gpio\n");
509 return ERR_PTR(-EINVAL
);
512 if (of_property_read_u32(np
, "x-size", &pdata
->x_max
)) {
513 dev_err(dev
, "failed to get x-size property\n");
514 return ERR_PTR(-EINVAL
);
517 if (of_property_read_u32(np
, "y-size", &pdata
->y_max
)) {
518 dev_err(dev
, "failed to get y-size property\n");
519 return ERR_PTR(-EINVAL
);
522 /* default to asserting the interrupt when the screen is touched */
523 pdata
->int_setting
= AUO_PIXCIR_INT_TOUCH_IND
;
528 static struct auo_pixcir_ts_platdata
*auo_pixcir_parse_dt(struct device
*dev
)
530 return ERR_PTR(-EINVAL
);
534 static void auo_pixcir_reset(void *data
)
536 struct auo_pixcir_ts
*ts
= data
;
538 gpio_set_value(ts
->pdata
->gpio_rst
, 0);
541 static int auo_pixcir_probe(struct i2c_client
*client
,
542 const struct i2c_device_id
*id
)
544 const struct auo_pixcir_ts_platdata
*pdata
;
545 struct auo_pixcir_ts
*ts
;
546 struct input_dev
*input_dev
;
550 pdata
= dev_get_platdata(&client
->dev
);
552 pdata
= auo_pixcir_parse_dt(&client
->dev
);
554 return PTR_ERR(pdata
);
557 ts
= devm_kzalloc(&client
->dev
,
558 sizeof(struct auo_pixcir_ts
), GFP_KERNEL
);
562 input_dev
= devm_input_allocate_device(&client
->dev
);
564 dev_err(&client
->dev
, "could not allocate input device\n");
570 ts
->input
= input_dev
;
571 ts
->touch_ind_mode
= 0;
573 init_waitqueue_head(&ts
->wait
);
575 snprintf(ts
->phys
, sizeof(ts
->phys
),
576 "%s/input0", dev_name(&client
->dev
));
578 input_dev
->name
= "AUO-Pixcir touchscreen";
579 input_dev
->phys
= ts
->phys
;
580 input_dev
->id
.bustype
= BUS_I2C
;
582 input_dev
->open
= auo_pixcir_input_open
;
583 input_dev
->close
= auo_pixcir_input_close
;
585 __set_bit(EV_ABS
, input_dev
->evbit
);
586 __set_bit(EV_KEY
, input_dev
->evbit
);
588 __set_bit(BTN_TOUCH
, input_dev
->keybit
);
590 /* For single touch */
591 input_set_abs_params(input_dev
, ABS_X
, 0, pdata
->x_max
, 0, 0);
592 input_set_abs_params(input_dev
, ABS_Y
, 0, pdata
->y_max
, 0, 0);
594 /* For multi touch */
595 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0,
597 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0,
599 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
, 0,
600 AUO_PIXCIR_MAX_AREA
, 0, 0);
601 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MINOR
, 0,
602 AUO_PIXCIR_MAX_AREA
, 0, 0);
603 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
, 0, 1, 0, 0);
605 input_set_drvdata(ts
->input
, ts
);
607 error
= devm_gpio_request_one(&client
->dev
, pdata
->gpio_int
,
608 GPIOF_DIR_IN
, "auo_pixcir_ts_int");
610 dev_err(&client
->dev
, "request of gpio %d failed, %d\n",
611 pdata
->gpio_int
, error
);
615 error
= devm_gpio_request_one(&client
->dev
, pdata
->gpio_rst
,
616 GPIOF_DIR_OUT
| GPIOF_INIT_HIGH
,
617 "auo_pixcir_ts_rst");
619 dev_err(&client
->dev
, "request of gpio %d failed, %d\n",
620 pdata
->gpio_rst
, error
);
624 error
= devm_add_action(&client
->dev
, auo_pixcir_reset
, ts
);
626 auo_pixcir_reset(ts
);
627 dev_err(&client
->dev
, "failed to register reset action, %d\n",
634 version
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_VERSION
);
640 dev_info(&client
->dev
, "firmware version 0x%X\n", version
);
642 error
= auo_pixcir_int_config(ts
, pdata
->int_setting
);
646 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
647 NULL
, auo_pixcir_interrupt
,
648 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
649 input_dev
->name
, ts
);
651 dev_err(&client
->dev
, "irq %d requested failed, %d\n",
656 /* stop device and put it into deep sleep until it is opened */
657 error
= auo_pixcir_stop(ts
);
661 error
= input_register_device(input_dev
);
663 dev_err(&client
->dev
, "could not register input device, %d\n",
668 i2c_set_clientdata(client
, ts
);
673 static const struct i2c_device_id auo_pixcir_idtable
[] = {
674 { "auo_pixcir_ts", 0 },
677 MODULE_DEVICE_TABLE(i2c
, auo_pixcir_idtable
);
680 static const struct of_device_id auo_pixcir_ts_dt_idtable
[] = {
681 { .compatible
= "auo,auo_pixcir_ts" },
684 MODULE_DEVICE_TABLE(of
, auo_pixcir_ts_dt_idtable
);
687 static struct i2c_driver auo_pixcir_driver
= {
689 .owner
= THIS_MODULE
,
690 .name
= "auo_pixcir_ts",
691 .pm
= &auo_pixcir_pm_ops
,
692 .of_match_table
= of_match_ptr(auo_pixcir_ts_dt_idtable
),
694 .probe
= auo_pixcir_probe
,
695 .id_table
= auo_pixcir_idtable
,
698 module_i2c_driver(auo_pixcir_driver
);
700 MODULE_DESCRIPTION("AUO-PIXCIR touchscreen driver");
701 MODULE_LICENSE("GPL v2");
702 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");