summaryrefstree
path: root/kernel/drivers/sensorhub/brcm/sx9360.c
blob: 2902a59a76c20f4d245f6b8fa310ffe34ec8c3d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
/*
 * Copyright (C) 2018 Semtech Corporation. All rights reserved.
 *
 * Copyright (C) 2013 Samsung Electronics. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/wakelock.h>
#include <linux/interrupt.h>
#include <linux/regulator/consumer.h>
#include <linux/power_supply.h>
#include "sx9360_reg.h"
#ifdef CONFIG_CCIC_NOTIFIER
#include <linux/ccic/ccic_notifier.h>
#endif
#if defined(CONFIG_USB_TYPEC_MANAGER_NOTIFIER)
#include <linux/usb/manager/usb_typec_manager_notifier.h>
#endif

#ifdef CONFIG_VBUS_NOTIFIER
#include <linux/vbus_notifier.h>
#endif

#define VENDOR_NAME              "SEMTECH"
#define MODEL_NAME               "SX9360"
#define MODULE_NAME              "grip_sensor"

#define I2C_M_WR                 0 /* for i2c Write */
#define I2c_M_RD                 1 /* for i2c Read */

#define IDLE                     0
#define ACTIVE                   1

#define SX9360_MODE_SLEEP        0
#define SX9360_MODE_NORMAL       1

#define DIFF_READ_NUM            10
#define GRIP_LOG_TIME            15 /* 30 sec */

/* CS Main */
#define ENABLE_CSX               0x03

#define CSX_STATUS_REG           SX9360_STAT_PROXSTAT_FLAG

#define IRQ_PROCESS_CONDITION   (SX9360_IRQSTAT_TOUCH_FLAG	\
				| SX9360_IRQSTAT_RELEASE_FLAG	\
				| SX9360_IRQSTAT_COMPDONE_FLAG)

#if defined(CONFIG_FOLDER_HALL)
#define HALLIC_PATH		"/sys/class/sec/sec_flip/flipStatus"
#else
#define HALLIC_PATH		"/sys/class/sec/hall_ic/hall_detect"
#endif

struct sx9360_p {
	struct i2c_client *client;
	struct input_dev *input;
	struct device *factory_device;
	struct delayed_work init_work;
	struct delayed_work irq_work;
	struct delayed_work debug_work;
	struct wake_lock grip_wake_lock;
	struct mutex mode_mutex;
	struct mutex read_mutex;
#ifdef CONFIG_CCIC_NOTIFIER
	struct notifier_block ccic_nb;
#endif
#ifdef CONFIG_VBUS_NOTIFIER
	struct notifier_block vbus_nb;
#endif

	bool skip_data;

	int irq;
	int gpio_nirq;
	int state;
	int init_done;

	atomic_t enable;

	int again_m;
	int dgain_m;

	u16 detect_threshold;
	u16 offset;
	s32 capMain;
	s32 useful;
	s16 avg;
	s16 diff;

	s16 diff_avg;
	int diff_cnt;
	s32 useful_avg;

	int irq_count;
	int abnormal_mode;
	s16 max_diff;
	s16 max_normal_diff;

	int debug_count;
	char hall_ic[6];
#if !defined(CONFIG_SEC_FACTORY) && defined(CONFIG_SUPPORT_MCC_THRESHOLD_CHANGE)
	int mcc;
	u8 default_threshold;
	u8 mcc_threshold;
#endif	
};

static int sx9360_check_hallic_state(char *file_path, char hall_ic_status[])
{
	int iRet = 0;
	mm_segment_t old_fs;
	struct file *filep;
	char hall_sysfs[5];

	old_fs = get_fs();
	set_fs(KERNEL_DS);

	filep = filp_open(file_path, O_RDONLY, 0440);
	if (IS_ERR(filep)) {
		iRet = PTR_ERR(filep);
		pr_err("[SX9360]: %s - file open fail %d\n", __func__, iRet);
		set_fs(old_fs);
		return iRet;
	}

	iRet = filep->f_op->read(filep, hall_sysfs,
		sizeof(hall_sysfs), &filep->f_pos);

	if (iRet <= 0) {
		pr_err("[SX9360]: %s - file read fail %d\n", __func__, iRet);
		filp_close(filep, current->files);
		set_fs(old_fs);
		return -EIO;
	} else {
		strncpy(hall_ic_status, hall_sysfs, sizeof(hall_sysfs));
	}

	filp_close(filep, current->files);
	set_fs(old_fs);

	return iRet;
}

static int sx9360_get_nirq_state(struct sx9360_p *data)
{
	return gpio_get_value_cansleep(data->gpio_nirq);
}

static int sx9360_i2c_write(struct sx9360_p *data, u8 reg_addr, u8 buf)
{
	int ret;
	struct i2c_msg msg;
	unsigned char w_buf[2];

	w_buf[0] = reg_addr;
	w_buf[1] = buf;

	msg.addr = data->client->addr;
	msg.flags = I2C_M_WR;
	msg.len = 2;
	msg.buf = (char *)w_buf;

	ret = i2c_transfer(data->client->adapter, &msg, 1);
	if (ret < 0)
		pr_err("[SX9360]: %s - i2c write error %d\n", __func__, ret);

	return 0;
}

static int sx9360_i2c_read(struct sx9360_p *data, u8 reg_addr, u8 *buf)
{
	int ret;
	struct i2c_msg msg[2];

	msg[0].addr = data->client->addr;
	msg[0].flags = I2C_M_WR;
	msg[0].len = 1;
	msg[0].buf = &reg_addr;

	msg[1].addr = data->client->addr;
	msg[1].flags = I2C_M_RD;
	msg[1].len = 1;
	msg[1].buf = buf;

	ret = i2c_transfer(data->client->adapter, msg, 2);
	if (ret < 0)
		pr_err("[SX9360]: %s - i2c read error %d\n", __func__, ret);

	return ret;
}

static u8 sx9360_read_irqstate(struct sx9360_p *data)
{
	u8 val = 0;

	if (sx9360_i2c_read(data, SX9360_IRQSTAT_REG, &val) >= 0)
		return (val & 0xFF);

	return 0;
}

static void sx9360_initialize_register(struct sx9360_p *data)
{
	u8 val = 0;
	int idx;

	for (idx = 0; idx < (sizeof(setup_reg) >> 1); idx++) {		
		sx9360_i2c_write(data, setup_reg[idx].reg, setup_reg[idx].val);
		pr_info("[SX9360]: %s - Write Reg: 0x%x Value: 0x%x\n",
			__func__, setup_reg[idx].reg, setup_reg[idx].val);

		sx9360_i2c_read(data, setup_reg[idx].reg, &val);
		pr_info("[SX9360]: %s - Read Reg: 0x%x Value: 0x%x\n\n",
			__func__, setup_reg[idx].reg, val);
	}

	sx9360_i2c_read(data, SX9360_PROXCTRL5_REG, &val);
	data->detect_threshold = (u16)val * (u16)val / 2;

	sx9360_i2c_read(data, SX9360_PROXCTRL4_REG, &val);
	val = (val & 0x30) >> 4;

	if(val)
		data->detect_threshold += data->detect_threshold >> (5 - val);

	pr_info("[SX9360]: %s - detect threshold: %u\n", __func__, data->detect_threshold);

	data->init_done = ON;
}

static void sx9360_initialize_chip(struct sx9360_p *data)
{
	int cnt = 0;

	while((sx9360_get_nirq_state(data) == 0) && (cnt++ < 10)) {
		sx9360_read_irqstate(data);
		msleep(20);
	}

	if (cnt >= 10)
		pr_err("[SX9360]: %s - s/w reset fail(%d)\n", __func__, cnt);

	sx9360_initialize_register(data);
}

static int sx9360_set_offset_calibration(struct sx9360_p *data)
{
	int ret = 0;

	pr_info("[SX9360]: %s\n", __func__);
	ret = sx9360_i2c_write(data, SX9360_STAT_REG,
                                 SX9360_STAT_COMPSTAT_ALL_FLAG);

	return ret;
}

static void sx9360_send_event(struct sx9360_p *data, u8 state)
{
	if (data->skip_data == true) {
		pr_info("[SX9360]: %s - skip grip event\n", __func__);
		return;
	}

	if (state == ACTIVE) {
		data->state = ACTIVE;
		pr_info("[SX9360]: %s - touched\n", __func__);
	} else {
		data->state = IDLE;
		pr_info("[SX9360]: %s - released\n", __func__);
	}

	if (state == ACTIVE)
		input_report_rel(data->input, REL_MISC, 1);
	else
		input_report_rel(data->input, REL_MISC, 2);

	input_sync(data->input);
}

static void sx9360_display_data_reg(struct sx9360_p *data)
{
	u8 val, reg;

	pr_info("[SX9360]: ############# %d reference #############\n", 0);
	for (reg = SX9360_REGUSEMSBPHR; reg <= SX9360_REGOFFSETLSBPHR; reg++)
	{
		sx9360_i2c_read(data, reg, &val);
		pr_info("[SX9360]: %s - Register(0x%2x) data(0x%2x)\n",
			__func__, reg, val);
	}
	pr_info("[SX9360]: ############# %d Main #############\n", 0);
	for (reg = SX9360_REGUSEMSBPHM; reg <= SX9360_REGOFFSETLSBPHM; reg++)
	{
		sx9360_i2c_read(data, reg, &val);
		pr_info("[SX9360]: %s - Register(0x%2x) data(0x%2x)\n",
			__func__, reg, val);
	}
}

static void sx9360_get_gain(struct sx9360_p *data)
{
	u8 msByte;
	static const int again_phm[]={7500,22500,37500,52500,60000,75000,90000,105000};  
	sx9360_i2c_read(data, SX9360_AFEPARAM1PHM_REG, &msByte);
	msByte=(msByte>>4) & 0x07;
	data->again_m = again_phm[msByte];
	
	sx9360_i2c_read(data, SX9360_PROXCTRL0PHM_REG, &msByte);
	msByte=(msByte>>3) & 0x07;
	if(msByte)
		data->dgain_m = 1 << (msByte-1);
	else
		data->dgain_m = 1;
}


static void sx9360_get_data(struct sx9360_p *data)
{
	u8 msByte = 0;
	u8 lsByte = 0;
	u16 offset = 0;
	s32 capMain = 0, useful = 0;
	s16 avg = 0, diff = 0;
	s16 retry = 0;
	u8 convstat = 0;

	mutex_lock(&data->read_mutex);

	sx9360_get_gain(data);

	while(1)
	{
		sx9360_i2c_read(data, SX9360_STAT_REG, &convstat);
		convstat &= 0x01;

		if(++retry > 5 || convstat == 0)
			break;

		usleep_range(10000, 11000);
	}
	pr_info("[SX9360]: %s retry : %d, CONVSTAT : %u\n", __func__, retry, convstat);

	/* diff read */
	sx9360_i2c_read(data, SX9360_REGDIFFMSBPHM, &msByte);
	sx9360_i2c_read(data, SX9360_REGDIFFLSBPHM, &lsByte);

	diff = (s16)msByte;
	diff = (diff << 8) | ((s16)lsByte);

	/* Calculate out the Main Cap information */
	sx9360_i2c_read(data, SX9360_REGUSEMSBPHM, &msByte);
	sx9360_i2c_read(data, SX9360_REGUSELSBPHM, &lsByte);

	useful = (s32)msByte;
	useful = (useful << 8) | ((s32)lsByte);
	if (useful > 32767)
		useful -= 65536;

	sx9360_i2c_read(data, SX9360_REGOFFSETMSBPHM, &msByte);
	sx9360_i2c_read(data, SX9360_REGOFFSETLSBPHM, &lsByte);

	offset = (u16)msByte;
	offset = (offset << 8) | ((u16)lsByte);

	msByte = (u8)((offset >> 7) & 0x7F);
	lsByte = (u8)((offset)      & 0x7F);

	capMain = (((s32)msByte * 30000) + ((s32)lsByte * 500)) +
            		(s32)(((s64)useful * data->again_m) / (data->dgain_m * 32768));

	/* avg read */
	sx9360_i2c_read(data, SX9360_REGAVGMSBPHM, &msByte);
	sx9360_i2c_read(data, SX9360_REGAVGLSBPHM, &lsByte);

	avg = (s16)msByte;
	avg = (avg << 8) | ((s16)lsByte);

	data->useful = useful;
	data->offset = offset;
	data->capMain = capMain;
	data->avg = avg;
	data->diff = diff;

	mutex_unlock(&data->read_mutex);

	pr_info("[SX9360]: %s - capMain: %ld, useful: %ld, avg: %d, diff: %d, Offset: %u\n",
		__func__, (long int)capMain, (long int)useful, avg, diff, offset);
}

static int sx9360_set_mode(struct sx9360_p *data, unsigned char mode)
{
	int ret = -EINVAL;

	pr_info("[SX9360]: %s %u\n", __func__, mode);

	mutex_lock(&data->mode_mutex);
	if (mode == SX9360_MODE_SLEEP) {
		ret = sx9360_i2c_write(data, SX9360_GNRLCTRL0_REG, SX9360_GNRLCTRL0_VAL_PHOFF);
	} else if (mode == SX9360_MODE_NORMAL) {
		ret = sx9360_i2c_write(data, SX9360_GNRLCTRL0_REG,
			SX9360_GNRLCTRL0_VAL_PHOFF | ENABLE_CSX);
		msleep(20);

		sx9360_set_offset_calibration(data);
		msleep(450);
	}

	pr_info("[SX9360]: %s - change the mode : %u\n", __func__, mode);

	mutex_unlock(&data->mode_mutex);
	return ret;
}

static void sx9360_check_status(struct sx9360_p *data)
{
	u8 status = 0;	

	sx9360_i2c_read(data, SX9360_STAT_REG, &status);

	pr_info("[SX9360]: %s - (status: 0x%x)\n", __func__, status);

	if (data->skip_data == true) {
		input_report_rel(data->input, REL_MISC, 2);
		input_sync(data->input);
		return;
	}

	if ((status & CSX_STATUS_REG) && (data->diff > data->detect_threshold)) {
		sx9360_send_event(data, ACTIVE);
	} else {
		sx9360_send_event(data, IDLE);
	}
}

static void sx9360_set_enable(struct sx9360_p *data, int enable)
{
	int pre_enable = atomic_read(&data->enable);

	pr_info("[SX9360]: %s %d\n", __func__, enable);

	if (enable) {
		if (pre_enable == OFF) {
			data->diff_avg = 0;
			data->diff_cnt = 0;
			data->useful_avg = 0;
			sx9360_get_data(data);
			sx9360_check_status(data);

			msleep(20);

			/* make sure no interrupts are pending since enabling irq
			 * will only work on next falling edge */
			sx9360_read_irqstate(data);

			/* enable interrupt */
			sx9360_i2c_write(data, SX9360_IRQ_ENABLE_REG, 0x0E);

			enable_irq(data->irq);
			enable_irq_wake(data->irq);
			
			atomic_set(&data->enable, ON);
		}
	} else {
		if (pre_enable == ON) {
			/* disable interrupt */
			sx9360_i2c_write(data, SX9360_IRQ_ENABLE_REG, 0x00);

			disable_irq(data->irq);
			disable_irq_wake(data->irq);

			atomic_set(&data->enable, OFF);
		}
	}
}

static void sx9360_set_debug_work(struct sx9360_p *data, u8 enable,
		unsigned int time_ms)
{
	if (enable == ON) {
		data->debug_count = 0;
		schedule_delayed_work(&data->debug_work,
			msecs_to_jiffies(time_ms));
	} else {
		cancel_delayed_work_sync(&data->debug_work);
	}
}

static ssize_t sx9360_get_offset_calibration_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	u8 val = 0;
	struct sx9360_p *data = dev_get_drvdata(dev);

	sx9360_i2c_read(data, SX9360_IRQSTAT_REG, &val);

	return snprintf(buf, PAGE_SIZE, "%d\n", val);
}

static ssize_t sx9360_set_offset_calibration_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	unsigned long val;
	struct sx9360_p *data = dev_get_drvdata(dev);

	if (kstrtoul(buf, 10, &val)) {
		pr_err("[SX9360]: %s - Invalid Argument\n", __func__);
		return -EINVAL;
	}

	if (val)
		sx9360_set_offset_calibration(data);

	return count;
}

static ssize_t sx9360_register_write_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	int regist = 0, val = 0;
	struct sx9360_p *data = dev_get_drvdata(dev);

	if (sscanf(buf, "%2x,%2x", &regist, &val) != 2) {
		pr_err("[SX9360]: %s - The number of data are wrong\n",
			__func__);
		return -EINVAL;
	}

	sx9360_i2c_write(data, (unsigned char)regist, (unsigned char)val);
	pr_info("[SX9360]: %s - Register(0x%2x) data(0x%2x)\n",
		__func__, regist, val);

	return count;
}

static ssize_t sx9360_register_read_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	u8 val = 0;
	int offset = 0, idx = 0;
	struct sx9360_p *data = dev_get_drvdata(dev);

	for (idx = 0; idx < (int)(ARRAY_SIZE(setup_reg)); idx++) {
		sx9360_i2c_read(data, setup_reg[idx].reg, &val);
		pr_info("[SX9360]: %s - Read Reg: 0x%x Value: 0x%x\n\n",
			__func__, setup_reg[idx].reg, val);

		offset += snprintf(buf + offset, PAGE_SIZE - offset,
		"Reg: 0x%x Value: 0x%08x\n", setup_reg[idx].reg, val);
	}

	return offset;
}

static ssize_t sx9360_read_data_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);

	sx9360_display_data_reg(data);

	return snprintf(buf, PAGE_SIZE, "%d\n", 0);
}

static ssize_t sx9360_sw_reset_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);

	pr_info("[SX9360]: %s\n", __func__);
	sx9360_set_offset_calibration(data);
	msleep(450);
	sx9360_get_data(data);

	return snprintf(buf, PAGE_SIZE, "%d\n", 0);
}

static ssize_t sx9360_vendor_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
}

static ssize_t sx9360_name_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%s\n", MODEL_NAME);
}

static ssize_t sx9360_touch_mode_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "1\n");
}

static ssize_t sx9360_raw_data_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	static s32 sum_diff, sum_useful;
	struct sx9360_p *data = dev_get_drvdata(dev);

	sx9360_get_data(data);

	if (data->diff_cnt == 0) {
		sum_diff = (s32)data->diff;
		sum_useful = data->useful;
	}
	else {
		sum_diff += (s32)data->diff;
		sum_useful += data->useful;
	}

	if (++data->diff_cnt >= DIFF_READ_NUM) {
		data->diff_avg = (s16)(sum_diff / DIFF_READ_NUM);
		data->useful_avg = sum_useful / DIFF_READ_NUM;
		data->diff_cnt = 0;
	}

	return snprintf(buf, PAGE_SIZE, "%ld,%ld,%u,%d,%d\n", (long int)data->capMain,
		(long int)data->useful, data->offset, data->diff, data->avg);
}

static ssize_t sx9360_diff_avg_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%d\n", data->diff_avg);
}

static ssize_t sx9360_useful_avg_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%ld\n", (long int)data->useful_avg);
}

static ssize_t sx9360_avgnegfilt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	u8 avgnegfilt = 0;

	sx9360_i2c_read(data, SX9360_PROXCTRL3_REG, &avgnegfilt);

	avgnegfilt = (avgnegfilt & 0x38) >> 3;

	if (avgnegfilt == 7)
		return snprintf(buf, PAGE_SIZE, "1\n");
	else if (avgnegfilt > 0 && avgnegfilt < 7)
		return snprintf(buf, PAGE_SIZE, "1-1/%d\n", 1 << avgnegfilt);
	else if (avgnegfilt == 0)
		return snprintf(buf, PAGE_SIZE, "0\n");

	return snprintf(buf, PAGE_SIZE, "not set\n");
}

static ssize_t sx9360_avgposfilt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	u8 avgposfilt = 0;

	sx9360_i2c_read(data, SX9360_PROXCTRL3_REG, &avgposfilt);
	avgposfilt = avgposfilt & 0x07;

	if (avgposfilt == 7)
		return snprintf(buf, PAGE_SIZE, "1\n");
	else if (avgposfilt > 1 && avgposfilt < 7)
		return snprintf(buf, PAGE_SIZE, "1-1/%d\n", 16 << avgposfilt);
	else if (avgposfilt == 0)
		return snprintf(buf, PAGE_SIZE, "0\n");
	else if (avgposfilt == 1)
		return snprintf(buf, PAGE_SIZE, "1-1/16\n");

	return snprintf(buf, PAGE_SIZE, "not set\n");
}

static ssize_t sx9360_gain_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	u8 gain = 0;

	sx9360_i2c_read(data, SX9360_PROXCTRL0PHM_REG, &gain);
	gain = (gain & 0x38) >> 3;

	if (gain > 0 && gain < 5)
		return snprintf(buf, PAGE_SIZE, "x%u\n", 1 << (gain - 1));

	return snprintf(buf, PAGE_SIZE, "Reserved\n");
}

static ssize_t sx9360_range_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "None\n");
}

static ssize_t sx9360_avgthresh_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	u8 avgthresh = 0;

	sx9360_i2c_read(data, SX9360_PROXCTRL1_REG, &avgthresh);
	avgthresh = avgthresh & 0x3F;

	return snprintf(buf, PAGE_SIZE, "%ld\n", 512 * (long int)avgthresh);
}

static ssize_t sx9360_rawfilt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	u8 rawfilt = 0;

	sx9360_i2c_read(data, SX9360_PROXCTRL0PHM_REG, &rawfilt);
	rawfilt = rawfilt & 0x07;

	if (rawfilt > 0 && rawfilt < 8)
		return snprintf(buf, PAGE_SIZE, "1-1/%d\n", 1 << rawfilt);
	else if (rawfilt == 0)
		return snprintf(buf, PAGE_SIZE, "0\n");

	return snprintf(buf, PAGE_SIZE, "not set\n");
}

static ssize_t sx9360_sampling_freq_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	u8 sampling_freq = 0;
	const char *table[16] = {
		"250", "200", "166.67", "142.86", "125", "100",	"83.33", "71.43",
		"62.50", "50", "41.67", "35.71", "27.78", "20.83", "15.62", "7.81"};

	sx9360_i2c_read(data, SX9360_AFEPARAM1PHM_REG, &sampling_freq);
	sampling_freq = sampling_freq & 0x0F;

	return snprintf(buf, PAGE_SIZE, "%skHz\n", table[sampling_freq]);
}

static ssize_t sx9360_scan_period_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	u8 scan_period = 0;

	sx9360_i2c_read(data, SX9360_GNRLCTRL2_REG, &scan_period);

	return snprintf(buf, PAGE_SIZE, "%ld\n", (long int)(((long int)scan_period << 11) / 1000));
}

static ssize_t sx9360_again_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	const char *table[8] = {
		"+/-0.75", "+/-2.25", "+/-3.75", "+/-5.25",
		"+/-6", "+/-7.5", "+/-9", "+/-10.5"};
	u8 again = 0;

	sx9360_i2c_read(data, SX9360_AFEPARAM1PHM_REG, &again);
	again = (again & 0x70) >> 4;

	return snprintf(buf, PAGE_SIZE, "%spF\n", table[again]);
}

static ssize_t sx9360_phase_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "1\n");
}

static ssize_t sx9360_hysteresis_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	const char *table[4] = {"None", "+/-6%", "+/-12%", "+/-25%"};
	u8 hyst = 0;

	sx9360_i2c_read(data, SX9360_PROXCTRL4_REG, &hyst);
	hyst = (hyst & 0x30) >> 4;

	return snprintf(buf, PAGE_SIZE, "%s\n", table[hyst]);
}

static ssize_t sx9360_resolution_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	u8 resolution = 0;

	sx9360_i2c_read(data, SX9360_AFEPARAM0PHM_REG, &resolution);
	resolution = resolution & 0x7;

	return snprintf(buf, PAGE_SIZE, "%u\n", 1 << (resolution + 3));
}

static ssize_t sx9360_adc_filt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "None\n");
}

static ssize_t sx9360_useful_filt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	u8 useful_filt = 0;

	sx9360_i2c_read(data, SX9360_USEFILTER4_REG, &useful_filt);
	useful_filt = useful_filt & 0x01;

	return snprintf(buf, PAGE_SIZE, "%s\n", useful_filt ? "on" : "off");
}

static ssize_t sx9360_irq_count_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);

	int ret = 0;
	s16 max_diff_val = 0;

	if (data->irq_count) {
		ret = -1;
		max_diff_val = data->max_diff;
	} else {
		max_diff_val = data->max_normal_diff;
	}

	pr_info("[SX9360]: %s - called\n", __func__);

	return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
		ret, data->irq_count, max_diff_val);
}

static ssize_t sx9360_irq_count_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct sx9360_p *data = dev_get_drvdata(dev);

	u8 onoff;
	int ret;

	ret = kstrtou8(buf, 10, &onoff);
	if (ret < 0) {
		pr_err("[SX9360]: %s - kstrtou8 failed.(%d)\n", __func__, ret);
		return count;
	}

	mutex_lock(&data->read_mutex);

	if (onoff == 0) {
		data->abnormal_mode = OFF;
	} else if (onoff == 1) {
		data->abnormal_mode = ON;
		data->irq_count = 0;
		data->max_diff = 0;
		data->max_normal_diff = 0;
	} else {
		pr_err("[SX9360]: %s - unknown value %d\n", __func__, onoff);
	}

	mutex_unlock(&data->read_mutex);

	pr_info("[SX9360]: %s - %d\n", __func__, onoff);

	return count;
}

static ssize_t sx9360_normal_threshold_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	u8 th_buf = 0, hyst = 0;
	u32 threshold = 0;

	sx9360_i2c_read(data, SX9360_PROXCTRL5_REG, &th_buf);
	threshold = (u32)th_buf * (u32)th_buf / 2;

	sx9360_i2c_read(data, SX9360_PROXCTRL4_REG, &hyst);
	hyst = (hyst & 0x30) >> 4;

	switch (hyst) {
	case 0x01: /* 6% */
		hyst = threshold >> 4;
		break;
	case 0x02: /* 12% */
		hyst = threshold >> 3;
		break;
	case 0x03: /* 25% */
		hyst = threshold >> 2;
		break;
	default:
		/* None */
		break;
	}

	return snprintf(buf, PAGE_SIZE, "%lu,%lu\n",
		(u32)threshold + (u32)hyst, (u32)threshold - (u32)hyst);
}

static ssize_t sx9360_onoff_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%u\n", !data->skip_data);
}

static ssize_t sx9360_onoff_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	u8 val;
	int ret;
	struct sx9360_p *data = dev_get_drvdata(dev);

	ret = kstrtou8(buf, 2, &val);
	if (ret) {
		pr_err("[SX9360]: %s - Invalid Argument\n", __func__);
		return ret;
	}

	if (val == 0) {
		data->skip_data = true;
		if (atomic_read(&data->enable) == ON) {
			data->state = IDLE;
			input_report_rel(data->input, REL_MISC, 2);
			input_sync(data->input);
		}		
	} else {
		data->skip_data = false;
	}

	pr_info("[SX9360]: %s -%u\n", __func__, val);
	return count;
}

#if !defined(CONFIG_SEC_FACTORY) && defined(CONFIG_SUPPORT_MCC_THRESHOLD_CHANGE)
static ssize_t sx9360_mcc_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	int ret, mcc;
	u8 threshold;
	struct sx9360_p *data = dev_get_drvdata(dev);

	ret = kstrtoint(buf, 10, &mcc);
	if (ret) {
		pr_err("[SX9360]: %s - Invalid Argument\n", __func__);
		return ret; 		
	}

	data->mcc = mcc;

	pr_info("[SX9360]: %s - mcc value %d\n", __func__, data->mcc);	

	// 001 : call box, 440/441 : jpn, 450 : kor, 460 : chn
	if ((data->mcc != 001) && (data->mcc != 440) && (data->mcc != 441) &&
		(data->mcc != 450) && (data->mcc != 460)) {
		pr_info("[SX9360]: %s - default threshold %u\n", __func__, data->default_threshold);
		threshold = data->default_threshold; /* DEFAULT KOR THRESHOLD > 1912  */
		sx9360_i2c_write(data, SX9360_PROXCTRL5_REG, threshold);
	} else {
		threshold = data->mcc_threshold;
		sx9360_i2c_write(data, SX9360_PROXCTRL5_REG, threshold);
	}

	pr_info("[SX9360]: %s - change threshold %u\n", __func__, threshold);
	setup_reg[SX9360_PROXTHRESH_REG_IDX].val = threshold;

	return count;
}

static ssize_t sx9360_mcc_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%d\n", data->mcc);
}

static DEVICE_ATTR(mcc, S_IRUGO |S_IWUSR | S_IWGRP,
		sx9360_mcc_show, sx9360_mcc_store);
#endif

static DEVICE_ATTR(menual_calibrate, S_IRUGO | S_IWUSR | S_IWGRP,
		sx9360_get_offset_calibration_show,
		sx9360_set_offset_calibration_store);
static DEVICE_ATTR(register_write, S_IWUSR | S_IWGRP,
		NULL, sx9360_register_write_store);
static DEVICE_ATTR(register_read, S_IRUGO, sx9360_register_read_show, NULL);
static DEVICE_ATTR(readback, S_IRUGO, sx9360_read_data_show, NULL);
static DEVICE_ATTR(reset, S_IRUGO, sx9360_sw_reset_show, NULL);

static DEVICE_ATTR(name, S_IRUGO, sx9360_name_show, NULL);
static DEVICE_ATTR(vendor, S_IRUGO, sx9360_vendor_show, NULL);
static DEVICE_ATTR(mode, S_IRUGO, sx9360_touch_mode_show, NULL);
static DEVICE_ATTR(raw_data, S_IRUGO, sx9360_raw_data_show, NULL);
static DEVICE_ATTR(diff_avg, S_IRUGO, sx9360_diff_avg_show, NULL);
static DEVICE_ATTR(useful_avg, S_IRUGO, sx9360_useful_avg_show, NULL);
static DEVICE_ATTR(onoff, S_IRUGO | S_IWUSR | S_IWGRP,
		sx9360_onoff_show, sx9360_onoff_store);
static DEVICE_ATTR(normal_threshold, S_IRUGO,
		sx9360_normal_threshold_show, NULL);

static DEVICE_ATTR(avg_negfilt, S_IRUGO, sx9360_avgnegfilt_show, NULL);
static DEVICE_ATTR(avg_posfilt, S_IRUGO, sx9360_avgposfilt_show, NULL);
static DEVICE_ATTR(avg_thresh, S_IRUGO, sx9360_avgthresh_show, NULL);
static DEVICE_ATTR(rawfilt, S_IRUGO, sx9360_rawfilt_show, NULL);
static DEVICE_ATTR(sampling_freq, S_IRUGO, sx9360_sampling_freq_show, NULL);
static DEVICE_ATTR(scan_period, S_IRUGO, sx9360_scan_period_show, NULL);
static DEVICE_ATTR(gain, S_IRUGO, sx9360_gain_show, NULL);
static DEVICE_ATTR(range, S_IRUGO, sx9360_range_show, NULL);
static DEVICE_ATTR(analog_gain, S_IRUGO, sx9360_again_show, NULL);
static DEVICE_ATTR(phase, S_IRUGO, sx9360_phase_show, NULL);
static DEVICE_ATTR(hysteresis, S_IRUGO, sx9360_hysteresis_show, NULL);
static DEVICE_ATTR(irq_count, S_IRUGO | S_IWUSR | S_IWGRP,
		sx9360_irq_count_show, sx9360_irq_count_store);
static DEVICE_ATTR(resolution, S_IRUGO, sx9360_resolution_show, NULL);
static DEVICE_ATTR(adc_filt, S_IRUGO, sx9360_adc_filt_show, NULL);
static DEVICE_ATTR(useful_filt, S_IRUGO, sx9360_useful_filt_show, NULL);

static struct device_attribute *sensor_attrs[] = {
	&dev_attr_menual_calibrate,
	&dev_attr_register_write,
	&dev_attr_register_read,
	&dev_attr_readback,
	&dev_attr_reset,
	&dev_attr_name,
	&dev_attr_vendor,
	&dev_attr_mode,
	&dev_attr_raw_data,
	&dev_attr_diff_avg,
	&dev_attr_useful_avg,
	&dev_attr_onoff,
	&dev_attr_normal_threshold,
	&dev_attr_avg_negfilt,
	&dev_attr_avg_posfilt,
	&dev_attr_avg_thresh,
	&dev_attr_rawfilt,
	&dev_attr_sampling_freq,
	&dev_attr_scan_period,
	&dev_attr_gain,
	&dev_attr_range,
	&dev_attr_analog_gain,
	&dev_attr_phase,
	&dev_attr_hysteresis,
	&dev_attr_irq_count,
	&dev_attr_resolution,
	&dev_attr_adc_filt,
	&dev_attr_useful_filt,
#if !defined(CONFIG_SEC_FACTORY) && defined(CONFIG_SUPPORT_MCC_THRESHOLD_CHANGE)
	&dev_attr_mcc,
#endif
	NULL,
};

/*****************************************************************************/
static ssize_t sx9360_enable_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	u8 enable;
	int ret;
	struct sx9360_p *data = dev_get_drvdata(dev);

	ret = kstrtou8(buf, 2, &enable);
	if (ret) {
		pr_err("[SX9360]: %s - Invalid Argument\n", __func__);
		return ret;
	}

	pr_info("[SX9360]: %s - new_value = %u\n", __func__, enable);
	if ((enable == 0) || (enable == 1))
		sx9360_set_enable(data, (int)enable);
	return size;
}

static ssize_t sx9360_enable_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9360_p *data = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&data->enable));
}

static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
		sx9360_enable_show, sx9360_enable_store);

static struct attribute *sx9360_attributes[] = {
	&dev_attr_enable.attr,
	NULL
};

static struct attribute_group sx9360_attribute_group = {
	.attrs = sx9360_attributes
};

static void sx9360_touch_process(struct sx9360_p *data)
{
	u8 status = 0;

	sx9360_i2c_read(data, SX9360_STAT_REG, &status);
	pr_info("[SX9360]: %s - 0x%x\n", __func__, status);

	if (data->abnormal_mode) {
		if (status & CSX_STATUS_REG) {
			sx9360_get_data(data);
			if (data->max_diff < data->diff)
				data->max_diff = data->diff;
			data->irq_count++;
		}
	}

	if (data->state == IDLE) {
		if (status & CSX_STATUS_REG)
			sx9360_send_event(data, ACTIVE);
		else
			pr_info("[SX9360]: %s - 0x%x already released.\n",
				__func__, status);
	} else { /* User released button */
		if (!(status & CSX_STATUS_REG)) {
			sx9360_send_event(data, IDLE);
		} else {
			pr_info("[SX9360]: %s - 0x%x still touched\n",
				__func__, status);
		}
	}
}

static void sx9360_process_interrupt(struct sx9360_p *data)
{
	u8 status = 0;

	/* since we are not in an interrupt don't need to disable irq. */
	status = sx9360_read_irqstate(data);

	pr_info("[SX9360]: %s - status %d\n", __func__, status);

	if (status & IRQ_PROCESS_CONDITION)
		sx9360_touch_process(data);
}

static void sx9360_init_work_func(struct work_struct *work)
{
	struct sx9360_p *data = container_of((struct delayed_work *)work,
		struct sx9360_p, init_work);

	int retry = 0;

	sx9360_initialize_chip(data);

	sx9360_set_mode(data, SX9360_MODE_NORMAL);
	/* make sure no interrupts are pending since enabling irq
	 * will only work on next falling edge */
	sx9360_read_irqstate(data);
	msleep(20);

	while(retry++ < 10) {
		sx9360_get_data(data);
		/* Defence code */
		if (data->capMain == 0 && data->avg == 0 && data->diff == 0
			&& data->useful == 0 && data->offset == 0) {
			pr_info("[SX9360]: Defence code for grip sensor - retry: %d\n", retry);

			sx9360_i2c_write(data, SX9360_SOFTRESET_REG, SX9360_SOFTRESET);
			msleep(300);
			sx9360_initialize_chip(data);
			sx9360_set_mode(data, SX9360_MODE_NORMAL);
			sx9360_read_irqstate(data);
			msleep(20);
		} else {
			break;
		}
	}
}

static void sx9360_irq_work_func(struct work_struct *work)
{
	struct sx9360_p *data = container_of((struct delayed_work *)work,
		struct sx9360_p, irq_work);

	if (sx9360_get_nirq_state(data) == 0)
		sx9360_process_interrupt(data);
	else
		pr_err("[SX9360]: %s - nirq read high %d\n",
			__func__, sx9360_get_nirq_state(data));
}

static void sx9360_debug_work_func(struct work_struct *work)
{
	struct sx9360_p *data = container_of((struct delayed_work *)work,
		struct sx9360_p, debug_work);
	static int hall_flag = 1;

#if defined(CONFIG_FOLDER_HALL)
	char str[2] = "0";
#else
	char str[6] = "CLOSE";
#endif

	sx9360_check_hallic_state(HALLIC_PATH, data->hall_ic);

	data->hall_ic[sizeof(str)-1] = '\0';

	if (strcmp(data->hall_ic, str) == 0) {
		if (hall_flag) {
			pr_info("[SX9360]: %s - hall IC is closed\n", __func__);
			sx9360_set_offset_calibration(data);
			hall_flag = 0;
		}
	} else {
		hall_flag = 1;
	}

	if (atomic_read(&data->enable) == ON) {
		if (data->abnormal_mode) {
			sx9360_get_data(data);
			if (data->max_normal_diff < data->diff)
				data->max_normal_diff = data->diff;
		} else {
			if (data->debug_count >= GRIP_LOG_TIME) {
				sx9360_get_data(data);
				data->debug_count = 0;
			} else {
				data->debug_count++;
			}
		}
	}

	schedule_delayed_work(&data->debug_work, msecs_to_jiffies(2000));
}

static irqreturn_t sx9360_interrupt_thread(int irq, void *pdata)
{
	struct sx9360_p *data = pdata;

	wake_lock_timeout(&data->grip_wake_lock, 3 * HZ);
	schedule_delayed_work(&data->irq_work, msecs_to_jiffies(100));

	return IRQ_HANDLED;
}

static int sx9360_input_init(struct sx9360_p *data)
{
	int ret = 0;
	struct input_dev *dev = NULL;

	/* Create the input device */
	dev = input_allocate_device();
	if (!dev)
		return -ENOMEM;

	dev->name = MODULE_NAME;
	dev->id.bustype = BUS_I2C;

	input_set_capability(dev, EV_REL, REL_MISC);
	input_set_drvdata(dev, data);

	ret = input_register_device(dev);
	if (ret < 0) {
		input_free_device(dev);
		return ret;
	}

	ret = sensors_create_symlink(dev);
	if (ret < 0) {
		input_unregister_device(dev);
		return ret;
	}

	ret = sysfs_create_group(&dev->dev.kobj, &sx9360_attribute_group);
	if (ret < 0) {
		sensors_remove_symlink(dev);
		input_unregister_device(dev);
		return ret;
	}

	/* save the input pointer and finish initialization */
	data->input = dev;

	return 0;
}

static int sx9360_setup_pin(struct sx9360_p *data)
{
	int ret;

	ret = gpio_request(data->gpio_nirq, "SX9360_nIRQ");
	if (ret < 0) {
		pr_err("[SX9360]: %s - gpio %d request failed (%d)\n",
			__func__, data->gpio_nirq, ret);
		return ret;
	}

	ret = gpio_direction_input(data->gpio_nirq);
	if (ret < 0) {
		pr_err("[SX9360]: %s - failed to set gpio %d as input (%d)\n",
			__func__, data->gpio_nirq, ret);
		gpio_free(data->gpio_nirq);
		return ret;
	}

	return 0;
}

static void sx9360_initialize_variable(struct sx9360_p *data)
{
	data->init_done = OFF;
	data->skip_data = false;
	data->state = IDLE;

	atomic_set(&data->enable, OFF);
}

static int sx9360_read_setupreg(struct device_node *dnode, char *str, u32 *val)
{
	u32 temp_val;
	int ret;

	ret = of_property_read_u32(dnode, str, &temp_val);

	if (!ret)
		*val = temp_val;
	else
		pr_err("[SX9360]: %s - %s: property read err 0x%2x (%d)\n",
			__func__, str, temp_val, ret);

	return ret;
}

static int sx9360_parse_dt(struct sx9360_p *data, struct device *dev)
{
	struct device_node *dNode = dev->of_node;
	enum of_gpio_flags flags;

	u32 val = 0;

	if (dNode == NULL)
		return -ENODEV;

	data->gpio_nirq = of_get_named_gpio_flags(dNode,
		"sx9360,nirq-gpio", 0, &flags);
	if (data->gpio_nirq < 0) {
		pr_err("[SX9360]: %s - get gpio_nirq error\n", __func__);
		return -ENODEV;
	}

	if (!sx9360_read_setupreg(dNode, SX9360_REFRESOLUTION, &val))
		setup_reg[SX9360_REFRESOLUTION_REG_IDX].val = (u8)val;
	if (!sx9360_read_setupreg(dNode, SX9360_REFAGAINFREQ, &val))
		setup_reg[SX9360_REFAGAINFREQ_REG_IDX].val = (u8)val;
	if (!sx9360_read_setupreg(dNode, SX9360_RESOLUTION, &val))
		setup_reg[SX9360_RESOLUTION_REG_IDX].val = (u8)val;	
	if (!sx9360_read_setupreg(dNode, SX9360_AGAINFREQ, &val))
		setup_reg[SX9360_AGAINFREQ_REG_IDX].val = (u8)val;
	if (!sx9360_read_setupreg(dNode, SX9360_REFGAINRAWFILT, &val))
		setup_reg[SX9360_REFGAINRAWFILT_REG_IDX].val = (u8)val;
	if (!sx9360_read_setupreg(dNode, SX9360_GAINRAWFILT, &val))
		setup_reg[SX9360_GAINRAWFILT_REG_IDX].val = (u8)val;
	if (!sx9360_read_setupreg(dNode, SX9360_HYST, &val))
		setup_reg[SX9360_HYST_REG_IDX].val = (u8)val;
	if (!sx9360_read_setupreg(dNode, SX9360_PROXTHRESH, &val)) {
		setup_reg[SX9360_PROXTHRESH_REG_IDX].val = (u8)val;
#if !defined(CONFIG_SEC_FACTORY) && defined(CONFIG_SUPPORT_MCC_THRESHOLD_CHANGE)
		data->default_threshold = val;
	}
	if (!sx9360_read_setupreg(dNode, SX9360_PROXTHRESH_MCC, &val)) {
		data->mcc_threshold = val;
	} else {
		data->mcc_threshold = data->default_threshold;
#endif
	}

	return 0;
}

#if defined(CONFIG_CCIC_NOTIFIER) && defined(CONFIG_USB_TYPEC_MANAGER_NOTIFIER)
static int sx9360_ccic_handle_notification(struct notifier_block *nb,
		unsigned long action, void *data)
{
	CC_NOTI_USB_STATUS_TYPEDEF usb_status =
		*(CC_NOTI_USB_STATUS_TYPEDEF *) data;
	struct sx9360_p *pdata =
		container_of(nb, struct sx9360_p, ccic_nb);
	static int pre_attach;

	if ((usb_status.drp != USB_STATUS_NOTIFY_ATTACH_DFP) && (usb_status.drp != USB_STATUS_NOTIFY_DETACH))
		return 0;

	if (pre_attach == usb_status.drp)
		return 0;

	if (pdata->init_done == ON) {
		switch (usb_status.drp) {
		case USB_STATUS_NOTIFY_ATTACH_DFP:
		case USB_STATUS_NOTIFY_DETACH:
			pr_info("[SX9360]: %s accept attach = %d\n",
				__func__, usb_status.drp);
			sx9360_set_offset_calibration(pdata);
			break;
		default:
			pr_info("[SX9360]: %s skip attach = %d\n",
				__func__, usb_status.drp);
			break;
		}
	}

	pre_attach = usb_status.drp;

	return 0;
}
#endif
#ifdef CONFIG_VBUS_NOTIFIER
static int sx9360_vbus_handle_notification(struct notifier_block *nb,
		unsigned long action, void *data)
{
	vbus_status_t vbus_type = *(vbus_status_t *) data;
	struct sx9360_p *pdata =
		container_of(nb, struct sx9360_p, vbus_nb);
	static int pre_attach;

	if (pre_attach == vbus_type)
		return 0;

	if (pdata->init_done == ON) {
		switch (vbus_type) {
		case STATUS_VBUS_HIGH:
		case STATUS_VBUS_LOW:
			pr_info("[SX9360]: %s accept attach = %d\n",
				__func__, vbus_type);
			sx9360_set_offset_calibration(pdata);
			break;
		default:
			pr_info("[SX9360]: %s skip attach = %d\n",
				__func__, vbus_type);
			break;
		}
	}

	pre_attach = vbus_type;

	return 0;
}
#endif

static int sx9360_check_chip_id(struct sx9360_p *data)
{
	int ret;
	u8 value = 0;
	
	ret = sx9360_i2c_read(data, SX9360_WHOAMI_REG, &value);
	if (ret < 0) {
		pr_err("[SX9360]: whoami[0x%x] read failed %d\n", value, ret);
		return ret;
	}
	if(value != WHO_AM_I)
	{
		pr_err("[SX9360]: invalid whoami(%x)\n", value);
		return -1;
	}

	return 0;
}

static int sx9360_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	int ret = -ENODEV;
	struct sx9360_p *data = NULL;

	pr_info("[SX9360]: %s - Probe Start!\n", __func__);
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		pr_err("[SX9360]: %s - i2c_check_functionality error\n",
			__func__);
		goto exit;
	}

	/* create memory for main struct */
	data = kzalloc(sizeof(struct sx9360_p), GFP_KERNEL);
	if (data == NULL) {
		pr_err("[SX9360]: %s - kzalloc error\n", __func__);
		ret = -ENOMEM;
		goto exit_kzalloc;
	}

	i2c_set_clientdata(client, data);
	data->client = client;
	data->factory_device = &client->dev;

	ret = sx9360_input_init(data);
	if (ret < 0)
		goto exit_input_init;

	wake_lock_init(&data->grip_wake_lock,
		WAKE_LOCK_SUSPEND, "grip_wake_lock");
	mutex_init(&data->mode_mutex);
	mutex_init(&data->read_mutex);

	ret = sx9360_parse_dt(data, &client->dev);
	if (ret < 0) {
		pr_err("[SX9360]: %s - of_node error\n", __func__);
		ret = -ENODEV;
		goto exit_of_node;
	}

	ret = sx9360_setup_pin(data);
	if (ret) {
		pr_err("[SX9360]: %s - could not setup pin\n", __func__);
		goto exit_setup_pin;
	}

	/* read chip id */
	ret = sx9360_check_chip_id(data);
	if (ret < 0) {
		pr_err("[SX9360]: %s - chip id check failed %d\n", __func__, ret);
		goto exit_chip_reset;
	}

	ret = sx9360_i2c_write(data, SX9360_SOFTRESET_REG, SX9360_SOFTRESET);
	if (ret < 0) {
		pr_err("[SX9360]: %s - chip reset failed %d\n", __func__, ret);
		goto exit_chip_reset;
	}

	sx9360_initialize_variable(data);
	INIT_DELAYED_WORK(&data->init_work, sx9360_init_work_func);
	INIT_DELAYED_WORK(&data->irq_work, sx9360_irq_work_func);
	INIT_DELAYED_WORK(&data->debug_work, sx9360_debug_work_func);

	data->irq = gpio_to_irq(data->gpio_nirq);
	/* initailize interrupt reporting */
	ret = request_threaded_irq(data->irq, NULL, sx9360_interrupt_thread,
			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
			"sx9360_irq", data);
	if (ret < 0) {
		pr_err("[SX9360]: %s - failed to set request_threaded_irq %d"
			" as returning (%d)\n", __func__, data->irq, ret);
		goto exit_request_threaded_irq;
	}
	disable_irq(data->irq);

	ret = sensors_register(data->factory_device,
		data, sensor_attrs, MODULE_NAME);
	if (ret) {
		pr_err("[SX9360] %s - cound not register sensor(%d).\n",
			__func__, ret);
		goto exit_register_failed;
	}

	schedule_delayed_work(&data->init_work, msecs_to_jiffies(300));
	sx9360_set_debug_work(data, ON, 20000);

#if defined(CONFIG_CCIC_NOTIFIER) && defined(CONFIG_USB_TYPEC_MANAGER_NOTIFIER)
	pr_info("[SX9360]: %s - register ccic notifier\n", __func__);
	manager_notifier_register(&data->ccic_nb,
					sx9360_ccic_handle_notification,
					MANAGER_NOTIFY_CCIC_USB);
#endif
#ifdef CONFIG_VBUS_NOTIFIER
	pr_info("[SX9360]: %s - register vbus notifier\n", __func__);
	vbus_notifier_register(&data->vbus_nb, sx9360_vbus_handle_notification,
					VBUS_NOTIFY_DEV_CHARGER);
#endif

	pr_info("[SX9360]: %s - Probe done!\n", __func__);

	return 0;

exit_register_failed:
	free_irq(data->irq, data);
exit_request_threaded_irq:
exit_chip_reset:
	gpio_free(data->gpio_nirq);
exit_setup_pin:
exit_of_node:
	mutex_destroy(&data->mode_mutex);
	mutex_destroy(&data->read_mutex);
	wake_lock_destroy(&data->grip_wake_lock);
	sysfs_remove_group(&data->input->dev.kobj, &sx9360_attribute_group);
	sensors_remove_symlink(data->input);
	input_unregister_device(data->input);
exit_input_init:
	kfree(data);
exit_kzalloc:
exit:
	pr_err("[SX9360]: %s - Probe fail!\n", __func__);
	return ret;
}

static int sx9360_remove(struct i2c_client *client)
{
	struct sx9360_p *data = (struct sx9360_p *)i2c_get_clientdata(client);

	if (atomic_read(&data->enable) == ON)
		sx9360_set_enable(data, OFF);

	sx9360_set_mode(data, SX9360_MODE_SLEEP);

	cancel_delayed_work_sync(&data->init_work);
	cancel_delayed_work_sync(&data->irq_work);
	cancel_delayed_work_sync(&data->debug_work);
	free_irq(data->irq, data);
	gpio_free(data->gpio_nirq);

	wake_lock_destroy(&data->grip_wake_lock);
	sensors_unregister(data->factory_device, sensor_attrs);
	sensors_remove_symlink(data->input);
	sysfs_remove_group(&data->input->dev.kobj, &sx9360_attribute_group);
	input_unregister_device(data->input);
	mutex_destroy(&data->mode_mutex);
	mutex_destroy(&data->read_mutex);

	kfree(data);

	return 0;
}

static int sx9360_suspend(struct device *dev)
{
	struct sx9360_p *data = dev_get_drvdata(dev);
	int cnt = 0;

	pr_info("[SX9360]: %s\n", __func__);
	/* before go to sleep, make the interrupt pin as high*/
	while ((sx9360_get_nirq_state(data) == 0) && (cnt++ < 3)) {
		sx9360_read_irqstate(data);
		msleep(20);
	}
	if (cnt >= 3)
		pr_err("[SX9360]: %s - s/w reset fail(%d)\n", __func__, cnt);

	sx9360_set_debug_work(data, OFF, 1000);

	return 0;
}

static int sx9360_resume(struct device *dev)
{
	struct sx9360_p *data = dev_get_drvdata(dev);

	pr_info("[SX9360]: %s\n", __func__);
	sx9360_set_debug_work(data, ON, 1000);

	return 0;
}

static void sx9360_shutdown(struct i2c_client *client)
{
	struct sx9360_p *data = i2c_get_clientdata(client);

	pr_info("[SX9360]: %s\n", __func__);
	sx9360_set_debug_work(data, OFF, 1000);	
	if (atomic_read(&data->enable) == ON)
		sx9360_set_enable(data, OFF);

	sx9360_set_mode(data, SX9360_MODE_SLEEP);
}

static struct of_device_id sx9360_match_table[] = {
	{ .compatible = "sx9360",},
	{},
};

static const struct i2c_device_id sx9360_id[] = {
	{ "sx9360_match_table", 0 },
	{ }
};

static const struct dev_pm_ops sx9360_pm_ops = {
	.suspend = sx9360_suspend,
	.resume = sx9360_resume,
};

static struct i2c_driver sx9360_driver = {
	.driver = {
		.name	= MODEL_NAME,
		.owner	= THIS_MODULE,
		.of_match_table = sx9360_match_table,
		.pm = &sx9360_pm_ops
	},
	.probe		= sx9360_probe,
	.remove		= sx9360_remove,
	.shutdown	= sx9360_shutdown,
	.id_table	= sx9360_id,
};

static int __init sx9360_init(void)
{
	return i2c_add_driver(&sx9360_driver);
}

static void __exit sx9360_exit(void)
{
	i2c_del_driver(&sx9360_driver);
}

module_init(sx9360_init);
module_exit(sx9360_exit);

MODULE_DESCRIPTION("Semtech Corp. SX9360 Capacitive Touch Controller Driver");
MODULE_AUTHOR("Samsung Electronics");
MODULE_LICENSE("GPL");