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