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
|
/*
* Copyright (C) 2013 Samsung Electronics. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "fingerprint.h"
#include "et310.h"
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/sched.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/io.h>
#include <linux/interrupt.h>
#include <linux/uaccess.h>
#include <mach/hardware.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
static DECLARE_BITMAP(minors, N_SPI_MINORS);
static LIST_HEAD(device_list);
static DEFINE_MUTEX(device_list_lock);
static int gpio_irq;
static struct etspi_data *g_data;
static DECLARE_WAIT_QUEUE_HEAD(interrupt_waitq);
static unsigned bufsiz = 4096;
module_param(bufsiz, uint, S_IRUGO);
MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
static void etspi_interrupt_timer_routine(unsigned long _data)
{
struct etspi_data *etspi = (struct etspi_data *)_data;
pr_info("%s FPS interrupt count = %d\n", __func__, etspi->int_count);
if (etspi->int_count >= etspi->detect_threshold) {
etspi->finger_on = 1;
pr_info("%s FPS triggered\n", __func__);
}
etspi->int_count = 0;
wake_up_interruptible(&interrupt_waitq);
}
static irqreturn_t etspi_fingerprint_interrupt(int irq , void *dev_id)
{
struct etspi_data *etspi = (struct etspi_data *)dev_id;
if (!etspi->int_count)
mod_timer(&etspi->timer,
jiffies + msecs_to_jiffies(etspi->detect_period));
etspi->int_count++;
return IRQ_HANDLED;
}
int etspi_Interrupt_Init(
struct etspi_data *etspi,
int int_num,
int detect_period,
int detect_threshold)
{
int status = 0;
pr_info("%s\n", __func__);
pr_info("FP %s int_num = %d detect_period = %d detect_threshold = %d\n",
__func__,
int_num,
detect_period,
detect_threshold);
etspi->int_count = 0;
etspi->detect_period = detect_period;
etspi->detect_threshold = detect_threshold;
gpio_irq = gpio_to_irq(etspi->drdyPin);
if (gpio_irq < 0) {
pr_err("%s gpio_to_irq failed\n", __func__);
status = gpio_irq;
goto done;
}
if (request_irq
(gpio_irq, etspi_fingerprint_interrupt, IRQ_TYPE_EDGE_RISING,
"etspi_irq", etspi) < 0) {
etspi->drdy_irq_flag = DRDY_IRQ_DISABLE;
pr_err("%s drdy request_irq failed\n", __func__);
status = -EBUSY;
goto done;
} else {
etspi->drdy_irq_flag = DRDY_IRQ_ENABLE;
}
done:
etspi->finger_on = 0;
return 0;
}
int etspi_Interrupt_Free(struct etspi_data *etspi)
{
pr_info("%s\n", __func__);
if (etspi != NULL)
free_irq(gpio_irq, etspi);
return 0;
}
void etspi_Interrupt_Abort(struct etspi_data *etspi)
{
etspi->finger_on = 0;
wake_up_interruptible(&interrupt_waitq);
}
unsigned int etspi_fps_interrupt_poll(
struct file *file,
struct poll_table_struct *wait)
{
unsigned int mask = 0;
struct etspi_data *etspi = file->private_data;
pr_info("%s FPS fps_interrupt_poll\n", __func__);
etspi->int_count = 0;
poll_wait(file, &interrupt_waitq, wait);
if (etspi->finger_on) {
mask |= POLLIN | POLLRDNORM;
etspi->finger_on = 0;
}
return mask;
}
/*-------------------------------------------------------------------------*/
static void etspi_reset(struct etspi_data *etspi)
{
pr_info("%s\n", __func__);
pr_info("%s set gpio %d value %d\n", __func__, etspi->sleepPin, 0);
gpio_set_value(etspi->sleepPin, 0);
usleep_range(50, 100);
pr_info("%s set gpio %d value %d\n", __func__, etspi->sleepPin, 1);
gpio_set_value(etspi->sleepPin, 1);
usleep_range(50, 100);
pr_info("%s end\n", __func__);
}
static void etspi_reset_set(struct etspi_data *etspi, int enable)
{
pr_info("%s\n", __func__);
pr_info("%s enable %d\n", __func__, enable);
if (enable == 0)
gpio_set_value(etspi->sleepPin, 0);
else
gpio_set_value(etspi->sleepPin, 1);
usleep_range(50, 100);
}
static void etspi_power_control(struct etspi_data *etspi, int status)
{
pr_info("%s status = %d\n", __func__, status);
if (status == 1) {
gpio_set_value(etspi->ocp_en, 1);
usleep_range(2950, 3000);
gpio_set_value(etspi->ldo_pin, 1);
if (etspi->ldo_pin2)
gpio_set_value(etspi->ldo_pin2, 1);
msleep(20);
} else if (status == 0) {
gpio_set_value(etspi->ldo_pin, 0);
if (etspi->ldo_pin2)
gpio_set_value(etspi->ldo_pin2, 0);
usleep_range(2950, 3000);
gpio_set_value(etspi->ocp_en, 0);
}
pr_info("%s end\n", __func__);
}
static ssize_t etspi_read(struct file *filp,
char __user *buf,
size_t count,
loff_t *f_pos)
{
/*Implement by vendor if needed*/
return 0;
}
static ssize_t etspi_write(struct file *filp,
const char __user *buf,
size_t count,
loff_t *f_pos)
{
/*Implement by vendor if needed*/
return 0;
}
#ifdef ENABLE_SENSORS_FPRINT_SECURE
static int sec_spi_prepare(struct sec_spi_info *spi_info, struct spi_device *spi)
{
struct s3c64xx_spi_csinfo *cs;
struct s3c64xx_spi_driver_data *sdd = NULL;
sdd = spi_master_get_devdata(spi->master);
if (!sdd)
return -EFAULT;
pm_runtime_get_sync(&sdd->pdev->dev);
/* set spi clock rate */
clk_set_rate(sdd->src_clk, spi_info->speed * 2);
/* enable chip select */
cs = spi->controller_data;
if(cs->line != 0)
gpio_set_value(cs->line, 0);
return 0;
}
static int sec_spi_unprepare(struct sec_spi_info *spi_info, struct spi_device *spi)
{
struct s3c64xx_spi_csinfo *cs;
struct s3c64xx_spi_driver_data *sdd = NULL;
sdd = spi_master_get_devdata(spi->master);
if (!sdd)
return -EFAULT;
/* disable chip select */
cs = spi->controller_data;
if(cs->line != 0)
gpio_set_value(cs->line, 1);
pm_runtime_put(&sdd->pdev->dev);
return 0;
}
#endif
static long etspi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
int err = 0, retval = 0;
struct etspi_data *etspi;
struct spi_device *spi;
u32 tmp;
struct egis_ioc_transfer *ioc = NULL;
#ifdef ENABLE_SENSORS_FPRINT_SECURE
struct sec_spi_info *spi_info = NULL;
#endif
/* Check type and command number */
if (_IOC_TYPE(cmd) != EGIS_IOC_MAGIC) {
pr_err("%s _IOC_TYPE(cmd) != EGIS_IOC_MAGIC", __func__);
return -ENOTTY;
}
/* Check access direction once here; don't repeat below.
* IOC_DIR is from the user perspective, while access_ok is
* from the kernel perspective; so they look reversed.
*/
if (_IOC_DIR(cmd) & _IOC_READ)
err = !access_ok(VERIFY_WRITE,
(void __user *)arg,
_IOC_SIZE(cmd));
if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
err = !access_ok(VERIFY_READ,
(void __user *)arg,
_IOC_SIZE(cmd));
if (err) {
pr_err("%s err", __func__);
return -EFAULT;
}
/* guard against device removal before, or while,
* we issue this ioctl.
*/
etspi = filp->private_data;
spin_lock_irq(&etspi->spi_lock);
spi = spi_dev_get(etspi->spi);
spin_unlock_irq(&etspi->spi_lock);
if (spi == NULL) {
pr_err("%s spi == NULL", __func__);
return -ESHUTDOWN;
}
mutex_lock(&etspi->buf_lock);
/* segmented and/or full-duplex I/O request */
if (_IOC_NR(cmd) != _IOC_NR(EGIS_IOC_MESSAGE(0))
|| _IOC_DIR(cmd) != _IOC_WRITE) {
retval = -ENOTTY;
goto out;
}
tmp = _IOC_SIZE(cmd);
if ((tmp == 0) || (tmp % sizeof(struct egis_ioc_transfer)) != 0) {
retval = -EINVAL;
goto out;
}
/* copy into scratch area */
ioc = kmalloc(tmp, GFP_KERNEL);
if (ioc == NULL) {
retval = -ENOMEM;
goto out;
}
if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
retval = -EFAULT;
goto out;
}
/*
* Read register
* tx_buf include register address will be read
*/
if (ioc->opcode == FP_REGISTER_READ) {
u8 *address = ioc->tx_buf;
u8 *result = ioc->rx_buf;
pr_debug("etspi FP_REGISTER_READ\n");
retval = etspi_io_read_register(etspi, address, result);
if (retval < 0) {
pr_err("%s FP_REGISTER_READ error retval = %d\n"
, __func__, retval);
goto out;
}
}
/*
* Write data to register
* tx_buf includes address and value will be wrote
*/
if (ioc->opcode == FP_REGISTER_WRITE) {
u8 *buf = ioc->tx_buf;
pr_debug("%s FP_REGISTER_WRITE\n", __func__);
retval = etspi_io_write_register(etspi, buf);
if (retval < 0) {
pr_err("%s FP_REGISTER_WRITE error retval = %d\n"
, __func__, retval);
goto out;
}
}
/* Get one frame data from sensor */
if (ioc->opcode == FP_GET_ONE_IMG) {
u8 *buf = ioc->tx_buf;
u8 *image_buf = ioc->rx_buf;
pr_debug("%s FP_GET_ONE_IMG\n", __func__);
retval = etspi_io_get_one_image(etspi, buf, image_buf);
if (retval < 0) {
pr_err("%s FP_GET_ONE_IMG error retval = %d\n"
, __func__, retval);
goto out;
}
}
if (ioc->opcode == FP_SENSOR_RESET) {
pr_info("%s FP_SENSOR_RESET\n", __func__);
etspi_reset(etspi);
}
if (ioc->opcode == FP_RESET_SET) {
pr_info("%s FP_SENSOR_RESET\n", __func__);
pr_info("%s status = %d\n", __func__, ioc->len);
etspi_reset_set(etspi, ioc->len);
}
if (ioc->opcode == FP_POWER_CONTROL) {
pr_info("%s FP_POWER_CONTROL\n", __func__);
pr_info("%s status = %d\n", __func__, ioc->len);
etspi_power_control(etspi, ioc->len);
}
if (ioc->opcode == FP_SET_SPI_CLOCK) {
pr_info("%s FP_SET_SPI_CLOCK\n", __func__);
pr_info("%s clock = %d\n", __func__, ioc->speed_hz);
spi->max_speed_hz = ioc->speed_hz;
#ifdef ENABLE_SENSORS_FPRINT_SECURE
if (!etspi->enabled_clk) {
spi_info = kmalloc(sizeof(struct sec_spi_info),
GFP_KERNEL);
if (spi_info != NULL) {
pr_info("%s ENABLE_SPI_CLOCK\n", __func__);
/* System LSI W/A Code */
exynos7_update_media_scenario(TYPE_SPDMA, 1, 0);
spi_info->speed = spi->max_speed_hz;
retval = sec_spi_prepare(spi_info, spi);
if (retval < 0)
pr_err("%s: Unable to enable spi clk\n",
__func__);
kfree(spi_info);
#ifdef FEATURE_SPI_WAKELOCK
wake_lock(&etspi->fp_spi_lock);
#endif
etspi->enabled_clk = true;
} else
retval = -ENOMEM;
}
#endif
}
if (ioc->opcode == FP_EEPROM_WREN) {
pr_info("%s FP_EEPROM_WREN\n", __func__);
etspi_reset_set(etspi, 0);
etspi_eeprom_wren(etspi);
etspi_reset_set(etspi, 1);
}
if (ioc->opcode == FP_EEPROM_WRDI) {
pr_info("%s FP_EEPROM_WRDI\n", __func__);
etspi_reset_set(etspi, 0);
etspi_eeprom_wrdi(etspi);
etspi_reset_set(etspi, 1);
}
if (ioc->opcode == FP_EEPROM_RDSR) {
u8 *result = ioc->rx_buf;
pr_info("%s FP_EEPROM_RDSR\n", __func__);
etspi_reset_set(etspi, 0);
etspi_eeprom_rdsr(etspi, result);
etspi_reset_set(etspi, 1);
}
if (ioc->opcode == FP_EEPROM_WRSR) {
u8 *buf = ioc->tx_buf;
pr_info("%s FP_EEPROM_WRSR\n", __func__);
etspi_reset_set(etspi, 0);
etspi_eeprom_wrsr(etspi, buf);
etspi_reset_set(etspi, 1);
}
if (ioc->opcode == FP_EEPROM_READ) {
u8 *buf = ioc->tx_buf;
u8 *result = ioc->rx_buf;
pr_info("%s FP_EEPROM_READ\n", __func__);
etspi_reset_set(etspi, 0);
etspi_eeprom_read(etspi, buf, result, ioc->len);
etspi_reset_set(etspi, 1);
}
if (ioc->opcode == FP_EEPROM_WRITE) {
u8 *buf = ioc->tx_buf;
pr_info("%s FP_EEPROM_WRITE\n", __func__);
etspi_reset_set(etspi, 0);
etspi_eeprom_write(etspi, buf, ioc->len);
etspi_reset_set(etspi, 1);
}
/*
* Trigger inital routine
*/
if (ioc->opcode == INT_TRIGGER_INIT) {
pr_info("%s Trigger function init\n", __func__);
retval = etspi_Interrupt_Init(
etspi,
(int)ioc->pad[0],
(int)ioc->pad[1],
(int)ioc->pad[2]);
pr_info("%s trigger init = %d\n", __func__, retval);
}
/* trigger */
if (ioc->opcode == INT_TRIGGER_CLOSE) {
pr_info("%s Trigger function close\n", __func__);
retval = etspi_Interrupt_Free(etspi);
pr_info("%s trigger close = %d\n", __func__, retval);
}
/* Poll Abort */
if (ioc->opcode == INT_TRIGGER_ABORT) {
pr_info("%s Trigger function close\n", __func__);
etspi_Interrupt_Abort(etspi);
}
#ifdef ENABLE_SENSORS_FPRINT_SECURE
if (ioc->opcode == FP_DIABLE_SPI_CLOCK) {
pr_info("%s FP_DISABLE_SPI_CLOCK\n", __func__);
if (etspi->enabled_clk) {
pr_info("%s DISABLE_SPI_CLOCK\n", __func__);
retval = sec_spi_unprepare(spi_info, spi);
if (retval < 0)
pr_err("%s: couldn't disable spi clks\n", __func__);
#ifdef FEATURE_SPI_WAKELOCK
wake_unlock(&etspi->fp_spi_lock);
#endif
etspi->enabled_clk = false;
/* System LSI W/A Code */
exynos7_update_media_scenario(TYPE_SPDMA, 0, 0);
}
}
if (ioc->opcode == FP_CPU_SPEEDUP) {
pr_info("%s FP_CPU_SPEEDUP\n", __func__);
if (ioc->len) {
u8 retry_cnt = 0;
pr_info("%s FP_CPU_SPEEDUP ON:%d, retry: %d\n",
__func__, ioc->len, retry_cnt);
#if defined(CONFIG_SECURE_OS_BOOSTER_API)
do {
retval = secos_booster_start(ioc->len - 1);
retry_cnt++;
if (retval) {
pr_err("%s: booster start failed. (%d) retry: %d\n"
, __func__, retval, retry_cnt);
if (retry_cnt < 7)
usleep_range(500, 510);
}
} while (retval && retry_cnt < 7);
#endif
} else {
pr_info("%s FP_CPU_SPEEDUP OFF\n", __func__);
#if defined(CONFIG_SECURE_OS_BOOSTER_API)
retval = secos_booster_stop();
if (retval)
pr_err("%s: booster stop failed. (%d)\n"
, __func__, retval);
#endif
}
}
#endif
out:
if (ioc != NULL)
kfree(ioc);
mutex_unlock(&etspi->buf_lock);
spi_dev_put(spi);
if (retval < 0)
pr_err("%s retval = %d\n", __func__, retval);
return retval;
}
#ifdef CONFIG_COMPAT
static long etspi_compat_ioctl(struct file *filp,
unsigned int cmd,
unsigned long arg)
{
return etspi_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
}
#else
#define etspi_compat_ioctl NULL
#endif
/* CONFIG_COMPAT */
static int etspi_open(struct inode *inode, struct file *filp)
{
struct etspi_data *etspi;
int status = -ENXIO;
pr_info("%s\n", __func__);
mutex_lock(&device_list_lock);
list_for_each_entry(etspi, &device_list, device_entry)
{
if (etspi->devt == inode->i_rdev) {
status = 0;
break;
}
}
if (status == 0) {
if (etspi->buffer == NULL) {
etspi->buffer = kmalloc(bufsiz, GFP_KERNEL);
if (etspi->buffer == NULL) {
dev_dbg(&etspi->spi->dev, "open/ENOMEM\n");
status = -ENOMEM;
}
}
if (status == 0) {
etspi->users++;
filp->private_data = etspi;
nonseekable_open(inode, filp);
}
} else
pr_debug("%s nothing for minor %d\n"
, __func__, iminor(inode));
mutex_unlock(&device_list_lock);
return status;
}
static int etspi_release(struct inode *inode, struct file *filp)
{
struct etspi_data *etspi;
pr_info("%s\n", __func__);
mutex_lock(&device_list_lock);
etspi = filp->private_data;
filp->private_data = NULL;
/* last close? */
etspi->users--;
if (etspi->users == 0) {
int dofree;
kfree(etspi->buffer);
etspi->buffer = NULL;
/* ... after we unbound from the underlying device? */
spin_lock_irq(&etspi->spi_lock);
dofree = (etspi->spi == NULL);
spin_unlock_irq(&etspi->spi_lock);
if (dofree)
kfree(etspi);
}
mutex_unlock(&device_list_lock);
return 0;
}
int etspi_platformInit(struct etspi_data *etspi)
{
int status = 0;
pr_info("%s\n", __func__);
setup_timer(&etspi->timer, etspi_interrupt_timer_routine,
(unsigned long)etspi);
/* gpio setting for ldo, ldo2, sleep, drdy pin */
if (etspi != NULL) {
if (etspi->ocp_en) {
status = gpio_request(etspi->ocp_en, "etspi_ocp_en");
if (status < 0) {
pr_err("%s gpio_request etspi_ocp_en failed\n",
__func__);
}
gpio_direction_output(etspi->ocp_en, 0);
}
if (etspi->ldo_pin) {
status = gpio_request(etspi->ldo_pin, "etspi_ldo_en");
if (status < 0) {
pr_err("%s gpio_request etspi_ldo_en failed\n",
__func__);
goto etspi_platformInit_ldo_failed;
}
gpio_direction_output(etspi->ldo_pin, 0);
}
if (etspi->ldo_pin2) {
status = gpio_request(etspi->ldo_pin2, "etspi_ldo_en2");
if (status < 0) {
pr_err("%s gpio_request etspi_ldo_en2 failed\n",
__func__);
goto etspi_platformInit_ldo2_failed;
}
gpio_direction_output(etspi->ldo_pin2, 0);
}
status = gpio_request(etspi->sleepPin, "etspi_sleep");
if (status < 0) {
pr_err("%s gpio_requset etspi_sleep failed\n",
__func__);
goto etspi_platformInit_sleep_failed;
}
gpio_direction_output(etspi->sleepPin, 1);
if (status < 0) {
pr_err("%s gpio_direction_output SLEEP failed\n", __func__);
status = -EBUSY;
goto etspi_platformInit_sleep_failed;
}
status = gpio_request(etspi->drdyPin, "etspi_drdy");
if (status < 0) {
pr_err("%s gpio_request etspi_drdy failed\n",
__func__);
goto etspi_platformInit_drdy_failed;
}
status = gpio_direction_input(etspi->drdyPin);
if (status < 0) {
pr_err("%s gpio_direction_input DRDY failed\n",
__func__);
goto etspi_platformInit_gpio_init_failed;
}
pr_info("%s sleep value =%d\n"
"%s ldo en value =%d\n",
__func__, gpio_get_value(etspi->sleepPin),
__func__, gpio_get_value(etspi->ldo_pin));
if (etspi->ldo_pin2) {
pr_info("%s ldo en2 value =%d\n",
__func__, gpio_get_value(etspi->ldo_pin2));
}
} else {
status = -EFAULT;
}
#ifdef ENABLE_SENSORS_FPRINT_SECURE
#ifdef FEATURE_SPI_WAKELOCK
wake_lock_init(&etspi->fp_spi_lock,
WAKE_LOCK_SUSPEND, "etspi_wake_lock");
#endif
#endif
pr_info("%s successful status=%d\n", __func__, status);
return status;
etspi_platformInit_gpio_init_failed:
gpio_free(etspi->drdyPin);
etspi_platformInit_drdy_failed:
gpio_free(etspi->sleepPin);
etspi_platformInit_sleep_failed:
if (etspi->ldo_pin2)
gpio_free(etspi->ldo_pin2);
etspi_platformInit_ldo2_failed:
gpio_free(etspi->ldo_pin);
etspi_platformInit_ldo_failed:
gpio_free(etspi->ocp_en);
pr_err("%s is failed\n", __func__);
return status;
}
void etspi_platformUninit(struct etspi_data *etspi)
{
pr_info("%s\n", __func__);
if (etspi != NULL) {
disable_irq(gpio_irq);
free_irq(gpio_irq, etspi);
etspi->drdy_irq_flag = DRDY_IRQ_DISABLE;
if (etspi->ldo_pin)
gpio_free(etspi->ldo_pin);
if (etspi->ldo_pin2)
gpio_free(etspi->ldo_pin2);
gpio_free(etspi->sleepPin);
gpio_free(etspi->drdyPin);
#ifdef ENABLE_SENSORS_FPRINT_SECURE
#ifdef FEATURE_SPI_WAKELOCK
wake_lock_destroy(&etspi->fp_spi_lock);
#endif
#endif
}
}
static int etspi_parse_dt(struct device *dev,
struct etspi_data *data)
{
struct device_node *np = dev->of_node;
enum of_gpio_flags flags;
int errorno = 0;
int gpio;
gpio = of_get_named_gpio_flags(np, "etspi-sleepPin",
0, &flags);
if (gpio < 0) {
errorno = gpio;
goto dt_exit;
} else {
data->sleepPin = gpio;
pr_info("%s: sleepPin=%d\n",
__func__, data->sleepPin);
}
gpio = of_get_named_gpio_flags(np, "etspi-drdyPin",
0, &flags);
if (gpio < 0) {
errorno = gpio;
goto dt_exit;
} else {
data->drdyPin = gpio;
pr_info("%s: drdyPin=%d\n",
__func__, data->drdyPin);
}
gpio = of_get_named_gpio_flags(np, "etspi-ocpen",
0, &flags);
if (gpio < 0) {
data->ocp_en = 0;
pr_err("%s: fail to get ocp_en\n", __func__);
} else {
data->ocp_en = gpio;
pr_info("%s: ocp_en=%d\n",
__func__, data->ocp_en);
}
gpio = of_get_named_gpio_flags(np, "etspi-ldoPin",
0, &flags);
if (gpio < 0) {
data->ldo_pin = 0;
pr_err("%s: fail to get ldo_pin\n", __func__);
} else {
data->ldo_pin = gpio;
pr_info("%s: ldo_pin=%d\n",
__func__, data->ldo_pin);
}
if (!of_find_property(np, "etspi-ldoPin2", NULL)) {
pr_err("%s: not set ldo2 in dts\n", __func__);
} else {
gpio = of_get_named_gpio(np, "etspi-ldoPin2", 0);
if (gpio < 0) {
data->ldo_pin2 = 0;
pr_err("%s: fail to get ldo_pin2 : (%d)\n",
__func__, gpio);
} else {
data->ldo_pin2 = gpio;
pr_info("%s: ldo_pin2=%d\n",
__func__, data->ldo_pin2);
}
}
pr_info("%s is successful\n", __func__);
return errorno;
dt_exit:
pr_err("%s is failed\n", __func__);
return errorno;
}
static const struct file_operations etspi_fops = {
.owner = THIS_MODULE,
.write = etspi_write,
.read = etspi_read,
.unlocked_ioctl = etspi_ioctl,
.compat_ioctl = etspi_compat_ioctl,
.open = etspi_open,
.release = etspi_release,
.llseek = no_llseek,
.poll = etspi_fps_interrupt_poll
};
static int etspi_type_check(struct etspi_data *etspi)
{
u8 buf1, buf2, buf3, buf4;
etspi_power_control(g_data, 1);
etspi_read_register(etspi, 0x10, &buf1);
etspi_read_register(etspi, 0x11, &buf2);
etspi_read_register(etspi, 0x12, &buf3);
etspi_read_register(etspi, 0x13, &buf4);
etspi_power_control(g_data, 0);
pr_info("%s buf1: %x, buf2: %x, buf3: %x, buf4: %x\n",
__func__, buf1, buf2, buf3, buf4);
if ((buf1 == 0x00) && (buf2 == 0x3F)
&& (buf3 == 0x00) && (buf4 == 0x8f)) {
etspi->sensortype = SENSOR_EGIS;
pr_info("%s sensor type is EGIS sensor\n", __func__);
return 0;
} else {
etspi->sensortype = SENSOR_FAILED;
pr_info("%s sensor type is FAILED\n", __func__);
return -1;
}
}
#ifdef CONFIG_SENSORS_FINGERPRINT_SYSFS
static ssize_t etspi_type_check_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct etspi_data *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%u\n", data->sensortype);
}
static ssize_t etspi_vendor_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR);
}
static ssize_t etspi_name_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_ID);
}
static DEVICE_ATTR(type_check, S_IRUGO,
etspi_type_check_show, NULL);
static DEVICE_ATTR(vendor, S_IRUGO,
etspi_vendor_show, NULL);
static DEVICE_ATTR(name, S_IRUGO,
etspi_name_show, NULL);
static struct device_attribute *fp_attrs[] = {
&dev_attr_type_check,
&dev_attr_vendor,
&dev_attr_name,
NULL,
};
#endif
static void etspi_work_func_debug(struct work_struct *work)
{
u8 ldo_value = 0;
if (g_data->ldo_pin) {
ldo_value = gpio_get_value(g_data->ldo_pin);
if (g_data->ldo_pin2) {
ldo_value = (gpio_get_value(g_data->ldo_pin2) << 1)
| gpio_get_value(g_data->ldo_pin);
}
}
pr_info("%s ocpen: %d, ldo: %d, sleep: %d, tz: %d, type: %s\n",
__func__, gpio_get_value(g_data->ocp_en),
ldo_value, gpio_get_value(g_data->sleepPin),
g_data->tz_mode,
sensor_status[g_data->sensortype]);
}
static void etspi_enable_debug_timer(void)
{
mod_timer(&g_data->dbg_timer,
round_jiffies_up(jiffies + FPSENSOR_DEBUG_TIMER_SEC));
}
static void etspi_disable_debug_timer(void)
{
del_timer_sync(&g_data->dbg_timer);
cancel_work_sync(&g_data->work_debug);
}
static void etspi_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 etspi_set_timer(struct etspi_data *etspi)
{
int status = 0;
setup_timer(&etspi->dbg_timer,
etspi_timer_func, (unsigned long)etspi);
etspi->wq_dbg =
create_singlethread_workqueue("etspi_debug_wq");
if (!etspi->wq_dbg) {
status = -ENOMEM;
pr_err("%s could not create workqueue\n", __func__);
return status;
}
INIT_WORK(&etspi->work_debug, etspi_work_func_debug);
return status;
}
/*-------------------------------------------------------------------------*/
static struct class *etspi_class;
/*-------------------------------------------------------------------------*/
static int etspi_probe(struct spi_device *spi)
{
struct etspi_data *etspi;
int status;
unsigned long minor;
int retry = 0;
pr_info("%s\n", __func__);
/* Allocate driver data */
etspi = kzalloc(sizeof(*etspi), GFP_KERNEL);
if (etspi == NULL) {
pr_err("%s - Failed to kzalloc\n", __func__);
return -ENOMEM;
}
/* device tree call */
if (spi->dev.of_node) {
status = etspi_parse_dt(&spi->dev, etspi);
if (status) {
pr_err("%s - Failed to parse DT\n", __func__);
goto etspi_probe_parse_dt_failed;
}
}
/* Initialize the driver data */
etspi->spi = spi;
g_data = etspi;
spin_lock_init(&etspi->spi_lock);
mutex_init(&etspi->buf_lock);
mutex_init(&device_list_lock);
INIT_LIST_HEAD(&etspi->device_entry);
/* platform init */
status = etspi_platformInit(etspi);
if (status != 0) {
pr_err("%s platforminit failed\n", __func__);
goto etspi_probe_platformInit_failed;
}
spi->bits_per_word = 8;
spi->max_speed_hz = SLOW_BAUD_RATE;
spi->mode = SPI_MODE_3;
spi->chip_select = 0;
status = spi_setup(spi);
if (status != 0) {
pr_err("%s spi_setup() is failed. status : %d\n",
__func__, status);
return status;
}
/* sensor hw type check */
do {
etspi_type_check(etspi);
pr_info("%s type (%u), retry (%d)\n"
, __func__, etspi->sensortype, retry);
} while (!etspi->sensortype && ++retry < 3);
#ifdef ENABLE_SENSORS_FPRINT_SECURE
etspi->tz_mode = true;
#endif
/* If we can allocate a minor number, hook up this device.
* Reusing minors is fine so long as udev or mdev is working.
*/
mutex_lock(&device_list_lock);
minor = find_first_zero_bit(minors, N_SPI_MINORS);
if (minor < N_SPI_MINORS) {
struct device *dev;
etspi->devt = MKDEV(ET310_MAJOR, minor);
dev = device_create(etspi_class, &spi->dev, etspi->devt,
etspi, "esfp0");
status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
} else{
dev_dbg(&spi->dev, "no minor number available!\n");
status = -ENODEV;
}
if (status == 0) {
set_bit(minor, minors);
list_add(&etspi->device_entry, &device_list);
}
mutex_unlock(&device_list_lock);
if (status == 0)
spi_set_drvdata(spi, etspi);
else
goto etspi_probe_failed;
#ifdef CONFIG_SENSORS_FINGERPRINT_SYSFS
status = fingerprint_register(etspi->fp_device,
etspi, fp_attrs, "fingerprint");
if (status) {
pr_err("%s sysfs register failed\n", __func__);
goto etspi_probe_failed;
}
#endif
status = etspi_set_timer(etspi);
if (status)
goto etspi_sysfs_failed;
etspi_enable_debug_timer();
pr_info("%s is successful\n", __func__);
return status;
etspi_sysfs_failed:
#ifdef CONFIG_SENSORS_FINGERPRINT_SYSFS
fingerprint_unregister(etspi->fp_device, fp_attrs);
#endif
etspi_probe_failed:
device_destroy(etspi_class, etspi->devt);
class_destroy(etspi_class);
etspi_platformUninit(etspi);
etspi_probe_platformInit_failed:
etspi_probe_parse_dt_failed:
kfree(etspi);
pr_err("%s is failed\n", __func__);
return status;
}
static int etspi_remove(struct spi_device *spi)
{
struct etspi_data *etspi = spi_get_drvdata(spi);
pr_info("%s\n", __func__);
if (etspi != NULL) {
etspi_disable_debug_timer();
etspi_platformUninit(etspi);
/* make sure ops on existing fds can abort cleanly */
spin_lock_irq(&etspi->spi_lock);
etspi->spi = NULL;
spi_set_drvdata(spi, NULL);
spin_unlock_irq(&etspi->spi_lock);
/* prevent new opens */
mutex_lock(&device_list_lock);
#ifdef CONFIG_SENSORS_FINGERPRINT_SYSFS
fingerprint_unregister(etspi->fp_device, fp_attrs);
#endif
list_del(&etspi->device_entry);
device_destroy(etspi_class, etspi->devt);
clear_bit(MINOR(etspi->devt), minors);
if (etspi->users == 0)
kfree(etspi);
mutex_unlock(&device_list_lock);
}
return 0;
}
#undef ETSPI_PM_SUSPEND
#ifdef ETSPI_PM_SUSPEND
static int etspi_pm_suspend(struct device *dev)
{
if (g_data != NULL) {
etspi_disable_debug_timer();
etspi_power_control(g_data, 0);
}
pr_info("%s\n", __func__);
return 0;
}
static int etspi_pm_resume(struct device *dev)
{
if (g_data != NULL) {
etspi_power_control(g_data, 1);
etspi_enable_debug_timer();
}
pr_info("%s\n", __func__);
return 0;
}
static const struct dev_pm_ops etspi_pm_ops = {
.suspend = etspi_pm_suspend,
.resume = etspi_pm_resume
};
#endif
static struct of_device_id etspi_match_table[] = {
{ .compatible = "etspi,et310",},
{},
};
static struct spi_driver etspi_spi_driver = {
.driver = {
.name = "egis_fingerprint",
.owner = THIS_MODULE,
#ifdef ETSPI_PM_SUSPEND
.pm = &etspi_pm_ops,
#endif
.of_match_table = etspi_match_table
},
.probe = etspi_probe,
.remove = etspi_remove,
};
/*-------------------------------------------------------------------------*/
static int __init etspi_init(void)
{
int status;
pr_info("%s\n", __func__);
#ifdef ENABLE_VENDOR_CHECK
/* vendor check */
pr_info("%s FP_CHECK value (%d)\n", __func__, FP_CHECK);
if (FP_CHECK) {
pr_err("%s It is not egis sensor\n", __func__);
return -ENODEV;
}
#endif
/* Claim our 256 reserved device numbers. Then register a class
* that will key udev/mdev to add/remove /dev nodes. Last, register
* the driver which manages those device numbers.
*/
BUILD_BUG_ON(N_SPI_MINORS > 256);
status = register_chrdev(ET310_MAJOR, "egis_fingerprint", &etspi_fops);
if (status < 0) {
pr_err("%s register_chrdev error.\n", __func__);
return status;
}
etspi_class = class_create(THIS_MODULE, "egis_fingerprint");
if (IS_ERR(etspi_class)) {
pr_err("%s class_create error.\n", __func__);
unregister_chrdev(ET310_MAJOR, etspi_spi_driver.driver.name);
return PTR_ERR(etspi_class);
}
status = spi_register_driver(&etspi_spi_driver);
if (status < 0) {
pr_err("%s spi_register_driver error.\n", __func__);
class_destroy(etspi_class);
unregister_chrdev(ET310_MAJOR, etspi_spi_driver.driver.name);
}
pr_info("%s is successful\n", __func__);
return status;
}
static void __exit etspi_exit(void)
{
pr_info("%s\n", __func__);
spi_unregister_driver(&etspi_spi_driver);
class_destroy(etspi_class);
unregister_chrdev(ET310_MAJOR, etspi_spi_driver.driver.name);
}
module_init(etspi_init);
module_exit(etspi_exit);
MODULE_AUTHOR("Wang YuWei, <robert.wang@egistec.com>");
MODULE_DESCRIPTION("SPI Interface for ET310");
MODULE_LICENSE("GPL");
|