]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/input/touchscreen/elan_ts.c
783a72f15d7e0bd706d9b1253bd8331db0c60fc9
[karo-tx-linux.git] / drivers / input / touchscreen / elan_ts.c
1 /*
2  * Copyright (C) 2007-2008 HTC Corporation.
3  *
4  * Copyright (C) 2013 Freescale Semiconductor, Inc.
5  *
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>
10  *
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.
14  *
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.
19  *
20  */
21
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>
34
35 static const char ELAN_TS_NAME[] = "elan-touch";
36
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
42
43 enum {
44         hello_packet = 0x55,
45         idx_coordinate_packet = 0x5a,
46 };
47
48 enum {
49         idx_finger_state = 7,
50 };
51
52 static struct workqueue_struct *elan_wq;
53
54 static struct elan_data {
55         int intr_gpio;
56         int use_irq;
57         struct hrtimer timer;
58         struct work_struct work;
59         struct i2c_client *client;
60         struct input_dev *input;
61         wait_queue_head_t wait;
62 } elan_touch_data;
63
64 /*--------------------------------------------------------------*/
65 static int elan_touch_detect_int_level(void)
66 {
67         int v;
68         v = gpio_get_value(elan_touch_data.intr_gpio);
69
70         return v;
71 }
72
73 static int __elan_touch_poll(struct i2c_client *client)
74 {
75         int status = 0, retry = 20;
76
77         do {
78                 status = elan_touch_detect_int_level();
79                 retry--;
80                 mdelay(20);
81         } while (status == 1 && retry > 0);
82
83         return (status == 0 ? 0 : -ETIMEDOUT);
84 }
85
86 static int elan_touch_poll(struct i2c_client *client)
87 {
88         return __elan_touch_poll(client);
89 }
90
91 static int __hello_packet_handler(struct i2c_client *client)
92 {
93         int rc;
94         uint8_t buf_recv[4] = { 0 };
95
96         rc = elan_touch_poll(client);
97
98         if (rc < 0)
99                 return -EINVAL;
100
101         rc = i2c_master_recv(client, buf_recv, 4);
102
103         if (rc != 4) {
104                 return rc;
105         } else {
106                 int i;
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]);
109
110                 for (i = 0; i < 4; i++)
111                         if (buf_recv[i] != hello_packet)
112                                 return -EINVAL;
113         }
114
115         return 0;
116 }
117
118 static inline int elan_touch_parse_xy(uint8_t *data, uint16_t *x,
119                                       uint16_t *y)
120 {
121         *x = (data[0] & 0xf0);
122         *x <<= 4;
123         *x |= data[1];
124         if (*x >= ELAN_TS_X_MAX)
125                 *x = ELAN_TS_X_MAX;
126         *x = ((((ELAN_TS_X_MAX -
127                  *x) * 1000) / ELAN_TS_X_MAX) * ELAN_USER_X_MAX) / 1000;
128
129         *y = (data[0] & 0x0f);
130         *y <<= 8;
131         *y |= data[2];
132         if (*y >= ELAN_TS_Y_MAX)
133                 *y = ELAN_TS_Y_MAX;
134         *y = ((((ELAN_TS_Y_MAX -
135                  *y) * 1000) / ELAN_TS_Y_MAX) * ELAN_USER_Y_MAX) / 1000;
136
137         return 0;
138 }
139
140 /*      __elan_touch_init -- hand shaking with touch panel
141  *
142  *      1.recv hello packet
143  */
144 static int __elan_touch_init(struct i2c_client *client)
145 {
146         int rc;
147         rc = __hello_packet_handler(client);
148         if (rc < 0)
149                 goto hand_shake_failed;
150
151 hand_shake_failed:
152         return rc;
153 }
154
155 static int elan_touch_recv_data(struct i2c_client *client, uint8_t * buf)
156 {
157         int rc, bytes_to_recv = IDX_PACKET_SIZE;
158
159         if (buf == NULL)
160                 return -EINVAL;
161
162         memset(buf, 0, bytes_to_recv);
163         rc = i2c_master_recv(client, buf, bytes_to_recv);
164         if (rc != bytes_to_recv)
165                 return -EINVAL;
166
167         return rc;
168 }
169
170 static void elan_touch_report_data(struct i2c_client *client, uint8_t * buf)
171 {
172         switch (buf[0]) {
173         case idx_coordinate_packet:
174         {
175                 uint16_t x1, x2, y1, y2;
176                 uint8_t finger_stat;
177
178                 finger_stat = (buf[idx_finger_state] & 0x06) >> 1;
179
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);
198                 }
199                 input_sync(elan_touch_data.input);
200                 break;
201         }
202
203         default:
204                 break;
205         }
206 }
207
208 static void elan_touch_work_func(struct work_struct *work)
209 {
210         int rc;
211         uint8_t buf[IDX_PACKET_SIZE] = { 0 };
212         struct i2c_client *client = elan_touch_data.client;
213
214         if (elan_touch_detect_int_level())
215                 return;
216
217         rc = elan_touch_recv_data(client, buf);
218         if (rc < 0)
219                 return;
220
221         elan_touch_report_data(client, buf);
222 }
223
224 static irqreturn_t elan_touch_ts_interrupt(int irq, void *dev_id)
225 {
226         queue_work(elan_wq, &elan_touch_data.work);
227
228         return IRQ_HANDLED;
229 }
230
231 static enum hrtimer_restart elan_touch_timer_func(struct hrtimer *timer)
232 {
233         queue_work(elan_wq, &elan_touch_data.work);
234         hrtimer_start(&elan_touch_data.timer, ktime_set(0, 12500000),
235                       HRTIMER_MODE_REL);
236
237         return HRTIMER_NORESTART;
238 }
239
240 static int elan_touch_register_interrupt(struct i2c_client *client)
241 {
242         int err = 0;
243
244         if (client->irq) {
245                 elan_touch_data.use_irq = 1;
246                 err =
247                     request_irq(client->irq, elan_touch_ts_interrupt,
248                                 IRQF_TRIGGER_FALLING, ELAN_TS_NAME,
249                                 &elan_touch_data);
250
251                 if (err < 0) {
252                         pr_info("%s(%s): Can't allocate irq %d\n", __FILE__,
253                                __func__, client->irq);
254                         elan_touch_data.use_irq = 0;
255                 }
256         }
257
258         if (!elan_touch_data.use_irq) {
259                 hrtimer_init(&elan_touch_data.timer, CLOCK_MONOTONIC,
260                              HRTIMER_MODE_REL);
261                 elan_touch_data.timer.function = elan_touch_timer_func;
262                 hrtimer_start(&elan_touch_data.timer, ktime_set(1, 0),
263                               HRTIMER_MODE_REL);
264         }
265
266         pr_info("elan ts starts in %s mode.\n",
267                elan_touch_data.use_irq == 1 ? "interrupt" : "polling");
268
269         return 0;
270 }
271
272 static int elan_touch_probe(struct i2c_client *client,
273                             const struct i2c_device_id *id)
274 {
275         struct device_node *np = client->dev.of_node;
276         int gpio_elan_cs, gpio_elan_rst, err = 0;
277
278         if (!np)
279                 return -ENODEV;
280
281         elan_touch_data.intr_gpio = of_get_named_gpio(np, "gpio_intr", 0);
282         if (!gpio_is_valid(elan_touch_data.intr_gpio))
283                 return -ENODEV;
284
285         err = devm_gpio_request_one(&client->dev, elan_touch_data.intr_gpio,
286                                 GPIOF_IN, "gpio_elan_intr");
287         if (err < 0) {
288                 dev_err(&client->dev,
289                         "request gpio failed: %d\n", err);
290                 return err;
291         }
292
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))
296                 return -ENODEV;
297
298         err = devm_gpio_request_one(&client->dev, gpio_elan_cs,
299                                 GPIOF_OUT_INIT_HIGH, "gpio_elan_cs");
300         if (err < 0) {
301                 dev_err(&client->dev,
302                         "request gpio failed: %d\n", err);
303                 return err;
304         }
305         gpio_set_value(gpio_elan_cs, 0);
306
307         gpio_elan_rst = of_get_named_gpio(np, "gpio_elan_rst", 0);
308         if (!gpio_is_valid(gpio_elan_rst))
309                 return -ENODEV;
310
311         err = devm_gpio_request_one(&client->dev, gpio_elan_rst,
312                                 GPIOF_OUT_INIT_HIGH, "gpio_elan_rst");
313         if (err < 0) {
314                 dev_err(&client->dev,
315                         "request gpio failed: %d\n", err);
316                 return err;
317         }
318         gpio_set_value(gpio_elan_rst, 0);
319         msleep(10);
320         gpio_set_value(gpio_elan_rst, 1);
321
322         gpio_set_value(gpio_elan_cs, 1);
323         msleep(100);
324
325         elan_wq = create_singlethread_workqueue("elan_wq");
326         if (!elan_wq) {
327                 err = -ENOMEM;
328                 goto fail;
329         }
330
331         elan_touch_data.client = client;
332         strlcpy(client->name, ELAN_TS_NAME, I2C_NAME_SIZE);
333
334         INIT_WORK(&elan_touch_data.work, elan_touch_work_func);
335
336         elan_touch_data.input = input_allocate_device();
337         if (elan_touch_data.input == NULL) {
338                 err = -ENOMEM;
339                 goto fail;
340         }
341
342         err = __elan_touch_init(client);
343         if (err < 0) {
344                 dev_err(&client->dev, "elan - Read Hello Packet Failed\n");
345                 goto fail;
346         }
347
348         elan_touch_data.input->name = ELAN_TS_NAME;
349         elan_touch_data.input->id.bustype = BUS_I2C;
350
351         set_bit(EV_SYN, elan_touch_data.input->evbit);
352
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);
356
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);
362
363         input_set_abs_params(elan_touch_data.input, ABS_X, 0, ELAN_USER_X_MAX,
364                              0, 0);
365         input_set_abs_params(elan_touch_data.input, ABS_Y, 0, ELAN_USER_Y_MAX,
366                              0, 0);
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);
371
372         err = input_register_device(elan_touch_data.input);
373         if (err < 0)
374                 goto fail;
375
376         elan_touch_register_interrupt(elan_touch_data.client);
377
378         return 0;
379
380 fail:
381         input_free_device(elan_touch_data.input);
382         if (elan_wq)
383                 destroy_workqueue(elan_wq);
384         return err;
385 }
386
387 static int elan_touch_remove(struct i2c_client *client)
388 {
389         if (elan_wq)
390                 destroy_workqueue(elan_wq);
391
392         input_unregister_device(elan_touch_data.input);
393
394         if (elan_touch_data.use_irq)
395                 free_irq(client->irq, client);
396         else
397                 hrtimer_cancel(&elan_touch_data.timer);
398         return 0;
399 }
400
401 /* -------------------------------------------------------------------- */
402 static const struct i2c_device_id elan_touch_id[] = {
403         {"elan-touch", 0},
404         {}
405 };
406
407 static const struct of_device_id elan_dt_ids[] = {
408         {
409                 .compatible = "elan,elan-touch",
410         }, {
411                 /* sentinel */
412         }
413 };
414 MODULE_DEVICE_TABLE(of, elan_dt_ids);
415
416 static int elan_suspend(struct device *dev)
417 {
418         return 0;
419 }
420
421 static int elan_resume(struct device *dev)
422 {
423         uint8_t buf[IDX_PACKET_SIZE] = { 0 };
424
425         if (0 == elan_touch_detect_int_level()) {
426                 dev_dbg(dev, "Got touch during suspend period.\n");
427                 /*
428                  * if touch screen during suspend, recv and drop the
429                  * data, then touch interrupt pin will return high after
430                  * receving data.
431                  */
432                 elan_touch_recv_data(elan_touch_data.client, buf);
433         }
434
435         return 0;
436 }
437
438 static const struct dev_pm_ops elan_dev_pm_ops = {
439         .suspend = elan_suspend,
440         .resume  = elan_resume,
441 };
442
443 static struct i2c_driver elan_touch_driver = {
444         .probe = elan_touch_probe,
445         .remove = elan_touch_remove,
446         .id_table = elan_touch_id,
447         .driver = {
448                    .name = "elan-touch",
449                    .owner = THIS_MODULE,
450                    .of_match_table = elan_dt_ids,
451 #ifdef CONFIG_PM
452                    .pm = &elan_dev_pm_ops,
453 #endif
454                    },
455 };
456
457 static int __init elan_touch_init(void)
458 {
459         return i2c_add_driver(&elan_touch_driver);
460 }
461
462 static void __exit elan_touch_exit(void)
463 {
464         i2c_del_driver(&elan_touch_driver);
465 }
466
467 module_init(elan_touch_init);
468 module_exit(elan_touch_exit);
469
470 MODULE_AUTHOR("Stanley Zeng <stanley.zeng@emc.com.tw>");
471 MODULE_DESCRIPTION("ELAN Touch Screen driver");
472 MODULE_LICENSE("GPL");