input: touch: sharp: update to version 12.
[linux-3.10.git] / drivers / input / touchscreen / lr388k7_ts.c
1 /*
2  * LR388K7 touchscreen driver
3  *
4  * Copyright (C) 2014, Sharp Corporation
5  *
6  * Author: Makoto Itsuki <itsuki.makoto@sharp.co.jp>
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 as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/delay.h>
20 #include <linux/gpio.h>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23 #include <linux/of_gpio.h>
24 #include <linux/pm.h>
25 #include <linux/spi/spi.h>
26 #include <linux/spi/lr388k7_ts.h>
27 #include <linux/input/mt.h>
28 #include <linux/miscdevice.h>
29 #include <linux/sysfs.h>
30 #include <linux/clk.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/uaccess.h>
33 #include <linux/workqueue.h>
34
35 /* DEBUG */
36 #ifndef DEBUG_LR388K7
37 #define DEBUG_LR388K7
38 #endif
39
40 /* INPUT_ENABLE */
41 #define INPUT_ENABLE
42 /* ACTIVE */
43 /* #define ACTIVE_ENABLE */
44 /* UDEV */
45 #define UDEV_ENABLE
46
47
48 #ifdef __min
49 #undef __min
50 #define __min(x, y) ((x) < (y) ? (x) : (y))
51 #else
52 #define __min(x, y) ((x) < (y) ? (x) : (y))
53 #endif
54
55 #ifdef __max
56 #undef __max
57 #define __max(x, y) ((x) > (y) ? (x) : (y))
58 #else
59 #define __max(x, y) ((x) > (y) ? (x) : (y))
60 #endif
61
62 #ifdef __neg_chk
63 #undef __neg_chk
64 #define __neg_chk(a) ((a) < 0 ? 0 : (a))
65 #else
66 #define __neg_chk(a) ((a) < 0 ? 0 : (a))
67 #endif
68
69
70 #define MAX_16BIT                       0xffff
71 #define MAX_12BIT                       0xfff
72 #define MAX_10BIT                       0x3ff
73 #define K7_MAX_TOUCH_NUM (10)
74 #define K7_MAX_SPEED_HZ (25000000)
75 #define K7_RD_HEADER_SIZE (5)
76 #define K7_WR_OPCODE (0x02)
77 #define K7_RD_OPCODE (0x0B)
78 #define K7_STATE_CTL_ADDR (0x000024)
79 #define K7_INT_STATUS_ADDR (0x000000)
80 #define K7_IND_DONE_ADDR (0x0003E1)
81 #define K7_DATA_OFFSET (0x1000)
82 #define K7_DATA_HEADER_WORD_SIZE (0x08)
83 #define K7_DATA_HEADER_BYTE_SIZE (K7_DATA_HEADER_WORD_SIZE * 2)
84 #define K7_DATA_READ_SIZE (K7_DATA_HEADER_BYTE_SIZE + K7_RD_HEADER_SIZE)
85 #define K7_DATA_SIZE (8800 + K7_DATA_READ_SIZE)
86 #define K7_CMD_DATA_OFFSET (0x030400)
87 #define K7_CMD_DATA_SIZE (251 + K7_RD_HEADER_SIZE)
88 #define K7_Q_SIZE (8)
89 #define K7_DRIVER_VERSION (12)
90 #define K7_INT_STATUS_MASK_DATA (0x01)
91 #define K7_INT_STATUS_MASK_BOOT (0x02)
92 #define K7_INT_STATUS_MASK_CMD  (0x04)
93 #define K7_INT_STATUS_MASK_ERR  (0x08)
94 #define K7_INT_STATUS_MASK_CRC  (0x10)
95 #define K7_POWER_CTL_TAP_WAKEUP (0x00)
96 #define K7_POWER_CTL_SLEEP (0x01)
97 #define K7_POWER_CTL_RESUME (0x02)
98 #define K7_REMOTE_WAKEUP_CODE (0xEA)
99 #define K7_DEFAULT_MODE (3)
100
101 enum K7_SCAN_STATE_OPTIONS {
102         K7_SCAN_STATE_IDLE = 0,
103         K7_SCAN_STATE_ACTIVE,
104 };
105
106 enum K7_SLOW_SCAN_OPTIONS {
107         K7_SLOW_SCAN_30 = 30,
108         K7_SLOW_SCAN_60 = 60,
109         K7_SLOW_SCAN_100 = 100,
110         K7_SLOW_SCAN_120 = 120,
111 };
112
113 enum K7_NUM_TAP_OPTIONS {
114         K7_NUM_TAP_2 = 2,
115         K7_NUM_TAP_3,
116         K7_NUM_TAP_4,
117 };
118
119 struct lr388k7_ts_parameter {
120         u32 u32_pid;
121         bool b_is_init_finish;
122         bool b_is_calc_finish;
123         bool b_is_suspended;
124         bool b_is_reset;
125         u32 u32SCK;
126         u16 u16fw_ver;
127         u16 u16module_ver;
128         struct workqueue_struct *st_wq_k7;
129         struct work_struct st_work_k7;
130 };
131
132 struct lr388k7 {
133         struct spi_device       *spi;
134         struct device           *dev;
135         struct input_dev        *idev;
136 #if defined(ACTIVE_ENABLE)
137         struct input_dev        *idev_active;
138         int                     tool;
139         unsigned int            gpio_clk_sel;
140 #endif
141         char                    phys[32];
142         struct mutex            mutex;
143         unsigned int            irq;
144         unsigned int            gpio_reset;
145         unsigned int            gpio_irq;
146         spinlock_t              lock;
147         struct timer_list       timer;
148         unsigned int            max_num_touch;
149         int                     max_x;
150         int                     max_y;
151         int                     max_z;
152         int                     platform_id;
153         bool                    flip_x;
154         bool                    flip_y;
155         bool                    swap_xy;
156         bool                    opened;
157         bool                    suspended;
158         bool                    b_eraser_active;
159         struct clk              *clk;
160         struct regulator        *regulator_3v3;
161         struct regulator        *regulator_1v8;
162 };
163
164 struct lr388k7_queue_info {
165         u8(*p_queue)[K7_DATA_SIZE];
166         u16 u16_front;
167         u16 u16_rear;
168 };
169
170 struct lr388k7_cmd_queue_info {
171         u8(*p_queue)[K7_CMD_DATA_SIZE];
172         u16 u16_front;
173         u16 u16_rear;
174 };
175
176 struct lr388k7_touch_report {
177         u8 u8_num_of_touch;
178         struct st_touch_point_t tc[K7_MAX_TOUCH_NUM];
179 };
180
181 #if defined(ACTIVE_ENABLE)
182 struct lr388k7_active_report {
183         s16 x;
184         s16 y;
185         u16 z;
186         u8 status;
187 };
188 #endif
189
190
191 #if defined(UDEV_ENABLE)
192 struct lr388k7_udev_event {
193         u8 str[512];
194 };
195 #endif
196
197 static int dev_open(struct inode *inode, struct file *filp);
198 static int dev_release(struct inode *inode, struct file *filp);
199 static ssize_t
200 dev_read(struct file *filp, char __user *buf, size_t count, loff_t *pos);
201 static ssize_t
202 dev_write(struct file *filp, const char __user *buf,
203           size_t count, loff_t *pos);
204 static long dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
205
206 static const struct file_operations dev_fops = {
207         .owner = THIS_MODULE,
208         .open = dev_open,
209         .release = dev_release,
210         .read = dev_read,
211         .write = dev_write,
212         .unlocked_ioctl = dev_ioctl,
213         .compat_ioctl = dev_ioctl,
214 };
215
216 static struct miscdevice lr388k7_ts_miscdev = {
217         .minor = MISC_DYNAMIC_MINOR,
218         .name = "touch",
219         .fops = &dev_fops,
220 };
221
222 static struct spi_device *g_spi;
223 static struct lr388k7_ts_parameter g_st_state;
224 static struct lr388k7_queue_info g_st_q;
225 static struct lr388k7_cmd_queue_info g_st_cmd_q;
226 static struct lr388k7_ts_dbg g_st_dbg;
227 static u8 g_u8_mode;
228 static u8 g_u8_scan;
229 static u16 g_u16_wakeup_enable;
230 static u32 g_u32_raw_data_size = 50 * 88 * 2;
231
232 #define IS_DBG (g_st_dbg.u8ForceCap == 1 || \
233                 g_st_dbg.u8Dump == 1 || \
234                 g_st_dbg.slowscan.enable == 1 ? true : false)
235
236 static int lr388k7_spi_write(u8 *txbuf, size_t len);
237 static int lr388k7_spi_read(u8 *txbuf, u8 *rxbuf, size_t len);
238 static void lr388k7_init_parameter(void);
239 static long lr388k7_spi_read_cmd_data(u8 *p, size_t count);
240 static int lr388k7_input_enable(struct input_dev *idev);
241 static int lr388k7_input_disable(struct input_dev *idev);
242
243 static void lr388k7_queue_reset(void)
244 {
245         g_st_q.u16_rear = 0;
246         g_st_q.u16_front = 0;
247         g_st_cmd_q.u16_rear = 0;
248         g_st_cmd_q.u16_front = 0;
249 }
250
251 static int lr388k7_hardreset(unsigned long wait)
252 {
253         struct lr388k7 *ts = spi_get_drvdata(g_spi);
254         if (!ts)
255                 return false;
256
257         gpio_set_value(ts->gpio_reset, 0);
258         g_st_state.b_is_reset = true;
259         usleep_range(wait * 1000, wait * 1000 + 1000);
260         gpio_set_value(ts->gpio_reset, 1);
261         g_st_state.b_is_reset = false;
262         return true;
263 }
264
265 #if defined(ACTIVE_ENABLE)
266 static int lr388k7_set_clk_sel(unsigned int sel)
267 {
268         struct lr388k7 *ts = spi_get_drvdata(g_spi);
269
270         if (!ts)
271                 return false;
272
273         if (!sel)
274                 gpio_set_value(ts->gpio_clk_sel, 0);
275         else
276                 gpio_set_value(ts->gpio_clk_sel, 1);
277         return true;
278 }
279 #endif
280
281 static int lr388k7_queue_init(void)
282 {
283         lr388k7_queue_reset();
284         /*
285          * Memory Allocation
286          */
287         g_st_q.p_queue = kmalloc(K7_DATA_SIZE * K7_Q_SIZE, GFP_KERNEL);
288         if (g_st_q.p_queue == NULL)
289                 return -ENOMEM;
290
291         g_st_cmd_q.p_queue = kmalloc(K7_CMD_DATA_SIZE * K7_Q_SIZE, GFP_KERNEL);
292         if (g_st_cmd_q.p_queue == NULL) {
293                 kfree(g_st_q.p_queue);
294                 return -ENOMEM;
295         }
296
297         return 0;
298 }
299
300 static void lr388k7_queue_free(void)
301 {
302         if (!g_st_q.p_queue)
303                 return;
304         kfree(g_st_q.p_queue);
305         g_st_q.p_queue = NULL;
306
307         if (!g_st_cmd_q.p_queue)
308                 return;
309         kfree(g_st_cmd_q.p_queue);
310         g_st_cmd_q.p_queue = NULL;
311 }
312
313
314 static int lr388k7_queue_is_full(void)
315 {
316         u16 u16_front = g_st_q.u16_front;
317         if (g_st_q.u16_rear + 1 == u16_front)
318                 return 1;
319
320         if ((g_st_q.u16_rear == (K7_Q_SIZE - 1)) && (u16_front == 0))
321                 return 1;
322
323         return 0;
324 }
325
326 static int lr388k7_queue_is_empty(void)
327 {
328         if (g_st_q.u16_rear == g_st_q.u16_front)
329                 return 1;
330         return 0;
331 }
332
333 static int lr388k7_cmd_queue_is_full(void)
334 {
335         u16 u16_front = g_st_cmd_q.u16_front;
336         if (g_st_cmd_q.u16_rear + 1 == u16_front)
337                 return 1;
338
339         if ((g_st_cmd_q.u16_rear == (K7_Q_SIZE - 1)) && (u16_front == 0))
340                 return 1;
341
342         return 0;
343 }
344
345 static int lr388k7_cmd_queue_is_empty(void)
346 {
347         if (g_st_cmd_q.u16_rear == g_st_cmd_q.u16_front)
348                 return 1;
349         return 0;
350 }
351
352 static void *lr388k7_enqueue_start(void)
353 {
354         if (!g_st_q.p_queue)
355                 return NULL;
356
357         if (!lr388k7_queue_is_full())
358                 return &g_st_q.p_queue[g_st_q.u16_rear];
359
360         return NULL;
361 }
362
363 static void lr388k7_enqueue_finish(void)
364 {
365         if (g_st_q.u16_rear == (K7_Q_SIZE - 1))
366                 g_st_q.u16_rear = 0;
367         else
368                 g_st_q.u16_rear++;
369 }
370
371 static void *lr388k7_dequeue_start(void)
372 {
373         if (!lr388k7_queue_is_empty())
374                 return &g_st_q.p_queue[g_st_q.u16_front];
375
376         return NULL;
377 }
378
379 static void lr388k7_dequeue_finish(void)
380 {
381         if (g_st_q.u16_front == (K7_Q_SIZE - 1))
382                 g_st_q.u16_front = 0;
383         else
384                 g_st_q.u16_front++;
385 }
386
387 static int lr388k7_queue_read_raw_data(u8 *p, u32 u32_len)
388 {
389         u8 *p_queue;
390         u32 u32_ret;
391         p_queue = lr388k7_dequeue_start();
392         if (!p_queue)
393                 return 0;
394
395         u32_ret = copy_to_user(p, p_queue, u32_len);
396         if (u32_ret != 0)
397                 return 0;
398
399         lr388k7_dequeue_finish();
400         return 1;
401 }
402
403 static void *lr388k7_cmd_enqueue_start(void)
404 {
405         if (!g_st_cmd_q.p_queue)
406                 return NULL;
407
408         if (!lr388k7_cmd_queue_is_full())
409                 return &g_st_cmd_q.p_queue[g_st_cmd_q.u16_rear];
410
411         return NULL;
412 }
413
414 static void lr388k7_cmd_enqueue_finish(void)
415 {
416         if (g_st_cmd_q.u16_rear == (K7_Q_SIZE - 1))
417                 g_st_cmd_q.u16_rear = 0;
418         else
419                 g_st_cmd_q.u16_rear++;
420 }
421
422 static void *lr388k7_cmd_dequeue_start(void)
423 {
424         if (!lr388k7_cmd_queue_is_empty())
425                 return &g_st_cmd_q.p_queue[g_st_cmd_q.u16_front];
426
427         return NULL;
428 }
429
430 static void lr388k7_cmd_dequeue_finish(void)
431 {
432         if (g_st_cmd_q.u16_front == (K7_Q_SIZE - 1))
433                 g_st_cmd_q.u16_front = 0;
434         else
435                 g_st_cmd_q.u16_front++;
436 }
437
438 static int lr388k7_cmd_queue_read(u8 *p, u32 u32_len)
439 {
440         u8 *p_queue;
441         u32 u32_ret;
442         p_queue = lr388k7_cmd_dequeue_start();
443         if (!p_queue)
444                 return 0;
445
446         u32_ret = copy_to_user(p, p_queue, u32_len);
447         if (u32_ret != 0)
448                 return 0;
449
450         lr388k7_cmd_dequeue_finish();
451         return 1;
452 }
453
454 static u8 lr388k7_clear_irq(void)
455 {
456         u8 u8_tx_buf[K7_RD_HEADER_SIZE + 1], u8_rx_buf[K7_RD_HEADER_SIZE + 1];
457         u8 u8Ret = 0;
458         size_t count = 1;
459
460         if (g_st_state.b_is_reset)
461                 return u8Ret;
462
463         u8_tx_buf[0] = K7_RD_OPCODE;
464         u8_tx_buf[1] = (K7_INT_STATUS_ADDR >> 16) & 0xFF;
465         u8_tx_buf[2] = (K7_INT_STATUS_ADDR >>  8) & 0xFF;
466         u8_tx_buf[3] = (K7_INT_STATUS_ADDR >>  0) & 0xFF;
467         u8_tx_buf[4] = 0x00;
468
469         g_st_state.u32SCK = 0;
470
471         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count))
472                 u8Ret = u8_rx_buf[K7_RD_HEADER_SIZE];
473
474         g_st_state.u32SCK = g_spi->max_speed_hz;
475
476         return u8Ret;
477 }
478
479 static long lr388k7_ts_get_mode(u8 *p)
480 {
481         ssize_t status = 0;
482
483         if (p == 0) {
484                 status = -EFAULT;
485                 goto exit_get_mode;
486         }
487
488         if (copy_to_user(p, &g_u8_mode, sizeof(u8)))
489                 status = -EFAULT;
490
491  exit_get_mode:
492         return status;
493 }
494
495 static long lr388k7_ts_get_debug_status(u8 *p)
496 {
497         ssize_t status = 0;
498
499         if (p == 0) {
500                 status = -EFAULT;
501                 goto exit_get_debug_status;
502         }
503
504         if (copy_to_user(p, &g_st_dbg, sizeof(struct lr388k7_ts_dbg)))
505                 status = -EFAULT;
506
507  exit_get_debug_status:
508         return status;
509 }
510
511 static void lr388k7_indicate_done(void)
512 {
513 #if 0
514         u8 u8_buf[5];
515         size_t count = 0;
516
517         u8_buf[count++] = K7_WR_OPCODE;
518         u8_buf[count++] = (K7_IND_DONE_ADDR >> 16) & 0xFF;
519         u8_buf[count++] = (K7_IND_DONE_ADDR >>  8) & 0xFF;
520         u8_buf[count++] = (K7_IND_DONE_ADDR >>  0) & 0xFF;
521         u8_buf[count++] = 0x01;
522         lr388k7_spi_write(u8_buf, count);
523 #endif
524 }
525
526 static int lr388k7_read_data(u8 *p)
527 {
528         u8 *p_tx_buf;
529         size_t count = 0;
530         ssize_t status = 0;
531
532         /*
533          * Memory Allocation
534          */
535         count = (size_t)g_u32_raw_data_size;
536
537         p_tx_buf = kmalloc(K7_DATA_READ_SIZE + count, GFP_KERNEL);
538         if (p_tx_buf == NULL) {
539                 status = -ENOMEM;
540                 goto exit_read_data;
541         }
542
543         p_tx_buf[0] = K7_RD_OPCODE;
544         p_tx_buf[1] = (K7_DATA_OFFSET >> 16) & 0xFF;
545         p_tx_buf[2] = (K7_DATA_OFFSET >>  8) & 0xFF;
546         p_tx_buf[3] = (K7_DATA_OFFSET >>  0) & 0xFF;
547         p_tx_buf[4] = 0x00;
548
549         if (lr388k7_spi_read(p_tx_buf, p, K7_DATA_HEADER_BYTE_SIZE + count))
550                 status = count;
551         else{
552                 status = -EFAULT;
553                 goto exit_read_data_free_tx;
554         }
555
556  exit_read_data_free_tx:
557         kfree(p_tx_buf);
558  exit_read_data:
559         if (status < 0)
560                 return 0;
561         return 1;
562 }
563
564 int lr388k7_send_signal(int pid, int n_info)
565 {
566         struct siginfo info;
567         struct task_struct *t;
568         int ret = 0;
569
570         static DEFINE_MUTEX(lock);
571
572         if (!pid)
573                 return ret;
574
575         mutex_lock(&lock);
576
577         /*
578          * Set signal parameters
579          */
580         memset(&info, 0, sizeof(struct siginfo));
581         info.si_signo = LR388K7_SIGNAL;
582         info.si_code = SI_QUEUE;
583         info.si_int = n_info;/*real time signals may have 32 bits of data. */
584
585         rcu_read_lock();
586         /* To avoid link error for module build, it was replaced by pid_task.
587         t = find_task_by_vpid(pid);
588         */
589         t = pid_task(find_vpid(pid), PIDTYPE_PID);
590         rcu_read_unlock();
591         if (t == NULL) {
592                 dev_err(&g_spi->dev, " : no such pid\n");
593                 ret = -ENODEV;
594         } else{
595                 ret = send_sig_info(LR388K7_SIGNAL, &info, t);
596         }
597
598         if (ret < 0)
599                 dev_err(&g_spi->dev, " : sending signal\n");
600
601         mutex_unlock(&lock);
602
603         return ret;
604 }
605
606 static void lr388k7_read_spec_size(void)
607 {
608         int n_ret;
609         u8 *p_buf;
610
611         p_buf = kmalloc(K7_DATA_SIZE, GFP_KERNEL);
612         if (p_buf) {
613                 n_ret = lr388k7_read_data((u8 *)p_buf);
614                 kfree(p_buf);
615         } else {
616                 dev_err(&g_spi->dev, " : %d\n", -ENOMEM);
617         }
618 }
619
620 static bool lr388k7_check_status(void)
621 {
622         u8 u8_tx_buf[K7_RD_HEADER_SIZE + 1], u8_rx_buf[K7_RD_HEADER_SIZE + 1];
623         u8 u8Ret = 0;
624         size_t count = 1;
625
626         u8_tx_buf[0] = K7_RD_OPCODE;
627         u8_tx_buf[1] = (K7_CMD_DATA_OFFSET >> 16) & 0xFF;
628         u8_tx_buf[2] = (K7_CMD_DATA_OFFSET >>  8) & 0xFF;
629         u8_tx_buf[3] = (K7_CMD_DATA_OFFSET >>  0) & 0xFF;
630         u8_tx_buf[4] = 0x00;
631
632         g_st_state.u32SCK = 0;
633
634         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
635                 u8Ret = u8_rx_buf[K7_RD_HEADER_SIZE];
636
637                 if (u8Ret == K7_REMOTE_WAKEUP_CODE)
638                         return true;
639         }
640
641         g_st_state.u32SCK = g_spi->max_speed_hz;
642
643         return false;
644 }
645
646 static void lr388k7_event_handler(u8 u8Status)
647 {
648         void *p_q_buf;
649         void *p_cmd_q_buf;
650         int n_ret;
651
652         if (u8Status & K7_INT_STATUS_MASK_BOOT) {
653                 /*
654                  * Let module know device has booted up.
655                  */
656 #if defined(DEBUG_LR388K7)
657                 dev_info(&g_spi->dev, "boot\n");
658 #endif
659                 lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_BOOT);
660         }
661
662         if (u8Status & K7_INT_STATUS_MASK_CRC)
663                 lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_CRCE);
664
665         if (u8Status & K7_INT_STATUS_MASK_DATA) {
666
667                 /*
668                  * Handle data
669                  */
670                 if (g_st_state.b_is_init_finish) {
671
672                         /*
673                          * Get pointer of queue buffer
674                          */
675                         p_q_buf = lr388k7_enqueue_start();
676                         if (p_q_buf) {
677                                 n_ret = lr388k7_read_data((u8 *)p_q_buf);
678                                 if (n_ret)
679                                         lr388k7_enqueue_finish();
680                         } else {
681                                 lr388k7_read_spec_size();
682                         }
683                 } else {
684                         lr388k7_read_spec_size();
685                 }
686
687                 if (g_st_state.b_is_calc_finish) {
688                         g_st_state.b_is_calc_finish = 0;
689                         lr388k7_send_signal(
690                                             g_st_state.u32_pid,
691                                             LR388K7_SIGNAL_INTR
692                                             );
693                 }
694                 lr388k7_indicate_done();
695         }
696
697         if (u8Status & K7_INT_STATUS_MASK_CMD) {
698
699                 /*
700                  * Process command result
701                  */
702                 p_cmd_q_buf = lr388k7_cmd_enqueue_start();
703                 if (p_cmd_q_buf) {
704                         n_ret = lr388k7_spi_read_cmd_data(
705                                                           (u8 *)p_cmd_q_buf,
706                                                           K7_CMD_DATA_SIZE
707                                                           - K7_RD_HEADER_SIZE
708                                                           );
709                         if (n_ret) {
710                                 lr388k7_cmd_enqueue_finish();
711                                 lr388k7_send_signal(
712                                                     g_st_state.u32_pid,
713                                                     LR388K7_SIGNAL_CMDR
714                                                     );
715                         }
716                 }
717         }
718 }
719
720 static void lr388k7_work_handler(struct work_struct *work)
721 {
722         struct lr388k7 *ts = spi_get_drvdata(g_spi);
723         u8 u8_status;
724         int irq_value;
725
726         if (g_st_state.b_is_suspended) {
727                 dev_dbg(
728                         &g_spi->dev,
729                         "[WARN] work handler is called regardless of b_is_suspended\n");
730                 return;
731         }
732
733         mutex_lock(&ts->mutex);
734
735         /*
736          * Clear Interrupt
737          */
738         u8_status = lr388k7_clear_irq();
739
740         while (u8_status) {
741
742                 lr388k7_event_handler(u8_status);
743
744                 /*
745                  * Check if irq is already cleared.
746                  */
747                 irq_value = gpio_get_value(ts->gpio_irq);
748
749                 if (!irq_value)
750                         u8_status = lr388k7_clear_irq();
751                 else
752                         u8_status = 0;
753         }
754
755         mutex_unlock(&ts->mutex);
756 }
757
758 static void lr388k7_enable_irq(struct lr388k7 *ts)
759 {
760         if (g_st_dbg.wakeup.enable == 1)
761                 disable_irq_wake(ts->irq);
762         else
763                 enable_irq(ts->irq);
764 }
765
766 static void lr388k7_disable_irq(struct lr388k7 *ts)
767 {
768         if (g_st_dbg.wakeup.enable == 1)
769                 enable_irq_wake(ts->irq);
770         else
771                 disable_irq(ts->irq);
772 }
773
774 static irqreturn_t lr388k7_wakeup_thread(int irq, void *_ts)
775 {
776         struct lr388k7 *ts = (struct lr388k7 *)_ts;
777         struct input_dev *input_dev = ts->idev;
778         u8 u8_status;
779
780         dev_info(&g_spi->dev, "[ENTER] Wakeup thread\n");
781
782         u8_status = lr388k7_clear_irq();
783         if (u8_status & K7_INT_STATUS_MASK_CMD) {
784                 if (!lr388k7_check_status()) {
785                         lr388k7_event_handler(u8_status);
786                         return IRQ_HANDLED;
787                 }
788         }
789
790         dev_info(&g_spi->dev, "Tap wakeup\n");
791
792         input_report_key(input_dev, KEY_POWER, 1);
793         input_sync(input_dev);
794         input_report_key(input_dev, KEY_POWER, 0);
795         input_sync(input_dev);
796
797         if (u8_status & K7_INT_STATUS_MASK_DATA) {
798                 dev_info(&g_spi->dev, "[DATA] Wakeup thread\n");
799                 lr388k7_read_spec_size();
800         }
801
802         if (g_st_state.b_is_suspended)
803                 g_st_state.b_is_suspended = false;
804
805         dev_info(&g_spi->dev, "[EXIT] Wakeup thread\n");
806
807         return IRQ_HANDLED;
808 }
809
810 static irqreturn_t lr388k7_irq_thread(int irq, void *_ts)
811 {
812         struct lr388k7 *ts = (struct lr388k7 *)_ts;
813         struct input_dev *input_dev = ts->idev;
814
815         if (g_u8_scan == K7_SCAN_STATE_IDLE) {
816                 input_event(input_dev, EV_MSC, MSC_ACTIVITY, 1);
817                 input_sync(input_dev);
818         }
819
820         if (g_st_state.b_is_suspended) {
821                 if (g_st_dbg.wakeup.enable == 1) {
822                         dev_info(&g_spi->dev, "Throw IRQ_WAKE_THREAD\n");
823                         return IRQ_WAKE_THREAD;
824                 }
825                 return IRQ_HANDLED;
826         }
827
828         queue_work(g_st_state.st_wq_k7, &g_st_state.st_work_k7);
829         return IRQ_HANDLED;
830 }
831
832 static void lr388k7_start_scan(struct lr388k7 *ts)
833 {
834         lr388k7_enable_irq(ts);
835 }
836
837 static void lr388k7_stop_scan(struct lr388k7 *ts)
838 {
839         lr388k7_disable_irq(ts);
840 }
841
842 /* must be called with ts->mutex held */
843 static void __lr388k7_disable(struct lr388k7 *ts)
844 {
845         lr388k7_stop_scan(ts);
846 }
847
848 /* must be called with ts->mutex held */
849 static void __lr388k7_enable(struct lr388k7 *ts)
850 {
851         lr388k7_start_scan(ts);
852 }
853
854 static ssize_t lr388k7_ts_force_cap_show(struct device *dev,
855                                          struct device_attribute *attr,
856                                          char *buf)
857 {
858         return sprintf(buf, "%d\n", g_st_dbg.u8ForceCap);
859 }
860
861 static ssize_t lr388k7_ts_force_cap_store(struct device *dev,
862                                           struct device_attribute *attr,
863                                           const char *buf,
864                                           size_t count)
865 {
866         ssize_t ret;
867
868         ret = (ssize_t)count;
869
870         if (g_st_state.b_is_suspended)
871                 return ret;
872
873         if (count != 1)
874                 return ret;
875
876         if (buf[0] == '0')
877                 g_st_dbg.u8ForceCap = 0;
878         else if (buf[0] == '1')
879                 g_st_dbg.u8ForceCap = 1;
880
881         lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_CTRL);
882
883         return ret;
884 }
885
886 static ssize_t lr388k7_ts_dump_show(struct device *dev,
887                                     struct device_attribute *attr,
888                                     char *buf)
889 {
890         return sprintf(buf, "%d\n", g_st_dbg.u8Dump);
891 }
892
893 static ssize_t lr388k7_ts_dump_store(struct device *dev,
894                                      struct device_attribute *attr,
895                                      const char *buf,
896                                      size_t count)
897 {
898         ssize_t ret;
899
900         ret = (ssize_t)count;
901
902         if (g_st_state.b_is_suspended)
903                 return ret;
904
905         if (count != 1)
906                 return ret;
907
908         if (buf[0] == '0')
909                 g_st_dbg.u8Dump = 0;
910         else if (buf[0] == '1')
911                 g_st_dbg.u8Dump = 1;
912
913         lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_CTRL);
914
915         return ret;
916 }
917
918 static ssize_t lr388k7_ts_report_mode_show(struct device *dev,
919                                     struct device_attribute *attr,
920                                     char *buf)
921 {
922         return sprintf(buf, "%d\n", g_u8_mode);
923 }
924
925 static ssize_t lr388k7_ts_report_mode_store(struct device *dev,
926                                      struct device_attribute *attr,
927                                      const char *buf,
928                                      size_t count)
929 {
930         ssize_t ret;
931         u8 prev_mode = g_u8_mode;
932
933         ret = (ssize_t)count;
934
935         if (count != 2)
936                 return ret;
937
938         if (buf[0] >= 0x30 && buf[0] <= 0x36)
939                 g_u8_mode = (u8)buf[0] - 0x30;
940
941         if (prev_mode != g_u8_mode)
942                 lr388k7_send_signal(g_st_state.u32_pid,
943                                     LR388K7_SIGNAL_MODE);
944
945         return ret;
946 }
947
948 static ssize_t lr388k7_ts_version_show(struct device *dev,
949         struct device_attribute *attr,
950         char *buf)
951 {
952         return sprintf(buf, "FW %d, Driver %d, Module %d\n",
953                        g_st_state.u16fw_ver,
954                        K7_DRIVER_VERSION,
955                        g_st_state.u16module_ver
956                        );
957 }
958
959 static ssize_t lr388k7_ts_version_store(struct device *dev,
960         struct device_attribute *attr,
961         const char *buf, size_t count)
962 {
963         return count;
964 }
965
966
967 static ssize_t lr388k7_ts_slowscan_enable_store(struct device *dev,
968         struct device_attribute *attr,
969         const char *buf, size_t count)
970 {
971 #ifdef ENABLE_SLOW_SCAN
972         unsigned long val;
973         ssize_t err;
974         ssize_t ret;
975         if (count < 2)
976                 return -EINVAL;
977
978         if (g_st_state.b_is_suspended &&
979             g_st_dbg.wakeup.enable == 0)
980                 return -EINVAL;
981
982         ret = (ssize_t) count;
983
984         if (count == 2) {
985                 if (buf[0] == '0') {
986                         g_st_dbg.slowscan.enable = 0;
987                         lr388k7_send_signal(g_st_state.u32_pid,
988                                             LR388K7_SIGNAL_CTRL);
989                 } else if (buf[0] == '1') {
990                         g_st_dbg.slowscan.enable = 1;
991                         lr388k7_send_signal(g_st_state.u32_pid,
992                                             LR388K7_SIGNAL_CTRL);
993                 }
994         } else if ((buf[0] == '2') && (buf[1] == ' ')) {
995                 err = kstrtoul(&buf[2], 10, &val);
996
997                 if (err) {
998                         ret = err;
999                 } else {
1000                         /* check if prefined value*/
1001                         if ((val == K7_SLOW_SCAN_30) ||
1002                             (val == K7_SLOW_SCAN_60) ||
1003                             (val == K7_SLOW_SCAN_100) ||
1004                             (val == K7_SLOW_SCAN_120)) {
1005                                 g_st_dbg.slowscan.scan_rate = (u16)val;
1006                                 g_st_dbg.slowscan.enable = 1;
1007                                 lr388k7_send_signal(g_st_state.u32_pid,
1008                                                     LR388K7_SIGNAL_CTRL);
1009                         }
1010                 }
1011         }
1012
1013         return ret;
1014 #else
1015         return count;
1016 #endif
1017 }
1018
1019 static ssize_t lr388k7_ts_slowscan_enable_show(struct device *dev,
1020         struct device_attribute *attr,
1021         char *buf)
1022 {
1023 #ifdef ENABLE_SLOW_SCAN
1024         return sprintf(buf, "Slow Scan:%s Scan Rate:%dHz\n",
1025                        g_st_dbg.slowscan.enable ?
1026                        "Enabled" : "Disabled",
1027                        g_st_dbg.slowscan.scan_rate);
1028 #else
1029         return sprintf(buf, "Not implemented yet\n");
1030 #endif
1031 }
1032
1033 static ssize_t lr388k7_ts_wakeup_enable_store(struct device *dev,
1034         struct device_attribute *attr,
1035         const char *buf, size_t count)
1036 {
1037         ssize_t ret;
1038
1039         if (count < 2)
1040                 return -EINVAL;
1041
1042         if (g_st_state.b_is_suspended &&
1043             g_st_dbg.wakeup.enable == 0)
1044                 return -EINVAL;
1045
1046         ret = (ssize_t) count;
1047
1048         if (count == 2) {
1049                 if (buf[0] == '0') {
1050                         g_st_dbg.wakeup.enable = 0;
1051                         lr388k7_send_signal(g_st_state.u32_pid,
1052                                             LR388K7_SIGNAL_CTRL);
1053                 } else if (buf[0] == '2') {
1054                         if (g_st_dbg.wakeup.enable <= 1)
1055                                 g_u16_wakeup_enable = g_st_dbg.wakeup.enable;
1056                         g_st_dbg.wakeup.enable = 2;
1057                         lr388k7_send_signal(g_st_state.u32_pid,
1058                                             LR388K7_SIGNAL_CTRL);
1059                 }
1060         } else if ((buf[0] == '1') && (buf[1] == ' ')) {
1061                 /* check if prefined value*/
1062                 if ((buf[2] == '2') ||
1063                     (buf[2] == '3') ||
1064                     (buf[2] == '4')) {
1065                         g_st_dbg.wakeup.num_tap = buf[2] - 0x30;
1066                         g_st_dbg.wakeup.enable = 1;
1067                         lr388k7_send_signal(g_st_state.u32_pid,
1068                                             LR388K7_SIGNAL_CTRL);
1069                 }
1070         }
1071
1072         return ret;
1073 }
1074
1075 static ssize_t lr388k7_ts_wakeup_enable_show(struct device *dev,
1076         struct device_attribute *attr,
1077         char *buf)
1078 {
1079         return sprintf(buf, "wakeup_enable:%s(%d) Number of taps:%d\n",
1080                        g_st_dbg.wakeup.enable == 1 ?
1081                        "Enabled" : "Disabled",
1082                        g_st_dbg.wakeup.enable,
1083                        g_st_dbg.wakeup.num_tap);
1084 }
1085
1086 static ssize_t lr388k7_ts_test_store(struct device *dev,
1087         struct device_attribute *attr,
1088         const char *buf, size_t count)
1089 {
1090         ssize_t ret;
1091
1092         if (count != 2)
1093                 return -EINVAL;
1094
1095         ret = (ssize_t) count;
1096
1097         if (buf[0] == '1') {
1098                 g_st_dbg.u8Test = 1;
1099                 lr388k7_send_signal(g_st_state.u32_pid,
1100                                     LR388K7_SIGNAL_CTRL);
1101         } else if (buf[0] == '0') {
1102                 g_st_dbg.u8Test = 0;
1103                 lr388k7_send_signal(g_st_state.u32_pid,
1104                                     LR388K7_SIGNAL_CTRL);
1105         }
1106
1107         return ret;
1108 }
1109
1110 static ssize_t lr388k7_ts_test_show(struct device *dev,
1111         struct device_attribute *attr,
1112         char *buf)
1113 {
1114         return sprintf(buf, "%s\n", g_st_dbg.u8Test == 1 ?
1115                        "Enabled" : "Disabled");
1116 }
1117
1118 #if defined(DEBUG_LR388K7)
1119 static ssize_t lr388k7_ts_check_state_store(struct device *dev,
1120         struct device_attribute *attr,
1121         const char *buf, size_t count)
1122 {
1123         ssize_t ret;
1124         ret = (ssize_t) count;
1125         return ret;
1126 }
1127
1128 static ssize_t lr388k7_ts_check_state_show(struct device *dev,
1129         struct device_attribute *attr,
1130         char *buf)
1131 {
1132         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1133         u8 u8_tx_buf[K7_RD_HEADER_SIZE + 5], u8_rx_buf[K7_RD_HEADER_SIZE + 5];
1134         u8 u8Ret;
1135         u8 u8HWR;
1136         u32 u32RES, u32FWR;
1137         size_t count = 0;
1138
1139         u8_tx_buf[count++] = K7_RD_OPCODE;
1140         u8_tx_buf[count++] = (K7_STATE_CTL_ADDR >> 16) & 0xFF;
1141         u8_tx_buf[count++] = (K7_STATE_CTL_ADDR >>  8) & 0xFF;
1142         u8_tx_buf[count++] = (K7_STATE_CTL_ADDR >>  0) & 0xFF;
1143         u8_tx_buf[count++] = 0x00;
1144
1145         g_st_state.u32SCK = 0;
1146
1147         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1148                 u8Ret = u8_rx_buf[K7_RD_HEADER_SIZE];
1149         } else {
1150                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1151                        gpio_get_value(ts->gpio_irq) ?
1152                        "High" : "Low"
1153                        );
1154         }
1155
1156         count = 0;
1157         u8_tx_buf[count++] = K7_RD_OPCODE;
1158         u8_tx_buf[count++] = 0x00;
1159         u8_tx_buf[count++] = 0x02;
1160         u8_tx_buf[count++] = 0xAC;
1161         u8_tx_buf[count++] = 0x00;
1162
1163         g_st_state.u32SCK = 0;
1164
1165         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1166                 u8HWR = u8_rx_buf[K7_RD_HEADER_SIZE];
1167         } else {
1168                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1169                        gpio_get_value(ts->gpio_irq) ?
1170                        "High" : "Low"
1171                        );
1172         }
1173
1174         count = 0;
1175         u8_tx_buf[count++] = K7_RD_OPCODE;
1176         u8_tx_buf[count++] = 0x00;
1177         u8_tx_buf[count++] = 0x02;
1178         u8_tx_buf[count++] = 0xB0;
1179         u8_tx_buf[count++] = 0x00;
1180         u8_tx_buf[count++] = 0x00;
1181         u8_tx_buf[count++] = 0x00;
1182         u8_tx_buf[count++] = 0x00;
1183
1184         g_st_state.u32SCK = 0;
1185
1186         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1187                 u32FWR = u8_rx_buf[K7_RD_HEADER_SIZE] |
1188                         u8_rx_buf[K7_RD_HEADER_SIZE + 1] << 8 |
1189                         u8_rx_buf[K7_RD_HEADER_SIZE + 2] << 16 |
1190                         u8_rx_buf[K7_RD_HEADER_SIZE + 3] << 24;
1191         } else {
1192                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1193                        gpio_get_value(ts->gpio_irq) ?
1194                        "High" : "Low"
1195                        );
1196         }
1197
1198         count = 0;
1199         u8_tx_buf[count++] = K7_RD_OPCODE;
1200         u8_tx_buf[count++] = 0x00;
1201         u8_tx_buf[count++] = 0x00;
1202         u8_tx_buf[count++] = 0x40;
1203         u8_tx_buf[count++] = 0x00;
1204         u8_tx_buf[count++] = 0x00;
1205         u8_tx_buf[count++] = 0x00;
1206         u8_tx_buf[count++] = 0x00;
1207
1208         g_st_state.u32SCK = 0;
1209
1210         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1211                 u32RES = u8_rx_buf[K7_RD_HEADER_SIZE] |
1212                         u8_rx_buf[K7_RD_HEADER_SIZE + 1] << 8 |
1213                         u8_rx_buf[K7_RD_HEADER_SIZE + 2] << 16 |
1214                         u8_rx_buf[K7_RD_HEADER_SIZE + 3] << 24;
1215         } else {
1216                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1217                        gpio_get_value(ts->gpio_irq) ?
1218                        "High" : "Low"
1219                        );
1220         }
1221
1222         g_st_state.u32SCK = g_spi->max_speed_hz;
1223
1224         return sprintf(
1225                        buf,
1226                        "IRQ(%s) status=0x%02X, HWRev=%d, FWRev=0x%0X, Res=0x%04X\n",
1227                        gpio_get_value(ts->gpio_irq) ?
1228                        "High" : "Low",
1229                        u8Ret,
1230                        u8HWR,
1231                        u32FWR,
1232                        u32RES
1233                        );
1234 }
1235 #endif
1236
1237 static DEVICE_ATTR(force_cap, 0666, lr388k7_ts_force_cap_show,
1238                         lr388k7_ts_force_cap_store);
1239 static DEVICE_ATTR(dump, 0666, lr388k7_ts_dump_show,
1240                         lr388k7_ts_dump_store);
1241 static DEVICE_ATTR(report_mode, 0640, lr388k7_ts_report_mode_show,
1242                         lr388k7_ts_report_mode_store);
1243 static DEVICE_ATTR(version, 0640,
1244                    lr388k7_ts_version_show,
1245                    lr388k7_ts_version_store);
1246 static DEVICE_ATTR(slowscan_enable, 0640,
1247                    lr388k7_ts_slowscan_enable_show,
1248                    lr388k7_ts_slowscan_enable_store);
1249 static DEVICE_ATTR(wakeup_enable, 0640,
1250                    lr388k7_ts_wakeup_enable_show,
1251                    lr388k7_ts_wakeup_enable_store);
1252 static DEVICE_ATTR(test, 0640,
1253                    lr388k7_ts_test_show,
1254                    lr388k7_ts_test_store);
1255 #if defined(DEBUG_LR388K7)
1256 static DEVICE_ATTR(check_state, 0666,
1257                    lr388k7_ts_check_state_show,
1258                    lr388k7_ts_check_state_store);
1259 #endif
1260
1261 static struct attribute *lr388k7_ts_attributes[] = {
1262         &dev_attr_force_cap.attr,
1263         &dev_attr_dump.attr,
1264         &dev_attr_report_mode.attr,
1265         &dev_attr_version.attr,
1266         &dev_attr_slowscan_enable.attr,
1267         &dev_attr_wakeup_enable.attr,
1268         &dev_attr_test.attr,
1269 #if defined(DEBUG_LR388K7)
1270         &dev_attr_check_state.attr,
1271 #endif
1272         NULL
1273 };
1274
1275 static const struct attribute_group lr388k7_ts_attr_group = {
1276         .attrs = lr388k7_ts_attributes,
1277 };
1278
1279 static int lr388k7_open(struct input_dev *input)
1280 {
1281         struct lr388k7 *ts = input_get_drvdata(input);
1282
1283         dev_info(&ts->spi->dev, "[ENTER] open\n");
1284
1285         mutex_lock(&ts->mutex);
1286
1287         __lr388k7_enable(ts);
1288
1289         ts->opened = true;
1290
1291         mutex_unlock(&ts->mutex);
1292
1293         dev_info(&ts->spi->dev, "[EXIT] open\n");
1294         return 0;
1295 }
1296
1297 static void lr388k7_close(struct input_dev *input)
1298 {
1299         struct lr388k7 *ts = input_get_drvdata(input);
1300         mutex_lock(&ts->mutex);
1301
1302         __lr388k7_disable(ts);
1303
1304         ts->opened = false;
1305
1306         mutex_unlock(&ts->mutex);
1307 }
1308
1309
1310 #if defined(UDEV_ENABLE)
1311 static void lr388k7_send_udev_event(void *p)
1312 {
1313         struct lr388k7_udev_event *p_udev_event;
1314         char *envp[2];
1315         char *event;
1316
1317         if (!p)
1318                 return;
1319
1320         p_udev_event = kmalloc(
1321                                sizeof(struct lr388k7_udev_event),
1322                                GFP_KERNEL);
1323
1324         if (!p_udev_event)
1325                 return;
1326
1327         if (copy_from_user(p_udev_event,
1328                           p,
1329                           sizeof(struct lr388k7_udev_event))) {
1330                 kfree(p_udev_event);
1331                 return;
1332         }
1333
1334         event = kasprintf(GFP_KERNEL, "TRIGGER=%s", p_udev_event->str);
1335         if (event) {
1336                 envp[0] = event;
1337                 envp[1] = NULL;
1338                 kobject_uevent_env(&lr388k7_ts_miscdev.this_device->kobj,
1339                                    KOBJ_CHANGE,
1340                                    envp);
1341                 kfree(event);
1342         }
1343         kfree(p_udev_event);
1344 }
1345 #endif
1346
1347 #if defined(ACTIVE_ENABLE)
1348 static void lr388k7_active_report(void *p)
1349 {
1350         struct lr388k7_active_report *p_active_report;
1351         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1352         struct input_dev *input_dev = ts->idev_active;
1353         int x, y, z;
1354         int tool = 0;
1355         u8 status;
1356
1357         if (!p)
1358                 return;
1359
1360         /*
1361          * Memory Allocation
1362          */
1363         p_active_report = kmalloc(
1364                                  sizeof(struct lr388k7_active_report),
1365                                  GFP_KERNEL);
1366         if (!p_active_report)
1367                 return;
1368
1369         if (copy_from_user(p_active_report,
1370                            p,
1371                            sizeof(struct lr388k7_active_report))) {
1372                 kfree(p_active_report);
1373                 return;
1374         }
1375
1376         status = p_active_report->status;
1377
1378         /* Get current tool */
1379         if (status & 0x0C)
1380                 tool = BTN_TOOL_RUBBER;
1381         else
1382                 tool = BTN_TOOL_PEN;
1383
1384         if (!status)
1385                 tool = 0; /* Reset */
1386
1387         /* Check if tool changed then notify input subsystem */
1388         if (ts->tool != tool) {
1389                 if (ts->tool) {
1390                         /* Reset old tool state */
1391                         input_report_key(input_dev, BTN_TOUCH, 0);
1392                         input_report_key(input_dev, BTN_STYLUS, 0);
1393                         input_report_abs(input_dev, ABS_PRESSURE, 0);
1394                 }
1395                 input_report_key(input_dev, ts->tool, 0);
1396                 input_sync(input_dev);
1397
1398                 /* Update current*/
1399                 ts->tool = tool;
1400                 if (tool)
1401                         input_report_key(input_dev, tool, 1);
1402         }
1403
1404         if (tool) {
1405                 x = p_active_report->x;
1406                 y = p_active_report->y;
1407                 z = p_active_report->z;
1408
1409                 input_report_abs(input_dev, ABS_X, x);
1410                 input_report_abs(input_dev, ABS_Y, y);
1411                 input_report_abs(input_dev, ABS_PRESSURE, z);
1412                 input_report_key(input_dev, BTN_TOUCH, status & 0x09);
1413                 input_report_key(input_dev, BTN_STYLUS, status & 0x10);
1414                 input_sync(input_dev);
1415         }
1416
1417         kfree(p_active_report);
1418 }
1419 #endif
1420
1421 #if defined(PROTOCOL_A)
1422 static void lr388k7_touch_report(void *p)
1423 {
1424         u8 u8_num, i, u8_num_of_touch;
1425         struct lr388k7_touch_report *p_touch_report;
1426         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1427         struct input_dev *input_dev = ts->idev;
1428         bool b_is_eraser = false;
1429
1430         if (!p)
1431                 return;
1432
1433         /*
1434          * Memory Allocation
1435          */
1436         p_touch_report = kmalloc(
1437                                  sizeof(struct lr388k7_touch_report),
1438                                  GFP_KERNEL);
1439         if (!p_touch_report)
1440                 return;
1441
1442         if (copy_from_user(p_touch_report,
1443                            p,
1444                            sizeof(struct lr388k7_touch_report))) {
1445                 kfree(p_touch_report);
1446                 return;
1447         }
1448
1449         u8_num = p_touch_report->u8_num_of_touch;
1450
1451         if (u8_num == 0) {
1452                 if (ts->b_eraser_active) {
1453                         ts->b_eraser_active = false;
1454                         input_report_key(input_dev, BTN_TOOL_RUBBER, 0);
1455                 }
1456                 input_mt_sync(input_dev);
1457                 input_sync(input_dev);
1458                 kfree(p_touch_report);
1459                 return;
1460         }
1461         u8_num_of_touch = 0;
1462
1463         for (i = 0; i < u8_num; i++) {
1464
1465 #if defined(DEBUG_LR388K7_REPORT)
1466                 dev_info(&g_spi->dev, "ID=%2d, status=%02d, x=%5d, y=%5d, w=%d, h=%d, z=%5d, num=%2d\n",
1467                          p_touch_report->tc[i] . id,
1468                          p_touch_report->tc[i] . status,
1469                          p_touch_report->tc[i] . x,
1470                          p_touch_report->tc[i] . y,
1471                          p_touch_report->tc[i] . width,
1472                          p_touch_report->tc[i] . height,
1473                          p_touch_report->tc[i] . z,
1474                          u8_num
1475                          );
1476 #endif
1477
1478                 if ((p_touch_report->tc[i] . status & 0x80) != 0)
1479                         b_is_eraser = true;
1480                 else
1481                         b_is_eraser = false;
1482
1483                 if (b_is_eraser) {
1484                         input_report_key(input_dev, BTN_TOOL_RUBBER, 1);
1485                         ts->b_eraser_active = true;
1486                 } else if ((p_touch_report->tc[i] . status & 0x7F) <= 3 ||
1487                           (p_touch_report->tc[i] . status & 0x7F) == 8)
1488                         input_report_abs(input_dev,
1489                                          ABS_MT_TOOL_TYPE, MT_TOOL_FINGER);
1490                 else if ((p_touch_report->tc[i] . status & 0x7F) <= 7 ||
1491                          (p_touch_report->tc[i] . status & 0x7F) == 12)
1492                         input_report_abs(input_dev,
1493                                          ABS_MT_TOOL_TYPE, MT_TOOL_PEN);
1494                 input_report_abs(input_dev,
1495                                  ABS_MT_TRACKING_ID,
1496                                  p_touch_report->tc[i] .id);
1497                 input_report_abs(input_dev,
1498                                  ABS_MT_POSITION_X,
1499                                  p_touch_report->tc[i] . x);
1500                 input_report_abs(input_dev,
1501                                  ABS_MT_POSITION_Y,
1502                                  p_touch_report->tc[i] . y);
1503                 input_report_abs(input_dev,
1504                                  ABS_MT_PRESSURE,
1505                                  p_touch_report->tc[i] . z);
1506 #if 0
1507                 input_report_abs(input_dev,
1508                                  ABS_MT_TOUCH_MAJOR,
1509                                  p_touch_report->tc[i] .width);
1510                 input_report_abs(input_dev,
1511                                  ABS_MT_TOUCH_MINOR,
1512                                  p_touch_report->tc[i] .height);
1513 #endif
1514                 input_mt_sync(input_dev);
1515                 u8_num_of_touch++;
1516         }
1517         input_sync(input_dev);
1518
1519         kfree(p_touch_report);
1520 }
1521 #else /* PROTOCOL_B */
1522 static void lr388k7_touch_report(void *p)
1523 {
1524         u8 u8_num, i;
1525         struct lr388k7_touch_report *p_touch_report;
1526         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1527         struct input_dev *input_dev = ts->idev;
1528
1529         if (!p)
1530                 return;
1531
1532         /*
1533          * Memory Allocation
1534          */
1535         p_touch_report = kmalloc(
1536                                  sizeof(struct lr388k7_touch_report),
1537                                  GFP_KERNEL);
1538         if (!p_touch_report)
1539                 return;
1540
1541         if (copy_from_user(p_touch_report,
1542                            p,
1543                            sizeof(struct lr388k7_touch_report))) {
1544                 kfree(p_touch_report);
1545                 return;
1546         }
1547
1548         u8_num = p_touch_report->u8_num_of_touch;
1549
1550         for (i = 0; i < u8_num; i++) {
1551
1552 #if defined(DEBUG_LR388K7_REPORT)
1553                 dev_info(&g_spi->dev, "ID=%2d, status=%02d, x=%5d, y=%5d, w=%d, h=%d, z=%5d, num=%2d\n",
1554                          p_touch_report->tc[i] . id,
1555                          p_touch_report->tc[i] . status,
1556                          p_touch_report->tc[i] . x,
1557                          p_touch_report->tc[i] . y,
1558                          p_touch_report->tc[i] . width,
1559                          p_touch_report->tc[i] . height,
1560                          p_touch_report->tc[i] . z,
1561                          u8_num
1562                          );
1563 #endif
1564                 input_mt_slot(input_dev,
1565                               p_touch_report->tc[i] . id);
1566                 if (((p_touch_report->tc[i] . status & 0x7F) == 8) ||
1567                     ((p_touch_report->tc[i] . status & 0x7F) == 12) ||
1568                     ((p_touch_report->tc[i] . status & 0x7F) == 14)) {
1569                         input_mt_report_slot_state(input_dev,
1570                                                    MT_TOOL_FINGER,
1571                                                    false);
1572                         continue;
1573                 }
1574
1575                 if ((p_touch_report->tc[i] . status & 0x7F) <= 3)
1576                         input_report_abs(input_dev,
1577                                          ABS_MT_TOOL_TYPE, MT_TOOL_FINGER);
1578                 else if (((p_touch_report->tc[i] . status & 0x7F) <= 7) ||
1579                          ((p_touch_report->tc[i] . status & 0x7F) > 12))
1580                         input_report_abs(input_dev,
1581                                          ABS_MT_TOOL_TYPE, MT_TOOL_PEN);
1582                 input_report_abs(input_dev,
1583                                  ABS_MT_TRACKING_ID,
1584                                  p_touch_report->tc[i] .id);
1585                 input_report_abs(input_dev,
1586                                  ABS_MT_POSITION_X,
1587                                  p_touch_report->tc[i].x);
1588                 input_report_abs(input_dev,
1589                                  ABS_MT_POSITION_Y,
1590                                  p_touch_report->tc[i].y);
1591                 input_report_abs(input_dev,
1592                                  ABS_MT_PRESSURE,
1593                                  p_touch_report->tc[i] . z);
1594         }
1595         input_sync(input_dev);
1596 }
1597 #endif /* #if defined(PROTOCOL_A) */
1598
1599 static int dev_open(struct inode *inode, struct file *filp)
1600 {
1601 #if defined(DEBUG_LR388K7)
1602         dev_info(&g_spi->dev, "dev_open\n");
1603 #endif
1604
1605         return 0;
1606 }
1607
1608 static int dev_release(struct inode *inode, struct file *filp)
1609 {
1610         int ret;
1611 #if defined(DEBUG_LR388K7)
1612         dev_info(&g_spi->dev, "dev_release\n");
1613 #endif
1614         g_st_state.b_is_init_finish = 0;
1615
1616         ret = lr388k7_hardreset(1);
1617
1618         return 0;
1619 }
1620
1621 /**
1622  * @brief lr388k7 spi interface
1623  * @param[in] txbuf command to read data
1624  * @param[in] rxbuf data to be read
1625  * @param[in] len length of data
1626  * @retval TRUE if success, otherwise FALSE
1627  */
1628 static int lr388k7_spi_read(u8 *txbuf, u8 *rxbuf, size_t len)
1629 {
1630         static DEFINE_MUTEX(lock);
1631
1632         int status;
1633         struct spi_message msg;
1634         struct spi_transfer t = {
1635                 .bits_per_word  = 8,
1636                 .tx_buf = txbuf,
1637                 .rx_buf = rxbuf,
1638                 .speed_hz =
1639                 g_st_state.u32SCK == 0 ? 12000000 : g_spi->max_speed_hz,
1640                 /*    .speed_hz           = 18000000, */
1641         };
1642
1643         mutex_lock(&lock);
1644
1645         t.len = len + K7_RD_HEADER_SIZE;
1646
1647         spi_message_init(&msg);
1648         spi_message_add_tail(&t, &msg);
1649         status = spi_sync(g_spi, &msg);
1650
1651         mutex_unlock(&lock);
1652
1653         if (status)
1654                 return false;
1655
1656         return true;
1657 }
1658
1659 static long lr388k7_spi_read_cmd_data(u8 *p, size_t count)
1660 {
1661         u8 *p_tx_buf;
1662         ssize_t status = 0;
1663
1664         /*
1665          * Memory Allocation
1666          */
1667         p_tx_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1668         if (p_tx_buf == NULL) {
1669                 status = -ENOMEM;
1670                 goto exit_dev_read_cmd_data;
1671         }
1672
1673         /*
1674          * Clear and Set command to read data
1675          */
1676         memset(p_tx_buf, 0, count + K7_RD_HEADER_SIZE);
1677
1678         p_tx_buf[0] = K7_RD_OPCODE;
1679         p_tx_buf[1] = (K7_CMD_DATA_OFFSET >> 16) & 0xFF;
1680         p_tx_buf[2] = (K7_CMD_DATA_OFFSET >>  8) & 0xFF;
1681         p_tx_buf[3] = (K7_CMD_DATA_OFFSET >>  0) & 0xFF;
1682         p_tx_buf[4] = 0x00;
1683
1684         /*
1685          * Read data
1686          */
1687         if (lr388k7_spi_read(p_tx_buf, p, count))
1688                 status = count;
1689         else
1690                 status = -EFAULT;
1691
1692         kfree(p_tx_buf);
1693
1694  exit_dev_read_cmd_data:
1695         return status;
1696 }
1697
1698 static long lr388k7_spi_read_wo_limit(u8 *p, size_t count)
1699 {
1700         u8 *p_buf;
1701         u8 *p_tx_buf;
1702         ssize_t status = 0;
1703
1704         /*
1705          * Memory Allocation
1706          */
1707         p_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1708         if (p_buf == NULL) {
1709                 status = -ENOMEM;
1710                 goto ext_dev_read_wo_limit;
1711         }
1712
1713         p_tx_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1714         if (p_tx_buf == NULL) {
1715                 status = -ENOMEM;
1716                 goto ext_dev_read_wo_limit_free;
1717         }
1718
1719         /*
1720          * Clear and Set command to read data
1721          */
1722         memset(p_tx_buf, 0, count + K7_RD_HEADER_SIZE);
1723
1724         if (copy_from_user(p_tx_buf, p, K7_RD_HEADER_SIZE)) {
1725                 status = -EFAULT;
1726                 goto ext_dev_read_wo_limit_free_tx;
1727         }
1728
1729         /*
1730          * Read data
1731          */
1732         if (lr388k7_spi_read(p_tx_buf, p_buf, count)) {
1733                 if (copy_to_user(p, &p_buf[K7_RD_HEADER_SIZE], count)) {
1734                         status = -EFAULT;
1735                         goto ext_dev_read_wo_limit_free_tx;
1736                 }
1737
1738                 status = count;
1739
1740         } else
1741                 status = -EFAULT;
1742
1743  ext_dev_read_wo_limit_free_tx:
1744         kfree(p_tx_buf);
1745  ext_dev_read_wo_limit_free:
1746         kfree(p_buf);
1747  ext_dev_read_wo_limit:
1748         return status;
1749 }
1750
1751 static long lr388k7_spi_read_from_ioctl(u8 *p, size_t count)
1752 {
1753         u8 *p_buf;
1754         u8 *p_tx_buf;
1755         ssize_t status = 0;
1756
1757         /*
1758          * Memory Allocation
1759          */
1760         p_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1761         if (p_buf == NULL) {
1762                 status = -ENOMEM;
1763                 goto ext_dev_read_from_ioctl;
1764         }
1765
1766         p_tx_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1767         if (p_tx_buf == NULL) {
1768                 status = -ENOMEM;
1769                 goto ext_dev_read_from_ioctl_free;
1770         }
1771
1772         /*
1773          * Clear and Set command to read data
1774          */
1775         memset(p_tx_buf, 0, count + K7_RD_HEADER_SIZE);
1776
1777         if (copy_from_user(p_tx_buf, p, K7_RD_HEADER_SIZE)) {
1778                 status = -EFAULT;
1779                 goto ext_dev_read_from_ioctl_free_tx;
1780         }
1781
1782         g_st_state.u32SCK = 0;
1783         /*
1784          * Read data
1785          */
1786         if (lr388k7_spi_read(p_tx_buf, p_buf, count)) {
1787                 if (copy_to_user(p, &p_buf[K7_RD_HEADER_SIZE], count)) {
1788                         status = -EFAULT;
1789                         goto ext_dev_read_from_ioctl_free_tx;
1790                 }
1791
1792                 status = count;
1793
1794         } else
1795                 status = -EFAULT;
1796
1797  ext_dev_read_from_ioctl_free_tx:
1798         kfree(p_tx_buf);
1799  ext_dev_read_from_ioctl_free:
1800         kfree(p_buf);
1801  ext_dev_read_from_ioctl:
1802         g_st_state.u32SCK = g_spi->max_speed_hz;
1803         return status;
1804 }
1805
1806 static ssize_t
1807 dev_read(struct file *filp, char __user *buf, size_t count, loff_t *pos)
1808 {
1809 #if 0
1810         u8 *p_buf;
1811         ssize_t status = 0;
1812
1813         p_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1814         if (!p_buf) {
1815                 status = -ENOMEM;
1816                 goto exit_dev_read;
1817         }
1818
1819         memset(p_buf, 0, count + K7_RD_HEADER_SIZE);
1820
1821         if (copy_from_user(p_buf, buf, K7_RD_HEADER_SIZE)) {
1822                 status = -EFAULT;
1823                 goto exit_dev_read_free;
1824         }
1825
1826         if (lr388k7_spi_read(p_buf, count)) {
1827                 if (copy_to_user(buf, &p_buf[K7_RD_HEADER_SIZE], count)) {
1828                         status = -EFAULT;
1829                         goto exit_dev_read_free;
1830                 }
1831
1832                 status = count;
1833
1834         } else
1835                 status = -EFAULT;
1836
1837  exit_dev_read_free:
1838         kfree(p_buf);
1839  exit_dev_read:
1840         return status;
1841 #else
1842         return 0;
1843 #endif
1844 }
1845
1846 /**
1847  * @brief lr388k7 spi interface
1848  * @param[in] txbuf data to be written
1849  * @param[in] len length of data
1850  * @retval TRUE if success, otherwise FALSE
1851  */
1852 static int lr388k7_spi_write(u8 *txbuf, size_t len)
1853 {
1854         static DEFINE_MUTEX(lock);
1855         int status;
1856
1857         mutex_lock(&lock);
1858
1859         /*It returns zero on succcess,else a negative error code. */
1860         status = spi_write(g_spi, txbuf, len);
1861
1862         mutex_unlock(&lock);
1863
1864         if (status)
1865                 return false;
1866
1867         return true;
1868 }
1869
1870 static u32 lr388k7_get_platform_id(u8 *p)
1871 {
1872         u32 ret;
1873         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1874
1875         ret = copy_to_user(p,
1876                            &ts->platform_id,
1877                            sizeof(ts->platform_id));
1878
1879         return ret;
1880 }
1881
1882 static u32 lr388k7_get_value(u8 *arg, unsigned int index)
1883 {
1884         u32 ret = 0;
1885         switch (index) {
1886         case LR388K7_GET_PLATFORM_ID:
1887                 ret = lr388k7_get_platform_id(arg);
1888                 break;
1889         default:
1890                 ret = -EINVAL;
1891                 break;
1892         }
1893         return ret;
1894 }
1895
1896
1897 static ssize_t
1898 dev_write(struct file *filp, const char __user *buf,
1899                 size_t count, loff_t *pos)
1900 {
1901         u8 *p_buf;
1902         ssize_t status = 0;
1903
1904         p_buf = kmalloc(count, GFP_KERNEL);
1905         if (!p_buf) {
1906                 status = -ENOMEM;
1907                 goto exit_dev_write;
1908         }
1909
1910         if (copy_from_user(p_buf, buf, count)) {
1911                 status = -EFAULT;
1912                 goto exit_dev_write_free;
1913         }
1914
1915         if (lr388k7_spi_write(p_buf, count))
1916                 status = count;
1917         else
1918                 status = -EFAULT;
1919
1920  exit_dev_write_free:
1921         kfree(p_buf);
1922  exit_dev_write:
1923         return status;
1924 }
1925
1926 static long dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1927 {
1928         long ret = true;
1929         unsigned int index;
1930
1931         index = (cmd >> 16) & 0xFFFF;
1932
1933         switch (cmd & 0xFFFF) {
1934         case LR388K7_IOCTL_TOUCH_REPORT:
1935                 lr388k7_touch_report((void *) arg);
1936                 break;
1937
1938         case LR388K7_IOCTL_READ_REGISTER:
1939                 ret = lr388k7_spi_read_from_ioctl((u8 *) arg, index);
1940                 break;
1941
1942         case LR388K7_IOCTL_READ_WO_LIMIT:
1943                 ret = lr388k7_spi_read_wo_limit((u8 *) arg, index);
1944                 break;
1945
1946         case LR388K7_IOCTL_SET_HAL_PID:
1947                 g_st_state.u32_pid = arg;
1948                 g_st_state.b_is_calc_finish = 1;
1949                 break;
1950
1951         case LR388K7_IOCTL_READ_RAW_DATA:
1952                 ret = lr388k7_queue_read_raw_data((u8 *) arg, index);
1953                 break;
1954
1955         case LR388K7_IOCTL_READ_CMD_DATA:
1956                 ret = lr388k7_cmd_queue_read((u8 *) arg, index);
1957                 break;
1958
1959         case LR388K7_IOCTL_FINISH_CALC:
1960                 g_st_state.b_is_calc_finish = 1;
1961                 break;
1962
1963         case LR388K7_IOCTL_SET_RAW_DATA_SIZE:
1964                 g_u32_raw_data_size = arg;
1965                 break;
1966
1967         case LR388K7_IOCTL_SET_REVISION:
1968                 g_st_state.u16fw_ver = (arg & 0xFFFF0000) >> 16;
1969                 g_st_state.u16module_ver = arg & 0x0000FFFF;
1970
1971                 dev_info(&g_spi->dev, "Version %d.%d.%d\n",
1972                          g_st_state.u16fw_ver,
1973                          K7_DRIVER_VERSION,
1974                          g_st_state.u16module_ver
1975                          );
1976                 break;
1977
1978         case LR388K7_IOCTL_GET_DEBUG_STATUS:
1979                 lr388k7_ts_get_debug_status((u8 *)arg);
1980                 break;
1981
1982         case LR388K7_IOCTL_SET_MODE:
1983                 g_u8_mode = (u8)arg;
1984                 break;
1985
1986         case LR388K7_IOCTL_GET_MODE:
1987                 lr388k7_ts_get_mode((u8 *)arg);
1988                 break;
1989
1990         case LR388K7_IOCTL_RESET:
1991                 ret = lr388k7_hardreset(arg);
1992                 break;
1993
1994         case LR388K7_IOCTL_CLEAR_BUFFER:
1995                 lr388k7_queue_reset();
1996                 g_st_state.b_is_init_finish = arg;
1997                 if (g_st_state.b_is_init_finish)
1998                         lr388k7_send_signal(g_st_state.u32_pid,
1999                                             LR388K7_SIGNAL_MODE);
2000                 break;
2001
2002         case LR388K7_IOCTL_ACTIVE_REPORT:
2003 #if defined(ACTIVE_ENABLE)
2004                 lr388k7_active_report((void *) arg);
2005 #endif
2006                 break;
2007
2008         case LR388K7_IOCTL_GET_VALUE:
2009                 ret = lr388k7_get_value((u8 *)arg, index);
2010                 break;
2011
2012         case LR388K7_IOCTL_SET_CLK_SEL:
2013 #if defined(ACTIVE_ENABLE)
2014                 lr388k7_set_clk_sel((unsigned int)arg);
2015 #endif
2016                 break;
2017
2018         case LR388K7_IOCTL_SET_STATE:
2019                 g_u8_scan = (u8)arg;
2020                 break;
2021
2022 #if defined(UDEV_ENABLE)
2023         case LR388K7_IOCTL_SEND_UDEV:
2024                 lr388k7_send_udev_event((void *)arg);
2025                 break;
2026 #endif
2027
2028         default:
2029                 ret = false;
2030                 break;
2031         }
2032
2033         return ret;
2034 }
2035
2036 static void lr388k7_init_parameter(void)
2037 {
2038         g_u8_mode = K7_DEFAULT_MODE;
2039         g_u8_scan = K7_SCAN_STATE_IDLE;
2040         g_st_state.u32_pid = 0;
2041         g_st_state.b_is_init_finish = false;
2042         g_st_state.b_is_suspended = false;
2043         g_st_state.b_is_reset = false;
2044         g_st_state.u32SCK = 0;
2045         lr388k7_queue_reset();
2046
2047         g_st_dbg.slowscan.enable = 0;
2048         g_st_dbg.slowscan.scan_rate = K7_SLOW_SCAN_60;
2049         g_st_dbg.wakeup.enable = 0;
2050         g_st_dbg.wakeup.num_tap = K7_NUM_TAP_2;
2051         g_st_dbg.u8ForceCap = 0;
2052         g_st_dbg.u8Dump = 0;
2053         g_st_dbg.u8Test = 0;
2054         g_u16_wakeup_enable = 0;
2055 }
2056
2057 static void lr388k7_init_ts(void)
2058 {
2059         lr388k7_init_parameter();
2060         memset(&g_st_state, 0, sizeof(struct lr388k7_ts_parameter));
2061
2062         g_st_state.st_wq_k7 = alloc_workqueue("lr388k7_work", WQ_HIGHPRI, 1);
2063         INIT_WORK(&g_st_state.st_work_k7, lr388k7_work_handler);
2064 }
2065
2066 static struct lr388k7_platform_data *lr388k7_parse_dt(struct device *dev,
2067                                                       int irq)
2068 {
2069         struct lr388k7_platform_data *pdata;
2070         struct device_node *np = dev->of_node;
2071         int ret, val, irq_gpio;
2072         const char *str;
2073
2074         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2075         if (!pdata)
2076                 return ERR_PTR(-ENOMEM);
2077
2078         pdata->gpio_reset = of_get_named_gpio_flags(np, "reset-gpio", 0, NULL);
2079         if (!gpio_is_valid(pdata->gpio_reset)) {
2080                 dev_err(dev, "Invalid reset-gpio\n");
2081                 return ERR_PTR(-EINVAL);
2082         }
2083         ret = gpio_request(pdata->gpio_reset, "reset-gpio");
2084         if (ret < 0) {
2085                 dev_err(dev, "gpio_request failed\n");
2086                 return ERR_PTR(-EINVAL);
2087         }
2088         gpio_direction_output(pdata->gpio_reset, 0);
2089
2090         ret = of_property_read_u32(np, "interrupts", &irq_gpio);
2091         if (!gpio_is_valid(irq_gpio)) {
2092                 dev_err(dev, "Invalid irq-gpio\n");
2093                 ret = -EINVAL;
2094                 goto exit_release_reset_gpio;
2095         }
2096         ret = gpio_request(irq_gpio, "irq-gpio");
2097         if (ret < 0) {
2098                 dev_err(dev, "irq_request fail\n");
2099                 ret = -EINVAL;
2100                 goto exit_release_reset_gpio;
2101         }
2102         gpio_direction_input(irq_gpio);
2103         pdata->gpio_irq = irq_gpio;
2104
2105         ret = of_property_read_u32(np, "x-max", &val);
2106         if (ret < 0)
2107                 goto exit_release_all_gpio;
2108         pdata->ts_x_max = val;
2109
2110         ret = of_property_read_u32(np, "y-max", &val);
2111         if (ret < 0)
2112                 goto exit_release_all_gpio;
2113         pdata->ts_y_max = val;
2114
2115         ret = of_property_read_u32(np, "z-max", &val);
2116         if (ret < 0)
2117                 goto exit_release_all_gpio;
2118         pdata->ts_pressure_max = val;
2119
2120         ret = of_property_read_u32(np, "touch-num-max", &val);
2121         if (ret < 0)
2122                 goto exit_release_all_gpio;
2123         pdata->ts_touch_num_max = val;
2124
2125         ret = of_property_read_string(np, "name-of-clock", &str);
2126         if (ret < 0)
2127                 goto exit_release_all_gpio;
2128         pdata->name_of_clock = (char *)str;
2129
2130         ret = of_property_read_string(np, "name-of-clock-con", &str);
2131         if (ret < 0)
2132                 goto exit_release_all_gpio;
2133         pdata->name_of_clock_con = (char *)str;
2134
2135 #if defined(ACTIVE_ENABLE)
2136         pdata->gpio_clk_sel = of_get_named_gpio_flags(np,
2137                                                       "clock-sel-gpio",
2138                                                       0,
2139                                                       NULL);
2140         if (!gpio_is_valid(pdata->gpio_clk_sel)) {
2141                 dev_err(dev, "Invalid clock-sel-gpio\n");
2142                 return ERR_PTR(-EINVAL);
2143         }
2144         ret = gpio_request(pdata->gpio_clk_sel, "clock-sel-gpio");
2145         if (ret < 0) {
2146                 dev_err(dev, "gpio_request failed\n");
2147                 return ERR_PTR(-EINVAL);
2148         }
2149         gpio_direction_output(pdata->gpio_clk_sel, 0);
2150 #endif
2151
2152         ret = of_property_read_u32(np, "platform-id", &val);
2153         if (ret < 0)
2154                 goto exit_release_all_gpio;
2155         pdata->platform_id = val;
2156
2157         return pdata;
2158
2159  exit_release_all_gpio:
2160         gpio_free(irq_gpio);
2161  exit_release_reset_gpio:
2162         gpio_free(pdata->gpio_reset);
2163         return ERR_PTR(ret);
2164 }
2165
2166 static int lr388k7_probe(struct spi_device *spi)
2167 {
2168         struct lr388k7_platform_data *pdata;/* = spi->dev.platform_data;*/
2169         struct lr388k7 *ts;
2170         struct input_dev *input_dev;
2171 #if defined(ACTIVE_ENABLE)
2172         struct input_dev *input_dev_active;
2173 #endif
2174         struct device *dev;
2175         int error;
2176
2177         dev = &spi->dev;
2178         dev_info(dev, "[ENTER] probe\n");
2179
2180         lr388k7_init_ts();
2181
2182         g_spi = spi;
2183
2184         if (spi->irq <= 0) {
2185                 dev_dbg(dev, "no irq\n");
2186                 return -ENODEV;
2187         }
2188
2189         spi->mode = SPI_MODE_3;
2190         spi->bits_per_word = 8;
2191         if (!spi->max_speed_hz)
2192                 spi->max_speed_hz = K7_MAX_SPEED_HZ;
2193
2194         error = spi_setup(spi);
2195         if (error)
2196                 return error;
2197
2198         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
2199         input_dev = input_allocate_device();
2200 #if defined(ACTIVE_ENABLE)
2201         input_dev_active = input_allocate_device();
2202         if (!ts || !input_dev || !input_dev_active) {
2203 #else
2204         if (!ts || !input_dev) {
2205 #endif
2206                 error = -ENOMEM;
2207                 goto err_free_mem;
2208         }
2209
2210
2211         ts->spi = spi;
2212         ts->dev = dev;
2213         ts->idev = input_dev;
2214 #if defined(ACTIVE_ENABLE)
2215         ts->idev_active = input_dev_active;
2216 #endif
2217         ts->irq = spi->irq;
2218
2219         if (dev->of_node) {
2220
2221                 dev_info(dev, "DT support\n");
2222                 /* Loading data from DT */
2223                 pdata = lr388k7_parse_dt(dev, spi->irq);
2224                 if (IS_ERR(pdata)) {
2225                         dev_err(dev, "failed to parse device tree\n");
2226                         error = -EINVAL;
2227                         goto err_free_mem;
2228                 }
2229                 dev->platform_data = pdata;
2230         } else {
2231                 /* Non-DT */
2232                 dev_info(dev, "Non-DT\n");
2233         }
2234
2235         pdata = dev->platform_data;
2236
2237         ts->max_num_touch = __min(pdata->ts_touch_num_max, K7_MAX_TOUCH_NUM);
2238         ts->max_x       = pdata->ts_x_max ? : MAX_16BIT;
2239         ts->max_y       = pdata->ts_y_max ? : MAX_16BIT;
2240         ts->max_z       = pdata->ts_pressure_max ? : MAX_16BIT;
2241         ts->swap_xy     = false;
2242         ts->flip_x      = false;
2243         ts->flip_y      = false;
2244         ts->gpio_reset  = pdata->gpio_reset;
2245         ts->gpio_irq  = pdata->gpio_irq;
2246         ts->b_eraser_active = false;
2247 #if defined(ACTIVE_ENABLE)
2248         ts->tool        = 0;
2249         ts->gpio_clk_sel = pdata->gpio_clk_sel;
2250 #endif
2251         ts->platform_id = pdata->platform_id;
2252
2253         mutex_init(&ts->mutex);
2254
2255         spin_lock_init(&ts->lock);
2256
2257         snprintf(ts->phys,
2258                  sizeof(ts->phys),
2259                  "%s/input-ts",
2260                  dev_name(dev));
2261
2262         /* misc */
2263         if (misc_register(&lr388k7_ts_miscdev) != 0) {
2264                 dev_err(dev, "cannot register miscdev\n");
2265                 error = -ENOMEM;
2266                 goto err_free_mem;
2267         }
2268
2269 #if defined(DEBUG_LR388K7)
2270         dev_info(&spi->dev, "Success register miscdev\n");
2271 #endif
2272
2273         if (lr388k7_queue_init()) {
2274                 dev_err(dev, "Cannot allocate queue memory\n");
2275                 error = -ENOMEM;
2276                 goto err_free_mem;
2277         }
2278
2279 #if defined(ACTIVE_ENABLE)
2280         /* Select external clock */
2281         gpio_set_value(ts->gpio_clk_sel, 0);
2282 #endif
2283         /* Reset assert */
2284         gpio_set_value(ts->gpio_reset, 0);
2285         g_st_state.b_is_reset = true;
2286
2287         /* regulator */
2288         ts->regulator_3v3 = devm_regulator_get(&g_spi->dev, "avdd");
2289         if (IS_ERR(ts->regulator_3v3)) {
2290                 dev_err(dev,
2291                         "LR388K7 TS: regulator_get failed: %ld\n",
2292                         PTR_ERR(ts->regulator_3v3));
2293                 return -ENODEV;
2294         }
2295
2296         ts->regulator_1v8 = devm_regulator_get(&g_spi->dev, "dvdd");
2297         if (IS_ERR(ts->regulator_1v8)) {
2298                 dev_err(dev,
2299                         "LR388K7 TS: regulator_get failed: %ld\n",
2300                         PTR_ERR(ts->regulator_1v8));
2301                 return -ENODEV;
2302         }
2303
2304         /* Enable 1v8 first*/
2305         error = regulator_enable(ts->regulator_1v8);
2306         if (error < 0)
2307                 dev_err(dev,
2308                         "LR388K7 TS: regulator enable failed: %d\n",
2309                         error);
2310         usleep_range(5000, 6000);
2311         error = regulator_enable(ts->regulator_3v3);
2312         if (error < 0)
2313                 dev_err(dev,
2314                         "LR388K7 TS: regulator enable failed: %d\n",
2315                         error);
2316
2317         /* clk */
2318         if (pdata->name_of_clock || pdata->name_of_clock_con) {
2319                 ts->clk =
2320                         clk_get_sys(pdata->name_of_clock,
2321                                     pdata->name_of_clock_con);
2322                 if (IS_ERR(ts->clk)) {
2323                         dev_err(dev,
2324                                 "failed to get touch_clk:(%s, %s)\n",
2325                                 pdata->name_of_clock,
2326                                 pdata->name_of_clock_con);
2327                         error = -EINVAL;
2328                         goto err_free_mem;
2329                 }
2330         }
2331
2332         if (ts->clk)
2333                 clk_enable(ts->clk);
2334
2335         input_dev->name = "touch";
2336         input_dev->phys = ts->phys;
2337         input_dev->id.bustype = BUS_SPI;
2338         input_dev->dev.parent = &spi->dev;
2339         __set_bit(EV_SYN, input_dev->evbit);
2340         __set_bit(EV_ABS, input_dev->evbit);
2341         __set_bit(EV_KEY, input_dev->evbit);
2342         __set_bit(KEY_POWER, input_dev->keybit);
2343
2344         if (ts->swap_xy) {
2345                 input_set_abs_params(input_dev,
2346                                      ABS_MT_POSITION_X,
2347                                      0,
2348                                      ts->max_y,
2349                                      0,
2350                                      0);
2351                 input_set_abs_params(input_dev,
2352                                      ABS_MT_POSITION_Y,
2353                                      0,
2354                                      ts->max_x,
2355                                      0,
2356                                      0);
2357         } else {
2358                 input_set_abs_params(input_dev,
2359                                      ABS_MT_POSITION_X,
2360                                      0,
2361                                      ts->max_x,
2362                                      0,
2363                                      0);
2364                 input_set_abs_params(input_dev,
2365                                      ABS_MT_POSITION_Y,
2366                                      0, ts->max_y,
2367                                      0,
2368                                      0);
2369         }
2370         input_set_abs_params(input_dev,
2371                              ABS_MT_PRESSURE,
2372                              0,
2373                              ts->max_z,
2374                              0,
2375                              0);
2376         input_set_abs_params(input_dev,
2377                              ABS_MT_TOOL_TYPE,
2378                              0,
2379                              MT_TOOL_MAX,
2380                              0,
2381                              0);
2382 #if defined(PROTOCOL_A)
2383         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
2384
2385         input_set_abs_params(input_dev,
2386                              ABS_MT_TRACKING_ID,
2387                              0,
2388                              ts->max_num_touch,
2389                              0,
2390                              0);
2391 #else /* PROTOCOL B */
2392         error = input_mt_init_slots(input_dev,
2393                                     ts->max_num_touch,
2394                                     0);
2395         if (error) {
2396                 dev_err(dev, "Failed to mt_init_slots err: %d\n", error);
2397                 goto err_free_mem;
2398         }
2399
2400 #endif
2401
2402         input_set_capability(input_dev, EV_MSC, MSC_ACTIVITY);
2403
2404         input_dev->open = lr388k7_open;
2405         input_dev->close = lr388k7_close;
2406         input_dev->enable = lr388k7_input_enable;
2407         input_dev->disable = lr388k7_input_disable;
2408         input_dev->enabled = true;
2409         input_set_drvdata(input_dev, ts);
2410
2411 #if defined(ACTIVE_ENABLE)
2412         input_dev_active->name = "touch_active";
2413         input_dev_active->phys = ts->phys;
2414         input_dev_active->id.bustype = BUS_SPI;
2415         input_dev_active->dev.parent = &spi->dev;
2416
2417         __set_bit(EV_ABS, input_dev_active->evbit);
2418         __set_bit(EV_KEY, input_dev_active->evbit);
2419         __set_bit(INPUT_PROP_POINTER, input_dev_active->propbit);
2420         __set_bit(BTN_TOUCH, input_dev_active->keybit);
2421         __set_bit(BTN_TOOL_PEN, input_dev_active->keybit);
2422         __set_bit(BTN_TOOL_RUBBER, input_dev_active->keybit);
2423         __set_bit(BTN_STYLUS, input_dev_active->keybit);
2424
2425         if (ts->swap_xy) {
2426                 input_set_abs_params(input_dev_active,
2427                                      ABS_X,
2428                                      0,
2429                                      ts->max_y,
2430                                      0,
2431                                      0);
2432                 input_set_abs_params(input_dev_active,
2433                                      ABS_Y,
2434                                      0,
2435                                      ts->max_x,
2436                                      0,
2437                                      0);
2438         } else {
2439                 input_set_abs_params(input_dev_active,
2440                                      ABS_X,
2441                                      0,
2442                                      ts->max_x,
2443                                      0,
2444                                      0);
2445                 input_set_abs_params(input_dev_active,
2446                                      ABS_Y,
2447                                      0, ts->max_y,
2448                                      0,
2449                                      0);
2450         }
2451         input_set_abs_params(input_dev_active,
2452                              ABS_PRESSURE,
2453                              0,
2454                              MAX_10BIT,
2455                              0,
2456                              0);
2457         input_set_drvdata(input_dev_active, ts);
2458 #endif
2459
2460 #if 0
2461         error = request_irq(spi->irq, lr388k7_irq_thread,
2462                             IRQF_TRIGGER_FALLING,
2463                             "lr388k7_ts", ts);
2464 #else
2465         error = request_threaded_irq(spi->irq,
2466                                      lr388k7_irq_thread,
2467                                      lr388k7_wakeup_thread,
2468                                      IRQF_TRIGGER_FALLING,
2469                                      "lr388k7_ts", ts);
2470
2471 #endif
2472
2473         if (error) {
2474                 dev_err(dev, "Failed to request irq, err: %d\n", error);
2475                 goto err_free_mem;
2476         }
2477
2478         lr388k7_disable_irq(ts);
2479
2480         spi_set_drvdata(spi, ts);
2481
2482         error = sysfs_create_link(&dev->kobj,
2483                                   &lr388k7_ts_miscdev.this_device->kobj,
2484                                   "touch");
2485         if (error) {
2486                 dev_err(dev, "failed to create sysfs group\n");
2487                 goto err_clear_drvdata;
2488         }
2489
2490         error = input_register_device(ts->idev);
2491         if (error) {
2492                 dev_err(dev,
2493                         "Failed to register input device, err: %d\n", error);
2494                 goto err_clear_drvdata;
2495         }
2496
2497 #if defined(ACTIVE_ENABLE)
2498         error = input_register_device(ts->idev_active);
2499         if (error) {
2500                 dev_err(dev,
2501                         "Failed to register input active device , err: %d\n",
2502                         error);
2503                 goto err_clear_drvdata;
2504         }
2505 #endif
2506
2507         error = sysfs_create_group(&lr388k7_ts_miscdev.this_device->kobj,
2508                                    &lr388k7_ts_attr_group);
2509         if (error) {
2510                 dev_err(dev, "failed to create sysfs group\n");
2511                 goto err_clear_drvdata;
2512         }
2513
2514         /* Enable async suspend/resume to reduce LP0 latency */
2515         device_enable_async_suspend(dev);
2516
2517         dev_info(dev, "[EXIT] probe\n");
2518         return 0;
2519
2520  err_clear_drvdata:
2521         spi_set_drvdata(spi, NULL);
2522         free_irq(spi->irq, ts);
2523  err_free_mem:
2524 #if defined(ACTIVE_ENABLE)
2525         input_free_device(input_dev_active);
2526 #endif
2527         input_free_device(input_dev);
2528         kfree(ts);
2529         return error;
2530 }
2531
2532 static int lr388k7_remove(struct spi_device *spi)
2533 {
2534         struct lr388k7 *ts = spi_get_drvdata(spi);
2535
2536         lr388k7_queue_free();
2537
2538         if (g_st_state.st_wq_k7)
2539                 destroy_workqueue(g_st_state.st_wq_k7);
2540
2541         sysfs_remove_group(&lr388k7_ts_miscdev.this_device->kobj,
2542                            &lr388k7_ts_attr_group);
2543
2544         sysfs_remove_link(&ts->dev->kobj, "touch");
2545
2546         misc_deregister(&lr388k7_ts_miscdev);
2547         free_irq(ts->spi->irq, ts);
2548
2549         gpio_free(ts->gpio_reset);
2550         gpio_free(ts->gpio_irq);
2551 #if defined(ACTIVE_ENABLE)
2552         gpio_free(ts->gpio_clk_sel);
2553 #endif
2554         input_unregister_device(ts->idev);
2555
2556         kfree(ts);
2557
2558         spi_set_drvdata(spi, NULL);
2559         return 0;
2560 }
2561
2562 static void lr388k7_shutdown(struct spi_device *spi)
2563 {
2564         struct lr388k7 *ts = spi_get_drvdata(spi);
2565
2566         free_irq(ts->irq, ts);
2567
2568         if (ts->regulator_3v3)
2569                 regulator_disable(ts->regulator_3v3);
2570
2571         if (ts->regulator_1v8)
2572                 regulator_disable(ts->regulator_1v8);
2573 }
2574
2575 static void lr388k7_ctrl_resume(struct lr388k7 *ts)
2576 {
2577         u8 u8_buf[5];
2578         size_t count = 0;
2579
2580         u8_buf[count++] = K7_WR_OPCODE;
2581         u8_buf[count++] = (K7_STATE_CTL_ADDR >> 16) & 0xFF;
2582         u8_buf[count++] = (K7_STATE_CTL_ADDR >>  8) & 0xFF;
2583         u8_buf[count++] = (K7_STATE_CTL_ADDR >>  0) & 0xFF;
2584         u8_buf[count++] = K7_POWER_CTL_RESUME;
2585         lr388k7_spi_write(u8_buf, count);
2586 }
2587
2588 static void lr388k7_start(struct lr388k7 *ts)
2589 {
2590         int error;
2591
2592         g_st_state.b_is_suspended = false;
2593
2594         mutex_lock(&ts->mutex);
2595
2596         if (g_st_dbg.wakeup.enable == 1) {
2597                 lr388k7_ctrl_resume(ts);
2598                 mutex_unlock(&ts->mutex);
2599                 return;
2600         }
2601
2602         /* Reset assert */
2603         gpio_set_value(ts->gpio_reset, 0);
2604         g_st_state.b_is_reset = true;
2605
2606         /*
2607          * Enable regulator, if necessary
2608          */
2609         /* Enable 1v8 first*/
2610         error = regulator_enable(ts->regulator_1v8);
2611         if (error < 0)
2612                 dev_err(&g_spi->dev,
2613                         "LR388K7 TS: regulator enable failed: %d\n", error);
2614
2615         error = regulator_enable(ts->regulator_3v3);
2616         if (error < 0)
2617                 dev_err(&g_spi->dev,
2618                         "LR388K7 TS: regulator enable failed: %d\n", error);
2619
2620         usleep_range(5000, 6000);
2621
2622         /*
2623          * Enable clock, if necessary
2624          */
2625         if (ts->clk)
2626                 clk_enable(ts->clk);
2627
2628         /* Reset deassert */
2629         gpio_set_value(ts->gpio_reset, 1);
2630         g_st_state.b_is_reset = false;
2631
2632         usleep_range(12000, 13000);
2633
2634         mutex_unlock(&ts->mutex);
2635 }
2636
2637 static void lr388k7_ctrl_suspend(struct lr388k7 *ts)
2638 {
2639         u8 u8_buf[5];
2640         size_t count = 0;
2641         int error;
2642         u8 u8_status = 0;
2643         int irq_value;
2644
2645         mutex_lock(&ts->mutex);
2646
2647         if (g_st_dbg.wakeup.enable == 1) {
2648                 u8_buf[count++] = K7_WR_OPCODE;
2649                 u8_buf[count++] = (K7_STATE_CTL_ADDR >> 16) & 0xFF;
2650                 u8_buf[count++] = (K7_STATE_CTL_ADDR >>  8) & 0xFF;
2651                 u8_buf[count++] = (K7_STATE_CTL_ADDR >>  0) & 0xFF;
2652                 u8_buf[count++] = K7_POWER_CTL_TAP_WAKEUP;
2653                 lr388k7_spi_write(u8_buf, count);
2654
2655                 irq_value = gpio_get_value(ts->gpio_irq);
2656
2657                 if (!irq_value) {
2658                         u8_status = lr388k7_clear_irq();
2659
2660                         lr388k7_event_handler(u8_status);
2661                 }
2662
2663                 mutex_unlock(&ts->mutex);
2664                 return;
2665         }
2666
2667         /* Disable (3.3V) */
2668         if (ts->regulator_3v3) {
2669                 error = regulator_disable(ts->regulator_3v3);
2670                 if (error < 0)
2671                         dev_err(&g_spi->dev,
2672                                 "lr388k7 regulator 3.3V disable failed: %d\n",
2673                                 error);
2674         }
2675         /* handle platforms w/ and w/out regulator switches */
2676         /* 2) delay for platforms w/ regulator switches */
2677         /* usleep_range(15000, 20000);  */ /*msleep(15); */
2678         /* 3) disable clock */
2679         if (ts->clk)
2680                 clk_disable(ts->clk);
2681         /* 4) disable 1.8 */
2682         if (ts->regulator_1v8 && ts->regulator_3v3) {
2683                 error = regulator_disable(ts->regulator_1v8);
2684                 if (error < 0)
2685                         dev_err(&g_spi->dev,
2686                                 "lr388k7 1.8V disable failed: %d\n",
2687                                 error);
2688         }
2689
2690         mutex_unlock(&ts->mutex);
2691 }
2692
2693 static void lr388k7_stop(struct lr388k7 *ts)
2694 {
2695         flush_workqueue(g_st_state.st_wq_k7);
2696
2697         lr388k7_ctrl_suspend(ts);
2698 }
2699
2700 #ifdef CONFIG_PM
2701 static int lr388k7_suspend(struct device *dev)
2702 {
2703         struct lr388k7 *ts = dev_get_drvdata(dev);
2704
2705         if (g_st_state.b_is_suspended)
2706                 return 0;
2707
2708         lr388k7_disable_irq(ts);
2709
2710         lr388k7_stop(ts);
2711
2712         lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_SLEP);
2713
2714         g_st_state.b_is_suspended = true;
2715
2716         return 0;
2717 }
2718
2719 static int lr388k7_resume(struct device *dev)
2720 {
2721         struct lr388k7 *ts = dev_get_drvdata(dev);
2722         u8 u8_status = 0;
2723         int irq_value;
2724         lr388k7_start(ts);
2725
2726         lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_WAKE);
2727
2728         lr388k7_enable_irq(ts);
2729
2730         if (g_st_dbg.wakeup.enable == 2)
2731                 g_st_dbg.wakeup.enable = g_u16_wakeup_enable;
2732
2733         if (IS_DBG) {
2734                 g_st_dbg.u8ForceCap = 0;
2735                 g_st_dbg.u8Dump = 0;
2736                 g_st_dbg.slowscan.enable = 0;
2737
2738                 lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_CTRL);
2739         }
2740
2741         if (g_st_dbg.wakeup.enable == 0)
2742                 return 0;
2743
2744         /* check irq */
2745         irq_value = gpio_get_value(ts->gpio_irq);
2746         if (!irq_value) {
2747
2748                 u8_status = lr388k7_clear_irq();
2749
2750                 while (u8_status) {
2751
2752                         lr388k7_event_handler(u8_status);
2753                         irq_value = gpio_get_value(ts->gpio_irq);
2754
2755                         if (!irq_value)
2756                                 u8_status = lr388k7_clear_irq();
2757                         else
2758                                 u8_status = 0;
2759                 }
2760         }
2761
2762         return 0;
2763 }
2764 #endif /*CONFIG_PM */
2765
2766 static int lr388k7_input_enable(struct input_dev *idev)
2767 {
2768         struct lr388k7 *ts = input_get_drvdata(idev);
2769         int err = 0;
2770 #if defined(DEBUG_LR388K7)
2771         dev_info(ts->dev, "[ENTER] input enable\n");
2772 #endif
2773
2774 #ifdef CONFIG_PM
2775         err = lr388k7_resume(ts->dev);
2776 #endif
2777
2778 #if defined(DEBUG_LR388K7)
2779         dev_info(ts->dev, "[EXIT] input enable\n");
2780 #endif
2781         return 0;
2782 }
2783
2784 static int lr388k7_input_disable(struct input_dev *idev)
2785 {
2786         struct lr388k7 *ts = input_get_drvdata(idev);
2787         int err = 0;
2788 #if defined(DEBUG_LR388K7)
2789         dev_info(ts->dev, "[ENTER] input disable\n");
2790 #endif
2791
2792 #ifdef CONFIG_PM
2793         err = lr388k7_suspend(ts->dev);
2794 #endif
2795
2796 #if defined(DEBUG_LR388K7)
2797         dev_info(ts->dev, "[EXIT] input disable\n");
2798 #endif
2799         return 0;
2800 }
2801
2802 #if defined(CONFIG_PM) & !defined(INPUT_ENABLE)
2803 static const struct dev_pm_ops lr388k7_pm_ops = {
2804         .suspend = lr388k7_suspend,
2805         .resume = lr388k7_resume,
2806 };
2807 #endif
2808
2809 static const struct of_device_id lr388k7_ts_match[] = {
2810         { .compatible = "sharp,lr388k7_ts" },
2811         { },
2812 };
2813 MODULE_DEVICE_TABLE(of, lr388k7_ts_match);
2814
2815 static struct spi_driver lr388k7_driver = {
2816         .driver = {
2817                 .name   = "lr388k7_ts",
2818                 .owner  = THIS_MODULE,
2819 #if defined(CONFIG_PM) & !defined(INPUT_ENABLE)
2820                 .pm     = &lr388k7_pm_ops,
2821 #endif
2822                 .of_match_table = lr388k7_ts_match,
2823         },
2824         .probe  = lr388k7_probe,
2825         .remove = lr388k7_remove,
2826         .shutdown = lr388k7_shutdown,
2827 };
2828
2829 module_spi_driver(lr388k7_driver);
2830
2831 MODULE_AUTHOR("Makoto Itsuki <itsuki.makoto@sharp.co.jp>");
2832 MODULE_DESCRIPTION("LR388K7 Touchscreen Driver");
2833 MODULE_LICENSE("GPL");