1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Driver for AUO in-cell touchscreens
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2011 Heiko Stuebner <heiko@sntech.de>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * loosely based on auo_touch.c from Dell Streak vendor-kernel
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Copyright (c) 2008 QUALCOMM Incorporated.
10*4882a593Smuzhiyun * Copyright (c) 2008 QUALCOMM USA, INC.
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/interrupt.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/input.h>
18*4882a593Smuzhiyun #include <linux/jiffies.h>
19*4882a593Smuzhiyun #include <linux/i2c.h>
20*4882a593Smuzhiyun #include <linux/mutex.h>
21*4882a593Smuzhiyun #include <linux/delay.h>
22*4882a593Smuzhiyun #include <linux/gpio.h>
23*4882a593Smuzhiyun #include <linux/input/auo-pixcir-ts.h>
24*4882a593Smuzhiyun #include <linux/of.h>
25*4882a593Smuzhiyun #include <linux/of_gpio.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun * Coordinate calculation:
29*4882a593Smuzhiyun * X1 = X1_LSB + X1_MSB*256
30*4882a593Smuzhiyun * Y1 = Y1_LSB + Y1_MSB*256
31*4882a593Smuzhiyun * X2 = X2_LSB + X2_MSB*256
32*4882a593Smuzhiyun * Y2 = Y2_LSB + Y2_MSB*256
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun #define AUO_PIXCIR_REG_X1_LSB 0x00
35*4882a593Smuzhiyun #define AUO_PIXCIR_REG_X1_MSB 0x01
36*4882a593Smuzhiyun #define AUO_PIXCIR_REG_Y1_LSB 0x02
37*4882a593Smuzhiyun #define AUO_PIXCIR_REG_Y1_MSB 0x03
38*4882a593Smuzhiyun #define AUO_PIXCIR_REG_X2_LSB 0x04
39*4882a593Smuzhiyun #define AUO_PIXCIR_REG_X2_MSB 0x05
40*4882a593Smuzhiyun #define AUO_PIXCIR_REG_Y2_LSB 0x06
41*4882a593Smuzhiyun #define AUO_PIXCIR_REG_Y2_MSB 0x07
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #define AUO_PIXCIR_REG_STRENGTH 0x0d
44*4882a593Smuzhiyun #define AUO_PIXCIR_REG_STRENGTH_X1_LSB 0x0e
45*4882a593Smuzhiyun #define AUO_PIXCIR_REG_STRENGTH_X1_MSB 0x0f
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #define AUO_PIXCIR_REG_RAW_DATA_X 0x2b
48*4882a593Smuzhiyun #define AUO_PIXCIR_REG_RAW_DATA_Y 0x4f
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun #define AUO_PIXCIR_REG_X_SENSITIVITY 0x6f
51*4882a593Smuzhiyun #define AUO_PIXCIR_REG_Y_SENSITIVITY 0x70
52*4882a593Smuzhiyun #define AUO_PIXCIR_REG_INT_SETTING 0x71
53*4882a593Smuzhiyun #define AUO_PIXCIR_REG_INT_WIDTH 0x72
54*4882a593Smuzhiyun #define AUO_PIXCIR_REG_POWER_MODE 0x73
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun #define AUO_PIXCIR_REG_VERSION 0x77
57*4882a593Smuzhiyun #define AUO_PIXCIR_REG_CALIBRATE 0x78
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun #define AUO_PIXCIR_REG_TOUCHAREA_X1 0x1e
60*4882a593Smuzhiyun #define AUO_PIXCIR_REG_TOUCHAREA_Y1 0x1f
61*4882a593Smuzhiyun #define AUO_PIXCIR_REG_TOUCHAREA_X2 0x20
62*4882a593Smuzhiyun #define AUO_PIXCIR_REG_TOUCHAREA_Y2 0x21
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun #define AUO_PIXCIR_REG_EEPROM_CALIB_X 0x42
65*4882a593Smuzhiyun #define AUO_PIXCIR_REG_EEPROM_CALIB_Y 0xad
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #define AUO_PIXCIR_INT_TPNUM_MASK 0xe0
68*4882a593Smuzhiyun #define AUO_PIXCIR_INT_TPNUM_SHIFT 5
69*4882a593Smuzhiyun #define AUO_PIXCIR_INT_RELEASE (1 << 4)
70*4882a593Smuzhiyun #define AUO_PIXCIR_INT_ENABLE (1 << 3)
71*4882a593Smuzhiyun #define AUO_PIXCIR_INT_POL_HIGH (1 << 2)
72*4882a593Smuzhiyun #define AUO_PIXCIR_INT_MODE_MASK 0x03
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * Power modes:
76*4882a593Smuzhiyun * active: scan speed 60Hz
77*4882a593Smuzhiyun * sleep: scan speed 10Hz can be auto-activated, wakeup on 1st touch
78*4882a593Smuzhiyun * deep sleep: scan speed 1Hz can only be entered or left manually.
79*4882a593Smuzhiyun */
80*4882a593Smuzhiyun #define AUO_PIXCIR_POWER_ACTIVE 0x00
81*4882a593Smuzhiyun #define AUO_PIXCIR_POWER_SLEEP 0x01
82*4882a593Smuzhiyun #define AUO_PIXCIR_POWER_DEEP_SLEEP 0x02
83*4882a593Smuzhiyun #define AUO_PIXCIR_POWER_MASK 0x03
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun #define AUO_PIXCIR_POWER_ALLOW_SLEEP (1 << 2)
86*4882a593Smuzhiyun #define AUO_PIXCIR_POWER_IDLE_TIME(ms) ((ms & 0xf) << 4)
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun #define AUO_PIXCIR_CALIBRATE 0x03
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun #define AUO_PIXCIR_EEPROM_CALIB_X_LEN 62
91*4882a593Smuzhiyun #define AUO_PIXCIR_EEPROM_CALIB_Y_LEN 36
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun #define AUO_PIXCIR_RAW_DATA_X_LEN 18
94*4882a593Smuzhiyun #define AUO_PIXCIR_RAW_DATA_Y_LEN 11
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun #define AUO_PIXCIR_STRENGTH_ENABLE (1 << 0)
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /* Touchscreen absolute values */
99*4882a593Smuzhiyun #define AUO_PIXCIR_REPORT_POINTS 2
100*4882a593Smuzhiyun #define AUO_PIXCIR_MAX_AREA 0xff
101*4882a593Smuzhiyun #define AUO_PIXCIR_PENUP_TIMEOUT_MS 10
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun struct auo_pixcir_ts {
104*4882a593Smuzhiyun struct i2c_client *client;
105*4882a593Smuzhiyun struct input_dev *input;
106*4882a593Smuzhiyun const struct auo_pixcir_ts_platdata *pdata;
107*4882a593Smuzhiyun char phys[32];
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* special handling for touch_indicate interupt mode */
110*4882a593Smuzhiyun bool touch_ind_mode;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun wait_queue_head_t wait;
113*4882a593Smuzhiyun bool stopped;
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun struct auo_point_t {
117*4882a593Smuzhiyun int coord_x;
118*4882a593Smuzhiyun int coord_y;
119*4882a593Smuzhiyun int area_major;
120*4882a593Smuzhiyun int area_minor;
121*4882a593Smuzhiyun int orientation;
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun
auo_pixcir_collect_data(struct auo_pixcir_ts * ts,struct auo_point_t * point)124*4882a593Smuzhiyun static int auo_pixcir_collect_data(struct auo_pixcir_ts *ts,
125*4882a593Smuzhiyun struct auo_point_t *point)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun struct i2c_client *client = ts->client;
128*4882a593Smuzhiyun const struct auo_pixcir_ts_platdata *pdata = ts->pdata;
129*4882a593Smuzhiyun uint8_t raw_coord[8];
130*4882a593Smuzhiyun uint8_t raw_area[4];
131*4882a593Smuzhiyun int i, ret;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /* touch coordinates */
134*4882a593Smuzhiyun ret = i2c_smbus_read_i2c_block_data(client, AUO_PIXCIR_REG_X1_LSB,
135*4882a593Smuzhiyun 8, raw_coord);
136*4882a593Smuzhiyun if (ret < 0) {
137*4882a593Smuzhiyun dev_err(&client->dev, "failed to read coordinate, %d\n", ret);
138*4882a593Smuzhiyun return ret;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /* touch area */
142*4882a593Smuzhiyun ret = i2c_smbus_read_i2c_block_data(client, AUO_PIXCIR_REG_TOUCHAREA_X1,
143*4882a593Smuzhiyun 4, raw_area);
144*4882a593Smuzhiyun if (ret < 0) {
145*4882a593Smuzhiyun dev_err(&client->dev, "could not read touch area, %d\n", ret);
146*4882a593Smuzhiyun return ret;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun for (i = 0; i < AUO_PIXCIR_REPORT_POINTS; i++) {
150*4882a593Smuzhiyun point[i].coord_x =
151*4882a593Smuzhiyun raw_coord[4 * i + 1] << 8 | raw_coord[4 * i];
152*4882a593Smuzhiyun point[i].coord_y =
153*4882a593Smuzhiyun raw_coord[4 * i + 3] << 8 | raw_coord[4 * i + 2];
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun if (point[i].coord_x > pdata->x_max ||
156*4882a593Smuzhiyun point[i].coord_y > pdata->y_max) {
157*4882a593Smuzhiyun dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
158*4882a593Smuzhiyun point[i].coord_x, point[i].coord_y);
159*4882a593Smuzhiyun point[i].coord_x = point[i].coord_y = 0;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /* determine touch major, minor and orientation */
163*4882a593Smuzhiyun point[i].area_major = max(raw_area[2 * i], raw_area[2 * i + 1]);
164*4882a593Smuzhiyun point[i].area_minor = min(raw_area[2 * i], raw_area[2 * i + 1]);
165*4882a593Smuzhiyun point[i].orientation = raw_area[2 * i] > raw_area[2 * i + 1];
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun return 0;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
auo_pixcir_interrupt(int irq,void * dev_id)171*4882a593Smuzhiyun static irqreturn_t auo_pixcir_interrupt(int irq, void *dev_id)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun struct auo_pixcir_ts *ts = dev_id;
174*4882a593Smuzhiyun const struct auo_pixcir_ts_platdata *pdata = ts->pdata;
175*4882a593Smuzhiyun struct auo_point_t point[AUO_PIXCIR_REPORT_POINTS];
176*4882a593Smuzhiyun int i;
177*4882a593Smuzhiyun int ret;
178*4882a593Smuzhiyun int fingers = 0;
179*4882a593Smuzhiyun int abs = -1;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun while (!ts->stopped) {
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /* check for up event in touch touch_ind_mode */
184*4882a593Smuzhiyun if (ts->touch_ind_mode) {
185*4882a593Smuzhiyun if (gpio_get_value(pdata->gpio_int) == 0) {
186*4882a593Smuzhiyun input_mt_sync(ts->input);
187*4882a593Smuzhiyun input_report_key(ts->input, BTN_TOUCH, 0);
188*4882a593Smuzhiyun input_sync(ts->input);
189*4882a593Smuzhiyun break;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun ret = auo_pixcir_collect_data(ts, point);
194*4882a593Smuzhiyun if (ret < 0) {
195*4882a593Smuzhiyun /* we want to loop only in touch_ind_mode */
196*4882a593Smuzhiyun if (!ts->touch_ind_mode)
197*4882a593Smuzhiyun break;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun wait_event_timeout(ts->wait, ts->stopped,
200*4882a593Smuzhiyun msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS));
201*4882a593Smuzhiyun continue;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun for (i = 0; i < AUO_PIXCIR_REPORT_POINTS; i++) {
205*4882a593Smuzhiyun if (point[i].coord_x > 0 || point[i].coord_y > 0) {
206*4882a593Smuzhiyun input_report_abs(ts->input, ABS_MT_POSITION_X,
207*4882a593Smuzhiyun point[i].coord_x);
208*4882a593Smuzhiyun input_report_abs(ts->input, ABS_MT_POSITION_Y,
209*4882a593Smuzhiyun point[i].coord_y);
210*4882a593Smuzhiyun input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
211*4882a593Smuzhiyun point[i].area_major);
212*4882a593Smuzhiyun input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
213*4882a593Smuzhiyun point[i].area_minor);
214*4882a593Smuzhiyun input_report_abs(ts->input, ABS_MT_ORIENTATION,
215*4882a593Smuzhiyun point[i].orientation);
216*4882a593Smuzhiyun input_mt_sync(ts->input);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /* use first finger as source for singletouch */
219*4882a593Smuzhiyun if (fingers == 0)
220*4882a593Smuzhiyun abs = i;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* number of touch points could also be queried
223*4882a593Smuzhiyun * via i2c but would require an additional call
224*4882a593Smuzhiyun */
225*4882a593Smuzhiyun fingers++;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun input_report_key(ts->input, BTN_TOUCH, fingers > 0);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun if (abs > -1) {
232*4882a593Smuzhiyun input_report_abs(ts->input, ABS_X, point[abs].coord_x);
233*4882a593Smuzhiyun input_report_abs(ts->input, ABS_Y, point[abs].coord_y);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun input_sync(ts->input);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* we want to loop only in touch_ind_mode */
239*4882a593Smuzhiyun if (!ts->touch_ind_mode)
240*4882a593Smuzhiyun break;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun wait_event_timeout(ts->wait, ts->stopped,
243*4882a593Smuzhiyun msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS));
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun return IRQ_HANDLED;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun /*
250*4882a593Smuzhiyun * Set the power mode of the device.
251*4882a593Smuzhiyun * Valid modes are
252*4882a593Smuzhiyun * - AUO_PIXCIR_POWER_ACTIVE
253*4882a593Smuzhiyun * - AUO_PIXCIR_POWER_SLEEP - automatically left on first touch
254*4882a593Smuzhiyun * - AUO_PIXCIR_POWER_DEEP_SLEEP
255*4882a593Smuzhiyun */
auo_pixcir_power_mode(struct auo_pixcir_ts * ts,int mode)256*4882a593Smuzhiyun static int auo_pixcir_power_mode(struct auo_pixcir_ts *ts, int mode)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun struct i2c_client *client = ts->client;
259*4882a593Smuzhiyun int ret;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_POWER_MODE);
262*4882a593Smuzhiyun if (ret < 0) {
263*4882a593Smuzhiyun dev_err(&client->dev, "unable to read reg %Xh, %d\n",
264*4882a593Smuzhiyun AUO_PIXCIR_REG_POWER_MODE, ret);
265*4882a593Smuzhiyun return ret;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun ret &= ~AUO_PIXCIR_POWER_MASK;
269*4882a593Smuzhiyun ret |= mode;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_POWER_MODE, ret);
272*4882a593Smuzhiyun if (ret) {
273*4882a593Smuzhiyun dev_err(&client->dev, "unable to write reg %Xh, %d\n",
274*4882a593Smuzhiyun AUO_PIXCIR_REG_POWER_MODE, ret);
275*4882a593Smuzhiyun return ret;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun return 0;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
auo_pixcir_int_config(struct auo_pixcir_ts * ts,int int_setting)281*4882a593Smuzhiyun static int auo_pixcir_int_config(struct auo_pixcir_ts *ts,
282*4882a593Smuzhiyun int int_setting)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun struct i2c_client *client = ts->client;
285*4882a593Smuzhiyun const struct auo_pixcir_ts_platdata *pdata = ts->pdata;
286*4882a593Smuzhiyun int ret;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_INT_SETTING);
289*4882a593Smuzhiyun if (ret < 0) {
290*4882a593Smuzhiyun dev_err(&client->dev, "unable to read reg %Xh, %d\n",
291*4882a593Smuzhiyun AUO_PIXCIR_REG_INT_SETTING, ret);
292*4882a593Smuzhiyun return ret;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun ret &= ~AUO_PIXCIR_INT_MODE_MASK;
296*4882a593Smuzhiyun ret |= int_setting;
297*4882a593Smuzhiyun ret |= AUO_PIXCIR_INT_POL_HIGH; /* always use high for interrupts */
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_INT_SETTING,
300*4882a593Smuzhiyun ret);
301*4882a593Smuzhiyun if (ret < 0) {
302*4882a593Smuzhiyun dev_err(&client->dev, "unable to write reg %Xh, %d\n",
303*4882a593Smuzhiyun AUO_PIXCIR_REG_INT_SETTING, ret);
304*4882a593Smuzhiyun return ret;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun ts->touch_ind_mode = pdata->int_setting == AUO_PIXCIR_INT_TOUCH_IND;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun return 0;
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /* control the generation of interrupts on the device side */
auo_pixcir_int_toggle(struct auo_pixcir_ts * ts,bool enable)313*4882a593Smuzhiyun static int auo_pixcir_int_toggle(struct auo_pixcir_ts *ts, bool enable)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun struct i2c_client *client = ts->client;
316*4882a593Smuzhiyun int ret;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_INT_SETTING);
319*4882a593Smuzhiyun if (ret < 0) {
320*4882a593Smuzhiyun dev_err(&client->dev, "unable to read reg %Xh, %d\n",
321*4882a593Smuzhiyun AUO_PIXCIR_REG_INT_SETTING, ret);
322*4882a593Smuzhiyun return ret;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun if (enable)
326*4882a593Smuzhiyun ret |= AUO_PIXCIR_INT_ENABLE;
327*4882a593Smuzhiyun else
328*4882a593Smuzhiyun ret &= ~AUO_PIXCIR_INT_ENABLE;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_INT_SETTING,
331*4882a593Smuzhiyun ret);
332*4882a593Smuzhiyun if (ret < 0) {
333*4882a593Smuzhiyun dev_err(&client->dev, "unable to write reg %Xh, %d\n",
334*4882a593Smuzhiyun AUO_PIXCIR_REG_INT_SETTING, ret);
335*4882a593Smuzhiyun return ret;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun return 0;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
auo_pixcir_start(struct auo_pixcir_ts * ts)341*4882a593Smuzhiyun static int auo_pixcir_start(struct auo_pixcir_ts *ts)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun struct i2c_client *client = ts->client;
344*4882a593Smuzhiyun int ret;
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun ret = auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_ACTIVE);
347*4882a593Smuzhiyun if (ret < 0) {
348*4882a593Smuzhiyun dev_err(&client->dev, "could not set power mode, %d\n",
349*4882a593Smuzhiyun ret);
350*4882a593Smuzhiyun return ret;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun ts->stopped = false;
354*4882a593Smuzhiyun mb();
355*4882a593Smuzhiyun enable_irq(client->irq);
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun ret = auo_pixcir_int_toggle(ts, 1);
358*4882a593Smuzhiyun if (ret < 0) {
359*4882a593Smuzhiyun dev_err(&client->dev, "could not enable interrupt, %d\n",
360*4882a593Smuzhiyun ret);
361*4882a593Smuzhiyun disable_irq(client->irq);
362*4882a593Smuzhiyun return ret;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun return 0;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
auo_pixcir_stop(struct auo_pixcir_ts * ts)368*4882a593Smuzhiyun static int auo_pixcir_stop(struct auo_pixcir_ts *ts)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun struct i2c_client *client = ts->client;
371*4882a593Smuzhiyun int ret;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun ret = auo_pixcir_int_toggle(ts, 0);
374*4882a593Smuzhiyun if (ret < 0) {
375*4882a593Smuzhiyun dev_err(&client->dev, "could not disable interrupt, %d\n",
376*4882a593Smuzhiyun ret);
377*4882a593Smuzhiyun return ret;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /* disable receiving of interrupts */
381*4882a593Smuzhiyun disable_irq(client->irq);
382*4882a593Smuzhiyun ts->stopped = true;
383*4882a593Smuzhiyun mb();
384*4882a593Smuzhiyun wake_up(&ts->wait);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun return auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_DEEP_SLEEP);
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun
auo_pixcir_input_open(struct input_dev * dev)389*4882a593Smuzhiyun static int auo_pixcir_input_open(struct input_dev *dev)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun struct auo_pixcir_ts *ts = input_get_drvdata(dev);
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun return auo_pixcir_start(ts);
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun
auo_pixcir_input_close(struct input_dev * dev)396*4882a593Smuzhiyun static void auo_pixcir_input_close(struct input_dev *dev)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun struct auo_pixcir_ts *ts = input_get_drvdata(dev);
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun auo_pixcir_stop(ts);
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun
auo_pixcir_suspend(struct device * dev)403*4882a593Smuzhiyun static int __maybe_unused auo_pixcir_suspend(struct device *dev)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
406*4882a593Smuzhiyun struct auo_pixcir_ts *ts = i2c_get_clientdata(client);
407*4882a593Smuzhiyun struct input_dev *input = ts->input;
408*4882a593Smuzhiyun int ret = 0;
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun mutex_lock(&input->mutex);
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun /* when configured as wakeup source, device should always wake system
413*4882a593Smuzhiyun * therefore start device if necessary
414*4882a593Smuzhiyun */
415*4882a593Smuzhiyun if (device_may_wakeup(&client->dev)) {
416*4882a593Smuzhiyun /* need to start device if not open, to be wakeup source */
417*4882a593Smuzhiyun if (!input->users) {
418*4882a593Smuzhiyun ret = auo_pixcir_start(ts);
419*4882a593Smuzhiyun if (ret)
420*4882a593Smuzhiyun goto unlock;
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun enable_irq_wake(client->irq);
424*4882a593Smuzhiyun ret = auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_SLEEP);
425*4882a593Smuzhiyun } else if (input->users) {
426*4882a593Smuzhiyun ret = auo_pixcir_stop(ts);
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun unlock:
430*4882a593Smuzhiyun mutex_unlock(&input->mutex);
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun return ret;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun
auo_pixcir_resume(struct device * dev)435*4882a593Smuzhiyun static int __maybe_unused auo_pixcir_resume(struct device *dev)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
438*4882a593Smuzhiyun struct auo_pixcir_ts *ts = i2c_get_clientdata(client);
439*4882a593Smuzhiyun struct input_dev *input = ts->input;
440*4882a593Smuzhiyun int ret = 0;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun mutex_lock(&input->mutex);
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun if (device_may_wakeup(&client->dev)) {
445*4882a593Smuzhiyun disable_irq_wake(client->irq);
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun /* need to stop device if it was not open on suspend */
448*4882a593Smuzhiyun if (!input->users) {
449*4882a593Smuzhiyun ret = auo_pixcir_stop(ts);
450*4882a593Smuzhiyun if (ret)
451*4882a593Smuzhiyun goto unlock;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun /* device wakes automatically from SLEEP */
455*4882a593Smuzhiyun } else if (input->users) {
456*4882a593Smuzhiyun ret = auo_pixcir_start(ts);
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun unlock:
460*4882a593Smuzhiyun mutex_unlock(&input->mutex);
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun return ret;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(auo_pixcir_pm_ops,
466*4882a593Smuzhiyun auo_pixcir_suspend, auo_pixcir_resume);
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun #ifdef CONFIG_OF
auo_pixcir_parse_dt(struct device * dev)469*4882a593Smuzhiyun static struct auo_pixcir_ts_platdata *auo_pixcir_parse_dt(struct device *dev)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun struct auo_pixcir_ts_platdata *pdata;
472*4882a593Smuzhiyun struct device_node *np = dev->of_node;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun if (!np)
475*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
478*4882a593Smuzhiyun if (!pdata)
479*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun pdata->gpio_int = of_get_gpio(np, 0);
482*4882a593Smuzhiyun if (!gpio_is_valid(pdata->gpio_int)) {
483*4882a593Smuzhiyun dev_err(dev, "failed to get interrupt gpio\n");
484*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun pdata->gpio_rst = of_get_gpio(np, 1);
488*4882a593Smuzhiyun if (!gpio_is_valid(pdata->gpio_rst)) {
489*4882a593Smuzhiyun dev_err(dev, "failed to get reset gpio\n");
490*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun if (of_property_read_u32(np, "x-size", &pdata->x_max)) {
494*4882a593Smuzhiyun dev_err(dev, "failed to get x-size property\n");
495*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun if (of_property_read_u32(np, "y-size", &pdata->y_max)) {
499*4882a593Smuzhiyun dev_err(dev, "failed to get y-size property\n");
500*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /* default to asserting the interrupt when the screen is touched */
504*4882a593Smuzhiyun pdata->int_setting = AUO_PIXCIR_INT_TOUCH_IND;
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun return pdata;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun #else
auo_pixcir_parse_dt(struct device * dev)509*4882a593Smuzhiyun static struct auo_pixcir_ts_platdata *auo_pixcir_parse_dt(struct device *dev)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun #endif
514*4882a593Smuzhiyun
auo_pixcir_reset(void * data)515*4882a593Smuzhiyun static void auo_pixcir_reset(void *data)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun struct auo_pixcir_ts *ts = data;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun gpio_set_value(ts->pdata->gpio_rst, 0);
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun
auo_pixcir_probe(struct i2c_client * client,const struct i2c_device_id * id)522*4882a593Smuzhiyun static int auo_pixcir_probe(struct i2c_client *client,
523*4882a593Smuzhiyun const struct i2c_device_id *id)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun const struct auo_pixcir_ts_platdata *pdata;
526*4882a593Smuzhiyun struct auo_pixcir_ts *ts;
527*4882a593Smuzhiyun struct input_dev *input_dev;
528*4882a593Smuzhiyun int version;
529*4882a593Smuzhiyun int error;
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun pdata = dev_get_platdata(&client->dev);
532*4882a593Smuzhiyun if (!pdata) {
533*4882a593Smuzhiyun pdata = auo_pixcir_parse_dt(&client->dev);
534*4882a593Smuzhiyun if (IS_ERR(pdata))
535*4882a593Smuzhiyun return PTR_ERR(pdata);
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun ts = devm_kzalloc(&client->dev,
539*4882a593Smuzhiyun sizeof(struct auo_pixcir_ts), GFP_KERNEL);
540*4882a593Smuzhiyun if (!ts)
541*4882a593Smuzhiyun return -ENOMEM;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun input_dev = devm_input_allocate_device(&client->dev);
544*4882a593Smuzhiyun if (!input_dev) {
545*4882a593Smuzhiyun dev_err(&client->dev, "could not allocate input device\n");
546*4882a593Smuzhiyun return -ENOMEM;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun ts->pdata = pdata;
550*4882a593Smuzhiyun ts->client = client;
551*4882a593Smuzhiyun ts->input = input_dev;
552*4882a593Smuzhiyun ts->touch_ind_mode = 0;
553*4882a593Smuzhiyun ts->stopped = true;
554*4882a593Smuzhiyun init_waitqueue_head(&ts->wait);
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun snprintf(ts->phys, sizeof(ts->phys),
557*4882a593Smuzhiyun "%s/input0", dev_name(&client->dev));
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun input_dev->name = "AUO-Pixcir touchscreen";
560*4882a593Smuzhiyun input_dev->phys = ts->phys;
561*4882a593Smuzhiyun input_dev->id.bustype = BUS_I2C;
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun input_dev->open = auo_pixcir_input_open;
564*4882a593Smuzhiyun input_dev->close = auo_pixcir_input_close;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun __set_bit(EV_ABS, input_dev->evbit);
567*4882a593Smuzhiyun __set_bit(EV_KEY, input_dev->evbit);
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun __set_bit(BTN_TOUCH, input_dev->keybit);
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun /* For single touch */
572*4882a593Smuzhiyun input_set_abs_params(input_dev, ABS_X, 0, pdata->x_max, 0, 0);
573*4882a593Smuzhiyun input_set_abs_params(input_dev, ABS_Y, 0, pdata->y_max, 0, 0);
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun /* For multi touch */
576*4882a593Smuzhiyun input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
577*4882a593Smuzhiyun pdata->x_max, 0, 0);
578*4882a593Smuzhiyun input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
579*4882a593Smuzhiyun pdata->y_max, 0, 0);
580*4882a593Smuzhiyun input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
581*4882a593Smuzhiyun AUO_PIXCIR_MAX_AREA, 0, 0);
582*4882a593Smuzhiyun input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
583*4882a593Smuzhiyun AUO_PIXCIR_MAX_AREA, 0, 0);
584*4882a593Smuzhiyun input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun input_set_drvdata(ts->input, ts);
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun error = devm_gpio_request_one(&client->dev, pdata->gpio_int,
589*4882a593Smuzhiyun GPIOF_DIR_IN, "auo_pixcir_ts_int");
590*4882a593Smuzhiyun if (error) {
591*4882a593Smuzhiyun dev_err(&client->dev, "request of gpio %d failed, %d\n",
592*4882a593Smuzhiyun pdata->gpio_int, error);
593*4882a593Smuzhiyun return error;
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun error = devm_gpio_request_one(&client->dev, pdata->gpio_rst,
597*4882a593Smuzhiyun GPIOF_DIR_OUT | GPIOF_INIT_HIGH,
598*4882a593Smuzhiyun "auo_pixcir_ts_rst");
599*4882a593Smuzhiyun if (error) {
600*4882a593Smuzhiyun dev_err(&client->dev, "request of gpio %d failed, %d\n",
601*4882a593Smuzhiyun pdata->gpio_rst, error);
602*4882a593Smuzhiyun return error;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun error = devm_add_action_or_reset(&client->dev, auo_pixcir_reset, ts);
606*4882a593Smuzhiyun if (error) {
607*4882a593Smuzhiyun dev_err(&client->dev, "failed to register reset action, %d\n",
608*4882a593Smuzhiyun error);
609*4882a593Smuzhiyun return error;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun msleep(200);
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun version = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_VERSION);
615*4882a593Smuzhiyun if (version < 0) {
616*4882a593Smuzhiyun error = version;
617*4882a593Smuzhiyun return error;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun dev_info(&client->dev, "firmware version 0x%X\n", version);
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun error = auo_pixcir_int_config(ts, pdata->int_setting);
623*4882a593Smuzhiyun if (error)
624*4882a593Smuzhiyun return error;
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun error = devm_request_threaded_irq(&client->dev, client->irq,
627*4882a593Smuzhiyun NULL, auo_pixcir_interrupt,
628*4882a593Smuzhiyun IRQF_TRIGGER_RISING | IRQF_ONESHOT,
629*4882a593Smuzhiyun input_dev->name, ts);
630*4882a593Smuzhiyun if (error) {
631*4882a593Smuzhiyun dev_err(&client->dev, "irq %d requested failed, %d\n",
632*4882a593Smuzhiyun client->irq, error);
633*4882a593Smuzhiyun return error;
634*4882a593Smuzhiyun }
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun /* stop device and put it into deep sleep until it is opened */
637*4882a593Smuzhiyun error = auo_pixcir_stop(ts);
638*4882a593Smuzhiyun if (error)
639*4882a593Smuzhiyun return error;
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun error = input_register_device(input_dev);
642*4882a593Smuzhiyun if (error) {
643*4882a593Smuzhiyun dev_err(&client->dev, "could not register input device, %d\n",
644*4882a593Smuzhiyun error);
645*4882a593Smuzhiyun return error;
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun i2c_set_clientdata(client, ts);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun return 0;
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun static const struct i2c_device_id auo_pixcir_idtable[] = {
654*4882a593Smuzhiyun { "auo_pixcir_ts", 0 },
655*4882a593Smuzhiyun { }
656*4882a593Smuzhiyun };
657*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, auo_pixcir_idtable);
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun #ifdef CONFIG_OF
660*4882a593Smuzhiyun static const struct of_device_id auo_pixcir_ts_dt_idtable[] = {
661*4882a593Smuzhiyun { .compatible = "auo,auo_pixcir_ts" },
662*4882a593Smuzhiyun {},
663*4882a593Smuzhiyun };
664*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, auo_pixcir_ts_dt_idtable);
665*4882a593Smuzhiyun #endif
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun static struct i2c_driver auo_pixcir_driver = {
668*4882a593Smuzhiyun .driver = {
669*4882a593Smuzhiyun .name = "auo_pixcir_ts",
670*4882a593Smuzhiyun .pm = &auo_pixcir_pm_ops,
671*4882a593Smuzhiyun .of_match_table = of_match_ptr(auo_pixcir_ts_dt_idtable),
672*4882a593Smuzhiyun },
673*4882a593Smuzhiyun .probe = auo_pixcir_probe,
674*4882a593Smuzhiyun .id_table = auo_pixcir_idtable,
675*4882a593Smuzhiyun };
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun module_i2c_driver(auo_pixcir_driver);
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun MODULE_DESCRIPTION("AUO-PIXCIR touchscreen driver");
680*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
681*4882a593Smuzhiyun MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
682