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>
36 * Coordinate calculation:
37 * X1 = X1_LSB + X1_MSB*256
38 * Y1 = Y1_LSB + Y1_MSB*256
39 * X2 = X2_LSB + X2_MSB*256
40 * Y2 = Y2_LSB + Y2_MSB*256
42 #define AUO_PIXCIR_REG_X1_LSB 0x00
43 #define AUO_PIXCIR_REG_X1_MSB 0x01
44 #define AUO_PIXCIR_REG_Y1_LSB 0x02
45 #define AUO_PIXCIR_REG_Y1_MSB 0x03
46 #define AUO_PIXCIR_REG_X2_LSB 0x04
47 #define AUO_PIXCIR_REG_X2_MSB 0x05
48 #define AUO_PIXCIR_REG_Y2_LSB 0x06
49 #define AUO_PIXCIR_REG_Y2_MSB 0x07
51 #define AUO_PIXCIR_REG_STRENGTH 0x0d
52 #define AUO_PIXCIR_REG_STRENGTH_X1_LSB 0x0e
53 #define AUO_PIXCIR_REG_STRENGTH_X1_MSB 0x0f
55 #define AUO_PIXCIR_REG_RAW_DATA_X 0x2b
56 #define AUO_PIXCIR_REG_RAW_DATA_Y 0x4f
58 #define AUO_PIXCIR_REG_X_SENSITIVITY 0x6f
59 #define AUO_PIXCIR_REG_Y_SENSITIVITY 0x70
60 #define AUO_PIXCIR_REG_INT_SETTING 0x71
61 #define AUO_PIXCIR_REG_INT_WIDTH 0x72
62 #define AUO_PIXCIR_REG_POWER_MODE 0x73
64 #define AUO_PIXCIR_REG_VERSION 0x77
65 #define AUO_PIXCIR_REG_CALIBRATE 0x78
67 #define AUO_PIXCIR_REG_TOUCHAREA_X1 0x1e
68 #define AUO_PIXCIR_REG_TOUCHAREA_Y1 0x1f
69 #define AUO_PIXCIR_REG_TOUCHAREA_X2 0x20
70 #define AUO_PIXCIR_REG_TOUCHAREA_Y2 0x21
72 #define AUO_PIXCIR_REG_EEPROM_CALIB_X 0x42
73 #define AUO_PIXCIR_REG_EEPROM_CALIB_Y 0xad
75 #define AUO_PIXCIR_INT_TPNUM_MASK 0xe0
76 #define AUO_PIXCIR_INT_TPNUM_SHIFT 5
77 #define AUO_PIXCIR_INT_RELEASE (1 << 4)
78 #define AUO_PIXCIR_INT_ENABLE (1 << 3)
79 #define AUO_PIXCIR_INT_POL_HIGH (1 << 2)
80 #define AUO_PIXCIR_INT_MODE_MASK 0x03
84 * active: scan speed 60Hz
85 * sleep: scan speed 10Hz can be auto-activated, wakeup on 1st touch
86 * deep sleep: scan speed 1Hz can only be entered or left manually.
88 #define AUO_PIXCIR_POWER_ACTIVE 0x00
89 #define AUO_PIXCIR_POWER_SLEEP 0x01
90 #define AUO_PIXCIR_POWER_DEEP_SLEEP 0x02
91 #define AUO_PIXCIR_POWER_MASK 0x03
93 #define AUO_PIXCIR_POWER_ALLOW_SLEEP (1 << 2)
94 #define AUO_PIXCIR_POWER_IDLE_TIME(ms) ((ms & 0xf) << 4)
96 #define AUO_PIXCIR_CALIBRATE 0x03
98 #define AUO_PIXCIR_EEPROM_CALIB_X_LEN 62
99 #define AUO_PIXCIR_EEPROM_CALIB_Y_LEN 36
101 #define AUO_PIXCIR_RAW_DATA_X_LEN 18
102 #define AUO_PIXCIR_RAW_DATA_Y_LEN 11
104 #define AUO_PIXCIR_STRENGTH_ENABLE (1 << 0)
106 /* Touchscreen absolute values */
107 #define AUO_PIXCIR_REPORT_POINTS 2
108 #define AUO_PIXCIR_MAX_AREA 0xff
109 #define AUO_PIXCIR_PENUP_TIMEOUT_MS 10
111 struct auo_pixcir_ts
{
112 struct i2c_client
*client
;
113 struct input_dev
*input
;
116 /* special handling for touch_indicate interupt mode */
119 wait_queue_head_t wait
;
131 static int auo_pixcir_collect_data(struct auo_pixcir_ts
*ts
,
132 struct auo_point_t
*point
)
134 struct i2c_client
*client
= ts
->client
;
135 const struct auo_pixcir_ts_platdata
*pdata
= client
->dev
.platform_data
;
136 uint8_t raw_coord
[8];
140 /* touch coordinates */
141 ret
= i2c_smbus_read_i2c_block_data(client
, AUO_PIXCIR_REG_X1_LSB
,
144 dev_err(&client
->dev
, "failed to read coordinate, %d\n", ret
);
149 ret
= i2c_smbus_read_i2c_block_data(client
, AUO_PIXCIR_REG_TOUCHAREA_X1
,
152 dev_err(&client
->dev
, "could not read touch area, %d\n", ret
);
156 for (i
= 0; i
< AUO_PIXCIR_REPORT_POINTS
; i
++) {
158 raw_coord
[4 * i
+ 1] << 8 | raw_coord
[4 * i
];
160 raw_coord
[4 * i
+ 3] << 8 | raw_coord
[4 * i
+ 2];
162 if (point
[i
].coord_x
> pdata
->x_max
||
163 point
[i
].coord_y
> pdata
->y_max
) {
164 dev_warn(&client
->dev
, "coordinates (%d,%d) invalid\n",
165 point
[i
].coord_x
, point
[i
].coord_y
);
166 point
[i
].coord_x
= point
[i
].coord_y
= 0;
169 /* determine touch major, minor and orientation */
170 point
[i
].area_major
= max(raw_area
[2 * i
], raw_area
[2 * i
+ 1]);
171 point
[i
].area_minor
= min(raw_area
[2 * i
], raw_area
[2 * i
+ 1]);
172 point
[i
].orientation
= raw_area
[2 * i
] > raw_area
[2 * i
+ 1];
178 static irqreturn_t
auo_pixcir_interrupt(int irq
, void *dev_id
)
180 struct auo_pixcir_ts
*ts
= dev_id
;
181 struct i2c_client
*client
= ts
->client
;
182 const struct auo_pixcir_ts_platdata
*pdata
= client
->dev
.platform_data
;
183 struct auo_point_t point
[AUO_PIXCIR_REPORT_POINTS
];
189 while (!ts
->stopped
) {
191 /* check for up event in touch touch_ind_mode */
192 if (ts
->touch_ind_mode
) {
193 if (gpio_get_value(pdata
->gpio_int
) == 0) {
194 input_mt_sync(ts
->input
);
195 input_report_key(ts
->input
, BTN_TOUCH
, 0);
196 input_sync(ts
->input
);
201 ret
= auo_pixcir_collect_data(ts
, point
);
203 /* we want to loop only in touch_ind_mode */
204 if (!ts
->touch_ind_mode
)
207 wait_event_timeout(ts
->wait
, ts
->stopped
,
208 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS
));
212 for (i
= 0; i
< AUO_PIXCIR_REPORT_POINTS
; i
++) {
213 if (point
[i
].coord_x
> 0 || point
[i
].coord_y
> 0) {
214 input_report_abs(ts
->input
, ABS_MT_POSITION_X
,
216 input_report_abs(ts
->input
, ABS_MT_POSITION_Y
,
218 input_report_abs(ts
->input
, ABS_MT_TOUCH_MAJOR
,
219 point
[i
].area_major
);
220 input_report_abs(ts
->input
, ABS_MT_TOUCH_MINOR
,
221 point
[i
].area_minor
);
222 input_report_abs(ts
->input
, ABS_MT_ORIENTATION
,
223 point
[i
].orientation
);
224 input_mt_sync(ts
->input
);
226 /* use first finger as source for singletouch */
230 /* number of touch points could also be queried
231 * via i2c but would require an additional call
237 input_report_key(ts
->input
, BTN_TOUCH
, fingers
> 0);
240 input_report_abs(ts
->input
, ABS_X
, point
[abs
].coord_x
);
241 input_report_abs(ts
->input
, ABS_Y
, point
[abs
].coord_y
);
244 input_sync(ts
->input
);
246 /* we want to loop only in touch_ind_mode */
247 if (!ts
->touch_ind_mode
)
250 wait_event_timeout(ts
->wait
, ts
->stopped
,
251 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS
));
258 * Set the power mode of the device.
260 * - AUO_PIXCIR_POWER_ACTIVE
261 * - AUO_PIXCIR_POWER_SLEEP - automatically left on first touch
262 * - AUO_PIXCIR_POWER_DEEP_SLEEP
264 static int auo_pixcir_power_mode(struct auo_pixcir_ts
*ts
, int mode
)
266 struct i2c_client
*client
= ts
->client
;
269 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_POWER_MODE
);
271 dev_err(&client
->dev
, "unable to read reg %Xh, %d\n",
272 AUO_PIXCIR_REG_POWER_MODE
, ret
);
276 ret
&= ~AUO_PIXCIR_POWER_MASK
;
279 ret
= i2c_smbus_write_byte_data(client
, AUO_PIXCIR_REG_POWER_MODE
, ret
);
281 dev_err(&client
->dev
, "unable to write reg %Xh, %d\n",
282 AUO_PIXCIR_REG_POWER_MODE
, ret
);
289 static __devinit
int auo_pixcir_int_config(struct auo_pixcir_ts
*ts
,
292 struct i2c_client
*client
= ts
->client
;
293 struct auo_pixcir_ts_platdata
*pdata
= client
->dev
.platform_data
;
296 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
);
298 dev_err(&client
->dev
, "unable to read reg %Xh, %d\n",
299 AUO_PIXCIR_REG_INT_SETTING
, ret
);
303 ret
&= ~AUO_PIXCIR_INT_MODE_MASK
;
305 ret
|= AUO_PIXCIR_INT_POL_HIGH
; /* always use high for interrupts */
307 ret
= i2c_smbus_write_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
,
310 dev_err(&client
->dev
, "unable to write reg %Xh, %d\n",
311 AUO_PIXCIR_REG_INT_SETTING
, ret
);
315 ts
->touch_ind_mode
= pdata
->int_setting
== AUO_PIXCIR_INT_TOUCH_IND
;
320 /* control the generation of interrupts on the device side */
321 static int auo_pixcir_int_toggle(struct auo_pixcir_ts
*ts
, bool enable
)
323 struct i2c_client
*client
= ts
->client
;
326 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
);
328 dev_err(&client
->dev
, "unable to read reg %Xh, %d\n",
329 AUO_PIXCIR_REG_INT_SETTING
, ret
);
334 ret
|= AUO_PIXCIR_INT_ENABLE
;
336 ret
&= ~AUO_PIXCIR_INT_ENABLE
;
338 ret
= i2c_smbus_write_byte_data(client
, AUO_PIXCIR_REG_INT_SETTING
,
341 dev_err(&client
->dev
, "unable to write reg %Xh, %d\n",
342 AUO_PIXCIR_REG_INT_SETTING
, ret
);
349 static int auo_pixcir_start(struct auo_pixcir_ts
*ts
)
351 struct i2c_client
*client
= ts
->client
;
354 ret
= auo_pixcir_power_mode(ts
, AUO_PIXCIR_POWER_ACTIVE
);
356 dev_err(&client
->dev
, "could not set power mode, %d\n",
363 enable_irq(client
->irq
);
365 ret
= auo_pixcir_int_toggle(ts
, 1);
367 dev_err(&client
->dev
, "could not enable interrupt, %d\n",
369 disable_irq(client
->irq
);
376 static int auo_pixcir_stop(struct auo_pixcir_ts
*ts
)
378 struct i2c_client
*client
= ts
->client
;
381 ret
= auo_pixcir_int_toggle(ts
, 0);
383 dev_err(&client
->dev
, "could not disable interrupt, %d\n",
388 /* disable receiving of interrupts */
389 disable_irq(client
->irq
);
394 return auo_pixcir_power_mode(ts
, AUO_PIXCIR_POWER_DEEP_SLEEP
);
397 static int auo_pixcir_input_open(struct input_dev
*dev
)
399 struct auo_pixcir_ts
*ts
= input_get_drvdata(dev
);
402 ret
= auo_pixcir_start(ts
);
409 static void auo_pixcir_input_close(struct input_dev
*dev
)
411 struct auo_pixcir_ts
*ts
= input_get_drvdata(dev
);
418 #ifdef CONFIG_PM_SLEEP
419 static int auo_pixcir_suspend(struct device
*dev
)
421 struct i2c_client
*client
= to_i2c_client(dev
);
422 struct auo_pixcir_ts
*ts
= i2c_get_clientdata(client
);
423 struct input_dev
*input
= ts
->input
;
426 mutex_lock(&input
->mutex
);
428 /* when configured as wakeup source, device should always wake system
429 * therefore start device if necessary
431 if (device_may_wakeup(&client
->dev
)) {
432 /* need to start device if not open, to be wakeup source */
434 ret
= auo_pixcir_start(ts
);
439 enable_irq_wake(client
->irq
);
440 ret
= auo_pixcir_power_mode(ts
, AUO_PIXCIR_POWER_SLEEP
);
441 } else if (input
->users
) {
442 ret
= auo_pixcir_stop(ts
);
446 mutex_unlock(&input
->mutex
);
451 static int auo_pixcir_resume(struct device
*dev
)
453 struct i2c_client
*client
= to_i2c_client(dev
);
454 struct auo_pixcir_ts
*ts
= i2c_get_clientdata(client
);
455 struct input_dev
*input
= ts
->input
;
458 mutex_lock(&input
->mutex
);
460 if (device_may_wakeup(&client
->dev
)) {
461 disable_irq_wake(client
->irq
);
463 /* need to stop device if it was not open on suspend */
465 ret
= auo_pixcir_stop(ts
);
470 /* device wakes automatically from SLEEP */
471 } else if (input
->users
) {
472 ret
= auo_pixcir_start(ts
);
476 mutex_unlock(&input
->mutex
);
482 static SIMPLE_DEV_PM_OPS(auo_pixcir_pm_ops
, auo_pixcir_suspend
,
485 static int __devinit
auo_pixcir_probe(struct i2c_client
*client
,
486 const struct i2c_device_id
*id
)
488 const struct auo_pixcir_ts_platdata
*pdata
= client
->dev
.platform_data
;
489 struct auo_pixcir_ts
*ts
;
490 struct input_dev
*input_dev
;
496 ts
= kzalloc(sizeof(struct auo_pixcir_ts
), GFP_KERNEL
);
500 ret
= gpio_request(pdata
->gpio_int
, "auo_pixcir_ts_int");
502 dev_err(&client
->dev
, "request of gpio %d failed, %d\n",
503 pdata
->gpio_int
, ret
);
508 pdata
->init_hw(client
);
511 ts
->touch_ind_mode
= 0;
512 init_waitqueue_head(&ts
->wait
);
514 snprintf(ts
->phys
, sizeof(ts
->phys
),
515 "%s/input0", dev_name(&client
->dev
));
517 input_dev
= input_allocate_device();
519 dev_err(&client
->dev
, "could not allocate input device\n");
520 goto err_input_alloc
;
523 ts
->input
= input_dev
;
525 input_dev
->name
= "AUO-Pixcir touchscreen";
526 input_dev
->phys
= ts
->phys
;
527 input_dev
->id
.bustype
= BUS_I2C
;
528 input_dev
->dev
.parent
= &client
->dev
;
530 input_dev
->open
= auo_pixcir_input_open
;
531 input_dev
->close
= auo_pixcir_input_close
;
533 __set_bit(EV_ABS
, input_dev
->evbit
);
534 __set_bit(EV_KEY
, input_dev
->evbit
);
536 __set_bit(BTN_TOUCH
, input_dev
->keybit
);
538 /* For single touch */
539 input_set_abs_params(input_dev
, ABS_X
, 0, pdata
->x_max
, 0, 0);
540 input_set_abs_params(input_dev
, ABS_Y
, 0, pdata
->y_max
, 0, 0);
542 /* For multi touch */
543 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0,
545 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0,
547 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
, 0,
548 AUO_PIXCIR_MAX_AREA
, 0, 0);
549 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MINOR
, 0,
550 AUO_PIXCIR_MAX_AREA
, 0, 0);
551 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
, 0, 1, 0, 0);
553 ret
= i2c_smbus_read_byte_data(client
, AUO_PIXCIR_REG_VERSION
);
556 dev_info(&client
->dev
, "firmware version 0x%X\n", ret
);
558 ret
= auo_pixcir_int_config(ts
, pdata
->int_setting
);
562 input_set_drvdata(ts
->input
, ts
);
565 ret
= request_threaded_irq(client
->irq
, NULL
, auo_pixcir_interrupt
,
566 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
567 input_dev
->name
, ts
);
569 dev_err(&client
->dev
, "irq %d requested failed\n", client
->irq
);
573 /* stop device and put it into deep sleep until it is opened */
574 ret
= auo_pixcir_stop(ts
);
576 goto err_input_register
;
578 ret
= input_register_device(input_dev
);
580 dev_err(&client
->dev
, "could not register input device\n");
581 goto err_input_register
;
584 i2c_set_clientdata(client
, ts
);
589 free_irq(client
->irq
, ts
);
591 input_free_device(input_dev
);
594 pdata
->exit_hw(client
);
595 gpio_free(pdata
->gpio_int
);
602 static int __devexit
auo_pixcir_remove(struct i2c_client
*client
)
604 struct auo_pixcir_ts
*ts
= i2c_get_clientdata(client
);
605 const struct auo_pixcir_ts_platdata
*pdata
= client
->dev
.platform_data
;
607 free_irq(client
->irq
, ts
);
609 input_unregister_device(ts
->input
);
612 pdata
->exit_hw(client
);
614 gpio_free(pdata
->gpio_int
);
621 static const struct i2c_device_id auo_pixcir_idtable
[] = {
622 { "auo_pixcir_ts", 0 },
625 MODULE_DEVICE_TABLE(i2c
, auo_pixcir_idtable
);
627 static struct i2c_driver auo_pixcir_driver
= {
629 .owner
= THIS_MODULE
,
630 .name
= "auo_pixcir_ts",
631 .pm
= &auo_pixcir_pm_ops
,
633 .probe
= auo_pixcir_probe
,
634 .remove
= __devexit_p(auo_pixcir_remove
),
635 .id_table
= auo_pixcir_idtable
,
638 static int __init
auo_pixcir_init(void)
640 return i2c_add_driver(&auo_pixcir_driver
);
642 module_init(auo_pixcir_init
);
644 static void __exit
auo_pixcir_exit(void)
646 i2c_del_driver(&auo_pixcir_driver
);
648 module_exit(auo_pixcir_exit
);
650 MODULE_DESCRIPTION("AUO-PIXCIR touchscreen driver");
651 MODULE_LICENSE("GPL v2");
652 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");