input: touch: sharp:fix unbalanced irq disable.
[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 (18)
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 irqreturn_t lr388k7_wakeup_thread(int irq, void *_ts)
784 {
785         struct lr388k7 *ts = (struct lr388k7 *)_ts;
786         struct input_dev *input_dev = ts->idev;
787         u8 u8_status;
788
789         dev_info(&g_spi->dev, "[ENTER] Wakeup thread\n");
790
791         /* just try to clear */
792         u8_status = lr388k7_clear_irq();
793
794         dev_info(&g_spi->dev, "Tap wakeup\n");
795
796         input_report_key(input_dev, KEY_POWER, 1);
797         input_sync(input_dev);
798         input_report_key(input_dev, KEY_POWER, 0);
799         input_sync(input_dev);
800
801         if (g_st_state.b_is_suspended)
802                 g_st_state.b_is_suspended = false;
803
804         dev_info(&g_spi->dev, "[EXIT] Wakeup thread\n");
805
806         return IRQ_HANDLED;
807 }
808
809 static irqreturn_t lr388k7_irq_thread(int irq, void *_ts)
810 {
811         struct lr388k7 *ts = (struct lr388k7 *)_ts;
812         struct input_dev *input_dev = ts->idev;
813
814         if (g_u8_scan == K7_SCAN_STATE_IDLE) {
815                 input_event(input_dev, EV_MSC, MSC_ACTIVITY, 1);
816                 input_sync(input_dev);
817         }
818
819         if (g_st_state.b_is_suspended) {
820                 if (g_st_dbg.wakeup.enable == 1) {
821                         dev_info(&g_spi->dev, "Throw IRQ_WAKE_THREAD\n");
822                         return IRQ_WAKE_THREAD;
823                 }
824                 return IRQ_HANDLED;
825         }
826
827         queue_work(g_st_state.st_wq_k7, &g_st_state.st_work_k7);
828         return IRQ_HANDLED;
829 }
830
831 /* must be called with ts->mutex held */
832 static void __lr388k7_disable(struct lr388k7 *ts)
833 {
834         disable_irq(ts->irq);
835 }
836
837 /* must be called with ts->mutex held */
838 static void __lr388k7_enable(struct lr388k7 *ts)
839 {
840         enable_irq(ts->irq);
841 }
842
843 static ssize_t lr388k7_ts_force_cap_show(struct device *dev,
844                                          struct device_attribute *attr,
845                                          char *buf)
846 {
847         return sprintf(buf, "%d\n", g_st_dbg.u8ForceCap);
848 }
849
850 static ssize_t lr388k7_ts_force_cap_store(struct device *dev,
851                                           struct device_attribute *attr,
852                                           const char *buf,
853                                           size_t count)
854 {
855         ssize_t ret;
856
857         ret = (ssize_t)count;
858
859         if (g_st_state.b_is_suspended)
860                 return ret;
861
862         if (count != 1)
863                 return ret;
864
865         if (buf[0] == '0')
866                 g_st_dbg.u8ForceCap = 0;
867         else if (buf[0] == '1')
868                 g_st_dbg.u8ForceCap = 1;
869
870         lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_CTRL);
871
872         return ret;
873 }
874
875 static ssize_t lr388k7_ts_dump_show(struct device *dev,
876                                     struct device_attribute *attr,
877                                     char *buf)
878 {
879         return sprintf(buf, "%d\n", g_st_dbg.u8Dump);
880 }
881
882 static ssize_t lr388k7_ts_dump_store(struct device *dev,
883                                      struct device_attribute *attr,
884                                      const char *buf,
885                                      size_t count)
886 {
887         ssize_t ret;
888
889         ret = (ssize_t)count;
890
891         if (g_st_state.b_is_suspended)
892                 return ret;
893
894         if (count != 1)
895                 return ret;
896
897         if (buf[0] == '0')
898                 g_st_dbg.u8Dump = 0;
899         else if (buf[0] == '1')
900                 g_st_dbg.u8Dump = 1;
901
902         lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_CTRL);
903
904         return ret;
905 }
906
907 static ssize_t lr388k7_ts_report_mode_show(struct device *dev,
908                                     struct device_attribute *attr,
909                                     char *buf)
910 {
911         return sprintf(buf, "%d\n", g_u8_mode);
912 }
913
914 static ssize_t lr388k7_ts_report_mode_store(struct device *dev,
915                                      struct device_attribute *attr,
916                                      const char *buf,
917                                      size_t count)
918 {
919         ssize_t ret;
920         u8 prev_mode = g_u8_mode;
921
922         ret = (ssize_t)count;
923
924         if (count != 2)
925                 return ret;
926
927         if (buf[0] >= 0x30 && buf[0] <= 0x36)
928                 g_u8_mode = (u8)buf[0] - 0x30;
929
930         if (prev_mode != g_u8_mode)
931                 lr388k7_send_signal(g_st_state.u32_pid,
932                                     LR388K7_SIGNAL_MODE);
933
934         return ret;
935 }
936
937 static ssize_t lr388k7_ts_version_show(struct device *dev,
938         struct device_attribute *attr,
939         char *buf)
940 {
941
942 #ifdef CONFIG_TOUCHSCREEN_NVTOUCH
943         return sprintf(buf, "FW %d, Driver %d, Module %d, %s\n",
944                         g_st_state.u16fw_ver,
945                         K7_DRIVER_VERSION,
946                         g_st_state.u16module_ver,
947                         nvtouch_kernel_get_version_string()
948                         );
949 #else
950         return sprintf(buf, "FW %d, Driver %d, Module %d\n",
951                        g_st_state.u16fw_ver,
952                        K7_DRIVER_VERSION,
953                        g_st_state.u16module_ver
954                        );
955 #endif
956 }
957
958 static ssize_t lr388k7_ts_version_store(struct device *dev,
959         struct device_attribute *attr,
960         const char *buf, size_t count)
961 {
962         return count;
963 }
964
965
966 static ssize_t lr388k7_ts_slowscan_enable_store(struct device *dev,
967         struct device_attribute *attr,
968         const char *buf, size_t count)
969 {
970 #ifdef ENABLE_SLOW_SCAN
971         unsigned long val;
972         ssize_t err;
973         ssize_t ret;
974         if (count < 2)
975                 return -EINVAL;
976
977         if (g_st_state.b_is_suspended)
978                 return -EINVAL;
979
980         ret = (ssize_t) count;
981
982         if (count == 2) {
983                 if (buf[0] == '0') {
984                         g_st_dbg.slowscan.enable = 0;
985                         lr388k7_send_signal(g_st_state.u32_pid,
986                                             LR388K7_SIGNAL_CTRL);
987                 } else if (buf[0] == '1') {
988                         g_st_dbg.slowscan.enable = 1;
989                         lr388k7_send_signal(g_st_state.u32_pid,
990                                             LR388K7_SIGNAL_CTRL);
991                 }
992         } else if ((buf[0] == '2') && (buf[1] == ' ')) {
993                 err = kstrtoul(&buf[2], 10, &val);
994
995                 if (err) {
996                         ret = err;
997                 } else {
998                         /* check if prefined value*/
999                         if ((val == K7_SLOW_SCAN_30) ||
1000                             (val == K7_SLOW_SCAN_60) ||
1001                             (val == K7_SLOW_SCAN_100) ||
1002                             (val == K7_SLOW_SCAN_120)) {
1003                                 g_st_dbg.slowscan.scan_rate = (u16)val;
1004                                 g_st_dbg.slowscan.enable = 1;
1005                                 lr388k7_send_signal(g_st_state.u32_pid,
1006                                                     LR388K7_SIGNAL_CTRL);
1007                         }
1008                 }
1009         }
1010
1011         return ret;
1012 #else
1013         return count;
1014 #endif
1015 }
1016
1017 static ssize_t lr388k7_ts_slowscan_enable_show(struct device *dev,
1018         struct device_attribute *attr,
1019         char *buf)
1020 {
1021 #ifdef ENABLE_SLOW_SCAN
1022         return sprintf(buf, "Slow Scan:%s Scan Rate:%dHz\n",
1023                        g_st_dbg.slowscan.enable ?
1024                        "Enabled" : "Disabled",
1025                        g_st_dbg.slowscan.scan_rate);
1026 #else
1027         return sprintf(buf, "Not implemented yet\n");
1028 #endif
1029 }
1030
1031 static ssize_t lr388k7_ts_wakeup_enable_store(struct device *dev,
1032         struct device_attribute *attr,
1033         const char *buf, size_t count)
1034 {
1035         ssize_t ret;
1036
1037         if (count < 2)
1038                 return -EINVAL;
1039
1040         if (g_st_state.b_is_suspended)
1041                 return -EINVAL;
1042
1043         ret = (ssize_t) count;
1044
1045         if (count == 2) {
1046                 if (buf[0] == '0') {
1047                         g_st_dbg.wakeup.enable = 0;
1048                         lr388k7_send_signal(g_st_state.u32_pid,
1049                                             LR388K7_SIGNAL_CTRL);
1050                 } else if (buf[0] == '2') {
1051                         if (g_st_dbg.wakeup.enable <= 1)
1052                                 g_u16_wakeup_enable = g_st_dbg.wakeup.enable;
1053                         g_st_dbg.wakeup.enable = 2;
1054                         lr388k7_send_signal(g_st_state.u32_pid,
1055                                             LR388K7_SIGNAL_CTRL);
1056                 }
1057         } else if ((buf[0] == '1') && (buf[1] == ' ')) {
1058                 /* check if prefined value*/
1059                 if ((buf[2] == '2') ||
1060                     (buf[2] == '3') ||
1061                     (buf[2] == '4')) {
1062                         g_st_dbg.wakeup.num_tap = buf[2] - 0x30;
1063                         g_st_dbg.wakeup.enable = 1;
1064                         lr388k7_send_signal(g_st_state.u32_pid,
1065                                             LR388K7_SIGNAL_CTRL);
1066                 }
1067         }
1068
1069         return ret;
1070 }
1071
1072 static ssize_t lr388k7_ts_wakeup_enable_show(struct device *dev,
1073         struct device_attribute *attr,
1074         char *buf)
1075 {
1076         return sprintf(buf, "wakeup_enable:%s(%d) Number of taps:%d\n",
1077                        g_st_dbg.wakeup.enable == 1 ?
1078                        "Enabled" : "Disabled",
1079                        g_st_dbg.wakeup.enable,
1080                        g_st_dbg.wakeup.num_tap);
1081 }
1082
1083 static ssize_t lr388k7_ts_test_store(struct device *dev,
1084         struct device_attribute *attr,
1085         const char *buf, size_t count)
1086 {
1087         ssize_t ret;
1088
1089         if (count != 2)
1090                 return -EINVAL;
1091
1092         ret = (ssize_t) count;
1093
1094         if (buf[0] == '1') {
1095                 g_st_dbg.u8Test = 1;
1096                 lr388k7_send_signal(g_st_state.u32_pid,
1097                                     LR388K7_SIGNAL_CTRL);
1098         } else if (buf[0] == '0') {
1099                 g_st_dbg.u8Test = 0;
1100                 lr388k7_send_signal(g_st_state.u32_pid,
1101                                     LR388K7_SIGNAL_CTRL);
1102         }
1103
1104         return ret;
1105 }
1106
1107 static ssize_t lr388k7_ts_test_show(struct device *dev,
1108         struct device_attribute *attr,
1109         char *buf)
1110 {
1111         return sprintf(buf, "%s\n", g_st_dbg.u8Test == 1 ?
1112                        "Enabled" : "Disabled");
1113 }
1114
1115 #if defined(DEBUG_LR388K7)
1116 static ssize_t lr388k7_ts_check_state_store(struct device *dev,
1117         struct device_attribute *attr,
1118         const char *buf, size_t count)
1119 {
1120         ssize_t ret;
1121         ret = (ssize_t) count;
1122         return ret;
1123 }
1124
1125 static ssize_t lr388k7_ts_check_state_show(struct device *dev,
1126         struct device_attribute *attr,
1127         char *buf)
1128 {
1129         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1130         u8 u8_tx_buf[K7_RD_HEADER_SIZE + 5], u8_rx_buf[K7_RD_HEADER_SIZE + 5];
1131         u8 u8Ret;
1132         u8 u8HWR;
1133         u32 u32RES, u32FWR;
1134         size_t count = 0;
1135
1136         u8_tx_buf[count++] = K7_RD_OPCODE;
1137         u8_tx_buf[count++] = (K7_STATE_CTL_ADDR >> 16) & 0xFF;
1138         u8_tx_buf[count++] = (K7_STATE_CTL_ADDR >>  8) & 0xFF;
1139         u8_tx_buf[count++] = (K7_STATE_CTL_ADDR >>  0) & 0xFF;
1140         u8_tx_buf[count++] = 0x00;
1141
1142         g_st_state.u32SCK = 0;
1143
1144         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1145                 u8Ret = u8_rx_buf[K7_RD_HEADER_SIZE];
1146         } else {
1147                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1148                        gpio_get_value(ts->gpio_irq) ?
1149                        "High" : "Low"
1150                        );
1151         }
1152
1153         count = 0;
1154         u8_tx_buf[count++] = K7_RD_OPCODE;
1155         u8_tx_buf[count++] = 0x00;
1156         u8_tx_buf[count++] = 0x02;
1157         u8_tx_buf[count++] = 0xAC;
1158         u8_tx_buf[count++] = 0x00;
1159
1160         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1161                 u8HWR = u8_rx_buf[K7_RD_HEADER_SIZE];
1162         } else {
1163                 g_st_state.u32SCK = g_spi->max_speed_hz;
1164                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1165                        gpio_get_value(ts->gpio_irq) ?
1166                        "High" : "Low"
1167                        );
1168         }
1169
1170         count = 0;
1171         u8_tx_buf[count++] = K7_RD_OPCODE;
1172         u8_tx_buf[count++] = 0x00;
1173         u8_tx_buf[count++] = 0x02;
1174         u8_tx_buf[count++] = 0xB0;
1175         u8_tx_buf[count++] = 0x00;
1176         u8_tx_buf[count++] = 0x00;
1177         u8_tx_buf[count++] = 0x00;
1178         u8_tx_buf[count++] = 0x00;
1179
1180         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1181                 u32FWR = u8_rx_buf[K7_RD_HEADER_SIZE] |
1182                         u8_rx_buf[K7_RD_HEADER_SIZE + 1] << 8 |
1183                         u8_rx_buf[K7_RD_HEADER_SIZE + 2] << 16 |
1184                         u8_rx_buf[K7_RD_HEADER_SIZE + 3] << 24;
1185         } else {
1186                 g_st_state.u32SCK = g_spi->max_speed_hz;
1187                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1188                        gpio_get_value(ts->gpio_irq) ?
1189                        "High" : "Low"
1190                        );
1191         }
1192
1193         count = 0;
1194         u8_tx_buf[count++] = K7_RD_OPCODE;
1195         u8_tx_buf[count++] = 0x00;
1196         u8_tx_buf[count++] = 0x00;
1197         u8_tx_buf[count++] = 0x40;
1198         u8_tx_buf[count++] = 0x00;
1199         u8_tx_buf[count++] = 0x00;
1200         u8_tx_buf[count++] = 0x00;
1201         u8_tx_buf[count++] = 0x00;
1202
1203         if (lr388k7_spi_read(u8_tx_buf, u8_rx_buf, count)) {
1204                 u32RES = u8_rx_buf[K7_RD_HEADER_SIZE] |
1205                         u8_rx_buf[K7_RD_HEADER_SIZE + 1] << 8 |
1206                         u8_rx_buf[K7_RD_HEADER_SIZE + 2] << 16 |
1207                         u8_rx_buf[K7_RD_HEADER_SIZE + 3] << 24;
1208         } else {
1209                 g_st_state.u32SCK = g_spi->max_speed_hz;
1210                 return sprintf(buf, "status :IRQ(%s) Failed to read\n",
1211                        gpio_get_value(ts->gpio_irq) ?
1212                        "High" : "Low"
1213                        );
1214         }
1215
1216         g_st_state.u32SCK = g_spi->max_speed_hz;
1217
1218         return sprintf(
1219                        buf,
1220                        "IRQ(%s) status=0x%02X, HWRev=%d, FWRev=0x%0X, Res=0x%04X\n",
1221                        gpio_get_value(ts->gpio_irq) ?
1222                        "High" : "Low",
1223                        u8Ret,
1224                        u8HWR,
1225                        u32FWR,
1226                        u32RES
1227                        );
1228 }
1229 #endif
1230
1231 #define LR388K7_LOG_MAX_SIZE (8 * 1024 * 1024)
1232 #define LR388K7_LOG_DATA_SIZE (512)
1233
1234 struct lr388k7_log {
1235         unsigned long seq_num;
1236         unsigned long time;
1237         unsigned char data[LR388K7_LOG_DATA_SIZE];
1238 };
1239
1240 DEFINE_SPINLOCK(lr388k7log_lock);
1241 struct lr388k7_log glog[LR388K7_LOG_MAX_SIZE / sizeof(struct lr388k7_log)];
1242 static int log_size = sizeof(glog) / sizeof(glog[0]);
1243 static unsigned long g_seq_num;
1244 static int g_log_front;
1245 static int g_log_rear;
1246
1247
1248 static ssize_t lr388k7_ts_log_store(struct device *dev,
1249         struct device_attribute *attr,
1250         const char *buf, size_t count)
1251 {
1252         ssize_t ret;
1253         struct lr388k7_log log;
1254         unsigned long flags;
1255
1256         ret = (ssize_t) count;
1257
1258         g_seq_num++;
1259         log.seq_num = g_seq_num;
1260         log.time = jiffies;
1261         memcpy(log.data, buf, sizeof(log.data));
1262         spin_lock_irqsave(&lr388k7log_lock, flags);
1263
1264         if (((g_log_rear + 1) % log_size) == g_log_front) {
1265                 g_log_front++;
1266                 if (g_log_front >= log_size)
1267                         g_log_front = 0;
1268                 g_log_rear++;
1269                 if (g_log_rear >= log_size)
1270                         g_log_rear = 0;
1271         } else {
1272                 g_log_rear++;
1273                 if (g_log_rear >= log_size)
1274                         g_log_rear = 0;
1275         }
1276
1277         glog[g_log_rear] = log;
1278
1279         spin_unlock_irqrestore(&lr388k7log_lock, flags);
1280
1281         return ret;
1282 }
1283
1284 static ssize_t lr388k7_ts_log_show(struct device *dev,
1285         struct device_attribute *attr,
1286         char *buf)
1287 {
1288         char *s;
1289         unsigned long flags;
1290         struct lr388k7_log log;
1291
1292         s = buf;
1293
1294         sprintf(s,
1295                 "FW %d, Driver %d, Module %d\n",
1296                 g_st_state.u16fw_ver,
1297                 K7_DRIVER_VERSION,
1298                 g_st_state.u16module_ver
1299                 );
1300
1301         s += strlen(s);
1302
1303         for (; (s - buf) + LR388K7_LOG_DATA_SIZE < PAGE_SIZE; ) {
1304                 spin_lock_irqsave(&lr388k7log_lock, flags);
1305
1306                 if (g_log_rear == g_log_front) {
1307                         spin_unlock_irqrestore(&lr388k7log_lock, flags);
1308                         return (ssize_t)(s - buf);
1309                 } else {
1310                         g_log_front++;
1311                         if (g_log_front >= log_size)
1312                                 g_log_front = 0;
1313                 }
1314                 log = glog[g_log_front];
1315                 spin_unlock_irqrestore(&lr388k7log_lock, flags);
1316
1317                 sprintf(s, "[%08lx|%08lx] %s",
1318                         log.seq_num,
1319                         log.time,
1320                         log.data);
1321                 s += strlen(s);
1322         }
1323
1324         return (ssize_t)(s - buf);
1325 }
1326
1327
1328 static DEVICE_ATTR(force_cap, 0660, lr388k7_ts_force_cap_show,
1329                         lr388k7_ts_force_cap_store);
1330 static DEVICE_ATTR(dump, 0666, lr388k7_ts_dump_show,
1331                         lr388k7_ts_dump_store);
1332 static DEVICE_ATTR(report_mode, 0640, lr388k7_ts_report_mode_show,
1333                         lr388k7_ts_report_mode_store);
1334 static DEVICE_ATTR(version, 0640,
1335                    lr388k7_ts_version_show,
1336                    lr388k7_ts_version_store);
1337 static DEVICE_ATTR(slowscan_enable, 0640,
1338                    lr388k7_ts_slowscan_enable_show,
1339                    lr388k7_ts_slowscan_enable_store);
1340 static DEVICE_ATTR(wakeup_enable, 0640,
1341                    lr388k7_ts_wakeup_enable_show,
1342                    lr388k7_ts_wakeup_enable_store);
1343 static DEVICE_ATTR(test, 0640,
1344                    lr388k7_ts_test_show,
1345                    lr388k7_ts_test_store);
1346 #if defined(DEBUG_LR388K7)
1347 static DEVICE_ATTR(check_state, 0660,
1348                    lr388k7_ts_check_state_show,
1349                    lr388k7_ts_check_state_store);
1350
1351 static DEVICE_ATTR(log, 0660,
1352                    lr388k7_ts_log_show,
1353                    lr388k7_ts_log_store);
1354 #endif
1355
1356 static struct attribute *lr388k7_ts_attributes[] = {
1357         &dev_attr_force_cap.attr,
1358         &dev_attr_dump.attr,
1359         &dev_attr_report_mode.attr,
1360         &dev_attr_version.attr,
1361         &dev_attr_slowscan_enable.attr,
1362         &dev_attr_wakeup_enable.attr,
1363         &dev_attr_test.attr,
1364 #if defined(DEBUG_LR388K7)
1365         &dev_attr_check_state.attr,
1366         &dev_attr_log.attr,
1367 #endif
1368         NULL
1369 };
1370
1371 static const struct attribute_group lr388k7_ts_attr_group = {
1372         .attrs = lr388k7_ts_attributes,
1373 };
1374
1375 static int lr388k7_open(struct input_dev *input)
1376 {
1377         struct lr388k7 *ts = input_get_drvdata(input);
1378
1379         dev_info(&ts->spi->dev, "[ENTER] open\n");
1380
1381         mutex_lock(&ts->mutex);
1382
1383         __lr388k7_enable(ts);
1384
1385         ts->opened = true;
1386
1387         mutex_unlock(&ts->mutex);
1388
1389         dev_info(&ts->spi->dev, "[EXIT] open\n");
1390         return 0;
1391 }
1392
1393 static void lr388k7_close(struct input_dev *input)
1394 {
1395         struct lr388k7 *ts = input_get_drvdata(input);
1396
1397         dev_info(&ts->spi->dev, "[ENTER] close\n");
1398         mutex_lock(&ts->mutex);
1399
1400         __lr388k7_disable(ts);
1401
1402         ts->opened = false;
1403
1404         mutex_unlock(&ts->mutex);
1405         dev_info(&ts->spi->dev, "[EXIT] close\n");
1406 }
1407
1408
1409 #if defined(UDEV_ENABLE)
1410 static void lr388k7_send_udev_event(void *p)
1411 {
1412         struct lr388k7_udev_event *p_udev_event;
1413         char *envp[2];
1414         char *event;
1415
1416         if (!p)
1417                 return;
1418
1419         p_udev_event = kmalloc(
1420                                sizeof(struct lr388k7_udev_event),
1421                                GFP_KERNEL);
1422
1423         if (!p_udev_event)
1424                 return;
1425
1426         if (copy_from_user(p_udev_event,
1427                           p,
1428                           sizeof(struct lr388k7_udev_event))) {
1429                 kfree(p_udev_event);
1430                 return;
1431         }
1432
1433         event = kasprintf(GFP_KERNEL, "TRIGGER=%s", p_udev_event->str);
1434         if (event) {
1435                 envp[0] = event;
1436                 envp[1] = NULL;
1437                 kobject_uevent_env(&lr388k7_ts_miscdev.this_device->kobj,
1438                                    KOBJ_CHANGE,
1439                                    envp);
1440                 kfree(event);
1441         }
1442         kfree(p_udev_event);
1443 }
1444 #endif
1445
1446 #if defined(ACTIVE_ENABLE)
1447 static void lr388k7_active_report(void *p)
1448 {
1449         struct lr388k7_active_report *p_active_report;
1450         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1451         struct input_dev *input_dev = ts->idev_active;
1452         int x, y, z;
1453         int tool = 0;
1454         u8 status;
1455
1456         if (!p)
1457                 return;
1458
1459         /*
1460          * Memory Allocation
1461          */
1462         p_active_report = kmalloc(
1463                                  sizeof(struct lr388k7_active_report),
1464                                  GFP_KERNEL);
1465         if (!p_active_report)
1466                 return;
1467
1468         if (copy_from_user(p_active_report,
1469                            p,
1470                            sizeof(struct lr388k7_active_report))) {
1471                 kfree(p_active_report);
1472                 return;
1473         }
1474
1475         status = p_active_report->status;
1476
1477         /* Get current tool */
1478         if (status & 0x0C)
1479                 tool = BTN_TOOL_RUBBER;
1480         else
1481                 tool = BTN_TOOL_PEN;
1482
1483         if (!status)
1484                 tool = 0; /* Reset */
1485
1486         /* Check if tool changed then notify input subsystem */
1487         if (ts->tool != tool) {
1488                 if (ts->tool) {
1489                         /* Reset old tool state */
1490                         input_report_key(input_dev, BTN_TOUCH, 0);
1491                         input_report_key(input_dev, BTN_STYLUS, 0);
1492                         input_report_abs(input_dev, ABS_PRESSURE, 0);
1493                 }
1494                 input_report_key(input_dev, ts->tool, 0);
1495                 input_sync(input_dev);
1496
1497                 /* Update current*/
1498                 ts->tool = tool;
1499                 if (tool)
1500                         input_report_key(input_dev, tool, 1);
1501         }
1502
1503         if (tool) {
1504                 x = p_active_report->x;
1505                 y = p_active_report->y;
1506                 z = p_active_report->z;
1507
1508                 input_report_abs(input_dev, ABS_X, x);
1509                 input_report_abs(input_dev, ABS_Y, y);
1510                 input_report_abs(input_dev, ABS_PRESSURE, z);
1511                 input_report_key(input_dev, BTN_TOUCH, status & 0x09);
1512                 input_report_key(input_dev, BTN_STYLUS, status & 0x10);
1513                 input_sync(input_dev);
1514         }
1515
1516         kfree(p_active_report);
1517 }
1518 #endif
1519
1520 #if defined(PROTOCOL_A)
1521 static void lr388k7_touch_report(void *p)
1522 {
1523         u8 u8_num, i, u8_num_of_touch;
1524         struct lr388k7_touch_report *p_touch_report;
1525         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1526         struct input_dev *input_dev = ts->idev;
1527         bool b_is_eraser = false;
1528
1529         if (!p)
1530                 return;
1531
1532         /*
1533          * Memory Allocation
1534          */
1535         p_touch_report = kmalloc(
1536                                  sizeof(struct lr388k7_touch_report),
1537                                  GFP_KERNEL);
1538         if (!p_touch_report)
1539                 return;
1540
1541         if (copy_from_user(p_touch_report,
1542                            p,
1543                            sizeof(struct lr388k7_touch_report))) {
1544                 kfree(p_touch_report);
1545                 return;
1546         }
1547
1548         u8_num = p_touch_report->u8_num_of_touch;
1549
1550 #ifdef CONFIG_TOUCHSCREEN_NVTOUCH
1551
1552         /* Send events to nvtouch (for tracing or reporting) */
1553         {
1554                 /* report events through the same interface with nvtouch */
1555                 struct nvtouch_events *nv_events =
1556                         nvtouch_kernel_get_vendor_events_ptr();
1557                 for (i = 0; i < u8_num; i++) {
1558
1559                         if (ts->flip_x)
1560                                 p_touch_report->tc[i].x =
1561                                 (ts->max_x - p_touch_report->tc[i].x) < 0 ?
1562                                 0 : ts->max_x - p_touch_report->tc[i].x;
1563                         if (ts->flip_y)
1564                                 p_touch_report->tc[i].y =
1565                                 (ts->max_y - p_touch_report->tc[i].y) < 0 ?
1566                                 0 : ts->max_y - p_touch_report->tc[i].y;
1567                         if (ts->swap_xy) {
1568                                 u16 tmp;
1569                                 tmp = p_touch_report->tc[i] . x;
1570                                 p_touch_report->tc[i].x =
1571                                         p_touch_report->tc[i].y;
1572                                 p_touch_report->tc[i].y = tmp;
1573                         }
1574
1575                         nv_events->events[i].tracking_id =
1576                                 p_touch_report->tc[i].id;
1577                         nv_events->events[i].position_y =
1578                                 p_touch_report->tc[i].x;
1579                         nv_events->events[i].position_x =
1580                                 p_touch_report->tc[i].y;
1581                         nv_events->events[i].pressure = p_touch_report->tc[i].z;
1582                         nv_events->events[i].tool = NVTOUCH_EVENT_TOOL_PEN;
1583                         nv_events->timestamp = nvtouch_get_time_us();
1584                 }
1585                 nv_events->event_count = u8_num;
1586         }
1587
1588         if ((nvtouch_get_driver_mode() !=
1589                 NVTOUCH_DRIVER_CONFIG_MODE_VENDOR_ONLY)
1590                 && (nvtouch_get_driver_mode() !=
1591                 NVTOUCH_DRIVER_CONFIG_MODE_VENDOR_DTA)
1592         ) {
1593                 /* events will be reported via nvtouch */
1594                 kfree(p_touch_report);
1595                 return;
1596         }
1597 #endif
1598
1599         if (u8_num == 0) {
1600                 if (ts->b_eraser_active) {
1601                         ts->b_eraser_active = false;
1602                         input_report_key(input_dev, BTN_TOOL_RUBBER, 0);
1603                 }
1604                 input_mt_sync(input_dev);
1605                 input_sync(input_dev);
1606                 kfree(p_touch_report);
1607                 return;
1608         }
1609         u8_num_of_touch = 0;
1610
1611         for (i = 0; i < u8_num; i++) {
1612
1613 #if defined(DEBUG_LR388K7_REPORT)
1614                 dev_info(&g_spi->dev, "ID=%2d, status=%02d, x=%5d, y=%5d, w=%d, h=%d, z=%5d, num=%2d\n",
1615                          p_touch_report->tc[i] . id,
1616                          p_touch_report->tc[i] . status,
1617                          p_touch_report->tc[i] . x,
1618                          p_touch_report->tc[i] . y,
1619                          p_touch_report->tc[i] . width,
1620                          p_touch_report->tc[i] . height,
1621                          p_touch_report->tc[i] . z,
1622                          u8_num
1623                          );
1624 #endif
1625
1626                 if ((p_touch_report->tc[i] . status & 0x80) != 0)
1627                         b_is_eraser = true;
1628                 else
1629                         b_is_eraser = false;
1630
1631                 if (b_is_eraser) {
1632                         input_report_key(input_dev, BTN_TOOL_RUBBER, 1);
1633                         ts->b_eraser_active = true;
1634                 } else if ((p_touch_report->tc[i] . status & 0x7F) <= 3 ||
1635                           (p_touch_report->tc[i] . status & 0x7F) == 8)
1636                         input_report_abs(input_dev,
1637                                          ABS_MT_TOOL_TYPE, MT_TOOL_FINGER);
1638                 else if ((p_touch_report->tc[i] . status & 0x7F) <= 7 ||
1639                          (p_touch_report->tc[i] . status & 0x7F) == 12)
1640                         input_report_abs(input_dev,
1641                                          ABS_MT_TOOL_TYPE, MT_TOOL_PEN);
1642                 input_report_abs(input_dev,
1643                                  ABS_MT_TRACKING_ID,
1644                                  p_touch_report->tc[i] .id);
1645                 input_report_abs(input_dev,
1646                                  ABS_MT_POSITION_X,
1647                                  p_touch_report->tc[i] . x);
1648                 input_report_abs(input_dev,
1649                                  ABS_MT_POSITION_Y,
1650                                  p_touch_report->tc[i] . y);
1651                 input_report_abs(input_dev,
1652                                  ABS_MT_PRESSURE,
1653                                  p_touch_report->tc[i] . z);
1654                 input_mt_sync(input_dev);
1655                 u8_num_of_touch++;
1656         }
1657         input_sync(input_dev);
1658
1659         kfree(p_touch_report);
1660 }
1661 #else /* PROTOCOL_B */
1662 static void lr388k7_touch_report(void *p)
1663 {
1664         u8 u8_num, i;
1665         struct lr388k7_touch_report *p_touch_report;
1666         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1667         struct input_dev *input_dev = ts->idev;
1668
1669         if (!p)
1670                 return;
1671
1672         /*
1673          * Memory Allocation
1674          */
1675         p_touch_report = kmalloc(
1676                                  sizeof(struct lr388k7_touch_report),
1677                                  GFP_KERNEL);
1678         if (!p_touch_report)
1679                 return;
1680
1681         if (copy_from_user(p_touch_report,
1682                            p,
1683                            sizeof(struct lr388k7_touch_report))) {
1684                 kfree(p_touch_report);
1685                 return;
1686         }
1687
1688         u8_num = p_touch_report->u8_num_of_touch;
1689
1690         for (i = 0; i < u8_num; i++) {
1691
1692 #if defined(DEBUG_LR388K7_REPORT)
1693                 dev_info(&g_spi->dev, "ID=%2d, status=%02d, x=%5d, y=%5d, w=%d, h=%d, z=%5d, num=%2d\n",
1694                          p_touch_report->tc[i] . id,
1695                          p_touch_report->tc[i] . status,
1696                          p_touch_report->tc[i] . x,
1697                          p_touch_report->tc[i] . y,
1698                          p_touch_report->tc[i] . width,
1699                          p_touch_report->tc[i] . height,
1700                          p_touch_report->tc[i] . z,
1701                          u8_num
1702                          );
1703 #endif
1704                 input_mt_slot(input_dev,
1705                               p_touch_report->tc[i] . id);
1706                 if (((p_touch_report->tc[i] . status & 0x7F) == 8) ||
1707                     ((p_touch_report->tc[i] . status & 0x7F) == 12) ||
1708                     ((p_touch_report->tc[i] . status & 0x7F) == 14)) {
1709                         input_mt_report_slot_state(input_dev,
1710                                                    MT_TOOL_FINGER,
1711                                                    false);
1712                         continue;
1713                 }
1714
1715                 if ((p_touch_report->tc[i] . status & 0x7F) <= 3)
1716                         input_report_abs(input_dev,
1717                                          ABS_MT_TOOL_TYPE, MT_TOOL_FINGER);
1718                 else if (((p_touch_report->tc[i] . status & 0x7F) <= 7) ||
1719                          ((p_touch_report->tc[i] . status & 0x7F) > 12))
1720                         input_report_abs(input_dev,
1721                                          ABS_MT_TOOL_TYPE, MT_TOOL_PEN);
1722                 input_report_abs(input_dev,
1723                                  ABS_MT_TRACKING_ID,
1724                                  p_touch_report->tc[i] .id);
1725                 input_report_abs(input_dev,
1726                                  ABS_MT_POSITION_X,
1727                                  p_touch_report->tc[i].x);
1728                 input_report_abs(input_dev,
1729                                  ABS_MT_POSITION_Y,
1730                                  p_touch_report->tc[i].y);
1731                 input_report_abs(input_dev,
1732                                  ABS_MT_PRESSURE,
1733                                  p_touch_report->tc[i] . z);
1734         }
1735         input_sync(input_dev);
1736
1737         kfree(p_touch_report);
1738 }
1739 #endif /* #if defined(PROTOCOL_A) */
1740
1741
1742 static int access_num;
1743 static spinlock_t dev_spin_lock;
1744
1745 static int dev_open(struct inode *inode, struct file *filp)
1746 {
1747 #if defined(DEBUG_LR388K7)
1748         dev_info(&g_spi->dev, "dev_open\n");
1749 #endif
1750
1751         spin_lock(&dev_spin_lock);
1752
1753         if (access_num) {
1754                 spin_unlock(&dev_spin_lock);
1755                 dev_err(&g_spi->dev, "already open : %d\n", -EBUSY);
1756                 return -EBUSY;
1757         }
1758
1759         access_num++;
1760         spin_unlock(&dev_spin_lock);
1761
1762         return 0;
1763 }
1764
1765 static int dev_release(struct inode *inode, struct file *filp)
1766 {
1767         struct lr388k7 *ts = spi_get_drvdata(g_spi);
1768
1769 #if defined(DEBUG_LR388K7)
1770         dev_info(&g_spi->dev, "dev_release\n");
1771 #endif
1772         if (!ts)
1773                 return -EINVAL;
1774
1775         g_st_state.b_is_init_finish = 0;
1776
1777         spin_lock(&dev_spin_lock);
1778         access_num--;
1779         spin_unlock(&dev_spin_lock);
1780
1781         /* Reset assert */
1782         gpio_set_value(ts->gpio_reset, 0);
1783         g_st_state.b_is_reset = true;
1784
1785         return 0;
1786 }
1787
1788 /**
1789  * @brief lr388k7 spi interface
1790  * @param[in] txbuf command to read data
1791  * @param[in] rxbuf data to be read
1792  * @param[in] len length of data
1793  * @retval TRUE if success, otherwise FALSE
1794  */
1795 static int lr388k7_spi_read(u8 *txbuf, u8 *rxbuf, size_t len)
1796 {
1797         static DEFINE_MUTEX(lock);
1798
1799         int status;
1800         struct spi_message msg;
1801         struct spi_transfer t = {
1802                 .bits_per_word  = 8,
1803                 .tx_buf = txbuf,
1804                 .rx_buf = rxbuf,
1805                 .speed_hz =
1806                 g_st_state.u32SCK == 0 ? 12000000 : g_spi->max_speed_hz,
1807         };
1808
1809         mutex_lock(&lock);
1810
1811         t.len = len + K7_RD_HEADER_SIZE;
1812
1813         spi_message_init(&msg);
1814         spi_message_add_tail(&t, &msg);
1815         status = spi_sync(g_spi, &msg);
1816
1817         mutex_unlock(&lock);
1818
1819         if (status)
1820                 return false;
1821
1822         return true;
1823 }
1824
1825 static long lr388k7_spi_read_cmd_data(u8 *p, size_t count)
1826 {
1827         u8 *p_tx_buf;
1828         ssize_t status = 0;
1829
1830         /*
1831          * Memory Allocation
1832          */
1833         p_tx_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1834         if (p_tx_buf == NULL) {
1835                 status = -ENOMEM;
1836                 goto exit_dev_read_cmd_data;
1837         }
1838
1839         /*
1840          * Clear and Set command to read data
1841          */
1842         memset(p_tx_buf, 0, count + K7_RD_HEADER_SIZE);
1843
1844         p_tx_buf[0] = K7_RD_OPCODE;
1845         p_tx_buf[1] = (K7_CMD_DATA_OFFSET >> 16) & 0xFF;
1846         p_tx_buf[2] = (K7_CMD_DATA_OFFSET >>  8) & 0xFF;
1847         p_tx_buf[3] = (K7_CMD_DATA_OFFSET >>  0) & 0xFF;
1848         p_tx_buf[4] = 0x00;
1849
1850         /*
1851          * Read data
1852          */
1853         if (lr388k7_spi_read(p_tx_buf, p, count))
1854                 status = count;
1855         else
1856                 status = -EFAULT;
1857
1858         kfree(p_tx_buf);
1859
1860  exit_dev_read_cmd_data:
1861         return status;
1862 }
1863
1864 static long lr388k7_spi_read_wo_limit(u8 *p, size_t count)
1865 {
1866         u8 *p_buf;
1867         u8 *p_tx_buf;
1868         ssize_t status = 0;
1869
1870         /*
1871          * Memory Allocation
1872          */
1873         p_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1874         if (p_buf == NULL) {
1875                 status = -ENOMEM;
1876                 goto ext_dev_read_wo_limit;
1877         }
1878
1879         p_tx_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1880         if (p_tx_buf == NULL) {
1881                 status = -ENOMEM;
1882                 goto ext_dev_read_wo_limit_free;
1883         }
1884
1885         /*
1886          * Clear and Set command to read data
1887          */
1888         memset(p_tx_buf, 0, count + K7_RD_HEADER_SIZE);
1889
1890         if (copy_from_user(p_tx_buf, p, K7_RD_HEADER_SIZE)) {
1891                 status = -EFAULT;
1892                 goto ext_dev_read_wo_limit_free_tx;
1893         }
1894
1895         /*
1896          * Read data
1897          */
1898         if (lr388k7_spi_read(p_tx_buf, p_buf, count)) {
1899                 if (copy_to_user(p, &p_buf[K7_RD_HEADER_SIZE], count)) {
1900                         status = -EFAULT;
1901                         goto ext_dev_read_wo_limit_free_tx;
1902                 }
1903
1904                 status = count;
1905
1906         } else
1907                 status = -EFAULT;
1908
1909  ext_dev_read_wo_limit_free_tx:
1910         kfree(p_tx_buf);
1911  ext_dev_read_wo_limit_free:
1912         kfree(p_buf);
1913  ext_dev_read_wo_limit:
1914         return status;
1915 }
1916
1917 static long lr388k7_spi_read_from_ioctl(u8 *p, size_t count)
1918 {
1919         u8 *p_buf;
1920         u8 *p_tx_buf;
1921         ssize_t status = 0;
1922
1923         /*
1924          * Memory Allocation
1925          */
1926         p_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1927         if (p_buf == NULL) {
1928                 status = -ENOMEM;
1929                 goto ext_dev_read_from_ioctl;
1930         }
1931
1932         p_tx_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1933         if (p_tx_buf == NULL) {
1934                 status = -ENOMEM;
1935                 goto ext_dev_read_from_ioctl_free;
1936         }
1937
1938         /*
1939          * Clear and Set command to read data
1940          */
1941         memset(p_tx_buf, 0, count + K7_RD_HEADER_SIZE);
1942
1943         if (copy_from_user(p_tx_buf, p, K7_RD_HEADER_SIZE)) {
1944                 status = -EFAULT;
1945                 goto ext_dev_read_from_ioctl_free_tx;
1946         }
1947
1948         g_st_state.u32SCK = 0;
1949         /*
1950          * Read data
1951          */
1952         if (lr388k7_spi_read(p_tx_buf, p_buf, count)) {
1953                 if (copy_to_user(p, &p_buf[K7_RD_HEADER_SIZE], count)) {
1954                         status = -EFAULT;
1955                         goto ext_dev_read_from_ioctl_free_tx;
1956                 }
1957
1958                 status = count;
1959
1960         } else
1961                 status = -EFAULT;
1962
1963  ext_dev_read_from_ioctl_free_tx:
1964         kfree(p_tx_buf);
1965  ext_dev_read_from_ioctl_free:
1966         kfree(p_buf);
1967  ext_dev_read_from_ioctl:
1968         g_st_state.u32SCK = g_spi->max_speed_hz;
1969         return status;
1970 }
1971
1972 static ssize_t
1973 dev_read(struct file *filp, char __user *buf, size_t count, loff_t *pos)
1974 {
1975 #if 0
1976         u8 *p_buf;
1977         ssize_t status = 0;
1978
1979         p_buf = kmalloc(count + K7_RD_HEADER_SIZE, GFP_KERNEL);
1980         if (!p_buf) {
1981                 status = -ENOMEM;
1982                 goto exit_dev_read;
1983         }
1984
1985         memset(p_buf, 0, count + K7_RD_HEADER_SIZE);
1986
1987         if (copy_from_user(p_buf, buf, K7_RD_HEADER_SIZE)) {
1988                 status = -EFAULT;
1989                 goto exit_dev_read_free;
1990         }
1991
1992         if (lr388k7_spi_read(p_buf, count)) {
1993                 if (copy_to_user(buf, &p_buf[K7_RD_HEADER_SIZE], count)) {
1994                         status = -EFAULT;
1995                         goto exit_dev_read_free;
1996                 }
1997
1998                 status = count;
1999
2000         } else
2001                 status = -EFAULT;
2002
2003  exit_dev_read_free:
2004         kfree(p_buf);
2005  exit_dev_read:
2006         return status;
2007 #else
2008         return 0;
2009 #endif
2010 }
2011
2012 /**
2013  * @brief lr388k7 spi interface
2014  * @param[in] txbuf data to be written
2015  * @param[in] len length of data
2016  * @retval TRUE if success, otherwise FALSE
2017  */
2018 static int lr388k7_spi_write(u8 *txbuf, size_t len)
2019 {
2020         static DEFINE_MUTEX(lock);
2021         int status;
2022         struct spi_message msg;
2023         struct spi_transfer t = {
2024                 .bits_per_word  = 8,
2025                 .tx_buf = txbuf,
2026                 .len = len,
2027                 .speed_hz =
2028                 len < 16 ? 12000000 : g_spi->max_speed_hz,
2029         };
2030
2031         mutex_lock(&lock);
2032
2033         spi_message_init(&msg);
2034         spi_message_add_tail(&t, &msg);
2035         /*It returns zero on succcess,else a negative error code. */
2036         status = spi_sync(g_spi, &msg);
2037
2038         mutex_unlock(&lock);
2039
2040         if (status)
2041                 return false;
2042
2043         return true;
2044 }
2045
2046 static u32 lr388k7_get_platform_id(u8 *p)
2047 {
2048         u32 ret;
2049         struct lr388k7 *ts = spi_get_drvdata(g_spi);
2050
2051         ret = copy_to_user(p,
2052                            &ts->platform_id,
2053                            sizeof(ts->platform_id));
2054
2055         return ret;
2056 }
2057
2058 static u32 lr388k7_get_value(u8 *arg, unsigned int index)
2059 {
2060         u32 ret = 0;
2061         switch (index) {
2062         case LR388K7_GET_PLATFORM_ID:
2063                 ret = lr388k7_get_platform_id(arg);
2064                 break;
2065         default:
2066                 ret = -EINVAL;
2067                 break;
2068         }
2069         return ret;
2070 }
2071
2072
2073 static ssize_t
2074 dev_write(struct file *filp, const char __user *buf,
2075                 size_t count, loff_t *pos)
2076 {
2077         u8 *p_buf;
2078         ssize_t status = 0;
2079
2080         p_buf = kmalloc(count, GFP_KERNEL);
2081         if (!p_buf) {
2082                 status = -ENOMEM;
2083                 goto exit_dev_write;
2084         }
2085
2086         if (copy_from_user(p_buf, buf, count)) {
2087                 status = -EFAULT;
2088                 goto exit_dev_write_free;
2089         }
2090
2091         if (lr388k7_spi_write(p_buf, count))
2092                 status = count;
2093         else
2094                 status = -EFAULT;
2095
2096  exit_dev_write_free:
2097         kfree(p_buf);
2098  exit_dev_write:
2099         return status;
2100 }
2101
2102 static long dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2103 {
2104         long ret = true;
2105         unsigned int index;
2106
2107         index = (cmd >> 16) & 0xFFFF;
2108
2109         switch (cmd & 0xFFFF) {
2110         case LR388K7_IOCTL_TOUCH_REPORT:
2111 #ifdef CONFIG_TOUCHSCREEN_NVTOUCH
2112                 if (nvtouch_get_driver_mode() ==
2113                         NVTOUCH_DRIVER_CONFIG_MODE_NVTOUCH_ONLY)
2114                         break;
2115 #endif
2116                 lr388k7_touch_report((void *) arg);
2117                 break;
2118
2119         case LR388K7_IOCTL_READ_REGISTER:
2120                 ret = lr388k7_spi_read_from_ioctl((u8 *) arg, index);
2121                 break;
2122
2123         case LR388K7_IOCTL_READ_WO_LIMIT:
2124                 ret = lr388k7_spi_read_wo_limit((u8 *) arg, index);
2125                 break;
2126
2127         case LR388K7_IOCTL_SET_HAL_PID:
2128                 g_st_state.u32_pid = arg;
2129                 g_st_state.b_is_calc_finish = 1;
2130                 break;
2131
2132         case LR388K7_IOCTL_READ_RAW_DATA:
2133                 ret = lr388k7_queue_read_raw_data((u8 *) arg, index);
2134                 break;
2135
2136         case LR388K7_IOCTL_READ_CMD_DATA:
2137                 ret = lr388k7_cmd_queue_read((u8 *) arg, index);
2138                 break;
2139
2140         case LR388K7_IOCTL_FINISH_CALC:
2141                 g_st_state.b_is_calc_finish = 1;
2142                 break;
2143
2144         case LR388K7_IOCTL_SET_RAW_DATA_SIZE:
2145                 g_u32_raw_data_size = arg;
2146                 break;
2147
2148         case LR388K7_IOCTL_SET_REVISION:
2149                 g_st_state.u16fw_ver = (arg & 0xFFFF0000) >> 16;
2150                 g_st_state.u16module_ver = arg & 0x0000FFFF;
2151
2152                 dev_info(&g_spi->dev, "Version %d.%d.%d\n",
2153                          g_st_state.u16fw_ver,
2154                          K7_DRIVER_VERSION,
2155                          g_st_state.u16module_ver
2156                          );
2157                 break;
2158
2159         case LR388K7_IOCTL_GET_DEBUG_STATUS:
2160                 lr388k7_ts_get_debug_status((u8 *)arg);
2161                 break;
2162
2163         case LR388K7_IOCTL_SET_MODE:
2164                 g_u8_mode = (u8)arg;
2165                 break;
2166
2167         case LR388K7_IOCTL_GET_MODE:
2168                 lr388k7_ts_get_mode((u8 *)arg);
2169                 break;
2170
2171         case LR388K7_IOCTL_RESET:
2172                 ret = lr388k7_hardreset(arg);
2173                 break;
2174
2175         case LR388K7_IOCTL_CLEAR_BUFFER:
2176                 lr388k7_queue_reset();
2177                 g_st_state.b_is_init_finish = arg;
2178                 if (g_st_state.b_is_init_finish)
2179                         lr388k7_send_signal(g_st_state.u32_pid,
2180                                             LR388K7_SIGNAL_MODE);
2181                 break;
2182
2183         case LR388K7_IOCTL_ACTIVE_REPORT:
2184 #if defined(ACTIVE_ENABLE)
2185                 lr388k7_active_report((void *) arg);
2186 #endif
2187                 break;
2188
2189         case LR388K7_IOCTL_GET_VALUE:
2190                 ret = lr388k7_get_value((u8 *)arg, index);
2191                 break;
2192
2193         case LR388K7_IOCTL_SET_CLK_SEL:
2194 #if defined(ACTIVE_ENABLE)
2195                 lr388k7_set_clk_sel((unsigned int)arg);
2196 #endif
2197                 break;
2198
2199         case LR388K7_IOCTL_SET_STATE:
2200                 g_u8_scan = (u8)arg;
2201                 break;
2202
2203 #if defined(UDEV_ENABLE)
2204         case LR388K7_IOCTL_SEND_UDEV:
2205                 lr388k7_send_udev_event((void *)arg);
2206                 break;
2207 #endif
2208
2209         default:
2210                 ret = false;
2211                 break;
2212         }
2213
2214         return ret;
2215 }
2216
2217 static void lr388k7_init_parameter(void)
2218 {
2219         unsigned long flags;
2220         g_u8_mode = K7_DEFAULT_MODE;
2221         g_u8_scan = K7_SCAN_STATE_IDLE;
2222         g_st_state.u32_pid = 0;
2223         g_st_state.b_is_init_finish = false;
2224         g_st_state.b_is_suspended = false;
2225         g_st_state.b_is_reset = false;
2226         g_st_state.b_is_disabled = false;
2227         g_st_state.u32SCK = 0;
2228         lr388k7_queue_reset();
2229
2230         g_st_dbg.slowscan.enable = 0;
2231         g_st_dbg.slowscan.scan_rate = K7_SLOW_SCAN_60;
2232         g_st_dbg.wakeup.enable = 0;
2233         g_st_dbg.wakeup.num_tap = K7_NUM_TAP_2;
2234         g_st_dbg.u8ForceCap = 0;
2235         g_st_dbg.u8Dump = 0;
2236         g_st_dbg.u8Test = 0;
2237         g_u16_wakeup_enable = 0;
2238
2239         access_num = 0;
2240
2241         spin_lock_irqsave(&lr388k7log_lock, flags);
2242         g_log_front = 0;
2243         g_log_rear = 0;
2244         spin_unlock_irqrestore(&lr388k7log_lock, flags);
2245 }
2246
2247 static void lr388k7_init_ts(void)
2248 {
2249         lr388k7_init_parameter();
2250         memset(&g_st_state, 0, sizeof(struct lr388k7_ts_parameter));
2251
2252         g_st_state.st_wq_k7 = create_singlethread_workqueue("lr388k7_work");
2253         INIT_WORK(&g_st_state.st_work_k7, lr388k7_work_handler);
2254 }
2255
2256 static struct lr388k7_platform_data *lr388k7_parse_dt(struct device *dev,
2257                                                       int irq)
2258 {
2259         struct lr388k7_platform_data *pdata;
2260         struct device_node *np = dev->of_node;
2261         int ret, val, irq_gpio;
2262         const char *str;
2263
2264         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2265         if (!pdata)
2266                 return ERR_PTR(-ENOMEM);
2267
2268         pdata->gpio_reset = of_get_named_gpio_flags(np, "reset-gpio", 0, NULL);
2269         if (!gpio_is_valid(pdata->gpio_reset)) {
2270                 dev_err(dev, "Invalid reset-gpio\n");
2271                 return ERR_PTR(-EINVAL);
2272         }
2273         ret = gpio_request(pdata->gpio_reset, "reset-gpio");
2274         if (ret < 0) {
2275                 dev_err(dev, "gpio_request failed\n");
2276                 return ERR_PTR(-EINVAL);
2277         }
2278         gpio_direction_output(pdata->gpio_reset, 0);
2279
2280         ret = of_property_read_u32(np, "interrupts", &irq_gpio);
2281         if (!gpio_is_valid(irq_gpio)) {
2282                 dev_err(dev, "Invalid irq-gpio\n");
2283                 ret = -EINVAL;
2284                 goto exit_release_reset_gpio;
2285         }
2286         ret = gpio_request(irq_gpio, "irq-gpio");
2287         if (ret < 0) {
2288                 dev_err(dev, "irq_request fail\n");
2289                 ret = -EINVAL;
2290                 goto exit_release_reset_gpio;
2291         }
2292         gpio_direction_input(irq_gpio);
2293         pdata->gpio_irq = irq_gpio;
2294
2295         ret = of_property_read_u32(np, "x-max", &val);
2296         if (ret < 0)
2297                 goto exit_release_all_gpio;
2298         pdata->ts_x_max = val;
2299
2300         ret = of_property_read_u32(np, "y-max", &val);
2301         if (ret < 0)
2302                 goto exit_release_all_gpio;
2303         pdata->ts_y_max = val;
2304
2305         ret = of_property_read_u32(np, "z-max", &val);
2306         if (ret < 0)
2307                 goto exit_release_all_gpio;
2308         pdata->ts_pressure_max = val;
2309
2310         ret = of_property_read_u32(np, "touch-num-max", &val);
2311         if (ret < 0)
2312                 goto exit_release_all_gpio;
2313         pdata->ts_touch_num_max = val;
2314
2315         ret = of_property_read_string(np, "name-of-clock", &str);
2316         if (ret < 0)
2317                 goto exit_release_all_gpio;
2318         pdata->name_of_clock = (char *)str;
2319
2320         ret = of_property_read_string(np, "name-of-clock-con", &str);
2321         if (ret < 0)
2322                 goto exit_release_all_gpio;
2323         pdata->name_of_clock_con = (char *)str;
2324
2325 #if defined(ACTIVE_ENABLE)
2326         pdata->gpio_clk_sel = of_get_named_gpio_flags(np,
2327                                                       "clock-sel-gpio",
2328                                                       0,
2329                                                       NULL);
2330         if (!gpio_is_valid(pdata->gpio_clk_sel)) {
2331                 dev_err(dev, "Invalid clock-sel-gpio\n");
2332                 return ERR_PTR(-EINVAL);
2333         }
2334         ret = gpio_request(pdata->gpio_clk_sel, "clock-sel-gpio");
2335         if (ret < 0) {
2336                 dev_err(dev, "gpio_request failed\n");
2337                 return ERR_PTR(-EINVAL);
2338         }
2339         gpio_direction_output(pdata->gpio_clk_sel, 0);
2340 #endif
2341
2342         ret = of_property_read_u32(np, "platform-id", &val);
2343         if (ret < 0)
2344                 goto exit_release_all_gpio;
2345         pdata->platform_id = val;
2346
2347         return pdata;
2348
2349  exit_release_all_gpio:
2350         gpio_free(irq_gpio);
2351  exit_release_reset_gpio:
2352         gpio_free(pdata->gpio_reset);
2353         return ERR_PTR(ret);
2354 }
2355
2356 static void init_spi_pinctrl(struct lr388k7 *ts, struct device *dev)
2357 {
2358         struct pinctrl *pin = NULL;
2359         struct pinctrl_state *active, *inactive;
2360
2361         pin = devm_pinctrl_get(dev);
2362         if (IS_ERR(pin)) {
2363                 dev_err(dev, "missing pinctrl device\n");
2364                 return;
2365         }
2366         ts->pinctrl = pin;
2367
2368         active = pinctrl_lookup_state(pin, "spi_intf_normal");
2369         if (IS_ERR_OR_NULL(active)) {
2370                 dev_err(dev, "missing spi_intf_normal state\n");
2371                 goto out;
2372         }
2373         ts->spi_intf_en = active;
2374         inactive = pinctrl_lookup_state(pin, "spi_intf_lowpower");
2375         if (IS_ERR_OR_NULL(active)) {
2376                 dev_err(dev, "missing spi_intf_lowpower state\n");
2377                 goto out;
2378         }
2379         ts->spi_intf_dis = inactive;
2380
2381         return;
2382 out:
2383         if (ts->pinctrl)
2384                 ts->pinctrl = NULL;
2385
2386 }
2387
2388 static int lr388k7_probe(struct spi_device *spi)
2389 {
2390         struct lr388k7_platform_data *pdata;/* = spi->dev.platform_data;*/
2391         struct lr388k7 *ts;
2392         struct input_dev *input_dev;
2393 #if defined(ACTIVE_ENABLE)
2394         struct input_dev *input_dev_active;
2395 #endif
2396         struct device *dev;
2397         int error;
2398
2399         dev = &spi->dev;
2400         dev_info(dev, "[ENTER] probe\n");
2401
2402         lr388k7_init_ts();
2403
2404         g_spi = spi;
2405
2406         if (spi->irq <= 0) {
2407                 dev_dbg(dev, "no irq\n");
2408                 return -ENODEV;
2409         }
2410
2411         spi->mode = SPI_MODE_3;
2412         spi->bits_per_word = 8;
2413         if (!spi->max_speed_hz)
2414                 spi->max_speed_hz = K7_MAX_SPEED_HZ;
2415
2416         error = spi_setup(spi);
2417         if (error)
2418                 return error;
2419
2420         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
2421         input_dev = input_allocate_device();
2422 #if defined(ACTIVE_ENABLE)
2423         input_dev_active = input_allocate_device();
2424         if (!ts || !input_dev || !input_dev_active) {
2425 #else
2426         if (!ts || !input_dev) {
2427 #endif
2428                 error = -ENOMEM;
2429                 goto err_free_mem;
2430         }
2431
2432
2433         ts->spi = spi;
2434         ts->dev = dev;
2435         ts->idev = input_dev;
2436 #if defined(ACTIVE_ENABLE)
2437         ts->idev_active = input_dev_active;
2438 #endif
2439         ts->irq = spi->irq;
2440
2441         if (dev->of_node) {
2442
2443                 dev_info(dev, "DT support\n");
2444                 /* Loading data from DT */
2445                 pdata = lr388k7_parse_dt(dev, spi->irq);
2446                 if (IS_ERR(pdata)) {
2447                         dev_err(dev, "failed to parse device tree\n");
2448                         error = -EINVAL;
2449                         goto err_free_mem;
2450                 }
2451                 dev->platform_data = pdata;
2452         } else {
2453                 /* Non-DT */
2454                 dev_info(dev, "Non-DT\n");
2455         }
2456
2457         pdata = dev->platform_data;
2458
2459         ts->max_num_touch = __min(pdata->ts_touch_num_max, K7_MAX_TOUCH_NUM);
2460         ts->max_x       = pdata->ts_x_max ? : MAX_16BIT;
2461         ts->max_y       = pdata->ts_y_max ? : MAX_16BIT;
2462         ts->max_z       = pdata->ts_pressure_max ? : MAX_16BIT;
2463         ts->swap_xy     = false;
2464         ts->flip_x      = false;
2465         ts->flip_y      = false;
2466         ts->gpio_reset  = pdata->gpio_reset;
2467         ts->gpio_irq  = pdata->gpio_irq;
2468         ts->b_eraser_active = false;
2469 #if defined(ACTIVE_ENABLE)
2470         ts->tool        = 0;
2471         ts->gpio_clk_sel = pdata->gpio_clk_sel;
2472 #endif
2473         ts->platform_id = pdata->platform_id;
2474
2475         mutex_init(&ts->mutex);
2476
2477         spin_lock_init(&ts->lock);
2478
2479         snprintf(ts->phys,
2480                  sizeof(ts->phys),
2481                  "%s/input-ts",
2482                  dev_name(dev));
2483
2484         /* misc */
2485         if (misc_register(&lr388k7_ts_miscdev) != 0) {
2486                 dev_err(dev, "cannot register miscdev\n");
2487                 error = -ENOMEM;
2488                 goto err_free_mem;
2489         }
2490
2491 #if defined(DEBUG_LR388K7)
2492         dev_info(&spi->dev, "Success register miscdev\n");
2493 #endif
2494
2495         if (lr388k7_queue_init()) {
2496                 dev_err(dev, "Cannot allocate queue memory\n");
2497                 error = -ENOMEM;
2498                 goto err_free_mem;
2499         }
2500
2501 #if defined(ACTIVE_ENABLE)
2502         /* Select external clock */
2503         gpio_set_value(ts->gpio_clk_sel, 0);
2504 #endif
2505         /* Reset assert */
2506         gpio_set_value(ts->gpio_reset, 0);
2507         g_st_state.b_is_reset = true;
2508
2509         init_spi_pinctrl(ts, dev);
2510
2511         /* regulator */
2512         ts->regulator_3v3 = devm_regulator_get(&g_spi->dev, "avdd");
2513         if (IS_ERR(ts->regulator_3v3)) {
2514                 dev_err(dev,
2515                         "LR388K7 TS: regulator_get failed: %ld\n",
2516                         PTR_ERR(ts->regulator_3v3));
2517                 return -ENODEV;
2518         }
2519
2520         ts->regulator_1v8 = devm_regulator_get(&g_spi->dev, "dvdd");
2521         if (IS_ERR(ts->regulator_1v8)) {
2522                 dev_err(dev,
2523                         "LR388K7 TS: regulator_get failed: %ld\n",
2524                         PTR_ERR(ts->regulator_1v8));
2525                 return -ENODEV;
2526         }
2527
2528         /* Enable 1v8 first*/
2529         error = regulator_enable(ts->regulator_1v8);
2530         if (error < 0)
2531                 dev_err(dev,
2532                         "LR388K7 TS: regulator enable failed: %d\n",
2533                         error);
2534         usleep_range(5000, 6000);
2535         error = regulator_enable(ts->regulator_3v3);
2536         if (error < 0)
2537                 dev_err(dev,
2538                         "LR388K7 TS: regulator enable failed: %d\n",
2539                         error);
2540
2541         /* clk */
2542         if (pdata->name_of_clock || pdata->name_of_clock_con) {
2543                 ts->clk =
2544                         clk_get_sys(pdata->name_of_clock,
2545                                     pdata->name_of_clock_con);
2546                 if (IS_ERR(ts->clk)) {
2547                         dev_err(dev,
2548                                 "failed to get touch_clk:(%s, %s)\n",
2549                                 pdata->name_of_clock,
2550                                 pdata->name_of_clock_con);
2551                         error = -EINVAL;
2552                         goto err_free_mem;
2553                 }
2554         }
2555
2556         if (ts->clk)
2557                 clk_enable(ts->clk);
2558
2559         input_dev->name = "touch";
2560         input_dev->phys = ts->phys;
2561         input_dev->id.bustype = BUS_SPI;
2562         input_dev->dev.parent = &spi->dev;
2563         __set_bit(EV_SYN, input_dev->evbit);
2564         __set_bit(EV_ABS, input_dev->evbit);
2565         __set_bit(EV_KEY, input_dev->evbit);
2566         __set_bit(KEY_POWER, input_dev->keybit);
2567
2568         if (ts->swap_xy) {
2569                 input_set_abs_params(input_dev,
2570                                      ABS_MT_POSITION_X,
2571                                      0,
2572                                      ts->max_y,
2573                                      0,
2574                                      0);
2575                 input_set_abs_params(input_dev,
2576                                      ABS_MT_POSITION_Y,
2577                                      0,
2578                                      ts->max_x,
2579                                      0,
2580                                      0);
2581         } else {
2582                 input_set_abs_params(input_dev,
2583                                      ABS_MT_POSITION_X,
2584                                      0,
2585                                      ts->max_x,
2586                                      0,
2587                                      0);
2588                 input_set_abs_params(input_dev,
2589                                      ABS_MT_POSITION_Y,
2590                                      0, ts->max_y,
2591                                      0,
2592                                      0);
2593         }
2594         input_set_abs_params(input_dev,
2595                              ABS_MT_PRESSURE,
2596                              0,
2597                              ts->max_z,
2598                              0,
2599                              0);
2600         input_set_abs_params(input_dev,
2601                              ABS_MT_TOOL_TYPE,
2602                              0,
2603                              MT_TOOL_MAX,
2604                              0,
2605                              0);
2606 #if defined(PROTOCOL_A)
2607         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
2608
2609         input_set_abs_params(input_dev,
2610                              ABS_MT_TRACKING_ID,
2611                              0,
2612                              ts->max_num_touch,
2613                              0,
2614                              0);
2615 #else /* PROTOCOL B */
2616         error = input_mt_init_slots(input_dev,
2617                                     ts->max_num_touch,
2618                                     0);
2619         if (error) {
2620                 dev_err(dev, "Failed to mt_init_slots err: %d\n", error);
2621                 goto err_free_mem;
2622         }
2623
2624 #endif
2625
2626         input_set_capability(input_dev, EV_MSC, MSC_ACTIVITY);
2627
2628         input_dev->open = lr388k7_open;
2629         input_dev->close = lr388k7_close;
2630         input_dev->enable = lr388k7_input_enable;
2631         input_dev->disable = lr388k7_input_disable;
2632         input_dev->enabled = true;
2633         input_set_drvdata(input_dev, ts);
2634
2635 #if defined(ACTIVE_ENABLE)
2636         input_dev_active->name = "touch_active";
2637         input_dev_active->phys = ts->phys;
2638         input_dev_active->id.bustype = BUS_SPI;
2639         input_dev_active->dev.parent = &spi->dev;
2640
2641         __set_bit(EV_ABS, input_dev_active->evbit);
2642         __set_bit(EV_KEY, input_dev_active->evbit);
2643         __set_bit(INPUT_PROP_POINTER, input_dev_active->propbit);
2644         __set_bit(BTN_TOUCH, input_dev_active->keybit);
2645         __set_bit(BTN_TOOL_PEN, input_dev_active->keybit);
2646         __set_bit(BTN_TOOL_RUBBER, input_dev_active->keybit);
2647         __set_bit(BTN_STYLUS, input_dev_active->keybit);
2648
2649         if (ts->swap_xy) {
2650                 input_set_abs_params(input_dev_active,
2651                                      ABS_X,
2652                                      0,
2653                                      ts->max_y,
2654                                      0,
2655                                      0);
2656                 input_set_abs_params(input_dev_active,
2657                                      ABS_Y,
2658                                      0,
2659                                      ts->max_x,
2660                                      0,
2661                                      0);
2662         } else {
2663                 input_set_abs_params(input_dev_active,
2664                                      ABS_X,
2665                                      0,
2666                                      ts->max_x,
2667                                      0,
2668                                      0);
2669                 input_set_abs_params(input_dev_active,
2670                                      ABS_Y,
2671                                      0, ts->max_y,
2672                                      0,
2673                                      0);
2674         }
2675         input_set_abs_params(input_dev_active,
2676                              ABS_PRESSURE,
2677                              0,
2678                              MAX_10BIT,
2679                              0,
2680                              0);
2681         input_set_drvdata(input_dev_active, ts);
2682 #endif
2683
2684         error = request_threaded_irq(spi->irq,
2685                                      lr388k7_irq_thread,
2686                                      lr388k7_wakeup_thread,
2687                                      IRQF_TRIGGER_FALLING,
2688                                      "lr388k7_ts", ts);
2689
2690         if (error) {
2691                 dev_err(dev, "Failed to request irq, err: %d\n", error);
2692                 goto err_free_mem;
2693         }
2694
2695         disable_irq(ts->irq);
2696
2697         spi_set_drvdata(spi, ts);
2698
2699         error = sysfs_create_link(&dev->kobj,
2700                                   &lr388k7_ts_miscdev.this_device->kobj,
2701                                   "touch");
2702         if (error) {
2703                 dev_err(dev, "failed to create sysfs group\n");
2704                 goto err_clear_drvdata;
2705         }
2706
2707         error = input_register_device(ts->idev);
2708         if (error) {
2709                 dev_err(dev,
2710                         "Failed to register input device, err: %d\n", error);
2711                 goto err_clear_drvdata;
2712         }
2713
2714 #if defined(ACTIVE_ENABLE)
2715         error = input_register_device(ts->idev_active);
2716         if (error) {
2717                 dev_err(dev,
2718                         "Failed to register input active device , err: %d\n",
2719                         error);
2720                 goto err_clear_drvdata;
2721         }
2722 #endif
2723
2724         error = sysfs_create_group(&lr388k7_ts_miscdev.this_device->kobj,
2725                                    &lr388k7_ts_attr_group);
2726         if (error) {
2727                 dev_err(dev, "failed to create sysfs group\n");
2728                 goto err_clear_drvdata;
2729         }
2730
2731         /* Enable async suspend/resume to reduce LP0 latency */
2732         device_enable_async_suspend(dev);
2733
2734         dev_info(dev, "[EXIT] probe\n");
2735
2736 #ifdef CONFIG_TOUCHSCREEN_NVTOUCH
2737         nvtouch_kernel_init(58, 37, 15360, 9600, 0, 1, input_dev);
2738         pr_info("NvTouch init completed\n");
2739 #endif
2740         return 0;
2741
2742  err_clear_drvdata:
2743         spi_set_drvdata(spi, NULL);
2744         free_irq(spi->irq, ts);
2745  err_free_mem:
2746 #if defined(ACTIVE_ENABLE)
2747         input_free_device(input_dev_active);
2748 #endif
2749         input_free_device(input_dev);
2750         kfree(ts);
2751         return error;
2752 }
2753
2754 static int lr388k7_remove(struct spi_device *spi)
2755 {
2756         struct lr388k7 *ts = spi_get_drvdata(spi);
2757
2758         lr388k7_queue_free();
2759
2760         if (g_st_state.st_wq_k7)
2761                 destroy_workqueue(g_st_state.st_wq_k7);
2762
2763         sysfs_remove_group(&lr388k7_ts_miscdev.this_device->kobj,
2764                            &lr388k7_ts_attr_group);
2765
2766         sysfs_remove_link(&ts->dev->kobj, "touch");
2767
2768         misc_deregister(&lr388k7_ts_miscdev);
2769         free_irq(ts->spi->irq, ts);
2770
2771         gpio_free(ts->gpio_reset);
2772         gpio_free(ts->gpio_irq);
2773 #if defined(ACTIVE_ENABLE)
2774         gpio_free(ts->gpio_clk_sel);
2775 #endif
2776         input_unregister_device(ts->idev);
2777
2778         kfree(ts);
2779
2780         spi_set_drvdata(spi, NULL);
2781         return 0;
2782 }
2783
2784 static void lr388k7_shutdown(struct spi_device *spi)
2785 {
2786         struct lr388k7 *ts = spi_get_drvdata(spi);
2787
2788         free_irq(ts->irq, ts);
2789
2790         if (ts->regulator_3v3)
2791                 regulator_disable(ts->regulator_3v3);
2792
2793         if (ts->regulator_1v8)
2794                 regulator_disable(ts->regulator_1v8);
2795 }
2796
2797 static void lr388k7_ctrl_resume(struct lr388k7 *ts)
2798 {
2799         u8 u8_buf[5];
2800         size_t count = 0;
2801
2802         u8_buf[count++] = K7_WR_OPCODE;
2803         u8_buf[count++] = (K7_STATE_CTL_ADDR >> 16) & 0xFF;
2804         u8_buf[count++] = (K7_STATE_CTL_ADDR >>  8) & 0xFF;
2805         u8_buf[count++] = (K7_STATE_CTL_ADDR >>  0) & 0xFF;
2806         u8_buf[count++] = K7_POWER_CTL_RESUME;
2807         lr388k7_spi_write(u8_buf, count);
2808 }
2809
2810 static void lr388k7_start(struct lr388k7 *ts)
2811 {
2812         int error;
2813
2814         g_st_state.b_is_suspended = false;
2815
2816         mutex_lock(&ts->mutex);
2817
2818         if (g_st_dbg.wakeup.enable == 1) {
2819                 lr388k7_ctrl_resume(ts);
2820                 mutex_unlock(&ts->mutex);
2821                 return;
2822         }
2823
2824         /* Reset assert */
2825         gpio_set_value(ts->gpio_reset, 0);
2826         g_st_state.b_is_reset = true;
2827
2828         /*
2829          * Enable regulator, if necessary
2830          */
2831         /* Enable 1v8 first*/
2832         error = regulator_enable(ts->regulator_1v8);
2833         if (error < 0)
2834                 dev_err(&g_spi->dev,
2835                         "LR388K7 TS: regulator enable failed: %d\n", error);
2836
2837         error = regulator_enable(ts->regulator_3v3);
2838         if (error < 0)
2839                 dev_err(&g_spi->dev,
2840                         "LR388K7 TS: regulator enable failed: %d\n", error);
2841
2842         usleep_range(5000, 6000);
2843
2844         if (ts->spi_intf_en)
2845                 pinctrl_select_state(ts->pinctrl,
2846                         ts->spi_intf_en);
2847
2848         /*
2849          * Enable clock, if necessary
2850          */
2851         if (ts->clk)
2852                 clk_enable(ts->clk);
2853
2854         /* Reset deassert */
2855         gpio_set_value(ts->gpio_reset, 1);
2856         g_st_state.b_is_reset = false;
2857
2858         usleep_range(12000, 13000);
2859
2860         mutex_unlock(&ts->mutex);
2861 }
2862
2863 static void lr388k7_ctrl_suspend(struct lr388k7 *ts)
2864 {
2865         u8 u8_buf[5];
2866         size_t count = 0;
2867         int error;
2868         u8 u8_status = 0;
2869         int irq_value;
2870
2871         mutex_lock(&ts->mutex);
2872
2873         if (g_st_dbg.wakeup.enable == 1) {
2874                 u8_buf[count++] = K7_WR_OPCODE;
2875                 u8_buf[count++] = (K7_STATE_CTL_ADDR >> 16) & 0xFF;
2876                 u8_buf[count++] = (K7_STATE_CTL_ADDR >>  8) & 0xFF;
2877                 u8_buf[count++] = (K7_STATE_CTL_ADDR >>  0) & 0xFF;
2878                 u8_buf[count++] = K7_POWER_CTL_TAP_WAKEUP;
2879                 lr388k7_spi_write(u8_buf, count);
2880
2881                 irq_value = gpio_get_value(ts->gpio_irq);
2882
2883                 if (!irq_value) {
2884                         u8_status = lr388k7_clear_irq();
2885
2886                         lr388k7_event_handler(u8_status);
2887                 }
2888
2889                 mutex_unlock(&ts->mutex);
2890                 return;
2891         }
2892
2893         if (ts->spi_intf_dis)
2894                 pinctrl_select_state(ts->pinctrl,
2895                                 ts->spi_intf_dis);
2896
2897         /* Disable (3.3V) */
2898         if (ts->regulator_3v3) {
2899                 error = regulator_disable(ts->regulator_3v3);
2900                 if (error < 0)
2901                         dev_err(&g_spi->dev,
2902                                 "lr388k7 regulator 3.3V disable failed: %d\n",
2903                                 error);
2904         }
2905         /* handle platforms w/ and w/out regulator switches */
2906         /* 2) delay for platforms w/ regulator switches */
2907         /* usleep_range(15000, 20000);  */ /*msleep(15); */
2908         /* 3) disable clock */
2909         if (ts->clk)
2910                 clk_disable(ts->clk);
2911         /* 4) disable 1.8 */
2912         if (ts->regulator_1v8 && ts->regulator_3v3) {
2913                 error = regulator_disable(ts->regulator_1v8);
2914                 if (error < 0)
2915                         dev_err(&g_spi->dev,
2916                                 "lr388k7 1.8V disable failed: %d\n",
2917                                 error);
2918         }
2919
2920         /* Reset assert */
2921         gpio_set_value(ts->gpio_reset, 0);
2922         g_st_state.b_is_reset = true;
2923
2924         mutex_unlock(&ts->mutex);
2925 }
2926
2927 static void lr388k7_stop(struct lr388k7 *ts)
2928 {
2929         flush_workqueue(g_st_state.st_wq_k7);
2930
2931         lr388k7_ctrl_suspend(ts);
2932 }
2933
2934 static int lr388k7_suspend(struct lr388k7 *ts)
2935 {
2936
2937         lr388k7_stop(ts);
2938
2939         lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_SLEP);
2940
2941         g_st_state.b_is_suspended = true;
2942
2943         return 0;
2944 }
2945
2946 static int lr388k7_resume(struct lr388k7 *ts)
2947 {
2948         u8 u8_status = 0;
2949         int irq_value;
2950
2951         lr388k7_start(ts);
2952
2953         lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_WAKE);
2954
2955
2956         if (g_st_dbg.wakeup.enable == 2)
2957                 g_st_dbg.wakeup.enable = g_u16_wakeup_enable;
2958
2959         if (IS_DBG) {
2960                 g_st_dbg.u8ForceCap = 0;
2961                 g_st_dbg.u8Dump = 0;
2962                 g_st_dbg.slowscan.enable = 0;
2963
2964                 lr388k7_send_signal(g_st_state.u32_pid, LR388K7_SIGNAL_CTRL);
2965         }
2966
2967         if (g_st_dbg.wakeup.enable == 0)
2968                 return 0;
2969
2970         /* check irq */
2971         irq_value = gpio_get_value(ts->gpio_irq);
2972         if (!irq_value) {
2973
2974                 u8_status = lr388k7_clear_irq();
2975
2976                 while (u8_status) {
2977
2978                         lr388k7_event_handler(u8_status);
2979                         irq_value = gpio_get_value(ts->gpio_irq);
2980
2981                         if (!irq_value)
2982                                 u8_status = lr388k7_clear_irq();
2983                         else
2984                                 u8_status = 0;
2985                 }
2986         }
2987
2988         return 0;
2989 }
2990
2991 #ifdef CONFIG_PM
2992 static int lr388k7_dev_pm_suspend(struct device *dev)
2993 {
2994         struct lr388k7 *ts = dev_get_drvdata(dev);
2995
2996         if (!g_st_state.b_is_suspended && !g_st_state.b_is_disabled) {
2997                 /* only called when input device is not disabled/enabled via
2998                  * /sys/class/input/input0/enabled interface.
2999                  * Android uses sysfs by default and will not run into here
3000                  */
3001                 lr388k7_suspend(ts);
3002 #if defined(CONFIG_ANDROID)
3003                 dev_info(ts->dev, "disabled without input powerhal support.\n");
3004 #endif
3005         }
3006         if (g_st_dbg.wakeup.enable == 1)
3007                 enable_irq_wake(ts->irq);
3008
3009         return 0;
3010 }
3011
3012 static int lr388k7_dev_pm_resume(struct device *dev)
3013 {
3014         struct lr388k7 *ts = dev_get_drvdata(dev);
3015
3016         if (!g_st_state.b_is_disabled) {
3017                 /* only called when input device is not disabled/enabled via
3018                  * /sys/class/input/input0/enabled interface.
3019                  * Android uses sysfs by default and will not run into here
3020                  */
3021                 lr388k7_resume(ts);
3022 #if defined(CONFIG_ANDROID)
3023                 dev_info(ts->dev, "enabled without input powerhal support.\n");
3024 #endif
3025         }
3026         if (g_st_dbg.wakeup.enable == 1)
3027                 disable_irq_wake(ts->irq);
3028
3029         return 0;
3030 }
3031 #endif /*CONFIG_PM */
3032
3033 static int lr388k7_input_enable(struct input_dev *idev)
3034 {
3035         struct lr388k7 *ts = input_get_drvdata(idev);
3036         int err = 0;
3037 #if defined(DEBUG_LR388K7)
3038         dev_info(ts->dev, "[ENTER] input enable\n");
3039 #endif
3040
3041         g_st_state.b_is_disabled = false;
3042         err = lr388k7_resume(ts);
3043
3044 #if defined(DEBUG_LR388K7)
3045         dev_info(ts->dev, "[EXIT] input enable\n");
3046 #endif
3047         return 0;
3048 }
3049
3050 static int lr388k7_input_disable(struct input_dev *idev)
3051 {
3052         struct lr388k7 *ts = input_get_drvdata(idev);
3053         int err = 0;
3054
3055 #if defined(DEBUG_LR388K7)
3056         dev_info(ts->dev, "[ENTER] input disable\n");
3057 #endif
3058
3059         err = lr388k7_suspend(ts);
3060         g_st_state.b_is_disabled = true;
3061
3062 #if defined(DEBUG_LR388K7)
3063         dev_info(ts->dev, "[EXIT] input disable\n");
3064 #endif
3065         return 0;
3066 }
3067
3068 #if defined(CONFIG_PM)
3069 static const struct dev_pm_ops lr388k7_pm_ops = {
3070         .suspend = lr388k7_dev_pm_suspend,
3071         .resume = lr388k7_dev_pm_resume,
3072 };
3073 #endif
3074
3075 static const struct of_device_id lr388k7_ts_match[] = {
3076         { .compatible = "sharp,lr388k7_ts" },
3077         { },
3078 };
3079 MODULE_DEVICE_TABLE(of, lr388k7_ts_match);
3080
3081 static struct spi_driver lr388k7_driver = {
3082         .driver = {
3083                 .name   = "lr388k7_ts",
3084                 .owner  = THIS_MODULE,
3085 #if defined(CONFIG_PM)
3086                 .pm     = &lr388k7_pm_ops,
3087 #endif
3088                 .of_match_table = lr388k7_ts_match,
3089         },
3090         .probe  = lr388k7_probe,
3091         .remove = lr388k7_remove,
3092         .shutdown = lr388k7_shutdown,
3093 };
3094
3095 module_spi_driver(lr388k7_driver);
3096
3097 MODULE_AUTHOR("Makoto Itsuki <itsuki.makoto@sharp.co.jp>");
3098 MODULE_DESCRIPTION("LR388K7 Touchscreen Driver");
3099 MODULE_LICENSE("GPL");