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