2 * Copyright (C) 2007-2008 HTC Corporation.
4 * Copyright (C) 2013 Freescale Semiconductor, Inc.
6 * This driver is adapted from elan8232_i2c.c written by Shan-Fu Chiou
7 * <sfchiou@gmail.com> and Jay Tu <jay_tu@htc.com>.
8 * This driver is also adapted from the ELAN Touch Screen driver
9 * written by Stanley Zeng <stanley.zeng@emc.com.tw>
11 * This software is licensed under the terms of the GNU General Public
12 * License version 2, as published by the Free Software Foundation, and
13 * may be copied, distributed, and modified under those terms.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
22 #include <linux/input.h>
23 #include <linux/device.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/jiffies.h>
27 #include <linux/interrupt.h>
28 #include <linux/i2c.h>
29 #include <linux/delay.h>
30 #include <linux/hrtimer.h>
31 #include <linux/of_gpio.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/gpio.h>
35 static const char ELAN_TS_NAME[] = "elan-touch";
37 #define ELAN_TS_X_MAX 1088
38 #define ELAN_TS_Y_MAX 768
39 #define ELAN_USER_X_MAX 800
40 #define ELAN_USER_Y_MAX 600
41 #define IDX_PACKET_SIZE 8
45 idx_coordinate_packet = 0x5a,
52 static struct workqueue_struct *elan_wq;
54 static struct elan_data {
58 struct work_struct work;
59 struct i2c_client *client;
60 struct input_dev *input;
61 wait_queue_head_t wait;
64 /*--------------------------------------------------------------*/
65 static int elan_touch_detect_int_level(void)
68 v = gpio_get_value(elan_touch_data.intr_gpio);
73 static int __elan_touch_poll(struct i2c_client *client)
75 int status = 0, retry = 20;
78 status = elan_touch_detect_int_level();
81 } while (status == 1 && retry > 0);
83 return (status == 0 ? 0 : -ETIMEDOUT);
86 static int elan_touch_poll(struct i2c_client *client)
88 return __elan_touch_poll(client);
91 static int __hello_packet_handler(struct i2c_client *client)
94 uint8_t buf_recv[4] = { 0 };
96 rc = elan_touch_poll(client);
101 rc = i2c_master_recv(client, buf_recv, 4);
107 pr_info("hello packet: [0x%02x 0x%02x 0x%02x 0x%02x]\n",
108 buf_recv[0], buf_recv[1], buf_recv[2], buf_recv[3]);
110 for (i = 0; i < 4; i++)
111 if (buf_recv[i] != hello_packet)
118 static inline int elan_touch_parse_xy(uint8_t *data, uint16_t *x,
121 *x = (data[0] & 0xf0);
124 if (*x >= ELAN_TS_X_MAX)
126 *x = ((((ELAN_TS_X_MAX -
127 *x) * 1000) / ELAN_TS_X_MAX) * ELAN_USER_X_MAX) / 1000;
129 *y = (data[0] & 0x0f);
132 if (*y >= ELAN_TS_Y_MAX)
134 *y = ((((ELAN_TS_Y_MAX -
135 *y) * 1000) / ELAN_TS_Y_MAX) * ELAN_USER_Y_MAX) / 1000;
140 /* __elan_touch_init -- hand shaking with touch panel
142 * 1.recv hello packet
144 static int __elan_touch_init(struct i2c_client *client)
147 rc = __hello_packet_handler(client);
149 goto hand_shake_failed;
155 static int elan_touch_recv_data(struct i2c_client *client, uint8_t * buf)
157 int rc, bytes_to_recv = IDX_PACKET_SIZE;
162 memset(buf, 0, bytes_to_recv);
163 rc = i2c_master_recv(client, buf, bytes_to_recv);
164 if (rc != bytes_to_recv)
170 static void elan_touch_report_data(struct i2c_client *client, uint8_t * buf)
173 case idx_coordinate_packet:
175 uint16_t x1, x2, y1, y2;
178 finger_stat = (buf[idx_finger_state] & 0x06) >> 1;
180 if (finger_stat == 0) {
181 input_report_key(elan_touch_data.input, BTN_TOUCH, 0);
182 input_report_key(elan_touch_data.input, BTN_2, 0);
183 } else if (finger_stat == 1) {
184 elan_touch_parse_xy(&buf[1], &x1, &y1);
185 input_report_abs(elan_touch_data.input, ABS_X, x1);
186 input_report_abs(elan_touch_data.input, ABS_Y, y1);
187 input_report_key(elan_touch_data.input, BTN_TOUCH, 1);
188 input_report_key(elan_touch_data.input, BTN_2, 0);
189 } else if (finger_stat == 2) {
190 elan_touch_parse_xy(&buf[1], &x1, &y1);
191 input_report_abs(elan_touch_data.input, ABS_X, x1);
192 input_report_abs(elan_touch_data.input, ABS_Y, y1);
193 input_report_key(elan_touch_data.input, BTN_TOUCH, 1);
194 elan_touch_parse_xy(&buf[4], &x2, &y2);
195 input_report_abs(elan_touch_data.input, ABS_HAT0X, x2);
196 input_report_abs(elan_touch_data.input, ABS_HAT0Y, y2);
197 input_report_key(elan_touch_data.input, BTN_2, 1);
199 input_sync(elan_touch_data.input);
208 static void elan_touch_work_func(struct work_struct *work)
211 uint8_t buf[IDX_PACKET_SIZE] = { 0 };
212 struct i2c_client *client = elan_touch_data.client;
214 if (elan_touch_detect_int_level())
217 rc = elan_touch_recv_data(client, buf);
221 elan_touch_report_data(client, buf);
224 static irqreturn_t elan_touch_ts_interrupt(int irq, void *dev_id)
226 queue_work(elan_wq, &elan_touch_data.work);
231 static enum hrtimer_restart elan_touch_timer_func(struct hrtimer *timer)
233 queue_work(elan_wq, &elan_touch_data.work);
234 hrtimer_start(&elan_touch_data.timer, ktime_set(0, 12500000),
237 return HRTIMER_NORESTART;
240 static int elan_touch_register_interrupt(struct i2c_client *client)
245 elan_touch_data.use_irq = 1;
247 request_irq(client->irq, elan_touch_ts_interrupt,
248 IRQF_TRIGGER_FALLING, ELAN_TS_NAME,
252 pr_info("%s(%s): Can't allocate irq %d\n", __FILE__,
253 __func__, client->irq);
254 elan_touch_data.use_irq = 0;
258 if (!elan_touch_data.use_irq) {
259 hrtimer_init(&elan_touch_data.timer, CLOCK_MONOTONIC,
261 elan_touch_data.timer.function = elan_touch_timer_func;
262 hrtimer_start(&elan_touch_data.timer, ktime_set(1, 0),
266 pr_info("elan ts starts in %s mode.\n",
267 elan_touch_data.use_irq == 1 ? "interrupt" : "polling");
272 static int elan_touch_probe(struct i2c_client *client,
273 const struct i2c_device_id *id)
275 struct device_node *np = client->dev.of_node;
276 int gpio_elan_cs, gpio_elan_rst, err = 0;
281 elan_touch_data.intr_gpio = of_get_named_gpio(np, "gpio_intr", 0);
282 if (!gpio_is_valid(elan_touch_data.intr_gpio))
285 err = devm_gpio_request_one(&client->dev, elan_touch_data.intr_gpio,
286 GPIOF_IN, "gpio_elan_intr");
288 dev_err(&client->dev,
289 "request gpio failed: %d\n", err);
293 /* elan touch init */
294 gpio_elan_cs = of_get_named_gpio(np, "gpio_elan_cs", 0);
295 if (!gpio_is_valid(gpio_elan_cs))
298 err = devm_gpio_request_one(&client->dev, gpio_elan_cs,
299 GPIOF_OUT_INIT_HIGH, "gpio_elan_cs");
301 dev_err(&client->dev,
302 "request gpio failed: %d\n", err);
305 gpio_set_value(gpio_elan_cs, 0);
307 gpio_elan_rst = of_get_named_gpio(np, "gpio_elan_rst", 0);
308 if (!gpio_is_valid(gpio_elan_rst))
311 err = devm_gpio_request_one(&client->dev, gpio_elan_rst,
312 GPIOF_OUT_INIT_HIGH, "gpio_elan_rst");
314 dev_err(&client->dev,
315 "request gpio failed: %d\n", err);
318 gpio_set_value(gpio_elan_rst, 0);
320 gpio_set_value(gpio_elan_rst, 1);
322 gpio_set_value(gpio_elan_cs, 1);
325 elan_wq = create_singlethread_workqueue("elan_wq");
331 elan_touch_data.client = client;
332 strlcpy(client->name, ELAN_TS_NAME, I2C_NAME_SIZE);
334 INIT_WORK(&elan_touch_data.work, elan_touch_work_func);
336 elan_touch_data.input = input_allocate_device();
337 if (elan_touch_data.input == NULL) {
342 err = __elan_touch_init(client);
344 dev_err(&client->dev, "elan - Read Hello Packet Failed\n");
348 elan_touch_data.input->name = ELAN_TS_NAME;
349 elan_touch_data.input->id.bustype = BUS_I2C;
351 set_bit(EV_SYN, elan_touch_data.input->evbit);
353 set_bit(EV_KEY, elan_touch_data.input->evbit);
354 set_bit(BTN_TOUCH, elan_touch_data.input->keybit);
355 set_bit(BTN_2, elan_touch_data.input->keybit);
357 set_bit(EV_ABS, elan_touch_data.input->evbit);
358 set_bit(ABS_X, elan_touch_data.input->absbit);
359 set_bit(ABS_Y, elan_touch_data.input->absbit);
360 set_bit(ABS_HAT0X, elan_touch_data.input->absbit);
361 set_bit(ABS_HAT0Y, elan_touch_data.input->absbit);
363 input_set_abs_params(elan_touch_data.input, ABS_X, 0, ELAN_USER_X_MAX,
365 input_set_abs_params(elan_touch_data.input, ABS_Y, 0, ELAN_USER_Y_MAX,
367 input_set_abs_params(elan_touch_data.input, ABS_HAT0X, 0,
368 ELAN_USER_X_MAX, 0, 0);
369 input_set_abs_params(elan_touch_data.input, ABS_HAT0Y, 0,
370 ELAN_USER_Y_MAX, 0, 0);
372 err = input_register_device(elan_touch_data.input);
376 elan_touch_register_interrupt(elan_touch_data.client);
381 input_free_device(elan_touch_data.input);
383 destroy_workqueue(elan_wq);
387 static int elan_touch_remove(struct i2c_client *client)
390 destroy_workqueue(elan_wq);
392 input_unregister_device(elan_touch_data.input);
394 if (elan_touch_data.use_irq)
395 free_irq(client->irq, client);
397 hrtimer_cancel(&elan_touch_data.timer);
401 /* -------------------------------------------------------------------- */
402 static const struct i2c_device_id elan_touch_id[] = {
407 static const struct of_device_id elan_dt_ids[] = {
409 .compatible = "elan,elan-touch",
414 MODULE_DEVICE_TABLE(of, elan_dt_ids);
416 static int elan_suspend(struct device *dev)
421 static int elan_resume(struct device *dev)
423 uint8_t buf[IDX_PACKET_SIZE] = { 0 };
425 if (0 == elan_touch_detect_int_level()) {
426 dev_dbg(dev, "Got touch during suspend period.\n");
428 * if touch screen during suspend, recv and drop the
429 * data, then touch interrupt pin will return high after
432 elan_touch_recv_data(elan_touch_data.client, buf);
438 static const struct dev_pm_ops elan_dev_pm_ops = {
439 .suspend = elan_suspend,
440 .resume = elan_resume,
443 static struct i2c_driver elan_touch_driver = {
444 .probe = elan_touch_probe,
445 .remove = elan_touch_remove,
446 .id_table = elan_touch_id,
448 .name = "elan-touch",
449 .owner = THIS_MODULE,
450 .of_match_table = elan_dt_ids,
452 .pm = &elan_dev_pm_ops,
457 static int __init elan_touch_init(void)
459 return i2c_add_driver(&elan_touch_driver);
462 static void __exit elan_touch_exit(void)
464 i2c_del_driver(&elan_touch_driver);
467 module_init(elan_touch_init);
468 module_exit(elan_touch_exit);
470 MODULE_AUTHOR("Stanley Zeng <stanley.zeng@emc.com.tw>");
471 MODULE_DESCRIPTION("ELAN Touch Screen driver");
472 MODULE_LICENSE("GPL");