1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for EETI eGalax Multiple Touch Controller
5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
7 * based on max11801_ts.c
10 /* EETI eGalax serial touch screen controller is a I2C based multiple
11 * touch screen controller, it supports 5 point multiple touch. */
14 - auto idle mode support
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/input.h>
22 #include <linux/irq.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/bitops.h>
27 #include <linux/input/mt.h>
30 * Mouse Mode: some panel may configure the controller to mouse mode,
31 * which can only report one point at a given time.
32 * This driver will ignore events in this mode.
34 #define REPORT_MODE_MOUSE 0x1
36 * Vendor Mode: this mode is used to transfer some vendor specific
38 * This driver will ignore events in this mode.
40 #define REPORT_MODE_VENDOR 0x3
41 /* Multiple Touch Mode */
42 #define REPORT_MODE_MTTOUCH 0x4
44 #define MAX_SUPPORT_POINTS 5
46 #define EVENT_VALID_OFFSET 7
47 #define EVENT_VALID_MASK (0x1 << EVENT_VALID_OFFSET)
48 #define EVENT_ID_OFFSET 2
49 #define EVENT_ID_MASK (0xf << EVENT_ID_OFFSET)
50 #define EVENT_IN_RANGE (0x1 << 1)
51 #define EVENT_DOWN_UP (0X1 << 0)
53 #define MAX_I2C_DATA_LEN 10
55 #define EGALAX_MAX_X 32760
56 #define EGALAX_MAX_Y 32760
57 #define EGALAX_MAX_TRIES 100
60 struct i2c_client
*client
;
61 struct input_dev
*input_dev
;
64 static irqreturn_t
egalax_ts_interrupt(int irq
, void *dev_id
)
66 struct egalax_ts
*ts
= dev_id
;
67 struct input_dev
*input_dev
= ts
->input_dev
;
68 struct i2c_client
*client
= ts
->client
;
69 u8 buf
[MAX_I2C_DATA_LEN
];
76 ret
= i2c_master_recv(client
, buf
, MAX_I2C_DATA_LEN
);
77 } while (ret
== -EAGAIN
&& tries
++ < EGALAX_MAX_TRIES
);
82 if (buf
[0] != REPORT_MODE_MTTOUCH
) {
83 /* ignore mouse events and vendor events */
88 x
= (buf
[3] << 8) | buf
[2];
89 y
= (buf
[5] << 8) | buf
[4];
90 z
= (buf
[7] << 8) | buf
[6];
92 valid
= state
& EVENT_VALID_MASK
;
93 id
= (state
& EVENT_ID_MASK
) >> EVENT_ID_OFFSET
;
94 down
= state
& EVENT_DOWN_UP
;
96 if (!valid
|| id
> MAX_SUPPORT_POINTS
) {
97 dev_dbg(&client
->dev
, "point invalid\n");
101 input_mt_slot(input_dev
, id
);
102 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, down
);
104 dev_dbg(&client
->dev
, "%s id:%d x:%d y:%d z:%d",
105 down
? "down" : "up", id
, x
, y
, z
);
108 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
109 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
110 input_report_abs(input_dev
, ABS_MT_PRESSURE
, z
);
113 input_mt_report_pointer_emulation(input_dev
, true);
114 input_sync(input_dev
);
119 /* wake up controller by an falling edge of interrupt gpio. */
120 static int egalax_wake_up_device(struct i2c_client
*client
)
122 struct gpio_desc
*gpio
;
125 /* wake up controller via an falling edge on IRQ gpio. */
126 gpio
= gpiod_get(&client
->dev
, "wakeup", GPIOD_OUT_HIGH
);
127 ret
= PTR_ERR_OR_ZERO(gpio
);
129 if (ret
!= -EPROBE_DEFER
)
130 dev_err(&client
->dev
,
131 "failed to request wakeup gpio, cannot wake up controller: %d\n",
136 /* release the line */
137 gpiod_set_value_cansleep(gpio
, 0);
139 /* controller should be woken up, return irq. */
140 gpiod_direction_input(gpio
);
146 static int egalax_firmware_version(struct i2c_client
*client
)
148 static const u8 cmd
[MAX_I2C_DATA_LEN
] = { 0x03, 0x03, 0xa, 0x01, 0x41 };
151 ret
= i2c_master_send(client
, cmd
, MAX_I2C_DATA_LEN
);
158 static int egalax_ts_probe(struct i2c_client
*client
)
160 struct egalax_ts
*ts
;
161 struct input_dev
*input_dev
;
164 ts
= devm_kzalloc(&client
->dev
, sizeof(struct egalax_ts
), GFP_KERNEL
);
166 dev_err(&client
->dev
, "Failed to allocate memory\n");
170 input_dev
= devm_input_allocate_device(&client
->dev
);
172 dev_err(&client
->dev
, "Failed to allocate memory\n");
177 ts
->input_dev
= input_dev
;
179 /* controller may be in sleep, wake it up. */
180 error
= egalax_wake_up_device(client
);
184 error
= egalax_firmware_version(client
);
186 dev_err(&client
->dev
, "Failed to read firmware version\n");
190 input_dev
->name
= "EETI eGalax Touch Screen";
191 input_dev
->id
.bustype
= BUS_I2C
;
193 __set_bit(EV_ABS
, input_dev
->evbit
);
194 __set_bit(EV_KEY
, input_dev
->evbit
);
195 __set_bit(BTN_TOUCH
, input_dev
->keybit
);
197 input_set_abs_params(input_dev
, ABS_X
, 0, EGALAX_MAX_X
, 0, 0);
198 input_set_abs_params(input_dev
, ABS_Y
, 0, EGALAX_MAX_Y
, 0, 0);
199 input_set_abs_params(input_dev
,
200 ABS_MT_POSITION_X
, 0, EGALAX_MAX_X
, 0, 0);
201 input_set_abs_params(input_dev
,
202 ABS_MT_POSITION_Y
, 0, EGALAX_MAX_Y
, 0, 0);
203 input_mt_init_slots(input_dev
, MAX_SUPPORT_POINTS
, 0);
205 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
206 NULL
, egalax_ts_interrupt
,
207 IRQF_ONESHOT
, "egalax_ts", ts
);
209 dev_err(&client
->dev
, "Failed to register interrupt\n");
213 error
= input_register_device(ts
->input_dev
);
220 static const struct i2c_device_id egalax_ts_id
[] = {
224 MODULE_DEVICE_TABLE(i2c
, egalax_ts_id
);
226 static int egalax_ts_suspend(struct device
*dev
)
228 static const u8 suspend_cmd
[MAX_I2C_DATA_LEN
] = {
229 0x3, 0x6, 0xa, 0x3, 0x36, 0x3f, 0x2, 0, 0, 0
231 struct i2c_client
*client
= to_i2c_client(dev
);
234 if (device_may_wakeup(dev
))
235 return enable_irq_wake(client
->irq
);
237 ret
= i2c_master_send(client
, suspend_cmd
, MAX_I2C_DATA_LEN
);
238 return ret
> 0 ? 0 : ret
;
241 static int egalax_ts_resume(struct device
*dev
)
243 struct i2c_client
*client
= to_i2c_client(dev
);
245 if (device_may_wakeup(dev
))
246 return disable_irq_wake(client
->irq
);
248 return egalax_wake_up_device(client
);
251 static DEFINE_SIMPLE_DEV_PM_OPS(egalax_ts_pm_ops
,
252 egalax_ts_suspend
, egalax_ts_resume
);
254 static const struct of_device_id egalax_ts_dt_ids
[] = {
255 { .compatible
= "eeti,egalax_ts" },
258 MODULE_DEVICE_TABLE(of
, egalax_ts_dt_ids
);
260 static struct i2c_driver egalax_ts_driver
= {
263 .pm
= pm_sleep_ptr(&egalax_ts_pm_ops
),
264 .of_match_table
= egalax_ts_dt_ids
,
266 .id_table
= egalax_ts_id
,
267 .probe
= egalax_ts_probe
,
270 module_i2c_driver(egalax_ts_driver
);
272 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
273 MODULE_DESCRIPTION("Touchscreen driver for EETI eGalax touch controller");
274 MODULE_LICENSE("GPL");