xref: /linux/drivers/input/touchscreen/auo-pixcir-ts.c (revision 9ea370f3416ecc4b22d49b24e2c7fdc9c9ba3a0e)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25245db49SHeiko Stübner /*
35245db49SHeiko Stübner  * Driver for AUO in-cell touchscreens
45245db49SHeiko Stübner  *
55245db49SHeiko Stübner  * Copyright (c) 2011 Heiko Stuebner <heiko@sntech.de>
65245db49SHeiko Stübner  *
75245db49SHeiko Stübner  * loosely based on auo_touch.c from Dell Streak vendor-kernel
85245db49SHeiko Stübner  *
95245db49SHeiko Stübner  * Copyright (c) 2008 QUALCOMM Incorporated.
105245db49SHeiko Stübner  * Copyright (c) 2008 QUALCOMM USA, INC.
115245db49SHeiko Stübner  */
125245db49SHeiko Stübner 
13a750e24aSDmitry Torokhov #include <linux/err.h>
145245db49SHeiko Stübner #include <linux/kernel.h>
155245db49SHeiko Stübner #include <linux/module.h>
165245db49SHeiko Stübner #include <linux/interrupt.h>
175245db49SHeiko Stübner #include <linux/slab.h>
185245db49SHeiko Stübner #include <linux/input.h>
195245db49SHeiko Stübner #include <linux/jiffies.h>
205245db49SHeiko Stübner #include <linux/i2c.h>
215245db49SHeiko Stübner #include <linux/mutex.h>
225245db49SHeiko Stübner #include <linux/delay.h>
23a750e24aSDmitry Torokhov #include <linux/gpio/consumer.h>
247241443fSHeiko Stübner #include <linux/of.h>
25770a71b2SDmitry Torokhov #include <linux/property.h>
265245db49SHeiko Stübner 
275245db49SHeiko Stübner /*
285245db49SHeiko Stübner  * Coordinate calculation:
295245db49SHeiko Stübner  * X1 = X1_LSB + X1_MSB*256
305245db49SHeiko Stübner  * Y1 = Y1_LSB + Y1_MSB*256
315245db49SHeiko Stübner  * X2 = X2_LSB + X2_MSB*256
325245db49SHeiko Stübner  * Y2 = Y2_LSB + Y2_MSB*256
335245db49SHeiko Stübner  */
345245db49SHeiko Stübner #define AUO_PIXCIR_REG_X1_LSB		0x00
355245db49SHeiko Stübner #define AUO_PIXCIR_REG_X1_MSB		0x01
365245db49SHeiko Stübner #define AUO_PIXCIR_REG_Y1_LSB		0x02
375245db49SHeiko Stübner #define AUO_PIXCIR_REG_Y1_MSB		0x03
385245db49SHeiko Stübner #define AUO_PIXCIR_REG_X2_LSB		0x04
395245db49SHeiko Stübner #define AUO_PIXCIR_REG_X2_MSB		0x05
405245db49SHeiko Stübner #define AUO_PIXCIR_REG_Y2_LSB		0x06
415245db49SHeiko Stübner #define AUO_PIXCIR_REG_Y2_MSB		0x07
425245db49SHeiko Stübner 
435245db49SHeiko Stübner #define AUO_PIXCIR_REG_STRENGTH		0x0d
445245db49SHeiko Stübner #define AUO_PIXCIR_REG_STRENGTH_X1_LSB	0x0e
455245db49SHeiko Stübner #define AUO_PIXCIR_REG_STRENGTH_X1_MSB	0x0f
465245db49SHeiko Stübner 
475245db49SHeiko Stübner #define AUO_PIXCIR_REG_RAW_DATA_X	0x2b
485245db49SHeiko Stübner #define AUO_PIXCIR_REG_RAW_DATA_Y	0x4f
495245db49SHeiko Stübner 
505245db49SHeiko Stübner #define AUO_PIXCIR_REG_X_SENSITIVITY	0x6f
515245db49SHeiko Stübner #define AUO_PIXCIR_REG_Y_SENSITIVITY	0x70
525245db49SHeiko Stübner #define AUO_PIXCIR_REG_INT_SETTING	0x71
535245db49SHeiko Stübner #define AUO_PIXCIR_REG_INT_WIDTH	0x72
545245db49SHeiko Stübner #define AUO_PIXCIR_REG_POWER_MODE	0x73
555245db49SHeiko Stübner 
565245db49SHeiko Stübner #define AUO_PIXCIR_REG_VERSION		0x77
575245db49SHeiko Stübner #define AUO_PIXCIR_REG_CALIBRATE	0x78
585245db49SHeiko Stübner 
595245db49SHeiko Stübner #define AUO_PIXCIR_REG_TOUCHAREA_X1	0x1e
605245db49SHeiko Stübner #define AUO_PIXCIR_REG_TOUCHAREA_Y1	0x1f
615245db49SHeiko Stübner #define AUO_PIXCIR_REG_TOUCHAREA_X2	0x20
625245db49SHeiko Stübner #define AUO_PIXCIR_REG_TOUCHAREA_Y2	0x21
635245db49SHeiko Stübner 
645245db49SHeiko Stübner #define AUO_PIXCIR_REG_EEPROM_CALIB_X	0x42
655245db49SHeiko Stübner #define AUO_PIXCIR_REG_EEPROM_CALIB_Y	0xad
665245db49SHeiko Stübner 
675245db49SHeiko Stübner #define AUO_PIXCIR_INT_TPNUM_MASK	0xe0
685245db49SHeiko Stübner #define AUO_PIXCIR_INT_TPNUM_SHIFT	5
695245db49SHeiko Stübner #define AUO_PIXCIR_INT_RELEASE		(1 << 4)
705245db49SHeiko Stübner #define AUO_PIXCIR_INT_ENABLE		(1 << 3)
715245db49SHeiko Stübner #define AUO_PIXCIR_INT_POL_HIGH		(1 << 2)
72882597afSDmitry Torokhov 
73882597afSDmitry Torokhov /*
74882597afSDmitry Torokhov  * Interrupt modes:
75882597afSDmitry Torokhov  * periodical:		interrupt is asserted periodicaly
76882597afSDmitry Torokhov  * compare coordinates:	interrupt is asserted when coordinates change
77882597afSDmitry Torokhov  * indicate touch:	interrupt is asserted during touch
78882597afSDmitry Torokhov  */
79882597afSDmitry Torokhov #define AUO_PIXCIR_INT_PERIODICAL	0x00
80882597afSDmitry Torokhov #define AUO_PIXCIR_INT_COMP_COORD	0x01
81882597afSDmitry Torokhov #define AUO_PIXCIR_INT_TOUCH_IND	0x02
825245db49SHeiko Stübner #define AUO_PIXCIR_INT_MODE_MASK	0x03
835245db49SHeiko Stübner 
845245db49SHeiko Stübner /*
855245db49SHeiko Stübner  * Power modes:
865245db49SHeiko Stübner  * active:	scan speed 60Hz
875245db49SHeiko Stübner  * sleep:	scan speed 10Hz can be auto-activated, wakeup on 1st touch
885245db49SHeiko Stübner  * deep sleep:	scan speed 1Hz can only be entered or left manually.
895245db49SHeiko Stübner  */
905245db49SHeiko Stübner #define AUO_PIXCIR_POWER_ACTIVE		0x00
915245db49SHeiko Stübner #define AUO_PIXCIR_POWER_SLEEP		0x01
925245db49SHeiko Stübner #define AUO_PIXCIR_POWER_DEEP_SLEEP	0x02
935245db49SHeiko Stübner #define AUO_PIXCIR_POWER_MASK		0x03
945245db49SHeiko Stübner 
955245db49SHeiko Stübner #define AUO_PIXCIR_POWER_ALLOW_SLEEP	(1 << 2)
965245db49SHeiko Stübner #define AUO_PIXCIR_POWER_IDLE_TIME(ms)	((ms & 0xf) << 4)
975245db49SHeiko Stübner 
985245db49SHeiko Stübner #define AUO_PIXCIR_CALIBRATE		0x03
995245db49SHeiko Stübner 
1005245db49SHeiko Stübner #define AUO_PIXCIR_EEPROM_CALIB_X_LEN	62
1015245db49SHeiko Stübner #define AUO_PIXCIR_EEPROM_CALIB_Y_LEN	36
1025245db49SHeiko Stübner 
1035245db49SHeiko Stübner #define AUO_PIXCIR_RAW_DATA_X_LEN	18
1045245db49SHeiko Stübner #define AUO_PIXCIR_RAW_DATA_Y_LEN	11
1055245db49SHeiko Stübner 
1065245db49SHeiko Stübner #define AUO_PIXCIR_STRENGTH_ENABLE	(1 << 0)
1075245db49SHeiko Stübner 
1085245db49SHeiko Stübner /* Touchscreen absolute values */
1095245db49SHeiko Stübner #define AUO_PIXCIR_REPORT_POINTS	2
1105245db49SHeiko Stübner #define AUO_PIXCIR_MAX_AREA		0xff
1115245db49SHeiko Stübner #define AUO_PIXCIR_PENUP_TIMEOUT_MS	10
1125245db49SHeiko Stübner 
1135245db49SHeiko Stübner struct auo_pixcir_ts {
1145245db49SHeiko Stübner 	struct i2c_client	*client;
1155245db49SHeiko Stübner 	struct input_dev	*input;
116a750e24aSDmitry Torokhov 	struct gpio_desc	*gpio_int;
117a750e24aSDmitry Torokhov 	struct gpio_desc	*gpio_rst;
1185245db49SHeiko Stübner 	char			phys[32];
1195245db49SHeiko Stübner 
120882597afSDmitry Torokhov 	unsigned int		x_max;
121882597afSDmitry Torokhov 	unsigned int		y_max;
122882597afSDmitry Torokhov 
123882597afSDmitry Torokhov 	/* special handling for touch_indicate interrupt mode */
1245245db49SHeiko Stübner 	bool			touch_ind_mode;
1255245db49SHeiko Stübner 
1265245db49SHeiko Stübner 	wait_queue_head_t	wait;
1275245db49SHeiko Stübner 	bool			stopped;
1285245db49SHeiko Stübner };
1295245db49SHeiko Stübner 
1305245db49SHeiko Stübner struct auo_point_t {
1315245db49SHeiko Stübner 	int	coord_x;
1325245db49SHeiko Stübner 	int	coord_y;
1335245db49SHeiko Stübner 	int	area_major;
1345245db49SHeiko Stübner 	int	area_minor;
1355245db49SHeiko Stübner 	int	orientation;
1365245db49SHeiko Stübner };
1375245db49SHeiko Stübner 
auo_pixcir_collect_data(struct auo_pixcir_ts * ts,struct auo_point_t * point)1385245db49SHeiko Stübner static int auo_pixcir_collect_data(struct auo_pixcir_ts *ts,
1395245db49SHeiko Stübner 				   struct auo_point_t *point)
1405245db49SHeiko Stübner {
1415245db49SHeiko Stübner 	struct i2c_client *client = ts->client;
1425245db49SHeiko Stübner 	uint8_t raw_coord[8];
1435245db49SHeiko Stübner 	uint8_t raw_area[4];
1445245db49SHeiko Stübner 	int i, ret;
1455245db49SHeiko Stübner 
1465245db49SHeiko Stübner 	/* touch coordinates */
1475245db49SHeiko Stübner 	ret = i2c_smbus_read_i2c_block_data(client, AUO_PIXCIR_REG_X1_LSB,
1485245db49SHeiko Stübner 					    8, raw_coord);
1495245db49SHeiko Stübner 	if (ret < 0) {
1505245db49SHeiko Stübner 		dev_err(&client->dev, "failed to read coordinate, %d\n", ret);
1515245db49SHeiko Stübner 		return ret;
1525245db49SHeiko Stübner 	}
1535245db49SHeiko Stübner 
1545245db49SHeiko Stübner 	/* touch area */
1555245db49SHeiko Stübner 	ret = i2c_smbus_read_i2c_block_data(client, AUO_PIXCIR_REG_TOUCHAREA_X1,
1565245db49SHeiko Stübner 					    4, raw_area);
1575245db49SHeiko Stübner 	if (ret < 0) {
1585245db49SHeiko Stübner 		dev_err(&client->dev, "could not read touch area, %d\n", ret);
1595245db49SHeiko Stübner 		return ret;
1605245db49SHeiko Stübner 	}
1615245db49SHeiko Stübner 
1625245db49SHeiko Stübner 	for (i = 0; i < AUO_PIXCIR_REPORT_POINTS; i++) {
1635245db49SHeiko Stübner 		point[i].coord_x =
1645245db49SHeiko Stübner 			raw_coord[4 * i + 1] << 8 | raw_coord[4 * i];
1655245db49SHeiko Stübner 		point[i].coord_y =
1665245db49SHeiko Stübner 			raw_coord[4 * i + 3] << 8 | raw_coord[4 * i + 2];
1675245db49SHeiko Stübner 
168882597afSDmitry Torokhov 		if (point[i].coord_x > ts->x_max ||
169882597afSDmitry Torokhov 		    point[i].coord_y > ts->y_max) {
1705245db49SHeiko Stübner 			dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
1715245db49SHeiko Stübner 				point[i].coord_x, point[i].coord_y);
1725245db49SHeiko Stübner 			point[i].coord_x = point[i].coord_y = 0;
1735245db49SHeiko Stübner 		}
1745245db49SHeiko Stübner 
1755245db49SHeiko Stübner 		/* determine touch major, minor and orientation */
1765245db49SHeiko Stübner 		point[i].area_major = max(raw_area[2 * i], raw_area[2 * i + 1]);
1775245db49SHeiko Stübner 		point[i].area_minor = min(raw_area[2 * i], raw_area[2 * i + 1]);
1785245db49SHeiko Stübner 		point[i].orientation = raw_area[2 * i] > raw_area[2 * i + 1];
1795245db49SHeiko Stübner 	}
1805245db49SHeiko Stübner 
1815245db49SHeiko Stübner 	return 0;
1825245db49SHeiko Stübner }
1835245db49SHeiko Stübner 
auo_pixcir_interrupt(int irq,void * dev_id)1845245db49SHeiko Stübner static irqreturn_t auo_pixcir_interrupt(int irq, void *dev_id)
1855245db49SHeiko Stübner {
1865245db49SHeiko Stübner 	struct auo_pixcir_ts *ts = dev_id;
1875245db49SHeiko Stübner 	struct auo_point_t point[AUO_PIXCIR_REPORT_POINTS];
1885245db49SHeiko Stübner 	int i;
1895245db49SHeiko Stübner 	int ret;
1905245db49SHeiko Stübner 	int fingers = 0;
1915245db49SHeiko Stübner 	int abs = -1;
1925245db49SHeiko Stübner 
1935245db49SHeiko Stübner 	while (!ts->stopped) {
1945245db49SHeiko Stübner 
1955245db49SHeiko Stübner 		/* check for up event in touch touch_ind_mode */
1965245db49SHeiko Stübner 		if (ts->touch_ind_mode) {
197a750e24aSDmitry Torokhov 			if (gpiod_get_value_cansleep(ts->gpio_int) == 0) {
1985245db49SHeiko Stübner 				input_mt_sync(ts->input);
1995245db49SHeiko Stübner 				input_report_key(ts->input, BTN_TOUCH, 0);
2005245db49SHeiko Stübner 				input_sync(ts->input);
2015245db49SHeiko Stübner 				break;
2025245db49SHeiko Stübner 			}
2035245db49SHeiko Stübner 		}
2045245db49SHeiko Stübner 
2055245db49SHeiko Stübner 		ret = auo_pixcir_collect_data(ts, point);
2065245db49SHeiko Stübner 		if (ret < 0) {
2075245db49SHeiko Stübner 			/* we want to loop only in touch_ind_mode */
2085245db49SHeiko Stübner 			if (!ts->touch_ind_mode)
2095245db49SHeiko Stübner 				break;
2105245db49SHeiko Stübner 
2115245db49SHeiko Stübner 			wait_event_timeout(ts->wait, ts->stopped,
2125245db49SHeiko Stübner 				msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS));
2135245db49SHeiko Stübner 			continue;
2145245db49SHeiko Stübner 		}
2155245db49SHeiko Stübner 
2165245db49SHeiko Stübner 		for (i = 0; i < AUO_PIXCIR_REPORT_POINTS; i++) {
2175245db49SHeiko Stübner 			if (point[i].coord_x > 0 || point[i].coord_y > 0) {
2185245db49SHeiko Stübner 				input_report_abs(ts->input, ABS_MT_POSITION_X,
2195245db49SHeiko Stübner 						 point[i].coord_x);
2205245db49SHeiko Stübner 				input_report_abs(ts->input, ABS_MT_POSITION_Y,
2215245db49SHeiko Stübner 						 point[i].coord_y);
2225245db49SHeiko Stübner 				input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
2235245db49SHeiko Stübner 						 point[i].area_major);
2245245db49SHeiko Stübner 				input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
2255245db49SHeiko Stübner 						 point[i].area_minor);
2265245db49SHeiko Stübner 				input_report_abs(ts->input, ABS_MT_ORIENTATION,
2275245db49SHeiko Stübner 						 point[i].orientation);
2285245db49SHeiko Stübner 				input_mt_sync(ts->input);
2295245db49SHeiko Stübner 
2305245db49SHeiko Stübner 				/* use first finger as source for singletouch */
2315245db49SHeiko Stübner 				if (fingers == 0)
2325245db49SHeiko Stübner 					abs = i;
2335245db49SHeiko Stübner 
2345245db49SHeiko Stübner 				/* number of touch points could also be queried
2355245db49SHeiko Stübner 				 * via i2c but would require an additional call
2365245db49SHeiko Stübner 				 */
2375245db49SHeiko Stübner 				fingers++;
2385245db49SHeiko Stübner 			}
2395245db49SHeiko Stübner 		}
2405245db49SHeiko Stübner 
2415245db49SHeiko Stübner 		input_report_key(ts->input, BTN_TOUCH, fingers > 0);
2425245db49SHeiko Stübner 
2435245db49SHeiko Stübner 		if (abs > -1) {
2445245db49SHeiko Stübner 			input_report_abs(ts->input, ABS_X, point[abs].coord_x);
2455245db49SHeiko Stübner 			input_report_abs(ts->input, ABS_Y, point[abs].coord_y);
2465245db49SHeiko Stübner 		}
2475245db49SHeiko Stübner 
2485245db49SHeiko Stübner 		input_sync(ts->input);
2495245db49SHeiko Stübner 
2505245db49SHeiko Stübner 		/* we want to loop only in touch_ind_mode */
2515245db49SHeiko Stübner 		if (!ts->touch_ind_mode)
2525245db49SHeiko Stübner 			break;
2535245db49SHeiko Stübner 
2545245db49SHeiko Stübner 		wait_event_timeout(ts->wait, ts->stopped,
2555245db49SHeiko Stübner 				 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS));
2565245db49SHeiko Stübner 	}
2575245db49SHeiko Stübner 
2585245db49SHeiko Stübner 	return IRQ_HANDLED;
2595245db49SHeiko Stübner }
2605245db49SHeiko Stübner 
2615245db49SHeiko Stübner /*
2625245db49SHeiko Stübner  * Set the power mode of the device.
2635245db49SHeiko Stübner  * Valid modes are
2645245db49SHeiko Stübner  * - AUO_PIXCIR_POWER_ACTIVE
2655245db49SHeiko Stübner  * - AUO_PIXCIR_POWER_SLEEP - automatically left on first touch
2665245db49SHeiko Stübner  * - AUO_PIXCIR_POWER_DEEP_SLEEP
2675245db49SHeiko Stübner  */
auo_pixcir_power_mode(struct auo_pixcir_ts * ts,int mode)2685245db49SHeiko Stübner static int auo_pixcir_power_mode(struct auo_pixcir_ts *ts, int mode)
2695245db49SHeiko Stübner {
2705245db49SHeiko Stübner 	struct i2c_client *client = ts->client;
2715245db49SHeiko Stübner 	int ret;
2725245db49SHeiko Stübner 
2735245db49SHeiko Stübner 	ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_POWER_MODE);
2745245db49SHeiko Stübner 	if (ret < 0) {
2755245db49SHeiko Stübner 		dev_err(&client->dev, "unable to read reg %Xh, %d\n",
2765245db49SHeiko Stübner 			AUO_PIXCIR_REG_POWER_MODE, ret);
2775245db49SHeiko Stübner 		return ret;
2785245db49SHeiko Stübner 	}
2795245db49SHeiko Stübner 
2805245db49SHeiko Stübner 	ret &= ~AUO_PIXCIR_POWER_MASK;
2815245db49SHeiko Stübner 	ret |= mode;
2825245db49SHeiko Stübner 
2835245db49SHeiko Stübner 	ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_POWER_MODE, ret);
2845245db49SHeiko Stübner 	if (ret) {
2855245db49SHeiko Stübner 		dev_err(&client->dev, "unable to write reg %Xh, %d\n",
2865245db49SHeiko Stübner 			AUO_PIXCIR_REG_POWER_MODE, ret);
2875245db49SHeiko Stübner 		return ret;
2885245db49SHeiko Stübner 	}
2895245db49SHeiko Stübner 
2905245db49SHeiko Stübner 	return 0;
2915245db49SHeiko Stübner }
2925245db49SHeiko Stübner 
auo_pixcir_int_config(struct auo_pixcir_ts * ts,int int_setting)293882597afSDmitry Torokhov static int auo_pixcir_int_config(struct auo_pixcir_ts *ts, int int_setting)
2945245db49SHeiko Stübner {
2955245db49SHeiko Stübner 	struct i2c_client *client = ts->client;
2965245db49SHeiko Stübner 	int ret;
2975245db49SHeiko Stübner 
2985245db49SHeiko Stübner 	ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_INT_SETTING);
2995245db49SHeiko Stübner 	if (ret < 0) {
3005245db49SHeiko Stübner 		dev_err(&client->dev, "unable to read reg %Xh, %d\n",
3015245db49SHeiko Stübner 			AUO_PIXCIR_REG_INT_SETTING, ret);
3025245db49SHeiko Stübner 		return ret;
3035245db49SHeiko Stübner 	}
3045245db49SHeiko Stübner 
3055245db49SHeiko Stübner 	ret &= ~AUO_PIXCIR_INT_MODE_MASK;
3065245db49SHeiko Stübner 	ret |= int_setting;
3075245db49SHeiko Stübner 	ret |= AUO_PIXCIR_INT_POL_HIGH; /* always use high for interrupts */
3085245db49SHeiko Stübner 
3095245db49SHeiko Stübner 	ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_INT_SETTING,
3105245db49SHeiko Stübner 					ret);
3115245db49SHeiko Stübner 	if (ret < 0) {
3125245db49SHeiko Stübner 		dev_err(&client->dev, "unable to write reg %Xh, %d\n",
3135245db49SHeiko Stübner 			AUO_PIXCIR_REG_INT_SETTING, ret);
3145245db49SHeiko Stübner 		return ret;
3155245db49SHeiko Stübner 	}
3165245db49SHeiko Stübner 
317882597afSDmitry Torokhov 	ts->touch_ind_mode = int_setting == AUO_PIXCIR_INT_TOUCH_IND;
3185245db49SHeiko Stübner 
3195245db49SHeiko Stübner 	return 0;
3205245db49SHeiko Stübner }
3215245db49SHeiko Stübner 
3225245db49SHeiko Stübner /* control the generation of interrupts on the device side */
auo_pixcir_int_toggle(struct auo_pixcir_ts * ts,bool enable)3235245db49SHeiko Stübner static int auo_pixcir_int_toggle(struct auo_pixcir_ts *ts, bool enable)
3245245db49SHeiko Stübner {
3255245db49SHeiko Stübner 	struct i2c_client *client = ts->client;
3265245db49SHeiko Stübner 	int ret;
3275245db49SHeiko Stübner 
3285245db49SHeiko Stübner 	ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_INT_SETTING);
3295245db49SHeiko Stübner 	if (ret < 0) {
3305245db49SHeiko Stübner 		dev_err(&client->dev, "unable to read reg %Xh, %d\n",
3315245db49SHeiko Stübner 			AUO_PIXCIR_REG_INT_SETTING, ret);
3325245db49SHeiko Stübner 		return ret;
3335245db49SHeiko Stübner 	}
3345245db49SHeiko Stübner 
3355245db49SHeiko Stübner 	if (enable)
3365245db49SHeiko Stübner 		ret |= AUO_PIXCIR_INT_ENABLE;
3375245db49SHeiko Stübner 	else
3385245db49SHeiko Stübner 		ret &= ~AUO_PIXCIR_INT_ENABLE;
3395245db49SHeiko Stübner 
3405245db49SHeiko Stübner 	ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_INT_SETTING,
3415245db49SHeiko Stübner 					ret);
3425245db49SHeiko Stübner 	if (ret < 0) {
3435245db49SHeiko Stübner 		dev_err(&client->dev, "unable to write reg %Xh, %d\n",
3445245db49SHeiko Stübner 			AUO_PIXCIR_REG_INT_SETTING, ret);
3455245db49SHeiko Stübner 		return ret;
3465245db49SHeiko Stübner 	}
3475245db49SHeiko Stübner 
3485245db49SHeiko Stübner 	return 0;
3495245db49SHeiko Stübner }
3505245db49SHeiko Stübner 
auo_pixcir_start(struct auo_pixcir_ts * ts)3515245db49SHeiko Stübner static int auo_pixcir_start(struct auo_pixcir_ts *ts)
3525245db49SHeiko Stübner {
3535245db49SHeiko Stübner 	struct i2c_client *client = ts->client;
3545245db49SHeiko Stübner 	int ret;
3555245db49SHeiko Stübner 
3565245db49SHeiko Stübner 	ret = auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_ACTIVE);
3575245db49SHeiko Stübner 	if (ret < 0) {
3585245db49SHeiko Stübner 		dev_err(&client->dev, "could not set power mode, %d\n",
3595245db49SHeiko Stübner 			ret);
3605245db49SHeiko Stübner 		return ret;
3615245db49SHeiko Stübner 	}
3625245db49SHeiko Stübner 
3635245db49SHeiko Stübner 	ts->stopped = false;
3645245db49SHeiko Stübner 	mb();
3655245db49SHeiko Stübner 	enable_irq(client->irq);
3665245db49SHeiko Stübner 
3675245db49SHeiko Stübner 	ret = auo_pixcir_int_toggle(ts, 1);
3685245db49SHeiko Stübner 	if (ret < 0) {
3695245db49SHeiko Stübner 		dev_err(&client->dev, "could not enable interrupt, %d\n",
3705245db49SHeiko Stübner 			ret);
3715245db49SHeiko Stübner 		disable_irq(client->irq);
3725245db49SHeiko Stübner 		return ret;
3735245db49SHeiko Stübner 	}
3745245db49SHeiko Stübner 
3755245db49SHeiko Stübner 	return 0;
3765245db49SHeiko Stübner }
3775245db49SHeiko Stübner 
auo_pixcir_stop(struct auo_pixcir_ts * ts)3785245db49SHeiko Stübner static int auo_pixcir_stop(struct auo_pixcir_ts *ts)
3795245db49SHeiko Stübner {
3805245db49SHeiko Stübner 	struct i2c_client *client = ts->client;
3815245db49SHeiko Stübner 	int ret;
3825245db49SHeiko Stübner 
3835245db49SHeiko Stübner 	ret = auo_pixcir_int_toggle(ts, 0);
3845245db49SHeiko Stübner 	if (ret < 0) {
3855245db49SHeiko Stübner 		dev_err(&client->dev, "could not disable interrupt, %d\n",
3865245db49SHeiko Stübner 			ret);
3875245db49SHeiko Stübner 		return ret;
3885245db49SHeiko Stübner 	}
3895245db49SHeiko Stübner 
3905245db49SHeiko Stübner 	/* disable receiving of interrupts */
3915245db49SHeiko Stübner 	disable_irq(client->irq);
3925245db49SHeiko Stübner 	ts->stopped = true;
3935245db49SHeiko Stübner 	mb();
3945245db49SHeiko Stübner 	wake_up(&ts->wait);
3955245db49SHeiko Stübner 
3965245db49SHeiko Stübner 	return auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_DEEP_SLEEP);
3975245db49SHeiko Stübner }
3985245db49SHeiko Stübner 
auo_pixcir_input_open(struct input_dev * dev)3995245db49SHeiko Stübner static int auo_pixcir_input_open(struct input_dev *dev)
4005245db49SHeiko Stübner {
4015245db49SHeiko Stübner 	struct auo_pixcir_ts *ts = input_get_drvdata(dev);
4025245db49SHeiko Stübner 
403901ea5e3SJavier Martinez Canillas 	return auo_pixcir_start(ts);
4045245db49SHeiko Stübner }
4055245db49SHeiko Stübner 
auo_pixcir_input_close(struct input_dev * dev)4065245db49SHeiko Stübner static void auo_pixcir_input_close(struct input_dev *dev)
4075245db49SHeiko Stübner {
4085245db49SHeiko Stübner 	struct auo_pixcir_ts *ts = input_get_drvdata(dev);
4095245db49SHeiko Stübner 
4105245db49SHeiko Stübner 	auo_pixcir_stop(ts);
4115245db49SHeiko Stübner }
4125245db49SHeiko Stübner 
auo_pixcir_suspend(struct device * dev)41348877f8eSJonathan Cameron static int auo_pixcir_suspend(struct device *dev)
4145245db49SHeiko Stübner {
4155245db49SHeiko Stübner 	struct i2c_client *client = to_i2c_client(dev);
4165245db49SHeiko Stübner 	struct auo_pixcir_ts *ts = i2c_get_clientdata(client);
4175245db49SHeiko Stübner 	struct input_dev *input = ts->input;
4185245db49SHeiko Stübner 	int ret = 0;
4195245db49SHeiko Stübner 
4205245db49SHeiko Stübner 	mutex_lock(&input->mutex);
4215245db49SHeiko Stübner 
4225245db49SHeiko Stübner 	/* when configured as wakeup source, device should always wake system
4235245db49SHeiko Stübner 	 * therefore start device if necessary
4245245db49SHeiko Stübner 	 */
4255245db49SHeiko Stübner 	if (device_may_wakeup(&client->dev)) {
4265245db49SHeiko Stübner 		/* need to start device if not open, to be wakeup source */
427d69f0a43SAndrzej Pietrasiewicz 		if (!input_device_enabled(input)) {
4285245db49SHeiko Stübner 			ret = auo_pixcir_start(ts);
4295245db49SHeiko Stübner 			if (ret)
4305245db49SHeiko Stübner 				goto unlock;
4315245db49SHeiko Stübner 		}
4325245db49SHeiko Stübner 
4335245db49SHeiko Stübner 		enable_irq_wake(client->irq);
4345245db49SHeiko Stübner 		ret = auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_SLEEP);
435d69f0a43SAndrzej Pietrasiewicz 	} else if (input_device_enabled(input)) {
4365245db49SHeiko Stübner 		ret = auo_pixcir_stop(ts);
4375245db49SHeiko Stübner 	}
4385245db49SHeiko Stübner 
4395245db49SHeiko Stübner unlock:
4405245db49SHeiko Stübner 	mutex_unlock(&input->mutex);
4415245db49SHeiko Stübner 
4425245db49SHeiko Stübner 	return ret;
4435245db49SHeiko Stübner }
4445245db49SHeiko Stübner 
auo_pixcir_resume(struct device * dev)44548877f8eSJonathan Cameron static int auo_pixcir_resume(struct device *dev)
4465245db49SHeiko Stübner {
4475245db49SHeiko Stübner 	struct i2c_client *client = to_i2c_client(dev);
4485245db49SHeiko Stübner 	struct auo_pixcir_ts *ts = i2c_get_clientdata(client);
4495245db49SHeiko Stübner 	struct input_dev *input = ts->input;
4505245db49SHeiko Stübner 	int ret = 0;
4515245db49SHeiko Stübner 
4525245db49SHeiko Stübner 	mutex_lock(&input->mutex);
4535245db49SHeiko Stübner 
4545245db49SHeiko Stübner 	if (device_may_wakeup(&client->dev)) {
4555245db49SHeiko Stübner 		disable_irq_wake(client->irq);
4565245db49SHeiko Stübner 
4575245db49SHeiko Stübner 		/* need to stop device if it was not open on suspend */
458d69f0a43SAndrzej Pietrasiewicz 		if (!input_device_enabled(input)) {
4595245db49SHeiko Stübner 			ret = auo_pixcir_stop(ts);
4605245db49SHeiko Stübner 			if (ret)
4615245db49SHeiko Stübner 				goto unlock;
4625245db49SHeiko Stübner 		}
4635245db49SHeiko Stübner 
4645245db49SHeiko Stübner 		/* device wakes automatically from SLEEP */
465d69f0a43SAndrzej Pietrasiewicz 	} else if (input_device_enabled(input)) {
4665245db49SHeiko Stübner 		ret = auo_pixcir_start(ts);
4675245db49SHeiko Stübner 	}
4685245db49SHeiko Stübner 
4695245db49SHeiko Stübner unlock:
4705245db49SHeiko Stübner 	mutex_unlock(&input->mutex);
4715245db49SHeiko Stübner 
4725245db49SHeiko Stübner 	return ret;
4735245db49SHeiko Stübner }
4745245db49SHeiko Stübner 
47548877f8eSJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(auo_pixcir_pm_ops,
4767241443fSHeiko Stübner 				auo_pixcir_suspend, auo_pixcir_resume);
4777241443fSHeiko Stübner 
auo_pixcir_reset(void * data)478bfc29e95SDmitry Torokhov static void auo_pixcir_reset(void *data)
479bfc29e95SDmitry Torokhov {
480bfc29e95SDmitry Torokhov 	struct auo_pixcir_ts *ts = data;
481bfc29e95SDmitry Torokhov 
482a750e24aSDmitry Torokhov 	gpiod_set_value_cansleep(ts->gpio_rst, 1);
483bfc29e95SDmitry Torokhov }
484bfc29e95SDmitry Torokhov 
auo_pixcir_probe(struct i2c_client * client)485771d0539SUwe Kleine-König static int auo_pixcir_probe(struct i2c_client *client)
4865245db49SHeiko Stübner {
4875245db49SHeiko Stübner 	struct auo_pixcir_ts *ts;
4885245db49SHeiko Stübner 	struct input_dev *input_dev;
489bfc29e95SDmitry Torokhov 	int version;
490bfc29e95SDmitry Torokhov 	int error;
4915245db49SHeiko Stübner 
492882597afSDmitry Torokhov 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
4935245db49SHeiko Stübner 	if (!ts)
4945245db49SHeiko Stübner 		return -ENOMEM;
4955245db49SHeiko Stübner 
496bfc29e95SDmitry Torokhov 	input_dev = devm_input_allocate_device(&client->dev);
497bfc29e95SDmitry Torokhov 	if (!input_dev) {
498bfc29e95SDmitry Torokhov 		dev_err(&client->dev, "could not allocate input device\n");
499bfc29e95SDmitry Torokhov 		return -ENOMEM;
5005245db49SHeiko Stübner 	}
5015245db49SHeiko Stübner 
5025245db49SHeiko Stübner 	ts->client = client;
503bfc29e95SDmitry Torokhov 	ts->input = input_dev;
5045245db49SHeiko Stübner 	ts->touch_ind_mode = 0;
505bfc29e95SDmitry Torokhov 	ts->stopped = true;
5065245db49SHeiko Stübner 	init_waitqueue_head(&ts->wait);
5075245db49SHeiko Stübner 
5085245db49SHeiko Stübner 	snprintf(ts->phys, sizeof(ts->phys),
5095245db49SHeiko Stübner 		 "%s/input0", dev_name(&client->dev));
5105245db49SHeiko Stübner 
511770a71b2SDmitry Torokhov 	if (device_property_read_u32(&client->dev, "x-size", &ts->x_max)) {
512770a71b2SDmitry Torokhov 		dev_err(&client->dev, "failed to get x-size property\n");
513770a71b2SDmitry Torokhov 		return -EINVAL;
514770a71b2SDmitry Torokhov 	}
515770a71b2SDmitry Torokhov 
516770a71b2SDmitry Torokhov 	if (device_property_read_u32(&client->dev, "y-size", &ts->y_max)) {
517770a71b2SDmitry Torokhov 		dev_err(&client->dev, "failed to get y-size property\n");
518770a71b2SDmitry Torokhov 		return -EINVAL;
519770a71b2SDmitry Torokhov 	}
520882597afSDmitry Torokhov 
5215245db49SHeiko Stübner 	input_dev->name = "AUO-Pixcir touchscreen";
5225245db49SHeiko Stübner 	input_dev->phys = ts->phys;
5235245db49SHeiko Stübner 	input_dev->id.bustype = BUS_I2C;
5245245db49SHeiko Stübner 
5255245db49SHeiko Stübner 	input_dev->open = auo_pixcir_input_open;
5265245db49SHeiko Stübner 	input_dev->close = auo_pixcir_input_close;
5275245db49SHeiko Stübner 
5285245db49SHeiko Stübner 	__set_bit(EV_ABS, input_dev->evbit);
5295245db49SHeiko Stübner 	__set_bit(EV_KEY, input_dev->evbit);
5305245db49SHeiko Stübner 
5315245db49SHeiko Stübner 	__set_bit(BTN_TOUCH, input_dev->keybit);
5325245db49SHeiko Stübner 
5335245db49SHeiko Stübner 	/* For single touch */
534882597afSDmitry Torokhov 	input_set_abs_params(input_dev, ABS_X, 0, ts->x_max, 0, 0);
535882597afSDmitry Torokhov 	input_set_abs_params(input_dev, ABS_Y, 0, ts->y_max, 0, 0);
5365245db49SHeiko Stübner 
5375245db49SHeiko Stübner 	/* For multi touch */
538882597afSDmitry Torokhov 	input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0);
539882597afSDmitry Torokhov 	input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0);
540882597afSDmitry Torokhov 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
541882597afSDmitry Torokhov 			     0, AUO_PIXCIR_MAX_AREA, 0, 0);
542882597afSDmitry Torokhov 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
543882597afSDmitry Torokhov 			     0, AUO_PIXCIR_MAX_AREA, 0, 0);
5445245db49SHeiko Stübner 	input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
5455245db49SHeiko Stübner 
5465245db49SHeiko Stübner 	input_set_drvdata(ts->input, ts);
5475245db49SHeiko Stübner 
548a750e24aSDmitry Torokhov 	ts->gpio_int = devm_gpiod_get_index(&client->dev, NULL, 0, GPIOD_IN);
549a750e24aSDmitry Torokhov 	error = PTR_ERR_OR_ZERO(ts->gpio_int);
550bfc29e95SDmitry Torokhov 	if (error) {
551a750e24aSDmitry Torokhov 		dev_err(&client->dev,
552a750e24aSDmitry Torokhov 			"request of int gpio failed: %d\n", error);
553bfc29e95SDmitry Torokhov 		return error;
554bfc29e95SDmitry Torokhov 	}
555bfc29e95SDmitry Torokhov 
556a750e24aSDmitry Torokhov 	gpiod_set_consumer_name(ts->gpio_int, "auo_pixcir_ts_int");
557a750e24aSDmitry Torokhov 
558a750e24aSDmitry Torokhov 	/* Take the chip out of reset */
559a750e24aSDmitry Torokhov 	ts->gpio_rst = devm_gpiod_get_index(&client->dev, NULL, 1,
560a750e24aSDmitry Torokhov 					    GPIOD_OUT_LOW);
561a750e24aSDmitry Torokhov 	error = PTR_ERR_OR_ZERO(ts->gpio_rst);
562bfc29e95SDmitry Torokhov 	if (error) {
563a750e24aSDmitry Torokhov 		dev_err(&client->dev,
564a750e24aSDmitry Torokhov 			"request of reset gpio failed: %d\n", error);
565bfc29e95SDmitry Torokhov 		return error;
566bfc29e95SDmitry Torokhov 	}
567bfc29e95SDmitry Torokhov 
568a750e24aSDmitry Torokhov 	gpiod_set_consumer_name(ts->gpio_rst, "auo_pixcir_ts_rst");
569a750e24aSDmitry Torokhov 
570e2c063c8SFuqian Huang 	error = devm_add_action_or_reset(&client->dev, auo_pixcir_reset, ts);
571bfc29e95SDmitry Torokhov 	if (error) {
572bfc29e95SDmitry Torokhov 		dev_err(&client->dev, "failed to register reset action, %d\n",
573bfc29e95SDmitry Torokhov 			error);
574bfc29e95SDmitry Torokhov 		return error;
575bfc29e95SDmitry Torokhov 	}
576bfc29e95SDmitry Torokhov 
577bfc29e95SDmitry Torokhov 	msleep(200);
578bfc29e95SDmitry Torokhov 
579bfc29e95SDmitry Torokhov 	version = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_VERSION);
580bfc29e95SDmitry Torokhov 	if (version < 0) {
581bfc29e95SDmitry Torokhov 		error = version;
582bfc29e95SDmitry Torokhov 		return error;
583bfc29e95SDmitry Torokhov 	}
584bfc29e95SDmitry Torokhov 
585bfc29e95SDmitry Torokhov 	dev_info(&client->dev, "firmware version 0x%X\n", version);
586bfc29e95SDmitry Torokhov 
587882597afSDmitry Torokhov 	/* default to asserting the interrupt when the screen is touched */
588882597afSDmitry Torokhov 	error = auo_pixcir_int_config(ts, AUO_PIXCIR_INT_TOUCH_IND);
589bfc29e95SDmitry Torokhov 	if (error)
590bfc29e95SDmitry Torokhov 		return error;
591bfc29e95SDmitry Torokhov 
592bfc29e95SDmitry Torokhov 	error = devm_request_threaded_irq(&client->dev, client->irq,
593bfc29e95SDmitry Torokhov 					  NULL, auo_pixcir_interrupt,
59460b7a6d0SDmitry Torokhov 					  IRQF_ONESHOT,
5955245db49SHeiko Stübner 					  input_dev->name, ts);
596bfc29e95SDmitry Torokhov 	if (error) {
597bfc29e95SDmitry Torokhov 		dev_err(&client->dev, "irq %d requested failed, %d\n",
598bfc29e95SDmitry Torokhov 			client->irq, error);
599bfc29e95SDmitry Torokhov 		return error;
6005245db49SHeiko Stübner 	}
6015245db49SHeiko Stübner 
6025245db49SHeiko Stübner 	/* stop device and put it into deep sleep until it is opened */
603bfc29e95SDmitry Torokhov 	error = auo_pixcir_stop(ts);
604bfc29e95SDmitry Torokhov 	if (error)
605bfc29e95SDmitry Torokhov 		return error;
6065245db49SHeiko Stübner 
607bfc29e95SDmitry Torokhov 	error = input_register_device(input_dev);
608bfc29e95SDmitry Torokhov 	if (error) {
609bfc29e95SDmitry Torokhov 		dev_err(&client->dev, "could not register input device, %d\n",
610bfc29e95SDmitry Torokhov 			error);
611bfc29e95SDmitry Torokhov 		return error;
6125245db49SHeiko Stübner 	}
6135245db49SHeiko Stübner 
6145245db49SHeiko Stübner 	i2c_set_clientdata(client, ts);
6155245db49SHeiko Stübner 
6165245db49SHeiko Stübner 	return 0;
6175245db49SHeiko Stübner }
6185245db49SHeiko Stübner 
6195245db49SHeiko Stübner static const struct i2c_device_id auo_pixcir_idtable[] = {
620*5852f2afSUwe Kleine-König 	{ "auo_pixcir_ts" },
6215245db49SHeiko Stübner 	{ }
6225245db49SHeiko Stübner };
6235245db49SHeiko Stübner MODULE_DEVICE_TABLE(i2c, auo_pixcir_idtable);
6245245db49SHeiko Stübner 
6257241443fSHeiko Stübner #ifdef CONFIG_OF
62669f8cafdSJingoo Han static const struct of_device_id auo_pixcir_ts_dt_idtable[] = {
6277241443fSHeiko Stübner 	{ .compatible = "auo,auo_pixcir_ts" },
6287241443fSHeiko Stübner 	{},
6297241443fSHeiko Stübner };
6307241443fSHeiko Stübner MODULE_DEVICE_TABLE(of, auo_pixcir_ts_dt_idtable);
6317241443fSHeiko Stübner #endif
6327241443fSHeiko Stübner 
6335245db49SHeiko Stübner static struct i2c_driver auo_pixcir_driver = {
6345245db49SHeiko Stübner 	.driver = {
6355245db49SHeiko Stübner 		.name	= "auo_pixcir_ts",
63648877f8eSJonathan Cameron 		.pm	= pm_sleep_ptr(&auo_pixcir_pm_ops),
6377241443fSHeiko Stübner 		.of_match_table	= of_match_ptr(auo_pixcir_ts_dt_idtable),
6385245db49SHeiko Stübner 	},
639d8bde56dSUwe Kleine-König 	.probe		= auo_pixcir_probe,
6405245db49SHeiko Stübner 	.id_table	= auo_pixcir_idtable,
6415245db49SHeiko Stübner };
6425245db49SHeiko Stübner 
6434a533835SDmitry Torokhov module_i2c_driver(auo_pixcir_driver);
6445245db49SHeiko Stübner 
6455245db49SHeiko Stübner MODULE_DESCRIPTION("AUO-PIXCIR touchscreen driver");
6465245db49SHeiko Stübner MODULE_LICENSE("GPL v2");
6475245db49SHeiko Stübner MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
648