summaryrefslogtreecommitdiff
path: root/drivers/bif/qpnp-bsi.c
blob: d61eb39dc5bf29e83b927cdef0efa5a64ac6dd92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
/* Copyright (c) 2013-2015, The Linux Foundation. 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 and
 * only 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.
 */

#define pr_fmt(fmt) "%s: " fmt, __func__

#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/regmap.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/spmi.h>
#include <linux/platform_device.h>
#include <linux/workqueue.h>
#include <linux/bif/driver.h>
#include <linux/qpnp/qpnp-adc.h>

enum qpnp_bsi_irq {
	QPNP_BSI_IRQ_ERR,
	QPNP_BSI_IRQ_RX,
	QPNP_BSI_IRQ_TX,
	QPNP_BSI_IRQ_COUNT,
};

enum qpnp_bsi_com_mode {
	QPNP_BSI_COM_MODE_IRQ,
	QPNP_BSI_COM_MODE_POLL,
};

struct qpnp_bsi_chip {
	struct bif_ctrl_desc	bdesc;
	struct platform_device	*pdev;
	struct regmap		*regmap;
	struct bif_ctrl_dev	*bdev;
	struct work_struct	slave_irq_work;
	u16			base_addr;
	u16			batt_id_stat_addr;
	int			r_pullup_ohm;
	int			vid_ref_uV;
	int			tau_index;
	int			tau_sampling_mask;
	enum bif_bus_state	state;
	enum qpnp_bsi_com_mode	com_mode;
	int			irq[QPNP_BSI_IRQ_COUNT];
	atomic_t		irq_flag[QPNP_BSI_IRQ_COUNT];
	int			batt_present_irq;
	enum qpnp_vadc_channels	batt_id_adc_channel;
	struct qpnp_vadc_chip	*vadc_dev;
};

#define QPNP_BSI_DRIVER_NAME	"qcom,qpnp-bsi"

enum qpnp_bsi_registers {
	QPNP_BSI_REG_TYPE		= 0x04,
	QPNP_BSI_REG_SUBTYPE		= 0x05,
	QPNP_BSI_REG_STATUS		= 0x08,
	QPNP_BSI_REG_ENABLE		= 0x46,
	QPNP_BSI_REG_CLEAR_ERROR	= 0x4F,
	QPNP_BSI_REG_FORCE_BCL_LOW	= 0x51,
	QPNP_BSI_REG_TAU_CONFIG		= 0x52,
	QPNP_BSI_REG_MODE		= 0x53,
	QPNP_BSI_REG_RX_TX_ENABLE	= 0x54,
	QPNP_BSI_REG_TX_DATA_LOW	= 0x5A,
	QPNP_BSI_REG_TX_DATA_HIGH	= 0x5B,
	QPNP_BSI_REG_TX_CTRL		= 0x5D,
	QPNP_BSI_REG_RX_DATA_LOW	= 0x60,
	QPNP_BSI_REG_RX_DATA_HIGH	= 0x61,
	QPNP_BSI_REG_RX_SOURCE		= 0x62,
	QPNP_BSI_REG_BSI_ERROR		= 0x70,
};

#define QPNP_BSI_TYPE			0x02
#define QPNP_BSI_SUBTYPE		0x10

#define QPNP_BSI_STATUS_ERROR		0x10
#define QPNP_BSI_STATUS_TX_BUSY		0x08
#define QPNP_BSI_STATUS_RX_BUSY		0x04
#define QPNP_BSI_STATUS_TX_GO_BUSY	0x02
#define QPNP_BSI_STATUS_RX_DATA_READY	0x01

#define QPNP_BSI_ENABLE_MASK		0x80
#define QPNP_BSI_ENABLE			0x80
#define QPNP_BSI_DISABLE		0x00

#define QPNP_BSI_TAU_CONFIG_SAMPLE_MASK	0x10
#define QPNP_BSI_TAU_CONFIG_SAMPLE_8X	0x10
#define QPNP_BSI_TAU_CONFIG_SAMPLE_4X	0x00
#define QPNP_BSI_TAU_CONFIG_SPEED_MASK	0x07

#define QPNP_BSI_MODE_TX_PULSE_MASK	0x10
#define QPNP_BSI_MODE_TX_PULSE_INT	0x10
#define QPNP_BSI_MODE_TX_PULSE_DATA	0x00
#define QPNP_BSI_MODE_RX_PULSE_MASK	0x08
#define QPNP_BSI_MODE_RX_PULSE_INT	0x08
#define QPNP_BSI_MODE_RX_PULSE_DATA	0x00
#define QPNP_BSI_MODE_TX_PULSE_T_MASK	0x04
#define QPNP_BSI_MODE_TX_PULSE_T_WAKE	0x04
#define QPNP_BSI_MODE_TX_PULSE_T_1_TAU	0x00
#define QPNP_BSI_MODE_RX_FORMAT_MASK	0x02
#define QPNP_BSI_MODE_RX_FORMAT_17_BIT	0x02
#define QPNP_BSI_MODE_RX_FORMAT_11_BIT	0x00
#define QPNP_BSI_MODE_TX_FORMAT_MASK	0x01
#define QPNP_BSI_MODE_TX_FORMAT_17_BIT	0x01
#define QPNP_BSI_MODE_TX_FORMAT_11_BIT	0x00

#define QPNP_BSI_TX_ENABLE_MASK		0x80
#define QPNP_BSI_TX_ENABLE		0x80
#define QPNP_BSI_TX_DISABLE		0x00
#define QPNP_BSI_RX_ENABLE_MASK		0x40
#define QPNP_BSI_RX_ENABLE		0x40
#define QPNP_BSI_RX_DISABLE		0x00

#define QPNP_BSI_TX_DATA_HIGH_MASK	0x07

#define QPNP_BSI_TX_CTRL_GO		0x01

#define QPNP_BSI_RX_DATA_HIGH_MASK	0x07

#define QPNP_BSI_RX_SRC_LOOPBACK_FLAG	0x10

#define QPNP_BSI_BSI_ERROR_CLEAR	0x80

#define QPNP_SMBB_BAT_IF_BATT_PRES_MASK	0x80
#define QPNP_SMBB_BAT_IF_BATT_ID_MASK	0x01

#define QPNP_BSI_NUM_CLOCK_PERIODS	8

struct qpnp_bsi_tau {
	int period_4x_ns[QPNP_BSI_NUM_CLOCK_PERIODS];
	int period_8x_ns[QPNP_BSI_NUM_CLOCK_PERIODS];
	int period_4x_us[QPNP_BSI_NUM_CLOCK_PERIODS];
	int period_8x_us[QPNP_BSI_NUM_CLOCK_PERIODS];
};

/* Tau BIF clock periods in ns supported by BSI for either 4x or 8x sampling. */
static const struct qpnp_bsi_tau qpnp_bsi_tau_period = {
	.period_4x_ns = {
		150420, 122080, 61040, 31670, 15830, 7920, 3960, 2080
	},
	.period_8x_ns = {
		150420, 122080, 63330, 31670, 15830, 7920, 4170, 2080
	},
	.period_4x_us = {
		151, 122, 61, 32, 16, 8, 4, 2
	},
	.period_8x_us = {
		151, 122, 64, 32, 16, 8, 4, 2
	},

};
#define QPNP_BSI_MIN_CLOCK_SPEED_NS	2080
#define QPNP_BSI_MAX_CLOCK_SPEED_NS	150420

#define QPNP_BSI_MIN_PULLUP_OHM		1000
#define QPNP_BSI_MAX_PULLUP_OHM		500000
#define QPNP_BSI_DEFAULT_PULLUP_OHM	100000
#define QPNP_BSI_MIN_VID_REF_UV		500000
#define QPNP_BSI_MAX_VID_REF_UV		5000000
#define QPNP_BSI_DEFAULT_VID_REF_UV	1800000

/* These have units of tau_bif. */
#define QPNP_BSI_MAX_TRANSMIT_CYCLES	46
#define QPNP_BSI_MIN_RECEIVE_CYCLES	24
#define QPNP_BSI_MAX_BUS_QUERY_CYCLES	17

/*
 * Maximum time in microseconds for a slave to transition from suspend to active
 * state.
 */
#define QPNP_BSI_MAX_SLAVE_ACTIVIATION_DELAY_US	50

/*
 * Maximum time in milliseconds for a slave to transition from power down to
 * active state.
 */
#define QPNP_BSI_MAX_SLAVE_POWER_UP_DELAY_MS	10

#define QPNP_BSI_POWER_UP_LOW_DELAY_US		240

/*
 * Latencies that are used when determining if polling or interrupts should be
 * used for a given transaction.
 */
#define QPNP_BSI_MAX_IRQ_LATENCY_US		170
#define QPNP_BSI_MAX_BSI_DATA_READ_LATENCY_US	16

static int qpnp_bsi_set_bus_state(struct bif_ctrl_dev *bdev, int state);

static inline int qpnp_bsi_read(struct qpnp_bsi_chip *chip, u16 addr, u8 *buf,
				int len)
{
	int rc;

	rc = regmap_bulk_read(chip->regmap, chip->base_addr + addr, buf, len);
	if (rc)
		dev_err(&chip->pdev->dev,
			"%s: regmap_bulk_readl failed. sid=%d, addr=%04X, len=%d, rc=%d\n",
			__func__,
			to_spmi_device(chip->pdev->dev.parent)->usid,
			chip->base_addr + addr,
			len, rc);

	return rc;
}

static inline int qpnp_bsi_write(struct qpnp_bsi_chip *chip, u16 addr, u8 *buf,
				int len)
{
	int rc;

	rc = regmap_bulk_write(chip->regmap, chip->base_addr + addr, buf, len);

	if (rc)
		dev_err(&chip->pdev->dev,
			"%s: regmap_bulk_write failed. sid=%d, addr=%04X, len=%d, rc=%d\n",
			__func__,
			to_spmi_device(chip->pdev->dev.parent)->usid,
			chip->base_addr + addr,
			len, rc);

	return rc;
}

enum qpnp_bsi_rx_tx_state {
	QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF,
	QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA,
	QPNP_BSI_RX_TX_STATE_RX_OFF_TX_INT,
	QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA,
	QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA,
	QPNP_BSI_RX_TX_STATE_RX_INT_TX_OFF,
};

static int qpnp_bsi_rx_tx_config(struct qpnp_bsi_chip *chip,
				    enum qpnp_bsi_rx_tx_state state)
{
	u8 buf[2] = {0, 0};
	int rc;

	buf[0] = QPNP_BSI_MODE_TX_FORMAT_11_BIT
		 | QPNP_BSI_MODE_RX_FORMAT_11_BIT;

	switch (state) {
	case QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF:
		buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
			QPNP_BSI_MODE_RX_PULSE_DATA;
		buf[1] = QPNP_BSI_TX_DISABLE | QPNP_BSI_RX_DISABLE;
		break;
	case QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA:
		buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
			QPNP_BSI_MODE_RX_PULSE_DATA;
		buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_DISABLE;
		break;
	case QPNP_BSI_RX_TX_STATE_RX_OFF_TX_INT:
		buf[0] |= QPNP_BSI_MODE_TX_PULSE_INT |
			QPNP_BSI_MODE_RX_PULSE_DATA;
		buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_DISABLE;
		break;
	case QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA:
		buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
			QPNP_BSI_MODE_RX_PULSE_INT;
		buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_ENABLE;
		break;
	case QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA:
		buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
			QPNP_BSI_MODE_RX_PULSE_DATA;
		buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_ENABLE;
		break;
	case QPNP_BSI_RX_TX_STATE_RX_INT_TX_OFF:
		buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
			QPNP_BSI_MODE_RX_PULSE_INT;
		buf[1] = QPNP_BSI_TX_DISABLE | QPNP_BSI_RX_DISABLE;
		break;
	default:
		dev_err(&chip->pdev->dev, "%s: invalid state=%d\n",
			__func__, state);
		return -EINVAL;
	}

	rc = qpnp_bsi_write(chip, QPNP_BSI_REG_MODE, buf, 2);
	if (rc)
		dev_err(&chip->pdev->dev,
			"%s: qpnp_bsi_write() failed, rc=%d\n",
			__func__, rc);

	return rc;
}

static void qpnp_bsi_slave_irq_work(struct work_struct *work)
{
	struct qpnp_bsi_chip *chip
		= container_of(work, struct qpnp_bsi_chip, slave_irq_work);
	int rc;

	rc = bif_ctrl_notify_slave_irq(chip->bdev);
	if (rc)
		pr_err("Could not notify BIF core about slave interrupt, rc=%d\n",
			rc);
}

static irqreturn_t qpnp_bsi_isr(int irq, void *data)
{
	struct qpnp_bsi_chip *chip = data;
	bool found = false;
	int i;

	for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++) {
		if (irq == chip->irq[i]) {
			found = true;
			atomic_cmpxchg(&chip->irq_flag[i], 0, 1);

			/* Check if this is a slave interrupt. */
			if (i == QPNP_BSI_IRQ_RX
			    && chip->state == BIF_BUS_STATE_INTERRUPT) {
				/* Slave IRQ makes the bus active. */
				qpnp_bsi_rx_tx_config(chip,
					QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
				chip->state = BIF_BUS_STATE_ACTIVE;
				schedule_work(&chip->slave_irq_work);
			}
		}
	}

	if (!found)
		pr_err("Unknown interrupt: %d\n", irq);

	return IRQ_HANDLED;
}

static irqreturn_t qpnp_bsi_batt_present_isr(int irq, void *data)
{
	struct qpnp_bsi_chip *chip = data;
	int rc;

	if (!chip->bdev)
		return IRQ_HANDLED;

	rc = bif_ctrl_notify_battery_changed(chip->bdev);
	if (rc)
		pr_err("Could not notify about battery state change, rc=%d\n",
			rc);

	return IRQ_HANDLED;
}

static void qpnp_bsi_set_com_mode(struct qpnp_bsi_chip *chip,
				enum qpnp_bsi_com_mode mode)
{
	int i;

	if (chip->com_mode == mode)
		return;

	if (mode == QPNP_BSI_COM_MODE_IRQ)
		for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++)
			enable_irq(chip->irq[i]);
	else
		for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++)
			disable_irq(chip->irq[i]);

	chip->com_mode = mode;
}

static inline bool qpnp_bsi_check_irq(struct qpnp_bsi_chip *chip, int irq)
{
	return atomic_cmpxchg(&chip->irq_flag[irq], 1, 0);
}

static void qpnp_bsi_clear_irq_flags(struct qpnp_bsi_chip *chip)
{
	int i;

	for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++)
		atomic_set(&chip->irq_flag[i], 0);
}

static inline int qpnp_bsi_get_tau_ns(struct qpnp_bsi_chip *chip)
{
	if (chip->tau_sampling_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X)
		return qpnp_bsi_tau_period.period_4x_ns[chip->tau_index];
	else
		return qpnp_bsi_tau_period.period_8x_ns[chip->tau_index];
}

static inline int qpnp_bsi_get_tau_us(struct qpnp_bsi_chip *chip)
{
	if (chip->tau_sampling_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X)
		return qpnp_bsi_tau_period.period_4x_us[chip->tau_index];
	else
		return qpnp_bsi_tau_period.period_8x_us[chip->tau_index];
}

/* Checks if BSI is in an error state and clears the error if it is. */
static int qpnp_bsi_clear_bsi_error(struct qpnp_bsi_chip *chip)
{
	int rc, delay_us;
	u8 reg;

	rc = qpnp_bsi_read(chip, QPNP_BSI_REG_BSI_ERROR, &reg, 1);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: qpnp_bsi_read() failed, rc=%d\n",
			__func__, rc);
		return rc;
	}

	if (reg > 0) {
		/*
		 * Delay before clearing the BSI error in case a transaction is
		 * still in flight.
		 */
		delay_us = QPNP_BSI_MAX_TRANSMIT_CYCLES
				* qpnp_bsi_get_tau_us(chip);
		udelay(delay_us);

		pr_info("PMIC BSI module in error state, error=%d\n", reg);

		reg = QPNP_BSI_BSI_ERROR_CLEAR;
		rc = qpnp_bsi_write(chip, QPNP_BSI_REG_CLEAR_ERROR, &reg, 1);
		if (rc)
			dev_err(&chip->pdev->dev,
				"%s: qpnp_bsi_write() failed, rc=%d\n",
				__func__, rc);
	}

	return rc;
}

static int qpnp_bsi_get_bsi_error(struct qpnp_bsi_chip *chip)
{
	int rc;
	u8 reg;

	rc = qpnp_bsi_read(chip, QPNP_BSI_REG_BSI_ERROR, &reg, 1);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: qpnp_bsi_read() failed, rc=%d\n",
			__func__, rc);
		return rc;
	}

	return reg;
}

static int qpnp_bsi_wait_for_tx(struct qpnp_bsi_chip *chip, int timeout)
{
	int rc = 0;

	/* Wait for TX or ERR IRQ. */
	while (timeout > 0) {
		if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_ERR)) {
			dev_err(&chip->pdev->dev,
				"%s: transaction error occurred, BSI error=%d\n",
				__func__, qpnp_bsi_get_bsi_error(chip));
			return -EIO;
		}

		if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_TX))
			break;

		udelay(1);
		timeout--;
	}

	if (timeout == 0) {
		rc = -ETIMEDOUT;
		dev_err(&chip->pdev->dev,
			"%s: transaction timed out, no interrupts received, rc=%d\n",
			__func__, rc);
		return rc;
	}

	return rc;
}

static int qpnp_bsi_issue_transaction(struct qpnp_bsi_chip *chip,
		int transaction, u8 data)
{
	int rc;
	u8 buf[4];

	/* MIPI_BIF_DATA_TX_0 = BIF word bits 7 to 0 */
	buf[0] = data;
	/* MIPI_BIF_DATA_TX_1 = BIF word BCF, bits 9 to 8 */
	buf[1] = transaction & QPNP_BSI_TX_DATA_HIGH_MASK;
	/* MIPI_BIF_DATA_TX_2 ignored */
	buf[2] = 0x00;
	/* MIPI_BIF_TX_CTL bit 0 written to start the transaction. */
	buf[3] = QPNP_BSI_TX_CTRL_GO;

	/* Write the TX_DATA bytes and initiate the transaction. */
	rc = qpnp_bsi_write(chip, QPNP_BSI_REG_TX_DATA_LOW, buf, 4);
	if (rc)
		dev_err(&chip->pdev->dev,
			"%s: qpnp_bsi_write() failed, rc=%d\n",
			__func__, rc);
	return rc;
}

static int qpnp_bsi_issue_transaction_wait_for_tx(struct qpnp_bsi_chip *chip,
		int transaction, u8 data)
{
	int rc, timeout;

	rc = qpnp_bsi_issue_transaction(chip, transaction, data);
	if (rc)
		return rc;

	timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip)
			+ QPNP_BSI_MAX_IRQ_LATENCY_US;

	rc = qpnp_bsi_wait_for_tx(chip, timeout);

	return rc;
}

static int qpnp_bsi_wait_for_rx(struct qpnp_bsi_chip *chip, int timeout)
{
	int rc = 0;

	/* Wait for RX IRQ to indicate that data is ready to read. */
	while (timeout > 0) {
		if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_ERR)) {
			dev_err(&chip->pdev->dev,
				"%s: transaction error occurred, BSI error=%d\n",
				__func__, qpnp_bsi_get_bsi_error(chip));
			return -EIO;
		}

		if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_RX))
			break;

		udelay(1);
		timeout--;
	}

	if (timeout == 0)
		rc = -ETIMEDOUT;

	return rc;
}

static int qpnp_bsi_bus_transaction(struct bif_ctrl_dev *bdev, int transaction,
				u8 data)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
	int rc;

	qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);

	rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: failed to set bus state, rc=%d\n",
			__func__, rc);
		return rc;
	}

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA);
	if (rc)
		return rc;

	rc = qpnp_bsi_clear_bsi_error(chip);
	if (rc)
		return rc;

	qpnp_bsi_clear_irq_flags(chip);

	rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, transaction, data);
	if (rc)
		return rc;

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);

	return rc;
}

static int qpnp_bsi_bus_transaction_query(struct bif_ctrl_dev *bdev,
				int transaction, u8 data, bool *query_response)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
	int rc, timeout;

	qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);

	rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: failed to set bus state, rc=%d\n",
			__func__, rc);
		return rc;
	}

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA);
	if (rc)
		return rc;

	rc = qpnp_bsi_clear_bsi_error(chip);
	if (rc)
		return rc;

	qpnp_bsi_clear_irq_flags(chip);

	rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, transaction, data);
	if (rc)
		return rc;

	timeout = QPNP_BSI_MAX_BUS_QUERY_CYCLES * qpnp_bsi_get_tau_us(chip)
				+ QPNP_BSI_MAX_IRQ_LATENCY_US;

	rc = qpnp_bsi_wait_for_rx(chip, timeout);
	if (rc == 0) {
		*query_response = true;
	} else if (rc == -ETIMEDOUT) {
		*query_response = false;
		rc = 0;
	}

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);

	return rc;
}

static int qpnp_bsi_bus_transaction_read(struct bif_ctrl_dev *bdev,
				int transaction, u8 data, int *response)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
	int rc, timeout;
	u8 buf[3];

	qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);

	rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: failed to set bus state, rc=%d\n",
			__func__, rc);
		return rc;
	}

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA);
	if (rc)
		return rc;

	rc = qpnp_bsi_clear_bsi_error(chip);
	if (rc)
		return rc;

	qpnp_bsi_clear_irq_flags(chip);

	rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, transaction, data);
	if (rc)
		return rc;

	timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip)
				+ QPNP_BSI_MAX_IRQ_LATENCY_US;

	rc = qpnp_bsi_wait_for_rx(chip, timeout);
	if (rc) {
		if (rc == -ETIMEDOUT) {
			/*
			 * No error message is printed in this case in order
			 * to provide silent operation when checking if a slave
			 * is selected using the transaction query bus command.
			 */
			dev_dbg(&chip->pdev->dev,
				"%s: transaction timed out, no interrupts received, rc=%d\n",
					__func__, rc);
		}
		return rc;
	}

	/* Read the RX_DATA bytes. */
	rc = qpnp_bsi_read(chip, QPNP_BSI_REG_RX_DATA_LOW, buf, 3);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: qpnp_bsi_read() failed, rc=%d\n",
			__func__, rc);
		return rc;
	}

	if (buf[2] & QPNP_BSI_RX_SRC_LOOPBACK_FLAG) {
		rc = -EIO;
		dev_err(&chip->pdev->dev,
			"%s: unexpected loopback data read, rc=%d\n",
			__func__, rc);
		return rc;
	}

	*response = ((int)(buf[1] & QPNP_BSI_RX_DATA_HIGH_MASK) << 8) | buf[0];

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);

	return 0;
}

/*
 * Wait for RX_FLOW_STATUS to be set to 1 which indicates that another BIF word
 * can be read from PMIC registers.
 */
static int qpnp_bsi_wait_for_rx_data(struct qpnp_bsi_chip *chip)
{
	int rc = 0;
	int timeout;
	u8 reg;

	timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip);

	/* Wait for RX_FLOW_STATUS == 1 or ERR_FLAG == 1. */
	while (timeout > 0) {
		rc = qpnp_bsi_read(chip, QPNP_BSI_REG_STATUS, &reg, 1);
		if (rc) {
			dev_err(&chip->pdev->dev,
				"%s: qpnp_bsi_write() failed, rc=%d\n",
				__func__, rc);
			return rc;
		}

		if (reg & QPNP_BSI_STATUS_ERROR) {
			dev_err(&chip->pdev->dev,
				"%s: transaction error occurred, BSI error=%d\n",
				__func__, qpnp_bsi_get_bsi_error(chip));
			return -EIO;
		}

		if (reg & QPNP_BSI_STATUS_RX_DATA_READY) {
			/* BSI RX has data word latched. */
			return 0;
		}

		udelay(1);
		timeout--;
	}

	rc = -ETIMEDOUT;
	dev_err(&chip->pdev->dev,
		"%s: transaction timed out, RX_FLOW_STATUS never set to 1, rc=%d\n",
		__func__, rc);

	return rc;
}

/*
 * Wait for TX_GO_STATUS to be set to 0 which indicates that another BIF word
 * can be enqueued.
 */
static int qpnp_bsi_wait_for_tx_go(struct qpnp_bsi_chip *chip)
{
	int rc = 0;
	int timeout;
	u8 reg;

	timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip);

	/* Wait for TX_GO_STATUS == 0 or ERR_FLAG == 1. */
	while (timeout > 0) {
		rc = qpnp_bsi_read(chip, QPNP_BSI_REG_STATUS, &reg, 1);
		if (rc) {
			dev_err(&chip->pdev->dev,
				"%s: qpnp_bsi_write() failed, rc=%d\n",
				__func__, rc);
			return rc;
		}

		if (reg & QPNP_BSI_STATUS_ERROR) {
			dev_err(&chip->pdev->dev,
				"%s: transaction error occurred, BSI error=%d\n",
				__func__, qpnp_bsi_get_bsi_error(chip));
			return -EIO;
		}

		if (!(reg & QPNP_BSI_STATUS_TX_GO_BUSY)) {
			/* BSI TX is ready to accept the next word. */
			return 0;
		}

		udelay(1);
		timeout--;
	}

	rc = -ETIMEDOUT;
	dev_err(&chip->pdev->dev,
		"%s: transaction timed out, TX_GO_STATUS never set to 0, rc=%d\n",
		__func__, rc);

	return rc;
}

/*
 * Wait for TX_BUSY to be set to 0 which indicates that the TX data has been
 * successfully transmitted.
 */
static int qpnp_bsi_wait_for_tx_idle(struct qpnp_bsi_chip *chip)
{
	int rc = 0;
	int timeout;
	u8 reg;

	timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip);

	/* Wait for TX_BUSY == 0 or ERR_FLAG == 1. */
	while (timeout > 0) {
		rc = qpnp_bsi_read(chip, QPNP_BSI_REG_STATUS, &reg, 1);
		if (rc) {
			dev_err(&chip->pdev->dev,
				"%s: qpnp_bsi_write() failed, rc=%d\n",
				__func__, rc);
			return rc;
		}

		if (reg & QPNP_BSI_STATUS_ERROR) {
			dev_err(&chip->pdev->dev,
				"%s: transaction error occurred, BSI error=%d\n",
				__func__, qpnp_bsi_get_bsi_error(chip));
			return -EIO;
		}

		if (!(reg & QPNP_BSI_STATUS_TX_BUSY)) {
			/* BSI TX is idle. */
			return 0;
		}

		udelay(1);
		timeout--;
	}

	rc = -ETIMEDOUT;
	dev_err(&chip->pdev->dev,
		"%s: transaction timed out, TX_BUSY never set to 0, rc=%d\n",
		__func__, rc);

	return rc;
}

/*
 * For burst read length greater than 1, send necessary RBL and RBE BIF bus
 * commands.
 */
static int qpnp_bsi_send_burst_length(struct qpnp_bsi_chip *chip, int burst_len)
{
	int rc = 0;

	/*
	 * Send burst read length bus commands according to the following:
	 *
	 * 1                     --> No RBE or RBL
	 * 2  - 15  = x          --> RBLx
	 * 16 - 255 = 16 * y + x --> RBEy and RBLx (RBL0 not sent)
	 * 256                   --> RBL0
	 */
	if (burst_len == 256) {
		rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_BC,
						BIF_CMD_RBL);
		if (rc)
			return rc;

		rc = qpnp_bsi_wait_for_tx_go(chip);
		if (rc)
			return rc;
	} else if (burst_len >= 16) {
		rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_BC,
					BIF_CMD_RBE + (burst_len / 16));
		if (rc)
			return rc;

		rc = qpnp_bsi_wait_for_tx_go(chip);
		if (rc)
			return rc;
	}

	if (burst_len % 16 && burst_len > 1) {
		rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_BC,
					BIF_CMD_RBL + (burst_len % 16));
		if (rc)
			return rc;

		rc = qpnp_bsi_wait_for_tx_go(chip);
		if (rc)
			return rc;
	}

	return rc;
}

/* Perform validation steps on received BIF data. */
static int qpnp_bsi_validate_rx_data(struct qpnp_bsi_chip *chip, int response,
					u8 rx2_data, bool last_word)
{
	int err = -EIO;

	if (rx2_data & QPNP_BSI_RX_SRC_LOOPBACK_FLAG) {
		dev_err(&chip->pdev->dev,
			"%s: unexpected loopback data read, rc=%d\n",
			__func__, err);
		return err;
	}

	if (!(response & BIF_SLAVE_RD_ACK)) {
		dev_err(&chip->pdev->dev,
			"%s: BIF register read error=0x%02X\n",
			__func__, response & BIF_SLAVE_RD_ERR);
		return err;
	}

	if (last_word && !(response & BIF_SLAVE_RD_EOT)) {
		dev_err(&chip->pdev->dev,
			"%s: BIF register read error, last RD packet has EOT=0\n",
			__func__);
		return err;
	} else if (!last_word && (response & BIF_SLAVE_RD_EOT)) {
		dev_err(&chip->pdev->dev,
			"%s: BIF register read error, RD packet other than last has EOT=1\n",
			__func__);
		return err;
	}

	return 0;
}

/* Performs all BIF transactions in order to utilize burst reads. */
static int qpnp_bsi_read_slave_registers(struct bif_ctrl_dev *bdev, u16 addr,
						u8 *data, int len)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
	int response = 0;
	unsigned long flags;
	int rc, rc2, i, burst_len;
	u8 buf[3];

	qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_POLL);

	rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: failed to set bus state, rc=%d\n",
			__func__, rc);
		return rc;
	}

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA);
	if (rc)
		return rc;

	rc = qpnp_bsi_clear_bsi_error(chip);
	if (rc)
		return rc;

	qpnp_bsi_clear_irq_flags(chip);

	while (len > 0) {
		burst_len = min(len, 256);

		rc = qpnp_bsi_send_burst_length(chip, burst_len);
		if (rc)
			return rc;

		rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_ERA, addr >> 8);
		if (rc)
			return rc;

		rc = qpnp_bsi_wait_for_tx_go(chip);
		if (rc)
			return rc;

		/* Perform burst read in atomic context. */
		local_irq_save(flags);

		rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_RRA,
						addr & 0xFF);
		if (rc)
			goto burst_err;

		for (i = 0; i < burst_len; i++) {
			rc = qpnp_bsi_wait_for_rx_data(chip);
			if (rc)
				goto burst_err;

			/* Read the RX_DATA bytes. */
			rc = qpnp_bsi_read(chip, QPNP_BSI_REG_RX_DATA_LOW, buf,
					   3);
			if (rc) {
				dev_err(&chip->pdev->dev,
					"%s: qpnp_bsi_read() failed, rc=%d\n",
					__func__, rc);
				goto burst_err;
			}

			response = ((buf[1] & QPNP_BSI_RX_DATA_HIGH_MASK) << 8)
					| buf[0];

			rc = qpnp_bsi_validate_rx_data(chip, response, buf[2],
					i == burst_len - 1);
			if (rc)
				goto burst_err;

			data[i] = buf[0];
		}
		local_irq_restore(flags);

		addr += burst_len;
		data += burst_len;
		len -= burst_len;
	}

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);

	return rc;

burst_err:
	local_irq_restore(flags);

	rc2 = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
	if (rc2 < 0)
		rc = rc2;

	return rc;
}

/* Performs all BIF transactions in order to utilize burst writes. */
static int qpnp_bsi_write_slave_registers(struct bif_ctrl_dev *bdev, u16 addr,
						const u8 *data, int len)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
	unsigned long flags;
	int rc, rc2, i;

	qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_POLL);

	rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: failed to set bus state, rc=%d\n",
			__func__, rc);
		return rc;
	}

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA);
	if (rc)
		return rc;

	rc = qpnp_bsi_clear_bsi_error(chip);
	if (rc)
		return rc;

	qpnp_bsi_clear_irq_flags(chip);

	rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_ERA, addr >> 8);
	if (rc)
		return rc;

	rc = qpnp_bsi_wait_for_tx_go(chip);
	if (rc)
		return rc;

	rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_WRA, addr & 0xFF);
	if (rc)
		return rc;

	rc = qpnp_bsi_wait_for_tx_go(chip);
	if (rc)
		return rc;

	/* Perform burst write in atomic context. */
	local_irq_save(flags);

	for (i = 0; i < len; i++) {
		rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_WD, data[i]);
		if (rc)
			goto burst_err;

		rc = qpnp_bsi_wait_for_tx_go(chip);
		if (rc)
			goto burst_err;
	}

	rc = qpnp_bsi_wait_for_tx_idle(chip);
	if (rc)
		goto burst_err;

	local_irq_restore(flags);

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);

	return rc;

burst_err:
	local_irq_restore(flags);

	rc2 = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
	if (rc2 < 0)
		rc = rc2;

	return rc;
}


static int qpnp_bsi_bus_set_interrupt_mode(struct bif_ctrl_dev *bdev)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
	int rc;

	qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);

	/*
	 * Temporarily change the bus to active state so that the EINT command
	 * can be issued.
	 */
	rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: failed to set bus state, rc=%d\n",
			__func__, rc);
		return rc;
	}

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA);
	if (rc)
		return rc;

	/*
	 * Set the bus state to interrupt mode so that an RX interrupt which
	 * occurs immediately after issuing the EINT command is handled
	 * properly.
	 */
	chip->state = BIF_BUS_STATE_INTERRUPT;

	rc = qpnp_bsi_clear_bsi_error(chip);
	if (rc)
		return rc;

	qpnp_bsi_clear_irq_flags(chip);

	/* Send EINT bus command. */
	rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, BIF_TRANS_BC,
							BIF_CMD_EINT);
	if (rc)
		return rc;

	rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_INT_TX_OFF);

	return rc;
}

static int qpnp_bsi_bus_set_active_mode(struct bif_ctrl_dev *bdev,
					int prev_state)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
	int rc;
	u8 buf[2];

	rc = qpnp_bsi_clear_bsi_error(chip);
	if (rc)
		return rc;

	buf[0] = QPNP_BSI_MODE_TX_PULSE_INT |
		QPNP_BSI_MODE_RX_PULSE_DATA;
	buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_DISABLE;

	if (prev_state == BIF_BUS_STATE_INTERRUPT)
		buf[0] |= QPNP_BSI_MODE_TX_PULSE_T_1_TAU;
	else
		buf[0] |= QPNP_BSI_MODE_TX_PULSE_T_WAKE;

	rc = qpnp_bsi_write(chip, QPNP_BSI_REG_MODE, buf, 2);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: qpnp_bsi_write() failed, rc=%d\n",
			__func__, rc);
		return rc;
	}

	buf[0] = QPNP_BSI_TX_CTRL_GO;
	/* Initiate BCL low pulse. */
	rc = qpnp_bsi_write(chip, QPNP_BSI_REG_TX_CTRL, buf, 1);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: qpnp_bsi_write() failed, rc=%d\n",
			__func__, rc);
		return rc;
	}

	switch (prev_state) {
	case BIF_BUS_STATE_INTERRUPT:
		udelay(qpnp_bsi_get_tau_us(chip) * 4);
		break;
	case BIF_BUS_STATE_STANDBY:
		udelay(qpnp_bsi_get_tau_us(chip)
			+ QPNP_BSI_MAX_SLAVE_ACTIVIATION_DELAY_US
			+ QPNP_BSI_POWER_UP_LOW_DELAY_US);
		break;
	case BIF_BUS_STATE_POWER_DOWN:
	case BIF_BUS_STATE_MASTER_DISABLED:
		msleep(QPNP_BSI_MAX_SLAVE_POWER_UP_DELAY_MS);
		break;
	}

	return rc;
}

static int qpnp_bsi_get_bus_state(struct bif_ctrl_dev *bdev)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);

	return chip->state;
}

static int qpnp_bsi_set_bus_state(struct bif_ctrl_dev *bdev, int state)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
	int rc = 0;
	u8 reg;

	if (state == chip->state)
		return 0;

	if (chip->state == BIF_BUS_STATE_MASTER_DISABLED) {
		/*
		 * Enable the BSI peripheral when transitioning from a disabled
		 * bus state to any of the active bus states so that BIF
		 * transactions can take place.
		 */
		reg = QPNP_BSI_ENABLE;
		rc = qpnp_bsi_write(chip, QPNP_BSI_REG_ENABLE, &reg, 1);
		if (rc) {
			dev_err(&chip->pdev->dev,
				"%s: qpnp_bsi_write() failed, rc=%d\n",
				__func__, rc);
			return rc;
		}
	}

	switch (state) {
	case BIF_BUS_STATE_MASTER_DISABLED:
		/* Disable the BSI peripheral. */
		reg = QPNP_BSI_DISABLE;
		rc = qpnp_bsi_write(chip, QPNP_BSI_REG_ENABLE, &reg, 1);
		if (rc)
			dev_err(&chip->pdev->dev,
				"%s: qpnp_bsi_write() failed, rc=%d\n",
				__func__, rc);
		break;
	case BIF_BUS_STATE_POWER_DOWN:
		rc = qpnp_bsi_bus_transaction(bdev, BIF_TRANS_BC, BIF_CMD_PDWN);
		if (rc)
			dev_err(&chip->pdev->dev,
				"%s: failed to enable power down mode, rc=%d\n",
				__func__, rc);
		break;
	case BIF_BUS_STATE_STANDBY:
		rc = qpnp_bsi_bus_transaction(bdev, BIF_TRANS_BC, BIF_CMD_STBY);
		if (rc)
			dev_err(&chip->pdev->dev,
				"%s: failed to enable standby mode, rc=%d\n",
				__func__, rc);
		break;
	case BIF_BUS_STATE_ACTIVE:
		rc = qpnp_bsi_bus_set_active_mode(bdev, chip->state);
		if (rc)
			dev_err(&chip->pdev->dev,
				"%s: failed to enable active mode, rc=%d\n",
				__func__, rc);
		break;
	case BIF_BUS_STATE_INTERRUPT:
		/*
		 * qpnp_bsi_bus_set_interrupt_mode() internally sets
		 * chip->state = BIF_BUS_STATE_INTERRUPT immediately before
		 * issuing the EINT command.
		 */
		rc = qpnp_bsi_bus_set_interrupt_mode(bdev);
		if (rc) {
			dev_err(&chip->pdev->dev,
				"%s: failed to enable interrupt mode, rc=%d\n",
				__func__, rc);
		} else if (chip->state == BIF_BUS_STATE_ACTIVE) {
			/*
			 * A slave interrupt was received immediately after
			 * issuing the EINT command.  Therefore, stay in active
			 * communication mode.
			 */
			state = BIF_BUS_STATE_ACTIVE;
		}
		break;
	default:
		rc = -EINVAL;
		dev_err(&chip->pdev->dev, "%s: invalid state=%d\n",
			__func__, state);
	}

	if (!rc)
		chip->state = state;

	return rc;
}

/* Returns the smallest tau_bif that is greater than or equal to period_ns. */
static int qpnp_bsi_tau_bif_higher(int period_ns, int sample_mask)
{
	const int *supported_period_ns =
			(sample_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X ?
				qpnp_bsi_tau_period.period_4x_ns :
				qpnp_bsi_tau_period.period_8x_ns);
	int smallest_tau_bif = INT_MAX;
	int i;

	for (i = QPNP_BSI_NUM_CLOCK_PERIODS - 1; i >= 0; i--) {
		if (period_ns <= supported_period_ns[i]) {
			smallest_tau_bif = supported_period_ns[i];
			break;
		}
	}

	return smallest_tau_bif;
}

/* Returns the largest tau_bif that is less than or equal to period_ns. */
static int qpnp_bsi_tau_bif_lower(int period_ns, int sample_mask)
{
	const int *supported_period_ns =
			(sample_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X ?
				qpnp_bsi_tau_period.period_4x_ns :
				qpnp_bsi_tau_period.period_8x_ns);
	int largest_tau_bif = 0;
	int i;

	for (i = 0; i < QPNP_BSI_NUM_CLOCK_PERIODS; i++) {
		if (period_ns >= supported_period_ns[i]) {
			largest_tau_bif = supported_period_ns[i];
			break;
		}
	}

	return largest_tau_bif;
}

/*
 * Moves period_ns into allowed range and then sets tau bif to the period that
 * is greater than or equal to period_ns.
 */
static int qpnp_bsi_set_tau_bif(struct qpnp_bsi_chip *chip, int period_ns)
{
	const int *supported_period_ns =
		(chip->tau_sampling_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X ?
			qpnp_bsi_tau_period.period_4x_ns :
			qpnp_bsi_tau_period.period_8x_ns);
	int idx = 0;
	int i, rc;
	u8 reg;

	if (period_ns < chip->bdesc.bus_clock_min_ns)
		period_ns = chip->bdesc.bus_clock_min_ns;
	else if (period_ns > chip->bdesc.bus_clock_max_ns)
		period_ns = chip->bdesc.bus_clock_max_ns;

	for (i = QPNP_BSI_NUM_CLOCK_PERIODS - 1; i >= 0; i--) {
		if (period_ns <= supported_period_ns[i]) {
			idx = i;
			break;
		}
	}

	/* Set the tau BIF clock period and sampling rate. */
	reg = chip->tau_sampling_mask | idx;
	rc = qpnp_bsi_write(chip, QPNP_BSI_REG_TAU_CONFIG, &reg, 1);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: qpnp_bsi_write() failed, rc=%d\n",
			__func__, rc);
		return rc;
	}

	chip->tau_index = idx;

	return 0;
}

static int qpnp_bsi_get_bus_period(struct bif_ctrl_dev *bdev)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);

	return qpnp_bsi_get_tau_ns(chip);
}

static int qpnp_bsi_set_bus_period(struct bif_ctrl_dev *bdev, int period_ns)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);

	return qpnp_bsi_set_tau_bif(chip, period_ns);
}

static int qpnp_bsi_get_battery_rid(struct bif_ctrl_dev *bdev)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
	struct qpnp_vadc_result adc_result;
	int rid_ohm, vid_uV, rc;
	s64 temp;

	if (chip->batt_id_adc_channel >= ADC_MAX_NUM) {
		dev_err(&chip->pdev->dev,
			"%s: no ADC channel specified for Rid measurement\n",
			__func__);
		return -ENXIO;
	}

	rc = qpnp_vadc_read(chip->vadc_dev, chip->batt_id_adc_channel,
								&adc_result);
	if (!rc) {
		vid_uV = adc_result.physical;

		if (chip->vid_ref_uV - vid_uV <= 0) {
			rid_ohm = INT_MAX;
		} else {
			temp = (s64)chip->r_pullup_ohm * (s64)vid_uV;
			do_div(temp, chip->vid_ref_uV - vid_uV);
			if (temp > INT_MAX)
				rid_ohm = INT_MAX;
			else
				rid_ohm = temp;
		}
	} else {
		dev_err(&chip->pdev->dev,
			"%s: qpnp_vadc_read(%d) failed, rc=%d\n",
			__func__, chip->batt_id_adc_channel, rc);
		rid_ohm = rc;
	}

	return rid_ohm;
}

/*
 * Returns 1 if a battery pack is present on the BIF bus, 0 if a battery pack
 * is not present, or errno if detection fails.
 *
 * Battery detection is based upon the idle BCL voltage.
 */
static int qpnp_bsi_get_battery_presence(struct bif_ctrl_dev *bdev)
{
	struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
	int rc;
	uint val;

	rc = regmap_read(chip->regmap, chip->batt_id_stat_addr, &val);
	if (rc) {
		dev_err(&chip->pdev->dev,
			"%s: regmap_bulk_readl failed, rc=%d\n",
			__func__, rc);
		return rc;
	}

	return !!(val & QPNP_SMBB_BAT_IF_BATT_PRES_MASK);
}

static struct bif_ctrl_ops qpnp_bsi_ops = {
	.bus_transaction	= qpnp_bsi_bus_transaction,
	.bus_transaction_query	= qpnp_bsi_bus_transaction_query,
	.bus_transaction_read	= qpnp_bsi_bus_transaction_read,
	.get_bus_state		= qpnp_bsi_get_bus_state,
	.set_bus_state		= qpnp_bsi_set_bus_state,
	.get_bus_period		= qpnp_bsi_get_bus_period,
	.set_bus_period		= qpnp_bsi_set_bus_period,
	.read_slave_registers	= qpnp_bsi_read_slave_registers,
	.write_slave_registers	= qpnp_bsi_write_slave_registers,
	.get_battery_rid	= qpnp_bsi_get_battery_rid,
	.get_battery_presence	= qpnp_bsi_get_battery_presence,
};

/* Load all BSI properties from device tree. */
static int qpnp_bsi_parse_dt(struct qpnp_bsi_chip *chip,
			struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *node = pdev->dev.of_node;
	unsigned int base;
	int rc, temp;

	chip->batt_id_adc_channel = ADC_MAX_NUM;
	rc = of_property_read_u32(node, "qcom,channel-num",
				  &chip->batt_id_adc_channel);
	if (!rc && (chip->batt_id_adc_channel < 0
			|| chip->batt_id_adc_channel >= ADC_MAX_NUM)) {
		dev_err(dev, "%s: invalid qcom,channel-num=%d specified\n",
			__func__, chip->batt_id_adc_channel);
		return -EINVAL;
	}

	chip->r_pullup_ohm = QPNP_BSI_DEFAULT_PULLUP_OHM;
	rc = of_property_read_u32(node, "qcom,pullup-ohms",
					&chip->r_pullup_ohm);
	if (!rc && (chip->r_pullup_ohm < QPNP_BSI_MIN_PULLUP_OHM ||
			chip->r_pullup_ohm > QPNP_BSI_MAX_PULLUP_OHM)) {
		dev_err(dev, "%s: invalid qcom,pullup-ohms=%d property value\n",
			__func__, chip->r_pullup_ohm);
		return -EINVAL;
	}

	chip->vid_ref_uV = QPNP_BSI_DEFAULT_VID_REF_UV;
	rc = of_property_read_u32(node, "qcom,vref-microvolts",
					&chip->vid_ref_uV);
	if (!rc && (chip->vid_ref_uV < QPNP_BSI_MIN_VID_REF_UV ||
			chip->vid_ref_uV > QPNP_BSI_MAX_VID_REF_UV)) {
		dev_err(dev, "%s: invalid qcom,vref-microvolts=%d property value\n",
			__func__, chip->vid_ref_uV);
		return -EINVAL;
	}

	rc = of_property_read_u32(pdev->dev.of_node, "bsi-base", &base);
	if (rc < 0) {
		dev_err(&pdev->dev,
			"Couldn't find bsi-base in node = %s rc = %d\n",
			pdev->dev.of_node->full_name, rc);
		return rc;
	}
	chip->base_addr = base;

	rc = of_property_read_u32(pdev->dev.of_node, "batt-id-status", &base);
	if (rc < 0) {
		dev_err(&pdev->dev,
			"Couldn't find batt-if-status in node = %s rc = %d\n",
			pdev->dev.of_node->full_name, rc);
		return rc;
	}
	chip->batt_id_stat_addr = base;

	chip->bdesc.name = dev_name(&pdev->dev);
	if (!chip->bdesc.name) {
		dev_err(dev, "%s: label binding undefined for node %s\n",
			__func__, pdev->dev.of_node->full_name);
		return -EINVAL;
	}

	/* Use maximum range by default. */
	chip->bdesc.bus_clock_min_ns	= QPNP_BSI_MIN_CLOCK_SPEED_NS;
	chip->bdesc.bus_clock_max_ns	= QPNP_BSI_MAX_CLOCK_SPEED_NS;
	chip->tau_sampling_mask		= QPNP_BSI_TAU_CONFIG_SAMPLE_4X;

	rc = of_property_read_u32(node, "qcom,sample-rate", &temp);
	if (rc == 0) {
		if (temp == 4) {
			chip->tau_sampling_mask = QPNP_BSI_TAU_CONFIG_SAMPLE_4X;
		} else if (temp == 8) {
			chip->tau_sampling_mask = QPNP_BSI_TAU_CONFIG_SAMPLE_8X;
		} else {
			dev_err(dev, "%s: invalid qcom,sample-rate=%d.  Only values of 4 and 8 are supported.\n",
				__func__, temp);
			return -EINVAL;
		}
	}

	rc = of_property_read_u32(node, "qcom,min-clock-period", &temp);
	if (rc == 0)
		chip->bdesc.bus_clock_min_ns = qpnp_bsi_tau_bif_higher(temp,
						chip->tau_sampling_mask);

	rc = of_property_read_u32(node, "qcom,max-clock-period", &temp);
	if (rc == 0)
		chip->bdesc.bus_clock_max_ns = qpnp_bsi_tau_bif_lower(temp,
						chip->tau_sampling_mask);

	if (chip->bdesc.bus_clock_min_ns > chip->bdesc.bus_clock_max_ns) {
		dev_err(dev, "%s: invalid qcom,min/max-clock-period.\n",
			__func__);
		return -EINVAL;
	}

	chip->irq[QPNP_BSI_IRQ_ERR] = platform_get_irq_byname(pdev, "err");
	if (chip->irq[QPNP_BSI_IRQ_ERR] < 0) {
		dev_err(dev, "%s: node is missing err irq\n", __func__);
		return chip->irq[QPNP_BSI_IRQ_ERR];
	}

	chip->irq[QPNP_BSI_IRQ_RX] = platform_get_irq_byname(pdev, "rx");
	if (chip->irq[QPNP_BSI_IRQ_RX] < 0) {
		dev_err(dev, "%s: node is missing rx irq\n", __func__);
		return chip->irq[QPNP_BSI_IRQ_RX];
	}

	chip->irq[QPNP_BSI_IRQ_TX] = platform_get_irq_byname(pdev, "tx");
	if (chip->irq[QPNP_BSI_IRQ_TX] < 0) {
		dev_err(dev, "%s: node is missing tx irq\n", __func__);
		return chip->irq[QPNP_BSI_IRQ_TX];
	}

	chip->batt_present_irq = platform_get_irq_byname(pdev, "batt-present");
	if (chip->batt_present_irq < 0) {
		dev_err(dev, "%s: node is missing batt-present irq\n",
			__func__);
		return chip->batt_present_irq;
	}

	return rc;
}

/* Request all BSI and battery presence IRQs and set them as wakeable. */
static int qpnp_bsi_init_irqs(struct qpnp_bsi_chip *chip,
			struct device *dev)
{
	int rc;

	rc = devm_request_irq(dev, chip->irq[QPNP_BSI_IRQ_ERR],
			qpnp_bsi_isr, IRQF_TRIGGER_HIGH, "bsi-err", chip);
	if (rc < 0) {
		dev_err(dev, "%s: request for bsi-err irq %d failed, rc=%d\n",
			__func__, chip->irq[QPNP_BSI_IRQ_ERR], rc);
		return rc;
	}

	rc = irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_ERR], 1);
	if (rc < 0) {
		dev_err(dev, "%s: unable to set bsi-err irq %d as wakeable, rc=%d\n",
			__func__, chip->irq[QPNP_BSI_IRQ_ERR], rc);
		return rc;
	}

	rc = devm_request_irq(dev, chip->irq[QPNP_BSI_IRQ_RX],
			qpnp_bsi_isr, IRQF_TRIGGER_HIGH, "bsi-rx", chip);
	if (rc < 0) {
		dev_err(dev, "%s: request for bsi-rx irq %d failed, rc=%d\n",
			__func__, chip->irq[QPNP_BSI_IRQ_RX], rc);
		goto set_unwakeable_irq_err;
	}

	rc = irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_RX], 1);
	if (rc < 0) {
		dev_err(dev, "%s: unable to set bsi-rx irq %d as wakeable, rc=%d\n",
			__func__, chip->irq[QPNP_BSI_IRQ_RX], rc);
		goto set_unwakeable_irq_err;
	}

	rc = devm_request_irq(dev, chip->irq[QPNP_BSI_IRQ_TX],
			qpnp_bsi_isr, IRQF_TRIGGER_HIGH, "bsi-tx", chip);
	if (rc < 0) {
		dev_err(dev, "%s: request for bsi-tx irq %d failed, rc=%d\n",
			__func__, chip->irq[QPNP_BSI_IRQ_TX], rc);
		goto set_unwakeable_irq_rx;
	}

	rc = irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_TX], 1);
	if (rc < 0) {
		dev_err(dev, "%s: unable to set bsi-tx irq %d as wakeable, rc=%d\n",
			__func__, chip->irq[QPNP_BSI_IRQ_TX], rc);
		goto set_unwakeable_irq_rx;
	}

	rc = devm_request_threaded_irq(dev, chip->batt_present_irq, NULL,
		qpnp_bsi_batt_present_isr,
		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_SHARED
			| IRQF_ONESHOT,
		"bsi-batt-present", chip);
	if (rc < 0) {
		dev_err(dev, "%s: request for bsi-batt-present irq %d failed, rc=%d\n",
			__func__, chip->batt_present_irq, rc);
		goto set_unwakeable_irq_tx;
	}

	rc = irq_set_irq_wake(chip->batt_present_irq, 1);
	if (rc < 0) {
		dev_err(dev, "%s: unable to set bsi-batt-present irq %d as wakeable, rc=%d\n",
			__func__, chip->batt_present_irq, rc);
		goto set_unwakeable_irq_tx;
	}

	return rc;

set_unwakeable_irq_tx:
	irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_TX], 0);
set_unwakeable_irq_rx:
	irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_RX], 0);
set_unwakeable_irq_err:
	irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_ERR], 0);
	return rc;
}

static void qpnp_bsi_cleanup_irqs(struct qpnp_bsi_chip *chip)
{
	irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_ERR], 0);
	irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_RX], 0);
	irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_TX], 0);
	irq_set_irq_wake(chip->batt_present_irq, 0);
}

static int qpnp_bsi_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct qpnp_bsi_chip *chip;
	int rc;
	u8 type[2];

	if (!pdev->dev.of_node) {
		dev_err(dev, "%s: device node missing\n", __func__);
		return -ENODEV;
	}

	chip = devm_kzalloc(dev, sizeof(struct qpnp_bsi_chip), GFP_KERNEL);
	if (!chip) {
		dev_err(dev, "%s: Can't allocate qpnp_bsi\n", __func__);
		return -ENOMEM;
	}
	chip->regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!chip->regmap) {
		dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
		return -EINVAL;
	}

	rc = qpnp_bsi_parse_dt(chip, pdev);
	if (rc) {
		dev_err(dev, "%s: device tree parsing failed, rc=%d\n",
			__func__, rc);
		return rc;
	}

	INIT_WORK(&chip->slave_irq_work, qpnp_bsi_slave_irq_work);

	rc = qpnp_bsi_init_irqs(chip, dev);
	if (rc) {
		dev_err(dev, "%s: IRQ initialization failed, rc=%d\n",
			__func__, rc);
		return rc;
	}

	chip->pdev		= pdev;
	chip->bdesc.ops		= &qpnp_bsi_ops;
	chip->state		= BIF_BUS_STATE_MASTER_DISABLED;
	chip->com_mode		= QPNP_BSI_COM_MODE_IRQ;

	rc = qpnp_bsi_read(chip, QPNP_BSI_REG_TYPE, type, 2);
	if (rc) {
		dev_err(dev, "%s: could not read type register, rc=%d\n",
			__func__, rc);
		goto cleanup_irqs;
	}

	if (type[0] != QPNP_BSI_TYPE || type[1] != QPNP_BSI_SUBTYPE) {
		dev_err(dev, "%s: BSI peripheral is not present; type=0x%02X, subtype=0x%02X\n",
			__func__, type[0], type[1]);
		rc = -ENODEV;
		goto cleanup_irqs;
	}

	/* Ensure that ADC channel is available if it was specified. */
	if (chip->batt_id_adc_channel < ADC_MAX_NUM) {
		chip->vadc_dev = qpnp_get_vadc(dev, "bsi");
		if (IS_ERR(chip->vadc_dev)) {
			rc = PTR_ERR(chip->vadc_dev);
			if (rc != -EPROBE_DEFER)
				pr_err("missing vadc property, rc=%d\n", rc);
			/* Probe retry, do not print an error message */
			goto cleanup_irqs;
		}
	}

	rc = qpnp_bsi_set_tau_bif(chip, chip->bdesc.bus_clock_min_ns);
	if (rc) {
		dev_err(dev, "%s: qpnp_bsi_set_tau_bif() failed, rc=%d\n",
			__func__, rc);
		goto cleanup_irqs;
	}

	chip->bdev = bif_ctrl_register(&chip->bdesc, dev, chip,
					pdev->dev.of_node);
	if (IS_ERR(chip->bdev)) {
		rc = PTR_ERR(chip->bdev);
		dev_err(dev, "%s: bif_ctrl_register failed, rc=%d\n",
			__func__, rc);
		goto cleanup_irqs;
	}

	dev_set_drvdata(dev, chip);

	return rc;

cleanup_irqs:
	qpnp_bsi_cleanup_irqs(chip);
	return rc;
}

static int qpnp_bsi_remove(struct platform_device *pdev)
{
	struct qpnp_bsi_chip *chip = dev_get_drvdata(&pdev->dev);

	dev_set_drvdata(&pdev->dev, NULL);

	if (chip) {
		bif_ctrl_unregister(chip->bdev);
		qpnp_bsi_cleanup_irqs(chip);
	}

	return 0;
}

static struct of_device_id spmi_match_table[] = {
	{ .compatible = QPNP_BSI_DRIVER_NAME, },
	{}
};

static const struct platform_device_id qpnp_bsi_id[] = {
	{ QPNP_BSI_DRIVER_NAME, 0 },
	{ }
};
MODULE_DEVICE_TABLE(spmi, qpnp_bsi_id);

static struct platform_driver qpnp_bsi_driver = {
	.driver = {
		.name		= QPNP_BSI_DRIVER_NAME,
		.of_match_table	= spmi_match_table,
		.owner		= THIS_MODULE,
	},
	.probe		= qpnp_bsi_probe,
	.remove		= qpnp_bsi_remove,
	.id_table	= qpnp_bsi_id,
};

static int __init qpnp_bsi_init(void)
{
	return platform_driver_register(&qpnp_bsi_driver);
}

static void __exit qpnp_bsi_exit(void)
{
	platform_driver_unregister(&qpnp_bsi_driver);
}

MODULE_DESCRIPTION("QPNP PMIC BSI driver");
MODULE_LICENSE("GPL v2");

arch_initcall(qpnp_bsi_init);
module_exit(qpnp_bsi_exit);