input: touch: raydium: update power sequence
[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  * Copyright (C) 2012 - 2013, NVIDIA Corporation, All Rights Reserved.
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  *
12  */
13 /*=========================================================================*/
14 /*INCLUDED FILES */
15 /*=========================================================================*/
16 #include <linux/module.h>
17 #include <linux/input.h>        /* BUS_SPI */
18 #include <linux/spi/spi.h>
19 #include <linux/device.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
25 #include <linux/gpio.h>
26 #include <linux/sched.h>        /* wake_up_process() */
27 #include <linux/kthread.h>      /* kthread_create(),kthread_run() */
28 #include <linux/uaccess.h>      /* copy_to_user(), */
29 #include <linux/miscdevice.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pm_qos.h>       /* pm qos for CPU boosting */
32 #include <linux/sysfs.h>        /* sysfs for pm qos attributes */
33 #include <linux/clk.h>
34
35 #include <linux/spi/rm31080a_ts.h>
36 #include <linux/spi/rm31080a_ctrl.h>
37 #include <../clock.h>
38
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/touchscreen_raydium.h>
41 /*=========================================================================*/
42 /*DEFINITIONS */
43 /*=========================================================================*/
44 #define ENABLE_RAW_DATA_QUEUE
45 #define ENABLE_WORK_QUEUE
46 #define ENABLE_REPORT_TO_UART
47 #define ENABLE_RM31080_DEEP_SLEEP
48 #define ENABLE_AUTO_SCAN
49 /*#define ENABLE_SPEED_TEST_FUNCTION */
50 /*#define ENABLE_CALC_QUEUE_COUNT */
51 /*#define ENABLE_SPI_BURST_READ_WRITE */
52 /*#define ENABLE_SPI_SETTING */
53 #define ENABLE_SMOOTH_LEVEL
54 /*#define ENABLE_SUPPORT_4_7*/ /* for 4.7 inch display  */
55 #define ENABLE_NEW_INPUT_DEV
56
57 /* undef to disable CPU boost while leaving idle mode */
58 #define NV_ENABLE_CPU_BOOST
59
60 #define MAX_SPI_FREQ_HZ      50000000
61 #define TS_PEN_UP_TIMEOUT    msecs_to_jiffies(50)
62
63 #ifdef ENABLE_RAW_DATA_QUEUE
64 #define QUEUE_COUNT       128
65 #define RAW_DATA_LENGTH  (RM_MAX_MT_COUNT + RM_MAX_CHANNEL_COUNT)
66 #endif
67
68 #define RM_SCAN_MODE_MANUAL          0x00
69 #define RM_SCAN_MODE_PREPARE_AUTO    0x01
70 #define RM_SCAN_MODE_AUTO_SCAN       0x02
71
72 #define RM_NEED_NONE                 0x00
73 #define RM_NEED_TO_SEND_SCAN         0x01
74 #define RM_NEED_TO_READ_RAW_DATA     0x02
75 #define RM_NEED_TO_SEND_SIGNAL       0x04
76
77 #ifdef ENABLE_SMOOTH_LEVEL
78 #define RM_SMOOTH_LEVEL_NORMAL    0
79 #define RM_SMOOTH_LEVEL_MAX       4
80 #endif
81
82 #ifdef NV_ENABLE_CPU_BOOST
83 /* disable CPU boosting if autoscan mode is disabled */
84 #ifndef ENABLE_AUTO_SCAN
85 #undef NV_ENABLE_CPU_BOOST
86 #endif
87 #endif
88
89 #ifdef ENABLE_WORK_QUEUE
90 #include <linux/workqueue.h>
91 #endif
92
93 #define rm_printk(msg...)       do { dev_info(&g_spi->dev, msg); } while (0)
94 #define rmd_printk(msg...)      do {  } while (0)
95 /*=========================================================================*/
96 /*STRUCTURE DECLARATION */
97 /*=========================================================================*/
98 /*TouchScreen Parameters */
99 struct rm31080a_ts_para {
100         unsigned long ulHalPID;
101         bool bInitFinish;
102         bool bCalcFinish;
103         bool bEnableScriber;
104         bool bEnableAutoScan;
105         bool bIsSuspended;
106
107         struct mutex mutex_scan_mode;
108 #ifdef ENABLE_WORK_QUEUE
109         struct workqueue_struct *rm_workqueue;
110         struct work_struct rm_work;
111 #endif
112 #ifdef ENABLE_RAW_DATA_QUEUE
113         u8 u8ScanModeState;
114 #endif
115
116 #ifdef ENABLE_SLOW_SCAN
117         bool bEnableSlowScan;
118         u32 u32SlowScanLevel;
119 #endif
120
121 #ifdef ENABLE_SMOOTH_LEVEL
122         u32 u32SmoothLevel;
123 #endif
124
125         u8 u8SelfTestStatus;
126         u8 u8SelfTestResult;
127         u8 u8Version;
128         u8 u8Repeat;
129 };
130
131 struct rm31080_ts {
132         const struct rm31080_bus_ops *bops;
133         struct device *dev;
134         struct input_dev *input;
135         unsigned int irq;
136         bool disabled;
137         bool suspended;
138         char phys[32];
139         struct mutex access_mutex;
140         struct regulator *regulator_3v3;
141         struct regulator *regulator_1v8;
142         struct notifier_block nb_3v3;
143         struct notifier_block nb_1v8;
144         struct clk *clk;
145 };
146
147 struct rm31080_bus_ops {
148         u16 bustype;
149         int (*read) (struct device *dev, u8 reg);
150         int (*write) (struct device *dev, u8 reg, u16 val);
151 };
152
153 #ifdef ENABLE_RAW_DATA_QUEUE
154 struct rm31080_queue_info {
155         u8(*pQueue)[RAW_DATA_LENGTH];
156         u16 u16Front;
157         u16 u16Rear;
158 };
159 #endif
160
161 /*=========================================================================*/
162 /*GLOBAL VARIABLES DECLARATION */
163 /*=========================================================================*/
164 struct input_dev *g_input_dev;
165 struct spi_device *g_spi;
166 struct rm31080a_ts_para g_stTs;
167
168 #ifdef ENABLE_RAW_DATA_QUEUE
169 struct rm31080_queue_info g_stQ;
170 #endif
171
172 #ifdef ENABLE_SLOW_SCAN
173 struct rm_cmd_slow_scan g_stCmdSlowScan[RM_SLOW_SCAN_LEVEL_COUNT];
174 #endif
175
176 /*========================================================================= */
177 /*FUNCTION DECLARATION */
178 /*========================================================================= */
179 static int rm31080_spi_checking(bool bInfinite);
180 /*=========================================================================
181  * Description:
182  *      Debug function: test speed.
183  * Input:
184  *      N/A
185  * Output:
186  *      1:succeed
187  *      0:failed
188  *========================================================================= */
189 #ifdef ENABLE_SPEED_TEST_FUNCTION
190 void my_calc_time(int iStart)
191 {
192         static unsigned int u32Max = UINT_MAX;
193
194         static long iTimebuffer[1000];
195         static unsigned long long t1, t2;
196         unsigned long nanosec_rem;
197         static int iIndex;
198
199         if (iStart) {
200                 t1 = cpu_clock(u32Max);
201                 return;
202         } else
203                 t2 = cpu_clock(u32Max);
204
205         t2 = t2 - t1;
206
207         nanosec_rem = do_div(t2, 1000000000);
208
209         if (t2) {               /*more than 1 Second */
210                 iTimebuffer[iIndex] = 999999;
211         } else {
212                 iTimebuffer[iIndex] = nanosec_rem / 1000; /*micro second */
213         }
214
215         iIndex++;
216         if (iIndex == 1000) {
217                 for (iIndex = 0; iIndex < 1000; iIndex++) {
218                         rm_printk("   %04d,%06d\n", iIndex,
219                                   (u32) iTimebuffer[iIndex]);
220                 }
221                 iIndex = 0;
222         }
223
224 }
225 #endif /*ENABLE_SPEED_TEST_FUNCTION */
226 /*=========================================================================
227  * Description:
228  *      RM31080 spi interface.
229  * Input:
230  *
231  * Output:
232  *      1:succeed
233  *      0:failed
234  *=========================================================================
235  */
236 static int rm31080_spi_read(u8 u8addr, u8 *rxbuf, size_t len)
237 {
238         static DEFINE_MUTEX(lock);
239
240         int status;
241         struct spi_message message;
242         struct spi_transfer x[2];
243
244         mutex_lock(&lock);
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(&lock);
262
263         if (status)
264                 return false;
265
266         return true;
267 }
268
269 /*=========================================================================
270  * Description:
271  *      RM31080 spi interface.
272  * Input:
273  *
274  * Output:
275  *      1:succeed
276  *      0:failed
277  *=========================================================================
278  */
279 static int rm31080_spi_write(u8 *txbuf, size_t len)
280 {
281         static DEFINE_MUTEX(lock);
282         int status;
283         /*It returns zero on succcess,else a negative error code. */
284         mutex_lock(&lock);
285
286         status = spi_write(g_spi, txbuf, len);
287
288         mutex_unlock(&lock);
289
290         if (status)
291                 return false;
292
293         return true;
294 }
295
296 /*=========================================================================
297  * Description:
298  *      RM31080 spi interface.
299  * Input:
300  *
301  * Output:
302  *      1:succeed
303  *      0:failed
304  *=========================================================================
305  */
306 int rm31080_spi_byte_read(unsigned char u8Addr, unsigned char *pu8Value)
307 {
308         return rm31080_spi_read(u8Addr, pu8Value, 1);
309 }
310
311 /*=========================================================================
312  * Description:
313  *      RM31080 spi interface.
314  * Input:
315  *
316  * Output:
317  *      1:succeed
318  *      0:failed
319  *=========================================================================
320  */
321 int rm31080_spi_byte_write(unsigned char u8Addr, unsigned char u8Value)
322 {
323         u8 buf[2];
324         buf[0] = u8Addr;
325         buf[1] = u8Value;
326         return rm31080_spi_write(buf, 2);
327 }
328
329 /*=========================================================================
330  * Description:
331  *      RM31080 spi interface.
332  * Input:
333  *
334  * Output:
335  *      1:succeed
336  *      0:failed
337  *=========================================================================
338  */
339 #ifdef ENABLE_SPI_BURST_READ_WRITE
340 static int rm31080_spi_burst_read(unsigned char u8Addr,
341                                 unsigned char *pu8Value,
342                                 u32 u32len)
343 {
344         int ret;
345         u8 *pMyBuf;
346
347         pMyBuf = kmalloc(u32len, GFP_KERNEL);
348         if (pMyBuf == NULL)
349                 return false;
350
351         ret = rm31080_spi_read(u8Addr, pMyBuf, u32len);
352
353         if (ret)
354                 memcpy(pu8Value, pMyBuf, u32len);
355
356         kfree(pMyBuf);
357
358         return ret;
359 }
360 #endif
361 /*=========================================================================
362  * Description:
363  *      RM31080 spi interface.
364  * Input:
365  *
366  * Output:
367  *      1:succeed
368  *      0:failed
369  *=========================================================================
370  */
371 int rm31080_spi_burst_write(unsigned char *pBuf, unsigned int u32Len)
372 {
373         u8 *pMyBuf;
374         int ret;
375
376         pMyBuf = kmalloc(u32Len, GFP_KERNEL);
377         if (pMyBuf == NULL)
378                 return false;
379
380         memcpy(pMyBuf, pBuf, u32Len);
381         ret = rm31080_spi_write(pMyBuf, u32Len);
382         kfree(pMyBuf);
383         return ret;
384 }
385
386 /*=========================================================================*/
387 #ifdef ENABLE_AUTO_SCAN
388 void raydium_change_scan_mode(u8 u8TouchCount)
389 {
390         static u32 u32NoTouchCount;
391         u16 u16NTCountThd;
392
393         u16NTCountThd = (u16) g_stCtrl.bTime2Idle * 10;
394
395         if (u8TouchCount) {
396                 u32NoTouchCount = 0;
397                 return;
398         }
399         if (u32NoTouchCount < u16NTCountThd) {
400                 u32NoTouchCount++;
401         } else if (g_stTs.u8ScanModeState == RM_SCAN_MODE_MANUAL) {
402                 if (g_stTs.bEnableAutoScan)
403                         g_stTs.u8ScanModeState = RM_SCAN_MODE_PREPARE_AUTO;
404                 u32NoTouchCount = 0;
405         }
406 }
407 #endif /*ENABLE_AUTO_SCAN */
408 /*=========================================================================
409   report touch data for scriber
410
411   =========================================================================*/
412 #ifdef ENABLE_REPORT_TO_UART
413 void raydium_report_to_uart_printf(unsigned char *ucData,
414                                 unsigned char ucCount)
415 {
416         unsigned char i;
417         for (i = 0; i < ucCount; i++)
418                 rm_printk("%02X", ucData[i]);
419
420         rm_printk("\n");
421 }
422
423 void raydium_report_to_uart(void *p)
424 {
425         /*1=Tag,1=Touch count,4=(xH xL ,yH yL) ,12=max point,1=Check sum */
426         unsigned char ucData[1 + 1 + (4 * 12) + 1];
427         struct rm_touch_event *spTP;
428         unsigned short usX, usY;
429         int i, j;
430
431         if (g_stTs.bEnableScriber == 0)
432                 return;
433
434         spTP = (struct rm_touch_event *)p;
435
436         ucData[0] = 0x8E;
437         ucData[1] = spTP->ucTouchCount;
438         j = 2;
439         for (i = 0; i < spTP->ucTouchCount; i++) {
440                 usX = spTP->usX[i] + 1;
441                 usY = spTP->usY[i] + 1;
442                 ucData[j++] = ((usX >> 8) & 0xFF) | (spTP->ucID[i] << 4);
443                 ucData[j++] = ((usX) & 0xFF);
444                 ucData[j++] = ((usY >> 8) & 0xFF);
445                 ucData[j++] = ((usY) & 0xFF);
446         }
447
448         /*check sum */
449         ucData[j] = 0;
450         for (i = 0; i < j; i++)
451                 ucData[j] += ucData[i];
452
453         ucData[j] = 0x100 - ucData[j];
454         j++;
455
456         /*print */
457         raydium_report_to_uart_printf(ucData, j);
458         if (spTP->ucTouchCount == 0) {
459                 raydium_report_to_uart_printf(ucData, j);
460                 raydium_report_to_uart_printf(ucData, j);
461         }
462 }
463 #endif                          /*ENABLE_REPORT_TO_UART */
464 #ifdef ENABLE_SUPPORT_4_7       /*for 4.7 inch display */
465 #define _RM_SCALE_LEFT          (40 + 0)
466 #define _RM_SCALE_RIGHT         (30 + 0)
467 #define _RM_SCALE_TOP           (16 + 0)
468 #define _RM_SCALE_BOTTOM        (30 + 0)
469 void raydium_scale_touch_point(struct rm_touch_event *spTP,
470                                 int iMaxX, int iMaxY,
471                                 int left, int right,
472                                 int top, int bottom)
473 {
474         int i;
475         int iNewX, iNewY;
476         iNewX = iMaxX - _RM_SCALE_LEFT - _RM_SCALE_RIGHT;
477         iNewY = iMaxY - _RM_SCALE_TOP - _RM_SCALE_BOTTOM;
478
479         for (i = 0; i < spTP->ucTouchCount; i++) {
480                 /* cut out */
481                 if (spTP->usX[i] < _RM_SCALE_LEFT)
482                         spTP->usX[i] = _RM_SCALE_LEFT;
483                 else if (spTP->usX[i] > (iMaxX - _RM_SCALE_RIGHT))
484                         spTP->usX[i] = (iMaxX - _RM_SCALE_RIGHT);
485                 if (spTP->usY[i] < _RM_SCALE_TOP)
486                         spTP->usY[i] = _RM_SCALE_TOP;
487                 else if (spTP->usY[i] > (iMaxY - _RM_SCALE_BOTTOM))
488                         spTP->usY[i] = (iMaxY - _RM_SCALE_BOTTOM);
489                 /* shift */
490                 spTP->usX[i] -= _RM_SCALE_LEFT;
491                 spTP->usY[i] -= _RM_SCALE_TOP;
492                 /* scale */
493                 if (iNewX)
494                         spTP->usX[i] = (int)spTP->usX[i] * iMaxX / iNewX;
495                 if (iNewY)
496                         spTP->usY[i] = (int)spTP->usY[i] * iMaxY / iNewY;
497         }
498 }
499 #endif
500
501 void raydium_report_pointer(void *p)
502 {
503         static unsigned char ucLastTouchCount;
504         int i;
505         int iCount;
506         int iMaxX, iMaxY;
507         struct rm_touch_event *spTP;
508 #ifdef ENABLE_SUPPORT_4_7
509         struct rm_touch_event stTP;
510         unsigned long missing;
511 #endif
512         spTP = (struct rm_touch_event *)p;
513
514
515         if ((g_stCtrl.u16ResolutionX != 0) &&
516                 (g_stCtrl.u16ResolutionY != 0)) {
517                 iMaxX = g_stCtrl.u16ResolutionX;
518                 iMaxY = g_stCtrl.u16ResolutionY;
519         } else {
520                 iMaxX = RM_INPUT_RESOLUTION_X;
521                 iMaxY = RM_INPUT_RESOLUTION_Y;
522         }
523
524 #ifdef ENABLE_SUPPORT_4_7
525         missing = copy_from_user(&stTP, p, sizeof(stTP));
526         if (missing == 0) {
527                 raydium_scale_touch_point(&stTP, iMaxX, iMaxY,
528                                         _RM_SCALE_LEFT,
529                                         _RM_SCALE_RIGHT,
530                                         _RM_SCALE_TOP,
531                                         _RM_SCALE_BOTTOM);
532                 spTP = &stTP;
533         }
534 #endif
535
536         iCount = max(ucLastTouchCount, spTP->ucTouchCount);
537         if (iCount) {
538                 for (i = 0; i < iCount; i++) {
539                         if (i == 10)
540                                 break;
541
542                         if (i < spTP->ucTouchCount) {
543                                 input_report_abs(g_input_dev,
544                                                  ABS_MT_TRACKING_ID,
545                                                  spTP->ucID[i]);
546                                 input_report_abs(g_input_dev,
547                                                  ABS_MT_TOUCH_MAJOR,
548                                                  spTP->usZ[i]);
549                                 input_report_abs(g_input_dev,
550                                                  ABS_MT_WIDTH_MAJOR,
551                                                  spTP->usZ[i]);
552                                 input_report_abs(g_input_dev,
553                                                  ABS_MT_PRESSURE,
554                                                  spTP->usZ[i]);
555
556                                 if (spTP->usX[i] >= (iMaxX - 1))
557                                         input_report_abs(g_input_dev,
558                                                          ABS_MT_POSITION_X,
559                                                          (iMaxX - 1));
560                                 else
561                                         input_report_abs(g_input_dev,
562                                                          ABS_MT_POSITION_X,
563                                                          spTP->usX[i]);
564
565                                 if (spTP->usY[i] >= (iMaxY - 1))
566                                         input_report_abs(g_input_dev,
567                                                          ABS_MT_POSITION_Y,
568                                                          (iMaxY - 1));
569                                 else
570                                         input_report_abs(g_input_dev,
571                                                          ABS_MT_POSITION_Y,
572                                                          spTP->usY[i]);
573                         }
574                         input_mt_sync(g_input_dev);
575                 }
576                 ucLastTouchCount = spTP->ucTouchCount;
577                 input_report_key(g_input_dev, BTN_TOUCH,
578                                  spTP->ucTouchCount > 0);
579                 input_sync(g_input_dev);
580 #ifdef ENABLE_REPORT_TO_UART
581                 raydium_report_to_uart(p);
582 #endif
583
584         }
585 #ifdef ENABLE_AUTO_SCAN
586         if (g_stCtrl.bfPowerMode)
587                 raydium_change_scan_mode(spTP->ucTouchCount);
588 #endif
589 }
590
591 /*=========================================================================
592  * Description:
593  *      RM31080 control functions.
594  * Input:
595  *      N/A
596  * Output:
597  *      1:succeed
598  *      0:failed
599  *=========================================================================
600  */
601 #ifdef ENABLE_RAW_DATA_QUEUE
602 int rm31080_ctrl_read_raw_data(unsigned char *p)
603 {
604 #define T007B1_ST_DATA_ADDR                             0x1CA0
605 #define T007C1_ST_DATA_ADDR                             0x0514
606 #define T007B1_ST_NO_SWAP                                       0
607
608         int ret;
609
610         char buf[3];
611
612         if (g_stCtrl.bICVersion != T007A6) {
613                 ret = rm31080_spi_byte_write(RM31080B1_REG_BANK0_00H, 0x00);
614                 if (ret)
615                         ret = rm31080_spi_byte_write(RM31080B1_REG_BANK0_01H,
616                                                 0x00);
617
618                 if (ret)
619                         ret = rm31080_spi_read(RM31080B1_REG_BANK0_03H|0x80, p,
620                                                 g_stCtrl.u16DataLength);
621         } else {
622                 ret = rm31080_spi_byte_write(RM31080_REG_01, 0x10);
623                 if (ret)
624                         ret = rm31080_spi_byte_write(RM31080_REG_02, 0x00);
625
626                 if (ret)
627                         ret = rm31080_spi_read(RM31080_REG_80, p,
628                                                 g_stCtrl.u16DataLength);
629         }
630
631         if (g_stCtrl.bSTScan) {
632                 if (g_stCtrl.bICVersion == T007A6)
633                         return ret;
634
635                 if (!ret)
636                         return ret;
637
638                 buf[0] = RM31080B1_REG_BANK0_00H;
639                 if (g_stCtrl.bICVersion == T007_VERSION_B) {
640                         buf[1] = (T007B1_ST_DATA_ADDR >> 8) & 0x1F;
641                         buf[2] = T007B1_ST_DATA_ADDR & 0xFF;
642                 } else if (g_stCtrl.bICVersion == T007_VERSION_C) {
643                         buf[1] = (T007C1_ST_DATA_ADDR >> 8) & 0x1F;
644                         buf[2] = T007C1_ST_DATA_ADDR & 0xFF;
645                 }
646                 ret = rm31080_spi_burst_write(buf, sizeof(buf));
647
648                 if (ret)
649                         ret = rm31080_spi_read(RM31080B1_REG_BANK0_03H |
650                                 0x80, p + RM_MAX_MT_COUNT,
651                                 RM_MAX_CHANNEL_COUNT);
652         }
653         return ret;
654 }
655
656 #ifdef ENABLE_AUTO_SCAN
657 void rm_set_idle(u8 OnOff)
658 {
659         u8 reg_46h;
660         switch (OnOff) {
661         case 1:
662                 rm31080_spi_read(0x46 | 0x80, &reg_46h, 1);
663                 rm31080_spi_byte_write(0x46, reg_46h | 0xF0);
664                 break;
665         default:
666                 rm31080_spi_read(0x46 | 0x80, &reg_46h, 1);
667                 rm31080_spi_byte_write(0x46, reg_46h & 0x0F);
668                 break;
669         }
670 }
671
672 void rm_set_auto(u8 OnOff)
673 {
674         u8 reg_09h = 0;
675         u8 reg_0Ah = 0;
676         switch (OnOff) {
677         case 1:
678                 rm31080_spi_read(0x09 | 0x80, &reg_09h, 1);
679                 rm31080_spi_byte_write(0x09, reg_09h | 0x50);
680                 rm31080_spi_read(0x0A | 0x80, &reg_0Ah, 1);
681                 rm31080_spi_byte_write(0x0A, reg_0Ah & 0x0F);
682                 break;
683         default:
684                 rm31080_spi_read(0x09 | 0x80, &reg_09h, 1);
685                 rm31080_spi_byte_write(0x09, reg_09h & 0x0F);
686                 rm31080_spi_read(0x0A | 0x80, &reg_0Ah, 1);
687                 rm31080_spi_byte_write(0x0A, reg_0Ah | 0x40);
688                 break;
689         }
690 }
691
692 void rm31080_ctrl_enter_auto_mode(void)
693 {
694         if (g_stCtrl.bICVersion == T007A6)
695                 rm31080_ts_send_signal(g_stTs.ulHalPID,
696                                        RM_SIGNAL_ENTER_AUTO_SCAN);
697
698         /*Enable auto scan */
699         if (g_stCtrl.bfIdleMessage)
700                 rm_printk("Enter Auto Scan Mode\n");
701         /*Set idle*/
702         rm_set_idle(1);
703         if (g_stCtrl.bSTScan)
704                 rm_set_auto(1);
705         else
706                 rm31080_spi_byte_write(RM31080_REG_09, 0x10 | 0x40);
707 }
708
709 void rm31080_ctrl_leave_auto_mode(void)
710 {
711         /*Disable auto scan */
712         g_stCtrl.bfIdleModeCheck = 1;
713
714         if (g_stCtrl.bfIdleMessage)
715                 rm_printk("Leave Auto Scan Mode\n");
716         /*leave idle*/
717         rm_set_idle(0);
718         if (g_stCtrl.bSTScan)
719                 rm_set_auto(0);
720         else
721                 rm31080_spi_byte_write(RM31080_REG_09, 0x00);
722
723         if (g_stCtrl.bICVersion == T007A6)
724                 rm31080_ts_send_signal(g_stTs.ulHalPID,
725                         RM_SIGNAL_LEAVE_AUTO_SCAN);
726 }
727
728 void rm31080_ctrl_pause_auto_mode(void)
729 {
730         u8 u8reg11;
731         rm31080_spi_byte_write(RM31080_REG_09, 0x40);   /*disable auto scan */
732         rm31080_spi_byte_read(RM31080_REG_11, &u8reg11);
733         u8reg11 &= ~0x01;
734         /*set scan start=0 */
735         rm31080_spi_byte_write(RM31080_REG_11, u8reg11);
736 }
737 #endif /*ENABLE_AUTO_SCAN */
738
739 static u32 rm31080_ctrl_configure(void)
740 {
741         u32 u32Flag;
742
743         switch (g_stTs.u8ScanModeState) {
744         case RM_SCAN_MODE_MANUAL:
745                 u32Flag =
746                         RM_NEED_TO_SEND_SCAN | RM_NEED_TO_READ_RAW_DATA |
747                         RM_NEED_TO_SEND_SIGNAL;
748
749                 break;
750 #ifdef ENABLE_AUTO_SCAN
751         case RM_SCAN_MODE_PREPARE_AUTO:
752                 rm31080_ctrl_enter_auto_mode();
753                 g_stTs.u8ScanModeState = RM_SCAN_MODE_AUTO_SCAN;
754                 u32Flag = RM_NEED_NONE;
755                 break;
756         case RM_SCAN_MODE_AUTO_SCAN:
757                 rm31080_ctrl_leave_auto_mode();
758                 rm31080_ctrl_scan_start();
759                 g_stTs.u8ScanModeState = RM_SCAN_MODE_MANUAL;
760
761                 if (g_stCtrl.bICVersion != T007A6) {
762                         u32Flag =
763                                 RM_NEED_TO_SEND_SCAN |
764                                 RM_NEED_TO_READ_RAW_DATA |
765                                 RM_NEED_TO_SEND_SIGNAL;
766                 } else
767                         u32Flag = RM_NEED_TO_SEND_SCAN;
768                 break;
769 #endif /*ENABLE_AUTO_SCAN */
770         default:
771                 u32Flag = RM_NEED_NONE;
772                 break;
773         }
774
775         return u32Flag;
776 }
777
778 #endif  /*ENABLE_RAW_DATA_QUEUE */
779 /*========================================================================= */
780 #ifdef ENABLE_RM31080_DEEP_SLEEP
781
782 static int rm31080_ctrl_suspend(struct rm31080_ts *ts)
783 {
784         /* handle touch suspend */
785         int error;
786         /*Flow designed by Roger 20110930 */
787         /*rm31080_ts_send_signal(g_stTs.ulHalPID,RM_SIGNAL_SUSPEND); */
788         g_stTs.bInitFinish = 0;
789         mutex_lock(&g_stTs.mutex_scan_mode);
790         usleep_range(8000, 9000);/*msleep(8); */
791         rm31080_ctrl_clear_int();
792         /*disable auto scan */
793
794         if (g_stCtrl.bICVersion == T007A6) {
795                 rm31080_spi_byte_write(RM31080_REG_09, 0x00);
796                 rm31080_spi_byte_write(RM31080_REG_10, 0x14);
797                 rm31080_ctrl_scan_start();
798                 rm31080_ctrl_scan_start();
799                 usleep_range(15000, 20000);     /*msleep(15); */
800         }
801
802         rm31080_spi_byte_write(RM31080_REG_11, 0x06);
803
804         msleep(100);
805         /* 1.disable (3.3v) */
806         if (ts->regulator_3v3) {
807                 error = regulator_disable(ts->regulator_3v3);
808                 if (error < 0)
809                         dev_err(&g_spi->dev,
810                                 "raydium regulator 3.3V disable failed: %d\n",
811                                 error);
812         }
813
814         mutex_unlock(&g_stTs.mutex_scan_mode);
815         return 1;
816 }
817 #endif
818
819 /*========================================================================= */
820 static void rm31080_enter_manual_mode(void)
821 {
822         flush_workqueue(g_stTs.rm_workqueue);
823
824         if (g_stTs.u8ScanModeState == RM_SCAN_MODE_MANUAL)
825                 return;
826
827         if (g_stTs.u8ScanModeState == RM_SCAN_MODE_PREPARE_AUTO) {
828                 g_stTs.u8ScanModeState = RM_SCAN_MODE_MANUAL;
829                 return;
830         }
831
832         if (g_stTs.u8ScanModeState == RM_SCAN_MODE_AUTO_SCAN) {
833                 rm31080_ctrl_leave_auto_mode();
834                 g_stTs.u8ScanModeState = RM_SCAN_MODE_MANUAL;
835                 usleep_range(10000, 12000);     /*msleep(10); */
836         }
837 }
838
839 /*=============================================================================
840  * Description:
841  *      Copy Config(Parameters) to HAL's Buffer
842  * Input:
843  *      p: HAL's buffer
844  *      u32Len : buffer size
845  * Output:
846  *      1: succeed
847  *      0: failed
848  *=============================================================================
849  */
850 static long rm31080_get_config(u8 *p, u32 u32Len)
851 {
852         u32 u32Ret;
853         struct rm_spi_ts_platform_data *pdata;
854
855         u8 var;
856         if (rm31080_spi_byte_read(RM31080_REG_7E, &var))
857                 g_stCtrl.bICVersion = var & 0xF0;
858         else
859                 g_stCtrl.bICVersion = T007A6;
860
861         pdata = g_input_dev->dev.parent->platform_data;
862
863         switch (g_stCtrl.bICVersion) {
864         case T007_VERSION_B:
865                 u32Ret = copy_to_user(p, pdata->config +
866                                 (PARAMETER_AMOUNT*VERSION_B_PARAMETER_OFFSET),
867                                 u32Len);
868                 break;
869         case T007_VERSION_C:
870                 u32Ret = copy_to_user(p, pdata->config +
871                                 (PARAMETER_AMOUNT*VERSION_C_PARAMETER_OFFSET),
872                                 u32Len);
873                 break;
874         default:
875                 u32Ret = copy_to_user(p, pdata->config, u32Len);
876         }
877
878         if (u32Ret != 0)
879                 return 0;
880         return 1;
881 }
882
883 static u32 rm31080_get_platform_id(u8 *p)
884 {
885         u32 u32Ret;
886         struct rm_spi_ts_platform_data *pdata;
887         pdata = g_input_dev->dev.parent->platform_data;
888         u32Ret = copy_to_user(p, &pdata->platform_id,
889                                 sizeof(pdata->platform_id));
890         if (u32Ret != 0)
891                 return 0;
892         return 1;
893 }
894
895 /*=========================================================================*/
896 int rm31080_ts_send_signal(int pid, int iInfo)
897 {
898         struct siginfo info;
899         struct task_struct *t;
900         int ret = 0;
901
902         static DEFINE_MUTEX(lock);
903
904         if (!pid)
905                 return ret;
906
907         mutex_lock(&lock);
908         /* send the signal */
909         memset(&info, 0, sizeof(struct siginfo));
910         info.si_signo = RM_TS_SIGNAL;
911         info.si_code = SI_QUEUE;
912         /* this is bit of a trickery: SI_QUEUE is normally
913          * used by sigqueue from user space, and kernel space
914          * should use SI_KERNEL. But if SI_KERNEL is used the real_time data
915          * is not delivered to the user space signal handler function.
916          */
917         info.si_int = iInfo;/*real time signals may have 32 bits of data. */
918
919         rcu_read_lock();
920         t = find_task_by_vpid(pid);
921         rcu_read_unlock();
922         if (t == NULL) {
923                 rmd_printk("no such pid\n");
924                 ret = -ENODEV;
925         } else
926                 ret = send_sig_info(RM_TS_SIGNAL, &info, t);
927
928         if (ret < 0)
929                 rmd_printk("error sending signal\n");
930
931         mutex_unlock(&lock);
932         return ret;
933 }
934
935 /*=============================================================================
936  * Description:
937  *      Queuing functions.
938  * Input:
939  *      N/A
940  * Output:
941  *      0:succeed
942  *      others:error code
943  *=============================================================================
944  */
945 #ifdef ENABLE_RAW_DATA_QUEUE
946 static void rm31080_queue_reset(void)
947 {
948         g_stQ.u16Rear = 0;
949         g_stQ.u16Front = 0;
950 }
951
952 static int rm31080_queue_init(void)
953 {
954         rm31080_queue_reset();
955         g_stQ.pQueue = kmalloc(QUEUE_COUNT * RAW_DATA_LENGTH, GFP_KERNEL);
956         if (g_stQ.pQueue == NULL)
957                 return -ENOMEM;
958         return 0;
959 }
960
961 static void rm31080_queue_free(void)
962 {
963         if (!g_stQ.pQueue)
964                 return;
965         kfree(g_stQ.pQueue);
966         g_stQ.pQueue = NULL;
967 }
968
969 #ifdef ENABLE_CALC_QUEUE_COUNT
970 static int rm31080_queue_get_current_count(void)
971 {
972         if (g_stQ.u16Rear >= g_stQ.u16Front)
973                 return g_stQ.u16Rear - g_stQ.u16Front;
974
975         return (QUEUE_COUNT - g_stQ.u16Front) + g_stQ.u16Rear;
976 }
977 #endif
978
979 /*=============================================================================
980  * Description:
981  *  About full/empty buffer distinction,
982  *  There are a number of solutions like:
983  *  1.Always keep one slot open.
984  *  2.Use a fill count to distinguish the two cases.
985  *  3.Use read and write counts to get the fill count from.
986  *  4.Use absolute indices.
987  *  we chose "keep one slot open" to make it simple and robust
988  *  and also avoid race condition.
989  * Input:
990  *      N/A
991  * Output:
992  *      1:empty
993  *      0:not empty
994  *=============================================================================
995  */
996 static int rm31080_queue_is_empty(void)
997 {
998         if (g_stQ.u16Rear == g_stQ.u16Front)
999                 return 1;
1000         return 0;
1001 }
1002
1003 /*=============================================================================
1004  * Description:
1005  *  check queue full.
1006  * Input:
1007  *      N/A
1008  * Output:
1009  *      1:full
1010  *      0:not full
1011  *=============================================================================
1012  */
1013 static int rm31080_queue_is_full(void)
1014 {
1015         u16 u16Front = g_stQ.u16Front;
1016         if (g_stQ.u16Rear + 1 == u16Front)
1017                 return 1;
1018
1019         if ((g_stQ.u16Rear == (QUEUE_COUNT - 1)) && (u16Front == 0))
1020                 return 1;
1021
1022         return 0;
1023 }
1024
1025 static void *rm31080_enqueue_start(void)
1026 {
1027         if (!g_stQ.pQueue)      /*error handling for no memory */
1028                 return NULL;
1029
1030         if (!rm31080_queue_is_full())
1031                 return &g_stQ.pQueue[g_stQ.u16Rear];
1032
1033         /* rm_printk("rm31080:touch service is busy,try again.\n"); */
1034         return NULL;
1035 }
1036
1037 static void rm31080_enqueue_finish(void)
1038 {
1039         if (g_stQ.u16Rear == (QUEUE_COUNT - 1))
1040                 g_stQ.u16Rear = 0;
1041         else
1042                 g_stQ.u16Rear++;
1043 }
1044
1045 static void *rm31080_dequeue_start(void)
1046 {
1047         if (!rm31080_queue_is_empty())
1048                 return &g_stQ.pQueue[g_stQ.u16Front];
1049
1050         return NULL;
1051 }
1052
1053 static void rm31080_dequeue_finish(void)
1054 {
1055         if (g_stQ.u16Front == (QUEUE_COUNT - 1))
1056                 g_stQ.u16Front = 0;
1057         else
1058                 g_stQ.u16Front++;
1059 }
1060
1061 static long rm31080_queue_read_raw_data(u8 *p, u32 u32Len)
1062 {
1063         u8 *pQueue;
1064         u32 u32Ret;
1065         pQueue = rm31080_dequeue_start();
1066         if (!pQueue)
1067                 return 0;
1068
1069         u32Ret = copy_to_user(p, pQueue, u32Len);
1070         if (u32Ret != 0)
1071                 return 0;
1072
1073         rm31080_dequeue_finish();
1074         return 1;
1075 }
1076 #endif /*ENABLE_RAW_DATA_QUEUE */
1077 /*=====================================================================*/
1078 #ifdef ENABLE_WORK_QUEUE
1079 static void rm_work_handler(struct work_struct *work)
1080 {
1081         void *pKernelBuffer;
1082         u32 u32Flag;
1083         int iRet;
1084
1085         if (g_stTs.bIsSuspended)
1086                 return;
1087
1088         mutex_lock(&g_stTs.mutex_scan_mode);
1089
1090         iRet = rm31080_ctrl_clear_int();
1091
1092         u32Flag = rm31080_ctrl_configure();
1093
1094         if (u32Flag & RM_NEED_TO_SEND_SCAN)
1095                 rm31080_ctrl_scan_start();
1096
1097         if (u32Flag & RM_NEED_TO_READ_RAW_DATA) {
1098                 pKernelBuffer = rm31080_enqueue_start();
1099                 if (pKernelBuffer) {
1100                         iRet = rm31080_ctrl_read_raw_data((u8 *) pKernelBuffer);
1101
1102                         if (iRet)
1103                                 rm31080_enqueue_finish();
1104                 }
1105         }
1106         mutex_unlock(&g_stTs.mutex_scan_mode);
1107
1108         if (u32Flag & RM_NEED_TO_SEND_SIGNAL) {
1109                 if (g_stTs.bCalcFinish) {
1110                         g_stTs.bCalcFinish = 0;
1111                         rm31080_ts_send_signal(g_stTs.ulHalPID, RM_SIGNAL_INTR);
1112                 }
1113         }
1114 }
1115 #endif /*ENABLE_WORK_QUEUE */
1116 /*========================================================================= */
1117 static void __rm31080_enable(struct rm31080_ts *ts)
1118 {
1119         enable_irq(ts->irq);
1120 }
1121
1122 static void __rm31080_disable(struct rm31080_ts *ts)
1123 {
1124         disable_irq(ts->irq);
1125 }
1126
1127 void rm31080_send_command(struct rm_cmd_list *rm_cmd_list)
1128 {
1129         unsigned char i;
1130         unsigned char addr;
1131         unsigned char value;
1132
1133         for (i = 0; i < rm_cmd_list->count; i++) {
1134                 addr = rm_cmd_list->cmd[i].addr;
1135                 value = rm_cmd_list->cmd[i].value;
1136                 rmd_printk("No:[%d]addr:0x%x,value:0x%x\n", i, addr, value);
1137                 rm31080_spi_byte_write(addr, value);
1138         }
1139 }
1140
1141 #ifdef ENABLE_SLOW_SCAN
1142 void rm31080_set_command(struct rm_cmd_list *cmd_list, char *buf, int count)
1143 {
1144         int i;
1145         if (count > RM_SLOW_SCAN_CMD_COUNT)
1146                 count = RM_SLOW_SCAN_CMD_COUNT;
1147         cmd_list->count = count;
1148         for (i = 0; i < count; i++) {
1149                 cmd_list->cmd[i].addr = buf[(i << 1) + 0];
1150                 cmd_list->cmd[i].value = buf[(i << 1) + 1];
1151         }
1152 }
1153
1154 static void rm31080_set_slowscan_para(u8 *p, int index)
1155 {
1156         ssize_t missing;
1157         u8 buf[256];
1158         u8 size = p[0];
1159         missing = copy_from_user(buf, p, size);
1160         if (missing != 0)
1161                 return;
1162         if (index > RM_SLOW_SCAN_LEVEL_MAX)
1163                 return;
1164         rm31080_set_command((struct rm_cmd_list *)
1165                 &g_stCmdSlowScan[index],
1166                 &buf[1],
1167                 (size - 1) >> 1);
1168 }
1169
1170 void rm31080_slow_scan_init(void)
1171 {
1172         int i;
1173         for (i = 0; i < RM_SLOW_SCAN_LEVEL_COUNT; i++)
1174                 g_stCmdSlowScan[i].count = 0;
1175 }
1176
1177 /*=============================================================================
1178  * Description:
1179  *      Context dependent touch system.
1180  *      Change scan speed for slowscan function.
1181  *      Change scan speed flow: (by CY,20120305)
1182  *      1.Disable auto scan ([0x09]bit4=0,[0x09]bit6=1)
1183  *      2.Clear Scan start bit ([0x11]bit0=0)
1184  *      3.Read Scan start bit until it equals 0
1185  *      4.Set LACTIVE and YACTIVE configuration
1186  *      5.Enable autoscan ([0x09]bit4=1,[0x09]bit6=1)
1187  *      6.Sleep 1 minisecond.
1188  *      7.Set Scan start bit ([0x11]bit0=1)
1189  * Input:
1190  *      N/A
1191  * Output:
1192  *      N/A
1193  *=============================================================================
1194  */
1195 static void rm31080_ctrl_slowscan(u32 level)
1196 {
1197         if (g_stTs.u8ScanModeState == RM_SCAN_MODE_AUTO_SCAN)
1198                 rm31080_ctrl_pause_auto_mode();
1199
1200         rm31080_ctrl_wait_for_scan_finish();
1201
1202         rmd_printk("P005:change level:%d\n", level);
1203
1204         if (level == RM_SLOW_SCAN_LEVEL_NORMAL)
1205                 level = RM_SLOW_SCAN_LEVEL_20;
1206
1207         if (level > RM_SLOW_SCAN_LEVEL_100)
1208                 level = RM_SLOW_SCAN_LEVEL_MAX;
1209
1210         rm31080_send_command((struct rm_cmd_list *)
1211                                 &g_stCmdSlowScan[level]);
1212
1213         if (g_stTs.u8ScanModeState == RM_SCAN_MODE_AUTO_SCAN) {
1214                 rm31080_ctrl_enter_auto_mode();
1215                 usleep_range(1000, 2000);       /*msleep(1); */
1216                 rm31080_ctrl_scan_start();
1217         }
1218 }
1219
1220 static u32 rm31080_slowscan_round(u32 val)
1221 {
1222         u32 i;
1223         for (i = 0; i < RM_SLOW_SCAN_LEVEL_COUNT; i++) {
1224                 if ((i * RM_SLOW_SCAN_INTERVAL) >= val)
1225                         break;
1226         }
1227         return i;
1228
1229 }
1230
1231 static ssize_t rm31080_slowscan_handler(const char *buf, size_t count)
1232 {
1233         unsigned long val;
1234         ssize_t error;
1235         ssize_t ret;
1236
1237         if (count < 2)
1238                 return count;
1239
1240         ret = (ssize_t) count;
1241         mutex_lock(&g_stTs.mutex_scan_mode);
1242
1243         if (count == 2) {
1244                 if (buf[0] == '0') {
1245                         g_stTs.bEnableSlowScan = false;
1246                         rm31080_ctrl_slowscan(RM_SLOW_SCAN_LEVEL_MAX);
1247                 } else if (buf[0] == '1') {
1248                         g_stTs.bEnableSlowScan = true;
1249                         rm31080_ctrl_slowscan(RM_SLOW_SCAN_LEVEL_60);
1250                         g_stTs.u32SlowScanLevel = RM_SLOW_SCAN_LEVEL_60;
1251                 }
1252         } else if ((buf[0] == '2') && (buf[1] == ' ')) {
1253                 error = kstrtoul(&buf[2], 10, &val);
1254                 if (error) {
1255                         ret = error;
1256                 } else {
1257                         g_stTs.bEnableSlowScan = true;
1258                         g_stTs.u32SlowScanLevel = rm31080_slowscan_round(val);
1259                         rm31080_ctrl_slowscan(g_stTs.u32SlowScanLevel);
1260                 }
1261         }
1262
1263         mutex_unlock(&g_stTs.mutex_scan_mode);
1264         return ret;
1265 }
1266 #endif
1267
1268 static ssize_t rm31080_slowscan_show(struct device *dev,
1269                                      struct device_attribute *attr, char *buf)
1270 {
1271 #ifdef ENABLE_SLOW_SCAN
1272         return sprintf(buf, "Slow Scan:%s\nScan Rate:%dHz\n",
1273                         g_stTs.bEnableSlowScan ?
1274                         "Enabled" : "Disabled",
1275                         g_stTs.u32SlowScanLevel * RM_SLOW_SCAN_INTERVAL);
1276
1277 #else
1278         return sprintf(buf, "0\n");
1279 #endif
1280 }
1281
1282 static ssize_t rm31080_slowscan_store(struct device *dev,
1283                                                 struct device_attribute *attr,
1284                                                 const char *buf, size_t count)
1285 {
1286 #ifdef ENABLE_SLOW_SCAN
1287         return rm31080_slowscan_handler(buf, count);
1288 #else
1289         return count;
1290 #endif
1291 }
1292
1293 static void rm31080_smooth_level_change(unsigned long val)
1294 {
1295         int iInfo;
1296
1297         if (val > RM_SMOOTH_LEVEL_MAX)
1298                 return;
1299
1300         g_stTs.u32SmoothLevel = val;
1301
1302         iInfo = (RM_SIGNAL_PARA_SMOOTH << 24) |
1303                         (val << 16) |
1304                         RM_SIGNAL_CHANGE_PARA;
1305
1306         rm31080_ts_send_signal(g_stTs.ulHalPID, iInfo);
1307 }
1308
1309 static ssize_t rm31080_smooth_level_handler(const char *buf, size_t count)
1310 {
1311         unsigned long val;
1312         ssize_t error;
1313         ssize_t ret;
1314
1315         if (count != 2)
1316                 return count;
1317
1318         ret = (ssize_t) count;
1319         error = kstrtoul(buf, 10, &val);
1320         if (error)
1321                 ret = error;
1322         else
1323                 rm31080_smooth_level_change(val);
1324
1325         return ret;
1326 }
1327
1328 static ssize_t rm31080_self_test_handler(const char *buf, size_t count)
1329 {
1330         unsigned long val;
1331         ssize_t error;
1332         ssize_t ret;
1333         int iInfo;
1334
1335         ret = (ssize_t) count;
1336
1337         if (count != 2)
1338                 return ret;
1339
1340         if (g_stTs.u8SelfTestStatus == RM_SELF_TEST_STATUS_TESTING)
1341                 return ret;
1342         g_stTs.u8SelfTestResult = RM_SELF_TEST_RESULT_PASS;
1343
1344         error = kstrtoul(buf, 10, &val);
1345         if (error) {
1346                 ret = error;
1347         } else if (val == 0) {
1348                 g_stTs.bInitFinish = 0;
1349                 msleep(1000);
1350                 rm31080_spi_checking(1);
1351         } else if ((val >= 0x01) && (val <= 0xFF)) {
1352                 g_stTs.u8SelfTestStatus = RM_SELF_TEST_STATUS_TESTING;
1353                 iInfo = (RM_SIGNAL_PARA_SELF_TEST << 24) |
1354                                 (val << 16) |
1355                                 RM_SIGNAL_CHANGE_PARA;
1356                 rm31080_ts_send_signal(g_stTs.ulHalPID, iInfo);
1357         }
1358
1359         return ret;
1360 }
1361
1362 static ssize_t rm31080_smooth_level_show(struct device *dev,
1363                                          struct device_attribute *attr,
1364                                          char *buf)
1365 {
1366         return sprintf(buf, "Smooth level:%d\n", g_stTs.u32SmoothLevel);
1367 }
1368
1369 static ssize_t rm31080_smooth_level_store(struct device *dev,
1370                                           struct device_attribute *attr,
1371                                           const char *buf, size_t count)
1372 {
1373         rm31080_smooth_level_handler(buf, count);
1374         return count;
1375 }
1376
1377 static ssize_t rm31080_self_test_show(struct device *dev,
1378                                                 struct device_attribute *attr,
1379                                                 char *buf)
1380 {
1381         return sprintf(buf, "Self_Test:Status:%d ,Result:%d\n",
1382                                         g_stTs.u8SelfTestStatus,
1383                                         g_stTs.u8SelfTestResult);
1384 }
1385
1386 static ssize_t rm31080_self_test_store(struct device *dev,
1387                                         struct device_attribute *attr,
1388                                         const char *buf, size_t count)
1389 {
1390         rm31080_self_test_handler(buf, count);
1391         return count;
1392 }
1393
1394 static ssize_t rm31080_version_show(struct device *dev,
1395                                         struct device_attribute *attr,
1396                                         char *buf)
1397 {
1398         return sprintf(buf, "0x%02X\n", g_stTs.u8Version);
1399 }
1400
1401 static ssize_t rm31080_version_store(struct device *dev,
1402                                         struct device_attribute *attr,
1403                                         const char *buf, size_t count)
1404 {
1405         return count;
1406 }
1407
1408 static void rm31080_ctrl_registry(void)
1409 {
1410         int i;
1411         unsigned char buf;
1412         if (g_stTs.u8ScanModeState == RM_SCAN_MODE_AUTO_SCAN)
1413                 rm31080_ctrl_pause_auto_mode();
1414
1415         for (i = 4; i < 128; i++) {
1416                 rm31080_spi_byte_read(i, &buf);
1417                 rm_printk("BANK0 %2xh = 0x%2x\n", i, buf);
1418         }
1419         rm31080_spi_byte_write(0x7F, 0x01);
1420         for (i = 0; i < 128; i++) {
1421                 rm31080_spi_byte_read(i, &buf);
1422                 rm_printk("BANK1 %2xh = 0x%2x\n", i, buf);
1423         }
1424         rm31080_spi_byte_write(0x7F, 0x00);
1425
1426         if (g_stTs.u8ScanModeState == RM_SCAN_MODE_AUTO_SCAN) {
1427                 rm31080_ctrl_enter_auto_mode();
1428                 usleep_range(1000, 2000);       /*msleep(1); */
1429                 rm31080_ctrl_scan_start();
1430         }
1431 }
1432
1433 static ssize_t rm31080_registry_show(struct device *dev,
1434                                         struct device_attribute *attr,
1435                                         char *buf)
1436 {
1437         mutex_lock(&g_stTs.mutex_scan_mode);
1438
1439         rm31080_ctrl_registry();
1440
1441         mutex_unlock(&g_stTs.mutex_scan_mode);
1442
1443         return sprintf(buf, "Finish!\n");
1444 }
1445
1446 static ssize_t rm31080_registry_store(struct device *dev,
1447                                         struct device_attribute *attr,
1448                                         const char *buf, size_t count)
1449 {
1450         return count;
1451 }
1452
1453 static DEVICE_ATTR(slowscan_enable, 0640, rm31080_slowscan_show,
1454                         rm31080_slowscan_store);
1455 static DEVICE_ATTR(smooth_level, 0640, rm31080_smooth_level_show,
1456                         rm31080_smooth_level_store);
1457 static DEVICE_ATTR(self_test, 0640, rm31080_self_test_show,
1458                         rm31080_self_test_store);
1459 static DEVICE_ATTR(version, 0640, rm31080_version_show,
1460                         rm31080_version_store);
1461 static DEVICE_ATTR(registry, 0640, rm31080_registry_show,
1462                         rm31080_registry_store);
1463
1464 static struct attribute *rm_ts_attributes[] = {
1465         &dev_attr_slowscan_enable.attr,
1466         &dev_attr_smooth_level.attr,
1467         &dev_attr_self_test.attr,
1468         &dev_attr_version.attr,
1469         &dev_attr_registry.attr,
1470         NULL
1471 };
1472
1473 static const struct attribute_group rm_ts_attr_group = {
1474         .attrs = rm_ts_attributes,
1475 };
1476
1477 static int rm31080_input_open(struct input_dev *input)
1478 {
1479         struct rm31080_ts *ts = input_get_drvdata(input);
1480
1481         if (!ts->disabled && !ts->suspended)
1482                 __rm31080_enable(ts);
1483
1484         return 0;
1485 }
1486
1487 static void rm31080_input_close(struct input_dev *input)
1488 {
1489         struct rm31080_ts *ts = input_get_drvdata(input);
1490
1491         if (!ts->disabled && !ts->suspended)
1492                 __rm31080_disable(ts);
1493 }
1494
1495 /*=========================================================================*/
1496 static irqreturn_t rm31080_irq(int irq, void *handle)
1497 {
1498         /*struct rm31080_ts *ts = handle; */
1499         if (!g_stTs.bInitFinish)
1500                 return IRQ_HANDLED;
1501
1502         trace_touchscreen_raydium_irq("Raydium_interrupt");
1503
1504 #ifdef NV_ENABLE_CPU_BOOST
1505         if (g_stCtrl.bfPowerMode &&
1506                         (g_stTs.u8ScanModeState == RM_SCAN_MODE_AUTO_SCAN))
1507                 input_event(g_input_dev, EV_MSC, MSC_ACTIVITY, 1);
1508 #endif
1509
1510 #ifdef ENABLE_WORK_QUEUE
1511         queue_work(g_stTs.rm_workqueue, &g_stTs.rm_work);
1512 #endif
1513         return IRQ_HANDLED;
1514 }
1515
1516 /*==========================================================================*/
1517 static void rm31080_init_ts_structure_part(void)
1518 {
1519         g_stTs.bInitFinish = 0;
1520         g_stTs.bCalcFinish = 0;
1521         g_stTs.bEnableScriber = 0;
1522         g_stTs.bIsSuspended = 0;
1523         g_stTs.bEnableAutoScan = 1;
1524
1525         g_stTs.bEnableSlowScan = false;
1526
1527 #ifdef ENABLE_RAW_DATA_QUEUE
1528         g_stTs.u8ScanModeState = RM_SCAN_MODE_MANUAL;
1529 #endif
1530         rm31080_ctrl_init();
1531 }
1532
1533 void rm31080_disable_touch(void)
1534 {
1535         g_stTs.bInitFinish = 0;
1536 }
1537
1538 void rm31080_enable_touch(void)
1539 {
1540         g_stTs.bInitFinish = 1;
1541 }
1542
1543 void rm31080_set_autoscan(unsigned char val)
1544 {
1545         g_stTs.bEnableAutoScan = val;
1546 }
1547
1548 void rm31080_set_variable(unsigned int index, unsigned int arg)
1549 {
1550         switch (index) {
1551         case RM_VARIABLE_SELF_TEST_RESULT:
1552                 g_stTs.u8SelfTestResult = (u8) arg;
1553                 g_stTs.u8SelfTestStatus = RM_SELF_TEST_STATUS_FINISH;
1554                 break;
1555         case RM_VARIABLE_SCRIBER_FLAG:
1556                 g_stTs.bEnableScriber = (bool) arg;
1557                 break;
1558         case RM_VARIABLE_AUTOSCAN_FLAG:
1559                 g_stTs.bEnableAutoScan = (bool) arg;
1560                 break;
1561         case RM_VARIABLE_VERSION:
1562                 g_stTs.u8Version = (u8) arg;
1563                 dev_info(&g_spi->dev, "Raydium TS:Firmware v%d\n",
1564                                 g_stTs.u8Version);
1565                 break;
1566         case RM_VARIABLE_IDLEMODECHECK:
1567                 g_stCtrl.bfIdleModeCheck = (u8) arg;
1568                 if (g_stCtrl.bfIdleMessage)
1569                         rm_printk(
1570                         "## Raydium debug: bfIdleModeCheck %2x\n", arg);
1571                 break;
1572         case RM_VARIABLE_REPEAT:
1573                 rm_printk("Repeat %d\n", arg);
1574                 g_stTs.u8Repeat = (u8) arg;
1575                 break;
1576         default:
1577                 break;
1578         }
1579
1580 }
1581
1582 static u32 rm31080_get_variable(unsigned int index, unsigned int arg)
1583 {
1584         u32 ret = 0;
1585         switch (index) {
1586         case RM_VARIABLE_PLATFORM_ID:
1587                 ret = rm31080_get_platform_id((u8 *) arg);
1588                 break;
1589         default:
1590                 break;
1591         }
1592
1593         return ret;
1594 }
1595
1596 static void rm31080_init_ts_structure(void)
1597 {
1598         g_stTs.ulHalPID = 0;
1599         memset(&g_stTs, 0, sizeof(struct rm31080a_ts_para));
1600
1601 #ifdef ENABLE_SLOW_SCAN
1602         rm31080_slow_scan_init();
1603         g_stTs.u32SlowScanLevel = RM_SLOW_SCAN_LEVEL_140;
1604 #endif
1605
1606 #ifdef ENABLE_WORK_QUEUE
1607         g_stTs.rm_workqueue = create_singlethread_workqueue("rm_work");
1608         INIT_WORK(&g_stTs.rm_work, rm_work_handler);
1609 #endif
1610         mutex_init(&g_stTs.mutex_scan_mode);
1611 }
1612
1613 /*=========================================================================*/
1614 #ifdef ENABLE_SPI_SETTING
1615 static int rm31080_spi_setting(u32 speed)
1616 {
1617         int err;
1618         if ((speed == 0) || (speed > 18))
1619                 return false;
1620
1621         g_spi->max_speed_hz = speed * 1000 * 1000;
1622         err = spi_setup(g_spi);
1623         if (err) {
1624                 dev_dbg(&g_spi->dev, "Change SPI setting failed\n");
1625                 return false;
1626         }
1627         return true;
1628 }
1629 #endif
1630 /*=========================================================================*/
1631 static int rm31080_spi_checking(bool bInfinite)
1632 {
1633         unsigned int i;
1634         unsigned int iTestCount = 0;
1635         unsigned char wbuf[] = { 0x50, 0x55, 0xAA, 0x00, 0xFF };
1636         unsigned char rbuf[sizeof(wbuf)];
1637         unsigned int iLen;
1638         int iFail;
1639         unsigned char var;
1640
1641         if (rm31080_spi_byte_read(RM31080_REG_7E, &var)) {
1642                 if ((var & 0xF0) != T007A6) {
1643                         wbuf[0] = 0x6E;
1644                         var = 0x01;
1645                         rm31080_spi_byte_write(RM31080_REG_7F, var);
1646                 }
1647         }
1648
1649         rbuf[0] = wbuf[0] | 0x80;       /*address */
1650         iLen = sizeof(wbuf) - 1;
1651         do {
1652                 spi_write(g_spi, wbuf, sizeof(wbuf));
1653                 memset(&rbuf[1], 0, iLen);
1654                 /*spi_write_then_read(g_spi, &rbuf[0], 1, &rbuf[1], iLen); */
1655                 rm31080_spi_read(rbuf[0], &rbuf[1], iLen);
1656
1657                 /*compare */
1658                 iFail = 0;
1659                 for (i = 1; i < iLen + 1; i++) {
1660                         if (wbuf[i] != rbuf[i]) {
1661                                 iFail = 1;
1662                                 break;
1663                         }
1664                 }
1665
1666                 if (iFail) {
1667                         rm_printk("Raydium SPI Checking:Compare fail\n");
1668                         rm_printk("SPI Speed:%d hz,Mode:%x\n",
1669                                         g_spi->max_speed_hz, g_spi->mode);
1670                         for (i = 1; i < iLen + 1; i++) {
1671                                 rm_printk("[%02d]Write Data:0x%02x,", i,
1672                                         wbuf[i]);
1673                                 rm_printk("Read Data:0x%02x\n", rbuf[i]);
1674                         }
1675                         msleep(300);
1676                 } else {
1677                         iTestCount++;
1678                         if ((iTestCount % 10000) == 0) {
1679                                 rm_printk("SPI test:%d times ok.", iTestCount);
1680                                 rm_printk("(Speed:%d hz,Mode:%x)\n",
1681                                 g_spi->max_speed_hz, g_spi->mode);
1682                         }
1683                 }
1684         } while (bInfinite);
1685
1686         if (!iFail) {
1687                 if (wbuf[0] == 0x6E) {
1688                         var = 0x00;
1689                         rm31080_spi_byte_write(RM31080_REG_7F, var);
1690                 }
1691                 rm_printk("Raydium SPI Checking: ok\n");
1692         }
1693
1694         return !iFail;
1695
1696 }
1697
1698 static int rm31080_voltage_notifier_1v8(struct notifier_block *nb,
1699                                         unsigned long event, void *ignored)
1700 {
1701         int error;
1702         struct rm31080_ts *ts = input_get_drvdata(g_input_dev);
1703
1704         rm_printk("rm31080 REGULATOR EVENT:0x%x\n", (unsigned int)event);
1705
1706         if (event & REGULATOR_EVENT_POST_ENABLE) {
1707                 /* 1. 1v8 power on */
1708                 /* 2. wait 5ms */
1709                 usleep_range(5000, 6000);
1710                 /* 3. 3v3 power on */
1711                 error = regulator_enable(ts->regulator_3v3);
1712                 if (error < 0) {
1713                         dev_err(&g_spi->dev,
1714                                 "raydium regulator 3V3 enable failed: %d\n",
1715                                 error);
1716                         return NOTIFY_BAD;
1717                 }
1718         }
1719
1720         return NOTIFY_OK;
1721 }
1722
1723 static int rm31080_voltage_notifier_3v3(struct notifier_block *nb,
1724                                         unsigned long event, void *ignored)
1725 {
1726         struct rm_spi_ts_platform_data *pdata;
1727         struct rm31080_ts *ts;
1728         int error;
1729
1730         pdata = g_input_dev->dev.parent->platform_data;
1731         ts = input_get_drvdata(g_input_dev);
1732
1733         rm_printk("rm31080 REGULATOR EVENT:0x%x\n", (unsigned int)event);
1734
1735         if (event & REGULATOR_EVENT_POST_ENABLE) {
1736                 /* 4. sleep 5ms */
1737                 usleep_range(5000, 6000);
1738                 if (ts->clk)
1739                         /* 5. enable clock */
1740                         clk_enable(ts->clk);
1741                 /* 6. reset GPIO */
1742                 gpio_set_value(pdata->gpio_reset, 1);
1743         }
1744
1745         if (event & REGULATOR_EVENT_DISABLE) {
1746                 /* 1. 3v3 power off */
1747                 /* 2. sleep 30ms */
1748                 msleep(30);
1749                 /* 3. pull low reset */
1750                 gpio_set_value(pdata->gpio_reset, 0);
1751                 /* 4 disable clock */
1752                 if (ts->clk)
1753                         clk_disable(ts->clk);
1754                 /* 5. disable 1.8 */
1755                 error = regulator_disable(ts->regulator_1v8);
1756                 if (error < 0) {
1757                         dev_err(&g_spi->dev,
1758                         "raydium regulator 1.8V disable failed: %d\n",
1759                                 error);
1760                         return NOTIFY_BAD;
1761                 }
1762         }
1763         return NOTIFY_OK;
1764 }
1765
1766 /*=========================================================================*/
1767 static void rm31080_start(struct rm31080_ts *ts)
1768 {                               /* handle touch resume */
1769         int error;
1770
1771         if (!g_stTs.bIsSuspended)
1772                 return;
1773         g_stTs.bIsSuspended = false;
1774
1775         mutex_lock(&g_stTs.mutex_scan_mode);
1776
1777 #ifdef ENABLE_RM31080_DEEP_SLEEP
1778         /* 1. enable (1.8v) */
1779         if (ts->regulator_1v8) {
1780                 error = regulator_enable(ts->regulator_1v8);
1781                 if (error < 0)
1782                         dev_err(&g_spi->dev,
1783                                 "raydium regulator 1.8V enable failed: %d\n",
1784                                 error);
1785         }
1786
1787         rm31080_init_ts_structure_part();
1788         rm31080_ts_send_signal(g_stTs.ulHalPID, RM_SIGNAL_RESUME);
1789 #elif defined(ENABLE_AUTO_SCAN)
1790         rm31080_ctrl_clear_int();
1791         rm31080_ctrl_scan_start();
1792 #endif
1793         mutex_unlock(&g_stTs.mutex_scan_mode);
1794 }
1795
1796 static void rm31080_stop(struct rm31080_ts *ts)
1797 {
1798         if (g_stTs.bIsSuspended)
1799                 return;
1800
1801         g_stTs.bIsSuspended = true;
1802
1803         flush_workqueue(g_stTs.rm_workqueue);
1804
1805 #ifdef ENABLE_RM31080_DEEP_SLEEP
1806         rm31080_ctrl_suspend(ts);
1807 #endif
1808 }
1809
1810 #ifdef CONFIG_PM
1811 static int rm31080_suspend(struct device *dev)
1812 {
1813         struct rm31080_ts *ts = dev_get_drvdata(dev);
1814         rm31080_stop(ts);
1815         return 0;
1816 }
1817
1818 static int rm31080_resume(struct device *dev)
1819 {
1820         struct rm31080_ts *ts = dev_get_drvdata(dev);
1821         rm31080_start(ts);
1822         return 0;
1823 }
1824
1825 static const struct dev_pm_ops rm31080_pm_ops = {
1826         .suspend = rm31080_suspend,
1827         .resume = rm31080_resume,
1828 };
1829
1830 #endif /*CONFIG_PM */
1831
1832 /* NVIDIA 20121026*/
1833 #ifdef ENABLE_NEW_INPUT_DEV
1834 static int rm31080_input_enable(struct input_dev *in_dev)
1835 {
1836         int error = 0;
1837
1838 #ifdef CONFIG_PM
1839         struct rm31080_ts *ts = input_get_drvdata(in_dev);
1840
1841         error = rm31080_resume(ts->dev);
1842         if (error)
1843                 dev_err(ts->dev, "%s: failed\n", __func__);
1844 #endif
1845
1846         return error;
1847 }
1848
1849 static int rm31080_input_disable(struct input_dev *in_dev)
1850 {
1851         int error = 0;
1852
1853 #ifdef CONFIG_PM
1854         struct rm31080_ts *ts = input_get_drvdata(in_dev);
1855
1856         error = rm31080_suspend(ts->dev);
1857         if (error)
1858                 dev_err(ts->dev, "%s: failed\n", __func__);
1859 #endif
1860
1861         return error;
1862 }
1863 #endif /*ENABLE_NEW_INPUT_DEV */
1864
1865 static void rm31080_set_input_resolution(unsigned int x, unsigned int y)
1866 {
1867         input_set_abs_params(g_input_dev, ABS_X, 0, x - 1, 0, 0);
1868         input_set_abs_params(g_input_dev, ABS_Y, 0, y - 1, 0, 0);
1869         input_set_abs_params(g_input_dev, ABS_MT_POSITION_X, 0, x - 1, 0, 0);
1870         input_set_abs_params(g_input_dev, ABS_MT_POSITION_Y, 0, y - 1, 0, 0);
1871 }
1872
1873 struct rm31080_ts *rm31080_input_init(struct device *dev, unsigned int irq,
1874                                         const struct rm31080_bus_ops *bops)
1875 {
1876
1877         struct rm31080_ts *ts;
1878         struct input_dev *input_dev;
1879         struct rm_spi_ts_platform_data *pdata;
1880         int err;
1881
1882         if (!irq) {
1883                 dev_err(dev, "no IRQ?\n");
1884                 err = -EINVAL;
1885                 goto err_out;
1886         }
1887
1888         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
1889
1890         input_dev = input_allocate_device();
1891
1892         if (!ts || !input_dev) {
1893                 dev_err(dev, "Failed to allocate memory\n");
1894                 err = -ENOMEM;
1895                 goto err_free_mem;
1896         }
1897
1898         g_input_dev = input_dev;
1899
1900         ts->bops = bops;
1901         ts->dev = dev;
1902         ts->input = input_dev;
1903         ts->irq = irq;
1904
1905         pdata = dev->platform_data;
1906         if (pdata->name_of_clock || pdata->name_of_clock_con) {
1907                 ts->clk = clk_get_sys(pdata->name_of_clock,
1908                         pdata->name_of_clock_con);
1909                 if (IS_ERR(ts->clk)) {
1910                         dev_err(&g_spi->dev, "failed to get touch_clk: (%s, %s)\n",
1911                                 pdata->name_of_clock, pdata->name_of_clock_con);
1912                         err = -EINVAL;
1913                         goto err_free_mem;
1914                 }
1915         }
1916
1917         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
1918
1919         input_dev->name = "raydium_ts";
1920         input_dev->phys = ts->phys;
1921         input_dev->dev.parent = dev;
1922         input_dev->id.bustype = bops->bustype;
1923
1924         input_dev->open = rm31080_input_open;
1925         input_dev->close = rm31080_input_close;
1926 #ifdef ENABLE_NEW_INPUT_DEV
1927         input_dev->enable = rm31080_input_enable;
1928         input_dev->disable = rm31080_input_disable;
1929         input_dev->enabled = true;
1930 #endif
1931         input_dev->hint_events_per_packet = 256U;
1932
1933         input_set_drvdata(input_dev, ts);
1934         input_set_capability(input_dev, EV_MSC, MSC_ACTIVITY);
1935
1936         __set_bit(EV_ABS, input_dev->evbit);
1937         __set_bit(ABS_X, input_dev->absbit);
1938         __set_bit(ABS_Y, input_dev->absbit);
1939
1940         __set_bit(EV_KEY, input_dev->evbit);
1941         __set_bit(BTN_TOUCH, input_dev->keybit);
1942
1943         rm31080_set_input_resolution(RM_INPUT_RESOLUTION_X,
1944                                                 RM_INPUT_RESOLUTION_Y);
1945         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 0xFF, 0, 0);
1946         input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 0xFF, 0, 0);
1947         input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 0xFF, 0, 0);
1948         input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, 32, 0, 0);
1949
1950         err = request_threaded_irq(ts->irq, NULL, rm31080_irq,
1951                                         IRQF_TRIGGER_RISING, dev_name(dev), ts);
1952         if (err) {
1953                 dev_err(dev, "irq %d busy?\n", ts->irq);
1954                 goto err_free_mem;
1955         }
1956         mutex_init(&ts->access_mutex);
1957
1958         __rm31080_disable(ts);
1959
1960         err = sysfs_create_group(&dev->kobj, &rm_ts_attr_group);
1961         if (err)
1962                 goto err_free_irq;
1963
1964         err = input_register_device(input_dev);
1965         if (err)
1966                 goto err_remove_attr;
1967
1968         return ts;
1969
1970 err_remove_attr:
1971         sysfs_remove_group(&dev->kobj, &rm_ts_attr_group);
1972 err_free_irq:
1973         free_irq(ts->irq, ts);
1974 err_free_mem:
1975         input_free_device(input_dev);
1976         kfree(ts);
1977 err_out:
1978         return ERR_PTR(err);
1979 }
1980
1981 static int dev_open(struct inode *inode, struct file *filp)
1982 {
1983         return 0;
1984 }
1985
1986 static int dev_release(struct inode *inode, struct file *filp)
1987 {
1988         g_stTs.bInitFinish = 0;
1989         rm31080_enter_manual_mode();
1990         return 0;
1991 }
1992
1993 static ssize_t
1994 dev_read(struct file *filp, char __user *buf, size_t count, loff_t *pos)
1995 {
1996         ssize_t missing, status;
1997         int ret;
1998         u8 *pMyBuf;
1999
2000         pMyBuf = kmalloc(count, GFP_KERNEL);
2001         if (pMyBuf == NULL)
2002                 return -ENOMEM;
2003
2004         pMyBuf[0] = buf[0];
2005         ret = rm31080_spi_read(pMyBuf[0], pMyBuf, count);
2006
2007         if (ret) {
2008                 status = count;
2009                 missing = copy_to_user(buf, pMyBuf, count);
2010
2011                 if (missing == status)
2012                         status = -EFAULT;
2013                 else
2014                         status = status - missing;
2015         } else {
2016                 status = -EFAULT;
2017                 rmd_printk("rm31080_spi_read() fail\n");
2018         }
2019
2020         kfree(pMyBuf);
2021         return status;
2022 }
2023
2024 static ssize_t
2025 dev_write(struct file *filp, const char __user *buf,
2026                 size_t count, loff_t *pos)
2027 {
2028         u8 *pMyBuf;
2029         int ret;
2030         unsigned long missing;
2031         ssize_t status = 0;
2032
2033         pMyBuf = kmalloc(count, GFP_KERNEL);
2034         if (pMyBuf == NULL)
2035                 return -ENOMEM;
2036
2037         missing = copy_from_user(pMyBuf, buf, count);
2038         if (missing == 0) {
2039                 ret = rm31080_spi_write(pMyBuf, count);
2040                 if (ret)
2041                         status = count;
2042                 else
2043                         status = -EFAULT;
2044         } else
2045                 status = -EFAULT;
2046
2047         kfree(pMyBuf);
2048         return status;
2049 }
2050
2051 /*==============================================================================
2052  * Description:
2053  *      I/O Control routine.
2054  * Input:
2055  *      file:
2056  *      cmd :
2057  *      arg :
2058  * Output:
2059  *      1: succeed
2060  *      0: failed
2061  * Note: To avoid context switch,
2062  *       please don't add debug message in this function.
2063  *==============================================================================
2064  */
2065 static long dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2066 {
2067         long ret = true;
2068         unsigned int index;
2069         index = (cmd >> 16) & 0xFFFF;
2070         switch (cmd & 0xFFFF) {
2071         case RM_IOCTL_REPORT_POINT:
2072                 raydium_report_pointer((void *)arg);
2073                 break;
2074         case RM_IOCTL_SET_HAL_PID:
2075                 g_stTs.ulHalPID = arg;
2076                 break;
2077         case RM_IOCTL_INIT_START:
2078                 g_stTs.bInitFinish = 0;
2079                 rm31080_enter_manual_mode();
2080                 break;
2081         case RM_IOCTL_INIT_END:
2082                 g_stTs.bInitFinish = 1;
2083                 g_stTs.bCalcFinish = 1;
2084 #ifdef ENABLE_RAW_DATA_QUEUE
2085                 ret = rm31080_ctrl_scan_start();
2086 #endif
2087                 break;
2088         case RM_IOCTL_FINISH_CALC:
2089                 g_stTs.bCalcFinish = 1;
2090                 break;
2091         case RM_IOCTL_SCRIBER_CTRL:
2092                 g_stTs.bEnableScriber = (bool) arg;
2093                 break;
2094         case RM_IOCTL_AUTOSCAN_CTRL:
2095                 g_stTs.bEnableAutoScan = (bool) arg;
2096                 break;
2097 #ifdef ENABLE_RAW_DATA_QUEUE
2098         case RM_IOCTL_READ_RAW_DATA:
2099                 ret = rm31080_queue_read_raw_data((u8 *) arg, index);
2100                 break;
2101 #endif
2102         case RM_IOCTL_NOISE_CHECK:
2103                 ret = rm31080_ctrl_get_noise_mode((u8 *) arg);
2104                 break;
2105         case RM_IOCTL_GET_PARAMETER:
2106                 rm31080_ctrl_get_parameter((void *)arg);
2107                 rm31080_set_input_resolution(g_stCtrl.u16ResolutionX,
2108                                                 g_stCtrl.u16ResolutionY);
2109                 break;
2110         case RM_IOCTL_SET_PARAMETER:
2111                 ret = rm31080_get_config((u8 *) arg, index);
2112                 break;
2113         case RM_IOCTL_SEND_BASELINE:    /* Noise_Detector */
2114                 break;
2115         case RM_IOCTL_SET_VARIABLE:
2116                 rm31080_set_variable(index, arg);
2117                 break;
2118
2119         case RM_IOCTL_GET_VARIABLE:
2120                 ret = rm31080_get_variable(index, arg);
2121                 break;
2122 #ifdef ENABLE_SLOW_SCAN
2123         case RM_IOCTL_SET_SLOWSCAN_PARA:
2124                 rm31080_set_slowscan_para((u8 *) arg, index);
2125                 break;
2126 #endif
2127         case RM_IOCTL_GET_SACN_MODE:
2128                 rm31080_ctrl_get_idle_mode((u8 *) arg);
2129                 break;
2130
2131         default:
2132                 break;
2133         }
2134         return ret;
2135 }
2136
2137 static const struct file_operations dev_fops = {
2138         .owner = THIS_MODULE,
2139         .open = dev_open,
2140         .release = dev_release,
2141         .read = dev_read,
2142         .write = dev_write,
2143         .unlocked_ioctl = dev_ioctl,
2144 };
2145
2146 static struct miscdevice raydium_ts_miscdev = {
2147         .minor = MISC_DYNAMIC_MINOR,
2148         .name = "raydium_ts",
2149         .fops = &dev_fops,
2150 };
2151
2152 static const struct rm31080_bus_ops rm31080_spi_bus_ops = {
2153         .bustype = BUS_SPI,
2154 };
2155
2156 static int rm31080_misc_init(void)
2157 {
2158         int err;
2159         err = sysfs_create_group(&raydium_ts_miscdev.this_device->kobj,
2160                                  &rm_ts_attr_group);
2161         if (err)
2162                 return false;
2163         return true;
2164 }
2165
2166 static void rm31080_init_regulator(struct rm31080_ts *ts)
2167 {
2168         int error;
2169
2170         ts->regulator_3v3 = devm_regulator_get(&g_spi->dev, "avdd");
2171         if (IS_ERR(ts->regulator_3v3)) {
2172                 dev_err(&g_spi->dev, "Raydium TS: regulator_get failed: %ld\n",
2173                         PTR_ERR(ts->regulator_3v3));
2174                 goto err_null_regulator;
2175         }
2176
2177         ts->regulator_1v8 = devm_regulator_get(&g_spi->dev, "dvdd");
2178         if (IS_ERR(ts->regulator_1v8)) {
2179                 dev_err(&g_spi->dev, "Raydium TS: regulator_get failed: %ld\n",
2180                         PTR_ERR(ts->regulator_1v8));
2181                 goto err_null_regulator;
2182         }
2183
2184         /* Enable 1v8 first*/
2185         error = regulator_enable(ts->regulator_1v8);
2186         if (error < 0)
2187                 dev_err(&g_spi->dev,
2188                         "Raydium TS: regulator enable failed: %d\n", error);
2189
2190         usleep_range(5000, 6000);
2191         /* Enable 1v8 first*/
2192         error = regulator_enable(ts->regulator_3v3);
2193         if (error < 0)
2194                 dev_err(&g_spi->dev,
2195                         "Raydium TS: regulator enable failed: %d\n", error);
2196
2197         ts->nb_1v8.notifier_call = &rm31080_voltage_notifier_1v8;
2198         error = regulator_register_notifier(ts->regulator_1v8, &ts->nb_1v8);
2199         if (error) {
2200                 dev_err(&g_spi->dev,
2201                         "regulator notifier request failed: %d\n", error);
2202                 goto err_disable_regulator;
2203         }
2204
2205         ts->nb_3v3.notifier_call = &rm31080_voltage_notifier_3v3;
2206         error = regulator_register_notifier(ts->regulator_3v3, &ts->nb_3v3);
2207         if (error) {
2208                 dev_err(&g_spi->dev,
2209                         "regulator notifier request failed: %d\n", error);
2210                 goto err_unregister_notifier;
2211         }
2212
2213         return;
2214
2215 err_unregister_notifier:
2216         regulator_unregister_notifier(ts->regulator_1v8, &ts->nb_1v8);
2217 err_disable_regulator:
2218         regulator_disable(ts->regulator_3v3);
2219         regulator_disable(ts->regulator_1v8);
2220 err_null_regulator:
2221         ts->regulator_3v3 = NULL;
2222         ts->regulator_1v8 = NULL;
2223 }
2224
2225 static int __devexit rm31080_spi_remove(struct spi_device *spi)
2226 {
2227         struct rm31080_ts *ts = spi_get_drvdata(spi);
2228
2229 #ifdef ENABLE_RAW_DATA_QUEUE
2230         rm31080_queue_free();
2231 #endif
2232
2233 #ifdef ENABLE_WORK_QUEUE
2234         if (g_stTs.rm_workqueue)
2235                 destroy_workqueue(g_stTs.rm_workqueue);
2236 #endif
2237         sysfs_remove_group(&raydium_ts_miscdev.this_device->kobj,
2238                            &rm_ts_attr_group);
2239         misc_deregister(&raydium_ts_miscdev);
2240         sysfs_remove_group(&ts->dev->kobj, &rm_ts_attr_group);
2241         free_irq(ts->irq, ts);
2242         input_unregister_device(ts->input);
2243         if (ts->regulator_3v3 && ts->regulator_1v8) {
2244                 regulator_unregister_notifier(ts->regulator_3v3, &ts->nb_3v3);
2245                 regulator_unregister_notifier(ts->regulator_1v8, &ts->nb_1v8);
2246                 regulator_disable(ts->regulator_3v3);
2247                 regulator_disable(ts->regulator_1v8);
2248         }
2249         if (ts->clk)
2250                 clk_disable(ts->clk);
2251         kfree(ts);
2252         spi_set_drvdata(spi, NULL);
2253         return 0;
2254 }
2255
2256 static int __devinit rm31080_spi_probe(struct spi_device *spi)
2257 {
2258         struct rm31080_ts *ts;
2259         struct rm_spi_ts_platform_data *pdata;
2260
2261         g_spi = spi;
2262
2263         rm31080_init_ts_structure();
2264
2265         ts = rm31080_input_init(&spi->dev, spi->irq, &rm31080_spi_bus_ops);
2266         if (IS_ERR(ts))
2267                 return PTR_ERR(ts);
2268         spi_set_drvdata(spi, ts);
2269
2270         rm31080_init_regulator(ts);
2271
2272         pdata = g_input_dev->dev.parent->platform_data;
2273         usleep_range(5000, 6000);
2274         if (ts->clk)
2275                 clk_enable(ts->clk);
2276
2277         gpio_set_value(pdata->gpio_reset, 0);
2278         msleep(120);
2279         gpio_set_value(pdata->gpio_reset, 1);
2280         usleep_range(15000, 190000);
2281
2282         if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
2283                 dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
2284                 goto err_free;
2285         }
2286
2287         rm31080_init_ts_structure_part();
2288
2289         if (!rm31080_spi_checking(0))
2290                 goto err_free;
2291
2292         if (misc_register(&raydium_ts_miscdev) != 0) {
2293                 dev_err(&spi->dev, "Raydium TS: cannot register miscdev\n");
2294                 kfree(ts);
2295                 return 0;
2296         }
2297         rm31080_misc_init();
2298
2299 #ifdef ENABLE_RAW_DATA_QUEUE
2300         rm31080_queue_init();
2301 #endif
2302
2303         /* Enable async suspend/resume to reduce LP0 latency */
2304         device_enable_async_suspend(&spi->dev);
2305
2306         return 0;
2307
2308 err_free:
2309         sysfs_remove_group(&ts->dev->kobj, &rm_ts_attr_group);
2310         free_irq(ts->irq, ts);
2311         input_unregister_device(ts->input);
2312         if (ts->regulator_3v3 && ts->regulator_1v8) {
2313                 regulator_unregister_notifier(ts->regulator_3v3, &ts->nb_3v3);
2314                 regulator_unregister_notifier(ts->regulator_1v8, &ts->nb_1v8);
2315                 regulator_disable(ts->regulator_3v3);
2316                 regulator_disable(ts->regulator_1v8);
2317         }
2318         if (ts->clk)
2319                 clk_disable(ts->clk);
2320         kfree(ts);
2321         spi_set_drvdata(spi, NULL);
2322         return -EINVAL;
2323 }
2324
2325 static struct spi_driver rm31080_spi_driver = {
2326         .driver = {
2327                         .name = "rm_ts_spidev",
2328                         .bus = &spi_bus_type,
2329                         .owner = THIS_MODULE,
2330
2331 #if defined(CONFIG_PM)
2332                         .pm = &rm31080_pm_ops,
2333 #endif
2334                         },
2335         .probe = rm31080_spi_probe,
2336         .remove = __devexit_p(rm31080_spi_remove),
2337 };
2338
2339 static int __init rm31080_spi_init(void)
2340 {
2341         return spi_register_driver(&rm31080_spi_driver);
2342 }
2343
2344 static void __exit rm31080_spi_exit(void)
2345 {
2346         spi_unregister_driver(&rm31080_spi_driver);
2347 }
2348
2349 module_init(rm31080_spi_init);
2350 module_exit(rm31080_spi_exit);
2351
2352 MODULE_AUTHOR("Valentine Hsu <valentine.hsu@rad-ic.com>");
2353 MODULE_DESCRIPTION("Raydium touchscreen SPI bus driver");
2354 MODULE_LICENSE("GPL");
2355 MODULE_ALIAS("spi:raydium-t007");