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>
24 #include <linux/of_gpio.h>
27 * Coordinate calculation:
28 * X1 = X1_LSB + X1_MSB*256
29 * Y1 = Y1_LSB + Y1_MSB*256
30 * X2 = X2_LSB + X2_MSB*256
31 * Y2 = Y2_LSB + Y2_MSB*256
33 #define AUO_PIXCIR_REG_X1_LSB 0x00
34 #define AUO_PIXCIR_REG_X1_MSB 0x01
35 #define AUO_PIXCIR_REG_Y1_LSB 0x02
36 #define AUO_PIXCIR_REG_Y1_MSB 0x03
37 #define AUO_PIXCIR_REG_X2_LSB 0x04
38 #define AUO_PIXCIR_REG_X2_MSB 0x05
39 #define AUO_PIXCIR_REG_Y2_LSB 0x06
40 #define AUO_PIXCIR_REG_Y2_MSB 0x07
42 #define AUO_PIXCIR_REG_STRENGTH 0x0d
43 #define AUO_PIXCIR_REG_STRENGTH_X1_LSB 0x0e
44 #define AUO_PIXCIR_REG_STRENGTH_X1_MSB 0x0f
46 #define AUO_PIXCIR_REG_RAW_DATA_X 0x2b
47 #define AUO_PIXCIR_REG_RAW_DATA_Y 0x4f
49 #define AUO_PIXCIR_REG_X_SENSITIVITY 0x6f
50 #define AUO_PIXCIR_REG_Y_SENSITIVITY 0x70
51 #define AUO_PIXCIR_REG_INT_SETTING 0x71
52 #define AUO_PIXCIR_REG_INT_WIDTH 0x72
53 #define AUO_PIXCIR_REG_POWER_MODE 0x73
55 #define AUO_PIXCIR_REG_VERSION 0x77
56 #define AUO_PIXCIR_REG_CALIBRATE 0x78
58 #define AUO_PIXCIR_REG_TOUCHAREA_X1 0x1e
59 #define AUO_PIXCIR_REG_TOUCHAREA_Y1 0x1f
60 #define AUO_PIXCIR_REG_TOUCHAREA_X2 0x20
61 #define AUO_PIXCIR_REG_TOUCHAREA_Y2 0x21
63 #define AUO_PIXCIR_REG_EEPROM_CALIB_X 0x42
64 #define AUO_PIXCIR_REG_EEPROM_CALIB_Y 0xad
66 #define AUO_PIXCIR_INT_TPNUM_MASK 0xe0
67 #define AUO_PIXCIR_INT_TPNUM_SHIFT 5
68 #define AUO_PIXCIR_INT_RELEASE (1 << 4)
69 #define AUO_PIXCIR_INT_ENABLE (1 << 3)
70 #define AUO_PIXCIR_INT_POL_HIGH (1 << 2)
74 * periodical: interrupt is asserted periodicaly
75 * compare coordinates: interrupt is asserted when coordinates change
76 * indicate touch: interrupt is asserted during touch
78 #define AUO_PIXCIR_INT_PERIODICAL 0x00
79 #define AUO_PIXCIR_INT_COMP_COORD 0x01
80 #define AUO_PIXCIR_INT_TOUCH_IND 0x02
81 #define AUO_PIXCIR_INT_MODE_MASK 0x03
85 * active: scan speed 60Hz
86 * sleep: scan speed 10Hz can be auto-activated, wakeup on 1st touch
87 * deep sleep: scan speed 1Hz can only be entered or left manually.
89 #define AUO_PIXCIR_POWER_ACTIVE 0x00
90 #define AUO_PIXCIR_POWER_SLEEP 0x01
91 #define AUO_PIXCIR_POWER_DEEP_SLEEP 0x02
92 #define AUO_PIXCIR_POWER_MASK 0x03
94 #define AUO_PIXCIR_POWER_ALLOW_SLEEP (1 << 2)
95 #define AUO_PIXCIR_POWER_IDLE_TIME(ms) ((ms & 0xf) << 4)
97 #define AUO_PIXCIR_CALIBRATE 0x03
99 #define AUO_PIXCIR_EEPROM_CALIB_X_LEN 62
100 #define AUO_PIXCIR_EEPROM_CALIB_Y_LEN 36
102 #define AUO_PIXCIR_RAW_DATA_X_LEN 18
103 #define AUO_PIXCIR_RAW_DATA_Y_LEN 11
105 #define AUO_PIXCIR_STRENGTH_ENABLE (1 << 0)
107 /* Touchscreen absolute values */
108 #define AUO_PIXCIR_REPORT_POINTS 2
109 #define AUO_PIXCIR_MAX_AREA 0xff
110 #define AUO_PIXCIR_PENUP_TIMEOUT_MS 10
112 struct auo_pixcir_ts {
113 struct i2c_client *client;
114 struct input_dev *input;
122 /* special handling for touch_indicate interrupt mode */
125 wait_queue_head_t wait;
137 static int auo_pixcir_collect_data(struct auo_pixcir_ts *ts,
138 struct auo_point_t *point)
140 struct i2c_client *client = ts->client;
141 uint8_t raw_coord[8];
145 /* touch coordinates */
146 ret = i2c_smbus_read_i2c_block_data(client, AUO_PIXCIR_REG_X1_LSB,
149 dev_err(&client->dev, "failed to read coordinate, %d\n", ret);
154 ret = i2c_smbus_read_i2c_block_data(client, AUO_PIXCIR_REG_TOUCHAREA_X1,
157 dev_err(&client->dev, "could not read touch area, %d\n", ret);
161 for (i = 0; i < AUO_PIXCIR_REPORT_POINTS; i++) {
163 raw_coord[4 * i + 1] << 8 | raw_coord[4 * i];
165 raw_coord[4 * i + 3] << 8 | raw_coord[4 * i + 2];
167 if (point[i].coord_x > ts->x_max ||
168 point[i].coord_y > ts->y_max) {
169 dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
170 point[i].coord_x, point[i].coord_y);
171 point[i].coord_x = point[i].coord_y = 0;
174 /* determine touch major, minor and orientation */
175 point[i].area_major = max(raw_area[2 * i], raw_area[2 * i + 1]);
176 point[i].area_minor = min(raw_area[2 * i], raw_area[2 * i + 1]);
177 point[i].orientation = raw_area[2 * i] > raw_area[2 * i + 1];
183 static irqreturn_t auo_pixcir_interrupt(int irq, void *dev_id)
185 struct auo_pixcir_ts *ts = dev_id;
186 struct auo_point_t point[AUO_PIXCIR_REPORT_POINTS];
192 while (!ts->stopped) {
194 /* check for up event in touch touch_ind_mode */
195 if (ts->touch_ind_mode) {
196 if (gpio_get_value(ts->gpio_int) == 0) {
197 input_mt_sync(ts->input);
198 input_report_key(ts->input, BTN_TOUCH, 0);
199 input_sync(ts->input);
204 ret = auo_pixcir_collect_data(ts, point);
206 /* we want to loop only in touch_ind_mode */
207 if (!ts->touch_ind_mode)
210 wait_event_timeout(ts->wait, ts->stopped,
211 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS));
215 for (i = 0; i < AUO_PIXCIR_REPORT_POINTS; i++) {
216 if (point[i].coord_x > 0 || point[i].coord_y > 0) {
217 input_report_abs(ts->input, ABS_MT_POSITION_X,
219 input_report_abs(ts->input, ABS_MT_POSITION_Y,
221 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
222 point[i].area_major);
223 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
224 point[i].area_minor);
225 input_report_abs(ts->input, ABS_MT_ORIENTATION,
226 point[i].orientation);
227 input_mt_sync(ts->input);
229 /* use first finger as source for singletouch */
233 /* number of touch points could also be queried
234 * via i2c but would require an additional call
240 input_report_key(ts->input, BTN_TOUCH, fingers > 0);
243 input_report_abs(ts->input, ABS_X, point[abs].coord_x);
244 input_report_abs(ts->input, ABS_Y, point[abs].coord_y);
247 input_sync(ts->input);
249 /* we want to loop only in touch_ind_mode */
250 if (!ts->touch_ind_mode)
253 wait_event_timeout(ts->wait, ts->stopped,
254 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS));
261 * Set the power mode of the device.
263 * - AUO_PIXCIR_POWER_ACTIVE
264 * - AUO_PIXCIR_POWER_SLEEP - automatically left on first touch
265 * - AUO_PIXCIR_POWER_DEEP_SLEEP
267 static int auo_pixcir_power_mode(struct auo_pixcir_ts *ts, int mode)
269 struct i2c_client *client = ts->client;
272 ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_POWER_MODE);
274 dev_err(&client->dev, "unable to read reg %Xh, %d\n",
275 AUO_PIXCIR_REG_POWER_MODE, ret);
279 ret &= ~AUO_PIXCIR_POWER_MASK;
282 ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_POWER_MODE, ret);
284 dev_err(&client->dev, "unable to write reg %Xh, %d\n",
285 AUO_PIXCIR_REG_POWER_MODE, ret);
292 static int auo_pixcir_int_config(struct auo_pixcir_ts *ts, int int_setting)
294 struct i2c_client *client = ts->client;
297 ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_INT_SETTING);
299 dev_err(&client->dev, "unable to read reg %Xh, %d\n",
300 AUO_PIXCIR_REG_INT_SETTING, ret);
304 ret &= ~AUO_PIXCIR_INT_MODE_MASK;
306 ret |= AUO_PIXCIR_INT_POL_HIGH; /* always use high for interrupts */
308 ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_INT_SETTING,
311 dev_err(&client->dev, "unable to write reg %Xh, %d\n",
312 AUO_PIXCIR_REG_INT_SETTING, ret);
316 ts->touch_ind_mode = int_setting == AUO_PIXCIR_INT_TOUCH_IND;
321 /* control the generation of interrupts on the device side */
322 static int auo_pixcir_int_toggle(struct auo_pixcir_ts *ts, bool enable)
324 struct i2c_client *client = ts->client;
327 ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_INT_SETTING);
329 dev_err(&client->dev, "unable to read reg %Xh, %d\n",
330 AUO_PIXCIR_REG_INT_SETTING, ret);
335 ret |= AUO_PIXCIR_INT_ENABLE;
337 ret &= ~AUO_PIXCIR_INT_ENABLE;
339 ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_INT_SETTING,
342 dev_err(&client->dev, "unable to write reg %Xh, %d\n",
343 AUO_PIXCIR_REG_INT_SETTING, ret);
350 static int auo_pixcir_start(struct auo_pixcir_ts *ts)
352 struct i2c_client *client = ts->client;
355 ret = auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_ACTIVE);
357 dev_err(&client->dev, "could not set power mode, %d\n",
364 enable_irq(client->irq);
366 ret = auo_pixcir_int_toggle(ts, 1);
368 dev_err(&client->dev, "could not enable interrupt, %d\n",
370 disable_irq(client->irq);
377 static int auo_pixcir_stop(struct auo_pixcir_ts *ts)
379 struct i2c_client *client = ts->client;
382 ret = auo_pixcir_int_toggle(ts, 0);
384 dev_err(&client->dev, "could not disable interrupt, %d\n",
389 /* disable receiving of interrupts */
390 disable_irq(client->irq);
395 return auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_DEEP_SLEEP);
398 static int auo_pixcir_input_open(struct input_dev *dev)
400 struct auo_pixcir_ts *ts = input_get_drvdata(dev);
402 return auo_pixcir_start(ts);
405 static void auo_pixcir_input_close(struct input_dev *dev)
407 struct auo_pixcir_ts *ts = input_get_drvdata(dev);
412 static int __maybe_unused auo_pixcir_suspend(struct device *dev)
414 struct i2c_client *client = to_i2c_client(dev);
415 struct auo_pixcir_ts *ts = i2c_get_clientdata(client);
416 struct input_dev *input = ts->input;
419 mutex_lock(&input->mutex);
421 /* when configured as wakeup source, device should always wake system
422 * therefore start device if necessary
424 if (device_may_wakeup(&client->dev)) {
425 /* need to start device if not open, to be wakeup source */
426 if (!input_device_enabled(input)) {
427 ret = auo_pixcir_start(ts);
432 enable_irq_wake(client->irq);
433 ret = auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_SLEEP);
434 } else if (input_device_enabled(input)) {
435 ret = auo_pixcir_stop(ts);
439 mutex_unlock(&input->mutex);
444 static int __maybe_unused auo_pixcir_resume(struct device *dev)
446 struct i2c_client *client = to_i2c_client(dev);
447 struct auo_pixcir_ts *ts = i2c_get_clientdata(client);
448 struct input_dev *input = ts->input;
451 mutex_lock(&input->mutex);
453 if (device_may_wakeup(&client->dev)) {
454 disable_irq_wake(client->irq);
456 /* need to stop device if it was not open on suspend */
457 if (!input_device_enabled(input)) {
458 ret = auo_pixcir_stop(ts);
463 /* device wakes automatically from SLEEP */
464 } else if (input_device_enabled(input)) {
465 ret = auo_pixcir_start(ts);
469 mutex_unlock(&input->mutex);
474 static SIMPLE_DEV_PM_OPS(auo_pixcir_pm_ops,
475 auo_pixcir_suspend, auo_pixcir_resume);
478 static int auo_pixcir_parse_dt(struct device *dev, struct auo_pixcir_ts *ts)
480 struct device_node *np = dev->of_node;
485 ts->gpio_int = of_get_gpio(np, 0);
486 if (!gpio_is_valid(ts->gpio_int)) {
487 dev_err(dev, "failed to get interrupt gpio\n");
491 ts->gpio_rst = of_get_gpio(np, 1);
492 if (!gpio_is_valid(ts->gpio_rst)) {
493 dev_err(dev, "failed to get reset gpio\n");
497 if (of_property_read_u32(np, "x-size", &ts->x_max)) {
498 dev_err(dev, "failed to get x-size property\n");
502 if (of_property_read_u32(np, "y-size", &ts->y_max)) {
503 dev_err(dev, "failed to get y-size property\n");
510 static int auo_pixcir_parse_dt(struct device *dev, struct auo_pixcir_ts *ts)
516 static void auo_pixcir_reset(void *data)
518 struct auo_pixcir_ts *ts = data;
520 gpio_set_value(ts->gpio_rst, 0);
523 static int auo_pixcir_probe(struct i2c_client *client,
524 const struct i2c_device_id *id)
526 struct auo_pixcir_ts *ts;
527 struct input_dev *input_dev;
531 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
535 input_dev = devm_input_allocate_device(&client->dev);
537 dev_err(&client->dev, "could not allocate input device\n");
542 ts->input = input_dev;
543 ts->touch_ind_mode = 0;
545 init_waitqueue_head(&ts->wait);
547 snprintf(ts->phys, sizeof(ts->phys),
548 "%s/input0", dev_name(&client->dev));
550 error = auo_pixcir_parse_dt(&client->dev, ts);
554 input_dev->name = "AUO-Pixcir touchscreen";
555 input_dev->phys = ts->phys;
556 input_dev->id.bustype = BUS_I2C;
558 input_dev->open = auo_pixcir_input_open;
559 input_dev->close = auo_pixcir_input_close;
561 __set_bit(EV_ABS, input_dev->evbit);
562 __set_bit(EV_KEY, input_dev->evbit);
564 __set_bit(BTN_TOUCH, input_dev->keybit);
566 /* For single touch */
567 input_set_abs_params(input_dev, ABS_X, 0, ts->x_max, 0, 0);
568 input_set_abs_params(input_dev, ABS_Y, 0, ts->y_max, 0, 0);
570 /* For multi touch */
571 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0);
572 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0);
573 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
574 0, AUO_PIXCIR_MAX_AREA, 0, 0);
575 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
576 0, AUO_PIXCIR_MAX_AREA, 0, 0);
577 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
579 input_set_drvdata(ts->input, ts);
581 error = devm_gpio_request_one(&client->dev, ts->gpio_int,
582 GPIOF_DIR_IN, "auo_pixcir_ts_int");
584 dev_err(&client->dev, "request of gpio %d failed, %d\n",
585 ts->gpio_int, error);
589 error = devm_gpio_request_one(&client->dev, ts->gpio_rst,
590 GPIOF_DIR_OUT | GPIOF_INIT_HIGH,
591 "auo_pixcir_ts_rst");
593 dev_err(&client->dev, "request of gpio %d failed, %d\n",
594 ts->gpio_rst, error);
598 error = devm_add_action_or_reset(&client->dev, auo_pixcir_reset, ts);
600 dev_err(&client->dev, "failed to register reset action, %d\n",
607 version = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_VERSION);
613 dev_info(&client->dev, "firmware version 0x%X\n", version);
615 /* default to asserting the interrupt when the screen is touched */
616 error = auo_pixcir_int_config(ts, AUO_PIXCIR_INT_TOUCH_IND);
620 error = devm_request_threaded_irq(&client->dev, client->irq,
621 NULL, auo_pixcir_interrupt,
622 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
623 input_dev->name, ts);
625 dev_err(&client->dev, "irq %d requested failed, %d\n",
630 /* stop device and put it into deep sleep until it is opened */
631 error = auo_pixcir_stop(ts);
635 error = input_register_device(input_dev);
637 dev_err(&client->dev, "could not register input device, %d\n",
642 i2c_set_clientdata(client, ts);
647 static const struct i2c_device_id auo_pixcir_idtable[] = {
648 { "auo_pixcir_ts", 0 },
651 MODULE_DEVICE_TABLE(i2c, auo_pixcir_idtable);
654 static const struct of_device_id auo_pixcir_ts_dt_idtable[] = {
655 { .compatible = "auo,auo_pixcir_ts" },
658 MODULE_DEVICE_TABLE(of, auo_pixcir_ts_dt_idtable);
661 static struct i2c_driver auo_pixcir_driver = {
663 .name = "auo_pixcir_ts",
664 .pm = &auo_pixcir_pm_ops,
665 .of_match_table = of_match_ptr(auo_pixcir_ts_dt_idtable),
667 .probe = auo_pixcir_probe,
668 .id_table = auo_pixcir_idtable,
671 module_i2c_driver(auo_pixcir_driver);
673 MODULE_DESCRIPTION("AUO-PIXCIR touchscreen driver");
674 MODULE_LICENSE("GPL v2");
675 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");