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
|
/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/module.h>
#include <linux/firmware.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/elf.h>
#include <linux/wait.h>
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/pm_qos.h>
#include <linux/dma-mapping.h>
#include <sound/soc.h>
#include <sound/info.h>
#include <sound/lsm_params.h>
#include <sound/cpe_core.h>
#include <sound/cpe_cmi.h>
#include <soc/qcom/pm.h>
#include <linux/mfd/wcd9xxx/core.h>
#include <linux/mfd/wcd9xxx/core-resource.h>
#include <linux/mfd/wcd9xxx/wcd9330_registers.h>
#include <sound/audio_cal_utils.h>
#include "wcd_cpe_core.h"
#include "wcd_cpe_services.h"
#include "wcd_cmi_api.h"
#define CMI_CMD_TIMEOUT (10 * HZ)
#define WCD_CPE_LSM_MAX_SESSIONS 1
#define WCD_CPE_AFE_MAX_PORTS 1
#define WCD_CPE_DRAM_SIZE 0x30000
#define WCD_CPE_DRAM_OFFSET 0x50000
#define ELF_FLAG_EXECUTE (1 << 0)
#define ELF_FLAG_WRITE (1 << 1)
#define ELF_FLAG_READ (1 << 2)
#define ELF_FLAG_RW (ELF_FLAG_READ | ELF_FLAG_WRITE)
#define WCD_CPE_GRAB_LOCK(lock, name) \
{ \
pr_debug("%s: %s lock acquire\n", \
__func__, name); \
mutex_lock(lock); \
}
#define WCD_CPE_REL_LOCK(lock, name) \
{ \
pr_debug("%s: %s lock release\n", \
__func__, name); \
mutex_unlock(lock); \
}
#define WCD_CPE_STATE_MAX_LEN 11
#define CPE_OFFLINE_WAIT_TIMEOUT (2 * HZ)
#define CPE_READY_WAIT_TIMEOUT (3 * HZ)
#define SVASS_INT_STATUS_RCO_WDOG 0x20
#define SVASS_INT_STATUS_WDOG_BITE 0x02
/* Add any SVA IRQs that are to be treated as FATAL */
#define SVASS_FATAL_IRQS \
(SVASS_INT_STATUS_RCO_WDOG | \
SVASS_INT_STATUS_WDOG_BITE)
enum afe_port_state {
AFE_PORT_STATE_DEINIT = 0,
AFE_PORT_STATE_INIT,
AFE_PORT_STATE_CONFIG,
AFE_PORT_STATE_STARTED,
AFE_PORT_STATE_SUSPENDED,
};
struct wcd_cmi_afe_port_data {
u8 port_id;
struct mutex afe_lock;
struct completion afe_cmd_complete;
enum afe_port_state port_state;
u8 cmd_result;
u32 mem_handle;
};
static struct wcd_cpe_core *core_d;
static struct cpe_lsm_session
*lsm_sessions[WCD_CPE_LSM_MAX_SESSIONS + 1];
struct wcd_cpe_core * (*wcd_get_cpe_core) (struct snd_soc_codec *);
static struct wcd_cmi_afe_port_data afe_ports[WCD_CPE_AFE_MAX_PORTS + 1];
static void wcd_cpe_svc_event_cb(const struct cpe_svc_notification *param);
static int wcd_cpe_setup_irqs(struct wcd_cpe_core *core);
static void wcd_cpe_cleanup_irqs(struct wcd_cpe_core *core);
static u32 ramdump_enable;
/* wcd_cpe_lsm_session_active: check if any session is active
* return true if any session is active.
*/
static bool wcd_cpe_lsm_session_active(void)
{
int index = 1;
bool lsm_active = false;
/* session starts from index 1 */
for (; index <= WCD_CPE_LSM_MAX_SESSIONS; index++) {
if (lsm_sessions[index] != NULL) {
lsm_active = true;
break;
} else {
lsm_active = false;
}
}
return lsm_active;
}
static int wcd_cpe_collect_ramdump(struct wcd_cpe_core *core)
{
struct cpe_svc_mem_segment dump_seg;
int rc;
dump_seg.type = CPE_SVC_DATA_MEM;
dump_seg.cpe_addr = WCD_CPE_DRAM_OFFSET;
dump_seg.size = WCD_CPE_DRAM_SIZE;
dump_seg.data = core->cpe_dump_v_addr;
dev_dbg(core->dev,
"%s: Reading ramdump from CPE\n",
__func__);
rc = cpe_svc_ramdump(core->cpe_handle, &dump_seg);
if (IS_ERR_VALUE(rc)) {
dev_err(core->dev,
"%s: Failed to read CPE ramdump, err = %d\n",
__func__, rc);
return rc;
}
dev_dbg(core->dev,
"%s: completed reading ramdump from CPE\n",
__func__);
core->cpe_ramdump_seg.address = (unsigned long) core->cpe_dump_addr;
core->cpe_ramdump_seg.size = WCD_CPE_DRAM_SIZE;
core->cpe_ramdump_seg.v_address = core->cpe_dump_v_addr;
rc = do_ramdump(core->cpe_ramdump_dev,
&core->cpe_ramdump_seg, 1);
if (rc)
dev_err(core->dev,
"%s: fail to dump cpe ram to device, err = %d\n",
__func__, rc);
return rc;
}
/* wcd_cpe_is_valid_elf_hdr: check if the ELF header is valid
* @core: handle to wcd_cpe_core
* @fw_size: size of firmware from request_firmware
* @ehdr: the elf header to be checked for
* return true if all checks pass, true if any elf check fails
*/
static bool wcd_cpe_is_valid_elf_hdr(struct wcd_cpe_core *core, size_t fw_size,
const struct elf32_hdr *ehdr)
{
if (fw_size < sizeof(*ehdr)) {
dev_err(core->dev, "%s:Firmware too small\n", __func__);
goto elf_check_fail;
}
if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
dev_err(core->dev, "%s: Not an ELF file\n", __func__);
goto elf_check_fail;
}
if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
dev_err(core->dev, "%s: Not a executable image\n", __func__);
goto elf_check_fail;
}
if (ehdr->e_phnum == 0) {
dev_err(core->dev, "%s: no segments to load\n", __func__);
goto elf_check_fail;
}
if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
sizeof(struct elf32_hdr) > fw_size) {
dev_err(core->dev, "%s: Too small MDT file\n", __func__);
goto elf_check_fail;
}
return true;
elf_check_fail:
return false;
}
/*
* wcd_cpe_load_each_segment: download segment to CPE
* @core: handle to struct wcd_cpe_core
* @file_idx: index of split firmware image file name
* @phdr: program header from metadata
*/
static int wcd_cpe_load_each_segment(struct wcd_cpe_core *core,
int file_idx, const struct elf32_phdr *phdr)
{
const struct firmware *split_fw;
char split_fname[32];
int ret = 0;
struct cpe_svc_mem_segment *segment;
if (!core || !phdr) {
pr_err("%s: Invalid params\n", __func__);
return -EINVAL;
}
/* file size can be 0 for bss segments */
if (phdr->p_filesz == 0 || phdr->p_memsz == 0)
return 0;
segment = kzalloc(sizeof(struct cpe_svc_mem_segment), GFP_KERNEL);
if (!segment) {
dev_err(core->dev,
"%s: no memory for segment info, file_idx = %d\n"
, __func__, file_idx);
return -ENOMEM;
}
snprintf(split_fname, sizeof(split_fname), "%s.b%02d",
core->fname, file_idx);
ret = request_firmware(&split_fw, split_fname, core->dev);
if (ret) {
dev_err(core->dev, "firmware %s not found\n",
split_fname);
ret = -EIO;
goto fw_req_fail;
}
if (phdr->p_flags & ELF_FLAG_EXECUTE)
segment->type = CPE_SVC_INSTRUCTION_MEM;
else if (phdr->p_flags & ELF_FLAG_RW)
segment->type = CPE_SVC_DATA_MEM;
else {
dev_err(core->dev, "%s invalid flags 0x%x\n",
__func__, phdr->p_flags);
goto done;
}
segment->cpe_addr = phdr->p_paddr;
segment->size = phdr->p_filesz;
segment->data = (u8 *) split_fw->data;
dev_dbg(core->dev,
"%s: cpe segment type %s read from firmware\n", __func__,
(segment->type == CPE_SVC_INSTRUCTION_MEM) ?
"INSTRUCTION" : "DATA");
ret = cpe_svc_download_segment(core->cpe_handle, segment);
if (ret) {
dev_err(core->dev,
"%s: Failed to download %s, error = %d\n",
__func__, split_fname, ret);
goto done;
}
done:
release_firmware(split_fw);
fw_req_fail:
kfree(segment);
return ret;
}
/*
* wcd_cpe_enable_cpe_clks: enable the clocks for CPE
* @core: handle to wcd_cpe_core
* @enable: flag indicating whether to enable/disable cpe clocks
*/
static int wcd_cpe_enable_cpe_clks(struct wcd_cpe_core *core, bool enable)
{
int ret = 0;
if (!core || !core->cpe_cdc_cb ||
!core->cpe_cdc_cb->cpe_clk_en) {
pr_err("%s: invalid handle\n",
__func__);
return -EINVAL;
}
ret = core->cpe_cdc_cb->cdc_clk_en(core->codec, enable);
if (ret) {
dev_err(core->dev, "%s: Failed to enable RCO\n",
__func__);
return ret;
}
ret = core->cpe_cdc_cb->cpe_clk_en(core->codec, enable);
if (ret) {
dev_err(core->dev,
"%s: cpe_clk_en() failed, err = %d\n",
__func__, ret);
return ret;
}
return 0;
}
/*
* wcd_cpe_load_fw: Function to load the fw image
* @core: cpe core pointer
* @load_type: indicates whether to load to data section
* or the instruction section
*
* Parse the mdt file to look for program headers, load each
* split file corresponding to the program headers.
*/
static int wcd_cpe_load_fw(struct wcd_cpe_core *core,
unsigned int load_type)
{
int ret, phdr_idx;
struct snd_soc_codec *codec = NULL;
struct wcd9xxx *wcd9xxx = NULL;
const struct elf32_hdr *ehdr;
const struct elf32_phdr *phdr;
const struct firmware *fw;
const u8 *elf_ptr;
char mdt_name[64];
bool img_dload_fail = false;
bool load_segment;
if (!core || !core->cpe_handle) {
pr_err("%s: Error CPE core %p\n", __func__,
core);
return -EINVAL;
}
codec = core->codec;
wcd9xxx = codec->control_data;
snprintf(mdt_name, sizeof(mdt_name), "%s.mdt", core->fname);
ret = request_firmware(&fw, mdt_name, core->dev);
if (IS_ERR_VALUE(ret)) {
dev_err(core->dev, "firmware %s not found\n", mdt_name);
return ret;
}
ehdr = (struct elf32_hdr *) fw->data;
if (!wcd_cpe_is_valid_elf_hdr(core, fw->size, ehdr)) {
dev_err(core->dev, "%s: fw mdt %s is invalid\n",
__func__, mdt_name);
ret = -EINVAL;
goto done;
}
elf_ptr = fw->data + sizeof(*ehdr);
if (load_type == ELF_FLAG_EXECUTE) {
/* Reset CPE first */
ret = cpe_svc_reset(core->cpe_handle);
if (IS_ERR_VALUE(ret)) {
dev_err(core->dev,
"%s: Failed to reset CPE with error %d\n",
__func__, ret);
goto done;
}
}
dev_dbg(core->dev, "%s: start image dload, name = %s, load_type = 0x%x\n",
__func__, core->fname, load_type);
/* parse every program header and request corresponding firmware */
for (phdr_idx = 0; phdr_idx < ehdr->e_phnum; phdr_idx++) {
phdr = (struct elf32_phdr *)elf_ptr;
load_segment = false;
dev_dbg(core->dev,
"index = %d, vaddr = 0x%x, paddr = 0x%x, "
"filesz = 0x%x, memsz = 0x%x, flags = 0x%x\n"
, phdr_idx, phdr->p_vaddr, phdr->p_paddr,
phdr->p_filesz, phdr->p_memsz, phdr->p_flags);
switch (load_type) {
case ELF_FLAG_EXECUTE:
if (phdr->p_flags & load_type)
load_segment = true;
break;
case ELF_FLAG_RW:
if (!(phdr->p_flags & ELF_FLAG_EXECUTE) &&
(phdr->p_flags & load_type))
load_segment = true;
break;
default:
pr_err("%s: Invalid load_type 0x%x\n",
__func__, load_type);
ret = -EINVAL;
goto done;
}
if (load_segment) {
ret = wcd_cpe_load_each_segment(core,
phdr_idx, phdr);
if (IS_ERR_VALUE(ret)) {
dev_err(core->dev,
"Failed to load segment %d, aborting img dload\n",
phdr_idx);
img_dload_fail = true;
goto done;
}
} else {
dev_dbg(core->dev,
"%s: skipped segment with index %d\n",
__func__, phdr_idx);
}
elf_ptr = elf_ptr + sizeof(*phdr);
}
if (load_type == ELF_FLAG_EXECUTE)
core->ssr_type = WCD_CPE_IMEM_DOWNLOADED;
done:
release_firmware(fw);
return ret;
}
/*
* wcd_cpe_change_online_state - mark cpe online/offline state
* @core: core session to mark
* @online: whether online of offline
*
*/
static void wcd_cpe_change_online_state(struct wcd_cpe_core *core,
int online)
{
struct wcd_cpe_ssr_entry *ssr_entry = NULL;
unsigned long ret;
if (!core) {
pr_err("%s: Invalid core handle\n",
__func__);
return;
}
ssr_entry = &core->ssr_entry;
WCD_CPE_GRAB_LOCK(&core->ssr_lock, "SSR");
ssr_entry->offline = !online;
wmb();
ret = xchg(&ssr_entry->offline_change, 1);
wake_up_interruptible(&ssr_entry->offline_poll_wait);
WCD_CPE_REL_LOCK(&core->ssr_lock, "SSR");
pr_debug("%s: change state 0x%x offline_change 0x%x\n"
" core->offline 0x%x, ret = %ld\n",
__func__, online,
ssr_entry->offline_change,
core->ssr_entry.offline, ret);
}
/*
* wcd_cpe_load_fw_image: work function to load the fw image
* @work: work that is scheduled to perform the image loading
*
* Parse the mdt file to look for program headers, load each
* split file corresponding to the program headers.
*/
static void wcd_cpe_load_fw_image(struct work_struct *work)
{
struct wcd_cpe_core *core;
int ret = 0;
core = container_of(work, struct wcd_cpe_core, load_fw_work);
ret = wcd_cpe_load_fw(core, ELF_FLAG_EXECUTE);
if (!ret)
wcd_cpe_change_online_state(core, 1);
else
pr_err("%s: failed to load instruction section, err = %d\n",
__func__, ret);
return;
}
/*
* wcd_cpe_get_core_handle: get the handle to wcd_cpe_core
* @codec: codec from which this handle is to be obtained
* Codec driver should provide a callback function to obtain
* handle to wcd_cpe_core during initialization of wcd_cpe_core
*/
void *wcd_cpe_get_core_handle(
struct snd_soc_codec *codec)
{
struct wcd_cpe_core *core = NULL;
if (!codec) {
pr_err("%s: Invalid codec handle\n",
__func__);
goto done;
}
if (!wcd_get_cpe_core) {
dev_err(codec->dev,
"%s: codec callback not available\n",
__func__);
goto done;
}
core = wcd_get_cpe_core(codec);
if (!core)
dev_err(codec->dev,
"%s: handle to core not available\n",
__func__);
done:
return core;
}
/*
* svass_engine_irq: threaded interrupt handler for svass engine irq
* @irq: interrupt number
* @data: data pointer passed during irq registration
*/
static irqreturn_t svass_engine_irq(int irq, void *data)
{
struct wcd_cpe_core *core = data;
int ret = 0;
if (!core) {
pr_err("%s: Invalid data for interrupt handler\n",
__func__);
goto done;
}
ret = cpe_svc_process_irq(core->cpe_handle, CPE_IRQ_OUTBOX_IRQ);
if (IS_ERR_VALUE(ret))
dev_err(core->dev,
"%s: Error processing irq from cpe_Services\n",
__func__);
done:
return IRQ_HANDLED;
}
/*
* wcd_cpe_state_read - update read status in procfs
* @entry: snd_info_entry
* @buf: buffer where the read status is updated.
*
*/
static ssize_t wcd_cpe_state_read(struct snd_info_entry *entry,
void *file_private_data, struct file *file,
char __user *buf, size_t count, loff_t pos)
{
int len = 0;
char buffer[WCD_CPE_STATE_MAX_LEN];
struct wcd_cpe_core *core = NULL;
struct wcd_cpe_ssr_entry *ssr_entry = NULL;
core = (struct wcd_cpe_core *) entry->private_data;
if (!core) {
pr_err("%s: CPE core NULL\n", __func__);
return -EINVAL;
}
ssr_entry = &core->ssr_entry;
rmb();
dev_dbg(core->dev,
"%s: Offline 0x%x\n", __func__,
ssr_entry->offline);
WCD_CPE_GRAB_LOCK(&core->ssr_lock, "SSR");
len = snprintf(buffer, sizeof(buffer), "%s\n",
ssr_entry->offline ? "OFFLINE" : "ONLINE");
WCD_CPE_REL_LOCK(&core->ssr_lock, "SSR");
return simple_read_from_buffer(buf, count, &pos, buffer, len);
}
/*
* wcd_cpe_state_poll - polls for change state
* @entry: snd_info_entry
* @wait: wait for duration for poll wait
*
*/
static unsigned int wcd_cpe_state_poll(struct snd_info_entry *entry,
void *private_data, struct file *file,
poll_table *wait)
{
struct wcd_cpe_core *core = NULL;
struct wcd_cpe_ssr_entry *ssr_entry = NULL;
int ret = 0;
core = (struct wcd_cpe_core *) entry->private_data;
if (!core) {
pr_err("%s: CPE core NULL\n", __func__);
return -EINVAL;
}
ssr_entry = &core->ssr_entry;
dev_dbg(core->dev, "%s: CPE Poll wait\n",
__func__);
poll_wait(file, &ssr_entry->offline_poll_wait, wait);
dev_dbg(core->dev, "%s: Wake-up Poll wait\n",
__func__);
WCD_CPE_GRAB_LOCK(&core->ssr_lock, "SSR");
if (xchg(&ssr_entry->offline_change, 0))
ret = POLLIN | POLLPRI | POLLRDNORM;
WCD_CPE_REL_LOCK(&core->ssr_lock, "SSR");
dev_dbg(core->dev, "%s: ret (%d) from poll_wait\n",
__func__, ret);
return ret;
}
/*
* wcd_cpe_is_online_state - return true if card is online state
* @core: core offline to query
*/
bool wcd_cpe_is_online_state(void *core_handle)
{
struct wcd_cpe_core *core = core_handle;
if (core_handle) {
return !core->ssr_entry.offline;
} else {
pr_err("%s: Core handle NULL\n", __func__);
/* still return 1- offline if core ptr null */
return false;
}
}
static struct snd_info_entry_ops wcd_cpe_state_proc_ops = {
.read = wcd_cpe_state_read,
.poll = wcd_cpe_state_poll,
};
static int wcd_cpe_enable(struct wcd_cpe_core *core,
bool enable)
{
int ret = 0;
if (enable) {
ret = wcd_cpe_setup_irqs(core);
if (ret) {
dev_err(core->dev,
"%s: CPE IRQs setup failed, error = %d\n",
__func__, ret);
goto done;
}
/* Dload data section */
ret = wcd_cpe_load_fw(core, ELF_FLAG_RW);
if (ret) {
dev_err(core->dev,
"%s: Failed to dload data section, err = %d\n",
__func__, ret);
goto fail_boot;
}
wcd_cpe_enable_cpe_clks(core, true);
ret = cpe_svc_boot(core->cpe_handle,
core->cpe_debug_mode);
if (IS_ERR_VALUE(ret)) {
dev_err(core->dev,
"%s: Failed to boot CPE\n",
__func__);
goto fail_boot;
}
/* wait for CPE to be online */
dev_dbg(core->dev,
"%s: waiting for CPE bootup\n",
__func__);
wait_for_completion(&core->online_compl);
dev_dbg(core->dev,
"%s: CPE bootup done\n",
__func__);
core->ssr_type = WCD_CPE_ENABLED;
} else {
if (core->ssr_type == WCD_CPE_BUS_DOWN_EVENT ||
core->ssr_type == WCD_CPE_SSR_EVENT) {
/*
* If this disable vote is when
* SSR is in progress, do not disable CPE here,
* instead SSR handler will control CPE.
*/
wcd_cpe_enable_cpe_clks(core, false);
wcd_cpe_cleanup_irqs(core);
goto done;
}
/* Reset CPE first */
ret = cpe_svc_reset(core->cpe_handle);
if (IS_ERR_VALUE(ret)) {
dev_err(core->dev,
"%s: Failed to reset CPE with error %d\n",
__func__, ret);
goto done;
}
wcd_cpe_enable_cpe_clks(core, false);
wcd_cpe_cleanup_irqs(core);
core->ssr_type = WCD_CPE_IMEM_DOWNLOADED;
}
return ret;
fail_boot:
wcd_cpe_cleanup_irqs(core);
done:
return ret;
}
/*
* wcd_cpe_boot_ssr: Load the images to CPE after ssr and bootup cpe
* @core: handle to the core
*/
static int wcd_cpe_boot_ssr(struct wcd_cpe_core *core)
{
int rc = 0;
if (!core || !core->cpe_handle) {
pr_err("%s: Invalid handle\n", __func__);
rc = -EINVAL;
goto fail;
}
/* Load the instruction section and mark CPE as online */
rc = wcd_cpe_load_fw(core, ELF_FLAG_EXECUTE);
if (rc) {
dev_err(core->dev,
"%s: Failed to load instruction, err = %d\n",
__func__, rc);
goto fail;
} else {
wcd_cpe_change_online_state(core, 1);
}
fail:
return rc;
}
/*
* wcd_cpe_clr_ready_status:
* Clear the value from the ready status for CPE
* @core: handle to the core
* @value: flag/bitmask that is to be cleared
*
* This function should not be invoked with ssr_lock acquired
*/
static void wcd_cpe_clr_ready_status(struct wcd_cpe_core *core,
u8 value)
{
WCD_CPE_GRAB_LOCK(&core->ssr_lock, "SSR");
core->ready_status &= ~(value);
dev_dbg(core->dev,
"%s: ready_status = 0x%x\n",
__func__, core->ready_status);
WCD_CPE_REL_LOCK(&core->ssr_lock, "SSR");
}
/*
* wcd_cpe_set_and_complete:
* Set the ready status with the provided value and
* flag the completion object if ready status moves
* to ready to download
* @core: handle to the core
* @value: flag/bitmask that is to be set
*/
static void wcd_cpe_set_and_complete(struct wcd_cpe_core *core,
u8 value)
{
WCD_CPE_GRAB_LOCK(&core->ssr_lock, "SSR");
core->ready_status |= value;
if ((core->ready_status & WCD_CPE_READY_TO_DLOAD) ==
WCD_CPE_READY_TO_DLOAD) {
dev_dbg(core->dev,
"%s: marking ready, status = 0x%x\n",
__func__, core->ready_status);
complete(&core->ready_compl);
}
WCD_CPE_REL_LOCK(&core->ssr_lock, "SSR");
}
/*
* wcd_cpe_ssr_work: work function to handle CPE SSR
* @work: work that is scheduled to perform CPE shutdown
* and restart
*/
void wcd_cpe_ssr_work(struct work_struct *work)
{
int rc = 0;
u32 irq = 0;
struct wcd_cpe_core *core = NULL;
core = container_of(work, struct wcd_cpe_core, ssr_work);
if (!core) {
pr_err("%s: Core handle NULL\n", __func__);
return;
}
/* Obtain pm request up in case of suspend mode */
pm_qos_add_request(&core->pm_qos_req,
PM_QOS_CPU_DMA_LATENCY,
PM_QOS_DEFAULT_VALUE);
pm_qos_update_request(&core->pm_qos_req,
msm_cpuidle_get_deep_idle_latency());
dev_dbg(core->dev,
"%s: CPE SSR with event %d\n",
__func__, core->ssr_type);
if (core->ssr_type == WCD_CPE_SSR_EVENT) {
rc = snd_soc_read(core->codec,
TOMTOM_A_SVASS_INT_STATUS);
if (rc & SVASS_INT_STATUS_RCO_WDOG)
irq = CPE_IRQ_RCO_WDOG_INT;
else
/*
* For all other IRQ's treat
* as WDOG_BITE internally
*/
irq = CPE_IRQ_WDOG_BITE;
} else {
/* If bus is down, cdc reg cannot be read */
irq = CPE_IRQ_WDOG_BITE;
}
if (core->cpe_users > 0) {
rc = cpe_svc_process_irq(core->cpe_handle, irq);
if (IS_ERR_VALUE(rc))
/*
* Even if process_irq fails,
* wait for cpe to move to offline state
*/
dev_err(core->dev,
"%s: irq processing failed, error = %d\n",
__func__, rc);
rc = wait_for_completion_timeout(&core->offline_compl,
CPE_OFFLINE_WAIT_TIMEOUT);
if (!rc) {
dev_err(core->dev,
"%s: wait for cpe offline timed out\n",
__func__);
goto err_ret;
}
if (core->ssr_type != WCD_CPE_BUS_DOWN_EVENT &&
ramdump_enable) {
/*
* Ramdump has to be explicitly enabled
* through debugfs and cannot be collected
* when bus is down.
*/
wcd_cpe_collect_ramdump(core);
}
} else {
pr_err("%s: no cpe users, mark as offline\n", __func__);
wcd_cpe_change_online_state(core, 0);
wcd_cpe_set_and_complete(core,
WCD_CPE_BLK_READY);
}
rc = wait_for_completion_timeout(&core->ready_compl,
CPE_READY_WAIT_TIMEOUT);
if (!rc) {
dev_err(core->dev,
"%s: ready to online timed out, status = %u\n",
__func__, core->ready_status);
goto err_ret;
}
rc = wcd_cpe_boot_ssr(core);
/* Once image are downloaded make sure all
* error interrupts are cleared
*/
snd_soc_update_bits(core->codec,
TOMTOM_A_SVASS_INT_CLR,
0x3F, 0x3F);
err_ret:
/* remove after default pm qos */
pm_qos_update_request(&core->pm_qos_req,
PM_QOS_DEFAULT_VALUE);
pm_qos_remove_request(&core->pm_qos_req);
}
/*
* wcd_cpe_ssr_handle: handle SSR events here.
* @core_handle: handle to the cpe core
* @event: indicates ADSP or CDSP SSR.
*/
int wcd_cpe_ssr_event(void *core_handle,
enum wcd_cpe_ssr_state_event event)
{
struct wcd_cpe_core *core = core_handle;
if (!core) {
pr_err("%s: Invalid handle to core\n",
__func__);
return -EINVAL;
}
/*
* If CPE is not even enabled, the SSR event for
* CPE needs to be ignored
*/
if (core->ssr_type == WCD_CPE_INITIALIZED) {
dev_info(core->dev,
"%s: CPE initialized but not enabled, skip CPE ssr\n",
__func__);
return 0;
}
dev_dbg(core->dev,
"%s: Schedule ssr work, event = %d\n",
__func__, core->ssr_type);
switch (event) {
case WCD_CPE_BUS_DOWN_EVENT:
/*
* If bus down, then CPE block is also
* treated to be down
*/
wcd_cpe_clr_ready_status(core, WCD_CPE_READY_TO_DLOAD);
core->ssr_type = event;
schedule_work(&core->ssr_work);
break;
case WCD_CPE_SSR_EVENT:
wcd_cpe_clr_ready_status(core, WCD_CPE_BLK_READY);
core->ssr_type = event;
schedule_work(&core->ssr_work);
break;
case WCD_CPE_BUS_UP_EVENT:
wcd_cpe_set_and_complete(core, WCD_CPE_BUS_READY);
/*
* In case of bus up event ssr_type will be changed
* to WCD_CPE_ACTIVE once CPE is online
*/
break;
default:
dev_err(core->dev,
"%s: unhandled SSR event %d\n",
__func__, event);
return -EINVAL;
}
return 0;
}
EXPORT_SYMBOL(wcd_cpe_ssr_event);
/*
* svass_exception_irq: threaded irq handler for sva error interrupts
* @irq: interrupt number
* @data: data pointer passed during irq registration
*
* Once a error interrupt is received, it is not cleared, since
* clearing this interrupt will raise spurious interrupts unless
* CPE is reset.
*/
static irqreturn_t svass_exception_irq(int irq, void *data)
{
struct wcd_cpe_core *core = data;
u8 status = 0;
status = snd_soc_read(core->codec,
TOMTOM_A_SVASS_INT_STATUS);
dev_err(core->dev,
"%s: err_interrupt status = 0x%x\n",
__func__, status);
if (status & SVASS_FATAL_IRQS) {
wcd_cpe_ssr_event(core, WCD_CPE_SSR_EVENT);
} else {
/* Make sure all error interrupts are cleared */
snd_soc_update_bits(core->codec,
TOMTOM_A_SVASS_INT_CLR,
0x3F, 0x3F);
}
return IRQ_HANDLED;
}
/*
* wcd_cpe_cmi_afe_cb: callback called on response to afe commands
* @param: parameter containing the response code, etc
*
* Process the request to the command sent to CPE and wakeup the
* command send wait.
*/
void wcd_cpe_cmi_afe_cb(const struct cmi_api_notification *param)
{
struct cmi_hdr *hdr;
struct wcd_cmi_afe_port_data *afe_port_d;
u8 port_id;
if (!param) {
pr_err("%s: param is null\n", __func__);
return;
}
if (param->event != CMI_API_MSG) {
pr_err("%s: unhandled event 0x%x\n",
__func__, param->event);
return;
}
pr_debug("%s: param->result = %d\n",
__func__, param->result);
hdr = (struct cmi_hdr *) param->message;
/*
* for AFE cmd response, port id is
* stored at session id field of header
*/
port_id = CMI_HDR_GET_SESSION_ID(hdr);
if (port_id > WCD_CPE_AFE_MAX_PORTS) {
pr_err("%s: invalid port_id %d\n",
__func__, port_id);
return;
}
afe_port_d = &(afe_ports[port_id]);
if (hdr->opcode == CPE_CMI_BASIC_RSP_OPCODE) {
u8 *payload = ((u8 *)param->message) + (sizeof(struct cmi_hdr));
u8 result = payload[0];
afe_port_d->cmd_result = result;
complete(&afe_port_d->afe_cmd_complete);
} else if (hdr->opcode == CPE_AFE_PORT_CMDRSP_SHARED_MEM_ALLOC) {
struct cpe_cmdrsp_shmem_alloc *cmdrsp_shmem_alloc =
(struct cpe_cmdrsp_shmem_alloc *) param->message;
if (cmdrsp_shmem_alloc->addr == 0) {
pr_err("%s: Failed AFE shared mem alloc\n", __func__);
afe_port_d->cmd_result = CMI_SHMEM_ALLOC_FAILED;
} else {
pr_debug("%s AFE shared mem addr = 0x%x\n",
__func__, cmdrsp_shmem_alloc->addr);
afe_port_d->mem_handle = cmdrsp_shmem_alloc->addr;
afe_port_d->cmd_result = 0;
}
complete(&afe_port_d->afe_cmd_complete);
}
return;
}
/*
* wcd_cpe_initialize_afe_port_data: Initialize all AFE ports
*
* Initialize the data for all the afe ports. Assign the
* afe port state to INIT state.
*/
static void wcd_cpe_initialize_afe_port_data(void)
{
struct wcd_cmi_afe_port_data *afe_port_d;
int i = 0;
for (i = 1; i <= WCD_CPE_AFE_MAX_PORTS; i++) {
afe_port_d = &afe_ports[i];
afe_port_d->port_id = i;
init_completion(&afe_port_d->afe_cmd_complete);
afe_port_d->port_state = AFE_PORT_STATE_INIT;
mutex_init(&afe_port_d->afe_lock);
}
}
/*
* wcd_cpe_deinitialize_afe_port_data: De-initialize all AFE ports
*
* De-Initialize the data for all the afe ports. Assign the
* afe port state to DEINIT state.
*/
static void wcd_cpe_deinitialize_afe_port_data(void)
{
struct wcd_cmi_afe_port_data *afe_port_d;
int i = 0;
for (i = 1; i <= WCD_CPE_AFE_MAX_PORTS; i++) {
afe_port_d = &afe_ports[i];
afe_port_d->port_state = AFE_PORT_STATE_DEINIT;
mutex_destroy(&afe_port_d->afe_lock);
}
}
/*
* wcd_cpe_svc_event_cb: callback from cpe services, indicating
* CPE is online or offline.
* @param: parameter / payload for event to be notified
*/
static void wcd_cpe_svc_event_cb(const struct cpe_svc_notification *param)
{
struct snd_soc_codec *codec;
struct wcd_cpe_core *core;
bool active_sessions;
if (!param) {
pr_err("%s: Invalid event\n", __func__);
return;
}
codec = param->private_data;
if (!codec) {
pr_err("%s: Invalid handle to codec\n",
__func__);
return;
}
core = wcd_cpe_get_core_handle(codec);
if (!core) {
pr_err("%s: Invalid handle to core\n",
__func__);
return;
}
dev_dbg(core->dev,
"%s: event = 0x%x, ssr_type = 0x%x\n",
__func__, param->event, core->ssr_type);
switch (param->event) {
case CPE_SVC_ONLINE:
core->ssr_type = WCD_CPE_ACTIVE;
dev_dbg(core->dev, "%s CPE is now online\n",
__func__);
complete(&core->online_compl);
break;
case CPE_SVC_OFFLINE:
active_sessions = wcd_cpe_lsm_session_active();
wcd_cpe_change_online_state(core, 0);
complete(&core->offline_compl);
dev_err(core->dev, "%s: CPE is now offline\n",
__func__);
break;
case CPE_SVC_CMI_CLIENTS_DEREG:
/*
* Only when either CPE SSR is in progress,
* or the bus is down, we need to mark the CPE
* as ready. In all other cases, this event is
* ignored
*/
if (core->ssr_type == WCD_CPE_SSR_EVENT ||
core->ssr_type == WCD_CPE_BUS_DOWN_EVENT)
wcd_cpe_set_and_complete(core,
WCD_CPE_BLK_READY);
break;
default:
dev_err(core->dev,
"%s: unhandled notification\n",
__func__);
break;
}
return;
}
/*
* wcd_cpe_cleanup_irqs: free the irq resources required by cpe
* @core: handle the cpe core
*
* This API will free the IRQs for CPE but does not mask the
* CPE interrupts. If masking is needed, it has to be done
* explicity by caller.
*/
static void wcd_cpe_cleanup_irqs(struct wcd_cpe_core *core)
{
struct snd_soc_codec *codec = core->codec;
struct wcd9xxx *wcd9xxx = codec->control_data;
struct wcd9xxx_core_resource *core_res = &wcd9xxx->core_res;
wcd9xxx_free_irq(core_res,
WCD9330_IRQ_SVASS_ENGINE,
core);
wcd9xxx_free_irq(core_res,
WCD9330_IRQ_SVASS_ERR_EXCEPTION,
core);
}
/*
* wcd_cpe_setup_sva_err_intr: setup the irqs for CPE
* @core: handle to wcd_cpe_core
* All interrupts needed for CPE are acquired. If any
* request_irq fails, then all irqs are free'd
*/
static int wcd_cpe_setup_irqs(struct wcd_cpe_core *core)
{
int ret;
struct snd_soc_codec *codec = core->codec;
struct wcd9xxx *wcd9xxx = codec->control_data;
struct wcd9xxx_core_resource *core_res = &wcd9xxx->core_res;
ret = wcd9xxx_request_irq(core_res, WCD9330_IRQ_SVASS_ENGINE,
svass_engine_irq, "SVASS_Engine", core);
if (ret) {
dev_err(core->dev,
"%s: Failed to request svass engine irq\n",
__func__);
goto fail_engine_irq;
}
/* Make sure all error interrupts are cleared */
snd_soc_update_bits(codec, TOMTOM_A_SVASS_INT_CLR,
0x3F, 0x3F);
/* Enable required error interrupts */
snd_soc_update_bits(codec, TOMTOM_A_SVASS_INT_MASK,
0x3F, 0x0C);
ret = wcd9xxx_request_irq(core_res, WCD9330_IRQ_SVASS_ERR_EXCEPTION,
svass_exception_irq, "SVASS_Exception", core);
if (ret) {
dev_err(core->dev,
"%s: Failed to request svass err irq\n",
__func__);
goto fail_exception_irq;
}
return 0;
fail_exception_irq:
wcd9xxx_free_irq(core_res, WCD9330_IRQ_SVASS_ENGINE, core);
fail_engine_irq:
return ret;
}
static int wcd_cpe_get_cal_index(int32_t cal_type)
{
int cal_index = -EINVAL;
if (cal_type == ULP_AFE_CAL_TYPE)
cal_index = WCD_CPE_LSM_CAL_AFE;
else if (cal_type == ULP_LSM_CAL_TYPE)
cal_index = WCD_CPE_LSM_CAL_LSM;
return cal_index;
}
static int wcd_cpe_alloc_cal(int32_t cal_type, size_t data_size, void *data)
{
int ret = 0;
int cal_index;
cal_index = wcd_cpe_get_cal_index(cal_type);
if (cal_index < 0) {
pr_err("%s: invalid caltype %d\n",
__func__, cal_type);
return -EINVAL;
}
ret = cal_utils_alloc_cal(data_size, data,
core_d->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);
return ret;
}
static int wcd_cpe_dealloc_cal(int32_t cal_type, size_t data_size,
void *data)
{
int ret = 0;
int cal_index;
cal_index = wcd_cpe_get_cal_index(cal_type);
if (cal_index < 0) {
pr_err("%s: invalid caltype %d\n",
__func__, cal_type);
return -EINVAL;
}
ret = cal_utils_dealloc_cal(data_size, data,
core_d->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);
return ret;
}
static int wcd_cpe_set_cal(int32_t cal_type, size_t data_size, void *data)
{
int ret = 0;
int cal_index;
cal_index = wcd_cpe_get_cal_index(cal_type);
if (cal_index < 0) {
pr_err("%s: invalid caltype %d\n",
__func__, cal_type);
return -EINVAL;
}
ret = cal_utils_set_cal(data_size, data,
core_d->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);
return ret;
}
static int wcd_cpe_cal_init(struct wcd_cpe_core *core)
{
int ret = 0;
struct cal_type_info cal_type_info[] = {
{{ULP_AFE_CAL_TYPE,
{wcd_cpe_alloc_cal, wcd_cpe_dealloc_cal, NULL,
wcd_cpe_set_cal, NULL, NULL} },
{NULL, NULL, cal_utils_match_buf_num} },
{{ULP_LSM_CAL_TYPE,
{wcd_cpe_alloc_cal, wcd_cpe_dealloc_cal, NULL,
wcd_cpe_set_cal, NULL, NULL} },
{NULL, NULL, cal_utils_match_buf_num} },
};
ret = cal_utils_create_cal_types(WCD_CPE_LSM_CAL_MAX,
core->cal_data,
cal_type_info);
if (ret < 0)
pr_err("%s: could not create cal type!\n",
__func__);
return ret;
}
/*
* wcd_cpe_enable: setup the cpe interrupts and schedule
* the work to download image and bootup the CPE.
* core: handle to cpe core structure
*/
static int wcd_cpe_vote(struct wcd_cpe_core *core,
bool enable)
{
int ret = 0;
if (!core) {
pr_err("%s: Invalid handle to core\n",
__func__);
ret = -EINVAL;
goto done;
}
dev_dbg(core->dev,
"%s: enter, enable = %s, cpe_users = %u\n",
__func__, (enable ? "true" : "false"),
core->cpe_users);
if (enable) {
if (core->cpe_users == 0) {
ret = wcd_cpe_enable(core, enable);
if (ret) {
dev_err(core->dev,
"%s: CPE enable failed, err = %d\n",
__func__, ret);
goto done;
}
core->cpe_users++;
} else {
dev_dbg(core->dev,
"%s: cpe already enabled, users = %u\n",
__func__, core->cpe_users);
goto done;
}
} else {
if (core->cpe_users == 1) {
ret = wcd_cpe_enable(core, enable);
if (ret) {
dev_err(core->dev,
"%s: CPE disable failed, err = %d\n",
__func__, ret);
goto done;
}
core->cpe_users--;
} else {
dev_dbg(core->dev,
"%s: %u valid users on cpe\n",
__func__, core->cpe_users);
goto done;
}
}
dev_dbg(core->dev,
"%s: leave, enable = %s, cpe_users = %u\n",
__func__, (enable ? "true" : "false"),
core->cpe_users);
done:
return ret;
}
static int wcd_cpe_debugfs_init(struct wcd_cpe_core *core)
{
int rc;
struct dentry *dir = debugfs_create_dir("wcd_cpe", NULL);
if (IS_ERR_OR_NULL(dir)) {
dir = NULL;
rc = -ENODEV;
goto err_create_dir;
}
if (!debugfs_create_u32("ramdump_enable", S_IRUGO | S_IWUSR,
dir, &ramdump_enable)) {
dev_err(core->dev, "%s: Failed to create debugfs node %s\n",
__func__, "ramdump_enable");
rc = -ENODEV;
goto err_create_entry;
}
err_create_entry:
debugfs_remove(dir);
err_create_dir:
return rc;
}
/*
* wcd_cpe_init: Initialize CPE related structures
* @img_fname: filename for firmware image
* @codec: handle to codec requesting for image download
* @params: parameter structure passed from caller
*
* This API will initialize the cpe core but will not
* download the image or boot the cpe core.
*/
struct wcd_cpe_core *wcd_cpe_init(const char *img_fname,
struct snd_soc_codec *codec,
struct wcd_cpe_params *params)
{
struct wcd_cpe_core *core;
int ret = 0;
struct snd_card *card = NULL;
struct snd_info_entry *entry = NULL;
char proc_name[WCD_CPE_STATE_MAX_LEN];
const char *cpe_name = "cpe";
const char *state_name = "_state";
int id = 0;
if (!codec) {
pr_err("%s: Invalid codec\n", __func__);
return NULL;
}
if (!params) {
dev_err(codec->dev,
"%s: No params supplied for codec %s\n",
__func__, codec->name);
return NULL;
}
if (!params->codec || !params->get_cpe_core ||
!params->cdc_cb) {
dev_err(codec->dev,
"%s: Invalid params for codec %s\n",
__func__, codec->name);
return NULL;
}
core = kzalloc(sizeof(struct wcd_cpe_core), GFP_KERNEL);
if (!core) {
dev_err(codec->dev,
"%s: Failed to allocate cpe core data\n",
__func__);
return NULL;
}
snprintf(core->fname, sizeof(core->fname), "%s", img_fname);
wcd_get_cpe_core = params->get_cpe_core;
core->codec = params->codec;
core->dev = params->codec->dev;
core->cpe_debug_mode = params->dbg_mode;
core->cdc_info.major_version = params->cdc_major_ver;
core->cdc_info.minor_version = params->cdc_minor_ver;
core->cdc_info.id = params->cdc_id;
core->cpe_cdc_cb = params->cdc_cb;
INIT_WORK(&core->load_fw_work, wcd_cpe_load_fw_image);
INIT_WORK(&core->ssr_work, wcd_cpe_ssr_work);
init_completion(&core->offline_compl);
init_completion(&core->ready_compl);
init_completion(&core->online_compl);
init_waitqueue_head(&core->ssr_entry.offline_poll_wait);
mutex_init(&core->ssr_lock);
core->cpe_users = 0;
/*
* By default, during probe, it is assumed that
* both CPE hardware block and underlying bus to codec
* are ready
*/
core->ready_status = WCD_CPE_READY_TO_DLOAD;
core->cpe_handle = cpe_svc_initialize(NULL, &core->cdc_info, codec);
if (!core->cpe_handle) {
dev_err(core->dev,
"%s: failed to initialize cpe services\n",
__func__);
goto fail_cpe_initialize;
}
core->cpe_reg_handle = cpe_svc_register(core->cpe_handle,
wcd_cpe_svc_event_cb,
CPE_SVC_ONLINE | CPE_SVC_OFFLINE |
CPE_SVC_CMI_CLIENTS_DEREG,
"codec cpe handler");
if (!core->cpe_reg_handle) {
dev_err(core->dev,
"%s: failed to register cpe service\n",
__func__);
goto fail_cpe_register;
}
card = codec->card->snd_card;
snprintf(proc_name, (sizeof("cpe") + sizeof("_state") +
sizeof(id) - 2), "%s%d%s", cpe_name, id, state_name);
entry = snd_info_create_card_entry(card, proc_name,
card->proc_root);
if (entry) {
core->ssr_entry.entry = entry;
core->ssr_entry.offline = 1;
entry->size = WCD_CPE_STATE_MAX_LEN;
entry->content = SNDRV_INFO_CONTENT_DATA;
entry->c.ops = &wcd_cpe_state_proc_ops;
entry->private_data = core;
ret = snd_info_register(entry);
if (ret < 0) {
dev_err(core->dev,
"%s: snd_info_register failed (%d)\n",
__func__, ret);
snd_info_free_entry(entry);
entry = NULL;
}
} else {
dev_err(core->dev,
"%s: Failed to create CPE SSR status entry\n",
__func__);
/*
* Even if SSR entry creation fails, continue
* with image download
*/
}
core_d = core;
ret = wcd_cpe_cal_init(core);
if (IS_ERR_VALUE(ret)) {
dev_err(core->dev,
"%s: CPE calibration init failed, err = %d\n",
__func__, ret);
goto fail_cpe_reset;
}
wcd_cpe_debugfs_init(core);
/* Setup the ramdump device and buffer */
core->cpe_ramdump_dev = create_ramdump_device("cpe",
core->dev);
if (!core->cpe_ramdump_dev) {
dev_err(core->dev,
"%s: Failed to create ramdump device\n",
__func__);
goto schedule_dload_work;
}
core->cpe_dump_v_addr = dma_alloc_coherent(core->dev,
WCD_CPE_DRAM_SIZE,
&core->cpe_dump_addr,
GFP_KERNEL);
if (!core->cpe_dump_v_addr) {
dev_err(core->dev,
"%s: Failed to alloc memory for cpe dump, size = %d\n",
__func__, WCD_CPE_DRAM_SIZE);
goto schedule_dload_work;
} else {
memset(core->cpe_dump_v_addr, 0, WCD_CPE_DRAM_SIZE);
}
schedule_dload_work:
core->ssr_type = WCD_CPE_INITIALIZED;
schedule_work(&core->load_fw_work);
return core;
fail_cpe_reset:
cpe_svc_deregister(core->cpe_handle, core->cpe_reg_handle);
fail_cpe_register:
cpe_svc_deinitialize(core->cpe_handle);
fail_cpe_initialize:
kfree(core);
return NULL;
}
EXPORT_SYMBOL(wcd_cpe_init);
/*
* wcd_cpe_cmi_lsm_callback: callback called from cpe services
* to notify command response for lsm
* service
* @param: param containing the response code and status
*
* This callback is registered with cpe services while registering
* the LSM service
*/
void wcd_cpe_cmi_lsm_callback(const struct cmi_api_notification *param)
{
struct cmi_hdr *hdr;
struct cpe_lsm_session *lsm_session;
u8 session_id;
if (!param) {
pr_err("%s: param is null\n", __func__);
return;
}
if (param->event != CMI_API_MSG) {
pr_err("%s: unhandled event 0x%x\n", __func__, param->event);
return;
}
hdr = (struct cmi_hdr *) param->message;
session_id = CMI_HDR_GET_SESSION_ID(hdr);
if (session_id > WCD_CPE_LSM_MAX_SESSIONS) {
pr_err("%s: invalid lsm session id = %d\n",
__func__, session_id);
return;
}
lsm_session = lsm_sessions[session_id];
if (hdr->opcode == CPE_CMI_BASIC_RSP_OPCODE) {
u8 *payload = ((u8 *)param->message) + (sizeof(struct cmi_hdr));
u8 result = payload[0];
lsm_session->cmd_err_code |= result;
complete(&lsm_session->cmd_comp);
} else if (hdr->opcode == CPE_LSM_SESSION_CMDRSP_SHARED_MEM_ALLOC) {
struct cpe_cmdrsp_shmem_alloc *cmdrsp_shmem_alloc =
(struct cpe_cmdrsp_shmem_alloc *) param->message;
if (cmdrsp_shmem_alloc->addr == 0) {
pr_err("%s: Failed LSM shared mem alloc\n", __func__);
lsm_session->cmd_err_code = CMI_SHMEM_ALLOC_FAILED;
} else {
pr_debug("%s LSM shared mem addr = 0x%x\n",
__func__, cmdrsp_shmem_alloc->addr);
lsm_session->lsm_mem_handle = cmdrsp_shmem_alloc->addr;
lsm_session->cmd_err_code = 0;
}
complete(&lsm_session->cmd_comp);
} else if (hdr->opcode == CPE_LSM_SESSION_EVENT_DETECTION_STATUS_V2) {
struct cpe_lsm_event_detect_v2 *event_detect_v2 =
(struct cpe_lsm_event_detect_v2 *) param->message;
if (!lsm_session->priv_d) {
pr_err("%s: private data is not present\n",
__func__);
return;
}
pr_debug("%s: event payload, status = %u, size = %u\n",
__func__, event_detect_v2->detection_status,
event_detect_v2->size);
if (lsm_session->event_cb)
lsm_session->event_cb(
lsm_session->priv_d,
event_detect_v2->detection_status,
event_detect_v2->size,
event_detect_v2->payload);
}
return;
}
/*
* wcd_cpe_cmi_send_lsm_msg: send a message to lsm service
* @core: handle to cpe core
* @session: session on which to send the message
* @message: actual message containing header and payload
*
* Sends message to lsm service for specified session and wait
* for response back on the message.
* should be called after acquiring session specific mutex
*/
static int wcd_cpe_cmi_send_lsm_msg(
struct wcd_cpe_core *core,
struct cpe_lsm_session *session,
void *message)
{
int ret = 0;
struct cmi_hdr *hdr = message;
pr_debug("%s: sending message with opcode 0x%x\n",
__func__, hdr->opcode);
if (unlikely(!wcd_cpe_is_online_state(core))) {
dev_err(core->dev,
"%s: MSG not sent, CPE offline\n",
__func__);
return 0;
}
ret = cmi_send_msg(message);
if (ret) {
pr_err("%s: msg opcode (0x%x) send failed (%d)\n",
__func__, hdr->opcode, ret);
return ret;
}
ret = wait_for_completion_timeout(&session->cmd_comp,
CMI_CMD_TIMEOUT);
if (ret > 0) {
pr_debug("%s: command 0x%x, received response 0x%x\n",
__func__, hdr->opcode, session->cmd_err_code);
if (session->cmd_err_code)
return session->cmd_err_code;
} else {
pr_err("%s: command (0x%x) send timed out\n",
__func__, hdr->opcode);
return -ETIMEDOUT;
}
INIT_COMPLETION(session->cmd_comp);
return 0;
}
/*
* fill_cmi_header: fill the cmi header with specified values
*
* @hdr: header to be updated with values
* @session_id: session id of the header,
* in case of AFE service it is port_id
* @service_id: afe/lsm, etc
* @version: update the version field in header
* @payload_size: size of the payload following after header
* @opcode: opcode of the message
* @obm_flag: indicates if this header is for obm message
*
*/
static int fill_cmi_header(struct cmi_hdr *hdr,
u8 session_id, u8 service_id,
bool version, u8 payload_size,
u16 opcode, bool obm_flag)
{
/* sanitize the data */
if (!IS_VALID_SESSION_ID(session_id) ||
!IS_VALID_SERVICE_ID(service_id) ||
!IS_VALID_PLD_SIZE(payload_size)) {
pr_err("Invalid header creation request\n");
return -EINVAL;
}
CMI_HDR_SET_SESSION(hdr, session_id);
CMI_HDR_SET_SERVICE(hdr, service_id);
if (version)
CMI_HDR_SET_VERSION(hdr, 1);
else
CMI_HDR_SET_VERSION(hdr, 0);
CMI_HDR_SET_PAYLOAD_SIZE(hdr, payload_size);
hdr->opcode = opcode;
if (obm_flag)
CMI_HDR_SET_OBM(hdr, CMI_OBM_FLAG_OUT_BAND);
else
CMI_HDR_SET_OBM(hdr, CMI_OBM_FLAG_IN_BAND);
return 0;
}
/*
* fill_lsm_cmd_header_v0_inband:
* Given the header, fill the header with information
* for lsm service, version 0 and inband message
* @hdr: the cmi header to be filled.
* @session_id: ID for the lsm session
* @payload_size: size for cmi message payload
* @opcode: opcode for cmi message
*/
static int fill_lsm_cmd_header_v0_inband(struct cmi_hdr *hdr,
u8 session_id, u8 payload_size, u16 opcode)
{
return fill_cmi_header(hdr, session_id,
CMI_CPE_LSM_SERVICE_ID, false,
payload_size, opcode, false);
}
/*
* wcd_cpe_is_valid_lsm_session:
* Check session paramters to identify validity for the sesion
* @core: handle to cpe core
* @session: handle to the lsm session
* @func: invoking function to be printed in error logs
*/
static int wcd_cpe_is_valid_lsm_session(struct wcd_cpe_core *core,
struct cpe_lsm_session *session,
const char *func)
{
if (unlikely(IS_ERR_OR_NULL(core))) {
pr_err("%s: invalid handle to core\n",
func);
return -EINVAL;
}
if (unlikely(IS_ERR_OR_NULL(session))) {
dev_err(core->dev, "%s: invalid session\n",
func);
return -EINVAL;
}
if (session->id > WCD_CPE_LSM_MAX_SESSIONS) {
dev_err(core->dev, "%s: invalid session id (%u)\n",
func, session->id);
return -EINVAL;
}
dev_dbg(core->dev, "%s: session_id = %u\n",
func, session->id);
return 0;
}
/*
* wcd_cpe_cmd_lsm_open_tx: compose and send lsm open command
* @core_handle: handle to cpe core
* @session: session for which the command needs to be sent
* @app_id: application id part of the command
* @sample_rate: sample rate for this session
*/
static int wcd_cpe_cmd_lsm_open_tx(void *core_handle,
struct cpe_lsm_session *session,
u16 app_id, u16 sample_rate)
{
struct cpe_lsm_cmd_open_tx cmd_open_tx;
struct wcd_cpe_core *core = core_handle;
int ret = 0;
ret = wcd_cpe_is_valid_lsm_session(core, session,
__func__);
if (ret)
return ret;
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
memset(&cmd_open_tx, 0, sizeof(struct cpe_lsm_cmd_open_tx));
if (fill_lsm_cmd_header_v0_inband(&cmd_open_tx.hdr,
session->id, OPEN_CMD_PAYLOAD_SIZE,
CPE_LSM_SESSION_CMD_OPEN_TX)) {
ret = -EINVAL;
goto end_ret;
}
cmd_open_tx.app_id = app_id;
cmd_open_tx.sampling_rate = sample_rate;
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &cmd_open_tx);
if (ret)
dev_err(core->dev,
"%s: failed to send open_tx cmd, err = %d\n",
__func__, ret);
end_ret:
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return ret;
}
/*
* wcd_cpe_cmd_close_tx: compose and send lsm close command
* @core_handle: handle to cpe core
* @session: session for which the command needs to be sent
*/
static int wcd_cpe_cmd_lsm_close_tx(void *core_handle,
struct cpe_lsm_session *session)
{
struct cmi_hdr cmd_close_tx;
struct wcd_cpe_core *core = core_handle;
int ret = 0;
ret = wcd_cpe_is_valid_lsm_session(core, session,
__func__);
if (ret)
return ret;
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
memset(&cmd_close_tx, 0, sizeof(cmd_close_tx));
if (fill_lsm_cmd_header_v0_inband(&cmd_close_tx, session->id,
0, CPE_LSM_SESSION_CMD_CLOSE_TX)) {
ret = -EINVAL;
goto end_ret;
}
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &cmd_close_tx);
if (ret)
dev_err(core->dev,
"%s: lsm close_tx cmd failed, err = %d\n",
__func__, ret);
end_ret:
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return ret;
}
/*
* wcd_cpe_cmd_shmem_alloc: compose and send lsm shared
* memory allocation command
* @core_handle: handle to cpe core
* @session: session for which the command needs to be sent
* @size: size of memory to be allocated
*/
static int wcd_cpe_cmd_lsm_shmem_alloc(void *core_handle,
struct cpe_lsm_session *session,
u32 size)
{
struct cpe_cmd_shmem_alloc cmd_shmem_alloc;
struct wcd_cpe_core *core = core_handle;
int ret = 0;
ret = wcd_cpe_is_valid_lsm_session(core, session,
__func__);
if (ret)
return ret;
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
memset(&cmd_shmem_alloc, 0, sizeof(cmd_shmem_alloc));
if (fill_lsm_cmd_header_v0_inband(&cmd_shmem_alloc.hdr, session->id,
SHMEM_ALLOC_CMD_PLD_SIZE,
CPE_LSM_SESSION_CMD_SHARED_MEM_ALLOC)) {
ret = -EINVAL;
goto end_ret;
}
cmd_shmem_alloc.size = size;
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &cmd_shmem_alloc);
if (ret)
dev_err(core->dev,
"%s: lsm_shmem_alloc cmd send fail, %d\n",
__func__, ret);
end_ret:
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return ret;
}
/*
* wcd_cpe_cmd_lsm_shmem_dealloc: deallocate the shared memory
* for the specified session
* @core_handle: handle to cpe core
* @session: session for which memory needs to be deallocated.
*/
static int wcd_cpe_cmd_lsm_shmem_dealloc(void *core_handle,
struct cpe_lsm_session *session)
{
struct cpe_cmd_shmem_dealloc cmd_dealloc;
struct wcd_cpe_core *core = core_handle;
int ret = 0;
ret = wcd_cpe_is_valid_lsm_session(core, session,
__func__);
if (ret)
return ret;
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
memset(&cmd_dealloc, 0, sizeof(cmd_dealloc));
if (fill_lsm_cmd_header_v0_inband(&cmd_dealloc.hdr, session->id,
SHMEM_DEALLOC_CMD_PLD_SIZE,
CPE_LSM_SESSION_CMD_SHARED_MEM_DEALLOC)) {
ret = -EINVAL;
goto end_ret;
}
cmd_dealloc.addr = session->lsm_mem_handle;
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &cmd_dealloc);
if (ret) {
dev_err(core->dev,
"%s: lsm_shmem_dealloc cmd failed, rc %d\n",
__func__, ret);
goto end_ret;
}
memset(&session->lsm_mem_handle, 0,
sizeof(session->lsm_mem_handle));
end_ret:
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return ret;
}
/*
* wcd_cpe_send_lsm_cal: send the calibration for lsm service
* from acdb to the cpe
* @core: handle to cpe core
* @session: session for which the calibration needs to be set.
*/
static int wcd_cpe_send_lsm_cal(
struct wcd_cpe_core *core,
struct cpe_lsm_session *session)
{
u8 *msg_pld;
struct cmi_hdr *hdr;
struct cal_block_data *lsm_cal = NULL;
void *inb_msg;
int rc = 0;
if (core->cal_data[WCD_CPE_LSM_CAL_LSM] == NULL) {
pr_err("%s: LSM cal not allocated!\n", __func__);
return -EINVAL;
}
mutex_lock(&core->cal_data[WCD_CPE_LSM_CAL_LSM]->lock);
lsm_cal = cal_utils_get_only_cal_block(
core->cal_data[WCD_CPE_LSM_CAL_LSM]);
if (!lsm_cal) {
pr_err("%s: failed to get lsm cal block\n", __func__);
rc = -EINVAL;
goto unlock_cal_mutex;
}
inb_msg = kzalloc(sizeof(struct cmi_hdr) + lsm_cal->cal_data.size,
GFP_KERNEL);
if (!inb_msg) {
pr_err("%s: no memory for lsm acdb cal\n",
__func__);
rc = -ENOMEM;
goto unlock_cal_mutex;
}
hdr = (struct cmi_hdr *) inb_msg;
rc = fill_lsm_cmd_header_v0_inband(hdr, session->id,
lsm_cal->cal_data.size,
CPE_LSM_SESSION_CMD_SET_PARAMS);
if (rc) {
pr_err("%s: invalid params for header, err = %d\n",
__func__, rc);
goto free_msg;
}
msg_pld = ((u8 *) inb_msg) + sizeof(struct cmi_hdr);
memcpy(msg_pld, lsm_cal->cal_data.kvaddr,
lsm_cal->cal_data.size);
rc = wcd_cpe_cmi_send_lsm_msg(core, session, inb_msg);
if (rc)
pr_err("%s: acdb lsm_params send failed, err = %d\n",
__func__, rc);
free_msg:
kfree(inb_msg);
unlock_cal_mutex:
mutex_unlock(&core->cal_data[WCD_CPE_LSM_CAL_LSM]->lock);
return rc;
}
/*
* wcd_cpe_lsm_set_params: set the parameters for lsm service
* @core: handle to cpe core
* @session: session for which the parameters are to be set
* @detect_mode: mode for detection
* @detect_failure: flag indicating failure detection enabled/disabled
*
*/
static int wcd_cpe_lsm_set_params(
struct wcd_cpe_core *core,
struct cpe_lsm_session *session,
enum lsm_detection_mode detect_mode,
bool detect_failure)
{
struct cpe_lsm_params lsm_params;
struct cpe_lsm_operation_mode *op_mode = &lsm_params.op_mode;
struct cpe_lsm_connect_to_port *connect_port =
&lsm_params.connect_port;
int ret = 0;
u8 pld_size = CPE_PARAM_PAYLOAD_SIZE;
ret = wcd_cpe_send_lsm_cal(core, session);
if (ret) {
pr_err("%s: fail to sent acdb cal, err = %d",
__func__, ret);
return ret;
}
memset(&lsm_params, 0, sizeof(lsm_params));
if (fill_lsm_cmd_header_v0_inband(&lsm_params.hdr,
session->id,
pld_size,
CPE_LSM_SESSION_CMD_SET_PARAMS)) {
ret = -EINVAL;
goto err_ret;
}
op_mode->param.module_id = CPE_LSM_MODULE_ID_VOICE_WAKEUP;
op_mode->param.param_id = CPE_LSM_PARAM_ID_OPERATION_MODE;
op_mode->param.param_size = PARAM_SIZE_LSM_OP_MODE;
op_mode->param.reserved = 0;
op_mode->minor_version = 1;
if (detect_mode == LSM_MODE_KEYWORD_ONLY_DETECTION)
op_mode->mode = 1;
else
op_mode->mode = 3;
if (detect_failure)
op_mode->mode |= 0x04;
op_mode->reserved = 0;
connect_port->param.module_id = CPE_LSM_MODULE_ID_VOICE_WAKEUP;
connect_port->param.param_id = CPE_LSM_PARAM_ID_CONNECT_TO_PORT;
connect_port->param.param_size = PARAM_SIZE_LSM_CONNECT_PORT;
connect_port->param.reserved = 0;
connect_port->minor_version = 1;
connect_port->afe_port_id = CPE_AFE_PORT_1_TX;
connect_port->reserved = 0;
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &lsm_params);
if (ret)
pr_err("%s: lsm_set_params failed, rc %dn",
__func__, ret);
err_ret:
return ret;
}
/*
* wcd_cpe_lsm_set_conf_levels: send the confidence levels for listen
* @core: handle to cpe core
* @session: session for which the confidence levels are to be set
*
* The actual confidence levels are part of the session.
*/
static int wcd_cpe_lsm_set_conf_levels(
struct wcd_cpe_core *core,
struct cpe_lsm_session *session)
{
struct cpe_lsm_conf_level conf_level_data;
struct cmi_hdr *hdr = &(conf_level_data.hdr);
struct cpe_param_data *param_d = &(conf_level_data.param);
u8 pld_size = 0;
u8 pad_bytes = 0;
void *message;
int ret = 0;
memset(&conf_level_data, 0, sizeof(conf_level_data));
pld_size = (sizeof(struct cpe_lsm_conf_level) - sizeof(struct cmi_hdr));
pld_size += session->num_confidence_levels;
pad_bytes = ((4 - (pld_size % 4)) % 4);
pld_size += pad_bytes;
fill_cmi_header(hdr, session->id, CMI_CPE_LSM_SERVICE_ID,
false, pld_size,
CPE_LSM_SESSION_CMD_SET_PARAMS, false);
param_d->module_id = CPE_LSM_MODULE_ID_VOICE_WAKEUP;
param_d->param_id = CPE_LSM_PARAM_ID_MIN_CONFIDENCE_LEVELS;
param_d->param_size = pld_size -
sizeof(struct cpe_param_data);
param_d->reserved = 0;
conf_level_data.num_active_models = session->num_confidence_levels;
message = kzalloc(sizeof(struct cpe_lsm_conf_level) +
conf_level_data.num_active_models + pad_bytes,
GFP_KERNEL);
if (!message) {
pr_err("%s: no memory for conf_level\n", __func__);
return -ENOMEM;
}
memcpy(message, &conf_level_data,
sizeof(struct cpe_lsm_conf_level));
memcpy(((u8 *) message) + sizeof(struct cpe_lsm_conf_level),
session->conf_levels, conf_level_data.num_active_models);
ret = wcd_cpe_cmi_send_lsm_msg(core, session, message);
if (ret)
pr_err("%s: lsm_set_conf_levels failed, err = %d\n",
__func__, ret);
kfree(message);
return ret;
}
int wcd_cpe_lsm_set_data(void *core_handle,
struct cpe_lsm_session *session,
enum lsm_detection_mode detect_mode,
bool detect_failure)
{
struct wcd_cpe_core *core = core_handle;
int ret = 0;
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
if (session->num_confidence_levels > 0) {
ret = wcd_cpe_lsm_set_params(core, session, detect_mode,
detect_failure);
if (ret) {
dev_err(core->dev,
"%s: lsm set params failed, rc = %d\n",
__func__, ret);
goto err_ret;
}
ret = wcd_cpe_lsm_set_conf_levels(core, session);
if (ret) {
dev_err(core->dev,
"%s: lsm confidence levels failed, rc = %d\n",
__func__, ret);
goto err_ret;
}
} else {
dev_dbg(core->dev,
"%s: no conf levels to set\n",
__func__);
}
err_ret:
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return ret;
}
EXPORT_SYMBOL(wcd_cpe_lsm_set_data);
/*
* wcd_cpe_lsm_reg_snd_model: register the sound model for listen
* @session: session for which to register the sound model
* @detect_mode: detection mode, user dependent/independent
* @detect_failure: flag to indicate if failure detection is enabled
*
* The memory required for sound model should be pre-allocated on CPE
* before this function is invoked.
*/
static int wcd_cpe_lsm_reg_snd_model(void *core_handle,
struct cpe_lsm_session *session,
enum lsm_detection_mode detect_mode,
bool detect_failure)
{
int ret = 0;
struct cmi_obm_msg obm_msg;
struct wcd_cpe_core *core = core_handle;
ret = wcd_cpe_is_valid_lsm_session(core, session,
__func__);
if (ret)
return ret;
ret = wcd_cpe_lsm_set_data(core_handle, session,
detect_mode, detect_failure);
if (ret) {
dev_err(core->dev,
"%s: fail to set lsm data, err = %d\n",
__func__, ret);
return ret;
}
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
ret = fill_cmi_header(&obm_msg.hdr, session->id,
CMI_CPE_LSM_SERVICE_ID, 0, 20,
CPE_LSM_SESSION_CMD_REGISTER_SOUND_MODEL, true);
if (ret) {
dev_err(core->dev,
"%s: Invalid parameters, rc = %d\n",
__func__, ret);
goto err_ret;
}
obm_msg.pld.version = 0;
obm_msg.pld.size = session->snd_model_size;
obm_msg.pld.data_ptr.kvaddr = session->snd_model_data;
obm_msg.pld.mem_handle = session->lsm_mem_handle;
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &obm_msg);
if (ret)
dev_err(core->dev,
"%s: snd_model_register failed, %d\n",
__func__, ret);
err_ret:
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return ret;
}
/*
* wcd_cpe_lsm_dereg_snd_model: deregister the sound model for listen
* @core_handle: handle to cpe core
* @session: session for which to deregister the sound model
*
*/
static int wcd_cpe_lsm_dereg_snd_model(void *core_handle,
struct cpe_lsm_session *session)
{
struct cmi_hdr cmd_dereg_snd_model;
struct wcd_cpe_core *core = core_handle;
int ret = 0;
ret = wcd_cpe_is_valid_lsm_session(core, session,
__func__);
if (ret)
return ret;
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
memset(&cmd_dereg_snd_model, 0, sizeof(cmd_dereg_snd_model));
if (fill_lsm_cmd_header_v0_inband(&cmd_dereg_snd_model, session->id,
0, CPE_LSM_SESSION_CMD_DEREGISTER_SOUND_MODEL)) {
ret = -EINVAL;
goto end_ret;
}
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &cmd_dereg_snd_model);
if (ret)
dev_err(core->dev,
"%s: failed to send dereg_snd_model cmd\n",
__func__);
end_ret:
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return ret;
}
/*
* wcd_cpe_cmd_lsm_start: send the start command to lsm
* @core_handle: handle to the CPE core
* @session: session for which start command to be sent
*
*/
static int wcd_cpe_cmd_lsm_start(void *core_handle,
struct cpe_lsm_session *session)
{
struct cmi_hdr cmd_lsm_start;
struct wcd_cpe_core *core = core_handle;
int ret = 0;
ret = wcd_cpe_is_valid_lsm_session(core, session,
__func__);
if (ret)
return ret;
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
memset(&cmd_lsm_start, 0, sizeof(struct cmi_hdr));
if (fill_lsm_cmd_header_v0_inband(&cmd_lsm_start, session->id, 0,
CPE_LSM_SESSION_CMD_START)) {
ret = -EINVAL;
goto end_ret;
}
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &cmd_lsm_start);
if (ret)
dev_err(core->dev, "failed to send lsm_start cmd\n");
end_ret:
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return ret;
}
/*
* wcd_cpe_cmd_lsm_stop: send the stop command for LSM service
* @core_handle: handle to the cpe core
* @session: session for which stop command to be sent
*
*/
static int wcd_cpe_cmd_lsm_stop(void *core_handle,
struct cpe_lsm_session *session)
{
struct cmi_hdr cmd_lsm_stop;
struct wcd_cpe_core *core = core_handle;
int ret = 0;
ret = wcd_cpe_is_valid_lsm_session(core, session,
__func__);
if (ret)
return ret;
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
memset(&cmd_lsm_stop, 0, sizeof(struct cmi_hdr));
if (fill_lsm_cmd_header_v0_inband(&cmd_lsm_stop, session->id, 0,
CPE_LSM_SESSION_CMD_STOP)) {
ret = -EINVAL;
goto end_ret;
}
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &cmd_lsm_stop);
if (ret)
dev_err(core->dev,
"%s: failed to send lsm_stop cmd\n",
__func__);
end_ret:
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return ret;
}
/*
* wcd_cpe_alloc_lsm_session: allocate a lsm session
* @core: handle to wcd_cpe_core
* @lsm_priv_d: lsm private data
*/
static struct cpe_lsm_session *wcd_cpe_alloc_lsm_session(
void *core_handle, void *lsm_priv_d,
void (*event_cb) (void *, u8, u8, u8 *))
{
struct cpe_lsm_session *session;
int i, session_id = -1;
struct wcd_cpe_core *core = core_handle;
bool afe_register_service = false;
int ret = 0;
/*
* Even if multiple listen sessions can be
* allocated, the AFE service registration
* should be done only once as CPE can only
* have one instance of AFE service.
*
* If this is the first session to be allocated,
* only then register the afe service.
*/
if (!wcd_cpe_lsm_session_active())
afe_register_service = true;
for (i = 1; i <= WCD_CPE_LSM_MAX_SESSIONS; i++)
if (!lsm_sessions[i])
session_id = i;
if (session_id < 0) {
dev_err(core->dev,
"%s: max allowed sessions already allocated\n",
__func__);
return NULL;
}
ret = wcd_cpe_vote(core, true);
if (ret) {
dev_err(core->dev,
"%s: Failed to enable cpe, err = %d\n",
__func__, ret);
return NULL;
}
session = kzalloc(sizeof(struct cpe_lsm_session), GFP_KERNEL);
if (!session) {
dev_err(core->dev,
"%s: failed to allocate session, no memory\n",
__func__);
goto err_session_alloc;
}
session->id = session_id;
session->event_cb = event_cb;
session->cmi_reg_handle = cmi_register(wcd_cpe_cmi_lsm_callback,
CMI_CPE_LSM_SERVICE_ID);
if (!session->cmi_reg_handle) {
dev_err(core->dev,
"%s: Failed to register LSM service with CMI\n",
__func__);
goto err_ret;
}
session->priv_d = lsm_priv_d;
mutex_init(&session->lsm_lock);
if (afe_register_service) {
/* Register for AFE Service */
core->cmi_afe_handle = cmi_register(wcd_cpe_cmi_afe_cb,
CMI_CPE_AFE_SERVICE_ID);
wcd_cpe_initialize_afe_port_data();
if (!core->cmi_afe_handle) {
dev_err(core->dev,
"%s: Failed to register AFE service with CMI\n",
__func__);
goto err_afe_svc_reg;
}
}
session->lsm_mem_handle = 0;
init_completion(&session->cmd_comp);
lsm_sessions[session_id] = session;
return session;
err_afe_svc_reg:
cmi_deregister(session->cmi_reg_handle);
mutex_destroy(&session->lsm_lock);
err_ret:
kfree(session);
err_session_alloc:
wcd_cpe_vote(core, false);
return NULL;
}
/*
* wcd_cpe_lsm_config_lab_latency: send lab latency value
* @core: handle to wcd_cpe_core
* @session: lsm session
* @latency: the value of latency for lab setup in msec
*/
static int wcd_cpe_lsm_config_lab_latency(
struct wcd_cpe_core *core,
struct cpe_lsm_session *session,
u32 latency)
{
int ret = 0, pld_size = CPE_PARAM_LSM_LAB_LATENCY_SIZE;
struct cpe_lsm_lab_latency_config cpe_lab_latency;
struct cpe_lsm_lab_config *lab_lat = &cpe_lab_latency.latency_cfg;
if (fill_lsm_cmd_header_v0_inband(&cpe_lab_latency.hdr, session->id,
(u8) pld_size, CPE_LSM_SESSION_CMD_SET_PARAMS)) {
pr_err("%s: Failed to create header\n", __func__);
return -EINVAL;
}
if (latency == 0x00 || latency > WCD_CPE_LAB_MAX_LATENCY) {
pr_err("%s: Invalid latency %u\n",
__func__, latency);
return -EINVAL;
} else {
lab_lat->latency = latency;
}
lab_lat->minor_ver = 1;
lab_lat->param.module_id = CPE_LSM_MODULE_ID_LAB;
lab_lat->param.param_id = CPE_LSM_PARAM_ID_LAB_CONFIG;
lab_lat->param.param_size = PARAM_SIZE_LSM_LATENCY_SIZE;
lab_lat->param.reserved = 0;
pr_debug("%s: Module 0x%x Param 0x%x size 0x%x pld_size 0x%x\n",
__func__, lab_lat->param.module_id,
lab_lat->param.param_id, lab_lat->param.param_size,
pld_size);
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &cpe_lab_latency);
if (ret != 0) {
pr_err("%s: lsm_set_params failed, error = %d\n",
__func__, ret);
return -EINVAL;
}
return 0;
}
/*
* wcd_cpe_buf_alloc: allocate lab DMA buffer.
* @core: handle to wcd_cpe_core
* @session: lsm session to be deallocated
*/
static int wcd_cpe_buf_alloc(void *core_handle,
struct cpe_lsm_session *session,
u32 bufsz, u32 bufcnt)
{
int rc = 0;
int dma_alloc = 0;
u32 count = 0;
struct wcd_cpe_data_pcm_buf *pcm_buf = NULL;
struct wcd_cpe_core *core = (struct wcd_cpe_core *)core_handle;
struct wcd_cpe_lsm_lab *lab = NULL;
struct snd_soc_codec *codec;
struct wcd9xxx *wcd9xxx;
pr_debug("%s:Buf Size %d Buf count %d\n", __func__,
bufsz, bufcnt);
if (bufcnt <= 0 || bufsz <= 0) {
pr_err("%s:HW Params Error for LAB\n", __func__);
rc = -EINVAL;
goto exit;
}
if (core == NULL || session == NULL) {
pr_err("%s:Err core handle/Session ptr NULL\n", __func__);
rc = -EINVAL;
goto exit;
}
codec = core->codec;
wcd9xxx = codec->control_data;
if (session)
lab = &session->lab;
else {
pr_err("%s: Session ptr NULL\n", __func__);
rc = -EINVAL;
goto exit;
}
pcm_buf = kzalloc(((sizeof(struct wcd_cpe_data_pcm_buf)) * bufcnt),
GFP_KERNEL);
if (!pcm_buf) {
pr_err("%s: No memory for pcm_buf\n", __func__);
rc = -ENOMEM;
goto exit;
}
lab->pcm_buf = pcm_buf;
dma_alloc = bufsz * bufcnt;
pcm_buf->mem = NULL;
pcm_buf->mem = dma_alloc_coherent(wcd9xxx->slim->dev.parent,
dma_alloc,
&(pcm_buf->phys),
GFP_KERNEL);
if (pcm_buf->mem == NULL) {
pr_err("%s:DMA alloc failed size = %x\n",
__func__, dma_alloc);
rc = -ENOMEM;
goto fail;
}
count = 0;
while (count < bufcnt) {
pcm_buf[count].mem = pcm_buf[0].mem + (count * bufsz);
pcm_buf[count].phys = pcm_buf[0].phys + (count * bufsz);
if (!pcm_buf[count].mem) {
pr_err("%s: pcm buf mem Null\n", __func__);
rc = -EINVAL;
goto fail;
}
pr_debug("%s: pcm_buf[%d].mem %p pcm_buf[%d].phys %pa\n",
__func__, count,
(void *)pcm_buf[count].mem,
count, &(pcm_buf[count].phys));
count++;
}
return 0;
fail:
if (pcm_buf) {
if (pcm_buf->mem)
dma_free_coherent(wcd9xxx->slim->dev.parent, dma_alloc,
pcm_buf->mem, pcm_buf->phys);
kfree(pcm_buf);
}
exit:
return rc;
}
/*
* wcd_cpe_buf_dealloc: deallocate DMA buffers
* @core: handle to wcd_cpe_core
* @session: lsm session
* @bufz: buffer size
* @bufCnt: no of period or buffers
*/
static int wcd_cpe_buf_dealloc(void *core_handle,
struct cpe_lsm_session *session,
u32 bufsz, u32 bufcnt)
{
int rc = 0;
int dma_alloc = 0;
struct wcd_cpe_data_pcm_buf *pcm_buf = NULL;
struct wcd_cpe_core *core = (struct wcd_cpe_core *)core_handle;
struct wcd_cpe_lsm_lab *lab = NULL;
struct snd_soc_codec *codec;
struct wcd9xxx *wcd9xxx;
pr_debug("%s:Buf Size %d Buf count %d\n", __func__,
bufsz, bufcnt);
if (bufcnt <= 0 || bufsz <= 0) {
pr_err("%s:HW Params Error for LAB\n", __func__);
return -EINVAL;
}
if (core == NULL || session == NULL) {
pr_err("%s:Err core handle/Session ptr NULL\n", __func__);
rc = -ENOMEM;
return rc;
}
codec = core->codec;
wcd9xxx = codec->control_data;
if (session)
lab = &session->lab;
else {
pr_err("%s: Session ptr NULL\n", __func__);
rc = -EINVAL;
return rc;
}
pcm_buf = lab->pcm_buf;
dma_alloc = bufsz * bufcnt;
if (pcm_buf)
dma_free_coherent(wcd9xxx->slim->dev.parent, dma_alloc,
pcm_buf->mem, pcm_buf->phys);
kfree(pcm_buf);
lab->pcm_buf = NULL;
return rc;
}
/*
* wcd_cpe_lsm_lab_enable_disable: enable/disable lab
* @core: handle to wcd_cpe_core
* @session: lsm session
*/
static int wcd_cpe_lsm_lab_enable_disable(
struct wcd_cpe_core *core,
struct cpe_lsm_session *session,
bool enable)
{
int ret = 0, pld_size = CPE_PARAM_SIZE_LSM_LAB_CONTROL;
struct cpe_lsm_control_lab cpe_lab_enable;
struct cpe_lsm_lab_enable *lab_enable = &cpe_lab_enable.lab_enable;
pr_debug("%s: enter payload_size = %d Enable %d\n",
__func__, pld_size, enable);
if (fill_lsm_cmd_header_v0_inband(&cpe_lab_enable.hdr, session->id,
(u8) pld_size, CPE_LSM_SESSION_CMD_SET_PARAMS)) {
return -EINVAL;
}
if (enable == true)
lab_enable->enable = 1;
else
lab_enable->enable = 0;
lab_enable->param.module_id = CPE_LSM_MODULE_ID_LAB;
lab_enable->param.param_id = CPE_LSM_PARAM_ID_LAB_ENABLE;
lab_enable->param.param_size = PARAM_SIZE_LSM_CONTROL_SIZE;
lab_enable->param.reserved = 0;
pr_debug("%s: Module 0x%x, Param 0x%x size 0x%x pld_size 0x%x\n",
__func__, lab_enable->param.module_id,
lab_enable->param.param_id, lab_enable->param.param_size,
pld_size);
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &cpe_lab_enable);
if (ret != 0) {
pr_err("%s: lsm_set_params failed, error = %d\n",
__func__, ret);
return -EINVAL;
}
if (lab_enable->enable)
wcd_cpe_lsm_config_lab_latency(core, session,
WCD_CPE_LAB_MAX_LATENCY);
return 0;
}
static int wcd_cpe_lsm_control_lab(void *core_handle,
struct cpe_lsm_session *session,
u32 bufsz, u32 bufcnt, bool enable)
{
int rc = 0;
struct wcd_cpe_core *core = (struct wcd_cpe_core *)core_handle;
if (enable) {
rc = wcd_cpe_buf_alloc(core_handle, session, bufsz, bufcnt);
if (rc) {
pr_err("%s: DMA buffer allocation failed rc %d\n",
__func__, rc);
return rc;
}
rc = wcd_cpe_lsm_lab_enable_disable(core, session, enable);
if (rc) {
pr_err("%s: LAB disable/ Enable failed rc %d\n",
__func__, rc);
return rc;
}
session->lab.core_handle = core_handle;
session->lab.lsm_s = session;
} else {
rc = wcd_cpe_buf_dealloc(core_handle, session, bufsz, bufcnt);
/* do not return error for DMA dealloc put
* session in detection mode
*/
if (rc) {
pr_err("%s: DMA buffer De-allocation failed, rc %d\n",
__func__, rc);
}
rc = wcd_cpe_lsm_lab_enable_disable(core, session, enable);
if (rc) {
pr_err("%s: LAB disable/ Enable failed rc %d\n",
__func__, rc);
return rc;
}
session->lab.lab_enable = false;
}
return rc;
}
/*
* wcd_cpe_lsm_eob: stop lab
* @core: handle to wcd_cpe_core
* @session: lsm session to be deallocated
*/
static int wcd_cpe_lsm_eob(
struct wcd_cpe_core *core,
struct cpe_lsm_session *session)
{
int ret = 0;
struct cmi_hdr lab_eob;
if (fill_lsm_cmd_header_v0_inband(&lab_eob, session->id,
0, CPE_LSM_SESSION_CMD_EOB)) {
return -EINVAL;
}
ret = wcd_cpe_cmi_send_lsm_msg(core, session, &lab_eob);
if (ret != 0) {
pr_err("%s: lsm_set_params failed\n", __func__);
return -EINVAL;
}
return 0;
}
/*
* wcd_cpe_dealloc_lsm_session: deallocate lsm session
* @core: handle to wcd_cpe_core
* @session: lsm session to be deallocated
*/
static int wcd_cpe_dealloc_lsm_session(void *core_handle,
struct cpe_lsm_session *session)
{
struct wcd_cpe_core *core = core_handle;
int ret = 0;
if (!session) {
dev_err(core->dev,
"%s: Invalid lsm session\n", __func__);
return -EINVAL;
}
dev_dbg(core->dev, "%s: session %d being deallocated\n",
__func__, session->id);
if (session->id > WCD_CPE_LSM_MAX_SESSIONS) {
dev_err(core->dev,
"%s: Wrong session id %d max allowed = %d\n",
__func__, session->id,
WCD_CPE_LSM_MAX_SESSIONS);
return -EINVAL;
}
cmi_deregister(session->cmi_reg_handle);
mutex_destroy(&session->lsm_lock);
lsm_sessions[session->id] = NULL;
kfree(session);
ret = wcd_cpe_vote(core, false);
if (ret)
dev_dbg(core->dev,
"%s: Failed to un-vote cpe, err = %d\n",
__func__, ret);
if (!wcd_cpe_lsm_session_active()) {
cmi_deregister(core->cmi_afe_handle);
core->cmi_afe_handle = NULL;
wcd_cpe_deinitialize_afe_port_data();
}
return ret;
}
static int slim_master_read_enable(void *core_handle,
struct cpe_lsm_session *session)
{
int rc = 0;
struct wcd_cpe_lsm_lab *lab_s = NULL;
struct wcd_cpe_core *core = (struct wcd_cpe_core *)core_handle;
struct snd_soc_codec *codec;
struct wcd9xxx *wcd9xxx;
struct wcd_cpe_lab_hw_params *lsm_params;
codec = core->codec;
wcd9xxx = codec->control_data;
lab_s = &session->lab;
lsm_params = &lab_s->hw_params;
/* The sequence should be maintained strictly */
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
if (core->cpe_cdc_cb->cdc_ext_clk)
core->cpe_cdc_cb->cdc_ext_clk(codec, true, false);
else {
pr_err("%s: Invalid callback for codec ext clk\n",
__func__);
rc = -EINVAL;
goto exit;
}
if (core->cpe_cdc_cb->lab_cdc_ch_ctl)
core->cpe_cdc_cb->lab_cdc_ch_ctl(codec, 1);
else {
pr_err("%s: Failed to enable codec slave port\n",
__func__);
rc = -EINVAL;
goto fail_mclk;
}
lab_s->slim_handle = NULL;
rc = wcd9xxx_slim_ch_master_open(wcd9xxx, lsm_params->sample_rate,
lsm_params->sample_size,
&lab_s->slim_handle,
WCD_CPE_MAD_SLIM_CHANNEL);
if (rc || lab_s->slim_handle == NULL) {
pr_err("%s: Slim Open rc %d\n",
__func__, rc);
rc = -EINVAL;
goto fail_slim_open;
}
rc = wcd9xxx_slim_ch_master_enable_read(wcd9xxx, lab_s->slim_handle);
if (rc) {
pr_err("%s: Slim enable read rc %d\n",
__func__, rc);
rc = -EINVAL;
goto fail_slim_open;
}
rc = cpe_svc_toggle_lab(core->cpe_handle, true);
if (rc) {
pr_err("%s: SVC toggle codec LAB Enable error\n", __func__);
rc = -EINVAL;
goto fail_slim_open;
}
init_waitqueue_head(&lab_s->period_wait);
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return 0;
fail_slim_open:
core->cpe_cdc_cb->lab_cdc_ch_ctl(codec, 0);
fail_mclk:
core->cpe_cdc_cb->cdc_ext_clk(codec, false, false);
exit:
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return rc;
}
int slim_master_read_status(void *core_handle,
struct cpe_lsm_session *session,
phys_addr_t phys, u32 *len)
{
struct wcd_cpe_core *core = (struct wcd_cpe_core *)core_handle;
struct snd_soc_codec *codec;
struct wcd9xxx *wcd9xxx;
struct wcd_cpe_lsm_lab *lab = &session->lab;
int rc = 0;
codec = core->codec;
wcd9xxx = codec->control_data;
rc = wcd9xxx_slim_ch_master_status(wcd9xxx, lab->slim_handle,
phys, len);
return rc;
}
int slim_master_read(void *core_handle,
struct cpe_lsm_session *session,
phys_addr_t phys, u8 *mem,
u32 read_len)
{
struct wcd_cpe_core *core = (struct wcd_cpe_core *)core_handle;
struct snd_soc_codec *codec;
struct wcd9xxx *wcd9xxx;
struct wcd_cpe_lsm_lab *lab = &session->lab;
int rc = 0;
codec = core->codec;
wcd9xxx = codec->control_data;
rc = wcd9xxx_slim_ch_master_read(wcd9xxx, lab->slim_handle,
phys, mem, read_len);
return rc;
}
static int wcd_cpe_lsm_stop_lab(void *core_handle,
struct cpe_lsm_session *session)
{
struct wcd_cpe_lsm_lab *lab_s = NULL;
struct wcd_cpe_core *core = (struct wcd_cpe_core *)core_handle;
struct snd_soc_codec *codec;
struct wcd9xxx *wcd9xxx;
int rc = 0;
codec = core->codec;
wcd9xxx = codec->control_data;
lab_s = &session->lab;
WCD_CPE_GRAB_LOCK(&session->lsm_lock, "lsm");
/* This seqeunce should be followed strictly for closing sequence */
if (core->cpe_cdc_cb->lab_cdc_ch_ctl)
core->cpe_cdc_cb->lab_cdc_ch_ctl(codec, 0);
else
pr_err("%s: Failed to disable codec slave port\n",
__func__);
rc = wcd9xxx_slim_ch_master_close(wcd9xxx, &lab_s->slim_handle);
if (rc != 0)
pr_err("%s: wcd9xxx_slim_pcm_close rc %d\n",
__func__, rc);
rc = wcd_cpe_lsm_eob(core, session);
if (rc != 0)
dev_err(core->dev,
"%s: wcd_cpe_lsm_eob failed, rc %d\n",
__func__, rc);
rc = cpe_svc_toggle_lab(core->cpe_handle, false);
if (rc)
dev_err(core->dev,
"%s: LAB Voice Tx codec error, rc %d\n",
__func__, rc);
lab_s->buf_idx = 0;
lab_s->thread_status = MSM_LSM_LAB_THREAD_STOP;
atomic_set(&lab_s->in_count, 0);
lab_s->dma_write = 0;
if (core->cpe_cdc_cb->cdc_ext_clk)
core->cpe_cdc_cb->cdc_ext_clk(codec, false, false);
else
pr_err("%s: Failed to disable cdc ext clk\n",
__func__);
WCD_CPE_REL_LOCK(&session->lsm_lock, "lsm");
return rc;
}
/*
* wcd_cpe_get_lsm_ops: register lsm driver to codec
* @lsm_ops: structure with lsm callbacks
* @codec: codec to which this lsm driver is registered to
*/
int wcd_cpe_get_lsm_ops(struct wcd_cpe_lsm_ops *lsm_ops)
{
lsm_ops->lsm_alloc_session = wcd_cpe_alloc_lsm_session;
lsm_ops->lsm_dealloc_session = wcd_cpe_dealloc_lsm_session;
lsm_ops->lsm_open_tx = wcd_cpe_cmd_lsm_open_tx;
lsm_ops->lsm_close_tx = wcd_cpe_cmd_lsm_close_tx;
lsm_ops->lsm_shmem_alloc = wcd_cpe_cmd_lsm_shmem_alloc;
lsm_ops->lsm_shmem_dealloc = wcd_cpe_cmd_lsm_shmem_dealloc;
lsm_ops->lsm_register_snd_model = wcd_cpe_lsm_reg_snd_model;
lsm_ops->lsm_deregister_snd_model = wcd_cpe_lsm_dereg_snd_model;
lsm_ops->lsm_start = wcd_cpe_cmd_lsm_start;
lsm_ops->lsm_stop = wcd_cpe_cmd_lsm_stop;
lsm_ops->lsm_lab_control = wcd_cpe_lsm_control_lab;
lsm_ops->lsm_lab_stop = wcd_cpe_lsm_stop_lab;
lsm_ops->lsm_lab_data_channel_read = slim_master_read;
lsm_ops->lsm_lab_data_channel_read_status = slim_master_read_status;
lsm_ops->lsm_lab_data_channel_open = slim_master_read_enable;
lsm_ops->lsm_set_data = wcd_cpe_lsm_set_data;
return 0;
}
EXPORT_SYMBOL(wcd_cpe_get_lsm_ops);
static int fill_afe_cmd_header(struct cmi_hdr *hdr, u8 port_id,
u16 opcode, u8 pld_size,
bool obm_flag)
{
CMI_HDR_SET_SESSION(hdr, port_id);
CMI_HDR_SET_SERVICE(hdr, CMI_CPE_AFE_SERVICE_ID);
CMI_HDR_SET_PAYLOAD_SIZE(hdr, pld_size);
hdr->opcode = opcode;
if (obm_flag)
CMI_HDR_SET_OBM(hdr, CMI_OBM_FLAG_OUT_BAND);
else
CMI_HDR_SET_OBM(hdr, CMI_OBM_FLAG_IN_BAND);
return 0;
}
/*
* wcd_cpe_cmi_send_afe_msg: send message to AFE service
* @core: wcd cpe core handle
* @port_cfg: configuration data for the afe port
* for which this message is to be sent
* @message: actual message with header and payload
*
* Port specific lock needs to be acquired before this
* function can be invoked
*/
static int wcd_cpe_cmi_send_afe_msg(
struct wcd_cpe_core *core,
struct wcd_cmi_afe_port_data *port_d,
void *message)
{
int ret = 0;
struct cmi_hdr *hdr = message;
pr_debug("%s: sending message with opcode 0x%x\n",
__func__, hdr->opcode);
if (unlikely(!wcd_cpe_is_online_state(core))) {
dev_err(core->dev, "%s: CPE offline\n", __func__);
return 0;
}
ret = cmi_send_msg(message);
if (ret) {
pr_err("%s: cmd 0x%x send failed, err = %d\n",
__func__, hdr->opcode, ret);
return ret;
}
ret = wait_for_completion_timeout(&port_d->afe_cmd_complete,
CMI_CMD_TIMEOUT);
if (ret > 0) {
pr_debug("%s: command 0x%x, received response 0x%x\n",
__func__, hdr->opcode, port_d->cmd_result);
return port_d->cmd_result;
} else {
pr_err("%s: command 0x%x send timed out\n",
__func__, hdr->opcode);
return -ETIMEDOUT;
}
INIT_COMPLETION(port_d->afe_cmd_complete);
return ret;
}
/*
* wcd_cpe_afe_shmem_alloc: allocate the cpe memory for afe service
* @core: handle to cpe core
* @port_cfg: configuration data for the port which needs
* memory to be allocated on CPE
* @size: size of the memory to be allocated
*/
static int wcd_cpe_afe_shmem_alloc(
struct wcd_cpe_core *core,
struct wcd_cmi_afe_port_data *port_d,
u32 size)
{
struct cpe_cmd_shmem_alloc cmd_shmem_alloc;
int ret = 0;
pr_debug("%s: enter: size = %d\n", __func__, size);
memset(&cmd_shmem_alloc, 0, sizeof(cmd_shmem_alloc));
if (fill_afe_cmd_header(&cmd_shmem_alloc.hdr, port_d->port_id,
CPE_AFE_PORT_CMD_SHARED_MEM_ALLOC,
SHMEM_ALLOC_CMD_PLD_SIZE, false)) {
ret = -EINVAL;
goto end_ret;
}
cmd_shmem_alloc.size = size;
ret = wcd_cpe_cmi_send_afe_msg(core, port_d, &cmd_shmem_alloc);
if (ret) {
pr_err("%s: afe_shmem_alloc fail,ret = %d\n",
__func__, ret);
goto end_ret;
}
pr_debug("%s: completed %s, mem_handle = 0x%x\n",
__func__, "CPE_AFE_CMD_SHARED_MEM_ALLOC",
port_d->mem_handle);
end_ret:
return ret;
}
/*
* wcd_cpe_afe_shmem_dealloc: deallocate the cpe memory for
* afe service
* @core: handle to cpe core
* @port_d: configuration data for the port which needs
* memory to be deallocated on CPE
* The memory handle to be de-allocated is saved in the
* port configuration data
*/
static int wcd_cpe_afe_shmem_dealloc(
struct wcd_cpe_core *core,
struct wcd_cmi_afe_port_data *port_d)
{
struct cpe_cmd_shmem_dealloc cmd_dealloc;
int ret = 0;
pr_debug("%s: enter, port_id = %d\n",
__func__, port_d->port_id);
memset(&cmd_dealloc, 0, sizeof(cmd_dealloc));
if (fill_afe_cmd_header(&cmd_dealloc.hdr, port_d->port_id,
CPE_AFE_PORT_CMD_SHARED_MEM_DEALLOC,
SHMEM_DEALLOC_CMD_PLD_SIZE, false)) {
ret = -EINVAL;
goto end_ret;
}
cmd_dealloc.addr = port_d->mem_handle;
ret = wcd_cpe_cmi_send_afe_msg(core, port_d, &cmd_dealloc);
if (ret) {
pr_err("failed to send shmem_dealloc cmd\n");
goto end_ret;
}
memset(&port_d->mem_handle, 0,
sizeof(port_d->mem_handle));
end_ret:
return ret;
}
/*
* wcd_cpe_send_afe_cal: send the acdb calibration to AFE port
* @core: handle to cpe core
* @port_d: configuration data for the port for which the
* calibration needs to be appplied
*/
static int wcd_cpe_send_afe_cal(void *core_handle,
struct wcd_cmi_afe_port_data *port_d)
{
struct cal_block_data *afe_cal = NULL;
struct wcd_cpe_core *core = core_handle;
struct cmi_obm_msg obm_msg;
void *inb_msg = NULL;
void *msg;
int rc = 0;
bool is_obm_msg;
if (core->cal_data[WCD_CPE_LSM_CAL_AFE] == NULL) {
pr_err("%s: LSM cal not allocated!\n",
__func__);
rc = -EINVAL;
goto rel_cal_mutex;
}
mutex_lock(&core->cal_data[WCD_CPE_LSM_CAL_AFE]->lock);
afe_cal = cal_utils_get_only_cal_block(
core->cal_data[WCD_CPE_LSM_CAL_AFE]);
if (!afe_cal) {
pr_err("%s: failed to get afe cal block\n",
__func__);
rc = -EINVAL;
goto rel_cal_mutex;
}
is_obm_msg = (afe_cal->cal_data.size >
CMI_INBAND_MESSAGE_SIZE) ? true : false;
if (is_obm_msg) {
struct cmi_hdr *hdr = &(obm_msg.hdr);
struct cmi_obm *pld = &(obm_msg.pld);
rc = wcd_cpe_afe_shmem_alloc(core, port_d,
afe_cal->cal_data.size);
if (rc) {
dev_err(core->dev,
"%s: AFE shmem alloc fail %d\n",
__func__, rc);
goto rel_cal_mutex;
}
rc = fill_cmi_header(hdr, port_d->port_id,
CMI_CPE_AFE_SERVICE_ID,
0, 20, CPE_AFE_CMD_SET_PARAM,
true);
if (rc) {
dev_err(core->dev,
"%s: invalid params for header, err = %d\n",
__func__, rc);
wcd_cpe_afe_shmem_dealloc(core, port_d);
goto rel_cal_mutex;
}
pld->version = 0;
pld->size = afe_cal->cal_data.size;
pld->data_ptr.kvaddr = afe_cal->cal_data.kvaddr;
pld->mem_handle = port_d->mem_handle;
msg = &obm_msg;
} else {
u8 *msg_pld;
struct cmi_hdr *hdr;
inb_msg = kzalloc(sizeof(struct cmi_hdr) +
afe_cal->cal_data.size,
GFP_KERNEL);
if (!inb_msg) {
dev_err(core->dev,
"%s: no memory for afe cal inband\n",
__func__);
rc = -ENOMEM;
goto rel_cal_mutex;
}
hdr = (struct cmi_hdr *) inb_msg;
rc = fill_cmi_header(hdr, port_d->port_id,
CMI_CPE_AFE_SERVICE_ID,
0, afe_cal->cal_data.size,
CPE_AFE_CMD_SET_PARAM, false);
if (rc) {
dev_err(core->dev,
"%s: invalid params for header, err = %d\n",
__func__, rc);
kfree(inb_msg);
inb_msg = NULL;
goto rel_cal_mutex;
}
msg_pld = ((u8 *) inb_msg) + sizeof(struct cmi_hdr);
memcpy(msg_pld, afe_cal->cal_data.kvaddr,
afe_cal->cal_data.size);
msg = inb_msg;
}
rc = wcd_cpe_cmi_send_afe_msg(core, port_d, msg);
if (rc)
pr_err("%s: afe cal for listen failed, rc = %d\n",
__func__, rc);
if (is_obm_msg) {
wcd_cpe_afe_shmem_dealloc(core, port_d);
port_d->mem_handle = 0;
} else {
kfree(inb_msg);
inb_msg = NULL;
}
rel_cal_mutex:
mutex_unlock(&core->cal_data[WCD_CPE_LSM_CAL_AFE]->lock);
return rc;
}
/*
* wcd_cpe_is_valid_port: check validity of afe port id
* @core: handle to core to check for validity
* @afe_cfg: client provided afe configuration
* @func: function name invoking this validity check,
* used for logging purpose only.
*/
static int wcd_cpe_is_valid_port(struct wcd_cpe_core *core,
struct wcd_cpe_afe_port_cfg *afe_cfg,
const char *func)
{
if (unlikely(IS_ERR_OR_NULL(core))) {
pr_err("%s: Invalid core handle\n", func);
return -EINVAL;
}
if (afe_cfg->port_id > WCD_CPE_AFE_MAX_PORTS) {
dev_err(core->dev,
"%s: invalid afe port (%u)\n",
func, afe_cfg->port_id);
return -EINVAL;
}
dev_dbg(core->dev,
"%s: port_id = %u\n",
func, afe_cfg->port_id);
return 0;
}
/*
* wcd_cpe_afe_set_params: set the parameters for afe port
* @afe_cfg: configuration data for the port for which the
* parameters are to be set
*/
static int wcd_cpe_afe_set_params(void *core_handle,
struct wcd_cpe_afe_port_cfg *afe_cfg)
{
struct cpe_afe_params afe_params;
struct cpe_afe_hw_mad_ctrl *hw_mad_ctrl = &afe_params.hw_mad_ctrl;
struct cpe_afe_port_cfg *port_cfg = &afe_params.port_cfg;
struct wcd_cpe_core *core = core_handle;
struct wcd_cmi_afe_port_data *afe_port_d;
int ret = 0, pld_size = 0;
ret = wcd_cpe_is_valid_port(core, afe_cfg, __func__);
if (ret)
return ret;
afe_port_d = &afe_ports[afe_cfg->port_id];
afe_port_d->port_id = afe_cfg->port_id;
WCD_CPE_GRAB_LOCK(&afe_port_d->afe_lock, "afe");
ret = wcd_cpe_send_afe_cal(core, afe_port_d);
if (ret) {
dev_err(core->dev,
"%s: afe acdb cal send failed, err = %d\n",
__func__, ret);
goto err_ret;
}
pld_size = CPE_AFE_PARAM_PAYLOAD_SIZE;
memset(&afe_params, 0, sizeof(afe_params));
if (fill_afe_cmd_header(&afe_params.hdr,
afe_cfg->port_id,
CPE_AFE_CMD_SET_PARAM,
(u8) pld_size, false)) {
ret = -EINVAL;
goto err_ret;
}
hw_mad_ctrl->param.module_id = CPE_AFE_MODULE_HW_MAD;
hw_mad_ctrl->param.param_id = CPE_AFE_PARAM_ID_HW_MAD_CTL;
hw_mad_ctrl->param.param_size = PARAM_SIZE_AFE_HW_MAD_CTRL;
hw_mad_ctrl->param.reserved = 0;
hw_mad_ctrl->minor_version = 1;
hw_mad_ctrl->mad_type = MAD_TYPE_AUDIO;
hw_mad_ctrl->mad_enable = 1;
port_cfg->param.module_id = CPE_AFE_MODULE_AUDIO_DEV_INTERFACE;
port_cfg->param.param_id = CPE_AFE_PARAM_ID_GENERIC_PORT_CONFIG;
port_cfg->param.param_size = PARAM_SIZE_AFE_PORT_CFG;
port_cfg->param.reserved = 0;
port_cfg->minor_version = 1;
port_cfg->bit_width = afe_cfg->bit_width;
port_cfg->num_channels = afe_cfg->num_channels;
port_cfg->sample_rate = afe_cfg->sample_rate;
ret = wcd_cpe_cmi_send_afe_msg(core, afe_port_d, &afe_params);
if (ret)
dev_err(core->dev,
"%s: afe_port_config failed, err = %d\n",
__func__, ret);
err_ret:
WCD_CPE_REL_LOCK(&afe_port_d->afe_lock, "afe");
return ret;
}
/*
* wcd_cpe_afe_port_start: send the start command to afe service
* @core_handle: handle to the cpe core
* @port_cfg: configuration data for the afe port which needs
* to be started.
*/
static int wcd_cpe_afe_port_start(void *core_handle,
struct wcd_cpe_afe_port_cfg *port_cfg)
{
struct cmi_hdr hdr;
struct wcd_cpe_core *core = core_handle;
struct wcd_cmi_afe_port_data *afe_port_d;
int ret = 0;
ret = wcd_cpe_is_valid_port(core, port_cfg, __func__);
if (ret)
return ret;
afe_port_d = &afe_ports[port_cfg->port_id];
afe_port_d->port_id = port_cfg->port_id;
WCD_CPE_GRAB_LOCK(&afe_port_d->afe_lock, "afe");
memset(&hdr, 0, sizeof(struct cmi_hdr));
fill_afe_cmd_header(&hdr, port_cfg->port_id,
CPE_AFE_PORT_CMD_START,
0, false);
ret = wcd_cpe_cmi_send_afe_msg(core, afe_port_d, &hdr);
if (ret)
dev_err(core->dev,
"%s: afe_port_start cmd failed, err = %d\n",
__func__, ret);
WCD_CPE_REL_LOCK(&afe_port_d->afe_lock, "afe");
return ret;
}
/*
* wcd_cpe_afe_port_stop: send stop command to afe service
* @core_handle: handle to the cpe core
* @port_cfg: configuration data for the afe port which needs
* to be stopped.
*/
static int wcd_cpe_afe_port_stop(void *core_handle,
struct wcd_cpe_afe_port_cfg *port_cfg)
{
struct cmi_hdr hdr;
struct wcd_cpe_core *core = core_handle;
struct wcd_cmi_afe_port_data *afe_port_d;
int ret = 0;
ret = wcd_cpe_is_valid_port(core, port_cfg, __func__);
if (ret)
return ret;
afe_port_d = &afe_ports[port_cfg->port_id];
afe_port_d->port_id = port_cfg->port_id;
WCD_CPE_GRAB_LOCK(&afe_port_d->afe_lock, "afe");
memset(&hdr, 0, sizeof(hdr));
fill_afe_cmd_header(&hdr, port_cfg->port_id,
CPE_AFE_PORT_CMD_STOP,
0, false);
ret = wcd_cpe_cmi_send_afe_msg(core, afe_port_d, &hdr);
if (ret)
dev_err(core->dev,
"%s: afe_stop cmd failed, err = %d\n",
__func__, ret);
WCD_CPE_REL_LOCK(&afe_port_d->afe_lock, "afe");
return ret;
}
/*
* wcd_cpe_afe_port_suspend: send suspend command to afe service
* @core_handle: handle to the cpe core
* @port_cfg: configuration data for the afe port which needs
* to be suspended.
*/
static int wcd_cpe_afe_port_suspend(void *core_handle,
struct wcd_cpe_afe_port_cfg *port_cfg)
{
struct cmi_hdr hdr;
struct wcd_cpe_core *core = core_handle;
struct wcd_cmi_afe_port_data *afe_port_d;
int ret = 0;
ret = wcd_cpe_is_valid_port(core, port_cfg, __func__);
if (ret)
return ret;
afe_port_d = &afe_ports[port_cfg->port_id];
afe_port_d->port_id = port_cfg->port_id;
WCD_CPE_GRAB_LOCK(&afe_port_d->afe_lock, "afe");
memset(&hdr, 0, sizeof(struct cmi_hdr));
fill_afe_cmd_header(&hdr, port_cfg->port_id,
CPE_AFE_PORT_CMD_SUSPEND,
0, false);
ret = wcd_cpe_cmi_send_afe_msg(core, afe_port_d, &hdr);
if (ret)
dev_err(core->dev,
"%s: afe_suspend cmd failed, err = %d\n",
__func__, ret);
WCD_CPE_REL_LOCK(&afe_port_d->afe_lock, "afe");
return ret;
}
/*
* wcd_cpe_afe_port_resume: send the resume command to afe service
* @core_handle: handle to the cpe core
* @port_cfg: configuration data for the afe port which needs
* to be resumed.
*/
static int wcd_cpe_afe_port_resume(void *core_handle,
struct wcd_cpe_afe_port_cfg *port_cfg)
{
struct cmi_hdr hdr;
struct wcd_cpe_core *core = core_handle;
struct wcd_cmi_afe_port_data *afe_port_d;
int ret = 0;
ret = wcd_cpe_is_valid_port(core, port_cfg, __func__);
if (ret)
return ret;
afe_port_d = &afe_ports[port_cfg->port_id];
afe_port_d->port_id = port_cfg->port_id;
WCD_CPE_GRAB_LOCK(&afe_port_d->afe_lock, "afe");
memset(&hdr, 0, sizeof(hdr));
fill_afe_cmd_header(&hdr, port_cfg->port_id,
CPE_AFE_PORT_CMD_RESUME,
0, false);
ret = wcd_cpe_cmi_send_afe_msg(core, afe_port_d, &hdr);
if (ret)
dev_err(core->dev,
"%s: afe_resume cmd failed, err = %d\n",
__func__, ret);
WCD_CPE_REL_LOCK(&afe_port_d->afe_lock, "afe");
return ret;
}
/*
* wcd_cpe_register_afe_driver: register lsm driver to codec
* @cpe_ops: structure with lsm callbacks
* @codec: codec to which this lsm driver is registered to
*/
int wcd_cpe_get_afe_ops(struct wcd_cpe_afe_ops *afe_ops)
{
afe_ops->afe_set_params = wcd_cpe_afe_set_params;
afe_ops->afe_port_start = wcd_cpe_afe_port_start;
afe_ops->afe_port_stop = wcd_cpe_afe_port_stop;
afe_ops->afe_port_suspend = wcd_cpe_afe_port_suspend;
afe_ops->afe_port_resume = wcd_cpe_afe_port_resume;
return 0;
}
EXPORT_SYMBOL(wcd_cpe_get_afe_ops);
MODULE_DESCRIPTION("WCD CPE Core");
MODULE_LICENSE("GPL v2");
|