aboutsummaryrefslogtreecommitdiff
path: root/drivers/platform/msm/usb_bam.c
blob: 8bb4f9058169d16a1f1d46c97b1cec594e746ca4 (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
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
/* Copyright (c) 2011-2013, 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.
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/stat.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/usb/msm_hsusb.h>
#include <mach/usb_bam.h>
#include <mach/sps.h>
#include <mach/ipa.h>
#include <linux/workqueue.h>
#include <linux/dma-mapping.h>
#include <mach/msm_smsm.h>
#include <linux/pm_runtime.h>

#define USB_THRESHOLD 512
#define USB_BAM_MAX_STR_LEN 50
#define USB_BAM_TIMEOUT (10*HZ)

enum usb_bam_sm {
	USB_BAM_SM_INIT = 0,
	USB_BAM_SM_PLUG_NOTIFIED,
	USB_BAM_SM_PLUG_ACKED,
	USB_BAM_SM_UNPLUG_NOTIFIED,
};

struct usb_bam_peer_handshake_info {
	enum usb_bam_sm state;
	bool client_ready;
	bool ack_received;
	int pending_work;
	struct usb_bam_event_info reset_event;
};

struct usb_bam_sps_type {
	struct sps_bam_props usb_props;
	struct sps_pipe **sps_pipes;
	struct sps_connect *sps_connections;
};

/**
* struct usb_bam_ctx_type - represents the usb bam driver entity
* @usb_bam_sps: holds the sps pipes the usb bam driver holds
*	against the sps driver.
* @usb_bam_pdev: the platfrom device that represents the usb bam.
* @usb_bam_wq: Worqueue used for managing states of reset against
*	a peer bam.
* @qscratch_ram1_reg: The memory region mapped to the qscratch
*	registers.
* @max_connections: The maximum number of pipes that are configured
*	in the platform data.
* @mem_clk: Clock that controls the usb bam driver memory in
*	case the usb bam uses its private memory for the pipes.
* @mem_iface_clk: Clock that controls the usb bam private memory in
*	case the usb bam uses its private memory for the pipes.
* @qdss_core_name: Stores the name of the core ("ssusb", "hsusb" or "hsic")
*	that it used as a peer of the qdss in bam2bam mode.
* @h_bam: This array stores for each BAM ("ssusb", "hsusb" or "hsic") the
*	handle/device of the sps driver.
* @pipes_enabled_per_bam: This array stores for each BAM
*	("ssusb", "hsusb" or "hsic") the number of pipes currently enabled.
* @inactivity_timer_ms: The timeout configuration per each bam for inactivity
*	timer feature.
* @is_bam_inactivity: Is there no activity on all pipes belongs to a
*	specific bam. (no activity = no data is pulled or pushed
*	from/into ones of the pipes).
*/
struct usb_bam_ctx_type {
	struct usb_bam_sps_type usb_bam_sps;
	struct platform_device *usb_bam_pdev;
	struct workqueue_struct *usb_bam_wq;
	void __iomem *qscratch_ram1_reg;
	u8 max_connections;
	struct clk *mem_clk;
	struct clk *mem_iface_clk;
	char qdss_core_name[USB_BAM_MAX_STR_LEN];
	u32 h_bam[MAX_BAMS];
	u8 pipes_enabled_per_bam[MAX_BAMS];
	u32 inactivity_timer_ms[MAX_BAMS];
	bool is_bam_inactivity[MAX_BAMS];
	struct completion reset_done;
};

static char *bam_enable_strings[MAX_BAMS] = {
	[SSUSB_BAM] = "ssusb",
	[HSUSB_BAM] = "hsusb",
	[HSIC_BAM]  = "hsic",
};

static enum usb_bam ipa_rm_bams[] = {HSUSB_BAM, HSIC_BAM};

static enum ipa_client_type ipa_rm_resource_prod[MAX_BAMS] = {
	[HSUSB_BAM] = IPA_RM_RESOURCE_USB_PROD,
	[HSIC_BAM]  = IPA_RM_RESOURCE_HSIC_PROD,
};

static enum ipa_client_type ipa_rm_resource_cons[MAX_BAMS] = {
	[HSUSB_BAM] = IPA_RM_RESOURCE_USB_CONS,
	[HSIC_BAM]  = IPA_RM_RESOURCE_HSIC_CONS,
};

static int usb_cons_request_resource(void);
static int usb_cons_release_resource(void);
static int hsic_cons_request_resource(void);
static int hsic_cons_release_resource(void);

static int (*request_resource_cb[MAX_BAMS])(void) = {
	[HSUSB_BAM] = usb_cons_request_resource,
	[HSIC_BAM]  = hsic_cons_request_resource,
};

static int (*release_resource_cb[MAX_BAMS])(void)  = {
	[HSUSB_BAM] = usb_cons_release_resource,
	[HSIC_BAM]  = hsic_cons_release_resource,
};

struct usb_bam_ipa_handshake_info {
	enum ipa_rm_event cur_prod_state[MAX_BAMS];
	enum ipa_rm_event cur_cons_state[MAX_BAMS];

	bool lpm_wait_handshake[MAX_BAMS];
	int connect_complete;
	bool lpm_wait_pipes;
	int bus_suspend;
	bool disconnected;
	bool in_lpm[MAX_BAMS];
	bool pending_lpm;

	int (*wake_cb)(void *);
	void *wake_param;
	void (*start)(void *, enum usb_bam_pipe_dir);
	void (*stop)(void *, enum usb_bam_pipe_dir);
	void *start_stop_param;

	u32 src_idx;
	u32 dst_idx;
	bool cons_stopped;
	bool prod_stopped;

	struct completion prod_avail[MAX_BAMS];
	struct completion prod_released[MAX_BAMS];

	struct mutex suspend_resume_mutex;
	struct work_struct resume_work;
	struct work_struct suspend_work;
	struct work_struct finish_suspend_work;
};

struct usb_bam_hsic_host_info {
	struct device *dev;
	bool in_lpm;
};

static spinlock_t usb_bam_ipa_handshake_info_lock;
static struct usb_bam_ipa_handshake_info info;
static spinlock_t usb_bam_peer_handshake_info_lock;
static struct usb_bam_peer_handshake_info peer_handshake_info;
static spinlock_t usb_bam_lock; /* Protect ctx and usb_bam_connections */
static struct usb_bam_pipe_connect *usb_bam_connections;
static struct usb_bam_ctx_type ctx;
static struct usb_bam_hsic_host_info hsic_host_info;

static int __usb_bam_register_wake_cb(u8 idx, int (*callback)(void *user),
	void *param, bool trigger_cb_per_pipe);
static void wait_for_prod_release(enum usb_bam cur_bam);

void msm_bam_set_hsic_host_dev(struct device *dev)
{
	if (dev) {
		/* Hold the device until allowing lpm */
		info.in_lpm[HSIC_BAM] = false;
		pr_debug("%s: Getting hsic device %x\n", __func__,
			(int)dev);
		pm_runtime_get(dev);
	} else if (hsic_host_info.dev) {
		pr_debug("%s: Putting hsic device %x\n", __func__,
			(int)hsic_host_info.dev);
		/* Just free previous device*/
		info.in_lpm[HSIC_BAM] = true;
		pm_runtime_put(hsic_host_info.dev);
	}

	hsic_host_info.dev = dev;
	hsic_host_info.in_lpm = false;
}

static int get_bam_type_from_core_name(const char *name)
{
	if (strnstr(name, bam_enable_strings[SSUSB_BAM],
			USB_BAM_MAX_STR_LEN) ||
		strnstr(name, "dwc3", USB_BAM_MAX_STR_LEN))
		return SSUSB_BAM;
	else if (strnstr(name, bam_enable_strings[HSIC_BAM],
			USB_BAM_MAX_STR_LEN))
		return HSIC_BAM;
	else if (strnstr(name, bam_enable_strings[HSUSB_BAM],
			USB_BAM_MAX_STR_LEN) ||
		strnstr(name, "ci", USB_BAM_MAX_STR_LEN))
		return HSUSB_BAM;

	pr_err("%s: invalid BAM name(%s)\n", __func__, name);
	return -EINVAL;
}

static bool bam_use_private_mem(enum usb_bam bam)
{
	int i;

	for (i = 0; i < ctx.max_connections; i++)
		if (usb_bam_connections[i].bam_type == bam &&
			usb_bam_connections[i].mem_type == USB_PRIVATE_MEM)
				return true;

	return false;
}

static void usb_bam_set_inactivity_timer(enum usb_bam bam)
{
	struct sps_timer_ctrl timer_ctrl;
	struct usb_bam_pipe_connect *pipe_connect;
	struct sps_pipe *pipe = NULL;
	int i;

	pr_debug("%s: enter\n", __func__);

	/*
	 * Since we configure global incativity timer for all pipes
	 * and not per each pipe, it is enough to use some pipe
	 * handle associated with this bam, so just find the first one.
	 * This pipe handle is required due to SPS driver API we use below.
	 */
	for (i = 0; i < ctx.max_connections; i++) {
		pipe_connect = &usb_bam_connections[i];
		if (pipe_connect->bam_type == bam &&
		    pipe_connect->enabled) {
			pipe = ctx.usb_bam_sps.sps_pipes[i];
			break;
		}
	}

	if (!pipe) {
		pr_warning("%s: Bam %s has no connected pipes\n", __func__,
			bam_enable_strings[bam]);
		return;
	}

	timer_ctrl.op = SPS_TIMER_OP_CONFIG;
	timer_ctrl.mode = SPS_TIMER_MODE_ONESHOT;
	timer_ctrl.timeout_msec = ctx.inactivity_timer_ms[bam];
	sps_timer_ctrl(pipe, &timer_ctrl, NULL);

	timer_ctrl.op = SPS_TIMER_OP_RESET;
	sps_timer_ctrl(pipe, &timer_ctrl, NULL);
}

static int connect_pipe(u8 idx, u32 *usb_pipe_idx)
{
	int ret, ram1_value;
	enum usb_bam bam;
	struct usb_bam_sps_type usb_bam_sps = ctx.usb_bam_sps;
	struct sps_pipe **pipe = &(usb_bam_sps.sps_pipes[idx]);
	struct sps_connect *sps_connection = &usb_bam_sps.sps_connections[idx];
	struct msm_usb_bam_platform_data *pdata =
		ctx.usb_bam_pdev->dev.platform_data;
	struct usb_bam_pipe_connect *pipe_connect = &usb_bam_connections[idx];
	enum usb_bam_pipe_dir dir = pipe_connect->dir;
	struct sps_mem_buffer *data_buf = &(pipe_connect->data_mem_buf);
	struct sps_mem_buffer *desc_buf = &(pipe_connect->desc_mem_buf);

	*pipe = sps_alloc_endpoint();
	if (*pipe == NULL) {
		pr_err("%s: sps_alloc_endpoint failed\n", __func__);
		return -ENOMEM;
	}

	ret = sps_get_config(*pipe, sps_connection);
	if (ret) {
		pr_err("%s: tx get config failed %d\n", __func__, ret);
		goto free_sps_endpoint;
	}

	ret = sps_phy2h(pipe_connect->src_phy_addr, &(sps_connection->source));
	if (ret) {
		pr_err("%s: sps_phy2h failed (src BAM) %d\n", __func__, ret);
		goto free_sps_endpoint;
	}

	sps_connection->src_pipe_index = pipe_connect->src_pipe_index;
	ret = sps_phy2h(pipe_connect->dst_phy_addr,
		&(sps_connection->destination));
	if (ret) {
		pr_err("%s: sps_phy2h failed (dst BAM) %d\n", __func__, ret);
		goto free_sps_endpoint;
	}
	sps_connection->dest_pipe_index = pipe_connect->dst_pipe_index;

	if (dir == USB_TO_PEER_PERIPHERAL) {
		sps_connection->mode = SPS_MODE_SRC;
		*usb_pipe_idx = pipe_connect->src_pipe_index;
	} else {
		sps_connection->mode = SPS_MODE_DEST;
		*usb_pipe_idx = pipe_connect->dst_pipe_index;
	}

	switch (pipe_connect->mem_type) {
	case SPS_PIPE_MEM:
		pr_debug("%s: USB BAM using SPS pipe memory\n", __func__);
		ret = sps_setup_bam2bam_fifo(
			data_buf,
			pipe_connect->data_fifo_base_offset,
			pipe_connect->data_fifo_size, 1);
		if (ret) {
			pr_err("%s: data fifo setup failure %d\n", __func__,
				ret);
			goto free_sps_endpoint;
		}

		ret = sps_setup_bam2bam_fifo(
			desc_buf,
			pipe_connect->desc_fifo_base_offset,
			pipe_connect->desc_fifo_size, 1);
		if (ret) {
			pr_err("%s: desc. fifo setup failure %d\n", __func__,
				ret);
			goto free_sps_endpoint;
		}
		break;
	case USB_PRIVATE_MEM:
		pr_debug("%s: USB BAM using private memory\n", __func__);

		if (IS_ERR(ctx.mem_clk) || IS_ERR(ctx.mem_iface_clk)) {
			pr_err("%s: Failed to enable USB mem_clk\n", __func__);
			ret = IS_ERR(ctx.mem_clk);
			goto free_sps_endpoint;
		}

		clk_prepare_enable(ctx.mem_clk);
		clk_prepare_enable(ctx.mem_iface_clk);

		/*
		 * Enable USB PRIVATE RAM to be used for BAM FIFOs
		 * HSUSB: Only RAM13 is used for BAM FIFOs
		 * SSUSB: RAM11, 12, 13 are used for BAM FIFOs
		 */
		bam = pipe_connect->bam_type;
		if (bam < 0)
			goto free_sps_endpoint;

		if (bam == HSUSB_BAM)
			ram1_value = 0x4;
		else
			ram1_value = 0x7;

		pr_debug("Writing 0x%x to QSCRATCH_RAM1\n", ram1_value);
		writel_relaxed(ram1_value, ctx.qscratch_ram1_reg);
		/* fall through */
	case OCI_MEM:
		pr_debug("%s: USB BAM using oci memory\n", __func__);
		data_buf->phys_base =
			pipe_connect->data_fifo_base_offset +
				pdata->usb_bam_fifo_baseaddr;
		data_buf->size = pipe_connect->data_fifo_size;
		data_buf->base =
			ioremap(data_buf->phys_base, data_buf->size);
		memset(data_buf->base, 0, data_buf->size);

		desc_buf->phys_base =
			pipe_connect->desc_fifo_base_offset +
				pdata->usb_bam_fifo_baseaddr;
		desc_buf->size = pipe_connect->desc_fifo_size;
		desc_buf->base =
			ioremap(desc_buf->phys_base, desc_buf->size);
		memset(desc_buf->base, 0, desc_buf->size);
		break;
	case SYSTEM_MEM:
		pr_debug("%s: USB BAM using system memory\n", __func__);
		/* BAM would use system memory, allocate FIFOs */
		data_buf->size = pipe_connect->data_fifo_size;
		data_buf->base =
			dma_alloc_coherent(&ctx.usb_bam_pdev->dev,
			pipe_connect->data_fifo_size,
			&(data_buf->phys_base),
			0);
		memset(data_buf->base, 0, pipe_connect->data_fifo_size);

		desc_buf->size = pipe_connect->desc_fifo_size;
		desc_buf->base =
			dma_alloc_coherent(&ctx.usb_bam_pdev->dev,
			pipe_connect->desc_fifo_size,
			&(desc_buf->phys_base),
			0);
		memset(desc_buf->base, 0, pipe_connect->desc_fifo_size);
		break;
	default:
		pr_err("%s: invalid mem type\n", __func__);
		goto free_sps_endpoint;
	}

	sps_connection->data = *data_buf;
	sps_connection->desc = *desc_buf;
	sps_connection->event_thresh = 16;
	sps_connection->options = SPS_O_AUTO_ENABLE;

	ret = sps_connect(*pipe, sps_connection);
	if (ret < 0) {
		pr_err("%s: sps_connect failed %d\n", __func__, ret);
		goto error;
	}

	return 0;

error:
	sps_disconnect(*pipe);
free_sps_endpoint:
	sps_free_endpoint(*pipe);
	return ret;
}

static int connect_pipe_ipa(u8 idx,
			struct usb_bam_connect_ipa_params *ipa_params)
{
	int ret;
	struct usb_bam_sps_type usb_bam_sps = ctx.usb_bam_sps;
	enum usb_bam_pipe_dir dir = ipa_params->dir;
	struct sps_pipe **pipe = &(usb_bam_sps.sps_pipes[idx]);
	struct sps_connect *sps_connection = &usb_bam_sps.sps_connections[idx];
	struct usb_bam_pipe_connect *pipe_connect = &usb_bam_connections[idx];

	struct ipa_connect_params ipa_in_params;
	struct ipa_sps_params sps_out_params;
	u32 usb_handle, usb_phy_addr;
	u32 clnt_hdl = 0;

	memset(&ipa_in_params, 0, sizeof(ipa_in_params));
	memset(&sps_out_params, 0, sizeof(sps_out_params));

	if (dir == USB_TO_PEER_PERIPHERAL) {
		usb_phy_addr = pipe_connect->src_phy_addr;
		ipa_in_params.client_ep_idx = pipe_connect->src_pipe_index;
	} else {
		usb_phy_addr = pipe_connect->dst_phy_addr;
		ipa_in_params.client_ep_idx = pipe_connect->dst_pipe_index;
	}
	/* Get HSUSB / HSIC bam handle */
	ret = sps_phy2h(usb_phy_addr, &usb_handle);
	if (ret) {
		pr_err("%s: sps_phy2h failed (HSUSB/HSIC BAM) %d\n",
			__func__, ret);
		return ret;
	}

	pipe_connect->activity_notify = ipa_params->activity_notify;
	pipe_connect->inactivity_notify = ipa_params->inactivity_notify;
	pipe_connect->priv = ipa_params->priv;

	/* IPA input parameters */
	ipa_in_params.client_bam_hdl = usb_handle;
	ipa_in_params.desc_fifo_sz = pipe_connect->desc_fifo_size;
	ipa_in_params.data_fifo_sz = pipe_connect->data_fifo_size;
	ipa_in_params.notify = ipa_params->notify;
	ipa_in_params.priv = ipa_params->priv;
	ipa_in_params.client = ipa_params->client;

	/* If BAM is using dedicated SPS pipe memory, get it */

	if (pipe_connect->mem_type == SPS_PIPE_MEM) {
		pr_debug("%s: USB BAM using SPS pipe memory\n", __func__);
		ret = sps_setup_bam2bam_fifo(
			&pipe_connect->data_mem_buf,
			pipe_connect->data_fifo_base_offset,
			pipe_connect->data_fifo_size, 1);
		if (ret) {
			pr_err("%s: data fifo setup failure %d\n",
				__func__, ret);
			return ret;
		}

		ret = sps_setup_bam2bam_fifo(
			&pipe_connect->desc_mem_buf,
			pipe_connect->desc_fifo_base_offset,
			pipe_connect->desc_fifo_size, 1);
		if (ret) {
			pr_err("%s: desc. fifo setup failure %d\n",
				__func__, ret);
			return ret;
		}

		ipa_in_params.desc = pipe_connect->desc_mem_buf;
		ipa_in_params.data = pipe_connect->data_mem_buf;
	}

	memcpy(&ipa_in_params.ipa_ep_cfg, &ipa_params->ipa_ep_cfg,
		   sizeof(struct ipa_ep_cfg));

	ret = ipa_connect(&ipa_in_params, &sps_out_params, &clnt_hdl);
	if (ret) {
		pr_err("%s: ipa_connect failed\n", __func__);
		return ret;
	}
	pipe_connect->ipa_clnt_hdl = clnt_hdl;

	*pipe = sps_alloc_endpoint();
	if (*pipe == NULL) {
		pr_err("%s: sps_alloc_endpoint failed\n", __func__);
		ret = -ENOMEM;
		goto disconnect_ipa;
	}

	ret = sps_get_config(*pipe, sps_connection);
	if (ret) {
		pr_err("%s: tx get config failed %d\n", __func__, ret);
		goto free_sps_endpoints;
	}

	if (dir == USB_TO_PEER_PERIPHERAL) {
		/* USB src IPA dest */
		sps_connection->mode = SPS_MODE_SRC;
		ipa_params->cons_clnt_hdl = clnt_hdl;
		sps_connection->source = usb_handle;
		sps_connection->destination = sps_out_params.ipa_bam_hdl;
		sps_connection->src_pipe_index = pipe_connect->src_pipe_index;
		sps_connection->dest_pipe_index = sps_out_params.ipa_ep_idx;
		*(ipa_params->src_pipe) = sps_connection->src_pipe_index;
		pipe_connect->dst_pipe_index = sps_out_params.ipa_ep_idx;
		pr_debug("%s: BAM pipe usb[%x]->ipa[%x] connection\n",
			__func__,
			pipe_connect->src_pipe_index,
			pipe_connect->dst_pipe_index);
		sps_connection->options = SPS_O_NO_DISABLE;
	} else {
		/* IPA src, USB dest */
		sps_connection->mode = SPS_MODE_DEST;
		ipa_params->prod_clnt_hdl = clnt_hdl;
		sps_connection->source = sps_out_params.ipa_bam_hdl;
		sps_connection->destination = usb_handle;
		sps_connection->src_pipe_index = sps_out_params.ipa_ep_idx;
		sps_connection->dest_pipe_index = pipe_connect->dst_pipe_index;
		*(ipa_params->dst_pipe) = sps_connection->dest_pipe_index;
		pipe_connect->src_pipe_index = sps_out_params.ipa_ep_idx;
		pr_debug("%s: BAM pipe ipa[%x]->usb[%x] connection\n",
			__func__,
			pipe_connect->src_pipe_index,
			pipe_connect->dst_pipe_index);
		sps_connection->options = 0;
	}

	sps_connection->data = sps_out_params.data;
	sps_connection->desc = sps_out_params.desc;
	sps_connection->event_thresh = 16;
	sps_connection->options |= SPS_O_AUTO_ENABLE;

	ret = sps_connect(*pipe, sps_connection);
	if (ret < 0) {
		pr_err("%s: sps_connect failed %d\n", __func__, ret);
		goto error;
	}

	return 0;

error:
	sps_disconnect(*pipe);
free_sps_endpoints:
	sps_free_endpoint(*pipe);
disconnect_ipa:
	ipa_disconnect(clnt_hdl);
	return ret;
}

static int disconnect_pipe(u8 idx)
{
	struct usb_bam_pipe_connect *pipe_connect =
		&usb_bam_connections[idx];
	struct sps_pipe *pipe = ctx.usb_bam_sps.sps_pipes[idx];
	struct sps_connect *sps_connection =
		&ctx.usb_bam_sps.sps_connections[idx];

	sps_disconnect(pipe);
	sps_free_endpoint(pipe);

	switch (pipe_connect->mem_type) {
	case SYSTEM_MEM:
		pr_debug("%s: Freeing system memory used by PIPE\n", __func__);
		if (sps_connection->data.phys_base)
			dma_free_coherent(&ctx.usb_bam_pdev->dev,
					sps_connection->data.size,
					sps_connection->data.base,
					sps_connection->data.phys_base);
		if (sps_connection->desc.phys_base)
			dma_free_coherent(&ctx.usb_bam_pdev->dev,
					sps_connection->desc.size,
					sps_connection->desc.base,
					sps_connection->desc.phys_base);
		break;
	case USB_PRIVATE_MEM:
		pr_debug("Freeing private memory used by BAM PIPE\n");
		writel_relaxed(0x0, ctx.qscratch_ram1_reg);
		clk_disable_unprepare(ctx.mem_clk);
		clk_disable_unprepare(ctx.mem_iface_clk);
	case OCI_MEM:
		pr_debug("Freeing oci memory used by BAM PIPE\n");
		iounmap(sps_connection->data.base);
		iounmap(sps_connection->desc.base);
		break;
	case SPS_PIPE_MEM:
		pr_debug("%s: nothing to be be\n", __func__);
		break;
	}

	sps_connection->options &= ~SPS_O_AUTO_ENABLE;
	return 0;
}

static void usb_bam_resume_core(enum usb_bam cur_bam)
{
	struct usb_phy *trans = usb_get_transceiver();

	if (cur_bam != HSUSB_BAM)
		return;
	BUG_ON(trans == NULL);
	pr_debug("%s: resume core", __func__);
	pm_runtime_resume(trans->dev);
}

static void usb_bam_start_lpm(bool disconnect)
{
	struct usb_phy *trans = usb_get_transceiver();

	BUG_ON(trans == NULL);

	spin_lock(&usb_bam_ipa_handshake_info_lock);

	info.lpm_wait_handshake[HSUSB_BAM] = false;
	info.lpm_wait_pipes = 0;

	if (disconnect)
		pm_runtime_put_noidle(trans->dev);

	if (info.pending_lpm) {
		info.pending_lpm = 0;
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		pr_debug("%s: Going to LPM\n", __func__);
		pm_runtime_suspend(trans->dev);
	} else
		spin_unlock(&usb_bam_ipa_handshake_info_lock);

}

int usb_bam_connect(u8 idx, u32 *bam_pipe_idx)
{
	int ret;
	struct usb_bam_pipe_connect *pipe_connect = &usb_bam_connections[idx];
	struct msm_usb_bam_platform_data *pdata;

	if (!ctx.usb_bam_pdev) {
		pr_err("%s: usb_bam device not found\n", __func__);
		return -ENODEV;
	}

	pdata = ctx.usb_bam_pdev->dev.platform_data;

	if (pipe_connect->enabled) {
		pr_debug("%s: connection %d was already established\n",
			__func__, idx);
		return 0;
	}

	if (!bam_pipe_idx) {
		pr_err("%s: invalid bam_pipe_idx\n", __func__);
		return -EINVAL;
	}
	if (idx < 0 || idx > ctx.max_connections) {
		pr_err("idx is wrong %d", idx);
		return -EINVAL;
	}

	spin_lock(&usb_bam_lock);
	/* Check if BAM requires RESET before connect and reset of first pipe */
	if ((pdata->reset_on_connect[pipe_connect->bam_type] == true) &&
	    (ctx.pipes_enabled_per_bam[pipe_connect->bam_type] == 0))
		sps_device_reset(ctx.h_bam[pipe_connect->bam_type]);
	spin_unlock(&usb_bam_lock);

	ret = connect_pipe(idx, bam_pipe_idx);
	if (ret) {
		pr_err("%s: pipe connection[%d] failure\n", __func__, idx);
		return ret;
	}

	pipe_connect->enabled = 1;
	spin_lock(&usb_bam_lock);
	ctx.pipes_enabled_per_bam[pipe_connect->bam_type] += 1;
	spin_unlock(&usb_bam_lock);

	return 0;
}

static int ipa_suspend_pipes(void)
{
	struct usb_bam_pipe_connect *dst_pipe_connect, *src_pipe_connect;
	int ret1, ret2;

	dst_pipe_connect = &usb_bam_connections[info.dst_idx];
	src_pipe_connect = &usb_bam_connections[info.src_idx];

	if (dst_pipe_connect->ipa_clnt_hdl == -1 ||
		src_pipe_connect->ipa_clnt_hdl == -1) {
		pr_err("%s: One of handles is -1, not connected?", __func__);
	}

	ret1 = ipa_suspend(dst_pipe_connect->ipa_clnt_hdl);
	if (ret1)
		pr_err("%s: ipa_suspend on dst failed with %d", __func__, ret1);
	ret2 = ipa_suspend(src_pipe_connect->ipa_clnt_hdl);
	if (ret2)
		pr_err("%s: ipa_suspend on src failed with %d", __func__, ret2);

	return ret1 | ret2;
}

static int ipa_resume_pipes(void)
{
	struct usb_bam_pipe_connect *dst_pipe_connect, *src_pipe_connect;
	int ret1, ret2;

	src_pipe_connect = &usb_bam_connections[info.src_idx];
	dst_pipe_connect = &usb_bam_connections[info.dst_idx];

	if (dst_pipe_connect->ipa_clnt_hdl == -1 ||
		src_pipe_connect->ipa_clnt_hdl == -1) {
		pr_err("%s: One of handles is -1, not connected?", __func__);
	}

	ret1 = ipa_resume(dst_pipe_connect->ipa_clnt_hdl);
	if (ret1)
		pr_err("%s: ipa_resume on dst failed with %d", __func__, ret1);
	ret2 = ipa_resume(src_pipe_connect->ipa_clnt_hdl);
	if (ret2)
		pr_err("%s: ipa_resume on src failed with %d", __func__, ret2);

	return ret1 | ret2;
}

static void usb_bam_finish_suspend(void)
{
	int ret;
	u32 cons_empty;
	struct sps_pipe *cons_pipe = ctx.usb_bam_sps.sps_pipes[info.dst_idx];
	struct usb_bam_pipe_connect *pipe_connect = &
			usb_bam_connections[info.dst_idx];
	enum usb_bam cur_bam = pipe_connect->bam_type;

	if (cur_bam != HSUSB_BAM) {
		pr_err("%s: Wrong type of BAM=%s\n", __func__,
			bam_enable_strings[cur_bam]);
		return;
	}

	mutex_lock(&info.suspend_resume_mutex);

	spin_lock(&usb_bam_ipa_handshake_info_lock);
	/* If cable was disconnected, let disconnection seq do everything */
	if (info.disconnected || info.cons_stopped) {
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		mutex_unlock(&info.suspend_resume_mutex);
		pr_debug("%s: Cable disconnected\n", __func__);
		return;
	}

	/* If resume was called don't finish this work */
	if (!info.bus_suspend) {
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		pr_err("%s: Bus resume in progress\n", __func__);
		goto no_lpm;
	}
	spin_unlock(&usb_bam_ipa_handshake_info_lock);

	ret = sps_is_pipe_empty(cons_pipe, &cons_empty);
	if (ret) {
		pr_err("%s: sps_is_pipe_empty failed with %d\n", __func__, ret);
		goto no_lpm;
	}

	/* Stop CONS transfers and go to lpm if no more data in the pipe */
	if (cons_empty) {
		if (info.stop && !info.cons_stopped)
			info.stop(info.start_stop_param,
					PEER_PERIPHERAL_TO_USB);
		pr_err("%s: Starting LPM on Bus Suspend\n", __func__);
		info.cons_stopped = 1;
		if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_RELEASED) {
			ipa_rm_notify_completion(IPA_RM_RESOURCE_RELEASED,
				ipa_rm_resource_cons[cur_bam]);
		}
		ipa_suspend_pipes();
		usb_bam_start_lpm(0);
		mutex_unlock(&info.suspend_resume_mutex);
		return;
	}

no_lpm:

	/* Finish the handshake. Resume Sequence will start automatically
	   by the data in the pipes */
	if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_RELEASED)
		ipa_rm_notify_completion(IPA_RM_RESOURCE_RELEASED,
				ipa_rm_resource_cons[cur_bam]);
	mutex_unlock(&info.suspend_resume_mutex);
}

void usb_bam_finish_suspend_(struct work_struct *w)
{
	usb_bam_finish_suspend();
}

static void usb_prod_notify_cb(void *user_data, enum ipa_rm_event event,
	unsigned long data)
{
	enum usb_bam *cur_bam = (void *)user_data;

	switch (event) {
	case IPA_RM_RESOURCE_GRANTED:
		pr_debug("%s: %s_PROD resource granted\n",
			__func__, bam_enable_strings[*cur_bam]);
		info.cur_prod_state[*cur_bam] = IPA_RM_RESOURCE_GRANTED;
		complete_all(&info.prod_avail[*cur_bam]);
		break;
	case IPA_RM_RESOURCE_RELEASED:
		pr_debug("%s: %s_PROD resource released\n",
			__func__, bam_enable_strings[*cur_bam]);
		info.cur_prod_state[*cur_bam] = IPA_RM_RESOURCE_RELEASED;
		complete_all(&info.prod_released[*cur_bam]);
		break;
	default:
		break;
	}
	return;
}

/**
 * usb_bam_resume_hsic_host: vote for hsic host core resume.
 * In addition also resume all hsic pipes that are connected to
 * the ipa peer bam.
 *
 * NOTE: This function should be called in a context that hold
 *	 usb_bam_lock.
 */
static void usb_bam_resume_hsic_host(void)
{
	int i;
	struct usb_bam_pipe_connect *pipe_iter;

	/* Exit from "full suspend" in case of hsic host */
	if (hsic_host_info.dev && info.in_lpm[HSIC_BAM]) {
		pr_debug("%s: Getting hsic device %x\n", __func__,
			(int)hsic_host_info.dev);
		pm_runtime_get(hsic_host_info.dev);
		info.in_lpm[HSIC_BAM] = false;

		for (i = 0; i < ctx.max_connections; i++) {
			pipe_iter = &usb_bam_connections[i];
			if (pipe_iter->bam_type == HSIC_BAM &&
			    pipe_iter->enabled &&
			    pipe_iter->suspended) {
				spin_unlock(&usb_bam_lock);
				ipa_resume(pipe_iter->ipa_clnt_hdl);
				pipe_iter->suspended = false;
				spin_lock(&usb_bam_lock);
			}
		}
	}
}

static int cons_request_resource(enum usb_bam cur_bam)
{
	int ret = -EINPROGRESS;

	pr_debug("%s: Request %s_CONS resource\n",
			__func__, bam_enable_strings[cur_bam]);

	spin_lock(&usb_bam_ipa_handshake_info_lock);
	info.cur_cons_state[cur_bam] = IPA_RM_RESOURCE_GRANTED;

	spin_lock(&usb_bam_lock);

	switch (cur_bam) {
	case HSUSB_BAM:
		if (ctx.pipes_enabled_per_bam[HSUSB_BAM] &&
		    info.connect_complete &&
			!info.cons_stopped && !info.prod_stopped) {
			pr_debug("%s: ACK on cons_request", __func__);
			ret = 0;
		} else if (ctx.pipes_enabled_per_bam[HSUSB_BAM] &&
				   info.connect_complete && info.bus_suspend) {
			info.bus_suspend = 0;
			if (info.wake_cb)
				info.wake_cb(info.wake_param);
		}

		break;
	case HSIC_BAM:
		/*
		 * Vote for hsic resume, however the core
		 * resume may not be completed yet or on the other hand
		 * hsic core might already be resumed, due to a vote
		 * by other driver, in this case we will just renew our
		 * vote here.
		 */
		usb_bam_resume_hsic_host();

		/*
		 * Return sucess if there are pipes connected
		 * and hsic core is actually not in lpm.
		 * If in lpm, grant will occur on resume
		 * finish (see msm_bam_hsic_notify_on_resume)
		 */
		if (ctx.pipes_enabled_per_bam[cur_bam] &&
		    !hsic_host_info.in_lpm) {
			ret = 0;
		}

		break;
	case SSUSB_BAM:
	default:
		break;
	}

	spin_unlock(&usb_bam_ipa_handshake_info_lock);
	spin_unlock(&usb_bam_lock);

	if (ret == -EINPROGRESS)
		pr_debug("%s: EINPROGRESS on cons_request", __func__);

	return ret;
}

static int usb_cons_request_resource(void)
{
	return cons_request_resource(HSUSB_BAM);
}

static int hsic_cons_request_resource(void)
{
	return cons_request_resource(HSIC_BAM);
}

static int cons_release_resource(enum usb_bam cur_bam)
{
	pr_debug("%s: Release %s_CONS resource\n",
			__func__, bam_enable_strings[cur_bam]);

	info.cur_cons_state[cur_bam] = IPA_RM_RESOURCE_RELEASED;

	spin_lock(&usb_bam_lock);
	if (!ctx.pipes_enabled_per_bam[cur_bam]) {
		spin_unlock(&usb_bam_lock);
		pr_debug("%s: ACK on cons_release", __func__);
		return 0;
	}
	spin_unlock(&usb_bam_lock);

	if (cur_bam == HSUSB_BAM) {
		spin_lock(&usb_bam_ipa_handshake_info_lock);
		if (info.bus_suspend)
			queue_work(ctx.usb_bam_wq, &info.finish_suspend_work);
		spin_unlock(&usb_bam_ipa_handshake_info_lock);

		pr_debug("%s: EINPROGRESS cons_release", __func__);
		return -EINPROGRESS;
	} else if (cur_bam == HSIC_BAM) {

		/*
		 * Allow to go to lpm for now. Actual state will be checked
		 * in msm_bam_hsic_lpm_ok() just before going to lpm.
		 */
		if (hsic_host_info.dev && !info.in_lpm[HSIC_BAM]) {
			pr_debug("%s: Putting hsic device %x\n", __func__,
			(int)hsic_host_info.dev);
			pm_runtime_put(hsic_host_info.dev);
			info.in_lpm[HSIC_BAM] = true;
		}
	}

	return 0;
}

static int hsic_cons_release_resource(void)
{
	return cons_release_resource(HSIC_BAM);
}

static int usb_cons_release_resource(void)
{
	return cons_release_resource(HSUSB_BAM);
}

static void usb_bam_ipa_create_resources(void)
{
	struct ipa_rm_create_params usb_prod_create_params;
	struct ipa_rm_create_params usb_cons_create_params;
	enum usb_bam cur_bam;
	int ret, i;

	for (i = 0; i < ARRAY_SIZE(ipa_rm_bams); i++) {
		/* Create USB/HSIC_PROD entity */
		cur_bam = ipa_rm_bams[i];

		memset(&usb_prod_create_params, 0,
					sizeof(usb_prod_create_params));
		usb_prod_create_params.name = ipa_rm_resource_prod[cur_bam];
		usb_prod_create_params.reg_params.notify_cb =
							usb_prod_notify_cb;
		usb_prod_create_params.reg_params.user_data = &ipa_rm_bams[i];
		ret = ipa_rm_create_resource(&usb_prod_create_params);
		if (ret) {
			pr_err("%s: Failed to create USB_PROD resource\n",
								__func__);
			return;
		}

		/* Create USB_CONS entity */
		memset(&usb_cons_create_params, 0,
					sizeof(usb_cons_create_params));
		usb_cons_create_params.name = ipa_rm_resource_cons[cur_bam];
		usb_cons_create_params.request_resource =
						request_resource_cb[cur_bam];
		usb_cons_create_params.release_resource =
						release_resource_cb[cur_bam];
		ret = ipa_rm_create_resource(&usb_cons_create_params);
		if (ret) {
			pr_err("%s: Failed to create USB_CONS resource\n",
								__func__);
			return ;
		}
	}
}

static void wait_for_prod_granted(enum usb_bam cur_bam)
{
	int ret;

	pr_debug("%s Request %s_PROD_RES\n", __func__,
		bam_enable_strings[cur_bam]);
	if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_GRANTED)
		pr_debug("%s: CONS already granted for some reason\n",
			__func__);
	if (info.cur_prod_state[cur_bam] == IPA_RM_RESOURCE_GRANTED)
		pr_debug("%s: PROD already granted for some reason\n",
			__func__);

	init_completion(&info.prod_avail[cur_bam]);

	ret = ipa_rm_request_resource(ipa_rm_resource_prod[cur_bam]);
	if (!ret) {
		info.cur_prod_state[cur_bam] = IPA_RM_RESOURCE_GRANTED;
		complete_all(&info.prod_avail[cur_bam]);
		pr_debug("%s: PROD_GRANTED without wait\n", __func__);
	} else if (ret == -EINPROGRESS) {
		pr_debug("%s: Waiting for PROD_GRANTED\n", __func__);
		if (!wait_for_completion_timeout(&info.prod_avail[cur_bam],
			USB_BAM_TIMEOUT))
			pr_err("%s: Timeout wainting for PROD_GRANTED\n",
				__func__);
	} else
		pr_err("%s: ipa_rm_request_resource ret =%d\n", __func__, ret);
}

void notify_usb_connected(enum usb_bam cur_bam)
{
	pr_debug("%s: enter\n", __func__);

	spin_lock(&usb_bam_ipa_handshake_info_lock);
	if (cur_bam == HSUSB_BAM)
		info.connect_complete = 1;
	spin_unlock(&usb_bam_ipa_handshake_info_lock);

	if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_GRANTED) {
		pr_debug("%s: Notify %s_CONS_GRANTED\n", __func__,
			bam_enable_strings[cur_bam]);
		ipa_rm_notify_completion(IPA_RM_RESOURCE_GRANTED,
				 ipa_rm_resource_cons[cur_bam]);
	}
}

static void wait_for_prod_release(enum usb_bam cur_bam)
{
	int ret;

	if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_RELEASED)
		pr_debug("%s consumer already released\n", __func__);
	 if (info.cur_prod_state[cur_bam] == IPA_RM_RESOURCE_RELEASED)
		pr_debug("%s producer already released\n", __func__);

	init_completion(&info.prod_released[cur_bam]);
	pr_debug("%s: Releasing %s_PROD\n", __func__,
				bam_enable_strings[cur_bam]);
	ret = ipa_rm_release_resource(ipa_rm_resource_prod[cur_bam]);
	if (!ret) {
		pr_debug("%s: Released without waiting\n", __func__);
		info.cur_prod_state[cur_bam] = IPA_RM_RESOURCE_RELEASED;
		complete_all(&info.prod_released[cur_bam]);
	} else if (ret == -EINPROGRESS) {
		pr_debug("%s: Waiting for PROD_RELEASED\n", __func__);
		if (!wait_for_completion_timeout(&info.prod_released[cur_bam],
						USB_BAM_TIMEOUT))
			pr_err("%s: Timeout waiting for PROD_RELEASED\n",
			__func__);
	} else
		pr_err("%s: ipa_rm_request_resource ret =%d", __func__, ret);
}

static int check_pipes_empty(u8 src_idx, u8 dst_idx)
{
	struct sps_pipe *prod_pipe, *cons_pipe;
	u32 prod_empty, cons_empty;

	/* If we have any remaints in the pipes we don't go to sleep */
	prod_pipe = ctx.usb_bam_sps.sps_pipes[src_idx];
	cons_pipe = ctx.usb_bam_sps.sps_pipes[dst_idx];
	if (sps_is_pipe_empty(prod_pipe, &prod_empty) ||
			sps_is_pipe_empty(cons_pipe, &cons_empty)) {
		pr_err("%s: sps_is_pipe_empty failed with\n", __func__);
		return 0;
	}
	if (!prod_empty || !cons_empty) {
		pr_err("%s: pipes not empty prod=%d cond=%d", __func__,
			prod_empty, cons_empty);
		return 0;
	}

	return 1;
}

void usb_bam_suspend(struct usb_bam_connect_ipa_params *ipa_params)
{
	struct usb_bam_pipe_connect *pipe_connect;
	enum usb_bam cur_bam;
	u8 src_idx, dst_idx;

	if (!ipa_params) {
		pr_err("%s: Invalid ipa params\n", __func__);
		return;
	}

	src_idx = ipa_params->src_idx;
	dst_idx = ipa_params->dst_idx;

	if (src_idx >= ctx.max_connections || dst_idx >= ctx.max_connections) {
		pr_err("%s: Invalid connection index src=%d dst=%d\n",
			__func__, src_idx, dst_idx);
	}

	pipe_connect = &usb_bam_connections[src_idx];
	cur_bam = pipe_connect->bam_type;
	if (cur_bam != HSUSB_BAM)
		return;

	info.src_idx = src_idx;
	info.dst_idx = dst_idx;

	pr_err("%s: Starting suspend sequence(BAM=%s)\n", __func__,
			bam_enable_strings[cur_bam]);

	spin_lock(&usb_bam_ipa_handshake_info_lock);
	info.bus_suspend = 1;
	spin_unlock(&usb_bam_ipa_handshake_info_lock);

	/* Stop PROD transfers */
	if (info.stop) {
		spin_lock(&usb_bam_ipa_handshake_info_lock);
		info.stop(info.start_stop_param, USB_TO_PEER_PERIPHERAL);
		info.prod_stopped = true;
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
	}

	spin_lock(&usb_bam_ipa_handshake_info_lock);
	/* If cable was disconnected, let disconnection seq do everything */
	if (info.disconnected) {
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		pr_debug("%s: Cable disconnected\n", __func__);
		return;
	}

	/* Don't go to LPM if data in the pipes */
	if (!check_pipes_empty(src_idx, dst_idx)) {
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		pr_err("%s: pipes not empty, won't start suspend", __func__);
		return;
	}
	spin_unlock(&usb_bam_ipa_handshake_info_lock);

	queue_work(ctx.usb_bam_wq, &info.suspend_work);
}

static void usb_bam_start_suspend(struct work_struct *w)
{
	pr_debug("%s: enter", __func__);
	mutex_lock(&info.suspend_resume_mutex);

	spin_lock(&usb_bam_ipa_handshake_info_lock);
	/* If cable was disconnected, let disconnection seq do everything */
	if (info.disconnected) {
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		mutex_unlock(&info.suspend_resume_mutex);
		pr_debug("%s: Cable disconnected\n", __func__);
		return;
	}

	if (!info.bus_suspend) {
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		pr_err("%s: Resume started, not suspending", __func__);
		mutex_unlock(&info.suspend_resume_mutex);
		return;
	}

	/* Stop PROD transfers in case they were started */
	if (info.stop && !info.prod_stopped) {
		info.stop(info.start_stop_param, USB_TO_PEER_PERIPHERAL);
		info.prod_stopped = true;
	}
	spin_unlock(&usb_bam_ipa_handshake_info_lock);

	/* Don't start LPM seq if data in the pipes */
	if (!check_pipes_empty(info.src_idx, info.dst_idx)) {
		mutex_unlock(&info.suspend_resume_mutex);
		return;
	}

	spin_lock(&usb_bam_ipa_handshake_info_lock);
	info.lpm_wait_handshake[HSUSB_BAM] = true;
	spin_unlock(&usb_bam_ipa_handshake_info_lock);

	wait_for_prod_release(HSUSB_BAM);

	mutex_unlock(&info.suspend_resume_mutex);
	if (info.cur_cons_state[HSUSB_BAM] == IPA_RM_RESOURCE_RELEASED)
		usb_bam_finish_suspend();
	else
		pr_debug("Consumer not released yet\n");
}

static void usb_bam_finish_resume(struct work_struct *w)
{
	struct usb_phy *trans = usb_get_transceiver();

	BUG_ON(trans == NULL);
	pr_debug("%s: enter", __func__);
	mutex_lock(&info.suspend_resume_mutex);
	/* Suspend happened in the meantime */
	spin_lock(&usb_bam_ipa_handshake_info_lock);
	if (info.bus_suspend) {
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		pr_err("%s: Bus suspended, not resuming", __func__);
		mutex_unlock(&info.suspend_resume_mutex);
		return;
	}
	info.lpm_wait_handshake[HSUSB_BAM] = true;
	spin_unlock(&usb_bam_ipa_handshake_info_lock);

	wait_for_prod_granted(HSUSB_BAM);
	notify_usb_connected(HSUSB_BAM);
	if (info.cons_stopped) {
		ipa_resume_pipes();
		if (info.start) {
			pr_debug("%s: Enqueue CONS transfer", __func__);
			info.start(info.start_stop_param,
				PEER_PERIPHERAL_TO_USB);
			info.cons_stopped = 0;
		}
	}

	if (info.start) {
		spin_lock(&usb_bam_ipa_handshake_info_lock);
		info.start(info.start_stop_param, USB_TO_PEER_PERIPHERAL);
		info.prod_stopped = false;
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
	}
	if (info.cur_cons_state[HSUSB_BAM] == IPA_RM_RESOURCE_GRANTED) {
		pr_debug("%s: Notify CONS_GRANTED\n", __func__);
		ipa_rm_notify_completion(IPA_RM_RESOURCE_GRANTED,
				 ipa_rm_resource_cons[HSUSB_BAM]);
	}
	mutex_unlock(&info.suspend_resume_mutex);
	pr_debug("%s: done", __func__);
}

void usb_bam_resume(struct usb_bam_connect_ipa_params *ipa_params)
{
	enum usb_bam cur_bam;
	u8 src_idx, dst_idx;
	struct usb_bam_pipe_connect *pipe_connect;

	pr_debug("%s: Resuming\n", __func__);

	if (!ipa_params) {
		pr_err("%s: Invalid ipa params\n", __func__);
		return;
	}

	src_idx = ipa_params->src_idx;
	dst_idx = ipa_params->dst_idx;

	if (src_idx >= ctx.max_connections || dst_idx >= ctx.max_connections) {
		pr_err("%s: Invalid connection index src=%d dst=%d\n",
			__func__, src_idx, dst_idx);
		return;
	}

	pipe_connect = &usb_bam_connections[src_idx];
	cur_bam = pipe_connect->bam_type;
	if (cur_bam != HSUSB_BAM)
		return;

	info.in_lpm[HSUSB_BAM] = false;
	spin_lock(&usb_bam_ipa_handshake_info_lock);
	info.bus_suspend = 0;
	spin_unlock(&usb_bam_ipa_handshake_info_lock);
	queue_work(ctx.usb_bam_wq, &info.resume_work);
}

void msm_bam_wait_for_hsic_prod_granted(void)
{
	spin_lock(&usb_bam_lock);

	ctx.is_bam_inactivity[HSIC_BAM] = false;

	/* Get back to resume state including wakeup ipa */
	usb_bam_resume_hsic_host();

	/* Ensure getting the producer resource */
	wait_for_prod_granted(HSIC_BAM);

	spin_unlock(&usb_bam_lock);
}

void msm_bam_hsic_notify_on_resume(void)
{
	spin_lock(&usb_bam_lock);

	hsic_host_info.in_lpm = false;

	/* HSIC resume completed. Notify CONS grant if CONS was requested */
	notify_usb_connected(HSIC_BAM);

	/*
	 * This function is called to notify the usb bam driver
	 * that the hsic core and hsic bam hw are fully resumed
	 * and clocked on. Therefore we can now set the inactivity
	 * timer to the hsic bam hw.
	 */
	if (ctx.inactivity_timer_ms[HSIC_BAM])
		usb_bam_set_inactivity_timer(HSIC_BAM);

	spin_unlock(&usb_bam_lock);
}

bool msm_bam_hsic_lpm_ok(void)
{
	int i;
	struct usb_bam_pipe_connect *pipe_iter;

	if (hsic_host_info.dev) {

		pr_debug("%s: Starting hsic full suspend sequence\n",
			__func__);

		/*
		 * Start low power mode by releasing the device
		 * only in case that indeed the resources were released
		 * and we are still in inactivity state (wake event
		 * have not been occured while we were waiting to the
		 * resources release)
		 */
		spin_lock(&usb_bam_lock);

		if (info.cur_cons_state[HSIC_BAM] ==
			IPA_RM_RESOURCE_RELEASED &&
		    info.cur_prod_state[HSIC_BAM] ==
			IPA_RM_RESOURCE_RELEASED &&
		    ctx.is_bam_inactivity[HSIC_BAM] && info.in_lpm[HSIC_BAM]) {

			/* HSIC host will go now to lpm */
			pr_debug("%s: vote for suspend hsic %x\n",
				__func__, (int)hsic_host_info.dev);

			for (i = 0; i < ctx.max_connections; i++) {
				pipe_iter =
					&usb_bam_connections[i];
				if (pipe_iter->bam_type == HSIC_BAM &&
				    pipe_iter->enabled &&
				    !pipe_iter->suspended) {
					spin_unlock(&usb_bam_lock);
					ipa_suspend(
					   pipe_iter->ipa_clnt_hdl);
					pipe_iter->suspended = true;
					spin_lock(&usb_bam_lock);
				}
			}

			hsic_host_info.in_lpm = true;

			spin_unlock(&usb_bam_lock);
			return true;
		}

		/* We don't allow lpm, therefore renew our vote here */
		if (info.in_lpm[HSIC_BAM]) {
			pr_err("%s: Not allow lpm while ref count=0\n",
				__func__);
			pr_err("%s: inactivity=%d, c_s=%d p_s=%d lpm=%d\n",
				__func__, ctx.is_bam_inactivity[HSIC_BAM],
				info.cur_cons_state[HSIC_BAM],
				info.cur_prod_state[HSIC_BAM],
				info.in_lpm[HSIC_BAM]);
			pm_runtime_get(hsic_host_info.dev);
			info.in_lpm[HSIC_BAM] = false;
			spin_unlock(&usb_bam_lock);
		} else
			spin_unlock(&usb_bam_lock);

		return false;
	}

	return true;
}

int usb_bam_connect_ipa(struct usb_bam_connect_ipa_params *ipa_params)
{
	u8 idx;
	enum usb_bam cur_bam;
	struct usb_bam_pipe_connect *pipe_connect;
	int ret;
	struct msm_usb_bam_platform_data *pdata =
					ctx.usb_bam_pdev->dev.platform_data;

	if (!ipa_params) {
		pr_err("%s: Invalid ipa params\n",
			__func__);
		return -EINVAL;
	}

	if (ipa_params->dir == USB_TO_PEER_PERIPHERAL)
		idx = ipa_params->src_idx;
	else
		idx = ipa_params->dst_idx;

	if (idx >= ctx.max_connections) {
		pr_err("%s: Invalid connection index\n",
			__func__);
		return -EINVAL;
	}
	pipe_connect = &usb_bam_connections[idx];
	cur_bam = pipe_connect->bam_type;

	if (pipe_connect->enabled) {
		pr_err("%s: connection %d was already established\n",
			__func__, idx);
		return 0;
	}

	pr_debug("%s: enter", __func__);

	if (cur_bam == HSUSB_BAM) {
		mutex_lock(&info.suspend_resume_mutex);

		spin_lock(&usb_bam_lock);
		if (ctx.pipes_enabled_per_bam[HSUSB_BAM] == 0) {
			spin_unlock(&usb_bam_lock);
			spin_lock(&usb_bam_ipa_handshake_info_lock);
			info.lpm_wait_handshake[HSUSB_BAM] = true;
			info.connect_complete = 0;
			info.disconnected = 0;
			info.pending_lpm = 0;
			info.lpm_wait_pipes = 1;
			info.bus_suspend = 0;
			info.cons_stopped = 0;
			info.prod_stopped = 0;
			spin_unlock(&usb_bam_ipa_handshake_info_lock);
			usb_bam_resume_core(cur_bam);
		} else
			spin_unlock(&usb_bam_lock);
	}

	 /* Check if BAM requires RESET before connect and reset first pipe */
	 spin_lock(&usb_bam_lock);
	 if ((pdata->reset_on_connect[cur_bam] == true) &&
	     (ctx.pipes_enabled_per_bam[cur_bam] == 0)) {
		spin_unlock(&usb_bam_lock);
		sps_device_reset(ctx.h_bam[cur_bam]);

		/* On re-connect assume out from lpm for HSIC BAM */
		if (cur_bam == HSIC_BAM && hsic_host_info.dev &&
		    info.in_lpm[HSIC_BAM]) {
			pr_debug("%s: Getting hsic device %x\n",
					__func__, (int)hsic_host_info.dev);
			pm_runtime_get(hsic_host_info.dev);
		}

		/* On re-connect assume out from lpm for all BAMs */
		info.in_lpm[cur_bam] = false;
	 } else
		spin_unlock(&usb_bam_lock);

	if (ipa_params->dir == USB_TO_PEER_PERIPHERAL) {
		pr_debug("%s: Starting connect sequence\n", __func__);
		wait_for_prod_granted(cur_bam);
	}

	ret = connect_pipe_ipa(idx, ipa_params);
	if (ret) {
		pr_err("%s: pipe connection failure\n", __func__);
		if (cur_bam == HSUSB_BAM)
			mutex_unlock(&info.suspend_resume_mutex);
		return ret;
	}

	spin_lock(&usb_bam_lock);
	pipe_connect->enabled = 1;
	pipe_connect->suspended = 0;

	/* Set global inactivity timer upon first pipe connection */
	if (ctx.pipes_enabled_per_bam[pipe_connect->bam_type] == 0 &&
		ctx.inactivity_timer_ms[pipe_connect->bam_type] &&
		pipe_connect->inactivity_notify)
		usb_bam_set_inactivity_timer(pipe_connect->bam_type);

	ctx.pipes_enabled_per_bam[cur_bam] += 1;
	spin_unlock(&usb_bam_lock);
	if (ipa_params->dir == PEER_PERIPHERAL_TO_USB)
		notify_usb_connected(cur_bam);

	if (cur_bam == HSUSB_BAM)
		mutex_unlock(&info.suspend_resume_mutex);

	pr_debug("%s: done", __func__);

	return 0;
}
EXPORT_SYMBOL(usb_bam_connect_ipa);

int usb_bam_client_ready(bool ready)
{
	spin_lock(&usb_bam_peer_handshake_info_lock);
	if (peer_handshake_info.client_ready == ready) {
		pr_debug("%s: client state is already %d\n",
			__func__, ready);
		spin_unlock(&usb_bam_peer_handshake_info_lock);
		return 0;
	}

	peer_handshake_info.client_ready = ready;
	if (peer_handshake_info.state == USB_BAM_SM_PLUG_ACKED && !ready) {
		pr_debug("Starting reset sequence");
		INIT_COMPLETION(ctx.reset_done);
	}

	spin_unlock(&usb_bam_peer_handshake_info_lock);
	if (!queue_work(ctx.usb_bam_wq,
			&peer_handshake_info.reset_event.event_w)) {
		spin_lock(&usb_bam_peer_handshake_info_lock);
		peer_handshake_info.pending_work++;
		spin_unlock(&usb_bam_peer_handshake_info_lock);
	}

	return 0;
}

static void usb_bam_work(struct work_struct *w)
{
	int i;
	struct usb_bam_event_info *event_info =
		container_of(w, struct usb_bam_event_info, event_w);
	struct usb_bam_pipe_connect *pipe_connect =
		container_of(event_info, struct usb_bam_pipe_connect, event);
	struct usb_bam_pipe_connect *pipe_iter;
	int (*callback)(void *priv);
	void *param = NULL;

	switch (event_info->type) {
	case USB_BAM_EVENT_WAKEUP:
	case USB_BAM_EVENT_WAKEUP_PIPE:

		pr_debug("%s recieved USB_BAM_EVENT_WAKEUP\n", __func__);

		/*
		 * Make sure the PROD resource is granted before
		 * wakeup hsic host class driver (done by the callback below)
		 */
		if (pipe_connect->peer_bam == IPA_P_BAM &&
		    pipe_connect->bam_type == HSIC_BAM &&
		    info.cur_prod_state[HSIC_BAM] != IPA_RM_RESOURCE_GRANTED) {
			wait_for_prod_granted(HSIC_BAM);
		}

		/*
		 * Check if need to resume the hsic host.
		 * On one hand, since we got the wakeup interrupt
		 * the hsic bam clocks are already enabled, so no need
		 * to actualluy resume the hardware... However, we still need
		 * to update the usb bam driver state (to set in_lpm=false),
		 * and to wake ipa (ipa_resume) and to hold again the hsic host
		 * device again to avoid it going to low poer mode next time
		 * until we complete releasing the hsic consumer and producer
		 * resources against the ipa resource manager.
		 */
		spin_lock(&usb_bam_lock);
		if (pipe_connect->bam_type == HSIC_BAM)
			usb_bam_resume_hsic_host();
		spin_unlock(&usb_bam_lock);

		/* Notify about wakeup / activity of the bam */
		if (event_info->callback)
			event_info->callback(event_info->param);

		/*
		 * Reset inactivity timer counter if this pipe's bam
		 * has inactivity timeout.
		 */
		spin_lock(&usb_bam_lock);
		if (ctx.inactivity_timer_ms[pipe_connect->bam_type])
			usb_bam_set_inactivity_timer(pipe_connect->bam_type);
		spin_unlock(&usb_bam_lock);

		if (pipe_connect->bam_type == HSUSB_BAM) {
			/* A2 wakeup not from LPM (CONS was up) */
			wait_for_prod_granted(pipe_connect->bam_type);
			if (info.start) {
				pr_debug("%s: Enqueue PROD transfer", __func__);
				info.start(info.start_stop_param,
						USB_TO_PEER_PERIPHERAL);
			}
		}

		break;

	case USB_BAM_EVENT_INACTIVITY:

		pr_debug("%s recieved USB_BAM_EVENT_INACTIVITY\n", __func__);

		/*
		 * Since event info is one structure per pipe, it might be
		 * overriden when we will register the wakeup events below,
		 * and still we want ot register the wakeup events before we
		 * notify on the inactivity in order to identify the next
		 * activity as soon as possible.
		 */
		callback = event_info->callback;
		param = event_info->param;

		/*
		 * Upon inactivity, configure wakeup irq for all pipes
		 * that are into the usb bam.
		 */
		spin_lock(&usb_bam_lock);
		for (i = 0; i < ctx.max_connections; i++) {
			pipe_iter = &usb_bam_connections[i];
			if (pipe_iter->bam_type ==
				pipe_connect->bam_type &&
			    pipe_iter->dir ==
				PEER_PERIPHERAL_TO_USB &&
				pipe_iter->enabled) {
				pr_debug("%s: Register wakeup on pipe %x\n",
					__func__, (int)pipe_iter);
				__usb_bam_register_wake_cb(i,
					pipe_iter->activity_notify,
					pipe_iter->priv,
					false);
			}
		}
		spin_unlock(&usb_bam_lock);

		/* Notify about the inactivity to the USB class driver */
		if (callback)
			callback(param);

		wait_for_prod_release(pipe_connect->bam_type);
		pr_debug("%s: complete wait on hsic producer s=%d\n",
			__func__, info.cur_prod_state[pipe_connect->bam_type]);

		/*
		 * Allow to go to lpm for now if also consumer is down.
		 * If consumer is up, we will wait to the release consumer
		 * notification.
		 */
		if (hsic_host_info.dev &&
		    info.cur_cons_state[HSIC_BAM] ==
		    IPA_RM_RESOURCE_RELEASED && !info.in_lpm[HSIC_BAM]) {
			pr_debug("%s: Putting hsic device %x\n", __func__,
			(int)hsic_host_info.dev);
			pm_runtime_put(hsic_host_info.dev);
			info.in_lpm[HSIC_BAM] = true;
		}

		break;
	default:
		pr_err("%s: unknown usb bam event type %d\n", __func__,
			event_info->type);
	}
}

static void usb_bam_wake_cb(struct sps_event_notify *notify)
{
	struct usb_bam_event_info *event_info =
		(struct usb_bam_event_info *)notify->user;
	struct usb_bam_pipe_connect *pipe_connect =
		container_of(event_info,
			     struct usb_bam_pipe_connect,
			     event);
	enum usb_bam bam = pipe_connect->bam_type;

	spin_lock(&usb_bam_lock);

	if (event_info->type == USB_BAM_EVENT_WAKEUP_PIPE)
		queue_work(ctx.usb_bam_wq, &event_info->event_w);
	else if (event_info->type == USB_BAM_EVENT_WAKEUP &&
			ctx.is_bam_inactivity[bam]) {

		/*
		 * Sps wake event is per pipe, so usb_bam_wake_cb is
		 * called per pipe. However, we want to filter the wake
		 * event to be wake event per all the pipes.
		 * Therefore, the first pipe that awaked will be considered
		 * as global bam wake event.
		 */
		ctx.is_bam_inactivity[bam] = false;

		queue_work(ctx.usb_bam_wq, &event_info->event_w);
	}

	spin_unlock(&usb_bam_lock);
}

static void usb_bam_sm_work(struct work_struct *w)
{
	pr_debug("%s: current state: %d\n", __func__,
		peer_handshake_info.state);

	spin_lock(&usb_bam_peer_handshake_info_lock);

	switch (peer_handshake_info.state) {
	case USB_BAM_SM_INIT:
		if (peer_handshake_info.client_ready) {
			spin_unlock(&usb_bam_peer_handshake_info_lock);
			smsm_change_state(SMSM_APPS_STATE, 0,
				SMSM_USB_PLUG_UNPLUG);
			spin_lock(&usb_bam_peer_handshake_info_lock);
			peer_handshake_info.state = USB_BAM_SM_PLUG_NOTIFIED;
		}
		break;
	case USB_BAM_SM_PLUG_NOTIFIED:
		if (peer_handshake_info.ack_received) {
			peer_handshake_info.state = USB_BAM_SM_PLUG_ACKED;
			peer_handshake_info.ack_received = 0;
		}
		break;
	case USB_BAM_SM_PLUG_ACKED:
		if (!peer_handshake_info.client_ready) {
			spin_unlock(&usb_bam_peer_handshake_info_lock);
			pr_debug("Starting A2 reset sequence");
			smsm_change_state(SMSM_APPS_STATE,
				SMSM_USB_PLUG_UNPLUG, 0);
			spin_lock(&usb_bam_peer_handshake_info_lock);
			peer_handshake_info.state = USB_BAM_SM_UNPLUG_NOTIFIED;
		}
		break;
	case USB_BAM_SM_UNPLUG_NOTIFIED:
		if (peer_handshake_info.ack_received) {
			spin_unlock(&usb_bam_peer_handshake_info_lock);
			peer_handshake_info.reset_event.
				callback(peer_handshake_info.reset_event.param);
			spin_lock(&usb_bam_peer_handshake_info_lock);
			complete_all(&ctx.reset_done);
			pr_debug("Finished reset sequence");
			peer_handshake_info.state = USB_BAM_SM_INIT;
			peer_handshake_info.ack_received = 0;
		}
		break;
	}

	if (peer_handshake_info.pending_work) {
		peer_handshake_info.pending_work--;
		spin_unlock(&usb_bam_peer_handshake_info_lock);
		queue_work(ctx.usb_bam_wq,
			&peer_handshake_info.reset_event.event_w);
		spin_lock(&usb_bam_peer_handshake_info_lock);
	}
	spin_unlock(&usb_bam_peer_handshake_info_lock);
}

static void usb_bam_ack_toggle_cb(void *priv,
	uint32_t old_state, uint32_t new_state)
{
	static int last_processed_state;
	int current_state;

	spin_lock(&usb_bam_peer_handshake_info_lock);

	current_state = new_state & SMSM_USB_PLUG_UNPLUG;

	if (current_state == last_processed_state) {
		spin_unlock(&usb_bam_peer_handshake_info_lock);
		return;
	}

	last_processed_state = current_state;
	peer_handshake_info.ack_received = true;

	spin_unlock(&usb_bam_peer_handshake_info_lock);
	if (!queue_work(ctx.usb_bam_wq,
			&peer_handshake_info.reset_event.event_w)) {
		spin_lock(&usb_bam_peer_handshake_info_lock);
		peer_handshake_info.pending_work++;
		spin_unlock(&usb_bam_peer_handshake_info_lock);
	}
}

static int __usb_bam_register_wake_cb(u8 idx, int (*callback)(void *user),
	void *param, bool trigger_cb_per_pipe)
{
	struct sps_pipe *pipe = ctx.usb_bam_sps.sps_pipes[idx];
	struct sps_connect *sps_connection;
	struct usb_bam_pipe_connect *pipe_connect;
	struct usb_bam_event_info *wake_event_info;
	int ret;

	if (idx < 0 || idx > ctx.max_connections) {
		pr_err("%s:idx is wrong %d", __func__, idx);
		return -EINVAL;
	}
	pipe = ctx.usb_bam_sps.sps_pipes[idx];
	sps_connection = &ctx.usb_bam_sps.sps_connections[idx];
	pipe_connect = &usb_bam_connections[idx];
	wake_event_info = &pipe_connect->event;

	wake_event_info->type = (trigger_cb_per_pipe ?
				USB_BAM_EVENT_WAKEUP_PIPE :
				USB_BAM_EVENT_WAKEUP);
	wake_event_info->param = param;
	wake_event_info->callback = callback;
	wake_event_info->event.mode = SPS_TRIGGER_CALLBACK;
	wake_event_info->event.xfer_done = NULL;
	wake_event_info->event.callback = callback ? usb_bam_wake_cb : NULL;
	wake_event_info->event.user = wake_event_info;
	wake_event_info->event.options = SPS_O_WAKEUP;
	ret = sps_register_event(pipe, &wake_event_info->event);
	if (ret) {
		pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
		return ret;
	}

	sps_connection->options = callback ?
		(SPS_O_AUTO_ENABLE | SPS_O_WAKEUP | SPS_O_WAKEUP_IS_ONESHOT) :
		SPS_O_AUTO_ENABLE;
	ret = sps_set_config(pipe, sps_connection);
	if (ret) {
		pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
		return ret;
	}

	return 0;
}

int usb_bam_register_wake_cb(u8 idx, int (*callback)(void *user),
	void *param)
{
	info.wake_cb = callback;
	info.wake_param = param;
	return __usb_bam_register_wake_cb(idx, callback, param, true);
}

int usb_bam_register_start_stop_cbs(
	void (*start)(void *, enum usb_bam_pipe_dir),
	void (*stop)(void *, enum usb_bam_pipe_dir),
	void *param)
{
	info.start = start;
	info.stop = stop;
	info.start_stop_param = param;
	return 0;
}

int usb_bam_register_peer_reset_cb(int (*callback)(void *), void *param)
{
	u32 ret = 0;

	if (callback) {
		peer_handshake_info.reset_event.param = param;
		peer_handshake_info.reset_event.callback = callback;

		ret = smsm_state_cb_register(SMSM_MODEM_STATE,
			SMSM_USB_PLUG_UNPLUG, usb_bam_ack_toggle_cb, NULL);
		if (ret) {
			pr_err("%s: failed to register SMSM callback\n",
				__func__);
		} else {
			if (smsm_get_state(SMSM_MODEM_STATE) &
				SMSM_USB_PLUG_UNPLUG)
				usb_bam_ack_toggle_cb(NULL, 0,
					SMSM_USB_PLUG_UNPLUG);
		}
	} else {
		peer_handshake_info.reset_event.param = NULL;
		peer_handshake_info.reset_event.callback = NULL;
		smsm_state_cb_deregister(SMSM_MODEM_STATE,
			SMSM_USB_PLUG_UNPLUG, usb_bam_ack_toggle_cb, NULL);
	}

	return ret;
}

int usb_bam_disconnect_pipe(u8 idx)
{
	struct usb_bam_pipe_connect *pipe_connect;
	int ret;

	pipe_connect = &usb_bam_connections[idx];

	if (!pipe_connect->enabled) {
		pr_err("%s: connection %d isn't enabled\n",
			__func__, idx);
		return 0;
	}

	ret = disconnect_pipe(idx);
	if (ret) {
		pr_err("%s: src pipe disconnection failure\n", __func__);
		return ret;
	}

	pipe_connect->enabled = 0;
	spin_lock(&usb_bam_lock);
	if (ctx.pipes_enabled_per_bam[pipe_connect->bam_type] == 0)
		pr_err("%s: wrong pipes enabled counter for bam_type=%d\n",
			__func__, pipe_connect->bam_type);
	else
		ctx.pipes_enabled_per_bam[pipe_connect->bam_type] -= 1;
	spin_unlock(&usb_bam_lock);

	return 0;
}

int usb_bam_disconnect_ipa(struct usb_bam_connect_ipa_params *ipa_params)
{
	int ret;
	u8 idx;
	struct usb_bam_pipe_connect *pipe_connect;
	struct sps_connect *sps_connection;
	enum usb_bam cur_bam;

	if (!ipa_params->prod_clnt_hdl && !ipa_params->cons_clnt_hdl) {
		pr_err("%s: Both of the handles is missing\n", __func__);
		return -EINVAL;
	}

	pr_debug("%s: Starting disconnect sequence\n", __func__);

	mutex_lock(&info.suspend_resume_mutex);
	/* Delay USB core to go into lpm before we finish our handshake */
	if (ipa_params->prod_clnt_hdl) {
		idx = ipa_params->dst_idx;
		pipe_connect = &usb_bam_connections[idx];

		pipe_connect->activity_notify = NULL;
		pipe_connect->inactivity_notify = NULL;
		pipe_connect->priv = NULL;

		/* Do the release handshake with the A2 via RM */
		cur_bam = pipe_connect->bam_type;
		spin_lock(&usb_bam_ipa_handshake_info_lock);
		if (cur_bam == HSUSB_BAM) {
			info.connect_complete = 0;
			info.lpm_wait_pipes = 1;
			info.disconnected = 1;
		}
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		wait_for_prod_release(cur_bam);
		/* close USB -> IPA pipe */
		usb_bam_resume_core(cur_bam);
		ret = ipa_disconnect(ipa_params->prod_clnt_hdl);
		if (ret) {
			pr_err("%s: dst pipe disconnection failure\n",
				__func__);
			mutex_unlock(&info.suspend_resume_mutex);
			return ret;
		}
		sps_connection = &ctx.usb_bam_sps.sps_connections[idx];
		sps_connection->data.phys_base = 0;
		sps_connection->desc.phys_base = 0;

		ret = usb_bam_disconnect_pipe(idx);
		if (ret) {
			pr_err("%s: failure to disconnect pipe %d\n",
				__func__, idx);
			mutex_unlock(&info.suspend_resume_mutex);
			return ret;
		}
	}

	if (ipa_params->cons_clnt_hdl) {
		idx = ipa_params->src_idx;
		pipe_connect = &usb_bam_connections[idx];

		pipe_connect->activity_notify = NULL;
		pipe_connect->inactivity_notify = NULL;
		pipe_connect->priv = NULL;

		cur_bam = pipe_connect->bam_type;
		/* close IPA -> USB pipe */
		ret = ipa_disconnect(ipa_params->cons_clnt_hdl);
		if (ret) {
			pr_err("%s: src pipe disconnection failure\n",
				__func__);
			mutex_unlock(&info.suspend_resume_mutex);
			return ret;
		}

		sps_connection = &ctx.usb_bam_sps.sps_connections[idx];
		sps_connection->data.phys_base = 0;
		sps_connection->desc.phys_base = 0;

		ret = usb_bam_disconnect_pipe(idx);
		if (ret) {
			pr_err("%s: failure to disconnect pipe %d\n",
				__func__, idx);
			mutex_unlock(&info.suspend_resume_mutex);
			return ret;
		}

		pipe_connect->ipa_clnt_hdl = -1;

		if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_RELEASED) {
			pr_debug("%s Notify CONS _RELEASED\n", __func__);
			ipa_rm_notify_completion(IPA_RM_RESOURCE_RELEASED,
				ipa_rm_resource_cons[cur_bam]);
		}

		if (cur_bam == HSUSB_BAM) {
			/*
			 * Currently we have for HSUSB BAM only one consumer
			 * pipe. Therefore ending disconnect sequence and
			 * starting hsusb lpm. This limitation will be changed
			 * in future patch.
			 */
			pr_debug("%s Ended disconnect sequence\n", __func__);
			usb_bam_start_lpm(1);
		}

		mutex_unlock(&info.suspend_resume_mutex);
		return 0;
	}

	mutex_unlock(&info.suspend_resume_mutex);
	return 0;
}
EXPORT_SYMBOL(usb_bam_disconnect_ipa);

void usb_bam_reset_complete(void)
{
	pr_debug("Waiting for reset compelte");
	if (wait_for_completion_interruptible_timeout(&ctx.reset_done,
			10*HZ) <= 0)
		pr_warn("Timeout while waiting for reset");

	pr_debug("Finished Waiting for reset complete");
}

int usb_bam_a2_reset(bool to_reconnect)
{
	struct usb_bam_pipe_connect *pipe_connect;
	int i;
	int ret = 0, ret_int;
	u8 bam = -1;
	int reconnect_pipe_idx[ctx.max_connections];

	for (i = 0; i < ctx.max_connections; i++)
		reconnect_pipe_idx[i] = -1;

	/* Disconnect a2 pipes */
	for (i = 0; i < ctx.max_connections; i++) {
		pipe_connect = &usb_bam_connections[i];
		if (strnstr(pipe_connect->name, "a2", USB_BAM_MAX_STR_LEN) &&
				pipe_connect->enabled) {
			if (pipe_connect->dir == USB_TO_PEER_PERIPHERAL)
				reconnect_pipe_idx[i] =
					pipe_connect->src_pipe_index;
			else
				reconnect_pipe_idx[i] =
					pipe_connect->dst_pipe_index;

			bam = pipe_connect->bam_type;
			if (bam < 0) {
				ret = -EINVAL;
				continue;
			}
			ret_int = usb_bam_disconnect_pipe(i);
			if (ret_int) {
				pr_err("%s: failure to connect pipe %d\n",
					__func__, i);
				ret = ret_int;
				continue;
			}
		}
	}
	/* Reset A2 (USB/HSIC) BAM */
	if (bam != -1 && sps_device_reset(ctx.h_bam[bam]))
		pr_err("%s: BAM reset failed\n", __func__);

	if (!to_reconnect)
		return ret;

	/* Reconnect A2 pipes */
	for (i = 0; i < ctx.max_connections; i++) {
		pipe_connect = &usb_bam_connections[i];
		if (reconnect_pipe_idx[i] != -1) {
			ret_int = usb_bam_connect(i, &reconnect_pipe_idx[i]);
			if (ret_int) {
				pr_err("%s: failure to reconnect pipe %d\n",
					__func__, i);
				ret = ret_int;
				continue;
			}
		}
	}

	return ret;
}

static void usb_bam_sps_events(enum sps_callback_case sps_cb_case, void *user)
{
	int i;
	enum usb_bam bam;
	struct usb_bam_pipe_connect *pipe_connect;
	struct usb_bam_event_info *event_info;

	switch (sps_cb_case) {
	case SPS_CALLBACK_BAM_TIMER_IRQ:

		pr_debug("%s:recieved SPS_CALLBACK_BAM_TIMER_IRQ\n", __func__);

		spin_lock(&usb_bam_lock);

		bam = get_bam_type_from_core_name((char *)user);
		ctx.is_bam_inactivity[bam] = true;
		pr_debug("%s: Incativity happened on bam=%s,%d\n", __func__,
			(char *)user, bam);

		for (i = 0; i < ctx.max_connections; i++) {
			pipe_connect = &usb_bam_connections[i];

			/*
			 * Notify inactivity once, Since it is global
			 * for all pipes on bam. Notify only if we have
			 * connected pipes.
			 */
			if (pipe_connect->bam_type == bam &&
			    pipe_connect->enabled) {
				event_info = &pipe_connect->event;
				event_info->type = USB_BAM_EVENT_INACTIVITY;
				event_info->param = pipe_connect->priv;
				event_info->callback =
					pipe_connect->inactivity_notify;
				queue_work(ctx.usb_bam_wq,
						&event_info->event_w);
				break;
			}
		}

		spin_unlock(&usb_bam_lock);

		break;
	default:
		pr_debug("%s:received sps_cb_case=%d\n", __func__,
			(int)sps_cb_case);
	}
}

static struct msm_usb_bam_platform_data *usb_bam_dt_to_pdata(
	struct platform_device *pdev)
{
	struct msm_usb_bam_platform_data *pdata;
	struct device_node *node = pdev->dev.of_node;
	int rc = 0;
	u8 i = 0;
	bool reset_bam;
	enum usb_bam bam;
	u32 addr;

	ctx.max_connections = 0;
	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata) {
		pr_err("unable to allocate platform data\n");
		return NULL;
	}

	rc = of_property_read_u32(node, "qcom,usb-bam-num-pipes",
		&pdata->usb_bam_num_pipes);
	if (rc) {
		pr_err("Invalid usb bam num pipes property\n");
		return NULL;
	}

	rc = of_property_read_u32(node, "qcom,usb-bam-fifo-baseaddr",
			&addr);
	if (rc)
		pr_debug("%s: Invalid usb base address property\n", __func__);
	else
		pdata->usb_bam_fifo_baseaddr = addr;

	pdata->ignore_core_reset_ack = of_property_read_bool(node,
		"qcom,ignore-core-reset-ack");

	pdata->disable_clk_gating = of_property_read_bool(node,
		"qcom,disable-clk-gating");

	for_each_child_of_node(pdev->dev.of_node, node)
		ctx.max_connections++;

	if (!ctx.max_connections) {
		pr_err("%s: error: max_connections is zero\n", __func__);
		goto err;
	}

	usb_bam_connections = devm_kzalloc(&pdev->dev, ctx.max_connections *
		sizeof(struct usb_bam_pipe_connect), GFP_KERNEL);

	if (!usb_bam_connections) {
		pr_err("%s: devm_kzalloc failed(%d)\n", __func__,  __LINE__);
		return NULL;
	}

	/* retrieve device tree parameters */
	for_each_child_of_node(pdev->dev.of_node, node) {
		rc = of_property_read_string(node, "label",
			&usb_bam_connections[i].name);
		if (rc)
			goto err;

		rc = of_property_read_u32(node, "qcom,usb-bam-mem-type",
			&usb_bam_connections[i].mem_type);
		if (rc)
			goto err;

		if (usb_bam_connections[i].mem_type == USB_PRIVATE_MEM ||
				usb_bam_connections[i].mem_type == OCI_MEM) {
			if (!pdata->usb_bam_fifo_baseaddr) {
				pr_err("%s: base address is missing\n",
					__func__);
				goto err;
			}
		}

		rc = of_property_read_u32(node, "qcom,bam-type",
			&usb_bam_connections[i].bam_type);
		if (rc) {
			pr_err("%s: bam type is missing in device tree\n",
				__func__);
			goto err;
		}
		bam = usb_bam_connections[i].bam_type;

		rc = of_property_read_u32(node, "qcom,peer-bam",
			&usb_bam_connections[i].peer_bam);
		if (rc) {
			pr_err("%s: peer bam is missing in device tree\n",
				__func__);
			goto err;
		}
		rc = of_property_read_u32(node, "qcom,dir",
			&usb_bam_connections[i].dir);
		if (rc) {
			pr_err("%s: direction is missing in device tree\n",
				__func__);
			goto err;
		}

		rc = of_property_read_u32(node, "qcom,pipe-num",
			&usb_bam_connections[i].pipe_num);
		if (rc) {
			pr_err("%s: pipe num is missing in device tree\n",
				__func__);
			goto err;
		}

		reset_bam = of_property_read_bool(node,
			"qcom,reset-bam-on-connect");
		if (reset_bam)
			pdata->reset_on_connect[bam] = true;

		of_property_read_u32(node, "qcom,src-bam-physical-address",
			&usb_bam_connections[i].src_phy_addr);

		of_property_read_u32(node, "qcom,src-bam-pipe-index",
			&usb_bam_connections[i].src_pipe_index);

		of_property_read_u32(node, "qcom,dst-bam-physical-address",
			&usb_bam_connections[i].dst_phy_addr);

		of_property_read_u32(node, "qcom,dst-bam-pipe-index",
			&usb_bam_connections[i].dst_pipe_index);

		of_property_read_u32(node, "qcom,data-fifo-offset",
			&usb_bam_connections[i].data_fifo_base_offset);

		rc = of_property_read_u32(node, "qcom,data-fifo-size",
			&usb_bam_connections[i].data_fifo_size);
		if (rc)
			goto err;

		of_property_read_u32(node, "qcom,descriptor-fifo-offset",
			&usb_bam_connections[i].desc_fifo_base_offset);

		rc = of_property_read_u32(node, "qcom,descriptor-fifo-size",
			&usb_bam_connections[i].desc_fifo_size);
		if (rc)
			goto err;
		i++;
	}

	pdata->connections = usb_bam_connections;

	return pdata;
err:
	pr_err("%s: failed\n", __func__);
	return NULL;
}

static int usb_bam_init(int bam_idx)
{
	int ret, irq;
	void *usb_virt_addr;
	struct msm_usb_bam_platform_data *pdata =
		ctx.usb_bam_pdev->dev.platform_data;
	struct resource *res, *ram_resource;
	struct sps_bam_props props = ctx.usb_bam_sps.usb_props;

	pr_debug("%s: usb_bam_init - %s\n", __func__,
		bam_enable_strings[bam_idx]);
	res = platform_get_resource_byname(ctx.usb_bam_pdev, IORESOURCE_MEM,
		bam_enable_strings[bam_idx]);
	if (!res) {
		dev_dbg(&ctx.usb_bam_pdev->dev, "bam not initialized\n");
		return 0;
	}

	irq = platform_get_irq_byname(ctx.usb_bam_pdev,
		bam_enable_strings[bam_idx]);
	if (irq < 0) {
		dev_err(&ctx.usb_bam_pdev->dev, "Unable to get IRQ resource\n");
		return irq;
	}

	usb_virt_addr = devm_ioremap(&ctx.usb_bam_pdev->dev, res->start,
		resource_size(res));
	if (!usb_virt_addr) {
		pr_err("%s: ioremap failed\n", __func__);
		return -ENOMEM;
	}

	/* Check if USB3 pipe memory needs to be enabled */
	if (bam_idx == SSUSB_BAM && bam_use_private_mem(bam_idx)) {
		pr_debug("%s: Enabling USB private memory for: %s\n", __func__,
			bam_enable_strings[bam_idx]);

		ram_resource = platform_get_resource_byname(ctx.usb_bam_pdev,
			IORESOURCE_MEM, "qscratch_ram1_reg");
		if (!res) {
			dev_err(&ctx.usb_bam_pdev->dev, "Unable to get qscratch\n");
			ret = -ENODEV;
			goto free_bam_regs;
		}

		ctx.qscratch_ram1_reg = devm_ioremap(&ctx.usb_bam_pdev->dev,
			ram_resource->start,
			resource_size(ram_resource));
		if (!ctx.qscratch_ram1_reg) {
			pr_err("%s: ioremap failed for qscratch\n", __func__);
			ret = -ENOMEM;
			goto free_bam_regs;
		}
	}

	props.phys_addr = res->start;
	props.virt_addr = usb_virt_addr;
	props.virt_size = resource_size(res);
	props.irq = irq;
	props.summing_threshold = USB_THRESHOLD;
	props.event_threshold = USB_THRESHOLD;
	props.num_pipes = pdata->usb_bam_num_pipes;
	props.callback = usb_bam_sps_events;
	props.user = bam_enable_strings[bam_idx];
	props.options = SPS_BAM_OPT_IRQ_WAKEUP;

	/*
	* HSUSB and HSIC Cores don't support RESET ACK signal to BAMs
	* Hence, let BAM to ignore acknowledge from USB while resetting PIPE
	*/
	if (pdata->ignore_core_reset_ack && bam_idx != SSUSB_BAM)
		props.options = SPS_BAM_NO_EXT_P_RST;

	if (pdata->disable_clk_gating)
		props.options |= SPS_BAM_NO_LOCAL_CLK_GATING;

	ret = sps_register_bam_device(&props, &(ctx.h_bam[bam_idx]));
	if (ret < 0) {
		pr_err("%s: register bam error %d\n", __func__, ret);
		ret = -EFAULT;
		goto free_qscratch_reg;
	}

	return 0;

free_qscratch_reg:
	iounmap(ctx.qscratch_ram1_reg);
free_bam_regs:
	iounmap(usb_virt_addr);

	return ret;
}

static int enable_usb_bams(struct platform_device *pdev)
{
	int ret, i;

	for (i = 0; i < ARRAY_SIZE(bam_enable_strings); i++) {
		ret = usb_bam_init(i);
		if (ret) {
			pr_err("failed to init usb bam %s\n",
				bam_enable_strings[i]);
			return ret;
		}
	}

	ctx.usb_bam_sps.sps_pipes = devm_kzalloc(&pdev->dev,
		ctx.max_connections * sizeof(struct sps_pipe *),
		GFP_KERNEL);

	if (!ctx.usb_bam_sps.sps_pipes) {
		pr_err("%s: failed to allocate sps_pipes\n", __func__);
		return -ENOMEM;
	}

	ctx.usb_bam_sps.sps_connections = devm_kzalloc(&pdev->dev,
		ctx.max_connections * sizeof(struct sps_connect),
		GFP_KERNEL);
	if (!ctx.usb_bam_sps.sps_connections) {
		pr_err("%s: failed to allocate sps_connections\n", __func__);
		return -ENOMEM;
	}

	return 0;
}

static ssize_t
usb_bam_show_inactivity_timer(struct device *dev, struct device_attribute *attr,
		    char *buf)
{
	char *buff = buf;
	int i;

	spin_lock(&usb_bam_lock);

	for (i = 0; i < ARRAY_SIZE(bam_enable_strings); i++) {
		buff += snprintf(buff, PAGE_SIZE, "%s: %dms\n",
					bam_enable_strings[i],
					ctx.inactivity_timer_ms[i]);
	}

	spin_unlock(&usb_bam_lock);

	return buff - buf;
}

static ssize_t usb_bam_store_inactivity_timer(struct device *dev,
				     struct device_attribute *attr,
				     const char *buff, size_t count)
{
	char buf[USB_BAM_MAX_STR_LEN];
	char *trimmed_buf, *bam_str, *bam_name, *timer;
	int timer_d;
	enum usb_bam bam;

	if (strnstr(buff, "help", USB_BAM_MAX_STR_LEN)) {
		pr_info("Usage: <bam_name> <ms>,<bam_name> <ms>,...\n");
		pr_info("\tbam_name: [%s, %s, %s]\n",
			bam_enable_strings[SSUSB_BAM],
			bam_enable_strings[HSUSB_BAM],
			bam_enable_strings[HSIC_BAM]);
		pr_info("\tms: time in ms. Use 0 to disable timer\n");
		return count;
	}

	strlcpy(buf, buff, sizeof(buf));
	trimmed_buf = strim(buf);

	while (trimmed_buf) {
		bam_str = strsep(&trimmed_buf, ",");
		if (bam_str) {
			bam_name = strsep(&bam_str, " ");
			bam = get_bam_type_from_core_name(bam_name);

			timer = strsep(&bam_str, " ");
			sscanf(timer, "%d", &timer_d);

			spin_lock(&usb_bam_lock);

			/* Apply new timer setting if bam has running pipes */
			if (ctx.inactivity_timer_ms[bam] != timer_d) {
				ctx.inactivity_timer_ms[bam] = timer_d;
				if (ctx.pipes_enabled_per_bam[bam] > 0 &&
				    !info.in_lpm[bam])
					usb_bam_set_inactivity_timer(bam);
			}

			spin_unlock(&usb_bam_lock);
		}
	}

	return count;
}

static DEVICE_ATTR(inactivity_timer, S_IWUSR | S_IRUSR,
		   usb_bam_show_inactivity_timer,
		   usb_bam_store_inactivity_timer);


static int usb_bam_probe(struct platform_device *pdev)
{
	int ret, i;
	struct msm_usb_bam_platform_data *pdata;

	dev_dbg(&pdev->dev, "usb_bam_probe\n");

	ret = device_create_file(&pdev->dev, &dev_attr_inactivity_timer);
	if (ret) {
		dev_err(&pdev->dev, "failed to create fs node\n");
		return ret;
	}

	ctx.mem_clk = devm_clk_get(&pdev->dev, "mem_clk");
	if (IS_ERR(ctx.mem_clk))
		dev_dbg(&pdev->dev, "failed to get mem_clock\n");

	ctx.mem_iface_clk = devm_clk_get(&pdev->dev, "mem_iface_clk");
	if (IS_ERR(ctx.mem_iface_clk))
		dev_dbg(&pdev->dev, "failed to get mem_iface_clock\n");

	if (pdev->dev.of_node) {
		dev_dbg(&pdev->dev, "device tree enabled\n");
		pdata = usb_bam_dt_to_pdata(pdev);
		if (!pdata)
			return -EINVAL;
		pdev->dev.platform_data = pdata;
	} else if (!pdev->dev.platform_data) {
		dev_err(&pdev->dev, "missing platform_data\n");
		return -ENODEV;
	} else {
		pdata = pdev->dev.platform_data;
		usb_bam_connections = pdata->connections;
		ctx.max_connections = pdata->max_connections;
	}
	ctx.usb_bam_pdev = pdev;

	for (i = 0; i < ctx.max_connections; i++) {
		usb_bam_connections[i].enabled = 0;
		INIT_WORK(&usb_bam_connections[i].event.event_w,
			usb_bam_work);
	}

	for (i = 0; i < MAX_BAMS; i++) {
		ctx.pipes_enabled_per_bam[i] = 0;
		ctx.inactivity_timer_ms[i] = 0;
		ctx.is_bam_inactivity[i] = false;
		init_completion(&info.prod_avail[i]);
		complete(&info.prod_avail[i]);
		init_completion(&info.prod_released[i]);
		complete(&info.prod_released[i]);
		info.cur_prod_state[i] = IPA_RM_RESOURCE_RELEASED;
		info.cur_cons_state[i] = IPA_RM_RESOURCE_RELEASED;
		info.lpm_wait_handshake[i] = false;
	}

	INIT_WORK(&info.resume_work, usb_bam_finish_resume);
	INIT_WORK(&info.suspend_work, usb_bam_start_suspend);
	INIT_WORK(&info.finish_suspend_work, usb_bam_finish_suspend_);
	mutex_init(&info.suspend_resume_mutex);

	spin_lock_init(&usb_bam_peer_handshake_info_lock);
	INIT_WORK(&peer_handshake_info.reset_event.event_w, usb_bam_sm_work);
	init_completion(&ctx.reset_done);
	complete(&ctx.reset_done);

	ctx.usb_bam_wq = alloc_workqueue("usb_bam_wq",
		WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
	if (!ctx.usb_bam_wq) {
		pr_err("unable to create workqueue usb_bam_wq\n");
		return -ENOMEM;
	}

	ret = enable_usb_bams(pdev);
	if (ret) {
		destroy_workqueue(ctx.usb_bam_wq);
		return ret;
	}
	spin_lock_init(&usb_bam_ipa_handshake_info_lock);
	usb_bam_ipa_create_resources();
	spin_lock_init(&usb_bam_lock);

	return ret;
}

int usb_bam_get_qdss_idx(u8 num)
{
	return usb_bam_get_connection_idx(ctx.qdss_core_name, QDSS_P_BAM,
		PEER_PERIPHERAL_TO_USB, num);
}
EXPORT_SYMBOL(usb_bam_get_qdss_idx);

void usb_bam_set_qdss_core(const char *qdss_core)
{
	strlcpy(ctx.qdss_core_name, qdss_core, USB_BAM_MAX_STR_LEN);
}

int get_bam2bam_connection_info(u8 idx, u32 *usb_bam_handle,
	u32 *usb_bam_pipe_idx, u32 *peer_pipe_idx,
	struct sps_mem_buffer *desc_fifo, struct sps_mem_buffer *data_fifo)
{
	struct usb_bam_pipe_connect *pipe_connect = &usb_bam_connections[idx];
	enum usb_bam_pipe_dir dir = pipe_connect->dir;
	struct sps_connect *sps_connection =
		&ctx.usb_bam_sps.sps_connections[idx];

	if (dir == USB_TO_PEER_PERIPHERAL) {
		*usb_bam_handle = sps_connection->source;
		*usb_bam_pipe_idx = sps_connection->src_pipe_index;
		*peer_pipe_idx = sps_connection->dest_pipe_index;
	} else {
		*usb_bam_handle = sps_connection->destination;
		*usb_bam_pipe_idx = sps_connection->dest_pipe_index;
		*peer_pipe_idx = sps_connection->src_pipe_index;
	}
	if (data_fifo)
		memcpy(data_fifo, &pipe_connect->data_mem_buf,
		sizeof(struct sps_mem_buffer));
	if (desc_fifo)
		memcpy(desc_fifo, &pipe_connect->desc_mem_buf,
		sizeof(struct sps_mem_buffer));
	return 0;
}
EXPORT_SYMBOL(get_bam2bam_connection_info);


int usb_bam_get_connection_idx(const char *core_name, enum peer_bam client,
	enum usb_bam_pipe_dir dir, u32 num)
{
	u8 i;
	int bam_type;

	bam_type = get_bam_type_from_core_name(core_name);
	if (bam_type < 0)
		return -EINVAL;

	for (i = 0; i < ctx.max_connections; i++)
		if (usb_bam_connections[i].bam_type == bam_type &&
				usb_bam_connections[i].peer_bam == client &&
				usb_bam_connections[i].dir == dir &&
				usb_bam_connections[i].pipe_num == num) {
			pr_debug("%s: index %d was found\n", __func__, i);
			return i;
		}

	pr_err("%s: failed for %s\n", __func__, core_name);
	return -ENODEV;
}
EXPORT_SYMBOL(usb_bam_get_connection_idx);

bool msm_bam_lpm_ok(void)
{
	spin_lock(&usb_bam_ipa_handshake_info_lock);
	if (info.lpm_wait_handshake[HSUSB_BAM] || info.lpm_wait_pipes) {
		info.pending_lpm = 1;
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		pr_err("%s: Scheduling LPM for later\n", __func__);
		return 0;
	} else {
		info.pending_lpm = 0;
		info.in_lpm[HSUSB_BAM] = true;
		spin_unlock(&usb_bam_ipa_handshake_info_lock);
		pr_err("%s: Going to LPM now\n", __func__);
		return 1;
	}
}
EXPORT_SYMBOL(msm_bam_lpm_ok);

void msm_bam_notify_lpm_resume()
{
	/*
	 * If core was resumed from lpm, just clear the
	 * pending indication, in case it is set.
	*/
	info.pending_lpm = 0;
}
EXPORT_SYMBOL(msm_bam_notify_lpm_resume);

static int usb_bam_remove(struct platform_device *pdev)
{
	destroy_workqueue(ctx.usb_bam_wq);

	return 0;
}

static const struct of_device_id usb_bam_dt_match[] = {
	{ .compatible = "qcom,usb-bam-msm",
	},
	{}
};
MODULE_DEVICE_TABLE(of, usb_bam_dt_match);

static struct platform_driver usb_bam_driver = {
	.probe = usb_bam_probe,
	.remove = usb_bam_remove,
	.driver		= {
		.name	= "usb_bam",
		.of_match_table = usb_bam_dt_match,
	},
};

static int __init init(void)
{
	return platform_driver_register(&usb_bam_driver);
}
module_init(init);

static void __exit cleanup(void)
{
	platform_driver_unregister(&usb_bam_driver);
}
module_exit(cleanup);

MODULE_DESCRIPTION("MSM USB BAM DRIVER");
MODULE_LICENSE("GPL v2");