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

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/pm_runtime.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
#include <linux/acpi.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/mutex.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/jack.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>
#include <sound/tlv.h>
#include <sound/rt5682s.h>

#include "rt5682s.h"

#define DEVICE_ID 0x6749

static const struct rt5682s_platform_data i2s_default_platform_data = {
	.dmic1_data_pin = RT5682S_DMIC1_DATA_GPIO2,
	.dmic1_clk_pin = RT5682S_DMIC1_CLK_GPIO3,
	.jd_src = RT5682S_JD1,
	.dai_clk_names[RT5682S_DAI_WCLK_IDX] = "rt5682-dai-wclk",
	.dai_clk_names[RT5682S_DAI_BCLK_IDX] = "rt5682-dai-bclk",
};

static const char *rt5682s_supply_names[RT5682S_NUM_SUPPLIES] = {
	[RT5682S_SUPPLY_AVDD] = "AVDD",
	[RT5682S_SUPPLY_MICVDD] = "MICVDD",
};

static const struct reg_sequence patch_list[] = {
	{RT5682S_I2C_CTRL,			0x0007},
	{RT5682S_DIG_IN_CTRL_1,			0x0000},
	{RT5682S_CHOP_DAC_2,			0x2020},
	{RT5682S_VREF_REC_OP_FB_CAP_CTRL_2,	0x0101},
	{RT5682S_VREF_REC_OP_FB_CAP_CTRL_1,	0x80c0},
	{RT5682S_HP_CALIB_CTRL_9,		0x0002},
	{RT5682S_DEPOP_1,			0x0000},
	{RT5682S_HP_CHARGE_PUMP_2,		0x3c15},
	{RT5682S_DAC1_DIG_VOL,			0xfefe},
	{RT5682S_SAR_IL_CMD_2,			0xac00},
	{RT5682S_SAR_IL_CMD_3,			0x024c},
	{RT5682S_CBJ_CTRL_6,			0x0804},
};

static void rt5682s_apply_patch_list(struct rt5682s_priv *rt5682s,
		struct device *dev)
{
	int ret;

	ret = regmap_multi_reg_write(rt5682s->regmap, patch_list, ARRAY_SIZE(patch_list));
	if (ret)
		dev_warn(dev, "Failed to apply regmap patch: %d\n", ret);
}

static const struct reg_default rt5682s_reg[] = {
	{0x0002, 0x8080},
	{0x0003, 0x0001},
	{0x0005, 0x0000},
	{0x0006, 0x0000},
	{0x0008, 0x8007},
	{0x000b, 0x0000},
	{0x000f, 0x4000},
	{0x0010, 0x4040},
	{0x0011, 0x0000},
	{0x0012, 0x0000},
	{0x0013, 0x1200},
	{0x0014, 0x200a},
	{0x0015, 0x0404},
	{0x0016, 0x0404},
	{0x0017, 0x05a4},
	{0x0019, 0xffff},
	{0x001c, 0x2f2f},
	{0x001f, 0x0000},
	{0x0022, 0x5757},
	{0x0023, 0x0039},
	{0x0024, 0x000b},
	{0x0026, 0xc0c4},
	{0x0029, 0x8080},
	{0x002a, 0xa0a0},
	{0x002b, 0x0300},
	{0x0030, 0x0000},
	{0x003c, 0x08c0},
	{0x0044, 0x1818},
	{0x004b, 0x00c0},
	{0x004c, 0x0000},
	{0x004d, 0x0000},
	{0x0061, 0x00c0},
	{0x0062, 0x008a},
	{0x0063, 0x0800},
	{0x0064, 0x0000},
	{0x0065, 0x0000},
	{0x0066, 0x0030},
	{0x0067, 0x000c},
	{0x0068, 0x0000},
	{0x0069, 0x0000},
	{0x006a, 0x0000},
	{0x006b, 0x0000},
	{0x006c, 0x0000},
	{0x006d, 0x2200},
	{0x006e, 0x0810},
	{0x006f, 0xe4de},
	{0x0070, 0x3320},
	{0x0071, 0x0000},
	{0x0073, 0x0000},
	{0x0074, 0x0000},
	{0x0075, 0x0002},
	{0x0076, 0x0001},
	{0x0079, 0x0000},
	{0x007a, 0x0000},
	{0x007b, 0x0000},
	{0x007c, 0x0100},
	{0x007e, 0x0000},
	{0x007f, 0x0000},
	{0x0080, 0x0000},
	{0x0083, 0x0000},
	{0x0084, 0x0000},
	{0x0085, 0x0000},
	{0x0086, 0x0005},
	{0x0087, 0x0000},
	{0x0088, 0x0000},
	{0x008c, 0x0003},
	{0x008e, 0x0060},
	{0x008f, 0x4da1},
	{0x0091, 0x1c15},
	{0x0092, 0x0425},
	{0x0093, 0x0000},
	{0x0094, 0x0080},
	{0x0095, 0x008f},
	{0x0096, 0x0000},
	{0x0097, 0x0000},
	{0x0098, 0x0000},
	{0x0099, 0x0000},
	{0x009a, 0x0000},
	{0x009b, 0x0000},
	{0x009c, 0x0000},
	{0x009d, 0x0000},
	{0x009e, 0x0000},
	{0x009f, 0x0009},
	{0x00a0, 0x0000},
	{0x00a3, 0x0002},
	{0x00a4, 0x0001},
	{0x00b6, 0x0000},
	{0x00b7, 0x0000},
	{0x00b8, 0x0000},
	{0x00b9, 0x0002},
	{0x00be, 0x0000},
	{0x00c0, 0x0160},
	{0x00c1, 0x82a0},
	{0x00c2, 0x0000},
	{0x00d0, 0x0000},
	{0x00d2, 0x3300},
	{0x00d3, 0x2200},
	{0x00d4, 0x0000},
	{0x00d9, 0x0000},
	{0x00da, 0x0000},
	{0x00db, 0x0000},
	{0x00dc, 0x00c0},
	{0x00dd, 0x2220},
	{0x00de, 0x3131},
	{0x00df, 0x3131},
	{0x00e0, 0x3131},
	{0x00e2, 0x0000},
	{0x00e3, 0x4000},
	{0x00e4, 0x0aa0},
	{0x00e5, 0x3131},
	{0x00e6, 0x3131},
	{0x00e7, 0x3131},
	{0x00e8, 0x3131},
	{0x00ea, 0xb320},
	{0x00eb, 0x0000},
	{0x00f0, 0x0000},
	{0x00f6, 0x0000},
	{0x00fa, 0x0000},
	{0x00fb, 0x0000},
	{0x00fc, 0x0000},
	{0x00fd, 0x0000},
	{0x00fe, 0x10ec},
	{0x00ff, 0x6749},
	{0x0100, 0xa000},
	{0x010b, 0x0066},
	{0x010c, 0x6666},
	{0x010d, 0x2202},
	{0x010e, 0x6666},
	{0x010f, 0xa800},
	{0x0110, 0x0006},
	{0x0111, 0x0460},
	{0x0112, 0x2000},
	{0x0113, 0x0200},
	{0x0117, 0x8000},
	{0x0118, 0x0303},
	{0x0125, 0x0020},
	{0x0132, 0x5026},
	{0x0136, 0x8000},
	{0x0139, 0x0005},
	{0x013a, 0x3030},
	{0x013b, 0xa000},
	{0x013c, 0x4110},
	{0x013f, 0x0000},
	{0x0145, 0x0022},
	{0x0146, 0x0000},
	{0x0147, 0x0000},
	{0x0148, 0x0000},
	{0x0156, 0x0022},
	{0x0157, 0x0303},
	{0x0158, 0x2222},
	{0x0159, 0x0000},
	{0x0160, 0x4ec0},
	{0x0161, 0x0080},
	{0x0162, 0x0200},
	{0x0163, 0x0800},
	{0x0164, 0x0000},
	{0x0165, 0x0000},
	{0x0166, 0x0000},
	{0x0167, 0x000f},
	{0x0168, 0x000f},
	{0x0169, 0x0001},
	{0x0190, 0x4131},
	{0x0194, 0x0000},
	{0x0195, 0x0000},
	{0x0197, 0x0022},
	{0x0198, 0x0000},
	{0x0199, 0x0000},
	{0x01ac, 0x0000},
	{0x01ad, 0x0000},
	{0x01ae, 0x0000},
	{0x01af, 0x2000},
	{0x01b0, 0x0000},
	{0x01b1, 0x0000},
	{0x01b2, 0x0000},
	{0x01b3, 0x0017},
	{0x01b4, 0x004b},
	{0x01b5, 0x0000},
	{0x01b6, 0x03e8},
	{0x01b7, 0x0000},
	{0x01b8, 0x0000},
	{0x01b9, 0x0400},
	{0x01ba, 0xb5b6},
	{0x01bb, 0x9124},
	{0x01bc, 0x4924},
	{0x01bd, 0x0009},
	{0x01be, 0x0018},
	{0x01bf, 0x002a},
	{0x01c0, 0x004c},
	{0x01c1, 0x0097},
	{0x01c2, 0x01c3},
	{0x01c3, 0x03e9},
	{0x01c4, 0x1389},
	{0x01c5, 0xc351},
	{0x01c6, 0x02a0},
	{0x01c7, 0x0b0f},
	{0x01c8, 0x402f},
	{0x01c9, 0x0702},
	{0x01ca, 0x0000},
	{0x01cb, 0x0000},
	{0x01cc, 0x5757},
	{0x01cd, 0x5757},
	{0x01ce, 0x5757},
	{0x01cf, 0x5757},
	{0x01d0, 0x5757},
	{0x01d1, 0x5757},
	{0x01d2, 0x5757},
	{0x01d3, 0x5757},
	{0x01d4, 0x5757},
	{0x01d5, 0x5757},
	{0x01d6, 0x0000},
	{0x01d7, 0x0000},
	{0x01d8, 0x0162},
	{0x01d9, 0x0007},
	{0x01da, 0x0000},
	{0x01db, 0x0004},
	{0x01dc, 0x0000},
	{0x01de, 0x7c00},
	{0x01df, 0x0020},
	{0x01e0, 0x04c1},
	{0x01e1, 0x0000},
	{0x01e2, 0x0000},
	{0x01e3, 0x0000},
	{0x01e4, 0x0000},
	{0x01e5, 0x0000},
	{0x01e6, 0x0001},
	{0x01e7, 0x0000},
	{0x01e8, 0x0000},
	{0x01eb, 0x0000},
	{0x01ec, 0x0000},
	{0x01ed, 0x0000},
	{0x01ee, 0x0000},
	{0x01ef, 0x0000},
	{0x01f0, 0x0000},
	{0x01f1, 0x0000},
	{0x01f2, 0x0000},
	{0x01f3, 0x0000},
	{0x01f4, 0x0000},
	{0x0210, 0x6297},
	{0x0211, 0xa004},
	{0x0212, 0x0365},
	{0x0213, 0xf7ff},
	{0x0214, 0xf24c},
	{0x0215, 0x0102},
	{0x0216, 0x00a3},
	{0x0217, 0x0048},
	{0x0218, 0xa2c0},
	{0x0219, 0x0400},
	{0x021a, 0x00c8},
	{0x021b, 0x00c0},
	{0x021c, 0x0000},
	{0x021d, 0x024c},
	{0x02fa, 0x0000},
	{0x02fb, 0x0000},
	{0x02fc, 0x0000},
	{0x03fe, 0x0000},
	{0x03ff, 0x0000},
	{0x0500, 0x0000},
	{0x0600, 0x0000},
	{0x0610, 0x6666},
	{0x0611, 0xa9aa},
	{0x0620, 0x6666},
	{0x0621, 0xa9aa},
	{0x0630, 0x6666},
	{0x0631, 0xa9aa},
	{0x0640, 0x6666},
	{0x0641, 0xa9aa},
	{0x07fa, 0x0000},
	{0x08fa, 0x0000},
	{0x08fb, 0x0000},
	{0x0d00, 0x0000},
	{0x1100, 0x0000},
	{0x1101, 0x0000},
	{0x1102, 0x0000},
	{0x1103, 0x0000},
	{0x1104, 0x0000},
	{0x1105, 0x0000},
	{0x1106, 0x0000},
	{0x1107, 0x0000},
	{0x1108, 0x0000},
	{0x1109, 0x0000},
	{0x110a, 0x0000},
	{0x110b, 0x0000},
	{0x110c, 0x0000},
	{0x1111, 0x0000},
	{0x1112, 0x0000},
	{0x1113, 0x0000},
	{0x1114, 0x0000},
	{0x1115, 0x0000},
	{0x1116, 0x0000},
	{0x1117, 0x0000},
	{0x1118, 0x0000},
	{0x1119, 0x0000},
	{0x111a, 0x0000},
	{0x111b, 0x0000},
	{0x111c, 0x0000},
	{0x1401, 0x0404},
	{0x1402, 0x0007},
	{0x1403, 0x0365},
	{0x1404, 0x0210},
	{0x1405, 0x0365},
	{0x1406, 0x0210},
	{0x1407, 0x0000},
	{0x1408, 0x0000},
	{0x1409, 0x0000},
	{0x140a, 0x0000},
	{0x140b, 0x0000},
	{0x140c, 0x0000},
	{0x140d, 0x0000},
	{0x140e, 0x0000},
	{0x140f, 0x0000},
	{0x1410, 0x0000},
	{0x1411, 0x0000},
	{0x1801, 0x0004},
	{0x1802, 0x0000},
	{0x1803, 0x0000},
	{0x1804, 0x0000},
	{0x1805, 0x00ff},
	{0x2c00, 0x0000},
	{0x3400, 0x0200},
	{0x3404, 0x0000},
	{0x3405, 0x0000},
	{0x3406, 0x0000},
	{0x3407, 0x0000},
	{0x3408, 0x0000},
	{0x3409, 0x0000},
	{0x340a, 0x0000},
	{0x340b, 0x0000},
	{0x340c, 0x0000},
	{0x340d, 0x0000},
	{0x340e, 0x0000},
	{0x340f, 0x0000},
	{0x3410, 0x0000},
	{0x3411, 0x0000},
	{0x3412, 0x0000},
	{0x3413, 0x0000},
	{0x3414, 0x0000},
	{0x3415, 0x0000},
	{0x3424, 0x0000},
	{0x3425, 0x0000},
	{0x3426, 0x0000},
	{0x3427, 0x0000},
	{0x3428, 0x0000},
	{0x3429, 0x0000},
	{0x342a, 0x0000},
	{0x342b, 0x0000},
	{0x342c, 0x0000},
	{0x342d, 0x0000},
	{0x342e, 0x0000},
	{0x342f, 0x0000},
	{0x3430, 0x0000},
	{0x3431, 0x0000},
	{0x3432, 0x0000},
	{0x3433, 0x0000},
	{0x3434, 0x0000},
	{0x3435, 0x0000},
	{0x3440, 0x6319},
	{0x3441, 0x3771},
	{0x3500, 0x0002},
	{0x3501, 0x5728},
	{0x3b00, 0x3010},
	{0x3b01, 0x3300},
	{0x3b02, 0x2200},
	{0x3b03, 0x0100},
};

static bool rt5682s_volatile_register(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case RT5682S_RESET:
	case RT5682S_CBJ_CTRL_2:
	case RT5682S_I2S1_F_DIV_CTRL_2:
	case RT5682S_I2S2_F_DIV_CTRL_2:
	case RT5682S_INT_ST_1:
	case RT5682S_GPIO_ST:
	case RT5682S_IL_CMD_1:
	case RT5682S_4BTN_IL_CMD_1:
	case RT5682S_AJD1_CTRL:
	case RT5682S_VERSION_ID...RT5682S_DEVICE_ID:
	case RT5682S_STO_NG2_CTRL_1:
	case RT5682S_STO_NG2_CTRL_5...RT5682S_STO_NG2_CTRL_7:
	case RT5682S_STO1_DAC_SIL_DET:
	case RT5682S_HP_IMP_SENS_CTRL_1...RT5682S_HP_IMP_SENS_CTRL_4:
	case RT5682S_HP_IMP_SENS_CTRL_13:
	case RT5682S_HP_IMP_SENS_CTRL_14:
	case RT5682S_HP_IMP_SENS_CTRL_43...RT5682S_HP_IMP_SENS_CTRL_46:
	case RT5682S_HP_CALIB_CTRL_1:
	case RT5682S_HP_CALIB_CTRL_10:
	case RT5682S_HP_CALIB_ST_1...RT5682S_HP_CALIB_ST_11:
	case RT5682S_SAR_IL_CMD_2...RT5682S_SAR_IL_CMD_5:
	case RT5682S_SAR_IL_CMD_10:
	case RT5682S_SAR_IL_CMD_11:
	case RT5682S_VERSION_ID_HIDE:
	case RT5682S_VERSION_ID_CUS:
	case RT5682S_I2C_TRANS_CTRL:
	case RT5682S_DMIC_FLOAT_DET:
	case RT5682S_HA_CMP_OP_1:
	case RT5682S_NEW_CBJ_DET_CTL_10...RT5682S_NEW_CBJ_DET_CTL_16:
	case RT5682S_CLK_SW_TEST_1:
	case RT5682S_CLK_SW_TEST_2:
	case RT5682S_EFUSE_READ_1...RT5682S_EFUSE_READ_18:
	case RT5682S_PILOT_DIG_CTL_1:
		return true;
	default:
		return false;
	}
}

static bool rt5682s_readable_register(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case RT5682S_RESET:
	case RT5682S_VERSION_ID:
	case RT5682S_VENDOR_ID:
	case RT5682S_DEVICE_ID:
	case RT5682S_HP_CTRL_1:
	case RT5682S_HP_CTRL_2:
	case RT5682S_HPL_GAIN:
	case RT5682S_HPR_GAIN:
	case RT5682S_I2C_CTRL:
	case RT5682S_CBJ_BST_CTRL:
	case RT5682S_CBJ_DET_CTRL:
	case RT5682S_CBJ_CTRL_1...RT5682S_CBJ_CTRL_8:
	case RT5682S_DAC1_DIG_VOL:
	case RT5682S_STO1_ADC_DIG_VOL:
	case RT5682S_STO1_ADC_BOOST:
	case RT5682S_HP_IMP_GAIN_1:
	case RT5682S_HP_IMP_GAIN_2:
	case RT5682S_SIDETONE_CTRL:
	case RT5682S_STO1_ADC_MIXER:
	case RT5682S_AD_DA_MIXER:
	case RT5682S_STO1_DAC_MIXER:
	case RT5682S_A_DAC1_MUX:
	case RT5682S_DIG_INF2_DATA:
	case RT5682S_REC_MIXER:
	case RT5682S_CAL_REC:
	case RT5682S_HP_ANA_OST_CTRL_1...RT5682S_HP_ANA_OST_CTRL_3:
	case RT5682S_PWR_DIG_1...RT5682S_PWR_MIXER:
	case RT5682S_MB_CTRL:
	case RT5682S_CLK_GATE_TCON_1...RT5682S_CLK_GATE_TCON_3:
	case RT5682S_CLK_DET...RT5682S_LPF_AD_DMIC:
	case RT5682S_I2S1_SDP:
	case RT5682S_I2S2_SDP:
	case RT5682S_ADDA_CLK_1:
	case RT5682S_ADDA_CLK_2:
	case RT5682S_I2S1_F_DIV_CTRL_1:
	case RT5682S_I2S1_F_DIV_CTRL_2:
	case RT5682S_TDM_CTRL:
	case RT5682S_TDM_ADDA_CTRL_1:
	case RT5682S_TDM_ADDA_CTRL_2:
	case RT5682S_DATA_SEL_CTRL_1:
	case RT5682S_TDM_TCON_CTRL_1:
	case RT5682S_TDM_TCON_CTRL_2:
	case RT5682S_GLB_CLK:
	case RT5682S_PLL_TRACK_1...RT5682S_PLL_TRACK_6:
	case RT5682S_PLL_TRACK_11:
	case RT5682S_DEPOP_1:
	case RT5682S_HP_CHARGE_PUMP_1:
	case RT5682S_HP_CHARGE_PUMP_2:
	case RT5682S_HP_CHARGE_PUMP_3:
	case RT5682S_MICBIAS_1...RT5682S_MICBIAS_3:
	case RT5682S_PLL_TRACK_12...RT5682S_PLL_CTRL_7:
	case RT5682S_RC_CLK_CTRL:
	case RT5682S_I2S2_M_CLK_CTRL_1:
	case RT5682S_I2S2_F_DIV_CTRL_1:
	case RT5682S_I2S2_F_DIV_CTRL_2:
	case RT5682S_IRQ_CTRL_1...RT5682S_IRQ_CTRL_4:
	case RT5682S_INT_ST_1:
	case RT5682S_GPIO_CTRL_1:
	case RT5682S_GPIO_CTRL_2:
	case RT5682S_GPIO_ST:
	case RT5682S_HP_AMP_DET_CTRL_1:
	case RT5682S_MID_HP_AMP_DET:
	case RT5682S_LOW_HP_AMP_DET:
	case RT5682S_DELAY_BUF_CTRL:
	case RT5682S_SV_ZCD_1:
	case RT5682S_SV_ZCD_2:
	case RT5682S_IL_CMD_1...RT5682S_IL_CMD_6:
	case RT5682S_4BTN_IL_CMD_1...RT5682S_4BTN_IL_CMD_7:
	case RT5682S_ADC_STO1_HP_CTRL_1:
	case RT5682S_ADC_STO1_HP_CTRL_2:
	case RT5682S_AJD1_CTRL:
	case RT5682S_JD_CTRL_1:
	case RT5682S_DUMMY_1...RT5682S_DUMMY_3:
	case RT5682S_DAC_ADC_DIG_VOL1:
	case RT5682S_BIAS_CUR_CTRL_2...RT5682S_BIAS_CUR_CTRL_10:
	case RT5682S_VREF_REC_OP_FB_CAP_CTRL_1:
	case RT5682S_VREF_REC_OP_FB_CAP_CTRL_2:
	case RT5682S_CHARGE_PUMP_1:
	case RT5682S_DIG_IN_CTRL_1:
	case RT5682S_PAD_DRIVING_CTRL:
	case RT5682S_CHOP_DAC_1:
	case RT5682S_CHOP_DAC_2:
	case RT5682S_CHOP_ADC:
	case RT5682S_CALIB_ADC_CTRL:
	case RT5682S_VOL_TEST:
	case RT5682S_SPKVDD_DET_ST:
	case RT5682S_TEST_MODE_CTRL_1...RT5682S_TEST_MODE_CTRL_4:
	case RT5682S_PLL_INTERNAL_1...RT5682S_PLL_INTERNAL_4:
	case RT5682S_STO_NG2_CTRL_1...RT5682S_STO_NG2_CTRL_10:
	case RT5682S_STO1_DAC_SIL_DET:
	case RT5682S_SIL_PSV_CTRL1:
	case RT5682S_SIL_PSV_CTRL2:
	case RT5682S_SIL_PSV_CTRL3:
	case RT5682S_SIL_PSV_CTRL4:
	case RT5682S_SIL_PSV_CTRL5:
	case RT5682S_HP_IMP_SENS_CTRL_1...RT5682S_HP_IMP_SENS_CTRL_46:
	case RT5682S_HP_LOGIC_CTRL_1...RT5682S_HP_LOGIC_CTRL_3:
	case RT5682S_HP_CALIB_CTRL_1...RT5682S_HP_CALIB_CTRL_11:
	case RT5682S_HP_CALIB_ST_1...RT5682S_HP_CALIB_ST_11:
	case RT5682S_SAR_IL_CMD_1...RT5682S_SAR_IL_CMD_14:
	case RT5682S_DUMMY_4...RT5682S_DUMMY_6:
	case RT5682S_VERSION_ID_HIDE:
	case RT5682S_VERSION_ID_CUS:
	case RT5682S_SCAN_CTL:
	case RT5682S_HP_AMP_DET:
	case RT5682S_BIAS_CUR_CTRL_11:
	case RT5682S_BIAS_CUR_CTRL_12:
	case RT5682S_BIAS_CUR_CTRL_13:
	case RT5682S_BIAS_CUR_CTRL_14:
	case RT5682S_BIAS_CUR_CTRL_15:
	case RT5682S_BIAS_CUR_CTRL_16:
	case RT5682S_BIAS_CUR_CTRL_17:
	case RT5682S_BIAS_CUR_CTRL_18:
	case RT5682S_I2C_TRANS_CTRL:
	case RT5682S_DUMMY_7:
	case RT5682S_DUMMY_8:
	case RT5682S_DMIC_FLOAT_DET:
	case RT5682S_HA_CMP_OP_1...RT5682S_HA_CMP_OP_13:
	case RT5682S_HA_CMP_OP_14...RT5682S_HA_CMP_OP_25:
	case RT5682S_NEW_CBJ_DET_CTL_1...RT5682S_NEW_CBJ_DET_CTL_16:
	case RT5682S_DA_FILTER_1...RT5682S_DA_FILTER_5:
	case RT5682S_CLK_SW_TEST_1:
	case RT5682S_CLK_SW_TEST_2:
	case RT5682S_CLK_SW_TEST_3...RT5682S_CLK_SW_TEST_14:
	case RT5682S_EFUSE_MANU_WRITE_1...RT5682S_EFUSE_MANU_WRITE_6:
	case RT5682S_EFUSE_READ_1...RT5682S_EFUSE_READ_18:
	case RT5682S_EFUSE_TIMING_CTL_1:
	case RT5682S_EFUSE_TIMING_CTL_2:
	case RT5682S_PILOT_DIG_CTL_1:
	case RT5682S_PILOT_DIG_CTL_2:
	case RT5682S_HP_AMP_DET_CTL_1...RT5682S_HP_AMP_DET_CTL_4:
		return true;
	default:
		return false;
	}
}

static void rt5682s_reset(struct rt5682s_priv *rt5682s)
{
	regmap_write(rt5682s->regmap, RT5682S_RESET, 0);
}

static int rt5682s_button_detect(struct snd_soc_component *component)
{
	int btn_type, val;

	val = snd_soc_component_read(component, RT5682S_4BTN_IL_CMD_1);
	btn_type = val & 0xfff0;
	snd_soc_component_write(component, RT5682S_4BTN_IL_CMD_1, val);
	dev_dbg(component->dev, "%s btn_type=%x\n", __func__, btn_type);
	snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_2,
		RT5682S_SAR_ADC_PSV_MASK, RT5682S_SAR_ADC_PSV_ENTRY);

	return btn_type;
}

enum {
	SAR_PWR_OFF,
	SAR_PWR_NORMAL,
	SAR_PWR_SAVING,
};

static void rt5682s_sar_power_mode(struct snd_soc_component *component, int mode)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	mutex_lock(&rt5682s->sar_mutex);

	switch (mode) {
	case SAR_PWR_SAVING:
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_3,
			RT5682S_CBJ_IN_BUF_MASK, RT5682S_CBJ_IN_BUF_DIS);
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_1,
			RT5682S_MB1_PATH_MASK | RT5682S_MB2_PATH_MASK,
			RT5682S_CTRL_MB1_REG | RT5682S_CTRL_MB2_REG);
		snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_1,
			RT5682S_SAR_BUTDET_MASK | RT5682S_SAR_BUTDET_POW_MASK |
			RT5682S_SAR_SEL_MB1_2_CTL_MASK, RT5682S_SAR_BUTDET_DIS |
			RT5682S_SAR_BUTDET_POW_SAV | RT5682S_SAR_SEL_MB1_2_MANU);
		usleep_range(5000, 5500);
		snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_1,
			RT5682S_SAR_BUTDET_MASK, RT5682S_SAR_BUTDET_EN);
		usleep_range(5000, 5500);
		snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_2,
			RT5682S_SAR_ADC_PSV_MASK, RT5682S_SAR_ADC_PSV_ENTRY);
		break;
	case SAR_PWR_NORMAL:
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_3,
			RT5682S_CBJ_IN_BUF_MASK, RT5682S_CBJ_IN_BUF_EN);
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_1,
			RT5682S_MB1_PATH_MASK | RT5682S_MB2_PATH_MASK,
			RT5682S_CTRL_MB1_FSM | RT5682S_CTRL_MB2_FSM);
		snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_1,
			RT5682S_SAR_SEL_MB1_2_CTL_MASK, RT5682S_SAR_SEL_MB1_2_AUTO);
		usleep_range(5000, 5500);
		snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_1,
			RT5682S_SAR_BUTDET_MASK | RT5682S_SAR_BUTDET_POW_MASK,
			RT5682S_SAR_BUTDET_EN | RT5682S_SAR_BUTDET_POW_NORM);
		break;
	case SAR_PWR_OFF:
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_1,
			RT5682S_MB1_PATH_MASK | RT5682S_MB2_PATH_MASK,
			RT5682S_CTRL_MB1_FSM | RT5682S_CTRL_MB2_FSM);
		snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_1,
			RT5682S_SAR_BUTDET_MASK | RT5682S_SAR_BUTDET_POW_MASK |
			RT5682S_SAR_SEL_MB1_2_CTL_MASK, RT5682S_SAR_BUTDET_DIS |
			RT5682S_SAR_BUTDET_POW_SAV | RT5682S_SAR_SEL_MB1_2_MANU);
		break;
	default:
		dev_err(component->dev, "Invalid SAR Power mode: %d\n", mode);
		break;
	}

	mutex_unlock(&rt5682s->sar_mutex);
}

static void rt5682s_enable_push_button_irq(struct snd_soc_component *component)
{
	snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_13,
		RT5682S_SAR_SOUR_MASK, RT5682S_SAR_SOUR_BTN);
	snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_1,
		RT5682S_SAR_BUTDET_MASK | RT5682S_SAR_BUTDET_POW_MASK |
		RT5682S_SAR_SEL_MB1_2_CTL_MASK, RT5682S_SAR_BUTDET_EN |
		RT5682S_SAR_BUTDET_POW_NORM | RT5682S_SAR_SEL_MB1_2_AUTO);
	snd_soc_component_write(component, RT5682S_IL_CMD_1, 0x0040);
	snd_soc_component_update_bits(component, RT5682S_4BTN_IL_CMD_2,
		RT5682S_4BTN_IL_MASK | RT5682S_4BTN_IL_RST_MASK,
		RT5682S_4BTN_IL_EN | RT5682S_4BTN_IL_NOR);
	snd_soc_component_update_bits(component, RT5682S_IRQ_CTRL_3,
		RT5682S_IL_IRQ_MASK, RT5682S_IL_IRQ_EN);
}

static void rt5682s_disable_push_button_irq(struct snd_soc_component *component)
{
	snd_soc_component_update_bits(component, RT5682S_IRQ_CTRL_3,
		RT5682S_IL_IRQ_MASK, RT5682S_IL_IRQ_DIS);
	snd_soc_component_update_bits(component, RT5682S_4BTN_IL_CMD_2,
		RT5682S_4BTN_IL_MASK, RT5682S_4BTN_IL_DIS);
	snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_13,
		RT5682S_SAR_SOUR_MASK, RT5682S_SAR_SOUR_TYPE);
	snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_1,
		RT5682S_SAR_BUTDET_MASK | RT5682S_SAR_BUTDET_POW_MASK |
		RT5682S_SAR_SEL_MB1_2_CTL_MASK, RT5682S_SAR_BUTDET_DIS |
		RT5682S_SAR_BUTDET_POW_SAV | RT5682S_SAR_SEL_MB1_2_MANU);
}

/**
 * rt5682s_headset_detect - Detect headset.
 * @component: SoC audio component device.
 * @jack_insert: Jack insert or not.
 *
 * Detect whether is headset or not when jack inserted.
 *
 * Returns detect status.
 */
static int rt5682s_headset_detect(struct snd_soc_component *component, int jack_insert)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	unsigned int val, count;
	int jack_type = 0;

	if (jack_insert) {
		rt5682s_disable_push_button_irq(component);
		snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_1,
			RT5682S_PWR_VREF1 | RT5682S_PWR_VREF2 | RT5682S_PWR_MB,
			RT5682S_PWR_VREF1 | RT5682S_PWR_VREF2 | RT5682S_PWR_MB);
		snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_1,
			RT5682S_PWR_FV1 | RT5682S_PWR_FV2, 0);
		usleep_range(15000, 20000);
		snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_1,
			RT5682S_PWR_FV1 | RT5682S_PWR_FV2,
			RT5682S_PWR_FV1 | RT5682S_PWR_FV2);
		snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_3,
			RT5682S_PWR_CBJ, RT5682S_PWR_CBJ);
		snd_soc_component_write(component, RT5682S_SAR_IL_CMD_3, 0x0365);
		snd_soc_component_update_bits(component, RT5682S_HP_CHARGE_PUMP_2,
			RT5682S_OSW_L_MASK | RT5682S_OSW_R_MASK,
			RT5682S_OSW_L_DIS | RT5682S_OSW_R_DIS);
		snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_13,
			RT5682S_SAR_SOUR_MASK, RT5682S_SAR_SOUR_TYPE);
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_3,
			RT5682S_CBJ_IN_BUF_MASK, RT5682S_CBJ_IN_BUF_EN);
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_1,
			RT5682S_TRIG_JD_MASK, RT5682S_TRIG_JD_LOW);
		usleep_range(45000, 50000);
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_1,
			RT5682S_TRIG_JD_MASK, RT5682S_TRIG_JD_HIGH);

		count = 0;
		do {
			usleep_range(10000, 15000);
			val = snd_soc_component_read(component, RT5682S_CBJ_CTRL_2)
				& RT5682S_JACK_TYPE_MASK;
			count++;
		} while (val == 0 && count < 50);

		dev_dbg(component->dev, "%s, val=%d, count=%d\n", __func__, val, count);

		switch (val) {
		case 0x1:
		case 0x2:
			jack_type = SND_JACK_HEADSET;
			snd_soc_component_write(component, RT5682S_SAR_IL_CMD_3, 0x024c);
			snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_1,
				RT5682S_FAST_OFF_MASK, RT5682S_FAST_OFF_EN);
			snd_soc_component_update_bits(component, RT5682S_SAR_IL_CMD_1,
				RT5682S_SAR_SEL_MB1_2_MASK, val << RT5682S_SAR_SEL_MB1_2_SFT);
			rt5682s_enable_push_button_irq(component);
			rt5682s_sar_power_mode(component, SAR_PWR_SAVING);
			break;
		default:
			jack_type = SND_JACK_HEADPHONE;
			break;
		}
		snd_soc_component_update_bits(component, RT5682S_HP_CHARGE_PUMP_2,
			RT5682S_OSW_L_MASK | RT5682S_OSW_R_MASK,
			RT5682S_OSW_L_EN | RT5682S_OSW_R_EN);
		usleep_range(35000, 40000);
	} else {
		rt5682s_sar_power_mode(component, SAR_PWR_OFF);
		rt5682s_disable_push_button_irq(component);
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_1,
			RT5682S_TRIG_JD_MASK, RT5682S_TRIG_JD_LOW);

		if (!rt5682s->wclk_enabled) {
			snd_soc_component_update_bits(component,
				RT5682S_PWR_ANLG_1, RT5682S_PWR_VREF2 | RT5682S_PWR_MB, 0);
		}

		snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_3,
			RT5682S_PWR_CBJ, 0);
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_1,
			RT5682S_FAST_OFF_MASK, RT5682S_FAST_OFF_DIS);
		snd_soc_component_update_bits(component, RT5682S_CBJ_CTRL_3,
			RT5682S_CBJ_IN_BUF_MASK, RT5682S_CBJ_IN_BUF_DIS);
		jack_type = 0;
	}

	dev_dbg(component->dev, "jack_type = %d\n", jack_type);

	return jack_type;
}

static void rt5682s_jack_detect_handler(struct work_struct *work)
{
	struct rt5682s_priv *rt5682s =
		container_of(work, struct rt5682s_priv, jack_detect_work.work);
	struct snd_soc_dapm_context *dapm;
	int val, btn_type;

	if (!rt5682s->component || !rt5682s->component->card ||
	    !rt5682s->component->card->instantiated) {
		/* card not yet ready, try later */
		mod_delayed_work(system_power_efficient_wq,
				 &rt5682s->jack_detect_work, msecs_to_jiffies(15));
		return;
	}

	dapm = snd_soc_component_get_dapm(rt5682s->component);

	snd_soc_dapm_mutex_lock(dapm);
	mutex_lock(&rt5682s->calibrate_mutex);
	mutex_lock(&rt5682s->wclk_mutex);

	val = snd_soc_component_read(rt5682s->component, RT5682S_AJD1_CTRL)
		& RT5682S_JDH_RS_MASK;
	if (!val) {
		/* jack in */
		if (rt5682s->jack_type == 0) {
			/* jack was out, report jack type */
			rt5682s->jack_type = rt5682s_headset_detect(rt5682s->component, 1);
			rt5682s->irq_work_delay_time = 0;
		} else if ((rt5682s->jack_type & SND_JACK_HEADSET) == SND_JACK_HEADSET) {
			/* jack is already in, report button event */
			rt5682s->jack_type = SND_JACK_HEADSET;
			btn_type = rt5682s_button_detect(rt5682s->component);
			/**
			 * rt5682s can report three kinds of button behavior,
			 * one click, double click and hold. However,
			 * currently we will report button pressed/released
			 * event. So all the three button behaviors are
			 * treated as button pressed.
			 */
			switch (btn_type) {
			case 0x8000:
			case 0x4000:
			case 0x2000:
				rt5682s->jack_type |= SND_JACK_BTN_0;
				break;
			case 0x1000:
			case 0x0800:
			case 0x0400:
				rt5682s->jack_type |= SND_JACK_BTN_1;
				break;
			case 0x0200:
			case 0x0100:
			case 0x0080:
				rt5682s->jack_type |= SND_JACK_BTN_2;
				break;
			case 0x0040:
			case 0x0020:
			case 0x0010:
				rt5682s->jack_type |= SND_JACK_BTN_3;
				break;
			case 0x0000: /* unpressed */
				break;
			default:
				dev_err(rt5682s->component->dev,
					"Unexpected button code 0x%04x\n", btn_type);
				break;
			}
		}
	} else {
		/* jack out */
		rt5682s->jack_type = rt5682s_headset_detect(rt5682s->component, 0);
		rt5682s->irq_work_delay_time = 50;
	}

	mutex_unlock(&rt5682s->wclk_mutex);
	mutex_unlock(&rt5682s->calibrate_mutex);
	snd_soc_dapm_mutex_unlock(dapm);

	snd_soc_jack_report(rt5682s->hs_jack, rt5682s->jack_type,
		SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
		SND_JACK_BTN_2 | SND_JACK_BTN_3);

	if (rt5682s->jack_type & (SND_JACK_BTN_0 | SND_JACK_BTN_1 |
		SND_JACK_BTN_2 | SND_JACK_BTN_3))
		schedule_delayed_work(&rt5682s->jd_check_work, 0);
	else
		cancel_delayed_work_sync(&rt5682s->jd_check_work);
}

static void rt5682s_jd_check_handler(struct work_struct *work)
{
	struct rt5682s_priv *rt5682s =
		container_of(work, struct rt5682s_priv, jd_check_work.work);

	if (snd_soc_component_read(rt5682s->component, RT5682S_AJD1_CTRL) & RT5682S_JDH_RS_MASK) {
		/* jack out */
		schedule_delayed_work(&rt5682s->jack_detect_work, 0);
	} else {
		schedule_delayed_work(&rt5682s->jd_check_work, 500);
	}
}

static irqreturn_t rt5682s_irq(int irq, void *data)
{
	struct rt5682s_priv *rt5682s = data;

	mod_delayed_work(system_power_efficient_wq, &rt5682s->jack_detect_work,
		msecs_to_jiffies(rt5682s->irq_work_delay_time));

	return IRQ_HANDLED;
}

static int rt5682s_set_jack_detect(struct snd_soc_component *component,
		struct snd_soc_jack *hs_jack, void *data)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	int btndet_delay = 16;

	rt5682s->hs_jack = hs_jack;

	if (!hs_jack) {
		regmap_update_bits(rt5682s->regmap, RT5682S_IRQ_CTRL_2,
			RT5682S_JD1_EN_MASK, RT5682S_JD1_DIS);
		regmap_update_bits(rt5682s->regmap, RT5682S_RC_CLK_CTRL,
			RT5682S_POW_JDH, 0);
		cancel_delayed_work_sync(&rt5682s->jack_detect_work);

		return 0;
	}

	switch (rt5682s->pdata.jd_src) {
	case RT5682S_JD1:
		regmap_update_bits(rt5682s->regmap, RT5682S_CBJ_CTRL_5,
			RT5682S_JD_FAST_OFF_SRC_MASK, RT5682S_JD_FAST_OFF_SRC_JDH);
		regmap_update_bits(rt5682s->regmap, RT5682S_CBJ_CTRL_2,
			RT5682S_EXT_JD_SRC, RT5682S_EXT_JD_SRC_MANUAL);
		regmap_update_bits(rt5682s->regmap, RT5682S_CBJ_CTRL_1,
			RT5682S_EMB_JD_MASK | RT5682S_DET_TYPE |
			RT5682S_POL_FAST_OFF_MASK | RT5682S_MIC_CAP_MASK,
			RT5682S_EMB_JD_EN | RT5682S_DET_TYPE |
			RT5682S_POL_FAST_OFF_HIGH | RT5682S_MIC_CAP_HS);
		regmap_update_bits(rt5682s->regmap, RT5682S_SAR_IL_CMD_1,
			RT5682S_SAR_POW_MASK, RT5682S_SAR_POW_EN);
		regmap_update_bits(rt5682s->regmap, RT5682S_GPIO_CTRL_1,
			RT5682S_GP1_PIN_MASK, RT5682S_GP1_PIN_IRQ);
		regmap_update_bits(rt5682s->regmap, RT5682S_PWR_ANLG_3,
			RT5682S_PWR_BGLDO, RT5682S_PWR_BGLDO);
		regmap_update_bits(rt5682s->regmap, RT5682S_PWR_ANLG_2,
			RT5682S_PWR_JD_MASK, RT5682S_PWR_JD_ENABLE);
		regmap_update_bits(rt5682s->regmap, RT5682S_RC_CLK_CTRL,
			RT5682S_POW_IRQ | RT5682S_POW_JDH, RT5682S_POW_IRQ | RT5682S_POW_JDH);
		regmap_update_bits(rt5682s->regmap, RT5682S_IRQ_CTRL_2,
			RT5682S_JD1_EN_MASK | RT5682S_JD1_POL_MASK,
			RT5682S_JD1_EN | RT5682S_JD1_POL_NOR);
		regmap_update_bits(rt5682s->regmap, RT5682S_4BTN_IL_CMD_4,
			RT5682S_4BTN_IL_HOLD_WIN_MASK | RT5682S_4BTN_IL_CLICK_WIN_MASK,
			(btndet_delay << RT5682S_4BTN_IL_HOLD_WIN_SFT | btndet_delay));
		regmap_update_bits(rt5682s->regmap, RT5682S_4BTN_IL_CMD_5,
			RT5682S_4BTN_IL_HOLD_WIN_MASK | RT5682S_4BTN_IL_CLICK_WIN_MASK,
			(btndet_delay << RT5682S_4BTN_IL_HOLD_WIN_SFT | btndet_delay));
		regmap_update_bits(rt5682s->regmap, RT5682S_4BTN_IL_CMD_6,
			RT5682S_4BTN_IL_HOLD_WIN_MASK | RT5682S_4BTN_IL_CLICK_WIN_MASK,
			(btndet_delay << RT5682S_4BTN_IL_HOLD_WIN_SFT | btndet_delay));
		regmap_update_bits(rt5682s->regmap, RT5682S_4BTN_IL_CMD_7,
			RT5682S_4BTN_IL_HOLD_WIN_MASK | RT5682S_4BTN_IL_CLICK_WIN_MASK,
			(btndet_delay << RT5682S_4BTN_IL_HOLD_WIN_SFT | btndet_delay));

		mod_delayed_work(system_power_efficient_wq,
			&rt5682s->jack_detect_work, msecs_to_jiffies(250));
		break;

	case RT5682S_JD_NULL:
		regmap_update_bits(rt5682s->regmap, RT5682S_IRQ_CTRL_2,
			RT5682S_JD1_EN_MASK, RT5682S_JD1_DIS);
		regmap_update_bits(rt5682s->regmap, RT5682S_RC_CLK_CTRL,
			RT5682S_POW_JDH, 0);
		break;

	default:
		dev_warn(component->dev, "Wrong JD source\n");
		break;
	}

	return 0;
}

static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -9562, 75, 0);
static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -1725, 75, 0);
static const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);
static const DECLARE_TLV_DB_SCALE(cbj_bst_tlv, -1200, 150, 0);

static const struct snd_kcontrol_new rt5682s_snd_controls[] = {
	/* DAC Digital Volume */
	SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5682S_DAC1_DIG_VOL,
		RT5682S_L_VOL_SFT + 1, RT5682S_R_VOL_SFT + 1, 127, 0, dac_vol_tlv),

	/* CBJ Boost Volume */
	SOC_SINGLE_TLV("CBJ Boost Volume", RT5682S_REC_MIXER,
		RT5682S_BST_CBJ_SFT, 35, 0,  cbj_bst_tlv),

	/* ADC Digital Volume Control */
	SOC_DOUBLE("STO1 ADC Capture Switch", RT5682S_STO1_ADC_DIG_VOL,
		RT5682S_L_MUTE_SFT, RT5682S_R_MUTE_SFT, 1, 1),
	SOC_DOUBLE_TLV("STO1 ADC Capture Volume", RT5682S_STO1_ADC_DIG_VOL,
		RT5682S_L_VOL_SFT + 1, RT5682S_R_VOL_SFT + 1, 63, 0, adc_vol_tlv),

	/* ADC Boost Volume Control */
	SOC_DOUBLE_TLV("STO1 ADC Boost Gain Volume", RT5682S_STO1_ADC_BOOST,
		RT5682S_STO1_ADC_L_BST_SFT, RT5682S_STO1_ADC_R_BST_SFT, 3, 0, adc_bst_tlv),
};

/**
 * rt5682s_sel_asrc_clk_src - select ASRC clock source for a set of filters
 * @component: SoC audio component device.
 * @filter_mask: mask of filters.
 * @clk_src: clock source
 *
 * The ASRC function is for asynchronous MCLK and LRCK. Also, since RT5682S can
 * only support standard 32fs or 64fs i2s format, ASRC should be enabled to
 * support special i2s clock format such as Intel's 100fs(100 * sampling rate).
 * ASRC function will track i2s clock and generate a corresponding system clock
 * for codec. This function provides an API to select the clock source for a
 * set of filters specified by the mask. And the component driver will turn on
 * ASRC for these filters if ASRC is selected as their clock source.
 */
int rt5682s_sel_asrc_clk_src(struct snd_soc_component *component,
		unsigned int filter_mask, unsigned int clk_src)
{
	switch (clk_src) {
	case RT5682S_CLK_SEL_SYS:
	case RT5682S_CLK_SEL_I2S1_ASRC:
	case RT5682S_CLK_SEL_I2S2_ASRC:
		break;

	default:
		return -EINVAL;
	}

	if (filter_mask & RT5682S_DA_STEREO1_FILTER) {
		snd_soc_component_update_bits(component, RT5682S_PLL_TRACK_2,
			RT5682S_FILTER_CLK_SEL_MASK, clk_src << RT5682S_FILTER_CLK_SEL_SFT);
	}

	if (filter_mask & RT5682S_AD_STEREO1_FILTER) {
		snd_soc_component_update_bits(component, RT5682S_PLL_TRACK_3,
			RT5682S_FILTER_CLK_SEL_MASK, clk_src << RT5682S_FILTER_CLK_SEL_SFT);
	}

	snd_soc_component_update_bits(component, RT5682S_PLL_TRACK_11,
		RT5682S_ASRCIN_AUTO_CLKOUT_MASK, RT5682S_ASRCIN_AUTO_CLKOUT_EN);

	return 0;
}
EXPORT_SYMBOL_GPL(rt5682s_sel_asrc_clk_src);

static int rt5682s_div_sel(struct rt5682s_priv *rt5682s,
		int target, const int div[], int size)
{
	int i;

	if (rt5682s->sysclk < target) {
		dev_err(rt5682s->component->dev,
			"sysclk rate %d is too low\n", rt5682s->sysclk);
		return 0;
	}

	for (i = 0; i < size - 1; i++) {
		dev_dbg(rt5682s->component->dev, "div[%d]=%d\n", i, div[i]);
		if (target * div[i] == rt5682s->sysclk)
			return i;
		if (target * div[i + 1] > rt5682s->sysclk) {
			dev_dbg(rt5682s->component->dev,
				"can't find div for sysclk %d\n", rt5682s->sysclk);
			return i;
		}
	}

	if (target * div[i] < rt5682s->sysclk)
		dev_err(rt5682s->component->dev,
			"sysclk rate %d is too high\n", rt5682s->sysclk);

	return size - 1;
}

static int get_clk_info(int sclk, int rate)
{
	int i;
	static const int pd[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48};

	if (sclk <= 0 || rate <= 0)
		return -EINVAL;

	rate = rate << 8;
	for (i = 0; i < ARRAY_SIZE(pd); i++)
		if (sclk == rate * pd[i])
			return i;

	return -EINVAL;
}

/**
 * set_dmic_clk - Set parameter of dmic.
 *
 * @w: DAPM widget.
 * @kcontrol: The kcontrol of this widget.
 * @event: Event id.
 *
 * Choose dmic clock between 1MHz and 3MHz.
 * It is better for clock to approximate 3MHz.
 */
static int set_dmic_clk(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	int idx, dmic_clk_rate = 3072000;
	static const int div[] = {2, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128};

	if (rt5682s->pdata.dmic_clk_rate)
		dmic_clk_rate = rt5682s->pdata.dmic_clk_rate;

	idx = rt5682s_div_sel(rt5682s, dmic_clk_rate, div, ARRAY_SIZE(div));

	snd_soc_component_update_bits(component, RT5682S_DMIC_CTRL_1,
		RT5682S_DMIC_CLK_MASK, idx << RT5682S_DMIC_CLK_SFT);

	return 0;
}


static int rt5682s_set_pllb_power(struct rt5682s_priv *rt5682s, int on)
{
	struct snd_soc_component *component = rt5682s->component;

	if (on) {
		snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_3,
			RT5682S_PWR_LDO_PLLB | RT5682S_PWR_BIAS_PLLB | RT5682S_PWR_PLLB,
			RT5682S_PWR_LDO_PLLB | RT5682S_PWR_BIAS_PLLB | RT5682S_PWR_PLLB);
		snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_3,
			RT5682S_RSTB_PLLB, RT5682S_RSTB_PLLB);
	} else {
		snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_3,
			RT5682S_PWR_LDO_PLLB | RT5682S_PWR_BIAS_PLLB |
			RT5682S_RSTB_PLLB | RT5682S_PWR_PLLB, 0);
	}

	return 0;
}

static int set_pllb_event(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	int on = 0;

	if (rt5682s->wclk_enabled)
		return 0;

	if (SND_SOC_DAPM_EVENT_ON(event))
		on = 1;

	rt5682s_set_pllb_power(rt5682s, on);

	return 0;
}

static void rt5682s_set_filter_clk(struct rt5682s_priv *rt5682s, int reg, int ref)
{
	struct snd_soc_component *component = rt5682s->component;
	int idx;
	static const int div_f[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48};
	static const int div_o[] = {1, 2, 4, 6, 8, 12, 16, 24, 32, 48};

	idx = rt5682s_div_sel(rt5682s, ref, div_f, ARRAY_SIZE(div_f));

	snd_soc_component_update_bits(component, reg,
		RT5682S_FILTER_CLK_DIV_MASK, idx << RT5682S_FILTER_CLK_DIV_SFT);

	/* select over sample rate */
	for (idx = 0; idx < ARRAY_SIZE(div_o); idx++) {
		if (rt5682s->sysclk <= 12288000 * div_o[idx])
			break;
	}

	snd_soc_component_update_bits(component, RT5682S_ADDA_CLK_1,
		RT5682S_ADC_OSR_MASK | RT5682S_DAC_OSR_MASK,
		(idx << RT5682S_ADC_OSR_SFT) | (idx << RT5682S_DAC_OSR_SFT));
}

static int set_filter_clk(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	int ref, reg, val;

	val = snd_soc_component_read(component, RT5682S_GPIO_CTRL_1)
			& RT5682S_GP4_PIN_MASK;

	if (w->shift == RT5682S_PWR_ADC_S1F_BIT && val == RT5682S_GP4_PIN_ADCDAT2)
		ref = 256 * rt5682s->lrck[RT5682S_AIF2];
	else
		ref = 256 * rt5682s->lrck[RT5682S_AIF1];

	if (w->shift == RT5682S_PWR_ADC_S1F_BIT)
		reg = RT5682S_PLL_TRACK_3;
	else
		reg = RT5682S_PLL_TRACK_2;

	rt5682s_set_filter_clk(rt5682s, reg, ref);

	return 0;
}

static int set_dmic_power(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	unsigned int delay = 50, val;

	if (rt5682s->pdata.dmic_delay)
		delay = rt5682s->pdata.dmic_delay;

	switch (event) {
	case SND_SOC_DAPM_POST_PMU:
		val = (snd_soc_component_read(component, RT5682S_GLB_CLK)
			& RT5682S_SCLK_SRC_MASK) >> RT5682S_SCLK_SRC_SFT;
		if (val == RT5682S_CLK_SRC_PLL1 || val == RT5682S_CLK_SRC_PLL2)
			snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_1,
				RT5682S_PWR_VREF2 | RT5682S_PWR_MB,
				RT5682S_PWR_VREF2 | RT5682S_PWR_MB);

		/*Add delay to avoid pop noise*/
		msleep(delay);
		break;

	case SND_SOC_DAPM_POST_PMD:
		if (!rt5682s->jack_type && !rt5682s->wclk_enabled) {
			snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_1,
				RT5682S_PWR_VREF2 | RT5682S_PWR_MB, 0);
		}
		break;
	}

	return 0;
}

static void rt5682s_set_i2s(struct rt5682s_priv *rt5682s, int id, int on)
{
	struct snd_soc_component *component = rt5682s->component;
	int pre_div;
	unsigned int p_reg, p_mask, p_sft;
	unsigned int c_reg, c_mask, c_sft;

	if (id == RT5682S_AIF1) {
		c_reg = RT5682S_ADDA_CLK_1;
		c_mask = RT5682S_I2S_M_D_MASK;
		c_sft = RT5682S_I2S_M_D_SFT;
		p_reg = RT5682S_PWR_DIG_1;
		p_mask = RT5682S_PWR_I2S1;
		p_sft = RT5682S_PWR_I2S1_BIT;
	} else {
		c_reg = RT5682S_I2S2_M_CLK_CTRL_1;
		c_mask = RT5682S_I2S2_M_D_MASK;
		c_sft = RT5682S_I2S2_M_D_SFT;
		p_reg = RT5682S_PWR_DIG_1;
		p_mask = RT5682S_PWR_I2S2;
		p_sft = RT5682S_PWR_I2S2_BIT;
	}

	if (on && rt5682s->master[id]) {
		pre_div = get_clk_info(rt5682s->sysclk, rt5682s->lrck[id]);
		if (pre_div < 0) {
			dev_err(component->dev, "get pre_div failed\n");
			return;
		}

		dev_dbg(component->dev, "lrck is %dHz and pre_div is %d for iis %d master\n",
			rt5682s->lrck[id], pre_div, id);
		snd_soc_component_update_bits(component, c_reg, c_mask, pre_div << c_sft);
	}

	snd_soc_component_update_bits(component, p_reg, p_mask, on << p_sft);
}

static int set_i2s_event(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	int on = 0;

	if (SND_SOC_DAPM_EVENT_ON(event))
		on = 1;

	if (!strcmp(w->name, "I2S1") && !rt5682s->wclk_enabled)
		rt5682s_set_i2s(rt5682s, RT5682S_AIF1, on);
	else if (!strcmp(w->name, "I2S2"))
		rt5682s_set_i2s(rt5682s, RT5682S_AIF2, on);

	return 0;
}

static int is_sys_clk_from_plla(struct snd_soc_dapm_widget *w,
		struct snd_soc_dapm_widget *sink)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	if ((rt5682s->sysclk_src == RT5682S_CLK_SRC_PLL1) ||
	    (rt5682s->sysclk_src == RT5682S_CLK_SRC_PLL2 && rt5682s->pll_comb == USE_PLLAB))
		return 1;

	return 0;
}

static int is_sys_clk_from_pllb(struct snd_soc_dapm_widget *w,
		struct snd_soc_dapm_widget *sink)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	if (rt5682s->sysclk_src == RT5682S_CLK_SRC_PLL2)
		return 1;

	return 0;
}

static int is_using_asrc(struct snd_soc_dapm_widget *w,
		struct snd_soc_dapm_widget *sink)
{
	unsigned int reg, sft, val;
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);

	switch (w->shift) {
	case RT5682S_ADC_STO1_ASRC_SFT:
		reg = RT5682S_PLL_TRACK_3;
		sft = RT5682S_FILTER_CLK_SEL_SFT;
		break;
	case RT5682S_DAC_STO1_ASRC_SFT:
		reg = RT5682S_PLL_TRACK_2;
		sft = RT5682S_FILTER_CLK_SEL_SFT;
		break;
	default:
		return 0;
	}

	val = (snd_soc_component_read(component, reg) >> sft) & 0xf;
	switch (val) {
	case RT5682S_CLK_SEL_I2S1_ASRC:
	case RT5682S_CLK_SEL_I2S2_ASRC:
		return 1;
	default:
		return 0;
	}
}

static int rt5682s_hp_amp_event(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);

	switch (event) {
	case SND_SOC_DAPM_POST_PMU:
		snd_soc_component_update_bits(component, RT5682S_DEPOP_1,
			RT5682S_OUT_HP_L_EN | RT5682S_OUT_HP_R_EN,
			RT5682S_OUT_HP_L_EN | RT5682S_OUT_HP_R_EN);
		usleep_range(15000, 20000);
		snd_soc_component_update_bits(component, RT5682S_DEPOP_1,
			RT5682S_LDO_PUMP_EN | RT5682S_PUMP_EN |
			RT5682S_CAPLESS_L_EN | RT5682S_CAPLESS_R_EN,
			RT5682S_LDO_PUMP_EN | RT5682S_PUMP_EN |
			RT5682S_CAPLESS_L_EN | RT5682S_CAPLESS_R_EN);
		snd_soc_component_write(component, RT5682S_BIAS_CUR_CTRL_11, 0x6666);
		snd_soc_component_write(component, RT5682S_BIAS_CUR_CTRL_12, 0xa82a);

		snd_soc_component_update_bits(component, RT5682S_HP_CTRL_2,
			RT5682S_HPO_L_PATH_MASK | RT5682S_HPO_R_PATH_MASK |
			RT5682S_HPO_SEL_IP_EN_SW, RT5682S_HPO_L_PATH_EN |
			RT5682S_HPO_R_PATH_EN | RT5682S_HPO_IP_EN_GATING);
		usleep_range(5000, 10000);
		snd_soc_component_update_bits(component, RT5682S_HP_AMP_DET_CTL_1,
			RT5682S_CP_SW_SIZE_MASK, RT5682S_CP_SW_SIZE_L | RT5682S_CP_SW_SIZE_S);
		break;

	case SND_SOC_DAPM_POST_PMD:
		snd_soc_component_update_bits(component, RT5682S_HP_CTRL_2,
			RT5682S_HPO_L_PATH_MASK | RT5682S_HPO_R_PATH_MASK |
			RT5682S_HPO_SEL_IP_EN_SW, 0);
		snd_soc_component_update_bits(component, RT5682S_HP_AMP_DET_CTL_1,
			RT5682S_CP_SW_SIZE_MASK, RT5682S_CP_SW_SIZE_M);
		snd_soc_component_update_bits(component, RT5682S_DEPOP_1,
			RT5682S_LDO_PUMP_EN | RT5682S_PUMP_EN |
			RT5682S_CAPLESS_L_EN | RT5682S_CAPLESS_R_EN, 0);
		snd_soc_component_update_bits(component, RT5682S_DEPOP_1,
			RT5682S_OUT_HP_L_EN | RT5682S_OUT_HP_R_EN, 0);
		break;
	}

	return 0;
}

static int rt5682s_stereo1_adc_mixl_event(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	unsigned int delay = 0;

	if (rt5682s->pdata.amic_delay)
		delay = rt5682s->pdata.amic_delay;

	switch (event) {
	case SND_SOC_DAPM_POST_PMU:
		msleep(delay);
		snd_soc_component_update_bits(component, RT5682S_STO1_ADC_DIG_VOL,
			RT5682S_L_MUTE, 0);
		break;
	case SND_SOC_DAPM_PRE_PMD:
		snd_soc_component_update_bits(component, RT5682S_STO1_ADC_DIG_VOL,
			RT5682S_L_MUTE, RT5682S_L_MUTE);
		break;
	}

	return 0;
}

static int sar_power_event(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	if ((rt5682s->jack_type & SND_JACK_HEADSET) != SND_JACK_HEADSET)
		return 0;

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
		rt5682s_sar_power_mode(component, SAR_PWR_NORMAL);
		break;
	case SND_SOC_DAPM_POST_PMD:
		rt5682s_sar_power_mode(component, SAR_PWR_SAVING);
		break;
	}

	return 0;
}

/* Interface data select */
static const char * const rt5682s_data_select[] = {
	"L/R", "R/L", "L/L", "R/R"
};

static SOC_ENUM_SINGLE_DECL(rt5682s_if2_adc_enum, RT5682S_DIG_INF2_DATA,
	RT5682S_IF2_ADC_SEL_SFT, rt5682s_data_select);

static SOC_ENUM_SINGLE_DECL(rt5682s_if1_01_adc_enum, RT5682S_TDM_ADDA_CTRL_1,
	RT5682S_IF1_ADC1_SEL_SFT, rt5682s_data_select);

static SOC_ENUM_SINGLE_DECL(rt5682s_if1_23_adc_enum, RT5682S_TDM_ADDA_CTRL_1,
	RT5682S_IF1_ADC2_SEL_SFT, rt5682s_data_select);

static SOC_ENUM_SINGLE_DECL(rt5682s_if1_45_adc_enum, RT5682S_TDM_ADDA_CTRL_1,
	RT5682S_IF1_ADC3_SEL_SFT, rt5682s_data_select);

static SOC_ENUM_SINGLE_DECL(rt5682s_if1_67_adc_enum, RT5682S_TDM_ADDA_CTRL_1,
	RT5682S_IF1_ADC4_SEL_SFT, rt5682s_data_select);

static const struct snd_kcontrol_new rt5682s_if2_adc_swap_mux =
	SOC_DAPM_ENUM("IF2 ADC Swap Mux", rt5682s_if2_adc_enum);

static const struct snd_kcontrol_new rt5682s_if1_01_adc_swap_mux =
	SOC_DAPM_ENUM("IF1 01 ADC Swap Mux", rt5682s_if1_01_adc_enum);

static const struct snd_kcontrol_new rt5682s_if1_23_adc_swap_mux =
	SOC_DAPM_ENUM("IF1 23 ADC Swap Mux", rt5682s_if1_23_adc_enum);

static const struct snd_kcontrol_new rt5682s_if1_45_adc_swap_mux =
	SOC_DAPM_ENUM("IF1 45 ADC Swap Mux", rt5682s_if1_45_adc_enum);

static const struct snd_kcontrol_new rt5682s_if1_67_adc_swap_mux =
	SOC_DAPM_ENUM("IF1 67 ADC Swap Mux", rt5682s_if1_67_adc_enum);

/* Digital Mixer */
static const struct snd_kcontrol_new rt5682s_sto1_adc_l_mix[] = {
	SOC_DAPM_SINGLE("ADC1 Switch", RT5682S_STO1_ADC_MIXER,
			RT5682S_M_STO1_ADC_L1_SFT, 1, 1),
	SOC_DAPM_SINGLE("ADC2 Switch", RT5682S_STO1_ADC_MIXER,
			RT5682S_M_STO1_ADC_L2_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5682s_sto1_adc_r_mix[] = {
	SOC_DAPM_SINGLE("ADC1 Switch", RT5682S_STO1_ADC_MIXER,
			RT5682S_M_STO1_ADC_R1_SFT, 1, 1),
	SOC_DAPM_SINGLE("ADC2 Switch", RT5682S_STO1_ADC_MIXER,
			RT5682S_M_STO1_ADC_R2_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5682s_dac_l_mix[] = {
	SOC_DAPM_SINGLE("Stereo ADC Switch", RT5682S_AD_DA_MIXER,
			RT5682S_M_ADCMIX_L_SFT, 1, 1),
	SOC_DAPM_SINGLE("DAC1 Switch", RT5682S_AD_DA_MIXER,
			RT5682S_M_DAC1_L_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5682s_dac_r_mix[] = {
	SOC_DAPM_SINGLE("Stereo ADC Switch", RT5682S_AD_DA_MIXER,
			RT5682S_M_ADCMIX_R_SFT, 1, 1),
	SOC_DAPM_SINGLE("DAC1 Switch", RT5682S_AD_DA_MIXER,
			RT5682S_M_DAC1_R_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5682s_sto1_dac_l_mix[] = {
	SOC_DAPM_SINGLE("DAC L1 Switch", RT5682S_STO1_DAC_MIXER,
			RT5682S_M_DAC_L1_STO_L_SFT, 1, 1),
	SOC_DAPM_SINGLE("DAC R1 Switch", RT5682S_STO1_DAC_MIXER,
			RT5682S_M_DAC_R1_STO_L_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5682s_sto1_dac_r_mix[] = {
	SOC_DAPM_SINGLE("DAC L1 Switch", RT5682S_STO1_DAC_MIXER,
			RT5682S_M_DAC_L1_STO_R_SFT, 1, 1),
	SOC_DAPM_SINGLE("DAC R1 Switch", RT5682S_STO1_DAC_MIXER,
			RT5682S_M_DAC_R1_STO_R_SFT, 1, 1),
};

/* Analog Input Mixer */
static const struct snd_kcontrol_new rt5682s_rec1_l_mix[] = {
	SOC_DAPM_SINGLE("CBJ Switch", RT5682S_REC_MIXER,
			RT5682S_M_CBJ_RM1_L_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5682s_rec1_r_mix[] = {
	SOC_DAPM_SINGLE("CBJ Switch", RT5682S_REC_MIXER,
			RT5682S_M_CBJ_RM1_R_SFT, 1, 1),
};

/* STO1 ADC1 Source */
/* MX-26 [13] [5] */
static const char * const rt5682s_sto1_adc1_src[] = {
	"DAC MIX", "ADC"
};

static SOC_ENUM_SINGLE_DECL(rt5682s_sto1_adc1l_enum, RT5682S_STO1_ADC_MIXER,
	RT5682S_STO1_ADC1L_SRC_SFT, rt5682s_sto1_adc1_src);

static const struct snd_kcontrol_new rt5682s_sto1_adc1l_mux =
	SOC_DAPM_ENUM("Stereo1 ADC1L Source", rt5682s_sto1_adc1l_enum);

static SOC_ENUM_SINGLE_DECL(rt5682s_sto1_adc1r_enum, RT5682S_STO1_ADC_MIXER,
	RT5682S_STO1_ADC1R_SRC_SFT, rt5682s_sto1_adc1_src);

static const struct snd_kcontrol_new rt5682s_sto1_adc1r_mux =
	SOC_DAPM_ENUM("Stereo1 ADC1L Source", rt5682s_sto1_adc1r_enum);

/* STO1 ADC Source */
/* MX-26 [11:10] [3:2] */
static const char * const rt5682s_sto1_adc_src[] = {
	"ADC1 L", "ADC1 R"
};

static SOC_ENUM_SINGLE_DECL(rt5682s_sto1_adcl_enum, RT5682S_STO1_ADC_MIXER,
	RT5682S_STO1_ADCL_SRC_SFT, rt5682s_sto1_adc_src);

static const struct snd_kcontrol_new rt5682s_sto1_adcl_mux =
	SOC_DAPM_ENUM("Stereo1 ADCL Source", rt5682s_sto1_adcl_enum);

static SOC_ENUM_SINGLE_DECL(rt5682s_sto1_adcr_enum, RT5682S_STO1_ADC_MIXER,
	RT5682S_STO1_ADCR_SRC_SFT, rt5682s_sto1_adc_src);

static const struct snd_kcontrol_new rt5682s_sto1_adcr_mux =
	SOC_DAPM_ENUM("Stereo1 ADCR Source", rt5682s_sto1_adcr_enum);

/* STO1 ADC2 Source */
/* MX-26 [12] [4] */
static const char * const rt5682s_sto1_adc2_src[] = {
	"DAC MIX", "DMIC"
};

static SOC_ENUM_SINGLE_DECL(rt5682s_sto1_adc2l_enum, RT5682S_STO1_ADC_MIXER,
	RT5682S_STO1_ADC2L_SRC_SFT, rt5682s_sto1_adc2_src);

static const struct snd_kcontrol_new rt5682s_sto1_adc2l_mux =
	SOC_DAPM_ENUM("Stereo1 ADC2L Source", rt5682s_sto1_adc2l_enum);

static SOC_ENUM_SINGLE_DECL(rt5682s_sto1_adc2r_enum, RT5682S_STO1_ADC_MIXER,
	RT5682S_STO1_ADC2R_SRC_SFT, rt5682s_sto1_adc2_src);

static const struct snd_kcontrol_new rt5682s_sto1_adc2r_mux =
	SOC_DAPM_ENUM("Stereo1 ADC2R Source", rt5682s_sto1_adc2r_enum);

/* MX-79 [6:4] I2S1 ADC data location */
static const unsigned int rt5682s_if1_adc_slot_values[] = {
	0, 2, 4, 6,
};

static const char * const rt5682s_if1_adc_slot_src[] = {
	"Slot 0", "Slot 2", "Slot 4", "Slot 6"
};

static SOC_VALUE_ENUM_SINGLE_DECL(rt5682s_if1_adc_slot_enum,
	RT5682S_TDM_CTRL, RT5682S_TDM_ADC_LCA_SFT, RT5682S_TDM_ADC_LCA_MASK,
	rt5682s_if1_adc_slot_src, rt5682s_if1_adc_slot_values);

static const struct snd_kcontrol_new rt5682s_if1_adc_slot_mux =
	SOC_DAPM_ENUM("IF1 ADC Slot location", rt5682s_if1_adc_slot_enum);

/* Analog DAC L1 Source, Analog DAC R1 Source*/
/* MX-2B [4], MX-2B [0]*/
static const char * const rt5682s_alg_dac1_src[] = {
	"Stereo1 DAC Mixer", "DAC1"
};

static SOC_ENUM_SINGLE_DECL(rt5682s_alg_dac_l1_enum, RT5682S_A_DAC1_MUX,
	RT5682S_A_DACL1_SFT, rt5682s_alg_dac1_src);

static const struct snd_kcontrol_new rt5682s_alg_dac_l1_mux =
	SOC_DAPM_ENUM("Analog DAC L1 Source", rt5682s_alg_dac_l1_enum);

static SOC_ENUM_SINGLE_DECL(rt5682s_alg_dac_r1_enum, RT5682S_A_DAC1_MUX,
	RT5682S_A_DACR1_SFT, rt5682s_alg_dac1_src);

static const struct snd_kcontrol_new rt5682s_alg_dac_r1_mux =
	SOC_DAPM_ENUM("Analog DAC R1 Source", rt5682s_alg_dac_r1_enum);

static const unsigned int rt5682s_adcdat_pin_values[] = {
	1, 3,
};

static const char * const rt5682s_adcdat_pin_select[] = {
	"ADCDAT1", "ADCDAT2",
};

static SOC_VALUE_ENUM_SINGLE_DECL(rt5682s_adcdat_pin_enum,
	RT5682S_GPIO_CTRL_1, RT5682S_GP4_PIN_SFT, RT5682S_GP4_PIN_MASK,
	rt5682s_adcdat_pin_select, rt5682s_adcdat_pin_values);

static const struct snd_kcontrol_new rt5682s_adcdat_pin_ctrl =
	SOC_DAPM_ENUM("ADCDAT", rt5682s_adcdat_pin_enum);

static const struct snd_soc_dapm_widget rt5682s_dapm_widgets[] = {
	SND_SOC_DAPM_SUPPLY("LDO MB1", RT5682S_PWR_ANLG_3,
		RT5682S_PWR_LDO_MB1_BIT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("LDO MB2", RT5682S_PWR_ANLG_3,
		RT5682S_PWR_LDO_MB2_BIT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("LDO", RT5682S_PWR_ANLG_3,
		RT5682S_PWR_LDO_BIT, 0, NULL, 0),

	/* PLL Powers */
	SND_SOC_DAPM_SUPPLY_S("PLLA_LDO", 0, RT5682S_PWR_ANLG_3,
		RT5682S_PWR_LDO_PLLA_BIT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY_S("PLLA_BIAS", 0, RT5682S_PWR_ANLG_3,
		RT5682S_PWR_BIAS_PLLA_BIT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY_S("PLLA", 0, RT5682S_PWR_ANLG_3,
		RT5682S_PWR_PLLA_BIT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY_S("PLLA_RST", 1, RT5682S_PWR_ANLG_3,
		RT5682S_RSTB_PLLA_BIT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("PLLB", SND_SOC_NOPM, 0, 0,
		set_pllb_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),

	/* ASRC */
	SND_SOC_DAPM_SUPPLY_S("DAC STO1 ASRC", 1, RT5682S_PLL_TRACK_1,
		RT5682S_DAC_STO1_ASRC_SFT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY_S("ADC STO1 ASRC", 1, RT5682S_PLL_TRACK_1,
		RT5682S_ADC_STO1_ASRC_SFT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY_S("AD ASRC", 1, RT5682S_PLL_TRACK_1,
		RT5682S_AD_ASRC_SFT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY_S("DA ASRC", 1, RT5682S_PLL_TRACK_1,
		RT5682S_DA_ASRC_SFT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY_S("DMIC ASRC", 1, RT5682S_PLL_TRACK_1,
		RT5682S_DMIC_ASRC_SFT, 0, NULL, 0),

	/* Input Side */
	SND_SOC_DAPM_SUPPLY("MICBIAS1", RT5682S_PWR_ANLG_2,
		RT5682S_PWR_MB1_BIT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("MICBIAS2", RT5682S_PWR_ANLG_2,
		RT5682S_PWR_MB2_BIT, 0, NULL, 0),

	/* Input Lines */
	SND_SOC_DAPM_INPUT("DMIC L1"),
	SND_SOC_DAPM_INPUT("DMIC R1"),

	SND_SOC_DAPM_INPUT("IN1P"),

	SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
		set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
	SND_SOC_DAPM_SUPPLY("DMIC1 Power", RT5682S_DMIC_CTRL_1, RT5682S_DMIC_1_EN_SFT, 0,
		set_dmic_power, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),

	/* Boost */
	SND_SOC_DAPM_PGA("BST1 CBJ", SND_SOC_NOPM, 0, 0, NULL, 0),

	/* REC Mixer */
	SND_SOC_DAPM_MIXER("RECMIX1L", SND_SOC_NOPM, 0, 0, rt5682s_rec1_l_mix,
		ARRAY_SIZE(rt5682s_rec1_l_mix)),
	SND_SOC_DAPM_MIXER("RECMIX1R", SND_SOC_NOPM, 0, 0, rt5682s_rec1_r_mix,
		ARRAY_SIZE(rt5682s_rec1_r_mix)),
	SND_SOC_DAPM_SUPPLY("RECMIX1L Power", RT5682S_CAL_REC,
		RT5682S_PWR_RM1_L_BIT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("RECMIX1R Power", RT5682S_CAL_REC,
		RT5682S_PWR_RM1_R_BIT, 0, NULL, 0),

	/* ADCs */
	SND_SOC_DAPM_ADC("ADC1 L", NULL, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_ADC("ADC1 R", NULL, SND_SOC_NOPM, 0, 0),

	SND_SOC_DAPM_SUPPLY("ADC1 L Power", RT5682S_PWR_DIG_1,
		RT5682S_PWR_ADC_L1_BIT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("ADC1 R Power", RT5682S_PWR_DIG_1,
		RT5682S_PWR_ADC_R1_BIT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("ADC1 clock", RT5682S_CHOP_ADC,
		RT5682S_CKGEN_ADC1_SFT, 0, NULL, 0),

	/* ADC Mux */
	SND_SOC_DAPM_MUX("Stereo1 ADC L1 Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_sto1_adc1l_mux),
	SND_SOC_DAPM_MUX("Stereo1 ADC R1 Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_sto1_adc1r_mux),
	SND_SOC_DAPM_MUX("Stereo1 ADC L2 Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_sto1_adc2l_mux),
	SND_SOC_DAPM_MUX("Stereo1 ADC R2 Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_sto1_adc2r_mux),
	SND_SOC_DAPM_MUX("Stereo1 ADC L Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_sto1_adcl_mux),
	SND_SOC_DAPM_MUX("Stereo1 ADC R Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_sto1_adcr_mux),
	SND_SOC_DAPM_MUX("IF1_ADC Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_if1_adc_slot_mux),

	/* ADC Mixer */
	SND_SOC_DAPM_SUPPLY("ADC Stereo1 Filter", RT5682S_PWR_DIG_2,
		RT5682S_PWR_ADC_S1F_BIT, 0, set_filter_clk, SND_SOC_DAPM_PRE_PMU),
	SND_SOC_DAPM_MIXER_E("Stereo1 ADC MIXL", SND_SOC_NOPM, 0, 0,
		rt5682s_sto1_adc_l_mix, ARRAY_SIZE(rt5682s_sto1_adc_l_mix),
		rt5682s_stereo1_adc_mixl_event,
		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
	SND_SOC_DAPM_MIXER("Stereo1 ADC MIXR", RT5682S_STO1_ADC_DIG_VOL,
		RT5682S_R_MUTE_SFT, 1, rt5682s_sto1_adc_r_mix,
		ARRAY_SIZE(rt5682s_sto1_adc_r_mix)),

	/* ADC PGA */
	SND_SOC_DAPM_PGA("Stereo1 ADC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),

	/* Digital Interface */
	SND_SOC_DAPM_SUPPLY("I2S1", SND_SOC_NOPM, 0, 0,
		set_i2s_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
	SND_SOC_DAPM_SUPPLY("I2S2", SND_SOC_NOPM, 0, 0,
		set_i2s_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
	SND_SOC_DAPM_PGA("IF1 DAC1 L", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_PGA("IF1 DAC1 R", SND_SOC_NOPM, 0, 0, NULL, 0),

	/* Digital Interface Select */
	SND_SOC_DAPM_MUX("IF1 01 ADC Swap Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_if1_01_adc_swap_mux),
	SND_SOC_DAPM_MUX("IF1 23 ADC Swap Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_if1_23_adc_swap_mux),
	SND_SOC_DAPM_MUX("IF1 45 ADC Swap Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_if1_45_adc_swap_mux),
	SND_SOC_DAPM_MUX("IF1 67 ADC Swap Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_if1_67_adc_swap_mux),
	SND_SOC_DAPM_MUX("IF2 ADC Swap Mux", SND_SOC_NOPM, 0, 0,
		&rt5682s_if2_adc_swap_mux),

	SND_SOC_DAPM_MUX("ADCDAT Mux", SND_SOC_NOPM, 0, 0, &rt5682s_adcdat_pin_ctrl),

	/* Audio Interface */
	SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, RT5682S_I2S1_SDP,
		RT5682S_SEL_ADCDAT_SFT, 1),
	SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, RT5682S_I2S2_SDP,
		RT5682S_I2S2_PIN_CFG_SFT, 1),
	SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),

	/* Output Side */
	/* DAC mixer before sound effect  */
	SND_SOC_DAPM_MIXER("DAC1 MIXL", SND_SOC_NOPM, 0, 0,
		rt5682s_dac_l_mix, ARRAY_SIZE(rt5682s_dac_l_mix)),
	SND_SOC_DAPM_MIXER("DAC1 MIXR", SND_SOC_NOPM, 0, 0,
		rt5682s_dac_r_mix, ARRAY_SIZE(rt5682s_dac_r_mix)),

	/* DAC channel Mux */
	SND_SOC_DAPM_MUX("DAC L1 Source", SND_SOC_NOPM, 0, 0, &rt5682s_alg_dac_l1_mux),
	SND_SOC_DAPM_MUX("DAC R1 Source", SND_SOC_NOPM, 0, 0, &rt5682s_alg_dac_r1_mux),

	/* DAC Mixer */
	SND_SOC_DAPM_SUPPLY("DAC Stereo1 Filter", RT5682S_PWR_DIG_2,
		RT5682S_PWR_DAC_S1F_BIT, 0, set_filter_clk, SND_SOC_DAPM_PRE_PMU),
	SND_SOC_DAPM_MIXER("Stereo1 DAC MIXL", SND_SOC_NOPM, 0, 0,
		rt5682s_sto1_dac_l_mix, ARRAY_SIZE(rt5682s_sto1_dac_l_mix)),
	SND_SOC_DAPM_MIXER("Stereo1 DAC MIXR", SND_SOC_NOPM, 0, 0,
		rt5682s_sto1_dac_r_mix, ARRAY_SIZE(rt5682s_sto1_dac_r_mix)),

	/* DACs */
	SND_SOC_DAPM_DAC("DAC L1", NULL, RT5682S_PWR_DIG_1, RT5682S_PWR_DAC_L1_BIT, 0),
	SND_SOC_DAPM_DAC("DAC R1", NULL, RT5682S_PWR_DIG_1, RT5682S_PWR_DAC_R1_BIT, 0),

	/* HPO */
	SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0, rt5682s_hp_amp_event,
		SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),

	/* CLK DET */
	SND_SOC_DAPM_SUPPLY("CLKDET SYS", RT5682S_CLK_DET,
		RT5682S_SYS_CLK_DET_SFT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("CLKDET PLL1", RT5682S_CLK_DET,
		RT5682S_PLL1_CLK_DET_SFT, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("MCLK0 DET PWR", RT5682S_PWR_ANLG_2,
		RT5682S_PWR_MCLK0_WD_BIT, 0, NULL, 0),

	/* SAR */
	SND_SOC_DAPM_SUPPLY("SAR", SND_SOC_NOPM, 0, 0, sar_power_event,
		SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),

	/* Output Lines */
	SND_SOC_DAPM_OUTPUT("HPOL"),
	SND_SOC_DAPM_OUTPUT("HPOR"),
};

static const struct snd_soc_dapm_route rt5682s_dapm_routes[] = {
	/*PLL*/
	{"ADC Stereo1 Filter", NULL, "PLLA", is_sys_clk_from_plla},
	{"ADC Stereo1 Filter", NULL, "PLLB", is_sys_clk_from_pllb},
	{"DAC Stereo1 Filter", NULL, "PLLA", is_sys_clk_from_plla},
	{"DAC Stereo1 Filter", NULL, "PLLB", is_sys_clk_from_pllb},
	{"PLLA", NULL, "PLLA_LDO"},
	{"PLLA", NULL, "PLLA_BIAS"},
	{"PLLA", NULL, "PLLA_RST"},

	/*ASRC*/
	{"ADC Stereo1 Filter", NULL, "ADC STO1 ASRC", is_using_asrc},
	{"DAC Stereo1 Filter", NULL, "DAC STO1 ASRC", is_using_asrc},
	{"ADC STO1 ASRC", NULL, "AD ASRC"},
	{"ADC STO1 ASRC", NULL, "DA ASRC"},
	{"DAC STO1 ASRC", NULL, "AD ASRC"},
	{"DAC STO1 ASRC", NULL, "DA ASRC"},

	{"CLKDET SYS", NULL, "MCLK0 DET PWR"},

	{"BST1 CBJ", NULL, "IN1P"},
	{"BST1 CBJ", NULL, "SAR"},

	{"RECMIX1L", "CBJ Switch", "BST1 CBJ"},
	{"RECMIX1L", NULL, "RECMIX1L Power"},
	{"RECMIX1R", "CBJ Switch", "BST1 CBJ"},
	{"RECMIX1R", NULL, "RECMIX1R Power"},

	{"ADC1 L", NULL, "RECMIX1L"},
	{"ADC1 L", NULL, "ADC1 L Power"},
	{"ADC1 L", NULL, "ADC1 clock"},
	{"ADC1 R", NULL, "RECMIX1R"},
	{"ADC1 R", NULL, "ADC1 R Power"},
	{"ADC1 R", NULL, "ADC1 clock"},

	{"DMIC L1", NULL, "DMIC CLK"},
	{"DMIC L1", NULL, "DMIC1 Power"},
	{"DMIC R1", NULL, "DMIC CLK"},
	{"DMIC R1", NULL, "DMIC1 Power"},
	{"DMIC CLK", NULL, "DMIC ASRC"},

	{"Stereo1 ADC L Mux", "ADC1 L", "ADC1 L"},
	{"Stereo1 ADC L Mux", "ADC1 R", "ADC1 R"},
	{"Stereo1 ADC R Mux", "ADC1 L", "ADC1 L"},
	{"Stereo1 ADC R Mux", "ADC1 R", "ADC1 R"},

	{"Stereo1 ADC L1 Mux", "ADC", "Stereo1 ADC L Mux"},
	{"Stereo1 ADC L1 Mux", "DAC MIX", "Stereo1 DAC MIXL"},
	{"Stereo1 ADC L2 Mux", "DMIC", "DMIC L1"},
	{"Stereo1 ADC L2 Mux", "DAC MIX", "Stereo1 DAC MIXL"},

	{"Stereo1 ADC R1 Mux", "ADC", "Stereo1 ADC R Mux"},
	{"Stereo1 ADC R1 Mux", "DAC MIX", "Stereo1 DAC MIXR"},
	{"Stereo1 ADC R2 Mux", "DMIC", "DMIC R1"},
	{"Stereo1 ADC R2 Mux", "DAC MIX", "Stereo1 DAC MIXR"},

	{"Stereo1 ADC MIXL", "ADC1 Switch", "Stereo1 ADC L1 Mux"},
	{"Stereo1 ADC MIXL", "ADC2 Switch", "Stereo1 ADC L2 Mux"},
	{"Stereo1 ADC MIXL", NULL, "ADC Stereo1 Filter"},

	{"Stereo1 ADC MIXR", "ADC1 Switch", "Stereo1 ADC R1 Mux"},
	{"Stereo1 ADC MIXR", "ADC2 Switch", "Stereo1 ADC R2 Mux"},
	{"Stereo1 ADC MIXR", NULL, "ADC Stereo1 Filter"},

	{"Stereo1 ADC MIX", NULL, "Stereo1 ADC MIXL"},
	{"Stereo1 ADC MIX", NULL, "Stereo1 ADC MIXR"},

	{"IF1 01 ADC Swap Mux", "L/R", "Stereo1 ADC MIX"},
	{"IF1 01 ADC Swap Mux", "L/L", "Stereo1 ADC MIX"},
	{"IF1 01 ADC Swap Mux", "R/L", "Stereo1 ADC MIX"},
	{"IF1 01 ADC Swap Mux", "R/R", "Stereo1 ADC MIX"},
	{"IF1 23 ADC Swap Mux", "L/R", "Stereo1 ADC MIX"},
	{"IF1 23 ADC Swap Mux", "R/L", "Stereo1 ADC MIX"},
	{"IF1 23 ADC Swap Mux", "L/L", "Stereo1 ADC MIX"},
	{"IF1 23 ADC Swap Mux", "R/R", "Stereo1 ADC MIX"},
	{"IF1 45 ADC Swap Mux", "L/R", "Stereo1 ADC MIX"},
	{"IF1 45 ADC Swap Mux", "R/L", "Stereo1 ADC MIX"},
	{"IF1 45 ADC Swap Mux", "L/L", "Stereo1 ADC MIX"},
	{"IF1 45 ADC Swap Mux", "R/R", "Stereo1 ADC MIX"},
	{"IF1 67 ADC Swap Mux", "L/R", "Stereo1 ADC MIX"},
	{"IF1 67 ADC Swap Mux", "R/L", "Stereo1 ADC MIX"},
	{"IF1 67 ADC Swap Mux", "L/L", "Stereo1 ADC MIX"},
	{"IF1 67 ADC Swap Mux", "R/R", "Stereo1 ADC MIX"},

	{"IF1_ADC Mux", "Slot 0", "IF1 01 ADC Swap Mux"},
	{"IF1_ADC Mux", "Slot 2", "IF1 23 ADC Swap Mux"},
	{"IF1_ADC Mux", "Slot 4", "IF1 45 ADC Swap Mux"},
	{"IF1_ADC Mux", "Slot 6", "IF1 67 ADC Swap Mux"},
	{"ADCDAT Mux", "ADCDAT1", "IF1_ADC Mux"},
	{"AIF1TX", NULL, "I2S1"},
	{"AIF1TX", NULL, "ADCDAT Mux"},
	{"IF2 ADC Swap Mux", "L/R", "Stereo1 ADC MIX"},
	{"IF2 ADC Swap Mux", "R/L", "Stereo1 ADC MIX"},
	{"IF2 ADC Swap Mux", "L/L", "Stereo1 ADC MIX"},
	{"IF2 ADC Swap Mux", "R/R", "Stereo1 ADC MIX"},
	{"ADCDAT Mux", "ADCDAT2", "IF2 ADC Swap Mux"},
	{"AIF2TX", NULL, "ADCDAT Mux"},

	{"IF1 DAC1 L", NULL, "AIF1RX"},
	{"IF1 DAC1 L", NULL, "I2S1"},
	{"IF1 DAC1 L", NULL, "DAC Stereo1 Filter"},
	{"IF1 DAC1 R", NULL, "AIF1RX"},
	{"IF1 DAC1 R", NULL, "I2S1"},
	{"IF1 DAC1 R", NULL, "DAC Stereo1 Filter"},

	{"DAC1 MIXL", "Stereo ADC Switch", "Stereo1 ADC MIXL"},
	{"DAC1 MIXL", "DAC1 Switch", "IF1 DAC1 L"},
	{"DAC1 MIXR", "Stereo ADC Switch", "Stereo1 ADC MIXR"},
	{"DAC1 MIXR", "DAC1 Switch", "IF1 DAC1 R"},

	{"Stereo1 DAC MIXL", "DAC L1 Switch", "DAC1 MIXL"},
	{"Stereo1 DAC MIXL", "DAC R1 Switch", "DAC1 MIXR"},

	{"Stereo1 DAC MIXR", "DAC R1 Switch", "DAC1 MIXR"},
	{"Stereo1 DAC MIXR", "DAC L1 Switch", "DAC1 MIXL"},

	{"DAC L1 Source", "DAC1", "DAC1 MIXL"},
	{"DAC L1 Source", "Stereo1 DAC Mixer", "Stereo1 DAC MIXL"},
	{"DAC R1 Source", "DAC1", "DAC1 MIXR"},
	{"DAC R1 Source", "Stereo1 DAC Mixer", "Stereo1 DAC MIXR"},

	{"DAC L1", NULL, "DAC L1 Source"},
	{"DAC R1", NULL, "DAC R1 Source"},

	{"HP Amp", NULL, "DAC L1"},
	{"HP Amp", NULL, "DAC R1"},
	{"HP Amp", NULL, "CLKDET SYS"},
	{"HP Amp", NULL, "SAR"},

	{"HPOL", NULL, "HP Amp"},
	{"HPOR", NULL, "HP Amp"},
};

static int rt5682s_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
		unsigned int rx_mask, int slots, int slot_width)
{
	struct snd_soc_component *component = dai->component;
	unsigned int cl, val = 0;

	if (tx_mask || rx_mask)
		snd_soc_component_update_bits(component,
			RT5682S_TDM_ADDA_CTRL_2, RT5682S_TDM_EN, RT5682S_TDM_EN);
	else
		snd_soc_component_update_bits(component,
			RT5682S_TDM_ADDA_CTRL_2, RT5682S_TDM_EN, 0);

	switch (slots) {
	case 4:
		val |= RT5682S_TDM_TX_CH_4;
		val |= RT5682S_TDM_RX_CH_4;
		break;
	case 6:
		val |= RT5682S_TDM_TX_CH_6;
		val |= RT5682S_TDM_RX_CH_6;
		break;
	case 8:
		val |= RT5682S_TDM_TX_CH_8;
		val |= RT5682S_TDM_RX_CH_8;
		break;
	case 2:
		break;
	default:
		return -EINVAL;
	}

	snd_soc_component_update_bits(component, RT5682S_TDM_CTRL,
		RT5682S_TDM_TX_CH_MASK | RT5682S_TDM_RX_CH_MASK, val);

	switch (slot_width) {
	case 8:
		if (tx_mask || rx_mask)
			return -EINVAL;
		cl = RT5682S_I2S1_TX_CHL_8 | RT5682S_I2S1_RX_CHL_8;
		break;
	case 16:
		val = RT5682S_TDM_CL_16;
		cl = RT5682S_I2S1_TX_CHL_16 | RT5682S_I2S1_RX_CHL_16;
		break;
	case 20:
		val = RT5682S_TDM_CL_20;
		cl = RT5682S_I2S1_TX_CHL_20 | RT5682S_I2S1_RX_CHL_20;
		break;
	case 24:
		val = RT5682S_TDM_CL_24;
		cl = RT5682S_I2S1_TX_CHL_24 | RT5682S_I2S1_RX_CHL_24;
		break;
	case 32:
		val = RT5682S_TDM_CL_32;
		cl = RT5682S_I2S1_TX_CHL_32 | RT5682S_I2S1_RX_CHL_32;
		break;
	default:
		return -EINVAL;
	}

	snd_soc_component_update_bits(component, RT5682S_TDM_TCON_CTRL_1,
		RT5682S_TDM_CL_MASK, val);
	snd_soc_component_update_bits(component, RT5682S_I2S1_SDP,
		RT5682S_I2S1_TX_CHL_MASK | RT5682S_I2S1_RX_CHL_MASK, cl);

	return 0;
}

static int rt5682s_hw_params(struct snd_pcm_substream *substream,
		struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{
	struct snd_soc_component *component = dai->component;
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	unsigned int len_1 = 0, len_2 = 0;
	int frame_size;

	rt5682s->lrck[dai->id] = params_rate(params);

	frame_size = snd_soc_params_to_frame_size(params);
	if (frame_size < 0) {
		dev_err(component->dev, "Unsupported frame size: %d\n", frame_size);
		return -EINVAL;
	}

	switch (params_width(params)) {
	case 16:
		break;
	case 20:
		len_1 |= RT5682S_I2S1_DL_20;
		len_2 |= RT5682S_I2S2_DL_20;
		break;
	case 24:
		len_1 |= RT5682S_I2S1_DL_24;
		len_2 |= RT5682S_I2S2_DL_24;
		break;
	case 32:
		len_1 |= RT5682S_I2S1_DL_32;
		len_2 |= RT5682S_I2S2_DL_24;
		break;
	case 8:
		len_1 |= RT5682S_I2S2_DL_8;
		len_2 |= RT5682S_I2S2_DL_8;
		break;
	default:
		return -EINVAL;
	}

	switch (dai->id) {
	case RT5682S_AIF1:
		snd_soc_component_update_bits(component, RT5682S_I2S1_SDP,
			RT5682S_I2S1_DL_MASK, len_1);
		if (params_channels(params) == 1) /* mono mode */
			snd_soc_component_update_bits(component, RT5682S_I2S1_SDP,
				RT5682S_I2S1_MONO_MASK, RT5682S_I2S1_MONO_EN);
		else
			snd_soc_component_update_bits(component, RT5682S_I2S1_SDP,
				RT5682S_I2S1_MONO_MASK, RT5682S_I2S1_MONO_DIS);
		break;
	case RT5682S_AIF2:
		snd_soc_component_update_bits(component, RT5682S_I2S2_SDP,
			RT5682S_I2S2_DL_MASK, len_2);
		if (params_channels(params) == 1) /* mono mode */
			snd_soc_component_update_bits(component, RT5682S_I2S2_SDP,
				RT5682S_I2S2_MONO_MASK, RT5682S_I2S2_MONO_EN);
		else
			snd_soc_component_update_bits(component, RT5682S_I2S2_SDP,
				RT5682S_I2S2_MONO_MASK, RT5682S_I2S2_MONO_DIS);
		break;
	default:
		dev_err(component->dev, "Invalid dai->id: %d\n", dai->id);
		return -EINVAL;
	}

	return 0;
}

static int rt5682s_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
	struct snd_soc_component *component = dai->component;
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	unsigned int reg_val = 0, tdm_ctrl = 0;

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM:
		rt5682s->master[dai->id] = 1;
		break;
	case SND_SOC_DAIFMT_CBS_CFS:
		rt5682s->master[dai->id] = 0;
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
	case SND_SOC_DAIFMT_NB_NF:
		break;
	case SND_SOC_DAIFMT_IB_NF:
		reg_val |= RT5682S_I2S_BP_INV;
		tdm_ctrl |= RT5682S_TDM_S_BP_INV;
		break;
	case SND_SOC_DAIFMT_NB_IF:
		if (dai->id == RT5682S_AIF1)
			tdm_ctrl |= RT5682S_TDM_S_LP_INV | RT5682S_TDM_M_BP_INV;
		else
			return -EINVAL;
		break;
	case SND_SOC_DAIFMT_IB_IF:
		if (dai->id == RT5682S_AIF1)
			tdm_ctrl |= RT5682S_TDM_S_BP_INV | RT5682S_TDM_S_LP_INV |
				RT5682S_TDM_M_BP_INV | RT5682S_TDM_M_LP_INV;
		else
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		reg_val |= RT5682S_I2S_DF_LEFT;
		tdm_ctrl |= RT5682S_TDM_DF_LEFT;
		break;
	case SND_SOC_DAIFMT_DSP_A:
		reg_val |= RT5682S_I2S_DF_PCM_A;
		tdm_ctrl |= RT5682S_TDM_DF_PCM_A;
		break;
	case SND_SOC_DAIFMT_DSP_B:
		reg_val |= RT5682S_I2S_DF_PCM_B;
		tdm_ctrl |= RT5682S_TDM_DF_PCM_B;
		break;
	default:
		return -EINVAL;
	}

	switch (dai->id) {
	case RT5682S_AIF1:
		snd_soc_component_update_bits(component, RT5682S_I2S1_SDP,
			RT5682S_I2S_DF_MASK, reg_val);
		snd_soc_component_update_bits(component, RT5682S_TDM_TCON_CTRL_1,
			RT5682S_TDM_MS_MASK | RT5682S_TDM_S_BP_MASK |
			RT5682S_TDM_DF_MASK | RT5682S_TDM_M_BP_MASK |
			RT5682S_TDM_M_LP_MASK | RT5682S_TDM_S_LP_MASK,
			tdm_ctrl | rt5682s->master[dai->id]);
		break;
	case RT5682S_AIF2:
		if (rt5682s->master[dai->id] == 0)
			reg_val |= RT5682S_I2S2_MS_S;
		snd_soc_component_update_bits(component, RT5682S_I2S2_SDP,
			RT5682S_I2S2_MS_MASK | RT5682S_I2S_BP_MASK |
			RT5682S_I2S_DF_MASK, reg_val);
		break;
	default:
		dev_err(component->dev, "Invalid dai->id: %d\n", dai->id);
		return -EINVAL;
	}
	return 0;
}

static int rt5682s_set_component_sysclk(struct snd_soc_component *component,
		int clk_id, int source, unsigned int freq, int dir)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	unsigned int src = 0;

	if (freq == rt5682s->sysclk && clk_id == rt5682s->sysclk_src)
		return 0;

	switch (clk_id) {
	case RT5682S_SCLK_S_MCLK:
		src = RT5682S_CLK_SRC_MCLK;
		break;
	case RT5682S_SCLK_S_PLL1:
		src = RT5682S_CLK_SRC_PLL1;
		break;
	case RT5682S_SCLK_S_PLL2:
		src = RT5682S_CLK_SRC_PLL2;
		break;
	case RT5682S_SCLK_S_RCCLK:
		src = RT5682S_CLK_SRC_RCCLK;
		break;
	default:
		dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
		return -EINVAL;
	}

	snd_soc_component_update_bits(component, RT5682S_GLB_CLK,
		RT5682S_SCLK_SRC_MASK, src << RT5682S_SCLK_SRC_SFT);
	snd_soc_component_update_bits(component, RT5682S_ADDA_CLK_1,
		RT5682S_I2S_M_CLK_SRC_MASK, src << RT5682S_I2S_M_CLK_SRC_SFT);
	snd_soc_component_update_bits(component, RT5682S_I2S2_M_CLK_CTRL_1,
		RT5682S_I2S2_M_CLK_SRC_MASK, src << RT5682S_I2S2_M_CLK_SRC_SFT);

	rt5682s->sysclk = freq;
	rt5682s->sysclk_src = clk_id;

	dev_dbg(component->dev, "Sysclk is %dHz and clock id is %d\n",
		freq, clk_id);

	return 0;
}

static const struct pll_calc_map plla_table[] = {
	{2048000, 24576000, 0, 46, 2, true, false, false, false},
	{256000, 24576000, 0, 382, 2, true, false, false, false},
	{512000, 24576000, 0, 190, 2, true, false, false, false},
	{4096000, 24576000, 0, 22, 2, true, false, false, false},
	{1024000, 24576000, 0, 94, 2, true, false, false, false},
	{11289600, 22579200, 1, 22, 2, false, false, false, false},
	{1411200, 22579200, 0, 62, 2, true, false, false, false},
	{2822400, 22579200, 0, 30, 2, true, false, false, false},
	{12288000, 24576000, 1, 22, 2, false, false, false, false},
	{1536000, 24576000, 0, 62, 2, true, false, false, false},
	{3072000, 24576000, 0, 30, 2, true, false, false, false},
	{24576000, 49152000, 4, 22, 0, false, false, false, false},
	{3072000, 49152000, 0, 30, 0, true, false, false, false},
	{6144000, 49152000, 0, 30, 0, false, false, false, false},
	{49152000, 98304000, 10, 22, 0, false, true, false, false},
	{6144000, 98304000, 0, 30, 0, false, true, false, false},
	{12288000, 98304000, 1, 22, 0, false, true, false, false},
	{48000000, 3840000, 10, 22, 23, false, false, false, false},
	{24000000, 3840000, 4, 22, 23, false, false, false, false},
	{19200000, 3840000, 3, 23, 23, false, false, false, false},
	{38400000, 3840000, 8, 23, 23, false, false, false, false},
};

static const struct pll_calc_map pllb_table[] = {
	{48000000, 24576000, 8, 6, 3, false, false, false, false},
	{48000000, 22579200, 23, 12, 3, false, false, false, true},
	{24000000, 24576000, 3, 6, 3, false, false, false, false},
	{24000000, 22579200, 23, 26, 3, false, false, false, true},
	{19200000, 24576000, 2, 6, 3, false, false, false, false},
	{19200000, 22579200, 3, 5, 3, false, false, false, true},
	{38400000, 24576000, 6, 6, 3, false, false, false, false},
	{38400000, 22579200, 8, 5, 3, false, false, false, true},
	{3840000, 49152000, 0, 6, 0, true, false, false, false},
};

static int find_pll_inter_combination(unsigned int f_in, unsigned int f_out,
		struct pll_calc_map *a, struct pll_calc_map *b)
{
	int i, j;

	/* Look at PLLA table */
	for (i = 0; i < ARRAY_SIZE(plla_table); i++) {
		if (plla_table[i].freq_in == f_in && plla_table[i].freq_out == f_out) {
			memcpy(a, plla_table + i, sizeof(*a));
			return USE_PLLA;
		}
	}

	/* Look at PLLB table */
	for (i = 0; i < ARRAY_SIZE(pllb_table); i++) {
		if (pllb_table[i].freq_in == f_in && pllb_table[i].freq_out == f_out) {
			memcpy(b, pllb_table + i, sizeof(*b));
			return USE_PLLB;
		}
	}

	/* Find a combination of PLLA & PLLB */
	for (i = ARRAY_SIZE(plla_table) - 1; i >= 0; i--) {
		if (plla_table[i].freq_in == f_in && plla_table[i].freq_out == 3840000) {
			for (j = ARRAY_SIZE(pllb_table) - 1; j >= 0; j--) {
				if (pllb_table[j].freq_in == 3840000 &&
					pllb_table[j].freq_out == f_out) {
					memcpy(a, plla_table + i, sizeof(*a));
					memcpy(b, pllb_table + j, sizeof(*b));
					return USE_PLLAB;
				}
			}
		}
	}

	return -EINVAL;
}

static int rt5682s_set_component_pll(struct snd_soc_component *component,
		int pll_id, int source, unsigned int freq_in,
		unsigned int freq_out)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	struct pll_calc_map a_map, b_map;

	if (source == rt5682s->pll_src[pll_id] && freq_in == rt5682s->pll_in[pll_id] &&
	    freq_out == rt5682s->pll_out[pll_id])
		return 0;

	if (!freq_in || !freq_out) {
		dev_dbg(component->dev, "PLL disabled\n");
		rt5682s->pll_in[pll_id] = 0;
		rt5682s->pll_out[pll_id] = 0;
		snd_soc_component_update_bits(component, RT5682S_GLB_CLK,
			RT5682S_SCLK_SRC_MASK, RT5682S_CLK_SRC_MCLK << RT5682S_SCLK_SRC_SFT);
		return 0;
	}

	switch (source) {
	case RT5682S_PLL_S_MCLK:
		snd_soc_component_update_bits(component, RT5682S_GLB_CLK,
			RT5682S_PLL_SRC_MASK, RT5682S_PLL_SRC_MCLK);
		break;
	case RT5682S_PLL_S_BCLK1:
		snd_soc_component_update_bits(component, RT5682S_GLB_CLK,
			RT5682S_PLL_SRC_MASK, RT5682S_PLL_SRC_BCLK1);
		break;
	default:
		dev_err(component->dev, "Unknown PLL Source %d\n", source);
		return -EINVAL;
	}

	rt5682s->pll_comb = find_pll_inter_combination(freq_in, freq_out,
							&a_map, &b_map);

	if ((pll_id == RT5682S_PLL1 && rt5682s->pll_comb == USE_PLLA) ||
	    (pll_id == RT5682S_PLL2 && (rt5682s->pll_comb == USE_PLLB ||
					rt5682s->pll_comb == USE_PLLAB))) {
		dev_dbg(component->dev,
			"Supported freq conversion for PLL%d:(%d->%d): %d\n",
			pll_id + 1, freq_in, freq_out, rt5682s->pll_comb);
	} else {
		dev_err(component->dev,
			"Unsupported freq conversion for PLL%d:(%d->%d): %d\n",
			pll_id + 1, freq_in, freq_out, rt5682s->pll_comb);
		return -EINVAL;
	}

	if (rt5682s->pll_comb == USE_PLLA || rt5682s->pll_comb == USE_PLLAB) {
		dev_dbg(component->dev,
			"PLLA: fin=%d fout=%d m_bp=%d k_bp=%d m=%d n=%d k=%d\n",
			a_map.freq_in, a_map.freq_out, a_map.m_bp, a_map.k_bp,
			(a_map.m_bp ? 0 : a_map.m), a_map.n, (a_map.k_bp ? 0 : a_map.k));
		snd_soc_component_update_bits(component, RT5682S_PLL_CTRL_1,
			RT5682S_PLLA_N_MASK, a_map.n);
		snd_soc_component_update_bits(component, RT5682S_PLL_CTRL_2,
			RT5682S_PLLA_M_MASK | RT5682S_PLLA_K_MASK,
			a_map.m << RT5682S_PLLA_M_SFT | a_map.k);
		snd_soc_component_update_bits(component, RT5682S_PLL_CTRL_6,
			RT5682S_PLLA_M_BP_MASK | RT5682S_PLLA_K_BP_MASK,
			a_map.m_bp << RT5682S_PLLA_M_BP_SFT |
			a_map.k_bp << RT5682S_PLLA_K_BP_SFT);
	}

	if (rt5682s->pll_comb == USE_PLLB || rt5682s->pll_comb == USE_PLLAB) {
		dev_dbg(component->dev,
			"PLLB: fin=%d fout=%d m_bp=%d k_bp=%d m=%d n=%d k=%d byp_ps=%d sel_ps=%d\n",
			b_map.freq_in, b_map.freq_out, b_map.m_bp, b_map.k_bp,
			(b_map.m_bp ? 0 : b_map.m), b_map.n, (b_map.k_bp ? 0 : b_map.k),
			b_map.byp_ps, b_map.sel_ps);
		snd_soc_component_update_bits(component, RT5682S_PLL_CTRL_3,
			RT5682S_PLLB_N_MASK, b_map.n);
		snd_soc_component_update_bits(component, RT5682S_PLL_CTRL_4,
			RT5682S_PLLB_M_MASK | RT5682S_PLLB_K_MASK,
			b_map.m << RT5682S_PLLB_M_SFT | b_map.k);
		snd_soc_component_update_bits(component, RT5682S_PLL_CTRL_6,
			RT5682S_PLLB_SEL_PS_MASK | RT5682S_PLLB_BYP_PS_MASK |
			RT5682S_PLLB_M_BP_MASK | RT5682S_PLLB_K_BP_MASK,
			b_map.sel_ps << RT5682S_PLLB_SEL_PS_SFT |
			b_map.byp_ps << RT5682S_PLLB_BYP_PS_SFT |
			b_map.m_bp << RT5682S_PLLB_M_BP_SFT |
			b_map.k_bp << RT5682S_PLLB_K_BP_SFT);
	}

	if (rt5682s->pll_comb == USE_PLLB)
		snd_soc_component_update_bits(component, RT5682S_PLL_CTRL_7,
			RT5682S_PLLB_SRC_MASK, RT5682S_PLLB_SRC_DFIN);

	rt5682s->pll_in[pll_id] = freq_in;
	rt5682s->pll_out[pll_id] = freq_out;
	rt5682s->pll_src[pll_id] = source;

	return 0;
}

static int rt5682s_set_bclk1_ratio(struct snd_soc_dai *dai,
		unsigned int ratio)
{
	struct snd_soc_component *component = dai->component;
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	rt5682s->bclk[dai->id] = ratio;

	switch (ratio) {
	case 256:
		snd_soc_component_update_bits(component, RT5682S_TDM_TCON_CTRL_1,
			RT5682S_TDM_BCLK_MS1_MASK, RT5682S_TDM_BCLK_MS1_256);
		break;
	case 128:
		snd_soc_component_update_bits(component, RT5682S_TDM_TCON_CTRL_1,
			RT5682S_TDM_BCLK_MS1_MASK, RT5682S_TDM_BCLK_MS1_128);
		break;
	case 64:
		snd_soc_component_update_bits(component, RT5682S_TDM_TCON_CTRL_1,
			RT5682S_TDM_BCLK_MS1_MASK, RT5682S_TDM_BCLK_MS1_64);
		break;
	case 32:
		snd_soc_component_update_bits(component, RT5682S_TDM_TCON_CTRL_1,
			RT5682S_TDM_BCLK_MS1_MASK, RT5682S_TDM_BCLK_MS1_32);
		break;
	default:
		dev_err(dai->dev, "Invalid bclk1 ratio %d\n", ratio);
		return -EINVAL;
	}

	return 0;
}

static int rt5682s_set_bclk2_ratio(struct snd_soc_dai *dai, unsigned int ratio)
{
	struct snd_soc_component *component = dai->component;
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	rt5682s->bclk[dai->id] = ratio;

	switch (ratio) {
	case 64:
		snd_soc_component_update_bits(component, RT5682S_ADDA_CLK_2,
			RT5682S_I2S2_BCLK_MS2_MASK, RT5682S_I2S2_BCLK_MS2_64);
		break;
	case 32:
		snd_soc_component_update_bits(component, RT5682S_ADDA_CLK_2,
			RT5682S_I2S2_BCLK_MS2_MASK, RT5682S_I2S2_BCLK_MS2_32);
		break;
	default:
		dev_err(dai->dev, "Invalid bclk2 ratio %d\n", ratio);
		return -EINVAL;
	}

	return 0;
}

static int rt5682s_set_bias_level(struct snd_soc_component *component,
		enum snd_soc_bias_level level)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	switch (level) {
	case SND_SOC_BIAS_PREPARE:
		regmap_update_bits(rt5682s->regmap, RT5682S_PWR_DIG_1,
			RT5682S_PWR_LDO, RT5682S_PWR_LDO);
		break;
	case SND_SOC_BIAS_STANDBY:
		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
			regmap_update_bits(rt5682s->regmap, RT5682S_PWR_DIG_1,
				RT5682S_DIG_GATE_CTRL, RT5682S_DIG_GATE_CTRL);
		break;
	case SND_SOC_BIAS_OFF:
		regmap_update_bits(rt5682s->regmap, RT5682S_PWR_DIG_1, RT5682S_PWR_LDO, 0);
		if (!rt5682s->wclk_enabled)
			regmap_update_bits(rt5682s->regmap, RT5682S_PWR_DIG_1,
				RT5682S_DIG_GATE_CTRL, 0);
		break;
	case SND_SOC_BIAS_ON:
		break;
	}

	return 0;
}

#ifdef CONFIG_COMMON_CLK
#define CLK_PLL2_FIN 48000000
#define CLK_48 48000
#define CLK_44 44100

static bool rt5682s_clk_check(struct rt5682s_priv *rt5682s)
{
	if (!rt5682s->master[RT5682S_AIF1]) {
		dev_dbg(rt5682s->component->dev, "dai clk fmt not set correctly\n");
		return false;
	}
	return true;
}

static int rt5682s_wclk_prepare(struct clk_hw *hw)
{
	struct rt5682s_priv *rt5682s =
		container_of(hw, struct rt5682s_priv, dai_clks_hw[RT5682S_DAI_WCLK_IDX]);
	struct snd_soc_component *component = rt5682s->component;
	int ref, reg;

	if (!rt5682s_clk_check(rt5682s))
		return -EINVAL;

	mutex_lock(&rt5682s->wclk_mutex);

	snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_1,
		RT5682S_PWR_VREF2 | RT5682S_PWR_FV2 | RT5682S_PWR_MB,
		RT5682S_PWR_VREF2 | RT5682S_PWR_MB);
	usleep_range(15000, 20000);
	snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_1,
		RT5682S_PWR_FV2, RT5682S_PWR_FV2);

	/* Set and power on I2S1 */
	snd_soc_component_update_bits(component, RT5682S_PWR_DIG_1,
		RT5682S_DIG_GATE_CTRL, RT5682S_DIG_GATE_CTRL);
	rt5682s_set_i2s(rt5682s, RT5682S_AIF1, 1);

	/* Only need to power on PLLB due to the rate set restriction */
	reg = RT5682S_PLL_TRACK_2;
	ref = 256 * rt5682s->lrck[RT5682S_AIF1];
	rt5682s_set_filter_clk(rt5682s, reg, ref);
	rt5682s_set_pllb_power(rt5682s, 1);

	rt5682s->wclk_enabled = 1;

	mutex_unlock(&rt5682s->wclk_mutex);

	return 0;
}

static void rt5682s_wclk_unprepare(struct clk_hw *hw)
{
	struct rt5682s_priv *rt5682s =
		container_of(hw, struct rt5682s_priv, dai_clks_hw[RT5682S_DAI_WCLK_IDX]);
	struct snd_soc_component *component = rt5682s->component;

	if (!rt5682s_clk_check(rt5682s))
		return;

	mutex_lock(&rt5682s->wclk_mutex);

	if (!rt5682s->jack_type)
		snd_soc_component_update_bits(component, RT5682S_PWR_ANLG_1,
			RT5682S_PWR_VREF2 | RT5682S_PWR_FV2 | RT5682S_PWR_MB, 0);

	/* Power down I2S1 */
	rt5682s_set_i2s(rt5682s, RT5682S_AIF1, 0);
	snd_soc_component_update_bits(component, RT5682S_PWR_DIG_1,
		RT5682S_DIG_GATE_CTRL, 0);

	/* Power down PLLB */
	rt5682s_set_pllb_power(rt5682s, 0);

	rt5682s->wclk_enabled = 0;

	mutex_unlock(&rt5682s->wclk_mutex);
}

static unsigned long rt5682s_wclk_recalc_rate(struct clk_hw *hw,
					     unsigned long parent_rate)
{
	struct rt5682s_priv *rt5682s =
		container_of(hw, struct rt5682s_priv, dai_clks_hw[RT5682S_DAI_WCLK_IDX]);
	struct snd_soc_component *component = rt5682s->component;
	const char * const clk_name = clk_hw_get_name(hw);

	if (!rt5682s_clk_check(rt5682s))
		return 0;
	/*
	 * Only accept to set wclk rate to 44.1k or 48kHz.
	 */
	if (rt5682s->lrck[RT5682S_AIF1] != CLK_48 &&
	    rt5682s->lrck[RT5682S_AIF1] != CLK_44) {
		dev_warn(component->dev, "%s: clk %s only support %d or %d Hz output\n",
			__func__, clk_name, CLK_44, CLK_48);
		return 0;
	}

	return rt5682s->lrck[RT5682S_AIF1];
}

static long rt5682s_wclk_round_rate(struct clk_hw *hw, unsigned long rate,
				   unsigned long *parent_rate)
{
	struct rt5682s_priv *rt5682s =
		container_of(hw, struct rt5682s_priv, dai_clks_hw[RT5682S_DAI_WCLK_IDX]);
	struct snd_soc_component *component = rt5682s->component;
	const char * const clk_name = clk_hw_get_name(hw);

	if (!rt5682s_clk_check(rt5682s))
		return -EINVAL;
	/*
	 * Only accept to set wclk rate to 44.1k or 48kHz.
	 * It will force to 48kHz if not both.
	 */
	if (rate != CLK_48 && rate != CLK_44) {
		dev_warn(component->dev, "%s: clk %s only support %d or %d Hz output\n",
			__func__, clk_name, CLK_44, CLK_48);
		rate = CLK_48;
	}

	return rate;
}

static int rt5682s_wclk_set_rate(struct clk_hw *hw, unsigned long rate,
				unsigned long parent_rate)
{
	struct rt5682s_priv *rt5682s =
		container_of(hw, struct rt5682s_priv, dai_clks_hw[RT5682S_DAI_WCLK_IDX]);
	struct snd_soc_component *component = rt5682s->component;
	struct clk *parent_clk;
	const char * const clk_name = clk_hw_get_name(hw);
	unsigned int clk_pll2_fout;

	if (!rt5682s_clk_check(rt5682s))
		return -EINVAL;

	/*
	 * Whether the wclk's parent clk (mclk) exists or not, please ensure
	 * it is fixed or set to 48MHz before setting wclk rate. It's a
	 * temporary limitation. Only accept 48MHz clk as the clk provider.
	 *
	 * It will set the codec anyway by assuming mclk is 48MHz.
	 */
	parent_clk = clk_get_parent(hw->clk);
	if (!parent_clk)
		dev_warn(component->dev,
			"Parent mclk of wclk not acquired in driver. Please ensure mclk was provided as %d Hz.\n",
			CLK_PLL2_FIN);

	if (parent_rate != CLK_PLL2_FIN)
		dev_warn(component->dev, "clk %s only support %d Hz input\n",
			clk_name, CLK_PLL2_FIN);

	/*
	 * To achieve the rate conversion from 48MHz to 44.1k or 48kHz,
	 * PLL2 is needed.
	 */
	clk_pll2_fout = rate * 512;
	rt5682s_set_component_pll(component, RT5682S_PLL2, RT5682S_PLL_S_MCLK,
		CLK_PLL2_FIN, clk_pll2_fout);

	rt5682s_set_component_sysclk(component, RT5682S_SCLK_S_PLL2, 0,
		clk_pll2_fout, SND_SOC_CLOCK_IN);

	rt5682s->lrck[RT5682S_AIF1] = rate;

	return 0;
}

static unsigned long rt5682s_bclk_recalc_rate(struct clk_hw *hw,
					     unsigned long parent_rate)
{
	struct rt5682s_priv *rt5682s =
		container_of(hw, struct rt5682s_priv, dai_clks_hw[RT5682S_DAI_BCLK_IDX]);
	struct snd_soc_component *component = rt5682s->component;
	unsigned int bclks_per_wclk;

	bclks_per_wclk = snd_soc_component_read(component, RT5682S_TDM_TCON_CTRL_1);

	switch (bclks_per_wclk & RT5682S_TDM_BCLK_MS1_MASK) {
	case RT5682S_TDM_BCLK_MS1_256:
		return parent_rate * 256;
	case RT5682S_TDM_BCLK_MS1_128:
		return parent_rate * 128;
	case RT5682S_TDM_BCLK_MS1_64:
		return parent_rate * 64;
	case RT5682S_TDM_BCLK_MS1_32:
		return parent_rate * 32;
	default:
		return 0;
	}
}

static unsigned long rt5682s_bclk_get_factor(unsigned long rate,
					    unsigned long parent_rate)
{
	unsigned long factor;

	factor = rate / parent_rate;
	if (factor < 64)
		return 32;
	else if (factor < 128)
		return 64;
	else if (factor < 256)
		return 128;
	else
		return 256;
}

static long rt5682s_bclk_round_rate(struct clk_hw *hw, unsigned long rate,
				   unsigned long *parent_rate)
{
	struct rt5682s_priv *rt5682s =
		container_of(hw, struct rt5682s_priv, dai_clks_hw[RT5682S_DAI_BCLK_IDX]);
	unsigned long factor;

	if (!*parent_rate || !rt5682s_clk_check(rt5682s))
		return -EINVAL;

	/*
	 * BCLK rates are set as a multiplier of WCLK in HW.
	 * We don't allow changing the parent WCLK. We just do
	 * some rounding down based on the parent WCLK rate
	 * and find the appropriate multiplier of BCLK to
	 * get the rounded down BCLK value.
	 */
	factor = rt5682s_bclk_get_factor(rate, *parent_rate);

	return *parent_rate * factor;
}

static int rt5682s_bclk_set_rate(struct clk_hw *hw, unsigned long rate,
				unsigned long parent_rate)
{
	struct rt5682s_priv *rt5682s =
		container_of(hw, struct rt5682s_priv, dai_clks_hw[RT5682S_DAI_BCLK_IDX]);
	struct snd_soc_component *component = rt5682s->component;
	struct snd_soc_dai *dai;
	unsigned long factor;

	if (!rt5682s_clk_check(rt5682s))
		return -EINVAL;

	factor = rt5682s_bclk_get_factor(rate, parent_rate);

	for_each_component_dais(component, dai)
		if (dai->id == RT5682S_AIF1)
			return rt5682s_set_bclk1_ratio(dai, factor);

	dev_err(component->dev, "dai %d not found in component\n",
		RT5682S_AIF1);
	return -ENODEV;
}

static const struct clk_ops rt5682s_dai_clk_ops[RT5682S_DAI_NUM_CLKS] = {
	[RT5682S_DAI_WCLK_IDX] = {
		.prepare = rt5682s_wclk_prepare,
		.unprepare = rt5682s_wclk_unprepare,
		.recalc_rate = rt5682s_wclk_recalc_rate,
		.round_rate = rt5682s_wclk_round_rate,
		.set_rate = rt5682s_wclk_set_rate,
	},
	[RT5682S_DAI_BCLK_IDX] = {
		.recalc_rate = rt5682s_bclk_recalc_rate,
		.round_rate = rt5682s_bclk_round_rate,
		.set_rate = rt5682s_bclk_set_rate,
	},
};

static int rt5682s_register_dai_clks(struct snd_soc_component *component)
{
	struct device *dev = component->dev;
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	struct rt5682s_platform_data *pdata = &rt5682s->pdata;
	struct clk_hw *dai_clk_hw;
	int i, ret;

	for (i = 0; i < RT5682S_DAI_NUM_CLKS; ++i) {
		struct clk_init_data init = { };
		struct clk_parent_data parent_data;
		const struct clk_hw *parent;

		dai_clk_hw = &rt5682s->dai_clks_hw[i];

		switch (i) {
		case RT5682S_DAI_WCLK_IDX:
			/* Make MCLK the parent of WCLK */
			if (rt5682s->mclk) {
				parent_data = (struct clk_parent_data){
					.fw_name = "mclk",
				};
				init.parent_data = &parent_data;
				init.num_parents = 1;
			}
			break;
		case RT5682S_DAI_BCLK_IDX:
			/* Make WCLK the parent of BCLK */
			parent = &rt5682s->dai_clks_hw[RT5682S_DAI_WCLK_IDX];
			init.parent_hws = &parent;
			init.num_parents = 1;
			break;
		default:
			dev_err(dev, "Invalid clock index\n");
			return -EINVAL;
		}

		init.name = pdata->dai_clk_names[i];
		init.ops = &rt5682s_dai_clk_ops[i];
		init.flags = CLK_GET_RATE_NOCACHE | CLK_SET_RATE_GATE;
		dai_clk_hw->init = &init;

		ret = devm_clk_hw_register(dev, dai_clk_hw);
		if (ret) {
			dev_warn(dev, "Failed to register %s: %d\n", init.name, ret);
			return ret;
		}

		if (dev->of_node) {
			devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, dai_clk_hw);
		} else {
			ret = devm_clk_hw_register_clkdev(dev, dai_clk_hw,
							  init.name, dev_name(dev));
			if (ret)
				return ret;
		}
	}

	return 0;
}

static int rt5682s_dai_probe_clks(struct snd_soc_component *component)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);
	int ret;

	/* Check if MCLK provided */
	rt5682s->mclk = devm_clk_get(component->dev, "mclk");
	if (IS_ERR(rt5682s->mclk)) {
		if (PTR_ERR(rt5682s->mclk) != -ENOENT) {
			ret = PTR_ERR(rt5682s->mclk);
			return ret;
		}
		rt5682s->mclk = NULL;
	}

	/* Register CCF DAI clock control */
	ret = rt5682s_register_dai_clks(component);
	if (ret)
		return ret;

	/* Initial setup for CCF */
	rt5682s->lrck[RT5682S_AIF1] = CLK_48;

	return 0;
}
#else
static inline int rt5682s_dai_probe_clks(struct snd_soc_component *component)
{
	return 0;
}
#endif /* CONFIG_COMMON_CLK */

static int rt5682s_probe(struct snd_soc_component *component)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	rt5682s->component = component;

	return rt5682s_dai_probe_clks(component);
}

static void rt5682s_remove(struct snd_soc_component *component)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	rt5682s_reset(rt5682s);
}

#ifdef CONFIG_PM
static int rt5682s_suspend(struct snd_soc_component *component)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	cancel_delayed_work_sync(&rt5682s->jack_detect_work);
	cancel_delayed_work_sync(&rt5682s->jd_check_work);

	if (rt5682s->hs_jack)
		rt5682s->jack_type = rt5682s_headset_detect(component, 0);

	regcache_cache_only(rt5682s->regmap, true);
	regcache_mark_dirty(rt5682s->regmap);

	return 0;
}

static int rt5682s_resume(struct snd_soc_component *component)
{
	struct rt5682s_priv *rt5682s = snd_soc_component_get_drvdata(component);

	regcache_cache_only(rt5682s->regmap, false);
	regcache_sync(rt5682s->regmap);

	if (rt5682s->hs_jack) {
		mod_delayed_work(system_power_efficient_wq,
			&rt5682s->jack_detect_work, msecs_to_jiffies(0));
	}

	return 0;
}
#else
#define rt5682s_suspend NULL
#define rt5682s_resume NULL
#endif

static const struct snd_soc_dai_ops rt5682s_aif1_dai_ops = {
	.hw_params = rt5682s_hw_params,
	.set_fmt = rt5682s_set_dai_fmt,
	.set_tdm_slot = rt5682s_set_tdm_slot,
	.set_bclk_ratio = rt5682s_set_bclk1_ratio,
};

static const struct snd_soc_dai_ops rt5682s_aif2_dai_ops = {
	.hw_params = rt5682s_hw_params,
	.set_fmt = rt5682s_set_dai_fmt,
	.set_bclk_ratio = rt5682s_set_bclk2_ratio,
};

static const struct snd_soc_component_driver rt5682s_soc_component_dev = {
	.probe = rt5682s_probe,
	.remove = rt5682s_remove,
	.suspend = rt5682s_suspend,
	.resume = rt5682s_resume,
	.set_bias_level = rt5682s_set_bias_level,
	.controls = rt5682s_snd_controls,
	.num_controls = ARRAY_SIZE(rt5682s_snd_controls),
	.dapm_widgets = rt5682s_dapm_widgets,
	.num_dapm_widgets = ARRAY_SIZE(rt5682s_dapm_widgets),
	.dapm_routes = rt5682s_dapm_routes,
	.num_dapm_routes = ARRAY_SIZE(rt5682s_dapm_routes),
	.set_sysclk = rt5682s_set_component_sysclk,
	.set_pll = rt5682s_set_component_pll,
	.set_jack = rt5682s_set_jack_detect,
	.use_pmdown_time	= 1,
	.endianness		= 1,
};

static int rt5682s_parse_dt(struct rt5682s_priv *rt5682s, struct device *dev)
{
	device_property_read_u32(dev, "realtek,dmic1-data-pin",
		&rt5682s->pdata.dmic1_data_pin);
	device_property_read_u32(dev, "realtek,dmic1-clk-pin",
		&rt5682s->pdata.dmic1_clk_pin);
	device_property_read_u32(dev, "realtek,jd-src",
		&rt5682s->pdata.jd_src);
	device_property_read_u32(dev, "realtek,dmic-clk-rate-hz",
		&rt5682s->pdata.dmic_clk_rate);
	device_property_read_u32(dev, "realtek,dmic-delay-ms",
		&rt5682s->pdata.dmic_delay);
	device_property_read_u32(dev, "realtek,amic-delay-ms",
		&rt5682s->pdata.amic_delay);

	rt5682s->pdata.ldo1_en = of_get_named_gpio(dev->of_node,
		"realtek,ldo1-en-gpios", 0);

	if (device_property_read_string_array(dev, "clock-output-names",
					      rt5682s->pdata.dai_clk_names,
					      RT5682S_DAI_NUM_CLKS) < 0)
		dev_warn(dev, "Using default DAI clk names: %s, %s\n",
			 rt5682s->pdata.dai_clk_names[RT5682S_DAI_WCLK_IDX],
			 rt5682s->pdata.dai_clk_names[RT5682S_DAI_BCLK_IDX]);

	rt5682s->pdata.dmic_clk_driving_high = device_property_read_bool(dev,
		"realtek,dmic-clk-driving-high");

	return 0;
}

static void rt5682s_calibrate(struct rt5682s_priv *rt5682s)
{
	unsigned int count, value;

	mutex_lock(&rt5682s->calibrate_mutex);

	regmap_write(rt5682s->regmap, RT5682S_PWR_ANLG_1, 0xaa80);
	usleep_range(15000, 20000);
	regmap_write(rt5682s->regmap, RT5682S_PWR_ANLG_1, 0xfa80);
	regmap_write(rt5682s->regmap, RT5682S_PWR_DIG_1, 0x01c0);
	regmap_write(rt5682s->regmap, RT5682S_MICBIAS_2, 0x0380);
	regmap_write(rt5682s->regmap, RT5682S_GLB_CLK, 0x8000);
	regmap_write(rt5682s->regmap, RT5682S_ADDA_CLK_1, 0x1001);
	regmap_write(rt5682s->regmap, RT5682S_CHOP_DAC_2, 0x3030);
	regmap_write(rt5682s->regmap, RT5682S_CHOP_ADC, 0xb000);
	regmap_write(rt5682s->regmap, RT5682S_STO1_ADC_MIXER, 0x686c);
	regmap_write(rt5682s->regmap, RT5682S_CAL_REC, 0x5151);
	regmap_write(rt5682s->regmap, RT5682S_HP_CALIB_CTRL_2, 0x0321);
	regmap_write(rt5682s->regmap, RT5682S_HP_LOGIC_CTRL_2, 0x0004);
	regmap_write(rt5682s->regmap, RT5682S_HP_CALIB_CTRL_1, 0x7c00);
	regmap_write(rt5682s->regmap, RT5682S_HP_CALIB_CTRL_1, 0xfc00);

	for (count = 0; count < 60; count++) {
		regmap_read(rt5682s->regmap, RT5682S_HP_CALIB_ST_1, &value);
		if (!(value & 0x8000))
			break;

		usleep_range(10000, 10005);
	}

	if (count >= 60)
		dev_err(rt5682s->component->dev, "HP Calibration Failure\n");

	/* restore settings */
	regmap_write(rt5682s->regmap, RT5682S_MICBIAS_2, 0x0180);
	regmap_write(rt5682s->regmap, RT5682S_CAL_REC, 0x5858);
	regmap_write(rt5682s->regmap, RT5682S_STO1_ADC_MIXER, 0xc0c4);
	regmap_write(rt5682s->regmap, RT5682S_HP_CALIB_CTRL_2, 0x0320);
	regmap_write(rt5682s->regmap, RT5682S_PWR_DIG_1, 0x00c0);
	regmap_write(rt5682s->regmap, RT5682S_PWR_ANLG_1, 0x0800);
	regmap_write(rt5682s->regmap, RT5682S_GLB_CLK, 0x0000);

	mutex_unlock(&rt5682s->calibrate_mutex);
}

static const struct regmap_config rt5682s_regmap = {
	.reg_bits = 16,
	.val_bits = 16,
	.max_register = RT5682S_MAX_REG,
	.volatile_reg = rt5682s_volatile_register,
	.readable_reg = rt5682s_readable_register,
	.cache_type = REGCACHE_RBTREE,
	.reg_defaults = rt5682s_reg,
	.num_reg_defaults = ARRAY_SIZE(rt5682s_reg),
	.use_single_read = true,
	.use_single_write = true,
};

static struct snd_soc_dai_driver rt5682s_dai[] = {
	{
		.name = "rt5682s-aif1",
		.id = RT5682S_AIF1,
		.playback = {
			.stream_name = "AIF1 Playback",
			.channels_min = 1,
			.channels_max = 2,
			.rates = RT5682S_STEREO_RATES,
			.formats = RT5682S_FORMATS,
		},
		.capture = {
			.stream_name = "AIF1 Capture",
			.channels_min = 1,
			.channels_max = 2,
			.rates = RT5682S_STEREO_RATES,
			.formats = RT5682S_FORMATS,
		},
		.ops = &rt5682s_aif1_dai_ops,
	},
	{
		.name = "rt5682s-aif2",
		.id = RT5682S_AIF2,
		.capture = {
			.stream_name = "AIF2 Capture",
			.channels_min = 1,
			.channels_max = 2,
			.rates = RT5682S_STEREO_RATES,
			.formats = RT5682S_FORMATS,
		},
		.ops = &rt5682s_aif2_dai_ops,
	},
};

static void rt5682s_i2c_disable_regulators(void *data)
{
	struct rt5682s_priv *rt5682s = data;
	struct device *dev = regmap_get_device(rt5682s->regmap);
	int ret;

	ret = regulator_disable(rt5682s->supplies[RT5682S_SUPPLY_AVDD].consumer);
	if (ret)
		dev_err(dev, "Failed to disable supply AVDD: %d\n", ret);

	usleep_range(1000, 1500);

	ret = regulator_disable(rt5682s->supplies[RT5682S_SUPPLY_MICVDD].consumer);
	if (ret)
		dev_err(dev, "Failed to disable supply MICVDD: %d\n", ret);
}

static int rt5682s_i2c_probe(struct i2c_client *i2c)
{
	struct rt5682s_platform_data *pdata = dev_get_platdata(&i2c->dev);
	struct rt5682s_priv *rt5682s;
	int i, ret;
	unsigned int val;

	rt5682s = devm_kzalloc(&i2c->dev, sizeof(struct rt5682s_priv), GFP_KERNEL);
	if (!rt5682s)
		return -ENOMEM;

	i2c_set_clientdata(i2c, rt5682s);

	rt5682s->pdata = i2s_default_platform_data;

	if (pdata)
		rt5682s->pdata = *pdata;
	else
		rt5682s_parse_dt(rt5682s, &i2c->dev);

	rt5682s->regmap = devm_regmap_init_i2c(i2c, &rt5682s_regmap);
	if (IS_ERR(rt5682s->regmap)) {
		ret = PTR_ERR(rt5682s->regmap);
		dev_err(&i2c->dev, "Failed to allocate register map: %d\n", ret);
		return ret;
	}

	for (i = 0; i < ARRAY_SIZE(rt5682s->supplies); i++)
		rt5682s->supplies[i].supply = rt5682s_supply_names[i];

	ret = devm_regulator_bulk_get(&i2c->dev,
			ARRAY_SIZE(rt5682s->supplies), rt5682s->supplies);
	if (ret) {
		dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
		return ret;
	}

	ret = devm_add_action_or_reset(&i2c->dev, rt5682s_i2c_disable_regulators, rt5682s);
	if (ret)
		return ret;

	ret = regulator_enable(rt5682s->supplies[RT5682S_SUPPLY_MICVDD].consumer);
	if (ret) {
		dev_err(&i2c->dev, "Failed to enable supply MICVDD: %d\n", ret);
		return ret;
	}
	usleep_range(1000, 1500);

	ret = regulator_enable(rt5682s->supplies[RT5682S_SUPPLY_AVDD].consumer);
	if (ret) {
		dev_err(&i2c->dev, "Failed to enable supply AVDD: %d\n", ret);
		return ret;
	}

	if (gpio_is_valid(rt5682s->pdata.ldo1_en)) {
		if (devm_gpio_request_one(&i2c->dev, rt5682s->pdata.ldo1_en,
					  GPIOF_OUT_INIT_HIGH, "rt5682s"))
			dev_err(&i2c->dev, "Fail gpio_request gpio_ldo\n");
	}

	/* Sleep for 50 ms minimum */
	usleep_range(50000, 55000);

	regmap_read(rt5682s->regmap, RT5682S_DEVICE_ID, &val);
	if (val != DEVICE_ID) {
		dev_err(&i2c->dev, "Device with ID register %x is not rt5682s\n", val);
		return -ENODEV;
	}

	rt5682s_reset(rt5682s);
	rt5682s_apply_patch_list(rt5682s, &i2c->dev);

	regmap_update_bits(rt5682s->regmap, RT5682S_PWR_DIG_2,
		RT5682S_DLDO_I_LIMIT_MASK, RT5682S_DLDO_I_LIMIT_DIS);
	usleep_range(20000, 25000);

	mutex_init(&rt5682s->calibrate_mutex);
	mutex_init(&rt5682s->sar_mutex);
	mutex_init(&rt5682s->wclk_mutex);
	rt5682s_calibrate(rt5682s);

	regmap_update_bits(rt5682s->regmap, RT5682S_MICBIAS_2,
		RT5682S_PWR_CLK25M_MASK | RT5682S_PWR_CLK1M_MASK,
		RT5682S_PWR_CLK25M_PD | RT5682S_PWR_CLK1M_PU);
	regmap_update_bits(rt5682s->regmap, RT5682S_PWR_ANLG_1,
		RT5682S_PWR_BG, RT5682S_PWR_BG);
	regmap_update_bits(rt5682s->regmap, RT5682S_HP_LOGIC_CTRL_2,
		RT5682S_HP_SIG_SRC_MASK, RT5682S_HP_SIG_SRC_1BIT_CTL);
	regmap_update_bits(rt5682s->regmap, RT5682S_HP_CHARGE_PUMP_2,
		RT5682S_PM_HP_MASK, RT5682S_PM_HP_HV);
	regmap_update_bits(rt5682s->regmap, RT5682S_HP_AMP_DET_CTL_1,
		RT5682S_CP_SW_SIZE_MASK, RT5682S_CP_SW_SIZE_M);

	/* DMIC data pin */
	switch (rt5682s->pdata.dmic1_data_pin) {
	case RT5682S_DMIC1_DATA_NULL:
		break;
	case RT5682S_DMIC1_DATA_GPIO2: /* share with LRCK2 */
		regmap_update_bits(rt5682s->regmap, RT5682S_DMIC_CTRL_1,
			RT5682S_DMIC_1_DP_MASK, RT5682S_DMIC_1_DP_GPIO2);
		regmap_update_bits(rt5682s->regmap, RT5682S_GPIO_CTRL_1,
			RT5682S_GP2_PIN_MASK, RT5682S_GP2_PIN_DMIC_SDA);
		break;
	case RT5682S_DMIC1_DATA_GPIO5: /* share with DACDAT1 */
		regmap_update_bits(rt5682s->regmap, RT5682S_DMIC_CTRL_1,
			RT5682S_DMIC_1_DP_MASK, RT5682S_DMIC_1_DP_GPIO5);
		regmap_update_bits(rt5682s->regmap, RT5682S_GPIO_CTRL_1,
			RT5682S_GP5_PIN_MASK, RT5682S_GP5_PIN_DMIC_SDA);
		break;
	default:
		dev_warn(&i2c->dev, "invalid DMIC_DAT pin\n");
		break;
	}

	/* DMIC clk pin */
	switch (rt5682s->pdata.dmic1_clk_pin) {
	case RT5682S_DMIC1_CLK_NULL:
		break;
	case RT5682S_DMIC1_CLK_GPIO1: /* share with IRQ */
		regmap_update_bits(rt5682s->regmap, RT5682S_GPIO_CTRL_1,
			RT5682S_GP1_PIN_MASK, RT5682S_GP1_PIN_DMIC_CLK);
		break;
	case RT5682S_DMIC1_CLK_GPIO3: /* share with BCLK2 */
		regmap_update_bits(rt5682s->regmap, RT5682S_GPIO_CTRL_1,
			RT5682S_GP3_PIN_MASK, RT5682S_GP3_PIN_DMIC_CLK);
		if (rt5682s->pdata.dmic_clk_driving_high)
			regmap_update_bits(rt5682s->regmap, RT5682S_PAD_DRIVING_CTRL,
				RT5682S_PAD_DRV_GP3_MASK, RT5682S_PAD_DRV_GP3_HIGH);
		break;
	default:
		dev_warn(&i2c->dev, "invalid DMIC_CLK pin\n");
		break;
	}

	INIT_DELAYED_WORK(&rt5682s->jack_detect_work, rt5682s_jack_detect_handler);
	INIT_DELAYED_WORK(&rt5682s->jd_check_work, rt5682s_jd_check_handler);

	if (i2c->irq) {
		ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, rt5682s_irq,
			IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
			"rt5682s", rt5682s);
		if (ret)
			dev_err(&i2c->dev, "Failed to reguest IRQ: %d\n", ret);
	}

	return devm_snd_soc_register_component(&i2c->dev, &rt5682s_soc_component_dev,
			rt5682s_dai, ARRAY_SIZE(rt5682s_dai));
}

static void rt5682s_i2c_shutdown(struct i2c_client *client)
{
	struct rt5682s_priv *rt5682s = i2c_get_clientdata(client);

	disable_irq(client->irq);
	cancel_delayed_work_sync(&rt5682s->jack_detect_work);
	cancel_delayed_work_sync(&rt5682s->jd_check_work);

	rt5682s_reset(rt5682s);
}

static void rt5682s_i2c_remove(struct i2c_client *client)
{
	rt5682s_i2c_shutdown(client);
}

static const struct of_device_id rt5682s_of_match[] = {
	{.compatible = "realtek,rt5682s"},
	{},
};
MODULE_DEVICE_TABLE(of, rt5682s_of_match);

static const struct acpi_device_id rt5682s_acpi_match[] = {
	{"RTL5682", 0,},
	{},
};
MODULE_DEVICE_TABLE(acpi, rt5682s_acpi_match);

static const struct i2c_device_id rt5682s_i2c_id[] = {
	{"rt5682s", 0},
	{}
};
MODULE_DEVICE_TABLE(i2c, rt5682s_i2c_id);

static struct i2c_driver rt5682s_i2c_driver = {
	.driver = {
		.name = "rt5682s",
		.of_match_table = rt5682s_of_match,
		.acpi_match_table = rt5682s_acpi_match,
		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
	},
	.probe_new = rt5682s_i2c_probe,
	.remove = rt5682s_i2c_remove,
	.shutdown = rt5682s_i2c_shutdown,
	.id_table = rt5682s_i2c_id,
};
module_i2c_driver(rt5682s_i2c_driver);

MODULE_DESCRIPTION("ASoC RT5682I-VS driver");
MODULE_AUTHOR("Derek Fang <derek.fang@realtek.com>");
MODULE_LICENSE("GPL v2");