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
|
/*====================================================================================*/
/* */
/* Copyright 2018-2019 NXP */
/* */
/* 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 */
/* */
/*====================================================================================*/
/**
* \addtogroup spi_driver
*
* @{ */
#define pr_fmt(fmt) "[sr100] %s: " fmt, __func__
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/init.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/of_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>
#ifdef CONFIG_UWB_PMIC_CLOCK
#include <linux/clk.h>
#endif
#include <linux/mutex.h>
#include "sr100.h"
#include "../uwb/uwb_logger/uwb_logger.h"
#ifdef CONFIG_NFC_FEATURE_SN100U
/*Invoke cold reset if no response from eSE*/
extern long p61_cold_reset(void);
#endif
static bool read_abort_requested = false;
static bool is_fw_dwnld_enabled = false;
#define SR100_TXBUF_SIZE 4200
#define SR100_RXBUF_SIZE 4200
#define SR100_MAX_TX_BUF_SIZE 4200
#define MAX_READ_RETRY_COUNT 10
/* Macro to define SPI clock frequency */
#define SR100_SPI_CLOCK 16000000L;
#define ENABLE_THROUGHPUT_MEASUREMENT 0
/* Maximum UCI packet size supported from the driver */
#define MAX_UCI_PKT_SIZE 4200
#define DEBUG_LOG
/* Different driver debug lever */
enum SR100_DEBUG_LEVEL { SR100_DEBUG_OFF, SR100_FULL_DEBUG };
enum spi_status_codes{
spi_transcive_success,
spi_transcive_fail,
spi_irq_wait_request,
spi_irq_wait_timeout
};
enum spi_operation_modes{SR100_WRITE_MODE, SR100_READ_MODE};
/* Variable to store current debug level request by ioctl */
static unsigned char debug_level;
#define SR100_DBG_MSG(msg...) \
switch (debug_level) { \
case SR100_DEBUG_OFF: \
break; \
case SR100_FULL_DEBUG: \
UWB_LOG_INFO(msg); \
break; \
default: \
printk(KERN_ERR "[NXP-SR100] : Wrong debug level %d", debug_level); \
break; \
}
#define SR100_ERR_MSG(msg...) \
UWB_LOG_ERR(msg);
/* Device specific macro and structure */
struct sr100_dev {
wait_queue_head_t read_wq; /* wait queue for read interrupt */
struct spi_device* spi; /* spi device structure */
struct miscdevice sr100_device; /* char device as misc driver */
unsigned int ce_gpio; /* SW Reset gpio */
unsigned int irq_gpio; /* SR100 will interrupt DH for any ntf */
unsigned int spi_handshake_gpio;/* host ready to read data */
unsigned int rtc_sync_gpio; /* rtc sync support for helios ranging */
unsigned int ant_connection_status_gpio; /* antenna connection status */
bool irq_enabled; /* flag to indicate irq is used */
bool irq_received; /* flag to indicate that irq is received */
spinlock_t irq_enabled_lock; /* spin lock for read irq */
unsigned char* tx_buffer; /* transmit buffer */
unsigned char* rx_buffer; /* receive buffer buffer */
unsigned int write_count; /* Holds nubers of byte writen*/
unsigned int read_count; /* Hold nubers of byte read */
struct mutex sr100_access_lock;/* Hold mutex lock to between read and write */
size_t totalBtyesToRead;
size_t IsExtndLenIndication;
int mode;
long timeOutInMs;
const char *uwb_vdd_io;
const char *uwb_vdd;
const char *uwb_vdd_rf;
const char *uwb_vdd_se_i2c_pullup;
#ifdef CONFIG_UWB_PMIC_CLOCK
struct clk *clk;
#endif
};
#if (ENABLE_THROUGHPUT_MEASUREMENT == 1)
#define READ_THROUGH_PUT 0x01
#define WRITE_THROUGH_PUT 0x02
struct sr100_through_put {
struct timeval rstart_tv;
struct timeval wstart_tv;
struct timeval rstop_tv;
struct timeval wstop_tv;
unsigned long total_through_put_wbytes;
unsigned long total_through_put_rbytes;
unsigned long total_through_put_rtime;
unsigned long total_through_put_wtime;
};
static struct sr100_through_put sr100_through_put_t;
static void sr100_start_throughput_measurement(unsigned int type);
static void sr100_stop_throughput_measurement(unsigned int type,
int no_of_bytes);
/******************************************************************************
* Function : sr100_start_throughput_measurement
*
* Description : Start this api to measaure the spi performance
*
* Parameters : type : sr100 device Write/Read
*
* Returns : Returns void
****************************************************************************/
static void sr100_start_throughput_measurement(unsigned int type) {
if (type == READ_THROUGH_PUT) {
memset(&sr100_through_put_t.rstart_tv, 0x00, sizeof(struct timeval));
do_gettimeofday(&sr100_through_put_t.rstart_tv);
} else if (type == WRITE_THROUGH_PUT) {
memset(&sr100_through_put_t.wstart_tv, 0x00, sizeof(struct timeval));
do_gettimeofday(&sr100_through_put_t.wstart_tv);
} else {
pr_err(" sr100_start_throughput_measurement: wrong type = %d", type);
}
}
/******************************************************************************
* Function : sr100_stop_throughput_measurement
*
* Description : Stop this api to end the measaure of the spi performance
*
* Parameters : type : sr100 device Write/Read
*
* Returns : Returns void
****************************************************************************/
static void sr100_stop_throughput_measurement(unsigned int type,
int no_of_bytes) {
if (type == READ_THROUGH_PUT) {
memset(&sr100_through_put_t.rstop_tv, 0x00, sizeof(struct timeval));
do_gettimeofday(&sr100_through_put_t.rstop_tv);
sr100_through_put_t.total_through_put_rbytes += no_of_bytes;
sr100_through_put_t.total_through_put_rtime +=
(sr100_through_put_t.rstop_tv.tv_usec -
sr100_through_put_t.rstart_tv.tv_usec) +
((sr100_through_put_t.rstop_tv.tv_sec -
sr100_through_put_t.rstart_tv.tv_sec) *
1000000);
} else if (type == WRITE_THROUGH_PUT) {
memset(&sr100_through_put_t.wstop_tv, 0x00, sizeof(struct timeval));
do_gettimeofday(&sr100_through_put_t.wstop_tv);
sr100_through_put_t.total_through_put_wbytes += no_of_bytes;
sr100_through_put_t.total_through_put_wtime +=
(sr100_through_put_t.wstop_tv.tv_usec -
sr100_through_put_t.wstart_tv.tv_usec) +
((sr100_through_put_t.wstop_tv.tv_sec -
sr100_through_put_t.wstart_tv.tv_sec) *
1000000);
} else {
pr_err(" sr100_stop_throughput_measurement: wrong type = %d", type);
}
}
#endif
/******************************************************************************
* Function : sr100_dev_open
*
* Description : Open sr100 device node and returns instance to the user space
*
* Parameters : inode : sr100 device node path
* filep : File pointer to structure of sr100 device
*
* Returns : Returns file descriptor for sr100 device
* otherwise indicate each error code
****************************************************************************/
static int sr100_dev_open(struct inode* inode, struct file* filp) {
struct sr100_dev* sr100_dev =
container_of(filp->private_data, struct sr100_dev, sr100_device);
filp->private_data = sr100_dev;
SR100_DBG_MSG("%s : Major No: %d, Minor No: %d\n", __func__, imajor(inode),
iminor(inode));
return 0;
}
/******************************************************************************
* Function : sr100_disable_irq
*
* Description : To disable IRQ
*
* Parameters : sr100_dev : sr100 device structure pointer
*
* Returns : Returns void
****************************************************************************/
static void sr100_disable_irq(struct sr100_dev* sr100_dev) {
unsigned long flags;
#ifdef DEBUG_LOG
SR100_DBG_MSG("d i\n");
#endif
spin_lock_irqsave(&sr100_dev->irq_enabled_lock, flags);
if((sr100_dev->irq_enabled)){
disable_irq_nosync(sr100_dev->spi->irq);
sr100_dev->irq_received = true;
sr100_dev->irq_enabled = false;
}
spin_unlock_irqrestore(&sr100_dev->irq_enabled_lock, flags);
}
/******************************************************************************
* Function : sr100_enable_irq
*
* Description : Set the irq flag status
*
* Parameters : sr100_dev : sr100 device structure pointer
*
* Returns : Returns void
****************************************************************************/
static void sr100_enable_irq(struct sr100_dev* sr100_dev) {
unsigned long flags;
#ifdef DEBUG_LOG
SR100_DBG_MSG("e i\n");
#endif
spin_lock_irqsave(&sr100_dev->irq_enabled_lock, flags);
if(!sr100_dev->irq_enabled){
enable_irq(sr100_dev->spi->irq);
sr100_dev->irq_enabled = true;
sr100_dev->irq_received = false;
}
spin_unlock_irqrestore(&sr100_dev->irq_enabled_lock, flags);
}
/******************************************************************************
* Function : sr100_dev_irq_handler
*
* Description : Will get called when interrupt line asserted from SR100
*
* Parameters : irq : IRQ Number
* dev_id : sr100 device Id
*
* Returns : Returns IRQ Handler
****************************************************************************/
static irqreturn_t sr100_dev_irq_handler(int irq, void* dev_id) {
struct sr100_dev* sr100_dev = dev_id;
#ifdef DEBUG_LOG
SR100_DBG_MSG("h\n");
#endif
sr100_disable_irq(sr100_dev);
/* Wake up waiting readers */
wake_up(&sr100_dev->read_wq);
return IRQ_HANDLED;
}
/******************************************************************************
* Function : sr100_dev_iotcl
*
* Description : Input/OutPut control from user space to perform required
* operation on sr100 device.
*
* Parameters : cmd : Indicates what operation needs to be done sr100
* arg : Value to be passed to sr100 to do the required
* opeation
*
* Returns : 0 on success and (-1) on error
****************************************************************************/
static long sr100_dev_ioctl(struct file* filp, unsigned int cmd,
unsigned long arg) {
int ret = 0;
struct sr100_dev* sr100_dev = NULL;
SR100_DBG_MSG("i\n");
sr100_dev = filp->private_data;
switch (cmd) {
case SR100_SET_PWR:
if (arg == PWR_ENABLE) {
SR100_DBG_MSG("enable power request\n");
if((int)sr100_dev->rtc_sync_gpio > 0) {
gpio_set_value(sr100_dev->rtc_sync_gpio, 1);
}
gpio_set_value(sr100_dev->ce_gpio, 1);
msleep(10);
} else if (arg == PWR_DISABLE) {
SR100_DBG_MSG("disable power request\n");
gpio_set_value(sr100_dev->ce_gpio, 0);
if((int)sr100_dev->rtc_sync_gpio > 0) {
gpio_set_value(sr100_dev->rtc_sync_gpio, 0);
}
sr100_disable_irq(sr100_dev);
msleep(10);
} else if (arg == ABORT_READ_PENDING) {
SR100_DBG_MSG( "Abort Read Pending\n");
read_abort_requested = true;
sr100_disable_irq(sr100_dev);
/* Wake up waiting readers */
wake_up(&sr100_dev->read_wq);
}
break;
case SR100_SET_FWD:
if (arg == 1) {
is_fw_dwnld_enabled = true;
read_abort_requested = false;
SR100_DBG_MSG("FW download enabled\n");
} else if(arg == 0){
is_fw_dwnld_enabled = false;
SR100_DBG_MSG("FW download disabled\n");
}
break;
case SR100_GET_THROUGHPUT:
if (arg == 0) {
#if (ENABLE_THROUGHPUT_MEASUREMENT == 1)
pr_err(" **************** Write-Read Throughput: **************");
pr_err(" No of Write Bytes = %ld", sr100_through_put_t.total_through_put_wbytes);
pr_err(" No of Read Bytes = %ld", sr100_through_put_t.total_through_put_rbytes);
pr_err(" Total Write Time (uSec) = %ld", sr100_through_put_t.total_through_put_wtime);
pr_err(" Total Read Time (uSec) = %ld", sr100_through_put_t.total_through_put_rtime);
pr_err(" Total Write-Read Time (uSec) = %ld", sr100_through_put_t.total_through_put_wtime +
sr100_through_put_t.total_through_put_rtime);
sr100_through_put_t.total_through_put_wbytes = 0;
sr100_through_put_t.total_through_put_rbytes = 0;
sr100_through_put_t.total_through_put_wtime = 0;
sr100_through_put_t.total_through_put_rtime = 0;
pr_err(" **************** Write-Read Throughput: **************");
#endif
}
break;
#ifdef CONFIG_NFC_FEATURE_SN100U
case SR100_ESE_RESET:
SR100_DBG_MSG("SR100_ESE_RESET Enter\n");
ret = p61_cold_reset();
break;
#endif
case SR100_GET_ANT_CONNECTION_STATUS:
SR100_DBG_MSG("SR100_GET_ANT_CONNECTION_STATUS Enter\n");
if((int)sr100_dev->ant_connection_status_gpio > 0) {
ret = !gpio_get_value(sr100_dev->ant_connection_status_gpio);
} else {
ret = -1;
}
SR100_DBG_MSG("SR100_GET_ANT_CONNECTION_STATUS ret =%d \n", ret);
break;
default:
SR100_DBG_MSG(" Error case\n");
ret = -EINVAL; // ToDo: After adding proper switch cases we have to
// return with error statusi here
}
return ret;
}
/******************************************************************************
* Function : sr100_dev_transceive
*
* Description : Used to Write/read data from SR100
*
* Parameters : sr100_dev :sr100 device structure pointer
* op_mode :Indicates write/read mode
* count : Number of bytes to be write/read
* Returns : Number of bytes write/read if read is success else (-1)
* otherwise indicate each error code
****************************************************************************/
static int sr100_dev_transceive(struct sr100_dev* sr100_dev, int op_mode, int count ){
int ret,retry_count;
mutex_lock(&sr100_dev->sr100_access_lock);
sr100_dev->mode = op_mode;
sr100_dev->totalBtyesToRead = 0;
sr100_dev->IsExtndLenIndication = 0;
ret = -1;
retry_count = 0;
switch(sr100_dev->mode){
case SR100_WRITE_MODE:
{
sr100_dev->write_count = 0;
/* UCI Header write */
ret = spi_write(sr100_dev->spi, sr100_dev->tx_buffer, NORMAL_MODE_HEADER_LEN);
if (ret < 0) {
ret = -EIO;
SR100_ERR_MSG("spi_write header : Failed.\n");
goto transcive_end;
} else {
count -= NORMAL_MODE_HEADER_LEN;
}
if(count > 0) {
udelay(10);
/* UCI Payload write */
ret = spi_write(sr100_dev->spi, sr100_dev->tx_buffer + NORMAL_MODE_HEADER_LEN, count);
if (ret < 0) {
ret = -EIO;
SR100_ERR_MSG("spi_write payload : Failed.\n");
goto transcive_end;
}
}
sr100_dev->write_count = count + NORMAL_MODE_HEADER_LEN;
ret = spi_transcive_success;
}
break;
case SR100_READ_MODE:
{
if(!gpio_get_value(sr100_dev->irq_gpio)){
SR100_ERR_MSG("IRQ might have gone low due to write\n");
ret = spi_irq_wait_request;
goto transcive_end;
}
retry_count = 0;
gpio_set_value(sr100_dev->spi_handshake_gpio, 1);
while (gpio_get_value(sr100_dev->irq_gpio)) {
if (retry_count == 100) {
break;
}
udelay(10);
retry_count++;
}
sr100_enable_irq(sr100_dev);
sr100_dev->read_count = 0;
retry_count = 0;
/* wait for inetrrupt upto 500ms after that timeout will happen and returns read fail */
ret = wait_event_interruptible_timeout(sr100_dev->read_wq, sr100_dev->irq_received,sr100_dev->timeOutInMs);
if (ret == 0) {
SR100_ERR_MSG("wait_event_interruptible timeout() : Failed.\n");
ret = spi_irq_wait_timeout;
goto transcive_end;
}
#if 0 // ideally below code is not required to check the gpio status in loop
sr100_set_irq_flag(sr100_dev);
if(!gpio_get_value(sr100_dev->irq_gpio)){
SR100_ERR_MSG("Spurious interrupt detected at second irq\n");
retry_count++;
if(retry_count == MAX_READ_RETRY_COUNT){
retry_count = 0;
SR100_ERR_MSG("Max retry count reached at second irq\n");
} else{
msleep(3);
sr100_dev->irq_enabled = false;
goto second_irq_wait;
}
}
#endif
if(!gpio_get_value(sr100_dev->irq_gpio)){
SR100_ERR_MSG("Second IRQ is Low\n");
ret = -1;
goto transcive_end;
}
ret = spi_read(sr100_dev->spi, (void*)sr100_dev->rx_buffer, NORMAL_MODE_HEADER_LEN);
if (ret < 0) {
SR100_ERR_MSG("sr100_dev_read: spi read error %d\n ", ret);
goto transcive_end;
}
sr100_dev->IsExtndLenIndication = (sr100_dev->rx_buffer[EXTND_LEN_INDICATOR_OFFSET] & EXTND_LEN_INDICATOR_OFFSET_MASK);
sr100_dev->totalBtyesToRead = sr100_dev->rx_buffer[NORMAL_MODE_LEN_OFFSET];
if(sr100_dev->IsExtndLenIndication){
sr100_dev->totalBtyesToRead = ((sr100_dev->totalBtyesToRead << 8) | sr100_dev->rx_buffer[EXTENDED_LENGTH_OFFSET]);
}
if(sr100_dev->totalBtyesToRead > (MAX_UCI_PKT_SIZE - NORMAL_MODE_HEADER_LEN)) {
SR100_ERR_MSG("Length %d exceeds the max limit %d....\n",(int)sr100_dev->totalBtyesToRead,(int)MAX_UCI_PKT_SIZE);
ret = -1;
goto transcive_end;
}
if(sr100_dev->totalBtyesToRead > 0){
ret = spi_read(sr100_dev->spi, (void*)(sr100_dev->rx_buffer + NORMAL_MODE_HEADER_LEN), sr100_dev->totalBtyesToRead);
if (ret < 0) {
SR100_ERR_MSG("sr100_dev_read: spi read error.. %d\n ", ret);
goto transcive_end;
}
}
sr100_dev->read_count = (unsigned int)(sr100_dev->totalBtyesToRead + NORMAL_MODE_HEADER_LEN);
retry_count = 0;
do{
usleep_range(10,15);
retry_count++;
if(retry_count >= 1000){
SR100_ERR_MSG("IC not released the IRQ even after 10ms\n");
break;
}
}while(gpio_get_value(sr100_dev->irq_gpio));
ret = spi_transcive_success;
gpio_set_value(sr100_dev->spi_handshake_gpio, 0);
}
break;
default:
SR100_ERR_MSG("invalid operation .....\n");
break;
}
transcive_end:
if(sr100_dev->mode == SR100_READ_MODE){
gpio_set_value(sr100_dev->spi_handshake_gpio, 0);
}
mutex_unlock(&sr100_dev->sr100_access_lock);
return ret;
}
/******************************************************************************
* Function : sr100_hbci_write
*
* Description : Used to write hbci packets
*
* Parameters : sr100_dev :sr100 device structure pointer
* count : Number of bytes to be write
* Returns : return success(spi_transcive_success)or fail (-1)
****************************************************************************/
static int sr100_hbci_write(struct sr100_dev* sr100_dev, int count ){
int ret = -1;
sr100_dev->write_count = 0;
/* HBCI write */
ret = spi_write(sr100_dev->spi, sr100_dev->tx_buffer, count);
if (ret < 0) {
ret = -EIO;
SR100_ERR_MSG("spi_write fw download : Failed.\n");
goto hbci_write_fail;
}
sr100_dev->write_count = count;
sr100_enable_irq(sr100_dev);
ret = spi_transcive_success;
return ret;
hbci_write_fail:
SR100_ERR_MSG("sr100_hbci_write failed...%d", ret);
return ret;
}
/******************************************************************************
* Function : sr100_dev_write
*
* Description : Write Data to sr100 on SPI line
*
* Parameters : filp : Device Node File Pointer
* buf : Buffer which contains data to be sent to sr100
* count : Number of bytes to be send
* offset : Pointer to a object that indicates file position
* user is accessing.
* Returns : Number of bytes writen if write is success else (-1)
* otherwise indicate each error code
****************************************************************************/
static ssize_t sr100_dev_write(struct file* filp, const char* buf, size_t count,
loff_t* offset) {
int ret = -1;
struct sr100_dev* sr100_dev;
#ifdef DEBUG_LOG
SR100_DBG_MSG("w\n");
#endif
sr100_dev = filp->private_data;
if (count > SR100_MAX_TX_BUF_SIZE || count > SR100_TXBUF_SIZE) {
SR100_ERR_MSG("%s : Write Size Exceeds\n", __func__);
ret = -ENOBUFS;
goto write_end;
}
if (copy_from_user(sr100_dev->tx_buffer, buf, count)) {
SR100_ERR_MSG("%s : failed to copy from user space\n", __func__);
return -EFAULT;
}
#if (ENABLE_THROUGHPUT_MEASUREMENT == 1)
sr100_start_throughput_measurement(WRITE_THROUGH_PUT);
#endif
if(is_fw_dwnld_enabled){
ret = sr100_hbci_write(sr100_dev, count);
}else{
ret = sr100_dev_transceive(sr100_dev,SR100_WRITE_MODE, count);
}
if(ret == spi_transcive_success){
ret = sr100_dev->write_count;
} else{
SR100_ERR_MSG("write failed......\n");
}
#if (ENABLE_THROUGHPUT_MEASUREMENT == 1)
sr100_stop_throughput_measurement(WRITE_THROUGH_PUT, ret);
#endif
#ifdef DEBUG_LOG
SR100_DBG_MSG("w %d\n", ret);
#endif
write_end:
return ret;
}
/******************************************************************************
* Function : sr100_hbci_read
*
* Description : Read Data From sr100 on SPI line
*
* Parameters : sr100_dev : sr100 device structure
* buf : Buffer which contains data to be read from sr100
* count : Number of bytes to be read
*
* Returns : Number of bytes read if read is success else (-1)
* otherwise indicate each error code
****************************************************************************/
static ssize_t sr100_hbci_read(struct sr100_dev *sr100_dev,char* buf, size_t count){
int ret = -EIO;
if(count > SR100_RXBUF_SIZE) {
SR100_ERR_MSG("count(%d) out of range(0-%d)\n", count, SR100_RXBUF_SIZE);
ret = -EINVAL;
goto hbci_fail;
}
/* wait for inetrrupt upto 500ms after that timeout will happen and returns read fail */
ret = wait_event_interruptible_timeout(sr100_dev->read_wq, sr100_dev->irq_received, sr100_dev->timeOutInMs);
if (ret == 0) {
SR100_ERR_MSG("hbci wait_event_interruptible timeout() : Failed.\n");
ret = -1;
goto hbci_fail;
}
if (read_abort_requested) {
read_abort_requested = false;
SR100_ERR_MSG("HBCI Abort Read pending......");
return ret;
}
if(!gpio_get_value(sr100_dev->irq_gpio)){
SR100_DBG_MSG("IRQ is low during firmware download\n");
goto hbci_fail;
}
#if (ENABLE_THROUGHPUT_MEASUREMENT == 1)
sr100_start_throughput_measurement(READ_THROUGH_PUT);
#endif
ret = spi_read(sr100_dev->spi, (void*)sr100_dev->rx_buffer, count);
if (ret < 0) {
SR100_ERR_MSG("sr100_hbci_read: spi read error %d\n ", ret);
goto hbci_fail;
}
ret = count;
#if (ENABLE_THROUGHPUT_MEASUREMENT == 1)
sr100_stop_throughput_measurement(READ_THROUGH_PUT, count);
#endif
if (copy_to_user(buf, sr100_dev->rx_buffer, count)) {
SR100_ERR_MSG("sr100_hbci_read: copy to user failed\n");
ret = -EFAULT;
}
#ifdef DEBUG_LOG
SR100_DBG_MSG("R %d\n", ret);
#endif
return ret;
hbci_fail:
SR100_ERR_MSG("Error sr100_hbci_read ret %d Exit\n", ret);
return ret;
}
/******************************************************************************
* Function : sr100_dev_read
*
* Description : Used to read data from SR100
*
* Parameters : filp : Device Node File Pointer
* buf : Buffer which contains data to be read from sr100
* count : Number of bytes to be read
* offset : Pointer to a object that indicates file position
* user is accessing.
* Returns : Number of bytes read if read is success else (-1)
* otherwise indicate each error code
****************************************************************************/
static ssize_t sr100_dev_read(struct file* filp, char* buf, size_t count,
loff_t* offset) {
struct sr100_dev* sr100_dev = filp->private_data;
int ret = -EIO;
int retry_count = 0;
#ifdef DEBUG_LOG
SR100_DBG_MSG("r\n");
#endif
/*500ms timeout in jiffies*/
sr100_dev->timeOutInMs = ((500*HZ)/1000);
memset(sr100_dev->rx_buffer, 0x00, SR100_RXBUF_SIZE);
if (!gpio_get_value(sr100_dev->irq_gpio)) {
if (filp->f_flags & O_NONBLOCK) {
ret = -EAGAIN;
goto read_end;
}
}
/*HBCI packet read*/
if(is_fw_dwnld_enabled){
return sr100_hbci_read(sr100_dev,buf,count);
}
/*UCI packet read*/
first_irq_wait:
sr100_enable_irq(sr100_dev);
if (!read_abort_requested) {
ret = wait_event_interruptible(sr100_dev->read_wq, sr100_dev->irq_received);
if (ret) {
if (ret != -ERESTARTSYS) {
SR100_ERR_MSG("read_wq wait_event_interruptible() :%d Failed.\n", ret);
}
goto read_end;
}
}
if (read_abort_requested) {
read_abort_requested = false;
SR100_ERR_MSG("Abort Read pending......\n");
return ret;
}
ret = sr100_dev_transceive(sr100_dev,SR100_READ_MODE, count);
if(ret == spi_transcive_success){
if (copy_to_user(buf, sr100_dev->rx_buffer, sr100_dev->read_count)) {
SR100_ERR_MSG("sr100_dev_read: copy to user failed\n");
ret = -EFAULT;
goto read_end;
}
ret = sr100_dev->read_count;
} else if(ret == spi_irq_wait_request){
SR100_DBG_MSG(" irg is low due to write hence irq is requested again...\n");
goto first_irq_wait;
} else if(ret == spi_irq_wait_timeout){
SR100_DBG_MSG("second irq is not received..Time out...\n");
ret = -1;
} else {
SR100_ERR_MSG("spi read failed...%d\n", ret);
ret = -1;
}
read_end:
#ifdef DEBUG_LOG
if (ret != -ERESTARTSYS) {
SR100_DBG_MSG("r %d\n", ret);
}
#endif
retry_count = 0;
return ret;
}
/******************************************************************************
* Function : sr100_hw_setup
*
* Description : Used to read data from SR100
*
* Parameters : platform_data : struct sr100_spi_platform_data *
*
* Returns : retval 0 if ok else -1 on error
****************************************************************************/
static int sr100_hw_setup(struct sr100_spi_platform_data* platform_data) {
int ret = -1;
SR100_DBG_MSG("Entry : %s\n", __FUNCTION__);
ret = gpio_request(platform_data->irq_gpio, "sr100 irq");
if (ret < 0) {
SR100_ERR_MSG("gpio request failed gpio = 0x%x\n", platform_data->irq_gpio);
goto fail;
}
ret = gpio_direction_input(platform_data->irq_gpio);
if (ret < 0) {
SR100_ERR_MSG("gpio request failed gpio = 0x%x\n", platform_data->irq_gpio);
goto fail_irq;
}
ret = gpio_request(platform_data->ce_gpio, "sr100 ce");
if (ret < 0) {
SR100_ERR_MSG("sr100 - Failed requesting ce gpio - %d\n", platform_data->ce_gpio);
goto fail_gpio;
}
ret = gpio_direction_output(platform_data->ce_gpio, 0);
if (ret < 0) {
SR100_ERR_MSG("sr100 - Failed setting ce gpio - %d\n", platform_data->ce_gpio);
goto fail_gpio;
}
if((int)platform_data->rtc_sync_gpio > 0) {
ret = gpio_request(platform_data->rtc_sync_gpio, "sr100 rtc sync");
if (ret < 0) {
SR100_ERR_MSG("sr100 - Failed requesting rtc sync gpio - %d\n", platform_data->rtc_sync_gpio);
goto fail_gpio;
}
ret = gpio_direction_output(platform_data->rtc_sync_gpio, 0);
if (ret < 0) {
SR100_ERR_MSG("sr100 - Failed setting rtc sync gpio - %d\n", platform_data->rtc_sync_gpio);
goto fail_gpio;
}
}
ret = gpio_request(platform_data->spi_handshake_gpio, "sr100 ri");
if (ret < 0) {
SR100_ERR_MSG("sr100 - Failed requesting spi handshake gpio - %d\n", platform_data->spi_handshake_gpio);
goto fail_gpio;
}
ret = gpio_direction_output(platform_data->spi_handshake_gpio, 0);
if (ret < 0) {
SR100_ERR_MSG("sr100 - Failed setting spi handshake gpio - %d\n", platform_data->spi_handshake_gpio);
goto fail_gpio;
}
if((int)platform_data->ant_connection_status_gpio > 0) {
ret = gpio_request(platform_data->ant_connection_status_gpio, "sr100 ant connection status");
if (ret < 0) {
SR100_ERR_MSG("sr100 - Failed requesting ant connection status gpio - %d\n", platform_data->ant_connection_status_gpio);
goto fail_gpio;
}
ret = gpio_direction_input(platform_data->ant_connection_status_gpio);
if (ret < 0) {
SR100_ERR_MSG("sr100 - Failed setting ant connection status gpio - %d\n", platform_data->ant_connection_status_gpio);
goto fail_gpio;
}
}
ret = 0;
SR100_DBG_MSG("Exit : %s\n", __FUNCTION__);
return ret;
fail_gpio:
if((int)platform_data->ant_connection_status_gpio > 0) {
gpio_free(platform_data->ant_connection_status_gpio);
}
gpio_free(platform_data->spi_handshake_gpio);
gpio_free(platform_data->ce_gpio);
if((int)platform_data->rtc_sync_gpio > 0) {
gpio_free(platform_data->rtc_sync_gpio);
}
fail_irq:
gpio_free(platform_data->irq_gpio);
fail:
SR100_ERR_MSG("sr100_hw_setup failed\n");
return ret;
}
/******************************************************************************
* Function : sr100_set_data
*
* Description : Set the SR100 device specific context for future use
*
* Parameters : spi : struct spi_device *
* data: void*
*
* Returns : retval 0 if ok else -1 on error
****************************************************************************/
static inline void sr100_set_data(struct spi_device* spi, void* data) {
dev_set_drvdata(&spi->dev, data);
}
/******************************************************************************
* Function : sr100_get_data
*
* Description : Get the SR100 device specific context
*
* Parameters : spi : struct spi_device *
*
* Returns : retval 0 if ok else -1 on error
****************************************************************************/
static inline void* sr100_get_data(const struct spi_device* spi) {
return dev_get_drvdata(&spi->dev);
}
/* possible fops on the sr100 device */
static const struct file_operations sr100_dev_fops = {
.owner = THIS_MODULE,
.read = sr100_dev_read,
.write = sr100_dev_write,
.open = sr100_dev_open,
.unlocked_ioctl = sr100_dev_ioctl,
};
/******************************************************************************
* Function : sr100_parse_dt
*
* Description : Parse the dtsi configartion
*
* Parameters : dev : struct spi_device *
* pdata: Ponter to platform data
*
* Returns : retval 0 if ok else -1 on error
****************************************************************************/
static int sr100_parse_dt(struct device* dev,
struct sr100_spi_platform_data* pdata) {
struct device_node* np = dev->of_node;
SR100_DBG_MSG("Entry : %s\n", __FUNCTION__);
pdata->irq_gpio = of_get_named_gpio(np, "nxp,sr100-irq", 0);
if (!gpio_is_valid(pdata->irq_gpio)) {
SR100_ERR_MSG("nxp,sr100-irq not found\n");
return -EINVAL;
}
pdata->ce_gpio = of_get_named_gpio(np, "nxp,sr100-ce", 0);
if (!gpio_is_valid(pdata->ce_gpio)) {
SR100_ERR_MSG("nxp,sr100-ce not found\n");
return -EINVAL;
}
pdata->rtc_sync_gpio = of_get_named_gpio(np, "nxp,sr100-rtc-sync", 0);
if (!gpio_is_valid(pdata->rtc_sync_gpio)) {
SR100_ERR_MSG("nxp,sr100-rtc-sync not found\n");
//return -EINVAL;
}
pdata->spi_handshake_gpio = of_get_named_gpio(np, "nxp,sr100-ri", 0);
if (!gpio_is_valid(pdata->spi_handshake_gpio)) {
SR100_ERR_MSG("nxp,sr100-ri not found\n");
return -EINVAL;
}
pdata->ant_connection_status_gpio= of_get_named_gpio(np, "nxp,sr100-ant-connection-status", 0);
if (!gpio_is_valid(pdata->ant_connection_status_gpio)) {
SR100_ERR_MSG("nxp,sr100-ant-connection-status not found\n");
//return -EINVAL;
}
if (of_property_read_string(np, "nxp,vdd-io", &pdata->uwb_vdd_io) < 0) {
SR100_ERR_MSG("uwb_vdd_io not found\n");
pdata->uwb_vdd_io = NULL;
} else {
SR100_ERR_MSG("uwb_vdd_io :%s\n", pdata->uwb_vdd_io);
}
if (of_property_read_string(np, "nxp,vdd", &pdata->uwb_vdd) < 0) {
SR100_ERR_MSG("uwb_vdd not found\n");
pdata->uwb_vdd = NULL;
} else {
SR100_ERR_MSG("uwb_vdd :%s\n", pdata->uwb_vdd);
}
if (of_property_read_string(np, "nxp,vdd-rf", &pdata->uwb_vdd_rf) < 0) {
SR100_ERR_MSG("uwb_vdd_rf not found\n");
pdata->uwb_vdd_rf = NULL;
} else {
SR100_ERR_MSG("uwb_vdd_rf :%s\n", pdata->uwb_vdd_rf);
}
if (of_property_read_string(np, "nxp,vdd-se-i2c-pullup", &pdata->uwb_vdd_se_i2c_pullup) < 0) {
SR100_ERR_MSG("uwb_vdd_se_i2c_pullup not found\n");
pdata->uwb_vdd_se_i2c_pullup = NULL;
} else {
SR100_ERR_MSG("uwb_vdd_se_i2c_pullup :%s\n", pdata->uwb_vdd_se_i2c_pullup);
}
#ifdef CONFIG_UWB_PMIC_CLOCK
if (of_get_property(np, "nxp,sr100-pm-clk", NULL)) {
pdata->clk = clk_get(dev, "pll_clk");
if (IS_ERR(pdata->clk)) {
SR100_ERR_MSG("Couldn't get pll_clk\n");
} else {
SR100_DBG_MSG("got pll_clk\n");
/* sdm845: if prepare_enable clk, clk always generated*/
clk_prepare_enable(pdata->clk);
}
}
#endif
SR100_DBG_MSG("sr100 : irq_gpio = %u, ce_gpio = %u, rtc_sync_gpio = %u, spi_handshake_gpio = %u, ant_connection_status_gpio = %u\n",
pdata->irq_gpio, pdata->ce_gpio, pdata->rtc_sync_gpio, pdata->spi_handshake_gpio, pdata->ant_connection_status_gpio);
return 0;
}
static int sr100_regulator_onoff(struct device *dev, struct sr100_dev* pdev, bool onoff)
{
int rc = 0;
struct regulator *regulator_uwb_vdd_io = NULL, *regulator_uwb_vdd = NULL, *regulator_uwb_vdd_rf = NULL, *regulator_uwb_vdd_se_i2c_pullup = NULL;
if(pdev->uwb_vdd_io != NULL) {
regulator_uwb_vdd_io = regulator_get(dev, pdev->uwb_vdd_io);
if (IS_ERR(regulator_uwb_vdd_io) || regulator_uwb_vdd_io == NULL) {
SR100_ERR_MSG("regulator_uwb_vdd_io regulator_get fail\n");
return -ENODEV;
}
} else {
regulator_uwb_vdd_io = NULL;
SR100_ERR_MSG("regulator_uwb_vdd_io not support\n");
}
if(pdev->uwb_vdd != NULL) {
regulator_uwb_vdd = regulator_get(dev, pdev->uwb_vdd);
if (IS_ERR(regulator_uwb_vdd) || regulator_uwb_vdd == NULL) {
SR100_ERR_MSG("regulator_uwb_vdd regulator_get fail\n");
rc = -ENODEV;
goto regulator_err;
}
} else {
regulator_uwb_vdd = NULL;
SR100_ERR_MSG("regulator_uwb_vdd not support\n");
}
if(pdev->uwb_vdd_rf != NULL) {
regulator_uwb_vdd_rf = regulator_get(dev, pdev->uwb_vdd_rf);
if (IS_ERR(regulator_uwb_vdd_rf) || regulator_uwb_vdd_rf == NULL) {
SR100_ERR_MSG("regulator_uwb_vdd_rf regulator_get fail\n");
rc = -ENODEV;
goto regulator_err;
}
} else {
regulator_uwb_vdd_rf = NULL;
SR100_ERR_MSG("regulator_uwb_vdd_rf not support\n");
}
if(pdev->uwb_vdd_se_i2c_pullup != NULL) {
regulator_uwb_vdd_se_i2c_pullup = regulator_get(dev, pdev->uwb_vdd_se_i2c_pullup);
if (IS_ERR(regulator_uwb_vdd_se_i2c_pullup) || regulator_uwb_vdd_se_i2c_pullup == NULL) {
SR100_ERR_MSG("regulator_uwb_vdd_se_i2c_pullup regulator_get fail\n");
rc = -ENODEV;
goto regulator_err;
}
} else {
regulator_uwb_vdd_se_i2c_pullup = NULL;
SR100_ERR_MSG("regulator_uwb_vdd_se_i2c_pullup not support\n");
}
SR100_DBG_MSG("sr100_regulator_onoff = %d\n", onoff);
if (onoff == true) {
if(regulator_uwb_vdd_io != NULL) {
rc = regulator_enable(regulator_uwb_vdd_io);
if (rc) {
SR100_ERR_MSG("regulator_uwb_vdd_io enable failed, rc=%d\n", rc);
goto regulator_err;
}
}
if(regulator_uwb_vdd != NULL) {
rc = regulator_set_load(regulator_uwb_vdd, 400000);
if (rc) {
SR100_ERR_MSG("regulator_uwb_vdd set_load failed, rc=%d\n", rc);
goto regulator_err;
}
rc = regulator_enable(regulator_uwb_vdd);
if (rc) {
SR100_ERR_MSG("regulator_uwb_vdd enable failed, rc=%d\n", rc);
goto regulator_err;
}
}
if(regulator_uwb_vdd_rf != NULL) {
rc = regulator_set_load(regulator_uwb_vdd_rf, 400000);
if (rc) {
SR100_ERR_MSG("regulator_uwb_vdd_rf set_load failed, rc=%d\n", rc);
goto regulator_err;
}
rc = regulator_enable(regulator_uwb_vdd_rf);
if (rc) {
SR100_ERR_MSG("regulator_uwb_vdd_rf enable failed, rc=%d\n", rc);
goto regulator_err;
}
}
if(regulator_uwb_vdd_se_i2c_pullup != NULL) {
rc = regulator_enable(regulator_uwb_vdd_se_i2c_pullup);
if (rc) {
SR100_ERR_MSG("regulator_uwb_vdd_se_i2c_pullup enable failed, rc=%d\n", rc);
goto regulator_err;
}
}
} else {
if(regulator_uwb_vdd_rf != NULL) {
rc = regulator_disable(regulator_uwb_vdd_rf);
if (rc) {
SR100_ERR_MSG("regulator_uwb_vdd_rf disable failed, rc=%d\n", rc);
goto regulator_err;
}
}
if(regulator_uwb_vdd != NULL) {
rc = regulator_disable(regulator_uwb_vdd);
if (rc) {
SR100_ERR_MSG("regulator_uwb_vdd disable failed, rc=%d\n", rc);
goto regulator_err;
}
}
if(regulator_uwb_vdd_io != NULL) {
rc = regulator_disable(regulator_uwb_vdd_io);
if (rc) {
SR100_ERR_MSG("regulator_uwb_vdd_io disable failed, rc=%d\n", rc);
goto regulator_err;
}
}
if(regulator_uwb_vdd_se_i2c_pullup != NULL) {
rc = regulator_disable(regulator_uwb_vdd_se_i2c_pullup);
if (rc) {
SR100_ERR_MSG("regulator_uwb_vdd_se_i2c_pullup disable failed, rc=%d\n", rc);
goto regulator_err;
}
}
regulator_err:
if(regulator_uwb_vdd_rf != NULL) regulator_put(regulator_uwb_vdd_rf);
if(regulator_uwb_vdd != NULL) regulator_put(regulator_uwb_vdd);
if(regulator_uwb_vdd_io != NULL) regulator_put(regulator_uwb_vdd_io);
if(regulator_uwb_vdd_se_i2c_pullup != NULL) regulator_put(regulator_uwb_vdd_se_i2c_pullup);
}
return rc;
}
/******************************************************************************
* Function : sr100_probe
*
* Description : To probe for SR100 SPI interface. If found initialize the SPI
* clock,bit rate & SPI mode. It will create the dev entry
* (SR100) for user space.
* Parameters : spi : struct spi_device *
*
* Returns : retval 0 if ok else -1 on error
****************************************************************************/
static int sr100_probe(struct spi_device* spi) {
int ret = -1;//,i;
struct sr100_spi_platform_data* platform_data = NULL;
struct sr100_spi_platform_data platform_data1;
struct sr100_dev* sr100_dev = NULL;
unsigned int irq_flags;
SR100_DBG_MSG("Entry : %s\n", __FUNCTION__);
SR100_DBG_MSG("%s chip select : %d , bus number = %d \n", __FUNCTION__,
spi->chip_select, spi->master->bus_num);
ret = sr100_parse_dt(&spi->dev, &platform_data1);
if (ret) {
SR100_ERR_MSG("%s - Failed to parse DT\n", __func__);
goto err_exit;
}
platform_data = &platform_data1;
sr100_dev = kzalloc(sizeof(*sr100_dev), GFP_KERNEL);
if (sr100_dev == NULL) {
SR100_ERR_MSG("failed to allocate memory for module data\n");
ret = -ENOMEM;
goto err_exit;
}
ret = sr100_hw_setup(platform_data);
if (ret < 0) {
SR100_ERR_MSG("Failed to sr100_enable_SR100_IRQ_ENABLE\n");
goto err_exit0;
}
spi->bits_per_word = 8;
spi->mode = SPI_MODE_0;
spi->max_speed_hz = SR100_SPI_CLOCK;
ret = spi_setup(spi);
if (ret < 0) {
SR100_ERR_MSG("failed to do spi_setup()\n");
goto err_exit0;
}
sr100_dev->spi = spi;
sr100_dev->sr100_device.minor = MISC_DYNAMIC_MINOR;
sr100_dev->sr100_device.name = "sr100";
sr100_dev->sr100_device.fops = &sr100_dev_fops;
sr100_dev->sr100_device.parent = &spi->dev;
sr100_dev->irq_gpio = platform_data->irq_gpio;
sr100_dev->ce_gpio = platform_data->ce_gpio;
sr100_dev->rtc_sync_gpio = platform_data->rtc_sync_gpio;
sr100_dev->spi_handshake_gpio = platform_data->spi_handshake_gpio;
sr100_dev->ant_connection_status_gpio = platform_data->ant_connection_status_gpio;
sr100_dev->uwb_vdd_io = platform_data->uwb_vdd_io;
sr100_dev->uwb_vdd = platform_data->uwb_vdd;
sr100_dev->uwb_vdd_rf = platform_data->uwb_vdd_rf;
sr100_dev->uwb_vdd_se_i2c_pullup = platform_data->uwb_vdd_se_i2c_pullup;
sr100_dev->tx_buffer = kzalloc(SR100_TXBUF_SIZE, GFP_KERNEL);
sr100_dev->rx_buffer = kzalloc(SR100_RXBUF_SIZE, GFP_KERNEL);
if (sr100_dev->tx_buffer == NULL) {
ret = -ENOMEM;
goto err_exit1;
}
if (sr100_dev->rx_buffer == NULL) {
ret = -ENOMEM;
goto err_exit1;
}
dev_set_drvdata(&spi->dev, sr100_dev);
/* init mutex and queues */
init_waitqueue_head(&sr100_dev->read_wq);
mutex_init(&sr100_dev->sr100_access_lock);
spin_lock_init(&sr100_dev->irq_enabled_lock);
ret = misc_register(&sr100_dev->sr100_device);
if (ret < 0) {
SR100_ERR_MSG("misc_register failed! %d\n", ret);
goto err_exit2;
}
ret = sr100_regulator_onoff(&spi->dev, sr100_dev, true);
if (ret < 0) {
SR100_ERR_MSG("regulator_on fail err:%d\n", ret);
}
usleep_range(1000, 1100);
sr100_dev->spi->irq = gpio_to_irq(platform_data->irq_gpio);
SR100_DBG_MSG("sr100_dev->spi->irq = 0x%x %d\n",
sr100_dev->spi->irq,sr100_dev->spi->irq);
if (sr100_dev->spi->irq < 0) {
SR100_ERR_MSG("gpio_to_irq request failed gpio = 0x%x\n",
platform_data->irq_gpio);
goto err_exit3;
}
/* request irq. the irq is set whenever the chip has data available
* for reading. it is cleared when all data has been read.
*/
//irq_flags = IRQF_TRIGGER_RISING;
irq_flags = IRQ_TYPE_LEVEL_HIGH;
sr100_dev->irq_enabled = true;
sr100_dev->irq_received = false;
ret = request_irq(sr100_dev->spi->irq, sr100_dev_irq_handler, irq_flags,
sr100_dev->sr100_device.name, sr100_dev);
if (ret) {
SR100_ERR_MSG("request_irq failed\n");
goto err_exit3;
}
sr100_disable_irq(sr100_dev);
if((int)sr100_dev->rtc_sync_gpio > 0) {
SR100_DBG_MSG("gpio_set_value rtc_sync 0\n");
gpio_set_value(sr100_dev->rtc_sync_gpio, 0);
}
SR100_DBG_MSG("gpio_set_value spi_handshake_gpio 0\n");
gpio_set_value(sr100_dev->spi_handshake_gpio, 0);
SR100_DBG_MSG("Exit : %s\n", __FUNCTION__);
return ret;
err_exit3:
if (sr100_dev != NULL) {
misc_deregister(&sr100_dev->sr100_device);
}
err_exit2:
if (sr100_dev != NULL) {
mutex_destroy(&sr100_dev->sr100_access_lock);
}
err_exit1:
if (sr100_dev != NULL) {
if (sr100_dev->tx_buffer) {
kfree(sr100_dev->tx_buffer);
}
if (sr100_dev->rx_buffer) {
kfree(sr100_dev->rx_buffer);
}
}
err_exit0:
if (sr100_dev != NULL) kfree(sr100_dev);
err_exit:
SR100_ERR_MSG("ERROR: Exit : %s ret %d\n", __FUNCTION__, ret);
return ret;
}
/******************************************************************************
* Function : sr100_remove
*
* Description : Will get called when the device is removed to release the
* resources.
*
* Parameters : spi : struct spi_device *
*
* Returns : retval 0 if ok else -1 on error
****************************************************************************/
static int sr100_remove(struct spi_device* spi) {
struct sr100_dev* sr100_dev = sr100_get_data(spi);
SR100_DBG_MSG("Entry : %s\n", __FUNCTION__);
sr100_regulator_onoff(&spi->dev, sr100_dev, false);
gpio_free(sr100_dev->ce_gpio);
mutex_destroy(&sr100_dev->sr100_access_lock);
free_irq(sr100_dev->spi->irq, sr100_dev);
gpio_free(sr100_dev->irq_gpio);
gpio_free(sr100_dev->spi_handshake_gpio);
if((int)sr100_dev->rtc_sync_gpio > 0) {
gpio_free(sr100_dev->rtc_sync_gpio);
}
if((int)sr100_dev->ant_connection_status_gpio > 0) {
gpio_free(sr100_dev->ant_connection_status_gpio);
}
misc_deregister(&sr100_dev->sr100_device);
if (sr100_dev->tx_buffer != NULL) kfree(sr100_dev->tx_buffer);
if (sr100_dev->rx_buffer != NULL) kfree(sr100_dev->rx_buffer);
if (sr100_dev != NULL) kfree(sr100_dev);
SR100_DBG_MSG("Exit : %s\n", __FUNCTION__);
return 0;
}
static struct of_device_id sr100_dt_match[] = {{
.compatible = "nxp,sr100",
},
{}};
static struct spi_driver sr100_driver = {
.driver =
{
.name = "sr100",
.bus = &spi_bus_type,
.owner = THIS_MODULE,
.of_match_table = sr100_dt_match,
},
.probe = sr100_probe,
.remove = (sr100_remove),
};
/******************************************************************************
* Function : sr100_dev_init
*
* Description : Module init interface
*
* Parameters :void
*
* Returns : returns handle
****************************************************************************/
static int __init sr100_dev_init(void) {
int ret = -1;
uwb_logger_init();
debug_level = SR100_FULL_DEBUG;
SR100_DBG_MSG("Entry : %s\n", __FUNCTION__);
ret = spi_register_driver(&sr100_driver);
SR100_DBG_MSG("Exit : %s ret =%d\n", __FUNCTION__, ret);
return ret;
}
module_init(sr100_dev_init);
/******************************************************************************
* Function : sr100_dev_exit
*
* Description : Module Exit interface
*
* Parameters :void
*
* Returns : returns void
****************************************************************************/
static void __exit sr100_dev_exit(void) {
SR100_DBG_MSG("Entry : %s\n", __FUNCTION__);
spi_unregister_driver(&sr100_driver);
SR100_DBG_MSG("Exit : %s\n", __FUNCTION__);
}
module_exit(sr100_dev_exit);
MODULE_AUTHOR("Manjunatha Venkatesh");
MODULE_DESCRIPTION("NXP SR100 SPI driver");
MODULE_LICENSE("GPL");
/** @} */
|