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