summaryrefstree
path: root/kernel/drivers/sensorhub/brcm/sx9320_multi2.c
blob: 548e0b412779c24e1ff7941d77dd38a7b2d26dee (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
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
/*
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#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 "sx9320_reg_multi2.h"
#if defined(CONFIG_MUIC_NOTIFIER)
#include <linux/muic/muic.h>
#include <linux/muic/muic_notifier.h>
#ifdef CONFIG_CCIC_NOTIFIER
#include <linux/ccic/ccic_notifier.h>
#endif
#endif
#if defined(CONFIG_USB_TYPEC_MANAGER_NOTIFIER)
#include <linux/usb/manager/usb_typec_manager_notifier.h>
#endif

#define VENDOR_NAME              "SEMTECH"
#define MODEL_NAME               "SX9320_MULTI2"
#define MODULE_NAME              "grip_sensor_multi2"

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

#define IDLE                     0
#define ACTIVE                   1

#define SX9320_MULTI2_MODE_SLEEP        0
#define SX9320_MULTI2_MODE_NORMAL       1

#define MAIN_SENSOR              1
#define REF_SENSOR               2

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

/* CS0, CS1, CS2, CS3 */
#define TOTAL_BOTTON_COUNT       1
#define ENABLE_CSX               ((1 << MAIN_SENSOR) | (1 << REF_SENSOR))

#define IRQ_PROCESS_CONDITION   (SX9320_MULTI2_IRQSTAT_TOUCH_FLAG \
				| SX9320_MULTI2_IRQSTAT_RELEASE_FLAG)

#define NONE_ENABLE		-1
#define IDLE_STATE		0
#define TOUCH_STATE		1
#define BODY_STATE		2

#define HALLIC1_PATH		"/sys/class/sec/sec_key/hall_detect"

struct sx9320_multi2_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 read_mutex;
#if defined(CONFIG_MUIC_NOTIFIER)
	struct notifier_block cpuidle_muic_nb;
#endif
#if defined(CONFIG_CCIC_NOTIFIER)
	struct notifier_block cpuidle_ccic_nb;
#endif
	bool skip_data;
	bool check_usb;
	u8 normal_th;
	u8 normal_th_buf;
	u8 phen;
	u8 gain;
	u8 again;
	int again_ch;
	u8 scan_period;
	u8 range;
	u8 sampling_freq;
	u8 resolution;
	u8 rawfilt;
	u8 hyst;
	u8 avgposfilt;
	u8 avgnegfilt;
	u8 avgthresh;
	u8 debouncer;
	u8 afeph0;
	u8 afeph1;
	u8 afeph2;
	u8 afeph3;
	int irq;
	int gpio_nirq;
	int state;
	int debug_count;
	int diff_avg;
	int diff_cnt;
	int init_done;
	int irq_count;
	int abnormal_mode;

	s32 capmain;
	s32 useful;
	s32 useful_avg;
	s16 avg;
	s32 diff;
	s32 max_diff;
	s32 max_normal_diff;
	u16 offset;
	u16 freq;

	int ch1_state;
	int ch2_state;

	atomic_t enable;

	unsigned char hall_ic1[6];
	unsigned char hall_ic2[6];
};

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

	old_fs = get_fs();
	set_fs(KERNEL_DS);

	filep = filp_open(file_path, O_RDONLY, 0666);
	if (IS_ERR(filep)) {
		iRet = PTR_ERR(filep);
		set_fs(old_fs);
		goto exit;
	}

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

	if (iRet != sizeof(hall_sysfs))
		iRet = -EIO;
	else
		strncpy(hall_ic_status, hall_sysfs, sizeof(hall_sysfs));

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

exit:
	return iRet;
}

static int sx9320_multi2_get_nirq_state(struct sx9320_multi2_p *data)
{
	return gpio_get_value_cansleep(data->gpio_nirq);
}

static int sx9320_multi2_i2c_write(struct sx9320_multi2_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("[SX9320_MULTI2]: %s - i2c write error %d\n",
			__func__, ret);

	return ret;
}

static int sx9320_multi2_i2c_read(struct sx9320_multi2_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("[SX9320_MULTI2]: %s - i2c read error %d\n",
			__func__, ret);

	return ret;
}

static void sx9320_multi2_get_again(struct sx9320_multi2_p *data)
{
	switch (data->again) {
	case 0x06:
		data->again_ch = 1247;
		break;
	case 0x08:
		data->again_ch = 1000;
		break;
	case 0x0B:
		data->again_ch = 768;
		break;
	case 0x0F:
		data->again_ch = 552;
		break;
	default:
		data->again_ch = 1000;
	}
}

static u8 sx9320_multi2_read_irqstate(struct sx9320_multi2_p *data)
{
	u8 val = 0;

	if (sx9320_multi2_i2c_read(data, SX9320_MULTI2_IRQSTAT_REG, &val) >= 0)
		return val;

	return 0;
}

static void sx9320_multi2_initialize_register(struct sx9320_multi2_p *data)
{
	int idx;

	for (idx = 0; idx < (int)(sizeof(setup_reg) >> 1); idx++)
		sx9320_multi2_i2c_write(data, setup_reg[idx].reg,
			setup_reg[idx].val);

	if (data->phen < 2)
		sx9320_multi2_i2c_write(data, SX9320_MULTI2_PROXCTRL6_REG,
			data->normal_th);
	else
		sx9320_multi2_i2c_write(data, SX9320_MULTI2_PROXCTRL7_REG,
			data->normal_th);

	data->init_done = ON;
}

static void sx9320_multi2_initialize_chip(struct sx9320_multi2_p *data)
{
	int cnt = 0;

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

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

	sx9320_multi2_initialize_register(data);
}

static int sx9320_multi2_set_offset_calibration(struct sx9320_multi2_p *data)
{
	int ret = 0;

	ret = sx9320_multi2_i2c_write(data, SX9320_MULTI2_STAT2_REG, 0x0F);

	return ret;
}

static void sx9320_multi2_send_event(struct sx9320_multi2_p *data, u8 state)
{
	if (state == ACTIVE) {
		data->state = ACTIVE;
		pr_info("[SX9320_MULTI2]: %s - button touched\n", __func__);
	} else {
		data->state = IDLE;
		pr_info("[SX9320_MULTI2]: %s - button released\n", __func__);
	}

	if (data->skip_data == true) {
		pr_info("[SX9320_MULTI2]: %s - skip grip event\n", __func__);
		return;
	}

	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 sx9320_multi2_display_data_reg(struct sx9320_multi2_p *data)
{
	u8 val = 0;
	int idx;

	sx9320_multi2_i2c_write(data, SX9320_MULTI2_REGSENSORSELECT,
		(u8)data->phen);

	for (idx = 0; idx < (int)(sizeof(setup_reg) >> 1); idx++) {
		sx9320_multi2_i2c_read(data, setup_reg[idx].reg, &val);
		pr_info("[SX9320_MULTI2]: %s - Read Reg: 0x%x Value: 0x%x\n\n",
			__func__, setup_reg[idx].reg, val);
	}
}

static void sx9320_multi2_get_data(struct sx9320_multi2_p *data)
{
	u8 ms_byte = 0;
	u8 ls_byte = 0;
	s16 avg = 0;
	u16 offset = 0;
	s32 capMain = 0, useful = 0, range = 0;
	s32 gain, again;

	mutex_lock(&data->read_mutex);

	sx9320_multi2_i2c_write(data, SX9320_MULTI2_REGSENSORSELECT,
		(u8)data->phen);

	/* useful read */
	sx9320_multi2_i2c_read(data, SX9320_MULTI2_REGUSEMSB, &ms_byte);
	sx9320_multi2_i2c_read(data, SX9320_MULTI2_REGUSELSB, &ls_byte);

	useful = (s32)ms_byte;
	useful = (useful << 8) | ((s32)ls_byte);
	if (useful > 32767)
		useful -= 65536;

	/* offset read */
	sx9320_multi2_i2c_read(data, SX9320_MULTI2_REGOFFSETMSB, &ms_byte);
	sx9320_multi2_i2c_read(data, SX9320_MULTI2_REGOFFSETLSB, &ls_byte);

	offset = (u16)ms_byte;
	offset = (offset << 8) | ((u16)ls_byte);

	/* capMain calculate */
	ms_byte = (u8)((offset >> 7) & 0x7F);
	ls_byte = (u8)((offset)      & 0x7F);

	gain = 1 << (data->gain - 1);
	again = data->again_ch;

	/* range small(00) : 1.325 Large(01) : 2.65 */
	if (data->range == 1)
		range = SX9320_MULTI2_LARGE_RANGE_VALUE;
	else
		range = SX9320_MULTI2_SMALL_RANGE_VALUE;

	capMain = (((s32)ms_byte * 21234) + ((s32)ls_byte * 496))
			+ ((useful * range) / ((gain * again * 65536) / 1000));

	/* avg read */
	sx9320_multi2_i2c_read(data, SX9320_MULTI2_REGAVGMSB, &ms_byte);
	sx9320_multi2_i2c_read(data, SX9320_MULTI2_REGAVGLSB, &ls_byte);

	avg = (u16)ms_byte;
	avg = (avg << 8) | ((u16)ls_byte);

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

	mutex_unlock(&data->read_mutex);

	pr_info("[SX9320_MULTI2]: %s - Capmain: %d, Useful: %d, avg: %d, diff: %d, Offset: %u\n",
		__func__, data->capmain, data->useful, data->avg,
		data->diff, data->offset);
}

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

	/* sx9320_multi2 enables the each PHASES(PHEN reg) instead of CSX */
	if (mode == SX9320_MULTI2_MODE_SLEEP) {
		ret = sx9320_multi2_i2c_write(data, SX9320_MULTI2_GNRLCTRL1_REG,
			setup_reg[2].val);
	} else if (mode == SX9320_MULTI2_MODE_NORMAL) {
		ret = sx9320_multi2_i2c_write(data, SX9320_MULTI2_GNRLCTRL1_REG,
			setup_reg[2].val | (1 << data->phen));

		msleep(20);

		sx9320_multi2_set_offset_calibration(data);
		msleep(400);
	}

	pr_info("[SX9320_MULTI2]: %s - change the mode : %u\n", __func__, mode);
	return ret;
}

static void sx9320_multi2_read_ch_interrupt(struct sx9320_multi2_p *data,
	u8 status)
{
	if (status & (PHX_STATUS_REG << data->phen))
		data->ch1_state = TOUCH_STATE;
	else
		data->ch1_state = IDLE_STATE;

	pr_info("[SX9320_MULTI2]: %s - ch1:%d, ch2:%d\n",
		__func__, data->ch1_state, data->ch2_state);
}

static void sx9320_multi2_check_status(struct sx9320_multi2_p *data, int enable)
{
	/* this function has to be modified if chs are over 2 for SAR */
	u8 status = 0;

	sx9320_multi2_i2c_read(data, SX9320_MULTI2_STAT0_REG, &status);

	if (data->skip_data == true) {
		input_report_rel(data->input, REL_MISC, 2);
		input_sync(data->input);
	} else if (status & (PHX_STATUS_REG << data->phen)) {
		sx9320_multi2_send_event(data, ACTIVE);
	} else {
		sx9320_multi2_send_event(data, IDLE);
	}
}

static void sx9320_multi2_set_enable(struct sx9320_multi2_p *data, int enable)
{
	u8 status = 0;

	pr_info("[SX9320_MULTI2]: %s\n", __func__);

	if (enable == ON) {

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

		data->diff_avg = 0;
		data->diff_cnt = 0;
		data->useful_avg = 0;
		sx9320_multi2_get_data(data);
		sx9320_multi2_check_status(data, enable);

		msleep(20);
		/*
		 * make sure no interrupts are pending since enabling irq
		 * will only work on next falling edge
		 */
		sx9320_multi2_read_irqstate(data);
		/*
		 * enable interrupt
		 */
		sx9320_multi2_i2c_write(data, SX9320_MULTI2_IRQ_ENABLE_REG,
			0x70);

		enable_irq(data->irq);
		enable_irq_wake(data->irq);
	} else {
		pr_info("[SX9320_MULTI2]: %s - disable\n", __func__);

		/*
		 * disable interrupt
		 */
		sx9320_multi2_i2c_write(data, SX9320_MULTI2_IRQ_ENABLE_REG,
			0x00);

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

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

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

	sx9320_multi2_i2c_read(data, SX9320_MULTI2_IRQSTAT_REG, &val);

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

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

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

	if (val)
		sx9320_multi2_set_offset_calibration(data);

	return count;
}

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

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

	sx9320_multi2_i2c_write(data, (unsigned char)regist,
		(unsigned char)val);
	pr_info("[SX9320_MULTI2]: %s - Register(0x%2x) data(0x%2x)\n",
		__func__, regist, val);
	return count;
}

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

	for (idx = 0; idx < (int)(sizeof(setup_reg) >> 1); idx++) {
		sx9320_multi2_i2c_read(data, setup_reg[idx].reg, &val);

		pr_info("[SX9320_MULTI2]: %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%x\n", setup_reg[idx].reg, val);
	}

	return offset;
}

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

	sx9320_multi2_display_data_reg(data);

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

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

	pr_info("[SX9320_MULTI2]: %s\n", __func__);
	sx9320_multi2_set_offset_calibration(data);
	msleep(400);
	sx9320_multi2_get_data(data);

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

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

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

	data->freq = (u16)val;
	val = ((val << 3) | (setup_reg[4].val & 0x07)) & 0xff;
	sx9320_multi2_i2c_write(data, SX9320_MULTI2_AFECTRL4_REG, (u8)val);

	pr_info("[SX9320_MULTI2]: %s - Freq : 0x%x\n", __func__, data->freq);

	return count;
}

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

	pr_info("[SX9320_MULTI2]: %s - Freq : 0x%x\n", __func__, data->freq);

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

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

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

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

static ssize_t sx9320_multi2_raw_data_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	static int sum_diff;
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);

	sx9320_multi2_get_data(data);
	if (data->diff_cnt == 0)
		sum_diff = data->diff;
	else
		sum_diff += data->diff;

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

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

static ssize_t sx9320_multi2_threshold_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	/* It's for init touch */
	return snprintf(buf, PAGE_SIZE, "0\n");
}

static ssize_t sx9320_multi2_threshold_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	return count;
}

static ssize_t sx9320_multi2_normal_threshold_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);
	u16 thresh_temp = 0, hysteresis = 0;

	thresh_temp = data->normal_th;
	thresh_temp = thresh_temp * thresh_temp / 2;

	/* AdvCtrl13 */
	hysteresis = (setup_reg[33].val >> 2) & 0x3;

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

	return snprintf(buf, PAGE_SIZE, "%d,%d\n", thresh_temp + hysteresis,
			thresh_temp - hysteresis);
}

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

	/* It's for normal touch */
	if (kstrtoul(buf, 10, &val)) {
		pr_err("[SX9320_MULTI2]: %s - Invalid Argument\n", __func__);
		return count;
	}

	sx9320_multi2_i2c_write(data, SX9320_MULTI2_PROXCTRL7_REG, val);

	pr_info("[SX9320_MULTI2]: %s - normal threshold %lu\n", __func__, val);

	data->normal_th_buf = data->normal_th = (u8)(val);

	return count;
}

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

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

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

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

	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("[SX9320_MULTI2]: %s -%u\n", __func__, val);
	return count;
}

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

static ssize_t sx9320_multi2_calibration_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	return count;
}

static ssize_t sx9320_multi2_gain_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	int ret;
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);

	switch (data->gain) {
	case 0x01:
		ret = snprintf(buf, PAGE_SIZE, "x1\n");
		break;
	case 0x02:
		ret = snprintf(buf, PAGE_SIZE, "x2\n");
		break;
	case 0x03:
		ret = snprintf(buf, PAGE_SIZE, "x4\n");
		break;
	case 0x04:
		ret = snprintf(buf, PAGE_SIZE, "x8\n");
		break;
	default:
		ret = snprintf(buf, PAGE_SIZE, "Reserved\n");
		break;
	}

	return ret;
}

static ssize_t sx9320_multi2_range_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	int ret;

	struct sx9320_multi2_p *data = dev_get_drvdata(dev);

	switch (data->range) {
	case 0x00:
		ret = snprintf(buf, PAGE_SIZE, "Small\n");
		break;
	case 0x01:
		ret = snprintf(buf, PAGE_SIZE, "Large\n");
		break;
	default:
		ret = snprintf(buf, PAGE_SIZE, "Small\n");
		break;
	}

	return ret;
}

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

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

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

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

static ssize_t sx9320_multi2_ch_state_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	int ret;
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);

	if (data->skip_data == true) {
		ret = snprintf(buf, PAGE_SIZE, "%d,%d\n",
			NONE_ENABLE, NONE_ENABLE);
	} else if (atomic_read(&data->enable) == ON) {
		ret = snprintf(buf, PAGE_SIZE, "%d,%d\n",
			data->ch1_state, data->ch2_state);
	} else {
		ret = snprintf(buf, PAGE_SIZE, "%d,%d\n",
			NONE_ENABLE, NONE_ENABLE);
	}

	return ret;
}

static ssize_t sx9320_multi2_body_threshold_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);
	u16 thresh_temp = 0, hysteresis = 0;
	u16 thresh_table[8] = {0, 300, 600, 900, 1200, 1500, 1800, 30000};

	thresh_temp = (data->normal_th) & 0x07;
	thresh_temp = thresh_table[thresh_temp];

	/* CTRL10 */
	hysteresis = (setup_reg[16].val >> 4) & 0x3;

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

	return snprintf(buf, PAGE_SIZE, "%d,%d\n", thresh_temp + hysteresis,
			thresh_temp - hysteresis);
}

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

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

	data->normal_th &= 0xf8;
	data->normal_th |= val;

	pr_info("[SX9320_MULTI2]: %s - body threshold %lu\n", __func__, val);
	data->normal_th_buf = data->normal_th = (u8)(val);

	return count;
}

static ssize_t sx9320_multi2_grip_flush_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);
	int ret = 0;
	u8 handle = 0;

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

	pr_info("[SX9320_MULTI2]: %s - handle = %d\n", __func__, handle);

	input_report_rel(data->input, REL_MAX, handle);
	input_sync(data->input);
	return count;
}

static ssize_t sx9320_multi2_avgnegfilt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);
	int avgnegfilt = data->avgnegfilt;

	if (avgnegfilt == 7)
		return snprintf(buf, PAGE_SIZE, "1\n");
	else if (avgnegfilt > 0 && data->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 sx9320_multi2_avgposfilt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);

	switch (data->avgposfilt) {
	case 0x00:
		return snprintf(buf, PAGE_SIZE, "0\n");
	case 0x01:
		return snprintf(buf, PAGE_SIZE, "1-1/16\n");
	case 0x02:
		return snprintf(buf, PAGE_SIZE, "1-1/64\n");
	case 0x03:
		return snprintf(buf, PAGE_SIZE, "1-1/128\n");
	case 0x04:
		return snprintf(buf, PAGE_SIZE, "1-1/256\n");
	case 0x05:
		return snprintf(buf, PAGE_SIZE, "1-1/512\n");
	case 0x06:
		return snprintf(buf, PAGE_SIZE, "1-1/1024\n");
	case 0x07:
		return snprintf(buf, PAGE_SIZE, "1\n");
	default:
		break;
	}
	return snprintf(buf, PAGE_SIZE, "not set\n");
}

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

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

static ssize_t sx9320_multi2_rawfilt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);
	int rawfilt = data->rawfilt;

	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 sx9320_multi2_sampling_freq_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);
	int sampling_freq = data->sampling_freq;

	const char *table[32] = {
		"250", "200", "166.67", "142.86", "125", "111.11", "100",
		"90.91", "83.33", "76.92", "71.43", "66.67", "62.50", "58.82",
		"55.56", "52.63", "50", "45.45", "41.67", "38.46", "35.71",
		"31.25", "27.78", "25", "20.83", "17.86", "13.89", "11.36",
		"8.33", "6.58", "5.43", "4.63"};

	if (sampling_freq < 0 || sampling_freq > 31)
		return snprintf(buf, PAGE_SIZE, "not set\n");

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

static ssize_t sx9320_multi2_scan_period_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);
	int scan_period = data->scan_period;

	const char *table[30] = {
		"Min", "2ms", "4ms", "6ms", "8ms", "10ms", "14ms", "18ms",
		"22ms", "26ms", "30ms", "34ms", "38ms", "42ms", "46ms", "50ms",
		"56ms", "62ms", "68ms", "74ms", "80ms", "90ms", "100ms",
		"200ms", "300ms", "400ms", "600ms", "800ms", "1s", "2s"};

	if (scan_period < 0 || scan_period > 29)
		return snprintf(buf, PAGE_SIZE, "not set\n");

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

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

	switch (data->again_ch) {
	case 1247:
		return snprintf(buf, PAGE_SIZE, "x1.247\n");
	case 1000:
		return snprintf(buf, PAGE_SIZE, "x1\n");
	case 768:
		return snprintf(buf, PAGE_SIZE, "x0.768\n");
	case 552:
		return snprintf(buf, PAGE_SIZE, "x0.552\n");
	default:
		return snprintf(buf, PAGE_SIZE, "not set\n");
	}
}

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

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

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

	const char *table[4] = {"None", "+/-6%", "+/-12%", "+/-25%"};
	int hyst = data->hyst;

	if (hyst < 0 || hyst > 3)
		return snprintf(buf, PAGE_SIZE, "not set\n");

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

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

	int result = 0;
	s32 max_diff_val = 0;

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

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

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

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

	u8 onoff;
	int ret;

	ret = kstrtou8(buf, 10, &onoff);
	if (ret < 0) {
		pr_err("[SX9320_MULTI2]: %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("[SX9320_MULTI2]: %s - unknown value %d\n", __func__,
			onoff);
	}

	mutex_unlock(&data->read_mutex);

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

	return count;
}

static DEVICE_ATTR(menual_calibrate, 0664,
		sx9320_multi2_get_offset_calibration_show,
		sx9320_multi2_set_offset_calibration_store);
static DEVICE_ATTR(register_write, 0220,
		NULL, sx9320_multi2_register_write_store);
static DEVICE_ATTR(register_read, 0444,
		sx9320_multi2_register_read_show, NULL);
static DEVICE_ATTR(readback, 0444, sx9320_multi2_read_data_show, NULL);
static DEVICE_ATTR(reset, 0444, sx9320_multi2_sw_reset_show, NULL);
static DEVICE_ATTR(name, 0444, sx9320_multi2_name_show, NULL);
static DEVICE_ATTR(vendor, 0444, sx9320_multi2_vendor_show, NULL);
static DEVICE_ATTR(mode, 0444, sx9320_multi2_touch_mode_show, NULL);
static DEVICE_ATTR(raw_data, 0444, sx9320_multi2_raw_data_show, NULL);
static DEVICE_ATTR(diff_avg, 0444, sx9320_multi2_diff_avg_show, NULL);
static DEVICE_ATTR(useful_avg, 0444, sx9320_multi2_useful_avg_show, NULL);
static DEVICE_ATTR(calibration, 0664,
		sx9320_multi2_calibration_show,
		sx9320_multi2_calibration_store);
static DEVICE_ATTR(onoff, 0664,
		sx9320_multi2_onoff_show, sx9320_multi2_onoff_store);
static DEVICE_ATTR(threshold, 0664,
		sx9320_multi2_threshold_show, sx9320_multi2_threshold_store);
static DEVICE_ATTR(normal_threshold, 0664,
		sx9320_multi2_normal_threshold_show,
		sx9320_multi2_normal_threshold_store);
static DEVICE_ATTR(freq, 0664,
		sx9320_multi2_freq_show, sx9320_multi2_freq_store);
static DEVICE_ATTR(ch_state, 0444, sx9320_multi2_ch_state_show, NULL);
static DEVICE_ATTR(body_threshold, 0664,
		sx9320_multi2_body_threshold_show,
		sx9320_multi2_body_threshold_store);

static DEVICE_ATTR(avg_negfilt, 0444, sx9320_multi2_avgnegfilt_show, NULL);
static DEVICE_ATTR(avg_posfilt, 0444, sx9320_multi2_avgposfilt_show, NULL);
static DEVICE_ATTR(avg_thresh, 0444, sx9320_multi2_avgthresh_show, NULL);
static DEVICE_ATTR(rawfilt, 0444, sx9320_multi2_rawfilt_show, NULL);
static DEVICE_ATTR(sampling_freq, 0444, sx9320_multi2_sampling_freq_show,
	NULL);
static DEVICE_ATTR(scan_period, 0444, sx9320_multi2_scan_period_show, NULL);
static DEVICE_ATTR(gain, 0444, sx9320_multi2_gain_show, NULL);
static DEVICE_ATTR(range, 0444, sx9320_multi2_range_show, NULL);
static DEVICE_ATTR(analog_gain, 0444, sx9320_multi2_again_show, NULL);
static DEVICE_ATTR(phase, 0444, sx9320_multi2_phase_show, NULL);
static DEVICE_ATTR(hysteresis, 0444, sx9320_multi2_hysteresis_show, NULL);
static DEVICE_ATTR(irq_count, 0664,
		sx9320_multi2_irq_count_show, sx9320_multi2_irq_count_store);

static DEVICE_ATTR(grip_flush, 0220, NULL,
	sx9320_multi2_grip_flush_store);

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_diff_avg,
	&dev_attr_useful_avg,
	&dev_attr_raw_data,
	&dev_attr_threshold,
	&dev_attr_normal_threshold,
	&dev_attr_onoff,
	&dev_attr_calibration,
	&dev_attr_freq,
	&dev_attr_ch_state,
	&dev_attr_body_threshold,
	&dev_attr_grip_flush,
	&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,
	NULL,
};

static ssize_t sx9320_multi2_enable_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	u8 enable;
	int ret;
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);
	int pre_enable = atomic_read(&data->enable);

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

	pr_info("[SX9320_MULTI2]: %s - new_value = %u old_value = %d\n",
		__func__, enable, pre_enable);

	if (pre_enable == enable)
		return size;

	atomic_set(&data->enable, enable);
	sx9320_multi2_set_enable(data, enable);

	return size;
}

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

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

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

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

	if (enable == 1) {
		input_report_rel(data->input, REL_MAX, 1);
		input_sync(data->input);
	}

	return count;
}

static DEVICE_ATTR(enable, 0664,
		sx9320_multi2_enable_show, sx9320_multi2_enable_store);
static DEVICE_ATTR(flush, 0220,
		NULL, sx9320_multi2_flush_store);

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

static struct attribute_group sx9320_multi2_attribute_group = {
	.attrs = sx9320_multi2_attributes
};

static void sx9320_multi2_touch_process(struct sx9320_multi2_p *data, u8 flag)
{
	u8 status = 0;

	sx9320_multi2_i2c_read(data, SX9320_MULTI2_STAT0_REG, &status);
	pr_info("[SX9320_MULTI2]: %s - (status: 0x%x)\n", __func__, status);

	sx9320_multi2_get_data(data);

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

	sx9320_multi2_read_ch_interrupt(data, status);

	if (data->state == IDLE) {
		if (status & (PHX_STATUS_REG << data->phen))
			sx9320_multi2_send_event(data, ACTIVE);
		else
			pr_info("[SX9320_MULTI2]: %s - already released\n",
				__func__);
	} else {
		if (!(status & (PHX_STATUS_REG << data->phen)))
			sx9320_multi2_send_event(data, IDLE);
		else
			pr_info("[SX9320_MULTI2]: %s - still touched\n",
				__func__);
	}
}

static void sx9320_multi2_process_interrupt(struct sx9320_multi2_p *data)
{
	u8 flag = 0;

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

	if (flag & IRQ_PROCESS_CONDITION)
		sx9320_multi2_touch_process(data, flag);
	else
		pr_info("[SX9320_MULTI2]: %s interrupt generated but skip\n",
			__func__);
}

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

	sx9320_multi2_initialize_chip(data);

	/* set the threshold at init. register depends on phase */
	if (data->phen < 2)
		sx9320_multi2_i2c_write(data, SX9320_MULTI2_PROXCTRL6_REG,
			data->normal_th);
	else
		sx9320_multi2_i2c_write(data, SX9320_MULTI2_PROXCTRL7_REG,
			data->normal_th);

	sx9320_multi2_set_mode(data, SX9320_MULTI2_MODE_NORMAL);
	/*
	 * make sure no interrupts are pending since enabling irq
	 * will only work on next falling edge
	 */
	sx9320_multi2_read_irqstate(data);
}

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

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

static void sx9320_multi2_debug_work_func(struct work_struct *work)
{
	struct sx9320_multi2_p *data = container_of((struct delayed_work *)work,
		struct sx9320_multi2_p, debug_work);

	static int hall_flag = 1;

	sx9320_multi2_check_hallic_state(HALLIC1_PATH, data->hall_ic1);

	/* Hall IC closed : offset cal (once) */
	if (strcmp(data->hall_ic1, "CLOSE") == 0) {
		if (hall_flag) {
			pr_info("[SX9320_MULTI2]: %s - hall IC1 is closed\n",
				__func__);
			sx9320_multi2_set_offset_calibration(data);
			hall_flag = 0;
		}
	} else
		hall_flag = 1;

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

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

static irqreturn_t sx9320_multi2_interrupt_thread(int irq, void *pdata)
{
	struct sx9320_multi2_p *data = pdata;

	if (sx9320_multi2_get_nirq_state(data) == 1) {
		pr_err("[SX9320_MULTI2]: %s - nirq read high\n", __func__);
	} else {
		wake_lock_timeout(&data->grip_wake_lock, 3 * HZ);
		schedule_delayed_work(&data->irq_work, msecs_to_jiffies(100));
	}

	return IRQ_HANDLED;
}

static int sx9320_multi2_init_input(struct sx9320_multi2_p *data)
{
	int ret = 0;

	/* Create the input device */
	data->input = input_allocate_device();

	if (!data->input)
		return -ENOMEM;

	data->input->name = MODULE_NAME;
	data->input->id.bustype = BUS_I2C;

	input_set_capability(data->input, EV_REL, REL_MISC);
	input_set_capability(data->input, EV_REL, REL_MAX);
	input_set_drvdata(data->input, data);

	ret = input_register_device(data->input);
	if (ret < 0) {
		input_free_device(data->input);
		return ret;
	}

	ret = sensors_create_symlink(data->input);
	if (ret < 0) {
		input_unregister_device(data->input);
		return ret;
	}

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

	return 0;
}

static int sx9320_multi2_setup_pin(struct sx9320_multi2_p *data)
{
	int ret;

	ret = gpio_request(data->gpio_nirq, "SX9320_MULTI2_nIRQ");
	if (ret < 0) {
		pr_err("[SX9320_MULTI2]: %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("[SX9320_MULTI2]: %s - failed to set gpio %d(%d)\n",
			__func__, data->gpio_nirq, ret);
		gpio_free(data->gpio_nirq);
		return ret;
	}

	return 0;
}

static void sx9320_multi2_set_specific_register(int regi_num, int end,
	int start, u8 val)
{
	u16 clear_bit = 0x00;
	unsigned char temp_val;

	temp_val = setup_reg[regi_num].val;

	clear_bit = ~((1 << (end + 1)) - (1 << start));

	temp_val = (temp_val & clear_bit) | (val << start);

	setup_reg[regi_num].val = temp_val;
}

static void sx9320_multi2_initialize_variable(struct sx9320_multi2_p *data)
{
	data->state = IDLE;
	data->skip_data = false;
	data->check_usb = false;
	data->debug_count = 0;
	data->normal_th_buf = data->normal_th;
	data->ch1_state = IDLE;
	data->init_done = OFF;

	atomic_set(&data->enable, OFF);

	/* sampling freq, rawfilt, gain, hyst are depends on phase */
	if (data->phen < 2) {
		/* regi_num, end bit, start bit, val */
		sx9320_multi2_set_specific_register(5,  7, 3,
			data->sampling_freq);
		sx9320_multi2_set_specific_register(5,  2, 0,
			data->resolution);
		sx9320_multi2_set_specific_register(12, 2, 0, data->rawfilt);
		sx9320_multi2_set_specific_register(12, 5, 3, data->gain);
	} else {
		sx9320_multi2_set_specific_register(7,  7, 3,
			data->sampling_freq);
		sx9320_multi2_set_specific_register(7,  2, 0, data->resolution);
		sx9320_multi2_set_specific_register(13, 2, 0, data->rawfilt);
		sx9320_multi2_set_specific_register(13, 5, 3, data->gain);
	}
	sx9320_multi2_set_specific_register(0,  4, 0, data->scan_period);
	sx9320_multi2_set_specific_register(43, 3, 0, data->again);
	sx9320_multi2_set_specific_register(14, 5, 0, data->avgthresh);
	sx9320_multi2_set_specific_register(16, 2, 0, data->avgposfilt);
	sx9320_multi2_set_specific_register(16, 5, 3, data->avgnegfilt);
	sx9320_multi2_set_specific_register(17, 3, 2, data->debouncer);
	sx9320_multi2_set_specific_register(17, 5, 4, data->hyst);
	sx9320_multi2_set_specific_register(8, 5, 0, data->afeph0);
	sx9320_multi2_set_specific_register(9, 5, 0, data->afeph1);
	sx9320_multi2_set_specific_register(10, 5, 0, data->afeph2);
	sx9320_multi2_set_specific_register(11, 5, 0, data->afeph3);

	sx9320_multi2_get_again(data);
}

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

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

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

	return ret;
}

static int sx9320_multi2_parse_dt(struct sx9320_multi2_p *data,
	struct device *dev)
{
	struct device_node *node = dev->of_node;
	enum of_gpio_flags flags;

	if (node == NULL)
		return -ENODEV;

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

	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_PHEN, &data->phen);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_GAIN, &data->gain);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_AGAIN, &data->again);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_SCANPERIOD,
		&data->scan_period);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_RANGE, &data->range);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_SAMPLING_FREQ,
		&data->sampling_freq);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_RESOLUTION,
		&data->resolution);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_RAWFILT,
		&data->rawfilt);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_HYST, &data->hyst);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_AVGPOSFILT,
		&data->avgposfilt);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_AVGNEGFILT,
		&data->avgnegfilt);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_AVGTHRESH,
		&data->avgthresh);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_DEBOUNCER,
		&data->debouncer);
	sx9320_multi2_read_setupreg(node, SX9320_MULTI2_NORMALTHD,
		&data->normal_th);

	if (sx9320_multi2_read_setupreg(node, SX9320_MULTI2_AFEPH0,
		&data->afeph0))
		data->afeph0 = setup_reg[8].val;
	if (sx9320_multi2_read_setupreg(node, SX9320_MULTI2_AFEPH1,
		&data->afeph1))
		data->afeph1 = setup_reg[9].val;
	if (sx9320_multi2_read_setupreg(node, SX9320_MULTI2_AFEPH2,
		&data->afeph2))
		data->afeph2 = setup_reg[10].val;
	if (sx9320_multi2_read_setupreg(node, SX9320_MULTI2_AFEPH3,
		&data->afeph3))
		data->afeph3 = setup_reg[11].val;

	pr_info("[SX9320_MULTI2]: afeph0:%x, afeph1:%x, afeph2:%x, afeph3:%x\n",
		data->afeph0, data->afeph1, data->afeph2, data->afeph3);

	return 0;
}
#if defined(CONFIG_CCIC_NOTIFIER) && defined(CONFIG_USB_TYPEC_MANAGER_NOTIFIER)
static int sx9320_multi2_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 sx9320_multi2_p *pdata =
		container_of(nb, struct sx9320_multi2_p, cpuidle_ccic_nb);
	static int pre_attach;

	if (pre_attach == usb_status.attach)
		return 0;
	/*
	 * USB_STATUS_NOTIFY_DETACH = 0,
	 * USB_STATUS_NOTIFY_ATTACH_DFP = 1, // Host
	 * USB_STATUS_NOTIFY_ATTACH_UFP = 2, // Device
	 * USB_STATUS_NOTIFY_ATTACH_DRP = 3, // Dual role
	 */

	if (pdata->init_done == ON) {
		switch (usb_status.drp) {
		case USB_STATUS_NOTIFY_ATTACH_UFP:
		case USB_STATUS_NOTIFY_ATTACH_DFP:
		case USB_STATUS_NOTIFY_DETACH:
			pr_info("[SX9320_MULTI2]: %s - drp = %d attat = %d\n",
				__func__, usb_status.drp,
				usb_status.attach);
			sx9320_multi2_set_offset_calibration(pdata);
			break;
		default:
			pr_info("[SX9320_MULTI2]: %s - DRP type : %d\n",
				__func__, usb_status.drp);
			break;
		}
	}

	pre_attach = usb_status.attach;

	return 0;
}
#elif defined(CONFIG_MUIC_NOTIFIER)
static int sx9320_multi2_cpuidle_muic_notifier(struct notifier_block *nb,
				unsigned long action, void *data)
{
	struct sx9320_multi2_p *grip_data;

	muic_attached_dev_t attached_dev = *(muic_attached_dev_t *)data;

	grip_data = container_of(nb, struct sx9320_multi2_p, cpuidle_muic_nb);
	switch (attached_dev) {
	case ATTACHED_DEV_OTG_MUIC:
	case ATTACHED_DEV_USB_MUIC:
	case ATTACHED_DEV_TA_MUIC:
	case ATTACHED_DEV_AFC_CHARGER_PREPARE_MUIC:
	case ATTACHED_DEV_AFC_CHARGER_9V_MUIC:
		if (action == MUIC_NOTIFY_CMD_ATTACH)
			pr_info("[SX9320_MULTI2]: %s - TA/USB insert\n",
				__func__);
		else if (action == MUIC_NOTIFY_CMD_DETACH)
			pr_info("[SX9320_MULTI2]: %s - TA/USB remove\n",
				__func__);

		if (grip_data->init_done == ON)
			sx9320_multi2_set_offset_calibration(grip_data);
		else
			pr_info("[SX9320_MULTI2]: %s - not initialized\n",
				__func__);
		break;
	default:
		break;
	}

	pr_info("[SX9320_MULTI2]: %s dev=%d, action=%lu\n",
		__func__, attached_dev, action);

	return NOTIFY_DONE;
}
#endif

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

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

	/* create memory for main struct */
	data = kzalloc(sizeof(struct sx9320_multi2_p), GFP_KERNEL);

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

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

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

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

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

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

	sx9320_multi2_initialize_variable(data);
	INIT_DELAYED_WORK(&data->init_work, sx9320_multi2_init_work_func);
	INIT_DELAYED_WORK(&data->irq_work, sx9320_multi2_irq_work_func);
	INIT_DELAYED_WORK(&data->debug_work, sx9320_multi2_debug_work_func);

	data->irq = gpio_to_irq(data->gpio_nirq);
	ret = request_threaded_irq(data->irq, NULL, sx9320_multi2_interrupt_thread,
			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
			"sx9320_multi2_irq", data);
	if (ret < 0) {
		pr_err("[SX9320_MULTI2]: %s failed to set request irq %d(%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("[SX9320_MULTI2] %s - cound not register sensor(%d).\n",
			__func__, ret);
		goto grip_sensor_register_failed;
	}

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

#if defined(CONFIG_CCIC_NOTIFIER) && defined(CONFIG_USB_TYPEC_MANAGER_NOTIFIER)
	manager_notifier_register(&data->cpuidle_ccic_nb,
					sx9320_multi2_ccic_handle_notification,
					MANAGER_NOTIFY_CCIC_SENSORHUB);
#elif defined(CONFIG_MUIC_NOTIFIER)
	muic_notifier_register(&data->cpuidle_muic_nb,
		sx9320_multi2_cpuidle_muic_notifier, MUIC_NOTIFY_DEV_CPUIDLE);
#endif

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

	return 0;

grip_sensor_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->read_mutex);
	wake_lock_destroy(&data->grip_wake_lock);
	sysfs_remove_group(&data->input->dev.kobj,
		&sx9320_multi2_attribute_group);
	sensors_remove_symlink(data->input);
	input_unregister_device(data->input);
exit_input_init:
	kfree(data);
exit:
	pr_err("[SX9320_MULTI2]: %s - Probe fail!\n", __func__);
	return ret;
}

static int sx9320_multi2_remove(struct i2c_client *client)
{
	struct sx9320_multi2_p *data =
		(struct sx9320_multi2_p *)i2c_get_clientdata(client);

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

	sx9320_multi2_set_mode(data, SX9320_MULTI2_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,
		&sx9320_multi2_attribute_group);
	input_unregister_device(data->input);
	mutex_destroy(&data->read_mutex);

	kfree(data);

	return 0;
}

static int sx9320_multi2_suspend(struct device *dev)
{
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);
	int cnt = 0;

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

	sx9320_multi2_set_debug_work(data, OFF, 1000);

	return 0;
}

static int sx9320_multi2_resume(struct device *dev)
{
	struct sx9320_multi2_p *data = dev_get_drvdata(dev);

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

	return 0;
}

static void sx9320_multi2_shutdown(struct i2c_client *client)
{
	struct sx9320_multi2_p *data = i2c_get_clientdata(client);

	pr_info("[SX9320_MULTI2]: %s\n", __func__);
	sx9320_multi2_set_debug_work(data, OFF, 1000);
	if (atomic_read(&data->enable) == ON)
		sx9320_multi2_set_enable(data, OFF);
	sx9320_multi2_set_mode(data, SX9320_MULTI2_MODE_SLEEP);
}

static const struct of_device_id grip2_match_table[] = {
	{ .compatible = "sx9320_multi2",},
	{},
};

static const struct i2c_device_id sx9320_multi2_id[] = {
	{ "sx9320_multi2", 0 },
	{ }
};

static const struct dev_pm_ops sx9320_multi2_pm_ops = {
	.suspend = sx9320_multi2_suspend,
	.resume = sx9320_multi2_resume,
};

static struct i2c_driver grip2_driver = {
	.driver = {
		.name	= MODEL_NAME,
		.owner	= THIS_MODULE,
		.of_match_table = grip2_match_table,
		.pm = &sx9320_multi2_pm_ops
	},
	.probe		= sx9320_multi2_probe,
	.remove		= sx9320_multi2_remove,
	.shutdown	= sx9320_multi2_shutdown,
	.id_table	= sx9320_multi2_id,
};


static int __init grip2_init(void)
{
	return i2c_add_driver(&grip2_driver);
}

static void __exit grip2_exit(void)
{
	i2c_del_driver(&grip2_driver);
}

module_init(grip2_init);
module_exit(grip2_exit);

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