input: touch: raydium: v60.6 code drop
[linux-2.6.git] / drivers / input / touchscreen / rm31080a_ts.c
1 /*
2  * Raydium RM31080 touchscreen driver
3  *
4  * Copyright (C) 2012-2013, Raydium Semiconductor Corporation.
5  * All Rights Reserved.
6  * Copyright (C) 2012-2013, 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/sched.h>        /* wake_up_process() */
29 #include <linux/kthread.h>      /* kthread_create(),kthread_run() */
30 #include <linux/uaccess.h>      /* copy_to_user() */
31 #include <linux/miscdevice.h>
32 #include <asm/siginfo.h>        /* siginfo */
33 #include <linux/rcupdate.h>     /* rcu_read_lock */
34 #include <linux/sched.h>        /* find_task_by_pid_type */
35 #include <linux/syscalls.h>     /* sys_clock_gettime() */
36 #include <linux/random.h>       /* random32() */
37 #include <linux/suspend.h>      /* pm_notifier */
38 #include <linux/workqueue.h>
39 #include <linux/wakelock.h> /* wakelock */
40 #include <linux/regulator/consumer.h> /* regulator & voltage */
41 #include <linux/clk.h> /* clock */
42 #ifdef CONFIG_HAS_EARLYSUSPEND
43 #include <linux/earlysuspend.h>
44 #endif
45
46 #include <linux/timer.h>
47
48
49 #include <linux/spi/rm31080a_ts.h>
50 #include <linux/spi/rm31080a_ctrl.h>
51
52 #define CREATE_TRACE_POINTS
53 #include <trace/events/touchscreen_raydium.h>
54
55 /*=============================================================================
56         DEFINITIONS
57 =============================================================================*/
58 /*#define ENABLE_CALC_QUEUE_COUNT*/
59 #define ENABLE_SLOW_SCAN
60 #define ENABLE_SMOOTH_LEVEL
61 #define ENABLE_SPI_SETTING              0
62
63 #define MAX_SPI_FREQ_HZ                 50000000
64 #define TS_PEN_UP_TIMEOUT               msecs_to_jiffies(50)
65
66 #define QUEUE_COUNT                             128
67 #define RM_RAW_DATA_LENGTH                      3072
68
69 #define RM_SCAN_ACTIVE_MODE                     0x00
70 #define RM_SCAN_PRE_IDLE_MODE           0x01
71 #define RM_SCAN_IDLE_MODE                       0x02
72
73 #define RM_NEED_NONE                                    0x00
74 #define RM_NEED_TO_SEND_SCAN                    0x01
75 #define RM_NEED_TO_READ_RAW_DATA                0x02
76 #define RM_NEED_TO_SEND_SIGNAL                  0x04
77
78 #define TCH_WAKE_LOCK_TIMEOUT           (HZ/2)
79
80 #ifdef ENABLE_SLOW_SCAN
81 #define RM_SLOW_SCAN_INTERVAL                           20
82 #define RM_SLOW_SCAN_CMD_COUNT                          0x10
83 enum RM_SLOW_SCAN_LEVELS {
84         RM_SLOW_SCAN_LEVEL_NORMAL,
85         RM_SLOW_SCAN_LEVEL_20,
86         RM_SLOW_SCAN_LEVEL_40,
87         RM_SLOW_SCAN_LEVEL_60,
88         RM_SLOW_SCAN_LEVEL_80,
89         RM_SLOW_SCAN_LEVEL_100,
90         RM_SLOW_SCAN_LEVEL_MAX,
91         RM_SLOW_SCAN_LEVEL_COUNT
92 };
93 #endif
94
95 #ifdef ENABLE_SMOOTH_LEVEL
96 #define RM_SMOOTH_LEVEL_NORMAL          0
97 #define RM_SMOOTH_LEVEL_MAX                     4
98 #endif
99
100 #define RM_WINTEK_7_CHANNEL_X 30
101
102 #define TS_TIMER_PERIOD         HZ
103
104 struct timer_list ts_timer_triggle;
105 static void init_ts_timer(void);
106 static void ts_timer_triggle_function(unsigned long option);
107
108 /* do not use printk in kernel files */
109 #define rm_printk(msg...)       do { dev_info(&g_spi->dev, msg); } while (0)
110 /*=============================================================================
111         STRUCTURE DECLARATION
112 =============================================================================*/
113 /*TouchScreen Parameters*/
114 struct rm31080a_ts_para {
115         unsigned long ulHalPID;
116         bool bInitFinish;
117         bool bCalcFinish;
118         bool bEnableScriber;
119         bool bIsSuspended;
120
121         u32 u32WatchDogCnt;
122         u8 u8WatchDogFlg;
123         u8 u8WatchDogEnable;
124         bool u8WatchDogCheck;
125         u32 u32WatchDogTime;
126
127         u8 u8ScanModeState;
128
129 #ifdef ENABLE_SLOW_SCAN
130         bool bEnableSlowScan;
131         u32 u32SlowScanLevel;
132 #endif
133
134 #ifdef ENABLE_SMOOTH_LEVEL
135         u32 u32SmoothLevel;
136 #endif
137
138         u8 u8SelfTestStatus;
139         u8 u8SelfTestResult;
140         u8 u8Version;
141         u8 u8TestVersion;
142         u8 u8Repeat;
143
144         struct wake_lock Wakelock_Initialization;
145
146         struct mutex mutex_scan_mode;
147         struct mutex mutex_spi_rw;
148
149         struct workqueue_struct *rm_workqueue;
150         struct work_struct rm_work;
151
152         struct workqueue_struct *rm_timer_workqueue;
153         struct work_struct rm_timer_work;
154
155 };
156
157 struct rm_tch_ts {
158         const struct rm_tch_bus_ops *bops;
159         struct device *dev;
160         struct input_dev *input;
161         unsigned int irq;
162         bool disabled;
163         bool suspended;
164         char phys[32];
165         struct mutex access_mutex;
166 #ifdef CONFIG_HAS_EARLYSUSPEND
167         struct early_suspend early_suspend;
168 #endif
169         struct regulator *regulator_3v3;
170         struct regulator *regulator_1v8;
171         struct notifier_block nb_3v3;
172         struct notifier_block nb_1v8;
173         struct clk *clk;
174 };
175
176 struct rm_tch_bus_ops {
177         u16 bustype;
178         int (*read) (struct device *dev, u8 reg);
179         int (*write) (struct device *dev, u8 reg, u16 val);
180 };
181
182 struct rm_tch_queue_info {
183         u8(*pQueue)[RM_RAW_DATA_LENGTH];
184         u16 u16Front;
185         u16 u16Rear;
186 };
187
188 /*=============================================================================
189         GLOBAL VARIABLES DECLARATION
190 =============================================================================*/
191 struct input_dev *g_input_dev;
192 struct spi_device *g_spi;
193 struct rm31080a_ts_para g_stTs;
194 unsigned long g_smooth_level = 1;
195
196 struct rm_tch_queue_info g_stQ;
197
198 unsigned char *g_pu8BurstReadBuf;
199
200 unsigned char g_stCmdSetIdle[KRL_SIZE_SET_IDLE];
201 unsigned char g_stCmdPauseAuto[KRL_SIZE_PAUSE_AUTO];
202 unsigned char g_stRmStartCmd[KRL_SIZE_RM_START];
203 unsigned char g_stRmEndCmd[KRL_SIZE_RM_END];
204 unsigned char g_stRmReadImgCmd[KRL_SIZE_RM_READ_IMG];
205 unsigned char g_stRmWatchdogCmd[KRL_SIZE_RM_WATCHDOG];
206 unsigned char g_stRmTestModeCmd[KRL_SIZE_RM_TESTMODE];
207 unsigned char g_stRmSlowScanCmd[KRL_SIZE_RM_SLOWSCAN];
208
209 int g_service_busy_report_count;
210
211 /*=============================================================================
212         FUNCTION DECLARATION
213 =============================================================================*/
214 static int rm_tch_cmd_process(u8 selCase, u8 *pCmdTbl, struct rm_tch_ts *ts);
215 static int rm_tch_read_image_data(unsigned char *p);
216 #ifdef CONFIG_HAS_EARLYSUSPEND
217 static void rm_tch_early_suspend(struct early_suspend *es);
218 static void rm_tch_early_resume(struct early_suspend *es);
219 #endif
220 static int rm_tch_ts_send_signal(int pid, int iInfo);
221
222 static void rm_tch_enter_test_mode(u8 flag);
223
224 static void rm_ctrl_stop(struct rm_tch_ts *ts);
225 static void rm_ctrl_start(struct rm_tch_ts *ts);
226
227 static void rm_ctrl_watchdog_func(unsigned int u32Enable);
228
229 /*=============================================================================
230          Description:
231                         RM31080 spi interface.
232          Input:
233
234          Output:
235                         1:succeed
236                         0:failed
237 =============================================================================*/
238 static int rm_tch_spi_read(u8 u8addr, u8 *rxbuf, size_t len)
239 {
240         int status;
241         struct spi_message message;
242         struct spi_transfer x[2];
243
244         mutex_lock(&g_stTs.mutex_spi_rw);
245
246         spi_message_init(&message);
247         memset(x, 0, sizeof x);
248
249         u8addr |= 0x80;
250         x[0].len = 1;
251         x[0].tx_buf = &u8addr;
252         spi_message_add_tail(&x[0], &message);
253
254         x[1].len = len;
255         x[1].rx_buf = rxbuf;
256         spi_message_add_tail(&x[1], &message);
257
258         /*It returns zero on succcess,else a negative error code.*/
259         status = spi_sync(g_spi, &message);
260
261         mutex_unlock(&g_stTs.mutex_spi_rw);
262
263         if (status) {
264                 dev_err(&g_spi->dev, "Raydium - %s : spi_async failed - error %d\n",
265                         __func__, status);
266                 return FAIL;
267         }
268
269         return OK;
270 }
271
272 /*=============================================================================
273          Description:
274                         RM31080 spi interface.
275          Input:
276
277          Output:
278                         1:succeed
279                         0:failed
280 =============================================================================*/
281 static int rm_tch_spi_write(u8 *txbuf, size_t len)
282 {
283         int status;
284         /*It returns zero on succcess,else a negative error code.*/
285         mutex_lock(&g_stTs.mutex_spi_rw);
286
287         status = spi_write(g_spi, txbuf, len);
288
289         mutex_unlock(&g_stTs.mutex_spi_rw);
290
291         if (status) {
292                 dev_err(&g_spi->dev, "Raydium - %s : spi_write failed - error %d\n",
293                         __func__, status);
294                 return FAIL;
295         }
296
297         return OK;
298 }
299
300 /*=============================================================================
301          Description:
302                         RM31080 spi interface.
303          Input:
304
305          Output:
306                         1:succeed
307                         0:failed
308 =============================================================================*/
309 int rm_tch_spi_byte_read(unsigned char u8Addr, unsigned char *pu8Value)
310 {
311         return rm_tch_spi_read(u8Addr, pu8Value, 1);
312 }
313
314 /*=============================================================================
315          Description:
316                         RM31080 spi interface.
317          Input:
318
319          Output:
320                         1:succeed
321                         0:failed
322 =============================================================================*/
323 int rm_tch_spi_byte_write(unsigned char u8Addr, unsigned char u8Value)
324 {
325         u8 buf[2];
326         buf[0] = u8Addr;
327         buf[1] = u8Value;
328         return rm_tch_spi_write(buf, 2);
329 }
330
331 /*=============================================================================
332          Description:
333                         RM31080 spi interface.
334          Input:
335
336          Output:
337                         1:succeed
338                         0:failed
339 =============================================================================*/
340 int rm_tch_spi_burst_read(unsigned char u8Addr, unsigned char *pu8Value,
341         u32 u32len)
342 {
343         int ret;
344         u8 *pMyBuf;
345
346         pMyBuf = kmalloc(u32len, GFP_KERNEL);
347         if (pMyBuf == NULL)
348                 return FAIL;
349
350         ret = rm_tch_spi_read(u8Addr, pMyBuf, u32len);
351
352         if (ret)
353                 memcpy(pu8Value, pMyBuf, u32len);
354
355         kfree(pMyBuf);
356
357         return ret;
358 }
359 /*=============================================================================
360          Description:
361                         RM31080 spi interface.
362          Input:
363
364          Output:
365                         1:succeed
366                         0:failed
367 =============================================================================*/
368 int rm_tch_spi_burst_write(unsigned char *pBuf, unsigned int u32Len)
369 {
370         u8 *pMyBuf;
371         int ret;
372
373         pMyBuf = kmalloc(u32Len, GFP_KERNEL);
374         if (pMyBuf == NULL)
375                 return FAIL;
376
377         memcpy(pMyBuf, pBuf, u32Len);
378         ret = rm_tch_spi_write(pMyBuf, u32Len);
379         kfree(pMyBuf);
380         return ret;
381 }
382
383 /*===========================================================================*/
384 void raydium_change_scan_mode(u8 u8TouchCount)
385 {
386         static u32 u32NoTouchCount; /*= 0; remove by checkpatch*/
387         u16 u16NTCountThd;
388
389         u16NTCountThd = (u16)g_stCtrl.bTime2Idle * 100;
390
391         if (u8TouchCount) {
392                 u32NoTouchCount = 0;
393                 return;
394         }
395
396         mutex_lock(&g_stTs.mutex_scan_mode);
397         if (u32NoTouchCount < u16NTCountThd) {
398                 u32NoTouchCount++;
399         } else if (g_stTs.u8ScanModeState == RM_SCAN_ACTIVE_MODE) {
400                         g_stTs.u8ScanModeState = RM_SCAN_PRE_IDLE_MODE;
401                 u32NoTouchCount = 0;
402         }
403         mutex_unlock(&g_stTs.mutex_scan_mode);
404 }
405
406 void raydium_report_pointer(void *p)
407 {
408         static unsigned char ucLastTouchCount; /*= 0; remove by checkpatch*/
409         int i;
410         int iCount;
411         int iMaxX, iMaxY;
412         struct rm_touch_event *spTP;
413         spTP = (struct rm_touch_event *) p;
414
415         if ((g_stCtrl.u16ResolutionX != 0) && (g_stCtrl.u16ResolutionY != 0)) {
416                 iMaxX = g_stCtrl.u16ResolutionX;
417                 iMaxY = g_stCtrl.u16ResolutionY;
418         } else {
419                 iMaxX = RM_INPUT_RESOLUTION_X;
420                 iMaxY = RM_INPUT_RESOLUTION_Y;
421         }
422
423         iCount = max(ucLastTouchCount, spTP->ucTouchCount);
424         if (iCount) {
425                 for (i = 0; i < iCount; i++) {
426                         if (i == 10)
427                                 break;
428
429                         if (i < spTP->ucTouchCount) {
430                                 input_report_abs(g_input_dev,
431                                                         ABS_MT_TRACKING_ID,
432                                                         spTP->ucID[i]);
433                                 input_report_abs(g_input_dev,
434                                                         ABS_MT_TOUCH_MAJOR,
435                                                         spTP->usZ[i]);
436                                 input_report_abs(g_input_dev,
437                                                         ABS_MT_WIDTH_MAJOR,
438                                                         spTP->usZ[i]);
439                                 input_report_abs(g_input_dev,
440                                                         ABS_MT_PRESSURE,
441                                                         spTP->usZ[i]);
442
443                                 if (spTP->usX[i] >= (iMaxX - 1))
444                                         input_report_abs(g_input_dev,
445                                                         ABS_MT_POSITION_X,
446                                                         (iMaxX - 1));
447                                 else
448                                         input_report_abs(g_input_dev,
449                                                         ABS_MT_POSITION_X,
450                                                         spTP->usX[i]);
451
452                                 if (spTP->usY[i] >= (iMaxY - 1))
453                                         input_report_abs(g_input_dev,
454                                                         ABS_MT_POSITION_Y,
455                                                         (iMaxY - 1));
456                                 else
457                                         input_report_abs(g_input_dev,
458                                                         ABS_MT_POSITION_Y,
459                                                         spTP->usY[i]);
460                         }
461                         input_mt_sync(g_input_dev);
462                 }
463                 ucLastTouchCount = spTP->ucTouchCount;
464                 input_report_key(g_input_dev, BTN_TOUCH,
465                                                 spTP->ucTouchCount > 0);
466                 input_sync(g_input_dev);
467         }
468
469         if (g_stCtrl.bfPowerMode)
470                 raydium_change_scan_mode(spTP->ucTouchCount);
471
472 }
473
474 /*=============================================================================
475          Description:
476                         RM31080 control functions.
477          Input:
478                         N/A
479          Output:
480                         1:succeed
481                         0:failed
482 =============================================================================*/
483
484 /*=============================================================================
485          Description: Read Sensor Raw Data
486
487          Input:
488                         *p : Raw Data Buffer Address
489          Output:
490                         none
491 =============================================================================*/
492 static int rm_tch_read_image_data(unsigned char *p)
493 {
494         int ret;
495         g_pu8BurstReadBuf = p;
496         ret = rm_tch_cmd_process(0, g_stRmReadImgCmd, NULL);
497         return ret;
498 }
499
500 void rm_ctrl_set_idle(u8 OnOff)
501 {
502         rm_tch_cmd_process(OnOff, g_stCmdSetIdle, NULL);
503 }
504
505 void rm_tch_ctrl_enter_auto_mode(void)
506 {
507         /*g_stCtrl.bfIdleModeCheck = 0; */
508         g_stCtrl.bfIdleModeCheck &= ~0x01;
509
510         /*Enable auto scan*/
511         if ((g_stCtrl.bDebugMessage & DEBUG_DRIVER) == DEBUG_DRIVER)
512                 rm_printk("Raydium - Enter Auto Scan Mode\n");
513
514         if (g_stCtrl.bICVersion < 0xD0)
515                 rm_set_repeat_times(g_stCtrl.bIdleRepeatTimes[0]);
516
517         rm_ctrl_set_idle(1);
518
519 #if (ENABLE_MANUAL_IDLE_MODE == 1)
520         rm_tch_spi_byte_write(0x09, 0x40);
521 #else
522         rm_tch_spi_byte_write(0x09, 0x10 | 0x40);
523 #endif
524 }
525
526 void rm_tch_ctrl_leave_auto_mode(void)
527 {
528         g_stCtrl.bfIdleModeCheck |= 0x01;
529         /*Disable auto scan*/
530
531         rm_ctrl_set_idle(0);
532
533         if (g_stCtrl.bICVersion < 0xD0)
534                 rm_set_repeat_times(g_stCtrl.bActiveRepeatTimes[0]);
535
536         rm_tch_spi_byte_write(0x09, 0x00);
537         if ((g_stCtrl.bDebugMessage & DEBUG_DRIVER) == DEBUG_DRIVER)
538                 rm_printk("Raydium - Leave Auto Scan Mode\n");
539 }
540
541 void rm_ctrl_pause_auto_mode(void)
542 {
543         rm_tch_cmd_process(0, g_stCmdPauseAuto, NULL);
544 }
545
546 static u32 rm_tch_ctrl_configure(void)
547 {
548         u32 u32Flag;
549
550         switch (g_stTs.u8ScanModeState) {
551         case RM_SCAN_ACTIVE_MODE:
552                 u32Flag =
553                         RM_NEED_TO_SEND_SCAN | RM_NEED_TO_READ_RAW_DATA |
554                         RM_NEED_TO_SEND_SIGNAL;
555                 break;
556
557         case RM_SCAN_PRE_IDLE_MODE:
558                 rm_tch_ctrl_enter_auto_mode();
559                 g_stTs.u8ScanModeState = RM_SCAN_IDLE_MODE;
560                 u32Flag = RM_NEED_NONE;
561                 break;
562
563         case RM_SCAN_IDLE_MODE:
564                 rm_tch_ctrl_leave_auto_mode();
565                 rm_tch_ctrl_scan_start();
566                 g_stTs.u8ScanModeState = RM_SCAN_ACTIVE_MODE;
567                 if (g_stCtrl.bICVersion >= 0xD0)
568                         u32Flag = RM_NEED_TO_SEND_SCAN;
569                 else
570                         u32Flag =
571                                 RM_NEED_TO_SEND_SCAN |
572                                 RM_NEED_TO_READ_RAW_DATA |
573                                 RM_NEED_TO_SEND_SIGNAL;
574                 break;
575
576         default:
577                 u32Flag = RM_NEED_NONE;
578                 break;
579         }
580
581         return u32Flag;
582 }
583
584 int KRL_CMD_CONFIG_1V8_Handler(u8 CMD, u8 OnOff, struct rm_tch_ts *ts)
585 {
586         int ret = FAIL;
587         struct rm_spi_ts_platform_data *pdata;
588
589         pdata = g_input_dev->dev.parent->platform_data;
590
591         if (CMD == KRL_SUB_CMD_SET_1V8_REGULATOR) {
592                 if (ts) {
593                         if (ts->regulator_1v8) {
594                                 if (OnOff) {
595                                         ret = regulator_enable(
596                                                         ts->regulator_1v8);
597                                         if (ret < 0) {
598                                                 dev_err(&g_spi->dev,
599                                                         "Raydium - regulator 1.8V enable failed: %d\n",
600                                                         ret);
601                                                 ret = FAIL;
602                                         } else
603                                                 ret = OK;
604                                 } else if (ts->regulator_3v3) {
605                                         ret = regulator_disable(
606                                                         ts->regulator_1v8);
607                                         if (ret < 0) {
608                                                 dev_err(&g_spi->dev,
609                                                         "Raydium - regulator 1.8V disable failed: %d\n",
610                                                         ret);
611                                                 ret = FAIL;
612                                         } else
613                                                 ret = OK;
614                                 }
615                         } else
616                                 dev_err(&g_spi->dev,
617                                         "Raydium - regulator 1.8V handler fail: %d\n",
618                                         ret);
619                 } else
620                         dev_err(&g_spi->dev,
621                                 "Raydium - regulator 1.8V ts fail: %d\n",
622                                 ret);
623                 /*gpio_set_value(pdata->gpio_1v8, pCmdTbl[_DATA]);*/
624         } else if (CMD == KRL_SUB_CMD_SET_1V8_GPIO) {
625                 gpio_direction_output(pdata->gpio_1v8, OnOff);
626                 ret = OK;
627         }
628         return ret;
629 }
630
631 int KRL_CMD_CONFIG_3V3_Handler(u8 CMD, u8 OnOff, struct rm_tch_ts *ts)
632 {
633         int ret = FAIL;
634         struct rm_spi_ts_platform_data *pdata;
635
636         pdata = g_input_dev->dev.parent->platform_data;
637
638         if (CMD == KRL_SUB_CMD_SET_3V3_REGULATOR) {
639                 if (ts) {
640                         if (ts->regulator_3v3) {
641                                 if (OnOff) {
642                                         ret = regulator_enable(
643                                                         ts->regulator_3v3);
644                                         if (ret < 0) {
645                                                 dev_err(&g_spi->dev,
646                                                         "Raydium - regulator 3.3V enable failed: %d\n",
647                                                         ret);
648                                                 ret = FAIL;
649                                         } else
650                                                 ret = OK;
651                                 } else {
652                                         ret = regulator_disable(
653                                                         ts->regulator_3v3);
654                                         if (ret < 0) {
655                                                 dev_err(&g_spi->dev,
656                                                         "Raydium - regulator 3.3V disable failed: %d\n",
657                                                         ret);
658                                                 ret = FAIL;
659                                         } else
660                                                 ret = OK;
661                                 }
662                         } else
663                                 dev_err(&g_spi->dev,
664                                         "Raydium - regulator 3.3V handler fail: %d\n",
665                                         ret);
666                 } else
667                         dev_err(&g_spi->dev,
668                                 "Raydium - regulator 3.3V ts fail: %d\n",
669                                 ret);
670                 /*gpio_set_value(pdata->gpio_3v3, pCmdTbl[_DATA]);*/
671         } else if (CMD == KRL_SUB_CMD_SET_3V3_GPIO) {
672                 gpio_direction_output(pdata->gpio_3v3, OnOff);
673                 ret = OK;
674         }
675         return ret;
676 }
677
678 static int rm_tch_cmd_process(u8 selCase, u8 *pCmdTbl, struct rm_tch_ts *ts)
679 {
680 #define _CMD u16j
681 #define _ADDR (u16j+1)
682 #define _SUB_CMD (u16j+1)
683 #define _DATA (u16j+2)
684
685         static DEFINE_MUTEX(lock);
686         u16 u16j = 0, u16strIdx, u16TblLenth, u16Tmp;
687         u8 u8i, u8reg = 0;
688         int ret = FAIL;
689         struct rm_spi_ts_platform_data *pdata;
690
691         mutex_lock(&lock);
692
693         pdata = g_input_dev->dev.parent->platform_data;
694
695         u16TblLenth = pCmdTbl[KRL_TBL_FIELD_POS_LEN_H];
696         u16TblLenth <<= 8;
697         u16TblLenth |= pCmdTbl[KRL_TBL_FIELD_POS_LEN_L];
698         if (u16TblLenth < 3) {
699                 if ((g_stCtrl.bDebugMessage & DEBUG_DRIVER) == DEBUG_DRIVER)
700                         dev_info(&g_spi->dev, "Raydium - Null CMD %s : [0x%x]\n",
701                                 __func__, (u32)pCmdTbl);
702                 mutex_unlock(&lock);
703                 return ret;
704         }
705
706         u16strIdx = KRL_TBL_FIELD_POS_CASE_NUM
707                 + pCmdTbl[KRL_TBL_FIELD_POS_CASE_NUM] + 1;
708         for (u8i = 0; u8i < selCase; u8i++)
709                 u16strIdx += (pCmdTbl[u8i + KRL_TBL_FIELD_POS_CMD_NUM]
710                         * KRL_TBL_CMD_LEN);
711
712         for (u8i = 0; u8i < pCmdTbl[selCase + KRL_TBL_FIELD_POS_CMD_NUM];
713                 u8i++) {
714                 u16j = u16strIdx + (KRL_TBL_CMD_LEN * u8i);
715                 ret = FAIL;
716                 switch (pCmdTbl[_CMD]) {
717                 case KRL_CMD_READ:
718                         ret = rm_tch_spi_read(pCmdTbl[_ADDR], &u8reg, 1);
719                         /*rm_printk("Raydium - KRL_CMD_READ "
720                         "- 0x%x:0x%x\n", pCmdTbl[_ADDR], u8reg);*/
721                         break;
722                 case KRL_CMD_WRITE_W_DATA:
723                         /*rm_printk("Raydium - KRL_CMD_WRITE_W_DATA "
724                         "- 0x%x:0x%x\n", pCmdTbl[_ADDR], pCmdTbl[_DATA]);*/
725                         ret = rm_tch_spi_byte_write(pCmdTbl[_ADDR],
726                                 pCmdTbl[_DATA]);
727                         break;
728                 case KRL_CMD_WRITE_WO_DATA:
729                         /*rm_printk("Raydium - KRL_CMD_WRITE_WO_DATA "
730                         "- 0x%x:0x%x\n", pCmdTbl[_ADDR], u8reg);*/
731                         ret = rm_tch_spi_byte_write(pCmdTbl[_ADDR], u8reg);
732                         break;
733                 case KRL_CMD_AND:
734                         u8reg &= pCmdTbl[_DATA];
735                         ret = OK;
736                         break;
737                 case KRL_CMD_OR:
738                         u8reg |= pCmdTbl[_DATA];
739                         ret = OK;
740                         break;
741                 case KRL_CMD_NOT:
742                         u8reg = ~u8reg;
743                         ret = OK;
744                         break;
745                 case KRL_CMD_XOR:
746                         u8reg ^= pCmdTbl[_DATA];
747                         ret = OK;
748                         break;
749                 case KRL_CMD_SEND_SIGNAL:
750                         u16Tmp = pCmdTbl[_DATA];
751                         /*rm_printk("Raydium - KRL_SEND_SIGNAL_CM "
752                         "- %d\n", u16Tmp);*/
753                         ret = rm_tch_ts_send_signal(g_stTs.ulHalPID,
754                                 (int)u16Tmp);
755                         break;
756                 case KRL_CMD_CONFIG_RST:
757                         /*rm_printk("Raydium - KRL_CMD_CONFIG_RST "
758                         "- %d - %d\n", pCmdTbl[_SUB_CMD], pCmdTbl[_DATA]);*/
759                         switch (pCmdTbl[_SUB_CMD]) {
760                         case KRL_SUB_CMD_SET_RST_GPIO:
761                                 gpio_direction_output(pdata->gpio_reset,
762                                         pCmdTbl[_DATA]);
763                                 break;
764                         case KRL_SUB_CMD_SET_RST_VALUE:
765                                 gpio_set_value(pdata->gpio_reset,
766                                         pCmdTbl[_DATA]);
767                                 break;
768                         }
769                         ret = OK;
770                         break;
771                 case KRL_CMD_CONFIG_3V3:/*Need to check qpio setting*/
772                         /*rm_printk("Raydium - KRL_CMD_CONFIG_3V3 "
773                         "- %d - %d\n", pCmdTbl[_SUB_CMD], pCmdTbl[_DATA]);
774                         rm_printk("Raydium - 3.3V regulator is %d\n",
775                         regulator_is_enabled(ts->regulator_3v3));*/
776                         ret = KRL_CMD_CONFIG_3V3_Handler(pCmdTbl[_SUB_CMD],
777                                 pCmdTbl[_DATA], ts);
778                         /*rm_printk("Raydium - 3.3V regulator is %d\n",
779                         regulator_is_enabled(ts->regulator_3v3));*/
780                         break;
781                 case KRL_CMD_CONFIG_1V8:/*Need to check qpio setting*/
782                         /*rm_printk("Raydium - KRL_CMD_CONFIG_1V8 "
783                         "- %d - %d\n", pCmdTbl[_SUB_CMD], pCmdTbl[_DATA]);
784                         rm_printk("Raydium - 1.8V regulator is %d\n",
785                         regulator_is_enabled(ts->regulator_1v8));*/
786                         ret = KRL_CMD_CONFIG_1V8_Handler(pCmdTbl[_SUB_CMD],
787                                 pCmdTbl[_DATA], ts);
788                         /*rm_printk("Raydium - 1.8V regulator is %d\n",
789                         regulator_is_enabled(ts->regulator_1v8));*/
790                         break;
791                 case KRL_CMD_CONFIG_CLK:
792                         /*rm_printk("Raydium - KRL_CMD_CONFIG_CLK"
793                         " - %d - %d\n", pCmdTbl[_SUB_CMD], pCmdTbl[_DATA]);*/
794                         ret = OK;
795                         /* Temporarily solving external clk issue for NV
796                              for different kind of clk source*//*
797                         if (ts && ts->clk) {
798                                 if (pCmdTbl[_SUB_CMD] == KRL_SUB_CMD_SET_CLK) {
799                                         if (pCmdTbl[_DATA])
800                                                 clk_enable(ts->clk);
801                                         else
802                                                 clk_disable(ts->clk);
803                                 } else
804                                         ret = FAIL;
805                         } else {
806                                 ret = FAIL;
807                         }
808                         */
809                         if (pCmdTbl[_SUB_CMD] == KRL_SUB_CMD_SET_CLK) {
810                                 if (ts && ts->clk) {
811                                         if (pCmdTbl[_DATA])
812                                                 clk_enable(ts->clk);
813                                         else
814                                                 clk_disable(ts->clk);
815                                 } else {
816                                         dev_err(&g_spi->dev, "Raydium - %s : No clk handler!\n",
817                                                 __func__);
818                                 }
819                         } else
820                                 ret = FAIL;
821                         break;
822                 case KRL_CMD_SET_TIMER:
823                         /*rm_printk("Raydium - KRL_CMD_SET_TIMER "
824                         "- %d\n", pCmdTbl[_SUB_CMD]);*/
825                         ret = OK;
826                         if (pCmdTbl[_SUB_CMD] == KRL_SUB_CMD_INIT_TIMER) {
827                                 init_ts_timer();
828                         } else if (pCmdTbl[_SUB_CMD] == KRL_SUB_CMD_ADD_TIMER) {
829                                 if (!timer_pending(&ts_timer_triggle))
830                                         add_timer(&ts_timer_triggle);
831                         } else if (pCmdTbl[_SUB_CMD] == KRL_SUB_CMD_DEL_TIMER) {
832                                 del_timer(&ts_timer_triggle);
833                         } else
834                                 ret = FAIL;
835                         break;
836                 case KRL_CMD_USLEEP:
837                         /*rm_printk("Raydium - KRL_CMD_USLEEP "
838                         "- %d ms\n", pCmdTbl[_DATA]);*/
839                         u16Tmp = pCmdTbl[_DATA];
840                         u16Tmp *= 1000;
841                         usleep_range(u16Tmp, u16Tmp+200);
842                         ret = OK;
843                         break;
844                 case KRL_CMD_MSLEEP:
845                         u16Tmp = (u16)(pCmdTbl[_DATA] |
846                                 (pCmdTbl[_SUB_CMD] << 8));
847                         /*rm_printk("Raydium - KRL_CMD_MSLEEP "
848                         "- %d ms\n", u16Tmp);*/
849                         msleep(u16Tmp);
850                         ret = OK;
851                         break;
852                 case KRL_CMD_FLUSH_QU:
853                         /*rm_printk("Raydium - KRL_CMD_FLUSH_QU "
854                         "- %d\n", pCmdTbl[_SUB_CMD]);*/
855                         ret = OK;
856                         if (pCmdTbl[_SUB_CMD] == KRL_SUB_CMD_SENSOR_QU)
857                                 flush_workqueue(g_stTs.rm_workqueue);
858                         else if (pCmdTbl[_SUB_CMD] == KRL_SUB_CMD_TIMER_QU)
859                                 flush_workqueue(g_stTs.rm_timer_workqueue);
860                         else
861                                 ret = FAIL;
862                         break;
863                 case KRL_CMD_READ_IMG:
864                         /*rm_printk("Raydium - KRL_CMD_READ_IMG "
865                         "- 0x%x:0x%x:%d\n",
866                         pCmdTbl[_ADDR],
867                         g_pu8BurstReadBuf,
868                         g_stCtrl.u16DataLength);*/
869                         if (g_pu8BurstReadBuf)
870                                 ret = rm_tch_spi_read(pCmdTbl[_ADDR],
871                                                 g_pu8BurstReadBuf,
872                                                 g_stCtrl.u16DataLength);
873                         g_pu8BurstReadBuf = NULL;
874                         break;
875                 default:
876                         break;
877                 }
878
879
880                 if (ret == FAIL) {
881                         dev_err(&g_spi->dev, "Raydium - %s : [0x%x] cmd failed\n",
882                                 __func__,
883                                 (u32)pCmdTbl);
884                         dev_err(&g_spi->dev, "Raydium - cmd:0x%x, addr:0x%x, data:0x%x\n",
885                                 pCmdTbl[_CMD],
886                                 pCmdTbl[_ADDR],
887                                 pCmdTbl[_DATA]);
888                         break;
889                 }
890         }
891
892         mutex_unlock(&lock);
893         return ret;
894 }
895
896 int rm_set_kernel_tbl(int iFuncIdx, u8 *u8pSrc)
897 {
898         ssize_t missing;
899         u16 u16len;
900         u8 *u8pDst;
901
902         switch (iFuncIdx) {
903         case KRL_INDEX_FUNC_SET_IDLE:
904                 u8pDst = g_stCmdSetIdle;
905                 break;
906         case KRL_INDEX_FUNC_PAUSE_AUTO:
907                 u8pDst = g_stCmdPauseAuto;
908                 break;
909         case KRL_INDEX_RM_START:
910                 u8pDst = g_stRmStartCmd;
911                 break;
912         case KRL_INDEX_RM_END:
913                 u8pDst = g_stRmEndCmd;
914                 break;
915         case KRL_INDEX_RM_READ_IMG:
916                 u8pDst = g_stRmReadImgCmd;
917                 break;
918         case KRL_INDEX_RM_WATCHDOG:
919                 u8pDst = g_stRmWatchdogCmd;
920                 break;
921         case KRL_INDEX_RM_TESTMODE:
922                 u8pDst = g_stRmTestModeCmd;
923                 break;
924         case KRL_INDEX_RM_SLOWSCAN:
925                 u8pDst = g_stRmSlowScanCmd;
926                 break;
927         default:
928                 dev_err(&g_spi->dev, "Raydium - %s : no kernel table - err:%d\n",
929                         __func__, iFuncIdx);
930                 return FAIL;
931         }
932
933         u16len = u8pSrc[KRL_TBL_FIELD_POS_LEN_H];
934         u16len <<= 8;
935         u16len |= u8pSrc[KRL_TBL_FIELD_POS_LEN_L];
936
937         missing = copy_from_user(u8pDst, u8pSrc, u16len);
938         if (missing) {
939                 dev_err(&g_spi->dev, "Raydium - %s : copy failed - len:%d, miss:%d\n",
940                         __func__, u16len, missing);
941                 return FAIL;
942         }
943
944         /*dev_info(&g_spi->dev, "Raydium - %s : CMD_TAB_%d[0x%x]\n",
945         __func__, iFuncIdx, (u32)u8pDst);*/
946
947         return OK;
948 }
949 /*=============================================================================
950
951 =============================================================================*/
952 static void rm_tch_enter_manual_mode(void)
953 {
954         flush_workqueue(g_stTs.rm_workqueue);
955
956         mutex_lock(&g_stTs.mutex_scan_mode);
957         if (g_stTs.u8ScanModeState == RM_SCAN_ACTIVE_MODE) {
958                 mutex_unlock(&g_stTs.mutex_scan_mode);
959                 return;
960         }
961
962         if (g_stTs.u8ScanModeState == RM_SCAN_PRE_IDLE_MODE) {
963                 g_stTs.u8ScanModeState = RM_SCAN_ACTIVE_MODE;
964                 mutex_unlock(&g_stTs.mutex_scan_mode);
965                 return;
966         }
967
968         if (g_stTs.u8ScanModeState == RM_SCAN_IDLE_MODE) {
969                 rm_tch_ctrl_leave_auto_mode();
970                 g_stTs.u8ScanModeState = RM_SCAN_ACTIVE_MODE;
971                 usleep_range(10000, 10050);/*msleep(10);*/
972         }
973         mutex_unlock(&g_stTs.mutex_scan_mode);
974 }
975
976 static u32 rm_tch_get_platform_id(u8 *p)
977 {
978         u32 u32Ret;
979         struct rm_spi_ts_platform_data *pdata;
980         pdata = g_input_dev->dev.parent->platform_data;
981         u32Ret = copy_to_user(p, &pdata->platform_id,
982                 sizeof(pdata->platform_id));
983         if (u32Ret != 0)
984                 return 0;
985         return 1;
986 }
987
988 static u32 rm_tch_get_gpio_sensor_select(u8 *p)
989 {
990         u32 u32Ret = 0;
991         /* wait to be implemented...
992                 struct rm_spi_ts_platform_data *pdata;
993                 pdata = g_input_dev->dev.parent->platform_data;
994                 u32Ret = gpio_set_value(pdata->gpio_sensor_select0)
995                         | (1 << gpio_set_value(pdata->gpio_sensor_select1));
996         */
997         u32Ret = copy_to_user(p, &u32Ret, sizeof(u32Ret));
998         if (u32Ret != 0)
999                 return FAIL;
1000
1001         return OK;
1002 }
1003
1004 /*===========================================================================*/
1005 static int rm_tch_ts_send_signal(int pid, int iInfo)
1006 {
1007         struct siginfo info;
1008         struct task_struct *t;
1009         int ret = OK;
1010
1011         static DEFINE_MUTEX(lock);
1012
1013         if (!pid) {
1014                 dev_err(&g_spi->dev, "Raydium - %s : pid failed\n", __func__);
1015                 return FAIL;
1016         }
1017
1018         mutex_lock(&lock);
1019         /* send the signal */
1020         memset(&info, 0, sizeof(struct siginfo));
1021         info.si_signo = RM_TS_SIGNAL;
1022         info.si_code = SI_QUEUE;
1023         /*
1024                 this is bit of a trickery: SI_QUEUE is normally
1025                 used by sigqueue from user space, and kernel
1026                 space should use SI_KERNEL. But if SI_KERNEL
1027                 is used the real_time data is not delivered to
1028                 the user space signal handler function.
1029         */
1030         info.si_int = iInfo;    /*real time signals may have 32 bits of data.*/
1031
1032         rcu_read_lock();
1033         t = find_task_by_vpid(pid);
1034         rcu_read_unlock();
1035         if (t == NULL) {
1036                 dev_err(&g_spi->dev, "Raydium - %s : no such pid\n", __func__);
1037                 return FAIL;
1038         } else
1039                 /*send the signal*/
1040                 ret = send_sig_info(RM_TS_SIGNAL, &info, t);
1041
1042         if (ret < 0) {
1043                 dev_err(&g_spi->dev, "Raydium - %s : send sig failed err:%d\n",
1044                         __func__, ret);
1045                 return FAIL;
1046         }
1047         mutex_unlock(&lock);
1048         return OK;
1049 }
1050
1051 /*=============================================================================
1052         Description:
1053                 Queuing functions.
1054         Input:
1055                 N/A
1056         Output:
1057                 0:succeed
1058                 others:error code
1059 =============================================================================*/
1060 static void rm_tch_queue_reset(void)
1061 {
1062         g_stQ.u16Rear = 0;
1063         g_stQ.u16Front = 0;
1064 }
1065
1066 static int rm_tch_queue_init(void)
1067 {
1068         rm_tch_queue_reset();
1069         g_stQ.pQueue = kmalloc(QUEUE_COUNT * RM_RAW_DATA_LENGTH, GFP_KERNEL);
1070         if (g_stQ.pQueue == NULL)
1071                 return -ENOMEM;
1072
1073         return 0;
1074 }
1075
1076 static void rm_tch_queue_free(void)
1077 {
1078         if (!g_stQ.pQueue)
1079                 return;
1080         kfree(g_stQ.pQueue);
1081         g_stQ.pQueue = NULL;
1082 }
1083
1084 #ifdef ENABLE_CALC_QUEUE_COUNT
1085 static int rm_tch_queue_get_current_count(void)
1086 {
1087         if (g_stQ.u16Rear >= g_stQ.u16Front)
1088                 return g_stQ.u16Rear - g_stQ.u16Front;
1089
1090         return (QUEUE_COUNT - g_stQ.u16Front) + g_stQ.u16Rear;
1091 }
1092 #endif
1093
1094 /*=============================================================================
1095         Description:
1096         About full/empty buffer distinction,
1097         There are a number of solutions like:
1098         1.Always keep one slot open.
1099         2.Use a fill count to distinguish the two cases.
1100         3.Use read and write counts to get the fill count from.
1101         4.Use absolute indices.
1102         we chose "keep one slot open" to make it simple and robust
1103         and also avoid race condition.
1104         Input:
1105                 N/A
1106         Output:
1107                 1:empty
1108                 0:not empty
1109 =============================================================================*/
1110 static int rm_tch_queue_is_empty(void)
1111 {
1112         if (g_stQ.u16Rear == g_stQ.u16Front)
1113                 return 1;
1114         return 0;
1115 }
1116
1117 /*=============================================================================
1118         Description:
1119         check queue full.
1120         Input:
1121                 N/A
1122         Output:
1123                 1:full
1124                 0:not full
1125 =============================================================================*/
1126 static int rm_tch_queue_is_full(void)
1127 {
1128         u16 u16Front = g_stQ.u16Front;
1129         if (g_stQ.u16Rear + 1 == u16Front)
1130                 return 1;
1131
1132         if ((g_stQ.u16Rear == (QUEUE_COUNT - 1)) && (u16Front == 0))
1133                 return 1;
1134
1135         return 0;
1136 }
1137
1138 static void *rm_tch_enqueue_start(void)
1139 {
1140         if (!g_stQ.pQueue)      /*error handling for no memory*/
1141                 return NULL;
1142
1143         if (!rm_tch_queue_is_full()) {
1144                 g_service_busy_report_count = 100 ;
1145                 return &g_stQ.pQueue[g_stQ.u16Rear];
1146         }
1147
1148         if (g_service_busy_report_count < 0) {
1149                 g_service_busy_report_count = 100;
1150                 rm_printk("Raydium - touch service is busy,try again.\n");
1151         } else {
1152                 g_service_busy_report_count--;
1153         }
1154         return NULL;
1155 }
1156
1157 static void rm_tch_enqueue_finish(void)
1158 {
1159         if (g_stQ.u16Rear == (QUEUE_COUNT - 1))
1160                 g_stQ.u16Rear = 0;
1161         else
1162                 g_stQ.u16Rear++;
1163 }
1164
1165 static void *rm_tch_dequeue_start(void)
1166 {
1167         if (!rm_tch_queue_is_empty())
1168                 return &g_stQ.pQueue[g_stQ.u16Front];
1169
1170         return NULL;
1171 }
1172
1173 static void rm_tch_dequeue_finish(void)
1174 {
1175         if (g_stQ.u16Front == (QUEUE_COUNT - 1))
1176                 g_stQ.u16Front = 0;
1177         else
1178                 g_stQ.u16Front++;
1179 }
1180
1181 static long rm_tch_queue_read_raw_data(u8 *p, u32 u32Len)
1182 {
1183         u8 *pQueue;
1184         u32 u32Ret;
1185         pQueue = rm_tch_dequeue_start();
1186         if (!pQueue)
1187                 return 0;
1188
1189         u32Ret = copy_to_user(p, pQueue, u32Len);
1190         if (u32Ret != 0)
1191                 return 0;
1192
1193         rm_tch_dequeue_finish();
1194         return 1;
1195 }
1196 /*===========================================================================*/
1197 static void rm_work_handler(struct work_struct *work)
1198 {
1199         void *pKernelBuffer;
1200         u32 u32Flag;
1201         int iRet;
1202
1203         if (g_stTs.bInitFinish == false || g_stTs.bIsSuspended)
1204                 return;
1205
1206         mutex_lock(&g_stTs.mutex_scan_mode);
1207
1208         iRet = rm_tch_ctrl_clear_int();
1209
1210         u32Flag = rm_tch_ctrl_configure();
1211
1212         if (u32Flag & RM_NEED_TO_SEND_SCAN)
1213                 rm_tch_ctrl_scan_start();
1214
1215         if (u32Flag & RM_NEED_TO_READ_RAW_DATA) {
1216                 pKernelBuffer = rm_tch_enqueue_start();
1217                 if (pKernelBuffer) {
1218                         iRet = rm_tch_read_image_data((u8 *) pKernelBuffer);
1219                         if (iRet)
1220                                 rm_tch_enqueue_finish();
1221                 }
1222         }
1223         mutex_unlock(&g_stTs.mutex_scan_mode);
1224
1225         if (u32Flag & RM_NEED_TO_SEND_SIGNAL) {
1226                 if (g_stTs.bCalcFinish) {
1227                         g_stTs.bCalcFinish = 0;
1228                         rm_tch_ts_send_signal(g_stTs.ulHalPID, RM_SIGNAL_INTR);
1229                 }
1230         }
1231 }
1232
1233 static void rm_tch_init_ts_structure_part(void)
1234 {
1235         g_stTs.bInitFinish = 0;
1236         g_stTs.bCalcFinish = 0;
1237         g_stTs.bEnableScriber = 0;
1238         g_stTs.bIsSuspended = 0;
1239 #ifdef ENABLE_SLOW_SCAN
1240         g_stTs.bEnableSlowScan = false;
1241 #endif
1242         g_stTs.u8ScanModeState = RM_SCAN_ACTIVE_MODE;
1243
1244         g_pu8BurstReadBuf = NULL;
1245
1246         rm_ctrl_watchdog_func(0);
1247         rm_tch_ctrl_init();
1248 }
1249 /*===========================================================================*/
1250 static void rm_ctrl_watchdog_func(unsigned int u32Enable)
1251 {
1252         g_stTs.u8WatchDogFlg = 0;
1253         g_stTs.u32WatchDogCnt = 0;
1254         g_stTs.u8WatchDogCheck = 0;
1255
1256         g_stTs.u8WatchDogEnable = u32Enable & 0x01;
1257         g_stTs.u32WatchDogTime = u32Enable >> 16;
1258
1259         if (u32Enable&0x01) {
1260                 g_stTs.u8WatchDogEnable = 1;
1261                 g_stTs.u32WatchDogTime = u32Enable >> 16;
1262         } else {
1263                 g_stTs.u8WatchDogEnable = 0;
1264                 g_stTs.u32WatchDogTime = 0xFFFFFFFF;
1265         }
1266
1267         if ((g_stCtrl.bDebugMessage & DEBUG_DRIVER) == DEBUG_DRIVER)
1268                 rm_printk("Raydium - WatchDogEnable=%d, WatchDogTime=%d\n",
1269                         g_stTs.u8WatchDogEnable, g_stTs.u32WatchDogTime);
1270
1271 }
1272
1273 static void rm_watchdog_work_function(unsigned char scan_mode)
1274 {
1275         if ((g_stTs.u8WatchDogEnable == 0) || (g_stTs.bInitFinish == 0))
1276                 return;
1277
1278         if (g_stTs.u32WatchDogCnt++ >= g_stTs.u32WatchDogTime) {
1279                 if ((g_stCtrl.bDebugMessage & DEBUG_DRIVER) == DEBUG_DRIVER)
1280                         rm_printk("##Raydium - watchdog work: Time:%dsec Cnt:%d,Flg:%d(%x)\n",
1281                                 g_stTs.u32WatchDogTime,
1282                                 g_stTs.u32WatchDogCnt,
1283                                 g_stTs.u8WatchDogFlg,
1284                                 g_stTs.u8ScanModeState);
1285
1286                 switch (scan_mode) {
1287                 case RM_SCAN_ACTIVE_MODE:
1288                         g_stTs.u32WatchDogCnt = 0;
1289                         g_stTs.u8WatchDogFlg = 1;
1290                         break;
1291                 case RM_SCAN_IDLE_MODE:
1292                         g_stTs.u32WatchDogCnt = 0;
1293                         g_stTs.u8WatchDogCheck = 1;
1294                         break;
1295                 }
1296         }
1297
1298         if (g_stTs.u8WatchDogFlg) {
1299                 /*WATCH DOG RESET*/
1300                 rm_printk("##Raydium - WatchDog Resume\n");
1301                 rm_tch_init_ts_structure_part();
1302                 g_stTs.bIsSuspended = true;
1303                 rm_tch_cmd_process(0, g_stRmWatchdogCmd, NULL);
1304                 g_stTs.bIsSuspended = false;
1305                 return;
1306         }
1307
1308 }
1309
1310 static u8 rm_timer_trigger_function(void)
1311 {
1312         static u32 u32TimerCnt; /*= 0; remove by checkpatch*/
1313
1314         if (u32TimerCnt++ < g_stCtrl.bTimerTriggerScale) {
1315                 return 0;
1316         } else {
1317                 /*rm_printk("##Raydium - rm_timer_work_handler:%x,%x\n",
1318                 g_stCtrl.bTimerTriggerScale, u32TimerCnt);*/
1319                 u32TimerCnt = 0;
1320                 return 1;
1321         }
1322
1323 }
1324
1325 static void rm_timer_work_handler(struct work_struct *work)
1326 {
1327         static u16 u32TimerCnt; /*= 0; remove by checkpatch*/
1328         if (g_stTs.bIsSuspended)
1329                 return;
1330
1331         mutex_lock(&g_stTs.mutex_scan_mode);
1332         if (rm_timer_trigger_function()) {
1333                 if (g_stTs.u8ScanModeState != RM_SCAN_ACTIVE_MODE) {
1334                         rm_watchdog_work_function(RM_SCAN_IDLE_MODE);
1335 #if (ENABLE_MANUAL_IDLE_MODE == 1)
1336                         rm_tch_spi_byte_write(RM31080_REG_11, 0x17);
1337 #endif
1338                 } else {
1339                         rm_watchdog_work_function(RM_SCAN_ACTIVE_MODE);
1340                 }
1341         }
1342         mutex_unlock(&g_stTs.mutex_scan_mode);
1343         if (g_stTs.u8WatchDogCheck == 1) {
1344                 rm_tch_ts_send_signal(g_stTs.ulHalPID,
1345                         RM_SIGNAL_WATCH_DOG_CHECK);
1346                 g_stTs.u8WatchDogCheck = 0;
1347         }
1348
1349         if (g_stTs.u8WatchDogEnable) {
1350                 u32TimerCnt++;
1351                 if (u32TimerCnt > g_stTs.u32WatchDogTime &&
1352                         g_stTs.u32WatchDogTime != g_stCtrl.bWatchDogNormalCnt) {
1353                         g_stTs.u32WatchDogTime = g_stCtrl.bWatchDogNormalCnt;
1354                         if ((g_stCtrl.bDebugMessage & DEBUG_DRIVER)
1355                                 == DEBUG_DRIVER)
1356                                 rm_printk("Raydium - WDT:Normal mode\n");
1357                         u32TimerCnt = 0;
1358                 }
1359         } else {
1360                 u32TimerCnt = 0;
1361         }
1362 }
1363
1364 /*========================================================================= */
1365 static void rm_tch_enable_irq(struct rm_tch_ts *ts)
1366 {
1367         enable_irq(ts->irq);
1368 }
1369
1370 static void rm_tch_disable_irq(struct rm_tch_ts *ts)
1371 {
1372         disable_irq(ts->irq);
1373 }
1374
1375 #ifdef ENABLE_SLOW_SCAN
1376 /*=============================================================================
1377  * Description:
1378  *              Context dependent touch system.
1379  *              Change scan speed for slowscan function.
1380  *              Change scan speed flow: (by CY,20120305)
1381  *              1.Disable auto scan ([0x09]bit4=0,[0x09]bit6=1)
1382  *              2.Clear Scan start bit ([0x11]bit0=0)
1383  *              3.Read Scan start bit until it equals 0
1384  *              4.Set LACTIVE and YACTIVE configuration
1385  *              5.Enable autoscan ([0x09]bit4=1,[0x09]bit6=1)
1386  *              6.Sleep 1 minisecond.
1387  *              7.Set Scan start bit ([0x11]bit0=1)
1388  * Input:
1389  *              N/A
1390  * Output:
1391  *              N/A
1392  *=============================================================================
1393 */
1394 static void rm_tch_ctrl_slowscan(u32 level)
1395 {
1396         if (g_stTs.u8ScanModeState == RM_SCAN_IDLE_MODE) {
1397                 rm_tch_ctrl_leave_auto_mode();
1398                 rm_ctrl_pause_auto_mode();
1399         }
1400
1401         rm_tch_ctrl_wait_for_scan_finish();
1402
1403         if (level == RM_SLOW_SCAN_LEVEL_NORMAL)
1404                 level = RM_SLOW_SCAN_LEVEL_20;
1405
1406         if (level > RM_SLOW_SCAN_LEVEL_100)
1407                 level = RM_SLOW_SCAN_LEVEL_MAX;
1408
1409         rm_tch_cmd_process((u8)(level - 1), g_stRmSlowScanCmd, NULL);
1410
1411         rm_printk("##Raydium - rm_tch_ctrl_slowscan:%x,%x,%x\n",
1412                 (level - 1),
1413                 g_stRmSlowScanCmd[0],
1414                 g_stRmSlowScanCmd[1]);
1415
1416
1417         if (g_stTs.u8ScanModeState == RM_SCAN_IDLE_MODE) {
1418                 rm_tch_ctrl_enter_auto_mode();
1419                 usleep_range(1000, 1200);
1420                 rm_tch_ctrl_scan_start();
1421         }
1422 }
1423
1424 static u32 rm_tch_slowscan_round(u32 val)
1425 {
1426         u32 i;
1427         for (i = 0; i < RM_SLOW_SCAN_LEVEL_COUNT; i++) {
1428                 if ((i * RM_SLOW_SCAN_INTERVAL) >= val)
1429                         break;
1430         }
1431         return i;
1432 }
1433
1434 static ssize_t rm_tch_slowscan_handler(const char *buf, size_t count)
1435 {
1436         unsigned long val;
1437         ssize_t error;
1438         ssize_t ret;
1439
1440         if (count < 2)
1441                 return count;
1442
1443         ret = (ssize_t) count;
1444         mutex_lock(&g_stTs.mutex_scan_mode);
1445
1446         if (count == 2) {
1447                 if (buf[0] == '0') {
1448                         g_stTs.bEnableSlowScan = false;
1449                         rm_tch_ctrl_slowscan(RM_SLOW_SCAN_LEVEL_MAX);
1450                 } else if (buf[0] == '1') {
1451                         g_stTs.bEnableSlowScan = true;
1452                         rm_tch_ctrl_slowscan(RM_SLOW_SCAN_LEVEL_60);
1453                         g_stTs.u32SlowScanLevel = RM_SLOW_SCAN_LEVEL_60;
1454                 }
1455         } else if ((buf[0] == '2') && (buf[1] == ' ')) {
1456                 error = kstrtoul(&buf[2], 10, &val);
1457
1458                 if (error) {
1459                         ret = error;
1460                 } else {
1461                         g_stTs.bEnableSlowScan = true;
1462                         g_stTs.u32SlowScanLevel = rm_tch_slowscan_round(val);
1463                         rm_tch_ctrl_slowscan(g_stTs.u32SlowScanLevel);
1464                 }
1465         }
1466
1467         mutex_unlock(&g_stTs.mutex_scan_mode);
1468         return ret;
1469 }
1470 #endif
1471
1472 static ssize_t rm_tch_slowscan_show(struct device *dev,
1473         struct device_attribute *attr, char *buf)
1474 {
1475 #ifdef ENABLE_SLOW_SCAN
1476         return sprintf(buf, "Slow Scan:%s\nScan Rate:%dHz\n",
1477                         g_stTs.bEnableSlowScan ?
1478                         "Enabled" : "Disabled",
1479                         g_stTs.u32SlowScanLevel * RM_SLOW_SCAN_INTERVAL);
1480 #else
1481         return sprintf(buf, "Not implemented yet\n");
1482 #endif
1483 }
1484
1485 static ssize_t rm_tch_slowscan_store(struct device *dev,
1486         struct device_attribute *attr,
1487         const char *buf, size_t count)
1488 {
1489 #ifdef ENABLE_SLOW_SCAN
1490         return rm_tch_slowscan_handler(buf, count);
1491 #else
1492         return count;
1493 #endif
1494 }
1495
1496 static void rm_tch_smooth_level_change(unsigned long val)
1497 {
1498         int iInfo;
1499
1500         if (val > RM_SMOOTH_LEVEL_MAX)
1501                 return;
1502
1503         g_stTs.u32SmoothLevel = val;
1504
1505         iInfo = (RM_SIGNAL_PARA_SMOOTH << 24) |
1506                         (val << 16) |
1507                         RM_SIGNAL_CHANGE_PARA;
1508
1509         rm_tch_ts_send_signal(g_stTs.ulHalPID, iInfo);
1510 }
1511
1512 static ssize_t rm_tch_smooth_level_handler(const char *buf, size_t count)
1513 {
1514         unsigned long val;
1515         ssize_t error;
1516         ssize_t ret;
1517
1518         if (count != 2)
1519                 return count;
1520
1521         ret = (ssize_t) count;
1522         error = kstrtoul(buf, 10, &val);
1523
1524         if (error)
1525                 ret = error;
1526         else
1527                 rm_tch_smooth_level_change(val);
1528
1529         return ret;
1530 }
1531
1532
1533 static ssize_t rm_tch_self_test_handler(struct rm_tch_ts *ts,
1534         const char *buf, size_t count)
1535 {
1536         unsigned long val;
1537         ssize_t error;
1538         ssize_t ret;
1539         int iInfo;
1540
1541         ret = (ssize_t) count;
1542
1543         if (count != 2)
1544                 return ret;
1545
1546         if (g_stTs.u8SelfTestStatus == RM_SELF_TEST_STATUS_TESTING)
1547                 return ret;
1548
1549         rm_tch_enter_test_mode(1);
1550
1551         g_stTs.u8SelfTestResult = RM_SELF_TEST_RESULT_PASS;
1552
1553         error = kstrtoul(buf, 10, &val);
1554
1555         if (error) {
1556                 ret = error;
1557                 rm_tch_enter_test_mode(0);
1558         } else if (val == 0) {
1559                 rm_tch_enter_test_mode(0);
1560         } else if ((val >= 0x01) && (val <= 0xFF)) {
1561                 iInfo = (RM_SIGNAL_PARA_SELF_TEST << 24) |
1562                                 (val << 16) |
1563                                 RM_SIGNAL_CHANGE_PARA;
1564                 rm_tch_ts_send_signal(g_stTs.ulHalPID, iInfo);
1565         }
1566
1567         return ret;
1568 }
1569
1570 static ssize_t rm_tch_smooth_level_show(struct device *dev,
1571         struct device_attribute *attr,
1572         char *buf)
1573 {
1574         return sprintf(buf, "Smooth level:%d\n", g_stTs.u32SmoothLevel);
1575 }
1576
1577 static ssize_t rm_tch_smooth_level_store(struct device *dev,
1578         struct device_attribute *attr,
1579         const char *buf, size_t count)
1580 {
1581         rm_tch_smooth_level_handler(buf, count);
1582         return count;
1583 }
1584
1585 static ssize_t rm_tch_self_test_show(struct device *dev,
1586         struct device_attribute *attr,
1587         char *buf)
1588 {
1589         return sprintf(buf, "Self_Test:Status:%d ,Result:%d\n",
1590                                         g_stTs.u8SelfTestStatus,
1591                                         g_stTs.u8SelfTestResult);
1592 }
1593
1594 static ssize_t rm_tch_self_test_store(struct device *dev,
1595         struct device_attribute *attr,
1596         const char *buf, size_t count)
1597 {
1598         struct rm_tch_ts *ts = dev_get_drvdata(dev);
1599         rm_tch_self_test_handler(ts, buf, count);
1600         return count;
1601 }
1602
1603 static ssize_t rm_tch_version_show(struct device *dev,
1604         struct device_attribute *attr,
1605         char *buf)
1606 {
1607         return sprintf(buf, "Release V 0x%02X, Test V 0x%02X\n",
1608                 g_stTs.u8Version,
1609                 g_stTs.u8TestVersion);
1610 }
1611
1612 static ssize_t rm_tch_version_store(struct device *dev,
1613         struct device_attribute *attr,
1614         const char *buf, size_t count)
1615 {
1616         return count;
1617 }
1618
1619 static ssize_t rm_tch_module_detect_show(struct device *dev,
1620         struct device_attribute *attr,
1621         char *buf)
1622 {
1623         return sprintf(buf, "%s\n", "Raydium Touch Module");
1624 }
1625
1626 static ssize_t rm_tch_module_detect_store(struct device *dev,
1627         struct device_attribute *attr,
1628         const char *buf, size_t count)
1629 {
1630         return count;
1631 }
1632
1633 static DEVICE_ATTR(slowscan_enable, 0640, rm_tch_slowscan_show,
1634                                         rm_tch_slowscan_store);
1635 static DEVICE_ATTR(smooth_level, 0640, rm_tch_smooth_level_show,
1636                                         rm_tch_smooth_level_store);
1637 static DEVICE_ATTR(self_test, 0640, rm_tch_self_test_show,
1638                                         rm_tch_self_test_store);
1639 static DEVICE_ATTR(version, 0640, rm_tch_version_show,
1640                                         rm_tch_version_store);
1641 static DEVICE_ATTR(module_detect, 0640, rm_tch_module_detect_show,
1642                                         rm_tch_module_detect_store);
1643
1644 static struct attribute *rm_ts_attributes[] = {
1645         &dev_attr_slowscan_enable.attr,
1646         &dev_attr_smooth_level.attr,
1647         &dev_attr_self_test.attr,
1648         &dev_attr_version.attr,
1649         &dev_attr_module_detect.attr,
1650         NULL
1651 };
1652
1653 static const struct attribute_group rm_ts_attr_group = {
1654         .attrs = rm_ts_attributes,
1655 };
1656
1657 static int rm_tch_input_open(struct input_dev *input)
1658 {
1659         struct rm_tch_ts *ts = input_get_drvdata(input);
1660
1661         if (!ts->disabled && !ts->suspended)
1662                 rm_tch_enable_irq(ts);
1663
1664         return 0;
1665 }
1666
1667 static void rm_tch_input_close(struct input_dev *input)
1668 {
1669         struct rm_tch_ts *ts = input_get_drvdata(input);
1670
1671         if (!ts->disabled && !ts->suspended)
1672                 rm_tch_disable_irq(ts);
1673 }
1674
1675 static irqreturn_t rm_tch_irq(int irq, void *handle)
1676 {
1677         g_stTs.u32WatchDogCnt = 0;
1678
1679         trace_touchscreen_raydium_irq("Raydium_interrupt");
1680         mutex_lock(&g_stTs.mutex_scan_mode);
1681         if (g_stCtrl.bfPowerMode &&
1682                         (g_stTs.u8ScanModeState == RM_SCAN_IDLE_MODE))
1683                 input_event(g_input_dev, EV_MSC, MSC_ACTIVITY, 1);
1684         mutex_unlock(&g_stTs.mutex_scan_mode);
1685
1686         if (g_stTs.bInitFinish && g_stTs.bIsSuspended == false)
1687                 queue_work(g_stTs.rm_workqueue, &g_stTs.rm_work);
1688
1689         return IRQ_HANDLED;
1690 }
1691
1692 static void rm_tch_enter_test_mode(u8 flag)
1693 {
1694         if (flag) { /*enter test mode*/
1695                 g_stTs.u8SelfTestStatus = RM_SELF_TEST_STATUS_TESTING;
1696                 g_stTs.bIsSuspended = true;
1697                 flush_workqueue(g_stTs.rm_workqueue);
1698                 flush_workqueue(g_stTs.rm_timer_workqueue);
1699         } else {/*leave test mode*/
1700                 g_stTs.bIsSuspended = false;
1701                 rm_tch_init_ts_structure_part();
1702         }
1703
1704         rm_tch_cmd_process(flag, g_stRmTestModeCmd, NULL);
1705
1706         if (!flag)
1707                 g_stTs.u8SelfTestStatus = RM_SELF_TEST_STATUS_FINISH;
1708 }
1709
1710 void rm_tch_set_variable(unsigned int index, unsigned int arg)
1711 {
1712         switch (index) {
1713         case RM_VARIABLE_SELF_TEST_RESULT:
1714                 g_stTs.u8SelfTestResult = (u8) arg;
1715                 rm_tch_enter_test_mode(0);
1716                 break;
1717         case RM_VARIABLE_SCRIBER_FLAG:
1718                 g_stTs.bEnableScriber = (bool) arg;
1719                 break;
1720         case RM_VARIABLE_AUTOSCAN_FLAG:
1721                 g_stCtrl.bfPowerMode = (bool) arg;
1722                 break;
1723         case RM_VARIABLE_TEST_VERSION:
1724                 g_stTs.u8TestVersion = (u8) arg;
1725                 break;
1726         case RM_VARIABLE_VERSION:
1727                 g_stTs.u8Version = (u8) arg;
1728                 dev_info(&g_spi->dev, "Raydium - Firmware v%d.%d\n",
1729                                 g_stTs.u8Version, g_stTs.u8TestVersion);
1730                 break;
1731         case RM_VARIABLE_IDLEMODECHECK:
1732                 g_stCtrl.bfIdleModeCheck = (u8) arg;
1733                 if ((g_stCtrl.bDebugMessage & DEBUG_DRIVER)
1734                         == DEBUG_DRIVER)
1735                         rm_printk("Raydium - bfIdleModeCheck %2x\n",
1736                                 arg);
1737                 break;
1738         case RM_VARIABLE_REPEAT:
1739                 /*rm_printk("Raydium - Repeat %d\n", arg);*/
1740                 g_stTs.u8Repeat = (u8) arg;
1741         case RM_VARIABLE_WATCHDOG_FLAG:
1742                 rm_ctrl_watchdog_func(arg);
1743                 break;
1744         default:
1745                 break;
1746         }
1747
1748 }
1749 static u32 rm_tch_get_variable(unsigned int index, unsigned int arg)
1750 {
1751         u32 ret = 0;
1752         switch (index) {
1753         case RM_VARIABLE_PLATFORM_ID:
1754                 ret = rm_tch_get_platform_id((u8 *) arg);
1755                 break;
1756         case RM_VARIABLE_GPIO_SELECT:
1757                 ret = rm_tch_get_gpio_sensor_select((u8 *) arg);
1758                 break;
1759         default:
1760                 break;
1761         }
1762         return ret;
1763 }
1764
1765 static void rm_tch_init_ts_structure(void)
1766 {
1767         g_stTs.ulHalPID = 0;
1768         memset(&g_stTs, 0, sizeof(struct rm31080a_ts_para));
1769
1770 #ifdef ENABLE_SLOW_SCAN
1771         g_stTs.u32SlowScanLevel = RM_SLOW_SCAN_LEVEL_MAX;
1772 #endif
1773
1774         g_stTs.rm_workqueue = create_singlethread_workqueue("rm_work");
1775         INIT_WORK(&g_stTs.rm_work, rm_work_handler);
1776
1777         g_stTs.rm_timer_workqueue =
1778                 create_singlethread_workqueue("rm_idle_work");
1779         INIT_WORK(&g_stTs.rm_timer_work, rm_timer_work_handler);
1780
1781         wake_lock_init(&g_stTs.Wakelock_Initialization,
1782                 WAKE_LOCK_SUSPEND, "TouchInitialLock");
1783
1784         mutex_init(&g_stTs.mutex_scan_mode);
1785         mutex_init(&g_stTs.mutex_spi_rw);
1786 }
1787 static int rm31080_voltage_notifier_1v8(struct notifier_block *nb,
1788         unsigned long event, void *ignored)
1789 {
1790         if ((g_stCtrl.bDebugMessage & DEBUG_DRIVER) == DEBUG_DRIVER)
1791                 rm_printk("Raydium - REGULATOR EVENT 1.8V:0x%x\n",
1792                         (unsigned int)event);
1793
1794         return NOTIFY_OK;
1795 }
1796
1797 static int rm31080_voltage_notifier_3v3(struct notifier_block *nb,
1798         unsigned long event, void *ignored)
1799 {
1800         if ((g_stCtrl.bDebugMessage & DEBUG_DRIVER) == DEBUG_DRIVER)
1801                 rm_printk("Raydium - REGULATOR EVENT 3.3V:0x%x\n",
1802                         (unsigned int)event);
1803
1804         return NOTIFY_OK;
1805 }
1806 /*===========================================================================*/
1807 static void rm_ctrl_start(struct rm_tch_ts *ts)
1808 {
1809         if (g_stTs.bIsSuspended == false)
1810                 return;
1811         g_stTs.bIsSuspended = false;
1812
1813         mutex_lock(&g_stTs.mutex_scan_mode);
1814
1815         rm_tch_init_ts_structure_part();
1816
1817         rm_tch_cmd_process(0, g_stRmStartCmd, ts);
1818
1819         mutex_unlock(&g_stTs.mutex_scan_mode);
1820 }
1821
1822 static void rm_ctrl_stop(struct rm_tch_ts *ts)
1823 {
1824         if (g_stTs.bIsSuspended == true)
1825                 return;
1826
1827         g_stTs.bIsSuspended = true;
1828         g_stTs.bInitFinish = 0;
1829
1830         if (g_stTs.u8ScanModeState == RM_SCAN_IDLE_MODE)
1831                 rm_ctrl_pause_auto_mode();
1832
1833         rm_tch_ctrl_wait_for_scan_finish();
1834
1835         mutex_lock(&g_stTs.mutex_scan_mode);
1836
1837         rm_tch_cmd_process(0, g_stRmEndCmd, ts);
1838
1839         rm_tch_ctrl_wait_for_scan_finish();
1840
1841         rm_tch_cmd_process(1, g_stRmEndCmd, ts);
1842
1843         mutex_unlock(&g_stTs.mutex_scan_mode);
1844 }
1845
1846 #ifdef CONFIG_PM
1847 static int rm_tch_suspend(struct device *dev)
1848 {
1849         struct rm_tch_ts *ts = dev_get_drvdata(dev);
1850         rm_ctrl_stop(ts);
1851         return 0;
1852 }
1853
1854 static int rm_tch_resume(struct device *dev)
1855 {
1856         struct rm_tch_ts *ts = dev_get_drvdata(dev);
1857         wake_lock_timeout(&g_stTs.Wakelock_Initialization,
1858                 TCH_WAKE_LOCK_TIMEOUT);
1859         if (wake_lock_active(&g_stTs.Wakelock_Initialization))
1860                 rm_ctrl_start(ts);
1861         return 0;
1862 }
1863
1864 #ifdef CONFIG_HAS_EARLYSUSPEND
1865 static void rm_tch_early_suspend(struct early_suspend *es)
1866 {
1867         struct rm_tch_ts *ts;
1868         struct device *dev;
1869
1870         ts = container_of(es, struct rm_tch_ts, early_suspend);
1871         dev = ts->dev;
1872
1873         if (rm_tch_suspend(dev) != 0)
1874                 dev_err(dev, "Raydium - %s : failed\n", __func__);
1875 }
1876
1877 static void rm_tch_early_resume(struct early_suspend *es)
1878 {
1879         struct rm_tch_ts *ts;
1880         struct device *dev;
1881
1882         ts = container_of(es, struct rm_tch_ts, early_suspend);
1883         dev = ts->dev;
1884
1885         if (rm_tch_resume(dev) != 0)
1886                 dev_err(dev, "Raydium - %s : failed\n", __func__);
1887 }
1888 #else
1889 static const struct dev_pm_ops rm_tch_pm_ops = {
1890         .suspend = rm_tch_suspend,
1891         .resume = rm_tch_resume,
1892 };
1893 #endif                  /*CONFIG_HAS_EARLYSUSPEND*/
1894 #endif                  /*CONFIG_PM*/
1895
1896 /* NVIDIA 20121026 */
1897 /* support to disable power and clock when display is off */
1898 static int rm_tch_input_enable(struct input_dev *in_dev)
1899 {
1900         int error = 0;
1901
1902 #ifdef CONFIG_PM
1903         struct rm_tch_ts *ts = input_get_drvdata(in_dev);
1904
1905         dev_info(ts->dev, "Raydium - resume\n");
1906         error = rm_tch_resume(ts->dev);
1907         if (error)
1908                 dev_err(ts->dev, "Raydium - %s : failed\n", __func__);
1909 #endif
1910
1911         return error;
1912 }
1913
1914 static int rm_tch_input_disable(struct input_dev *in_dev)
1915 {
1916         int error = 0;
1917
1918 #ifdef CONFIG_PM
1919         struct rm_tch_ts *ts = input_get_drvdata(in_dev);
1920
1921         dev_info(ts->dev, "Raydium - suspend\n");
1922         error = rm_tch_suspend(ts->dev);
1923         if (error)
1924                 dev_err(ts->dev, "Raydium - %s : failed\n", __func__);
1925         else
1926                 dev_info(ts->dev, "Raydium - suspend done\n");
1927 #endif
1928
1929         return error;
1930 }
1931
1932 static void rm_tch_set_input_resolution(unsigned int x, unsigned int y)
1933 {
1934         input_set_abs_params(g_input_dev, ABS_X, 0, x - 1, 0, 0);
1935         input_set_abs_params(g_input_dev, ABS_Y, 0, y - 1, 0, 0);
1936         input_set_abs_params(g_input_dev, ABS_MT_POSITION_X, 0, x - 1, 0, 0);
1937         input_set_abs_params(g_input_dev, ABS_MT_POSITION_Y, 0, y - 1, 0, 0);
1938 }
1939
1940 struct rm_tch_ts *rm_tch_input_init(struct device *dev, unsigned int irq,
1941         const struct rm_tch_bus_ops *bops) {
1942
1943         struct rm_tch_ts *ts;
1944         struct input_dev *input_dev;
1945         struct rm_spi_ts_platform_data *pdata;
1946         int err;
1947
1948         if (!irq) {
1949                 dev_err(dev, "Raydium - no IRQ?\n");
1950                 err = -EINVAL;
1951                 goto err_out;
1952         }
1953
1954         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
1955
1956         input_dev = input_allocate_device();
1957
1958         if (!ts || !input_dev) {
1959                 dev_err(dev, "Raydium - Failed to allocate memory\n");
1960                 err = -ENOMEM;
1961                 goto err_free_mem;
1962         }
1963
1964         g_input_dev = input_dev;
1965
1966         ts->bops = bops;
1967         ts->dev = dev;
1968         ts->input = input_dev;
1969         ts->irq = irq;
1970
1971         pdata = dev->platform_data;
1972
1973         if (pdata->name_of_clock || pdata->name_of_clock_con) {
1974                 ts->clk = clk_get_sys(pdata->name_of_clock,
1975                         pdata->name_of_clock_con);
1976                 if (IS_ERR(ts->clk)) {
1977                         dev_err(&g_spi->dev, "Raydium - failed to get touch_clk: (%s, %s)\n",
1978                                 pdata->name_of_clock, pdata->name_of_clock_con);
1979                         err = -EINVAL;
1980                         goto err_free_mem;
1981                 }
1982         }
1983
1984         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
1985
1986         input_dev->name = "raydium_ts";
1987         input_dev->phys = ts->phys;
1988         input_dev->dev.parent = dev;
1989         input_dev->id.bustype = bops->bustype;
1990
1991         input_dev->enable = rm_tch_input_enable;
1992         input_dev->disable = rm_tch_input_disable;
1993         input_dev->enabled = true;
1994         input_dev->open = rm_tch_input_open;
1995         input_dev->close = rm_tch_input_close;
1996         input_dev->hint_events_per_packet = 256U;
1997
1998         input_set_drvdata(input_dev, ts);
1999         input_set_capability(input_dev, EV_MSC, MSC_ACTIVITY);
2000
2001         __set_bit(EV_ABS, input_dev->evbit);
2002         __set_bit(ABS_X, input_dev->absbit);
2003         __set_bit(ABS_Y, input_dev->absbit);
2004
2005         __set_bit(EV_KEY, input_dev->evbit);
2006         __set_bit(BTN_TOUCH, input_dev->keybit);
2007
2008         rm_tch_set_input_resolution(RM_INPUT_RESOLUTION_X,
2009                                                         RM_INPUT_RESOLUTION_Y);
2010         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 0xFF, 0, 0);
2011         input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 0xFF, 0, 0);
2012         input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 0xFF, 0, 0);
2013         input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, 32, 0, 0);
2014
2015         err = request_threaded_irq(ts->irq, NULL, rm_tch_irq,
2016                         IRQF_TRIGGER_RISING | IRQF_ONESHOT, dev_name(dev), ts);
2017         if (err) {
2018                 dev_err(dev, "Raydium - irq %d busy?\n", ts->irq);
2019                 goto err_free_mem;
2020         }
2021         mutex_init(&ts->access_mutex);
2022 #ifdef CONFIG_HAS_EARLYSUSPEND
2023         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
2024         ts->early_suspend.suspend = rm_tch_early_suspend;
2025         ts->early_suspend.resume = rm_tch_early_resume;
2026         register_early_suspend(&ts->early_suspend);
2027 #endif
2028
2029         rm_tch_disable_irq(ts);
2030
2031         err = sysfs_create_group(&dev->kobj, &rm_ts_attr_group);
2032         if (err)
2033                 goto err_free_irq;
2034
2035         err = input_register_device(input_dev);
2036         if (err)
2037                 goto err_remove_attr;
2038
2039         return ts;
2040
2041 err_remove_attr:
2042         sysfs_remove_group(&dev->kobj, &rm_ts_attr_group);
2043 err_free_irq:
2044         free_irq(ts->irq, ts);
2045 err_free_mem:
2046         input_free_device(input_dev);
2047         kfree(ts);
2048 err_out:
2049         return ERR_PTR(err);
2050 }
2051
2052 static int dev_open(struct inode *inode, struct file *filp)
2053 {
2054         return 0;
2055 }
2056
2057 static int dev_release(struct inode *inode, struct file *filp)
2058 {
2059         g_stTs.bInitFinish = 0;
2060         rm_tch_enter_manual_mode();
2061         return 0;
2062 }
2063
2064 static ssize_t
2065 dev_read(struct file *filp, char __user *buf, size_t count, loff_t *pos)
2066 {
2067         ssize_t missing, status;
2068         int ret;
2069         u8 *pMyBuf;
2070
2071         pMyBuf = kmalloc(count, GFP_KERNEL);
2072         if (pMyBuf == NULL)
2073                 return -ENOMEM;
2074
2075         pMyBuf[0] = buf[0];
2076         ret = rm_tch_spi_read(pMyBuf[0], pMyBuf, count);
2077
2078         if (ret) {
2079                 status = count;
2080                 missing = copy_to_user(buf, pMyBuf, count);
2081
2082                 if (missing == status)
2083                         status = -EFAULT;
2084                 else
2085                         status = status - missing;
2086         } else {
2087                 status = -EFAULT;
2088                 rm_printk("Raydium - rm_tch_spi_read() fail\n");
2089         }
2090
2091         kfree(pMyBuf);
2092         return status;
2093 }
2094
2095 static ssize_t
2096 dev_write(struct file *filp, const char __user *buf,
2097         size_t count, loff_t *pos)
2098 {
2099         u8 *pMyBuf;
2100         int ret;
2101         unsigned long missing;
2102         ssize_t status = 0;
2103
2104         pMyBuf = kmalloc(count, GFP_KERNEL);
2105         if (pMyBuf == NULL)
2106                 return -ENOMEM;
2107
2108         missing = copy_from_user(pMyBuf, buf, count);
2109         if (missing == 0) {
2110                 ret = rm_tch_spi_write(pMyBuf, count);
2111                 if (ret)
2112                         status = count;
2113                 else
2114                         status = -EFAULT;
2115         } else
2116                 status = -EFAULT;
2117
2118         kfree(pMyBuf);
2119         return status;
2120 }
2121
2122 /*=============================================================================
2123         Description:
2124                 I/O Control routin.
2125         Input:
2126                 file:
2127                 cmd :
2128                 arg :
2129         Output:
2130                 1: succeed
2131                 0: failed
2132         Note: To avoid context switch,please don't add debug message
2133                   in this function.
2134 =============================================================================*/
2135 static long dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2136 {
2137         long ret = true;
2138         unsigned int index;
2139         index = (cmd >> 16) & 0xFFFF;
2140         switch (cmd & 0xFFFF) {
2141         case RM_IOCTL_REPORT_POINT:
2142                 raydium_report_pointer((void *)arg);
2143                 break;
2144         case RM_IOCTL_SET_HAL_PID:
2145                 g_stTs.ulHalPID = arg;
2146                 break;
2147         case RM_IOCTL_INIT_START:
2148                 g_stTs.bInitFinish = 0;
2149                 rm_tch_enter_manual_mode();
2150                 break;
2151         case RM_IOCTL_INIT_END:
2152                 g_stTs.bInitFinish = 1;
2153                 g_stTs.bCalcFinish = 1;
2154                 if (wake_lock_active(&g_stTs.Wakelock_Initialization))
2155                         wake_unlock(&g_stTs.Wakelock_Initialization);
2156                 rm_printk("Raydium - resume done\n");
2157                 break;
2158         case RM_IOCTL_FINISH_CALC:
2159                 g_stTs.bCalcFinish = 1;
2160                 break;
2161         case RM_IOCTL_SCRIBER_CTRL:
2162                 g_stTs.bEnableScriber = (bool) arg;
2163                 break;
2164         case RM_IOCTL_READ_RAW_DATA:
2165                 ret = rm_tch_queue_read_raw_data((u8 *) arg, index);
2166                 break;
2167         case RM_IOCTL_SET_PARAMETER:
2168                 rm_tch_ctrl_set_parameter((void *)arg);
2169                 rm_tch_set_input_resolution(g_stCtrl.u16ResolutionX,
2170                         g_stCtrl.u16ResolutionY);
2171                 break;
2172         case RM_IOCTL_SET_VARIABLE:
2173                 rm_tch_set_variable(index, arg);
2174                 break;
2175         case RM_IOCTL_GET_SACN_MODE:
2176                 rm_tch_ctrl_get_idle_mode((u8 *) arg);
2177                 break;
2178         case RM_IOCTL_GET_VARIABLE:
2179                 ret = rm_tch_get_variable(index, arg);
2180                 break;
2181         case RM_IOCTL_SET_KRL_TBL:
2182                 rm_set_kernel_tbl(index, (u8 *)arg);
2183                 break;
2184         case RM_IOCTL_WATCH_DOG:
2185                 g_stTs.u8WatchDogFlg = 1;
2186                 g_stTs.u8WatchDogCheck = 0;
2187                 break;
2188         default:
2189                 break;
2190         }
2191         return ret;
2192 }
2193
2194 static const struct file_operations dev_fops = {
2195         .owner = THIS_MODULE,
2196         .open = dev_open,
2197         .release = dev_release,
2198         .read = dev_read,
2199         .write = dev_write,
2200         .unlocked_ioctl = dev_ioctl,
2201 };
2202
2203 static struct miscdevice raydium_ts_miscdev = {
2204         .minor = MISC_DYNAMIC_MINOR,
2205         .name = "raydium_ts",
2206         .fops = &dev_fops,
2207 };
2208
2209 static const struct rm_tch_bus_ops rm_tch_spi_bus_ops = {
2210         .bustype = BUS_SPI,
2211 };
2212
2213 static void init_ts_timer(void)
2214 {
2215         init_timer(&ts_timer_triggle);
2216         ts_timer_triggle.function = ts_timer_triggle_function;
2217         ts_timer_triggle.data = ((unsigned long) 0);
2218         ts_timer_triggle.expires = jiffies + TS_TIMER_PERIOD;
2219 }
2220 static void ts_timer_triggle_function(unsigned long option)
2221 {
2222         queue_work(g_stTs.rm_timer_workqueue, &g_stTs.rm_timer_work);
2223         ts_timer_triggle.expires = jiffies + TS_TIMER_PERIOD;
2224         add_timer(&ts_timer_triggle);
2225 }
2226
2227 /*===========================================================================*/
2228 #if ENABLE_SPI_SETTING
2229 static int rm_tch_spi_setting(u32 speed)
2230 {
2231         int err;
2232         if ((speed == 0) || (speed > 18))
2233                 return FAIL;
2234
2235         g_spi->max_speed_hz = speed * 1000 * 1000;
2236         err = spi_setup(g_spi);
2237         if (err) {
2238                 dev_dbg(&g_spi->dev, "Raydium - Change SPI setting failed\n");
2239                 return FAIL;
2240         }
2241         return OK;
2242 }
2243 #endif
2244
2245 static int rm_tch_spi_remove(struct spi_device *spi)
2246 {
2247         struct rm_tch_ts *ts = spi_get_drvdata(spi);
2248         del_timer(&ts_timer_triggle);
2249         if (g_stTs.rm_timer_workqueue)
2250                 destroy_workqueue(g_stTs.rm_timer_workqueue);
2251
2252         rm_tch_queue_free();
2253
2254         if (g_stTs.rm_workqueue)
2255                 destroy_workqueue(g_stTs.rm_workqueue);
2256
2257         if (&g_stTs.Wakelock_Initialization)
2258                 wake_lock_destroy(&g_stTs.Wakelock_Initialization);
2259
2260         mutex_destroy(&g_stTs.mutex_scan_mode);
2261         mutex_destroy(&g_stTs.mutex_spi_rw);
2262
2263         sysfs_remove_group(&raydium_ts_miscdev.this_device->kobj,
2264                                                 &rm_ts_attr_group);
2265         misc_deregister(&raydium_ts_miscdev);
2266         sysfs_remove_group(&ts->dev->kobj, &rm_ts_attr_group);
2267         free_irq(ts->irq, ts);
2268         input_unregister_device(ts->input);
2269
2270         if (ts->regulator_3v3 && ts->regulator_1v8) {
2271                 regulator_unregister_notifier(ts->regulator_3v3, &ts->nb_3v3);
2272                 regulator_unregister_notifier(ts->regulator_1v8, &ts->nb_1v8);
2273                 regulator_disable(ts->regulator_3v3);
2274                 regulator_disable(ts->regulator_1v8);
2275         }
2276
2277         if (ts->clk)
2278                 clk_disable(ts->clk);
2279
2280         kfree(ts);
2281         spi_set_drvdata(spi, NULL);
2282         return 0;
2283 }
2284
2285 static int rm_tch_regulator_init(struct rm_tch_ts *ts)
2286 {
2287         int error;
2288
2289         ts->regulator_3v3 = devm_regulator_get(&g_spi->dev, "avdd");
2290         if (IS_ERR(ts->regulator_3v3)) {
2291                 dev_err(&g_spi->dev, "Raydium - regulator_get failed: %ld\n",
2292                         PTR_ERR(ts->regulator_3v3));
2293                 goto err_null_regulator;
2294         }
2295
2296         ts->regulator_1v8 = devm_regulator_get(&g_spi->dev, "dvdd");
2297         if (IS_ERR(ts->regulator_1v8)) {
2298                 dev_err(&g_spi->dev, "Raydium - regulator_get failed: %ld\n",
2299                         PTR_ERR(ts->regulator_1v8));
2300                 goto err_null_regulator;
2301         }
2302
2303         /* Enable 1v8 first*/
2304         error = regulator_enable(ts->regulator_1v8);
2305         if (error < 0)
2306                 dev_err(&g_spi->dev,
2307                         "Raydium - regulator enable failed: %d\n", error);
2308
2309         usleep_range(5000, 6000);
2310         /* Enable 1v8 first*/
2311         error = regulator_enable(ts->regulator_3v3);
2312         if (error < 0)
2313                 dev_err(&g_spi->dev,
2314                         "Raydium - regulator enable failed: %d\n", error);
2315
2316         ts->nb_1v8.notifier_call = &rm31080_voltage_notifier_1v8;
2317         error = regulator_register_notifier(ts->regulator_1v8, &ts->nb_1v8);
2318         if (error) {
2319                 dev_err(&g_spi->dev,
2320                         "Raydium - regulator notifier request failed: %d\n",
2321                         error);
2322                 goto err_disable_regulator;
2323         }
2324
2325         ts->nb_3v3.notifier_call = &rm31080_voltage_notifier_3v3;
2326         error = regulator_register_notifier(ts->regulator_3v3, &ts->nb_3v3);
2327         if (error) {
2328                 dev_err(&g_spi->dev,
2329                         "Raydium - regulator notifier request failed: %d\n",
2330                         error);
2331                 goto err_unregister_notifier;
2332         }
2333
2334         return 0;
2335
2336 err_unregister_notifier:
2337         regulator_unregister_notifier(ts->regulator_1v8, &ts->nb_1v8);
2338 err_disable_regulator:
2339         regulator_disable(ts->regulator_3v3);
2340         regulator_disable(ts->regulator_1v8);
2341 err_null_regulator:
2342         ts->regulator_3v3 = NULL;
2343         ts->regulator_1v8 = NULL;
2344         return 1;
2345 }
2346
2347 static int rm_tch_spi_probe(struct spi_device *spi)
2348 {
2349         struct rm_tch_ts *ts;
2350         struct rm_spi_ts_platform_data *pdata;
2351         int ret;
2352
2353         g_spi = spi;
2354
2355         rm_tch_init_ts_structure();
2356         rm_tch_init_ts_structure_part();
2357
2358         if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
2359                 dev_err(&spi->dev, "Raydium - SPI CLK %d Hz?\n",
2360                         spi->max_speed_hz);
2361                 ret = -EINVAL;
2362                 goto err_spi_speed;
2363         }
2364         ts = rm_tch_input_init(&spi->dev, spi->irq, &rm_tch_spi_bus_ops);
2365         if (IS_ERR(ts)) {
2366                 dev_err(&spi->dev, "Raydium - Input Device Initialization Fail!\n");
2367                 ret = PTR_ERR(ts);
2368                 goto err_spi_speed;
2369         }
2370
2371         spi_set_drvdata(spi, ts);
2372
2373         if (rm_tch_regulator_init(ts)) {
2374                 dev_err(&spi->dev, "Raydium - regulator Initialization Fail!\n");
2375                 ret = -EINVAL;
2376                 goto err_regulator_init;
2377         }
2378
2379         pdata = g_input_dev->dev.parent->platform_data;
2380         usleep_range(5000, 6000);
2381         if (ts->clk)
2382                 clk_enable(ts->clk);
2383
2384         gpio_set_value(pdata->gpio_reset, 0);
2385         msleep(120);
2386         gpio_set_value(pdata->gpio_reset, 1);
2387         msleep(20);
2388
2389         ret = misc_register(&raydium_ts_miscdev);
2390         if (ret != 0) {
2391                 dev_err(&spi->dev, "Raydium - cannot register miscdev: %d\n",
2392                         ret);
2393                 goto err_misc_reg;
2394         }
2395         ret = sysfs_create_group(&raydium_ts_miscdev.this_device->kobj,
2396                                                 &rm_ts_attr_group);
2397         if (ret != 0) {
2398                 dev_err(&spi->dev, "Raydium - cannot create group: %d\n",
2399                         ret);
2400                 goto err_create_sysfs;
2401         }
2402
2403         ret = rm_tch_queue_init();
2404         if (ret != 0) {
2405                 dev_err(&spi->dev, "Raydium - could not init queue: %d\n",
2406                         ret);
2407                 goto err_queue_init;
2408         }
2409
2410         init_ts_timer();
2411         add_timer(&ts_timer_triggle);
2412
2413         rm_printk("Raydium - Spi Probe Done!!\n");
2414         return 0;
2415
2416 err_queue_init:
2417         sysfs_remove_group(&raydium_ts_miscdev.this_device->kobj,
2418                                                 &rm_ts_attr_group);
2419 err_create_sysfs:
2420         misc_deregister(&raydium_ts_miscdev);
2421 err_misc_reg:
2422         if (ts->regulator_3v3 && ts->regulator_1v8) {
2423                 regulator_unregister_notifier(ts->regulator_3v3, &ts->nb_3v3);
2424                 regulator_unregister_notifier(ts->regulator_1v8, &ts->nb_1v8);
2425                 regulator_disable(ts->regulator_3v3);
2426                 regulator_disable(ts->regulator_1v8);
2427         }
2428         if (ts->clk)
2429                 clk_disable(ts->clk);
2430 err_regulator_init:
2431         spi_set_drvdata(spi, NULL);
2432         input_unregister_device(ts->input);
2433         sysfs_remove_group(&ts->dev->kobj, &rm_ts_attr_group);
2434 #ifdef CONFIG_HAS_EARLYSUSPEND
2435         unregister_early_suspend(&ts->early_suspend);
2436 #endif
2437         mutex_destroy(&ts->access_mutex);
2438         free_irq(ts->irq, ts);
2439         input_free_device(g_input_dev);
2440         kfree(ts);
2441 err_spi_speed:
2442         if (g_stTs.rm_timer_workqueue)
2443                 destroy_workqueue(g_stTs.rm_timer_workqueue);
2444         if (g_stTs.rm_workqueue)
2445                 destroy_workqueue(g_stTs.rm_workqueue);
2446         mutex_destroy(&g_stTs.mutex_scan_mode);
2447         mutex_destroy(&g_stTs.mutex_spi_rw);
2448         return ret;
2449 }
2450
2451 static struct spi_driver rm_tch_spi_driver = {
2452         .driver = {
2453                 .name = "rm_ts_spidev",
2454                 .bus = &spi_bus_type,
2455                 .owner = THIS_MODULE,
2456 #if !defined(CONFIG_HAS_EARLYSUSPEND)
2457 #ifdef CONFIG_PM
2458                 .pm = &rm_tch_pm_ops,
2459 #endif
2460 #endif
2461         },
2462         .probe = rm_tch_spi_probe,
2463         .remove = rm_tch_spi_remove,
2464 };
2465
2466 static int __init rm_tch_spi_init(void)
2467 {
2468         return spi_register_driver(&rm_tch_spi_driver);
2469 }
2470
2471 static void __exit rm_tch_spi_exit(void)
2472 {
2473         spi_unregister_driver(&rm_tch_spi_driver);
2474 }
2475
2476 module_init(rm_tch_spi_init);
2477 module_exit(rm_tch_spi_exit);
2478
2479 MODULE_AUTHOR("Valentine Hsu <valentine.hsu@rad-ic.com>");
2480 MODULE_DESCRIPTION("Raydium touchscreen SPI bus driver");
2481 MODULE_LICENSE("GPL");
2482 MODULE_ALIAS("spi:raydium-t007");