summaryrefslogtreecommitdiff
path: root/dsp/q6adm.c
blob: e63547d85738235b5a95fb9fc02a6851f134f3d1 (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
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
 */
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/jiffies.h>
#include <linux/uaccess.h>
#include <linux/atomic.h>
#include <sound/asound.h>
#include <dsp/msm-dts-srs-tm-config.h>
#include <dsp/apr_audio-v2.h>
#include <dsp/q6adm-v2.h>
#include <dsp/q6audio-v2.h>
#include <dsp/q6afe-v2.h>
#include <dsp/q6core.h>
#include <dsp/audio_cal_utils.h>
#include <dsp/q6common.h>
#include <ipc/apr.h>
#include "adsp_err.h"

#define TIMEOUT_MS 1000

#define RESET_COPP_ID 99
#define INVALID_COPP_ID 0xFF
/* Used for inband payload copy, max size is 4k */
/* 3 is to account for module, instance & param ID in payload */
#define ADM_GET_PARAMETER_LENGTH (4096 - APR_HDR_SIZE - 3 * sizeof(uint32_t))

#define ULL_SUPPORTED_BITS_PER_SAMPLE 16
#define ULL_SUPPORTED_SAMPLE_RATE 48000

#ifndef CONFIG_DOLBY_DAP
#undef DOLBY_ADM_COPP_TOPOLOGY_ID
#define DOLBY_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFE
#endif

#ifndef CONFIG_DOLBY_DS2
#undef DS2_ADM_COPP_TOPOLOGY_ID
#define DS2_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFF
#endif

#define SESSION_TYPE_RX 0

/* ENUM for adm_status */
enum adm_cal_status {
	ADM_STATUS_CALIBRATION_REQUIRED = 0,
	ADM_STATUS_MAX,
};

struct adm_copp {

	atomic_t id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t cnt[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t topology[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t mode[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t rate[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t bit_width[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t channels[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t app_type[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t acdb_id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t session_type[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	wait_queue_head_t wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	wait_queue_head_t adm_delay_wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	atomic_t adm_delay_stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	uint32_t adm_delay[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
	unsigned long adm_status[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
};

struct source_tracking_data {
	struct dma_buf *dma_buf;
	struct param_outband memmap;
	int apr_cmd_status;
};

struct adm_ctl {
	void *apr;

	struct adm_copp copp;

	atomic_t matrix_map_stat;
	wait_queue_head_t matrix_map_wait;

	atomic_t adm_stat;
	wait_queue_head_t adm_wait;

	struct cal_type_data *cal_data[ADM_MAX_CAL_TYPES];

	atomic_t mem_map_handles[ADM_MEM_MAP_INDEX_MAX];
	atomic_t mem_map_index;

	struct param_outband outband_memmap;
	struct source_tracking_data sourceTrackingData;

	struct mutex adm_apr_lock;
	int set_custom_topology;
	int ec_ref_rx;
	int num_ec_ref_rx_chans;
	int ec_ref_rx_bit_width;
	int ec_ref_rx_sampling_rate;
	int num_ec_ref_rx_chans_downmixed;
	uint16_t ec_ref_chmixer_weights[PCM_FORMAT_MAX_NUM_CHANNEL_V8]
						[PCM_FORMAT_MAX_NUM_CHANNEL_V8];
	int ffecns_port_id;
	int native_mode;
};

static struct adm_ctl			this_adm;

struct adm_multi_ch_map {
	bool set_channel_map;
	char channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL_V8];
};

#define ADM_MCH_MAP_IDX_PLAYBACK 0
#define ADM_MCH_MAP_IDX_REC 1
static struct adm_multi_ch_map multi_ch_maps[2] = {
			{ false,
			{0, 0, 0, 0, 0, 0, 0, 0,
			 0, 0, 0, 0, 0, 0, 0, 0,
			 0, 0, 0, 0, 0, 0, 0, 0,
			 0, 0, 0, 0, 0, 0, 0, 0}
			},
			{ false,
			{0, 0, 0, 0, 0, 0, 0, 0,
			 0, 0, 0, 0, 0, 0, 0, 0,
			 0, 0, 0, 0, 0, 0, 0, 0,
			 0, 0, 0, 0, 0, 0, 0, 0}
			}
};

static struct adm_multi_ch_map port_channel_map[AFE_MAX_PORTS];

static int adm_get_parameters[MAX_COPPS_PER_PORT * ADM_GET_PARAMETER_LENGTH];
static int adm_module_topo_list[MAX_COPPS_PER_PORT *
				ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH];
static struct mutex dts_srs_lock;

void msm_dts_srs_acquire_lock(void)
{
	mutex_lock(&dts_srs_lock);
}

void msm_dts_srs_release_lock(void)
{
	mutex_unlock(&dts_srs_lock);
}

static int adm_arrange_mch_map_v8(
		struct adm_device_endpoint_payload *ep_payload,
		int path,
		int channel_mode,
		int port_idx);

/**
 * adm_validate_and_get_port_index -
 *        validate given port id
 *
 * @port_id: Port ID number
 *
 * Returns valid index on success or error on failure
 */
int adm_validate_and_get_port_index(int port_id)
{
	int index;
	int ret;

	ret = q6audio_validate_port(port_id);
	if (ret < 0) {
		pr_err("%s: port validation failed id 0x%x ret %d\n",
			__func__, port_id, ret);
		return -EINVAL;
	}

	index = afe_get_port_index(port_id);
	if (index < 0 || index >= AFE_MAX_PORTS) {
		pr_err("%s: Invalid port idx %d port_id 0x%x\n",
			__func__, index,
			port_id);
		return -EINVAL;
	}
	pr_debug("%s: port_idx- %d\n", __func__, index);
	return index;
}
EXPORT_SYMBOL(adm_validate_and_get_port_index);

/**
 * adm_get_default_copp_idx -
 *        retrieve default copp_idx for given port
 *
 * @port_id: Port ID number
 *
 * Returns valid value on success or error on failure
 */
int adm_get_default_copp_idx(int port_id)
{
	int port_idx = adm_validate_and_get_port_index(port_id), idx;

	if (port_idx < 0) {
		pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
		return -EINVAL;
	}
	pr_debug("%s: port_idx:%d\n", __func__, port_idx);
	for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
		if (atomic_read(&this_adm.copp.id[port_idx][idx]) !=
			RESET_COPP_ID)
			return idx;
	}
	return -EINVAL;
}
EXPORT_SYMBOL(adm_get_default_copp_idx);

int adm_get_topology_for_port_from_copp_id(int port_id, int copp_id)
{
	int port_idx = adm_validate_and_get_port_index(port_id), idx;

	if (port_idx < 0) {
		pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
		return 0;
	}
	for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
		if (atomic_read(&this_adm.copp.id[port_idx][idx]) == copp_id)
			return atomic_read(&this_adm.copp.topology[port_idx]
								  [idx]);
	pr_err("%s: Invalid copp_id %d port_id 0x%x\n",
		__func__, copp_id, port_id);
	return 0;
}

/**
 * adm_get_topology_for_port_copp_idx -
 *        retrieve topology of given port/copp_idx
 *
 * @port_id: Port ID number
 * @copp_idx: copp index of ADM copp
 *
 * Returns valid value on success or 0 on failure
 */
int adm_get_topology_for_port_copp_idx(int port_id, int copp_idx)
{
	int port_idx = adm_validate_and_get_port_index(port_id);

	if (port_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid port: 0x%x copp id: 0x%x",
				__func__, port_id, copp_idx);
		return 0;
	}
	return atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
}
EXPORT_SYMBOL(adm_get_topology_for_port_copp_idx);

int adm_get_indexes_from_copp_id(int copp_id, int *copp_idx, int *port_idx)
{
	int p_idx, c_idx;

	for (p_idx = 0; p_idx < AFE_MAX_PORTS; p_idx++) {
		for (c_idx = 0; c_idx < MAX_COPPS_PER_PORT; c_idx++) {
			if (atomic_read(&this_adm.copp.id[p_idx][c_idx])
								== copp_id) {
				if (copp_idx != NULL)
					*copp_idx = c_idx;
				if (port_idx != NULL)
					*port_idx = p_idx;
				return 0;
			}
		}
	}
	return -EINVAL;
}

static int adm_get_copp_id(int port_idx, int copp_idx)
{
	pr_debug("%s: port_idx:%d copp_idx:%d\n", __func__, port_idx, copp_idx);

	if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
		return -EINVAL;
	}
	return atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
}

static int adm_get_idx_if_copp_exists(int port_idx, int topology, int mode,
				 int rate, int bit_width, int app_type,
				 int session_type)
{
	int idx;

	pr_debug("%s: port_idx-%d, topology-0x%x, mode-%d, rate-%d, bit_width-%d\n",
		 __func__, port_idx, topology, mode, rate, bit_width);

	for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
		if ((topology ==
			atomic_read(&this_adm.copp.topology[port_idx][idx])) &&
		    (mode == atomic_read(&this_adm.copp.mode[port_idx][idx])) &&
		    (rate == atomic_read(&this_adm.copp.rate[port_idx][idx])) &&
		    (bit_width ==
			atomic_read(&this_adm.copp.bit_width[port_idx][idx])) &&
		    (session_type ==
			atomic_read(
				&this_adm.copp.session_type[port_idx][idx])) &&
		    (app_type ==
			atomic_read(&this_adm.copp.app_type[port_idx][idx])))
			return idx;
	return -EINVAL;
}

static int adm_get_next_available_copp(int port_idx)
{
	int idx;

	pr_debug("%s:\n", __func__);
	for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
		pr_debug("%s: copp_id:0x%x port_idx:%d idx:%d\n", __func__,
			 atomic_read(&this_adm.copp.id[port_idx][idx]),
			 port_idx, idx);
		if (atomic_read(&this_adm.copp.id[port_idx][idx]) ==
								RESET_COPP_ID)
			break;
	}
	return idx;
}

/**
 * srs_trumedia_open -
 *        command to set SRS trumedia open
 *
 * @port_id: Port ID number
 * @copp_idx: copp index of ADM copp
 * @srs_tech_id: SRS tech index
 * @srs_params: params pointer
 *
 * Returns 0 on success or error on failure
 */
int srs_trumedia_open(int port_id, int copp_idx, __s32 srs_tech_id,
		      void *srs_params)
{
	struct param_hdr_v3 param_hdr;
	struct mem_mapping_hdr mem_hdr;
	u32 total_param_size = 0;
	bool outband = false;
	int port_idx;
	int ret = 0;

	pr_debug("SRS - %s", __func__);

	memset(&param_hdr, 0, sizeof(param_hdr));
	memset(&mem_hdr, 0, sizeof(mem_hdr));
	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
		return -EINVAL;
	}

	param_hdr.module_id = SRS_TRUMEDIA_MODULE_ID;
	param_hdr.instance_id = INSTANCE_ID_0;

	switch (srs_tech_id) {
	case SRS_ID_GLOBAL: {
		param_hdr.param_id = SRS_TRUMEDIA_PARAMS;
		param_hdr.param_size =
			sizeof(struct srs_trumedia_params_GLOBAL);
		break;
	}
	case SRS_ID_WOWHD: {
		param_hdr.param_id = SRS_TRUMEDIA_PARAMS_WOWHD;
		param_hdr.param_size = sizeof(struct srs_trumedia_params_WOWHD);
		break;
	}
	case SRS_ID_CSHP: {
		param_hdr.param_id = SRS_TRUMEDIA_PARAMS_CSHP;
		param_hdr.param_size = sizeof(struct srs_trumedia_params_CSHP);
		break;
	}
	case SRS_ID_HPF: {
		param_hdr.param_id = SRS_TRUMEDIA_PARAMS_HPF;
		param_hdr.param_size = sizeof(struct srs_trumedia_params_HPF);
		break;
	}
	case SRS_ID_AEQ: {
		u8 *update_params_ptr = (u8 *) this_adm.outband_memmap.kvaddr;

		outband = true;

		if (update_params_ptr == NULL) {
			pr_err("ADM_SRS_TRUMEDIA - %s: null memmap for AEQ params\n",
				__func__);
			ret = -EINVAL;
			goto fail_cmd;
		}

		param_hdr.param_id = SRS_TRUMEDIA_PARAMS_AEQ;
		param_hdr.param_size = sizeof(struct srs_trumedia_params_AEQ);

		ret = q6common_pack_pp_params(update_params_ptr, &param_hdr,
					      srs_params, &total_param_size);
		if (ret) {
			pr_err("%s: Failed to pack param header and data, error %d\n",
			       __func__, ret);
			goto fail_cmd;
		}
		break;
	}
	case SRS_ID_HL: {
		param_hdr.param_id = SRS_TRUMEDIA_PARAMS_HL;
		param_hdr.param_size = sizeof(struct srs_trumedia_params_HL);
		break;
	}
	case SRS_ID_GEQ: {
		param_hdr.param_id = SRS_TRUMEDIA_PARAMS_GEQ;
		param_hdr.param_size = sizeof(struct srs_trumedia_params_GEQ);
		break;
	}
	default:
		goto fail_cmd;
	}

	if (outband && this_adm.outband_memmap.paddr) {
		mem_hdr.data_payload_addr_lsw =
			lower_32_bits(this_adm.outband_memmap.paddr);
		mem_hdr.data_payload_addr_msw =
			msm_audio_populate_upper_32_bits(
				this_adm.outband_memmap.paddr);
		mem_hdr.mem_map_handle = atomic_read(
			&this_adm.mem_map_handles[ADM_SRS_TRUMEDIA]);

		ret = adm_set_pp_params(port_id, copp_idx, &mem_hdr, NULL,
					total_param_size);
	} else {
		ret = adm_pack_and_set_one_pp_param(port_id, copp_idx,
						    param_hdr,
						    (u8 *) srs_params);
	}

	if (ret < 0)
		pr_err("SRS - %s: ADM enable for port %d failed\n", __func__,
			port_id);

fail_cmd:
	return ret;
}
EXPORT_SYMBOL(srs_trumedia_open);

static int adm_populate_channel_weight(u16 *ptr,
					struct msm_pcm_channel_mixer *ch_mixer,
					int channel_index)
{
	u16 i, j, start_index = 0;

	if (channel_index > ch_mixer->output_channel) {
		pr_err("%s: channel index %d is larger than output_channel %d\n",
			 __func__, channel_index, ch_mixer->output_channel);
		return -EINVAL;
	}

	for (i = 0; i < ch_mixer->output_channel; i++) {
		pr_debug("%s: weight for output %d:", __func__, i);
		for (j = 0; j < ADM_MAX_CHANNELS; j++)
			pr_debug(" %d",
				ch_mixer->channel_weight[i][j]);
		pr_debug("\n");
	}

	for (i = 0; i < channel_index; ++i)
		start_index += ch_mixer->input_channels[i];

	for (i = 0; i < ch_mixer->output_channel; ++i) {
		for (j = start_index;
			j < start_index +
			ch_mixer->input_channels[channel_index]; j++) {
			*ptr = ch_mixer->channel_weight[i][j];
			 pr_debug("%s: ptr[%d][%d] = %d\n",
				__func__, i, j, *ptr);
			 ptr++;
		}
	}

	return 0;
}

/*
 * adm_programable_channel_mixer
 *
 * Receives port_id, copp_idx, session_id, session_type, ch_mixer
 * and channel_index to send ADM command to mix COPP data.
 *
 * port_id - Passed value, port_id for which backend is wanted
 * copp_idx - Passed value, copp_idx for which COPP is wanted
 * session_id - Passed value, session_id for which session is needed
 * session_type - Passed value, session_type for RX or TX
 * ch_mixer - Passed value, ch_mixer for which channel mixer config is needed
 * channel_index - Passed value, channel_index for which channel is needed
 */
int adm_programable_channel_mixer(int port_id, int copp_idx, int session_id,
				  int session_type,
				  struct msm_pcm_channel_mixer *ch_mixer,
				  int channel_index)
{
	struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
	struct param_hdr_v1 data_v5;
	int ret = 0, port_idx, sz = 0, param_size = 0;
	struct adm_device_endpoint_payload ep_params = {0, 0, 0, {0}};
	u16 *adm_pspd_params;
	u16 *ptr;
	int index = 0, i = 0, path_type = ADM_PATH_PLAYBACK;

	pr_debug("%s: port_id = %d\n", __func__, port_id);
	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
		return -EINVAL;
	}

	/*
	 * check if PSPD is already configured
	 * if it is configured already, return 0 without applying PSPD.
	 */
	if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) > 1) {
		pr_debug("%s: copp.cnt:%#x\n", __func__,
			atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]));
		return 0;
	}

	/*
	 * First 8 bytes are 4 bytes as rule number, 2 bytes as output
	 * channel and 2 bytes as input channel.
	 * 2 * ch_mixer->output_channel means output channel mapping.
	 * 2 * ch_mixer->input_channels[channel_index]) means input
	 * channel mapping.
	 * 2 * ch_mixer->input_channels[channel_index] *
	 * ch_mixer->output_channel) means the channel mixer weighting
	 * coefficients.
	 * param_size needs to be a multiple of 4 bytes.
	 */

	param_size = 2 * (4 + ch_mixer->output_channel +
			ch_mixer->input_channels[channel_index] +
			ch_mixer->input_channels[channel_index] *
			ch_mixer->output_channel);
	param_size = roundup(param_size, 4);

	sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
	     sizeof(struct default_chmixer_param_id_coeff) +
	     sizeof(struct param_hdr_v1) + param_size;
	pr_debug("%s: sz = %d\n", __func__, sz);
	adm_params = kzalloc(sz, GFP_KERNEL);
	if (!adm_params)
		return -ENOMEM;

	adm_params->payload_addr_lsw = 0;
	adm_params->payload_addr_msw = 0;
	adm_params->mem_map_handle = 0;
	adm_params->direction = session_type;
	adm_params->sessionid = session_id;
	pr_debug("%s: copp_id = %d, session id  %d\n", __func__,
		atomic_read(&this_adm.copp.id[port_idx][copp_idx]),
			session_id);
	adm_params->deviceid = atomic_read(
				&this_adm.copp.id[port_idx][copp_idx]);
	adm_params->reserved = 0;

	/*
	 * This module is internal to ADSP and cannot be configured with
	 * an instance id
	 */
	data_v5.module_id = MTMX_MODULE_ID_DEFAULT_CHMIXER;
	data_v5.param_id =  DEFAULT_CHMIXER_PARAM_ID_COEFF;
	data_v5.reserved = 0;
	data_v5.param_size = param_size;
	adm_params->payload_size =
		sizeof(struct default_chmixer_param_id_coeff) +
		sizeof(struct param_hdr_v1) + data_v5.param_size;
	adm_pspd_params = (u16 *)((u8 *)adm_params +
			sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5));
	memcpy(adm_pspd_params, &data_v5, sizeof(data_v5));

	adm_pspd_params = (u16 *)((u8 *)adm_params +
			sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)
			+ sizeof(data_v5));

	adm_pspd_params[0] = ch_mixer->rule;
	adm_pspd_params[2] = ch_mixer->output_channel;
	adm_pspd_params[3] = ch_mixer->input_channels[channel_index];
	index = 4;

	path_type = (session_type == SESSION_TYPE_RX) ?
				ADM_PATH_PLAYBACK : ADM_PATH_LIVE_REC;

	if (ch_mixer->override_out_ch_map) {
		memcpy(&adm_pspd_params[index], &ch_mixer->out_ch_map,
			ch_mixer->output_channel * sizeof(uint16_t));
		index += ch_mixer->output_channel;
	} else {
		ep_params.dev_num_channel = ch_mixer->output_channel;
		adm_arrange_mch_map_v8(&ep_params, path_type,
				       ep_params.dev_num_channel, port_idx);
		for (i = 0; i < ch_mixer->output_channel; i++)
			adm_pspd_params[index++] = ep_params.dev_channel_mapping[i];
	}

	if (ch_mixer->override_in_ch_map) {
		memcpy(&adm_pspd_params[index], &ch_mixer->in_ch_map,
			ch_mixer->input_channel * sizeof(uint16_t));
		index += ch_mixer->input_channel;
	} else {
		ep_params.dev_num_channel = ch_mixer->input_channels[channel_index];
		adm_arrange_mch_map_v8(&ep_params, path_type,
				       ep_params.dev_num_channel, port_idx);
		for (i = 0; i < ch_mixer->input_channels[channel_index]; i++)
			adm_pspd_params[index++] = ep_params.dev_channel_mapping[i];
	}

	ret = adm_populate_channel_weight(&adm_pspd_params[index],
					ch_mixer, channel_index);
	if (ret) {
		pr_err("%s: fail to get channel weight with error %d\n",
			__func__, ret);
		goto fail_cmd;
	}

	adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
				APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
	adm_params->hdr.src_svc = APR_SVC_ADM;
	adm_params->hdr.src_domain = APR_DOMAIN_APPS;
	adm_params->hdr.src_port = port_id;
	adm_params->hdr.dest_svc = APR_SVC_ADM;
	adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
	adm_params->hdr.dest_port =
			atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
	adm_params->hdr.token = port_idx << 16 | copp_idx;
	adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
	adm_params->hdr.pkt_size = sz;
	adm_params->payload_addr_lsw = 0;
	adm_params->payload_addr_msw = 0;
	adm_params->mem_map_handle = 0;
	adm_params->reserved = 0;

	ptr = (u16 *)adm_params;
	for (index = 0; index < (sz / 2); index++)
		pr_debug("%s: adm_params[%d] = 0x%x\n",
			__func__, index, (unsigned int)ptr[index]);

	atomic_set(&this_adm.copp.stat[port_idx][copp_idx], 0);
	ret = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
	if (ret < 0) {
		pr_err("%s: Set params failed port %d rc %d\n", __func__,
			port_id, ret);
		ret = -EINVAL;
		goto fail_cmd;
	}

	ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
			atomic_read(
			&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
			msecs_to_jiffies(TIMEOUT_MS));
	if (!ret) {
		pr_err("%s: set params timed out port = %d\n",
			__func__, port_id);
		ret = -ETIMEDOUT;
		goto fail_cmd;
	}
	ret = 0;
fail_cmd:
	kfree(adm_params);

	return ret;
}
EXPORT_SYMBOL(adm_programable_channel_mixer);

/**
 * adm_set_stereo_to_custom_stereo -
 *        command to update custom stereo
 *
 * @port_id: Port ID number
 * @copp_idx: copp index of ADM copp
 * @session_id: session id to be updated
 * @params: params pointer
 * @param_length: length of params
 *
 * Returns 0 on success or error on failure
 */
int adm_set_stereo_to_custom_stereo(int port_id, int copp_idx,
				    unsigned int session_id, char *params,
				    uint32_t params_length)
{
	struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
	int sz, rc = 0, port_idx;

	pr_debug("%s:\n", __func__);
	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
		return -EINVAL;
	}

	sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
		params_length;
	adm_params = kzalloc(sz, GFP_KERNEL);
	if (!adm_params) {
		pr_err("%s, adm params memory alloc failed\n", __func__);
		return -ENOMEM;
	}

	memcpy(((u8 *)adm_params +
		sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)),
		params, params_length);
	adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
					APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
	adm_params->hdr.pkt_size = sz;
	adm_params->hdr.src_svc = APR_SVC_ADM;
	adm_params->hdr.src_domain = APR_DOMAIN_APPS;
	adm_params->hdr.src_port = port_id;
	adm_params->hdr.dest_svc = APR_SVC_ADM;
	adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
	adm_params->hdr.dest_port = 0; /* Ignored */;
	adm_params->hdr.token = port_idx << 16 | copp_idx;
	adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
	adm_params->payload_addr_lsw = 0;
	adm_params->payload_addr_msw = 0;
	adm_params->mem_map_handle = 0;
	adm_params->payload_size = params_length;
	/* direction RX as 0 */
	adm_params->direction = ADM_MATRIX_ID_AUDIO_RX;
	/* session id for this cmd to be applied on */
	adm_params->sessionid = session_id;
	adm_params->deviceid =
			atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
	adm_params->reserved = 0;
	pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
		__func__, adm_params->deviceid, adm_params->sessionid,
		adm_params->hdr.src_port, adm_params->hdr.dest_port);
	atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
	rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
	if (rc < 0) {
		pr_err("%s: Set params failed port = 0x%x rc %d\n",
			__func__, port_id, rc);
		rc = -EINVAL;
		goto set_stereo_to_custom_stereo_return;
	}
	/* Wait for the callback */
	rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
				atomic_read(&this_adm.copp.stat
				[port_idx][copp_idx]) >= 0,
				msecs_to_jiffies(TIMEOUT_MS));
	if (!rc) {
		pr_err("%s: Set params timed out port = 0x%x\n", __func__,
			port_id);
		rc = -EINVAL;
		goto set_stereo_to_custom_stereo_return;
	} else if (atomic_read(&this_adm.copp.stat
				[port_idx][copp_idx]) > 0) {
		pr_err("%s: DSP returned error[%s]\n", __func__,
			adsp_err_get_err_str(atomic_read(
			&this_adm.copp.stat
			[port_idx][copp_idx])));
		rc = adsp_err_get_lnx_err_code(
				atomic_read(&this_adm.copp.stat
					[port_idx][copp_idx]));
		goto set_stereo_to_custom_stereo_return;
	}
	rc = 0;
set_stereo_to_custom_stereo_return:
	kfree(adm_params);
	return rc;
}
EXPORT_SYMBOL(adm_set_stereo_to_custom_stereo);

/*
 * adm_set_custom_chmix_cfg:
 *	Set the custom channel mixer configuration for ADM
 *
 * @port_id: Backend port id
 * @copp_idx: ADM copp index
 * @session_id: ID of the requesting session
 * @params: Expected packaged params for channel mixer
 * @params_length: Length of the params to be set
 * @direction: RX or TX direction
 * @stream_type: Audio or Listen stream type
 */
int adm_set_custom_chmix_cfg(int port_id, int copp_idx,
			     unsigned int session_id, char *params,
			     uint32_t params_length, int direction,
			     int stream_type)
{
	struct adm_cmd_set_pspd_mtmx_strtr_params_v6 *adm_params = NULL;
	int sz, rc = 0, port_idx;

	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
		return -EINVAL;
	}

	sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v6) +
		params_length;
	adm_params = kzalloc(sz, GFP_KERNEL);
	if (!adm_params) {
		pr_err("%s, adm params memory alloc failed\n", __func__);
		return -ENOMEM;
	}

	memcpy(((u8 *)adm_params +
		sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v6)),
		params, params_length);
	adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
					APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
	adm_params->hdr.pkt_size = sz;
	adm_params->hdr.src_svc = APR_SVC_ADM;
	adm_params->hdr.src_domain = APR_DOMAIN_APPS;
	adm_params->hdr.src_port = port_id;
	adm_params->hdr.dest_svc = APR_SVC_ADM;
	adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
	adm_params->hdr.dest_port = 0; /* Ignored */;
	adm_params->hdr.token = port_idx << 16 | copp_idx;
	adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V6;
	adm_params->payload_addr_lsw = 0;
	adm_params->payload_addr_msw = 0;
	adm_params->mem_map_handle = 0;
	adm_params->payload_size = params_length;
	adm_params->direction = direction;
	/* session id for this cmd to be applied on */
	adm_params->sessionid = session_id;
	adm_params->deviceid =
			atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
	/* connecting stream type i.e. lsm or asm */
	adm_params->stream_type = stream_type;
	pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
		__func__, adm_params->deviceid, adm_params->sessionid,
		adm_params->hdr.src_port, adm_params->hdr.dest_port);
	atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
	rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
	if (rc < 0) {
		pr_err("%s: Set params failed port = 0x%x rc %d\n",
			__func__, port_id, rc);
		rc = -EINVAL;
		goto exit;
	}
	/* Wait for the callback */
	rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
				atomic_read(&this_adm.copp.stat
				[port_idx][copp_idx]),
				msecs_to_jiffies(TIMEOUT_MS));
	if (!rc) {
		pr_err("%s: Set params timed out port = 0x%x\n", __func__,
			port_id);
		rc = -EINVAL;
		goto exit;
	} else if (atomic_read(&this_adm.copp.stat
				[port_idx][copp_idx]) > 0) {
		pr_err("%s: DSP returned error[%s]\n", __func__,
			adsp_err_get_err_str(atomic_read(
				&this_adm.copp.stat
				[port_idx][copp_idx])));
		rc = adsp_err_get_lnx_err_code(
				atomic_read(&this_adm.copp.stat
					[port_idx][copp_idx]));
		goto exit;
	}

	rc = 0;
exit:
	kfree(adm_params);
	return rc;
}
EXPORT_SYMBOL(adm_set_custom_chmix_cfg);

/*
 * adm_apr_send_pkt : returns 0 on success, negative otherwise.
 */
int adm_apr_send_pkt(void *data, wait_queue_head_t *wait,
			int port_idx, int copp_idx, int opcode)
{
	int ret = 0;
	atomic_t *copp_stat = NULL;
	int32_t time_out = msecs_to_jiffies(TIMEOUT_MS);
	wait = &this_adm.copp.wait[port_idx][copp_idx];

	if (!wait)
		return -EINVAL;

	mutex_lock(&this_adm.adm_apr_lock);
	pr_debug("%s: port idx  %d copp idx  %d\n", __func__,
				port_idx, copp_idx);
	copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
	atomic_set(copp_stat, -1);

	if (opcode != ADM_CMD_DEVICE_OPEN_V8 &&
		opcode != ADM_CMD_DEVICE_OPEN_V6 &&
		opcode != ADM_CMD_DEVICE_OPEN_V5 &&
		opcode != ADM_CMD_DEVICE_CLOSE_V5) {
		if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx])
			== 0) {
			pr_err("%s: port[0x%x] copp[0x%x] inactive\n",
				__func__, port_idx, copp_idx);
			mutex_unlock(&this_adm.adm_apr_lock);
			return -EINVAL;
		}
	}

	if (opcode == ADM_CMD_DEVICE_OPEN_V8 ||
		opcode == ADM_CMD_DEVICE_OPEN_V6 ||
		opcode == ADM_CMD_DEVICE_OPEN_V5) {
		time_out = msecs_to_jiffies(2 * TIMEOUT_MS);
	}

	ret = apr_send_pkt(this_adm.apr, data);
	if (ret > 0) {
		ret = wait_event_timeout(*wait,
			atomic_read(copp_stat) >= 0,
			time_out);
		if (atomic_read(copp_stat) > 0) {
			pr_err("%s: DSP returned error[%s]\n", __func__,
				adsp_err_get_err_str(atomic_read(copp_stat)));
			ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
		} else	if (!ret) {
			pr_err_ratelimited("%s: request timedout\n",
				__func__);
			ret = -ETIMEDOUT;
		} else {
			ret = 0;
		}
	} else if (ret == 0) {
		pr_err("%s: packet not transmitted\n", __func__);
		/* apr_send_pkt can return 0 when nothing is transmitted */
		ret = -EINVAL;
	}

	mutex_unlock(&this_adm.adm_apr_lock);
	return ret;
}

/*
 * With pre-packed data, only the opcode differes from V5 and V6.
 * Use q6common_pack_pp_params to pack the data correctly.
 */
int adm_set_pp_params(int port_id, int copp_idx,
		      struct mem_mapping_hdr *mem_hdr, u8 *param_data,
		      u32 param_size)
{
	struct adm_cmd_set_pp_params *adm_set_params = NULL;
	int size = 0;
	int port_idx = 0;
	int ret = 0;

	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
		pr_err("%s: Invalid port_idx 0x%x\n", __func__, port_idx);
		return -EINVAL;
	} else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
		return -EINVAL;
	}

	/* Only add params_size in inband case */
	size = sizeof(struct adm_cmd_set_pp_params);
	if (param_data != NULL)
		size += param_size;
	adm_set_params = kzalloc(size, GFP_KERNEL);
	if (!adm_set_params)
		return -ENOMEM;

	adm_set_params->apr_hdr.hdr_field =
		APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
			      APR_PKT_VER);
	adm_set_params->apr_hdr.pkt_size = size;
	adm_set_params->apr_hdr.src_svc = APR_SVC_ADM;
	adm_set_params->apr_hdr.src_domain = APR_DOMAIN_APPS;
	adm_set_params->apr_hdr.src_port = port_id;
	adm_set_params->apr_hdr.dest_svc = APR_SVC_ADM;
	adm_set_params->apr_hdr.dest_domain = APR_DOMAIN_ADSP;
	adm_set_params->apr_hdr.dest_port =
		atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
	adm_set_params->apr_hdr.token = port_idx << 16 | copp_idx;

	if (q6common_is_instance_id_supported())
		adm_set_params->apr_hdr.opcode = ADM_CMD_SET_PP_PARAMS_V6;
	else
		adm_set_params->apr_hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;

	adm_set_params->payload_size = param_size;

	if (mem_hdr != NULL) {
		/* Out of Band Case */
		adm_set_params->mem_hdr = *mem_hdr;
	} else if (param_data != NULL) {
		/*
		 * In band case. Parameter data must be pre-packed with its
		 * header before calling this function. Use
		 * q6common_pack_pp_params to pack parameter data and header
		 * correctly.
		 */
		memcpy(&adm_set_params->param_data, param_data, param_size);
	} else {
		pr_err("%s: Received NULL pointers for both memory header and param data\n",
		       __func__);
		ret = -EINVAL;
		goto done;
	}
	ret = adm_apr_send_pkt((uint32_t *) adm_set_params,
			&this_adm.copp.wait[port_idx][copp_idx],
			port_idx, copp_idx, adm_set_params->apr_hdr.opcode);
done:
	kfree(adm_set_params);
	return ret;
}
EXPORT_SYMBOL(adm_set_pp_params);

int adm_pack_and_set_one_pp_param(int port_id, int copp_idx,
				  struct param_hdr_v3 param_hdr, u8 *param_data)
{
	u8 *packed_data = NULL;
	u32 total_size = 0;
	int ret = 0;

	total_size = sizeof(union param_hdrs) + param_hdr.param_size;
	packed_data = kzalloc(total_size, GFP_KERNEL);
	if (!packed_data)
		return -ENOMEM;

	ret = q6common_pack_pp_params(packed_data, &param_hdr, param_data,
				      &total_size);
	if (ret) {
		pr_err("%s: Failed to pack parameter data, error %d\n",
		       __func__, ret);
		goto done;
	}

	ret = adm_set_pp_params(port_id, copp_idx, NULL, packed_data,
				total_size);
	if (ret)
		pr_err("%s: Failed to set parameter data, error %d\n", __func__,
		       ret);
done:
	kfree(packed_data);
	return ret;
}
EXPORT_SYMBOL(adm_pack_and_set_one_pp_param);

/*
 * Only one parameter can be requested at a time. Therefore, packing and sending
 * the request can be handled locally.
 */
int adm_get_pp_params(int port_id, int copp_idx, uint32_t client_id,
		      struct mem_mapping_hdr *mem_hdr,
		      struct param_hdr_v3 *param_hdr, u8 *returned_param_data)
{
	struct adm_cmd_get_pp_params adm_get_params;
	int total_size = 0;
	int get_param_array_sz = ARRAY_SIZE(adm_get_parameters);
	int returned_param_size = 0;
	int returned_param_size_in_bytes = 0;
	int port_idx = 0;
	int idx = 0;
	int ret = 0;

	if (param_hdr == NULL) {
		pr_err("%s: Received NULL pointer for parameter header\n",
		       __func__);
		return -EINVAL;
	}

	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
		pr_err("%s: Invalid port_idx 0x%x\n", __func__, port_idx);
		return -EINVAL;
	}
	if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
		return -EINVAL;
	}

	memset(&adm_get_params, 0, sizeof(adm_get_params));

	if (mem_hdr != NULL)
		adm_get_params.mem_hdr = *mem_hdr;

	q6common_pack_pp_params((u8 *) &adm_get_params.param_hdr, param_hdr,
				NULL, &total_size);

	/* Pack APR header after filling body so total_size has correct value */
	adm_get_params.apr_hdr.hdr_field =
		APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
			      APR_PKT_VER);
	adm_get_params.apr_hdr.pkt_size = sizeof(adm_get_params);
	adm_get_params.apr_hdr.src_svc = APR_SVC_ADM;
	adm_get_params.apr_hdr.src_domain = APR_DOMAIN_APPS;
	adm_get_params.apr_hdr.src_port = port_id;
	adm_get_params.apr_hdr.dest_svc = APR_SVC_ADM;
	adm_get_params.apr_hdr.dest_domain = APR_DOMAIN_ADSP;
	adm_get_params.apr_hdr.dest_port =
		atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
	adm_get_params.apr_hdr.token =
		port_idx << 16 | client_id << 8 | copp_idx;

	if (q6common_is_instance_id_supported())
		adm_get_params.apr_hdr.opcode = ADM_CMD_GET_PP_PARAMS_V6;
	else
		adm_get_params.apr_hdr.opcode = ADM_CMD_GET_PP_PARAMS_V5;

	ret = adm_apr_send_pkt((uint32_t *) &adm_get_params,
			&this_adm.copp.wait[port_idx][copp_idx],
			port_idx, copp_idx, adm_get_params.apr_hdr.opcode);

	/* Copy data to caller if sent in band */
	if (!returned_param_data) {
		pr_debug("%s: Received NULL pointer for param destination, not copying payload\n",
			__func__);
		return 0;
	}

	idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
	returned_param_size = adm_get_parameters[idx];
	if (returned_param_size < 0 ||
	    returned_param_size + idx + 1 > get_param_array_sz) {
		pr_err("%s: Invalid parameter size %d\n", __func__,
		       returned_param_size);
		return -EINVAL;
	}

	returned_param_size_in_bytes = returned_param_size * sizeof(uint32_t);
	if (param_hdr->param_size < returned_param_size_in_bytes) {
		pr_err("%s: Provided buffer is not big enough, provided buffer size(%d) size needed(%d)\n",
		       __func__, param_hdr->param_size,
		       returned_param_size_in_bytes);
		return -EINVAL;
	}

	memcpy(returned_param_data, &adm_get_parameters[idx + 1],
	       returned_param_size_in_bytes);
	return ret;
}
EXPORT_SYMBOL(adm_get_pp_params);

int adm_get_pp_topo_module_list_v2(int port_id, int copp_idx,
				   int32_t param_length,
				   int32_t *returned_params)
{
	struct adm_cmd_get_pp_topo_module_list adm_get_module_list;
	bool iid_supported = q6common_is_instance_id_supported();
	int *topo_list;
	int num_modules = 0;
	int list_size = 0;
	int port_idx, idx;
	int i = 0;
	atomic_t *copp_stat = NULL;
	int ret = 0;

	pr_debug("%s : port_id %x", __func__, port_id);
	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
		return -EINVAL;
	}

	if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
		return -EINVAL;
	}

	memset(&adm_get_module_list, 0, sizeof(adm_get_module_list));

	adm_get_module_list.apr_hdr.pkt_size = sizeof(adm_get_module_list);
	adm_get_module_list.apr_hdr.src_svc = APR_SVC_ADM;
	adm_get_module_list.apr_hdr.src_domain = APR_DOMAIN_APPS;
	adm_get_module_list.apr_hdr.src_port = port_id;
	adm_get_module_list.apr_hdr.dest_svc = APR_SVC_ADM;
	adm_get_module_list.apr_hdr.dest_domain = APR_DOMAIN_ADSP;
	adm_get_module_list.apr_hdr.dest_port =
		atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
	adm_get_module_list.apr_hdr.token = port_idx << 16 | copp_idx;
	/*
	 * Out of band functionality is not currently utilized.
	 * Assume in band.
	 */
	if (iid_supported) {
		adm_get_module_list.apr_hdr.opcode =
			ADM_CMD_GET_PP_TOPO_MODULE_LIST_V2;
		adm_get_module_list.param_max_size = param_length;
	} else {
		adm_get_module_list.apr_hdr.opcode =
			ADM_CMD_GET_PP_TOPO_MODULE_LIST;

		if (param_length > U16_MAX) {
			pr_err("%s: Invalid param length for V1 %d\n", __func__,
			       param_length);
			return -EINVAL;
		}
		adm_get_module_list.param_max_size = param_length << 16;
	}

	copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
	atomic_set(copp_stat, -1);
	ret = apr_send_pkt(this_adm.apr, (uint32_t *) &adm_get_module_list);
	if (ret < 0) {
		pr_err("%s: APR send pkt failed for port_id: 0x%x failed ret %d\n",
		       __func__, port_id, ret);
		ret = -EINVAL;
		goto done;
	}
	ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
				 atomic_read(copp_stat) >= 0,
				 msecs_to_jiffies(TIMEOUT_MS));
	if (!ret) {
		pr_err("%s: Timeout for port_id: 0x%x\n", __func__, port_id);
		ret = -ETIMEDOUT;
		goto done;
	}
	if (atomic_read(copp_stat) > 0) {
		pr_err("%s: DSP returned error[%s]\n", __func__,
		       adsp_err_get_err_str(atomic_read(copp_stat)));
		ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
		goto done;
	}

	ret = 0;

	if (returned_params) {
		/*
		 * When processing ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST IID is
		 * added since it is not present. Therefore, there is no need to
		 * do anything different if IID is not supported here as it is
		 * already taken care of.
		 */
		idx = ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH * copp_idx;
		num_modules = adm_module_topo_list[idx];
		if (num_modules < 0 || num_modules > MAX_MODULES_IN_TOPO) {
			pr_err("%s: Invalid number of modules returned %d\n",
			       __func__, num_modules);
			return -EINVAL;
		}

		list_size = num_modules * sizeof(struct module_instance_info);
		if (param_length < list_size) {
			pr_err("%s: Provided buffer not big enough to hold module-instance list, provided size %d, needed size %d\n",
			       __func__, param_length, list_size);
			return -EINVAL;
		}

		topo_list = (int32_t *) (&adm_module_topo_list[idx]);
		memcpy(returned_params, topo_list, list_size);
		for (i = 1; i <= num_modules; i += 2) {
			pr_debug("module = 0x%x instance = 0x%x\n",
				 returned_params[i], returned_params[i + 1]);
		}
	}
done:
	return ret;
}
EXPORT_SYMBOL(adm_get_pp_topo_module_list_v2);

static void adm_callback_debug_print(struct apr_client_data *data)
{
	uint32_t *payload;

	payload = data->payload;

	if (data->payload_size >= 8)
		pr_debug("%s: code = 0x%x PL#0[0x%x], PL#1[0x%x], size = %d\n",
			__func__, data->opcode, payload[0], payload[1],
			data->payload_size);
	else if (data->payload_size >= 4)
		pr_debug("%s: code = 0x%x PL#0[0x%x], size = %d\n",
			__func__, data->opcode, payload[0],
			data->payload_size);
	else
		pr_debug("%s: code = 0x%x, size = %d\n",
			__func__, data->opcode, data->payload_size);
}

/**
 * adm_set_multi_ch_map -
 *        Update multi channel map info
 *
 * @channel_map: pointer with channel map info
 * @path: direction or ADM path type
 *
 * Returns 0 on success or error on failure
 */
int adm_set_multi_ch_map(char *channel_map, int path)
{
	int idx;

	if (path == ADM_PATH_PLAYBACK) {
		idx = ADM_MCH_MAP_IDX_PLAYBACK;
	} else if (path == ADM_PATH_LIVE_REC) {
		idx = ADM_MCH_MAP_IDX_REC;
	} else {
		pr_err("%s: invalid attempt to set path %d\n", __func__, path);
		return -EINVAL;
	}

	memcpy(multi_ch_maps[idx].channel_mapping, channel_map,
			PCM_FORMAT_MAX_NUM_CHANNEL_V8);
	multi_ch_maps[idx].set_channel_map = true;

	return 0;
}
EXPORT_SYMBOL(adm_set_multi_ch_map);

/**
 * adm_get_multi_ch_map -
 *        Retrieves multi channel map info
 *
 * @channel_map: pointer to be updated with channel map
 * @path: direction or ADM path type
 *
 * Returns 0 on success or error on failure
 */
int adm_get_multi_ch_map(char *channel_map, int path)
{
	int idx;

	if (path == ADM_PATH_PLAYBACK) {
		idx = ADM_MCH_MAP_IDX_PLAYBACK;
	} else if (path == ADM_PATH_LIVE_REC) {
		idx = ADM_MCH_MAP_IDX_REC;
	} else {
		pr_err("%s: invalid attempt to get path %d\n", __func__, path);
		return -EINVAL;
	}

	if (multi_ch_maps[idx].set_channel_map) {
		memcpy(channel_map, multi_ch_maps[idx].channel_mapping,
				PCM_FORMAT_MAX_NUM_CHANNEL_V8);
	}

	return 0;
}
EXPORT_SYMBOL(adm_get_multi_ch_map);

/**
 * adm_set_port_multi_ch_map -
 *        Update port specific channel map info
 *
 * @channel_map: pointer with channel map info
 * @port_id: port for which chmap is set
 */
void adm_set_port_multi_ch_map(char *channel_map, int port_id)
{
	int port_idx;

	port_id = q6audio_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);

	if (port_idx < 0) {
		pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
		return;
	}

	memcpy(port_channel_map[port_idx].channel_mapping, channel_map,
			PCM_FORMAT_MAX_NUM_CHANNEL_V8);
	port_channel_map[port_idx].set_channel_map = true;
}
EXPORT_SYMBOL(adm_set_port_multi_ch_map);

static int adm_process_get_param_response(u32 opcode, u32 idx, u32 *payload,
					  u32 payload_size)
{
	struct adm_cmd_rsp_get_pp_params_v5 *v5_rsp = NULL;
	struct adm_cmd_rsp_get_pp_params_v6 *v6_rsp = NULL;
	u32 *param_data = NULL;
	int data_size = 0;
	int struct_size = 0;

	if (payload == NULL) {
		pr_err("%s: Payload is NULL\n", __func__);
		return -EINVAL;
	}

	switch (opcode) {
	case ADM_CMDRSP_GET_PP_PARAMS_V5:
		struct_size = sizeof(struct adm_cmd_rsp_get_pp_params_v5);
		if (payload_size < struct_size) {
			pr_err("%s: payload size %d < expected size %d\n",
				__func__, payload_size, struct_size);
			break;
		}
		v5_rsp = (struct adm_cmd_rsp_get_pp_params_v5 *) payload;
		data_size = v5_rsp->param_hdr.param_size;
		param_data = v5_rsp->param_data;
		break;
	case ADM_CMDRSP_GET_PP_PARAMS_V6:
		struct_size = sizeof(struct adm_cmd_rsp_get_pp_params_v6);
		if (payload_size < struct_size) {
			pr_err("%s: payload size %d < expected size %d\n",
				__func__, payload_size, struct_size);
			break;
		}
		v6_rsp = (struct adm_cmd_rsp_get_pp_params_v6 *) payload;
		data_size = v6_rsp->param_hdr.param_size;
		param_data = v6_rsp->param_data;
		break;
	default:
		pr_err("%s: Invalid opcode %d\n", __func__, opcode);
		return -EINVAL;
	}

	/*
	 * Just store the returned parameter data, not the header. The calling
	 * function is expected to know what it asked for. Therefore, there is
	 * no difference between V5 and V6.
	 */
	if ((payload_size >= struct_size + data_size) &&
	    (ARRAY_SIZE(adm_get_parameters) > idx) &&
	    (ARRAY_SIZE(adm_get_parameters) > idx + 1 + data_size)) {
		pr_debug("%s: Received parameter data in band\n",
					__func__);
		/*
		 * data_size is expressed in number of bytes, store in number of
		 * ints
		 */
		adm_get_parameters[idx] =
			data_size / sizeof(*adm_get_parameters);
		pr_debug("%s: GET_PP PARAM: received parameter length: 0x%x\n",
			 __func__, adm_get_parameters[idx]);
		/* store params after param_size */
		memcpy(&adm_get_parameters[idx + 1], param_data, data_size);
	} else if (payload_size == sizeof(uint32_t)) {
		adm_get_parameters[idx] = -1;
		pr_debug("%s: Out of band case, setting size to %d\n",
			 __func__, adm_get_parameters[idx]);
	} else {
		pr_err("%s: Invalid parameter combination, payload_size %d, idx %d\n",
		       __func__, payload_size, idx);
		return -EINVAL;
	}
	return 0;
}

static int adm_process_get_topo_list_response(u32 opcode, int copp_idx,
					      u32 num_modules, u32 *payload,
					      u32 payload_size)
{
	u32 *fill_list = NULL;
	int idx = 0;
	int i = 0;
	int j = 0;

	if (payload == NULL) {
		pr_err("%s: Payload is NULL\n", __func__);
		return -EINVAL;
	} else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid COPP index %d\n", __func__, copp_idx);
		return -EINVAL;
	}

	idx = ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH * copp_idx;
	fill_list = adm_module_topo_list + idx;
	*fill_list++ = num_modules;
	for (i = 0; i < num_modules; i++) {
		if (j > payload_size / sizeof(u32)) {
			pr_err("%s: Invalid number of modules specified %d\n",
			       __func__, num_modules);
			return -EINVAL;
		}

		/* store module ID */
		*fill_list++ = payload[j];
		j++;

		switch (opcode) {
		case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST_V2:
			/* store instance ID */
			*fill_list++ = payload[j];
			j++;
			break;
		case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
			/* Insert IID 0 when repacking */
			*fill_list++ = INSTANCE_ID_0;
			break;
		default:
			pr_err("%s: Invalid opcode %d\n", __func__, opcode);
			return -EINVAL;
		}
	}

	return 0;
}

static void adm_reset_data(void)
{
	int i, j;

	apr_reset(this_adm.apr);
	for (i = 0; i < AFE_MAX_PORTS; i++) {
		for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
			atomic_set(&this_adm.copp.id[i][j],
				   RESET_COPP_ID);
			atomic_set(&this_adm.copp.cnt[i][j], 0);
			atomic_set(
			   &this_adm.copp.topology[i][j], 0);
			atomic_set(&this_adm.copp.mode[i][j],
				   0);
			atomic_set(&this_adm.copp.stat[i][j],
				   0);
			atomic_set(&this_adm.copp.rate[i][j],
				   0);
			atomic_set(
				&this_adm.copp.channels[i][j],
				   0);
			atomic_set(
			    &this_adm.copp.bit_width[i][j], 0);
			atomic_set(
			    &this_adm.copp.app_type[i][j], 0);
			atomic_set(
			   &this_adm.copp.acdb_id[i][j], 0);
			atomic_set(
			   &this_adm.copp.session_type[i][j], 0);
			this_adm.copp.adm_status[i][j] =
				ADM_STATUS_CALIBRATION_REQUIRED;
		}
	}
	this_adm.apr = NULL;
	cal_utils_clear_cal_block_q6maps(ADM_MAX_CAL_TYPES,
		this_adm.cal_data);
	mutex_lock(&this_adm.cal_data
		[ADM_CUSTOM_TOP_CAL]->lock);
	this_adm.set_custom_topology = 1;
	mutex_unlock(&this_adm.cal_data[
		ADM_CUSTOM_TOP_CAL]->lock);
	rtac_clear_mapping(ADM_RTAC_CAL);
	/*
	 * Free the ION memory and clear the map handles
	 * for Source Tracking
	 */
	if (this_adm.sourceTrackingData.memmap.paddr != 0) {
		msm_audio_ion_free(
			this_adm.sourceTrackingData.dma_buf);
		this_adm.sourceTrackingData.dma_buf = NULL;
		this_adm.sourceTrackingData.memmap.size = 0;
		this_adm.sourceTrackingData.memmap.kvaddr =
							 NULL;
		this_adm.sourceTrackingData.memmap.paddr = 0;
		this_adm.sourceTrackingData.apr_cmd_status = -1;
		atomic_set(&this_adm.mem_map_handles[
			ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
	}
}

static int32_t adm_callback(struct apr_client_data *data, void *priv)
{
	uint32_t *payload;
	int port_idx, copp_idx, idx, client_id;
	int num_modules;
	int ret;

	if (data == NULL) {
		pr_err("%s: data parameter is null\n", __func__);
		return -EINVAL;
	}

	payload = data->payload;

	if (data->opcode == RESET_EVENTS) {
		pr_debug("%s: Reset event is received: %d %d apr[%pK]\n",
			__func__,
			data->reset_event, data->reset_proc, this_adm.apr);
		if (this_adm.apr)
			adm_reset_data();
		return 0;
	}

	adm_callback_debug_print(data);
	if (data->payload_size >= sizeof(uint32_t)) {
		copp_idx = (data->token) & 0XFF;
		port_idx = ((data->token) >> 16) & 0xFF;
		client_id = ((data->token) >> 8) & 0xFF;
		if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
			pr_err("%s: Invalid port idx %d token %d\n",
				__func__, port_idx, data->token);
			return 0;
		}
		if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
			pr_err("%s: Invalid copp idx %d token %d\n",
				__func__, copp_idx, data->token);
			return 0;
		}
		if (client_id < 0 || client_id >= ADM_CLIENT_ID_MAX) {
			pr_err("%s: Invalid client id %d\n", __func__,
				client_id);
			return 0;
		}
		if (data->opcode == APR_BASIC_RSP_RESULT) {
			pr_debug("%s: APR_BASIC_RSP_RESULT id 0x%x\n",
				__func__, payload[0]);

			if (!((client_id != ADM_CLIENT_ID_SOURCE_TRACKING) &&
			     ((payload[0] == ADM_CMD_SET_PP_PARAMS_V5) ||
			      (payload[0] == ADM_CMD_SET_PP_PARAMS_V6)))) {
				if (data->payload_size <
						(2 * sizeof(uint32_t))) {
					pr_err("%s: Invalid payload size %d\n",
						__func__, data->payload_size);
					return 0;
				}
			}

			if (payload[1] != 0) {
				pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
					__func__, payload[0], payload[1]);
			}
			switch (payload[0]) {
			case ADM_CMD_SET_PP_PARAMS_V5:
			case ADM_CMD_SET_PP_PARAMS_V6:
				pr_debug("%s: ADM_CMD_SET_PP_PARAMS\n",
					 __func__);
				if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
					this_adm.sourceTrackingData.
						apr_cmd_status = payload[1];
				else if (rtac_make_adm_callback(payload,
						data->payload_size)) {
					pr_debug("%s: rtac cmd response\n",
						 __func__);
				}
				atomic_set(&this_adm.copp.stat[port_idx]
						[copp_idx], payload[1]);
				wake_up(
				&this_adm.copp.wait[port_idx][copp_idx]);
				break;
				/*
				 * if soft volume is called and already
				 * interrupted break out of the sequence here
				 */
			case ADM_CMD_DEVICE_OPEN_V5:
			case ADM_CMD_DEVICE_CLOSE_V5:
			case ADM_CMD_DEVICE_OPEN_V6:
			case ADM_CMD_DEVICE_OPEN_V8:
				pr_debug("%s: Basic callback received for 0x%x, wake up.\n",
					__func__, payload[0]);
				atomic_set(&this_adm.copp.stat[port_idx]
						[copp_idx], payload[1]);
				wake_up(
				&this_adm.copp.wait[port_idx][copp_idx]);
				break;
			case ADM_CMD_ADD_TOPOLOGIES:
				pr_debug("%s: callback received, ADM_CMD_ADD_TOPOLOGIES.\n",
					__func__);
				atomic_set(&this_adm.adm_stat, payload[1]);
				wake_up(&this_adm.adm_wait);
				break;
			case ADM_CMD_MATRIX_MAP_ROUTINGS_V5:
			case ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5:
				pr_debug("%s: Basic callback received, wake up.\n",
					__func__);
				atomic_set(&this_adm.matrix_map_stat,
					payload[1]);
				wake_up(&this_adm.matrix_map_wait);
				break;
			case ADM_CMD_SHARED_MEM_UNMAP_REGIONS:
				pr_debug("%s: ADM_CMD_SHARED_MEM_UNMAP_REGIONS\n",
					__func__);
				atomic_set(&this_adm.adm_stat, payload[1]);
				wake_up(&this_adm.adm_wait);
				break;
			case ADM_CMD_SHARED_MEM_MAP_REGIONS:
				pr_debug("%s: ADM_CMD_SHARED_MEM_MAP_REGIONS\n",
					__func__);
				/* Should only come here if there is an APR */
				/* error or malformed APR packet. Otherwise */
				/* response will be returned as */
				if (payload[1] != 0) {
					pr_err("%s: ADM map error, resuming\n",
						__func__);
					atomic_set(&this_adm.adm_stat,
						payload[1]);
					wake_up(&this_adm.adm_wait);
				}
				break;
			case ADM_CMD_GET_PP_PARAMS_V5:
			case ADM_CMD_GET_PP_PARAMS_V6:
				pr_debug("%s: ADM_CMD_GET_PP_PARAMS\n",
					 __func__);
				/* Should only come here if there is an APR */
				/* error or malformed APR packet. Otherwise */
				/* response will be returned as */
				/* ADM_CMDRSP_GET_PP_PARAMS_V5 */
				if (client_id ==
					ADM_CLIENT_ID_SOURCE_TRACKING) {
					this_adm.sourceTrackingData.
						apr_cmd_status = payload[1];
					if (payload[1] != 0)
						pr_err("%s: ADM get param error = %d\n",
							__func__, payload[1]);

					atomic_set(&this_adm.copp.stat
						[port_idx][copp_idx],
						payload[1]);
					wake_up(&this_adm.copp.wait
							[port_idx][copp_idx]);
				} else {
					if (payload[1] != 0) {
						pr_err("%s: ADM get param error = %d, resuming\n",
							__func__, payload[1]);

						rtac_make_adm_callback(payload,
							data->payload_size);
					}
				}
				break;
			case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5:
			case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V6:
				pr_debug("%s:callback received PSPD MTMX, wake up\n",
					__func__);
				atomic_set(&this_adm.copp.stat[port_idx]
						[copp_idx], payload[1]);
				wake_up(
				&this_adm.copp.wait[port_idx][copp_idx]);
				break;
			case ADM_CMD_GET_PP_TOPO_MODULE_LIST:
			case ADM_CMD_GET_PP_TOPO_MODULE_LIST_V2:
				pr_debug("%s:ADM_CMD_GET_PP_TOPO_MODULE_LIST\n",
					 __func__);
				if (payload[1] != 0)
					pr_err("%s: ADM get topo list error = %d\n",
					       __func__, payload[1]);
				break;
			default:
				pr_err("%s: Unknown Cmd: 0x%x\n", __func__,
								payload[0]);
				break;
			}
			return 0;
		}

		switch (data->opcode) {
		case ADM_CMDRSP_DEVICE_OPEN_V5:
		case ADM_CMDRSP_DEVICE_OPEN_V6:
		case ADM_CMDRSP_DEVICE_OPEN_V8: {
			struct adm_cmd_rsp_device_open_v5 *open = NULL;
			if (data->payload_size <
				sizeof(struct adm_cmd_rsp_device_open_v5)) {
				pr_err("%s: Invalid payload size %d\n", __func__,
					data->payload_size);
				return 0;
			}
			open = (struct adm_cmd_rsp_device_open_v5 *)data->payload;
			if (open->copp_id == INVALID_COPP_ID) {
				pr_err("%s: invalid coppid rxed %d\n",
					__func__, open->copp_id);
				atomic_set(&this_adm.copp.stat[port_idx]
						[copp_idx], ADSP_EBADPARAM);
				wake_up(
				&this_adm.copp.wait[port_idx][copp_idx]);
				break;
			}
			atomic_set(&this_adm.copp.stat
				[port_idx][copp_idx], payload[0]);
			atomic_set(&this_adm.copp.id[port_idx][copp_idx],
				   open->copp_id);
			pr_debug("%s: coppid rxed=%d\n", __func__,
				 open->copp_id);
			wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
			}
			break;
		case ADM_CMDRSP_GET_PP_PARAMS_V5:
		case ADM_CMDRSP_GET_PP_PARAMS_V6:
			pr_debug("%s: ADM_CMDRSP_GET_PP_PARAMS\n", __func__);
			if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
				this_adm.sourceTrackingData.apr_cmd_status =
					payload[0];
			else if (rtac_make_adm_callback(payload,
						data->payload_size)) {
				pr_debug("%s: rtac cmd response\n", __func__);
				atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
					   payload[0]);
				wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
				break;
			}

			idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
			if (payload[0] == 0 && data->payload_size > 0) {
				ret = adm_process_get_param_response(
					data->opcode, idx, payload,
					data->payload_size);
				if (ret)
					pr_err("%s: Failed to process get param response, error %d\n",
					       __func__, ret);
			} else {
				adm_get_parameters[idx] = -1;
				pr_err("%s: ADM_CMDRSP_GET_PP_PARAMS returned error 0x%x\n",
				       __func__, payload[0]);
			}
			atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
				   payload[0]);
			wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
			break;
		case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
		case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST_V2:
			pr_debug("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST\n",
				 __func__);
			if (data->payload_size >= (2 * sizeof(uint32_t))) {
				num_modules = payload[1];
				pr_debug("%s: Num modules %d\n", __func__,
					 num_modules);
				if (payload[0]) {
					pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST, error = %d\n",
					       __func__, payload[0]);
				} else if (num_modules > MAX_MODULES_IN_TOPO) {
					pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST invalid num modules received, num modules = %d\n",
					       __func__, num_modules);
				} else {
					ret = adm_process_get_topo_list_response(
						data->opcode, copp_idx,
						num_modules, payload,
						data->payload_size);
					if (ret)
						pr_err("%s: Failed to process get topo modules list response, error %d\n",
						       __func__, ret);
				}
			} else {
				pr_err("%s: Invalid payload size %d\n",
					__func__, data->payload_size);
			}
			atomic_set(&this_adm.copp.stat[port_idx][copp_idx],
				   payload[0]);
			wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
			break;
		case ADM_CMDRSP_SHARED_MEM_MAP_REGIONS:
			pr_debug("%s: ADM_CMDRSP_SHARED_MEM_MAP_REGIONS\n",
				__func__);
			atomic_set(&this_adm.mem_map_handles[
				   atomic_read(&this_adm.mem_map_index)],
				   *payload);
			atomic_set(&this_adm.adm_stat, 0);
			wake_up(&this_adm.adm_wait);
			break;
		default:
			pr_err("%s: Unknown cmd:0x%x\n", __func__,
				data->opcode);
			break;
		}
	}
	return 0;
}

static int adm_memory_map_regions(phys_addr_t *buf_add, uint32_t mempool_id,
			   uint32_t *bufsz, uint32_t bufcnt)
{
	struct  avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
	struct  avs_shared_map_region_payload *mregions = NULL;
	void    *mmap_region_cmd = NULL;
	void    *payload = NULL;
	int     ret = 0;
	int     i = 0;
	int     cmd_size = 0;

	pr_debug("%s:\n", __func__);
	if (this_adm.apr == NULL) {
		this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
						0xFFFFFFFF, &this_adm);
		if (this_adm.apr == NULL) {
			pr_err("%s: Unable to register ADM\n", __func__);
			ret = -ENODEV;
			return ret;
		}
		rtac_set_adm_handle(this_adm.apr);
	}

	cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
			+ sizeof(struct avs_shared_map_region_payload)
			* bufcnt;

	mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
	if (!mmap_region_cmd)
		return -ENOMEM;

	mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
	mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
						APR_HDR_LEN(APR_HDR_SIZE),
								APR_PKT_VER);
	mmap_regions->hdr.pkt_size = cmd_size;
	mmap_regions->hdr.src_port = 0;

	mmap_regions->hdr.dest_port = 0;
	mmap_regions->hdr.token = 0;
	mmap_regions->hdr.opcode = ADM_CMD_SHARED_MEM_MAP_REGIONS;
	mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
	mmap_regions->num_regions = bufcnt & 0x00ff;
	mmap_regions->property_flag = 0x00;

	pr_debug("%s: map_regions->num_regions = %d\n", __func__,
				mmap_regions->num_regions);
	payload = ((u8 *) mmap_region_cmd +
				sizeof(struct avs_cmd_shared_mem_map_regions));
	mregions = (struct avs_shared_map_region_payload *)payload;

	for (i = 0; i < bufcnt; i++) {
		mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
		mregions->shm_addr_msw =
				msm_audio_populate_upper_32_bits(buf_add[i]);
		mregions->mem_size_bytes = bufsz[i];
		++mregions;
	}

	atomic_set(&this_adm.adm_stat, -1);
	ret = apr_send_pkt(this_adm.apr, (uint32_t *) mmap_region_cmd);
	if (ret < 0) {
		pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
					mmap_regions->hdr.opcode, ret);
		ret = -EINVAL;
		goto fail_cmd;
	}

	ret = wait_event_timeout(this_adm.adm_wait,
				 atomic_read(&this_adm.adm_stat) >= 0,
				 msecs_to_jiffies(TIMEOUT_MS));
	if (!ret) {
		pr_err("%s: timeout. waited for memory_map\n", __func__);
		ret = -EINVAL;
		goto fail_cmd;
	} else if (atomic_read(&this_adm.adm_stat) > 0) {
		pr_err("%s: DSP returned error[%s]\n",
				__func__, adsp_err_get_err_str(
				atomic_read(&this_adm.adm_stat)));
		ret = adsp_err_get_lnx_err_code(
				atomic_read(&this_adm.adm_stat));
		goto fail_cmd;
	}
fail_cmd:
	kfree(mmap_region_cmd);
	return ret;
}

static int adm_memory_unmap_regions(void)
{
	struct  avs_cmd_shared_mem_unmap_regions unmap_regions;
	int     ret = 0;

	pr_debug("%s:\n", __func__);
	if (this_adm.apr == NULL) {
		pr_err("%s: APR handle NULL\n", __func__);
		return -EINVAL;
	}

	unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
						APR_HDR_LEN(APR_HDR_SIZE),
							APR_PKT_VER);
	unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
	unmap_regions.hdr.src_port = 0;
	unmap_regions.hdr.dest_port = 0;
	unmap_regions.hdr.token = 0;
	unmap_regions.hdr.opcode = ADM_CMD_SHARED_MEM_UNMAP_REGIONS;
	unmap_regions.mem_map_handle = atomic_read(&this_adm.
		mem_map_handles[atomic_read(&this_adm.mem_map_index)]);
	atomic_set(&this_adm.adm_stat, -1);
	ret = apr_send_pkt(this_adm.apr, (uint32_t *) &unmap_regions);
	if (ret < 0) {
		pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
				unmap_regions.hdr.opcode, ret);
		ret = -EINVAL;
		goto fail_cmd;
	}

	ret = wait_event_timeout(this_adm.adm_wait,
				 atomic_read(&this_adm.adm_stat) >= 0,
				 msecs_to_jiffies(TIMEOUT_MS));
	if (!ret) {
		pr_err("%s: timeout. waited for memory_unmap\n",
		       __func__);
		ret = -EINVAL;
		goto fail_cmd;
	} else if (atomic_read(&this_adm.adm_stat) > 0) {
		pr_err("%s: DSP returned error[%s]\n",
				__func__, adsp_err_get_err_str(
				atomic_read(&this_adm.adm_stat)));
		ret = adsp_err_get_lnx_err_code(
				atomic_read(&this_adm.adm_stat));
		goto fail_cmd;
	} else {
		pr_debug("%s: Unmap handle 0x%x succeeded\n", __func__,
			 unmap_regions.mem_map_handle);
	}
fail_cmd:
	return ret;
}

static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
{
	int ret = 0;

	if (cal_block->map_data.dma_buf == NULL) {
		pr_err("%s: No ION allocation for cal index %d!\n",
			__func__, cal_index);
		ret = -EINVAL;
		goto done;
	}

	if ((cal_block->map_data.map_size > 0) &&
		(cal_block->map_data.q6map_handle == 0)) {
		atomic_set(&this_adm.mem_map_index, cal_index);
		ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
				(uint32_t *)&cal_block->map_data.map_size, 1);
		if (ret < 0) {
			pr_err("%s: ADM mmap did not work! size = %zd ret %d\n",
				__func__,
				cal_block->map_data.map_size, ret);
			pr_debug("%s: ADM mmap did not work! addr = 0x%pK, size = %zd ret %d\n",
				__func__,
				&cal_block->cal_data.paddr,
				cal_block->map_data.map_size, ret);
			goto done;
		}
		cal_block->map_data.q6map_handle = atomic_read(&this_adm.
			mem_map_handles[cal_index]);
	}
done:
	return ret;
}

static void send_adm_custom_topology(void)
{
	struct cal_block_data *cal_block = NULL;
	struct cmd_set_topologies adm_top;
	int cal_index = ADM_CUSTOM_TOP_CAL;
	int result;

	if (this_adm.cal_data[cal_index] == NULL)
		goto done;

	mutex_lock(&this_adm.cal_data[cal_index]->lock);
	if (!this_adm.set_custom_topology)
		goto unlock;
	this_adm.set_custom_topology = 0;

	cal_block = cal_utils_get_only_cal_block(this_adm.cal_data[cal_index]);
	if (cal_block == NULL || cal_utils_is_cal_stale(cal_block))
		goto unlock;

	pr_debug("%s: Sending cal_index %d\n", __func__, cal_index);

	result = remap_cal_data(cal_block, cal_index);
	if (result) {
		pr_err("%s: Remap_cal_data failed for cal %d!\n",
			__func__, cal_index);
		goto unlock;
	}
	atomic_set(&this_adm.mem_map_index, cal_index);
	atomic_set(&this_adm.mem_map_handles[cal_index],
		cal_block->map_data.q6map_handle);

	if (cal_block->cal_data.size == 0) {
		pr_debug("%s: No ADM cal to send\n", __func__);
		goto unlock;
	}

	adm_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
		APR_HDR_LEN(20), APR_PKT_VER);
	adm_top.hdr.pkt_size = sizeof(adm_top);
	adm_top.hdr.src_svc = APR_SVC_ADM;
	adm_top.hdr.src_domain = APR_DOMAIN_APPS;
	adm_top.hdr.src_port = 0;
	adm_top.hdr.dest_svc = APR_SVC_ADM;
	adm_top.hdr.dest_domain = APR_DOMAIN_ADSP;
	adm_top.hdr.dest_port = 0;
	adm_top.hdr.token = 0;
	adm_top.hdr.opcode = ADM_CMD_ADD_TOPOLOGIES;
	adm_top.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
	adm_top.payload_addr_msw = msm_audio_populate_upper_32_bits(
						cal_block->cal_data.paddr);
	adm_top.mem_map_handle = cal_block->map_data.q6map_handle;
	adm_top.payload_size = cal_block->cal_data.size;

	atomic_set(&this_adm.adm_stat, -1);
	pr_debug("%s: Sending ADM_CMD_ADD_TOPOLOGIES payload = 0x%pK, size = %d\n",
		__func__, &cal_block->cal_data.paddr,
		adm_top.payload_size);
	result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_top);
	if (result < 0) {
		pr_err("%s: Set topologies failed payload size = %zd result %d\n",
			__func__, cal_block->cal_data.size, result);
		goto unlock;
	}
	/* Wait for the callback */
	result = wait_event_timeout(this_adm.adm_wait,
				    atomic_read(&this_adm.adm_stat) >= 0,
				    msecs_to_jiffies(TIMEOUT_MS));
	if (!result) {
		pr_err("%s: Set topologies timed out payload size = %zd\n",
			__func__, cal_block->cal_data.size);
		goto unlock;
	} else if (atomic_read(&this_adm.adm_stat) > 0) {
		pr_err("%s: DSP returned error[%s]\n",
				__func__, adsp_err_get_err_str(
				atomic_read(&this_adm.adm_stat)));
		result = adsp_err_get_lnx_err_code(
				atomic_read(&this_adm.adm_stat));
		goto unlock;
	}
unlock:
	mutex_unlock(&this_adm.cal_data[cal_index]->lock);
done:
	return;
}

static int send_adm_cal_block(int port_id, int copp_idx,
			      struct cal_block_data *cal_block, int perf_mode)
{
	struct mem_mapping_hdr mem_hdr;
	int payload_size = 0;
	int port_idx = 0;
	int topology = 0;
	int result = 0;

	pr_debug("%s: Port id 0x%x,\n", __func__, port_id);

	if (!cal_block) {
		pr_debug("%s: No ADM cal to send for port_id = 0x%x!\n",
			__func__, port_id);
		result = -EINVAL;
		goto done;
	}
	if (cal_block->cal_data.size <= 0) {
		pr_debug("%s: No ADM cal sent for port_id = 0x%x!\n", __func__,
			 port_id);
		result = -EINVAL;
		goto done;
	}

	memset(&mem_hdr, 0, sizeof(mem_hdr));
	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
		pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
		return -EINVAL;
	} else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
		return -EINVAL;
	}

	topology = atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
	if (perf_mode == LEGACY_PCM_MODE &&
	    topology == DS2_ADM_COPP_TOPOLOGY_ID) {
		pr_err("%s: perf_mode %d, topology 0x%x\n", __func__, perf_mode,
		       topology);
		goto done;
	}

	mem_hdr.data_payload_addr_lsw =
		lower_32_bits(cal_block->cal_data.paddr);
	mem_hdr.data_payload_addr_msw =
		msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
	mem_hdr.mem_map_handle = cal_block->map_data.q6map_handle;
	payload_size = cal_block->cal_data.size;

	adm_set_pp_params(port_id, copp_idx, &mem_hdr, NULL, payload_size);

done:
	return result;
}

static struct cal_block_data *adm_find_cal_by_path(int cal_index, int path)
{
	struct list_head *ptr, *next;
	struct cal_block_data *cal_block = NULL;
	struct audio_cal_info_audproc *audproc_cal_info = NULL;
	struct audio_cal_info_audvol *audvol_cal_info = NULL;

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

	list_for_each_safe(ptr, next,
		&this_adm.cal_data[cal_index]->cal_blocks) {

		cal_block = list_entry(ptr,
			struct cal_block_data, list);

		if (cal_utils_is_cal_stale(cal_block))
			continue;

		if (cal_index == ADM_AUDPROC_CAL ||
		    cal_index == ADM_LSM_AUDPROC_CAL ||
		    cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL ||
		    cal_index == ADM_AUDPROC_PERSISTENT_CAL) {
			audproc_cal_info = cal_block->cal_info;
			if ((audproc_cal_info->path == path) &&
			    (cal_block->cal_data.size > 0))
				return cal_block;
		} else if (cal_index == ADM_AUDVOL_CAL) {
			audvol_cal_info = cal_block->cal_info;
			if ((audvol_cal_info->path == path) &&
			    (cal_block->cal_data.size > 0))
				return cal_block;
		}
	}
	pr_debug("%s: Can't find ADM cal for cal_index %d, path %d\n",
		__func__, cal_index, path);
	return NULL;
}

static struct cal_block_data *adm_find_cal_by_app_type(int cal_index, int path,
								int app_type)
{
	struct list_head *ptr, *next;
	struct cal_block_data *cal_block = NULL;
	struct audio_cal_info_audproc *audproc_cal_info = NULL;
	struct audio_cal_info_audvol *audvol_cal_info = NULL;

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

	list_for_each_safe(ptr, next,
		&this_adm.cal_data[cal_index]->cal_blocks) {

		cal_block = list_entry(ptr,
			struct cal_block_data, list);

		if (cal_utils_is_cal_stale(cal_block))
			continue;

		if (cal_index == ADM_AUDPROC_CAL ||
		    cal_index == ADM_LSM_AUDPROC_CAL ||
		    cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL ||
		    cal_index == ADM_AUDPROC_PERSISTENT_CAL) {
			audproc_cal_info = cal_block->cal_info;
			if ((audproc_cal_info->path == path) &&
			    (audproc_cal_info->app_type == app_type) &&
			    (cal_block->cal_data.size > 0))
				return cal_block;
		} else if (cal_index == ADM_AUDVOL_CAL) {
			audvol_cal_info = cal_block->cal_info;
			if ((audvol_cal_info->path == path) &&
			    (audvol_cal_info->app_type == app_type) &&
			    (cal_block->cal_data.size > 0))
				return cal_block;
		}
	}
	pr_debug("%s: Can't find ADM cali for cal_index %d, path %d, app %d, defaulting to search by path\n",
		__func__, cal_index, path, app_type);
	return adm_find_cal_by_path(cal_index, path);
}


static struct cal_block_data *adm_find_cal(int cal_index, int path,
					   int app_type, int acdb_id,
					   int sample_rate)
{
	struct list_head *ptr, *next;
	struct cal_block_data *cal_block = NULL;
	struct audio_cal_info_audproc *audproc_cal_info = NULL;
	struct audio_cal_info_audvol *audvol_cal_info = NULL;

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

	list_for_each_safe(ptr, next,
		&this_adm.cal_data[cal_index]->cal_blocks) {

		cal_block = list_entry(ptr,
			struct cal_block_data, list);
		if (cal_utils_is_cal_stale(cal_block))
			continue;

		if (cal_index == ADM_AUDPROC_CAL ||
		    cal_index == ADM_LSM_AUDPROC_CAL ||
		    cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL||
		    cal_index == ADM_AUDPROC_PERSISTENT_CAL) {
			audproc_cal_info = cal_block->cal_info;
			if ((audproc_cal_info->path == path) &&
			    (audproc_cal_info->app_type == app_type) &&
			    (audproc_cal_info->acdb_id == acdb_id) &&
			    (audproc_cal_info->sample_rate == sample_rate) &&
			    (cal_block->cal_data.size > 0))
				return cal_block;
		} else if (cal_index == ADM_AUDVOL_CAL) {
			audvol_cal_info = cal_block->cal_info;
			if ((audvol_cal_info->path == path) &&
			    (audvol_cal_info->app_type == app_type) &&
			    (audvol_cal_info->acdb_id == acdb_id) &&
			    (cal_block->cal_data.size > 0))
				return cal_block;
		}
	}
	pr_debug("%s: Can't find ADM cal for cal_index %d, path %d, app %d, acdb_id %d sample_rate %d defaulting to search by app type\n",
		__func__, cal_index, path, app_type, acdb_id, sample_rate);
	return adm_find_cal_by_app_type(cal_index, path, app_type);
}

static int adm_remap_and_send_cal_block(int cal_index, int port_id,
	int copp_idx, struct cal_block_data *cal_block, int perf_mode,
	int app_type, int acdb_id, int sample_rate)
{
	int ret = 0;

	pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
	ret = remap_cal_data(cal_block, cal_index);
	if (ret) {
		pr_err("%s: Remap_cal_data failed for cal %d!\n",
			__func__, cal_index);
		goto done;
	}
	ret = send_adm_cal_block(port_id, copp_idx, cal_block, perf_mode);
	if (ret < 0)
		pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d sample_rate %d\n",
			__func__, cal_index, port_id, ret, sample_rate);
done:
	return ret;
}

static void send_adm_cal_type(int cal_index, int path, int port_id,
			      int copp_idx, int perf_mode, int app_type,
			      int acdb_id, int sample_rate)
{
	struct cal_block_data		*cal_block = NULL;
	int ret;

	pr_debug("%s: cal index %d\n", __func__, cal_index);

	if (this_adm.cal_data[cal_index] == NULL) {
		pr_debug("%s: cal_index %d not allocated!\n",
			__func__, cal_index);
		goto done;
	}

	mutex_lock(&this_adm.cal_data[cal_index]->lock);
	cal_block = adm_find_cal(cal_index, path, app_type, acdb_id,
				sample_rate);
	if (cal_block == NULL)
		goto unlock;

	ret = adm_remap_and_send_cal_block(cal_index, port_id, copp_idx,
		cal_block, perf_mode, app_type, acdb_id, sample_rate);

	cal_utils_mark_cal_used(cal_block);
unlock:
	mutex_unlock(&this_adm.cal_data[cal_index]->lock);
done:
	return;
}

static int get_cal_path(int path)
{
	if (path == 0x1)
		return RX_DEVICE;
	else
		return TX_DEVICE;
}

static void send_adm_cal(int port_id, int copp_idx, int path, int perf_mode,
			 int app_type, int acdb_id, int sample_rate,
			 int passthr_mode)
{
	pr_debug("%s: port id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);

	if (passthr_mode != LISTEN) {
		send_adm_cal_type(ADM_AUDPROC_CAL, path, port_id, copp_idx,
				perf_mode, app_type, acdb_id, sample_rate);
		send_adm_cal_type(ADM_AUDPROC_PERSISTENT_CAL, path,
				  port_id, copp_idx, perf_mode, app_type,
				  acdb_id, sample_rate);
	} else {
		send_adm_cal_type(ADM_LSM_AUDPROC_CAL, path, port_id, copp_idx,
				  perf_mode, app_type, acdb_id, sample_rate);

		send_adm_cal_type(ADM_LSM_AUDPROC_PERSISTENT_CAL, path,
				  port_id, copp_idx, perf_mode, app_type,
				  acdb_id, sample_rate);
	}

	send_adm_cal_type(ADM_AUDVOL_CAL, path, port_id, copp_idx, perf_mode,
			  app_type, acdb_id, sample_rate);
}

/**
 * adm_connect_afe_port -
 *        command to send ADM connect AFE port
 *
 * @mode: value of mode for ADM connect AFE
 * @session_id: session active to connect
 * @port_id: Port ID number
 *
 * Returns 0 on success or error on failure
 */
int adm_connect_afe_port(int mode, int session_id, int port_id)
{
	struct adm_cmd_connect_afe_port_v5	cmd;
	int ret = 0;
	int port_idx, copp_idx = 0;

	pr_debug("%s: port_id: 0x%x session id:%d mode:%d\n", __func__,
				port_id, session_id, mode);

	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
		return -EINVAL;
	}

	if (this_adm.apr == NULL) {
		this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
						0xFFFFFFFF, &this_adm);
		if (this_adm.apr == NULL) {
			pr_err("%s: Unable to register ADM\n", __func__);
			ret = -ENODEV;
			return ret;
		}
		rtac_set_adm_handle(this_adm.apr);
	}
	pr_debug("%s: Port ID 0x%x, index %d\n", __func__, port_id, port_idx);

	cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
			APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
	cmd.hdr.pkt_size = sizeof(cmd);
	cmd.hdr.src_svc = APR_SVC_ADM;
	cmd.hdr.src_domain = APR_DOMAIN_APPS;
	cmd.hdr.src_port = port_id;
	cmd.hdr.dest_svc = APR_SVC_ADM;
	cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
	cmd.hdr.dest_port = 0; /* Ignored */
	cmd.hdr.token = port_idx << 16 | copp_idx;
	cmd.hdr.opcode = ADM_CMD_CONNECT_AFE_PORT_V5;

	cmd.mode = mode;
	cmd.session_id = session_id;
	cmd.afe_port_id = port_id;

	atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
	ret = apr_send_pkt(this_adm.apr, (uint32_t *)&cmd);
	if (ret < 0) {
		pr_err("%s: ADM enable for port_id: 0x%x failed ret %d\n",
					__func__, port_id, ret);
		ret = -EINVAL;
		goto fail_cmd;
	}
	/* Wait for the callback with copp id */
	ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
		atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
		msecs_to_jiffies(TIMEOUT_MS));
	if (!ret) {
		pr_err("%s: ADM connect timedout for port_id: 0x%x\n",
			__func__, port_id);
		ret = -EINVAL;
		goto fail_cmd;
	} else if (atomic_read(&this_adm.copp.stat
				[port_idx][copp_idx]) > 0) {
		pr_err("%s: DSP returned error[%s]\n",
				__func__, adsp_err_get_err_str(
				atomic_read(&this_adm.copp.stat
				[port_idx][copp_idx])));
		ret = adsp_err_get_lnx_err_code(
				atomic_read(&this_adm.copp.stat
					[port_idx][copp_idx]));
		goto fail_cmd;
	}
	atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
	return 0;

fail_cmd:

	return ret;
}
EXPORT_SYMBOL(adm_connect_afe_port);

int adm_arrange_mch_map(struct adm_cmd_device_open_v5 *open, int path,
			 int channel_mode, int port_idx)
{
	int rc = 0, idx;

	pr_debug("%s: channel mode %d", __func__, channel_mode);

	memset(open->dev_channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
	switch (path) {
	case ADM_PATH_PLAYBACK:
		idx = ADM_MCH_MAP_IDX_PLAYBACK;
		break;
	case ADM_PATH_LIVE_REC:
	case ADM_PATH_NONLIVE_REC:
		idx = ADM_MCH_MAP_IDX_REC;
		break;
	default:
		goto non_mch_path;
	};

	if ((open->dev_num_channel > 2) &&
		(port_channel_map[port_idx].set_channel_map ||
		 multi_ch_maps[idx].set_channel_map)) {
		if (port_channel_map[port_idx].set_channel_map)
			memcpy(open->dev_channel_mapping,
				port_channel_map[port_idx].channel_mapping,
				PCM_FORMAT_MAX_NUM_CHANNEL);
		else
			memcpy(open->dev_channel_mapping,
				multi_ch_maps[idx].channel_mapping,
				PCM_FORMAT_MAX_NUM_CHANNEL);
	} else {
		if (channel_mode == 1) {
			open->dev_channel_mapping[0] = PCM_CHANNEL_FC;
		} else if (channel_mode == 2) {
			open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		} else if (channel_mode == 3) {
			open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
		} else if (channel_mode == 4) {
			open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			open->dev_channel_mapping[2] = PCM_CHANNEL_LS;
			open->dev_channel_mapping[3] = PCM_CHANNEL_RS;
		} else if (channel_mode == 5) {
			open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
			open->dev_channel_mapping[3] = PCM_CHANNEL_LS;
			open->dev_channel_mapping[4] = PCM_CHANNEL_RS;
		} else if (channel_mode == 6) {
			open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
			open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
			open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
			open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
		} else if (channel_mode == 7) {
			open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
			open->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
			open->dev_channel_mapping[4] = PCM_CHANNEL_LB;
			open->dev_channel_mapping[5] = PCM_CHANNEL_RB;
			open->dev_channel_mapping[6] = PCM_CHANNEL_CS;
		} else if (channel_mode == 8) {
			open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
			open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
			open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
			open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
			open->dev_channel_mapping[6] = PCM_CHANNEL_LB;
			open->dev_channel_mapping[7] = PCM_CHANNEL_RB;
		} else {
			pr_err("%s: invalid num_chan %d\n", __func__,
				channel_mode);
			rc = -EINVAL;
			goto inval_ch_mod;
		}
	}

non_mch_path:
inval_ch_mod:
	return rc;
}

int adm_arrange_mch_ep2_map(struct adm_cmd_device_open_v6 *open_v6,
			 int channel_mode)
{
	int rc = 0;

	memset(open_v6->dev_channel_mapping_eid2, 0,
	       PCM_FORMAT_MAX_NUM_CHANNEL);

	if (channel_mode == 1)	{
		open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FC;
	} else if (channel_mode == 2) {
		open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
		open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
	} else if (channel_mode == 3)	{
		open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
		open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
		open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
	} else if (channel_mode == 4) {
		open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
		open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
		open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LS;
		open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_RS;
	} else if (channel_mode == 5) {
		open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
		open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
		open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
		open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_LS;
		open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_RS;
	} else if (channel_mode == 6) {
		open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
		open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
		open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
		open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
		open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
		open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
	} else if (channel_mode == 8) {
		open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
		open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
		open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
		open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
		open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
		open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
		open_v6->dev_channel_mapping_eid2[6] = PCM_CHANNEL_LB;
		open_v6->dev_channel_mapping_eid2[7] = PCM_CHANNEL_RB;
	} else {
		pr_err("%s: invalid num_chan %d\n", __func__,
			channel_mode);
		rc = -EINVAL;
	}

	return rc;
}

static int adm_arrange_mch_map_v8(
		struct adm_device_endpoint_payload *ep_payload,
		int path, int channel_mode, int port_idx)
{
	int rc = 0, idx;

	memset(ep_payload->dev_channel_mapping,
			0, PCM_FORMAT_MAX_NUM_CHANNEL_V8);
	switch (path) {
	case ADM_PATH_PLAYBACK:
		idx = ADM_MCH_MAP_IDX_PLAYBACK;
		break;
	case ADM_PATH_LIVE_REC:
	case ADM_PATH_NONLIVE_REC:
		idx = ADM_MCH_MAP_IDX_REC;
		break;
	default:
		goto non_mch_path;
	};

	if ((ep_payload->dev_num_channel > 2) &&
		(port_channel_map[port_idx].set_channel_map ||
		 multi_ch_maps[idx].set_channel_map)) {
		if (port_channel_map[port_idx].set_channel_map)
			memcpy(ep_payload->dev_channel_mapping,
				port_channel_map[port_idx].channel_mapping,
				PCM_FORMAT_MAX_NUM_CHANNEL_V8);
		else
			memcpy(ep_payload->dev_channel_mapping,
				multi_ch_maps[idx].channel_mapping,
				PCM_FORMAT_MAX_NUM_CHANNEL_V8);
	} else {
		if (channel_mode == 1) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FC;
		} else if (channel_mode == 2) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		} else if (channel_mode == 3) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
		} else if (channel_mode == 4) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LS;
			ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_RS;
		} else if (channel_mode == 5) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
			ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LS;
			ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_RS;
		} else if (channel_mode == 6) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
			ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
			ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
			ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
		} else if (channel_mode == 7) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
			ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
			ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
			ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
			ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_CS;
		} else if (channel_mode == 8) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
			ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
			ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
			ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
			ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
			ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
		} else if (channel_mode == 10) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
			ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
			ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
			ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
			ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
			ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
			ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
			ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
		} else if (channel_mode == 12) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
			ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
			ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
			ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
			ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
			ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
			ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
			ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
			ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
			ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
		} else if (channel_mode == 14) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
			ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
			ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
			ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
			ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
			ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
			ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
			ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
			ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
			ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
			ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
			ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
		} else if (channel_mode == 16) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
			ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
			ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LB;
			ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RB;
			ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LS;
			ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RS;
			ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
			ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
			ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
			ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
			ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
			ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
			ep_payload->dev_channel_mapping[14] = PCM_CHANNEL_RLC;
			ep_payload->dev_channel_mapping[15] = PCM_CHANNEL_RRC;
		} else if (channel_mode == 32) {
			ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
			ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
			ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
			ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
			ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
			ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
			ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
			ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
			ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_CS;
			ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TS;
			ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_CVH;
			ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_MS;
			ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
			ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
			ep_payload->dev_channel_mapping[14] = PCM_CHANNEL_RLC;
			ep_payload->dev_channel_mapping[15] = PCM_CHANNEL_RRC;
			ep_payload->dev_channel_mapping[16] = PCM_CHANNEL_LFE2;
			ep_payload->dev_channel_mapping[17] = PCM_CHANNEL_SL;
			ep_payload->dev_channel_mapping[18] = PCM_CHANNEL_SR;
			ep_payload->dev_channel_mapping[19] = PCM_CHANNEL_TFL;
			ep_payload->dev_channel_mapping[20] = PCM_CHANNEL_TFR;
			ep_payload->dev_channel_mapping[21] = PCM_CHANNEL_TC;
			ep_payload->dev_channel_mapping[22] = PCM_CHANNEL_TBL;
			ep_payload->dev_channel_mapping[23] = PCM_CHANNEL_TBR;
			ep_payload->dev_channel_mapping[24] = PCM_CHANNEL_TSL;
			ep_payload->dev_channel_mapping[25] = PCM_CHANNEL_TSR;
			ep_payload->dev_channel_mapping[26] = PCM_CHANNEL_TBC;
			ep_payload->dev_channel_mapping[27] = PCM_CHANNEL_BFC;
			ep_payload->dev_channel_mapping[28] = PCM_CHANNEL_BFL;
			ep_payload->dev_channel_mapping[29] = PCM_CHANNEL_BFR;
			ep_payload->dev_channel_mapping[30] = PCM_CHANNEL_LW;
			ep_payload->dev_channel_mapping[31] = PCM_CHANNEL_RW;
		} else {
			pr_err("%s: invalid num_chan %d\n", __func__,
				channel_mode);
			rc = -EINVAL;
			goto inval_ch_mod;
		}
	}

non_mch_path:
inval_ch_mod:
	return rc;
}

static int adm_arrange_mch_ep2_map_v8(
		struct adm_device_endpoint_payload *ep_payload,
		int channel_mode)
{
	int rc = 0;

	memset(ep_payload->dev_channel_mapping, 0,
	       PCM_FORMAT_MAX_NUM_CHANNEL_V8);

	if (channel_mode == 1) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FC;
	} else if (channel_mode == 2) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
		ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
	} else if (channel_mode == 3) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
		ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
	} else if (channel_mode == 4) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
		ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LS;
		ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_RS;
	} else if (channel_mode == 5) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
		ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_FC;
		ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_LS;
		ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_RS;
	} else if (channel_mode == 6) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
		ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
		ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
		ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
		ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
	} else if (channel_mode == 8) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
		ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
		ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
		ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
		ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
		ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
		ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
	}  else if (channel_mode == 10) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
		ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
		ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
		ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
		ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
		ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
		ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
		ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_CS;
		ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TS;
	} else if (channel_mode == 12) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
		ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
		ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
		ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
		ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
		ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
		ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
		ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_TFL;
		ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TFR;
		ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_TSL;
		ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_TSR;
	} else if (channel_mode == 16) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
		ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
		ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
		ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
		ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
		ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
		ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
		ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_CS;
		ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TS;
		ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_CVH;
		ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_MS;
		ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
		ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
		ep_payload->dev_channel_mapping[14] = PCM_CHANNEL_RLC;
		ep_payload->dev_channel_mapping[15] = PCM_CHANNEL_RRC;
	} else if (channel_mode == 32) {
		ep_payload->dev_channel_mapping[0] = PCM_CHANNEL_FL;
		ep_payload->dev_channel_mapping[1] = PCM_CHANNEL_FR;
		ep_payload->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
		ep_payload->dev_channel_mapping[3] = PCM_CHANNEL_FC;
		ep_payload->dev_channel_mapping[4] = PCM_CHANNEL_LS;
		ep_payload->dev_channel_mapping[5] = PCM_CHANNEL_RS;
		ep_payload->dev_channel_mapping[6] = PCM_CHANNEL_LB;
		ep_payload->dev_channel_mapping[7] = PCM_CHANNEL_RB;
		ep_payload->dev_channel_mapping[8] = PCM_CHANNEL_CS;
		ep_payload->dev_channel_mapping[9] = PCM_CHANNEL_TS;
		ep_payload->dev_channel_mapping[10] = PCM_CHANNEL_CVH;
		ep_payload->dev_channel_mapping[11] = PCM_CHANNEL_MS;
		ep_payload->dev_channel_mapping[12] = PCM_CHANNEL_FLC;
		ep_payload->dev_channel_mapping[13] = PCM_CHANNEL_FRC;
		ep_payload->dev_channel_mapping[14] = PCM_CHANNEL_RLC;
		ep_payload->dev_channel_mapping[15] = PCM_CHANNEL_RRC;
		ep_payload->dev_channel_mapping[16] = PCM_CHANNEL_LFE2;
		ep_payload->dev_channel_mapping[17] = PCM_CHANNEL_SL;
		ep_payload->dev_channel_mapping[18] = PCM_CHANNEL_SR;
		ep_payload->dev_channel_mapping[19] = PCM_CHANNEL_TFL;
		ep_payload->dev_channel_mapping[20] = PCM_CHANNEL_TFR;
		ep_payload->dev_channel_mapping[21] = PCM_CHANNEL_TC;
		ep_payload->dev_channel_mapping[22] = PCM_CHANNEL_TBL;
		ep_payload->dev_channel_mapping[23] = PCM_CHANNEL_TBR;
		ep_payload->dev_channel_mapping[24] = PCM_CHANNEL_TSL;
		ep_payload->dev_channel_mapping[25] = PCM_CHANNEL_TSR;
		ep_payload->dev_channel_mapping[26] = PCM_CHANNEL_TBC;
		ep_payload->dev_channel_mapping[27] = PCM_CHANNEL_BFC;
		ep_payload->dev_channel_mapping[28] = PCM_CHANNEL_BFL;
		ep_payload->dev_channel_mapping[29] = PCM_CHANNEL_BFR;
		ep_payload->dev_channel_mapping[30] = PCM_CHANNEL_LW;
		ep_payload->dev_channel_mapping[31] = PCM_CHANNEL_RW;
	} else {
		pr_err("%s: invalid num_chan %d\n", __func__,
			channel_mode);
		rc = -EINVAL;
	}

	return rc;
}

static int adm_copp_set_ec_ref_mfc_cfg(int port_id, int copp_idx,
					int sample_rate, int bps,
					int in_channels, int out_channels)
{
	struct audproc_mfc_param_media_fmt mfc_cfg;
	struct param_hdr_v3 param_hdr;
	u16 *chmixer_params = NULL;
	int rc = 0, i  = 0, j = 0, param_index = 0, param_size = 0;
	struct adm_device_endpoint_payload ep_payload = {0, 0, 0, {0}};

	memset(&mfc_cfg, 0, sizeof(mfc_cfg));
	memset(&ep_payload, 0, sizeof(ep_payload));
	memset(&param_hdr, 0, sizeof(param_hdr));

	param_hdr.module_id = AUDPROC_MODULE_ID_MFC_EC_REF;
	param_hdr.instance_id = INSTANCE_ID_0;

	pr_debug("%s: port_id %d copp_idx %d SR %d, BW %d in_ch %d out_ch %d\n",
			__func__, port_id, copp_idx, sample_rate,
			bps, in_channels, out_channels);

	if (out_channels <= 0 || out_channels > AUDPROC_MFC_OUT_CHANNELS_MAX) {
		pr_err("%s: unsupported out channels=%d\n", __func__, out_channels);
		return -EINVAL;
	}

	/* 1. Update Media Format */
	param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
	param_hdr.param_size = sizeof(mfc_cfg);

	mfc_cfg.sampling_rate = sample_rate;
	mfc_cfg.bits_per_sample = bps;
	mfc_cfg.num_channels = out_channels;

	ep_payload.dev_num_channel = out_channels;
	rc = adm_arrange_mch_ep2_map_v8(&ep_payload, out_channels);
	if (rc < 0) {
		pr_err("%s: unable to get map for out channels=%d\n",
				__func__, out_channels);
		return -EINVAL;
	}

	for (i = 0; i < out_channels; i++)
		mfc_cfg.channel_type[i] = (uint16_t) ep_payload.dev_channel_mapping[i];


	rc = adm_pack_and_set_one_pp_param(port_id, copp_idx,
				param_hdr, (uint8_t *) &mfc_cfg);
	if (rc) {
		pr_err("%s: Failed to set media format, err %d\n", __func__, rc);
		return rc;
	}

	/* 2. Send Channel Mixer params */
	param_size =  2 * (4 + out_channels + in_channels + (out_channels * in_channels));
	param_size = round_up(param_size, 4);
	param_hdr.param_id = DEFAULT_CHMIXER_PARAM_ID_COEFF;
	param_hdr.param_size = param_size;

	pr_debug("%s: chmixer param sz = %d\n", __func__, param_size);
	chmixer_params = kzalloc(param_size, GFP_KERNEL);
	if (!chmixer_params) {
		return -ENOMEM;
	}
	param_index = 2; /* param[0] and [1] represents chmixer rule(always 0) */
	chmixer_params[param_index++] = out_channels;
	chmixer_params[param_index++] = in_channels;

	/* output channel map is same as one set in media format */
	for (i = 0; i < out_channels; i++)
		chmixer_params[param_index++] = ep_payload.dev_channel_mapping[i];

	/* input channel map should be same as one set for ep2 during copp open */
	ep_payload.dev_num_channel = in_channels;
	rc = adm_arrange_mch_ep2_map_v8(&ep_payload, in_channels);
	if (rc < 0) {
		pr_err("%s: unable to get in channal map\n", __func__);
		goto exit;
	}
	for (i = 0; i < in_channels; i++)
		chmixer_params[param_index++] = ep_payload.dev_channel_mapping[i];

	for (i = 0; i < out_channels; i++)
		for (j = 0; j < in_channels; j++)
		chmixer_params[param_index++] = this_adm.ec_ref_chmixer_weights[i][j];

	rc = adm_pack_and_set_one_pp_param(port_id, copp_idx,
					   param_hdr, (uint8_t *) chmixer_params);
	if (rc)
		pr_err("%s: Failed to set chmixer params, err %d\n", __func__, rc);

exit:
	kfree(chmixer_params);
	return rc;
}

/**
 * adm_open -
 *        command to send ADM open
 *
 * @port_id: port id number
 * @path: direction or ADM path type
 * @rate: sample rate of session
 * @channel_mode: number of channels set
 * @topology: topology active for this session
 * @perf_mode: performance mode like LL/ULL/..
 * @bit_width: bit width to set for copp
 * @app_type: App type used for this session
 * @acdb_id: ACDB ID of this device
 * @session_type: type of session
 *
 * Returns 0 on success or error on failure
 */
int adm_open(int port_id, int path, int rate, int channel_mode, int topology,
	     int perf_mode, uint16_t bit_width, int app_type, int acdb_id,
	     int session_type, uint32_t passthr_mode)
{
	struct adm_cmd_device_open_v5	open;
	struct adm_cmd_device_open_v6	open_v6;
	struct adm_cmd_device_open_v8	open_v8;
	struct adm_device_endpoint_payload ep1_payload;
	struct adm_device_endpoint_payload ep2_payload;
	int ep1_payload_size = 0;
	int ep2_payload_size = 0;
	int ret = 0;
	int port_idx, flags;
	int copp_idx = -1;
	int tmp_port = q6audio_get_port_id(port_id);
	void *adm_params = NULL;
	int param_size;
	int num_ec_ref_rx_chans = this_adm.num_ec_ref_rx_chans;

	pr_debug("%s:port %#x path:%d rate:%d mode:%d perf_mode:%d,topo_id %d\n",
		 __func__, port_id, path, rate, channel_mode, perf_mode,
		 topology);

	port_id = q6audio_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
		return -EINVAL;
	}
	if (channel_mode < 0 || channel_mode > 32) {
		pr_err("%s: Invalid channel number 0x%x\n",
				__func__, channel_mode);
		return -EINVAL;
	}

	if (this_adm.apr == NULL) {
		this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
						0xFFFFFFFF, &this_adm);
		if (this_adm.apr == NULL) {
			pr_err("%s: Unable to register ADM\n", __func__);
			return -ENODEV;
		}
		rtac_set_adm_handle(this_adm.apr);
	}

	if (perf_mode == ULL_POST_PROCESSING_PCM_MODE) {
		flags = ADM_ULL_POST_PROCESSING_DEVICE_SESSION;
		if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
		    (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
		    (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
			topology = DEFAULT_COPP_TOPOLOGY;
	} else if (perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
		flags = ADM_ULTRA_LOW_LATENCY_DEVICE_SESSION;
		topology = NULL_COPP_TOPOLOGY;
		rate = ULL_SUPPORTED_SAMPLE_RATE;
		bit_width = ULL_SUPPORTED_BITS_PER_SAMPLE;
	} else if (perf_mode == LOW_LATENCY_PCM_MODE) {
		flags = ADM_LOW_LATENCY_DEVICE_SESSION;
		if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
		    (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
		    (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
			topology = DEFAULT_COPP_TOPOLOGY;
	} else {
		if ((path == ADM_PATH_COMPRESSED_RX) ||
		    (path == ADM_PATH_COMPRESSED_TX))
			flags = 0;
		else
			flags = ADM_LEGACY_DEVICE_SESSION;
	}

	if ((topology == VPM_TX_SM_ECNS_V2_COPP_TOPOLOGY) ||
	    (topology == VPM_TX_DM_FLUENCE_EF_COPP_TOPOLOGY)) {
		if ((rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_8K) &&
		    (rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_16K) &&
		    (rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_32K) &&
		    (rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_48K))
			rate = 16000;
	}
	if ((topology == VPM_TX_DM_FLUENCE_COPP_TOPOLOGY) ||
	    (topology == VPM_TX_DM_RFECNS_COPP_TOPOLOGY)) {
		if ((rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_8K) &&
		    (rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_16K) &&
		    (rate != ADM_CMD_COPP_OPEN_SAMPLE_RATE_32K))
			rate = 16000;
	}

	if (topology == FFECNS_TOPOLOGY) {
		this_adm.ffecns_port_id = port_id;
		pr_debug("%s: ffecns port id =%x\n", __func__,
				this_adm.ffecns_port_id);
	}

	if (topology == VPM_TX_VOICE_SMECNS_V2_COPP_TOPOLOGY ||
	    topology == VPM_TX_VOICE_FLUENCE_SM_COPP_TOPOLOGY)
		channel_mode = 1;

	/*
	 * Routing driver reuses the same adm for streams with the same
	 * app_type, sample_rate etc.
	 * This isn't allowed for ULL streams as per the DSP interface
	 */
	if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
		copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
						      perf_mode,
						      rate, bit_width,
						      app_type, session_type);

	if (copp_idx < 0) {
		copp_idx = adm_get_next_available_copp(port_idx);
		if (copp_idx >= MAX_COPPS_PER_PORT) {
			pr_err("%s: exceeded copp id %d\n",
				 __func__, copp_idx);
			return -EINVAL;
		}
		atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
		atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
			   topology);
		atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
			   perf_mode);
		atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
			   rate);
		atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
			   channel_mode);
		atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
			   bit_width);
		atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
			   app_type);
		atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
			   acdb_id);
		atomic_set(&this_adm.copp.session_type[port_idx][copp_idx],
			   session_type);
		set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
		(void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
		if ((path != ADM_PATH_COMPRESSED_RX) &&
		    (path != ADM_PATH_COMPRESSED_TX))
			send_adm_custom_topology();
	}

	if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
		perf_mode == LEGACY_PCM_MODE) {
		atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
			   1);
		this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
		wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
	}

	/* Create a COPP if port id are not enabled */
	if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
		pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
			 port_idx, copp_idx);
		if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
		      perf_mode == LEGACY_PCM_MODE) {
			int res;

			atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
			msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
			res = adm_memory_map_regions(
					&this_adm.outband_memmap.paddr, 0,
			(uint32_t *)&this_adm.outband_memmap.size, 1);
			if (res < 0) {
				pr_err("%s: SRS adm_memory_map_regions failed! addr = 0x%pK, size = %d\n",
					__func__,
					(void *)this_adm.outband_memmap.paddr,
					(uint32_t)this_adm.outband_memmap.size);
			}
		}


		if ((q6core_get_avcs_api_version_per_service(
				APRV2_IDS_SERVICE_ID_ADSP_ADM_V) >=
					ADSP_ADM_API_VERSION_V3)) {
			memset(&open_v8, 0, sizeof(open_v8));
			memset(&ep1_payload, 0, sizeof(ep1_payload));
			memset(&ep2_payload, 0, sizeof(ep2_payload));

			open_v8.hdr.hdr_field = APR_HDR_FIELD(
					APR_MSG_TYPE_SEQ_CMD,
					APR_HDR_LEN(APR_HDR_SIZE),
					APR_PKT_VER);
			open_v8.hdr.src_svc = APR_SVC_ADM;
			open_v8.hdr.src_domain = APR_DOMAIN_APPS;
			open_v8.hdr.src_port = tmp_port;
			open_v8.hdr.dest_svc = APR_SVC_ADM;
			open_v8.hdr.dest_domain = APR_DOMAIN_ADSP;
			open_v8.hdr.dest_port = tmp_port;
			open_v8.hdr.token = port_idx << 16 | copp_idx;
			open_v8.hdr.opcode = ADM_CMD_DEVICE_OPEN_V8;

			if (this_adm.native_mode != 0) {
				open_v8.flags = flags |
					(this_adm.native_mode << 11);
				this_adm.native_mode = 0;
			} else {
				open_v8.flags = flags;
			}
			open_v8.mode_of_operation = path;
			open_v8.endpoint_id_1 = tmp_port;
			open_v8.endpoint_id_2 = 0xFFFF;
			open_v8.endpoint_id_3 = 0xFFFF;

			if (((this_adm.ec_ref_rx & AFE_PORT_INVALID) !=
				AFE_PORT_INVALID) &&
				(path != ADM_PATH_PLAYBACK)) {
				if (this_adm.num_ec_ref_rx_chans != 0) {
					open_v8.endpoint_id_2 =
						this_adm.ec_ref_rx;
					this_adm.ec_ref_rx = AFE_PORT_INVALID;
				} else {
					pr_err("%s: EC channels not set %d\n",
						__func__,
						this_adm.num_ec_ref_rx_chans);
					return -EINVAL;
				}
			}

			open_v8.topology_id = topology;
			open_v8.compressed_data_type = 0;
			if (passthr_mode == COMPRESSED_PASSTHROUGH_DSD)
				open_v8.compressed_data_type = 1;

			/* variable endpoint payload */
			ep1_payload.dev_num_channel = channel_mode & 0x00FF;
			ep1_payload.bit_width = bit_width;
			ep1_payload.sample_rate  = rate;
			ret = adm_arrange_mch_map_v8(&ep1_payload, path,
					channel_mode, port_idx);
			if (ret)
				return ret;

			pr_debug("%s: port_id=0x%x %x %x topology_id=0x%X flags %x ref_ch %x\n",
				__func__, open_v8.endpoint_id_1,
				open_v8.endpoint_id_2,
				open_v8.endpoint_id_3,
				open_v8.topology_id,
				open_v8.flags,
				this_adm.num_ec_ref_rx_chans);

			ep1_payload_size = 8 +
				roundup(ep1_payload.dev_num_channel, 4);
			param_size = sizeof(struct adm_cmd_device_open_v8)
				+ ep1_payload_size;
			atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);

			if ((this_adm.num_ec_ref_rx_chans != 0)
				&& (path != ADM_PATH_PLAYBACK)
				&& (open_v8.endpoint_id_2 != 0xFFFF)) {
				ep2_payload.dev_num_channel =
					this_adm.num_ec_ref_rx_chans;

				if (this_adm.ec_ref_rx_bit_width != 0) {
					ep2_payload.bit_width =
						this_adm.ec_ref_rx_bit_width;
				} else {
					ep2_payload.bit_width = bit_width;
				}

				if (this_adm.ec_ref_rx_sampling_rate != 0) {
					ep2_payload.sample_rate =
					this_adm.ec_ref_rx_sampling_rate;
				} else {
					ep2_payload.sample_rate = rate;
				}

				pr_debug("%s: adm open_v8 eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
					__func__,
					ep2_payload.dev_num_channel,
					ep2_payload.bit_width,
					ep2_payload.sample_rate);

				ret = adm_arrange_mch_ep2_map_v8(&ep2_payload,
					ep2_payload.dev_num_channel);

				if (ret)
					return ret;
				ep2_payload_size = 8 +
					roundup(ep2_payload.dev_num_channel, 4);
				param_size += ep2_payload_size;
			}

			open_v8.hdr.pkt_size = param_size;
			adm_params = kzalloc(param_size, GFP_KERNEL);
			if (!adm_params)
				return -ENOMEM;
			memcpy(adm_params, &open_v8, sizeof(open_v8));
			memcpy(adm_params + sizeof(open_v8),
					(void *)&ep1_payload,
					ep1_payload_size);

			if ((this_adm.num_ec_ref_rx_chans != 0)
				&& (path != ADM_PATH_PLAYBACK)
				&& (open_v8.endpoint_id_2 != 0xFFFF)) {
				memcpy(adm_params + sizeof(open_v8)
						+ ep1_payload_size,
						(void *)&ep2_payload,
						ep2_payload_size);
			}

			ret = adm_apr_send_pkt((uint32_t *) adm_params,
				&this_adm.copp.wait[port_idx][copp_idx],
				port_idx, copp_idx, open_v8.hdr.opcode);
			if (ret < 0) {
				pr_err("%s: port_id: 0x%x for[0x%x] failed %d for open_v8\n",
					__func__, tmp_port, port_id, ret);
				return -EINVAL;
			}
			kfree(adm_params);
		} else {

			open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
				APR_HDR_LEN(APR_HDR_SIZE),
				APR_PKT_VER);
			open.hdr.pkt_size = sizeof(open);
			open.hdr.src_svc = APR_SVC_ADM;
			open.hdr.src_domain = APR_DOMAIN_APPS;
			open.hdr.src_port = tmp_port;
			open.hdr.dest_svc = APR_SVC_ADM;
			open.hdr.dest_domain = APR_DOMAIN_ADSP;
			open.hdr.dest_port = tmp_port;
			open.hdr.token = port_idx << 16 | copp_idx;
			open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
			open.flags = flags;
			open.mode_of_operation = path;
			open.endpoint_id_1 = tmp_port;
			open.endpoint_id_2 = 0xFFFF;

			if (this_adm.ec_ref_rx && (path != 1) &&
			    (afe_get_port_type(tmp_port) == MSM_AFE_PORT_TYPE_TX)) {
				open.endpoint_id_2 = this_adm.ec_ref_rx;
			}

			open.topology_id = topology;

			open.dev_num_channel = channel_mode & 0x00FF;
			open.bit_width = bit_width;
			WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
				(rate != ULL_SUPPORTED_SAMPLE_RATE));
			open.sample_rate  = rate;

			ret = adm_arrange_mch_map(&open, path, channel_mode,
						  port_idx);
			if (ret)
				return ret;

			pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
				__func__, open.endpoint_id_1, open.sample_rate,
				open.topology_id);

			atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);

			if ((this_adm.num_ec_ref_rx_chans != 0) &&
				(path != 1) && (open.endpoint_id_2 != 0xFFFF)) {
				memset(&open_v6, 0,
					sizeof(struct adm_cmd_device_open_v6));
				memcpy(&open_v6, &open,
					sizeof(struct adm_cmd_device_open_v5));
				open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
				open_v6.hdr.pkt_size = sizeof(open_v6);
				open_v6.dev_num_channel_eid2 =
					this_adm.num_ec_ref_rx_chans;

				if (this_adm.ec_ref_rx_bit_width != 0) {
					open_v6.bit_width_eid2 =
						this_adm.ec_ref_rx_bit_width;
				} else {
					open_v6.bit_width_eid2 = bit_width;
				}

				if (this_adm.ec_ref_rx_sampling_rate != 0) {
					open_v6.sample_rate_eid2 =
					       this_adm.ec_ref_rx_sampling_rate;
				} else {
					open_v6.sample_rate_eid2 = rate;
				}

				pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
					__func__, open_v6.dev_num_channel_eid2,
					open_v6.bit_width_eid2,
					open_v6.sample_rate_eid2);

				ret = adm_arrange_mch_ep2_map(&open_v6,
					open_v6.dev_num_channel_eid2);

				if (ret)
					return ret;

				ret = adm_apr_send_pkt((uint32_t *) &open_v6,
					&this_adm.copp.wait[port_idx][copp_idx],
					port_idx, copp_idx, open_v6.hdr.opcode);
			} else {
				ret = adm_apr_send_pkt((uint32_t *) &open,
					&this_adm.copp.wait[port_idx][copp_idx],
					port_idx, copp_idx, open.hdr.opcode);
			}
			if (ret < 0) {
				pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
					__func__, tmp_port, port_id, ret);
				return -EINVAL;
			}
		}
	}
	atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);

	/*
	 * Configure MFC(in ec_ref path) if chmixing param is applicable and set.
	 * Except channels and channel maps the media format config for this module
	 * should match with the COPP(EP1) config values.
	 */
	if (path != ADM_PATH_PLAYBACK &&
		this_adm.num_ec_ref_rx_chans_downmixed != 0 &&
		num_ec_ref_rx_chans != this_adm.num_ec_ref_rx_chans_downmixed) {
		ret = adm_copp_set_ec_ref_mfc_cfg(port_id, copp_idx,
				rate, bit_width, num_ec_ref_rx_chans,
				this_adm.num_ec_ref_rx_chans_downmixed);
		this_adm.num_ec_ref_rx_chans_downmixed = 0;
		if (ret)
			pr_err("%s: set EC REF MFC cfg failed, err %d\n", __func__, ret);
	}

	return copp_idx;
}
EXPORT_SYMBOL(adm_open);

/**
 * adm_copp_mfc_cfg -
 *        command to send ADM MFC config
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @dst_sample_rate: sink sample rate
 *
 */
void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
{
	struct audproc_mfc_param_media_fmt mfc_cfg;
	struct adm_cmd_device_open_v5 open;
	struct param_hdr_v3 param_hdr;
	int port_idx;
	int rc  = 0;
	int i  = 0;

	port_id = q6audio_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);

	if (port_idx < 0) {
		pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
		goto fail_cmd;
	}

	if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
		goto fail_cmd;
	}

	memset(&mfc_cfg, 0, sizeof(mfc_cfg));
	memset(&open, 0, sizeof(open));
	memset(&param_hdr, 0, sizeof(param_hdr));

	param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
	param_hdr.param_size = sizeof(mfc_cfg);

	mfc_cfg.sampling_rate = dst_sample_rate;
	mfc_cfg.bits_per_sample =
		atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
	open.dev_num_channel = mfc_cfg.num_channels =
		atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);

	rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
		mfc_cfg.num_channels, port_idx);
	if (rc < 0) {
		pr_err("%s: unable to get channal map\n", __func__);
		goto fail_cmd;
	}

	for (i = 0; i < mfc_cfg.num_channels; i++)
		mfc_cfg.channel_type[i] =
			(uint16_t) open.dev_channel_mapping[i];

	atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);

	pr_debug("%s: mfc config: port_idx %d copp_idx  %d copp SR %d copp BW %d copp chan %d o/p SR %d\n",
			__func__, port_idx, copp_idx,
			atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
			mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
			mfc_cfg.sampling_rate);

	rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
					   (uint8_t *) &mfc_cfg);
	if (rc)
		pr_err("%s: Failed to set media format configuration data, err %d\n",
		       __func__, rc);

fail_cmd:
	return;
}
EXPORT_SYMBOL(adm_copp_mfc_cfg);

static void route_set_opcode_matrix_id(
			struct adm_cmd_matrix_map_routings_v5 **route_addr,
			int path, uint32_t passthr_mode)
{
	struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;

	switch (path) {
	case ADM_PATH_PLAYBACK:
		route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
		route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
		break;
	case ADM_PATH_LIVE_REC:
		if (passthr_mode == LISTEN) {
			route->hdr.opcode =
				ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
			route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
			break;
		}
		/* fall through to set matrix id for non-listen case */
	case ADM_PATH_NONLIVE_REC:
		route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
		route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
		break;
	case ADM_PATH_COMPRESSED_RX:
		route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
		route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
		break;
	case ADM_PATH_COMPRESSED_TX:
		route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
		route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
		break;
	default:
		pr_err("%s: Wrong path set[%d]\n", __func__, path);
		break;
	}
	pr_debug("%s: opcode 0x%x, matrix id %d\n",
		 __func__, route->hdr.opcode, route->matrix_id);
}

/**
 * adm_matrix_map -
 *        command to send ADM matrix map for ADM copp list
 *
 * @path: direction or ADM path type
 * @payload_map: have info of session id and associated copp_idx/num_copps
 * @perf_mode: performance mode like LL/ULL/..
 * @passthr_mode: flag to indicate passthrough mode
 *
 * Returns 0 on success or error on failure
 */
int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
			uint32_t passthr_mode)
{
	struct adm_cmd_matrix_map_routings_v5	*route;
	struct adm_session_map_node_v5 *node;
	uint16_t *copps_list;
	int cmd_size = 0;
	int ret = 0, i = 0;
	void *payload = NULL;
	void *matrix_map = NULL;
	int port_idx, copp_idx;

	/* Assumes port_ids have already been validated during adm_open */
	cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
			sizeof(struct adm_session_map_node_v5) +
			(sizeof(uint32_t) * payload_map.num_copps));
	matrix_map = kzalloc(cmd_size, GFP_KERNEL);
	if (matrix_map == NULL) {
		pr_err("%s: Mem alloc failed\n", __func__);
		ret = -EINVAL;
		return ret;
	}
	route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;

	route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
				APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
	route->hdr.pkt_size = cmd_size;
	route->hdr.src_svc = 0;
	route->hdr.src_domain = APR_DOMAIN_APPS;
	route->hdr.src_port = 0; /* Ignored */;
	route->hdr.dest_svc = APR_SVC_ADM;
	route->hdr.dest_domain = APR_DOMAIN_ADSP;
	route->hdr.dest_port = 0; /* Ignored */;
	route->hdr.token = 0;
	route->num_sessions = 1;
	route_set_opcode_matrix_id(&route, path, passthr_mode);

	payload = ((u8 *)matrix_map +
			sizeof(struct adm_cmd_matrix_map_routings_v5));
	node = (struct adm_session_map_node_v5 *)payload;

	node->session_id = payload_map.session_id;
	node->num_copps = payload_map.num_copps;
	payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
	copps_list = (uint16_t *)payload;
	for (i = 0; i < payload_map.num_copps; i++) {
		port_idx =
		adm_validate_and_get_port_index(payload_map.port_id[i]);
		if (port_idx < 0) {
			pr_err("%s: Invalid port_id 0x%x\n", __func__,
				payload_map.port_id[i]);
			ret = -EINVAL;
			goto fail_cmd;
		}
		copp_idx = payload_map.copp_idx[i];
		copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
							     [copp_idx]);
	}
	atomic_set(&this_adm.matrix_map_stat, -1);

	ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
	if (ret < 0) {
		pr_err("%s: routing for syream %d failed ret %d\n",
			__func__, payload_map.session_id, ret);
		ret = -EINVAL;
		goto fail_cmd;
	}
	ret = wait_event_timeout(this_adm.matrix_map_wait,
				atomic_read(&this_adm.matrix_map_stat) >= 0,
				msecs_to_jiffies(TIMEOUT_MS));
	if (!ret) {
		pr_err("%s: routing for syream %d failed\n", __func__,
			payload_map.session_id);
		ret = -EINVAL;
		goto fail_cmd;
	} else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
		pr_err("%s: DSP returned error[%s]\n", __func__,
			adsp_err_get_err_str(atomic_read(
			&this_adm.matrix_map_stat)));
		ret = adsp_err_get_lnx_err_code(
				atomic_read(&this_adm.matrix_map_stat));
		goto fail_cmd;
	}

	if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
		 (path != ADM_PATH_COMPRESSED_RX)) {
		for (i = 0; i < payload_map.num_copps; i++) {
			port_idx = afe_get_port_index(payload_map.port_id[i]);
			copp_idx = payload_map.copp_idx[i];
			if (port_idx < 0 || copp_idx < 0 ||
			    (copp_idx > MAX_COPPS_PER_PORT - 1)) {
				pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
					__func__, port_idx, copp_idx);
				continue;
			}
			rtac_add_adm_device(payload_map.port_id[i],
					    atomic_read(&this_adm.copp.id
							[port_idx][copp_idx]),
					    get_cal_path(path),
					    payload_map.session_id,
					    payload_map.app_type[i],
					    payload_map.acdb_dev_id[i]);

			if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
				(void *)&this_adm.copp.adm_status[port_idx]
								[copp_idx])) {
				pr_debug("%s: adm copp[0x%x][%d] already sent",
						__func__, port_idx, copp_idx);
				continue;
			}
			send_adm_cal(payload_map.port_id[i], copp_idx,
				     get_cal_path(path), perf_mode,
				     payload_map.app_type[i],
				     payload_map.acdb_dev_id[i],
				     payload_map.sample_rate[i],
				     passthr_mode);
			/* ADM COPP calibration is already sent */
			clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
				(void *)&this_adm.copp.
				adm_status[port_idx][copp_idx]);
			pr_debug("%s: copp_id: %d\n", __func__,
				 atomic_read(&this_adm.copp.id[port_idx]
							      [copp_idx]));
		}
	}

fail_cmd:
	kfree(matrix_map);
	return ret;
}
EXPORT_SYMBOL(adm_matrix_map);

/**
 * adm_ec_ref_rx_id -
 *        Update EC ref port ID
 *
 */
void adm_ec_ref_rx_id(int port_id)
{
	this_adm.ec_ref_rx = port_id;
	pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
}
EXPORT_SYMBOL(adm_ec_ref_rx_id);

/**
 * adm_num_ec_ref_rx_chans -
 *        Update EC ref number of channels
 *
 */
void adm_num_ec_ref_rx_chans(int num_chans)
{
	this_adm.num_ec_ref_rx_chans = num_chans;
	pr_debug("%s: num_ec_ref_rx_chans:%d\n",
		__func__, this_adm.num_ec_ref_rx_chans);
}
EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);

/**
 * adm_num_ec_rx_ref_chans_downmixed -
 *        Update EC ref num of channels(downmixed) to be fed to EC algo
 *
 */
void adm_num_ec_ref_rx_chans_downmixed(int num_chans)
{
	this_adm.num_ec_ref_rx_chans_downmixed = num_chans;
	pr_debug("%s: num_ec_ref_rx_chans_downmixed:%d\n",
		__func__, this_adm.num_ec_ref_rx_chans_downmixed);
}
EXPORT_SYMBOL(adm_num_ec_ref_rx_chans_downmixed);

/**
 * adm_ec_ref_chmixer_weights -
 *        Update MFC(in ec ref) Channel Mixer Weights to be used
 *        for downmixing rx channels before feeding them to EC algo
 * @out_channel_idx: index of output channel to which weightages are applicable
 * @weights:         pointer to array having input weightages
 * @count:           array sizeof pointer weights, max supported value is
 *                   PCM_FORMAT_MAX_NUM_CHANNEL_V8
 * Returns 0 on success or error on failure
 */
int adm_ec_ref_chmixer_weights(int out_channel_idx,
				uint16_t *weights, int count)
{
	int i = 0;

	if (weights == NULL || count <= 0 || out_channel_idx < 0 ||
		count > PCM_FORMAT_MAX_NUM_CHANNEL_V8 ||
		out_channel_idx >= PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
		pr_err("%s: invalid weightages count(%d) ch_idx(%d)",
				__func__, count, out_channel_idx);
		return -EINVAL;
	}

	for (i = 0; i < count; i++) {
		this_adm.ec_ref_chmixer_weights[out_channel_idx][i] = weights[i];
		pr_debug("%s: out ch idx :%d, weight[%d] = %d\n",
			__func__, out_channel_idx, i, weights[i]);
	}

	return 0;
}
EXPORT_SYMBOL(adm_ec_ref_chmixer_weights);

/**
 * adm_ec_ref_rx_bit_width -
 *        Update EC ref bit_width
 *
 */
void adm_ec_ref_rx_bit_width(int bit_width)
{
	this_adm.ec_ref_rx_bit_width = bit_width;
	pr_debug("%s: ec_ref_rx_bit_width:%d\n",
		__func__, this_adm.ec_ref_rx_bit_width);
}
EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);

/**
 * adm_ec_ref_rx_sampling_rate -
 *        Update EC ref sample rate
 *
 */
void adm_ec_ref_rx_sampling_rate(int sampling_rate)
{
	this_adm.ec_ref_rx_sampling_rate = sampling_rate;
	pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
		__func__, this_adm.ec_ref_rx_sampling_rate);
}
EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);

/**
 * adm_set_native_mode -
 *      Set adm channel native mode.
 *      If enabled matrix mixer will be
 *      running in native mode for channel
 *      configuration for this device session.
 *
 */
void adm_set_native_mode(int mode)
{
	this_adm.native_mode = mode;
	pr_debug("%s: enable native_mode :%d\n",
		__func__, this_adm.native_mode);
}
EXPORT_SYMBOL(adm_set_native_mode);

/**
 * adm_close -
 *        command to close ADM copp
 *
 * @port_id: Port ID number
 * @perf_mode: performance mode like LL/ULL/..
 * @copp_idx: copp index assigned
 *
 * Returns 0 on success or error on failure
 */
int adm_close(int port_id, int perf_mode, int copp_idx)
{
	struct apr_hdr close;

	int ret = 0, port_idx;
	int copp_id = RESET_COPP_ID;

	pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
		 port_id, perf_mode, copp_idx);

	port_id = q6audio_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id 0x%x\n",
			__func__, port_id);
		return -EINVAL;
	}

	if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
		pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
		return -EINVAL;
	}

	port_channel_map[port_idx].set_channel_map = false;
	if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
		== LEGACY_PCM_MODE) {
		atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
			   1);
		this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
		wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
	}

	atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
	if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
		copp_id = adm_get_copp_id(port_idx, copp_idx);
		pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
			 __func__, port_idx, copp_idx, copp_id);
		if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
		    (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
			SRS_TRUMEDIA_TOPOLOGY_ID)) {
			atomic_set(&this_adm.mem_map_index,
				ADM_SRS_TRUMEDIA);
			ret = adm_memory_unmap_regions();
			if (ret < 0) {
				pr_err("%s: adm mem unmmap err %d",
					__func__, ret);
			} else {
				atomic_set(&this_adm.mem_map_handles
					   [ADM_SRS_TRUMEDIA], 0);
			}
		}


		if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
		    this_adm.sourceTrackingData.memmap.paddr) {
			atomic_set(&this_adm.mem_map_index,
				   ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
			ret = adm_memory_unmap_regions();
			if (ret < 0) {
				pr_err("%s: adm mem unmmap err %d",
					__func__, ret);
			}
			msm_audio_ion_free(
				this_adm.sourceTrackingData.dma_buf);
			this_adm.sourceTrackingData.dma_buf = NULL;
			this_adm.sourceTrackingData.memmap.size = 0;
			this_adm.sourceTrackingData.memmap.kvaddr = NULL;
			this_adm.sourceTrackingData.memmap.paddr = 0;
			this_adm.sourceTrackingData.apr_cmd_status = -1;
			atomic_set(&this_adm.mem_map_handles[
					ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
		}

		close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
						APR_HDR_LEN(APR_HDR_SIZE),
						APR_PKT_VER);
		close.pkt_size = sizeof(close);
		close.src_svc = APR_SVC_ADM;
		close.src_domain = APR_DOMAIN_APPS;
		close.src_port = port_id;
		close.dest_svc = APR_SVC_ADM;
		close.dest_domain = APR_DOMAIN_ADSP;
		close.dest_port = copp_id;
		close.token = port_idx << 16 | copp_idx;
		close.opcode = ADM_CMD_DEVICE_CLOSE_V5;

		atomic_set(&this_adm.copp.id[port_idx][copp_idx],
			   RESET_COPP_ID);
		atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
		atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
		atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
		atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
		atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
		atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
		atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
		atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
		atomic_set(&this_adm.copp.session_type[port_idx][copp_idx], 0);

		clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
			(void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
		ret = adm_apr_send_pkt((uint32_t *) &close,
			&this_adm.copp.wait[port_idx][copp_idx],
			port_idx, copp_idx, close.opcode);
		if (ret < 0) {
			pr_err("%s: ADM close failed %d\n", __func__, ret);
			return -EINVAL;
		}
	}

	if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
		pr_debug("%s: remove adm device from rtac\n", __func__);
		rtac_remove_adm_device(port_id, copp_id);
	}

	if (port_id == this_adm.ffecns_port_id)
		this_adm.ffecns_port_id = -1;

	return 0;
}
EXPORT_SYMBOL(adm_close);

int send_rtac_audvol_cal(void)
{
	int ret = 0;
	int ret2 = 0;
	int i = 0;
	int copp_idx, port_idx, acdb_id, app_id, path;
	struct cal_block_data *cal_block = NULL;
	struct audio_cal_info_audvol *audvol_cal_info = NULL;
	struct rtac_adm rtac_adm_data;

	mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);

	cal_block = cal_utils_get_only_cal_block(
		this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
	if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
		pr_err("%s: can't find cal block!\n", __func__);
		goto unlock;
	}

	audvol_cal_info = cal_block->cal_info;
	if (audvol_cal_info == NULL) {
		pr_err("%s: audvol_cal_info is NULL!\n", __func__);
		goto unlock;
	}

	get_rtac_adm_data(&rtac_adm_data);
	for (; i < rtac_adm_data.num_of_dev; i++) {

		acdb_id = rtac_adm_data.device[i].acdb_dev_id;
		if (acdb_id == 0)
			acdb_id = audvol_cal_info->acdb_id;

		app_id = rtac_adm_data.device[i].app_type;
		if (app_id == 0)
			app_id = audvol_cal_info->app_type;

		path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
		if ((acdb_id == audvol_cal_info->acdb_id) &&
			(app_id == audvol_cal_info->app_type) &&
			(path == audvol_cal_info->path)) {

			if (adm_get_indexes_from_copp_id(rtac_adm_data.
				device[i].copp, &copp_idx, &port_idx) != 0) {
				pr_debug("%s: Copp Id %d is not active\n",
					__func__,
					rtac_adm_data.device[i].copp);
				continue;
			}

			ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
				rtac_adm_data.device[i].afe_port,
				copp_idx, cal_block,
				atomic_read(&this_adm.copp.
				mode[port_idx][copp_idx]),
				audvol_cal_info->app_type,
				audvol_cal_info->acdb_id,
				atomic_read(&this_adm.copp.
				rate[port_idx][copp_idx]));
			if (ret2 < 0) {
				pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
					__func__, rtac_adm_data.device[i].copp,
					audvol_cal_info->acdb_id,
					audvol_cal_info->app_type,
					audvol_cal_info->path);
				ret = ret2;
			}
		}
	}
unlock:
	mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
	return ret;
}

int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
{
	int result = 0;

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

	if (cal_block == NULL) {
		pr_err("%s: cal_block is NULL!\n",
			__func__);
		result = -EINVAL;
		goto done;
	}

	if (cal_block->cal_data.paddr == 0) {
		pr_debug("%s: No address to map!\n",
			__func__);
		result = -EINVAL;
		goto done;
	}

	if (cal_block->map_data.map_size == 0) {
		pr_debug("%s: map size is 0!\n",
			__func__);
		result = -EINVAL;
		goto done;
	}

	/* valid port ID needed for callback use primary I2S */
	atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
	result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
					&cal_block->map_data.map_size, 1);
	if (result < 0) {
		pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
			__func__,
			cal_block->map_data.map_size, result);
		pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
			__func__,
			&cal_block->cal_data.paddr,
			cal_block->map_data.map_size);
		goto done;
	}

	cal_block->map_data.map_handle = atomic_read(
		&this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
done:
	return result;
}

int adm_unmap_rtac_block(uint32_t *mem_map_handle)
{
	int result = 0;

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

	if (mem_map_handle == NULL) {
		pr_debug("%s: Map handle is NULL, nothing to unmap\n",
			__func__);
		goto done;
	}

	if (*mem_map_handle == 0) {
		pr_debug("%s: Map handle is 0, nothing to unmap\n",
			__func__);
		goto done;
	}

	if (*mem_map_handle != atomic_read(
			&this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
		pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
			__func__, *mem_map_handle, atomic_read(
			&this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));

		/* if mismatch use handle passed in to unmap */
		atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
			   *mem_map_handle);
	}

	/* valid port ID needed for callback use primary I2S */
	atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
	result = adm_memory_unmap_regions();
	if (result < 0) {
		pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
			__func__, result);
	} else {
		atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
		*mem_map_handle = 0;
	}
done:
	return result;
}

static int get_cal_type_index(int32_t cal_type)
{
	int ret = -EINVAL;

	switch (cal_type) {
	case ADM_AUDPROC_CAL_TYPE:
		ret = ADM_AUDPROC_CAL;
		break;
	case ADM_LSM_AUDPROC_CAL_TYPE:
		ret = ADM_LSM_AUDPROC_CAL;
		break;
	case ADM_AUDVOL_CAL_TYPE:
		ret = ADM_AUDVOL_CAL;
		break;
	case ADM_CUST_TOPOLOGY_CAL_TYPE:
		ret = ADM_CUSTOM_TOP_CAL;
		break;
	case ADM_RTAC_INFO_CAL_TYPE:
		ret = ADM_RTAC_INFO_CAL;
		break;
	case ADM_RTAC_APR_CAL_TYPE:
		ret = ADM_RTAC_APR_CAL;
		break;
	case ADM_RTAC_AUDVOL_CAL_TYPE:
		ret = ADM_RTAC_AUDVOL_CAL;
		break;
	case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
		ret = ADM_LSM_AUDPROC_PERSISTENT_CAL;
		break;
	case ADM_AUDPROC_PERSISTENT_CAL_TYPE:
		ret = ADM_AUDPROC_PERSISTENT_CAL;
		break;
	default:
		pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
	}
	return ret;
}

static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
{
	int ret = 0;
	int cal_index;

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

	cal_index = get_cal_type_index(cal_type);
	if (cal_index < 0) {
		pr_err("%s: could not get cal index %d!\n",
			__func__, cal_index);
		ret = -EINVAL;
		goto done;
	}

	ret = cal_utils_alloc_cal(data_size, data,
		this_adm.cal_data[cal_index], 0, NULL);
	if (ret < 0) {
		pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
			__func__, ret, cal_type);
		ret = -EINVAL;
		goto done;
	}
done:
	return ret;
}

static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
{
	int ret = 0;
	int cal_index;

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

	cal_index = get_cal_type_index(cal_type);
	if (cal_index < 0) {
		pr_err("%s: could not get cal index %d!\n",
			__func__, cal_index);
		ret = -EINVAL;
		goto done;
	}

	ret = cal_utils_dealloc_cal(data_size, data,
		this_adm.cal_data[cal_index]);
	if (ret < 0) {
		pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
			__func__, ret, cal_type);
		ret = -EINVAL;
		goto done;
	}
done:
	return ret;
}

static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
{
	int ret = 0;
	int cal_index;

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

	cal_index = get_cal_type_index(cal_type);
	if (cal_index < 0) {
		pr_err("%s: could not get cal index %d!\n",
			__func__, cal_index);
		ret = -EINVAL;
		goto done;
	}

	ret = cal_utils_set_cal(data_size, data,
		this_adm.cal_data[cal_index], 0, NULL);
	if (ret < 0) {
		pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
			__func__, ret, cal_type);
		ret = -EINVAL;
		goto done;
	}

	if (cal_index == ADM_CUSTOM_TOP_CAL) {
		mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
		this_adm.set_custom_topology = 1;
		mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
	} else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
		send_rtac_audvol_cal();
	}
done:
	return ret;
}

static int adm_map_cal_data(int32_t cal_type,
			struct cal_block_data *cal_block)
{
	int ret = 0;
	int cal_index;

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

	cal_index = get_cal_type_index(cal_type);
	if (cal_index < 0) {
		pr_err("%s: could not get cal index %d!\n",
			__func__, cal_index);
		ret = -EINVAL;
		goto done;
	}

	atomic_set(&this_adm.mem_map_index, cal_index);
	ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
		(uint32_t *)&cal_block->map_data.map_size, 1);
	if (ret < 0) {
		pr_err("%s: map did not work! cal_type %i ret %d\n",
			__func__, cal_index, ret);
		ret = -ENODEV;
		goto done;
	}
	cal_block->map_data.q6map_handle = atomic_read(&this_adm.
		mem_map_handles[cal_index]);
done:
	return ret;
}

static int adm_unmap_cal_data(int32_t cal_type,
			struct cal_block_data *cal_block)
{
	int ret = 0;
	int cal_index;

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

	cal_index = get_cal_type_index(cal_type);
	if (cal_index < 0) {
		pr_err("%s: could not get cal index %d!\n",
			__func__, cal_index);
		ret = -EINVAL;
		goto done;
	}

	if (cal_block == NULL) {
		pr_err("%s: Cal block is NULL!\n",
						__func__);
		goto done;
	}

	if (cal_block->map_data.q6map_handle == 0) {
		pr_err("%s: Map handle is NULL, nothing to unmap\n",
				__func__);
		goto done;
	}

	atomic_set(&this_adm.mem_map_handles[cal_index],
		cal_block->map_data.q6map_handle);
	atomic_set(&this_adm.mem_map_index, cal_index);
	ret = adm_memory_unmap_regions();
	if (ret < 0) {
		pr_err("%s: unmap did not work! cal_type %i ret %d\n",
			__func__, cal_index, ret);
		ret = -ENODEV;
		goto done;
	}
	cal_block->map_data.q6map_handle = 0;
done:
	return ret;
}

static void adm_delete_cal_data(void)
{
	pr_debug("%s:\n", __func__);

	cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
}

static int adm_init_cal_data(void)
{
	int ret = 0;
	struct cal_type_info	cal_type_info[] = {
		{{ADM_CUST_TOPOLOGY_CAL_TYPE,
		{adm_alloc_cal, adm_dealloc_cal, NULL,
		adm_set_cal, NULL, NULL} },
		{adm_map_cal_data, adm_unmap_cal_data,
		cal_utils_match_buf_num} },

		{{ADM_AUDPROC_CAL_TYPE,
		{adm_alloc_cal, adm_dealloc_cal, NULL,
		adm_set_cal, NULL, NULL} },
		{adm_map_cal_data, adm_unmap_cal_data,
		cal_utils_match_buf_num} },

		{{ADM_LSM_AUDPROC_CAL_TYPE,
		{adm_alloc_cal, adm_dealloc_cal, NULL,
		adm_set_cal, NULL, NULL} },
		{adm_map_cal_data, adm_unmap_cal_data,
		cal_utils_match_buf_num} },

		{{ADM_AUDVOL_CAL_TYPE,
		{adm_alloc_cal, adm_dealloc_cal, NULL,
		adm_set_cal, NULL, NULL} },
		{adm_map_cal_data, adm_unmap_cal_data,
		cal_utils_match_buf_num} },

		{{ADM_RTAC_INFO_CAL_TYPE,
		{NULL, NULL, NULL, NULL, NULL, NULL} },
		{NULL, NULL, cal_utils_match_buf_num} },

		{{ADM_RTAC_APR_CAL_TYPE,
		{NULL, NULL, NULL, NULL, NULL, NULL} },
		{NULL, NULL, cal_utils_match_buf_num} },

		{{SRS_TRUMEDIA_CAL_TYPE,
		{NULL, NULL, NULL, NULL, NULL, NULL} },
		{NULL, NULL, cal_utils_match_buf_num} },

		{{ADM_RTAC_AUDVOL_CAL_TYPE,
		{adm_alloc_cal, adm_dealloc_cal, NULL,
		adm_set_cal, NULL, NULL} },
		{adm_map_cal_data, adm_unmap_cal_data,
		cal_utils_match_buf_num} },

		{{ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE,
		 {adm_alloc_cal, adm_dealloc_cal, NULL,
		  adm_set_cal, NULL, NULL} },
		 {adm_map_cal_data, adm_unmap_cal_data,
		  cal_utils_match_buf_num} },

		{{ADM_AUDPROC_PERSISTENT_CAL_TYPE,
		 {adm_alloc_cal, adm_dealloc_cal, NULL,
		  adm_set_cal, NULL, NULL} },
		 {adm_map_cal_data, adm_unmap_cal_data,
		  cal_utils_match_buf_num} },
	};
	pr_debug("%s:\n", __func__);

	ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
		cal_type_info);
	if (ret < 0) {
		pr_err("%s: could not create cal type! ret %d\n",
			__func__, ret);
		ret = -EINVAL;
		goto err;
	}

	return ret;
err:
	adm_delete_cal_data();
	return ret;
}

/**
 * adm_set_volume -
 *        command to set volume on ADM copp
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @volume: gain value to set
 *
 * Returns 0 on success or error on failure
 */
int adm_set_volume(int port_id, int copp_idx, int volume)
{
	struct audproc_volume_ctrl_master_gain audproc_vol;
	struct param_hdr_v3 param_hdr;
	int rc  = 0;

	pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);

	memset(&audproc_vol, 0, sizeof(audproc_vol));
	memset(&param_hdr, 0, sizeof(param_hdr));
	param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
	param_hdr.param_size = sizeof(audproc_vol);

	audproc_vol.master_gain = volume;

	rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
					   (uint8_t *) &audproc_vol);
	if (rc)
		pr_err("%s: Failed to set volume, err %d\n", __func__, rc);

	return rc;
}
EXPORT_SYMBOL(adm_set_volume);

/**
 * adm_set_softvolume -
 *        command to set softvolume
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @softvol_param: Params to set for softvolume
 *
 * Returns 0 on success or error on failure
 */
int adm_set_softvolume(int port_id, int copp_idx,
			struct audproc_softvolume_params *softvol_param)
{
	struct audproc_soft_step_volume_params audproc_softvol;
	struct param_hdr_v3 param_hdr;
	int rc  = 0;

	pr_debug("%s: period %d step %d curve %d\n", __func__,
		 softvol_param->period, softvol_param->step,
		 softvol_param->rampingcurve);

	memset(&audproc_softvol, 0, sizeof(audproc_softvol));
	memset(&param_hdr, 0, sizeof(param_hdr));
	param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
	param_hdr.param_size = sizeof(audproc_softvol);

	audproc_softvol.period = softvol_param->period;
	audproc_softvol.step = softvol_param->step;
	audproc_softvol.ramping_curve = softvol_param->rampingcurve;

	pr_debug("%s: period %d, step %d, curve %d\n", __func__,
		 audproc_softvol.period, audproc_softvol.step,
		 audproc_softvol.ramping_curve);

	rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
					   (uint8_t *) &audproc_softvol);
	if (rc)
		pr_err("%s: Failed to set soft volume, err %d\n", __func__, rc);

	return rc;
}
EXPORT_SYMBOL(adm_set_softvolume);

/**
 * adm_set_mic_gain -
 *        command to set MIC gain
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @volume: gain value to set
 *
 * Returns 0 on success or error on failure
 */
int adm_set_mic_gain(int port_id, int copp_idx, int volume)
{
	struct admx_mic_gain mic_gain_params;
	struct param_hdr_v3 param_hdr;
	int rc = 0;

	pr_debug("%s: Setting mic gain to %d at port_id 0x%x\n", __func__,
		 volume, port_id);

	memset(&mic_gain_params, 0, sizeof(mic_gain_params));
	memset(&param_hdr, 0, sizeof(param_hdr));
	param_hdr.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = ADM_PARAM_IDX_MIC_GAIN;
	param_hdr.param_size = sizeof(mic_gain_params);

	mic_gain_params.tx_mic_gain = volume;

	rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
					   (uint8_t *) &mic_gain_params);
	if (rc)
		pr_err("%s: Failed to set mic gain, err %d\n", __func__, rc);

	return rc;
}
EXPORT_SYMBOL(adm_set_mic_gain);

/**
 * adm_send_set_multichannel_ec_primary_mic_ch -
 *        command to set multi-ch EC primary mic
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @primary_mic_ch: channel number of primary mic
 *
 * Returns 0 on success or error on failure
 */
int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
			int primary_mic_ch)
{
	struct admx_sec_primary_mic_ch sec_primary_ch_params;
	struct param_hdr_v3 param_hdr;
	int rc = 0;

	pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
			__func__, port_id,  copp_idx,  primary_mic_ch);

	memset(&sec_primary_ch_params, 0, sizeof(sec_primary_ch_params));
	memset(&param_hdr, 0, sizeof(param_hdr));
	param_hdr.module_id = AUDPROC_MODULE_ID_VOICE_TX_SECNS;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
	param_hdr.param_size = sizeof(sec_primary_ch_params);

	sec_primary_ch_params.version = 0;
	sec_primary_ch_params.sec_primary_mic_ch = primary_mic_ch;

	rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
					   (uint8_t *) &sec_primary_ch_params);
	if (rc)
		pr_err("%s: Failed to set primary mic chanel, err %d\n",
		       __func__, rc);

	return rc;
}
EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);

/**
 * adm_set_ffecns_effect -
 *      command to set effect for ffecns module
 *
 * @effect: effect payload
 *
 * Returns 0 on success or error on failure
 */
int adm_set_ffecns_effect(int effect)
{
	struct ffecns_effect ffecns_params;
	struct param_hdr_v3 param_hdr;
	int rc = 0;
	int copp_idx = 0;

	copp_idx = adm_get_default_copp_idx(this_adm.ffecns_port_id);
	if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
		pr_err("%s, no active copp to query rms copp_idx:%d\n",
			__func__, copp_idx);
		return -EINVAL;
	}

	memset(&ffecns_params, 0, sizeof(ffecns_params));
	memset(&param_hdr, 0, sizeof(param_hdr));

	param_hdr.module_id = FFECNS_MODULE_ID;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = FLUENCE_CMN_GLOBAL_EFFECT_PARAM_ID;
	param_hdr.param_size = sizeof(ffecns_params);

	ffecns_params.payload = effect;

	rc = adm_pack_and_set_one_pp_param(this_adm.ffecns_port_id, copp_idx,
					param_hdr, (uint8_t *) &ffecns_params);
	if (rc)
		pr_err("%s: Failed to set ffecns effect, err %d\n",
		       __func__, rc);

	return rc;
}
EXPORT_SYMBOL(adm_set_ffecns_effect);

/**
 * adm_set_ffecns_freeze_event -
 *      command to set event for ffecns module
 *
 * @event: send ffecns freeze event true or false
 *
 * Returns 0 on success or error on failure
 */
int adm_set_ffecns_freeze_event(bool ffecns_freeze_event)
{
	struct ffv_spf_freeze_param_t ffv_param;
	struct param_hdr_v3 param_hdr;
	int rc = 0;
	int copp_idx = 0;

	memset(&param_hdr, 0, sizeof(param_hdr));
	memset(&ffv_param, 0, sizeof(ffv_param));

	ffv_param.freeze = ffecns_freeze_event ? 1 : 0;
	ffv_param.source_id = 0; /*default value*/

	copp_idx = adm_get_default_copp_idx(this_adm.ffecns_port_id);
	if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
		pr_err("%s, no active copp to query rms copp_idx:%d\n",
			__func__, copp_idx);
		return -EINVAL;
	}

	param_hdr.module_id = FFECNS_MODULE_ID;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = PARAM_ID_FFV_SPF_FREEZE;
	param_hdr.param_size = sizeof(ffv_param);

	rc = adm_pack_and_set_one_pp_param(this_adm.ffecns_port_id, copp_idx,
					param_hdr, (uint8_t *) &ffv_param);
	if (rc)
		pr_err("%s: Failed to set ffecns imc event, err %d\n",
		       __func__, rc);

	return rc;
}
EXPORT_SYMBOL(adm_set_ffecns_freeze_event);

/**
 * adm_param_enable -
 *      command to send params to ADM for given module
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @module_id: ADM module
 * @enable: flag to enable or disable module
 *
 * Returns 0 on success or error on failure
 */
int adm_param_enable(int port_id, int copp_idx, int module_id,  int enable)
{
	struct module_instance_info mod_inst_info;

	memset(&mod_inst_info, 0, sizeof(mod_inst_info));
	mod_inst_info.module_id = module_id;
	mod_inst_info.instance_id = INSTANCE_ID_0;

	return adm_param_enable_v2(port_id, copp_idx, mod_inst_info, enable);
}
EXPORT_SYMBOL(adm_param_enable);

/**
 * adm_param_enable_v2 -
 *      command to send params to ADM for given module
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @mod_inst_info: module and instance ID info
 * @enable: flag to enable or disable module
 *
 * Returns 0 on success or error on failure
 */
int adm_param_enable_v2(int port_id, int copp_idx,
			struct module_instance_info mod_inst_info, int enable)
{
	uint32_t enable_param;
	struct param_hdr_v3 param_hdr;
	int rc = 0;

	if (enable < 0 || enable > 1) {
		pr_err("%s: Invalid value for enable %d\n", __func__, enable);
		return -EINVAL;
	}

	pr_debug("%s port_id %d, module_id 0x%x, instance_id 0x%x, enable %d\n",
		 __func__, port_id, mod_inst_info.module_id,
		 mod_inst_info.instance_id, enable);

	memset(&param_hdr, 0, sizeof(param_hdr));
	param_hdr.module_id = mod_inst_info.module_id;
	param_hdr.instance_id = mod_inst_info.instance_id;
	param_hdr.param_id = AUDPROC_PARAM_ID_ENABLE;
	param_hdr.param_size = sizeof(enable_param);

	enable_param = enable;

	rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
					   (uint8_t *) &enable_param);
	if (rc)
		pr_err("%s: Failed to set enable of module(%d) instance(%d) to %d, err %d\n",
		       __func__, mod_inst_info.module_id,
		       mod_inst_info.instance_id, enable, rc);

	return rc;

}
EXPORT_SYMBOL(adm_param_enable_v2);

/**
 * adm_send_calibration -
 *        send ADM calibration to DSP
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @path: direction or ADM path type
 * @perf_mode: performance mode like LL/ULL/..
 * @cal_type: calibration type to use
 * @params: pointer with cal data
 * @size: cal size
 *
 * Returns 0 on success or error on failure
 */
int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
			 int cal_type, char *params, int size)
{

	int rc = 0;

	pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
		 __func__, port_id, path, perf_mode, cal_type, size);

	/* Maps audio_dev_ctrl path definition to ACDB definition */
	if (get_cal_path(path) != RX_DEVICE) {
		pr_err("%s: acdb_path %d\n", __func__, path);
		rc = -EINVAL;
		goto end;
	}

	rc = adm_set_pp_params(port_id, copp_idx, NULL, (u8 *) params, size);

end:
	return rc;
}
EXPORT_SYMBOL(adm_send_calibration);

/*
 * adm_update_wait_parameters must be called with routing driver locks.
 * adm_reset_wait_parameters must be called with routing driver locks.
 * set and reset parmeters are separated to make sure it is always called
 * under routing driver lock.
 * adm_wait_timeout is to block until timeout or interrupted. Timeout is
 * not a an error.
 */
int adm_set_wait_parameters(int port_id, int copp_idx)
{

	int ret = 0, port_idx;

	pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
		 copp_idx);
	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
		ret = -EINVAL;
		goto end;
	}

	if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
		return -EINVAL;
	}

	this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
	atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);

end:
	return ret;

}
EXPORT_SYMBOL(adm_set_wait_parameters);

/**
 * adm_reset_wait_parameters -
 *        reset wait parameters or ADM delay value
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 *
 * Returns 0 on success or error on failure
 */
int adm_reset_wait_parameters(int port_id, int copp_idx)
{
	int ret = 0, port_idx;

	pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
		 copp_idx);
	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
		ret = -EINVAL;
		goto end;
	}

	if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
		return -EINVAL;
	}

	atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
	this_adm.copp.adm_delay[port_idx][copp_idx] = 0;

end:
	return ret;
}
EXPORT_SYMBOL(adm_reset_wait_parameters);

/**
 * adm_wait_timeout -
 *        ADM wait command after command send to DSP
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @wait_time: value in ms for command timeout
 *
 * Returns 0 on success or error on failure
 */
int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
{
	int ret = 0, port_idx;

	pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
		 port_id, copp_idx, wait_time);
	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
		ret = -EINVAL;
		goto end;
	}

	if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
		return -EINVAL;
	}

	ret = wait_event_timeout(
		this_adm.copp.adm_delay_wait[port_idx][copp_idx],
		atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
		msecs_to_jiffies(wait_time));
	pr_debug("%s: return %d\n", __func__, ret);
	if (ret != 0)
		ret = -EINTR;
end:
	pr_debug("%s: return %d--\n", __func__, ret);
	return ret;
}
EXPORT_SYMBOL(adm_wait_timeout);

/**
 * adm_store_cal_data -
 *        Retrieve calibration data for ADM copp device
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @path: direction or copp type
 * @perf_mode: performance mode like LL/ULL/..
 * @cal_index: calibration index to use
 * @params: pointer to store cal data
 * @size: pointer to fill with cal size
 *
 * Returns 0 on success or error on failure
 */
int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
		       int cal_index, char *params, int *size)
{
	int rc = 0;
	struct cal_block_data		*cal_block = NULL;
	int app_type, acdb_id, port_idx, sample_rate;

	if (this_adm.cal_data[cal_index] == NULL) {
		pr_debug("%s: cal_index %d not allocated!\n",
			__func__, cal_index);
		goto end;
	}

	if (get_cal_path(path) != RX_DEVICE) {
		pr_debug("%s: Invalid path to store calibration %d\n",
			 __func__, path);
		rc = -EINVAL;
		goto end;
	}

	port_id = afe_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0) {
		pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
		rc = -EINVAL;
		goto end;
	}

	if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
		return -EINVAL;
	}

	acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
	app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
	sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);

	mutex_lock(&this_adm.cal_data[cal_index]->lock);
	cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
				acdb_id, sample_rate);
	if (cal_block == NULL)
		goto unlock;

	if (cal_block->cal_data.size <= 0) {
		pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
			__func__, port_id);
		rc = -EINVAL;
		goto unlock;
	}

	if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
		if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
			pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
				__func__, cal_block->cal_data.size, *size);
			rc = -ENOMEM;
			goto unlock;
		}
	} else if (cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
		if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
			pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
				__func__, cal_block->cal_data.size, *size);
			rc = -ENOMEM;
			goto unlock;
		}
	} else if (cal_index == ADM_AUDPROC_PERSISTENT_CAL) {
		if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
			pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
				__func__, cal_block->cal_data.size, *size);
			rc = -ENOMEM;
			goto unlock;
		}
	}
	else if (cal_index == ADM_AUDVOL_CAL) {
		if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
			pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
				__func__, cal_block->cal_data.size, *size);
			rc = -ENOMEM;
			goto unlock;
		}
	} else {
		pr_debug("%s: Not valid calibration for dolby topolgy\n",
			 __func__);
		rc = -EINVAL;
		goto unlock;
	}
	memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
	*size = cal_block->cal_data.size;

	pr_debug("%s:port_id %d, copp_idx %d, path %d",
		 __func__, port_id, copp_idx, path);
	pr_debug("perf_mode %d, cal_type %d, size %d\n",
		 perf_mode, cal_index, *size);

unlock:
	mutex_unlock(&this_adm.cal_data[cal_index]->lock);
end:
	return rc;
}
EXPORT_SYMBOL(adm_store_cal_data);

/**
 * adm_send_compressed_device_mute -
 *        command to send mute for compressed device
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @mute_on: flag to indicate mute or unmute
 *
 * Returns 0 on success or error on failure
 */
int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
{
	u32 mute_param = mute_on ? 1 : 0;
	struct param_hdr_v3 param_hdr;
	int ret = 0;

	pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
		 __func__, port_id, copp_idx, mute_on);

	memset(&param_hdr, 0, sizeof(param_hdr));
	param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
	param_hdr.param_size = sizeof(mute_param);

	ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
					    (uint8_t *) &mute_param);
	if (ret)
		pr_err("%s: Failed to set mute, err %d\n", __func__, ret);

	return ret;
}
EXPORT_SYMBOL(adm_send_compressed_device_mute);

/**
 * adm_send_compressed_device_latency -
 *        command to send latency for compressed device
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @latency: latency value to pass
 *
 * Returns 0 on success or error on failure
 */
int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
{
	u32 latency_param;
	struct param_hdr_v3 param_hdr;
	int ret = 0;

	pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
		 port_id, copp_idx, latency);

	if (latency < 0) {
		pr_err("%s: Invalid value for latency %d", __func__, latency);
		return -EINVAL;
	}

	memset(&param_hdr, 0, sizeof(param_hdr));
	param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
	param_hdr.param_size = sizeof(latency_param);

	latency_param = latency;

	ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
					    (uint8_t *) &latency_param);
	if (ret)
		pr_err("%s: Failed to set latency, err %d\n", __func__, ret);

	return ret;
}
EXPORT_SYMBOL(adm_send_compressed_device_latency);

/**
 * adm_swap_speaker_channels
 *
 * Receives port_id, copp_idx, sample rate, spk_swap and
 * send MFC command to swap speaker channel.
 * Return zero on success. On failure returns nonzero.
 *
 * port_id - Passed value, port_id for which channels swap is wanted
 * copp_idx - Passed value, copp_idx for which channels swap is wanted
 * sample_rate - Passed value, sample rate used by app type config
 * spk_swap  - Passed value, spk_swap for check if swap flag is set
 */
int adm_swap_speaker_channels(int port_id, int copp_idx,
			int sample_rate, bool spk_swap)
{
	struct audproc_mfc_param_media_fmt mfc_cfg;
	struct param_hdr_v3 param_hdr;
	uint16_t num_channels;
	int port_idx = 0;
	int ret  = 0;

	pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
		  __func__, port_id, copp_idx);
	port_id = q6audio_convert_virtual_to_portid(port_id);
	port_idx = adm_validate_and_get_port_index(port_id);
	if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
		pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
		return -EINVAL;
	} else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
		pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
		return -EINVAL;
	}

	num_channels = atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
	if (num_channels != 2) {
		pr_debug("%s: Invalid number of channels: %d\n",
			__func__, num_channels);
		return -EINVAL;
	}

	memset(&mfc_cfg, 0, sizeof(mfc_cfg));
	memset(&param_hdr, 0, sizeof(param_hdr));

	param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
	param_hdr.param_size = sizeof(mfc_cfg);

	mfc_cfg.sampling_rate = sample_rate;
	mfc_cfg.bits_per_sample =
		atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
	mfc_cfg.num_channels = num_channels;

	/* Currently applying speaker swap for only 2 channel use case */
	if (spk_swap) {
		mfc_cfg.channel_type[0] =
			(uint16_t) PCM_CHANNEL_FR;
		mfc_cfg.channel_type[1] =
			(uint16_t) PCM_CHANNEL_FL;
	} else {
		mfc_cfg.channel_type[0] =
			(uint16_t) PCM_CHANNEL_FL;
		mfc_cfg.channel_type[1] =
			(uint16_t) PCM_CHANNEL_FR;
	}

	ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
					    (u8 *) &mfc_cfg);
	if (ret < 0) {
		pr_err("%s: Failed to set swap speaker channels on port[0x%x] failed %d\n",
		       __func__, port_id, ret);
		return ret;
	}

	pr_debug("%s: mfc_cfg Set params returned success", __func__);
	return 0;
}
EXPORT_SYMBOL(adm_swap_speaker_channels);

/**
 * adm_set_sound_focus -
 *       Update sound focus info
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @soundFocusData: sound focus data to pass
 *
 * Returns 0 on success or error on failure
 */
int adm_set_sound_focus(int port_id, int copp_idx,
			struct sound_focus_param soundFocusData)
{
	struct adm_param_fluence_soundfocus_t soundfocus_params;
	struct param_hdr_v3 param_hdr;
	int ret  = 0;
	int i;

	pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
		  __func__, port_id, copp_idx);

	memset(&param_hdr, 0, sizeof(param_hdr));
	param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
	param_hdr.param_size = sizeof(soundfocus_params);

	memset(&(soundfocus_params), 0xFF, sizeof(soundfocus_params));
	for (i = 0; i < MAX_SECTORS; i++) {
		soundfocus_params.start_angles[i] =
			soundFocusData.start_angle[i];
		soundfocus_params.enables[i] = soundFocusData.enable[i];
		pr_debug("%s: start_angle[%d] = %d\n",
			  __func__, i, soundFocusData.start_angle[i]);
		pr_debug("%s: enable[%d] = %d\n",
			  __func__, i, soundFocusData.enable[i]);
	}
	soundfocus_params.gain_step = soundFocusData.gain_step;
	pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);

	soundfocus_params.reserved = 0;

	ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
					    (uint8_t *) &soundfocus_params);
	if (ret)
		pr_err("%s: Failed to set sound focus params, err %d\n",
		       __func__, ret);

	pr_debug("%s: Exit, ret=%d\n", __func__, ret);

	return ret;
}
EXPORT_SYMBOL(adm_set_sound_focus);

/**
 * adm_get_sound_focus -
 *        Retrieve sound focus info
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @soundFocusData: pointer for sound focus data to be updated with
 *
 * Returns 0 on success or error on failure
 */
int adm_get_sound_focus(int port_id, int copp_idx,
			struct sound_focus_param *soundFocusData)
{
	int ret = 0, i;
	char *params_value;
	uint32_t max_param_size = 0;
	struct adm_param_fluence_soundfocus_t *soundfocus_params = NULL;
	struct param_hdr_v3 param_hdr;

	pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
		  __func__, port_id, copp_idx);

	max_param_size = sizeof(struct adm_param_fluence_soundfocus_t) +
			 sizeof(union param_hdrs);
	params_value = kzalloc(max_param_size, GFP_KERNEL);
	if (!params_value)
		return -ENOMEM;

	memset(&param_hdr, 0, sizeof(param_hdr));
	param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
	param_hdr.param_size = max_param_size;
	ret = adm_get_pp_params(port_id, copp_idx,
				ADM_CLIENT_ID_SOURCE_TRACKING, NULL, &param_hdr,
				params_value);
	if (ret) {
		pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
		ret = -EINVAL;
		goto done;
	}

	if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
		pr_err("%s - get params returned error [%s]\n",
			__func__, adsp_err_get_err_str(
			this_adm.sourceTrackingData.apr_cmd_status));
		ret = adsp_err_get_lnx_err_code(
				this_adm.sourceTrackingData.apr_cmd_status);
		goto done;
	}

	soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
								params_value;
	for (i = 0; i < MAX_SECTORS; i++) {
		soundFocusData->start_angle[i] =
					soundfocus_params->start_angles[i];
		soundFocusData->enable[i] = soundfocus_params->enables[i];
		pr_debug("%s: start_angle[%d] = %d\n",
			  __func__, i, soundFocusData->start_angle[i]);
		pr_debug("%s: enable[%d] = %d\n",
			  __func__, i, soundFocusData->enable[i]);
	}
	soundFocusData->gain_step = soundfocus_params->gain_step;
	pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);

done:
	pr_debug("%s: Exit, ret = %d\n", __func__, ret);

	kfree(params_value);
	return ret;
}
EXPORT_SYMBOL(adm_get_sound_focus);

static int adm_source_tracking_alloc_map_memory(void)
{
	int ret;

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

	ret = msm_audio_ion_alloc(&this_adm.sourceTrackingData.dma_buf,
				  AUD_PROC_BLOCK_SIZE,
				  &this_adm.sourceTrackingData.memmap.paddr,
				  &this_adm.sourceTrackingData.memmap.size,
				  &this_adm.sourceTrackingData.memmap.kvaddr);
	if (ret) {
		pr_err("%s: failed to allocate memory\n", __func__);

		ret = -EINVAL;
		goto done;
	}

	atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
	ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
			0,
			(uint32_t *)&this_adm.sourceTrackingData.memmap.size,
			1);
	if (ret < 0) {
		pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
			__func__,
			(void *)this_adm.sourceTrackingData.memmap.paddr,
			(uint32_t)this_adm.sourceTrackingData.memmap.size);

		msm_audio_ion_free(this_adm.sourceTrackingData.dma_buf);
		this_adm.sourceTrackingData.dma_buf = NULL;
		this_adm.sourceTrackingData.memmap.size = 0;
		this_adm.sourceTrackingData.memmap.kvaddr = NULL;
		this_adm.sourceTrackingData.memmap.paddr = 0;
		this_adm.sourceTrackingData.apr_cmd_status = -1;
		atomic_set(&this_adm.mem_map_handles
				[ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);

		ret = -EINVAL;
		goto done;
	}
	ret = 0;
	pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
		  __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
		  (uint32_t)this_adm.sourceTrackingData.memmap.size,
		  atomic_read(&this_adm.mem_map_handles
			      [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));

done:
	pr_debug("%s: Exit, ret = %d\n", __func__, ret);

	return ret;
}

/**
 * adm_get_source_tracking -
 *        Retrieve source tracking info
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @sourceTrackingData: pointer for source track data to be updated with
 *
 * Returns 0 on success or error on failure
 */
int adm_get_source_tracking(int port_id, int copp_idx,
			    struct source_tracking_param *sourceTrackingData)
{
	struct adm_param_fluence_sourcetracking_t *source_tracking_params =
		NULL;
	struct mem_mapping_hdr mem_hdr;
	struct param_hdr_v3 param_hdr;
	int i = 0;
	int ret = 0;

	pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
		  __func__, port_id, copp_idx);

	if (!this_adm.sourceTrackingData.memmap.paddr) {
		/* Allocate and map shared memory for out of band usage */
		ret = adm_source_tracking_alloc_map_memory();
		if (ret != 0) {
			ret = -EINVAL;
			goto done;
		}
	}

	memset(&mem_hdr, 0, sizeof(mem_hdr));
	memset(&param_hdr, 0, sizeof(param_hdr));
	mem_hdr.data_payload_addr_lsw =
		lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
	mem_hdr.data_payload_addr_msw = msm_audio_populate_upper_32_bits(
		this_adm.sourceTrackingData.memmap.paddr);
	mem_hdr.mem_map_handle = atomic_read(
		&this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);

	param_hdr.module_id = VOICEPROC_MODULE_ID_FLUENCE_PRO_VC_TX;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
	/*
	 * This size should be the max size of the calibration data + header.
	 * Use the union size to ensure max size is used.
	 */
	param_hdr.param_size =
		sizeof(struct adm_param_fluence_sourcetracking_t) +
		sizeof(struct param_hdr_v3);

	/*
	 * Retrieving parameters out of band, so no need to provide a buffer for
	 * the returned parameter data as it will be at the memory location
	 * provided.
	 */
	ret = adm_get_pp_params(port_id, copp_idx,
				ADM_CLIENT_ID_SOURCE_TRACKING, &mem_hdr,
				&param_hdr, NULL);
	if (ret) {
		pr_err("%s: Failed to get params, error %d\n", __func__, ret);
		goto done;
	}

	if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
		pr_err("%s - get params returned error [%s]\n",
			__func__, adsp_err_get_err_str(
			this_adm.sourceTrackingData.apr_cmd_status));

		ret = adsp_err_get_lnx_err_code(
				this_adm.sourceTrackingData.apr_cmd_status);
		goto done;
	}

	/* How do we know what the param data was retrieved with for hdr size */
	source_tracking_params =
		(struct adm_param_fluence_sourcetracking_t
			 *) (this_adm.sourceTrackingData.memmap.kvaddr +
			     sizeof(struct param_hdr_v3));
	for (i = 0; i < MAX_SECTORS; i++) {
		sourceTrackingData->vad[i] = source_tracking_params->vad[i];
		pr_debug("%s: vad[%d] = %d\n",
			  __func__, i, sourceTrackingData->vad[i]);
	}
	sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
	pr_debug("%s: doa_speech = %d\n",
		  __func__, sourceTrackingData->doa_speech);

	for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
		sourceTrackingData->doa_noise[i] =
					source_tracking_params->doa_noise[i];
		pr_debug("%s: doa_noise[%d] = %d\n",
			  __func__, i, sourceTrackingData->doa_noise[i]);
	}
	for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
		sourceTrackingData->polar_activity[i] =
				source_tracking_params->polar_activity[i];
		pr_debug("%s: polar_activity[%d] = %d\n",
			  __func__, i, sourceTrackingData->polar_activity[i]);
	}

	ret = 0;

done:
	pr_debug("%s: Exit, ret=%d\n", __func__, ret);

	return ret;
}
EXPORT_SYMBOL(adm_get_source_tracking);

/**
 * adm_get_doa_tracking_mon -
 *        Retrieve doa tracking monitor info
 *
 * @port_id: Port ID number
 * @copp_idx: copp index assigned
 * @doa_tracking_data: pointer for doa data to be updated with
 *
 * Returns 0 on success or error on failure
 */
int adm_get_doa_tracking_mon(int port_id, int copp_idx,
	struct doa_tracking_mon_param *doa_tracking_data)
{
	int ret = 0, i;
	char *params_value;
	uint32_t max_param_size = 0;
	struct adm_param_doa_tracking_mon_t *doa_tracking_params = NULL;
	struct param_hdr_v3 param_hdr;

	pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
		 __func__, port_id, copp_idx);

	if (doa_tracking_data == NULL) {
		pr_err("%s: Received NULL pointer for doa tracking data\n",
			 __func__);
		return -EINVAL;
	}

	max_param_size = sizeof(struct adm_param_doa_tracking_mon_t) +
			 sizeof(union param_hdrs);
	params_value = kzalloc(max_param_size, GFP_KERNEL);
	if (!params_value)
		return -ENOMEM;

	memset(&param_hdr, 0, sizeof(param_hdr));
	param_hdr.module_id = AUDPROC_MODULE_ID_FFECNS;
	param_hdr.instance_id = INSTANCE_ID_0;
	param_hdr.param_id = AUDPROC_PARAM_ID_FFV_DOA_TRACKING_MONITOR;
	param_hdr.param_size = max_param_size;
	ret = adm_get_pp_params(port_id, copp_idx,
				ADM_CLIENT_ID_DEFAULT, NULL, &param_hdr,
				params_value);
	if (ret) {
		pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
		goto done;
	}

	doa_tracking_params =
		(struct adm_param_doa_tracking_mon_t *)params_value;
	for (i = 0; i < MAX_DOA_TRACKING_ANGLES; i++) {
		doa_tracking_data->target_angle_L16[i] =
			doa_tracking_params->target_angle_L16[i];
		pr_debug("%s: target angle[%d] = %d\n",
			 __func__, i, doa_tracking_data->target_angle_L16[i]);
	}

	for (i = 0; i < MAX_DOA_TRACKING_ANGLES; i++) {
		doa_tracking_data->interf_angle_L16[i] =
			doa_tracking_params->interf_angle_L16[i];
		pr_debug("%s: interference angle[%d] = %d\n",
			 __func__, i, doa_tracking_data->interf_angle_L16[i]);
	}

	for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
		doa_tracking_data->polar_activity[i] =
			doa_tracking_params->polar_activity[i];
	}

done:
	pr_debug("%s: Exit, ret = %d\n", __func__, ret);
	kfree(params_value);
	return ret;
}
EXPORT_SYMBOL(adm_get_doa_tracking_mon);

int __init adm_init(void)
{
	int i = 0, j;

	this_adm.ec_ref_rx = -1;
	this_adm.ffecns_port_id = -1;
	init_waitqueue_head(&this_adm.matrix_map_wait);
	init_waitqueue_head(&this_adm.adm_wait);
	mutex_init(&this_adm.adm_apr_lock);

	for (i = 0; i < AFE_MAX_PORTS; i++) {
		for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
			atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
			init_waitqueue_head(&this_adm.copp.wait[i][j]);
			init_waitqueue_head(
				&this_adm.copp.adm_delay_wait[i][j]);
		}
	}

	if (adm_init_cal_data())
		pr_err("%s: could not init cal data!\n", __func__);

	this_adm.sourceTrackingData.dma_buf = NULL;
	this_adm.sourceTrackingData.memmap.size = 0;
	this_adm.sourceTrackingData.memmap.kvaddr = NULL;
	this_adm.sourceTrackingData.memmap.paddr = 0;
	this_adm.sourceTrackingData.apr_cmd_status = -1;

	return 0;
}

void adm_exit(void)
{
	mutex_destroy(&this_adm.adm_apr_lock);
	if (this_adm.apr)
		adm_reset_data();
	adm_delete_cal_data();
}