tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / input / touchscreen / ist30xxa / ist30xx_misc.c
1 /*
2  *  Copyright (C) 2010,Imagis Technology Co. Ltd. All Rights Reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/firmware.h>
20 #include <linux/stat.h>
21
22 #include "ist30xx.h"
23 #include "ist30xx_update.h"
24 #include "ist30xx_misc.h"
25
26 #if IST30XX_CHKIC
27 #include "ist30xx_chkic_bin.h"
28 #endif
29
30 #define TSP_CH_SCREEN       (1)
31 #define TSP_CH_KEY          (2)
32
33 #define TOUCH_NODE_PARSING_DEBUG    (0)
34
35 extern struct ist30xx_data *ts_data;
36
37 TSP_INFO ist30xx_tsp_info;
38 TKEY_INFO ist30xx_tkey_info;
39
40 static u32 *ist30xx_frame_buf;
41 static u32 *ist30xx_frame_rawbuf;
42 static u32 *ist30xx_frame_fltbuf;
43
44
45 int ist30xx_tkey_update_info(void)
46 {
47         int ret = 0;
48         u32 tkey_info1, tkey_info2, tkey_info3;
49         TKEY_INFO *tkey = &ist30xx_tkey_info;
50
51         ret = ist30xx_read_cmd(ts_data->client, CMD_GET_KEY_INFO1, &tkey_info1);
52         if (unlikely(ret))
53                 return ret;
54         ret = ist30xx_read_cmd(ts_data->client, CMD_GET_KEY_INFO2, &tkey_info2);
55         if (unlikely(ret))
56                 return ret;
57         ret = ist30xx_read_cmd(ts_data->client, CMD_GET_KEY_INFO3, &tkey_info3);
58         if (unlikely(ret))
59                 return ret;
60
61         tkey->enable = ((tkey_info1 & (0xFF << 24)) ? true : false);
62         tkey->key_num = (tkey_info1 >> 16) & 0xFF;
63         tkey->ch_num[0] = (tkey_info2 >> 24) & 0xFF;
64         tkey->ch_num[1] = (tkey_info2 >> 16) & 0xFF;
65         tkey->ch_num[2] = (tkey_info2 >> 8) & 0xFF;
66         tkey->ch_num[3] = tkey_info2 & 0xFF;
67         tkey->ch_num[4] = (tkey_info3 >> 24) & 0xFF;
68
69         return ret;
70 }
71
72
73 #define TSP_INFO_SWAP_XY    (1 << 0)
74 #define TSP_INFO_FLIP_X     (1 << 1)
75 #define TSP_INFO_FLIP_Y     (1 << 2)
76 int ist30xx_tsp_update_info(void)
77 {
78         int ret = 0;
79         u32 tsp_ch_num, tsp_swap, tsp_dir;
80         TSP_INFO *tsp = &ist30xx_tsp_info;
81
82         ret = ist30xx_read_cmd(ts_data->client, CMD_GET_TSP_SWAP_INFO, &tsp_swap);
83         if (unlikely(ret)) return ret;
84
85         ret = ist30xx_read_cmd(ts_data->client, CMD_GET_TSP_DIRECTION, &tsp_dir);
86         if (unlikely(ret)) return ret;
87
88         ret = ist30xx_read_cmd(ts_data->client, CMD_GET_TSP_CHNUM1, &tsp_ch_num);
89         if (unlikely(ret || !tsp_ch_num)) return ret;
90
91         tsp->finger_num = IST30XX_MAX_MT_FINGERS;
92
93         tsp->ch_num.rx = tsp_ch_num >> 16;
94         tsp->ch_num.tx = tsp_ch_num & 0xFFFF;
95
96         tsp->dir.swap_xy = (tsp_swap & TSP_INFO_SWAP_XY ? true : false);
97         tsp->dir.flip_x = (tsp_swap & TSP_INFO_FLIP_X ? true : false);
98         tsp->dir.flip_y = (tsp_swap & TSP_INFO_FLIP_Y ? true : false);
99
100         tsp->node.len = tsp->ch_num.tx * tsp->ch_num.rx;
101         tsp->height = (tsp->dir.swap_xy ? tsp->ch_num.rx : tsp->ch_num.tx);
102         tsp->width = (tsp->dir.swap_xy ? tsp->ch_num.tx : tsp->ch_num.rx);
103
104         return ret;
105 }
106
107 int ist30xx_check_valid_ch(int ch_tx, int ch_rx)
108 {
109         TKEY_INFO *tkey = &ist30xx_tkey_info;
110         TSP_INFO *tsp = &ist30xx_tsp_info;
111
112         if (unlikely((ch_tx > tsp->ch_num.tx) || (ch_rx > tsp->ch_num.rx)))
113                 return 0;
114
115         if (tkey->enable) {
116                 if (tkey->axis_rx) {
117                         tsp_verb("tx: %d, rx: %d\n", ch_tx, ch_rx);
118                         if (ch_rx == tsp->ch_num.rx - 1) {
119                                 tsp_verb("ch_tx: %d\n", ch_tx);
120                                 if ((ch_tx == tkey->ch_num[0]) || (ch_tx == tkey->ch_num[1]) ||
121                                     (ch_tx == tkey->ch_num[2]) || (ch_tx == tkey->ch_num[3]) ||
122                                     (ch_tx == tkey->ch_num[4]))
123                                         return TSP_CH_KEY;
124                                 else
125                                         return 0;
126                         }
127                 } else {
128                         if (ch_tx == tsp->ch_num.tx - 1) {
129                                 if ((ch_rx == tkey->ch_num[0]) || (ch_rx == tkey->ch_num[1]) ||
130                                     (ch_rx == tkey->ch_num[2]) || (ch_rx == tkey->ch_num[3]) ||
131                                     (ch_rx == tkey->ch_num[4]))
132                                         return TSP_CH_KEY;
133                                 else
134                                         return 0;
135                         }
136                 }
137         }
138
139         return TSP_CH_SCREEN;
140 }
141
142
143 int ist30xx_parse_touch_node(u8 flag, struct TSP_NODE_BUF *node)
144 {
145 #if TOUCH_NODE_PARSING_DEBUG
146         int j;
147         TSP_INFO *tsp = &ist30xx_tsp_info;
148 #endif
149         int i;
150         u16 *raw = (u16 *)&node->raw;
151         u16 *base = (u16 *)&node->base;
152         u16 *filter = (u16 *)&node->filter;
153         u32 *tmp_rawbuf = ist30xx_frame_rawbuf;
154         u32 *tmp_fltbuf = ist30xx_frame_fltbuf;
155
156         for (i = 0; i < node->len; i++) {
157                 if (flag & (NODE_FLAG_RAW | NODE_FLAG_BASE)) {
158                         *raw++ = *tmp_rawbuf & 0xFFF;
159                         *base++ = (*tmp_rawbuf >> 16) & 0xFFF;
160
161                         tmp_rawbuf++;
162                 }
163                 if (flag & NODE_FLAG_FILTER)
164                         *filter++ = *tmp_fltbuf++ & 0xFFF;
165         }
166
167 #if TOUCH_NODE_PARSING_DEBUG
168         tsp_info("RAW - %d * %d\n", tsp->ch_num.tx, tsp->ch_num.rx);
169         for (i = 0; i < tsp->ch_num.tx; i++) {
170                 printk("\n[ TSP ] ");
171                 for (j = 0; j < tsp->ch_num.rx; j++)
172                         printk("%4d ", node->raw[i][j]);
173         }
174
175         tsp_info("BASE - %d * %d\n", tsp->ch_num.tx, tsp->ch_num.rx);
176         for (i = 0; i < tsp->ch_num.tx; i++) {
177                 printk("\n[ TSP ] ");
178                 for (j = 0; j < tsp->ch_num.rx; j++)
179                         printk("%4d ", node->base[i][j]);
180         }
181
182         tsp_info("FILTER - %d * %d\n", tsp->ch_num.tx, tsp->ch_num.rx);
183         for (i = 0; i < tsp->ch_num.tx; i++) {
184                 printk("\n[ TSP ] ");
185                 for (j = 0; j < tsp->ch_num.rx; j++)
186                         printk("%4d ", node->filter[i][j]);
187         }
188 #endif
189
190         return 0;
191 }
192
193 int print_touch_node(u8 flag, struct TSP_NODE_BUF *node, char *buf, bool ch_tsp)
194 {
195         int i, j;
196         int count = 0;
197         int val = 0;
198         const int msg_len = 128;
199         char msg[msg_len];
200         TSP_INFO *tsp = &ist30xx_tsp_info;
201
202         if (tsp->dir.swap_xy) {
203         } else {
204                 for (i = 0; i < tsp->ch_num.tx; i++) {
205                         for (j = 0; j < tsp->ch_num.rx; j++) {
206                                 if (ch_tsp && (ist30xx_check_valid_ch(i, j) != TSP_CH_SCREEN))
207                                         continue;
208
209                                 if (flag == NODE_FLAG_RAW)
210                                         val = (int)node->raw[i][j];
211                                 else if (flag == NODE_FLAG_BASE)
212                                         val = (int)node->base[i][j];
213                                 else if (flag == NODE_FLAG_FILTER)
214                                         val = (int)node->filter[i][j];
215                                 else if (flag == NODE_FLAG_DIFF)
216                                         val = (int)(node->raw[i][j] - node->base[i][j]);
217                                 else
218                                         return 0;
219
220                                 if (val < 0) val = 0;
221
222                                 count += snprintf(msg, msg_len, "%4d ", val);
223                                 strncat(buf, msg, msg_len);
224                         }
225
226                         count += snprintf(msg, msg_len, "\n");
227                         strncat(buf, msg, msg_len);
228                 }
229         }
230
231         return count;
232 }
233
234 int parse_tsp_node(u8 flag, struct TSP_NODE_BUF *node, s16 *buf16)
235 {
236         int i, j;
237         s16 val = 0;
238         TSP_INFO *tsp = &ist30xx_tsp_info;
239
240         if (unlikely((flag != NODE_FLAG_RAW) && (flag != NODE_FLAG_BASE) &&
241                      (flag != NODE_FLAG_FILTER) && (flag != NODE_FLAG_DIFF)))
242                 return -EPERM;
243
244         if (tsp->dir.swap_xy) {
245         } else {
246                 for (i = 0; i < tsp->ch_num.tx; i++) {
247                         for (j = 0; j < tsp->ch_num.rx; j++) {
248                                 if (ist30xx_check_valid_ch(i, j) != TSP_CH_SCREEN)
249                                         continue;
250
251                                 switch ((int)flag) {
252                                 case NODE_FLAG_RAW:
253                                         val = (s16)node->raw[i][j];
254                                         break;
255                                 case NODE_FLAG_BASE:
256                                         val = (s16)node->base[i][j];
257                                         break;
258                                 case NODE_FLAG_FILTER:
259                                         val = (s16)node->filter[i][j];
260                                         break;
261                                 case NODE_FLAG_DIFF:
262                                         val = (s16)(node->raw[i][j] - node->base[i][j]);
263                                         break;
264                                 }
265
266                                 if (val < 0) val = 0;
267
268                                 *buf16++ = val;
269                         }
270                 }
271         }
272
273         return 0;
274 }
275
276 int ist30xx_read_touch_node(u8 flag, struct TSP_NODE_BUF *node)
277 {
278         int ret = 0;
279         u32 addr = IST30XXB_RAW_ADDR;
280
281         if (ts_data->chip_id == IST3038_CHIP_ID)
282                 addr += 0xD4;
283
284         ist30xx_disable_irq(ts_data);
285
286         if (flag & NODE_FLAG_NO_CCP) {
287                 ist30xx_reset(false);
288
289                 ret = ist30xx_write_cmd(ts_data->client, CMD_USE_CORRECT_CP, 0);
290                 if (unlikely(ret))
291                         goto read_tsp_node_end;
292         }
293
294         ret = ist30xx_cmd_reg(ts_data->client, CMD_ENTER_REG_ACCESS);
295         if (unlikely(ret))
296                 goto read_tsp_node_end;
297
298         ret = ist30xx_write_cmd(ts_data->client, IST30XX_RX_CNT_ADDR, node->len);
299         if (unlikely(ret))
300                 goto reg_access_end;
301
302         if (flag & (NODE_FLAG_RAW | NODE_FLAG_BASE)) {
303                 tsp_info("Reg addr: %x, size: %d\n", addr, node->len);
304                 ret = ist30xx_read_buf(ts_data->client, addr,
305                                        ist30xx_frame_rawbuf, node->len);
306                 if (unlikely(ret))
307                         goto reg_access_end;
308         }
309
310         if (flag & NODE_FLAG_FILTER) {
311                 tsp_info("Reg addr: %x, size: %d\n", IST30XXB_FILTER_ADDR, node->len);
312                 ret = ist30xx_read_buf(ts_data->client, IST30XXB_FILTER_ADDR,
313                                        ist30xx_frame_fltbuf, node->len);
314                 if (unlikely(ret))
315                         goto reg_access_end;
316         }
317
318 reg_access_end:
319         ist30xx_cmd_reg(ts_data->client, CMD_EXIT_REG_ACCESS);
320         ist30xx_cmd_start_scan(ts_data->client);
321
322 read_tsp_node_end:
323         ist30xx_enable_irq(ts_data);
324
325         return ret;
326 }
327
328
329 /* sysfs: /sys/class/touch/node/refresh */
330 ssize_t ist30xx_frame_refresh(struct device *dev, struct device_attribute *attr,
331                               char *buf)
332 {
333         int ret = 0;
334         TSP_INFO *tsp = &ist30xx_tsp_info;
335         u8 flag = NODE_FLAG_RAW | NODE_FLAG_BASE | NODE_FLAG_FILTER;
336
337         tsp_info("refresh\n");
338
339         ret = ist30xx_read_touch_node(flag, &tsp->node);
340         if (unlikely(ret)) {
341                 tsp_err("cmd 1frame raw update fail\n");
342                 return sprintf(buf, "FAIL\n");
343         }
344
345         ist30xx_parse_touch_node(flag, &tsp->node);
346
347         return sprintf(buf, "OK\n");
348 }
349
350
351 /* sysfs: /sys/class/touch/node/read_nocp */
352 ssize_t ist30xx_frame_nocp(struct device *dev, struct device_attribute *attr,
353                            char *buf)
354 {
355         int ret = 0;
356         TSP_INFO *tsp = &ist30xx_tsp_info;
357         u8 flag = NODE_FLAG_RAW | NODE_FLAG_BASE | NODE_FLAG_FILTER |
358                   NODE_FLAG_NO_CCP;
359
360         ret = ist30xx_read_touch_node(flag, &tsp->node);
361         if (unlikely(ret)) {
362                 tsp_err("cmd 1frame raw update fail\n");
363                 return sprintf(buf, "FAIL\n");
364         }
365
366         ist30xx_parse_touch_node(flag, &tsp->node);
367
368         return sprintf(buf, "OK\n");
369 }
370
371
372 /* sysfs: /sys/class/touch/node/base */
373 ssize_t ist30xx_base_show(struct device *dev, struct device_attribute *attr,
374                           char *buf)
375 {
376         int count = 0;
377         TSP_INFO *tsp = &ist30xx_tsp_info;
378
379         buf[0] = '\0';
380         count = sprintf(buf, "dump ist30xxb baseline(%d)\n", tsp->node.len);
381
382         count += print_touch_node(NODE_FLAG_BASE, &tsp->node, buf, false);
383
384         return count;
385 }
386
387
388 /* sysfs: /sys/class/touch/node/raw */
389 ssize_t ist30xx_raw_show(struct device *dev, struct device_attribute *attr,
390                          char *buf)
391 {
392         int count = 0;
393         TSP_INFO *tsp = &ist30xx_tsp_info;
394
395         buf[0] = '\0';
396         count = sprintf(buf, "dump ist30xxb raw(%d)\n", tsp->node.len);
397
398         count += print_touch_node(NODE_FLAG_RAW, &tsp->node, buf, false);
399
400         return count;
401 }
402
403
404 /* sysfs: /sys/class/touch/node/diff */
405 ssize_t ist30xx_diff_show(struct device *dev, struct device_attribute *attr,
406                           char *buf)
407 {
408         int count = 0;
409         TSP_INFO *tsp = &ist30xx_tsp_info;
410
411         buf[0] = '\0';
412         count = sprintf(buf, "dump ist30xxb difference (%d)\n", tsp->node.len);
413
414         count += print_touch_node(NODE_FLAG_DIFF, &tsp->node, buf, false);
415
416         return count;
417 }
418
419
420 /* sysfs: /sys/class/touch/node/filter */
421 ssize_t ist30xx_filter_show(struct device *dev, struct device_attribute *attr,
422                             char *buf)
423 {
424         int count = 0;
425         TSP_INFO *tsp = &ist30xx_tsp_info;
426
427         buf[0] = '\0';
428         count = sprintf(buf, "dump ist30xxb filter (%d)\n", tsp->node.len);
429
430         count += print_touch_node(NODE_FLAG_FILTER, &tsp->node, buf, false);
431
432         return count;
433 }
434
435
436 extern int calib_ms_delay;
437 /* sysfs: /sys/class/touch/sys/clb_time */
438 ssize_t ist30xx_calib_time_store(struct device *dev,
439                                  struct device_attribute *attr, const char *buf, size_t size)
440 {
441         int ms_delay;
442
443         sscanf(buf, "%d", &ms_delay);
444
445         if (ms_delay > 10 && ms_delay < 1000) // 1sec ~ 100sec
446                 calib_ms_delay = ms_delay;
447
448         tsp_info("Calibration wait time %dsec\n", calib_ms_delay / 10);
449
450         return size;
451 }
452
453 /* sysfs: /sys/class/touch/sys/clb */
454 ssize_t ist30xx_calib_show(struct device *dev, struct device_attribute *attr,
455                            char *buf)
456 {
457         ist30xx_disable_irq(ts_data);
458
459         ist30xx_reset(false);
460
461         ist30xx_calibrate(1);
462
463         ist30xx_start(ts_data);
464
465         return 0;
466 }
467
468 /* sysfs: /sys/class/touch/sys/clb_result */
469 ssize_t ist30xx_calib_result_show(struct device *dev,
470                                   struct device_attribute *attr, char *buf)
471 {
472         int ret;
473         int count = 0;
474         u32 value;
475
476         mutex_lock(&ist30xx_mutex);
477         ist30xx_disable_irq(ts_data);
478         ist30xx_reset(false);
479
480         ret = ist30xx_read_cmd(ts_data->client, CMD_GET_CALIB_RESULT, &value);
481         if (unlikely(ret)) {
482                 count = sprintf(buf, "Error Read Calibration Result\n");
483                 goto calib_show_end;
484         }
485
486         count = sprintf(buf,
487                         "Calibration Status : %d, Max raw gap : %d - (raw: %08x)\n",
488                         CALIB_TO_STATUS(value), CALIB_TO_GAP(value), value);
489
490 calib_show_end:
491         ist30xx_start(ts_data);
492         ist30xx_enable_irq(ts_data);
493         mutex_unlock(&ist30xx_mutex);
494
495         return count;
496 }
497
498 /* sysfs: /sys/class/touch/sys/power_on */
499 ssize_t ist30xx_power_on_show(struct device *dev, struct device_attribute *attr,
500                               char *buf)
501 {
502         tsp_info("Power enable: %d\n", true);
503
504         mutex_lock(&ist30xx_mutex);
505         ist30xx_internal_resume(ts_data);
506         ist30xx_enable_irq(ts_data);
507         mutex_unlock(&ist30xx_mutex);
508
509         ist30xx_start(ts_data);
510
511         return 0;
512 }
513
514 /* sysfs: /sys/class/touch/sys/power_off */
515 ssize_t ist30xx_power_off_show(struct device *dev,
516                                struct device_attribute *attr, char *buf)
517 {
518         tsp_info("Power enable: %d\n", false);
519
520         mutex_lock(&ist30xx_mutex);
521         ist30xx_disable_irq(ts_data);
522         ist30xx_internal_suspend(ts_data);
523         mutex_unlock(&ist30xx_mutex);
524
525         return 0;
526 }
527
528 extern int ist30xx_max_error_cnt;
529 /* sysfs: /sys/class/touch/sys/errcnt */
530 ssize_t ist30xx_errcnt_store(struct device *dev, struct device_attribute *attr,
531                              const char *buf, size_t size)
532 {
533         int err_cnt;
534
535         sscanf(buf, "%d", &err_cnt);
536
537         if (unlikely(err_cnt < 0))
538                 return size;
539
540         tsp_info("Request reset error count: %d\n", err_cnt);
541
542         ist30xx_max_error_cnt = err_cnt;
543
544         return size;
545 }
546
547 #if IST30XX_EVENT_MODE
548 extern int ist30xx_max_scan_retry;
549 /* sysfs: /sys/class/touch/sys/scancnt */
550 ssize_t ist30xx_scancnt_store(struct device *dev, struct device_attribute *attr,
551                               const char *buf, size_t size)
552 {
553         int retry;
554
555         sscanf(buf, "%d", &retry);
556
557         if (unlikely(retry < 0))
558                 return size;
559
560         tsp_info("Timer scan count retry: %d\n", retry);
561
562         ist30xx_max_scan_retry = retry;
563
564         return size;
565 }
566
567 extern int timer_period_ms;
568 /* sysfs: /sys/class/touch/sys/timerms */
569 ssize_t ist30xx_timerms_store(struct device *dev, struct device_attribute *attr,
570                               const char *buf, size_t size)
571 {
572         int ms;
573
574         sscanf(buf, "%d", &ms);
575
576         if (unlikely((ms < 0) || (ms > 10000)))
577                 return size;
578
579         tsp_info("Timer period ms: %dms\n", ms);
580
581         timer_period_ms = ms;
582
583         return size;
584 }
585 #endif
586
587 extern int ist30xx_dbg_level;
588 /* sysfs: /sys/class/touch/sys/printk */
589 ssize_t ist30xx_printk_store(struct device *dev, struct device_attribute *attr,
590                              const char *buf, size_t size)
591 {
592         int level;
593
594         sscanf(buf, "%d", &level);
595
596         if (unlikely((level < DEV_ERR) || (level > DEV_VERB)))
597                 return size;
598
599         tsp_info("prink log level: %d\n", level);
600
601         ist30xx_dbg_level = level;
602
603         return size;
604 }
605
606 ssize_t ist30xx_printk_show(struct device *dev, struct device_attribute *attr,
607                             char *buf)
608 {
609         return sprintf(buf, "prink log level: %d\n", ist30xx_dbg_level);
610 }
611
612 /* sysfs: /sys/class/touch/sys/printk5 */
613 ssize_t ist30xx_printk5_show(struct device *dev, struct device_attribute *attr,
614                              char *buf)
615 {
616         tsp_info("prink log level:%d\n", DEV_DEBUG);
617
618         ist30xx_dbg_level = DEV_DEBUG;
619
620         return 0;
621 }
622
623 /* sysfs: /sys/class/touch/sys/printk6 */
624 ssize_t ist30xx_printk6_show(struct device *dev, struct device_attribute *attr,
625                              char *buf)
626 {
627         tsp_info("prink log level:%d\n", DEV_VERB);
628
629         ist30xx_dbg_level = DEV_VERB;
630
631         return 0;
632 }
633
634 /* sysfs: /sys/class/touch/sys/max_touch */
635 ssize_t ist30xx_touch_store(struct device *dev, struct device_attribute *attr,
636                             const char *buf, size_t size)
637 {
638         int finger;
639         int key;
640
641         sscanf(buf, "%d %d", &finger, &key);
642
643         tsp_info("fingers : %d, keys : %d\n", finger, key);
644
645         ts_data->max_fingers = finger;
646         ts_data->max_keys = key;
647
648         return size;
649 }
650
651 extern void ist30xx_scheduled_reset(void);
652 extern int ist30xx_report_rate;
653 /* sysfs: /sys/class/touch/sys/touch_rate */
654 ssize_t ist30xx_touch_rate_store(struct device *dev,
655                                  struct device_attribute *attr, const char *buf, size_t size)
656 {
657         int rate;
658
659         sscanf(buf, "%d", &rate);               // us
660
661         if (unlikely(rate > 0xFFFF))            // over 65.5ms
662                 return size;
663
664         tsp_info("touch reporting rate: %d\n", rate);
665
666         ist30xx_report_rate = rate;
667
668         ist30xx_scheduled_reset();
669
670         return size;
671 }
672
673 extern int ist30xx_idle_rate;
674 /* sysfs: /sys/class/touch/sys/idle_rate */
675 ssize_t ist30xx_idle_scan_rate_store(struct device *dev,
676                                      struct device_attribute *attr, const char *buf, size_t size)
677 {
678         int rate;
679
680         sscanf(buf, "%d", &rate);               // us
681
682         if (unlikely(rate > 0xFFFF))            // over 65.5ms
683                 return size;
684
685         tsp_info("touch idle scan rate: %d\n", rate);
686
687         ist30xx_idle_rate = rate;
688
689         ist30xx_scheduled_reset();
690
691         return size;
692 }
693
694 extern void ist30xx_set_ta_mode(bool charging);
695 /* sysfs: /sys/class/touch/sys/mode_ta */
696 ssize_t ist30xx_ta_mode_store(struct device *dev, struct device_attribute *attr,
697                               const char *buf, size_t size)
698 {
699         int mode;
700
701         sscanf(buf, "%d", &mode);
702
703         if (unlikely((mode != 0) && (mode != 1)))   // enable/disable
704                 return size;
705
706         ist30xx_set_ta_mode(mode);
707
708         return size;
709 }
710
711 extern void ist30xx_set_call_mode(int mode);
712 /* sysfs: /sys/class/touch/sys/mode_call */
713 ssize_t ist30xx_call_mode_store(struct device *dev,
714                                 struct device_attribute *attr, const char *buf, size_t size)
715 {
716         int mode;
717
718         sscanf(buf, "%d", &mode);
719
720         if (unlikely((mode != 0) && (mode != 1)))   // enable/disable
721                 return size;
722
723         ist30xx_set_call_mode(mode);
724
725         return size;
726 }
727
728 extern void ist30xx_set_cover_mode(int mode);
729 /* sysfs: /sys/class/touch/sys/mode_cover */
730 ssize_t ist30xx_cover_mode_store(struct device *dev,
731                                  struct device_attribute *attr, const char *buf, size_t size)
732 {
733         int mode;
734
735         sscanf(buf, "%d", &mode);
736
737         if (unlikely((mode != 0) && (mode != 1)))   // enable/disable
738                 return size;
739
740         ist30xx_set_cover_mode(mode);
741
742         return size;
743 }
744
745 #define TUNES_CMD_WRITE         (1)
746 #define TUNES_CMD_READ          (2)
747 #define TUNES_CMD_REG_ENTER     (3)
748 #define TUNES_CMD_REG_EXIT      (4)
749 #define TUNES_CMD_UPDATE_PARAM  (5)
750 #define TUNES_CMD_UPDATE_FW     (6)
751
752 #define DIRECT_ADDR(n)          (IST30XXB_DA_ADDR(n))
753 #define DIRECT_CMD_WRITE        ('w')
754 #define DIRECT_CMD_READ         ('r')
755
756 #pragma pack(1)
757 typedef struct {
758         u8      cmd;
759         u32     addr;
760         u16     len;
761 } TUNES_INFO;
762 #pragma pack()
763 #pragma pack(1)
764 typedef struct {
765         char    cmd;
766         u32     addr;
767         u32     val;
768 } DIRECT_INFO;
769 #pragma pack()
770
771 static TUNES_INFO ist30xx_tunes;
772 static DIRECT_INFO ist30xx_direct;
773 static bool tunes_cmd_done = false;
774 static bool ist30xx_reg_mode = false;
775
776 /* sysfs: /sys/class/touch/sys/direct */
777 ssize_t ist30xxb_direct_store(struct device *dev, struct device_attribute *attr,
778                               const char *buf, size_t size)
779 {
780         int ret = -EPERM;
781         DIRECT_INFO *direct = (DIRECT_INFO *)&ist30xx_direct;
782
783         sscanf(buf, "%c %x %x", &direct->cmd, &direct->addr, &direct->val);
784
785         tsp_debug("Direct cmd: %c, addr: %x, val: %x\n",
786                   direct->cmd, direct->addr, direct->val);
787
788         if (unlikely((direct->cmd != DIRECT_CMD_WRITE) &&
789                      (direct->cmd != DIRECT_CMD_READ))) {
790                 tsp_warn("Direct cmd is not correct!\n");
791                 return size;
792         }
793
794         if (ist30xx_intr_wait(30) < 0)
795                 return size;
796
797         ts_data->status.event_mode = false;
798         if (direct->cmd == DIRECT_CMD_WRITE) {
799                 ret = ist30xx_write_cmd(ts_data->client, DIRECT_ADDR(direct->addr),
800                                         direct->val);
801                 ret = ist30xx_read_cmd(ts_data->client, DIRECT_ADDR(direct->addr),
802                                        &direct->val);
803                 tsp_debug("Direct write addr: %x, val: %x\n",
804                           direct->addr, direct->val);
805         }
806         ts_data->status.event_mode = true;
807
808         return size;
809 }
810
811 #define DIRECT_BUF_COUNT        (4)
812 ssize_t ist30xxb_direct_show(struct device *dev, struct device_attribute *attr,
813                              char *buf)
814 {
815         int i, ret, count = 0;
816         int len;
817         u32 addr;
818         u32 buf32[DIRECT_BUF_COUNT];
819         int max_len = DIRECT_BUF_COUNT;
820         const int msg_len = 256;
821         char msg[msg_len];
822
823         DIRECT_INFO *direct = (DIRECT_INFO *)&ist30xx_direct;
824
825         if (unlikely(direct->cmd != DIRECT_CMD_READ))
826                 return sprintf(buf, "ex) echo r addr len > direct\n");
827
828         len = direct->val;
829         addr = DIRECT_ADDR(direct->addr);
830
831         if (ist30xx_intr_wait(30) < 0)
832                 return 0;
833         ts_data->status.event_mode = false;
834         while (len > 0) {
835                 if (len < max_len) max_len = len;
836
837                 memset(buf32, 0, sizeof(buf32));
838                 ret = ist30xxb_burst_read(ts_data->client, addr, buf32, max_len);
839                 if (unlikely(ret)) {
840                         count = sprintf(buf, "I2C Burst read fail, addr: %x\n", addr);
841                         break;
842                 }
843
844                 for (i = 0; i < max_len; i++) {
845                         count += snprintf(msg, msg_len, "0x%08x ", buf32[i]);
846                         strncat(buf, msg, msg_len);
847                 }
848                 count += snprintf(msg, msg_len, "\n");
849                 strncat(buf, msg, msg_len);
850
851                 addr += max_len * IST30XX_DATA_LEN;
852                 len -= max_len;
853         }
854         ts_data->status.event_mode = true;
855
856         tsp_debug("%s", buf);
857
858         return count;
859 }
860
861 /* sysfs: /sys/class/touch/tunes/node_info */
862 ssize_t tunes_node_info_show(struct device *dev,
863                                 struct device_attribute *attr, char *buf)
864 {
865         int size;
866         TSP_INFO *tsp = &ist30xx_tsp_info;
867         TKEY_INFO *tkey = &ist30xx_tkey_info;
868
869         size = sprintf(buf, "%d %d %d %d %d %d %d %d %d %d %d %d %d ",
870             ts_data->chip_id, tsp->dir.swap_xy, tsp->ch_num.tx, tsp->ch_num.rx, 
871             tsp->baseline, tkey->axis_rx, tkey->enable, tkey->baseline,
872             tkey->ch_num[0], tkey->ch_num[1], 
873             tkey->ch_num[2], tkey->ch_num[3], tkey->ch_num[4]);
874
875         return size;
876 }
877
878 /* sysfs: /sys/class/touch/tunes/regcmd */
879 ssize_t tunes_regcmd_store(struct device *dev, struct device_attribute *attr,
880                            const char *buf, size_t size)
881 {
882         int ret = -1;
883         u32 *buf32;
884
885         memcpy(&ist30xx_tunes, buf, sizeof(ist30xx_tunes));
886         buf += sizeof(ist30xx_tunes);
887         buf32 = (u32 *)buf;
888
889         tunes_cmd_done = false;
890
891         switch (ist30xx_tunes.cmd) {
892         case TUNES_CMD_WRITE:
893                 break;
894         case TUNES_CMD_READ:
895                 break;
896         case TUNES_CMD_REG_ENTER:
897                 ist30xx_disable_irq(ts_data);
898                 ist30xx_reset(false);
899
900                 /* enter reg access mode */
901                 ret = ist30xx_cmd_reg(ts_data->client, CMD_ENTER_REG_ACCESS);
902                 if (unlikely(ret))
903                         goto regcmd_fail;
904
905                 ist30xx_reg_mode = true;
906
907                 break;
908         case TUNES_CMD_REG_EXIT:
909                 /* exit reg access mode */
910                 ret = ist30xx_cmd_reg(ts_data->client, CMD_EXIT_REG_ACCESS);
911                 if (unlikely(ret))
912                         goto regcmd_fail;
913
914                 ret = ist30xx_cmd_start_scan(ts_data->client);
915                 if (unlikely(ret))
916                         goto regcmd_fail;
917
918                 ist30xx_reg_mode = false;
919
920                 ist30xx_enable_irq(ts_data);
921                 break;
922         default:
923                 ist30xx_enable_irq(ts_data);
924                 return size;
925         }
926         tunes_cmd_done = true;
927
928         return size;
929
930 regcmd_fail:
931         tsp_err("Tunes regcmd i2c_fail, ret=%d\n", ret);
932         return size;
933 }
934
935 ssize_t tunes_regcmd_show(struct device *dev, struct device_attribute *attr,
936                           char *buf)
937 {
938         int size;
939
940         size = sprintf(buf, "cmd: 0x%02x, addr: 0x%08x, len: 0x%04x\n",
941                        ist30xx_tunes.cmd, ist30xx_tunes.addr, ist30xx_tunes.len);
942
943         return size;
944 }
945
946 #define MAX_WRITE_LEN   (1)
947 /* sysfs: /sys/class/touch/tunes/reg */
948 ssize_t tunes_reg_store(struct device *dev, struct device_attribute *attr,
949                         const char *buf, size_t size)
950 {
951         int ret;
952         u32 *buf32 = (u32 *)buf;
953         int waddr, wcnt = 0, len = 0;
954
955         if (unlikely(ist30xx_tunes.cmd != TUNES_CMD_WRITE)) {
956                 tsp_err("error, IST30XX_REG_CMD is not correct!\n");
957                 return size;
958         }
959
960         if (unlikely(!ist30xx_reg_mode)) {
961                 tsp_err("error, IST30XX_REG_CMD is not ready!\n");
962                 return size;
963         }
964
965         if (unlikely(!tunes_cmd_done)) {
966                 tsp_err("error, IST30XX_REG_CMD is not ready!\n");
967                 return size;
968         }
969
970         waddr = ist30xx_tunes.addr;
971         if (ist30xx_tunes.len >= MAX_WRITE_LEN)
972                 len = MAX_WRITE_LEN;
973         else
974                 len = ist30xx_tunes.len;
975
976         while (wcnt < ist30xx_tunes.len) {
977                 ret = ist30xx_write_buf(ts_data->client, waddr, buf32, len);
978                 if (unlikely(ret)) {
979                         tsp_err("Tunes regstore i2c_fail, ret=%d\n", ret);
980                         return size;
981                 }
982
983                 wcnt += len;
984
985                 if ((ist30xx_tunes.len - wcnt) < MAX_WRITE_LEN)
986                         len = ist30xx_tunes.len - wcnt;
987
988                 buf32 += MAX_WRITE_LEN;
989                 waddr += MAX_WRITE_LEN * IST30XX_DATA_LEN;
990         }
991
992         tunes_cmd_done = false;
993
994         return size;
995 }
996
997 ssize_t tunes_reg_show(struct device *dev, struct device_attribute *attr,
998                        char *buf)
999 {
1000         int ret;
1001         int size;
1002         u32 *buf32 = (u32 *)buf;
1003
1004 #if I2C_MONOPOLY_MODE
1005         unsigned long flags;
1006 #endif
1007
1008         if (unlikely(ist30xx_tunes.cmd != TUNES_CMD_READ)) {
1009                 tsp_err("error, IST30XX_REG_CMD is not correct!\n");
1010                 return 0;
1011         }
1012
1013         if (unlikely(!tunes_cmd_done)) {
1014                 tsp_err("error, IST30XX_REG_CMD is not ready!\n");
1015                 return 0;
1016         }
1017
1018         size = ist30xx_tunes.len;
1019         ret = ist30xx_write_cmd(ts_data->client, IST30XX_RX_CNT_ADDR, size);
1020         if (unlikely(ret)) {
1021                 tsp_err("Tunes regshow i2c_fail, ret=%d\n", ret);
1022                 return 0;
1023         }
1024
1025 #if I2C_MONOPOLY_MODE
1026         local_irq_save(flags);          // Activated only when the GPIO I2C is used
1027 #endif
1028         ret = ist30xx_read_buf(ts_data->client, ist30xx_tunes.addr, buf32, size);
1029 #if I2C_MONOPOLY_MODE
1030         local_irq_restore(flags);       // Activated only when the GPIO I2C is used
1031 #endif
1032         if (unlikely(ret)) {
1033                 tsp_err("Tunes regshow i2c_fail, ret=%d\n", ret);
1034                 return size;
1035         }
1036
1037         size = ist30xx_tunes.len * IST30XX_DATA_LEN;
1038
1039         tunes_cmd_done = false;
1040
1041         return size;
1042 }
1043
1044 /* sysfs: /sys/class/touch/tunes/adb */
1045 ssize_t tunes_adb_store(struct device *dev, struct device_attribute *attr,
1046                         const char *buf, size_t size)
1047 {
1048         int ret;
1049         char *tmp, *ptr;
1050         char token[9];
1051         u32 cmd, addr, len, val;
1052         int write_len;
1053
1054         sscanf(buf, "%x %x %x", &cmd, &addr, &len);
1055
1056         switch (cmd) {
1057         case TUNES_CMD_WRITE:   /* write cmd */
1058                 write_len = 0;
1059                 ptr = (char *)(buf + 15);
1060
1061                 while (write_len < len) {
1062                         memcpy(token, ptr, 8);
1063                         token[8] = 0;
1064                         val = simple_strtoul(token, &tmp, 16);
1065                         ret = ist30xx_write_buf(ts_data->client, addr, &val, 1);
1066                         if (unlikely(ret)) {
1067                                 tsp_err("Tunes regstore i2c_fail, ret=%d\n", ret);
1068                                 return size;
1069                         }
1070
1071                         ptr += 8;
1072                         write_len++;
1073                         addr += 4;
1074                 }
1075                 break;
1076
1077         case TUNES_CMD_READ:   /* read cmd */
1078                 ist30xx_tunes.cmd = cmd;
1079                 ist30xx_tunes.addr = addr;
1080                 ist30xx_tunes.len = len;
1081                 break;
1082
1083         case TUNES_CMD_REG_ENTER:   /* enter */
1084                 ist30xx_disable_irq(ts_data);
1085                 ist30xx_reset(false);
1086
1087                 ret = ist30xx_cmd_reg(ts_data->client, CMD_ENTER_REG_ACCESS);
1088                 if (unlikely(ret < 0))
1089                         goto cmd_fail;
1090                 ist30xx_reg_mode = true;
1091                 break;
1092
1093         case TUNES_CMD_REG_EXIT:   /* exit */
1094                 if (ist30xx_reg_mode == true) {
1095                         ret = ist30xx_cmd_reg(ts_data->client, CMD_EXIT_REG_ACCESS);
1096                         if (unlikely(ret < 0))
1097                                 goto cmd_fail;
1098
1099                         ret = ist30xx_cmd_start_scan(ts_data->client);
1100                         if (unlikely(ret < 0))
1101                                 goto cmd_fail;
1102                         ist30xx_reg_mode = false;
1103                         ist30xx_enable_irq(ts_data);
1104                 }
1105                 break;
1106
1107         default:
1108                 break;
1109         }
1110
1111         return size;
1112
1113 cmd_fail:
1114         tsp_err("Tunes adb i2c_fail\n");
1115         return size;
1116 }
1117
1118 ssize_t tunes_adb_show(struct device *dev, struct device_attribute *attr,
1119                        char *buf)
1120 {
1121         int ret;
1122         int i, len, size = 0;
1123         char reg_val[10];
1124
1125 #if I2C_MONOPOLY_MODE
1126         unsigned long flags;
1127 #endif
1128
1129         ret = ist30xx_write_cmd(ts_data->client, IST30XX_RX_CNT_ADDR,
1130                                 ist30xx_tunes.len);
1131         if (unlikely(ret)) {
1132                 tsp_err("Tunes adbshow i2c_fail, ret=%d\n", ret);
1133                 return size;
1134         }
1135
1136 #if I2C_MONOPOLY_MODE
1137         local_irq_save(flags);
1138 #endif
1139         ret = ist30xx_read_buf(ts_data->client, ist30xx_tunes.addr,
1140                                ist30xx_frame_buf, ist30xx_tunes.len);
1141 #if I2C_MONOPOLY_MODE
1142         local_irq_restore(flags);
1143 #endif
1144         if (unlikely(ret)) {
1145                 tsp_err("Tunes adbshow i2c_fail, ret=%d\n", ret);
1146                 return size;
1147         }
1148
1149         size = 0;
1150         buf[0] = 0;
1151         len = sprintf(reg_val, "%08x", ist30xx_tunes.addr);
1152         strcat(buf, reg_val);
1153         size += len;
1154         for (i = 0; i < ist30xx_tunes.len; i++) {
1155                 len = sprintf(reg_val, "%08x", ist30xx_frame_buf[i]);
1156                 strcat(buf, reg_val);
1157                 size += len;
1158         }
1159
1160         return size;
1161 }
1162
1163 #if IST30XX_ALGORITHM_MODE
1164 /* sysfs: /sys/class/touch/tunes/algorithm */
1165 extern u32 ist30xx_algr_addr, ist30xx_algr_size;
1166 ssize_t ist30xx_algr_store(struct device *dev, struct device_attribute *attr,
1167                            const char *buf, size_t size)
1168 {
1169         sscanf(buf, "%x %d", &ist30xx_algr_addr, &ist30xx_algr_size);
1170     
1171         tsp_info("Algorithm addr: 0x%x, count: %d\n",
1172                  ist30xx_algr_addr, ist30xx_algr_size);
1173
1174         ist30xx_algr_addr |= IST30XXB_ACCESS_ADDR;
1175
1176         return size;
1177 }
1178
1179 ssize_t ist30xx_algr_show(struct device *dev, struct device_attribute *attr,
1180                           char *buf)
1181 {
1182         int ret;
1183         u32 algr_addr;
1184         int count = 0;
1185
1186         ret = ist30xx_read_cmd(ts_data->client, IST30XXB_MEM_ALGORITHM, &algr_addr);
1187         if (unlikely(ret)) {
1188                 tsp_warn("Algorithm mem addr read fail!\n");
1189                 return 0;
1190         }
1191
1192         tsp_info("algr_addr(0x%x): 0x%x\n", IST30XXB_MEM_ALGORITHM, algr_addr);
1193
1194         count = sprintf(buf, "Algorithm addr : 0x%x\n", algr_addr);
1195
1196         return count;
1197 }
1198 #endif // IST30XX_ALGORITHM_MODE
1199
1200 /* sysfs: /sys/class/touch/tunes/intr_debug */
1201 extern u32 intr_debug_addr, intr_debug_size;
1202 ssize_t intr_debug_store(struct device *dev, struct device_attribute *attr,
1203                          const char *buf, size_t size)
1204 {
1205         sscanf(buf, "%x %d", &intr_debug_addr, &intr_debug_size);
1206         tsp_info("Interrupt debug addr: 0x%x, count: %d\n",
1207                  intr_debug_addr, intr_debug_size);
1208
1209         intr_debug_addr |= IST30XXB_ACCESS_ADDR;
1210
1211         return size;
1212 }
1213
1214 ssize_t intr_debug_show(struct device *dev, struct device_attribute *attr,
1215                         char *buf)
1216 {
1217         int count = 0;
1218
1219         tsp_info("intr_debug_addr(0x%x): %d\n", intr_debug_addr, intr_debug_size);
1220
1221         count = sprintf(buf, "intr_debug_addr(0x%x): %d\n",
1222                         intr_debug_addr, intr_debug_size);
1223
1224         return count;
1225 }
1226
1227 /* sysfs: /sys/class/touch/tunes/intr_debug2 */
1228 extern u32 intr_debug2_addr, intr_debug2_size;
1229 ssize_t intr_debug2_store(struct device *dev, struct device_attribute *attr,
1230                          const char *buf, size_t size)
1231 {
1232         sscanf(buf, "%x %d", &intr_debug2_addr, &intr_debug2_size);
1233         tsp_info("Interrupt debug2 addr: 0x%x, count: %d\n",
1234                     intr_debug2_addr, intr_debug2_size);
1235
1236         intr_debug2_addr |= IST30XXB_ACCESS_ADDR;
1237
1238         return size;
1239 }
1240
1241 ssize_t intr_debug2_show(struct device *dev, struct device_attribute *attr,
1242                         char *buf)
1243 {
1244         int count = 0;
1245
1246         tsp_info("intr_debug2_addr(0x%x): %d\n", 
1247             intr_debug2_addr, intr_debug2_size);
1248
1249         count = sprintf(buf, "intr_debug2_addr(0x%x): %d\n",
1250                         intr_debug2_addr, intr_debug2_size);
1251
1252         return count;
1253 }
1254
1255 /* sysfs: /sys/class/touch/tunes/intr_debug3 */
1256 extern u32 intr_debug3_addr, intr_debug3_size;
1257 ssize_t intr_debug3_store(struct device *dev, struct device_attribute *attr,
1258                          const char *buf, size_t size)
1259 {
1260         sscanf(buf, "%x %d", &intr_debug3_addr, &intr_debug3_size);
1261         tsp_info("Interrupt debug3 addr: 0x%x, count: %d\n",
1262                     intr_debug3_addr, intr_debug3_size);
1263
1264         intr_debug3_addr |= IST30XXB_ACCESS_ADDR;
1265
1266         return size;
1267 }
1268
1269 ssize_t intr_debug3_show(struct device *dev, struct device_attribute *attr,
1270                         char *buf)
1271 {
1272         int count = 0;
1273
1274         tsp_info("intr_debug3_addr(0x%x): %d\n", 
1275             intr_debug3_addr, intr_debug3_size);
1276
1277         count = sprintf(buf, "intr_debug3_addr(0x%x): %d\n",
1278                         intr_debug3_addr, intr_debug3_size);
1279
1280         return count;
1281 }
1282
1283 #if IST30XX_CHKIC
1284 u8 *ts_chkic_bin = NULL;
1285 u32 ts_chkic_bin_size = 0;
1286 u32 ts_chkic_result = 0;
1287
1288 int chkic_ms_delay = IST30XX_CHKIC_WAIT;
1289 int ist30xx_chkic_wait(void)
1290 {
1291         int cnt = chkic_ms_delay;
1292
1293         ts_data->status.chkic_msg = 0;
1294         while (cnt-- > 0) {
1295                 msleep(100);
1296
1297                 if (ts_data->status.chkic_msg) {
1298                         if (ts_data->status.chkic_msg == IST30XX_CHKIC_END)
1299                                 return 0;
1300                         else
1301                                 return -EAGAIN;
1302                 }
1303         }
1304         tsp_warn("Fault test time out\n");
1305
1306         return -EPERM;
1307 }
1308
1309 #define chkic_next_step(ret)   { if (unlikely(ret)) goto end; msleep(5); }
1310 int ist30xx_chkic_test(const u8 *buf, int size)
1311 {
1312         int ret;
1313         int len;
1314         u32 chksum = 0;
1315         u32 chkic_chksum = 0;
1316         u32 *buf32;
1317         struct i2c_client *client = (struct i2c_client *)ts_data->client;
1318
1319         tsp_info("*** Check IC ***\n");
1320
1321         /* Result initial */
1322         ts_chkic_result = 0;
1323
1324         /* Parse chkicsum */
1325         chkic_chksum = *((u32 *)&buf[size - IST30XX_CHECKSUM_SIZE]);
1326         tsp_verb("Read binary info - chksum: 0x%08x size: 0x%x\n",
1327                  chkic_chksum, size);
1328
1329         ist30xx_disable_irq(ts_data);
1330
1331         ist30xx_reset(false);
1332
1333         /* Load IC chkic test code */
1334         ret = ist30xx_write_cmd(client, CMD_EXEC_MEM_CODE, 0);
1335         chkic_next_step(ret);
1336
1337         buf32 = (u32 *)buf;
1338         len = size / IST30XX_DATA_LEN;
1339         tsp_verb("%08x %08x %08x %08x\n", buf32[0], buf32[1], buf32[2], buf32[3]);
1340         ret = ist30xx_write_buf(client, len, buf32, len);
1341         chkic_next_step(ret);
1342
1343         /* Check load end */
1344         ret = ist30xx_read_cmd(client, CMD_DEFAULT, &chksum);
1345         chkic_next_step(ret);
1346         if (chksum != IST30XX_CHKIC_LOAD_END)
1347                 goto end;
1348         tsp_info("Check ic code ready!!\n");
1349
1350         /* Check chksum */
1351         ret = ist30xx_read_cmd(client, CMD_DEFAULT, &chksum);
1352         chkic_next_step(ret);
1353         tsp_info("Check ic chksum: %08x, %08x\n", chksum, chkic_chksum);
1354
1355         ist30xx_enable_irq(ts_data);
1356         /* Wait IC chkic result */
1357         if (ist30xx_chkic_wait() != 0)
1358                 tsp_info("Check ic timeout!!\n");
1359
1360         ist30xx_disable_irq(ts_data);
1361
1362         /* Read IC chkic result */
1363         ret = ist30xx_read_cmd(client, CMD_DEFAULT, &ts_chkic_result);
1364         chkic_next_step(ret);
1365         tsp_info("Read result value: %08x\n", ts_chkic_result);
1366
1367         ist30xx_reset(false);
1368
1369         ist30xx_start(ts_data);
1370
1371 end:
1372         if (unlikely(ret)) {
1373                 tsp_warn("Check ic error, ret=%d\n", ret);
1374         } else if (unlikely(chksum != chkic_chksum)) {
1375                 tsp_warn("Chksum error, chksum=%x(%x)\n", chksum, chkic_chksum);
1376                 ret = -ENOEXEC;
1377         }
1378
1379         ist30xx_enable_irq(ts_data);
1380
1381         return ret;
1382 }
1383
1384 /* sysfs: /sys/class/touch/chkic/chkic */
1385 static ssize_t ist30xx_chkic_show(struct device *dev,
1386                                   struct device_attribute *attr, char *buf)
1387 {
1388         int ret;
1389
1390         if ((ts_chkic_bin == NULL) || (ts_chkic_bin_size == 0))
1391                 return sprintf(buf, "Binary is not correct(%d)\n", ts_chkic_bin_size);
1392
1393         mutex_lock(&ist30xx_mutex);
1394         ret = ist30xx_chkic_test(ts_chkic_bin, ts_chkic_bin_size);
1395         mutex_unlock(&ist30xx_mutex);
1396
1397         if (ts_chkic_result == IST30XX_CHKIC_OK)
1398                 return sprintf(buf, "Check IC : OK");
1399         else if (ts_chkic_result == IST30XX_CHKIC_FAIL)
1400                 return sprintf(buf, "Check IC : FAIL");
1401         else
1402                 return sprintf(buf, "Check IC : ERROR");
1403 }
1404 #endif
1405
1406 /* sysfs : node */
1407 static DEVICE_ATTR(refresh, S_IRWXUGO, ist30xx_frame_refresh, NULL);
1408 static DEVICE_ATTR(nocp, S_IRWXUGO, ist30xx_frame_nocp, NULL);
1409 static DEVICE_ATTR(filter, S_IRWXUGO, ist30xx_filter_show, NULL);
1410 static DEVICE_ATTR(raw, S_IRWXUGO, ist30xx_raw_show, NULL);
1411 static DEVICE_ATTR(base, S_IRWXUGO, ist30xx_base_show, NULL);
1412 static DEVICE_ATTR(diff, S_IRWXUGO, ist30xx_diff_show, NULL);
1413
1414 /* sysfs : sys */
1415 static DEVICE_ATTR(printk, S_IRWXUGO,
1416                    ist30xx_printk_show, ist30xx_printk_store);
1417 static DEVICE_ATTR(printk5, S_IRWXUGO, ist30xx_printk5_show, NULL);
1418 static DEVICE_ATTR(printk6, S_IRWXUGO, ist30xx_printk6_show, NULL);
1419 static DEVICE_ATTR(direct, S_IRWXUGO,
1420                    ist30xxb_direct_show, ist30xxb_direct_store);
1421 static DEVICE_ATTR(clb_time, S_IRWXUGO, NULL, ist30xx_calib_time_store);
1422 static DEVICE_ATTR(clb, S_IRWXUGO, ist30xx_calib_show, NULL);
1423 static DEVICE_ATTR(clb_result, S_IRWXUGO, ist30xx_calib_result_show, NULL);
1424 static DEVICE_ATTR(tsp_power_on, S_IRWXUGO, ist30xx_power_on_show, NULL);
1425 static DEVICE_ATTR(tsp_power_off, S_IRWXUGO, ist30xx_power_off_show, NULL);
1426 static DEVICE_ATTR(errcnt, S_IRWXUGO, NULL, ist30xx_errcnt_store);
1427 #if IST30XX_EVENT_MODE
1428 static DEVICE_ATTR(scancnt, S_IRWXUGO, NULL, ist30xx_scancnt_store);
1429 static DEVICE_ATTR(timerms, S_IRWXUGO, NULL, ist30xx_timerms_store);
1430 #endif
1431 static DEVICE_ATTR(report_rate, S_IRWXUGO, NULL, ist30xx_touch_rate_store);
1432 static DEVICE_ATTR(idle_rate, S_IRWXUGO, NULL, ist30xx_idle_scan_rate_store);
1433 static DEVICE_ATTR(mode_ta, S_IRWXUGO, NULL, ist30xx_ta_mode_store);
1434 static DEVICE_ATTR(mode_call, S_IRWXUGO, NULL, ist30xx_call_mode_store);
1435 static DEVICE_ATTR(mode_cover, S_IRWXUGO, NULL, ist30xx_cover_mode_store);
1436 static DEVICE_ATTR(max_touch, S_IRWXUGO, NULL, ist30xx_touch_store);
1437
1438 /* sysfs : tunes */
1439 static DEVICE_ATTR(node_info, S_IRWXUGO, tunes_node_info_show, NULL);
1440 static DEVICE_ATTR(regcmd, S_IRWXUGO, tunes_regcmd_show, tunes_regcmd_store);
1441 static DEVICE_ATTR(reg, S_IRWXUGO, tunes_reg_show, tunes_reg_store);
1442 static DEVICE_ATTR(adb, S_IRWXUGO, tunes_adb_show, tunes_adb_store);
1443 #if IST30XX_ALGORITHM_MODE
1444 static DEVICE_ATTR(algorithm, S_IRWXUGO, ist30xx_algr_show, ist30xx_algr_store);
1445 #endif
1446 static DEVICE_ATTR(intr_debug, S_IRWXUGO, intr_debug_show, intr_debug_store);
1447 static DEVICE_ATTR(intr_debug2, S_IRWXUGO, intr_debug2_show, intr_debug2_store);
1448 static DEVICE_ATTR(intr_debug3, S_IRWXUGO, intr_debug3_show, intr_debug3_store);
1449
1450 #if IST30XX_CHKIC
1451 /* sysfs : chkic */
1452 static DEVICE_ATTR(chkic, S_IRUGO, ist30xx_chkic_show, NULL);
1453 #endif
1454
1455 static struct attribute *node_attributes[] = {
1456         &dev_attr_refresh.attr,
1457         &dev_attr_nocp.attr,
1458         &dev_attr_filter.attr,
1459         &dev_attr_raw.attr,
1460         &dev_attr_base.attr,
1461         &dev_attr_diff.attr,
1462         NULL,
1463 };
1464
1465 static struct attribute *sys_attributes[] = {
1466         &dev_attr_printk.attr,
1467         &dev_attr_printk5.attr,
1468         &dev_attr_printk6.attr,
1469         &dev_attr_direct.attr,
1470         &dev_attr_clb_time.attr,
1471         &dev_attr_clb.attr,
1472         &dev_attr_clb_result.attr,
1473         &dev_attr_tsp_power_on.attr,
1474         &dev_attr_tsp_power_off.attr,
1475         &dev_attr_errcnt.attr,
1476 #if IST30XX_EVENT_MODE
1477         &dev_attr_scancnt.attr,
1478         &dev_attr_timerms.attr,
1479 #endif
1480         &dev_attr_report_rate.attr,
1481         &dev_attr_idle_rate.attr,
1482         &dev_attr_mode_ta.attr,
1483         &dev_attr_mode_call.attr,
1484         &dev_attr_mode_cover.attr,
1485         &dev_attr_max_touch.attr,
1486         NULL,
1487 };
1488
1489 static struct attribute *tunes_attributes[] = {
1490         &dev_attr_node_info.attr,
1491         &dev_attr_regcmd.attr,
1492         &dev_attr_reg.attr,
1493         &dev_attr_adb.attr,
1494 #if IST30XX_ALGORITHM_MODE
1495         &dev_attr_algorithm.attr,
1496 #endif
1497         &dev_attr_intr_debug.attr,
1498     &dev_attr_intr_debug2.attr,
1499     &dev_attr_intr_debug3.attr,
1500         NULL,
1501 };
1502
1503 #if IST30XX_CHKIC
1504 static struct attribute *chkic_attributes[] = {
1505         &dev_attr_chkic.attr,
1506         NULL,
1507 };
1508 #endif
1509
1510 static struct attribute_group node_attr_group = {
1511         .attrs  = node_attributes,
1512 };
1513
1514 static struct attribute_group sys_attr_group = {
1515         .attrs  = sys_attributes,
1516 };
1517
1518 static struct attribute_group tunes_attr_group = {
1519         .attrs  = tunes_attributes,
1520 };
1521
1522 #if IST30XX_CHKIC
1523 static struct attribute_group chkic_attr_group = {
1524         .attrs  = chkic_attributes,
1525 };
1526 #endif
1527
1528 extern struct class *ist30xx_class;
1529 struct device *ist30xx_sys_dev;
1530 struct device *ist30xx_tunes_dev;
1531 struct device *ist30xx_node_dev;
1532 #if IST30XX_CHKIC
1533 struct device *ist30xx_chkic_dev;
1534 #endif
1535
1536 int ist30xx_init_misc_sysfs(void)
1537 {
1538         /* /sys/class/touch/sys */
1539         ist30xx_sys_dev = device_create(ist30xx_class, NULL, 0, NULL, "sys");
1540
1541         /* /sys/class/touch/sys/... */
1542         if (unlikely(sysfs_create_group(&ist30xx_sys_dev->kobj,
1543                                         &sys_attr_group)))
1544                 tsp_err("Failed to create sysfs group(%s)!\n", "sys");
1545
1546         /* /sys/class/touch/tunes */
1547         ist30xx_tunes_dev = device_create(ist30xx_class, NULL, 0, NULL, "tunes");
1548
1549         /* /sys/class/touch/tunes/... */
1550         if (unlikely(sysfs_create_group(&ist30xx_tunes_dev->kobj,
1551                                         &tunes_attr_group)))
1552                 tsp_err("Failed to create sysfs group(%s)!\n", "tunes");
1553
1554         /* /sys/class/touch/node */
1555         ist30xx_node_dev = device_create(ist30xx_class, NULL, 0, NULL, "node");
1556
1557         /* /sys/class/touch/node/... */
1558         if (unlikely(sysfs_create_group(&ist30xx_node_dev->kobj,
1559                                         &node_attr_group)))
1560                 tsp_err("Failed to create sysfs group(%s)!\n", "node");
1561
1562 #if IST30XX_CHKIC
1563         /* /sys/class/touch/chkic */
1564         ist30xx_chkic_dev = device_create(ist30xx_class, NULL, 0, NULL, "chkic");
1565
1566         /* /sys/class/touch/chkic/... */
1567         if (unlikely(sysfs_create_group(&ist30xx_chkic_dev->kobj,
1568                                         &chkic_attr_group)))
1569                 tsp_err("Failed to create sysfs group(%s)!\n", "chkic");
1570
1571         ts_chkic_bin = (u8 *)ist30xxb_chkic;
1572         ts_chkic_bin_size = sizeof(ist30xxb_chkic);
1573 #endif
1574
1575         ist30xx_frame_buf = kmalloc(4096, GFP_KERNEL);
1576         ist30xx_frame_rawbuf = kmalloc(4096, GFP_KERNEL);
1577         ist30xx_frame_fltbuf = kmalloc(4096, GFP_KERNEL);
1578         if (!ist30xx_frame_buf || !ist30xx_frame_rawbuf || !ist30xx_frame_fltbuf)
1579                 return -ENOMEM;
1580
1581         return 0;
1582 }