]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/input/touchscreen/rm31080a_ts.c
input: touchscreen: raydium: Disable slowscan
[sojka/nv-tegra/linux-3.10.git] / drivers / input / touchscreen / rm31080a_ts.c
1 /*
2  * Raydium RM31080 touchscreen driver
3  *
4  * Copyright (C) 2012-2014, Raydium Semiconductor Corporation.
5  * All Rights Reserved.
6  * Copyright (C) 2012-2014, NVIDIA Corporation.  All Rights Reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  *
13  */
14 /*=============================================================================
15         INCLUDED FILES
16 =============================================================================*/
17 #include <linux/module.h>
18 #include <linux/input.h>        /* BUS_SPI */
19 #include <linux/spi/spi.h>
20 #include <linux/device.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/slab.h>
26 #include <linux/fb.h>
27 #include <linux/gpio.h>
28 #include <linux/of.h>
29 #include <linux/of_device.h>
30 #include <linux/of_gpio.h>
31 #include <linux/sched.h>        /* wake_up_process() */
32 #include <linux/sched/rt.h>
33 #include <linux/kthread.h>      /* kthread_create(),kthread_run() */
34 #include <linux/uaccess.h>      /* copy_to_user() */
35 #include <linux/miscdevice.h>
36 #include <asm/siginfo.h>        /* siginfo */
37 #include <linux/rcupdate.h>     /* rcu_read_lock */
38 #include <linux/sched.h>        /* find_task_by_pid_type */
39 #include <linux/syscalls.h>     /* sys_clock_gettime() */
40 #include <linux/random.h>       /* random32() */
41 #include <linux/suspend.h>      /* pm_notifier */
42 #include <linux/workqueue.h>
43 #include <linux/wakelock.h> /* wakelock */
44 #include <linux/regulator/consumer.h> /* regulator & voltage */
45 #include <linux/clk.h> /* clock */
46 #ifdef CONFIG_HAS_EARLYSUSPEND
47 #include <linux/earlysuspend.h>
48 #endif
49
50 #include <linux/timer.h>
51
52 #include <linux/spi/rm31080a_ts.h>
53 #include <linux/spi/rm31080a_ctrl.h>
54
55 #define CREATE_TRACE_POINTS
56 #include <trace/events/touchscreen_raydium.h>
57
58 #if (INPUT_PROTOCOL_CURRENT_SUPPORT == INPUT_PROTOCOL_TYPE_B)
59 #include <linux/input/mt.h>
60 #endif
61
62 #if ENABLE_FB_CALLBACK && defined(CONFIG_FB)
63 #include <linux/fb.h>
64 #endif
65
66 /*=============================================================================
67         DEFINITIONS
68 =============================================================================*/
69 #define MAX_SPI_FREQ_HZ                 50000000
70 #define TS_PEN_UP_TIMEOUT               msecs_to_jiffies(50)
71
72 #define QUEUE_COUNT                                     128
73 #define RM_RAW_DATA_LENGTH                      6144
74
75 #define RM_SCAN_ACTIVE_MODE                     0x00
76 #define RM_SCAN_PRE_IDLE_MODE           0x01
77 #define RM_SCAN_IDLE_MODE                       0x02
78
79 #define RM_NEED_NONE                            0x00
80 #define RM_NEED_TO_SEND_SCAN            0x01
81 #define RM_NEED_TO_READ_RAW_DATA        0x02
82 #define RM_NEED_TO_SEND_SIGNAL          0x04
83
84 #define TCH_WAKE_LOCK_TIMEOUT           (HZ/2)
85
86 #if ENABLE_FREQ_HOPPING  /*ENABLE_SCAN_DATA_HEADER*/
87 #define QUEUE_HEADER_NUM                        (8)
88 #define SCAN_TYPE_MT                            (1)
89 #else
90 #define QUEUE_HEADER_NUM                        0
91 #endif
92
93 #ifdef ENABLE_SLOW_SCAN
94 #define RM_SLOW_SCAN_INTERVAL           20
95 #define RM_SLOW_SCAN_CMD_COUNT          0x10
96 enum RM_SLOW_SCAN_LEVELS {
97         RM_SLOW_SCAN_LEVEL_NORMAL,
98         RM_SLOW_SCAN_LEVEL_20,
99         RM_SLOW_SCAN_LEVEL_40,
100         RM_SLOW_SCAN_LEVEL_60,
101         RM_SLOW_SCAN_LEVEL_80,
102         RM_SLOW_SCAN_LEVEL_100,
103         RM_SLOW_SCAN_LEVEL_MAX,
104         RM_SLOW_SCAN_LEVEL_COUNT
105 };
106 #endif
107
108 enum RM_TEST_MODE {
109         RM_TEST_MODE_NULL,
110         RM_TEST_MODE_IDLE_SHOW,
111         RM_TEST_MODE_IDLE_LEVEL,
112         RM_TEST_MODE_CALC_TIME_SHOW,
113         RM_TEST_MODE_MAX
114 };
115
116 #ifdef ENABLE_SMOOTH_LEVEL
117 #define RM_SMOOTH_LEVEL_NORMAL          0
118 #define RM_SMOOTH_LEVEL_MAX                     4
119 #endif
120
121 #define TS_TIMER_PERIOD         HZ
122
123 #if (INPUT_PROTOCOL_CURRENT_SUPPORT == INPUT_PROTOCOL_TYPE_B)
124 #define MAX_SUPPORT_SLOT_AMOUNT MAX_REPORT_TOUCHED_POINTS
125 #endif
126
127 /*#define CS_SUPPORT*/
128 #define MASK_USER_SPACE_POINTER 0x00000000FFFFFFFF      /* 64-bit support */
129
130 #define ISR_POST_HANDLER WORK_QUEUE                 /*or KTHREAD*/
131 #define WORK_QUEUE      0
132 #define KTHREAD         1
133
134 /* do not use printk in kernel files */
135 #define rm_printk(msg...)       dev_info(&g_spi->dev, msg)
136
137 /*=============================================================================
138         STRUCTURE DECLARATION
139 =============================================================================*/
140 /* TouchScreen Parameters */
141 struct rm31080a_ts_para {
142         u32 u32_hal_pid;
143         bool b_init_finish;
144         bool b_calc_finish;
145         bool b_enable_scriber;
146         bool b_is_suspended;
147         bool b_init_service;
148
149         u32 u32_watch_dog_cnt;
150         u8 u8_watch_dog_flg;
151         u8 u8_watch_dog_enable;
152         bool b_watch_dog_check;
153         u32 u32_watch_dog_time;
154
155         u8 u8_scan_mode_state;
156         u8 u8_resume_cnt;
157
158 #ifdef ENABLE_SLOW_SCAN
159         bool b_enable_slow_scan;
160         bool b_slow_scan_flg;
161         u32 u32_slow_scan_level;
162 #endif
163
164         u8 u8_touchfile_check;
165         u8 u8_touch_event;
166
167 #ifdef ENABLE_SMOOTH_LEVEL
168         u32 u32_smooth_level;
169 #endif
170         bool b_selftest_enable;
171         u8 u8_selftest_status;
172         u8 u8_selftest_result;
173         u8 u8_version;
174         u8 u8_test_version;
175         u8 u8_repeat;
176         u16 u16_read_para;
177         u8 u8_spi_locked;
178         u8 u8_test_mode;
179         u8 u8_test_mode_type;
180 #if ENABLE_FREQ_HOPPING
181         u8 u8_ns_para[9];
182         u8 u8_ns_mode;
183         u8 u8_ns_sel;
184         u8 u8_ns_rpt;
185 #endif
186         struct wake_lock wakelock_initialization;
187
188         struct mutex mutex_scan_mode;
189         struct mutex mutex_ns_mode;
190
191 #if (ISR_POST_HANDLER == WORK_QUEUE)
192         struct workqueue_struct *rm_workqueue;
193         struct work_struct rm_work;
194 #elif (ISR_POST_HANDLER == KTHREAD)
195         struct mutex mutex_irq_wait;
196         bool b_irq_is_waited;
197         struct task_struct *rm_irq_post_thread;
198         struct sched_param      thread_sched;
199         bool b_thread_alive;
200         bool b_thread_active;
201         wait_queue_head_t rm_thread_wait_q;
202 #endif
203
204         struct workqueue_struct *rm_timer_workqueue;
205         struct work_struct rm_timer_work;
206 };
207
208 struct rm_tch_ts {
209         const struct rm_tch_bus_ops *bops;
210         struct device *dev;
211         struct input_dev *input;
212         unsigned int irq;
213         char phys[32];
214         struct mutex access_mutex;
215 #ifdef CONFIG_HAS_EARLYSUSPEND
216         struct early_suspend early_suspend;
217 #endif
218         struct regulator *regulator_3v3;
219         struct regulator *regulator_1v8;
220         struct notifier_block nb_3v3;
221         struct notifier_block nb_1v8;
222         struct clk *clk;
223         unsigned char u8_repeat_counter;
224 #if ENABLE_FB_CALLBACK && defined(CONFIG_FB)
225         struct notifier_block  fb_notifier;
226 #endif
227 };
228
229 struct rm_tch_bus_ops {
230         u16 bustype;
231         int (*read) (struct device *dev, u8 reg);
232         int (*write) (struct device *dev, u8 reg, u16 val);
233 };
234
235 struct rm_tch_queue_info {
236         u8(*p_u8_queue)[RM_RAW_DATA_LENGTH];
237         u16 u16_front;
238         u16 u16_rear;
239 };
240
241 /*=============================================================================
242         GLOBAL VARIABLES DECLARATION
243 =============================================================================*/
244 struct input_dev *g_input_dev;
245 struct spi_device *g_spi;
246 struct rm31080a_ts_para g_st_ts;
247 struct rm_tch_queue_info g_st_q;
248
249 bool g_timer_queue_is_flush;
250 #if (ISR_POST_HANDLER == WORK_QUEUE)
251 bool g_worker_queue_is_flush;
252 #endif
253
254 unsigned char *g_pu8_burstread_buf;
255
256 unsigned char g_st_cmd_set_idle[KRL_SIZE_SET_IDLE];
257 unsigned char g_st_cmd_pause_auto[KRL_SIZE_PAUSE_AUTO];
258 unsigned char g_st_rm_resume_cmd[KRL_SIZE_RM_RESUME];
259 unsigned char g_st_rm_suspend_cmd[KRL_SIZE_RM_SUSPEND];
260 unsigned char g_st_rm_readimg_cmd[KRL_SIZE_RM_READ_IMG];
261 unsigned char g_st_rm_watchdog_cmd[KRL_SIZE_RM_WATCHDOG];
262 unsigned char g_st_rm_testmode_cmd[KRL_SIZE_RM_TESTMODE];
263 unsigned char g_st_rm_slow_scan_cmd[KRL_SIZE_RM_SLOWSCAN];
264 unsigned char g_st_rm_clear_int_cmd[KRL_SIZE_RM_CLEARINT];
265 unsigned char g_st_rm_scan_start_cmd[KRL_SIZE_RM_SCANSTART];
266 unsigned char g_st_rm_wait_scan_ok_cmd[KRL_SIZE_RM_WAITSCANOK];
267 unsigned char g_st_rm_set_rep_time_cmd[KRL_SIZE_RM_SETREPTIME];
268 unsigned char g_st_rm_ns_para_cmd[KRL_SIZE_RM_NS_PARA];
269 unsigned char g_st_rm_writeimg_cmd[KRL_SIZE_RM_WRITE_IMAGE];
270 unsigned char g_st_rm_tlk_cmd[KRL_SIZE_RM_TLK];
271 unsigned char g_st_rm_kl_testmode_cmd[KRL_SIZE_RM_KL_TESTMODE];
272
273 int g_service_busy_report_count;
274 struct timer_list ts_timer_triggle;
275
276 static unsigned char g_spi_buf[8192];
277 static unsigned int g_spi_bufsize; /*= 0; remove by checkpatch*/
278 static unsigned char g_spi_addr;
279 bool b_bl_updated;
280 u8 g_u8_update_baseline[RM_RAW_DATA_LENGTH];
281
282 size_t g_u8_test_mode_count;
283 char *g_u8_test_mode_buf;
284
285 /*=============================================================================
286         FUNCTION DECLARATION
287 =============================================================================*/
288 static int rm_tch_cmd_process(u8 u8_sel_case, u8 *p_cmd_tbl,
289                 struct rm_tch_ts *ts);
290 #if (ISR_POST_HANDLER == KTHREAD)
291 static int rm_work_thread_function(void *data);
292 #endif
293 static int rm_tch_read_image_data(unsigned char *p);
294 #ifdef CONFIG_HAS_EARLYSUSPEND
295 static void rm_tch_early_suspend(struct early_suspend *es);
296 static void rm_tch_early_resume(struct early_suspend *es);
297 #endif
298 static int rm_tch_ts_send_signal(int pid, int i_info);
299 static void rm_tch_enter_test_mode(u8 flag);
300 static void rm_ctrl_suspend(struct rm_tch_ts *ts);
301 static void rm_ctrl_resume(struct rm_tch_ts *ts);
302 static void rm_ctrl_watchdog_func(unsigned int u32_enable);
303 static void init_ts_timer(void);
304 static void ts_timer_triggle_function(unsigned long option);
305
306 /*=============================================================================
307          Description:
308                         RM31080 spi interface.
309          Input:
310
311          Output:
312                         1:succeed
313                         0:failed
314 =============================================================================*/
315 static int rm_tch_spi_read(u8 u8addr, u8 *rxbuf, size_t len)
316 {
317         int status;
318         struct spi_message message;
319         struct spi_transfer x[2];
320
321         if (g_st_ts.u8_spi_locked) {
322                 memset(rxbuf, 0, len);
323                 if (g_st_ctrl.u8_kernel_msg & DEBUG_REGISTER)
324                         rm_printk("Raydium - SPI Read Locked!! 0x%x:%d\n",
325                                 u8addr, len);
326                 /*return RETURN_FAIL;*/
327                 return RETURN_OK;
328         }
329
330         spi_message_init(&message);
331         memset(x, 0, sizeof(x));
332
333         u8addr |= 0x80;
334         x[0].len = 1;
335         x[0].tx_buf = &u8addr;
336         spi_message_add_tail(&x[0], &message);
337
338         x[1].len = len;
339         x[1].rx_buf = rxbuf;
340         spi_message_add_tail(&x[1], &message);
341
342         /*It returns zero on succcess,else a negative error code.*/
343         status = spi_sync(g_spi, &message);
344
345         if (g_st_ctrl.u8_kernel_msg & DEBUG_REGISTER)
346                 if (g_st_ts.b_init_finish == 0)
347                         rm_printk("Raydium - READ: addr=0x%2x, value=0x%2x",
348                                 (u8addr&0x7F), rxbuf[0]);
349
350         if (status) {
351                 dev_err(&g_spi->dev, "Raydium - %s : spi_async failed - error %d\n",
352                         __func__, status);
353                 return RETURN_FAIL;
354         }
355
356         return RETURN_OK;
357 }
358
359 /*=============================================================================
360          Description:
361                         RM31080 spi interface.
362          Input:
363
364          Output:
365                         1:succeed
366                         0:failed
367 =============================================================================*/
368 static int rm_tch_spi_write(u8 *txbuf, size_t len)
369 {
370         int status;
371         /*It returns zero on succcess,else a negative error code.*/
372
373         if (g_st_ts.u8_spi_locked) {
374                 if (g_st_ctrl.u8_kernel_msg & DEBUG_REGISTER)
375                         rm_printk("Raydium - SPI write Locked!! 0x%x:0x%x\n",
376                                 txbuf[0], txbuf[1]);
377                 /*return RETURN_FAIL;*/
378                 return RETURN_OK;
379         }
380
381         status = spi_write(g_spi, txbuf, len);
382
383         if (g_st_ctrl.u8_kernel_msg & DEBUG_REGISTER)
384                 if (g_st_ts.b_init_finish == 0)
385                         rm_printk("Raydium - WRITE: addr=0x%2x, value=0x%2x",
386                                 txbuf[0], txbuf[1]);
387
388         if (status) {
389                 dev_err(&g_spi->dev, "Raydium - %s : spi_write failed - error %d\n",
390                         __func__, status);
391                 return RETURN_FAIL;
392         }
393
394         return RETURN_OK;
395 }
396
397 /*=============================================================================
398          Description:
399                         RM31080 spi interface.
400          Input:
401
402          Output:
403                         1:succeed
404                         0:failed
405 =============================================================================*/
406 static int rm_tch_spi_burst_write(u8 reg, u8 *txbuf, size_t len)
407 {
408         int i;
409         u8 *p_u8_Tmp;
410         /*to do: check result*/
411         p_u8_Tmp = kmalloc(len + 1, GFP_KERNEL);
412         p_u8_Tmp[0] = reg;
413         if (p_u8_Tmp == NULL)
414                 return -ENOMEM;
415         for (i = 0; i < len; i++)
416                 p_u8_Tmp[i + 1] = txbuf[i];
417         rm_tch_spi_write(p_u8_Tmp, len + 1);
418         kfree(p_u8_Tmp);
419         return RETURN_OK;
420 }
421
422 /*=============================================================================
423          Description:
424                         RM31080 spi interface.
425          Input:
426
427          Output:
428                         1:succeed
429                         0:failed
430 =============================================================================*/
431 int rm_tch_spi_byte_read(unsigned char u8_addr, unsigned char *p_u8_value)
432 {
433         return rm_tch_spi_read(u8_addr, p_u8_value, 1);
434 }
435
436 /*=============================================================================
437          Description:
438                         RM31080 spi interface.
439          Input:
440
441          Output:
442                         1:succeed
443                         0:failed
444 =============================================================================*/
445 int rm_tch_spi_byte_write(unsigned char u8_addr, unsigned char u8_value)
446 {
447         u8 buf[2];
448         buf[0] = u8_addr;
449         buf[1] = u8_value;
450         return rm_tch_spi_write(buf, 2);
451 }
452
453 /*===========================================================================*/
454 static void rm_tch_generate_event(struct rm_tch_ts *dev_touch,
455                                      enum tch_update_reason reason)
456 {
457         char *envp[2];
458
459         switch (reason) {
460         case STYLUS_DISABLE_BY_WATER:
461                 envp[0] = "STYLUS_DISABLE=Water";
462                 break;
463         case STYLUS_DISABLE_BY_NOISE:
464                 envp[0] = "STYLUS_DISABLE=Noise";
465                 break;
466         case STYLUS_IS_ENABLED:
467                 envp[0] = "STYLUS_DISABLE=None";
468                 break;
469         default:
470                 envp[0] = "STYLUS_DISABLE=Others";
471                 break;
472         }
473         envp[1] = NULL;
474         kobject_uevent_env(&dev_touch->dev->kobj, KOBJ_CHANGE, envp);
475 }
476
477 /*===========================================================================*/
478 void raydium_change_scan_mode(u8 u8_touch_count)
479 {
480         static u32 u32_no_touch_count; /*= 0; remove by checkpatch*/
481         u16 u16_nt_count_thd;
482
483         u16_nt_count_thd = (u16)g_st_ctrl.u8_time2idle * 100;
484
485         if (u8_touch_count) {
486                 u32_no_touch_count = 0;
487                 return;
488         }
489
490         if (u32_no_touch_count < u16_nt_count_thd)
491                 u32_no_touch_count++;
492         else {
493                 mutex_lock(&g_st_ts.mutex_scan_mode);
494                 if (g_st_ts.u8_scan_mode_state == RM_SCAN_ACTIVE_MODE) {
495                         g_st_ts.u8_scan_mode_state = RM_SCAN_PRE_IDLE_MODE;
496                         u32_no_touch_count = 0;
497                 }
498                 mutex_unlock(&g_st_ts.mutex_scan_mode);
499         }
500 }
501
502 #if (INPUT_PROTOCOL_CURRENT_SUPPORT == INPUT_PROTOCOL_TYPE_A)
503 void raydium_report_pointer(void *p)
504 {
505         static unsigned char u8_last_touch_count; /*= 0; remove by checkpatch*/
506         int i;
507         int i_count;
508         int i_max_x, i_max_y;
509         struct rm_touch_event *sp_tp;
510         ssize_t missing;
511
512         sp_tp = kmalloc(sizeof(struct rm_touch_event), GFP_KERNEL);
513         if (sp_tp == NULL)
514                 return;
515
516         missing = copy_from_user(sp_tp, p, sizeof(struct rm_touch_event));
517         if (missing) {
518                 dev_err(&g_spi->dev, "Raydium - %s : copy failed - len:%d, miss:%d\n",
519                         __func__, sizeof(struct rm_touch_event), missing);
520                 kfree(sp_tp);
521                 return;
522         }
523
524         if ((g_st_ctrl.u16_resolution_x != 0) &&
525                 (g_st_ctrl.u16_resolution_y != 0)) {
526                         i_max_x = g_st_ctrl.u16_resolution_x;
527                         i_max_y = g_st_ctrl.u16_resolution_y;
528         } else {
529                         i_max_x = RM_INPUT_RESOLUTION_X;
530                         i_max_y = RM_INPUT_RESOLUTION_Y;
531         }
532
533         i_count = max(u8_last_touch_count, sp_tp->uc_touch_count);
534
535         if (i_count && !sp_tp->uc_touch_count) {
536                 u8_last_touch_count = 0;
537                 input_report_key(g_input_dev,
538                         BTN_TOOL_RUBBER, 0);
539                 input_mt_sync(g_input_dev);
540                 input_sync(g_input_dev);
541         } else if (i_count) {
542                 for (i = 0; i < i_count; i++) {
543                         if (i == MAX_REPORT_TOUCHED_POINTS)
544                                 break;
545
546                         if ((sp_tp->uc_tool_type[i] == POINT_TYPE_ERASER) &&
547                                 (sp_tp->uc_slot[i] & INPUT_POINT_RESET)) {
548                                 input_report_key(g_input_dev,
549                                         BTN_TOOL_RUBBER, 0);
550                                 input_mt_sync(g_input_dev);
551                                 continue;
552                         }
553
554                         if (i < sp_tp->uc_touch_count) {
555                                 switch (sp_tp->uc_tool_type[i]) {
556                                 case POINT_TYPE_FINGER:
557                                         input_report_abs(g_input_dev,
558                                                 ABS_MT_TOOL_TYPE,
559                                                 MT_TOOL_FINGER);
560                                         break;
561                                 case POINT_TYPE_STYLUS:
562                                         input_report_abs(g_input_dev,
563                                                         ABS_MT_TOOL_TYPE,
564                                                         MT_TOOL_PEN);
565                                         break;
566                                 case POINT_TYPE_ERASER:
567                                         input_report_key(g_input_dev,
568                                                 BTN_TOOL_RUBBER, 1);
569                                         break;
570                                 default:
571                                         dev_err(&g_spi->dev,
572                                                 "Raydium - point %d is invalid input tool type: %d\n",
573                                                 i, sp_tp->uc_tool_type[i]);
574                                         break;
575                                 }
576
577                                 input_report_abs(g_input_dev,
578                                                         ABS_MT_TRACKING_ID,
579                                                         sp_tp->uc_id[i]);
580
581                                 if (sp_tp->us_x[i] >= (i_max_x - 1))
582                                         input_report_abs(g_input_dev,
583                                                         ABS_MT_POSITION_X,
584                                                         (i_max_x - 1));
585                                 else
586                                         input_report_abs(g_input_dev,
587                                                         ABS_MT_POSITION_X,
588                                                         sp_tp->us_x[i]);
589
590                                 if (sp_tp->us_y[i] >= (i_max_y - 1))
591                                         input_report_abs(g_input_dev,
592                                                         ABS_MT_POSITION_Y,
593                                                         (i_max_y - 1));
594                                 else
595                                         input_report_abs(g_input_dev,
596                                                         ABS_MT_POSITION_Y,
597                                                         sp_tp->us_y[i]);
598
599                                 input_report_abs(g_input_dev,
600                                                         ABS_MT_PRESSURE,
601                                                         sp_tp->us_z[i]);
602
603                         }
604                         input_mt_sync(g_input_dev);
605                 }
606                 input_sync(g_input_dev);
607                 u8_last_touch_count = sp_tp->uc_touch_count;
608         }
609
610         /*if (g_st_ctrl.u8_power_mode)
611                 raydium_change_scan_mode(sp_tp->uc_touch_count); */
612
613         kfree(sp_tp);
614 }
615 #else /*(INPUT_PROTOCOL_CURRENT_SUPPORT == INPUT_PROTOCOL_TYPE_B)*/
616 void raydium_report_pointer(void *p)
617 {
618         static unsigned char u8_last_touch_count; /*= 0; remove by checkpatch*/
619         unsigned int target_abs_mt_tool = MT_TOOL_FINGER;
620         unsigned int target_key_evt_btn_tool = BTN_TOOL_FINGER;
621         int i;
622         int i_count;
623         int i_max_x, i_max_y;
624         struct rm_touch_event *sp_tp;
625         ssize_t missing;
626         sp_tp = kmalloc(sizeof(struct rm_touch_event), GFP_KERNEL);
627         if (sp_tp == NULL)
628                 return;
629
630         missing = copy_from_user(sp_tp, p, sizeof(struct rm_touch_event));
631         if (missing) {
632                 dev_err(&g_spi->dev, "Raydium - %s : copy failed - len:%d, miss:%d\n",
633                         __func__, sizeof(struct rm_touch_event), missing);
634                 kfree(sp_tp);
635                 return;
636         }
637
638         if ((g_st_ctrl.u16_resolution_x != 0) &&
639                 (g_st_ctrl.u16_resolution_y != 0)) {
640                 i_max_x = g_st_ctrl.u16_resolution_x;
641                 i_max_y = g_st_ctrl.u16_resolution_y;
642         } else {
643                 i_max_x = RM_INPUT_RESOLUTION_X;
644                 i_max_y = RM_INPUT_RESOLUTION_Y;
645         }
646
647         i_count = max(u8_last_touch_count, sp_tp->uc_touch_count);
648
649         if (!i_count) {
650                 /*if (g_st_ctrl.u8_power_mode)
651                         raydium_change_scan_mode(sp_tp->uc_touch_count);*/
652                 kfree(sp_tp);
653                 return;
654         }
655
656         for (i = 0; i < i_count; i++) {
657                 if (i < sp_tp->uc_touch_count) {
658
659                         input_mt_slot(g_input_dev,
660                                                 sp_tp->uc_slot[i] & 0x7F);
661
662                         if ((sp_tp->uc_slot[i] & 0x80) ||
663                                 (sp_tp->uc_id[i] == 0xFF)) {
664                                 switch (sp_tp->uc_pre_tool_type[i]) {
665                                 case POINT_TYPE_FINGER:
666                                         if (i == MAX_SUPPORT_SLOT_AMOUNT)
667                                                 break;
668                                         target_abs_mt_tool = MT_TOOL_FINGER;
669                                         break;
670                                 case POINT_TYPE_STYLUS:
671                                         if (i == MAX_SUPPORT_SLOT_AMOUNT)
672                                                 break;
673                                         target_abs_mt_tool = MT_TOOL_PEN;
674                                         break;
675                                 case POINT_TYPE_ERASER:
676                                         if (i == MAX_SUPPORT_SLOT_AMOUNT)
677                                                 break;
678                                         target_abs_mt_tool = MT_TOOL_PEN;
679                                         target_key_evt_btn_tool =
680                                                         BTN_TOOL_RUBBER;
681                                         break;
682                                 default:
683                                         if (sp_tp->uc_id[i] != 0xFF) {
684                                                 dev_err(&g_spi->dev,
685                                                 "Raydium - point %d release invalid input tool type: %d, id=%d\n",
686                                                 i, sp_tp->uc_pre_tool_type[i],
687                                                 sp_tp->uc_id[i]);
688                                         }
689                                         break;
690                                 }
691
692                                 input_mt_report_slot_state(
693                                         g_input_dev,
694                                         target_abs_mt_tool, false);
695
696                                 if (sp_tp->uc_pre_tool_type[i] ==
697                                         POINT_TYPE_ERASER)
698                                         input_report_key(
699                                                 g_input_dev,
700                                                 target_key_evt_btn_tool, false);
701                         }
702
703                         if (sp_tp->uc_id[i] != 0xFF) {
704                                 switch (sp_tp->uc_tool_type[i]) {
705                                 case POINT_TYPE_FINGER:
706                                         if (i == MAX_SUPPORT_SLOT_AMOUNT)
707                                                 break;
708                                         target_abs_mt_tool = MT_TOOL_FINGER;
709                                         break;
710                                 case POINT_TYPE_STYLUS:
711                                         if (i == MAX_SUPPORT_SLOT_AMOUNT)
712                                                 break;
713                                         target_abs_mt_tool = MT_TOOL_PEN;
714                                         break;
715                                 case POINT_TYPE_ERASER:
716                                         if (i == MAX_SUPPORT_SLOT_AMOUNT)
717                                                 break;
718                                         target_abs_mt_tool = MT_TOOL_PEN;
719                                         target_key_evt_btn_tool =
720                                                         BTN_TOOL_RUBBER;
721                                         break;
722                                 default:
723                                         dev_err(&g_spi->dev,
724                                                 "Raydium - point %d has invalid input tool type: %d, id=%d\n",
725                                                 i, sp_tp->uc_tool_type[i],
726                                                 sp_tp->uc_id[i]);
727                                         break;
728                                 }
729
730                                 input_mt_report_slot_state(
731                                         g_input_dev,
732                                         target_abs_mt_tool, true);
733
734                                 if (sp_tp->us_x[i] >= (i_max_x - 1))
735                                         input_report_abs(
736                                                 g_input_dev,
737                                                 ABS_MT_POSITION_X,
738                                                 (i_max_x - 1));
739                                 else
740                                         input_report_abs(
741                                                 g_input_dev,
742                                                 ABS_MT_POSITION_X,
743                                                 sp_tp->us_x[i]);
744
745                                 if (sp_tp->us_y[i] >= (i_max_y - 1))
746                                         input_report_abs(
747                                                 g_input_dev,
748                                                 ABS_MT_POSITION_Y,
749                                                 (i_max_y - 1));
750                                 else
751                                         input_report_abs(
752                                                 g_input_dev,
753                                                 ABS_MT_POSITION_Y,
754                                                 sp_tp->us_y[i]);
755
756                                 input_report_abs(
757                                         g_input_dev,
758                                         ABS_MT_PRESSURE,
759                                         sp_tp->us_z[i]);
760
761                                 if (sp_tp->uc_tool_type[i] == POINT_TYPE_ERASER)
762                                         input_report_key(
763                                                 g_input_dev,
764                                                 target_key_evt_btn_tool, true);
765                         }
766                 }
767         }
768         u8_last_touch_count = sp_tp->uc_touch_count;
769         input_sync(g_input_dev);
770
771         /*if (g_st_ctrl.u8_power_mode)
772                 raydium_change_scan_mode(sp_tp->uc_touch_count);*/
773
774         kfree(sp_tp);
775 }
776 #endif
777
778 /*=============================================================================
779          Description: Read Sensor Raw Data
780
781          Input:
782                         *p : Raw Data Buffer Address
783          Output:
784                         none
785 =============================================================================*/
786 static int rm_tch_read_image_data(unsigned char *p)
787 {
788         int ret;
789
790         g_pu8_burstread_buf = p;
791
792 #if ENABLE_FREQ_HOPPING  /*ENABLE_SCAN_DATA_HEADER*/
793         g_pu8_burstread_buf[0] = SCAN_TYPE_MT;
794         g_pu8_burstread_buf[1] = (u8)(g_st_ctrl.u16_data_length >> 8);
795         g_pu8_burstread_buf[2] = (u8)(g_st_ctrl.u16_data_length);
796         if (g_st_ctrl.u8_ns_func_enable&0x01)
797                 g_pu8_burstread_buf[3] = g_st_ts.u8_ns_sel;
798         else
799                 g_pu8_burstread_buf[3] = 0;
800         g_pu8_burstread_buf[4] = g_st_ts.u8_test_mode_type;
801         g_pu8_burstread_buf[5] = 0x00;
802         g_pu8_burstread_buf[6] = 0x00;
803         g_pu8_burstread_buf[7] = 0x00;
804 #endif
805
806         ret = rm_tch_cmd_process(0, g_st_rm_readimg_cmd, NULL);
807         return ret;
808 }
809
810 void rm_tch_ctrl_set_baseline(u8 *arg, u16 u16Len)
811 {
812         ssize_t missing;
813
814         b_bl_updated = FALSE;
815         missing = copy_from_user(g_u8_update_baseline, arg, u16Len);
816         if (missing)
817                 return;
818         b_bl_updated = TRUE;
819 }
820
821 static int rm_tch_write_image_data(void)
822 {
823         int ret = RETURN_OK;
824
825         ret = rm_tch_cmd_process(0, g_st_rm_writeimg_cmd, NULL);
826         return ret;
827 }
828
829 #if ENABLE_FREQ_HOPPING
830 void rm_set_ns_para(u8 u8Idx, u8 *u8Para)
831 {
832         int ii;
833         struct rm_tch_ts *ts = input_get_drvdata(g_input_dev);
834
835         for (ii = 0; ii < g_st_rm_ns_para_cmd[2]; ii++) {
836                 ts->u8_repeat_counter = u8Para[ii*3+u8Idx];
837                 rm_tch_cmd_process(ii, g_st_rm_ns_para_cmd, ts);
838         }
839 }
840 #endif
841
842 void rm_tch_ctrl_enter_auto_mode(void)
843 {
844         g_st_ctrl.u8_idle_mode_check &= ~0x01;
845
846         if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
847                 rm_printk("Raydium - Enter Auto Scan Mode, bl_update=%d\n",
848                         b_bl_updated);
849
850         if (b_bl_updated) {
851                 rm_tch_write_image_data();
852                 b_bl_updated = FALSE;
853         }
854
855         rm_set_repeat_times(g_st_ctrl.u8_idle_digital_repeat_times);
856         rm_tch_cmd_process(1, g_st_cmd_set_idle, NULL);
857 }
858
859 void rm_tch_ctrl_leave_auto_mode(void)
860 {
861         struct rm_tch_ts *ts = input_get_drvdata(g_input_dev);
862
863         g_st_ctrl.u8_idle_mode_check |= 0x01;
864
865 #if ENABLE_FREQ_HOPPING
866         if (g_st_ctrl.u8_ns_func_enable&0x01) {
867                 mutex_lock(&g_st_ts.mutex_ns_mode);
868                 rm_set_ns_para(0, (u8 *)&g_st_ts.u8_ns_para[0]);
869                 mutex_unlock(&g_st_ts.mutex_ns_mode);
870         }
871 #endif
872
873         rm_tch_cmd_process(0, g_st_cmd_set_idle, ts);
874         rm_set_repeat_times(g_st_ctrl.u8_active_digital_repeat_times);
875
876         if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
877                 rm_printk("Raydium - Leave Auto Scan Mode\n");
878 }
879
880 void rm_ctrl_pause_auto_mode(void)
881 {
882         rm_tch_cmd_process(0, g_st_cmd_pause_auto, NULL);
883 }
884
885 int rm_tch_ctrl_clear_int(void)
886 {
887         return rm_tch_cmd_process(0, g_st_rm_clear_int_cmd, NULL);
888 }
889
890
891 int rm_tch_ctrl_wait_for_scan_finish(u8 u8Idx)
892 {
893         int i;
894
895         for (i = 0; i < 50; i++) { /*50ms = 20Hz*/
896                 rm_tch_cmd_process(0, g_st_rm_wait_scan_ok_cmd, NULL);
897
898                 if (g_st_ts.u16_read_para & 0x01) {
899                         if (u8Idx)
900                                 return 0;
901                         else
902                                 usleep_range(1000, 2000);       /* msleep(1); */
903                 } else
904                         break;
905         }
906         return 0;
907 }
908
909 int rm_tch_ctrl_scan_start(void)
910 {
911 #if ENABLE_FREQ_HOPPING
912         static u8 u8NsSel = 0, u8Rpt = 1;
913         if (g_st_ctrl.u8_ns_func_enable&0x01) {
914                 if (rm_tch_ctrl_wait_for_scan_finish(1))
915                         return rm_tch_cmd_process(0, g_st_rm_scan_start_cmd,
916                                 NULL);
917
918                 mutex_lock(&g_st_ts.mutex_ns_mode);
919                 g_st_ts.u8_ns_sel = g_st_ts.u8_ns_para[u8NsSel];
920
921                 if (u8NsSel < g_st_ts.u8_ns_mode)
922                         u8NsSel++;
923                 else
924                         u8NsSel = 0;
925
926                 if (u8Rpt != g_st_ts.u8_ns_rpt) {
927                         u8Rpt = g_st_ts.u8_ns_rpt;
928                         rm_set_repeat_times(u8Rpt);
929                 }
930                 rm_set_ns_para(u8NsSel, (u8 *)&g_st_ts.u8_ns_para[0]);
931
932                 mutex_unlock(&g_st_ts.mutex_ns_mode);
933         } else {
934                 u8NsSel = 0;
935                 g_st_ts.u8_ns_sel = 0;
936         }
937 #endif
938         return rm_tch_cmd_process(0, g_st_rm_scan_start_cmd, NULL);
939 }
940
941 void rm_set_repeat_times(u8 u8Times)
942 {
943         struct rm_tch_ts *ts = input_get_drvdata(g_input_dev);
944
945         if (u8Times <= 1)
946                 u8Times = 0;
947         else
948                 u8Times = u8Times - 1;
949
950         if (u8Times > 127)
951                 u8Times = 127;
952
953         /*ts->u8_repeat_counter = (u8Times & 0x1F);*/
954         ts->u8_repeat_counter = (u8Times & 0x7F);
955         if (u8Times == 0)
956                 rm_tch_cmd_process(0, g_st_rm_set_rep_time_cmd, ts);
957         else
958                 rm_tch_cmd_process(1, g_st_rm_set_rep_time_cmd, ts);
959 }
960
961 static u32 rm_tch_ctrl_configure(void)
962 {
963         u32 u32_flag;
964
965         switch (g_st_ts.u8_scan_mode_state) {
966         case RM_SCAN_ACTIVE_MODE:
967                 u32_flag =
968                         RM_NEED_TO_SEND_SCAN |
969                         RM_NEED_TO_READ_RAW_DATA |
970                         RM_NEED_TO_SEND_SIGNAL;
971                 break;
972
973         case RM_SCAN_PRE_IDLE_MODE:
974                 rm_tch_ctrl_enter_auto_mode();
975                 g_st_ts.u8_scan_mode_state = RM_SCAN_IDLE_MODE;
976                 u32_flag = RM_NEED_NONE;
977                 break;
978
979         case RM_SCAN_IDLE_MODE:
980                 rm_tch_ctrl_leave_auto_mode();
981                 g_st_ts.u8_scan_mode_state = RM_SCAN_ACTIVE_MODE;
982                 u32_flag = RM_NEED_TO_SEND_SCAN;
983                 break;
984
985         default:
986                 u32_flag = RM_NEED_NONE;
987                 break;
988         }
989
990         return u32_flag;
991 }
992
993 int KRL_CMD_CONFIG_1V8_Handler(u8 u8_cmd, u8 u8_on_off, struct rm_tch_ts *ts)
994 {
995         int ret = RETURN_FAIL;
996         struct rm_spi_ts_platform_data *pdata;
997
998         pdata = g_input_dev->dev.parent->platform_data;
999
1000         if (u8_cmd == KRL_SUB_CMD_SET_1V8_REGULATOR) {
1001                 if (ts) {
1002                         if (ts->regulator_1v8) {
1003                                 if (u8_on_off) {
1004                                         ret = regulator_enable(
1005                                                 ts->regulator_1v8);
1006                                         if (ret)
1007                                                 dev_err(&g_spi->dev,
1008                                                         "Raydium - regulator 1.8V enable failed: %d\n",
1009                                                         ret);
1010                                 } else {
1011                                         ret = regulator_disable(
1012                                                 ts->regulator_1v8);
1013                                         if (ret)
1014                                                 dev_err(&g_spi->dev,
1015                                                         "Raydium - regulator 1.8V disable failed: %d\n",
1016                                                         ret);
1017                                 }
1018                         } else
1019                                 dev_err(&g_spi->dev,
1020                                         "Raydium - regulator 1.8V handler fail: %d\n",
1021                                         ret);
1022                 } else
1023                         dev_err(&g_spi->dev,
1024                                 "Raydium - regulator 1.8V ts fail: %d\n",
1025                                 ret);
1026         } else if (u8_cmd == KRL_SUB_CMD_SET_1V8_GPIO)
1027                 ret = gpio_direction_output(pdata->gpio_1v8, u8_on_off);
1028
1029         return ret;
1030 }
1031
1032 int KRL_CMD_CONFIG_3V3_Handler(u8 u8_cmd, u8 u8_on_off, struct rm_tch_ts *ts)
1033 {
1034         int ret = RETURN_FAIL;
1035         struct rm_spi_ts_platform_data *pdata;
1036
1037         pdata = g_input_dev->dev.parent->platform_data;
1038
1039         if (u8_cmd == KRL_SUB_CMD_SET_3V3_REGULATOR) {
1040                 if (ts) {
1041                         if (ts->regulator_3v3) {
1042                                 if (u8_on_off) {
1043                                         ret = regulator_enable(
1044                                                 ts->regulator_3v3);
1045                                         if (ret)
1046                                                 dev_err(&g_spi->dev,
1047                                                         "Raydium - regulator 3.3V enable failed: %d\n",
1048                                                         ret);
1049                                 } else {
1050                                         ret = regulator_disable(
1051                                                 ts->regulator_3v3);
1052                                         if (ret)
1053                                                 dev_err(&g_spi->dev,
1054                                                         "Raydium - regulator 3.3V disable failed: %d\n",
1055                                                         ret);
1056                                 }
1057                         } else
1058                                 dev_err(&g_spi->dev,
1059                                         "Raydium - regulator 3.3V handler fail: %d\n",
1060                                         ret);
1061                 } else
1062                         dev_err(&g_spi->dev,
1063                                 "Raydium - regulator 3.3V ts fail: %d\n",
1064                                 ret);
1065         } else if (u8_cmd == KRL_SUB_CMD_SET_3V3_GPIO)
1066                 ret = gpio_direction_output(pdata->gpio_3v3, u8_on_off);
1067
1068         return ret;
1069 }
1070
1071 void rm_show_kernel_tbl_name(u8 *p_cmd_tbl)
1072 {
1073         char target_table_name[32];
1074
1075         memset(target_table_name, 0,
1076                 sizeof(target_table_name));
1077
1078         if (p_cmd_tbl == g_st_cmd_set_idle)
1079                 snprintf(target_table_name,
1080                         sizeof(target_table_name), "Set Idle");
1081         else if (p_cmd_tbl == g_st_cmd_pause_auto)
1082                 snprintf(target_table_name,
1083                         sizeof(target_table_name), "Pause Auto");
1084         else if (p_cmd_tbl == g_st_rm_resume_cmd)
1085                 snprintf(target_table_name,
1086                         sizeof(target_table_name), "Resume");
1087         else if (p_cmd_tbl == g_st_rm_suspend_cmd)
1088                 snprintf(target_table_name,
1089                         sizeof(target_table_name), "Suspend");
1090         else if (p_cmd_tbl == g_st_rm_readimg_cmd)
1091                 snprintf(target_table_name,
1092                         sizeof(target_table_name), "Read Image");
1093         else if (p_cmd_tbl == g_st_rm_watchdog_cmd)
1094                 snprintf(target_table_name,
1095                         sizeof(target_table_name), "WatchDog");
1096         else if (p_cmd_tbl == g_st_rm_testmode_cmd)
1097                 snprintf(target_table_name,
1098                         sizeof(target_table_name), "Test Mode");
1099         else if (p_cmd_tbl == g_st_rm_slow_scan_cmd)
1100                 snprintf(target_table_name,
1101                         sizeof(target_table_name), "Slowscan");
1102         else if (p_cmd_tbl == g_st_rm_clear_int_cmd)
1103                 snprintf(target_table_name,
1104                         sizeof(target_table_name), "Clear Intterupt");
1105         else if (p_cmd_tbl == g_st_rm_scan_start_cmd)
1106                 snprintf(target_table_name,
1107                         sizeof(target_table_name), "Scan Start");
1108         else if (p_cmd_tbl == g_st_rm_wait_scan_ok_cmd)
1109                 snprintf(target_table_name,
1110                         sizeof(target_table_name), "Wait Scan Okay");
1111         else if (p_cmd_tbl == g_st_rm_set_rep_time_cmd)
1112                 snprintf(target_table_name,
1113                         sizeof(target_table_name), "Set Repeat Time");
1114         else if (p_cmd_tbl == g_st_rm_ns_para_cmd)
1115                 snprintf(target_table_name,
1116                         sizeof(target_table_name), "Ns Parameter");
1117         else if (p_cmd_tbl == g_st_rm_writeimg_cmd)
1118                 snprintf(target_table_name,
1119                         sizeof(target_table_name), "Write Image");
1120         else if (p_cmd_tbl == g_st_rm_tlk_cmd)
1121                 snprintf(target_table_name,
1122                         sizeof(target_table_name), "TLK");
1123         else if (p_cmd_tbl == g_st_rm_kl_testmode_cmd)
1124                 snprintf(target_table_name,
1125                         sizeof(target_table_name), "Kernel_Test");
1126         else {
1127                 dev_err(&g_spi->dev, "Raydium - %s : no such kernel table - err:%p\n",
1128                         __func__, p_cmd_tbl);
1129         }
1130         dev_err(&g_spi->dev, "Raydium - Table %s cmd failed\n",
1131                 target_table_name);
1132 }
1133
1134 static int rm_tch_cmd_process(u8 u8_sel_case,
1135         u8 *p_cmd_tbl, struct rm_tch_ts *ts)
1136 {
1137 #define _CMD u16j
1138 #define _ADDR (u16j+1)
1139 #define _SUB_CMD (u16j+1)
1140 #define _DATA (u16j+2)
1141
1142         static DEFINE_MUTEX(lock);
1143         u16 u16j = 0, u16strIdx, u16TblLenth, u16Tmp;
1144         u32 u32Tmp;
1145         u8 u8i, u8reg = 0;
1146         int ret = RETURN_FAIL;
1147         struct rm_spi_ts_platform_data *pdata;
1148
1149         mutex_lock(&lock);
1150
1151         pdata = g_input_dev->dev.parent->platform_data;
1152
1153         if (p_cmd_tbl[KRL_TBL_FIELD_POS_LEN_H]) {
1154                 u16TblLenth = p_cmd_tbl[KRL_TBL_FIELD_POS_LEN_H];
1155                 u16TblLenth <<= 8;
1156                 u16TblLenth |= p_cmd_tbl[KRL_TBL_FIELD_POS_LEN_L];
1157         } else
1158                 u16TblLenth = p_cmd_tbl[KRL_TBL_FIELD_POS_LEN_L];
1159
1160         if (u16TblLenth < 3) {
1161                 if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
1162                         dev_info(&g_spi->dev, "Raydium - Null u8_cmd %s : [%p]\n",
1163                                 __func__, p_cmd_tbl);
1164                 mutex_unlock(&lock);
1165                 return ret;
1166         }
1167
1168         u16strIdx = KRL_TBL_FIELD_POS_CASE_NUM
1169                 + p_cmd_tbl[KRL_TBL_FIELD_POS_CASE_NUM] + 1;
1170
1171         if (u8_sel_case) {
1172                 for (u8i = 0; u8i < u8_sel_case; u8i++)
1173                         u16strIdx += (p_cmd_tbl[u8i + KRL_TBL_FIELD_POS_CMD_NUM]
1174                                 * KRL_TBL_CMD_LEN);
1175         }
1176
1177         for (u8i = 0; u8i < p_cmd_tbl[u8_sel_case + KRL_TBL_FIELD_POS_CMD_NUM];
1178                 u8i++) {
1179                 u16j = u16strIdx + (KRL_TBL_CMD_LEN * u8i);
1180                 ret = RETURN_FAIL;
1181                 switch (p_cmd_tbl[_CMD]) {
1182                 case KRL_CMD_READ:
1183                         ret = rm_tch_spi_read(p_cmd_tbl[_ADDR], &u8reg, 1);
1184                         g_st_ts.u16_read_para = u8reg;
1185                         /*rm_printk("Raydium - KRL_CMD_READ "
1186                         "- 0x%x:0x%x\n", p_cmd_tbl[_ADDR], u8reg);*/
1187                         break;
1188                 case KRL_CMD_WRITE_WO_DATA:
1189                         /*rm_printk("Raydium - KRL_CMD_WRITE_WO_DATA "
1190                         "- 0x%x:0x%x\n", p_cmd_tbl[_ADDR], u8reg);*/
1191                         ret = rm_tch_spi_byte_write(p_cmd_tbl[_ADDR], u8reg);
1192                         break;
1193                 case KRL_CMD_IF_AND_OR:
1194                         if (u8reg & p_cmd_tbl[_ADDR])
1195                                 u8reg |= p_cmd_tbl[_DATA];
1196                         ret = RETURN_OK;
1197                         break;
1198                 case KRL_CMD_AND:
1199                         u8reg &= p_cmd_tbl[_DATA];
1200                         ret = RETURN_OK;
1201                         break;
1202                 case KRL_CMD_OR:
1203                         u8reg |= p_cmd_tbl[_DATA];
1204                         ret = RETURN_OK;
1205                         break;
1206                 case KRL_CMD_DRAM_INIT:
1207                         ret = rm_tch_spi_byte_write(0x01, 0x00);
1208                         ret = rm_tch_spi_byte_write(0x02, 0x00);
1209                         break;
1210                 case KRL_CMD_READ_IMG:
1211                         /*rm_printk("Raydium - KRL_CMD_READ_IMG "
1212                         "- 0x%x:%p:%d\n",
1213                         p_cmd_tbl[_ADDR],
1214                         g_pu8_burstread_buf,
1215                         g_st_ctrl.u16_data_length);*/
1216                         if (g_pu8_burstread_buf != NULL) {
1217                                 ret = rm_tch_spi_read(p_cmd_tbl[_ADDR],
1218                                 g_pu8_burstread_buf + QUEUE_HEADER_NUM,
1219                                 g_st_ctrl.u16_data_length
1220                                 /* - QUEUE_HEADER_NUM*/);
1221                         }
1222                         g_pu8_burstread_buf = NULL;
1223                         break;
1224                 case KRL_CMD_WRITE_W_DATA:
1225                         /*rm_printk("Raydium - KRL_CMD_WRITE_W_DATA "
1226                         "- 0x%x:0x%x\n", p_cmd_tbl[_ADDR], p_cmd_tbl[_DATA]);*/
1227                         ret = rm_tch_spi_byte_write(p_cmd_tbl[_ADDR],
1228                                 p_cmd_tbl[_DATA]);
1229                         break;
1230                 case KRL_CMD_NOT:
1231                         u8reg = ~u8reg;
1232                         /*g_st_ts.u16_read_para = u8reg;*/
1233                         ret = RETURN_OK;
1234                         break;
1235                 case KRL_CMD_XOR:
1236                         u8reg ^= p_cmd_tbl[_DATA];
1237                         ret = RETURN_OK;
1238                         break;
1239                 case KRL_CMD_SEND_SIGNAL:
1240                         u16Tmp = p_cmd_tbl[_DATA];
1241                         /*rm_printk("Raydium - KRL_CMD_SEND_SIGNAL "
1242                         "- %d\n", u16Tmp);*/
1243                         ret = rm_tch_ts_send_signal(g_st_ts.u32_hal_pid,
1244                                 (int)u16Tmp);
1245                         if (u16Tmp == RM_SIGNAL_RESUME)
1246                                 g_st_ts.u8_resume_cnt++;
1247                         break;
1248                 case KRL_CMD_CONFIG_RST:
1249                         /*rm_printk("Raydium - KRL_CMD_CONFIG_RST "
1250                         "- %d - %d\n", p_cmd_tbl[_SUB_CMD], p_cmd_tbl[_DATA]);*/
1251                         switch (p_cmd_tbl[_SUB_CMD]) {
1252                         case KRL_SUB_CMD_SET_RST_GPIO:
1253                                 ret = gpio_direction_output(pdata->gpio_reset,
1254                                         p_cmd_tbl[_DATA]);
1255                                 break;
1256                         case KRL_SUB_CMD_SET_RST_VALUE:
1257                                 gpio_set_value(pdata->gpio_reset,
1258                                         p_cmd_tbl[_DATA]);
1259                                 ret = RETURN_OK;
1260                                 break;
1261                         }
1262                         break;
1263                 case KRL_CMD_CONFIG_3V3:/*Need to check qpio setting*/
1264                         /*rm_printk("Raydium - KRL_CMD_CONFIG_3V3 "
1265                         "- %d - %d\n", p_cmd_tbl[_SUB_CMD], p_cmd_tbl[_DATA]);
1266                         rm_printk("Raydium - 3.3V regulator is %d\n",
1267                         regulator_is_enabled(ts->regulator_3v3));*/
1268                         ret = KRL_CMD_CONFIG_3V3_Handler(p_cmd_tbl[_SUB_CMD],
1269                                 p_cmd_tbl[_DATA], ts);
1270                         /*rm_printk("Raydium - 3.3V regulator is %d\n",
1271                         regulator_is_enabled(ts->regulator_3v3));*/
1272                         break;
1273                 case KRL_CMD_CONFIG_1V8:/*Need to check qpio setting*/
1274                         /*rm_printk("Raydium - KRL_CMD_CONFIG_1V8 "
1275                         "- %d - %d\n", p_cmd_tbl[_SUB_CMD], p_cmd_tbl[_DATA]);
1276                         rm_printk("Raydium - 1.8V regulator is %d\n",
1277                         regulator_is_enabled(ts->regulator_1v8));*/
1278                         ret = KRL_CMD_CONFIG_1V8_Handler(p_cmd_tbl[_SUB_CMD],
1279                                 p_cmd_tbl[_DATA], ts);
1280                         /*rm_printk("Raydium - 1.8V regulator is %d\n",
1281                         regulator_is_enabled(ts->regulator_1v8));*/
1282                         break;
1283                 case KRL_CMD_CONFIG_CLK:
1284                         /*rm_printk("Raydium - KRL_CMD_CONFIG_CLK"
1285                         " - %d - %d\n", p_cmd_tbl[_SUB_CMD],
1286                         p_cmd_tbl[_DATA]);*/
1287                         if (p_cmd_tbl[_SUB_CMD] == KRL_SUB_CMD_SET_CLK) {
1288                                 if (ts && ts->clk) {
1289                                         if (p_cmd_tbl[_DATA])
1290                                                 ret = clk_enable(ts->clk);
1291                                         else {
1292                                                 clk_disable(ts->clk);
1293                                                 ret = RETURN_OK;
1294                                         }
1295                                 } else {
1296                                         dev_err(&g_spi->dev, "Raydium - %s : No clk handler!\n",
1297                                                 __func__);
1298                                         ret = RETURN_OK;
1299                                 }
1300                         } else
1301                                 ret = RETURN_FAIL;
1302                         break;
1303                 case KRL_CMD_CONFIG_CS:
1304                         /*rm_printk("Raydium - KRL_CMD_CONFIG_CS "
1305                         "- %d - %d\n", p_cmd_tbl[_SUB_CMD], p_cmd_tbl[_DATA]);*/
1306                         switch (p_cmd_tbl[_SUB_CMD]) {
1307                         case KRL_SUB_CMD_SET_CS_LOW:
1308 #ifdef CS_SUPPORT
1309                         /*
1310                         * spi_cs_low - set chip select pin state
1311                         * @spi: device for which chip select pin state to be set
1312                         * state: if true chip select pin will be kept low else
1313                         * high
1314                         * The return value is zero for success, else
1315                         * errno status code.
1316                         *
1317                         * int spi_cs_low(struct spi_device *spi, bool state)
1318                         */
1319                                 ret = spi_cs_low(g_spi,
1320                                         (bool)!!p_cmd_tbl[_DATA]);
1321 #else
1322                                 ret = RETURN_OK;
1323 #endif
1324                                 break;
1325                         }
1326                         break;
1327                 case KRL_CMD_SET_TIMER:
1328                         /*rm_printk("Raydium - KRL_CMD_SET_TIMER "
1329                         "- %d\n", p_cmd_tbl[_SUB_CMD]);*/
1330                         ret = RETURN_OK;
1331                         if (p_cmd_tbl[_SUB_CMD] == KRL_SUB_CMD_INIT_TIMER)
1332                                 init_ts_timer();
1333                         else if (p_cmd_tbl[_SUB_CMD] == KRL_SUB_CMD_ADD_TIMER) {
1334                                 if (!timer_pending(&ts_timer_triggle))
1335                                         add_timer(&ts_timer_triggle);
1336                         } else if (p_cmd_tbl[_SUB_CMD] == KRL_SUB_CMD_DEL_TIMER)
1337                                 /*del_timer of an inactive timer returns 0,
1338                                 del_timer of an active timer returns 1*/
1339                                 del_timer(&ts_timer_triggle);
1340                         else
1341                                 ret = RETURN_FAIL;
1342                         break;
1343                 case KRL_CMD_MSLEEP:
1344                         /*rm_printk("Raydium - KRL_CMD_MSLEEP "
1345                         "- %d ms\n", p_cmd_tbl[_DATA] |
1346                         (p_cmd_tbl[_SUB_CMD] << 8));*/
1347                         u32Tmp = (u16)(p_cmd_tbl[_DATA] |
1348                                         (p_cmd_tbl[_SUB_CMD] << 8));
1349
1350                         u32Tmp *= 1000;
1351                         usleep_range(u32Tmp, u32Tmp + 200);
1352                         ret = RETURN_OK;
1353                         break;
1354                 case KRL_CMD_FLUSH_QU:
1355                         /*rm_printk("Raydium - KRL_CMD_FLUSH_QU "
1356                         "- %d\n", p_cmd_tbl[_SUB_CMD]);*/
1357                         ret = RETURN_OK;
1358                         if (p_cmd_tbl[_SUB_CMD] == KRL_SUB_CMD_SENSOR_QU) {
1359 #if (ISR_POST_HANDLER == WORK_QUEUE)
1360                                 mutex_unlock(&lock);
1361                                 flush_workqueue(g_st_ts.rm_workqueue);
1362                                 g_worker_queue_is_flush = true;
1363                                 mutex_lock(&lock);
1364 #endif
1365                         } else if (p_cmd_tbl[_SUB_CMD] ==
1366                                                 KRL_SUB_CMD_TIMER_QU) {
1367                                 mutex_unlock(&lock);
1368                                 flush_workqueue(g_st_ts.rm_timer_workqueue);
1369                                 g_timer_queue_is_flush = true;
1370                                 mutex_lock(&lock);
1371                         } else
1372                                 ret = RETURN_FAIL;
1373                         break;
1374                 case KRL_CMD_WRITE_W_COUNT:
1375                         /*rm_printk("Raydium - KRL_CMD_WRITE_W_COUNT "
1376                         "- 0x%x: 0x%x..0x%x\n", p_cmd_tbl[_ADDR], u8reg,
1377                         ts->u8_repeat_counter);*/
1378                         if (ts)
1379                                 ret = rm_tch_spi_byte_write(p_cmd_tbl[_ADDR],
1380                                         u8reg | (ts->u8_repeat_counter));
1381                         else
1382                                 ret = RETURN_FAIL;
1383                         break;
1384                 case KRL_CMD_RETURN_RESULT:
1385                         g_st_ts.u16_read_para = u8reg;
1386                         ret = RETURN_OK;
1387                         break;
1388                 case KRL_CMD_RETURN_VALUE:
1389                         g_st_ts.u16_read_para = (p_cmd_tbl[_ADDR] << 8)
1390                                 + (p_cmd_tbl[_DATA]);
1391                         ret = RETURN_OK;
1392                         /*rm_printk("Raydium - KRL_CMD_RETURN_VALUE,
1393                         value=%d", ret);*/
1394                         break;
1395                 case KRL_CMD_WRITE_IMG:
1396                         /*rm_printk("Raydium - KRL_CMD_WRITE_IMG -
1397                         0x%x:0x%x:%d\n", p_cmd_tbl[_ADDR], g_pu8_burstread_buf,
1398                         g_st_ctrl.u16_data_length);*/
1399                         ret = rm_tch_spi_burst_write(p_cmd_tbl[_ADDR],
1400                                 &g_u8_update_baseline[0],
1401                                 g_st_ctrl.u16_data_length);
1402                         break;
1403                 case KRL_CMD_CONFIG_IRQ:
1404                         if (ts && (p_cmd_tbl[_SUB_CMD]
1405                                         == KRL_SUB_CMD_SET_IRQ)) {
1406                                 if (ts->irq) {
1407                                         if (p_cmd_tbl[_DATA])
1408                                                 enable_irq(ts->irq);
1409                                         else
1410                                                 disable_irq(ts->irq);
1411                                         } else {
1412                                                 dev_err(&g_spi->dev,
1413                                                         "Raydium - %s : No irq handler!\n",
1414                                                         __func__);
1415                                         }
1416                                 ret = RETURN_OK;
1417                         } else
1418                                 ret = RETURN_FAIL;
1419                         break;
1420                 default:
1421                         break;
1422                 }
1423
1424                 if (ret) {
1425                         rm_show_kernel_tbl_name(p_cmd_tbl);
1426
1427                         dev_err(&g_spi->dev, "Raydium - u8_cmd:0x%x, addr:0x%x, data:0x%x\n",
1428                                 p_cmd_tbl[_CMD],
1429                                 p_cmd_tbl[_ADDR],
1430                                 p_cmd_tbl[_DATA]);
1431                         break;
1432                 }
1433         }
1434
1435         mutex_unlock(&lock);
1436
1437         return ret;
1438 }
1439
1440 int rm_set_kernel_tbl(int i_func_idx, u8 *p_u8_src)
1441 {
1442         ssize_t missing;
1443         u16 u16_len = 0;
1444         u8 *p_u8_len;
1445         u8 *p_u8_dst;
1446
1447         switch (i_func_idx) {
1448         case KRL_INDEX_FUNC_SET_IDLE:
1449                 p_u8_dst = g_st_cmd_set_idle;
1450                 break;
1451         case KRL_INDEX_FUNC_PAUSE_AUTO:
1452                 p_u8_dst = g_st_cmd_pause_auto;
1453                 break;
1454         case KRL_INDEX_RM_RESUME:
1455                 p_u8_dst = g_st_rm_resume_cmd;
1456                 break;
1457         case KRL_INDEX_RM_SUSPEND:
1458                 p_u8_dst = g_st_rm_suspend_cmd;
1459                 break;
1460         case KRL_INDEX_RM_READ_IMG:
1461                 p_u8_dst = g_st_rm_readimg_cmd;
1462                 break;
1463         case KRL_INDEX_RM_WATCHDOG:
1464                 p_u8_dst = g_st_rm_watchdog_cmd;
1465                 break;
1466         case KRL_INDEX_RM_TESTMODE:
1467                 p_u8_dst = g_st_rm_testmode_cmd;
1468                 break;
1469         case KRL_INDEX_RM_SLOWSCAN:
1470                 p_u8_dst = g_st_rm_slow_scan_cmd;
1471                 break;
1472         case KRL_INDEX_RM_CLEARINT:
1473                 p_u8_dst = g_st_rm_clear_int_cmd;
1474                 break;
1475         case KRL_INDEX_RM_SCANSTART:
1476                 p_u8_dst = g_st_rm_scan_start_cmd;
1477                 break;
1478         case KRL_INDEX_RM_WAITSCANOK:
1479                 p_u8_dst = g_st_rm_wait_scan_ok_cmd;
1480                 break;
1481         case KRL_INDEX_RM_SETREPTIME:
1482                 p_u8_dst = g_st_rm_set_rep_time_cmd;
1483                 break;
1484         case KRL_INDEX_RM_NSPARA:
1485                 p_u8_dst = g_st_rm_ns_para_cmd;
1486                 break;
1487         case KRL_INDEX_RM_WRITE_IMG:
1488                 p_u8_dst = g_st_rm_writeimg_cmd;
1489                 break;
1490         case KRL_INDEX_RM_TLK:
1491                 p_u8_dst = g_st_rm_tlk_cmd;
1492                 break;
1493         case KRL_INDEX_RM_KL_TESTMODE:
1494                 p_u8_dst = g_st_rm_kl_testmode_cmd;
1495                 break;
1496         default:
1497                 dev_err(&g_spi->dev, "Raydium - %s : no such kernel table - err:%d\n",
1498                         __func__, i_func_idx);
1499                 return RETURN_FAIL;
1500         }
1501
1502         p_u8_len = kmalloc(KRL_TBL_FIELD_POS_CASE_NUM, GFP_KERNEL);
1503         if (p_u8_len == NULL)
1504                 return RETURN_FAIL;
1505
1506         missing = copy_from_user(p_u8_len, p_u8_src,
1507                 KRL_TBL_FIELD_POS_CASE_NUM);
1508         if (missing) {
1509                 dev_err(&g_spi->dev, "Raydium - %s : copy failed - len:%d, miss:%d\n",
1510                         __func__, KRL_TBL_FIELD_POS_CASE_NUM, missing);
1511                 kfree(p_u8_len);
1512                 return missing;
1513         }
1514
1515         u16_len = p_u8_len[KRL_TBL_FIELD_POS_LEN_H];
1516         u16_len <<= 8;
1517         u16_len |= p_u8_len[KRL_TBL_FIELD_POS_LEN_L];
1518
1519         missing = copy_from_user(p_u8_dst, p_u8_src, u16_len);
1520         if (missing) {
1521                 dev_err(&g_spi->dev, "Raydium - %s : copy failed - len:%d, miss:%d\n",
1522                         __func__, u16_len, missing);
1523                 kfree(p_u8_len);
1524                 return missing;
1525         }
1526
1527         kfree(p_u8_len);
1528         return RETURN_OK;
1529 }
1530 /*=============================================================================
1531
1532 =============================================================================*/
1533 static void rm_tch_enter_manual_mode(void)
1534 {
1535 #if (ISR_POST_HANDLER == WORK_QUEUE)
1536         flush_workqueue(g_st_ts.rm_workqueue);
1537 #endif
1538         mutex_lock(&g_st_ts.mutex_scan_mode);
1539         if (g_st_ts.u8_scan_mode_state == RM_SCAN_ACTIVE_MODE) {
1540                 mutex_unlock(&g_st_ts.mutex_scan_mode);
1541                 return;
1542         }
1543
1544         if (g_st_ts.u8_scan_mode_state == RM_SCAN_PRE_IDLE_MODE) {
1545                 g_st_ts.u8_scan_mode_state = RM_SCAN_ACTIVE_MODE;
1546                 mutex_unlock(&g_st_ts.mutex_scan_mode);
1547                 return;
1548         }
1549
1550         if (g_st_ts.u8_scan_mode_state == RM_SCAN_IDLE_MODE) {
1551                 rm_tch_ctrl_leave_auto_mode();
1552                 g_st_ts.u8_scan_mode_state = RM_SCAN_ACTIVE_MODE;
1553                 mutex_unlock(&g_st_ts.mutex_scan_mode);
1554                 usleep_range(10000, 10050);/*msleep(10);*/
1555                 return;
1556         }
1557
1558         mutex_unlock(&g_st_ts.mutex_scan_mode);
1559 }
1560
1561 static u32 rm_tch_get_platform_id(u8 *p)
1562 {
1563         u32 u32Ret;
1564         struct rm_spi_ts_platform_data *pdata;
1565         pdata = g_input_dev->dev.parent->platform_data;
1566
1567         u32Ret = copy_to_user(p, &pdata->platform_id,
1568                 sizeof(pdata->platform_id));
1569
1570         return u32Ret;
1571 }
1572
1573 static u32 rm_tch_get_gpio_sensor_select(u8 *p)
1574 {
1575         u32 u32Ret = 0;
1576         struct rm_spi_ts_platform_data *pdata;
1577         pdata = g_input_dev->dev.parent->platform_data;
1578
1579 /* Read from GPIO
1580         u32Ret = gpio_set_value(pdata->gpio_sensor_select0)
1581                 | (1 << gpio_set_value(pdata->gpio_sensor_select1));
1582 */
1583
1584         /* Read from data struct */
1585         if (pdata->gpio_sensor_select0)
1586                 u32Ret |= 0x01;
1587         if (pdata->gpio_sensor_select1)
1588                 u32Ret |= 0x02;
1589
1590         if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
1591                 rm_printk("Raydium - %s : %d\n",
1592                         __func__, u32Ret);
1593
1594         u32Ret = copy_to_user(p, &u32Ret, sizeof(u32Ret));
1595
1596         return u32Ret;
1597 }
1598
1599 static u32 rm_tch_get_spi_lock_status(u8 *p)
1600 {
1601         u32 u32Ret;
1602
1603         if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
1604                 rm_printk("Raydium - SPI_LOCK = %d\n", g_st_ts.u8_spi_locked);
1605
1606         u32Ret = copy_to_user(p, &g_st_ts.u8_spi_locked,
1607                                         sizeof(g_st_ts.u8_spi_locked));
1608
1609         if (g_st_ts.u8_spi_locked && g_st_ts.u8_resume_cnt)
1610                 g_st_ts.u8_resume_cnt--;
1611
1612         return u32Ret;
1613 }
1614
1615 /*===========================================================================*/
1616 static int rm_tch_ts_send_signal(int pid, int i_info)
1617 {
1618         struct siginfo info;
1619         struct task_struct *t;
1620         int ret = RETURN_OK;
1621
1622         if (!pid) {
1623                 dev_err(&g_spi->dev, "Raydium - %s : pid failed\n", __func__);
1624                 return RETURN_FAIL;
1625         }
1626
1627         /* send the signal */
1628         memset(&info, 0, sizeof(struct siginfo));
1629         info.si_signo = RM_TS_SIGNAL;
1630         info.si_code = SI_QUEUE;
1631         /*
1632                 this is bit of a trickery: SI_QUEUE is normally
1633                 used by sigqueue from user space, and kernel
1634                 space should use SI_KERNEL. But if SI_KERNEL
1635                 is used the real_time data is not delivered to
1636                 the user space signal handler function.
1637         */
1638         info.si_int = i_info;   /*real time signals may have 32 bits of data.*/
1639
1640         rcu_read_lock();
1641         t = find_task_by_vpid(pid);
1642         rcu_read_unlock();
1643         if (t == NULL) {
1644                 dev_err(&g_spi->dev, "Raydium - %s : no such pid\n", __func__);
1645                 return RETURN_FAIL;
1646         } else /*send the signal*/
1647                 ret = send_sig_info(RM_TS_SIGNAL, &info, t);
1648
1649         if (ret)
1650                 dev_err(&g_spi->dev, "Raydium - %s : send sig failed err:%d\n",
1651                         __func__, ret);
1652
1653         return ret;
1654 }
1655
1656 /*=============================================================================
1657         Description:
1658                 Queuing functions.
1659         Input:
1660                 N/A
1661         Output:
1662                 0:succeed
1663                 others:error code
1664 =============================================================================*/
1665 static void rm_tch_queue_reset(void)
1666 {
1667         g_st_q.u16_rear = 0;
1668         g_st_q.u16_front = 0;
1669 }
1670
1671 static int rm_tch_queue_init(void)
1672 {
1673         rm_tch_queue_reset();
1674         g_st_q.p_u8_queue = kmalloc(QUEUE_COUNT * RM_RAW_DATA_LENGTH,
1675                                                                 GFP_KERNEL);
1676         if (g_st_q.p_u8_queue == NULL)
1677                 return -ENOMEM;
1678
1679         return RETURN_OK;
1680 }
1681
1682 static void rm_tch_queue_free(void)
1683 {
1684         if (!g_st_q.p_u8_queue)
1685                 return;
1686         kfree(g_st_q.p_u8_queue);
1687         g_st_q.p_u8_queue = NULL;
1688 }
1689
1690 #ifdef ENABLE_CALC_QUEUE_COUNT
1691 static int rm_tch_queue_get_current_count(void)
1692 {
1693         if (g_st_q.u16_rear >= g_st_q.u16_front)
1694                 return g_st_q.u16_rear - g_st_q.u16_front;
1695
1696         return (QUEUE_COUNT - g_st_q.u16_front) + g_st_q.u16_rear;
1697 }
1698 #endif
1699
1700 /*=============================================================================
1701         Description:
1702         About full/empty buffer distinction,
1703         There are a number of solutions like:
1704         1.Always keep one slot open.
1705         2.Use a fill count to distinguish the two cases.
1706         3.Use read and write counts to get the fill count from.
1707         4.Use absolute indices.
1708         we chose "keep one slot open" to make it simple and robust
1709         and also avoid race condition.
1710         Input:
1711                 N/A
1712         Output:
1713                 1:empty
1714                 0:not empty
1715 =============================================================================*/
1716 static int rm_tch_queue_is_empty(void)
1717 {
1718         if (g_st_q.u16_rear == g_st_q.u16_front)
1719                 return TRUE;
1720         return FALSE;
1721 }
1722
1723 /*=============================================================================
1724         Description:
1725         check queue full.
1726         Input:
1727                 N/A
1728         Output:
1729                 1:full
1730                 0:not full
1731 =============================================================================*/
1732 static int rm_tch_queue_is_full(void)
1733 {
1734         u16 u16_front = g_st_q.u16_front;
1735
1736         if (g_st_q.u16_rear + 1 == u16_front)
1737                 return TRUE;
1738
1739         if ((g_st_q.u16_rear == (QUEUE_COUNT - 1)) && (u16_front == 0))
1740                 return TRUE;
1741
1742         return FALSE;
1743 }
1744
1745 static void *rm_tch_enqueue_start(void)
1746 {
1747         if (!g_st_q.p_u8_queue) /*error handling for no memory*/
1748                 return NULL;
1749
1750         if (!rm_tch_queue_is_full()) {
1751                 g_service_busy_report_count = 100;
1752                 return &g_st_q.p_u8_queue[g_st_q.u16_rear];
1753         }
1754
1755         if (g_service_busy_report_count < 0) {
1756                 g_service_busy_report_count = 100;
1757                 rm_printk("Raydium - touch service is busy,try again.\n");
1758         } else {
1759                 g_service_busy_report_count--;
1760         }
1761         return NULL;
1762 }
1763
1764 static void rm_tch_enqueue_finish(void)
1765 {
1766         if (g_st_q.u16_rear == (QUEUE_COUNT - 1))
1767                 g_st_q.u16_rear = 0;
1768         else
1769                 g_st_q.u16_rear++;
1770 }
1771
1772 static void *rm_tch_dequeue_start(void)
1773 {
1774         if (!rm_tch_queue_is_empty())
1775                 return &g_st_q.p_u8_queue[g_st_q.u16_front];
1776
1777         return NULL;
1778 }
1779
1780 static void rm_tch_dequeue_finish(void)
1781 {
1782         if (g_st_q.u16_front == (QUEUE_COUNT - 1))
1783                 g_st_q.u16_front = 0;
1784         else
1785                 g_st_q.u16_front++;
1786 }
1787
1788 static long rm_tch_queue_read_raw_data(u8 *p, u32 u32Len)
1789 {
1790         u8 *p_u8_queue;
1791         u32 u32Ret;
1792         p_u8_queue = rm_tch_dequeue_start();
1793         if (!p_u8_queue)
1794                 return RETURN_FAIL;
1795
1796         u32Ret = copy_to_user(p, p_u8_queue, u32Len);
1797         if (u32Ret)
1798                 return RETURN_FAIL;
1799
1800         rm_tch_dequeue_finish();
1801         return RETURN_OK;
1802 }
1803
1804 /*===========================================================================*/
1805 #if (ISR_POST_HANDLER == WORK_QUEUE)
1806 static void rm_work_handler(struct work_struct *work)
1807 {
1808         void *p_kernel_buffer;
1809         u32 u32_flag;
1810         int i_ret;
1811
1812         if (!g_st_ts.b_init_finish
1813                 || g_st_ts.b_is_suspended
1814                 || g_worker_queue_is_flush)
1815                 return;
1816
1817         i_ret = rm_tch_ctrl_clear_int();
1818
1819 #ifdef ENABLE_SLOW_SCAN
1820         if (g_st_ts.b_slow_scan_flg == true) {
1821                 rm_tch_cmd_process((u8)(g_st_ts.u32_slow_scan_level - 1),
1822                 g_st_rm_slow_scan_cmd, NULL);
1823                 g_st_ts.b_slow_scan_flg = false;
1824         }
1825 #endif ENABLE_SLOW_SCAN
1826
1827         u32_flag = rm_tch_ctrl_configure();
1828
1829         if (u32_flag & RM_NEED_TO_SEND_SCAN)
1830                 rm_tch_ctrl_scan_start();
1831
1832         if (u32_flag & RM_NEED_TO_READ_RAW_DATA) {
1833                 p_kernel_buffer = rm_tch_enqueue_start();
1834                 if (p_kernel_buffer) {
1835                         i_ret = rm_tch_read_image_data((u8 *) p_kernel_buffer);
1836                         if (!i_ret)
1837                                 rm_tch_enqueue_finish();
1838                 }
1839         }
1840
1841         if (u32_flag & RM_NEED_TO_SEND_SIGNAL) {
1842                 if (g_st_ts.b_calc_finish) {
1843                         g_st_ts.b_calc_finish = 0;
1844                         rm_tch_ts_send_signal(g_st_ts.u32_hal_pid,
1845                                 RM_SIGNAL_INTR);
1846                 }
1847         }
1848 }
1849 #elif (ISR_POST_HANDLER == KTHREAD)
1850 static int rm_work_thread_function(void *data)
1851 {
1852         void *p_kernel_buffer;
1853         u32 u32_flag;
1854         int i_ret;
1855
1856         sched_setscheduler(current_thread_info()->task,
1857                 SCHED_FIFO, &g_st_ts.thread_sched);
1858
1859         do {
1860                 wait_event_interruptible(g_st_ts.rm_thread_wait_q,
1861                         g_st_ts.b_thread_active);
1862
1863                 if (!g_st_ts.b_init_finish || g_st_ts.b_is_suspended)
1864                         continue;
1865
1866                 i_ret = rm_tch_ctrl_clear_int();
1867
1868                 u32_flag = rm_tch_ctrl_configure();
1869
1870                 if (u32_flag & RM_NEED_TO_SEND_SCAN)
1871                         rm_tch_ctrl_scan_start();
1872
1873                 if (u32_flag & RM_NEED_TO_READ_RAW_DATA) {
1874                         p_kernel_buffer = rm_tch_enqueue_start();
1875                         if (p_kernel_buffer) {
1876                                 i_ret = rm_tch_read_image_data(
1877                                         (u8 *) p_kernel_buffer);
1878                                 if (!i_ret)
1879                                         rm_tch_enqueue_finish();
1880                         }
1881                 }
1882
1883                 if (u32_flag & RM_NEED_TO_SEND_SIGNAL) {
1884                         if (g_st_ts.b_calc_finish) {
1885                                 g_st_ts.b_calc_finish = 0;
1886                                 rm_tch_ts_send_signal(g_st_ts.u32_hal_pid,
1887                                         RM_SIGNAL_INTR);
1888                         }
1889                 }
1890                 mutex_lock(&g_st_ts.mutex_irq_wait);
1891                 if (!g_st_ts.b_irq_is_waited)
1892                         g_st_ts.b_thread_active = false;
1893                 else
1894                         g_st_ts.b_irq_is_waited = false;
1895                 mutex_unlock(&g_st_ts.mutex_irq_wait);
1896         } while (!kthread_should_stop());
1897
1898         return 0;
1899 }
1900 #endif
1901
1902 static void rm_tch_init_ts_structure_part(void)
1903 {
1904         g_st_ts.b_init_finish = 0;
1905         g_st_ts.b_calc_finish = 0;
1906         g_st_ts.b_enable_scriber = 0;
1907
1908 #ifdef ENABLE_SLOW_SCAN
1909         g_st_ts.b_enable_slow_scan = false;
1910         g_st_ts.b_slow_scan_flg = false;
1911 #endif
1912         g_st_ts.u8_scan_mode_state = RM_SCAN_ACTIVE_MODE;
1913
1914         g_st_ctrl.u8_event_report_mode = EVENT_REPORT_MODE_STYLUS_ERASER_FINGER;
1915
1916         g_pu8_burstread_buf = NULL;
1917 #if (ISR_POST_HANDLER == WORK_QUEUE)
1918         g_worker_queue_is_flush = false;
1919 #elif (ISR_POST_HANDLER == KTHREAD)
1920         g_st_ts.b_irq_is_waited = false;
1921 #endif
1922         g_timer_queue_is_flush = false;
1923         g_st_ts.u16_read_para = 0;
1924
1925         rm_ctrl_watchdog_func(0);
1926
1927         g_st_ts.b_is_suspended = 0;
1928         /*g_st_ts.u8_test_mode = false;*/
1929         g_st_ts.u8_test_mode_type = RM_TEST_MODE_NULL;
1930
1931         b_bl_updated = false;
1932 }
1933
1934 /*===========================================================================*/
1935 static void rm_ctrl_watchdog_func(unsigned int u32_enable)
1936 {
1937         g_st_ts.u8_watch_dog_flg = 0;
1938         g_st_ts.u32_watch_dog_cnt = 0;
1939         g_st_ts.b_watch_dog_check = 0;
1940
1941         g_st_ts.u8_watch_dog_enable = u32_enable & 0x01;
1942         g_st_ts.u32_watch_dog_time = u32_enable >> 16;
1943
1944         if (u32_enable&0x01) {
1945                 g_st_ts.u8_watch_dog_enable = 1;
1946                 g_st_ts.u32_watch_dog_time = u32_enable >> 16;
1947         } else {
1948                 g_st_ts.u8_watch_dog_enable = 0;
1949                 g_st_ts.u32_watch_dog_time = 0xFFFFFFFF;
1950         }
1951
1952         if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
1953                 rm_printk("Raydium - WatchDogEnable=%d, WatchDogTime=%d\n",
1954                         g_st_ts.u8_watch_dog_enable,
1955                         g_st_ts.u32_watch_dog_time);
1956 }
1957
1958 static void rm_watchdog_work_function(unsigned char scan_mode)
1959 {
1960         if ((g_st_ts.u8_watch_dog_enable == 0) ||
1961                 (g_st_ts.b_init_finish == 0))
1962                 return;
1963
1964         if (g_st_ts.u32_watch_dog_cnt++ >= g_st_ts.u32_watch_dog_time) {
1965                 if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
1966                         rm_printk("Raydium - watchdog work: Time:%dsec Cnt:%d,Flg:%d(%x)\n",
1967                                 g_st_ts.u32_watch_dog_time,
1968                                 g_st_ts.u32_watch_dog_cnt,
1969                                 g_st_ts.u8_watch_dog_flg,
1970                                 g_st_ts.u8_scan_mode_state);
1971
1972                 switch (scan_mode) {
1973                 case RM_SCAN_ACTIVE_MODE:
1974                         g_st_ts.u32_watch_dog_cnt = 0;
1975                         g_st_ts.u8_watch_dog_flg = 1;
1976                         break;
1977                 case RM_SCAN_IDLE_MODE:
1978                         g_st_ts.u32_watch_dog_cnt = 0;
1979                         g_st_ts.b_watch_dog_check = 1;
1980                         break;
1981                 }
1982         }
1983
1984         if (g_st_ts.u8_watch_dog_flg) {
1985                 /*WATCH DOG RESET*/
1986                 /*rm_printk("Raydium - WatchDog Resume\n");*/
1987                 rm_tch_init_ts_structure_part();
1988                 g_st_ts.b_is_suspended = true;
1989                 rm_tch_cmd_process(0, g_st_rm_watchdog_cmd, NULL);
1990                 g_st_ts.b_is_suspended = false;
1991                 return;
1992         }
1993
1994 }
1995
1996 static u8 rm_timer_trigger_function(void)
1997 {
1998         static u32 u32TimerCnt; /*= 0; remove by checkpatch*/
1999
2000         if (u32TimerCnt++ < g_st_ctrl.u8_timer_trigger_scale) {
2001                 return FALSE;
2002         } else {
2003                 /*rm_printk("Raydium - rm_timer_work_handler:%x,%x\n",
2004                 g_st_ctrl.u8_timer_trigger_scale, u32TimerCnt);*/
2005                 u32TimerCnt = 0;
2006                 return TRUE;
2007         }
2008 }
2009
2010 static void rm_timer_work_handler(struct work_struct *work)
2011 {
2012         static u16 u32TimerCnt; /*= 0; remove by checkpatch*/
2013         if (g_st_ts.b_is_suspended)
2014                 return;
2015
2016         if (g_timer_queue_is_flush == true)
2017                 return;
2018
2019         if (rm_timer_trigger_function()) {
2020                 mutex_lock(&g_st_ts.mutex_scan_mode);
2021                 if (g_st_ts.u8_scan_mode_state != RM_SCAN_ACTIVE_MODE)
2022                         rm_watchdog_work_function(RM_SCAN_IDLE_MODE);
2023                 else
2024                         rm_watchdog_work_function(RM_SCAN_ACTIVE_MODE);
2025                 mutex_unlock(&g_st_ts.mutex_scan_mode);
2026         }
2027
2028         if (g_st_ts.b_watch_dog_check == 1) {
2029                 rm_tch_ts_send_signal(g_st_ts.u32_hal_pid,
2030                         RM_SIGNAL_WATCH_DOG_CHECK);
2031                 g_st_ts.b_watch_dog_check = 0;
2032         }
2033
2034         if (g_st_ts.u8_watch_dog_enable) {
2035                 u32TimerCnt++;
2036                 if (u32TimerCnt > g_st_ts.u32_watch_dog_time &&
2037                         g_st_ts.u32_watch_dog_time !=
2038                                 g_st_ctrl.u8_watch_dog_normal_cnt) {
2039                         g_st_ts.u32_watch_dog_time =
2040                                 g_st_ctrl.u8_watch_dog_normal_cnt;
2041                         if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
2042                                 rm_printk("Raydium - WDT:Normal mode\n");
2043                         u32TimerCnt = 0;
2044                 }
2045         } else {
2046                 u32TimerCnt = 0;
2047         }
2048 }
2049
2050 /*========================================================================= */
2051 static void rm_tch_enable_irq(struct rm_tch_ts *ts)
2052 {
2053         enable_irq(ts->irq);
2054 }
2055
2056 static void rm_tch_disable_irq(struct rm_tch_ts *ts)
2057 {
2058         disable_irq(ts->irq);
2059 }
2060
2061 #ifdef ENABLE_SLOW_SCAN
2062 /*=============================================================================
2063  * Description:
2064  *              Context dependent touch system.
2065  *              Change scan speed for slowscan function.
2066  *              Change scan speed flow: (by CY,20120305)
2067  *              1.Disable auto scan ([0x09]bit4=0,[0x09]bit6=1)
2068  *              2.Clear Scan start bit ([0x11]bit0=0)
2069  *              3.Read Scan start bit until it equals 0
2070  *              4.Set LACTIVE and YACTIVE configuration
2071  *              5.Enable autoscan ([0x09]bit4=1,[0x09]bit6=1)
2072  *              6.Sleep 1 minisecond.
2073  *              7.Set Scan start bit ([0x11]bit0=1)
2074  * Input:
2075  *              N/A
2076  * Output:
2077  *              N/A
2078  *===========================================================================*/
2079 static void rm_tch_ctrl_slowscan(u32 level)
2080 {
2081         if (level == RM_SLOW_SCAN_LEVEL_NORMAL)
2082                 level = RM_SLOW_SCAN_LEVEL_20;
2083         if (level > RM_SLOW_SCAN_LEVEL_100)
2084                 level = RM_SLOW_SCAN_LEVEL_MAX;
2085
2086         g_st_ts.u32_slow_scan_level = level;
2087         g_st_ts.b_slow_scan_flg = true;
2088
2089         if (g_st_ts.u8_scan_mode_state == RM_SCAN_IDLE_MODE) {
2090                 g_st_ts.u8_scan_mode_state = RM_SCAN_PRE_IDLE_MODE;
2091                 rm_tch_ctrl_leave_auto_mode();
2092         }
2093 }
2094
2095 static u32 rm_tch_slowscan_round(u32 val)
2096 {
2097         u32 i;
2098
2099         for (i = 0; i < RM_SLOW_SCAN_LEVEL_COUNT; i++) {
2100                 if ((i * RM_SLOW_SCAN_INTERVAL) >= val)
2101                         break;
2102         }
2103
2104         if (i > RM_SLOW_SCAN_LEVEL_MAX)
2105                 i = RM_SLOW_SCAN_LEVEL_MAX;
2106
2107         return i;
2108 }
2109
2110 static ssize_t rm_tch_slowscan_handler(const char *buf, size_t count)
2111 {
2112         unsigned long val;
2113         ssize_t error;
2114         ssize_t ret;
2115
2116         if (count < 2)
2117                 return -EINVAL;
2118
2119         ret = (ssize_t) count;
2120
2121         if (count == 2) {
2122                 if (buf[0] == '0') {
2123                         mutex_lock(&g_st_ts.mutex_scan_mode);
2124                         g_st_ts.b_enable_slow_scan = false;
2125                         rm_tch_ctrl_slowscan(RM_SLOW_SCAN_LEVEL_MAX);
2126                         mutex_unlock(&g_st_ts.mutex_scan_mode);
2127                 } else if (buf[0] == '1') {
2128                         mutex_lock(&g_st_ts.mutex_scan_mode);
2129                         g_st_ts.b_enable_slow_scan = true;
2130                         rm_tch_ctrl_slowscan(RM_SLOW_SCAN_LEVEL_60);
2131                         mutex_unlock(&g_st_ts.mutex_scan_mode);
2132                 }
2133         } else if ((buf[0] == '2') && (buf[1] == ' ')) {
2134                 error = kstrtoul(&buf[2], 10, &val);
2135
2136                 if (error) {
2137                         ret = error;
2138                 } else {
2139                         mutex_lock(&g_st_ts.mutex_scan_mode);
2140                         g_st_ts.b_enable_slow_scan = true;
2141                         rm_tch_ctrl_slowscan(rm_tch_slowscan_round((u32)val));
2142                         mutex_unlock(&g_st_ts.mutex_scan_mode);
2143                 }
2144         }
2145         return ret;
2146 }
2147 #endif
2148
2149 static ssize_t rm_tch_slowscan_show(struct device *dev,
2150         struct device_attribute *attr, char *buf)
2151 {
2152 #ifdef ENABLE_SLOW_SCAN
2153         return sprintf(buf, "Slow Scan:%s\nScan Rate:%dHz\n",
2154                         g_st_ts.b_enable_slow_scan ?
2155                         "Enabled" : "Disabled",
2156                         g_st_ts.u32_slow_scan_level * RM_SLOW_SCAN_INTERVAL);
2157 #else
2158         return sprintf(buf, "Not implemented yet\n");
2159 #endif
2160 }
2161
2162 static ssize_t rm_tch_slowscan_store(struct device *dev,
2163         struct device_attribute *attr,
2164         const char *buf, size_t count)
2165 {
2166 #ifdef ENABLE_SLOW_SCAN
2167         return rm_tch_slowscan_handler(buf, count);
2168 #else
2169         return count;
2170 #endif
2171 }
2172
2173 static ssize_t rm_tch_touchfile_check_show(struct device *dev,
2174         struct device_attribute *attr,
2175         char *buf)
2176 {
2177         return sprintf(buf, "0x%x\n",
2178                 g_st_ts.u8_touchfile_check);
2179 }
2180
2181 static ssize_t rm_tch_touchfile_check_store(struct device *dev,
2182         struct device_attribute *attr,
2183         const char *buf, size_t count)
2184 {
2185         return count;
2186 }
2187
2188 static ssize_t rm_tch_touch_event_show(struct device *dev,
2189         struct device_attribute *attr,
2190         char *buf)
2191 {
2192         return sprintf(buf, "0x%x\n",
2193                 g_st_ts.u8_touch_event);
2194 }
2195
2196 static ssize_t rm_tch_touch_event_store(struct device *dev,
2197         struct device_attribute *attr,
2198         const char *buf, size_t count)
2199 {
2200         return count;
2201 }
2202
2203 static void rm_tch_smooth_level_change(unsigned long val)
2204 {
2205         int i_info;
2206
2207         if (val > RM_SMOOTH_LEVEL_MAX)
2208                 return;
2209
2210         g_st_ts.u32_smooth_level = val;
2211
2212         i_info = (RM_SIGNAL_PARA_SMOOTH << 24) |
2213                         (val << 16) |
2214                         RM_SIGNAL_CHANGE_PARA;
2215
2216         rm_tch_ts_send_signal(g_st_ts.u32_hal_pid, i_info);
2217 }
2218
2219 static ssize_t rm_tch_smooth_level_handler(const char *buf, size_t count)
2220 {
2221         unsigned long val;
2222         ssize_t error;
2223         ssize_t ret;
2224
2225         if (count != 2)
2226                 return -EINVAL;
2227
2228         ret = (ssize_t) count;
2229         error = kstrtoul(buf, 10, &val);
2230
2231         if (error)
2232                 ret = error;
2233         else
2234                 rm_tch_smooth_level_change(val);
2235
2236         return ret;
2237 }
2238
2239 static ssize_t rm_tch_smooth_level_show(struct device *dev,
2240         struct device_attribute *attr,
2241         char *buf)
2242 {
2243         return sprintf(buf, "Smooth level:%d\n", g_st_ts.u32_smooth_level);
2244 }
2245
2246 static ssize_t rm_tch_smooth_level_store(struct device *dev,
2247         struct device_attribute *attr,
2248         const char *buf, size_t count)
2249 {
2250         rm_tch_smooth_level_handler(buf, count);
2251         return count;
2252 }
2253
2254 void rm_set_kernel_test_para(u8 u8Idx, u8 u8Para)
2255 {
2256         struct rm_tch_ts *ts = input_get_drvdata(g_input_dev);
2257
2258         ts->u8_repeat_counter = u8Para;
2259         rm_tch_cmd_process(u8Idx, g_st_rm_kl_testmode_cmd, ts);
2260 }
2261
2262 static ssize_t rm_tch_testmode_handler(const char *buf, size_t count)
2263 {
2264         unsigned long val = 0;
2265         ssize_t error;
2266         ssize_t ret;
2267
2268         if (count < 2)
2269                 return -EINVAL;
2270
2271         ret = (ssize_t) count;
2272
2273         if (count == 2) {
2274                 if (buf[0] == '0') {
2275                         g_st_ts.u8_test_mode = false;
2276                         g_st_ts.u8_test_mode_type = RM_TEST_MODE_NULL;
2277                         rm_set_kernel_test_para(0, g_st_ctrl.u8_idle_mode_thd);
2278                 } else if (buf[0] == '1') {
2279                         g_st_ts.u8_test_mode = true;
2280                         g_st_ts.u8_test_mode_type = RM_TEST_MODE_IDLE_SHOW;
2281                 }
2282         } else if ((buf[0] == '2') && (buf[1] == ' ')) {
2283                 error = kstrtoul(&buf[2], 10, &val);
2284
2285                 if (error) {
2286                         if ((buf[2] == '2') && (buf[3] == ' ')) {
2287                                 g_st_ts.u8_test_mode = true;
2288                                 g_st_ts.u8_test_mode_type =
2289                                 RM_TEST_MODE_IDLE_LEVEL;
2290                                 error = kstrtoul(&buf[4], 10, &val);
2291                                 if (error) {
2292                                         g_st_ts.u8_test_mode = false;
2293                                         ret = error;
2294                                 } else {
2295                                         rm_set_kernel_test_para(0, val);
2296                                 }
2297                         } else {
2298                                 g_st_ts.u8_test_mode = false;
2299                                 ret = error;
2300                         }
2301                 } else {
2302                 g_st_ts.u8_test_mode = true;
2303                 g_st_ts.u8_test_mode_type = 1 << ((u8)val - 1);
2304                 switch (val) {
2305                 case RM_TEST_MODE_IDLE_SHOW:
2306                 if (g_st_ts.u8_scan_mode_state == RM_SCAN_IDLE_MODE)
2307 #if (ISR_POST_HANDLER == WORK_QUEUE)
2308                         queue_work(g_st_ts.rm_workqueue,
2309                                 &g_st_ts.rm_work);
2310 #elif (ISR_POST_HANDLER == KTHREAD)
2311                 {
2312                 if (waitqueue_active(&g_st_ts.rm_thread_wait_q)) {
2313                         g_st_ts.b_thread_active = true;
2314                         wake_up_interruptible(&g_st_ts.rm_thread_wait_q);
2315                 } else {
2316                 mutex_lock(&g_st_ts.mutex_irq_wait);
2317                 g_st_ts.b_irq_is_waited = true;
2318                 mutex_unlock(&g_st_ts.mutex_irq_wait);
2319                 }
2320                 }
2321 #endif
2322                 break;
2323                 case RM_TEST_MODE_IDLE_LEVEL:
2324                         if ((buf[2] == '2') && (buf[3] == ' ')) {
2325                                 error = kstrtoul(&buf[4], 0, &val);
2326                                 if (error) {
2327                                         g_st_ts.u8_test_mode = false;
2328                                         ret = error;
2329                                 } else {
2330                                         rm_set_kernel_test_para(0, val);
2331                                 }
2332                         }
2333                         break;
2334                 case RM_TEST_MODE_CALC_TIME_SHOW:
2335                         break;
2336                 default:
2337                         g_st_ts.u8_test_mode = false;
2338                         g_st_ts.u8_test_mode_type = 0;
2339                 break;
2340                 }
2341                 }
2342         }
2343         rm_printk("Raydium - rm_kernel_test_mode:%s,Type:%d,Para:%d",
2344                 g_st_ts.u8_test_mode ?
2345                 "Enabled" : "Disabled",
2346                 g_st_ts.u8_test_mode_type, (u8)val);
2347                         return ret;
2348 }
2349
2350 static ssize_t rm_tch_test_mode_show(struct device *dev,
2351         struct device_attribute *attr,
2352         char *buf)
2353 {
2354                 return sprintf(buf, "Test Mode:%s\nType:%d\n",
2355                         g_st_ts.u8_test_mode ?
2356                         "Enabled" : "Disabled",
2357                         g_st_ts.u8_test_mode_type);
2358 }
2359
2360 static ssize_t rm_tch_test_mode_store(struct device *dev,
2361         struct device_attribute *attr,
2362         const char *buf, size_t count)
2363 {
2364         g_u8_test_mode_count = count;
2365         memcpy(&g_u8_test_mode_buf, &buf, sizeof(buf));
2366
2367         return rm_tch_testmode_handler(buf, count);
2368 }
2369
2370 static ssize_t rm_tch_self_test_handler(struct rm_tch_ts *ts,
2371         const char *buf, size_t count)
2372 {
2373         unsigned long val;
2374         ssize_t error;
2375         ssize_t ret;
2376         int i_info;
2377
2378         ret = (ssize_t) count;
2379
2380         if (count != 2)
2381                 return -EINVAL;
2382
2383         if (g_st_ts.u8_selftest_status == RM_SELF_TEST_STATUS_TESTING)
2384                 return ret;
2385
2386         rm_tch_enter_test_mode(1);
2387
2388         g_st_ts.u8_selftest_result = RM_SELF_TEST_RESULT_PASS;
2389
2390         error = kstrtoul(buf, 10, &val);
2391
2392         if (error) {
2393                 ret = error;
2394                 rm_tch_enter_test_mode(0);
2395         } else if (val == 0) {
2396                 rm_tch_enter_test_mode(0);
2397         } else if ((val >= 0x01) && (val <= 0xFF)) {
2398                 i_info = (RM_SIGNAL_PARA_SELF_TEST << 24) |
2399                                 (val << 16) |
2400                                 RM_SIGNAL_CHANGE_PARA;
2401                 rm_tch_ts_send_signal(g_st_ts.u32_hal_pid, i_info);
2402         }
2403
2404         return ret;
2405 }
2406 static ssize_t selftest_platform_id_gpio_set(struct device *dev,
2407         struct device_attribute *attr,
2408         const char *buf, size_t count)
2409 {
2410         return count;
2411 }
2412
2413 static ssize_t selftest_platform_id_gpio_get(struct device *dev,
2414         struct device_attribute *attr,
2415         char *buf)
2416 {
2417         struct rm_spi_ts_platform_data *pdata;
2418
2419         pdata = g_input_dev->dev.parent->platform_data;
2420
2421         buf[0] = (char)pdata->platform_id;
2422
2423         /* Read from data struct */
2424         if (pdata->gpio_sensor_select0)
2425                 buf[1] |= 0x01;
2426         if (pdata->gpio_sensor_select1)
2427                 buf[1] |= 0x02;
2428         return 2;
2429 }
2430
2431 static ssize_t selftest_enable_set(struct device *dev,
2432         struct device_attribute *attr,
2433         const char *buf, size_t count)
2434 {
2435         rm_printk("enter test mode buf[0] = %d\n", buf[0]);
2436         rm_tch_enter_test_mode(buf[0]);
2437         return count;
2438 }
2439
2440 static ssize_t selftest_enable_get(struct device *dev,
2441         struct device_attribute *attr,
2442         char *buf)
2443 {
2444         return 1;
2445 }
2446
2447 static ssize_t rm_tch_self_test_show(struct device *dev,
2448         struct device_attribute *attr,
2449         char *buf)
2450 {
2451         return sprintf(buf, "Self_Test:Status:%d ,Result:%d\n",
2452                                         g_st_ts.u8_selftest_status,
2453                                         g_st_ts.u8_selftest_result);
2454 }
2455
2456 static ssize_t rm_tch_self_test_store(struct device *dev,
2457         struct device_attribute *attr,
2458         const char *buf, size_t count)
2459 {
2460         struct rm_tch_ts *ts = dev_get_drvdata(dev);
2461         rm_tch_self_test_handler(ts, buf, count);
2462         return count;
2463 }
2464
2465 static ssize_t rm_tch_version_show(struct device *dev,
2466         struct device_attribute *attr,
2467         char *buf)
2468 {
2469         return sprintf(buf, "Release V 0x%02X, Test V 0x%02X\n",
2470                 g_st_ts.u8_version,
2471                 g_st_ts.u8_test_version);
2472 }
2473
2474 static ssize_t rm_tch_version_store(struct device *dev,
2475         struct device_attribute *attr,
2476         const char *buf, size_t count)
2477 {
2478         return count;
2479 }
2480
2481 static ssize_t rm_tch_module_detect_show(struct device *dev,
2482         struct device_attribute *attr,
2483         char *buf)
2484 {
2485         return sprintf(buf, "%s\n", "Raydium Touch Module");
2486 }
2487
2488 static ssize_t rm_tch_module_detect_store(struct device *dev,
2489         struct device_attribute *attr,
2490         const char *buf, size_t count)
2491 {
2492         return count;
2493 }
2494
2495 static void rm_tch_report_mode_change(unsigned long val)
2496 {
2497         int i_info;
2498
2499         g_st_ctrl.u8_event_report_mode = (u8)val;
2500
2501         i_info = (RM_SIGNAL_PARA_REPORT_MODE_CHANGE << 24) |
2502                         (g_st_ctrl.u8_event_report_mode << 16) |
2503                         RM_SIGNAL_REPORT_MODE_CHANGE;
2504
2505         rm_tch_ts_send_signal(g_st_ts.u32_hal_pid, i_info);
2506 }
2507
2508 static ssize_t rm_tch_report_mode_handler(const char *buf, size_t count)
2509 {
2510         unsigned long val;
2511         ssize_t error;
2512         ssize_t ret;
2513
2514         if (count < 2)
2515                 return -EINVAL;
2516
2517         ret = (ssize_t) count;
2518         error = kstrtoul(buf, 10, &val);
2519
2520         if (error)
2521                 ret = error;
2522         else {
2523                 if ((val >= EVENT_REPORT_MODE_STYLUS_ERASER_FINGER) &&
2524                         (val < (EVENT_REPORT_MODE_TYPE_NUM |
2525                         EVENT_REPORT_MODE_ALL_TYPE_POINTS)))
2526                         rm_tch_report_mode_change(val);
2527                 else
2528                         ret = RETURN_FAIL;
2529         }
2530
2531         return ret;
2532 }
2533
2534 static ssize_t rm_tch_report_mode_show(struct device *dev,
2535         struct device_attribute *attr,
2536         char *buf)
2537 {
2538         return sprintf(buf, "Raydium Touch Report Mode : 0x%x\n",
2539                 g_st_ctrl.u8_event_report_mode);
2540 }
2541
2542 static ssize_t rm_tch_report_mode_store(struct device *dev,
2543         struct device_attribute *attr,
2544         const char *buf, size_t count)
2545 {
2546         rm_tch_report_mode_handler(buf, count);
2547         return count;
2548 }
2549
2550 static ssize_t selftest_spi_byte_read_get(struct device *dev,
2551         struct device_attribute *attr, char *buf)
2552 {
2553         buf[0] = g_spi_buf[0];
2554         return 2;
2555 }
2556
2557 static ssize_t selftest_spi_byte_read_set(struct device *dev,
2558         struct device_attribute *attr,
2559         const char *buf, size_t count)
2560 {
2561         return rm_tch_spi_byte_read(buf[0], &g_spi_buf[0]);
2562 }
2563
2564 static ssize_t selftest_spi_byte_write_get(struct device *dev,
2565         struct device_attribute *attr, char *buf)
2566 {
2567         return 1;
2568 }
2569
2570 static ssize_t selftest_spi_byte_write_set(struct device *dev,
2571         struct device_attribute *attr,
2572         const char *buf, size_t count)
2573 {
2574         return rm_tch_spi_byte_write(buf[0], buf[1]);
2575 }
2576
2577 static ssize_t selftest_spi_burst_read_get(struct device *dev,
2578         struct device_attribute *attr, char *buf)
2579 {
2580         int ret;
2581
2582         /*rm_printk("selftest_spi_burst_read_get %d", g_spi_bufsize);*/
2583         ret = rm_tch_spi_read(g_spi_addr, buf, g_spi_bufsize);
2584         if (ret == RETURN_OK)
2585                 return g_spi_bufsize;
2586
2587         return 0;
2588 }
2589
2590
2591 static ssize_t selftest_spi_burst_read_set(struct device *dev,
2592         struct device_attribute *attr,
2593         const char *buf, size_t count)
2594 {
2595         /*rm_printk("selftest_spi_burst_read_set %d",count);*/
2596         g_spi_addr = 0;
2597         g_spi_bufsize = 0;
2598         g_spi_addr = buf[0];
2599         g_spi_bufsize = buf[1];
2600         g_spi_bufsize <<= 8;
2601         g_spi_bufsize |= buf[2];
2602
2603         return 0;
2604 }
2605
2606 static ssize_t selftest_spi_burst_write_get(struct device *dev,
2607         struct device_attribute *attr, char *buf)
2608 {
2609         return 1;
2610 }
2611
2612 static ssize_t selftest_spi_burst_write_set(struct device *dev,
2613         struct device_attribute *attr,
2614         const char *buf, size_t count)
2615 {
2616         return rm_tch_spi_write((u8 *)buf, count);
2617 }
2618
2619 static DEVICE_ATTR(get_platform_id_gpio, 0640,
2620                                         selftest_platform_id_gpio_get,
2621                                         selftest_platform_id_gpio_set);
2622
2623 static DEVICE_ATTR(selftest_enable, 0640,
2624                                         selftest_enable_get,
2625                                         selftest_enable_set);
2626
2627 static DEVICE_ATTR(selftest_spi_byte_read, 0640,
2628                                         selftest_spi_byte_read_get,
2629                                         selftest_spi_byte_read_set);
2630
2631 static DEVICE_ATTR(selftest_spi_byte_write, 0640,
2632                                         selftest_spi_byte_write_get,
2633                                         selftest_spi_byte_write_set);
2634
2635 static DEVICE_ATTR(selftest_spi_burst_read, 0640,
2636                                         selftest_spi_burst_read_get,
2637                                         selftest_spi_burst_read_set);
2638
2639 static DEVICE_ATTR(selftest_spi_burst_write, 0640,
2640                                         selftest_spi_burst_write_get,
2641                                         selftest_spi_burst_write_set);
2642
2643 static DEVICE_ATTR(slowscan_enable, 0640,
2644                                         rm_tch_slowscan_show,
2645                                         rm_tch_slowscan_store);
2646
2647 static DEVICE_ATTR(touchfile_check, 0640,
2648                                         rm_tch_touchfile_check_show,
2649                                         rm_tch_touchfile_check_store);
2650
2651 static DEVICE_ATTR(touch_event, 0640,
2652                                         rm_tch_touch_event_show,
2653                                         rm_tch_touch_event_store);
2654
2655 static DEVICE_ATTR(smooth_level, 0640,
2656                                         rm_tch_smooth_level_show,
2657                                         rm_tch_smooth_level_store);
2658
2659 static DEVICE_ATTR(self_test, 0640,
2660                                         rm_tch_self_test_show,
2661                                         rm_tch_self_test_store);
2662
2663 static DEVICE_ATTR(version, 0640,
2664                                         rm_tch_version_show,
2665                                         rm_tch_version_store);
2666
2667 static DEVICE_ATTR(module_detect, 0640,
2668                                         rm_tch_module_detect_show,
2669                                         rm_tch_module_detect_store);
2670
2671 static DEVICE_ATTR(report_mode, 0640,
2672                                         rm_tch_report_mode_show,
2673                                         rm_tch_report_mode_store);
2674
2675 static DEVICE_ATTR(test_mode, 0640,
2676                                         rm_tch_test_mode_show,
2677                                         rm_tch_test_mode_store);
2678
2679 static struct attribute *rm_ts_attributes[] = {
2680         &dev_attr_get_platform_id_gpio.attr,
2681         &dev_attr_slowscan_enable.attr,
2682         &dev_attr_touchfile_check.attr,
2683         &dev_attr_touch_event.attr,
2684         &dev_attr_selftest_enable.attr,
2685         &dev_attr_selftest_spi_byte_read.attr,
2686         &dev_attr_selftest_spi_byte_write.attr,
2687         &dev_attr_selftest_spi_burst_read.attr,
2688         &dev_attr_selftest_spi_burst_write.attr,
2689         &dev_attr_smooth_level.attr,
2690         &dev_attr_self_test.attr,
2691         &dev_attr_version.attr,
2692         &dev_attr_module_detect.attr,
2693         &dev_attr_report_mode.attr,
2694         &dev_attr_test_mode.attr,
2695         NULL
2696 };
2697
2698 static const struct attribute_group rm_ts_attr_group = {
2699         .attrs = rm_ts_attributes,
2700 };
2701
2702 static int rm_tch_input_open(struct input_dev *input)
2703 {
2704         struct rm_tch_ts *ts = input_get_drvdata(input);
2705
2706         rm_tch_enable_irq(ts);
2707
2708         return RETURN_OK;
2709 }
2710
2711 static void rm_tch_input_close(struct input_dev *input)
2712 {
2713         struct rm_tch_ts *ts = input_get_drvdata(input);
2714
2715         rm_tch_disable_irq(ts);
2716 }
2717
2718 static irqreturn_t rm_tch_irq(int irq, void *handle)
2719 {
2720         g_st_ts.u32_watch_dog_cnt = 0;
2721
2722         trace_touchscreen_raydium_irq("Raydium_interrupt");
2723
2724         if (/*g_st_ctrl.u8_power_mode &&*/
2725                 (g_st_ts.u8_scan_mode_state == RM_SCAN_IDLE_MODE)) {
2726                 input_event(g_input_dev, EV_MSC, MSC_ACTIVITY, 1);
2727 #if (INPUT_PROTOCOL_CURRENT_SUPPORT == INPUT_PROTOCOL_TYPE_B)
2728                 input_sync(g_input_dev);
2729 #endif
2730         }
2731
2732         if (g_st_ts.b_init_service && g_st_ts.b_init_finish
2733                 && g_st_ts.b_is_suspended == false) {
2734 #if (ISR_POST_HANDLER == WORK_QUEUE)
2735                 queue_work(g_st_ts.rm_workqueue, &g_st_ts.rm_work);
2736 #elif (ISR_POST_HANDLER == KTHREAD)
2737                 if (waitqueue_active(&g_st_ts.rm_thread_wait_q)) {
2738                         g_st_ts.b_thread_active = true;
2739                         wake_up_interruptible(&g_st_ts.rm_thread_wait_q);
2740                 } else {
2741                         mutex_lock(&g_st_ts.mutex_irq_wait);
2742                         g_st_ts.b_irq_is_waited = true;
2743                         mutex_unlock(&g_st_ts.mutex_irq_wait);
2744                 }
2745 #endif
2746         }
2747
2748         return IRQ_HANDLED;
2749 }
2750
2751 static void rm_tch_enter_test_mode(u8 flag)
2752 {
2753         if (flag) { /*enter test mode*/
2754                 g_st_ts.b_selftest_enable = 1;
2755                 g_st_ts.u8_selftest_status = RM_SELF_TEST_STATUS_TESTING;
2756                 g_st_ts.b_is_suspended = true;
2757 #if (ISR_POST_HANDLER == WORK_QUEUE)
2758                 flush_workqueue(g_st_ts.rm_workqueue);
2759 #endif
2760                 flush_workqueue(g_st_ts.rm_timer_workqueue);
2761         } else { /*leave test mode*/
2762                 g_st_ts.b_selftest_enable = 0;
2763
2764                 rm_tch_init_ts_structure_part();
2765                 g_st_ts.b_is_suspended = false;
2766         }
2767
2768         rm_tch_cmd_process(flag, g_st_rm_testmode_cmd, NULL);
2769
2770         if (!flag)
2771                 g_st_ts.u8_selftest_status = RM_SELF_TEST_STATUS_FINISH;
2772 }
2773
2774 void rm_tch_set_variable(unsigned int index, unsigned int arg)
2775 {
2776 #if ENABLE_FREQ_HOPPING
2777         ssize_t missing;
2778 #endif
2779         struct rm_tch_ts *ts = input_get_drvdata(g_input_dev);
2780
2781         switch (index) {
2782         case RM_VARIABLE_SELF_TEST_RESULT:
2783                 g_st_ts.u8_selftest_result = (u8) arg;
2784                 rm_tch_enter_test_mode(0);
2785                 break;
2786         case RM_VARIABLE_SCRIBER_FLAG:
2787                 g_st_ts.b_enable_scriber = (bool) arg;
2788                 break;
2789         case RM_VARIABLE_AUTOSCAN_FLAG:
2790                 /*g_st_ctrl.u8_power_mode = (bool) arg;*/
2791                 mutex_lock(&g_st_ts.mutex_scan_mode);
2792                 if (g_st_ts.u8_scan_mode_state == RM_SCAN_ACTIVE_MODE)
2793                         g_st_ts.u8_scan_mode_state = RM_SCAN_PRE_IDLE_MODE;
2794                 mutex_unlock(&g_st_ts.mutex_scan_mode);
2795                 break;
2796         case RM_VARIABLE_TEST_VERSION:
2797                 g_st_ts.u8_test_version = (u8) arg;
2798                 break;
2799         case RM_VARIABLE_VERSION:
2800                 g_st_ts.u8_version = (u8) arg;
2801                 dev_info(&g_spi->dev, "Raydium - Firmware v%d.%d\n",
2802                                 g_st_ts.u8_version, g_st_ts.u8_test_version);
2803                 break;
2804         case RM_VARIABLE_IDLEMODECHECK:
2805                 g_st_ctrl.u8_idle_mode_check = (u8) arg;
2806                 if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
2807                         rm_printk("Raydium - u8_idle_mode_check %2x\n",
2808                                 arg);
2809                 break;
2810         case RM_VARIABLE_REPEAT:
2811                 /*rm_printk("Raydium - Repeat %d\n", arg);*/
2812                 g_st_ts.u8_repeat = (u8) arg;
2813 #if ENABLE_FREQ_HOPPING
2814                 g_st_ts.u8_ns_rpt = (u8) arg;
2815                 if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
2816                         rm_printk("g_st_ctrl.u8_ns_rpt %d\n",
2817                                 g_st_ts.u8_ns_rpt);
2818 #endif
2819                 break;
2820         case RM_VARIABLE_WATCHDOG_FLAG:
2821                 rm_ctrl_watchdog_func(arg);
2822                 g_st_ctrl.u8_ns_func_enable = ((u8)arg >> 6);
2823                 if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
2824                         rm_printk("g_st_ctrl.u8_ns_func_enable %d\n",
2825                                 g_st_ctrl.u8_ns_func_enable);
2826                 break;
2827         case RM_VARIABLE_SET_SPI_UNLOCK:
2828                 if (g_st_ts.u8_resume_cnt > 1)
2829                         break;
2830                 g_st_ts.u8_spi_locked = 0;
2831                 if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
2832                         rm_printk("Raydium - SET_SPI_UNLOCK\n");
2833                 break;
2834         case RM_VARIABLE_SET_WAKE_UNLOCK:
2835                 if (wake_lock_active(&g_st_ts.wakelock_initialization))
2836                         wake_unlock(&g_st_ts.wakelock_initialization);
2837                 break;
2838 #if ENABLE_FREQ_HOPPING
2839         case RM_VARIABLE_DPW:
2840                 mutex_lock(&g_st_ts.mutex_ns_mode);
2841                 missing = copy_from_user(&g_st_ts.u8_ns_para[0],
2842                         ((u8 *)arg), 9);
2843                 /*missing = copy_from_user(&g_st_ts.bDP[0],
2844                         ((u8 *)arg), 3);*/
2845                 /*missing += copy_from_user(&g_st_ts.bSWCPW[0],
2846                         (((u8 *)arg) + 3), 3);*/
2847                 /*missing += copy_from_user(&g_st_ts.bSWC[0],
2848                         (((u8 *)arg) + 6), 3);*/
2849                 mutex_unlock(&g_st_ts.mutex_ns_mode);
2850                 if (missing)
2851                         dev_err(&g_spi->dev, "Raydium - %s RM_VARIABLE_DPW : copy failed - miss:%d\n",
2852                                 __func__, missing);
2853                 /*memcpy(&g_st_ts.bDP[0], ((u8 *)arg), 3);*/
2854                 /*memcpy(&g_st_ts.bSWCPW[0], (((u8 *)arg) + 3), 3);*/
2855                 /*memcpy(&g_st_ts.bSWC[0], (((u8 *)arg) + 6), 3);*/
2856                 if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
2857                         rm_printk("g_st_ctrl.DPW 0x%x:0x%x:0x%x # 0x%x:0x%x:0x%x,# 0x%x:0x%x:0x%x\n",
2858                         /*g_st_ts.bDP[0], g_st_ts.bDP[1],
2859                         g_st_ts.bDP[2], g_st_ts.bSWCPW[0],
2860                         g_st_ts.bSWCPW[1], g_st_ts.bSWCPW[2],
2861                         g_st_ts.bSWC[0], g_st_ts.bSWC[1],
2862                         g_st_ts.bSWC[2]);*/
2863                         g_st_ts.u8_ns_para[0], g_st_ts.u8_ns_para[1],
2864                         g_st_ts.u8_ns_para[2], g_st_ts.u8_ns_para[3],
2865                         g_st_ts.u8_ns_para[4], g_st_ts.u8_ns_para[5],
2866                         g_st_ts.u8_ns_para[6], g_st_ts.u8_ns_para[7],
2867                         g_st_ts.u8_ns_para[8]);
2868                 break;
2869         case RM_VARIABLE_NS_MODE:
2870                 mutex_lock(&g_st_ts.mutex_ns_mode);
2871                 g_st_ts.u8_ns_mode = (u8)arg;
2872                 mutex_unlock(&g_st_ts.mutex_ns_mode);
2873                 if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
2874                         rm_printk("g_st_ctrl.u8_ns_mode=%d\n",
2875                                 g_st_ts.u8_ns_mode);
2876                 break;
2877 #endif
2878         case RM_VARIABLE_TOUCHFILE_STATUS:
2879                 g_st_ts.u8_touchfile_check = (u8)(arg);
2880                 break;
2881
2882         case RM_VARIABLE_TOUCH_EVENT:
2883                 g_st_ts.u8_touch_event = (u8)(arg);
2884                 rm_tch_generate_event(ts, g_st_ts.u8_touch_event);
2885                 break;
2886
2887         default:
2888                 break;
2889         }
2890
2891 }
2892
2893 static u32 rm_tch_get_variable(unsigned int index, u8 *arg)
2894 {
2895         u32 ret = RETURN_OK;
2896         switch (index) {
2897         case RM_VARIABLE_PLATFORM_ID:
2898                 ret = rm_tch_get_platform_id((u8 *) arg);
2899                 break;
2900         case RM_VARIABLE_GPIO_SELECT:
2901                 ret = rm_tch_get_gpio_sensor_select((u8 *) arg);
2902                 break;
2903         case RM_VARIABLE_CHECK_SPI_LOCK:
2904                 ret = rm_tch_get_spi_lock_status((u8 *) arg);
2905                 break;
2906         default:
2907                 ret = -EINVAL;
2908                 break;
2909         }
2910         return ret;
2911 }
2912
2913 static void rm_tch_init_ts_structure(void)
2914 {
2915         g_st_ts.u32_hal_pid = 0;
2916         memset(&g_st_ts, 0, sizeof(struct rm31080a_ts_para));
2917
2918 #ifdef ENABLE_SLOW_SCAN
2919         g_st_ts.u32_slow_scan_level = RM_SLOW_SCAN_LEVEL_MAX;
2920 #endif
2921
2922 #if (ISR_POST_HANDLER == WORK_QUEUE)
2923         g_st_ts.rm_workqueue = create_singlethread_workqueue("rm_work");
2924         INIT_WORK(&g_st_ts.rm_work, rm_work_handler);
2925 #elif (ISR_POST_HANDLER == KTHREAD)
2926         init_waitqueue_head(&g_st_ts.rm_thread_wait_q);
2927         g_st_ts.b_thread_active = false;
2928         g_st_ts.b_thread_alive = false;
2929         mutex_init(&g_st_ts.mutex_irq_wait);
2930 #endif
2931
2932         g_st_ts.rm_timer_workqueue =
2933                 create_singlethread_workqueue("rm_idle_work");
2934         INIT_WORK(&g_st_ts.rm_timer_work, rm_timer_work_handler);
2935
2936         wake_lock_init(&g_st_ts.wakelock_initialization,
2937                 WAKE_LOCK_SUSPEND, "TouchInitialLock");
2938
2939         mutex_init(&g_st_ts.mutex_scan_mode);
2940         mutex_init(&g_st_ts.mutex_ns_mode);
2941
2942         g_st_ts.u8_resume_cnt = 0;
2943         g_st_ts.u8_touchfile_check = 0xFF;
2944         g_st_ts.u8_touch_event = 0xFF;
2945         g_st_ts.b_init_service = false;
2946         g_st_ts.u8_test_mode = false;
2947         rm_tch_ctrl_init();
2948 }
2949
2950 static int rm31080_voltage_notifier_1v8(struct notifier_block *nb,
2951         unsigned long event, void *ignored)
2952 {
2953         if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
2954                 rm_printk("Raydium - REGULATOR EVENT 1.8V:0x%x\n",
2955                         (unsigned int)event);
2956
2957         return NOTIFY_OK;
2958 }
2959
2960 static int rm31080_voltage_notifier_3v3(struct notifier_block *nb,
2961         unsigned long event, void *ignored)
2962 {
2963         if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
2964                 rm_printk("Raydium - REGULATOR EVENT 3.3V:0x%x\n",
2965                         (unsigned int)event);
2966
2967         return NOTIFY_OK;
2968 }
2969
2970 /*===========================================================================*/
2971 static void rm_ctrl_resume(struct rm_tch_ts *ts)
2972 {
2973         g_st_ts.u8_spi_locked = 1;
2974         if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
2975                 rm_printk("Raydium - SPI_LOCKED by resume!!\n");
2976
2977         mutex_lock(&g_st_ts.mutex_scan_mode);
2978         rm_tch_init_ts_structure_part();
2979         rm_tch_cmd_process(0, g_st_rm_resume_cmd, ts);
2980         mutex_unlock(&g_st_ts.mutex_scan_mode);
2981
2982         if (g_st_ts.u8_test_mode)
2983                 rm_tch_testmode_handler(g_u8_test_mode_buf,
2984                         g_u8_test_mode_count);
2985 }
2986
2987 static void rm_ctrl_suspend(struct rm_tch_ts *ts)
2988 {
2989 #if (INPUT_PROTOCOL_CURRENT_SUPPORT == INPUT_PROTOCOL_TYPE_B)
2990         int i;
2991 #endif
2992         if (g_st_ts.b_is_suspended == true)
2993                 return;
2994
2995         g_st_ts.b_is_suspended = true;
2996         g_st_ts.b_init_finish = 0;
2997
2998         if (g_st_ts.u8_scan_mode_state == RM_SCAN_IDLE_MODE)
2999                 rm_ctrl_pause_auto_mode();
3000
3001         rm_tch_ctrl_wait_for_scan_finish(0);
3002
3003 #if (INPUT_PROTOCOL_CURRENT_SUPPORT == INPUT_PROTOCOL_TYPE_B)
3004         for (i = 0; i < MAX_SUPPORT_SLOT_AMOUNT; i++) {
3005                 input_mt_slot(g_input_dev, i);
3006
3007                 input_mt_report_slot_state(
3008                         g_input_dev,
3009                         MT_TOOL_PEN, false);
3010
3011                 input_report_key(
3012                         g_input_dev,
3013                         BTN_STYLUS2, false);
3014         }
3015         input_sync(g_input_dev);
3016 #endif
3017
3018 #if (ISR_POST_HANDLER == KTHREAD)
3019         if (!IS_ERR(g_st_ts.rm_irq_post_thread)) {
3020                 g_st_ts.b_thread_active = true;
3021                 wake_up_interruptible(&g_st_ts.rm_thread_wait_q);
3022                 if (kthread_stop(g_st_ts.rm_irq_post_thread)) {
3023                         if (g_st_ctrl.u8_kernel_msg & DEBUG_KTHREAD)
3024                                 rm_printk("Raydium - Kill IRQ poster failed!\n");
3025                 } else {
3026                         g_st_ts.b_thread_alive = false;
3027                         if (g_st_ctrl.u8_kernel_msg & DEBUG_KTHREAD)
3028                                 rm_printk("Raydium - Kill IRQ poster successfully!\n");
3029                 }
3030         } else {
3031                 if (g_st_ctrl.u8_kernel_msg & DEBUG_KTHREAD)
3032                         rm_printk("Raydium - No IRQ poster exist!\n");
3033         }
3034 #endif
3035         mutex_lock(&g_st_ts.mutex_scan_mode);
3036         rm_tch_cmd_process(0, g_st_rm_suspend_cmd, ts);
3037         rm_tch_ctrl_wait_for_scan_finish(0);
3038         rm_tch_cmd_process(1, g_st_rm_suspend_cmd, ts);
3039         mutex_unlock(&g_st_ts.mutex_scan_mode);
3040         g_st_ts.u8_spi_locked = 1;
3041         if (g_st_ctrl.u8_kernel_msg & DEBUG_DRIVER)
3042                 rm_printk("Raydium - SPI_LOCKED by suspend!!\n");
3043
3044 }
3045
3046 #ifdef CONFIG_PM
3047 static int rm_tch_suspend(struct device *dev)
3048 {
3049         struct rm_tch_ts *ts = dev_get_drvdata(dev);
3050         if (g_st_ts.b_init_service) {
3051                 dev_info(ts->dev, "Raydium - Disable input device\n");
3052                 rm_ctrl_suspend(ts);
3053                 dev_info(ts->dev, "Raydium - Disable input device done\n");
3054         }
3055         return RETURN_OK;
3056 }
3057
3058 static int rm_tch_resume(struct device *dev)
3059 {
3060         struct rm_tch_ts *ts = dev_get_drvdata(dev);
3061
3062         if (g_st_ts.b_init_service) {
3063                 dev_info(ts->dev, "Raydium - Enable input device\n");
3064                 if (wake_lock_active(&g_st_ts.wakelock_initialization))
3065                         wake_unlock(&g_st_ts.wakelock_initialization);
3066                 wake_lock_timeout(&g_st_ts.wakelock_initialization,
3067                         TCH_WAKE_LOCK_TIMEOUT);
3068                 rm_ctrl_resume(ts);
3069         }
3070         return RETURN_OK;
3071 }
3072
3073 #ifdef CONFIG_HAS_EARLYSUSPEND
3074 static void rm_tch_early_suspend(struct early_suspend *es)
3075 {
3076         struct rm_tch_ts *ts;
3077         struct device *dev;
3078
3079 #if !(ENABLE_FB_CALLBACK && defined(CONFIG_FB))
3080         ts = container_of(es, struct rm_tch_ts, early_suspend);
3081         dev = ts->dev;
3082
3083         if (rm_tch_suspend(dev))
3084                 dev_err(dev, "Raydium - %s : failed\n", __func__);
3085 #endif
3086 }
3087
3088 static void rm_tch_early_resume(struct early_suspend *es)
3089 {
3090         struct rm_tch_ts *ts;
3091         struct device *dev;
3092 #if !(ENABLE_FB_CALLBACK && defined(CONFIG_FB))
3093         ts = container_of(es, struct rm_tch_ts, early_suspend);
3094         dev = ts->dev;
3095
3096         if (rm_tch_resume(dev))
3097                 dev_err(dev, "Raydium - %s : failed\n", __func__);
3098 #endif
3099 }
3100 #endif                  /*CONFIG_HAS_EARLYSUSPEND*/
3101 #endif                  /*CONFIG_PM*/
3102
3103 /* NVIDIA 20121026 */
3104 /* support to disable power and clock when display is off */
3105 static int rm_tch_input_enable(struct input_dev *in_dev)
3106 {
3107         int error = RETURN_OK;
3108
3109 #if !(ENABLE_FB_CALLBACK && defined(CONFIG_FB))
3110 #ifdef CONFIG_PM
3111         struct rm_tch_ts *ts = input_get_drvdata(in_dev);
3112
3113         error = rm_tch_resume(ts->dev);
3114         if (error)
3115                 dev_err(ts->dev, "Raydium - %s : failed\n", __func__);
3116 #endif
3117 #endif
3118         return error;
3119 }
3120
3121 static int rm_tch_input_disable(struct input_dev *in_dev)
3122 {
3123         int error = RETURN_OK;
3124
3125 #if !(ENABLE_FB_CALLBACK && defined(CONFIG_FB))
3126 #ifdef CONFIG_PM
3127         struct rm_tch_ts *ts = input_get_drvdata(in_dev);
3128
3129         error = rm_tch_suspend(ts->dev);
3130         if (error)
3131                 dev_err(ts->dev, "Raydium - %s : failed\n", __func__);
3132 #endif
3133 #endif
3134
3135         return error;
3136 }
3137
3138 #if ENABLE_FB_CALLBACK && defined(CONFIG_FB)
3139 static int rm_fb_notifier_callback(struct notifier_block *self,
3140         unsigned long event, void *data)
3141 {
3142         struct fb_event *evdata = data;
3143         int *blank;
3144         struct rm_tch_ts *ts = container_of(self,
3145                                         struct rm_tch_ts, fb_notifier);
3146         if (g_st_ts.b_init_service && evdata && evdata->data
3147                 && event == FB_EVENT_BLANK) {
3148                 blank = evdata->data;
3149                 if (*blank == FB_BLANK_UNBLANK)
3150                         rm_tch_resume(ts->dev);
3151                 else if (*blank == FB_BLANK_POWERDOWN)
3152                         rm_tch_suspend(ts->dev);
3153         }
3154         return RETURN_OK;
3155 }
3156 #endif
3157
3158 #if defined(CONFIG_TRUSTED_LITTLE_KERNEL)
3159 /*===========================================================================*/
3160 void raydium_tlk_ns_touch_suspend(void)
3161 {
3162         struct rm_tch_ts *ts = input_get_drvdata(g_input_dev);
3163
3164         rm_printk("tlk_ns_touch_suspend\n");
3165
3166         rm_tch_enter_manual_mode();
3167         mutex_lock(&g_st_ts.mutex_scan_mode);
3168         mutex_lock(&g_st_ts.mutex_ns_mode);
3169         rm_tch_cmd_process(0, g_st_rm_tlk_cmd, ts);
3170 }
3171 EXPORT_SYMBOL(raydium_tlk_ns_touch_suspend);
3172
3173 /*===========================================================================*/
3174 void raydium_tlk_ns_touch_resume(void)
3175 {
3176         struct rm_tch_ts *ts = input_get_drvdata(g_input_dev);
3177
3178         rm_printk("tlk_ns_touch_resume\n");
3179
3180         rm_tch_ts_send_signal(g_st_ts.u32_hal_pid, 0x03);
3181         rm_tch_cmd_process(1, g_st_rm_tlk_cmd, ts);
3182
3183         mutex_unlock(&g_st_ts.mutex_scan_mode);
3184         mutex_unlock(&g_st_ts.mutex_ns_mode);
3185 }
3186 EXPORT_SYMBOL(raydium_tlk_ns_touch_resume);
3187 /*===========================================================================*/
3188 #endif  /*CONFIG_TRUSTED_LITTLE_KERNEL*/
3189
3190 static void rm_tch_set_input_resolution(unsigned int x, unsigned int y)
3191 {
3192         input_set_abs_params(g_input_dev, ABS_MT_POSITION_X, 0, x - 1, 0, 0);
3193         input_set_abs_params(g_input_dev, ABS_MT_POSITION_Y, 0, y - 1, 0, 0);
3194 }
3195
3196 static struct rm_spi_ts_platform_data *rm_ts_parse_dt(struct device *dev,
3197                                         int irq)
3198 {
3199         struct rm_spi_ts_platform_data *pdata;
3200         struct device_node *np = dev->of_node;
3201         const char *str;
3202         int ret, val, irq_gpio;
3203
3204         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3205         if (!pdata)
3206                 return ERR_PTR(-ENOMEM);
3207
3208         pdata->gpio_reset = of_get_named_gpio_flags(np, "reset-gpio", 0, NULL);
3209         if (!gpio_is_valid(pdata->gpio_reset)) {
3210                 dev_err(dev, "Invalid reset-gpio\n");
3211                 return ERR_PTR(-EINVAL);
3212         }
3213         ret = gpio_request(pdata->gpio_reset, "reset-gpio");
3214         if (ret < 0) {
3215                 dev_err(dev, "gpio_request fail\n");
3216                 return ERR_PTR(-EINVAL);
3217         }
3218         gpio_direction_output(pdata->gpio_reset, 0);
3219
3220         ret = of_property_read_u32(np, "interrupts", &irq_gpio);
3221         if (!gpio_is_valid(irq_gpio)) {
3222                 dev_err(dev, "Invalid irq-gpio\n");
3223                 ret = -EINVAL;
3224                 goto exit_release_reset_gpio;
3225         }
3226
3227         ret = gpio_request(irq_gpio, "irq-gpio");
3228         if (ret < 0) {
3229                 dev_err(dev, "irq_request fail\n");
3230                 ret = -EINVAL;
3231                 goto exit_release_reset_gpio;
3232         }
3233         gpio_direction_input(irq_gpio);
3234
3235         ret = of_property_read_u32(np, "config", &val);
3236         if (ret < 0)
3237                 goto exit_release_all_gpio;
3238         pdata->config = (unsigned char *)val;
3239
3240         ret = of_property_read_u32(np, "platform-id", &val);
3241         if (ret < 0)
3242                 goto exit_release_all_gpio;
3243         pdata->platform_id = val;
3244
3245         ret = of_property_read_string(np, "name-of-clock", &str);
3246         if (ret < 0)
3247                 goto exit_release_all_gpio;
3248         pdata->name_of_clock = (char *)str;
3249
3250         ret = of_property_read_string(np, "name-of-clock-con", &str);
3251         if (ret < 0)
3252                 goto exit_release_all_gpio;
3253         pdata->name_of_clock_con = (char *)str;
3254
3255         return pdata;
3256
3257 exit_release_all_gpio:
3258         gpio_free(irq_gpio);
3259
3260 exit_release_reset_gpio:
3261         gpio_free(pdata->gpio_reset);
3262         return ERR_PTR(ret);
3263 }
3264
3265 struct rm_tch_ts *rm_tch_input_init(struct device *dev, unsigned int irq,
3266         const struct rm_tch_bus_ops *bops)
3267 {
3268         struct rm_tch_ts *ts;
3269         struct input_dev *input_dev;
3270         struct rm_spi_ts_platform_data *pdata;
3271         int err = -EINVAL;
3272
3273         if (!irq) {
3274                 dev_err(dev, "Raydium - no IRQ?\n");
3275                 err = -EINVAL;
3276                 goto err_out;
3277         }
3278
3279         ts = kzalloc(sizeof(struct rm_tch_ts), GFP_KERNEL);
3280
3281         input_dev = input_allocate_device();
3282
3283         if (!ts || !input_dev) {
3284                 dev_err(dev, "Raydium - Failed to allocate memory\n");
3285                 err = -ENOMEM;
3286                 goto err_free_mem;
3287         }
3288
3289         g_input_dev = input_dev;
3290
3291         ts->bops = bops;
3292         ts->dev = dev;
3293         ts->input = input_dev;
3294         ts->irq = irq;
3295
3296
3297         if (dev->of_node) {
3298                 pr_info("Load platform data from DT.\n");
3299                 pdata = rm_ts_parse_dt(dev, irq);
3300                 if (IS_ERR(pdata)) {
3301                         dev_err(&g_spi->dev, "Raydium - failed to parse dt\n");
3302                         err = -EINVAL;
3303                         goto err_free_mem;
3304                 }
3305                 dev->platform_data = pdata;
3306         }
3307
3308         pdata = dev->platform_data;
3309
3310         if (pdata->name_of_clock || pdata->name_of_clock_con) {
3311                 ts->clk = clk_get_sys(pdata->name_of_clock,
3312                         pdata->name_of_clock_con);
3313                 if (IS_ERR(ts->clk)) {
3314                         dev_err(&g_spi->dev, "Raydium - failed to get touch_clk: (%s, %s)\n",
3315                                 pdata->name_of_clock, pdata->name_of_clock_con);
3316                         err = -EINVAL;
3317                         goto err_free_mem;
3318                 }
3319         }
3320
3321         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
3322
3323         input_dev->name = "touch";
3324         input_dev->phys = ts->phys;
3325         input_dev->dev.parent = dev;
3326         input_dev->id.bustype = bops->bustype;
3327
3328         input_dev->enable = rm_tch_input_enable;
3329         input_dev->disable = rm_tch_input_disable;
3330         input_dev->enabled = true;
3331         input_dev->open = rm_tch_input_open;
3332         input_dev->close = rm_tch_input_close;
3333         input_dev->hint_events_per_packet = 256U;
3334
3335         input_set_drvdata(input_dev, ts);
3336         input_set_capability(input_dev, EV_MSC, MSC_ACTIVITY);
3337
3338 #if (INPUT_PROTOCOL_CURRENT_SUPPORT == INPUT_PROTOCOL_TYPE_A)
3339         __set_bit(EV_ABS, input_dev->evbit);
3340         input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 0xFF, 0, 0);
3341         input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, 32, 0, 0);
3342
3343         __set_bit(EV_KEY, input_dev->evbit);
3344         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3345 #else
3346         __set_bit(EV_KEY, input_dev->evbit);
3347         __set_bit(EV_ABS, input_dev->evbit);
3348         input_set_abs_params(input_dev, ABS_MT_PRESSURE,
3349                 0, 0xFF, 0, 0);
3350
3351         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3352 #ifdef INPUT_MT_DIRECT
3353         input_mt_init_slots(input_dev,
3354                 MAX_SUPPORT_SLOT_AMOUNT,
3355                 0);
3356 #else
3357         input_mt_init_slots(input_dev,
3358                 MAX_SUPPORT_SLOT_AMOUNT);
3359 #endif
3360 #endif
3361         input_set_abs_params(input_dev,
3362                 ABS_MT_TOOL_TYPE, 0,
3363                 MT_TOOL_MAX, 0, 0);
3364
3365         rm_tch_set_input_resolution(RM_INPUT_RESOLUTION_X,
3366                                 RM_INPUT_RESOLUTION_Y);
3367
3368         err = request_threaded_irq(ts->irq, NULL, rm_tch_irq,
3369                         IRQF_TRIGGER_RISING | IRQF_ONESHOT, dev_name(dev), ts);
3370         if (err) {
3371                 dev_err(dev, "Raydium - irq %d busy?\n", ts->irq);
3372                 goto err_free_mem;
3373         }
3374         mutex_init(&ts->access_mutex);
3375 #ifdef CONFIG_HAS_EARLYSUSPEND
3376         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
3377         ts->early_suspend.suspend = rm_tch_early_suspend;
3378         ts->early_suspend.resume = rm_tch_early_resume;
3379         register_early_suspend(&ts->early_suspend);
3380 #endif
3381
3382         rm_tch_disable_irq(ts);
3383
3384 #if ENABLE_FB_CALLBACK && defined(CONFIG_FB)
3385         ts->fb_notifier.notifier_call = rm_fb_notifier_callback;
3386         err = fb_register_client(&ts->fb_notifier);
3387         if (err) {
3388                 dev_err(dev, "Raydium - Unable to register fb_notifier: %d\n"
3389                         , err);
3390                 goto err_free_irq;
3391         }
3392 #endif
3393
3394         err = sysfs_create_group(&dev->kobj, &rm_ts_attr_group);
3395 #if ENABLE_FB_CALLBACK && defined(CONFIG_FB)
3396         if (err)
3397                 goto err_unregister_fb;
3398 #else
3399         if (err)
3400                 goto err_free_irq;
3401 #endif
3402
3403         err = input_register_device(input_dev);
3404         if (err)
3405                 goto err_remove_attr;
3406
3407         return ts;
3408
3409 err_remove_attr:
3410         sysfs_remove_group(&dev->kobj, &rm_ts_attr_group);
3411 #if ENABLE_FB_CALLBACK && defined(CONFIG_FB)
3412 err_unregister_fb:
3413         fb_unregister_client(&ts->fb_notifier);
3414 #endif
3415 err_free_irq:
3416         free_irq(ts->irq, ts);
3417 err_free_mem:
3418         input_free_device(input_dev);
3419         kfree(ts);
3420 err_out:
3421         return ERR_PTR(err);
3422 }
3423
3424 static int dev_open(struct inode *inode, struct file *filp)
3425 {
3426         return RETURN_OK;
3427 }
3428
3429 static int dev_release(struct inode *inode, struct file *filp)
3430 {
3431         g_st_ts.b_init_finish = 0;
3432
3433         rm_tch_enter_manual_mode();
3434         return RETURN_OK;
3435 }
3436
3437 static ssize_t
3438 dev_read(struct file *filp, char __user *buf, size_t count, loff_t *pos)
3439 {
3440         ssize_t missing, status;
3441         int ret;
3442         u8 *p_u8_my_buf;
3443
3444         p_u8_my_buf = kmalloc(count, GFP_KERNEL);
3445         if (p_u8_my_buf == NULL)
3446                 return -ENOMEM;
3447
3448         p_u8_my_buf[0] = buf[0];
3449         ret = rm_tch_spi_read(p_u8_my_buf[0], p_u8_my_buf, count);
3450
3451         if (ret) {
3452                 status = -EFAULT;
3453                 rm_printk("Raydium - rm_tch_spi_read() fail\n");
3454         } else {
3455                 status = count;
3456                 missing = copy_to_user(buf, p_u8_my_buf, count);
3457
3458                 if (missing) {
3459                         if (missing == status)
3460                                 status = -EFAULT;
3461                         else
3462                                 status = status - missing;
3463                 }
3464         }
3465
3466         kfree(p_u8_my_buf);
3467         return status;
3468 }
3469
3470 static ssize_t dev_write(struct file *filp,
3471                 const char __user *buf, size_t count, loff_t *pos)
3472 {
3473         u8 *p_u8_my_buf;
3474         int ret;
3475         unsigned long missing;
3476         ssize_t status = 0;
3477
3478         p_u8_my_buf = kmalloc(count, GFP_KERNEL);
3479         if (p_u8_my_buf == NULL)
3480                 return -ENOMEM;
3481
3482         missing = copy_from_user(p_u8_my_buf, buf, count);
3483         if (missing)
3484                 status = -EFAULT;
3485         else {
3486                 ret = rm_tch_spi_write(p_u8_my_buf, count);
3487                 if (ret)
3488                         status = -EFAULT;
3489                 else
3490                         status = count;
3491         }
3492
3493         kfree(p_u8_my_buf);
3494         return status;
3495 }
3496
3497 /*=============================================================================
3498         Description:
3499                 I/O Control routin.
3500         Input:
3501                 file:
3502                 u8_cmd :
3503                 arg :
3504         Output:
3505                 1: succeed
3506                 0: failed
3507         Note: To avoid context switch,please don't add debug message
3508                   in this function.
3509 =============================================================================*/
3510 static long dev_ioctl(struct file *file,
3511                 unsigned int u8_cmd, unsigned long arg)
3512 {
3513         long ret = RETURN_OK;
3514         unsigned int index;
3515
3516         index = (u8_cmd >> 16) & 0xFFFF;
3517
3518         switch (u8_cmd & 0xFFFF) {
3519         case RM_IOCTL_REPORT_POINT:
3520                 raydium_report_pointer((void *)(arg & MASK_USER_SPACE_POINTER));
3521                 break;
3522         case RM_IOCTL_FINISH_CALC:
3523                 g_st_ts.b_calc_finish = 1;
3524                 break;
3525         case RM_IOCTL_READ_RAW_DATA:
3526                 ret = rm_tch_queue_read_raw_data(
3527                         (u8 *)(arg & MASK_USER_SPACE_POINTER),
3528                         index);
3529                 break;
3530         case RM_IOCTL_GET_SACN_MODE:
3531                 ret = rm_tch_ctrl_get_idle_mode(
3532                         (u8 *)(arg & MASK_USER_SPACE_POINTER));
3533                 break;
3534         case RM_IOCTL_SET_HAL_PID:
3535                 g_st_ts.u32_hal_pid = (u32)arg;
3536                 break;
3537         case RM_IOCTL_WATCH_DOG:
3538                 g_st_ts.u8_watch_dog_flg = 1;
3539                 g_st_ts.b_watch_dog_check = 0;
3540                 break;
3541         case RM_IOCTL_GET_VARIABLE:
3542                 ret = rm_tch_get_variable(index,
3543                         ((u8 *)(arg & MASK_USER_SPACE_POINTER)));
3544                 break;
3545         case RM_IOCTL_INIT_START:
3546                 g_st_ts.b_init_finish = 0;
3547                 rm_tch_enter_manual_mode();
3548                 break;
3549         case RM_IOCTL_INIT_END:
3550                 g_st_ts.b_init_finish = 1;
3551                 g_st_ts.b_calc_finish = 1;
3552                 g_st_ts.b_is_suspended = false;
3553                 if (g_st_ts.u8_resume_cnt)      /* In case issued by boot-up*/
3554                         g_st_ts.u8_resume_cnt--;
3555                 if (wake_lock_active(&g_st_ts.wakelock_initialization))
3556                         wake_unlock(&g_st_ts.wakelock_initialization);
3557 #if (ISR_POST_HANDLER == KTHREAD)
3558                 if (!g_st_ts.b_thread_alive) {
3559                         g_st_ts.thread_sched.sched_priority =
3560                                 MAX_USER_RT_PRIO / 2;
3561                         g_st_ts.rm_irq_post_thread =
3562                                 kthread_run(rm_work_thread_function,
3563                                         NULL, "RaydiumIrq_Poster");
3564                         if (IS_ERR(g_st_ts.rm_irq_post_thread)) {
3565                                 if (g_st_ctrl.u8_kernel_msg & DEBUG_KTHREAD)
3566                                         rm_printk("Raydium - Create IRQ poster failed!\n");
3567                         } else {
3568                                 g_st_ts.b_thread_active = false;
3569                                 g_st_ts.b_thread_alive = true;
3570                                 mutex_lock(&g_st_ts.mutex_irq_wait);
3571                                 g_st_ts.b_irq_is_waited = false;
3572                                 mutex_unlock(&g_st_ts.mutex_irq_wait);
3573                                 if (g_st_ctrl.u8_kernel_msg & DEBUG_KTHREAD)
3574                                         rm_printk("Raydium - Create IRQ poster successfully!\n");
3575                         }
3576                 } else {
3577                         if (g_st_ctrl.u8_kernel_msg & DEBUG_KTHREAD)
3578                                 rm_printk("Raydium - IRQ poster is alive!\n");
3579                 }
3580 #endif
3581                 rm_printk("Raydium - Enable input device done\n");
3582                 break;
3583         case RM_IOCTL_SCRIBER_CTRL:
3584                 g_st_ts.b_enable_scriber = (bool) arg;
3585                 break;
3586         case RM_IOCTL_SET_PARAMETER:
3587                 rm_tch_ctrl_set_parameter(
3588                         (void *)(arg & MASK_USER_SPACE_POINTER));
3589                 rm_tch_set_input_resolution(g_st_ctrl.u16_resolution_x,
3590                         g_st_ctrl.u16_resolution_y);
3591                 break;
3592         case RM_IOCTL_SET_BASELINE:
3593                 rm_tch_ctrl_set_baseline(
3594                         (u8 *)(arg & MASK_USER_SPACE_POINTER),
3595                         g_st_ctrl.u16_data_length);
3596                 break;
3597         case RM_IOCTL_SET_VARIABLE:
3598                 rm_tch_set_variable(index, arg);
3599                 break;
3600         case RM_IOCTL_SET_KRL_TBL:
3601                 ret = rm_set_kernel_tbl(index,
3602                         ((u8 *)(arg & MASK_USER_SPACE_POINTER)));
3603                 break;
3604         case RM_IOCTL_INIT_SERVICE:
3605                 g_st_ts.b_init_service = true;
3606                 break;
3607         default:
3608                 return -EINVAL;
3609                 break;
3610         }
3611
3612         return !ret;
3613 }
3614
3615 static const struct file_operations dev_fops = {
3616         .owner = THIS_MODULE,
3617         .open = dev_open,
3618         .release = dev_release,
3619         .read = dev_read,
3620         .write = dev_write,
3621         .unlocked_ioctl = dev_ioctl,
3622 };
3623
3624 static struct miscdevice raydium_ts_miscdev = {
3625         .minor = MISC_DYNAMIC_MINOR,
3626         .name = "touch",
3627         .fops = &dev_fops,
3628 };
3629
3630 static const struct rm_tch_bus_ops rm_tch_spi_bus_ops = {
3631         .bustype = BUS_SPI,
3632 };
3633
3634 static void init_ts_timer(void)
3635 {
3636         init_timer(&ts_timer_triggle);
3637         ts_timer_triggle.function = ts_timer_triggle_function;
3638         ts_timer_triggle.data = ((unsigned long) 0);
3639         ts_timer_triggle.expires = jiffies + TS_TIMER_PERIOD;
3640 }
3641
3642 static void ts_timer_triggle_function(unsigned long option)
3643 {
3644         queue_work(g_st_ts.rm_timer_workqueue, &g_st_ts.rm_timer_work);
3645         ts_timer_triggle.expires = jiffies + TS_TIMER_PERIOD;
3646         add_timer(&ts_timer_triggle);
3647 }
3648
3649 /*===========================================================================*/
3650 #if ENABLE_SPI_SETTING
3651 static int rm_tch_spi_setting(u32 speed)
3652 {
3653         int err;
3654         if ((speed == 0) || (speed > 18))
3655                 return RETURN_FAIL;
3656
3657         g_spi->max_speed_hz = speed * 1000 * 1000;
3658         err = spi_setup(g_spi);
3659         if (err) {
3660                 dev_dbg(&g_spi->dev, "Raydium - Change SPI setting failed\n");
3661                 return err;
3662         }
3663         return RETURN_OK;
3664 }
3665 #endif
3666
3667 static int rm_tch_spi_remove(struct spi_device *spi)
3668 {
3669         struct rm_tch_ts *ts = spi_get_drvdata(spi);
3670
3671         del_timer(&ts_timer_triggle);
3672         if (g_st_ts.rm_timer_workqueue)
3673                 destroy_workqueue(g_st_ts.rm_timer_workqueue);
3674
3675         rm_tch_queue_free();
3676
3677 #if (ISR_POST_HANDLER == WORK_QUEUE)
3678         if (g_st_ts.rm_workqueue)
3679                 destroy_workqueue(g_st_ts.rm_workqueue);
3680 #endif
3681
3682         if (&g_st_ts.wakelock_initialization)
3683                 wake_lock_destroy(&g_st_ts.wakelock_initialization);
3684
3685         mutex_destroy(&g_st_ts.mutex_scan_mode);
3686         mutex_destroy(&g_st_ts.mutex_ns_mode);
3687
3688         sysfs_remove_group(&raydium_ts_miscdev.this_device->kobj,
3689                                                 &rm_ts_attr_group);
3690         misc_deregister(&raydium_ts_miscdev);
3691         sysfs_remove_group(&ts->dev->kobj, &rm_ts_attr_group);
3692         free_irq(ts->irq, ts);
3693         input_unregister_device(ts->input);
3694
3695 #if ENABLE_FB_CALLBACK && defined(CONFIG_FB)
3696         fb_unregister_client(&ts->fb_notifier);
3697 #endif
3698
3699         if (ts->regulator_3v3 && ts->regulator_1v8) {
3700                 regulator_unregister_notifier(ts->regulator_3v3, &ts->nb_3v3);
3701                 regulator_unregister_notifier(ts->regulator_1v8, &ts->nb_1v8);
3702                 regulator_disable(ts->regulator_3v3);
3703                 regulator_disable(ts->regulator_1v8);
3704         }
3705
3706         if (ts->clk)
3707                 clk_disable(ts->clk);
3708
3709         kfree(ts);
3710         spi_set_drvdata(spi, NULL);
3711
3712         return RETURN_OK;
3713 }
3714
3715 static int rm_tch_regulator_init(struct rm_tch_ts *ts)
3716 {
3717         int error;
3718
3719         ts->regulator_3v3 = devm_regulator_get(&g_spi->dev, "avdd");
3720         if (IS_ERR(ts->regulator_3v3)) {
3721                 dev_err(&g_spi->dev, "Raydium - regulator_get failed: %ld\n",
3722                         PTR_ERR(ts->regulator_3v3));
3723                 goto err_null_regulator;
3724         }
3725
3726         ts->regulator_1v8 = devm_regulator_get(&g_spi->dev, "dvdd");
3727         if (IS_ERR(ts->regulator_1v8)) {
3728                 dev_err(&g_spi->dev, "Raydium - regulator_get failed: %ld\n",
3729                         PTR_ERR(ts->regulator_1v8));
3730                 goto err_null_regulator;
3731         }
3732
3733         /* Enable 1v8 first*/
3734         if (regulator_is_enabled(ts->regulator_1v8))
3735                 rm_printk("Raydium - %s : 1.8V regulator is already enabled!\n",
3736                                 __func__);
3737
3738         error = regulator_enable(ts->regulator_1v8);
3739         if (error) {
3740                 dev_err(&g_spi->dev,
3741                         "Raydium - 1.8V regulator enable failed: %d\n", error);
3742                 goto err_null_regulator;
3743         }
3744
3745         usleep_range(5000, 6000);
3746         /* Enable 3v3 then*/
3747         if (regulator_is_enabled(ts->regulator_3v3))
3748                 rm_printk("Raydium - %s : 3.3V regulator is already enabled!\n",
3749                                 __func__);
3750         error = regulator_enable(ts->regulator_3v3);
3751         if (error) {
3752                 dev_err(&g_spi->dev,
3753                         "Raydium - regulator enable failed: %d\n", error);
3754                 goto err_disable_1v8_regulator;
3755         }
3756
3757         ts->nb_1v8.notifier_call = &rm31080_voltage_notifier_1v8;
3758         error = regulator_register_notifier(ts->regulator_1v8, &ts->nb_1v8);
3759         if (error) {
3760                 dev_err(&g_spi->dev,
3761                         "Raydium - regulator notifier request failed: %d\n",
3762                         error);
3763                 goto err_disable_regulator;
3764         }
3765
3766         ts->nb_3v3.notifier_call = &rm31080_voltage_notifier_3v3;
3767         error = regulator_register_notifier(ts->regulator_3v3, &ts->nb_3v3);
3768         if (error) {
3769                 dev_err(&g_spi->dev,
3770                         "Raydium - regulator notifier request failed: %d\n",
3771                         error);
3772                 goto err_unregister_notifier;
3773         }
3774
3775         return RETURN_OK;
3776
3777 err_unregister_notifier:
3778         regulator_unregister_notifier(ts->regulator_1v8, &ts->nb_1v8);
3779 err_disable_regulator:
3780         regulator_disable(ts->regulator_3v3);
3781 err_disable_1v8_regulator:
3782         regulator_disable(ts->regulator_1v8);
3783 err_null_regulator:
3784         ts->regulator_3v3 = NULL;
3785         ts->regulator_1v8 = NULL;
3786         return RETURN_FAIL;
3787 }
3788
3789 static int rm_tch_spi_probe(struct spi_device *spi)
3790 {
3791         struct rm_tch_ts *ts;
3792         struct rm_spi_ts_platform_data *pdata;
3793         int ret;
3794
3795         g_spi = spi;
3796
3797         rm_tch_init_ts_structure();
3798         rm_tch_init_ts_structure_part();
3799
3800         if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
3801                 dev_err(&spi->dev, "Raydium - SPI CLK %d Hz?\n",
3802                         spi->max_speed_hz);
3803                 ret = -EINVAL;
3804                 goto err_spi_speed;
3805         }
3806         ts = rm_tch_input_init(&spi->dev, spi->irq, &rm_tch_spi_bus_ops);
3807         if (IS_ERR(ts)) {
3808                 dev_err(&spi->dev, "Raydium - Input Device Initialization Fail!\n");
3809                 ret = PTR_ERR(ts);
3810                 goto err_spi_speed;
3811         }
3812
3813         spi_set_drvdata(spi, ts);
3814
3815         if (rm_tch_regulator_init(ts)) {
3816                 dev_err(&spi->dev, "Raydium - regulator Initialization Fail!\n");
3817                 ret = -EINVAL;
3818                 goto err_regulator_init;
3819         }
3820         pdata = g_input_dev->dev.parent->platform_data;
3821         usleep_range(5000, 6000);
3822         if (ts->clk)
3823                 clk_enable(ts->clk);
3824
3825         gpio_set_value(pdata->gpio_reset, 0);
3826         msleep(120);
3827         gpio_set_value(pdata->gpio_reset, 1);
3828         msleep(20);
3829
3830         ret = misc_register(&raydium_ts_miscdev);
3831         if (ret) {
3832                 dev_err(&spi->dev, "Raydium - cannot register miscdev: %d\n",
3833                         ret);
3834                 goto err_misc_reg;
3835         }
3836         ret = sysfs_create_group(&raydium_ts_miscdev.this_device->kobj,
3837                                                 &rm_ts_attr_group);
3838         if (ret) {
3839                 dev_err(&spi->dev, "Raydium - cannot create group: %d\n",
3840                         ret);
3841                 goto err_create_sysfs;
3842         }
3843
3844         ret = rm_tch_queue_init();
3845         if (ret) {
3846                 dev_err(&spi->dev, "Raydium - could not init queue: %d\n",
3847                         ret);
3848                 goto err_queue_init;
3849         }
3850
3851         init_ts_timer();
3852         add_timer(&ts_timer_triggle);
3853
3854         rm_printk("Raydium - Spi Probe Done!!\n");
3855         return RETURN_OK;
3856
3857 err_queue_init:
3858         sysfs_remove_group(&raydium_ts_miscdev.this_device->kobj,
3859                                                 &rm_ts_attr_group);
3860 err_create_sysfs:
3861         misc_deregister(&raydium_ts_miscdev);
3862 err_misc_reg:
3863         if (ts->regulator_3v3 && ts->regulator_1v8) {
3864                 regulator_unregister_notifier(ts->regulator_3v3, &ts->nb_3v3);
3865                 regulator_unregister_notifier(ts->regulator_1v8, &ts->nb_1v8);
3866                 regulator_disable(ts->regulator_3v3);
3867                 regulator_disable(ts->regulator_1v8);
3868         }
3869
3870         if (ts->clk)
3871                 clk_disable(ts->clk);
3872
3873 err_regulator_init:
3874         spi_set_drvdata(spi, NULL);
3875         input_unregister_device(ts->input);
3876         sysfs_remove_group(&ts->dev->kobj, &rm_ts_attr_group);
3877 #ifdef CONFIG_HAS_EARLYSUSPEND
3878         unregister_early_suspend(&ts->early_suspend);
3879 #endif
3880         mutex_destroy(&ts->access_mutex);
3881         free_irq(ts->irq, ts);
3882         input_free_device(g_input_dev);
3883         kfree(ts);
3884 err_spi_speed:
3885         if (g_st_ts.rm_timer_workqueue)
3886                 destroy_workqueue(g_st_ts.rm_timer_workqueue);
3887 #if (ISR_POST_HANDLER == WORK_QUEUE)
3888         if (g_st_ts.rm_workqueue)
3889                 destroy_workqueue(g_st_ts.rm_workqueue);
3890 #endif
3891         mutex_destroy(&g_st_ts.mutex_scan_mode);
3892         mutex_destroy(&g_st_ts.mutex_ns_mode);
3893         return ret;
3894 }
3895
3896 static const struct of_device_id rm_ts_dt_match[] = {
3897         { .compatible = "raydium, rm_ts_spidev" },
3898         { },
3899 };
3900 MODULE_DEVICE_TABLE(of, rm_ts_dt_match);
3901
3902 static struct spi_driver rm_tch_spi_driver = {
3903         .driver = {
3904                 .name = "rm_ts_spidev",
3905                 .bus = &spi_bus_type,
3906                 .owner = THIS_MODULE,
3907 #ifdef CONFIG_OF
3908                 .of_match_table = rm_ts_dt_match,
3909 #endif
3910         },
3911         .probe = rm_tch_spi_probe,
3912         .remove = rm_tch_spi_remove,
3913 };
3914
3915 static int __init rm_tch_spi_init(void)
3916 {
3917         return spi_register_driver(&rm_tch_spi_driver);
3918 }
3919
3920 static void __exit rm_tch_spi_exit(void)
3921 {
3922         spi_unregister_driver(&rm_tch_spi_driver);
3923 }
3924
3925 module_init(rm_tch_spi_init);
3926 module_exit(rm_tch_spi_exit);
3927
3928 MODULE_AUTHOR("Valentine Hsu <valentine.hsu@rad-ic.com>");
3929 MODULE_DESCRIPTION("Raydium touchscreen SPI bus driver");
3930 MODULE_LICENSE("GPL");
3931 MODULE_ALIAS("spi:raydium-t007");