Commit | Line | Data |
---|---|---|
59bae1db ZJ |
1 | /* |
2 | * Driver for EETI eGalax Multiple Touch Controller | |
3 | * | |
4 | * Copyright (C) 2011 Freescale Semiconductor, Inc. | |
5 | * | |
6 | * based on max11801_ts.c | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | /* EETI eGalax serial touch screen controller is a I2C based multiple | |
14 | * touch screen controller, it supports 5 point multiple touch. */ | |
15 | ||
16 | /* TODO: | |
17 | - auto idle mode support | |
18 | */ | |
19 | ||
20 | #include <linux/module.h> | |
59bae1db ZJ |
21 | #include <linux/i2c.h> |
22 | #include <linux/interrupt.h> | |
23 | #include <linux/input.h> | |
24 | #include <linux/irq.h> | |
25 | #include <linux/gpio.h> | |
26 | #include <linux/delay.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/bitops.h> | |
29 | #include <linux/input/mt.h> | |
ae495e84 | 30 | #include <linux/of_gpio.h> |
59bae1db ZJ |
31 | |
32 | /* | |
33 | * Mouse Mode: some panel may configure the controller to mouse mode, | |
34 | * which can only report one point at a given time. | |
35 | * This driver will ignore events in this mode. | |
36 | */ | |
37 | #define REPORT_MODE_MOUSE 0x1 | |
38 | /* | |
39 | * Vendor Mode: this mode is used to transfer some vendor specific | |
40 | * messages. | |
41 | * This driver will ignore events in this mode. | |
42 | */ | |
43 | #define REPORT_MODE_VENDOR 0x3 | |
44 | /* Multiple Touch Mode */ | |
45 | #define REPORT_MODE_MTTOUCH 0x4 | |
46 | ||
47 | #define MAX_SUPPORT_POINTS 5 | |
48 | ||
49 | #define EVENT_VALID_OFFSET 7 | |
50 | #define EVENT_VALID_MASK (0x1 << EVENT_VALID_OFFSET) | |
51 | #define EVENT_ID_OFFSET 2 | |
52 | #define EVENT_ID_MASK (0xf << EVENT_ID_OFFSET) | |
53 | #define EVENT_IN_RANGE (0x1 << 1) | |
54 | #define EVENT_DOWN_UP (0X1 << 0) | |
55 | ||
56 | #define MAX_I2C_DATA_LEN 10 | |
57 | ||
58 | #define EGALAX_MAX_X 32760 | |
59 | #define EGALAX_MAX_Y 32760 | |
60 | #define EGALAX_MAX_TRIES 100 | |
61 | ||
62 | struct egalax_ts { | |
63 | struct i2c_client *client; | |
64 | struct input_dev *input_dev; | |
65 | }; | |
66 | ||
67 | static irqreturn_t egalax_ts_interrupt(int irq, void *dev_id) | |
68 | { | |
69 | struct egalax_ts *ts = dev_id; | |
70 | struct input_dev *input_dev = ts->input_dev; | |
71 | struct i2c_client *client = ts->client; | |
72 | u8 buf[MAX_I2C_DATA_LEN]; | |
73 | int id, ret, x, y, z; | |
74 | int tries = 0; | |
75 | bool down, valid; | |
76 | u8 state; | |
77 | ||
78 | do { | |
79 | ret = i2c_master_recv(client, buf, MAX_I2C_DATA_LEN); | |
80 | } while (ret == -EAGAIN && tries++ < EGALAX_MAX_TRIES); | |
81 | ||
82 | if (ret < 0) | |
83 | return IRQ_HANDLED; | |
84 | ||
85 | if (buf[0] != REPORT_MODE_MTTOUCH) { | |
86 | /* ignore mouse events and vendor events */ | |
87 | return IRQ_HANDLED; | |
88 | } | |
89 | ||
90 | state = buf[1]; | |
91 | x = (buf[3] << 8) | buf[2]; | |
92 | y = (buf[5] << 8) | buf[4]; | |
93 | z = (buf[7] << 8) | buf[6]; | |
94 | ||
95 | valid = state & EVENT_VALID_MASK; | |
96 | id = (state & EVENT_ID_MASK) >> EVENT_ID_OFFSET; | |
97 | down = state & EVENT_DOWN_UP; | |
98 | ||
99 | if (!valid || id > MAX_SUPPORT_POINTS) { | |
100 | dev_dbg(&client->dev, "point invalid\n"); | |
101 | return IRQ_HANDLED; | |
102 | } | |
103 | ||
104 | input_mt_slot(input_dev, id); | |
105 | input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, down); | |
106 | ||
107 | dev_dbg(&client->dev, "%s id:%d x:%d y:%d z:%d", | |
108 | down ? "down" : "up", id, x, y, z); | |
109 | ||
110 | if (down) { | |
111 | input_report_abs(input_dev, ABS_MT_POSITION_X, x); | |
112 | input_report_abs(input_dev, ABS_MT_POSITION_Y, y); | |
113 | input_report_abs(input_dev, ABS_MT_PRESSURE, z); | |
114 | } | |
115 | ||
116 | input_mt_report_pointer_emulation(input_dev, true); | |
117 | input_sync(input_dev); | |
118 | ||
119 | return IRQ_HANDLED; | |
120 | } | |
121 | ||
122 | /* wake up controller by an falling edge of interrupt gpio. */ | |
123 | static int egalax_wake_up_device(struct i2c_client *client) | |
124 | { | |
ae495e84 HW |
125 | struct device_node *np = client->dev.of_node; |
126 | int gpio; | |
59bae1db ZJ |
127 | int ret; |
128 | ||
ae495e84 HW |
129 | if (!np) |
130 | return -ENODEV; | |
131 | ||
132 | gpio = of_get_named_gpio(np, "wakeup-gpios", 0); | |
133 | if (!gpio_is_valid(gpio)) | |
134 | return -ENODEV; | |
135 | ||
59bae1db ZJ |
136 | ret = gpio_request(gpio, "egalax_irq"); |
137 | if (ret < 0) { | |
138 | dev_err(&client->dev, | |
139 | "request gpio failed, cannot wake up controller: %d\n", | |
140 | ret); | |
141 | return ret; | |
142 | } | |
143 | ||
144 | /* wake up controller via an falling edge on IRQ gpio. */ | |
145 | gpio_direction_output(gpio, 0); | |
146 | gpio_set_value(gpio, 1); | |
147 | ||
148 | /* controller should be waken up, return irq. */ | |
149 | gpio_direction_input(gpio); | |
150 | gpio_free(gpio); | |
151 | ||
152 | return 0; | |
153 | } | |
154 | ||
5298cc4c | 155 | static int egalax_firmware_version(struct i2c_client *client) |
59bae1db ZJ |
156 | { |
157 | static const u8 cmd[MAX_I2C_DATA_LEN] = { 0x03, 0x03, 0xa, 0x01, 0x41 }; | |
158 | int ret; | |
159 | ||
160 | ret = i2c_master_send(client, cmd, MAX_I2C_DATA_LEN); | |
161 | if (ret < 0) | |
162 | return ret; | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
5298cc4c | 167 | static int egalax_ts_probe(struct i2c_client *client, |
1ffb064e | 168 | const struct i2c_device_id *id) |
59bae1db ZJ |
169 | { |
170 | struct egalax_ts *ts; | |
171 | struct input_dev *input_dev; | |
59bae1db ZJ |
172 | int error; |
173 | ||
1ffb064e | 174 | ts = devm_kzalloc(&client->dev, sizeof(struct egalax_ts), GFP_KERNEL); |
59bae1db ZJ |
175 | if (!ts) { |
176 | dev_err(&client->dev, "Failed to allocate memory\n"); | |
177 | return -ENOMEM; | |
178 | } | |
179 | ||
1ffb064e | 180 | input_dev = devm_input_allocate_device(&client->dev); |
59bae1db ZJ |
181 | if (!input_dev) { |
182 | dev_err(&client->dev, "Failed to allocate memory\n"); | |
1ffb064e | 183 | return -ENOMEM; |
59bae1db ZJ |
184 | } |
185 | ||
186 | ts->client = client; | |
187 | ts->input_dev = input_dev; | |
188 | ||
189 | /* controller may be in sleep, wake it up. */ | |
ae495e84 HW |
190 | error = egalax_wake_up_device(client); |
191 | if (error) { | |
192 | dev_err(&client->dev, "Failed to wake up the controller\n"); | |
1ffb064e | 193 | return error; |
ae495e84 | 194 | } |
59bae1db | 195 | |
1ffb064e AS |
196 | error = egalax_firmware_version(client); |
197 | if (error < 0) { | |
59bae1db | 198 | dev_err(&client->dev, "Failed to read firmware version\n"); |
1ffb064e | 199 | return error; |
59bae1db ZJ |
200 | } |
201 | ||
202 | input_dev->name = "EETI eGalax Touch Screen"; | |
203 | input_dev->id.bustype = BUS_I2C; | |
59bae1db ZJ |
204 | |
205 | __set_bit(EV_ABS, input_dev->evbit); | |
206 | __set_bit(EV_KEY, input_dev->evbit); | |
207 | __set_bit(BTN_TOUCH, input_dev->keybit); | |
208 | ||
209 | input_set_abs_params(input_dev, ABS_X, 0, EGALAX_MAX_X, 0, 0); | |
210 | input_set_abs_params(input_dev, ABS_Y, 0, EGALAX_MAX_Y, 0, 0); | |
211 | input_set_abs_params(input_dev, | |
212 | ABS_MT_POSITION_X, 0, EGALAX_MAX_X, 0, 0); | |
213 | input_set_abs_params(input_dev, | |
3c9cfa78 | 214 | ABS_MT_POSITION_Y, 0, EGALAX_MAX_Y, 0, 0); |
b4adbbef | 215 | input_mt_init_slots(input_dev, MAX_SUPPORT_POINTS, 0); |
59bae1db ZJ |
216 | |
217 | input_set_drvdata(input_dev, ts); | |
218 | ||
1ffb064e AS |
219 | error = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
220 | egalax_ts_interrupt, | |
221 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, | |
222 | "egalax_ts", ts); | |
59bae1db ZJ |
223 | if (error < 0) { |
224 | dev_err(&client->dev, "Failed to register interrupt\n"); | |
1ffb064e | 225 | return error; |
59bae1db ZJ |
226 | } |
227 | ||
228 | error = input_register_device(ts->input_dev); | |
229 | if (error) | |
1ffb064e | 230 | return error; |
59bae1db ZJ |
231 | |
232 | i2c_set_clientdata(client, ts); | |
233 | return 0; | |
59bae1db ZJ |
234 | } |
235 | ||
236 | static const struct i2c_device_id egalax_ts_id[] = { | |
237 | { "egalax_ts", 0 }, | |
238 | { } | |
239 | }; | |
240 | MODULE_DEVICE_TABLE(i2c, egalax_ts_id); | |
241 | ||
02b6a58b | 242 | static int __maybe_unused egalax_ts_suspend(struct device *dev) |
59bae1db ZJ |
243 | { |
244 | static const u8 suspend_cmd[MAX_I2C_DATA_LEN] = { | |
245 | 0x3, 0x6, 0xa, 0x3, 0x36, 0x3f, 0x2, 0, 0, 0 | |
246 | }; | |
247 | struct i2c_client *client = to_i2c_client(dev); | |
248 | int ret; | |
249 | ||
250 | ret = i2c_master_send(client, suspend_cmd, MAX_I2C_DATA_LEN); | |
251 | return ret > 0 ? 0 : ret; | |
252 | } | |
253 | ||
02b6a58b | 254 | static int __maybe_unused egalax_ts_resume(struct device *dev) |
59bae1db ZJ |
255 | { |
256 | struct i2c_client *client = to_i2c_client(dev); | |
257 | ||
258 | return egalax_wake_up_device(client); | |
259 | } | |
59bae1db ZJ |
260 | |
261 | static SIMPLE_DEV_PM_OPS(egalax_ts_pm_ops, egalax_ts_suspend, egalax_ts_resume); | |
262 | ||
a5fd844c | 263 | static const struct of_device_id egalax_ts_dt_ids[] = { |
ae495e84 HW |
264 | { .compatible = "eeti,egalax_ts" }, |
265 | { /* sentinel */ } | |
266 | }; | |
02d9bd05 | 267 | MODULE_DEVICE_TABLE(of, egalax_ts_dt_ids); |
ae495e84 | 268 | |
59bae1db ZJ |
269 | static struct i2c_driver egalax_ts_driver = { |
270 | .driver = { | |
271 | .name = "egalax_ts", | |
59bae1db | 272 | .pm = &egalax_ts_pm_ops, |
085f17c6 | 273 | .of_match_table = egalax_ts_dt_ids, |
59bae1db ZJ |
274 | }, |
275 | .id_table = egalax_ts_id, | |
276 | .probe = egalax_ts_probe, | |
59bae1db ZJ |
277 | }; |
278 | ||
1b92c1cf | 279 | module_i2c_driver(egalax_ts_driver); |
59bae1db ZJ |
280 | |
281 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); | |
282 | MODULE_DESCRIPTION("Touchscreen driver for EETI eGalax touch controller"); | |
283 | MODULE_LICENSE("GPL"); |