aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/omap/omap-abe-dsp.c
blob: 75caf0fd374b3a7f17393986f448806f856640bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
/*
 * omap-abe-dsp.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Copyright (C) 2010 Texas Instruments Inc.
 *
 * Authors: Liam Girdwood <lrg@ti.com>
 *          Misael Lopez Cruz <misael.lopez@ti.com>
 *          Sebastien Guiriec <s-guiriec@ti.com>
 *
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/i2c.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/workqueue.h>
#include <linux/i2c/twl.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/pm_runtime.h>
#include <linux/dma-mapping.h>
#include <linux/wait.h>
#include <linux/firmware.h>
#include <linux/debugfs.h>
#include <linux/opp.h>
#ifdef CONFIG_OMAP4_DPLL_CASCADING
#include <linux/earlysuspend.h>
#include <mach/omap4-common.h>
#endif

#include <plat/omap_hwmod.h>
#include <plat/omap_device.h>
#include <plat/dma.h>

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>
#include <sound/tlv.h>
#include <sound/omap-abe-dsp.h>

#include "omap-abe-dsp.h"
#include "omap-abe.h"
#include "abe/abe_main.h"
#include "abe/port_mgr.h"

#define OMAP_ABE_HS_DC_OFFSET_STEP	(1800 / 8)
#define OMAP_ABE_HF_DC_OFFSET_STEP	(4600 / 8)

#ifdef CONFIG_OMAP4_DPLL_CASCADING
#define ABE_FE_START           (ABE_NUM_MIXERS + ABE_NUM_MUXES)
#define ABE_NUM_FE             10
#define ABE_FE_END             (ABE_FE_START + ABE_NUM_FE)

static int abe_fe_event(struct snd_soc_dapm_widget *w,
			struct snd_kcontrol *kcontrol, int event);

static bool abe_can_enter_dpll_cascading;     /* initialized to false by gcc */
#endif

static const char *abe_memory_bank[5] = {
	"dmem",
	"cmem",
	"smem",
	"pmem",
	"mpu"
};


/*
 * ABE loadable coefficients.
 * The coefficient and their mixer configurations are loaded with the firmware
 * blob duing probe().
 */

struct coeff_config {
	char name[ABE_COEFF_NAME_SIZE];
	u32 count;
	u32 coeff;
	char texts[ABE_COEFF_NUM_TEXTS][ABE_COEFF_TEXT_SIZE];
};

/*
 * ABE Firmware Header.
 * The ABE firmware blob has a header that describes each data section. This
 * way we can store coefficients etc in the firmware.
 */
struct fw_header {
	u32 magic;			/* magic number */
	u32 crc;			/* optional crc */
	u32 firmware_size;	/* payload size */
	u32 coeff_size;		/* payload size */
	u32 coeff_version;	/* coefficent version */
	u32 firmware_version;	/* min version of ABE firmware required */
	u32 num_equ;		/* number of equalizers */
};

struct abe_opp_req {
	struct device *dev;
        struct list_head node;
	int opp;
};

/*
 * ABE private data.
 */
struct abe_data {
	struct omap4_abe_dsp_pdata *abe_pdata;
	struct device *dev;
	struct snd_soc_platform *platform;
	struct delayed_work delayed_work;
	struct mutex mutex;
	struct mutex opp_mutex;
	struct mutex opp_req_mutex;
	struct clk *clk;
	void __iomem *io_base[5];
	int irq;
	int opp;
	unsigned long opp_freqs[OMAP_ABE_OPP_COUNT];

	/* DC offset cancellation */
	int power_mode;
	u32 dc_hsl;
	u32 dc_hsr;
	u32 dc_hfl;
	u32 dc_hfr;

	int active;

	/* coefficients */
	struct fw_header hdr;
	u32 *firmware;
	s32 *equ[ABE_MAX_EQU];
	int equ_profile[ABE_MAX_EQU];
	struct soc_enum equalizer_enum[ABE_MAX_EQU];
	struct snd_kcontrol_new equalizer_control[ABE_MAX_EQU];
	struct coeff_config *equ_texts;

	int mono_mix[ABE_NUM_MONO_MIXERS];

	/* DAPM mixer config - TODO: some of this can be replaced with HAL update */
	u32 widget_opp[ABE_NUM_DAPM_REG + 1];

	struct list_head opp_req;
	int opp_req_count;

	u16 router[16];

	struct snd_pcm_substream *ping_pong_substream;
	int first_irq;

	struct snd_pcm_substream *psubs;

#ifdef CONFIG_DEBUG_FS
	/* ABE runtime debug config */

	/* its intended we can switch on/off individual debug items */
	u32 dbg_format1; /* TODO: match flag names here to debug format flags */
	u32 dbg_format2;
	u32 dbg_format3;

	u32 dbg_buffer_bytes;
	u32 dbg_circular;
	u32 dbg_buffer_msecs;  /* size of buffer in secs */
	u32 dbg_elem_bytes;
	dma_addr_t dbg_buffer_addr;
	wait_queue_head_t wait;
	int dbg_reader_offset;
	int dbg_dma_offset;
	int dbg_complete;
	struct dentry *debugfs_root;
	struct dentry *debugfs_fmt1;
	struct dentry *debugfs_fmt2;
	struct dentry *debugfs_fmt3;
	struct dentry *debugfs_size;
	struct dentry *debugfs_data;
	struct dentry *debugfs_circ;
	struct dentry *debugfs_elem_bytes;
	struct dentry *debugfs_opp_level;
	char *dbg_buffer;
	struct omap_pcm_dma_data *dma_data;
	int dma_ch;
	int dma_req;
#endif
#ifdef CONFIG_OMAP4_DPLL_CASCADING
	struct early_suspend early_suspend;
	int fe_active[ABE_NUM_FE];
	int early_suspended;
#endif
};

static struct abe_data *the_abe;

static int aess_set_runtime_opp_level(struct abe_data *abe);

// TODO: map to the new version of HAL
static unsigned int abe_dsp_read(struct snd_soc_platform *platform,
		unsigned int reg)
{
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);

	BUG_ON(reg > ABE_NUM_DAPM_REG);
	return abe->widget_opp[reg];
}

static int abe_dsp_write(struct snd_soc_platform *platform, unsigned int reg,
		unsigned int val)
{
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);

	BUG_ON(reg > ABE_NUM_DAPM_REG);
	abe->widget_opp[reg] = val;
	return 0;
}

static void abe_irq_pingpong_subroutine(u32 *data)
{
	u32 dst, n_bytes;

	abe_read_next_ping_pong_buffer(MM_DL_PORT, &dst, &n_bytes);
	abe_set_ping_pong_buffer(MM_DL_PORT, n_bytes);

	/* Do not call ALSA function for first IRQ */
	if (the_abe->first_irq) {
		the_abe->first_irq = 0;
	} else {
		if (the_abe->ping_pong_substream)
			snd_pcm_period_elapsed(the_abe->ping_pong_substream);
	}
}

static irqreturn_t abe_irq_handler(int irq, void *dev_id)
{
	struct abe_data *abe = dev_id;

	/* TODO: handle underruns/overruns/errors */
	pm_runtime_get_sync(abe->dev);
	abe_clear_irq();  // TODO: why is IRQ not cleared after processing ?
	abe_irq_processing();
	pm_runtime_put_sync_suspend(abe->dev);
	return IRQ_HANDLED;
}

// TODO: these should really be called internally since we will know the McPDM state
void abe_dsp_pm_get(void)
{
	pm_runtime_get_sync(the_abe->dev);
}
EXPORT_SYMBOL_GPL(abe_dsp_pm_get);

void abe_dsp_pm_put(void)
{
	pm_runtime_put_sync(the_abe->dev);
}
EXPORT_SYMBOL_GPL(abe_dsp_pm_put);

void abe_dsp_shutdown(void)
{
	struct omap4_abe_dsp_pdata *pdata = the_abe->abe_pdata;
	int ret;

#ifdef CONFIG_OMAP4_DPLL_CASCADING
	/*
	 * ensure we're out of DPLL cascading to properly
	 * enter into suspend state
	 */
	omap4_dpll_cascading_blocker_hold(the_abe->dev);
#endif

	if (!the_abe->active && !abe_check_activity()) {
		abe_set_opp_processing(ABE_OPP25);
		the_abe->opp = 25;
		abe_stop_event_generator();
		udelay(250);
		if (pdata && pdata->device_scale) {
			ret = pdata->device_scale(the_abe->dev, the_abe->dev,
				the_abe->opp_freqs[0]);
			if (ret)
				dev_err(the_abe->dev,
					"failed to scale to lowest OPP\n");
		}
	}
}
EXPORT_SYMBOL_GPL(abe_dsp_shutdown);

void abe_dsp_set_hs_offset(int left, int right, int mult)
{
	/* TODO: do not use abe global structure */
	if (the_abe == NULL)
		return;

	if (left >= 8)
		left -= 16;
	the_abe->dc_hsl = OMAP_ABE_HS_DC_OFFSET_STEP * left * mult;

	if (right >= 8)
		right -= 16;
	the_abe->dc_hsr = OMAP_ABE_HS_DC_OFFSET_STEP * right * mult;
}
EXPORT_SYMBOL(abe_dsp_set_hs_offset);

void abe_dsp_set_hf_offset(int left, int right)
{
	/* TODO: do not use abe global structure */
	if (the_abe == NULL)
		return;

	if (left >= 8)
		left -= 16;
	the_abe->dc_hfl = OMAP_ABE_HF_DC_OFFSET_STEP * left;

	if (right >= 8)
		right -= 16;
	the_abe->dc_hfr = OMAP_ABE_HF_DC_OFFSET_STEP * right;
}
EXPORT_SYMBOL(abe_dsp_set_hf_offset);

void abe_dsp_set_power_mode(int mode)
{
	if (the_abe == NULL)
		return;

	/* TODO: do not use abe global structure */
	the_abe->power_mode = mode;
}
EXPORT_SYMBOL(abe_dsp_set_power_mode);

/*
 * These TLV settings will need fine tuned for each individual control
 */

/* Media DL1 volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(mm_dl1_tlv, -12000, 100, 3000);

/* Media DL1 volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(tones_dl1_tlv, -12000, 100, 3000);

/* Media DL1 volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(voice_dl1_tlv, -12000, 100, 3000);

/* Media DL1 volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(capture_dl1_tlv, -12000, 100, 3000);

#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
/* Media DL2 volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(mm_dl2_tlv, -12000, 100, 3000);

/* Media DL2 volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(tones_dl2_tlv, -12000, 100, 3000);

/* Media DL2 volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(voice_dl2_tlv, -12000, 100, 3000);

/* Media DL2 volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(capture_dl2_tlv, -12000, 100, 3000);
#endif

/* SDT volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(sdt_ul_tlv, -12000, 100, 3000);

/* SDT volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(sdt_dl_tlv, -12000, 100, 3000);

/* AUDUL volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(audul_mm_tlv, -12000, 100, 3000);

/* AUDUL volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(audul_tones_tlv, -12000, 100, 3000);

/* AUDUL volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(audul_vx_ul_tlv, -12000, 100, 3000);

/* AUDUL volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(audul_vx_dl_tlv, -12000, 100, 3000);

/* VXREC volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(vxrec_mm_dl_tlv, -12000, 100, 3000);

/* VXREC volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(vxrec_tones_tlv, -12000, 100, 3000);

/* VXREC volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(vxrec_vx_dl_tlv, -12000, 100, 3000);

/* VXREC volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(vxrec_vx_ul_tlv, -12000, 100, 3000);

/* DMIC volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(dmic_tlv, -12000, 100, 3000);

/* BT UL volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(btul_tlv, -12000, 100, 3000);

/* AMIC volume control from -120 to 30 dB in 1 dB steps */
static DECLARE_TLV_DB_SCALE(amic_tlv, -12000, 100, 3000);

//TODO: we have to use the shift value atm to represent register id due to current HAL
static int dl1_put_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);

	// TODO: optimise all of these to call HAL abe_enable_gain(mixer, enable)
	if (ucontrol->value.integer.value[0]) {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 1);
		abe_enable_gain(MIXDL1, mc->reg);
	} else {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 0);
		abe_disable_gain(MIXDL1, mc->reg);
	}
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}

#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
static int dl2_put_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);

	if (ucontrol->value.integer.value[0]) {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 1);
		abe_enable_gain(MIXDL2, mc->reg);
	} else {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 0);
		abe_disable_gain(MIXDL2, mc->reg);
	}

	pm_runtime_put_sync(the_abe->dev);
	return 1;
}
#endif

static int audio_ul_put_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);

	if (ucontrol->value.integer.value[0]) {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 1);
		abe_enable_gain(MIXAUDUL, mc->reg);
	} else {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 0);
		abe_disable_gain(MIXAUDUL, mc->reg);
	}
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}

static int vxrec_put_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);

	if (ucontrol->value.integer.value[0]) {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 1);
		abe_enable_gain(MIXVXREC, mc->reg);
	} else {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 0);
		abe_disable_gain(MIXVXREC, mc->reg);
	}
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}

static int sdt_put_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);

	if (ucontrol->value.integer.value[0]) {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 1);
		abe_enable_gain(MIXSDT, mc->reg);
	} else {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 0);
		abe_disable_gain(MIXSDT, mc->reg);
	}
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}

static int abe_get_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	ucontrol->value.integer.value[0] = the_abe->widget_opp[mc->shift];
	return 0;
}

static int abe_dsp_set_mono_mixer(int id, int enable)
{
	int mixer;

	switch (id) {
	case MIX_DL1_MONO:
		mixer = MIXDL1;
		break;
#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	case MIX_DL2_MONO:
		mixer = MIXDL2;
		break;
#endif
	case MIX_AUDUL_MONO:
		mixer = MIXAUDUL;
		break;
	default:
		return -EINVAL;
	}

	pm_runtime_get_sync(the_abe->dev);
	abe_mono_mixer(mixer, enable);
	pm_runtime_put_sync(the_abe->dev);

	return 0;
}

static int abe_put_mono_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	int id = mc->shift - MIX_DL1_MONO;

	the_abe->mono_mix[id] = ucontrol->value.integer.value[0];
	abe_dsp_set_mono_mixer(mc->shift, the_abe->mono_mix[id]);

	return 1;
}

static int abe_get_mono_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	int id = mc->shift - MIX_DL1_MONO;

	ucontrol->value.integer.value[0] = the_abe->mono_mix[id];
	return 0;
}

/* router IDs that match our mixer strings */
static const abe_router_t router[] = {
		ZERO_labelID, /* strangely this is not 0 */
		DMIC1_L_labelID, DMIC1_R_labelID,
		DMIC2_L_labelID, DMIC2_R_labelID,
		DMIC3_L_labelID, DMIC3_R_labelID,
		BT_UL_L_labelID, BT_UL_R_labelID,
		MM_EXT_IN_L_labelID, MM_EXT_IN_R_labelID,
		AMIC_L_labelID, AMIC_R_labelID,
		VX_REC_L_labelID, VX_REC_R_labelID,
};

static int ul_mux_put_route(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
	int mux = ucontrol->value.enumerated.item[0];
	int reg = e->reg - ABE_MUX(0);

	pm_runtime_get_sync(the_abe->dev);

	if (mux > ABE_ROUTES_UL)
		return 0;

	// TODO: get all this via firmware
	if (reg < 8) {
		/* 0  .. 9   = MM_UL */
		the_abe->router[reg] = router[mux];
	} else if (reg < 12) {
		/* 10 .. 11  = MM_UL2 */
		/* 12 .. 13  = VX_UL */
		the_abe->router[reg + 2] = router[mux];
	}

	/* 2nd arg here is unused */
	abe_set_router_configuration(UPROUTE, 0, (u32 *)the_abe->router);

	if (router[mux] != ZERO_labelID)
		the_abe->widget_opp[e->reg] = e->shift_l;
	else
		the_abe->widget_opp[e->reg] = 0;

	snd_soc_dapm_mux_update_power(widget, kcontrol, 1, mux, e);
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}

static int ul_mux_get_route(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_enum *e =
		(struct soc_enum *)kcontrol->private_value;
	int reg = e->reg - ABE_MUX(0), i, rval = 0;

	// TODO: get all this via firmware
	if (reg < 8) {
		/* 0  .. 9   = MM_UL */
		rval = the_abe->router[reg];
	} else if (reg < 12) {
		/* 10 .. 11  = MM_UL2 */
		/* 12 .. 13  = VX_UL */
		rval = the_abe->router[reg + 2];
	}

	for (i = 0; i < ARRAY_SIZE(router); i++) {
		if (router[i] == rval) {
			ucontrol->value.integer.value[0] = i;
			return 0;
		}
	}

	return 1;
}


static int abe_put_switch(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);

	if (ucontrol->value.integer.value[0]) {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 1);
	} else {
		the_abe->widget_opp[mc->shift] = ucontrol->value.integer.value[0];
		snd_soc_dapm_mixer_update_power(widget, kcontrol, 0);
	}
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}


static int volume_put_sdt_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);

	abe_write_mixer(MIXSDT, abe_val_to_gain(ucontrol->value.integer.value[0]),
				RAMP_2MS, mc->reg);
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}

static int volume_put_audul_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);
	abe_write_mixer(MIXAUDUL, abe_val_to_gain(ucontrol->value.integer.value[0]),
				RAMP_2MS, mc->reg);
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}

static int volume_put_vxrec_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);
	abe_write_mixer(MIXVXREC, abe_val_to_gain(ucontrol->value.integer.value[0]),
				RAMP_2MS, mc->reg);
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}

static int volume_put_dl1_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);
	abe_write_mixer(MIXDL1, abe_val_to_gain(ucontrol->value.integer.value[0]),
				RAMP_2MS, mc->reg);
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}

#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
static int volume_put_dl2_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	pm_runtime_get_sync(the_abe->dev);
	abe_write_mixer(MIXDL2, abe_val_to_gain(ucontrol->value.integer.value[0]),
				RAMP_2MS, mc->reg);
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}
#endif

static int do_volume_put_gain(struct soc_mixer_control *mc, int lval, int rval,
	int ramp)
{
	pm_runtime_get_sync(the_abe->dev);
	abe_write_gain(mc->reg,
		       abe_val_to_gain(lval),
		       ramp, mc->shift);
	abe_write_gain(mc->reg,
		       abe_val_to_gain(rval),
		       ramp, mc->rshift);
	pm_runtime_put_sync(the_abe->dev);

	return 1;
}

static int volume_put_gain(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	int lval = ucontrol->value.integer.value[0];
	int rval = ucontrol->value.integer.value[1];
	int ramp = RAMP_2MS;

	do_volume_put_gain(mc, lval, rval, ramp);

	return 1;
}

static int volume_put_dmic_gain(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	int lval = ucontrol->value.integer.value[0];
	int rval = ucontrol->value.integer.value[1];
	int ramp = RAMP_2MS;

	/* DMic's typ. need 10ms to settle after applying bias and clocks.
	 * The OMAP DMic module is unable to start the clocks independent
	 * of starting the sDMA transfer, so there will always be a pop.
	 * This is worked around by setting a slower ramp for everything
	 * except muting.
	 */
	if ((lval >= 0) && (rval >= 0))
		ramp = RAMP_50MS;

	do_volume_put_gain(mc, lval, rval, ramp);

	return 1;
}

static int volume_get_dl1_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	u32 val;

	pm_runtime_get_sync(the_abe->dev);
	abe_read_mixer(MIXDL1, &val, mc->reg);
	ucontrol->value.integer.value[0] = abe_gain_to_val(val);
	pm_runtime_put_sync(the_abe->dev);

	return 0;
}

#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
static int volume_get_dl2_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	u32 val;

	pm_runtime_get_sync(the_abe->dev);
	abe_read_mixer(MIXDL2, &val, mc->reg);
	ucontrol->value.integer.value[0] = abe_gain_to_val(val);
	pm_runtime_put_sync(the_abe->dev);

	return 0;
}
#endif

static int volume_get_audul_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	u32 val;

	pm_runtime_get_sync(the_abe->dev);
	abe_read_mixer(MIXAUDUL, &val, mc->reg);
	ucontrol->value.integer.value[0] = abe_gain_to_val(val);
	pm_runtime_put_sync(the_abe->dev);

	return 0;
}

static int volume_get_vxrec_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	u32 val;

	pm_runtime_get_sync(the_abe->dev);
	abe_read_mixer(MIXVXREC, &val, mc->reg);
	ucontrol->value.integer.value[0] = abe_gain_to_val(val);
	pm_runtime_put_sync(the_abe->dev);

	return 0;
}

static int volume_get_sdt_mixer(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	u32 val;

	pm_runtime_get_sync(the_abe->dev);
	abe_read_mixer(MIXSDT, &val, mc->reg);
	ucontrol->value.integer.value[0] = abe_gain_to_val(val);
	pm_runtime_put_sync(the_abe->dev);

	return 0;
}

static int volume_get_gain(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	u32 val;

	pm_runtime_get_sync(the_abe->dev);
	abe_read_gain(mc->reg, &val, mc->shift);
	ucontrol->value.integer.value[0] = abe_gain_to_val(val);
	abe_read_gain(mc->reg, &val, mc->rshift);
	ucontrol->value.integer.value[1] = abe_gain_to_val(val);
	pm_runtime_put_sync(the_abe->dev);

	return 0;
}

static int abe_dsp_set_equalizer(unsigned int id, unsigned int profile)
{
	abe_equ_t equ_params;
	int len;

	if (id >= the_abe->hdr.num_equ)
		return -EINVAL;

	if (profile >= the_abe->equ_texts[id].count)
		return -EINVAL;

	len = the_abe->equ_texts[id].coeff;
	equ_params.equ_length = len;
	memcpy(equ_params.coef.type1, the_abe->equ[id] + profile * len,
		len * sizeof(u32));
	the_abe->equ_profile[id] = profile;

	pm_runtime_get_sync(the_abe->dev);
	abe_write_equalizer(id + 1, &equ_params);
	pm_runtime_put_sync(the_abe->dev);

	return 0;
}

static int abe_get_equalizer(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_value *ucontrol)
{
	struct soc_enum *eqc = (struct soc_enum *)kcontrol->private_value;

	ucontrol->value.integer.value[0] = the_abe->equ_profile[eqc->reg];
	return 0;
}

static int abe_put_equalizer(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_value *ucontrol)
{
	struct soc_enum *eqc = (struct soc_enum *)kcontrol->private_value;
	u16 val = ucontrol->value.enumerated.item[0];
	int ret;

	ret = abe_dsp_set_equalizer(eqc->reg, val);
	if (ret < 0)
		return ret;

	return 1;
}

int snd_soc_info_enum_ext1(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_info *uinfo)
{
	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = e->max;

	if (uinfo->value.enumerated.item > e->max - 1)
		uinfo->value.enumerated.item = e->max - 1;
	strcpy(uinfo->value.enumerated.name,
		snd_soc_get_enum_text(e, uinfo->value.enumerated.item));

	return 0;
}

static const char *route_ul_texts[] = {
	"None", "DMic0L", "DMic0R", "DMic1L", "DMic1R", "DMic2L", "DMic2R",
	"BT Left", "BT Right", "MMExt Left", "MMExt Right", "AMic0", "AMic1",
	"VX Left", "VX Right"
};

/* ROUTE_UL Mux table */
static const struct soc_enum abe_enum[] = {
		SOC_ENUM_SINGLE(MUX_MM_UL10, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_MM_UL11, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_MM_UL12, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_MM_UL13, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_MM_UL14, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_MM_UL15, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_MM_UL16, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_MM_UL17, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_MM_UL20, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_MM_UL21, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_VX_UL0, 0, 15, route_ul_texts),
		SOC_ENUM_SINGLE(MUX_VX_UL1, 0, 15, route_ul_texts),
};

static const struct snd_kcontrol_new mm_ul00_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[0],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_ul01_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[1],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_ul02_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[2],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_ul03_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[3],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_ul04_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[4],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_ul05_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[5],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_ul06_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[6],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_ul07_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[7],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_ul10_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[8],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_ul11_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[9],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_vx0_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[10],
	ul_mux_get_route, ul_mux_put_route);

static const struct snd_kcontrol_new mm_vx1_control =
	SOC_DAPM_ENUM_EXT("Route", abe_enum[11],
	ul_mux_get_route, ul_mux_put_route);

/* DL1 mixer paths */
static const struct snd_kcontrol_new dl1_mixer_controls[] = {
	SOC_SINGLE_EXT("Tones", MIX_DL1_INPUT_TONES, MIX_DL1_TONES, 1, 0,
		abe_get_mixer, dl1_put_mixer),
	SOC_SINGLE_EXT("Voice", MIX_DL1_INPUT_VX_DL, MIX_DL1_VOICE, 1, 0,
		abe_get_mixer, dl1_put_mixer),
	SOC_SINGLE_EXT("Capture", MIX_DL1_INPUT_MM_UL2, MIX_DL1_CAPTURE, 1, 0,
		abe_get_mixer, dl1_put_mixer),
	SOC_SINGLE_EXT("Multimedia", MIX_DL1_INPUT_MM_DL, MIX_DL1_MEDIA, 1, 0,
		abe_get_mixer, dl1_put_mixer),
};

#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
/* DL2 mixer paths */
static const struct snd_kcontrol_new dl2_mixer_controls[] = {
	SOC_SINGLE_EXT("Tones", MIX_DL2_INPUT_TONES, MIX_DL2_TONES, 1, 0,
		abe_get_mixer, dl2_put_mixer),
	SOC_SINGLE_EXT("Voice", MIX_DL2_INPUT_VX_DL, MIX_DL2_VOICE, 1, 0,
		abe_get_mixer, dl2_put_mixer),
	SOC_SINGLE_EXT("Capture", MIX_DL2_INPUT_MM_UL2, MIX_DL2_CAPTURE, 1, 0,
		abe_get_mixer, dl2_put_mixer),
	SOC_SINGLE_EXT("Multimedia", MIX_DL2_INPUT_MM_DL, MIX_DL2_MEDIA, 1, 0,
		abe_get_mixer, dl2_put_mixer),
};
#endif

/* AUDUL ("Voice Capture Mixer") mixer paths */
static const struct snd_kcontrol_new audio_ul_mixer_controls[] = {
	SOC_SINGLE_EXT("Tones Playback", MIX_AUDUL_INPUT_TONES, MIX_AUDUL_TONES, 1, 0,
		abe_get_mixer, audio_ul_put_mixer),
	SOC_SINGLE_EXT("Media Playback", MIX_AUDUL_INPUT_MM_DL, MIX_AUDUL_MEDIA, 1, 0,
		abe_get_mixer, audio_ul_put_mixer),
	SOC_SINGLE_EXT("Capture", MIX_AUDUL_INPUT_UPLINK, MIX_AUDUL_CAPTURE, 1, 0,
		abe_get_mixer, audio_ul_put_mixer),
};

/* VXREC ("Capture Mixer")  mixer paths */
static const struct snd_kcontrol_new vx_rec_mixer_controls[] = {
	SOC_SINGLE_EXT("Tones", MIX_VXREC_INPUT_TONES, MIX_VXREC_TONES, 1, 0,
		abe_get_mixer, vxrec_put_mixer),
	SOC_SINGLE_EXT("Voice Playback", MIX_VXREC_INPUT_VX_DL,
		MIX_VXREC_VOICE_PLAYBACK, 1, 0, abe_get_mixer, vxrec_put_mixer),
	SOC_SINGLE_EXT("Voice Capture", MIX_VXREC_INPUT_VX_UL,
		MIX_VXREC_VOICE_CAPTURE, 1, 0, abe_get_mixer, vxrec_put_mixer),
	SOC_SINGLE_EXT("Media Playback", MIX_VXREC_INPUT_MM_DL,
		MIX_VXREC_MEDIA, 1, 0, abe_get_mixer, vxrec_put_mixer),
};

/* SDT ("Sidetone Mixer") mixer paths */
static const struct snd_kcontrol_new sdt_mixer_controls[] = {
	SOC_SINGLE_EXT("Capture", MIX_SDT_INPUT_UP_MIXER, MIX_SDT_CAPTURE, 1, 0,
		abe_get_mixer, sdt_put_mixer),
	SOC_SINGLE_EXT("Playback", MIX_SDT_INPUT_DL1_MIXER, MIX_SDT_PLAYBACK, 1, 0,
		abe_get_mixer, sdt_put_mixer),
};

/* Virtual PDM_DL1 Switch */
static const struct snd_kcontrol_new pdm_dl1_switch_controls =
	SOC_SINGLE_EXT("Switch", ABE_VIRTUAL_SWITCH, MIX_SWITCH_PDM_DL1, 1, 0,
			abe_get_mixer, abe_put_switch);

#if !defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
/* Virtual PDM_DL2 Switch: DL1 -> PDM_DL2 */
static const struct snd_kcontrol_new pdm_dl2_switch_controls =
	SOC_SINGLE_EXT("Switch", ABE_VIRTUAL_SWITCH, MIX_SWITCH_PDM_DL2, 1, 0,
			abe_get_mixer, abe_put_switch);
#endif

/* Virtual BT_VX_DL Switch */
static const struct snd_kcontrol_new bt_vx_dl_switch_controls =
	SOC_SINGLE_EXT("Switch", ABE_VIRTUAL_SWITCH, MIX_SWITCH_BT_VX_DL, 1, 0,
			abe_get_mixer, abe_put_switch);

/* Virtual MM_EXT_DL Switch */
static const struct snd_kcontrol_new mm_ext_dl_switch_controls =
	SOC_SINGLE_EXT("Switch", ABE_VIRTUAL_SWITCH, MIX_SWITCH_MM_EXT_DL, 1, 0,
			abe_get_mixer, abe_put_switch);

static const struct snd_kcontrol_new abe_controls[] = {
	/* DL1 mixer gains */
	SOC_SINGLE_EXT_TLV("DL1 Media Playback Volume",
		MIX_DL1_INPUT_MM_DL, 0, 149, 0,
		volume_get_dl1_mixer, volume_put_dl1_mixer, mm_dl1_tlv),
	SOC_SINGLE_EXT_TLV("DL1 Tones Playback Volume",
		MIX_DL1_INPUT_TONES, 0, 149, 0,
		volume_get_dl1_mixer, volume_put_dl1_mixer, tones_dl1_tlv),
	SOC_SINGLE_EXT_TLV("DL1 Voice Playback Volume",
		MIX_DL1_INPUT_VX_DL, 0, 149, 0,
		volume_get_dl1_mixer, volume_put_dl1_mixer, voice_dl1_tlv),
	SOC_SINGLE_EXT_TLV("DL1 Capture Playback Volume",
		MIX_DL1_INPUT_MM_UL2, 0, 149, 0,
		volume_get_dl1_mixer, volume_put_dl1_mixer, capture_dl1_tlv),

#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	/* DL2 mixer gains */
	SOC_SINGLE_EXT_TLV("DL2 Media Playback Volume",
		MIX_DL2_INPUT_MM_DL, 0, 149, 0,
		volume_get_dl2_mixer, volume_put_dl2_mixer, mm_dl2_tlv),
	SOC_SINGLE_EXT_TLV("DL2 Tones Playback Volume",
		MIX_DL2_INPUT_TONES, 0, 149, 0,
		volume_get_dl2_mixer, volume_put_dl2_mixer, tones_dl2_tlv),
	SOC_SINGLE_EXT_TLV("DL2 Voice Playback Volume",
		MIX_DL2_INPUT_VX_DL, 0, 149, 0,
		volume_get_dl2_mixer, volume_put_dl2_mixer, voice_dl2_tlv),
	SOC_SINGLE_EXT_TLV("DL2 Capture Playback Volume",
		MIX_DL2_INPUT_MM_UL2, 0, 149, 0,
		volume_get_dl2_mixer, volume_put_dl2_mixer, capture_dl2_tlv),
#endif

	/* VXREC mixer gains */
	SOC_SINGLE_EXT_TLV("VXREC Media Volume",
		MIX_VXREC_INPUT_MM_DL, 0, 149, 0,
		volume_get_vxrec_mixer, volume_put_vxrec_mixer, vxrec_mm_dl_tlv),
	SOC_SINGLE_EXT_TLV("VXREC Tones Volume",
		MIX_VXREC_INPUT_TONES, 0, 149, 0,
		volume_get_vxrec_mixer, volume_put_vxrec_mixer, vxrec_tones_tlv),
	SOC_SINGLE_EXT_TLV("VXREC Voice DL Volume",
		MIX_VXREC_INPUT_VX_DL, 0, 149, 0,
		volume_get_vxrec_mixer, volume_put_vxrec_mixer, vxrec_vx_dl_tlv),
	SOC_SINGLE_EXT_TLV("VXREC Voice UL Volume",
		MIX_VXREC_INPUT_VX_UL, 0, 149, 0,
		volume_get_vxrec_mixer, volume_put_vxrec_mixer, vxrec_vx_ul_tlv),

	/* AUDUL mixer gains */
	SOC_SINGLE_EXT_TLV("AUDUL Media Volume",
		MIX_AUDUL_INPUT_MM_DL, 0, 149, 0,
		volume_get_audul_mixer, volume_put_audul_mixer, audul_mm_tlv),
	SOC_SINGLE_EXT_TLV("AUDUL Tones Volume",
		MIX_AUDUL_INPUT_TONES, 0, 149, 0,
		volume_get_audul_mixer, volume_put_audul_mixer, audul_tones_tlv),
	SOC_SINGLE_EXT_TLV("AUDUL Voice UL Volume",
		MIX_AUDUL_INPUT_UPLINK, 0, 149, 0,
		volume_get_audul_mixer, volume_put_audul_mixer, audul_vx_ul_tlv),
	SOC_SINGLE_EXT_TLV("AUDUL Voice DL Volume",
		MIX_AUDUL_INPUT_VX_DL, 0, 149, 0,
		volume_get_audul_mixer, volume_put_audul_mixer, audul_vx_dl_tlv),

	/* SDT mixer gains */
	SOC_SINGLE_EXT_TLV("SDT UL Volume",
		MIX_SDT_INPUT_UP_MIXER, 0, 149, 0,
		volume_get_sdt_mixer, volume_put_sdt_mixer, sdt_ul_tlv),
	SOC_SINGLE_EXT_TLV("SDT DL Volume",
		MIX_SDT_INPUT_DL1_MIXER, 0, 149, 0,
		volume_get_sdt_mixer, volume_put_sdt_mixer, sdt_dl_tlv),

	/* DMIC gains */
	SOC_DOUBLE_EXT_TLV("DMIC1 UL Volume",
		GAINS_DMIC1, GAIN_LEFT_OFFSET, GAIN_RIGHT_OFFSET, 149, 0,
		volume_get_gain, volume_put_dmic_gain, dmic_tlv),

	SOC_DOUBLE_EXT_TLV("DMIC2 UL Volume",
		GAINS_DMIC2, GAIN_LEFT_OFFSET, GAIN_RIGHT_OFFSET, 149, 0,
		volume_get_gain, volume_put_dmic_gain, dmic_tlv),

	SOC_DOUBLE_EXT_TLV("DMIC3 UL Volume",
		GAINS_DMIC3, GAIN_LEFT_OFFSET, GAIN_RIGHT_OFFSET, 149, 0,
		volume_get_gain, volume_put_dmic_gain, dmic_tlv),

	SOC_DOUBLE_EXT_TLV("AMIC UL Volume",
		GAINS_AMIC, GAIN_LEFT_OFFSET, GAIN_RIGHT_OFFSET, 149, 0,
		volume_get_gain, volume_put_gain, amic_tlv),

	SOC_DOUBLE_EXT_TLV("BT UL Volume",
		GAINS_BTUL, GAIN_LEFT_OFFSET, GAIN_RIGHT_OFFSET, 149, 0,
		volume_get_gain, volume_put_gain, btul_tlv),

	SOC_SINGLE_EXT("DL1 Mono Mixer", MIXDL1, MIX_DL1_MONO, 1, 0,
		abe_get_mono_mixer, abe_put_mono_mixer),
#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	SOC_SINGLE_EXT("DL2 Mono Mixer", MIXDL2, MIX_DL2_MONO, 1, 0,
		abe_get_mono_mixer, abe_put_mono_mixer),
#endif
	SOC_SINGLE_EXT("AUDUL Mono Mixer", MIXAUDUL, MIX_AUDUL_MONO, 1, 0,
		abe_get_mono_mixer, abe_put_mono_mixer),
};

static const struct snd_soc_dapm_widget abe_dapm_widgets[] = {

#ifdef CONFIG_OMAP4_DPLL_CASCADING
	/* Frontend AIFs */
	SND_SOC_DAPM_AIF_IN_E("TONES_DL", "Tones Playback", 0,
			W_AIF_TONES_DL, ABE_OPP_25, 0,
			abe_fe_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_AIF_IN_E("VX_DL", "Voice Playback", 0,
			W_AIF_VX_DL, ABE_OPP_50, 0,
			abe_fe_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_AIF_OUT_E("VX_UL", "Voice Capture", 0,
			W_AIF_VX_UL, ABE_OPP_50, 0,
			abe_fe_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),

	/* the MM_UL mapping is intentional */
	SND_SOC_DAPM_AIF_OUT_E("MM_UL1", "MultiMedia1 Capture", 0,
			W_AIF_MM_UL1, ABE_OPP_100, 0,
			abe_fe_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_AIF_OUT_E("MM_UL2", "MultiMedia2 Capture", 0,
			W_AIF_MM_UL2, ABE_OPP_50, 0,
			abe_fe_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_AIF_IN_E("MM_DL", " MultiMedia1 Playback", 0,
			W_AIF_MM_DL, ABE_OPP_25, 0,
			abe_fe_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_AIF_IN_E("MM_DL_LP", " MultiMedia1 LP Playback", 0,
			W_AIF_MM_DL_LP, ABE_OPP_25, 0,
			abe_fe_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_AIF_IN_E("VIB_DL", "Vibra Playback", 0,
			W_AIF_VIB_DL, ABE_OPP_100, 0,
			abe_fe_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_AIF_IN_E("MODEM_DL", "MODEM Playback", 0,
			W_AIF_MODEM_DL, ABE_OPP_50, 0,
			abe_fe_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_AIF_OUT_E("MODEM_UL", "MODEM Capture", 0,
			W_AIF_MODEM_UL, ABE_OPP_50, 0,
			abe_fe_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
#else
	/* Frontend AIFs */
	SND_SOC_DAPM_AIF_IN("TONES_DL", "Tones Playback", 0,
			W_AIF_TONES_DL, ABE_OPP_25, 0),
	SND_SOC_DAPM_AIF_IN("VX_DL", "Voice Playback", 0,
			W_AIF_VX_DL, ABE_OPP_50, 0),
	SND_SOC_DAPM_AIF_OUT("VX_UL", "Voice Capture", 0,
			W_AIF_VX_UL, ABE_OPP_50, 0),
	/* the MM_UL mapping is intentional */
	SND_SOC_DAPM_AIF_OUT("MM_UL1", "MultiMedia1 Capture", 0,
			W_AIF_MM_UL1, ABE_OPP_100, 0),
	SND_SOC_DAPM_AIF_OUT("MM_UL2", "MultiMedia2 Capture", 0,
			W_AIF_MM_UL2, ABE_OPP_50, 0),
	SND_SOC_DAPM_AIF_IN("MM_DL", " MultiMedia1 Playback", 0,
			W_AIF_MM_DL, ABE_OPP_25, 0),
	SND_SOC_DAPM_AIF_IN("MM_DL_LP", " MultiMedia1 LP Playback", 0,
			W_AIF_MM_DL_LP, ABE_OPP_25, 0),
	SND_SOC_DAPM_AIF_IN("VIB_DL", "Vibra Playback", 0,
			W_AIF_VIB_DL, ABE_OPP_100, 0),
	SND_SOC_DAPM_AIF_IN("MODEM_DL", "MODEM Playback", 0,
			W_AIF_MODEM_DL, ABE_OPP_50, 0),
	SND_SOC_DAPM_AIF_OUT("MODEM_UL", "MODEM Capture", 0,
			W_AIF_MODEM_UL, ABE_OPP_50, 0),
#endif

	/* Backend DAIs  */
	SND_SOC_DAPM_AIF_IN("PDM_UL1", "Analog Capture", 0,
			W_AIF_PDM_UL1, ABE_OPP_50, 0),
	SND_SOC_DAPM_AIF_OUT("PDM_DL1", "HS Playback", 0,
			W_AIF_PDM_DL1, ABE_OPP_25, 0),
#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	SND_SOC_DAPM_AIF_OUT("PDM_DL2", "HF Playback", 0,
			W_AIF_PDM_DL2, ABE_OPP_100, 0),
#else
	SND_SOC_DAPM_AIF_OUT("PDM_DL2", "HF Playback", 0,
			W_AIF_PDM_DL2, ABE_OPP_25, 0),
#endif
	SND_SOC_DAPM_AIF_OUT("PDM_VIB", "Vibra Playback", 0,
			W_AIF_PDM_VIB, ABE_OPP_100, 0),
	SND_SOC_DAPM_AIF_IN("BT_VX_UL", "BT Capture", 0,
			W_AIF_BT_VX_UL, ABE_OPP_50, 0),
	SND_SOC_DAPM_AIF_OUT("BT_VX_DL", "BT Playback", 0,
			W_AIF_BT_VX_DL, ABE_OPP_50, 0),
	SND_SOC_DAPM_AIF_IN("MM_EXT_UL", "FM Capture", 0,
			W_AIF_MM_EXT_UL, ABE_OPP_50, 0),
	SND_SOC_DAPM_AIF_OUT("MM_EXT_DL", "FM Playback", 0,
			W_AIF_MM_EXT_DL, ABE_OPP_25, 0),
	SND_SOC_DAPM_AIF_IN("DMIC0", "DMIC0 Capture", 0,
			W_AIF_DMIC0, ABE_OPP_50, 0),
	SND_SOC_DAPM_AIF_IN("DMIC1", "DMIC1 Capture", 0,
			W_AIF_DMIC1, ABE_OPP_50, 0),
	SND_SOC_DAPM_AIF_IN("DMIC2", "DMIC2 Capture", 0,
			W_AIF_DMIC2, ABE_OPP_50, 0),
	SND_SOC_DAPM_AIF_IN("VXREC", "VXREC Capture", 0,
			W_AIF_VXREC, ABE_OPP_50, 0),

	/* ROUTE_UL Capture Muxes */
	SND_SOC_DAPM_MUX("MUX_UL00",
			W_MUX_UL00, ABE_OPP_50, 0, &mm_ul00_control),
	SND_SOC_DAPM_MUX("MUX_UL01",
			W_MUX_UL01, ABE_OPP_50, 0, &mm_ul01_control),
	SND_SOC_DAPM_MUX("MUX_UL02",
			W_MUX_UL02, ABE_OPP_50, 0, &mm_ul02_control),
	SND_SOC_DAPM_MUX("MUX_UL03",
			W_MUX_UL03, ABE_OPP_50, 0, &mm_ul03_control),
	SND_SOC_DAPM_MUX("MUX_UL04",
			W_MUX_UL04, ABE_OPP_50, 0, &mm_ul04_control),
	SND_SOC_DAPM_MUX("MUX_UL05",
			W_MUX_UL05, ABE_OPP_50, 0, &mm_ul05_control),
	SND_SOC_DAPM_MUX("MUX_UL06",
			W_MUX_UL06, ABE_OPP_50, 0, &mm_ul06_control),
	SND_SOC_DAPM_MUX("MUX_UL07",
			W_MUX_UL07, ABE_OPP_50, 0, &mm_ul07_control),
	SND_SOC_DAPM_MUX("MUX_UL10",
			W_MUX_UL10, ABE_OPP_50, 0, &mm_ul10_control),
	SND_SOC_DAPM_MUX("MUX_UL11",
			W_MUX_UL11, ABE_OPP_50, 0, &mm_ul11_control),
	SND_SOC_DAPM_MUX("MUX_VX0",
			W_MUX_VX00, ABE_OPP_50, 0, &mm_vx0_control),
	SND_SOC_DAPM_MUX("MUX_VX1",
			W_MUX_VX01, ABE_OPP_50, 0, &mm_vx1_control),

	/* DL1 & DL2 Playback Mixers */
	SND_SOC_DAPM_MIXER("DL1 Mixer",
			W_MIXER_DL1, ABE_OPP_25, 0, dl1_mixer_controls,
			ARRAY_SIZE(dl1_mixer_controls)),
#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	SND_SOC_DAPM_MIXER("DL2 Mixer",
			W_MIXER_DL2, ABE_OPP_100, 0, dl2_mixer_controls,
			ARRAY_SIZE(dl2_mixer_controls)),
#endif

	/* DL1 Mixer Input volumes ?????*/
	SND_SOC_DAPM_PGA("DL1 Media Volume",
			W_VOLUME_DL1, 0, 0, NULL, 0),

	/* AUDIO_UL_MIXER */
	SND_SOC_DAPM_MIXER("Voice Capture Mixer",
			W_MIXER_AUDIO_UL, ABE_OPP_50, 0, audio_ul_mixer_controls,
			ARRAY_SIZE(audio_ul_mixer_controls)),

	/* VX_REC_MIXER */
	SND_SOC_DAPM_MIXER("Capture Mixer",
			W_MIXER_VX_REC, ABE_OPP_50, 0, vx_rec_mixer_controls,
			ARRAY_SIZE(vx_rec_mixer_controls)),

	/* SDT_MIXER  - TODO: shoult this not be OPP25 ??? */
	SND_SOC_DAPM_MIXER("Sidetone Mixer",
			W_MIXER_SDT, ABE_OPP_25, 0, sdt_mixer_controls,
			ARRAY_SIZE(sdt_mixer_controls)),

	/*
	 * The Following three are virtual switches to select the output port
	 * after DL1 Gain.
	 */

	/* Virtual PDM_DL1 Switch */
	SND_SOC_DAPM_MIXER("DL1 PDM",
			W_VSWITCH_DL1_PDM1, ABE_OPP_25, 0,
			&pdm_dl1_switch_controls, 1),

#if !defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	/* Virtual PDM_DL2 Switch */
	SND_SOC_DAPM_MIXER("DL1 PDM_DL2",
			W_VSWITCH_DL1_PDM2, ABE_OPP_25, 0,
			&pdm_dl2_switch_controls, 1),
#endif

	/* Virtual BT_VX_DL Switch */
	SND_SOC_DAPM_MIXER("DL1 BT_VX",
			W_VSWITCH_DL1_BT_VX, ABE_OPP_50, 0, &bt_vx_dl_switch_controls, 1),

	/* Virtual MM_EXT_DL Switch TODO: confrm OPP level here */
	SND_SOC_DAPM_MIXER("DL1 MM_EXT",
			W_VSWITCH_DL1_MM_EXT, ABE_OPP_50, 0, &mm_ext_dl_switch_controls, 1),

	/* Virtuals to join our capture sources */
	SND_SOC_DAPM_MIXER("Sidetone Capture VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_MIXER("Voice Capture VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_MIXER("DL1 Capture VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	SND_SOC_DAPM_MIXER("DL2 Capture VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
#endif

	/* Join our MM_DL and MM_DL_LP playback */
	SND_SOC_DAPM_MIXER("MM_DL VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),

	/* Virtual MODEM and VX_UL mixer */
	SND_SOC_DAPM_MIXER("VX UL VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_MIXER("VX DL VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),

	/* Virtual Pins to force backends ON atm */
	SND_SOC_DAPM_OUTPUT("BE_OUT"),
	SND_SOC_DAPM_INPUT("BE_IN"),
};

static const struct snd_soc_dapm_route intercon[] = {

	/* MUX_UL00 - ROUTE_UL - Chan 0  */
	{"MUX_UL00", "DMic0L", "DMIC0"},
	{"MUX_UL00", "DMic0R", "DMIC0"},
	{"MUX_UL00", "DMic1L", "DMIC1"},
	{"MUX_UL00", "DMic1R", "DMIC1"},
	{"MUX_UL00", "DMic2L", "DMIC2"},
	{"MUX_UL00", "DMic2R", "DMIC2"},
	{"MUX_UL00", "BT Left", "BT_VX_UL"},
	{"MUX_UL00", "BT Right", "BT_VX_UL"},
	{"MUX_UL00", "MMExt Left", "MM_EXT_UL"},
	{"MUX_UL00", "MMExt Right", "MM_EXT_UL"},
	{"MUX_UL00", "AMic0", "PDM_UL1"},
	{"MUX_UL00", "AMic1", "PDM_UL1"},
	{"MUX_UL00", "VX Left", "Capture Mixer"},
	{"MUX_UL00", "VX Right", "Capture Mixer"},
	{"MM_UL1", NULL, "MUX_UL00"},

	/* MUX_UL01 - ROUTE_UL - Chan 1  */
	{"MUX_UL01", "DMic0L", "DMIC0"},
	{"MUX_UL01", "DMic0R", "DMIC0"},
	{"MUX_UL01", "DMic1L", "DMIC1"},
	{"MUX_UL01", "DMic1R", "DMIC1"},
	{"MUX_UL01", "DMic2L", "DMIC2"},
	{"MUX_UL01", "DMic2R", "DMIC2"},
	{"MUX_UL01", "BT Left", "BT_VX_UL"},
	{"MUX_UL01", "BT Right", "BT_VX_UL"},
	{"MUX_UL01", "MMExt Left", "MM_EXT_UL"},
	{"MUX_UL01", "MMExt Right", "MM_EXT_UL"},
	{"MUX_UL01", "AMic0", "PDM_UL1"},
	{"MUX_UL01", "AMic1", "PDM_UL1"},
	{"MUX_UL01", "VX Left", "Capture Mixer"},
	{"MUX_UL01", "VX Right", "Capture Mixer"},
	{"MM_UL1", NULL, "MUX_UL01"},

	/* MUX_UL02 - ROUTE_UL - Chan 2  */
	{"MUX_UL02", "DMic0L", "DMIC0"},
	{"MUX_UL02", "DMic0R", "DMIC0"},
	{"MUX_UL02", "DMic1L", "DMIC1"},
	{"MUX_UL02", "DMic1R", "DMIC1"},
	{"MUX_UL02", "DMic2L", "DMIC2"},
	{"MUX_UL02", "DMic2R", "DMIC2"},
	{"MUX_UL02", "BT Left", "BT_VX_UL"},
	{"MUX_UL02", "BT Right", "BT_VX_UL"},
	{"MUX_UL02", "MMExt Left", "MM_EXT_UL"},
	{"MUX_UL02", "MMExt Right", "MM_EXT_UL"},
	{"MUX_UL02", "AMic0", "PDM_UL1"},
	{"MUX_UL02", "AMic1", "PDM_UL1"},
	{"MUX_UL02", "VX Left", "Capture Mixer"},
	{"MUX_UL02", "VX Right", "Capture Mixer"},
	{"MM_UL1", NULL, "MUX_UL02"},

	/* MUX_UL03 - ROUTE_UL - Chan 3  */
	{"MUX_UL03", "DMic0L", "DMIC0"},
	{"MUX_UL03", "DMic0R", "DMIC0"},
	{"MUX_UL03", "DMic1L", "DMIC1"},
	{"MUX_UL03", "DMic1R", "DMIC1"},
	{"MUX_UL03", "DMic2L", "DMIC2"},
	{"MUX_UL03", "DMic2R", "DMIC2"},
	{"MUX_UL03", "BT Left", "BT_VX_UL"},
	{"MUX_UL03", "BT Right", "BT_VX_UL"},
	{"MUX_UL03", "MMExt Left", "MM_EXT_UL"},
	{"MUX_UL03", "MMExt Right", "MM_EXT_UL"},
	{"MUX_UL03", "AMic0", "PDM_UL1"},
	{"MUX_UL03", "AMic1", "PDM_UL1"},
	{"MUX_UL03", "VX Left", "Capture Mixer"},
	{"MUX_UL03", "VX Right", "Capture Mixer"},
	{"MM_UL1", NULL, "MUX_UL03"},

	/* MUX_UL04 - ROUTE_UL - Chan 4  */
	{"MUX_UL04", "DMic0L", "DMIC0"},
	{"MUX_UL04", "DMic0R", "DMIC0"},
	{"MUX_UL04", "DMic1L", "DMIC1"},
	{"MUX_UL04", "DMic1R", "DMIC1"},
	{"MUX_UL04", "DMic2L", "DMIC2"},
	{"MUX_UL04", "DMic2R", "DMIC2"},
	{"MUX_UL04", "BT Left", "BT_VX_UL"},
	{"MUX_UL04", "BT Right", "BT_VX_UL"},
	{"MUX_UL04", "MMExt Left", "MM_EXT_UL"},
	{"MUX_UL04", "MMExt Right", "MM_EXT_UL"},
	{"MUX_UL04", "AMic0", "PDM_UL1"},
	{"MUX_UL04", "AMic1", "PDM_UL1"},
	{"MUX_UL04", "VX Left", "Capture Mixer"},
	{"MUX_UL04", "VX Right", "Capture Mixer"},
	{"MM_UL1", NULL, "MUX_UL04"},

	/* MUX_UL05 - ROUTE_UL - Chan 5  */
	{"MUX_UL05", "DMic0L", "DMIC0"},
	{"MUX_UL05", "DMic0R", "DMIC0"},
	{"MUX_UL05", "DMic1L", "DMIC1"},
	{"MUX_UL05", "DMic1R", "DMIC1"},
	{"MUX_UL05", "DMic2L", "DMIC2"},
	{"MUX_UL05", "DMic2R", "DMIC2"},
	{"MUX_UL05", "BT Left", "BT_VX_UL"},
	{"MUX_UL05", "BT Right", "BT_VX_UL"},
	{"MUX_UL05", "MMExt Left", "MM_EXT_UL"},
	{"MUX_UL05", "MMExt Right", "MM_EXT_UL"},
	{"MUX_UL05", "AMic0", "PDM_UL1"},
	{"MUX_UL05", "AMic1", "PDM_UL1"},
	{"MUX_UL05", "VX Left", "Capture Mixer"},
	{"MUX_UL05", "VX Right", "Capture Mixer"},
	{"MM_UL1", NULL, "MUX_UL05"},

	/* MUX_UL06 - ROUTE_UL - Chan 6  */
	{"MUX_UL06", "DMic0L", "DMIC0"},
	{"MUX_UL06", "DMic0R", "DMIC0"},
	{"MUX_UL06", "DMic1L", "DMIC1"},
	{"MUX_UL06", "DMic1R", "DMIC1"},
	{"MUX_UL06", "DMic2L", "DMIC2"},
	{"MUX_UL06", "DMic2R", "DMIC2"},
	{"MUX_UL06", "BT Left", "BT_VX_UL"},
	{"MUX_UL06", "BT Right", "BT_VX_UL"},
	{"MUX_UL06", "MMExt Left", "MM_EXT_UL"},
	{"MUX_UL06", "MMExt Right", "MM_EXT_UL"},
	{"MUX_UL06", "AMic0", "PDM_UL1"},
	{"MUX_UL06", "AMic1", "PDM_UL1"},
	{"MUX_UL06", "VX Left", "Capture Mixer"},
	{"MUX_UL06", "VX Right", "Capture Mixer"},
	{"MM_UL1", NULL, "MUX_UL06"},

	/* MUX_UL07 - ROUTE_UL - Chan 7  */
	{"MUX_UL07", "DMic0L", "DMIC0"},
	{"MUX_UL07", "DMic0R", "DMIC0"},
	{"MUX_UL07", "DMic1L", "DMIC1"},
	{"MUX_UL07", "DMic1R", "DMIC1"},
	{"MUX_UL07", "DMic2L", "DMIC2"},
	{"MUX_UL07", "DMic2R", "DMIC2"},
	{"MUX_UL07", "BT Left", "BT_VX_UL"},
	{"MUX_UL07", "BT Right", "BT_VX_UL"},
	{"MUX_UL07", "MMExt Left", "MM_EXT_UL"},
	{"MUX_UL07", "MMExt Right", "MM_EXT_UL"},
	{"MUX_UL07", "AMic0", "PDM_UL1"},
	{"MUX_UL07", "AMic1", "PDM_UL1"},
	{"MUX_UL07", "VX Left", "Capture Mixer"},
	{"MUX_UL07", "VX Right", "Capture Mixer"},
	{"MM_UL1", NULL, "MUX_UL07"},

	/* MUX_UL10 - ROUTE_UL - Chan 10  */
	{"MUX_UL10", "DMic0L", "DMIC0"},
	{"MUX_UL10", "DMic0R", "DMIC0"},
	{"MUX_UL10", "DMic1L", "DMIC1"},
	{"MUX_UL10", "DMic1R", "DMIC1"},
	{"MUX_UL10", "DMic2L", "DMIC2"},
	{"MUX_UL10", "DMic2R", "DMIC2"},
	{"MUX_UL10", "BT Left", "BT_VX_UL"},
	{"MUX_UL10", "BT Right", "BT_VX_UL"},
	{"MUX_UL10", "MMExt Left", "MM_EXT_UL"},
	{"MUX_UL10", "MMExt Right", "MM_EXT_UL"},
	{"MUX_UL10", "AMic0", "PDM_UL1"},
	{"MUX_UL10", "AMic1", "PDM_UL1"},
	{"MUX_UL10", "VX Left", "Capture Mixer"},
	{"MUX_UL10", "VX Right", "Capture Mixer"},
	{"MM_UL2", NULL, "MUX_UL10"},

	/* MUX_UL11 - ROUTE_UL - Chan 11  */
	{"MUX_UL11", "DMic0L", "DMIC0"},
	{"MUX_UL11", "DMic0R", "DMIC0"},
	{"MUX_UL11", "DMic1L", "DMIC1"},
	{"MUX_UL11", "DMic1R", "DMIC1"},
	{"MUX_UL11", "DMic2L", "DMIC2"},
	{"MUX_UL11", "DMic2R", "DMIC2"},
	{"MUX_UL11", "BT Left", "BT_VX_UL"},
	{"MUX_UL11", "BT Right", "BT_VX_UL"},
	{"MUX_UL11", "MMExt Left", "MM_EXT_UL"},
	{"MUX_UL11", "MMExt Right", "MM_EXT_UL"},
	{"MUX_UL11", "AMic0", "PDM_UL1"},
	{"MUX_UL11", "AMic1", "PDM_UL1"},
	{"MUX_UL11", "VX Left", "Capture Mixer"},
	{"MUX_UL11", "VX Right", "Capture Mixer"},
	{"MM_UL2", NULL, "MUX_UL11"},

	/* MUX_VX0 - ROUTE_UL - Chan 20  */
	{"MUX_VX0", "DMic0L", "DMIC0"},
	{"MUX_VX0", "DMic0R", "DMIC0"},
	{"MUX_VX0", "DMic1L", "DMIC1"},
	{"MUX_VX0", "DMic1R", "DMIC1"},
	{"MUX_VX0", "DMic2L", "DMIC2"},
	{"MUX_VX0", "DMic2R", "DMIC2"},
	{"MUX_VX0", "BT Left", "BT_VX_UL"},
	{"MUX_VX0", "BT Right", "BT_VX_UL"},
	{"MUX_VX0", "MMExt Left", "MM_EXT_UL"},
	{"MUX_VX0", "MMExt Right", "MM_EXT_UL"},
	{"MUX_VX0", "AMic0", "PDM_UL1"},
	{"MUX_VX0", "AMic1", "PDM_UL1"},
	{"MUX_VX0", "VX Left", "Capture Mixer"},
	{"MUX_VX0", "VX Right", "Capture Mixer"},

	/* MUX_VX1 - ROUTE_UL - Chan 20  */
	{"MUX_VX1", "DMic0L", "DMIC0"},
	{"MUX_VX1", "DMic0R", "DMIC0"},
	{"MUX_VX1", "DMic1L", "DMIC1"},
	{"MUX_VX1", "DMic1R", "DMIC1"},
	{"MUX_VX1", "DMic2L", "DMIC2"},
	{"MUX_VX1", "DMic2R", "DMIC2"},
	{"MUX_VX1", "BT Left", "BT_VX_UL"},
	{"MUX_VX1", "BT Right", "BT_VX_UL"},
	{"MUX_VX1", "MMExt Left", "MM_EXT_UL"},
	{"MUX_VX1", "MMExt Right", "MM_EXT_UL"},
	{"MUX_VX1", "AMic0", "PDM_UL1"},
	{"MUX_VX1", "AMic1", "PDM_UL1"},
	{"MUX_VX1", "VX Left", "Capture Mixer"},
	{"MUX_VX1", "VX Right", "Capture Mixer"},

	/* Headset (DL1)  playback path */
	{"DL1 Mixer", "Tones", "TONES_DL"},
	{"DL1 Mixer", "Voice", "VX DL VMixer"},
	{"DL1 Mixer", "Capture", "DL1 Capture VMixer"},
	{"DL1 Capture VMixer", NULL, "MUX_UL10"},
	{"DL1 Capture VMixer", NULL, "MUX_UL11"},
	{"DL1 Mixer", "Multimedia", "MM_DL VMixer"},
	{"MM_DL VMixer", NULL, "MM_DL"},
	{"MM_DL VMixer", NULL, "MM_DL_LP"},

	/* Sidetone Mixer */
	{"Sidetone Mixer", "Playback", "DL1 Mixer"},
	{"Sidetone Mixer", "Capture", "Sidetone Capture VMixer"},
	{"Sidetone Capture VMixer", NULL, "MUX_VX0"},
	{"Sidetone Capture VMixer", NULL, "MUX_VX1"},

	/* Playback Output selection after DL1 Gain */
	{"DL1 BT_VX", "Switch", "Sidetone Mixer"},
	{"DL1 MM_EXT", "Switch", "Sidetone Mixer"},
	{"DL1 PDM", "Switch", "Sidetone Mixer"},
	{"PDM_DL1", NULL, "DL1 PDM"},
	{"BT_VX_DL", NULL, "DL1 BT_VX"},
	{"MM_EXT_DL", NULL, "DL1 MM_EXT"},

	/* Handsfree (DL2) playback path */
#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	{"DL2 Mixer", "Tones", "TONES_DL"},
	{"DL2 Mixer", "Voice", "VX DL VMixer"},
	{"DL2 Mixer", "Capture", "DL2 Capture VMixer"},
	{"DL2 Capture VMixer", NULL, "MUX_UL10"},
	{"DL2 Capture VMixer", NULL, "MUX_UL11"},
	{"DL2 Mixer", "Multimedia", "MM_DL VMixer"},
	{"MM_DL VMixer", NULL, "MM_DL"},
	{"MM_DL VMixer", NULL, "MM_DL_LP"},
	{"PDM_DL2", NULL, "DL2 Mixer"},
#else
	{"DL1 PDM_DL2", "Switch", "Sidetone Mixer"},
	{"PDM_DL2", NULL, "DL1 PDM_DL2"},
#endif

	/* VxREC Mixer */
	{"Capture Mixer", "Tones", "VXREC"},
	{"Capture Mixer", "Voice Playback", "VXREC"},
	{"Capture Mixer", "Voice Capture", "VXREC"},
	{"Capture Mixer", "Media Playback", "VXREC"},
	{"MM_DL VMixer", NULL, "MM_DL"},
	{"MM_DL VMixer", NULL, "MM_DL_LP"},

	/* Audio UL mixer */
	{"Voice Capture Mixer", "Tones Playback", "TONES_DL"},
	{"Voice Capture Mixer", "Media Playback", "MM_DL VMixer"},
	{"MM_DL VMixer", NULL, "MM_DL"},
	{"MM_DL VMixer", NULL, "MM_DL_LP"},
	{"Voice Capture Mixer", "Capture", "Voice Capture VMixer"},
	{"Voice Capture VMixer", NULL, "MUX_VX0"},
	{"Voice Capture VMixer", NULL, "MUX_VX1"},

	/* BT */
	{"VX UL VMixer", NULL, "Voice Capture Mixer"},

	/* Vibra */
	{"PDM_VIB", NULL, "VIB_DL"},

	/* VX and MODEM */
	{"VX_UL", NULL, "VX UL VMixer"},
	{"MODEM_UL", NULL, "VX UL VMixer"},
	{"VX DL VMixer", NULL, "VX_DL"},
	{"VX DL VMixer", NULL, "MODEM_DL"},

	/* Backend Enablement - TODO: maybe re-work*/
	{"BE_OUT", NULL, "PDM_DL1"},
	{"BE_OUT", NULL, "PDM_DL2"},
	{"BE_OUT", NULL, "PDM_VIB"},
	{"BE_OUT", NULL, "MM_EXT_DL"},
	{"BE_OUT", NULL, "BT_VX_DL"},
	{"PDM_UL1", NULL, "BE_IN"},
	{"BT_VX_UL", NULL, "BE_IN"},
	{"MM_EXT_UL", NULL, "BE_IN"},
	{"DMIC0", NULL, "BE_IN"},
	{"DMIC1", NULL, "BE_IN"},
	{"DMIC2", NULL, "BE_IN"},
	{"VXREC", NULL, "BE_IN"},
};

#ifdef CONFIG_DEBUG_FS

static int abe_dbg_get_dma_pos(struct abe_data *abe)
{
	return omap_get_dma_dst_pos(abe->dma_ch) - abe->dbg_buffer_addr;
}

static void abe_dbg_dma_irq(int ch, u16 stat, void *data)
{
}

static int abe_dbg_start_dma(struct abe_data *abe, int circular)
{
	struct omap_dma_channel_params dma_params;
	int err;

	/* TODO: start the DMA in either :-
	 *
	 * 1) circular buffer mode where the DMA will restart when it get to
	 *    the end of the buffer.
	 * 2) default mode, where DMA stops at the end of the buffer.
	 */

	abe->dma_req = OMAP44XX_DMA_ABE_REQ_7;
	err = omap_request_dma(abe->dma_req, "ABE debug",
			       abe_dbg_dma_irq, abe, &abe->dma_ch);
	if (abe->dbg_circular) {
		/*
		 * Link channel with itself so DMA doesn't need any
		 * reprogramming while looping the buffer
		 */
		omap_dma_link_lch(abe->dma_ch, abe->dma_ch);
	}

	memset(&dma_params, 0, sizeof(dma_params));
	dma_params.data_type = OMAP_DMA_DATA_TYPE_S32;
	dma_params.trigger = abe->dma_req;
	dma_params.sync_mode = OMAP_DMA_SYNC_FRAME;
	dma_params.src_amode = OMAP_DMA_AMODE_DOUBLE_IDX;
	dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
	dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC;
	dma_params.src_start = D_DEBUG_FIFO_ADDR + ABE_DMEM_BASE_ADDRESS_L3;
	dma_params.dst_start = abe->dbg_buffer_addr;
	dma_params.src_port = OMAP_DMA_PORT_MPUI;
	dma_params.src_ei = 1;
	dma_params.src_fi = 1 - abe->dbg_elem_bytes;

	dma_params.elem_count = abe->dbg_elem_bytes >> 2; /* 128 bytes shifted into words */
	dma_params.frame_count = abe->dbg_buffer_bytes / abe->dbg_elem_bytes;
	omap_set_dma_params(abe->dma_ch, &dma_params);

	omap_enable_dma_irq(abe->dma_ch, OMAP_DMA_FRAME_IRQ);
	omap_set_dma_src_burst_mode(abe->dma_ch, OMAP_DMA_DATA_BURST_16);
	omap_set_dma_dest_burst_mode(abe->dma_ch, OMAP_DMA_DATA_BURST_16);

	abe->dbg_reader_offset = 0;

	pm_runtime_get_sync(abe->dev);
	omap_start_dma(abe->dma_ch);
	return 0;
}

static void abe_dbg_stop_dma(struct abe_data *abe)
{
	while (omap_get_dma_active_status(abe->dma_ch))
		omap_stop_dma(abe->dma_ch);

	if (abe->dbg_circular)
		omap_dma_unlink_lch(abe->dma_ch, abe->dma_ch);
	omap_free_dma(abe->dma_ch);
	pm_runtime_put_sync(abe->dev);
}

static int abe_open_data(struct inode *inode, struct file *file)
{
	struct abe_data *abe = inode->i_private;

	abe->dbg_elem_bytes = 128; /* size of debug data per tick */

	if (abe->dbg_format1)
		abe->dbg_elem_bytes += ABE_DBG_FLAG1_SIZE;
	if (abe->dbg_format2)
		abe->dbg_elem_bytes += ABE_DBG_FLAG2_SIZE;
	if (abe->dbg_format3)
		abe->dbg_elem_bytes += ABE_DBG_FLAG3_SIZE;

	abe->dbg_buffer_bytes = abe->dbg_elem_bytes * 4 *
							abe->dbg_buffer_msecs;

	abe->dbg_buffer = dma_alloc_writecombine(abe->dev,
			abe->dbg_buffer_bytes, &abe->dbg_buffer_addr, GFP_KERNEL);
	if (abe->dbg_buffer == NULL)
		return -ENOMEM;

	file->private_data = inode->i_private;
	abe->dbg_complete = 0;
	abe_dbg_start_dma(abe, abe->dbg_circular);

	return 0;
}

static int abe_release_data(struct inode *inode, struct file *file)
{
	struct abe_data *abe = inode->i_private;

	abe_dbg_stop_dma(abe);

	dma_free_writecombine(abe->dev, abe->dbg_buffer_bytes,
				      abe->dbg_buffer, abe->dbg_buffer_addr);
	return 0;
}

static ssize_t abe_copy_to_user(struct abe_data *abe, char __user *user_buf,
			       size_t count)
{
	/* check for reader buffer wrap */
	if (abe->dbg_reader_offset + count > abe->dbg_buffer_bytes) {
		int size = abe->dbg_buffer_bytes - abe->dbg_reader_offset;

		/* wrap */
		if (copy_to_user(user_buf,
			abe->dbg_buffer + abe->dbg_reader_offset, size))
			return -EFAULT;

		/* need to just return if non circular */
		if (!abe->dbg_circular) {
			abe->dbg_complete = 1;
			return count;
		}

		if (copy_to_user(user_buf,
			abe->dbg_buffer, count - size))
			return -EFAULT;
		abe->dbg_reader_offset = count - size;
		return count;
	} else {
		/* no wrap */
		if (copy_to_user(user_buf,
			abe->dbg_buffer + abe->dbg_reader_offset, count))
			return -EFAULT;
		abe->dbg_reader_offset += count;

		if (!abe->dbg_circular &&
				abe->dbg_reader_offset == abe->dbg_buffer_bytes)
			abe->dbg_complete = 1;

		return count;
	}
}

static ssize_t abe_read_data(struct file *file, char __user *user_buf,
			       size_t count, loff_t *ppos)
{
	ssize_t ret = 0;
	struct abe_data *abe = file->private_data;
	DECLARE_WAITQUEUE(wait, current);
	int dma_offset, bytes;

	add_wait_queue(&abe->wait, &wait);
	do {
		set_current_state(TASK_INTERRUPTIBLE);
		/* TODO: Check if really needed. Or adjust sleep delay
		 * If not delay trace is not working */
		msleep_interruptible(1);
		dma_offset = abe_dbg_get_dma_pos(abe);

		/* is DMA finished ? */
		if (abe->dbg_complete)
			break;

		/* get maximum amount of debug bytes we can read */
		if (dma_offset >= abe->dbg_reader_offset) {
			/* dma ptr is ahead of reader */
			bytes = dma_offset - abe->dbg_reader_offset;
		} else {
			/* dma ptr is behind reader */
			bytes = dma_offset + abe->dbg_buffer_bytes -
				abe->dbg_reader_offset;
		}

		if (count > bytes)
			count = bytes;

		if (count > 0) {
			ret = abe_copy_to_user(abe, user_buf, count);
			break;
		}

		if (file->f_flags & O_NONBLOCK) {
			ret = -EAGAIN;
			break;
		}

		if (signal_pending(current)) {
			ret = -ERESTARTSYS;
			break;
		}

		schedule();

	} while (1);

	__set_current_state(TASK_RUNNING);
	remove_wait_queue(&abe->wait, &wait);

	return ret;
}

static const struct file_operations abe_data_fops = {
	.open = abe_open_data,
	.read = abe_read_data,
	.release = abe_release_data,
};

static void abe_init_debugfs(struct abe_data *abe)
{
	abe->debugfs_root = debugfs_create_dir("omap4-abe", NULL);
	if (!abe->debugfs_root) {
		printk(KERN_WARNING "ABE: Failed to create debugfs directory\n");
		return;
	}

	abe->debugfs_fmt1 = debugfs_create_bool("format1", 0644,
						 abe->debugfs_root,
						 &abe->dbg_format1);
	if (!abe->debugfs_fmt1)
		printk(KERN_WARNING "ABE: Failed to create format1 debugfs file\n");

	abe->debugfs_fmt2 = debugfs_create_bool("format2", 0644,
						 abe->debugfs_root,
						 &abe->dbg_format2);
	if (!abe->debugfs_fmt2)
		printk(KERN_WARNING "ABE: Failed to create format2 debugfs file\n");

	abe->debugfs_fmt3 = debugfs_create_bool("format3", 0644,
						 abe->debugfs_root,
						 &abe->dbg_format3);
	if (!abe->debugfs_fmt3)
		printk(KERN_WARNING "ABE: Failed to create format3 debugfs file\n");

	abe->debugfs_elem_bytes = debugfs_create_u32("element_bytes", 0604,
						 abe->debugfs_root,
						 &abe->dbg_elem_bytes);
	if (!abe->debugfs_elem_bytes)
		printk(KERN_WARNING "ABE: Failed to create element size debugfs file\n");

	abe->debugfs_size = debugfs_create_u32("msecs", 0644,
						 abe->debugfs_root,
						 &abe->dbg_buffer_msecs);
	if (!abe->debugfs_size)
		printk(KERN_WARNING "ABE: Failed to create buffer size debugfs file\n");

	abe->debugfs_circ = debugfs_create_bool("circular", 0644,
						 abe->debugfs_root,
						 &abe->dbg_circular);
	if (!abe->debugfs_size)
		printk(KERN_WARNING "ABE: Failed to create circular mode debugfs file\n");

	abe->debugfs_data = debugfs_create_file("debug", 0644,
						 abe->debugfs_root,
						 abe, &abe_data_fops);
	if (!abe->debugfs_data)
		printk(KERN_WARNING "ABE: Failed to create data debugfs file\n");

	abe->debugfs_opp_level = debugfs_create_u32("opp_level", 0604,
						 abe->debugfs_root,
						 &abe->opp);
	if (!abe->debugfs_opp_level)
		printk(KERN_WARNING "ABE: Failed to create OPP level debugfs file\n");

	abe->dbg_buffer_msecs = 500;
	init_waitqueue_head(&abe->wait);
}

static void abe_cleanup_debugfs(struct abe_data *abe)
{
	debugfs_remove_recursive(abe->debugfs_root);
}

#else

static inline void abe_init_debugfs(struct abe_data *abe)
{
}

static inline void abe_cleanup_debugfs(struct abe_data *abe)
{
}
#endif

static const struct snd_pcm_hardware omap_abe_hardware = {
	.info			= SNDRV_PCM_INFO_MMAP |
				  SNDRV_PCM_INFO_MMAP_VALID |
				  SNDRV_PCM_INFO_INTERLEAVED |
				  SNDRV_PCM_INFO_BLOCK_TRANSFER |
				  SNDRV_PCM_INFO_PAUSE |
				  SNDRV_PCM_INFO_RESUME,
	.formats		= SNDRV_PCM_FMTBIT_S16_LE |
				  SNDRV_PCM_FMTBIT_S32_LE,
	.period_bytes_min	= 4 * 1024,
	.period_bytes_max	= 24 * 1024,
	.periods_min		= 4,
	.periods_max		= 4,
	.buffer_bytes_max	= 24 * 1024 * 2,
};

static struct abe_opp_req *abe_opp_req_lookup(struct abe_data *abe,
					struct device *dev)
{
	struct abe_opp_req *req, *tmp_req;

	req = NULL;
	list_for_each_entry(tmp_req, &abe->opp_req, node) {
		if (tmp_req->dev == dev) {
			req = tmp_req;
			break;
		}
	}

	return req;
}

static int abe_get_opp_req(struct abe_data *abe)
{
	struct abe_opp_req *req;
	int opp = 0;

	list_for_each_entry(req, &abe->opp_req, node)
		opp |= req->opp;

	opp = (1 << (fls(opp) - 1)) * 25;

	return opp;
}

int abe_add_opp_req(struct device *dev, int opp)
{
	struct abe_opp_req *req;
	int ret = 0;

	mutex_lock(&the_abe->opp_req_mutex);

	req = abe_opp_req_lookup(the_abe, dev);
	if (!req) {
		req = kzalloc(sizeof(struct abe_opp_req), GFP_KERNEL);
		if (!req) {
			ret = -ENOMEM;
			goto out;
		}
		req->dev = dev;
		/* use the same convention as ABE DSP DAPM */
		req->opp = 1 << opp;
		list_add(&req->node, &the_abe->opp_req);
		the_abe->opp_req_count++;
	} else {
		req->opp = opp;
	}

	aess_set_runtime_opp_level(the_abe);

out:
	mutex_unlock(&the_abe->opp_req_mutex);
	return ret;
}
EXPORT_SYMBOL(abe_add_opp_req);

int abe_remove_opp_req(struct device *dev)
{
	struct abe_opp_req *req;
	int ret = 0;

	mutex_lock(&the_abe->opp_req_mutex);

	req = abe_opp_req_lookup(the_abe, dev);
	if (!req) {
		dev_err(dev, "trying to remove an invalid opp req\n");
		ret = -EINVAL;
		goto out;
	}

	list_del(&req->node);
	the_abe->opp_req_count--;
	kfree(req);

	aess_set_runtime_opp_level(the_abe);

out:
	mutex_unlock(&the_abe->opp_req_mutex);
	return ret;
}
EXPORT_SYMBOL(abe_remove_opp_req);

static int abe_set_opp_mode(struct abe_data *abe, int opp)
{
	struct omap4_abe_dsp_pdata *pdata = abe->abe_pdata;
	int ret = 0;

	if (abe->opp > opp) {
		/* Decrease OPP mode - no need of OPP100% */
		switch (opp) {
		case 25:
			abe_set_opp_processing(ABE_OPP25);
			udelay(250);
			if (pdata && pdata->device_scale) {
				ret = pdata->device_scale(abe->dev, abe->dev,
					abe->opp_freqs[OMAP_ABE_OPP25]);
				if (ret)
					goto err_scale;
			}
			break;
		case 50:
		default:
			abe_set_opp_processing(ABE_OPP50);
			udelay(250);
			if (pdata && pdata->device_scale) {
				ret = pdata->device_scale(abe->dev, abe->dev,
					abe->opp_freqs[OMAP_ABE_OPP50]);
				if (ret)
					goto err_scale;
			}
			break;
		}
	} else if (abe->opp < opp) {
		/* Increase OPP mode */
		switch (opp) {
		case 25:
			if (pdata && pdata->device_scale) {
				pdata->device_scale(abe->dev, abe->dev,
					abe->opp_freqs[OMAP_ABE_OPP25]);
				if (ret)
					goto err_scale;
			}
			abe_set_opp_processing(ABE_OPP25);
			break;
		case 50:
			if (pdata && pdata->device_scale) {
				ret = pdata->device_scale(abe->dev, abe->dev,
					abe->opp_freqs[OMAP_ABE_OPP50]);
				if (ret)
					goto err_scale;
			}
			abe_set_opp_processing(ABE_OPP50);
			break;
		case 100:
		default:
			if (pdata && pdata->device_scale) {
				ret = pdata->device_scale(abe->dev, abe->dev,
					abe->opp_freqs[OMAP_ABE_OPP100]);
				if (ret)
					goto err_scale;
			}
			abe_set_opp_processing(ABE_OPP100);
			break;
		}
	}
	abe->opp = opp;
	dev_dbg(abe->dev, "new OPP level is %d\n", opp);

	return 0;

err_scale:
	dev_err(abe->dev, "failed to scale to OPP%d\n", opp);
	return ret;
}

static int aess_set_runtime_opp_level(struct abe_data *abe)
{
	int i, req_opp, opp = 0;

	mutex_lock(&abe->opp_mutex);

	/* now calculate OPP level based upon DAPM widget status */
	for (i = 0; i < ABE_NUM_WIDGETS; i++) {
		if (abe->widget_opp[ABE_WIDGET(i)]) {
			dev_dbg(abe->dev, "OPP: id %d = %d%%\n", i,
					abe->widget_opp[ABE_WIDGET(i)] * 25);
			opp |= abe->widget_opp[ABE_WIDGET(i)];
		}
	}
	opp = (1 << (fls(opp) - 1)) * 25;

	/* opps requested outside ABE DSP driver (e.g. McPDM) */
	req_opp = abe_get_opp_req(abe);

	pm_runtime_get_sync(abe->dev);
	abe_set_opp_mode(abe, max(opp, req_opp));
	pm_runtime_put_sync(abe->dev);

	mutex_unlock(&abe->opp_mutex);

	return 0;
}

#ifdef CONFIG_OMAP4_DPLL_CASCADING
static int abe_fe_active_count(struct abe_data *abe)
{
	int i, count = 0;

	for (i = 0; i < ABE_NUM_FE; i++)
		count += abe->fe_active[i];

	return count;
}

static int abe_fe_event(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	int index, active, ret = 0;

	if ((w->reg < ABE_FE_START) || (w->reg >= ABE_FE_END))
		return -EINVAL;

	index = w->reg - ABE_FE_START;

	if (SND_SOC_DAPM_EVENT_ON(event)) {
		the_abe->fe_active[index]++;
		active = abe_fe_active_count(the_abe);

		if (!the_abe->early_suspended || (active > 1))
			ret = omap4_dpll_cascading_blocker_hold(the_abe->dev);
	} else {
		the_abe->fe_active[index]--;
	}

	return ret;
}

bool omap4_abe_can_enter_dpll_cascading(void)
{
	return abe_can_enter_dpll_cascading;
}
#endif

static void abe_dsp_init_gains(struct abe_data *abe)
{
	/* Uplink gains */
	abe_mute_gain(MIXAUDUL, MIX_AUDUL_INPUT_MM_DL);
	abe_mute_gain(MIXAUDUL, MIX_AUDUL_INPUT_TONES);
	abe_mute_gain(MIXAUDUL, MIX_AUDUL_INPUT_UPLINK);
	abe_mute_gain(MIXAUDUL, MIX_AUDUL_INPUT_VX_DL);

	abe_mute_gain(MIXVXREC, MIX_VXREC_INPUT_TONES);
	abe_mute_gain(MIXVXREC, MIX_VXREC_INPUT_VX_DL);
	abe_mute_gain(MIXVXREC, MIX_VXREC_INPUT_MM_DL);
	abe_mute_gain(MIXVXREC, MIX_VXREC_INPUT_VX_UL);

	abe_mute_gain(GAINS_DMIC1, GAIN_LEFT_OFFSET);
	abe_mute_gain(GAINS_DMIC1, GAIN_RIGHT_OFFSET);
	abe_mute_gain(GAINS_DMIC2, GAIN_LEFT_OFFSET);
	abe_mute_gain(GAINS_DMIC2, GAIN_RIGHT_OFFSET);
	abe_mute_gain(GAINS_DMIC3, GAIN_LEFT_OFFSET);
	abe_mute_gain(GAINS_DMIC3, GAIN_RIGHT_OFFSET);

	abe_mute_gain(GAINS_AMIC, GAIN_LEFT_OFFSET);
	abe_mute_gain(GAINS_AMIC, GAIN_RIGHT_OFFSET);

	abe_mute_gain(GAINS_BTUL, GAIN_LEFT_OFFSET);
	abe_mute_gain(GAINS_BTUL, GAIN_RIGHT_OFFSET);

	/* Downlink gains */
	abe_write_gain(GAINS_DL1, GAIN_0dB, RAMP_2MS, GAIN_LEFT_OFFSET);
	abe_write_gain(GAINS_DL1, GAIN_0dB, RAMP_2MS, GAIN_RIGHT_OFFSET);
	abe_mute_gain(GAINS_DL1, GAIN_LEFT_OFFSET);
	abe_mute_gain(GAINS_DL1, GAIN_RIGHT_OFFSET);

	abe_write_gain(GAINS_DL2, GAIN_M7dB, RAMP_2MS, GAIN_LEFT_OFFSET);
	abe_write_gain(GAINS_DL2, GAIN_M7dB, RAMP_2MS, GAIN_RIGHT_OFFSET);
	abe_mute_gain(GAINS_DL2, GAIN_LEFT_OFFSET);
	abe_mute_gain(GAINS_DL2, GAIN_RIGHT_OFFSET);

	abe_mute_gain(MIXDL1, MIX_DL1_INPUT_MM_DL);
	abe_mute_gain(MIXDL1, MIX_DL1_INPUT_MM_UL2);
	abe_mute_gain(MIXDL1, MIX_DL1_INPUT_VX_DL);
	abe_mute_gain(MIXDL1, MIX_DL1_INPUT_TONES);

	abe_mute_gain(MIXDL2, MIX_DL2_INPUT_TONES);
	abe_mute_gain(MIXDL2, MIX_DL2_INPUT_VX_DL);
	abe_mute_gain(MIXDL2, MIX_DL2_INPUT_MM_DL);
	abe_mute_gain(MIXDL2, MIX_DL2_INPUT_MM_UL2);

	abe_mute_gain(MIXECHO, MIX_ECHO_DL1);
	abe_mute_gain(MIXECHO, MIX_ECHO_DL2);

	/* Sidetone gains */
	abe_mute_gain(MIXSDT, MIX_SDT_INPUT_UP_MIXER);
	abe_mute_gain(MIXSDT, MIX_SDT_INPUT_DL1_MIXER);
}

static int aess_save_context(struct abe_data *abe)
{
	/* mute gains not associated with FEs/BEs */
	abe_mute_gain(MIXAUDUL, MIX_AUDUL_INPUT_MM_DL);
	abe_mute_gain(MIXAUDUL, MIX_AUDUL_INPUT_TONES);
	abe_mute_gain(MIXAUDUL, MIX_AUDUL_INPUT_VX_DL);
	abe_mute_gain(MIXVXREC, MIX_VXREC_INPUT_TONES);
	abe_mute_gain(MIXVXREC, MIX_VXREC_INPUT_VX_DL);
	abe_mute_gain(MIXVXREC, MIX_VXREC_INPUT_MM_DL);
	abe_mute_gain(MIXVXREC, MIX_VXREC_INPUT_VX_UL);
	abe_mute_gain(MIXECHO, MIX_ECHO_DL1);
	abe_mute_gain(MIXECHO, MIX_ECHO_DL2);

	/* mute gains associated with DL1 BE
	 * ideally, these gains should be muted/saved when BE is muted, but
	 * when ABE McPDM is started for DL1 or DL2, PDM_DL1 port gets enabled
	 * which prevents to mute these gains since two ports on DL1 path are
	 * active when mute is called for BT_VX_DL or MM_EXT_DL.
	 *
	 * These gains are not restored along with the context because they
	 * are properly unmuted/restored when any of the DL1 BEs is unmuted
	*/
	abe_mute_gain(GAINS_DL1, GAIN_LEFT_OFFSET);
	abe_mute_gain(GAINS_DL1, GAIN_RIGHT_OFFSET);
	abe_mute_gain(MIXSDT, MIX_SDT_INPUT_DL1_MIXER);

	return 0;
}

static int aess_restore_context(struct abe_data *abe)
{
	struct omap4_abe_dsp_pdata *pdata = abe->abe_pdata;
	int i, ret;

	if (pdata && pdata->device_scale) {
		ret = pdata->device_scale(the_abe->dev, the_abe->dev,
				abe->opp_freqs[OMAP_ABE_OPP50]);
		if (ret) {
			dev_err(abe->dev, "failed to scale to OPP50\n");
			return ret;
		}
	}

	if (pdata->was_context_lost && pdata->was_context_lost(abe->dev))
		abe_reload_fw(abe->firmware);

#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	abe_write_select_pdm_output(1);
#else
	abe_write_select_pdm_output(3);
#endif

	/* unmute gains not associated with FEs/BEs */
	abe_unmute_gain(MIXAUDUL, MIX_AUDUL_INPUT_MM_DL);
	abe_unmute_gain(MIXAUDUL, MIX_AUDUL_INPUT_TONES);
	abe_unmute_gain(MIXAUDUL, MIX_AUDUL_INPUT_VX_DL);
	abe_unmute_gain(MIXVXREC, MIX_VXREC_INPUT_TONES);
	abe_unmute_gain(MIXVXREC, MIX_VXREC_INPUT_VX_DL);
	abe_unmute_gain(MIXVXREC, MIX_VXREC_INPUT_MM_DL);
	abe_unmute_gain(MIXVXREC, MIX_VXREC_INPUT_VX_UL);
	abe_unmute_gain(MIXECHO, MIX_ECHO_DL1);
	abe_unmute_gain(MIXECHO, MIX_ECHO_DL2);

	abe_set_router_configuration(UPROUTE, 0, (u32 *)abe->router);

	/* DC offset cancellation setting */
	if (abe->power_mode)
		abe_write_pdmdl_offset(1, abe->dc_hsl * 2, abe->dc_hsr * 2);
	else
		abe_write_pdmdl_offset(1, abe->dc_hsl, abe->dc_hsr);

	abe_write_pdmdl_offset(2, abe->dc_hfl, abe->dc_hfr);

	for (i = 0; i < abe->hdr.num_equ; i++)
		abe_dsp_set_equalizer(i, abe->equ_profile[i]);

	for (i = 0; i < ABE_NUM_MONO_MIXERS; i++)
		abe_dsp_set_mono_mixer(MIX_DL1_MONO + i, abe->mono_mix[i]);

       return 0;
}

static int aess_open(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_platform *platform = rtd->platform;
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);
	struct snd_soc_dai *dai = rtd->cpu_dai;
	int ret = 0;

	mutex_lock(&abe->mutex);

	dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);

	pm_runtime_get_sync(abe->dev);

	if (!abe->active++) {
		abe->opp = 0;
		aess_restore_context(abe);
		abe_wakeup();
	}

	switch (dai->id) {
	case ABE_FRONTEND_DAI_MODEM:
		break;
	case ABE_FRONTEND_DAI_LP_MEDIA:
		snd_soc_set_runtime_hwparams(substream, &omap_abe_hardware);
		ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
					 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 1024);
		break;
	default:
		break;
	}

	mutex_unlock(&abe->mutex);
	return ret;
}

static int aess_hw_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_soc_platform *platform = rtd->platform;
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);
	struct snd_soc_dai *dai = rtd->cpu_dai;
	abe_data_format_t format;
	size_t period_size;
	u32 dst;

	mutex_lock(&abe->mutex);

	dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);

	if (dai->id != ABE_FRONTEND_DAI_LP_MEDIA)
		goto out;

	/*Storing substream pointer for irq*/
	abe->ping_pong_substream = substream;

	format.f = params_rate(params);
	if (params_format(params) == SNDRV_PCM_FORMAT_S32_LE)
		format.samp_format = STEREO_MSB;
	else
		format.samp_format = STEREO_16_16;

	period_size = params_period_bytes(params);

	/*Adding ping pong buffer subroutine*/
	abe_plug_subroutine(&abe_irq_pingpong_player_id,
				(abe_subroutine2) abe_irq_pingpong_subroutine,
				SUB_1_PARAM, (u32 *)abe);

	/* Connect a Ping-Pong cache-flush protocol to MM_DL port */
	abe_connect_irq_ping_pong_port(MM_DL_PORT, &format,
				abe_irq_pingpong_player_id,
				period_size, &dst,
				PING_PONG_WITH_MCU_IRQ);

	/* Memory mapping for hw params */
	runtime->dma_area  = abe->io_base[0] + dst;
	runtime->dma_addr  = 0;
	runtime->dma_bytes = period_size * 4;

	/* Need to set the first buffer in order to get interrupt */
	abe_set_ping_pong_buffer(MM_DL_PORT, period_size);
	abe->first_irq = 1;

out:
	mutex_unlock(&abe->mutex);
	return 0;
}

static int aess_prepare(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_platform *platform = rtd->platform;
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);
	struct snd_soc_dai *dai = rtd->cpu_dai;

	mutex_lock(&abe->mutex);
	dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
	aess_set_runtime_opp_level(abe);
	mutex_unlock(&abe->mutex);
	return 0;
}

static int aess_close(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_platform *platform = rtd->platform;
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);
	struct snd_soc_dai *dai = rtd->cpu_dai;

	mutex_lock(&abe->mutex);

	dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);

	if (!--abe->active) {
		abe_disable_irq();
		aess_save_context(abe);
		abe_dsp_shutdown();
	} else {
		/* Only scale OPP level
		 * if ABE is still active */
		aess_set_runtime_opp_level(abe);
	}

	pm_runtime_put_sync(abe->dev);

	mutex_unlock(&abe->mutex);
	return 0;
}

static int aess_mmap(struct snd_pcm_substream *substream,
	struct vm_area_struct *vma)
{
	struct snd_soc_pcm_runtime  *rtd = substream->private_data;
	struct snd_soc_dai *dai = rtd->cpu_dai;
	int offset, size, err;

	if (dai->id != ABE_FRONTEND_DAI_LP_MEDIA)
		return -EINVAL;

	vma->vm_flags |= VM_IO | VM_RESERVED;
	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
	size = vma->vm_end - vma->vm_start;
	offset = vma->vm_pgoff << PAGE_SHIFT;

	err = io_remap_pfn_range(vma, vma->vm_start,
			(ABE_DMEM_BASE_ADDRESS_MPU +
			ABE_DMEM_BASE_OFFSET_PING_PONG + offset) >> PAGE_SHIFT,
			size, vma->vm_page_prot);

	if (err)
		return -EAGAIN;

	return 0;
}

static snd_pcm_uframes_t aess_pointer(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_platform *platform = rtd->platform;
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);
	snd_pcm_uframes_t offset = 0;
	u32 pingpong;

	if (!abe->first_irq) {
		abe_read_offset_from_ping_buffer(MM_DL_PORT, &pingpong);
		offset = (snd_pcm_uframes_t)pingpong;
	}

	return offset;
}

static struct snd_pcm_ops omap_aess_pcm_ops = {
	.open           = aess_open,
	.hw_params	= aess_hw_params,
	.prepare	= aess_prepare,
	.close	        = aess_close,
	.pointer	= aess_pointer,
	.mmap		= aess_mmap,
};

static int aess_stream_event(struct snd_soc_dapm_context *dapm)
{
	struct snd_soc_platform *platform = dapm->platform;
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);

	if (abe->active)
		aess_set_runtime_opp_level(abe);

#ifdef CONFIG_OMAP4_DPLL_CASCADING
	/*
	 * enter dpll cascading when all conditions are met:
	 * - system is in early suspend (screen is off)
	 * - single stream is active and is LP (ping-pong)
	 * - OPP is 50 or less (DL1 path only)
	 */
	if (abe->early_suspended &&
		(abe_fe_active_count(abe) == 1) &&
		(abe->opp <= 50))
		return omap4_dpll_cascading_blocker_release(abe->dev);
	else
		return omap4_dpll_cascading_blocker_hold(abe->dev);
#else
	return 0;
#endif
}

static int abe_add_widgets(struct snd_soc_platform *platform)
{
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);
	struct fw_header *hdr = &abe->hdr;
	int i, ii, j;

	/* create equalizer controls */
	for (i = 0, ii = 0; i < hdr->num_equ; i++) {
		struct soc_enum *equalizer_enum = &abe->equalizer_enum[ii];
		struct snd_kcontrol_new *equalizer_control =
				&abe->equalizer_control[ii];

#if !defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
		if (i+1 == EQ2L || i+1 == EQ2R)
			continue;
#endif

		ii++;
		equalizer_enum->reg = i;
		equalizer_enum->max = abe->equ_texts[i].count;
		for (j = 0; j < abe->equ_texts[i].count; j++)
			equalizer_enum->dtexts[j] = abe->equ_texts[i].texts[j];

		equalizer_control->name = abe->equ_texts[i].name;
		equalizer_control->private_value = (unsigned long)equalizer_enum;
		equalizer_control->get = abe_get_equalizer;
		equalizer_control->put = abe_put_equalizer;
		equalizer_control->info = snd_soc_info_enum_ext1;
		equalizer_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;

		dev_dbg(platform->dev, "added EQU mixer: %s profiles %d\n",
				abe->equ_texts[i].name, abe->equ_texts[i].count);

		for (j = 0; j < abe->equ_texts[i].count; j++)
			dev_dbg(platform->dev, " %s\n", equalizer_enum->dtexts[j]);
	}

	snd_soc_add_platform_controls(platform, abe->equalizer_control, ii);

	snd_soc_add_platform_controls(platform, abe_controls,
			ARRAY_SIZE(abe_controls));

	snd_soc_dapm_new_controls(&platform->dapm, abe_dapm_widgets,
				 ARRAY_SIZE(abe_dapm_widgets));

	snd_soc_dapm_add_routes(&platform->dapm, intercon, ARRAY_SIZE(intercon));

	snd_soc_dapm_new_widgets(&platform->dapm);

	return 0;
}

#ifdef CONFIG_PM
static int abe_suspend(struct snd_soc_dai *dai)
{
	struct abe_data *abe = the_abe;
	int ret = 0;

	dev_dbg(dai->dev, "%s: %s active %d\n",
		__func__, dai->name, dai->active);

	if (!dai->active)
		return 0;

	pm_runtime_get_sync(abe->dev);

	switch (dai->id) {
	case OMAP_ABE_DAI_PDM_UL:
		abe_mute_gain(GAINS_AMIC, GAIN_LEFT_OFFSET);
		abe_mute_gain(GAINS_AMIC, GAIN_RIGHT_OFFSET);
		break;
	case OMAP_ABE_DAI_PDM_DL1:
	case OMAP_ABE_DAI_PDM_DL2:
	case OMAP_ABE_DAI_PDM_VIB:
		break;
	case OMAP_ABE_DAI_BT_VX:
		abe_mute_gain(GAINS_BTUL, GAIN_LEFT_OFFSET);
		abe_mute_gain(GAINS_BTUL, GAIN_RIGHT_OFFSET);
		break;
	case OMAP_ABE_DAI_MM_FM:
	case OMAP_ABE_DAI_MODEM:
		break;
	case OMAP_ABE_DAI_DMIC0:
		abe_mute_gain(GAINS_DMIC1, GAIN_LEFT_OFFSET);
		abe_mute_gain(GAINS_DMIC1, GAIN_RIGHT_OFFSET);
		break;
	case OMAP_ABE_DAI_DMIC1:
		abe_mute_gain(GAINS_DMIC2, GAIN_LEFT_OFFSET);
		abe_mute_gain(GAINS_DMIC2, GAIN_RIGHT_OFFSET);
		break;
	case OMAP_ABE_DAI_DMIC2:
		abe_mute_gain(GAINS_DMIC3, GAIN_LEFT_OFFSET);
		abe_mute_gain(GAINS_DMIC3, GAIN_RIGHT_OFFSET);
		break;
	default:
		dev_err(dai->dev, "%s: invalid DAI id %d\n",
				__func__, dai->id);
		ret = -EINVAL;
		goto out;
	}

out:
	pm_runtime_put_sync(abe->dev);
	return ret;
}

static int abe_resume(struct snd_soc_dai *dai)
{
	struct abe_data *abe = the_abe;
	struct omap4_abe_dsp_pdata *pdata = abe->abe_pdata;
	int i, ret = 0;

	dev_dbg(dai->dev, "%s: %s active %d\n",
		__func__, dai->name, dai->active);

	if (!dai->active)
		return 0;

	/* context retained, no need to restore */
	if (pdata->was_context_lost && !pdata->was_context_lost(abe->dev))
		return 0;

	pm_runtime_get_sync(abe->dev);

	if (pdata && pdata->device_scale) {
		ret = pdata->device_scale(abe->dev, abe->dev,
				abe->opp_freqs[OMAP_ABE_OPP50]);
		if (ret) {
			dev_err(abe->dev, "failed to scale to OPP50\n");
			goto out;
		}
	}

	abe_reload_fw(abe->firmware);

#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	abe_write_select_pdm_output(1);
#else
	abe_write_select_pdm_output(3);
#endif

	switch (dai->id) {
	case OMAP_ABE_DAI_PDM_UL:
		abe_unmute_gain(GAINS_AMIC, GAIN_LEFT_OFFSET);
		abe_unmute_gain(GAINS_AMIC, GAIN_RIGHT_OFFSET);
		break;
	case OMAP_ABE_DAI_PDM_DL1:
	case OMAP_ABE_DAI_PDM_DL2:
	case OMAP_ABE_DAI_PDM_VIB:
		break;
	case OMAP_ABE_DAI_BT_VX:
		abe_unmute_gain(GAINS_BTUL, GAIN_LEFT_OFFSET);
		abe_unmute_gain(GAINS_BTUL, GAIN_RIGHT_OFFSET);
		break;
	case OMAP_ABE_DAI_MM_FM:
	case OMAP_ABE_DAI_MODEM:
		break;
	case OMAP_ABE_DAI_DMIC0:
		abe_unmute_gain(GAINS_DMIC1, GAIN_LEFT_OFFSET);
		abe_unmute_gain(GAINS_DMIC1, GAIN_RIGHT_OFFSET);
		break;
	case OMAP_ABE_DAI_DMIC1:
		abe_unmute_gain(GAINS_DMIC2, GAIN_LEFT_OFFSET);
		abe_unmute_gain(GAINS_DMIC2, GAIN_RIGHT_OFFSET);
		break;
	case OMAP_ABE_DAI_DMIC2:
		abe_unmute_gain(GAINS_DMIC3, GAIN_LEFT_OFFSET);
		abe_unmute_gain(GAINS_DMIC3, GAIN_RIGHT_OFFSET);
		break;
	default:
		dev_err(dai->dev, "%s: invalid DAI id %d\n",
				__func__, dai->id);
		ret = -EINVAL;
		goto out;
	}

	abe_set_router_configuration(UPROUTE, 0, (u32 *)abe->router);

	if (abe->power_mode)
		abe_write_pdmdl_offset(1, abe->dc_hsl * 2, abe->dc_hsr * 2);
	else
		abe_write_pdmdl_offset(1, abe->dc_hsl, abe->dc_hsr);

	abe_write_pdmdl_offset(2, abe->dc_hfl, abe->dc_hfr);

	for (i = 0; i < abe->hdr.num_equ; i++)
		abe_dsp_set_equalizer(i, abe->equ_profile[i]);

	for (i = 0; i < ABE_NUM_MONO_MIXERS; i++)
		abe_dsp_set_mono_mixer(MIX_DL1_MONO + i, abe->mono_mix[i]);
out:
	pm_runtime_put_sync(abe->dev);

#ifdef CONFIG_OMAP4_DPLL_CASCADING
	/* block DPLL cascading till conditions are met */
	omap4_dpll_cascading_blocker_hold(abe->dev);
#endif

	return ret;
}
#else
#define abe_suspend	NULL
#define abe_resume	NULL
#endif

static int abe_probe(struct snd_soc_platform *platform)
{
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);
	struct opp *opp;
	const u8 *fw_data;
	unsigned long freq = ULONG_MAX;
	int ret = 0, i, opp_count, offset = 0;
#if defined(CONFIG_SND_OMAP_SOC_ABE_DSP_MODULE)
	const struct firmware *fw;
#endif

	abe->platform = platform;

	pm_runtime_enable(abe->dev);
	pm_runtime_irq_safe(abe->dev);

#if defined(CONFIG_SND_OMAP_SOC_ABE_DSP_MODULE)
	/* request firmware & coefficients */
	ret = request_firmware(&fw, "omap4_abe", platform->dev);
	if (ret != 0) {
		dev_err(abe->dev, "Failed to load firmware: %d\n", ret);
		return ret;
	}
	fw_data = fw->data;
#else
	fw_data = (u8 *)abe_get_default_fw();
#endif

	/* get firmware and coefficients header info */
	memcpy(&abe->hdr, fw_data, sizeof(struct fw_header));
	if (abe->hdr.firmware_size > ABE_MAX_FW_SIZE) {
		dev_err(abe->dev, "Firmware too large at %d bytes: %d\n",
					abe->hdr.firmware_size, ret);
		ret = -EINVAL;
		goto err_fw;
	}
	dev_dbg(abe->dev, "ABE firmware size %d bytes\n", abe->hdr.firmware_size);

	if (abe->hdr.coeff_size > ABE_MAX_COEFF_SIZE) {
		dev_err(abe->dev, "Coefficients too large at %d bytes: %d\n",
					abe->hdr.coeff_size, ret);
			ret = -EINVAL;
			goto err_fw;
	}
	dev_dbg(abe->dev, "ABE coefficients size %d bytes\n", abe->hdr.coeff_size);

	/* get coefficient EQU mixer strings */
	if (abe->hdr.num_equ >= ABE_MAX_EQU) {
		dev_err(abe->dev, "Too many equalizers got %d\n", abe->hdr.num_equ);
		ret = -EINVAL;
		goto err_fw;
	}
	abe->equ_texts = kzalloc(abe->hdr.num_equ * sizeof(struct coeff_config),
			GFP_KERNEL);
	if (abe->equ_texts == NULL) {
		ret = -ENOMEM;
		goto err_fw;
	}
	offset = sizeof(struct fw_header);
	memcpy(abe->equ_texts, fw_data + offset,
			abe->hdr.num_equ * sizeof(struct coeff_config));

	/* get coefficients from firmware */
	abe->equ[0] = kmalloc(abe->hdr.coeff_size, GFP_KERNEL);
	if (abe->equ[0] == NULL) {
		ret = -ENOMEM;
		goto err_equ;
	}
	offset += abe->hdr.num_equ * sizeof(struct coeff_config);
	memcpy(abe->equ[0], fw_data + offset, abe->hdr.coeff_size);

	/* allocate coefficient mixer texts */
	dev_dbg(abe->dev, "loaded %d equalizers\n", abe->hdr.num_equ);
	for (i = 0; i < abe->hdr.num_equ; i++) {
		dev_dbg(abe->dev, "equ %d: %s profiles %d\n", i,
				abe->equ_texts[i].name, abe->equ_texts[i].count);
		if (abe->equ_texts[i].count >= ABE_MAX_PROFILES) {
			dev_err(abe->dev, "Too many profiles got %d for equ %d\n",
					abe->equ_texts[i].count, i);
			ret = -EINVAL;
			goto err_texts;
		}
		abe->equalizer_enum[i].dtexts =
				kzalloc(abe->equ_texts[i].count * sizeof(char *), GFP_KERNEL);
		if (abe->equalizer_enum[i].dtexts == NULL) {
			ret = -ENOMEM;
			goto err_texts;
		}
	}

	/* initialise coefficient equalizers */
	for (i = 1; i < abe->hdr.num_equ; i++) {
		abe->equ[i] = abe->equ[i - 1] +
			abe->equ_texts[i - 1].count * abe->equ_texts[i - 1].coeff;
	}

	/* store ABE firmware for later context restore */
	abe->firmware = kzalloc(abe->hdr.firmware_size, GFP_KERNEL);
	if (abe->firmware == NULL) {
		ret = -ENOMEM;
		goto err_texts;
	}

	memcpy(abe->firmware,
		fw_data + sizeof(struct fw_header) + abe->hdr.coeff_size,
		abe->hdr.firmware_size);

	ret = request_threaded_irq(abe->irq, NULL, abe_irq_handler,
				IRQF_ONESHOT, "ABE", (void *)abe);
	if (ret) {
		dev_err(platform->dev, "request for ABE IRQ %d failed %d\n",
				abe->irq, ret);
		goto err_irq;
	}

	/* query supported opps */
	rcu_read_lock();
	opp_count = opp_get_opp_count(abe->dev);
	if (opp_count <= 0) {
		dev_err(abe->dev, "invalid OPP data\n");
		ret = opp_count;
		goto err_opp;
	} else if (opp_count > OMAP_ABE_OPP_COUNT) {
		dev_err(abe->dev, "unsupported OPP count %d (max:%d)\n",
			opp_count, OMAP_ABE_OPP_COUNT);
		ret = -EINVAL;
		goto err_opp;
	}

	/* assume provided opps are always higher */
	for (i = OMAP_ABE_OPP_COUNT - 1; i >= 0; i--) {
		opp = opp_find_freq_floor(abe->dev, &freq);
		if (IS_ERR_OR_NULL(opp))
			break;
		abe->opp_freqs[i] = freq;
		/* prepare to obtain next available opp */
		freq--;
	}
	/* use lowest available opp for non-populated items */
	for (freq++; i >= 0; i--)
		abe->opp_freqs[i] = freq;
	rcu_read_unlock();

	/* aess_clk has to be enabled to access hal register.
	 * Disable the clk after it has been used.
	 */
	pm_runtime_get_sync(abe->dev);

	abe_init_mem(abe->io_base);

	abe_reset_hal();

	abe_load_fw(abe->firmware);

	/* "tick" of the audio engine */
	abe_write_event_generator(EVENT_TIMER);

	abe_dsp_init_gains(abe);

#if defined(CONFIG_SND_OMAP_SOC_ABE_DL2)
	abe_write_select_pdm_output(1);
#else
	abe_write_select_pdm_output(3);
#endif

	/* Stop the engine */
	abe_stop_event_generator();
	abe_disable_irq();

	pm_runtime_put_sync(abe->dev);
	abe_add_widgets(platform);

#if defined(CONFIG_SND_OMAP_SOC_ABE_DSP_MODULE)
	release_firmware(fw);
#endif
	return ret;

err_opp:
	rcu_read_unlock();
	free_irq(abe->irq, (void *)abe);
err_irq:
	kfree(abe->firmware);
err_texts:
	for (i = 0; i < abe->hdr.num_equ; i++)
		kfree(abe->equalizer_enum[i].texts);
	kfree(abe->equ[0]);
err_equ:
	kfree(abe->equ_texts);
err_fw:
#if defined(CONFIG_SND_OMAP_SOC_ABE_DSP_MODULE)
	release_firmware(fw);
#endif
	return ret;
}

static int abe_remove(struct snd_soc_platform *platform)
{
	struct abe_data *abe = snd_soc_platform_get_drvdata(platform);
	int i;

	free_irq(abe->irq, (void *)abe);

	for (i = 0; i < abe->hdr.num_equ; i++)
		kfree(abe->equalizer_enum[i].texts);

	kfree(abe->equ[0]);
	kfree(abe->equ_texts);
	kfree(abe->firmware);

	pm_runtime_disable(abe->dev);

	return 0;
}

static struct snd_soc_platform_driver omap_aess_platform = {
	.ops		= &omap_aess_pcm_ops,
	.probe		= abe_probe,
	.remove		= abe_remove,
	.suspend	= abe_suspend,
	.resume		= abe_resume,
	.read		= abe_dsp_read,
	.write		= abe_dsp_write,
	.stream_event = aess_stream_event,
};

#ifdef CONFIG_OMAP4_DPLL_CASCADING
static void abe_early_suspend(struct early_suspend *handler)
{
	struct abe_data *abe = container_of(handler, struct abe_data,
							early_suspend);
	int active = abe_fe_active_count(abe);

	/*
	 * enter dpll cascading when all conditions are met:
	 * - system is in early suspend (screen is off)
	 * - single stream is active and is LP (ping-pong)
	 * - OPP is 50 or less (DL1 path only)
	 */
	if ((active == 1) && (abe->opp <= 50))
		omap4_dpll_cascading_blocker_release(abe->dev);

	abe->early_suspended = 1;
}

static void abe_late_resume(struct early_suspend *handler)
{
	struct abe_data *abe = container_of(handler, struct abe_data,
							early_suspend);

	/* exit dpll cascading since screen will be turned on */
	omap4_dpll_cascading_blocker_hold(abe->dev);
	abe->early_suspended = 0;
}
#endif

static int __devinit abe_engine_probe(struct platform_device *pdev)
{
	struct resource *res;
	struct omap4_abe_dsp_pdata *pdata = pdev->dev.platform_data;
	struct abe_data *abe;
	int ret = -EINVAL, i;

	abe = kzalloc(sizeof(struct abe_data), GFP_KERNEL);
	if (abe == NULL)
		return -ENOMEM;
	dev_set_drvdata(&pdev->dev, abe);
	the_abe = abe;

	/* ZERO_labelID should really be 0 */
	for (i = 0; i < ABE_ROUTES_UL + 2; i++)
		abe->router[i] = ZERO_labelID;

	for (i = 0; i < 5; i++) {
		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
						   abe_memory_bank[i]);
		if (res == NULL) {
			dev_err(&pdev->dev, "no resource %s\n",
				abe_memory_bank[i]);
			goto err;
		}
		abe->io_base[i] = ioremap(res->start, resource_size(res));
		if (!abe->io_base[i]) {
			ret = -ENOMEM;
			goto err;
		}
	}

	abe->irq = platform_get_irq(pdev, 0);
	if (abe->irq < 0) {
		ret = abe->irq;
		goto err;
	}

	abe->abe_pdata = pdata;
	abe->dev = &pdev->dev;
	mutex_init(&abe->mutex);
	mutex_init(&abe->opp_mutex);
	mutex_init(&abe->opp_req_mutex);

#ifdef CONFIG_OMAP4_DPLL_CASCADING
	omap4_dpll_cascading_blocker_hold(abe->dev);
	abe_can_enter_dpll_cascading = true;
	abe->early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1;
	abe->early_suspend.suspend = abe_early_suspend;
	abe->early_suspend.resume = abe_late_resume;
	register_early_suspend(&abe->early_suspend);
#endif

	INIT_LIST_HEAD(&abe->opp_req);
	abe->opp_req_count = 0;

	ret = snd_soc_register_platform(abe->dev,
			&omap_aess_platform);

	if (ret)
		goto err;

	abe_init_debugfs(abe);
	return ret;

err:
	for (--i; i >= 0; i--)
		iounmap(abe->io_base[i]);

#ifdef CONFIG_OMAP4_DPLL_CASCADING
	unregister_early_suspend(&abe->early_suspend);
#endif

	kfree(abe);
	return ret;
}

static int __devexit abe_engine_remove(struct platform_device *pdev)
{
	struct abe_data *abe = dev_get_drvdata(&pdev->dev);
	int i;

	abe_cleanup_debugfs(abe);

#ifdef CONFIG_OMAP4_DPLL_CASCADING
	unregister_early_suspend(&abe->early_suspend);
#endif

	snd_soc_unregister_platform(&pdev->dev);
	for (i = 0; i < 5; i++)
		iounmap(abe->io_base[i]);
	kfree(abe);
	return 0;
}

static struct platform_driver omap_aess_driver = {
	.driver = {
		.name = "aess",
		.owner = THIS_MODULE,
	},
	.probe = abe_engine_probe,
	.remove = __devexit_p(abe_engine_remove),
};

static int __init abe_engine_init(void)
{
	return platform_driver_register(&omap_aess_driver);
}
module_init(abe_engine_init);

static void __exit abe_engine_exit(void)
{
	platform_driver_unregister(&omap_aess_driver);
}
module_exit(abe_engine_exit);

MODULE_DESCRIPTION("ASoC OMAP4 ABE");
MODULE_AUTHOR("Liam Girdwood <lrg@ti.com>");
MODULE_LICENSE("GPL");