summaryrefstree
path: root/kernel/drivers/nfc/p61.c
blob: 8c5d328c3126b0163bc59e0b1846b517a1146a62 (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
/*
 * Copyright (C) 2012-2020 NXP Semiconductors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/list.h>
#include <linux/irq.h>
#include <linux/jiffies.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/miscdevice.h>
#include <linux/spinlock.h>
#include <linux/spi/spi.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/regulator/consumer.h>
#include <linux/ioctl.h>
#include <linux/clk.h>
#include <linux/pm_runtime.h>
#include <linux/spi/spidev.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>

#include "nfc_wakelock.h"
#include "p61.h"
#include "pn547.h"
#include "cold_reset.h"
#include "./nfc_logger/nfc_logger.h"
#if defined(CONFIG_ESE_SECURE) && defined(CONFIG_ESE_USE_TZ_API)
#include "../misc/tzdev/include/tzdev/tee_client_api.h"
#endif

extern long  pn547_dev_ioctl(struct file *filp, unsigned int cmd,
	unsigned long arg);

/* size of maximum read/write buffer supported by driver */
#ifdef CONFIG_NFC_FEATURE_SN100U
#define MAX_BUFFER_SIZE   780U
#else
#define MAX_BUFFER_SIZE   258U
#endif

/* Different driver debug lever */
enum P61_DEBUG_LEVEL {
	P61_DEBUG_OFF,
	P61_FULL_DEBUG
};

/* Variable to store current debug level request by ioctl */
static unsigned char debug_level = P61_FULL_DEBUG;
static unsigned char pwr_req_on;
#define P61_DBG_MSG(msg...) {\
	switch (debug_level) {\
	case P61_DEBUG_OFF:\
		break;\
	case P61_FULL_DEBUG:\
		NFC_LOG_INFO("[NXP-P61] " msg);\
		break;\
		/*fallthrough*/\
	default:\
		NFC_LOG_ERR("[NXP-P61] Wrong debug level(%d)\n", debug_level);\
		break;\
	} \
}

#define P61_ERR_MSG(msg...) NFC_LOG_ERR("[NXP-P61] " msg)
#define P61_INFO_MSG(msg...) NFC_LOG_INFO("[NXP-P61] " msg)

#if defined(CONFIG_ESE_SECURE) && defined(CONFIG_ESE_USE_TZ_API)
static TEEC_UUID ese_drv_uuid = {
	0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x65, 0x73, 0x65, 0x44, 0x72, 0x76}
};

enum pm_mode {
	PM_SUSPEND,
	PM_RESUME,
	SECURE_CHECK,
};
#endif

char *g_pin_status[] = {"sleep", "default", "lpm", "ese_on", "ese_off"};
#define ESE_PIN_STATUS_CNT 5

/* Device specific macro and structure */
struct p61_device {
	wait_queue_head_t read_wq; /* wait queue for read interrupt */
	struct mutex read_mutex; /* read mutex */
	struct mutex write_mutex; /* write mutex */
	struct spi_device *spi;  /* spi device structure */
	struct miscdevice miscdev; /* char device as misc driver */
	unsigned int rst_gpio; /* SW Reset gpio */
	unsigned int irq_gpio; /* P61 will interrupt DH for any ntf */
	bool irq_enabled; /* flag to indicate irq is used */
	unsigned char enable_poll_mode; /* enable the poll mode */
	spinlock_t irq_enabled_lock; /*spin lock for read irq */

	bool tz_mode;
	spinlock_t ese_spi_lock;
	bool isGpio_cfgDone;
	struct nfc_wake_lock ese_lock;
	bool device_opened;

	struct pinctrl *pinctrl;
	struct clk *ese_spi_clk[MAX_SPI_CLK_CNT];
	struct pinctrl_state *pinctrl_state[ESE_PIN_STATUS_CNT];

#ifdef CONFIG_NFC_FEATURE_SN100U
	int spi_cs_gpio;
	int pid;
	bool pid_diff;
#endif
#if defined(CONFIG_ESE_SECURE) && defined(CONFIG_ESE_USE_TZ_API)
	int ese_secure_check;
#endif
	enum ap_vendors ap_vendor;
	unsigned char *buf;
	struct device_node *spi_device_node;
};
static struct p61_device *p61_dev;

/* T==1 protocol specific global data */
const unsigned char SOF = 0xA5u;

#if defined(CONFIG_ESE_SECURE)
static int p61_clk_control(struct p61_device *p61_dev, bool onoff);
#if defined(CONFIG_ESE_USE_TZ_API)
static uint32_t tz_tee_ese_drv(enum pm_mode mode);
#endif
#endif

static int ese_get_pin_ctrl_count(void)
{
	/* return ARRAY_SIZE(g_pin_status); */
	return ESE_PIN_STATUS_CNT;
}

#if defined(CONFIG_NFC_FEATURE_SN100U) || !defined(CONFIG_ESE_SECURE)
static int ese_get_pin_ctrl_number(char *str)
{
	int i;
	int cnt = ese_get_pin_ctrl_count();
	int ret = -1;

	for (i = 0; i < cnt; i++) {
		if (!strncmp(g_pin_status[i], str, strlen(str))) {
			ret = i;
			break;
		}
	}

	return ret;
}

static int ese_set_spi_configuration(char *name)
{
	int ret = 0;
	int pin_num = ese_get_pin_ctrl_number(name);

	NFC_LOG_INFO("%s [%s:%d]\n", __func__, name, pin_num);

	if (pin_num < 0 || !p61_dev->pinctrl_state[pin_num]) {
		NFC_LOG_INFO("%s pinctrl skip!(%d)\n", __func__, pin_num);
		return -EINVAL;
	}

	ret = pinctrl_select_state(p61_dev->pinctrl, p61_dev->pinctrl_state[pin_num]);
	if (ret < 0)
		NFC_LOG_INFO("%s pinctrl_select_state[%s] failed\n", __func__, name);

	return ret;
}
#endif

int ese_spi_pinctrl(int enable)
{
	int ret = 0;

	NFC_LOG_INFO("[p61] %s (%d)\n", __func__, enable);
	if (p61_dev->ap_vendor == AP_VENDOR_SLSI) {
		switch (enable) {
		case 0:
#ifdef CONFIG_ESE_SECURE
			p61_clk_control(p61_dev, false);
#ifdef CONFIG_ESE_USE_TZ_API
			tz_tee_ese_drv(PM_SUSPEND);
#endif
#else
			ese_set_spi_configuration("ese_off");
#endif
			break;
		case 1:
#ifdef CONFIG_ESE_SECURE
			p61_clk_control(p61_dev, true);
#ifdef CONFIG_ESE_USE_TZ_API
			tz_tee_ese_drv(PM_RESUME);
#endif
#else
			ese_set_spi_configuration("ese_on");
#endif

			break;
		default:
			NFC_LOG_ERR("%s no matching!\n", __func__);
			ret = -EINVAL;
		}
	} else if (p61_dev->ap_vendor == AP_VENDOR_MTK) {
		switch (enable) {
		case 0:
			ese_set_spi_configuration("ese_off");
			break;
		case 1:
			ese_set_spi_configuration("ese_on");
			break;
		default:
			NFC_LOG_ERR("%s no matching!\n", __func__);
			ret = -EINVAL;
		}
	}

	return ret;
}
EXPORT_SYMBOL_GPL(ese_spi_pinctrl);

#ifdef CONFIG_ESE_SECURE
static int p61_clk_control(struct p61_device *p61_dev, bool onoff)
{
	static bool old_value;
	int i;

	if (p61_dev->ap_vendor != AP_VENDOR_SLSI)
		return 0;

	if (old_value == onoff) {
		NFC_LOG_INFO("%s: ALREADY %s\n", __func__,
			onoff ? "enabled" : "disabled");
		return 0;
	}

	if (onoff == true) {
		for (i = 0; i < MAX_SPI_CLK_CNT; i++) {
			if (p61_dev->ese_spi_clk[i])
				clk_prepare_enable(p61_dev->ese_spi_clk[i]);
		}

		/* There is a quarter-multiplier before the USI_v2 SPI */
		clk_set_rate(p61_dev->ese_spi_clk[SPI_SRC_CLK], p61_dev->spi->max_speed_hz * 4);
		usleep_range(5000, 5100);
		NFC_LOG_INFO("%s clock:%lu\n", __func__, clk_get_rate(p61_dev->ese_spi_clk[SPI_SRC_CLK]));
	} else {
		for (i = 0; i < MAX_SPI_CLK_CNT; i++) {
			if (p61_dev->ese_spi_clk[i])
				clk_disable_unprepare(p61_dev->ese_spi_clk[i]);
		}
	}
	old_value = onoff;

	NFC_LOG_INFO("%s: clock %s\n", __func__, onoff ? "enabled" : "disabled");
	return 0;
}

static int p61_clk_setup(struct device *dev, struct p61_device *p61_dev)
{
	struct platform_device *spi_pdev;
	int clk_cnt;
	int i;
	int ret;

	if (p61_dev->ap_vendor == AP_VENDOR_SLSI) {
		spi_pdev = of_find_device_by_node(p61_dev->spi_device_node);
		if (IS_ERR_OR_NULL(spi_pdev))
			return -EPERM;

		clk_cnt = of_property_count_strings(p61_dev->spi_device_node, "clock-names");
		if (clk_cnt != MAX_SPI_CLK_CNT)
			NFC_LOG_ERR("ese spi clk cnt : %d, need to be checked!\n", clk_cnt);

		for (i = 0; i < MAX_SPI_CLK_CNT; i++) {
			const char *clock_name;

			ret = of_property_read_string_index(p61_dev->spi_device_node, "clock-names", i,
					&(clock_name));
			if (ret < 0) {
				NFC_LOG_ERR("Can't get clock-names[%d]\n", i);
				goto err_clk_get;
			}

			p61_dev->ese_spi_clk[i] = clk_get(&spi_pdev->dev, clock_name);
			if (IS_ERR(p61_dev->ese_spi_clk[i])) {
				NFC_LOG_ERR("Can't get %s\n", clock_name);
				p61_dev->ese_spi_clk[i] = NULL;
				goto err_clk_get;
			}
			NFC_LOG_INFO("get clock(%s)\n", clock_name);
		}
	}

	return 0;
err_clk_get:
	for (i--; i >= 0; i--)
		clk_put(p61_dev->ese_spi_clk[i]);
	return -EPERM;
}
#endif

#if defined(CONFIG_ESE_SECURE) && defined(CONFIG_ESE_USE_TZ_API)
static uint32_t tz_tee_ese_drv(enum pm_mode mode)
{
	TEEC_Context context;
	TEEC_Session session;
	TEEC_Result result;
	uint32_t returnOrigin = TEEC_NONE;

	result = TEEC_InitializeContext(NULL, &context);
	if (result != TEEC_SUCCESS)
		goto out;

	result = TEEC_OpenSession(&context, &session, &ese_drv_uuid, TEEC_LOGIN_PUBLIC,
			NULL, NULL, &returnOrigin);
	if (result != TEEC_SUCCESS)
		goto finalize_context;

	/* test with valid cmd id, expected result : TEEC_SUCCESS */
	result = TEEC_InvokeCommand(&session, mode, NULL, &returnOrigin);
	if (result != TEEC_SUCCESS) {
		P61_ERR_MSG("%s with cmd %d : FAIL\n", __func__, mode);
		goto close_session;
	}

	P61_ERR_MSG("%s: return origin %d\n", __func__, returnOrigin);

close_session:
	TEEC_CloseSession(&session);
finalize_context:
	TEEC_FinalizeContext(&context);
out:
	P61_INFO_MSG("%s: cmd %d result=%#x origin=%#x\n", __func__, mode, result, returnOrigin);

	return result;
}

int tz_tee_ese_secure_check(void)
{
	return	tz_tee_ese_drv(SECURE_CHECK);
}
#endif

#if !defined(CONFIG_ESE_SECURE)
static int p61_xfer(struct p61_device *p61_dev,
			struct p61_ioctl_transfer *tr)
{
	int status = 0;
	struct spi_message m;
	struct spi_transfer t;
	int read_write = 0; /* 0: write, 1: read */
	/*For SDM845 & linux4.9: need to change spi buffer
	 * from stack to dynamic memory
	 */

	if (p61_dev == NULL || tr == NULL)
		return -EFAULT;

	if (tr->len > DEFAULT_BUFFER_SIZE || !tr->len)
		return -EMSGSIZE;

	spi_message_init(&m);
	memset(&t, 0, sizeof(t));

	memset(p61_dev->buf, 0, tr->len); /*memset 0 for read */
	if (tr->tx_buffer != NULL) { /*write */
		read_write = 0;
		if (copy_from_user(p61_dev->buf, tr->tx_buffer, tr->len) != 0)
			return -EFAULT;
	}

	t.rx_buf = p61_dev->buf;
	t.tx_buf = p61_dev->buf;
	t.len = tr->len;

	spi_message_add_tail(&t, &m);

	status = spi_sync(p61_dev->spi, &m);
	if (status == 0) {
		if (tr->rx_buffer != NULL) { /*read */
			unsigned long missing = 0;

			read_write = 1;
			missing = copy_to_user(tr->rx_buffer, p61_dev->buf, tr->len);
			if (missing != 0)
				tr->len = tr->len - (unsigned int)missing;
		}
	}
	NFC_LOG_REC("%s: %s(%d)\n", __func__, read_write ? "read":"write", tr->len);

	return status;
} /* vfsspi_xfer */

static int p61_rw_spi_message(struct p61_device *p61_dev,
				 unsigned long arg)
{
	struct p61_ioctl_transfer   *dup = NULL;
	int err = 0;

	dup = kmalloc(sizeof(struct p61_ioctl_transfer), GFP_KERNEL);
	if (dup == NULL)
		return -ENOMEM;

	if (copy_from_user(dup, (void *)arg,
			   sizeof(struct p61_ioctl_transfer)) != 0) {
		kfree(dup);
		return -EFAULT;
	}

	err = p61_xfer(p61_dev, dup);
	if (err != 0) {
		kfree(dup);
		NFC_LOG_ERR("%s: p61_xfer failed!\n", __func__);
		return err;
	}

	if (copy_to_user((void *)arg, dup,
			 sizeof(struct p61_ioctl_transfer)) != 0) {
		kfree(dup);
		return -EFAULT;
	}
	kfree(dup);
	return 0;
}
#endif

/**
 * \ingroup spi_driver
 * \brief Called from SPI LibEse to initilaize the P61 device
 *
 * \param[in]       struct inode *
 * \param[in]       struct file *
 *
 * \retval 0 if ok.
 */
static int p61_dev_open(struct inode *inode, struct file *filp)
{
	struct p61_device *p61_dev = container_of(filp->private_data,
				struct p61_device, miscdev);
	struct spi_device *spidev = NULL;

	spidev = spi_dev_get(p61_dev->spi);

	filp->private_data = p61_dev;
	if (p61_dev->device_opened) {
		NFC_LOG_ERR("%s: already opened!\n", __func__);
		return -EBUSY;
	}
#ifdef CONFIG_NFC_FEATURE_SN100U
	ese_spi_pinctrl(1);
	msleep(60);
#endif
#if defined(CONFIG_ESE_SECURE) && defined(CONFIG_ESE_USE_TZ_API)
	if (p61_dev->ese_secure_check == NOT_CHECKED) {
		int ret = 0;

		ret = tz_tee_ese_secure_check();
		if (ret) {
			p61_dev->ese_secure_check = ESE_NOT_SECURED;
			P61_ERR_MSG("eSE spi is not Secured\n");
			return -EBUSY;
		}
		p61_dev->ese_secure_check = ESE_SECURED;
	} else if (p61_dev->ese_secure_check == ESE_NOT_SECURED) {
		P61_ERR_MSG("eSE spi is not Secured\n");
		return -EBUSY;
	}
#endif

	NFC_LOG_INFO("%s: Major No: %d, Minor No: %d\n", __func__,
			imajor(inode), iminor(inode));

	if (!wake_lock_active(&p61_dev->ese_lock)) {
		NFC_LOG_INFO("%s: [NFC-ESE] wake lock.\n", __func__);
		wake_lock(&p61_dev->ese_lock);
	}

	p61_dev->device_opened = true;
#ifdef CONFIG_NFC_FEATURE_SN100U
	p61_dev->pid = task_pid_nr(current);
	NFC_LOG_INFO("%s: pid:%d\n", __func__, p61_dev->pid);
#endif

	return 0;
}

/**
 * \ingroup spi_driver
 * \brief To configure the P61_SET_PWR/P61_SET_DBG/P61_SET_POLL
 * \n	P61_SET_PWR - hard reset (arg=2), soft reset (arg=1)
 * \n	P61_SET_DBG - Enable/Disable (based on arg value) the driver logs
 * \n	P61_SET_POLL - Configure the driver in poll (arg = 1),
 *							interrupt (arg = 0) based read operation
 * \param[in]       struct file *
 * \param[in]       unsigned int
 * \param[in]       unsigned long
 *
 * \retval 0 if ok.
 *
 */
static long p61_dev_ioctl(struct file *filp, unsigned int cmd,
	unsigned long arg)
{
	int ret = 0;
	struct p61_device *p61_dev = NULL;

	if (_IOC_TYPE(cmd) != P61_MAGIC) {
		NFC_LOG_ERR("%s: invalid magic. cmd=0x%X Received=0x%X Expected=0x%X\n",
			__func__, cmd, _IOC_TYPE(cmd), P61_MAGIC);
		return -ENOTTY;
	}

	p61_dev = filp->private_data;

	switch (cmd) {
	case P61_SET_PWR:
		if (arg == 2)
			NFC_LOG_INFO("%s: P61_SET_PWR. No Action.\n", __func__);
		break;

	case P61_SET_DBG:
		debug_level = (unsigned char)arg;
		P61_DBG_MSG(KERN_INFO"[NXP-P61] -  Debug level %d",
			debug_level);
		break;
	case P61_SET_POLL:
		p61_dev->enable_poll_mode = (unsigned char)arg;
		if (p61_dev->enable_poll_mode == 0) {
			P61_DBG_MSG(KERN_INFO"[NXP-P61] - IRQ Mode is set\n");
		} else {
			P61_DBG_MSG(KERN_INFO"[NXP-P61] - Poll Mode is set\n");
			p61_dev->enable_poll_mode = 1;
		}
		break;

#if !defined(CONFIG_NFC_FEATURE_SN100U)
	case P61_SET_SPI_CONFIG:
		NFC_LOG_INFO("%s P61_SET_SPI_CONFIG. No Action.\n", __func__);
		break;
	case P61_ENABLE_SPI_CLK:
		NFC_LOG_INFO("%s P61_ENABLE_SPI_CLK. No Action.\n", __func__);
		break;
	case P61_DISABLE_SPI_CLK:
		NFC_LOG_INFO("%s P61_DISABLE_SPI_CLK. No Action.\n", __func__);
		break;
#endif

	case P61_RW_SPI_DATA:
#if !defined(CONFIG_ESE_SECURE)
		ret = p61_rw_spi_message(p61_dev, arg);
#endif
		break;

	case P61_SET_SPM_PWR:
		NFC_LOG_INFO("%s: P61_SET_SPM_PWR: enter\n", __func__);
		ret = pn547_dev_ioctl(filp, P61_SET_SPI_PWR, arg);
		if (arg == 0 || arg == 1 || arg == 3)
			pwr_req_on = arg;
		NFC_LOG_INFO("%s: P61_SET_SPM_PWR: exit\n", __func__);
		break;

	case P61_GET_SPM_STATUS:
		NFC_LOG_INFO("%s: P61_GET_SPM_STATUS: enter\n", __func__);
		ret = pn547_dev_ioctl(filp, P61_GET_PWR_STATUS, arg);
		NFC_LOG_INFO("%s: P61_GET_SPM_STATUS: exit\n", __func__);
		break;

	case P61_GET_ESE_ACCESS:
		/*P61_DBG_MSG(KERN_ALERT " P61_GET_ESE_ACCESS: enter");*/
		ret = pn547_dev_ioctl(filp, P547_GET_ESE_ACCESS, arg);
		NFC_LOG_INFO("%s: P61_GET_ESE_ACCESS ret: %d exit\n", __func__, ret);
		break;

	case P61_SET_DWNLD_STATUS:
		P61_DBG_MSG(KERN_ALERT "P61_SET_DWNLD_STATUS: enter\n");
		ret = pn547_dev_ioctl(filp, PN547_SET_DWNLD_STATUS, arg);
		NFC_LOG_INFO("%s: P61_SET_DWNLD_STATUS: =%lu exit\n", __func__, arg);
		break;

#ifdef CONFIG_NFC_FEATURE_SN100U
	case ESE_PERFORM_COLD_RESET:
		P61_DBG_MSG(KERN_ALERT " ESE_PERFORM_COLD_RESET: enter");
		ret = ese_cold_reset(ESE_COLD_RESET_SOURCE_SPI);
		P61_DBG_MSG(KERN_ALERT " P61_INHIBIT_PWR_CNTRL ret: %d exit", ret);
		break;

	case PERFORM_RESET_PROTECTION:
		P61_DBG_MSG(KERN_ALERT " PERFORM_RESET_PROTECTION: enter");
		ret = do_reset_protection((arg == 1 ? true : false));
		P61_DBG_MSG(KERN_ALERT " PERFORM_RESET_PROTECTION ret: %d exit", ret);
	break;
#endif

	default:
		NFC_LOG_INFO("%s: no matching ioctl!\n", __func__);
		ret = -EINVAL;
	}

	return ret;
}

#ifdef CONFIG_NFC_FEATURE_SN100U
/* this function is defined temporarily to fix build error. this function is used by uwb */
long p61_cold_reset(void)
{
	int ret;

	NFC_LOG_INFO("UWB ESE_COLD_RESET: enter");
	ret = ese_cold_reset(ESE_COLD_RESET_SOURCE_UWB);
	NFC_LOG_INFO("ret: %d exit", ret);

	return ret;
}
EXPORT_SYMBOL(p61_cold_reset);
#endif

/*
 * Called when a process closes the device file.
 */
static int p61_dev_release(struct inode *inode, struct file *file)
{
	struct p61_device *p61_dev = file->private_data;
#ifdef CONFIG_NFC_FEATURE_SN100U
	int pid;
#endif

	NFC_LOG_INFO("%s: ++\n", __func__);

#ifdef CONFIG_NFC_FEATURE_SN100U
	do_reset_protection(false);
	ese_spi_pinctrl(0);
	msleep(60);
#endif

#ifdef CONFIG_NFC_FEATURE_SN100U
	pid = task_pid_nr(current);
	NFC_LOG_INFO("%s: open pid :%d, close pid :%d\n", __func__, p61_dev->pid, pid);
	if (pid != p61_dev->pid)
		p61_dev->pid_diff = true;
	else
		p61_dev->pid_diff = false;
#endif

	if (wake_lock_active(&p61_dev->ese_lock)) {
		NFC_LOG_INFO("%s: [NFC-ESE] wake unlock.\n", __func__);
		wake_unlock(&p61_dev->ese_lock);
	}

	if (pwr_req_on && (pwr_req_on != 5)) {
		NFC_LOG_INFO("%s: release spi session.\n", __func__);
		pwr_req_on = 0;
		pn547_dev_ioctl(file, P61_SET_SPI_PWR, 0);
		pn547_dev_ioctl(file, P61_SET_SPI_PWR, 5);
	}
	p61_dev->device_opened = false;

	return 0;
}

/**
 * \ingroup spi_driver
 * \brief Write data to P61 on SPI
 *
 * \param[in]       struct file *
 * \param[in]       const char *
 * \param[in]       size_t
 * \param[in]       loff_t *
 *
 * \retval data size
 *
 */
static ssize_t p61_dev_write(struct file *filp, const char *buf, size_t count,
	loff_t *offset)
{
	int ret = -1;
	struct p61_device *p61_dev;

	P61_DBG_MSG("%s: Enter count %zu\n", __func__, count);

#ifdef CONFIG_ESE_SECURE
	return 0;
#endif
	p61_dev = filp->private_data;

	mutex_lock(&p61_dev->write_mutex);
	if (count > MAX_BUFFER_SIZE)
		count = MAX_BUFFER_SIZE;

	memset(p61_dev->buf, 0, count);
	if (copy_from_user(p61_dev->buf, &buf[0], count)) {
		P61_ERR_MSG("%s: failed to copy from user space\n", __func__);
		mutex_unlock(&p61_dev->write_mutex);
		return -EFAULT;
	}
	/* Write data */
	ret = spi_write(p61_dev->spi, p61_dev->buf, count);
	if (ret < 0)
		ret = -EIO;
	else
		ret = count;

	mutex_unlock(&p61_dev->write_mutex);
	NFC_LOG_INFO("%s: -count %zu  %d- Exit\n", __func__, count, ret);

	return ret;
}

/**
 * \ingroup spi_driver
 * \brief Used to read data from P61 in Poll/interrupt mode configured using
 *  ioctl call
 *
 * \param[in]       struct file *
 * \param[in]       char *
 * \param[in]       size_t
 * \param[in]       loff_t *
 *
 * \retval read size
 *
 */
/* for p61 only */
static ssize_t p61_dev_read(struct file *filp, char *buf, size_t count,
	loff_t *offset)
{
	int ret = -EIO;
	struct p61_device *p61_dev = filp->private_data;
	unsigned char sof = 0x00;
	int total_count = 0;
	//unsigned char rx_buffer[MAX_BUFFER_SIZE];

	P61_DBG_MSG("%s: count %zu - Enter\n", __func__, count);

#ifdef CONFIG_ESE_SECURE
	return 0;
#endif
	if (count < 1) {
		P61_ERR_MSG("Invalid length (min : 258) [%zu]\n", count);
		return -EINVAL;
	}
	mutex_lock(&p61_dev->read_mutex);
	if (count > MAX_BUFFER_SIZE)
		count = MAX_BUFFER_SIZE;

	//memset(&rx_buffer[0], 0x00, sizeof(rx_buffer));
	memset(p61_dev->buf, 0x00, MAX_BUFFER_SIZE);

	P61_DBG_MSG(" %s Poll Mode Enabled\n", __func__);
	do {
		sof = 0x00;
		ret = spi_read(p61_dev->spi, (void *)&sof, 1);
		if (ret < 0) {
			P61_ERR_MSG("spi_read failed [SOF]\n");
			goto fail;
		}
		//P61_DBG_MSG(KERN_INFO"SPI_READ returned 0x%x\n", sof);
		/* if SOF not received, give some time to P61 */
		/* RC put the conditional delay only if SOF not received */
		if (sof != SOF)
			usleep_range(5000, 5100);
	} while (sof != SOF);
	P61_DBG_MSG("SPI_READ returned 0x%x...\n", sof);

	total_count = 1;
	//rx_buffer[0] = sof;
	*p61_dev->buf = sof;
	/* Read the HEADR of Two bytes*/
	ret = spi_read(p61_dev->spi, p61_dev->buf + 1, 2);
	if (ret < 0) {
		P61_ERR_MSG("spi_read fails after [PCB]\n");
		ret = -EIO;
		goto fail;
	}

	total_count += 2;
	/* Get the data length */
	//count = rx_buffer[2];
	count = *(p61_dev->buf + 2);
	NFC_LOG_REC("Data Length = %zu", count);
	/* Read the available data along with one byte LRC */
	ret = spi_read(p61_dev->spi, (void *)(p61_dev->buf + 3), (count+1));
	if (ret < 0) {
		NFC_LOG_ERR("%s: spi_read failed\n", __func__);
		ret = -EIO;
		goto fail;
	}
	total_count = (total_count + (count+1));
	P61_DBG_MSG(KERN_INFO"total_count = %d", total_count);

	if (copy_to_user(buf, p61_dev->buf, total_count)) {
		P61_ERR_MSG("%s : failed to copy to user space\n", __func__);
		ret = -EFAULT;
		goto fail;
	}
	ret = total_count;
	P61_DBG_MSG("%s: ret %d Exit\n", __func__, ret);

	mutex_unlock(&p61_dev->read_mutex);

	return ret;

fail:
	P61_ERR_MSG("%s: Error ret %d Exit\n", __func__, ret);
	NFC_LOG_INFO("%s: count %zu  %d- Exit\n", __func__, count, ret);

	mutex_unlock(&p61_dev->read_mutex);
	return ret;
}

/**
 * \ingroup spi_driver
 * \brief Set the P61 device specific context for future use.
 * \param[in]       struct spi_device *
 * \param[in]       void *
 * \retval void
 */
static inline void p61_set_data(struct spi_device *spi, void *data)
{
	dev_set_drvdata(&spi->dev, data);
}

/**
 * \ingroup spi_driver
 * \brief Get the P61 device specific context.
 * \param[in]       const struct spi_device *
 * \retval Device Parameters
 */
static inline void *p61_get_data(const struct spi_device *spi)
{
	return dev_get_drvdata(&spi->dev);
}

static const struct file_operations p61_dev_fops = {
	.owner = THIS_MODULE,
	.read = p61_dev_read,
	.write = p61_dev_write,
	.open = p61_dev_open,
	.unlocked_ioctl = p61_dev_ioctl,
	.release = p61_dev_release,
};

static int p61_parse_dt(struct device *dev,
	struct p61_device *p61_dev)
{
	struct device_node *np = dev->of_node;
	struct device_node *spi_device_node;
	struct platform_device *spi_pdev;
	int ese_det_gpio;
	const char *ap_str;

	ese_det_gpio = of_get_named_gpio(np, "ese-det-gpio", 0);
	if (!gpio_is_valid(ese_det_gpio)) {
		NFC_LOG_INFO("%s: ese-det-gpio is not set\n", __func__);
	} else {
		gpio_request(ese_det_gpio, "ese_det_gpio");
		gpio_direction_input(ese_det_gpio);
		if (!gpio_get_value(ese_det_gpio)) {
			NFC_LOG_INFO("%s: ese is not supported\n", __func__);
			return -ENODEV;
		}
		NFC_LOG_INFO("%s: ese is supported\n", __func__);
	}

	if (!of_property_read_string(np, "p61,ap_vendor", &ap_str)) {
		if (!strcmp(ap_str, "slsi"))
			p61_dev->ap_vendor = AP_VENDOR_SLSI;
		else if (!strcmp(ap_str, "qct") || !strcmp(ap_str, "qualcomm"))
			p61_dev->ap_vendor = AP_VENDOR_QCT;
		else if (!strcmp(ap_str, "mtk"))
			p61_dev->ap_vendor = AP_VENDOR_MTK;
		NFC_LOG_INFO("AP vendor is %d\n", p61_dev->ap_vendor);
	} else {
		NFC_LOG_INFO("AP vendor is not set\n");
	}

	spi_device_node = of_get_parent(np);
	if (!IS_ERR_OR_NULL(spi_device_node)) {
		int i;
		int pin_ctrl_cnt = ese_get_pin_ctrl_count();

		p61_dev->spi_device_node = spi_device_node;
		spi_pdev = of_find_device_by_node(spi_device_node);
		p61_dev->pinctrl = devm_pinctrl_get(&spi_pdev->dev);

		if (!IS_ERR(p61_dev->pinctrl)) {
			for (i = 0; i < pin_ctrl_cnt; i++) {
				p61_dev->pinctrl_state[i] = pinctrl_lookup_state(p61_dev->pinctrl, g_pin_status[i]);
				if (IS_ERR(p61_dev->pinctrl_state[i])) {
					NFC_LOG_INFO("p61_pinctrl[%s] not found(%ld)\n",
							g_pin_status[i], PTR_ERR(p61_dev->pinctrl_state[i]));
					p61_dev->pinctrl_state[i] = NULL;
				}
			}
		} else {
			NFC_LOG_INFO("%s: devm_pinctrl_get failed\n", __func__);
		}
	} else {
		NFC_LOG_INFO("target does not use spi pinctrl\n");
	}

#ifdef CONFIG_NFC_FEATURE_SN100U
	p61_dev->spi_cs_gpio = of_get_named_gpio(np, "ese-spi_cs-gpio", 0);
	if (!gpio_is_valid(p61_dev->spi_cs_gpio))
		NFC_LOG_INFO("%s: cs_gpio is not set\n", __func__);
	else
		NFC_LOG_INFO("%s: cs_gpio is %d\n", __func__, p61_dev->spi_cs_gpio);
#endif
	return 0;
}

#if defined(CONFIG_NFC_FEATURE_SN100U)
static void p61_shutdown(void)
{
	if (p61_dev && !p61_dev->device_opened && !p61_dev->pid_diff) {
		NFC_LOG_INFO("%s : pid_diff %s\n", __func__, p61_dev->pid_diff ? "true" : "false");
		p61_dev->device_opened = true;
		ese_set_spi_configuration("lpm");
		P61_DBG_MSG("%s\n", __func__);
	}
}
#endif

/**
 * \ingroup spi_driver
 * \brief To probe for P61 SPI interface. If found initialize the SPI clock,
 * bit rate & SPI mode. It will create the dev entry (P61) for user space.
 *
 * \param[in]       struct spi_device *
 *
 * \retval 0 if ok.
 *
 */
static int p61_probe(struct device *dev)
{
	int ret = -1;

	nfc_logger_init();

	p61_dev = kzalloc(sizeof(*p61_dev), GFP_KERNEL);
	if (p61_dev == NULL) {
		P61_ERR_MSG("failed to allocate memory for module data\n");
		ret = -ENOMEM;
		goto err_exit;
	}

	ret = p61_parse_dt(dev, p61_dev);
	if (ret) {
		NFC_LOG_ERR("%s: Failed to parse DT\n", __func__);
		goto p61_parse_dt_failed;
	}
	NFC_LOG_INFO("%s: tz_mode=%d, isGpio_cfgDone:%d\n", __func__,
			p61_dev->tz_mode, p61_dev->isGpio_cfgDone);

#if defined(CONFIG_ESE_SECURE)
#if defined(CONFIG_ESE_USE_TZ_API)
	p61_dev->ese_secure_check = NOT_CHECKED;
	NFC_LOG_INFO("%s: eSE Secured system\n", __func__);
#endif
	ret = p61_clk_setup(dev, p61_dev);
	if (ret)
		NFC_LOG_ERR("%s - Failed to do clk_setup\n", __func__);

#endif
	p61_dev->miscdev.minor = MISC_DYNAMIC_MINOR;
	p61_dev->miscdev.name = "p61";
	p61_dev->miscdev.fops = &p61_dev_fops;
	p61_dev->miscdev.parent = dev;

	dev_set_drvdata(dev, p61_dev);
#if defined(CONFIG_NFC_FEATURE_SN100U)
#if IS_ENABLED(CONFIG_BATTERY_SAMSUNG)
	if (lpcharge)
		ese_set_spi_configuration("lpm");
	else
#endif
		ese_set_spi_configuration("sleep");
#else
#if !defined(CONFIG_ESE_SECURE)
	ese_set_spi_configuration("sleep");
#endif
#endif

	/* init mutex and queues */
	init_waitqueue_head(&p61_dev->read_wq);
	mutex_init(&p61_dev->read_mutex);
	mutex_init(&p61_dev->write_mutex);
	spin_lock_init(&p61_dev->ese_spi_lock);

	wake_lock_init(&p61_dev->ese_lock, WAKE_LOCK_SUSPEND, "ese_wake_lock");
	p61_dev->device_opened = false;

#if IS_ENABLED(CONFIG_BATTERY_SAMSUNG)
	if (!lpcharge) {
#else
	{
#endif
		ret = misc_register(&p61_dev->miscdev);
		if (ret < 0) {
			P61_ERR_MSG("misc_register failed! %d\n", ret);
			goto err_exit0;
		}
	}

	p61_dev->enable_poll_mode = 1; /* No USE? */

	p61_dev->buf = kzalloc(sizeof(unsigned char) * MAX_BUFFER_SIZE, GFP_KERNEL);
	if (p61_dev->buf == NULL) {
		P61_ERR_MSG("failed to allocate for spi buffer\n");
		ret = -ENOMEM;
		goto err_exit0;
	}
#if defined(CONFIG_NFC_FEATURE_SN100U)
	pn547_register_ese_shutdown(p61_shutdown);
#endif

	NFC_LOG_INFO("%s: finished\n", __func__);
	return ret;

err_exit0:
	mutex_destroy(&p61_dev->read_mutex);
	mutex_destroy(&p61_dev->write_mutex);
	wake_lock_destroy(&p61_dev->ese_lock);

p61_parse_dt_failed:
	if (p61_dev != NULL)
		kfree(p61_dev);
err_exit:
	P61_DBG_MSG("ERROR: Exit : %s ret %d\n", __func__, ret);
	return ret;
}

static int p61_remove(struct device *dev)
{
	struct p61_device *p61_dev = dev_get_drvdata(dev);

	P61_DBG_MSG("Entry : %s\n", __func__);
	mutex_destroy(&p61_dev->read_mutex);
	misc_deregister(&p61_dev->miscdev);
	wake_lock_destroy(&p61_dev->ese_lock);
	kfree(p61_dev->buf);
	kfree(p61_dev);

	return 0;
}

static int p61_platform_probe(struct platform_device *pdev)
{
	int ret = -1;

	ret = p61_probe(&pdev->dev);
	if (ret)
		goto p61_platform_setup_failed;

	NFC_LOG_INFO("%s: finished...\n", __func__);
	return ret;

p61_platform_setup_failed:
	P61_DBG_MSG("ERROR: Exit : %s ret %d\n", __func__, ret);
	return ret;
}

static int p61_platform_remove(struct platform_device *pdev)
{
	P61_DBG_MSG("Entry : %s\n", __func__);
	p61_remove(&pdev->dev);
	P61_DBG_MSG("Exit : %s\n", __func__);
	return 0;
}

static const struct of_device_id p61_secure_match_table[] = {
	{ .compatible = "p61_secure",},
	{},
};

static struct platform_driver p61_platform_driver = {
	.driver = {
		.name = "p61",
		.owner = THIS_MODULE,
#ifdef CONFIG_OF
		.of_match_table = p61_secure_match_table,
#endif
	},
	.probe =  p61_platform_probe,
	.remove = p61_platform_remove,
};

static int p61_spi_probe(struct spi_device *spi)
{
	int ret = -1;

	NFC_LOG_INFO("chip select(%d), bus number(%d), speed(%u)\n",
		spi->chip_select, spi->master->bus_num, spi->max_speed_hz);


	spi->bits_per_word = 8;
	spi->mode = SPI_MODE_0;

	ret = spi_setup(spi);
	if (ret < 0) {
		P61_ERR_MSG("failed to do spi_setup()\n");
		goto p61_spi_setup_failed;
	}

	ret = p61_probe(&spi->dev);
	if (ret)
		goto p61_spi_setup_failed;

	p61_dev->spi = spi;


	NFC_LOG_INFO("%s: finished\n", __func__);
	return ret;

p61_spi_setup_failed:
	P61_DBG_MSG("ERROR: Exit : %s ret %d\n", __func__, ret);
	return ret;
}

static int p61_spi_remove(struct spi_device *spi)
{
	P61_DBG_MSG("Entry : %s\n", __func__);
	p61_remove(&spi->dev);
	P61_DBG_MSG("Exit : %s\n", __func__);
	return 0;
}

static const struct of_device_id p61_match_table[] = {
	{ .compatible = "p61",},
	{},
};

static struct spi_driver p61_driver = {
	.driver = {
		.name = "p61",
		.bus = &spi_bus_type,
		.owner = THIS_MODULE,
#ifdef CONFIG_OF
		.of_match_table = p61_match_table,
#endif
	},
	.probe =  p61_spi_probe,
	.remove = p61_spi_remove,
};

#if IS_MODULE(CONFIG_SAMSUNG_NFC)
int p61_dev_init(void)
{
	int ret;

	debug_level = P61_FULL_DEBUG;

	NFC_LOG_INFO("Entry\n");
	ret = platform_driver_register(&p61_platform_driver);
	NFC_LOG_INFO("eSE platform_driver_register, ret %d\n", ret);

	ret = spi_register_driver(&p61_driver);
	NFC_LOG_INFO("eSE spi driver register, ret %d\n", ret);

	return ret;
}
EXPORT_SYMBOL(p61_dev_init);
void p61_dev_exit(void)
{
#ifdef CONFIG_ESE_SECURE
	platform_driver_unregister(&p61_platform_driver);
#else
	spi_unregister_driver(&p61_driver);
#endif
	NFC_LOG_INFO("Exit\n");
}
EXPORT_SYMBOL(p61_dev_exit);
#else
/**
 * \ingroup ese_driver
 * \brief Module init interface
 *
 * \param[in]       void
 *
 * \retval handle
 *
 */
static int __init p61_dev_init(void)
{
	int ret;

	debug_level = P61_FULL_DEBUG;

	NFC_LOG_INFO("Entry\n");
	ret = platform_driver_register(&p61_platform_driver);
	NFC_LOG_INFO("eSE platform_driver_register, ret %d\n", ret);

	ret = spi_register_driver(&p61_driver);
	NFC_LOG_INFO("eSE spi driver register, ret %d\n", ret);

	return ret;
}

/**
 * \ingroup ese_driver
 * \brief Module exit interface
 *
 * \param[in]       void
 *
 * \retval void
 *
 */
static void __exit p61_dev_exit(void)
{
	NFC_LOG_INFO("Entry\n");
#ifdef CONFIG_ESE_SECURE
	platform_driver_unregister(&p61_platform_driver);
#else
	spi_unregister_driver(&p61_driver);
#endif
	NFC_LOG_INFO("Exit\n");
}

/* module_init(p61_dev_init); */
late_initcall(p61_dev_init);
module_exit(p61_dev_exit);

MODULE_AUTHOR("BHUPENDRA PAWAR");
MODULE_DESCRIPTION("NXP P61 SPI driver");
MODULE_LICENSE("GPL");
#endif