summaryrefstree
path: root/kernel/drivers/fingerprint/qbt2000_common.c
blob: 0bf9236691395c5c0c54a7816839ced40060d8b7 (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
/*
 * Copyright (C) 2018 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.
 */

#include "fingerprint.h"
#include "qbt2000_common.h"

static struct qbt2000_drvdata *g_data = NULL;

/*
 * struct ipc_msg_type_to_fw_event -
 *      entry in mapping between an IPC message type to a firmware event
 * @msg_type - IPC message type, as reported by firmware
 * @fw_event - corresponding firmware event code to report to driver client
 */
struct ipc_msg_type_to_fw_event {
	uint32_t msg_type;
	enum qbt2000_fw_event fw_event;
};

/* mapping between firmware IPC message types to HLOS firmware events */
struct ipc_msg_type_to_fw_event g_msg_to_event[] = {
	{IPC_MSG_ID_CBGE_REQUIRED, FW_EVENT_CBGE_REQUIRED},
	{IPC_MSG_ID_FINGER_ON_SENSOR, FW_EVENT_FINGER_DOWN},
	{IPC_MSG_ID_FINGER_OFF_SENSOR, FW_EVENT_FINGER_UP},
};

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

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

static ssize_t qbt2000_adm_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n", DETECT_ADM);
}

static ssize_t qbt2000_bfs_values_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct qbt2000_drvdata *drvdata = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "\"FP_SPICLK\":\"%d\"\n",
			drvdata->spi_speed);
}

static ssize_t qbt2000_type_check_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct qbt2000_drvdata *drvdata = dev_get_drvdata(dev);

	pr_info("%s\n", sensor_status[drvdata->sensortype + 2]);
	return snprintf(buf, PAGE_SIZE, "%d\n", drvdata->sensortype);
}

static ssize_t qbt2000_position_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct qbt2000_drvdata *drvdata = dev_get_drvdata(dev);
	return snprintf(buf, PAGE_SIZE, "%s\n", drvdata->sensor_position);
}

static ssize_t qbt2000_cbgecnt_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct qbt2000_drvdata *drvdata = dev_get_drvdata(dev);
	return snprintf(buf, PAGE_SIZE, "%d\n", drvdata->cbge_count);
}

static ssize_t qbt2000_cbgecnt_store(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t size)
{
	struct qbt2000_drvdata *drvdata = dev_get_drvdata(dev);

	if (sysfs_streq(buf, "c")) {
		drvdata->cbge_count = 0;
#ifdef QBT2000_AVOID_NOISE
		drvdata->ignored_cbge_count = 0;
#endif
		pr_info("initialization is done\n");
	} else if (sysfs_streq(buf, "wuhb")) {
	/* For User HwModuleTest IntTest */
		drvdata->wuhb_test_flag = 1;
		drvdata->wuhb_test_result = 0;

		if (drvdata->fd_gpio.gpio) {
			if (drvdata->enabled_wuhb) {
				drvdata->wuhb_test_flag = 0;
				drvdata->wuhb_test_result = -1;
				pr_err("wuhb test procedure can not perform.\n");
			} else {
				enable_irq(drvdata->fd_gpio.irq);
				drvdata->enabled_wuhb = true;
				pr_info("wuhb test start.flag:%d,result:%d,en:%d\n",
					drvdata->wuhb_test_flag,
					drvdata->wuhb_test_result,
					drvdata->enabled_wuhb);
			}
		} else {
			drvdata->wuhb_test_flag = 0;
			drvdata->wuhb_test_result = -1;
			pr_err("fd_gpio does not supports this hw rev.\n");
		}
	}
	return size;
}

static ssize_t qbt2000_intcnt_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct qbt2000_drvdata *drvdata = dev_get_drvdata(dev);
	return snprintf(buf, PAGE_SIZE, "%d\n", drvdata->wuhb_count);
}

static ssize_t qbt2000_intcnt_store(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t size)
{
	struct qbt2000_drvdata *drvdata = dev_get_drvdata(dev);

	if (sysfs_streq(buf, "c")) {
		drvdata->wuhb_count = 0;
		pr_info("initialization is done\n");
	}
	return size;
}

static ssize_t qbt2000_resetcnt_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct qbt2000_drvdata *drvdata = dev_get_drvdata(dev);
	return snprintf(buf, PAGE_SIZE, "%d\n", drvdata->reset_count);
}

static ssize_t qbt2000_resetcnt_store(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t size)
{
	struct qbt2000_drvdata *drvdata = dev_get_drvdata(dev);

	if (sysfs_streq(buf, "c")) {
		drvdata->reset_count = 0;
		pr_info("initialization is done\n");
	}
	return size;
}

static ssize_t qbt2000_wuhbtest_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct qbt2000_drvdata *drvdata = dev_get_drvdata(dev);
	int rc = drvdata->wuhb_test_result;

	if (drvdata->wuhb_test_flag == 1) {
		if (drvdata->enabled_wuhb) {
			disable_irq(drvdata->fd_gpio.irq);
			drvdata->enabled_wuhb = false;
		}
	}
	drvdata->wuhb_test_result = 0;
	drvdata->wuhb_test_flag = 0;
	pr_info("wuhb test complete.rc=%d,wuhb_en:%d\n", rc, drvdata->enabled_wuhb);
	return snprintf(buf, PAGE_SIZE, "%d\n", rc);
}

static DEVICE_ATTR(bfs_values, 0444, qbt2000_bfs_values_show, NULL);
static DEVICE_ATTR(type_check, 0444, qbt2000_type_check_show, NULL);
static DEVICE_ATTR(vendor, 0444, qbt2000_vendor_show, NULL);
static DEVICE_ATTR(name, 0444, qbt2000_name_show, NULL);
static DEVICE_ATTR(adm, 0444, qbt2000_adm_show, NULL);
static DEVICE_ATTR(position, 0444, qbt2000_position_show, NULL);
static DEVICE_ATTR(cbgecnt, 0664, qbt2000_cbgecnt_show, qbt2000_cbgecnt_store);
static DEVICE_ATTR(intcnt, 0664, qbt2000_intcnt_show, qbt2000_intcnt_store);
static DEVICE_ATTR(resetcnt, 0664, qbt2000_resetcnt_show, qbt2000_resetcnt_store);
static DEVICE_ATTR(wuhbtest, 0444, qbt2000_wuhbtest_show, NULL);

static struct device_attribute *fp_attrs[] = {
	&dev_attr_bfs_values,
	&dev_attr_type_check,
	&dev_attr_vendor,
	&dev_attr_name,
	&dev_attr_adm,
	&dev_attr_position,
	&dev_attr_cbgecnt,
	&dev_attr_intcnt,
	&dev_attr_resetcnt,
	&dev_attr_wuhbtest,
	NULL,
};

#if defined(ENABLE_SENSORS_FPRINT_SECURE)
int fpsensor_goto_suspend = 0;

int fps_resume_set(void) {
	int rc = 0;

	if (fpsensor_goto_suspend)
		fpsensor_goto_suspend = 0;
	return rc;
}
#endif

#ifdef QBT2000_AVOID_NOISE
static int qbt2000_noise_control(struct qbt2000_drvdata *drvdata, int control)
{
	int retry = 3;
	int rc = 0;

	if (control == QBT2000_NOISE_UNBLOCK) {
		drvdata->noise_onoff_flag = QBT2000_NOISE_UNBLOCK;
		rc = set_wacom_ble_charge_mode(true);
		pr_info("%d, rc:%d\n", control, rc);
	} else if ((control == QBT2000_NOISE_BLOCK) && (drvdata->noise_onoff_flag == QBT2000_NOISE_UNBLOCK)) {
		drvdata->noise_onoff_flag = QBT2000_NOISE_BLOCK;
		while (retry--) {
			rc = set_wacom_ble_charge_mode(false);
			pr_info("%d, retry:%d, rc:%d\n", control, retry, rc);
			if (rc == 0)
				break;
			usleep_range(4950, 5000);
		}
	} else {
		pr_err("invalid value %d,%d\n", control, drvdata->noise_onoff_flag);
	}

	if (rc < 0) {
		drvdata->noise_onoff_flag = control ? QBT2000_NOISE_BLOCK : QBT2000_NOISE_UNBLOCK;
		drvdata->i2c_error_set++;
	}

	return rc;
}
#endif

static int qbt2000_power_control(struct qbt2000_drvdata *drvdata, int onoff)
{
	int rc = 0;
	if (drvdata->ldogpio >= 0) {
		gpio_set_value(drvdata->ldogpio, onoff);
		drvdata->enabled_ldo = onoff;
#if !defined(ENABLE_SENSORS_FPRINT_SECURE) || defined(DISABLED_GPIO_PROTECTION)
		if (onoff) {
			if (drvdata->pins_poweron) {
				rc = pinctrl_select_state(drvdata->p, drvdata->pins_poweron);
				pr_debug("pinctrl for poweron\n");
			}
		} else {
			if (drvdata->pins_poweroff) {
				rc = pinctrl_select_state(drvdata->p, drvdata->pins_poweroff);
				pr_debug("pinctrl for poweroff\n");
			}
		}
#endif
		pr_info("%s\n", onoff ? "ON" : "OFF");
	} else if (drvdata->regulator_1p8 != NULL) {
		if (onoff) {
			rc = regulator_enable(drvdata->regulator_1p8);
			if (rc)
				pr_err("regulator enable failed, rc=%d\n", rc);
		} else {
			rc = regulator_disable(drvdata->regulator_1p8);
			if (rc)
				pr_err("regulator disable failed, rc=%d\n", rc);
		}
#if !defined(ENABLE_SENSORS_FPRINT_SECURE) || defined(DISABLED_GPIO_PROTECTION)
		if (onoff) {
			if (drvdata->pins_poweron) {
				rc = pinctrl_select_state(drvdata->p, drvdata->pins_poweron);
				pr_debug("pinctrl for poweron\n");
			}
		} else {
			if (drvdata->pins_poweroff) {
				rc = pinctrl_select_state(drvdata->p, drvdata->pins_poweroff);
				pr_debug("pinctrl for poweroff\n");
			}
		}
#endif
		drvdata->enabled_ldo = onoff;
		pr_info("%s\n", onoff ? "ON" : "OFF");
	} else {
		pr_info("This hw revision does not support power control\n");
	}
	return rc;
}

static int qbt2000_enable_spi_clock(struct qbt2000_drvdata *drvdata)
{
	int rc = 0;

	rc = qbt2000_set_clk(drvdata, 1);
	return rc;
}

static int qbt2000_disable_spi_clock(struct qbt2000_drvdata *drvdata)
{
	int rc = 0;

	rc = qbt2000_set_clk(drvdata, 0);
	return rc;
}

static int qbt2000_enable_ipc(struct qbt2000_drvdata *drvdata)
{
	int rc = 0;

	if (drvdata->fw_ipc.gpio) {
		if (drvdata->enabled_ipc) {
			rc = -EINVAL;
			pr_err("already enabled ipc\n");
		} else {
			enable_irq(drvdata->fw_ipc.irq);
#ifdef ENABLE_SENSORS_FPRINT_SECURE
			enable_irq_wake(drvdata->fw_ipc.irq);
#endif
			drvdata->enabled_ipc = true;
		}
	}
	return rc;
}

static int qbt2000_disable_ipc(struct qbt2000_drvdata *drvdata)
{
	int rc = 0;

	if (drvdata->fw_ipc.gpio) {
		if (drvdata->enabled_ipc) {
#ifdef ENABLE_SENSORS_FPRINT_SECURE
			disable_irq_wake(drvdata->fw_ipc.irq);
#endif
			disable_irq(drvdata->fw_ipc.irq);
			drvdata->enabled_ipc = false;
		} else {
			rc = -EINVAL;
			pr_err("already disabled ipc\n");			
		}
	}
	return rc;
}

static int qbt2000_enable_wuhb(struct qbt2000_drvdata *drvdata)
{
	int rc = 0;
	int gpio = 0;

	if (drvdata->fd_gpio.gpio) {
		if (drvdata->enabled_wuhb) {
			rc = -EINVAL;
			pr_err("already enabled wuhb\n");
		} else {
			enable_irq(drvdata->fd_gpio.irq);
			enable_irq_wake(drvdata->fd_gpio.irq);
			drvdata->enabled_wuhb = true;
			/* To prevent FingerUp Missing issue. */
			gpio = gpio_get_value(drvdata->fd_gpio.gpio);
			if (drvdata->fd_gpio.last_gpio_state == FINGER_DOWN_GPIO_STATE &&
				gpio == FINGER_LEAVE_GPIO_STATE) {
				pr_info("Finger leave event already occured. %d, %d\n",
					drvdata->fd_gpio.last_gpio_state, gpio);

				wake_lock_timeout(&drvdata->fp_signal_lock,
						msecs_to_jiffies(QBT2000_WAKELOCK_HOLD_TIME));
				schedule_work(&drvdata->fd_gpio.work);
			}
		}
	}
	return rc;
}

static int qbt2000_disable_wuhb(struct qbt2000_drvdata *drvdata)
{
	int rc = 0;

	if (drvdata->fd_gpio.gpio) {
		if (drvdata->enabled_wuhb) {
			disable_irq(drvdata->fd_gpio.irq);
			disable_irq_wake(drvdata->fd_gpio.irq);
			drvdata->enabled_wuhb = false;
		} else {
			rc = -EINVAL;
			pr_err("already disabled wuhb\n");
		}
	}
	return rc;
}

/**
 * qbt2000_open() - Function called when user space opens device.
 * Successful if driver not currently open.
 * @inode:	ptr to inode object
 * @file:	ptr to file object
 *
 * Return: 0 on success. Error code on failure.
 */
static int qbt2000_open(struct inode *inode, struct file *file)
{
	struct qbt2000_drvdata *drvdata = NULL;
	int rc = 0;
	int minor_no = iminor(inode);

	if (minor_no == MINOR_NUM_FD) {
		drvdata = container_of(inode->i_cdev, struct qbt2000_drvdata, qbt2000_fd_cdev);
	} else if (minor_no == MINOR_NUM_IPC) {
		drvdata = container_of(inode->i_cdev, struct qbt2000_drvdata, qbt2000_ipc_cdev);
	} else {
		pr_err("Invalid minor number\n");
		return -EINVAL;
	}
	file->private_data = drvdata;

	/* disallowing concurrent opens */
	if (minor_no == MINOR_NUM_FD && !atomic_dec_and_test(&drvdata->fd_available)) {
		atomic_inc(&drvdata->fd_available);
		pr_err("fd_unavailable\n");
		rc = -EBUSY;
	} else if (minor_no == MINOR_NUM_IPC && !atomic_dec_and_test(&drvdata->ipc_available)) {
		atomic_inc(&drvdata->ipc_available);
		pr_err("ipc_unavailable\n");
		rc = -EBUSY;
	}

	pr_debug("minor_no=%d, rc=%d,%d,%d\n", minor_no, rc,
		atomic_read(&drvdata->fd_available),
		atomic_read(&drvdata->ipc_available));
	return rc;
}

/**
 * qbt2000_release() - Function called when user space closes device.

 * @inode:	ptr to inode object
 * @file:	ptr to file object
 *
 * Return: 0 on success. Error code on failure.
 */
static int qbt2000_release(struct inode *inode, struct file *file)
{
	struct qbt2000_drvdata *drvdata;
	int minor_no;
	int rc = 0;

	if (!file || !file->private_data) {
		pr_err("qbt2000_release: NULL pointer passed\n");
		return -EINVAL;
	}
	drvdata = file->private_data;
	minor_no = iminor(inode);
	if (minor_no == MINOR_NUM_FD) {
		atomic_inc(&drvdata->fd_available);
	} else if (minor_no == MINOR_NUM_IPC) {
		atomic_inc(&drvdata->ipc_available);
	} else {
		pr_err("Invalid minor number\n");
		rc = -EINVAL;
	}
	pr_debug("minor_no=%d, rc=%d,%d,%d\n", minor_no, rc,
		atomic_read(&drvdata->fd_available),
		atomic_read(&drvdata->ipc_available));
	return rc;
}

/**
 * qbt2000_ioctl() - Function called when user space calls ioctl.
 * @file:	struct file - not used
 * @cmd:	cmd identifier:QBT2000_LOAD_APP,QBT2000_UNLOAD_APP,
 *              QBT2000_SEND_TZCMD
 * @arg:	ptr to relevant structe: either qbt2000_app or
 *              qbt2000_send_tz_cmd depending on which cmd is passed
 *
 * Return: 0 on success. Error code on failure.
 */
static long qbt2000_ioctl(
		struct file *file, unsigned int cmd, unsigned long arg)
{
	int rc = 0;
	int data = 0;
	void __user *priv_arg = (void __user *)arg;
	struct qbt2000_drvdata *drvdata;

	if (!file || !file->private_data) {
		pr_err("qbt2000_ioctl: NULL pointer passed\n");
		return -EINVAL;
	}

	drvdata = file->private_data;

	if (IS_ERR(priv_arg)) {
		pr_err("invalid user space pointer %lu\n", arg);
		return -EINVAL;
	}

	mutex_lock(&drvdata->ioctl_mutex);

	switch (cmd) {
	case QBT2000_IS_WHUB_CONNECTED:
		break;
	case QBT2000_POWER_CONTROL:
		if (copy_from_user(&data, (void *)arg, sizeof(int)) != 0) {
			pr_err("Failed copy from user.(POWER_CONTROL)\n");
			rc = -EFAULT;
			goto ioctl_failed;
		}
		if (drvdata->enabled_ldo != data) {
			pr_debug("POWER_CONTROL\n");
			qbt2000_power_control(drvdata, data);
		}
		break;
	case QBT2000_ENABLE_SPI_CLOCK:
		pr_info("ENABLE_SPI_CLOCK\n");
		rc = qbt2000_enable_spi_clock(drvdata);
		break;
	case QBT2000_DISABLE_SPI_CLOCK:
		pr_info("DISABLE_SPI_CLOCK\n");
		rc = qbt2000_disable_spi_clock(drvdata);
		break;
	case QBT2000_ENABLE_IPC:
		pr_info("ENABLE_IPC\n");
		rc = qbt2000_enable_ipc(drvdata);
		break;
	case QBT2000_DISABLE_IPC:
		pr_info("DISABLE_IPC\n");
		rc = qbt2000_disable_ipc(drvdata);
		break;
	case QBT2000_ENABLE_WUHB:
		pr_info("ENABLE_WUHB\n");
		drvdata->wuhb_test_flag = 0;
		rc = qbt2000_enable_wuhb(drvdata);
		break;
	case QBT2000_DISABLE_WUHB:
		pr_info("DISABLE_WUHB\n");
		/* initialize IntTest flag */
		drvdata->wuhb_test_flag = 0;
		rc = qbt2000_disable_wuhb(drvdata);
		break;
	case QBT2000_CPU_SPEEDUP:
		if (copy_from_user(&data, (void *)arg, sizeof(int)) != 0) {
			pr_err("Failed copy from user.(SPEEDUP)\n");
			rc = -EFAULT;
			goto ioctl_failed;
		}
		rc = qbt2000_set_cpu_speedup(drvdata, data);
		break;
	case QBT2000_SET_SENSOR_TYPE:
		if (copy_from_user(&data, (void *)arg, sizeof(int)) != 0) {
			pr_err("Failed to copy sensor type from user to kernel\n");
			rc = -EFAULT;
			goto ioctl_failed;
		}
		if (data >= SENSOR_OOO && data < SENSOR_MAXIMUM) {
			if (data == SENSOR_OOO && drvdata->sensortype == SENSOR_FAILED) {
				pr_err("Maintain type check from out of oder :%s\n",
						sensor_status[drvdata->sensortype + 2]);
			} else {
				drvdata->sensortype = data;
				pr_info("SET_SENSOR_TYPE :%s\n",
						sensor_status[drvdata->sensortype + 2]);
			}
		} else {
			pr_err("SET_SENSOR_TYPE : invalid value %d\n", data);
			drvdata->sensortype = SENSOR_UNKNOWN;
		}
		break;
	case QBT2000_SET_LOCKSCREEN:
		break;
	case QBT2000_SENSOR_RESET:
		drvdata->reset_count++;
		pr_err("SENSOR_RESET\n");
		break;
	case QBT2000_SENSOR_TEST:
		if (copy_from_user(&data, (void *)arg, sizeof(int)) != 0) {
			pr_err("Failed to copy BGECAL from user to kernel\n");
			rc = -EFAULT;
			goto ioctl_failed;
		}
#ifndef ENABLE_SENSORS_FPRINT_SECURE  //only for factory
#ifdef QBT2000_AVOID_NOISE
		if (data == QBT2000_SENSORTEST_DONE) {
			pr_info("SENSORTEST Finished\n");
			qbt2000_noise_control(drvdata, QBT2000_NOISE_BLOCK);
		} else {
			pr_info("SENSORTEST Start : 0x%x\n", data);
			qbt2000_noise_control(drvdata, QBT2000_NOISE_UNBLOCK);
		}
#endif
#endif
		break;
	case QBT2000_NOISE_REQUEST_STOP:
#ifdef QBT2000_AVOID_NOISE
		pr_info("QBT2000_NOISE_REQUEST_STOP. entry\n");
		drvdata->noise_i2c_result = 1;
		schedule_work(&drvdata->work_noise_control);
		msleep(50);
#endif
		break;
	case QBT2000_NOISE_I2C_RESULT_GET:
#ifdef QBT2000_AVOID_NOISE
		pr_info("QBT2000_NOISE_I2C_RESULT_GET : %d\n", drvdata->noise_i2c_result);
		if (copy_to_user((void __user *)priv_arg, &drvdata->noise_i2c_result, sizeof(drvdata->noise_status)) != 0) {
			pr_err("Failed to copy I2C_RESULT to user\n");
			rc = -EFAULT;
		}
#endif
		break;
	case QBT2000_NOISE_STATUS_GET:
#ifdef QBT2000_AVOID_NOISE
		drvdata->noise_status = get_wacom_scan_info(false);
		if (drvdata->noise_status == QBT2000_NOISE_MODE_CHANGED) {
			drvdata->ignored_cbge_count++;
		} else if (drvdata->noise_status == QBT2000_NOISE_CHARGING) {
			drvdata->i2c_charging++;
		} else if (drvdata->noise_status < 0) {
			drvdata->i2c_error_get++;
		}
		pr_info("QBT2000_NOISE_STATUS_GET : %d\n", drvdata->noise_status);
		if (copy_to_user((void __user *)priv_arg, &drvdata->noise_status, sizeof(drvdata->noise_status)) != 0) {
			pr_err("Failed to copy NOISE_STATUS to user\n");
			rc = -EFAULT;
		}
#endif
		break;
	case QBT2000_NOISE_REQUEST_START:
#ifdef QBT2000_AVOID_NOISE
		pr_info("QBT2000_NOISE_REQUEST_START\n");
		qbt2000_noise_control(drvdata, QBT2000_NOISE_UNBLOCK);
#endif
		break;
	case QBT2000_NOISE_REQUEST_STATUS:
#ifdef QBT2000_AVOID_NOISE
		pr_info("QBT2000_NOISE_REQUEST_STATUS : %d\n", drvdata->noise_onoff_flag);
		if (copy_to_user((void __user *)priv_arg, &drvdata->noise_onoff_flag, sizeof(drvdata->noise_onoff_flag)) != 0) {
			pr_err("Failed to copy REQUEST_STATUS to user\n");
			rc = -EFAULT;
		}
#endif
		break;
	case QBT2000_GET_MODELINFO:
		pr_info("QBT2000_GET_MODELINFO : %s\n", drvdata->model_info);
		if (copy_to_user((void __user *)priv_arg, drvdata->model_info, 10)) {
			pr_err("Failed to copy GET_MODELINFO to user\n");
			rc = -EFAULT;
		}
		break;
	default:
		pr_err("invalid cmd %d\n", cmd);
		rc = -ENOIOCTLCMD;
	}

ioctl_failed:
	mutex_unlock(&drvdata->ioctl_mutex);
	return rc;
}


static int get_events_fifo_len_locked(struct qbt2000_drvdata *drvdata, int minor_no)
{
	int len = 0;

	if (minor_no == MINOR_NUM_FD) {
		mutex_lock(&drvdata->fd_events_mutex);
		len = kfifo_len(&drvdata->fd_events);
		mutex_unlock(&drvdata->fd_events_mutex);
	} else if (minor_no == MINOR_NUM_IPC) {
		mutex_lock(&drvdata->ipc_events_mutex);
		len = kfifo_len(&drvdata->ipc_events);
		mutex_unlock(&drvdata->ipc_events_mutex);
	}

	return len;
}

static ssize_t qbt2000_read(struct file *filp, char __user *ubuf,
		size_t cnt, loff_t *ppos)
{
	struct fw_event_desc fw_event;
	struct qbt2000_drvdata *drvdata = filp->private_data;
	wait_queue_head_t *read_wait_queue = NULL;
	int rc = 0;
	int minor_no = iminor(filp->f_path.dentry->d_inode);
	int fifo_len = get_events_fifo_len_locked(drvdata, minor_no);

	if (cnt < sizeof(fw_event.ev)) {
		pr_err("Num bytes to read is too small, numBytes=%zd\n", cnt);
		return -EINVAL;
	}

	if (minor_no == MINOR_NUM_FD) {
		read_wait_queue = &drvdata->read_wait_queue_fd;
	} else if (minor_no == MINOR_NUM_IPC) {
		read_wait_queue = &drvdata->read_wait_queue_ipc;
	} else {
		pr_err("Invalid minor number\n");
		return -EINVAL;
	}

	while (fifo_len == 0) {
		if (filp->f_flags & O_NONBLOCK) {
			pr_debug("fw_events fifo: empty, returning\n");
			return -EAGAIN;
		}
		pr_debug("fw_events fifo: empty, waiting\n");
		if (wait_event_interruptible(*read_wait_queue,
				(get_events_fifo_len_locked(drvdata, minor_no) > 0)))
			return -ERESTARTSYS;

		fifo_len = get_events_fifo_len_locked(drvdata, minor_no);
	}

	if (minor_no == MINOR_NUM_FD) {
		mutex_lock(&drvdata->fd_events_mutex);
		rc = kfifo_get(&drvdata->fd_events, &fw_event);
		mutex_unlock(&drvdata->fd_events_mutex);
	} else if (minor_no == MINOR_NUM_IPC) {
		mutex_lock(&drvdata->ipc_events_mutex);
		rc = kfifo_get(&drvdata->ipc_events, &fw_event);
		mutex_unlock(&drvdata->ipc_events_mutex);
	} else {
		pr_err("Invalid minor number\n");
	}

	if (!rc) {
		pr_err("fw_events fifo: unexpectedly empty\n");
		return -EINVAL;
	}

	rc = copy_to_user(ubuf, &fw_event.ev, sizeof(fw_event.ev));
	if (rc != 0) {
		pr_err("Failed to copy_to_user:%d - event:%d, minor:%d\n",
			rc, (int)fw_event.ev, minor_no);
	} else {
		if (minor_no == MINOR_NUM_FD) {
#ifdef QBT2000_AVOID_NOISE
#ifdef ENABLE_SENSORS_FPRINT_SECURE
			mutex_unlock(&drvdata->fod_event_mutex);
#endif
#endif
			pr_info("Firmware event %d at minor no %d read at time %lu uS, mutex_unlock\n",
				(int)fw_event.ev, minor_no,
				(unsigned long)ktime_to_us(ktime_get()));
		} else {
			pr_info("Firmware event %d at minor no %d read at time %lu uS\n",
				(int)fw_event.ev, minor_no,
				(unsigned long)ktime_to_us(ktime_get()));
		}
	}
	return rc;
}

static unsigned int qbt2000_poll(struct file *filp,
	struct poll_table_struct *wait)
{
	struct qbt2000_drvdata *drvdata = filp->private_data;
	unsigned int mask = 0;
	int minor_no = iminor(filp->f_path.dentry->d_inode);

	if (minor_no == MINOR_NUM_FD) {
		poll_wait(filp, &drvdata->read_wait_queue_fd, wait);
		if (kfifo_len(&drvdata->fd_events) > 0) {
			mask |= (POLLIN | POLLRDNORM);
		}
	} else if (minor_no == MINOR_NUM_IPC) {
		poll_wait(filp, &drvdata->read_wait_queue_ipc, wait);
		if (kfifo_len(&drvdata->ipc_events) > 0) {
			mask |= (POLLIN | POLLRDNORM);
		}
	} else {
		pr_err("Invalid minor number\n");
		return -EINVAL;
	}

	return mask;
}

static const struct file_operations qbt2000_fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = qbt2000_ioctl,
	.open = qbt2000_open,
	.release = qbt2000_release,
	.read = qbt2000_read,
	.poll = qbt2000_poll
};

static int qbt2000_dev_register(struct qbt2000_drvdata *drvdata)
{
	dev_t dev_no, major_no;
	int rc = 0;
	struct device *dev = drvdata->dev;

	rc = alloc_chrdev_region(&dev_no, 0, 2, QBT2000_DEV);
	if (rc) {
		pr_err("alloc_chrdev_region failed %d\n", rc);
		goto err_alloc;
	}
	major_no = MAJOR(dev_no);

	cdev_init(&drvdata->qbt2000_fd_cdev, &qbt2000_fops);
	drvdata->qbt2000_fd_cdev.owner = THIS_MODULE;
	rc = cdev_add(&drvdata->qbt2000_fd_cdev, MKDEV(major_no, MINOR_NUM_FD), 1);
	if (rc) {
		pr_err("cdev_add failed for fd %d\n", rc);
		goto err_cdev_add;
	}

	cdev_init(&drvdata->qbt2000_ipc_cdev, &qbt2000_fops);
	drvdata->qbt2000_ipc_cdev.owner = THIS_MODULE;
	rc = cdev_add(&drvdata->qbt2000_ipc_cdev, MKDEV(major_no, MINOR_NUM_IPC), 1);
	if (rc) {
		pr_err("cdev_add failed for ipc %d\n", rc);
		goto err_cdev_add;
	}

	drvdata->qbt2000_class = class_create(THIS_MODULE, QBT2000_DEV);
	if (IS_ERR(drvdata->qbt2000_class)) {
		rc = PTR_ERR(drvdata->qbt2000_class);
		pr_err("class_create failed %d\n", rc);
		goto err_class_create;
	}

	dev = device_create(drvdata->qbt2000_class, NULL, drvdata->qbt2000_fd_cdev.dev,
			drvdata, "%s_fd", QBT2000_DEV);
	if (IS_ERR(dev)) {
		rc = PTR_ERR(dev);
		pr_err("fd device_create failed %d\n", rc);
		goto err_dev_create_fd;
	}

	dev = device_create(drvdata->qbt2000_class, NULL, drvdata->qbt2000_ipc_cdev.dev,
			drvdata, "%s_ipc", QBT2000_DEV);
	if (IS_ERR(dev)) {
		rc = PTR_ERR(dev);
		pr_err("ipc device_create failed %d\n", rc);
		goto err_dev_create_ipc;
	}
	pr_info("finished\n");
	return 0;
err_dev_create_ipc:
	device_destroy(drvdata->qbt2000_class, drvdata->qbt2000_fd_cdev.dev);
err_dev_create_fd:
	class_destroy(drvdata->qbt2000_class);
err_class_create:
	cdev_del(&drvdata->qbt2000_fd_cdev);
	cdev_del(&drvdata->qbt2000_ipc_cdev);
err_cdev_add:
	unregister_chrdev_region(drvdata->qbt2000_fd_cdev.dev, 1);
	unregister_chrdev_region(drvdata->qbt2000_ipc_cdev.dev, 1);
err_alloc:
	return rc;
}

int qbt2000_pinctrl_register(struct qbt2000_drvdata *drvdata)
{
	int rc = 0;

	drvdata->p = pinctrl_get_select_default(drvdata->dev);
	if (IS_ERR(drvdata->p)) {
		rc = -EINVAL;
		pr_err("failed pinctrl_get\n");
		goto pinctrl_register_default_exit;
	}

#if !defined(ENABLE_SENSORS_FPRINT_SECURE) || defined(DISABLED_GPIO_PROTECTION)
	drvdata->pins_poweroff = pinctrl_lookup_state(drvdata->p, "pins_poweroff");
	if (IS_ERR(drvdata->pins_poweroff)) {
		pr_err("could not get pins sleep_state (%li)\n",
			PTR_ERR(drvdata->pins_poweroff));
		drvdata->pins_poweroff = NULL;
		drvdata->pins_poweron = NULL;
		goto pinctrl_register_exit;
	}

	drvdata->pins_poweron = pinctrl_lookup_state(drvdata->p, "pins_poweron");
	if (IS_ERR(drvdata->pins_poweron)) {
		pr_err("could not get pins idle_state (%li)\n",
			PTR_ERR(drvdata->pins_poweron));
		drvdata->pins_poweron = NULL;
		goto pinctrl_register_exit;
	}
#endif
	pr_info("finished\n");
	return rc;
#if !defined(ENABLE_SENSORS_FPRINT_SECURE) || defined(DISABLED_GPIO_PROTECTION)
pinctrl_register_exit:
	pinctrl_put(drvdata->p);
#endif
pinctrl_register_default_exit:
	pr_err("failed %d\n", rc);
	return rc;
}

static void qbt2000_gpio_report_event(struct qbt2000_drvdata *drvdata)
{
	int state;
	struct fw_event_desc fw_event;

	state = (__gpio_get_value(drvdata->fd_gpio.gpio) ? FINGER_DOWN_GPIO_STATE : FINGER_LEAVE_GPIO_STATE)
		^ drvdata->fd_gpio.active_low;

	if (state == drvdata->fd_gpio.last_gpio_state) {
#ifdef QBT2000_AVOID_NOISE
#ifdef ENABLE_SENSORS_FPRINT_SECURE
		pr_debug("skip the report_event. this event already reported, last_gpio:%d\n", state);
		mutex_unlock(&drvdata->fod_event_mutex);
#endif
#endif
		return;
	}

	drvdata->fd_gpio.last_gpio_state = state;

	fw_event.ev = (state ? FW_EVENT_FINGER_DOWN : FW_EVENT_FINGER_UP);

	mutex_lock(&drvdata->fd_events_mutex);

	kfifo_reset(&drvdata->fd_events);

	if (!kfifo_put(&drvdata->fd_events, fw_event))
		pr_err("fw events fifo: error adding item\n");

	mutex_unlock(&drvdata->fd_events_mutex);
	wake_up_interruptible(&drvdata->read_wait_queue_fd);
	pr_info("state: %s\n", state ? "Finger Down" : "Finger Leave");
}

static void qbt2000_wuhb_work_func(struct work_struct *work)
{
	struct qbt2000_drvdata *drvdata =
		container_of(work, struct qbt2000_drvdata, fd_gpio.work);
#ifdef QBT2000_AVOID_NOISE
#ifdef ENABLE_SENSORS_FPRINT_SECURE
	mutex_lock(&drvdata->fod_event_mutex);
	pr_debug("mutex_lock\n");
#endif
#endif
	qbt2000_gpio_report_event(drvdata);
}

#ifdef QBT2000_AVOID_NOISE
static void qbt2000_wuhb_work_noise_down_func(struct work_struct *work)
{
	struct qbt2000_drvdata *drvdata =
		container_of(work, struct qbt2000_drvdata, fd_gpio.work_noise_down);
	int delay_time = QBT2000_NOISE_BLOCK_DELAY;

#ifdef ENABLE_SENSORS_FPRINT_SECURE
	mutex_lock(&drvdata->fod_event_mutex);
	pr_debug("mutex_lock\n");
#endif

	schedule_delayed_work(&drvdata->fd_gpio.delayed_noise_down_work, msecs_to_jiffies(delay_time));
	qbt2000_noise_control(drvdata, QBT2000_NOISE_BLOCK);
}
static void qbt2000_ipc_handler_noise_status(struct work_struct *work)
{
	struct qbt2000_drvdata *drvdata =
			container_of(work, struct qbt2000_drvdata, work_ipc_noise_status);

	drvdata->noise_status = get_wacom_scan_info(true);
	pr_info("entry : %d\n", drvdata->noise_status);
	if (drvdata->noise_status < 0)
		drvdata->i2c_error_get++;
	else if (drvdata->noise_status == QBT2000_NOISE_CHARGING)
		drvdata->i2c_charging++;

}

static void qbt2000_work_noise_control(struct work_struct *work)
{
	struct qbt2000_drvdata *drvdata =
		container_of(work, struct qbt2000_drvdata, work_noise_control);
	int rc = 0;

	pr_info("entry\n");
	rc = qbt2000_noise_control(drvdata, QBT2000_NOISE_BLOCK);
	drvdata->noise_i2c_result = rc;
	pr_info("done : %d\n", drvdata->noise_i2c_result);
}

static void qbt2000_gpio_report_event_delayed(struct qbt2000_drvdata *drvdata, int state)
{
	struct fw_event_desc fw_event;
	int rc = 0;

	if (state == drvdata->fd_gpio.last_gpio_state) {
		pr_info("already done %d %d, rc = %d\n", state, drvdata->fd_gpio.last_gpio_state, rc);
#ifdef ENABLE_SENSORS_FPRINT_SECURE
		mutex_unlock(&drvdata->fod_event_mutex);
#endif
		return;
	}
	drvdata->fd_gpio.last_gpio_state = state;

	fw_event.ev = (state ? FW_EVENT_FINGER_DOWN : FW_EVENT_FINGER_UP);

	mutex_lock(&drvdata->fd_events_mutex);

	kfifo_reset(&drvdata->fd_events);

	if (!kfifo_put(&drvdata->fd_events, fw_event))
		pr_err("fw events fifo: error adding item\n");

	mutex_unlock(&drvdata->fd_events_mutex);
	wake_up_interruptible(&drvdata->read_wait_queue_fd);
	pr_info("state: %s\n", state ? "Finger Down" : "Finger Leave");
}

static void qbt2000_wuhb_delayed_work_func(struct work_struct *work)
{
	struct qbt2000_drvdata *drvdata =
		container_of(work, struct qbt2000_drvdata, fd_gpio.delayed_noise_down_work.work);

	qbt2000_gpio_report_event_delayed(drvdata, drvdata->now_state);
}
#endif

static irqreturn_t qbt2000_wuhb_irq_handler(int irq, void *dev_id)
{
	struct qbt2000_drvdata *drvdata = dev_id;

	if (irq != drvdata->fd_gpio.irq) {
		pr_warn("invalid irq %d (expected %d)\n", irq, drvdata->fd_gpio.irq);
		return IRQ_HANDLED;
	}

	if (drvdata->wuhb_test_flag == 1) {
		pr_info("IntTest. interrupt occured.flag:%d,result:%d,en:%d\n",
					drvdata->wuhb_test_flag,
					drvdata->wuhb_test_result,
					drvdata->enabled_wuhb);
		drvdata->wuhb_test_result = 1;
		return IRQ_HANDLED;
	}

	drvdata->wuhb_count++;
	wake_lock_timeout(&drvdata->fp_signal_lock,
			msecs_to_jiffies(QBT2000_WAKELOCK_HOLD_TIME));
#ifndef QBT2000_AVOID_NOISE // not use digitizer
	schedule_work(&drvdata->fd_gpio.work);
#else
#ifndef ENABLE_SENSORS_FPRINT_SECURE // use digitizer but nontz
	schedule_work(&drvdata->fd_gpio.work);
#else // use digitizer and tz
	drvdata->now_state = (__gpio_get_value(drvdata->fd_gpio.gpio) ? FINGER_DOWN_GPIO_STATE : FINGER_LEAVE_GPIO_STATE) ^ drvdata->fd_gpio.active_low;
	pr_info("gpio state : %d\n", drvdata->now_state);
	if (drvdata->now_state == FINGER_DOWN_GPIO_STATE) {  // in case of finger down
		schedule_work(&drvdata->fd_gpio.work_noise_down);
	} else {
		schedule_work(&drvdata->fd_gpio.work);
	}
#endif //ENABLE_SENSORS_FPRINT_SECURE
#endif //QBT2000_AVOID_NOISE

	return IRQ_HANDLED;
}

/*
 * qbt2000_ipc_irq_handler() - function processes IPC
 * interrupts on its own thread
 * @irq:	the interrupt that occurred
 * @dev_id: pointer to the qbt2000_drvdata
 *
 * Return: IRQ_HANDLED when complete
 */
static irqreturn_t qbt2000_ipc_irq_handler(int irq, void *dev_id)
{
	struct qbt2000_drvdata *drvdata = (struct qbt2000_drvdata *)dev_id;
	enum qbt2000_fw_event ev = FW_EVENT_CBGE_REQUIRED;
	struct fw_event_desc fw_ev_des;

	wake_lock_timeout(&drvdata->fp_signal_lock,
			msecs_to_jiffies(QBT2000_WAKELOCK_HOLD_TIME));
	mutex_lock(&drvdata->mutex);

	if (irq != drvdata->fw_ipc.irq) {
		pr_warn("invalid irq %d (expected %d)\n", irq, drvdata->fw_ipc.irq);
		goto ipc_irq_failed;
	}
#ifdef ENABLE_SENSORS_FPRINT_SECURE
#ifdef QBT2000_AVOID_NOISE
	schedule_work(&drvdata->work_ipc_noise_status);
#endif
#endif

	mutex_lock(&drvdata->ipc_events_mutex);
	fw_ev_des.ev = ev;
	if (!kfifo_put(&drvdata->ipc_events, fw_ev_des))
		pr_err("fw events: fifo full, drop event %d\n", (int) ev);

	drvdata->cbge_count++;
	mutex_unlock(&drvdata->ipc_events_mutex);

	wake_up_interruptible(&drvdata->read_wait_queue_ipc);
	pr_debug("ipc interrupt received, irq=%d, event=%d\n", irq, (int)ev);
ipc_irq_failed:
	mutex_unlock(&drvdata->mutex);
	return IRQ_HANDLED;
}

static int qbt2000_setup_fd_gpio_irq(struct platform_device *pdev,
		struct qbt2000_drvdata *drvdata)
{
	int rc = 0;
	int irq;
	const char *desc = "qbt_finger_detect";

	rc = devm_gpio_request_one(&pdev->dev, drvdata->fd_gpio.gpio,
		GPIOF_IN, desc);
	if (rc < 0) {
		pr_err("failed to request gpio %d, error %d\n",
			drvdata->fd_gpio.gpio, rc);
		goto fd_gpio_failed;
	}

	irq = gpio_to_irq(drvdata->fd_gpio.gpio);
	if (irq < 0) {
		rc = irq;
		pr_err("unable to get irq number for gpio %d, error %d\n",
			drvdata->fd_gpio.gpio, rc);
		goto fd_gpio_failed_request;
	}

	drvdata->fd_gpio.irq = irq;
	INIT_WORK(&drvdata->fd_gpio.work, qbt2000_wuhb_work_func);
#ifdef QBT2000_AVOID_NOISE
	INIT_WORK(&drvdata->fd_gpio.work_noise_down, qbt2000_wuhb_work_noise_down_func);
	INIT_DELAYED_WORK(&drvdata->fd_gpio.delayed_noise_down_work, qbt2000_wuhb_delayed_work_func);
	INIT_WORK(&drvdata->work_ipc_noise_status, qbt2000_ipc_handler_noise_status);
	INIT_WORK(&drvdata->work_noise_control, qbt2000_work_noise_control);
#endif
	rc = devm_request_any_context_irq(&pdev->dev, drvdata->fd_gpio.irq,
		qbt2000_wuhb_irq_handler, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
		desc, drvdata);
	if (rc < 0) {
		pr_err("unable to claim irq %d; error %d\n",
			drvdata->fd_gpio.irq, rc);
		goto fd_gpio_failed_request;
	}
	enable_irq_wake(drvdata->fd_gpio.irq);
	drvdata->enabled_wuhb = true;
	qbt2000_disable_wuhb(drvdata);
	pr_debug("irq=%d,gpio=%d,rc=%d\n", drvdata->fd_gpio.irq, drvdata->fd_gpio.gpio, rc);
fd_gpio_failed_request:
	gpio_free(drvdata->fd_gpio.gpio);
fd_gpio_failed:
	return rc;
}

static int qbt2000_setup_ipc_irq(struct platform_device *pdev,
	struct qbt2000_drvdata *drvdata)
{
	int rc = 0;
	const char *desc = "qbt_ipc";

	drvdata->fw_ipc.irq = gpio_to_irq(drvdata->fw_ipc.gpio);
	if (drvdata->fw_ipc.irq < 0) {
		rc = drvdata->fw_ipc.irq;
		pr_err("no irq for gpio %d, error=%d\n",
		  drvdata->fw_ipc.gpio, rc);
		goto ipc_gpio_failed;
	}

	rc = devm_gpio_request_one(&pdev->dev, drvdata->fw_ipc.gpio,
			GPIOF_IN, desc);

	if (rc < 0) {
		pr_err("failed to request gpio %d, error %d\n",
			drvdata->fw_ipc.gpio, rc);
		goto ipc_gpio_failed;
	}

	rc = devm_request_threaded_irq(&pdev->dev,
		drvdata->fw_ipc.irq,
		NULL,
		qbt2000_ipc_irq_handler,
		IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
		desc,
		drvdata);

	if (rc < 0) {
		pr_err("failed to register for ipc irq %d, rc = %d\n",
			drvdata->fw_ipc.irq, rc);
		goto ipc_gpio_failed_request;
	}

#ifdef ENABLE_SENSORS_FPRINT_SECURE
	enable_irq_wake(drvdata->fw_ipc.irq);
#endif
	drvdata->enabled_ipc = true;
	qbt2000_disable_ipc(drvdata);
	pr_debug("irq=%d,gpio=%d,rc=%d\n", drvdata->fw_ipc.irq, drvdata->fw_ipc.gpio, rc);
ipc_gpio_failed_request:
	gpio_free(drvdata->fw_ipc.gpio);
ipc_gpio_failed:
	return rc;
}

/**
 * qbt2000_read_device_tree() - Function reads device tree
 * properties into driver data
 * @pdev:	ptr to platform device object
 * @drvdata:	ptr to driver data
 *
 * Return: 0 on success. Error code on failure.
 */
static int qbt2000_read_device_tree(struct platform_device *pdev,
	struct qbt2000_drvdata *drvdata)
{
	int rc = 0;
	enum of_gpio_flags flags;

	/* read IPC gpio */
	drvdata->fw_ipc.gpio = of_get_named_gpio(pdev->dev.of_node,
		"qcom,ipc-gpio", 0);
	if (drvdata->fw_ipc.gpio < 0) {
		rc = drvdata->fw_ipc.gpio;
		pr_err("ipc gpio not found, error=%d\n", rc);
		goto dt_failed;
	}

	/* read WUHB gpio */
	drvdata->fd_gpio.gpio = of_get_named_gpio_flags(pdev->dev.of_node,
				"qcom,wuhb-gpio", 0, &flags);
	if (drvdata->fd_gpio.gpio < 0) {
		rc = drvdata->fd_gpio.gpio;
		pr_err("wuhb gpio not found, error=%d\n", rc);
		goto dt_failed;
	} else {
		drvdata->fd_gpio.active_low = flags & OF_GPIO_ACTIVE_LOW;
	}

	drvdata->ldogpio = of_get_named_gpio(pdev->dev.of_node, "qcom,ldo-gpio", 0);
	if (drvdata->ldogpio < 0) {
		pr_info("ldo gpio not found. %d\n", drvdata->ldogpio);
	} else {			
		rc = gpio_request(drvdata->ldogpio, "qbt_ldo_en");
		gpio_direction_output(drvdata->ldogpio, 0);
		drvdata->enabled_ldo = 0;
	}

	if (of_property_read_string(pdev->dev.of_node, "qcom,btp-regulator", &drvdata->btp_vdd) < 0) {
		pr_info("not use btp_regulator\n");
		drvdata->btp_vdd = NULL;
	} else {
		drvdata->regulator_1p8 = regulator_get(NULL, drvdata->btp_vdd);
		if (IS_ERR(drvdata->regulator_1p8) ||
				(drvdata->regulator_1p8) == NULL) {
			pr_info("not use regulator_1p8\n");
			drvdata->regulator_1p8 = NULL;
		} else {
			pr_info("btp_regulator ok\n");
		}
	}

	if (of_property_read_u32(pdev->dev.of_node, "qcom,min_cpufreq_limit",
				&drvdata->min_cpufreq_limit))
		drvdata->min_cpufreq_limit = 0;

	if (of_property_read_string_index(pdev->dev.of_node, "qcom,position", 0,
			(const char **)&drvdata->sensor_position))
		drvdata->sensor_position = "13.77,0.00,9.00,4.00,14.80,14.80,11.00,11.00,5.00";
	pr_info("Sensor Position: %s\n", drvdata->sensor_position);

	if (of_property_read_string_index(pdev->dev.of_node, "qcom,modelinfo", 0,
			(const char **)&drvdata->model_info)) {
		drvdata->model_info = "NONE";
	}
	pr_info("modelinfo: %s\n", drvdata->model_info);

	pr_info("finished\n");
	return rc;
dt_failed:
	pr_err("failed:%d\n", rc);
	return rc;
}

static void qbt2000_work_func_debug(struct work_struct *work)
{
#ifndef QBT2000_AVOID_NOISE
	pr_info("ldo:%d,ipc:%d,wuhb:%d,tz:%d,type:%s,int:%d,%d\n",
		g_data->enabled_ldo, g_data->enabled_ipc,
		g_data->enabled_wuhb, g_data->tz_mode,
		sensor_status[g_data->sensortype + 2],
		g_data->cbge_count, g_data->wuhb_count);
#else
	pr_info("ldo:%d,ipc:%d,wuhb:%d,tz:%d,type:%s,int:%d,%d,%d,%d,%d,%d,%d\n",
		g_data->enabled_ldo, g_data->enabled_ipc,
		g_data->enabled_wuhb, g_data->tz_mode,
		sensor_status[g_data->sensortype + 2],
		g_data->cbge_count, g_data->ignored_cbge_count,
		g_data->wuhb_count, g_data->i2c_error_set,
		g_data->i2c_error_get, g_data->i2c_charging,
		g_data->noise_onoff_flag);
#endif
}

static void qbt2000_enable_debug_timer(void)
{
	mod_timer(&g_data->dbg_timer,
		round_jiffies_up(jiffies + FPSENSOR_DEBUG_TIMER_SEC));
}

static void qbt2000_disable_debug_timer(void)
{
	del_timer_sync(&g_data->dbg_timer);
	cancel_work_sync(&g_data->work_debug);
}

static void qbt2000_timer_func(unsigned long ptr)
{
	queue_work(g_data->wq_dbg, &g_data->work_debug);
	mod_timer(&g_data->dbg_timer,
		round_jiffies_up(jiffies + FPSENSOR_DEBUG_TIMER_SEC));
}

static int qbt2000_set_timer(struct qbt2000_drvdata *drvdata)
{
	int rc = 0;

	setup_timer(&drvdata->dbg_timer, qbt2000_timer_func,
			(unsigned long)drvdata);
	drvdata->wq_dbg = create_singlethread_workqueue("qbt2000_debug_wq");
	if (!drvdata->wq_dbg) {
		rc = -ENOMEM;
		pr_err("could not create workqueue\n");
		return rc;
	}
	INIT_WORK(&drvdata->work_debug, qbt2000_work_func_debug);

	return rc;
}

/**
 * qbt2000_probe() - Function loads hardware config from device tree
 * @pdev:	ptr to platform device object
 *
 * Return: 0 on success. Error code on failure.
 */
static int qbt2000_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct qbt2000_drvdata *drvdata;
	int rc = 0;

	pr_info("Start\n");
#ifdef CONFIG_BATTERY_SAMSUNG
	if (lpcharge) {
		pr_info("Do not load driver due to : lpm %d\n", lpcharge);
		return rc;
	}
#endif
	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
	if (!drvdata)
		return -ENOMEM;

	drvdata->dev = &pdev->dev;
	platform_set_drvdata(pdev, drvdata);

	rc = qbt2000_read_device_tree(pdev, drvdata);
	if (rc < 0)
		goto probe_failed_dt;

	atomic_set(&drvdata->fd_available, 1);
	atomic_set(&drvdata->ipc_available, 1);

	mutex_init(&drvdata->mutex);
	mutex_init(&drvdata->ioctl_mutex);
	mutex_init(&drvdata->fd_events_mutex);
	mutex_init(&drvdata->ipc_events_mutex);
#ifdef QBT2000_AVOID_NOISE
	mutex_init(&drvdata->fod_event_mutex);
#endif

	rc = qbt2000_dev_register(drvdata);
	if (rc < 0)
		goto probe_failed_dev_register;

	INIT_KFIFO(drvdata->fd_events);
	INIT_KFIFO(drvdata->ipc_events);
	init_waitqueue_head(&drvdata->read_wait_queue_fd);
	init_waitqueue_head(&drvdata->read_wait_queue_ipc);

	wake_lock_init(&drvdata->fp_spi_lock,
			WAKE_LOCK_SUSPEND, "qbt2000_spi_lock");
	wake_lock_init(&drvdata->fp_signal_lock,
			WAKE_LOCK_SUSPEND, "qbt2000_signal_lock");

	rc = qbt2000_pinctrl_register(drvdata);
	if (rc < 0)
		goto probe_failed_pinctrl;

	rc = qbt2000_setup_fd_gpio_irq(pdev, drvdata);
	if (rc < 0)
		goto probe_failed_fd_gpio;

	rc = qbt2000_setup_ipc_irq(pdev, drvdata);
	if (rc < 0)
		goto probe_failed_ipc_gpio;

	rc = device_init_wakeup(&pdev->dev, 1);
	if (rc < 0)
		goto probe_failed_device_init_wakeup;

	rc = qbt2000_register_platform_variable(drvdata);
	if (rc < 0)
		goto probe_failed_platform_variable;

	rc = fingerprint_register(drvdata->fp_device,
		drvdata, fp_attrs, "fingerprint");
	if (rc ) {
		pr_err("sysfs register failed\n");
		goto probe_failed_sysfs_register;
	}

#ifndef ENABLE_SENSORS_FPRINT_SECURE
	qbt2000_power_control(drvdata, 1);
#endif

	g_data = drvdata;
	drvdata->spi_speed = SPI_CLOCK_MAX;
#ifdef ENABLE_SENSORS_FPRINT_SECURE
	drvdata->enabled_clk = false;
	drvdata->tz_mode = true;
#else
	drvdata->enabled_clk = true;
	drvdata->tz_mode = false;
#endif
	drvdata->sensortype = SENSOR_QBT2000;
	drvdata->cbge_count = 0;
#ifdef QBT2000_AVOID_NOISE
	drvdata->ignored_cbge_count = 0;
	drvdata->i2c_error_set = 0;
	drvdata->i2c_error_get = 0;
	drvdata->i2c_charging = 0;
	drvdata->noise_status = QBT2000_NOISE_NO_CHARGING;
	drvdata->noise_onoff_flag = QBT2000_NOISE_UNBLOCK;
#endif
	drvdata->wuhb_count = 0;
	drvdata->reset_count = 0;
	drvdata->wuhb_test_flag = 0;
	drvdata->wuhb_test_result = 0;

	qbt2000_set_timer(drvdata);
	qbt2000_enable_debug_timer();

	pr_info("finished\n");
	return 0;

probe_failed_sysfs_register:
probe_failed_platform_variable:
probe_failed_device_init_wakeup:
	gpio_free(drvdata->fw_ipc.gpio);
probe_failed_ipc_gpio:
	gpio_free(drvdata->fd_gpio.gpio);
probe_failed_fd_gpio:
	pinctrl_put(drvdata->p);
probe_failed_pinctrl:
	wake_lock_destroy(&drvdata->fp_spi_lock);
	wake_lock_destroy(&drvdata->fp_signal_lock);
	device_destroy(drvdata->qbt2000_class, drvdata->qbt2000_ipc_cdev.dev);
	device_destroy(drvdata->qbt2000_class, drvdata->qbt2000_fd_cdev.dev);
	class_destroy(drvdata->qbt2000_class);
	cdev_del(&drvdata->qbt2000_fd_cdev);
	cdev_del(&drvdata->qbt2000_ipc_cdev);
	unregister_chrdev_region(drvdata->qbt2000_fd_cdev.dev, 1);
	unregister_chrdev_region(drvdata->qbt2000_ipc_cdev.dev, 1);
probe_failed_dev_register:
	if (drvdata->regulator_1p8)
		regulator_put(drvdata->regulator_1p8);
probe_failed_dt:
	kfree(drvdata);
	pr_err("failed: %d\n", rc);
	return rc;
}

static int qbt2000_remove(struct platform_device *pdev)
{
	struct qbt2000_drvdata *drvdata = platform_get_drvdata(pdev);

	pr_info("called\n");

	mutex_destroy(&drvdata->mutex);
	mutex_destroy(&drvdata->ioctl_mutex);
	mutex_destroy(&drvdata->fd_events_mutex);
	mutex_destroy(&drvdata->ipc_events_mutex);
#ifdef QBT2000_AVOID_NOISE
	mutex_destroy(&drvdata->fod_event_mutex);
#endif

	device_destroy(drvdata->qbt2000_class, drvdata->qbt2000_fd_cdev.dev);
	device_destroy(drvdata->qbt2000_class, drvdata->qbt2000_ipc_cdev.dev);

	qbt2000_disable_debug_timer();
	if (drvdata->regulator_1p8)
		regulator_put(drvdata->regulator_1p8);
	wake_lock_destroy(&drvdata->fp_spi_lock);
	wake_lock_destroy(&drvdata->fp_signal_lock);
	class_destroy(drvdata->qbt2000_class);
	cdev_del(&drvdata->qbt2000_fd_cdev);
	cdev_del(&drvdata->qbt2000_ipc_cdev);
	unregister_chrdev_region(drvdata->qbt2000_fd_cdev.dev, 1);
	unregister_chrdev_region(drvdata->qbt2000_ipc_cdev.dev, 1);
	fingerprint_unregister(drvdata->fp_device, fp_attrs);
	device_init_wakeup(&pdev->dev, 0);
	qbt2000_unregister_platform_variable(drvdata);
	pinctrl_put(drvdata->p);
	kfree(drvdata);

	return 0;
}

static int qbt2000_suspend(struct platform_device *pdev, pm_message_t state)
{
	int rc = 0;
	struct qbt2000_drvdata *drvdata = platform_get_drvdata(pdev);

#ifdef CONFIG_BATTERY_SAMSUNG
	if (lpcharge)
		return rc;
#endif

#if defined(ENABLE_SENSORS_FPRINT_SECURE)
	fpsensor_goto_suspend = 1;
#endif

	/*
	 * Returning an error code if driver currently making a TZ call.
	 * Note: The purpose of this driver is to ensure that the clocks are on
	 * while making a TZ call. Hence the clock check to determine if the
	 * driver will allow suspend to occur.
	 */
	if (!mutex_trylock(&drvdata->mutex))
		return -EBUSY;
	else {
#ifndef ENABLE_SENSORS_FPRINT_SECURE
		qbt2000_power_control(drvdata, 0);
#endif
		qbt2000_disable_debug_timer();
		pr_info("ret = %d\n", rc);
	}
	mutex_unlock(&drvdata->mutex);

	return 0;
}

static int qbt2000_resume(struct platform_device *pdev)
{
	int rc = 0;

#ifdef CONFIG_BATTERY_SAMSUNG
	if (lpcharge)
		return rc;
#endif

#if defined(ENABLE_SENSORS_FPRINT_SECURE)
	if (fpsensor_goto_suspend) {
		fps_resume_set();
	}
#else
	qbt2000_power_control(g_data, 1);
#endif
	qbt2000_enable_debug_timer();
	pr_info("ret = %d\n", rc);

	return 0;
}

static const struct of_device_id qbt2000_match[] = {
	{ .compatible = "qcom,qbt2000" },
	{}
};

static struct platform_driver qbt2000_plat_driver = {
	.probe = qbt2000_probe,
	.remove = qbt2000_remove,
	.suspend = qbt2000_suspend,
	.resume = qbt2000_resume,
	.driver = {
		.name = QBT2000_DEV,
		.owner = THIS_MODULE,
		.of_match_table = qbt2000_match,
	},
};

module_platform_driver(qbt2000_plat_driver);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Kangwook.Her");
MODULE_DESCRIPTION("Samsung Electronics Inc. QBT2000 driver");