input: touch: sharp: update to version 10.
[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 (10)
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 #if defined(DEBUG_LR388K7)
1087 static ssize_t lr388k7_ts_check_state_store(struct device *dev,
1088         struct device_attribute *attr,
1089         const char *buf, size_t count)
1090 {
1091         ssize_t ret;
1092         ret = (ssize_t) count;
1093         return ret;
1094 }
1095
1096 static ssize_t lr388k7_ts_check_state_show(struct device *dev,
1097         struct device_attribute *attr,
1098         char *buf)
1099 {
1100         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1101         u8 u8_tx_buf[K7_RD_HEADER_SIZE + 5], u8_rx_buf[K7_RD_HEADER_SIZE + 5];
1102         u8 u8Ret;
1103         u8 u8HWR;
1104         u32 u32RES, u32FWR;
1105         size_t count = 0;
1106
1107         u8_tx_buf[count++] = K7_RD_OPCODE;
1108         u8_tx_buf[count++] = (K7_STATE_CTL_ADDR >> 16) & 0xFF;
1109         u8_tx_buf[count++] = (K7_STATE_CTL_ADDR >>  8) & 0xFF;
1110         u8_tx_buf[count++] = (K7_STATE_CTL_ADDR >>  0) & 0xFF;
1111         u8_tx_buf[count++] = 0x00;
1112
1113         g_st_state.u32SCK = 0;
1114
1115         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1116                 u8Ret = u8_rx_buf[K7_RD_HEADER_SIZE];
1117         } else {
1118                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1119                        gpio_get_value(ts->gpio_irq) ?
1120                        "High" : "Low"
1121                        );
1122         }
1123
1124         count = 0;
1125         u8_tx_buf[count++] = K7_RD_OPCODE;
1126         u8_tx_buf[count++] = 0x00;
1127         u8_tx_buf[count++] = 0x02;
1128         u8_tx_buf[count++] = 0xAC;
1129         u8_tx_buf[count++] = 0x00;
1130
1131         g_st_state.u32SCK = 0;
1132
1133         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1134                 u8HWR = u8_rx_buf[K7_RD_HEADER_SIZE];
1135         } else {
1136                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1137                        gpio_get_value(ts->gpio_irq) ?
1138                        "High" : "Low"
1139                        );
1140         }
1141
1142         count = 0;
1143         u8_tx_buf[count++] = K7_RD_OPCODE;
1144         u8_tx_buf[count++] = 0x00;
1145         u8_tx_buf[count++] = 0x02;
1146         u8_tx_buf[count++] = 0xB0;
1147         u8_tx_buf[count++] = 0x00;
1148         u8_tx_buf[count++] = 0x00;
1149         u8_tx_buf[count++] = 0x00;
1150         u8_tx_buf[count++] = 0x00;
1151
1152         g_st_state.u32SCK = 0;
1153
1154         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1155                 u32FWR = u8_rx_buf[K7_RD_HEADER_SIZE] |
1156                         u8_rx_buf[K7_RD_HEADER_SIZE + 1] << 8 |
1157                         u8_rx_buf[K7_RD_HEADER_SIZE + 2] << 16 |
1158                         u8_rx_buf[K7_RD_HEADER_SIZE + 3] << 24;
1159         } else {
1160                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1161                        gpio_get_value(ts->gpio_irq) ?
1162                        "High" : "Low"
1163                        );
1164         }
1165
1166         count = 0;
1167         u8_tx_buf[count++] = K7_RD_OPCODE;
1168         u8_tx_buf[count++] = 0x00;
1169         u8_tx_buf[count++] = 0x00;
1170         u8_tx_buf[count++] = 0x40;
1171         u8_tx_buf[count++] = 0x00;
1172         u8_tx_buf[count++] = 0x00;
1173         u8_tx_buf[count++] = 0x00;
1174         u8_tx_buf[count++] = 0x00;
1175
1176         g_st_state.u32SCK = 0;
1177
1178         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1179                 u32RES = u8_rx_buf[K7_RD_HEADER_SIZE] |
1180                         u8_rx_buf[K7_RD_HEADER_SIZE + 1] << 8 |
1181                         u8_rx_buf[K7_RD_HEADER_SIZE + 2] << 16 |
1182                         u8_rx_buf[K7_RD_HEADER_SIZE + 3] << 24;
1183         } else {
1184                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1185                        gpio_get_value(ts->gpio_irq) ?
1186                        "High" : "Low"
1187                        );
1188         }
1189
1190         g_st_state.u32SCK = g_spi->max_speed_hz;
1191
1192         return sprintf(
1193                        buf,
1194                        "IRQ(%s) status=0x%02X, HWRev=%d, FWRev=0x%0X, Res=0x%04X\n",
1195                        gpio_get_value(ts->gpio_irq) ?
1196                        "High" : "Low",
1197                        u8Ret,
1198                        u8HWR,
1199                        u32FWR,
1200                        u32RES
1201                        );
1202 }
1203 #endif
1204
1205 static DEVICE_ATTR(force_cap, 0666, lr388k7_ts_force_cap_show,
1206                         lr388k7_ts_force_cap_store);
1207 static DEVICE_ATTR(dump, 0666, lr388k7_ts_dump_show,
1208                         lr388k7_ts_dump_store);
1209 static DEVICE_ATTR(report_mode, 0640, lr388k7_ts_report_mode_show,
1210                         lr388k7_ts_report_mode_store);
1211 static DEVICE_ATTR(version, 0640,
1212                    lr388k7_ts_version_show,
1213                    lr388k7_ts_version_store);
1214 static DEVICE_ATTR(slowscan_enable, 0640,
1215                    lr388k7_ts_slowscan_enable_show,
1216                    lr388k7_ts_slowscan_enable_store);
1217 static DEVICE_ATTR(wakeup_enable, 0640,
1218                    lr388k7_ts_wakeup_enable_show,
1219                    lr388k7_ts_wakeup_enable_store);
1220 #if defined(DEBUG_LR388K7)
1221 static DEVICE_ATTR(check_state, 0666,
1222                    lr388k7_ts_check_state_show,
1223                    lr388k7_ts_check_state_store);
1224 #endif
1225
1226
1227 static struct attribute *lr388k7_ts_attributes[] = {
1228         &dev_attr_force_cap.attr,
1229         &dev_attr_dump.attr,
1230         &dev_attr_report_mode.attr,
1231         &dev_attr_version.attr,
1232         &dev_attr_slowscan_enable.attr,
1233         &dev_attr_wakeup_enable.attr,
1234 #if defined(DEBUG_LR388K7)
1235         &dev_attr_check_state.attr,
1236 #endif
1237         NULL
1238 };
1239
1240 static const struct attribute_group lr388k7_ts_attr_group = {
1241         .attrs = lr388k7_ts_attributes,
1242 };
1243
1244 static int lr388k7_open(struct input_dev *input)
1245 {
1246         struct lr388k7 *ts = input_get_drvdata(input);
1247
1248         dev_info(&ts->spi->dev, "[ENTER] open\n");
1249
1250         mutex_lock(&ts->mutex);
1251
1252         __lr388k7_enable(ts);
1253
1254         ts->opened = true;
1255
1256         mutex_unlock(&ts->mutex);
1257
1258         dev_info(&ts->spi->dev, "[EXIT] open\n");
1259         return 0;
1260 }
1261
1262 static void lr388k7_close(struct input_dev *input)
1263 {
1264         struct lr388k7 *ts = input_get_drvdata(input);
1265         mutex_lock(&ts->mutex);
1266
1267         __lr388k7_disable(ts);
1268
1269         ts->opened = false;
1270
1271         mutex_unlock(&ts->mutex);
1272 }
1273
1274
1275 #if defined(UDEV_ENABLE)
1276 static void lr388k7_send_udev_event(void *p)
1277 {
1278         struct lr388k7_udev_event *p_udev_event;
1279         char *envp[2];
1280         char *event;
1281
1282         if (!p)
1283                 return;
1284
1285         p_udev_event = kmalloc(
1286                                sizeof(struct lr388k7_udev_event),
1287                                GFP_KERNEL);
1288
1289         if (!p_udev_event)
1290                 return;
1291
1292         if (copy_from_user(p_udev_event,
1293                           p,
1294                           sizeof(struct lr388k7_udev_event))) {
1295                 kfree(p_udev_event);
1296                 return;
1297         }
1298
1299         event = kasprintf(GFP_KERNEL, "TRIGGER=%s", p_udev_event->str);
1300         if (event) {
1301                 envp[0] = event;
1302                 envp[1] = NULL;
1303                 kobject_uevent_env(&lr388k7_ts_miscdev.this_device->kobj,
1304                                    KOBJ_CHANGE,
1305                                    envp);
1306                 kfree(event);
1307         }
1308         kfree(p_udev_event);
1309 }
1310 #endif
1311
1312 #if defined(ACTIVE_ENABLE)
1313 static void lr388k7_active_report(void *p)
1314 {
1315         struct lr388k7_active_report *p_active_report;
1316         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1317         struct input_dev *input_dev = ts->idev_active;
1318         int x, y, z;
1319         int tool = 0;
1320         u8 status;
1321
1322         if (!p)
1323                 return;
1324
1325         /*
1326          * Memory Allocation
1327          */
1328         p_active_report = kmalloc(
1329                                  sizeof(struct lr388k7_active_report),
1330                                  GFP_KERNEL);
1331         if (!p_active_report)
1332                 return;
1333
1334         if (copy_from_user(p_active_report,
1335                            p,
1336                            sizeof(struct lr388k7_active_report))) {
1337                 kfree(p_active_report);
1338                 return;
1339         }
1340
1341         status = p_active_report->status;
1342
1343         /* Get current tool */
1344         if (status & 0x0C)
1345                 tool = BTN_TOOL_RUBBER;
1346         else
1347                 tool = BTN_TOOL_PEN;
1348
1349         if (!status)
1350                 tool = 0; /* Reset */
1351
1352         /* Check if tool changed then notify input subsystem */
1353         if (ts->tool != tool) {
1354                 if (ts->tool) {
1355                         /* Reset old tool state */
1356                         input_report_key(input_dev, BTN_TOUCH, 0);
1357                         input_report_key(input_dev, BTN_STYLUS, 0);
1358                         input_report_abs(input_dev, ABS_PRESSURE, 0);
1359                 }
1360                 input_report_key(input_dev, ts->tool, 0);
1361                 input_sync(input_dev);
1362
1363                 /* Update current*/
1364                 ts->tool = tool;
1365                 if (tool)
1366                         input_report_key(input_dev, tool, 1);
1367         }
1368
1369         if (tool) {
1370                 x = p_active_report->x;
1371                 y = p_active_report->y;
1372                 z = p_active_report->z;
1373
1374                 input_report_abs(input_dev, ABS_X, x);
1375                 input_report_abs(input_dev, ABS_Y, y);
1376                 input_report_abs(input_dev, ABS_PRESSURE, z);
1377                 input_report_key(input_dev, BTN_TOUCH, status & 0x09);
1378                 input_report_key(input_dev, BTN_STYLUS, status & 0x10);
1379                 input_sync(input_dev);
1380         }
1381
1382         kfree(p_active_report);
1383 }
1384 #endif
1385
1386 #if defined(PROTOCOL_A)
1387 static void lr388k7_touch_report(void *p)
1388 {
1389         u8 u8_num, i, u8_num_of_touch;
1390         struct lr388k7_touch_report *p_touch_report;
1391         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1392         struct input_dev *input_dev = ts->idev;
1393         bool b_is_eraser = false;
1394
1395         if (!p)
1396                 return;
1397
1398         /*
1399          * Memory Allocation
1400          */
1401         p_touch_report = kmalloc(
1402                                  sizeof(struct lr388k7_touch_report),
1403                                  GFP_KERNEL);
1404         if (!p_touch_report)
1405                 return;
1406
1407         if (copy_from_user(p_touch_report,
1408                            p,
1409                            sizeof(struct lr388k7_touch_report))) {
1410                 kfree(p_touch_report);
1411                 return;
1412         }
1413
1414         u8_num = p_touch_report->u8_num_of_touch;
1415
1416         if (u8_num == 0) {
1417                 if (ts->b_eraser_active) {
1418                         ts->b_eraser_active = false;
1419                         input_report_key(input_dev, BTN_TOOL_RUBBER, 0);
1420                 }
1421                 input_mt_sync(input_dev);
1422                 input_sync(input_dev);
1423                 kfree(p_touch_report);
1424                 return;
1425         }
1426         u8_num_of_touch = 0;
1427
1428         for (i = 0; i < u8_num; i++) {
1429
1430 #if defined(DEBUG_LR388K7_REPORT)
1431                 dev_info(&g_spi->dev, "ID=%2d, status=%02d, x=%5d, y=%5d, w=%d, h=%d, z=%5d, num=%2d\n",
1432                          p_touch_report->tc[i] . id,
1433                          p_touch_report->tc[i] . status,
1434                          p_touch_report->tc[i] . x,
1435                          p_touch_report->tc[i] . y,
1436                          p_touch_report->tc[i] . width,
1437                          p_touch_report->tc[i] . height,
1438                          p_touch_report->tc[i] . z,
1439                          u8_num
1440                          );
1441 #endif
1442
1443                 if ((p_touch_report->tc[i] . status & 0x80) != 0)
1444                         b_is_eraser = true;
1445                 else
1446                         b_is_eraser = false;
1447
1448                 if (b_is_eraser) {
1449                         input_report_key(input_dev, BTN_TOOL_RUBBER, 1);
1450                         ts->b_eraser_active = true;
1451                 } else if ((p_touch_report->tc[i] . status & 0x7F) <= 3 ||
1452                           (p_touch_report->tc[i] . status & 0x7F) == 8)
1453                         input_report_abs(input_dev,
1454                                          ABS_MT_TOOL_TYPE, MT_TOOL_FINGER);
1455                 else if ((p_touch_report->tc[i] . status & 0x7F) <= 7 ||
1456                          (p_touch_report->tc[i] . status & 0x7F) == 12)
1457                         input_report_abs(input_dev,
1458                                          ABS_MT_TOOL_TYPE, MT_TOOL_PEN);
1459                 input_report_abs(input_dev,
1460                                  ABS_MT_TRACKING_ID,
1461                                  p_touch_report->tc[i] .id);
1462                 input_report_abs(input_dev,
1463                                  ABS_MT_POSITION_X,
1464                                  p_touch_report->tc[i] . x);
1465                 input_report_abs(input_dev,
1466                                  ABS_MT_POSITION_Y,
1467                                  p_touch_report->tc[i] . y);
1468                 input_report_abs(input_dev,
1469                                  ABS_MT_PRESSURE,
1470                                  p_touch_report->tc[i] . z);
1471 #if 0
1472                 input_report_abs(input_dev,
1473                                  ABS_MT_TOUCH_MAJOR,
1474                                  p_touch_report->tc[i] .width);
1475                 input_report_abs(input_dev,
1476                                  ABS_MT_TOUCH_MINOR,
1477                                  p_touch_report->tc[i] .height);
1478 #endif
1479                 input_mt_sync(input_dev);
1480                 u8_num_of_touch++;
1481         }
1482         input_sync(input_dev);
1483
1484         kfree(p_touch_report);
1485 }
1486 #else /* PROTOCOL_B */
1487 static void lr388k7_touch_report(void *p)
1488 {
1489         u8 u8_num, i;
1490         struct lr388k7_touch_report *p_touch_report;
1491         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1492         struct input_dev *input_dev = ts->idev;
1493
1494         if (!p)
1495                 return;
1496
1497         /*
1498          * Memory Allocation
1499          */
1500         p_touch_report = kmalloc(
1501                                  sizeof(struct lr388k7_touch_report),
1502                                  GFP_KERNEL);
1503         if (!p_touch_report)
1504                 return;
1505
1506         if (copy_from_user(p_touch_report,
1507                            p,
1508                            sizeof(struct lr388k7_touch_report))) {
1509                 kfree(p_touch_report);
1510                 return;
1511         }
1512
1513         u8_num = p_touch_report->u8_num_of_touch;
1514
1515         for (i = 0; i < u8_num; i++) {
1516
1517 #if defined(DEBUG_LR388K7_REPORT)
1518                 dev_info(&g_spi->dev, "ID=%2d, status=%02d, x=%5d, y=%5d, w=%d, h=%d, z=%5d, num=%2d\n",
1519                          p_touch_report->tc[i] . id,
1520                          p_touch_report->tc[i] . status,
1521                          p_touch_report->tc[i] . x,
1522                          p_touch_report->tc[i] . y,
1523                          p_touch_report->tc[i] . width,
1524                          p_touch_report->tc[i] . height,
1525                          p_touch_report->tc[i] . z,
1526                          u8_num
1527                          );
1528 #endif
1529                 input_mt_slot(input_dev,
1530                               p_touch_report->tc[i] . id);
1531                 if (((p_touch_report->tc[i] . status & 0x7F) == 8) ||
1532                     ((p_touch_report->tc[i] . status & 0x7F) == 12) ||
1533                     ((p_touch_report->tc[i] . status & 0x7F) == 14)) {
1534                         input_mt_report_slot_state(input_dev,
1535                                                    MT_TOOL_FINGER,
1536                                                    false);
1537                         continue;
1538                 }
1539
1540                 if ((p_touch_report->tc[i] . status & 0x7F) <= 3)
1541                         input_report_abs(input_dev,
1542                                          ABS_MT_TOOL_TYPE, MT_TOOL_FINGER);
1543                 else if (((p_touch_report->tc[i] . status & 0x7F) <= 7) ||
1544                          ((p_touch_report->tc[i] . status & 0x7F) > 12))
1545                         input_report_abs(input_dev,
1546                                          ABS_MT_TOOL_TYPE, MT_TOOL_PEN);
1547                 input_report_abs(input_dev,
1548                                  ABS_MT_TRACKING_ID,
1549                                  p_touch_report->tc[i] .id);
1550                 input_report_abs(input_dev,
1551                                  ABS_MT_POSITION_X,
1552                                  p_touch_report->tc[i].x);
1553                 input_report_abs(input_dev,
1554                                  ABS_MT_POSITION_Y,
1555                                  p_touch_report->tc[i].y);
1556                 input_report_abs(input_dev,
1557                                  ABS_MT_PRESSURE,
1558                                  p_touch_report->tc[i] . z);
1559         }
1560         input_sync(input_dev);
1561 }
1562 #endif /* #if defined(PROTOCOL_A) */
1563
1564 static int dev_open(struct inode *inode, struct file *filp)
1565 {
1566 #if defined(DEBUG_LR388K7)
1567         dev_info(&g_spi->dev, "dev_open\n");
1568 #endif
1569
1570         return 0;
1571 }
1572
1573 static int dev_release(struct inode *inode, struct file *filp)
1574 {
1575         int ret;
1576 #if defined(DEBUG_LR388K7)
1577         dev_info(&g_spi->dev, "dev_release\n");
1578 #endif
1579         g_st_state.b_is_init_finish = 0;
1580
1581         ret = lr388k7_hardreset(1);
1582
1583         return 0;
1584 }
1585
1586 /**
1587  * @brief lr388k7 spi interface
1588  * @param[in] txbuf command to read data
1589  * @param[in] rxbuf data to be read
1590  * @param[in] len length of data
1591  * @retval TRUE if success, otherwise FALSE
1592  */
1593 static int lr388k7_spi_read(u8 *txbuf, u8 *rxbuf, size_t len)
1594 {
1595         static DEFINE_MUTEX(lock);
1596
1597         int status;
1598         struct spi_message msg;
1599         struct spi_transfer t = {
1600                 .bits_per_word  = 8,
1601                 .tx_buf = txbuf,
1602                 .rx_buf = rxbuf,
1603                 .speed_hz =
1604                 g_st_state.u32SCK == 0 ? 12000000 : g_spi->max_speed_hz,
1605                 /*    .speed_hz           = 18000000, */
1606         };
1607
1608         mutex_lock(&lock);
1609
1610         t.len = len + K7_RD_HEADER_SIZE;
1611
1612         spi_message_init(&msg);
1613         spi_message_add_tail(&t, &msg);
1614         status = spi_sync(g_spi, &msg);
1615
1616         mutex_unlock(&lock);
1617
1618         if (status)
1619                 return false;
1620
1621         return true;
1622 }
1623
1624 static long lr388k7_spi_read_cmd_data(u8 *p, size_t count)
1625 {
1626         u8 *p_tx_buf;
1627         ssize_t status = 0;
1628
1629         /*
1630          * Memory Allocation
1631          */
1632         p_tx_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1633         if (p_tx_buf == NULL) {
1634                 status = -ENOMEM;
1635                 goto exit_dev_read_cmd_data;
1636         }
1637
1638         /*
1639          * Clear and Set command to read data
1640          */
1641         memset(p_tx_buf, 0, count + K7_RD_HEADER_SIZE);
1642
1643         p_tx_buf[0] = K7_RD_OPCODE;
1644         p_tx_buf[1] = (K7_CMD_DATA_OFFSET >> 16) & 0xFF;
1645         p_tx_buf[2] = (K7_CMD_DATA_OFFSET >>  8) & 0xFF;
1646         p_tx_buf[3] = (K7_CMD_DATA_OFFSET >>  0) & 0xFF;
1647         p_tx_buf[4] = 0x00;
1648
1649         /*
1650          * Read data
1651          */
1652         if (lr388k7_spi_read(p_tx_buf, p, count))
1653                 status = count;
1654         else
1655                 status = -EFAULT;
1656
1657         kfree(p_tx_buf);
1658
1659  exit_dev_read_cmd_data:
1660         return status;
1661 }
1662
1663 static long lr388k7_spi_read_wo_limit(u8 *p, size_t count)
1664 {
1665         u8 *p_buf;
1666         u8 *p_tx_buf;
1667         ssize_t status = 0;
1668
1669         /*
1670          * Memory Allocation
1671          */
1672         p_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1673         if (p_buf == NULL) {
1674                 status = -ENOMEM;
1675                 goto ext_dev_read_wo_limit;
1676         }
1677
1678         p_tx_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1679         if (p_tx_buf == NULL) {
1680                 status = -ENOMEM;
1681                 goto ext_dev_read_wo_limit_free;
1682         }
1683
1684         /*
1685          * Clear and Set command to read data
1686          */
1687         memset(p_tx_buf, 0, count + K7_RD_HEADER_SIZE);
1688
1689         if (copy_from_user(p_tx_buf, p, K7_RD_HEADER_SIZE)) {
1690                 status = -EFAULT;
1691                 goto ext_dev_read_wo_limit_free_tx;
1692         }
1693
1694         /*
1695          * Read data
1696          */
1697         if (lr388k7_spi_read(p_tx_buf, p_buf, count)) {
1698                 if (copy_to_user(p, &p_buf[K7_RD_HEADER_SIZE], count)) {
1699                         status = -EFAULT;
1700                         goto ext_dev_read_wo_limit_free_tx;
1701                 }
1702
1703                 status = count;
1704
1705         } else
1706                 status = -EFAULT;
1707
1708  ext_dev_read_wo_limit_free_tx:
1709         kfree(p_tx_buf);
1710  ext_dev_read_wo_limit_free:
1711         kfree(p_buf);
1712  ext_dev_read_wo_limit:
1713         return status;
1714 }
1715
1716 static long lr388k7_spi_read_from_ioctl(u8 *p, size_t count)
1717 {
1718         u8 *p_buf;
1719         u8 *p_tx_buf;
1720         ssize_t status = 0;
1721
1722         /*
1723          * Memory Allocation
1724          */
1725         p_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1726         if (p_buf == NULL) {
1727                 status = -ENOMEM;
1728                 goto ext_dev_read_from_ioctl;
1729         }
1730
1731         p_tx_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1732         if (p_tx_buf == NULL) {
1733                 status = -ENOMEM;
1734                 goto ext_dev_read_from_ioctl_free;
1735         }
1736
1737         /*
1738          * Clear and Set command to read data
1739          */
1740         memset(p_tx_buf, 0, count + K7_RD_HEADER_SIZE);
1741
1742         if (copy_from_user(p_tx_buf, p, K7_RD_HEADER_SIZE)) {
1743                 status = -EFAULT;
1744                 goto ext_dev_read_from_ioctl_free_tx;
1745         }
1746
1747         g_st_state.u32SCK = 0;
1748         /*
1749          * Read data
1750          */
1751         if (lr388k7_spi_read(p_tx_buf, p_buf, count)) {
1752                 if (copy_to_user(p, &p_buf[K7_RD_HEADER_SIZE], count)) {
1753                         status = -EFAULT;
1754                         goto ext_dev_read_from_ioctl_free_tx;
1755                 }
1756
1757                 status = count;
1758
1759         } else
1760                 status = -EFAULT;
1761
1762  ext_dev_read_from_ioctl_free_tx:
1763         kfree(p_tx_buf);
1764  ext_dev_read_from_ioctl_free:
1765         kfree(p_buf);
1766  ext_dev_read_from_ioctl:
1767         g_st_state.u32SCK = g_spi->max_speed_hz;
1768         return status;
1769 }
1770
1771 static ssize_t
1772 dev_read(struct file *filp, char __user *buf, size_t count, loff_t *pos)
1773 {
1774 #if 0
1775         u8 *p_buf;
1776         ssize_t status = 0;
1777
1778         p_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1779         if (!p_buf) {
1780                 status = -ENOMEM;
1781                 goto exit_dev_read;
1782         }
1783
1784         memset(p_buf, 0, count + K7_RD_HEADER_SIZE);
1785
1786         if (copy_from_user(p_buf, buf, K7_RD_HEADER_SIZE)) {
1787                 status = -EFAULT;
1788                 goto exit_dev_read_free;
1789         }
1790
1791         if (lr388k7_spi_read(p_buf, count)) {
1792                 if (copy_to_user(buf, &p_buf[K7_RD_HEADER_SIZE], count)) {
1793                         status = -EFAULT;
1794                         goto exit_dev_read_free;
1795                 }
1796
1797                 status = count;
1798
1799         } else
1800                 status = -EFAULT;
1801
1802  exit_dev_read_free:
1803         kfree(p_buf);
1804  exit_dev_read:
1805         return status;
1806 #else
1807         return 0;
1808 #endif
1809 }
1810
1811 /**
1812  * @brief lr388k7 spi interface
1813  * @param[in] txbuf data to be written
1814  * @param[in] len length of data
1815  * @retval TRUE if success, otherwise FALSE
1816  */
1817 static int lr388k7_spi_write(u8 *txbuf, size_t len)
1818 {
1819         static DEFINE_MUTEX(lock);
1820         int status;
1821
1822         mutex_lock(&lock);
1823
1824         /*It returns zero on succcess,else a negative error code. */
1825         status = spi_write(g_spi, txbuf, len);
1826
1827         mutex_unlock(&lock);
1828
1829         if (status)
1830                 return false;
1831
1832         return true;
1833 }
1834
1835 static u32 lr388k7_get_platform_id(u8 *p)
1836 {
1837         u32 ret;
1838         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1839
1840         ret = copy_to_user(p,
1841                            &ts->platform_id,
1842                            sizeof(ts->platform_id));
1843
1844         return ret;
1845 }
1846
1847 static u32 lr388k7_get_value(u8 *arg, unsigned int index)
1848 {
1849         u32 ret = 0;
1850         switch (index) {
1851         case LR388K7_GET_PLATFORM_ID:
1852                 ret = lr388k7_get_platform_id(arg);
1853                 break;
1854         default:
1855                 ret = -EINVAL;
1856                 break;
1857         }
1858         return ret;
1859 }
1860
1861
1862 static ssize_t
1863 dev_write(struct file *filp, const char __user *buf,
1864                 size_t count, loff_t *pos)
1865 {
1866         u8 *p_buf;
1867         ssize_t status = 0;
1868
1869         p_buf = kmalloc(count, GFP_KERNEL);
1870         if (!p_buf) {
1871                 status = -ENOMEM;
1872                 goto exit_dev_write;
1873         }
1874
1875         if (copy_from_user(p_buf, buf, count)) {
1876                 status = -EFAULT;
1877                 goto exit_dev_write_free;
1878         }
1879
1880         if (lr388k7_spi_write(p_buf, count))
1881                 status = count;
1882         else
1883                 status = -EFAULT;
1884
1885  exit_dev_write_free:
1886         kfree(p_buf);
1887  exit_dev_write:
1888         return status;
1889 }
1890
1891 static long dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1892 {
1893         long ret = true;
1894         unsigned int index;
1895
1896         index = (cmd >> 16) & 0xFFFF;
1897
1898         switch (cmd & 0xFFFF) {
1899         case LR388K7_IOCTL_TOUCH_REPORT:
1900                 lr388k7_touch_report((void *) arg);
1901                 break;
1902
1903         case LR388K7_IOCTL_READ_REGISTER:
1904                 ret = lr388k7_spi_read_from_ioctl((u8 *) arg, index);
1905                 break;
1906
1907         case LR388K7_IOCTL_READ_WO_LIMIT:
1908                 ret = lr388k7_spi_read_wo_limit((u8 *) arg, index);
1909                 break;
1910
1911         case LR388K7_IOCTL_SET_HAL_PID:
1912                 g_st_state.u32_pid = arg;
1913                 g_st_state.b_is_calc_finish = 1;
1914                 break;
1915
1916         case LR388K7_IOCTL_READ_RAW_DATA:
1917                 ret = lr388k7_queue_read_raw_data((u8 *) arg, index);
1918                 break;
1919
1920         case LR388K7_IOCTL_READ_CMD_DATA:
1921                 ret = lr388k7_cmd_queue_read((u8 *) arg, index);
1922                 break;
1923
1924         case LR388K7_IOCTL_FINISH_CALC:
1925                 g_st_state.b_is_calc_finish = 1;
1926                 break;
1927
1928         case LR388K7_IOCTL_SET_RAW_DATA_SIZE:
1929                 g_u32_raw_data_size = arg;
1930                 break;
1931
1932         case LR388K7_IOCTL_SET_REVISION:
1933                 g_st_state.u16fw_ver = (arg & 0xFFFF0000) >> 16;
1934                 g_st_state.u16module_ver = arg & 0x0000FFFF;
1935
1936                 dev_info(&g_spi->dev, "Version %d.%d.%d\n",
1937                          g_st_state.u16fw_ver,
1938                          K7_DRIVER_VERSION,
1939                          g_st_state.u16module_ver
1940                          );
1941                 break;
1942
1943         case LR388K7_IOCTL_GET_DEBUG_STATUS:
1944                 lr388k7_ts_get_debug_status((u8 *)arg);
1945                 break;
1946
1947         case LR388K7_IOCTL_SET_MODE:
1948                 g_u8_mode = (u8)arg;
1949                 break;
1950
1951         case LR388K7_IOCTL_GET_MODE:
1952                 lr388k7_ts_get_mode((u8 *)arg);
1953                 break;
1954
1955         case LR388K7_IOCTL_RESET:
1956                 ret = lr388k7_hardreset(arg);
1957                 break;
1958
1959         case LR388K7_IOCTL_CLEAR_BUFFER:
1960                 lr388k7_queue_reset();
1961                 g_st_state.b_is_init_finish = arg;
1962                 if (g_st_state.b_is_init_finish)
1963                         lr388k7_send_signal(g_st_state.u32_pid,
1964                                             LR388K7_SIGNAL_MODE);
1965                 break;
1966
1967         case LR388K7_IOCTL_ACTIVE_REPORT:
1968 #if defined(ACTIVE_ENABLE)
1969                 lr388k7_active_report((void *) arg);
1970 #endif
1971                 break;
1972
1973         case LR388K7_IOCTL_GET_VALUE:
1974                 ret = lr388k7_get_value((u8 *)arg, index);
1975                 break;
1976
1977         case LR388K7_IOCTL_SET_CLK_SEL:
1978 #if defined(ACTIVE_ENABLE)
1979                 lr388k7_set_clk_sel((unsigned int)arg);
1980 #endif
1981                 break;
1982
1983         case LR388K7_IOCTL_SET_STATE:
1984                 g_u8_scan = (u8)arg;
1985                 break;
1986
1987 #if defined(UDEV_ENABLE)
1988         case LR388K7_IOCTL_SEND_UDEV:
1989                 lr388k7_send_udev_event((void *)arg);
1990                 break;
1991 #endif
1992
1993         default:
1994                 ret = false;
1995                 break;
1996         }
1997
1998         return ret;
1999 }
2000
2001 static void lr388k7_init_parameter(void)
2002 {
2003         g_u8_mode = K7_DEFAULT_MODE;
2004         g_u8_scan = K7_SCAN_STATE_IDLE;
2005         g_st_state.u32_pid = 0;
2006         g_st_state.b_is_init_finish = false;
2007         g_st_state.b_is_suspended = false;
2008         g_st_state.b_is_reset = false;
2009         g_st_state.u32SCK = 0;
2010         lr388k7_queue_reset();
2011
2012         g_st_dbg.slowscan.enable = 0;
2013         g_st_dbg.slowscan.scan_rate = K7_SLOW_SCAN_60;
2014         g_st_dbg.wakeup.enable = 0;
2015         g_st_dbg.wakeup.num_tap = K7_NUM_TAP_2;
2016         g_st_dbg.u8ForceCap = 0;
2017         g_st_dbg.u8Dump = 0;
2018         g_u16_wakeup_enable = 0;
2019 }
2020
2021 static void lr388k7_init_ts(void)
2022 {
2023         lr388k7_init_parameter();
2024         memset(&g_st_state, 0, sizeof(struct lr388k7_ts_parameter));
2025
2026         g_st_state.st_wq_k7 = create_singlethread_workqueue("lr388k7_work");
2027         INIT_WORK(&g_st_state.st_work_k7, lr388k7_work_handler);
2028 }
2029
2030 static struct lr388k7_platform_data *lr388k7_parse_dt(struct device *dev,
2031                                                       int irq)
2032 {
2033         struct lr388k7_platform_data *pdata;
2034         struct device_node *np = dev->of_node;
2035         int ret, val, irq_gpio;
2036         const char *str;
2037
2038         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2039         if (!pdata)
2040                 return ERR_PTR(-ENOMEM);
2041
2042         pdata->gpio_reset = of_get_named_gpio_flags(np, "reset-gpio", 0, NULL);
2043         if (!gpio_is_valid(pdata->gpio_reset)) {
2044                 dev_err(dev, "Invalid reset-gpio\n");
2045                 return ERR_PTR(-EINVAL);
2046         }
2047         ret = gpio_request(pdata->gpio_reset, "reset-gpio");
2048         if (ret < 0) {
2049                 dev_err(dev, "gpio_request failed\n");
2050                 return ERR_PTR(-EINVAL);
2051         }
2052         gpio_direction_output(pdata->gpio_reset, 0);
2053
2054         ret = of_property_read_u32(np, "interrupts", &irq_gpio);
2055         if (!gpio_is_valid(irq_gpio)) {
2056                 dev_err(dev, "Invalid irq-gpio\n");
2057                 ret = -EINVAL;
2058                 goto exit_release_reset_gpio;
2059         }
2060         ret = gpio_request(irq_gpio, "irq-gpio");
2061         if (ret < 0) {
2062                 dev_err(dev, "irq_request fail\n");
2063                 ret = -EINVAL;
2064                 goto exit_release_reset_gpio;
2065         }
2066         gpio_direction_input(irq_gpio);
2067         pdata->gpio_irq = irq_gpio;
2068
2069         ret = of_property_read_u32(np, "x-max", &val);
2070         if (ret < 0)
2071                 goto exit_release_all_gpio;
2072         pdata->ts_x_max = val;
2073
2074         ret = of_property_read_u32(np, "y-max", &val);
2075         if (ret < 0)
2076                 goto exit_release_all_gpio;
2077         pdata->ts_y_max = val;
2078
2079         ret = of_property_read_u32(np, "z-max", &val);
2080         if (ret < 0)
2081                 goto exit_release_all_gpio;
2082         pdata->ts_pressure_max = val;
2083
2084         ret = of_property_read_u32(np, "touch-num-max", &val);
2085         if (ret < 0)
2086                 goto exit_release_all_gpio;
2087         pdata->ts_touch_num_max = val;
2088
2089         ret = of_property_read_string(np, "name-of-clock", &str);
2090         if (ret < 0)
2091                 goto exit_release_all_gpio;
2092         pdata->name_of_clock = (char *)str;
2093
2094         ret = of_property_read_string(np, "name-of-clock-con", &str);
2095         if (ret < 0)
2096                 goto exit_release_all_gpio;
2097         pdata->name_of_clock_con = (char *)str;
2098
2099 #if defined(ACTIVE_ENABLE)
2100         pdata->gpio_clk_sel = of_get_named_gpio_flags(np,
2101                                                       "clock-sel-gpio",
2102                                                       0,
2103                                                       NULL);
2104         if (!gpio_is_valid(pdata->gpio_clk_sel)) {
2105                 dev_err(dev, "Invalid clock-sel-gpio\n");
2106                 return ERR_PTR(-EINVAL);
2107         }
2108         ret = gpio_request(pdata->gpio_clk_sel, "clock-sel-gpio");
2109         if (ret < 0) {
2110                 dev_err(dev, "gpio_request failed\n");
2111                 return ERR_PTR(-EINVAL);
2112         }
2113         gpio_direction_output(pdata->gpio_clk_sel, 0);
2114 #endif
2115
2116         ret = of_property_read_u32(np, "platform-id", &val);
2117         if (ret < 0)
2118                 goto exit_release_all_gpio;
2119         pdata->platform_id = val;
2120
2121         return pdata;
2122
2123  exit_release_all_gpio:
2124         gpio_free(irq_gpio);
2125  exit_release_reset_gpio:
2126         gpio_free(pdata->gpio_reset);
2127         return ERR_PTR(ret);
2128 }
2129
2130 static int lr388k7_probe(struct spi_device *spi)
2131 {
2132         struct lr388k7_platform_data *pdata;/* = spi->dev.platform_data;*/
2133         struct lr388k7 *ts;
2134         struct input_dev *input_dev;
2135 #if defined(ACTIVE_ENABLE)
2136         struct input_dev *input_dev_active;
2137 #endif
2138         struct device *dev;
2139         int error;
2140
2141         dev = &spi->dev;
2142         dev_info(dev, "[ENTER] probe\n");
2143
2144         lr388k7_init_ts();
2145
2146         g_spi = spi;
2147
2148         if (spi->irq <= 0) {
2149                 dev_dbg(dev, "no irq\n");
2150                 return -ENODEV;
2151         }
2152
2153         spi->mode = SPI_MODE_3;
2154         spi->bits_per_word = 8;
2155         if (!spi->max_speed_hz)
2156                 spi->max_speed_hz = K7_MAX_SPEED_HZ;
2157
2158         error = spi_setup(spi);
2159         if (error)
2160                 return error;
2161
2162         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
2163         input_dev = input_allocate_device();
2164 #if defined(ACTIVE_ENABLE)
2165         input_dev_active = input_allocate_device();
2166         if (!ts || !input_dev || !input_dev_active) {
2167 #else
2168         if (!ts || !input_dev) {
2169 #endif
2170                 error = -ENOMEM;
2171                 goto err_free_mem;
2172         }
2173
2174
2175         ts->spi = spi;
2176         ts->dev = dev;
2177         ts->idev = input_dev;
2178 #if defined(ACTIVE_ENABLE)
2179         ts->idev_active = input_dev_active;
2180 #endif
2181         ts->irq = spi->irq;
2182
2183         if (dev->of_node) {
2184
2185                 dev_info(dev, "DT support\n");
2186                 /* Loading data from DT */
2187                 pdata = lr388k7_parse_dt(dev, spi->irq);
2188                 if (IS_ERR(pdata)) {
2189                         dev_err(dev, "failed to parse device tree\n");
2190                         error = -EINVAL;
2191                         goto err_free_mem;
2192                 }
2193                 dev->platform_data = pdata;
2194         } else {
2195                 /* Non-DT */
2196                 dev_info(dev, "Non-DT\n");
2197         }
2198
2199         pdata = dev->platform_data;
2200
2201         ts->max_num_touch = __min(pdata->ts_touch_num_max, K7_MAX_TOUCH_NUM);
2202         ts->max_x       = pdata->ts_x_max ? : MAX_16BIT;
2203         ts->max_y       = pdata->ts_y_max ? : MAX_16BIT;
2204         ts->max_z       = pdata->ts_pressure_max ? : MAX_16BIT;
2205         ts->swap_xy     = false;
2206         ts->flip_x      = false;
2207         ts->flip_y      = false;
2208         ts->gpio_reset  = pdata->gpio_reset;
2209         ts->gpio_irq  = pdata->gpio_irq;
2210         ts->b_eraser_active = false;
2211 #if defined(ACTIVE_ENABLE)
2212         ts->tool        = 0;
2213         ts->gpio_clk_sel = pdata->gpio_clk_sel;
2214 #endif
2215         ts->platform_id = pdata->platform_id;
2216
2217         mutex_init(&ts->mutex);
2218
2219         spin_lock_init(&ts->lock);
2220
2221         snprintf(ts->phys,
2222                  sizeof(ts->phys),
2223                  "%s/input-ts",
2224                  dev_name(dev));
2225
2226         /* misc */
2227         if (misc_register(&lr388k7_ts_miscdev) != 0) {
2228                 dev_err(dev, "cannot register miscdev\n");
2229                 error = -ENOMEM;
2230                 goto err_free_mem;
2231         }
2232
2233 #if defined(DEBUG_LR388K7)
2234         dev_info(&spi->dev, "Success register miscdev\n");
2235 #endif
2236
2237         if (lr388k7_queue_init()) {
2238                 dev_err(dev, "Cannot allocate queue memory\n");
2239                 error = -ENOMEM;
2240                 goto err_free_mem;
2241         }
2242
2243 #if defined(ACTIVE_ENABLE)
2244         /* Select external clock */
2245         gpio_set_value(ts->gpio_clk_sel, 0);
2246 #endif
2247         /* Reset assert */
2248         gpio_set_value(ts->gpio_reset, 0);
2249         g_st_state.b_is_reset = true;
2250
2251         /* regulator */
2252         ts->regulator_3v3 = devm_regulator_get(&g_spi->dev, "avdd");
2253         if (IS_ERR(ts->regulator_3v3)) {
2254                 dev_err(dev,
2255                         "LR388K7 TS: regulator_get failed: %ld\n",
2256                         PTR_ERR(ts->regulator_3v3));
2257                 return -ENODEV;
2258         }
2259
2260         ts->regulator_1v8 = devm_regulator_get(&g_spi->dev, "dvdd");
2261         if (IS_ERR(ts->regulator_1v8)) {
2262                 dev_err(dev,
2263                         "LR388K7 TS: regulator_get failed: %ld\n",
2264                         PTR_ERR(ts->regulator_1v8));
2265                 return -ENODEV;
2266         }
2267
2268         /* Enable 1v8 first*/
2269         error = regulator_enable(ts->regulator_1v8);
2270         if (error < 0)
2271                 dev_err(dev,
2272                         "LR388K7 TS: regulator enable failed: %d\n",
2273                         error);
2274         usleep_range(5000, 6000);
2275         error = regulator_enable(ts->regulator_3v3);
2276         if (error < 0)
2277                 dev_err(dev,
2278                         "LR388K7 TS: regulator enable failed: %d\n",
2279                         error);
2280
2281         /* clk */
2282         if (pdata->name_of_clock || pdata->name_of_clock_con) {
2283                 ts->clk =
2284                         clk_get_sys(pdata->name_of_clock,
2285                                     pdata->name_of_clock_con);
2286                 if (IS_ERR(ts->clk)) {
2287                         dev_err(dev,
2288                                 "failed to get touch_clk:(%s, %s)\n",
2289                                 pdata->name_of_clock,
2290                                 pdata->name_of_clock_con);
2291                         error = -EINVAL;
2292                         goto err_free_mem;
2293                 }
2294         }
2295
2296         if (ts->clk)
2297                 clk_enable(ts->clk);
2298
2299         input_dev->name = "touch";
2300         input_dev->phys = ts->phys;
2301         input_dev->id.bustype = BUS_SPI;
2302         input_dev->dev.parent = &spi->dev;
2303         __set_bit(EV_SYN, input_dev->evbit);
2304         __set_bit(EV_ABS, input_dev->evbit);
2305         __set_bit(EV_KEY, input_dev->evbit);
2306         __set_bit(KEY_POWER, input_dev->keybit);
2307
2308         if (ts->swap_xy) {
2309                 input_set_abs_params(input_dev,
2310                                      ABS_MT_POSITION_X,
2311                                      0,
2312                                      ts->max_y,
2313                                      0,
2314                                      0);
2315                 input_set_abs_params(input_dev,
2316                                      ABS_MT_POSITION_Y,
2317                                      0,
2318                                      ts->max_x,
2319                                      0,
2320                                      0);
2321         } else {
2322                 input_set_abs_params(input_dev,
2323                                      ABS_MT_POSITION_X,
2324                                      0,
2325                                      ts->max_x,
2326                                      0,
2327                                      0);
2328                 input_set_abs_params(input_dev,
2329                                      ABS_MT_POSITION_Y,
2330                                      0, ts->max_y,
2331                                      0,
2332                                      0);
2333         }
2334         input_set_abs_params(input_dev,
2335                              ABS_MT_PRESSURE,
2336                              0,
2337                              ts->max_z,
2338                              0,
2339                              0);
2340         input_set_abs_params(input_dev,
2341                              ABS_MT_TOOL_TYPE,
2342                              0,
2343                              MT_TOOL_MAX,
2344                              0,
2345                              0);
2346 #if defined(PROTOCOL_A)
2347         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
2348
2349         input_set_abs_params(input_dev,
2350                              ABS_MT_TRACKING_ID,
2351                              0,
2352                              ts->max_num_touch,
2353                              0,
2354                              0);
2355 #else /* PROTOCOL B */
2356         error = input_mt_init_slots(input_dev,
2357                                     ts->max_num_touch,
2358                                     0);
2359         if (error) {
2360                 dev_err(dev, "Failed to mt_init_slots err: %d\n", error);
2361                 goto err_free_mem;
2362         }
2363
2364 #endif
2365
2366         input_set_capability(input_dev, EV_MSC, MSC_ACTIVITY);
2367
2368         input_dev->open = lr388k7_open;
2369         input_dev->close = lr388k7_close;
2370         input_dev->enable = lr388k7_input_enable;
2371         input_dev->disable = lr388k7_input_disable;
2372         input_dev->enabled = true;
2373         input_set_drvdata(input_dev, ts);
2374
2375 #if defined(ACTIVE_ENABLE)
2376         input_dev_active->name = "touch_active";
2377         input_dev_active->phys = ts->phys;
2378         input_dev_active->id.bustype = BUS_SPI;
2379         input_dev_active->dev.parent = &spi->dev;
2380
2381         __set_bit(EV_ABS, input_dev_active->evbit);
2382         __set_bit(EV_KEY, input_dev_active->evbit);
2383         __set_bit(INPUT_PROP_POINTER, input_dev_active->propbit);
2384         __set_bit(BTN_TOUCH, input_dev_active->keybit);
2385         __set_bit(BTN_TOOL_PEN, input_dev_active->keybit);
2386         __set_bit(BTN_TOOL_RUBBER, input_dev_active->keybit);
2387         __set_bit(BTN_STYLUS, input_dev_active->keybit);
2388
2389         if (ts->swap_xy) {
2390                 input_set_abs_params(input_dev_active,
2391                                      ABS_X,
2392                                      0,
2393                                      ts->max_y,
2394                                      0,
2395                                      0);
2396                 input_set_abs_params(input_dev_active,
2397                                      ABS_Y,
2398                                      0,
2399                                      ts->max_x,
2400                                      0,
2401                                      0);
2402         } else {
2403                 input_set_abs_params(input_dev_active,
2404                                      ABS_X,
2405                                      0,
2406                                      ts->max_x,
2407                                      0,
2408                                      0);
2409                 input_set_abs_params(input_dev_active,
2410                                      ABS_Y,
2411                                      0, ts->max_y,
2412                                      0,
2413                                      0);
2414         }
2415         input_set_abs_params(input_dev_active,
2416                              ABS_PRESSURE,
2417                              0,
2418                              MAX_10BIT,
2419                              0,
2420                              0);
2421         input_set_drvdata(input_dev_active, ts);
2422 #endif
2423
2424 #if 0
2425         error = request_irq(spi->irq, lr388k7_irq_thread,
2426                             IRQF_TRIGGER_FALLING,
2427                             "lr388k7_ts", ts);
2428 #else
2429         error = request_threaded_irq(spi->irq,
2430                                      lr388k7_irq_thread,
2431                                      lr388k7_wakeup_thread,
2432                                      IRQF_TRIGGER_FALLING,
2433                                      "lr388k7_ts", ts);
2434
2435 #endif
2436
2437         if (error) {
2438                 dev_err(dev, "Failed to request irq, err: %d\n", error);
2439                 goto err_free_mem;
2440         }
2441
2442         lr388k7_disable_irq(ts);
2443
2444         spi_set_drvdata(spi, ts);
2445
2446         error = sysfs_create_link(&dev->kobj,
2447                                   &lr388k7_ts_miscdev.this_device->kobj,
2448                                   "touch");
2449         if (error) {
2450                 dev_err(dev, "failed to create sysfs group\n");
2451                 goto err_clear_drvdata;
2452         }
2453
2454         error = input_register_device(ts->idev);
2455         if (error) {
2456                 dev_err(dev,
2457                         "Failed to register input device, err: %d\n", error);
2458                 goto err_clear_drvdata;
2459         }
2460
2461 #if defined(ACTIVE_ENABLE)
2462         error = input_register_device(ts->idev_active);
2463         if (error) {
2464                 dev_err(dev,
2465                         "Failed to register input active device , err: %d\n",
2466                         error);
2467                 goto err_clear_drvdata;
2468         }
2469 #endif
2470
2471         error = sysfs_create_group(&lr388k7_ts_miscdev.this_device->kobj,
2472                                    &lr388k7_ts_attr_group);
2473         if (error) {
2474                 dev_err(dev, "failed to create sysfs group\n");
2475                 goto err_clear_drvdata;
2476         }
2477
2478         /* Enable async suspend/resume to reduce LP0 latency */
2479         device_enable_async_suspend(dev);
2480
2481         dev_info(dev, "[EXIT] probe\n");
2482         return 0;
2483
2484  err_clear_drvdata:
2485         spi_set_drvdata(spi, NULL);
2486         free_irq(spi->irq, ts);
2487  err_free_mem:
2488 #if defined(ACTIVE_ENABLE)
2489         input_free_device(input_dev_active);
2490 #endif
2491         input_free_device(input_dev);
2492         kfree(ts);
2493         return error;
2494 }
2495
2496 static int lr388k7_remove(struct spi_device *spi)
2497 {
2498         struct lr388k7 *ts = spi_get_drvdata(spi);
2499
2500         lr388k7_queue_free();
2501
2502         if (g_st_state.st_wq_k7)
2503                 destroy_workqueue(g_st_state.st_wq_k7);
2504
2505         sysfs_remove_group(&lr388k7_ts_miscdev.this_device->kobj,
2506                            &lr388k7_ts_attr_group);
2507
2508         sysfs_remove_link(&ts->dev->kobj, "touch");
2509
2510         misc_deregister(&lr388k7_ts_miscdev);
2511         free_irq(ts->spi->irq, ts);
2512
2513         gpio_free(ts->gpio_reset);
2514         gpio_free(ts->gpio_irq);
2515 #if defined(ACTIVE_ENABLE)
2516         gpio_free(ts->gpio_clk_sel);
2517 #endif
2518         input_unregister_device(ts->idev);
2519
2520         kfree(ts);
2521
2522         spi_set_drvdata(spi, NULL);
2523         return 0;
2524 }
2525
2526 static void lr388k7_shutdown(struct spi_device *spi)
2527 {
2528         struct lr388k7 *ts = spi_get_drvdata(spi);
2529
2530         free_irq(ts->irq, ts);
2531
2532         if (ts->regulator_3v3)
2533                 regulator_disable(ts->regulator_3v3);
2534
2535         if (ts->regulator_1v8)
2536                 regulator_disable(ts->regulator_1v8);
2537 }
2538
2539 static void lr388k7_ctrl_resume(struct lr388k7 *ts)
2540 {
2541         u8 u8_buf[5];
2542         size_t count = 0;
2543
2544         u8_buf[count++] = K7_WR_OPCODE;
2545         u8_buf[count++] = (K7_STATE_CTL_ADDR >> 16) & 0xFF;
2546         u8_buf[count++] = (K7_STATE_CTL_ADDR >>  8) & 0xFF;
2547         u8_buf[count++] = (K7_STATE_CTL_ADDR >>  0) & 0xFF;
2548         u8_buf[count++] = K7_POWER_CTL_RESUME;
2549         lr388k7_spi_write(u8_buf, count);
2550 }
2551
2552 static void lr388k7_start(struct lr388k7 *ts)
2553 {
2554         int error;
2555
2556         g_st_state.b_is_suspended = false;
2557
2558         mutex_lock(&ts->mutex);
2559
2560         if (g_st_dbg.wakeup.enable == 1 || g_st_dbg.wakeup.enable == 2) {
2561                 lr388k7_ctrl_resume(ts);
2562                 mutex_unlock(&ts->mutex);
2563                 return;
2564         }
2565
2566         /* Reset assert */
2567         gpio_set_value(ts->gpio_reset, 0);
2568         g_st_state.b_is_reset = true;
2569
2570         /*
2571          * Enable regulator, if necessary
2572          */
2573         /* Enable 1v8 first*/
2574         error = regulator_enable(ts->regulator_1v8);
2575         if (error < 0)
2576                 dev_err(&g_spi->dev,
2577                         "LR388K7 TS: regulator enable failed: %d\n", error);
2578
2579         error = regulator_enable(ts->regulator_3v3);
2580         if (error < 0)
2581                 dev_err(&g_spi->dev,
2582                         "LR388K7 TS: regulator enable failed: %d\n", error);
2583
2584         usleep_range(5000, 6000);
2585
2586         /*
2587          * Enable clock, if necessary
2588          */
2589         if (ts->clk)
2590                 clk_enable(ts->clk);
2591
2592         /* Reset deassert */
2593         gpio_set_value(ts->gpio_reset, 1);
2594         g_st_state.b_is_reset = false;
2595
2596         usleep_range(12000, 13000);
2597
2598         mutex_unlock(&ts->mutex);
2599 }
2600
2601 static void lr388k7_ctrl_suspend(struct lr388k7 *ts)
2602 {
2603         u8 u8_buf[5];
2604         size_t count = 0;
2605         int error;
2606         u8 u8_status = 0;
2607         int irq_value;
2608
2609         mutex_lock(&ts->mutex);
2610
2611         if (g_st_dbg.wakeup.enable == 1) {
2612                 u8_buf[count++] = K7_WR_OPCODE;
2613                 u8_buf[count++] = (K7_STATE_CTL_ADDR >> 16) & 0xFF;
2614                 u8_buf[count++] = (K7_STATE_CTL_ADDR >>  8) & 0xFF;
2615                 u8_buf[count++] = (K7_STATE_CTL_ADDR >>  0) & 0xFF;
2616                 u8_buf[count++] = K7_POWER_CTL_TAP_WAKEUP;
2617                 lr388k7_spi_write(u8_buf, count);
2618
2619                 irq_value = gpio_get_value(ts->gpio_irq);
2620
2621                 if (!irq_value) {
2622                         u8_status = lr388k7_clear_irq();
2623
2624                         lr388k7_event_handler(u8_status);
2625                 }
2626
2627                 mutex_unlock(&ts->mutex);
2628                 return;
2629         } else if (g_st_dbg.wakeup.enable == 2) {
2630                 u8_buf[count++] = K7_WR_OPCODE;
2631                 u8_buf[count++] = (K7_STATE_CTL_ADDR >> 16) & 0xFF;
2632                 u8_buf[count++] = (K7_STATE_CTL_ADDR >>  8) & 0xFF;
2633                 u8_buf[count++] = (K7_STATE_CTL_ADDR >>  0) & 0xFF;
2634                 u8_buf[count++] = K7_POWER_CTL_SLEEP;
2635                 lr388k7_spi_write(u8_buf, count);
2636
2637                 irq_value = gpio_get_value(ts->gpio_irq);
2638
2639                 if (!irq_value) {
2640                         u8_status = lr388k7_clear_irq();
2641
2642                         lr388k7_event_handler(u8_status);
2643                 }
2644
2645                 mutex_unlock(&ts->mutex);
2646                 return;
2647         }
2648
2649
2650         /* Disable (3.3V) */
2651         if (ts->regulator_3v3) {
2652                 error = regulator_disable(ts->regulator_3v3);
2653                 if (error < 0)
2654                         dev_err(&g_spi->dev,
2655                                 "lr388k7 regulator 3.3V disable failed: %d\n",
2656                                 error);
2657         }
2658         /* handle platforms w/ and w/out regulator switches */
2659         /* 2) delay for platforms w/ regulator switches */
2660         /* usleep_range(15000, 20000);  */ /*msleep(15); */
2661         /* 3) disable clock */
2662         if (ts->clk)
2663                 clk_disable(ts->clk);
2664         /* 4) disable 1.8 */
2665         if (ts->regulator_1v8 && ts->regulator_3v3) {
2666                 error = regulator_disable(ts->regulator_1v8);
2667                 if (error < 0)
2668                         dev_err(&g_spi->dev,
2669                                 "lr388k7 1.8V disable failed: %d\n",
2670                                 error);
2671         }
2672
2673         mutex_unlock(&ts->mutex);
2674 }
2675
2676 static void lr388k7_stop(struct lr388k7 *ts)
2677 {
2678         flush_workqueue(g_st_state.st_wq_k7);
2679
2680         lr388k7_ctrl_suspend(ts);
2681 }
2682
2683 #ifdef CONFIG_PM
2684 static int lr388k7_suspend(struct device *dev)
2685 {
2686         struct lr388k7 *ts = dev_get_drvdata(dev);
2687
2688         if (g_st_state.b_is_suspended)
2689                 return 0;
2690
2691         lr388k7_disable_irq(ts);
2692
2693         lr388k7_stop(ts);
2694
2695         lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_SLEP);
2696
2697         g_st_state.b_is_suspended = true;
2698
2699         return 0;
2700 }
2701
2702 static int lr388k7_resume(struct device *dev)
2703 {
2704         struct lr388k7 *ts = dev_get_drvdata(dev);
2705         u8 u8_status = 0;
2706         int irq_value;
2707         lr388k7_start(ts);
2708
2709         lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_WAKE);
2710
2711         lr388k7_enable_irq(ts);
2712
2713         if (g_st_dbg.wakeup.enable == 2)
2714                 g_st_dbg.wakeup.enable = g_u16_wakeup_enable;
2715
2716         if (IS_DBG) {
2717                 g_st_dbg.u8ForceCap = 0;
2718                 g_st_dbg.u8Dump = 0;
2719                 g_st_dbg.slowscan.enable = 0;
2720
2721                 lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_CTRL);
2722         }
2723
2724         if (g_st_dbg.wakeup.enable == 0)
2725                 return 0;
2726
2727         /* check irq */
2728         irq_value = gpio_get_value(ts->gpio_irq);
2729         if (!irq_value) {
2730
2731                 u8_status = lr388k7_clear_irq();
2732
2733                 while (u8_status) {
2734
2735                         lr388k7_event_handler(u8_status);
2736                         irq_value = gpio_get_value(ts->gpio_irq);
2737
2738                         if (!irq_value)
2739                                 u8_status = lr388k7_clear_irq();
2740                         else
2741                                 u8_status = 0;
2742                 }
2743         }
2744
2745         return 0;
2746 }
2747 #endif /*CONFIG_PM */
2748
2749 static int lr388k7_input_enable(struct input_dev *idev)
2750 {
2751         struct lr388k7 *ts = input_get_drvdata(idev);
2752         int err = 0;
2753 #if defined(DEBUG_LR388K7)
2754         dev_info(ts->dev, "[ENTER] input enable\n");
2755 #endif
2756
2757 #ifdef CONFIG_PM
2758         err = lr388k7_resume(ts->dev);
2759 #endif
2760
2761 #if defined(DEBUG_LR388K7)
2762         dev_info(ts->dev, "[EXIT] input enable\n");
2763 #endif
2764         return 0;
2765 }
2766
2767 static int lr388k7_input_disable(struct input_dev *idev)
2768 {
2769         struct lr388k7 *ts = input_get_drvdata(idev);
2770         int err = 0;
2771 #if defined(DEBUG_LR388K7)
2772         dev_info(ts->dev, "[ENTER] input disable\n");
2773 #endif
2774
2775 #ifdef CONFIG_PM
2776         err = lr388k7_suspend(ts->dev);
2777 #endif
2778
2779 #if defined(DEBUG_LR388K7)
2780         dev_info(ts->dev, "[EXIT] input disable\n");
2781 #endif
2782         return 0;
2783 }
2784
2785 #if defined(CONFIG_PM) & !defined(INPUT_ENABLE)
2786 static const struct dev_pm_ops lr388k7_pm_ops = {
2787         .suspend = lr388k7_suspend,
2788         .resume = lr388k7_resume,
2789 };
2790 #endif
2791
2792 static const struct of_device_id lr388k7_ts_match[] = {
2793         { .compatible = "sharp,lr388k7_ts" },
2794         { },
2795 };
2796 MODULE_DEVICE_TABLE(of, lr388k7_ts_match);
2797
2798 static struct spi_driver lr388k7_driver = {
2799         .driver = {
2800                 .name   = "lr388k7_ts",
2801                 .owner  = THIS_MODULE,
2802 #if defined(CONFIG_PM) & !defined(INPUT_ENABLE)
2803                 .pm     = &lr388k7_pm_ops,
2804 #endif
2805                 .of_match_table = lr388k7_ts_match,
2806         },
2807         .probe  = lr388k7_probe,
2808         .remove = lr388k7_remove,
2809         .shutdown = lr388k7_shutdown,
2810 };
2811
2812 module_spi_driver(lr388k7_driver);
2813
2814 MODULE_AUTHOR("Makoto Itsuki <itsuki.makoto@sharp.co.jp>");
2815 MODULE_DESCRIPTION("LR388K7 Touchscreen Driver");
2816 MODULE_LICENSE("GPL");