summaryrefslogtreecommitdiff
path: root/nuttx/drivers/audio/wm8904.c
blob: f2545e717c6038db204ed86d3acc1bb4618cf1a1 (plain) (blame)
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
/****************************************************************************
 * drivers/audio/wm8904.c
 *
 * Audio device driver for Wolfson Microelectronics WM8904 Audio codec.
 *
 *   Copyright (C) 2014 Gregory Nutt. All rights reserved.
 *   Author:  Gregory Nutt <gnutt@nuttx.org>
 *
 * References:
 * - "WM8904 Ultra Low Power CODEC for Portable Audio Applications, Pre-
 *    Production", September 2012, Rev 3.3, Wolfson Microelectronics
 *
 * -  The framework for this driver is based on Ken Pettit's VS1053 driver.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/types.h>
#include <sys/ioctl.h>

#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <fixedmath.h>
#include <queue.h>
#include <debug.h>

#include <nuttx/kmalloc.h>
#include <nuttx/clock.h>
#include <nuttx/wqueue.h>
#include <nuttx/i2c.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/audio/i2s.h>
#include <nuttx/audio/audio.h>
#include <nuttx/audio/wm8904.h>
#include <nuttx/math.h>

#include "wm8904.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

#if !defined(CONFIG_WM8904_REGDUMP) && !defined(CONFIG_WM8904_CLKDEBUG)
static
#endif
       uint16_t wm8904_readreg(FAR struct wm8904_dev_s *priv,
                  uint8_t regaddr);
static void     wm8904_writereg(FAR struct wm8904_dev_s *priv,
                  uint8_t regaddr, uint16_t regval);
static void     wm8904_takesem(sem_t *sem);
#define         wm8904_givesem(s) sem_post(s)

#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
static inline uint16_t wm8904_scalevolume(uint16_t volume, b16_t scale);
static void     wm8904_setvolume(FAR struct wm8904_dev_s *priv,
                 uint16_t volume, bool mute);
#endif
#ifndef CONFIG_AUDIO_EXCLUDE_TONE
static void     wm8904_setbass(FAR struct wm8904_dev_s *priv, uint8_t bass);
static void     wm8904_settreble(FAR struct wm8904_dev_s *priv, uint8_t treble);
#endif

static void     wm8904_setdatawidth(FAR struct wm8904_dev_s *priv);
static void     wm8904_setbitrate(FAR struct wm8904_dev_s *priv);

/* Audio lower half methods (and close friends) */

static int      wm8904_getcaps(FAR struct audio_lowerhalf_s *dev, int type,
                  FAR struct audio_caps_s *caps);
#ifdef CONFIG_AUDIO_MULTI_SESSION
static int      wm8904_configure(FAR struct audio_lowerhalf_s *dev,
                  FAR void *session, FAR const struct audio_caps_s *caps);
#else
static int      wm8904_configure(FAR struct audio_lowerhalf_s *dev,
                  FAR const struct audio_caps_s *caps);
#endif
static int      wm8904_shutdown(FAR struct audio_lowerhalf_s *dev);
static void     wm8904_senddone(FAR struct i2s_dev_s *i2s,
                  FAR struct ap_buffer_s *apb, FAR void *arg, int result);
static void     wm8904_returnbuffers(FAR struct wm8904_dev_s *priv);
static int      wm8904_sendbuffer(FAR struct wm8904_dev_s *priv);

#ifdef CONFIG_AUDIO_MULTI_SESSION
static int      wm8904_start(FAR struct audio_lowerhalf_s *dev,
                  FAR void *session);
#else
static int      wm8904_start(FAR struct audio_lowerhalf_s *dev);
#endif
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
#ifdef CONFIG_AUDIO_MULTI_SESSION
static int      wm8904_stop(FAR struct audio_lowerhalf_s *dev,
                  FAR void* session);
#else
static int      wm8904_stop(FAR struct audio_lowerhalf_s *dev);
#endif
#endif
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
#ifdef CONFIG_AUDIO_MULTI_SESSION
static int      wm8904_pause(FAR struct audio_lowerhalf_s *dev,
                  FAR void* session);
static int      wm8904_resume(FAR struct audio_lowerhalf_s *dev,
                  FAR void* session);
#else
static int      wm8904_pause(FAR struct audio_lowerhalf_s *dev);
static int      wm8904_resume(FAR struct audio_lowerhalf_s *dev);
#endif
#endif
static int      wm8904_enqueuebuffer(FAR struct audio_lowerhalf_s *dev,
                  FAR struct ap_buffer_s *apb);
static int      wm8904_cancelbuffer(FAR struct audio_lowerhalf_s *dev,
                  FAR struct ap_buffer_s *apb);
static int      wm8904_ioctl(FAR struct audio_lowerhalf_s *dev, int cmd,
                  unsigned long arg);
#ifdef CONFIG_AUDIO_MULTI_SESSION
static int      wm8904_reserve(FAR struct audio_lowerhalf_s *dev,
                  FAR void **session);
#else
static int      wm8904_reserve(FAR struct audio_lowerhalf_s *dev);
#endif
#ifdef CONFIG_AUDIO_MULTI_SESSION
static int      wm8904_release(FAR struct audio_lowerhalf_s *dev,
                  FAR void *session);
#else
static int      wm8904_release(FAR struct audio_lowerhalf_s *dev);
#endif

/* Interrupt handling an worker thread */

#ifdef WM8904_USE_FFLOCK_INT
static void     wm8904_interrupt_work(FAR void *arg);
static int      wm8904_interrupt(FAR const struct wm8904_lower_s *lower,
                  FAR void *arg);
#endif

static void    *wm8904_workerthread(pthread_addr_t pvarg);

/* Initialization */

static void     wm8904_audio_output(FAR struct wm8904_dev_s *priv);
#if 0 /* Not used */
static void     wm8904_audio_input(FAR struct wm8904_dev_s *priv);
#endif
#ifdef WM8904_USE_FFLOCK_INT
static void     wm8904_configure_ints(FAR struct wm8904_dev_s *priv);
#else
#  define       wm8904_configure_ints(p)
#endif
static void     wm8904_hw_reset(FAR struct wm8904_dev_s *priv);

/****************************************************************************
 * Private Data
 ****************************************************************************/

static const struct audio_ops_s g_audioops =
{
  wm8904_getcaps,       /* getcaps        */
  wm8904_configure,     /* configure      */
  wm8904_shutdown,      /* shutdown       */
  wm8904_start,         /* start          */
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
  wm8904_stop,          /* stop           */
#endif
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  wm8904_pause,         /* pause          */
  wm8904_resume,        /* resume         */
#endif
  NULL,                 /* allocbuffer    */
  NULL,                 /* freebuffer     */
  wm8904_enqueuebuffer, /* enqueue_buffer */
  wm8904_cancelbuffer,  /* cancel_buffer  */
  wm8904_ioctl,         /* ioctl          */
  NULL,                 /* read           */
  NULL,                 /* write          */
  wm8904_reserve,       /* reserve        */
  wm8904_release        /* release        */
};

#ifndef CONFIG_WM8904_CLKDEBUG
static
#endif
const uint8_t g_sysclk_scaleb1[WM8904_BCLK_MAXDIV+1] =
{
   2,  3,  4,  6,  8, 10, 11, /*  1,  1.5,  2,  3,  4,  5,  5.5 */
  12, 16, 20, 22, 24, 32, 40, /*  6,  8,   10, 11, 12, 16, 20   */
  44, 48, 50, 60, 64, 88, 96  /* 22, 24,   25, 30, 32, 44, 48   */
};

#ifndef CONFIG_WM8904_CLKDEBUG
static
#endif
const uint8_t g_fllratio[WM8904_NFLLRATIO] = {1, 2, 4, 8, 16};

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: wm8904_readreg
 *
 * Description
 *    Read the specified 16-bit register from the WM8904 device.
 *
 ****************************************************************************/

#if !defined(CONFIG_WM8904_REGDUMP) && !defined(CONFIG_WM8904_CLKDEBUG)
static
#endif
uint16_t wm8904_readreg(FAR struct wm8904_dev_s *priv, uint8_t regaddr)
{
  int retries;

  /* Try up to three times to read the register */

  for (retries = 1; retries <= 3; retries++)
    {
      struct i2c_msg_s msg[2];
      uint8_t data[2];
      int ret;

      /* Set up to write the address */

      msg[0].addr   = priv->lower->address;
      msg[0].flags  = 0;
      msg[0].buffer = &regaddr;
      msg[0].length = 1;

      /* Followed by the read data */

      msg[1].addr   = priv->lower->address;
      msg[1].flags  = I2C_M_READ;
      msg[1].buffer = data;
      msg[1].length = 2;

      /* Read the register data.  The returned value is the number messages
       * completed.
       */

      ret = I2C_TRANSFER(priv->i2c, msg, 2);
      if (ret < 0)
        {
#ifdef CONFIG_I2C_RESET
          /* Perhaps the I2C bus is locked up?  Try to shake the bus free */

          auddbg("WARNING: I2C_TRANSFER failed: %d ... Resetting\n", ret);

          ret = up_i2creset(priv->i2c);
          if (ret < 0)
            {
              auddbg("ERROR: up_i2creset failed: %d\n", ret);
              break;
            }
#else
          auddbg("ERROR: I2C_TRANSFER failed: %d\n", ret);
#endif
        }
      else
        {
          uint16_t regval;

          /* The I2C transfer was successful... break out of the loop and
           * return the value read.
           */

          regval = ((uint16_t)data[0] << 8) | (uint16_t)data[1];
          audvdbg("Read: %02x -> %04x\n", regaddr, regval);
          return regval;
        }

      audvdbg("retries=%d regaddr=%02x\n", retries, regaddr);
    }

  /* No error indication is returned on a failure... just return zero */

  return 0;
}

/************************************************************************************
 * Name: wm8904_writereg
 *
 * Description:
 *   Write the specified 16-bit register to the WM8904 device.
 *
 ************************************************************************************/

static void wm8904_writereg(FAR struct wm8904_dev_s *priv, uint8_t regaddr,
                            uint16_t regval)
{
  int retries;

  /* Try up to three times to read the register */

  for (retries = 1; retries <= 3; retries++)
    {
      uint8_t data[3];
      int ret;

      /* Set up the data to write */

      data[0] = regaddr;
      data[1] = regval >> 8;
      data[2] = regval & 0xff;

      /* Read the register data.  The returned value is the number messages
       * completed.
       */

      ret = I2C_WRITE(priv->i2c, data, 3);
      if (ret < 0)
        {
#ifdef CONFIG_I2C_RESET
          /* Perhaps the I2C bus is locked up?  Try to shake the bus free */

          auddbg("WARNING: I2C_TRANSFER failed: %d ... Resetting\n", ret);

          ret = up_i2creset(priv->i2c);
          if (ret < 0)
            {
              auddbg("ERROR: up_i2creset failed: %d\n", ret);
              break;
            }
#else
          auddbg("ERROR: I2C_TRANSFER failed: %d\n", ret);
#endif
        }
      else
        {
          /* The I2C transfer was successful... break out of the loop and
           * return the value read.
           */

          audvdbg("Write: %02x <- %04x\n", regaddr, regval);
          return;
        }

      audvdbg("retries=%d regaddr=%02x\n", retries, regaddr);
    }
}

/************************************************************************************
 * Name: wm8904_takesem
 *
 * Description:
 *  Take a semaphore count, handling the nasty EINTR return if we are interrupted
 *  by a signal.
 *
 ************************************************************************************/

static void wm8904_takesem(sem_t *sem)
{
  int ret;

  do
    {
      ret = sem_wait(sem);
      DEBUGASSERT(ret == 0 || errno == EINTR);
    }
  while (ret < 0);
}

/************************************************************************************
 * Name: wm8904_scalevolume
 *
 * Description:
 *   Set the right and left volume values in the WM8904 device based on the current
 *   volume and balance settings.
 *
 ************************************************************************************/

#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
static inline uint16_t wm8904_scalevolume(uint16_t volume, b16_t scale)
{
  return b16toi((b16_t)volume * scale);
}
#endif

/************************************************************************************
 * Name: wm8904_setvolume
 *
 * Description:
 *   Set the right and left volume values in the WM8904 device based on the current
 *   volume and balance settings.
 *
 ************************************************************************************/

#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
static void wm8904_setvolume(FAR struct wm8904_dev_s *priv, uint16_t volume,
                             bool mute)
{
  uint32_t leftlevel;
  uint32_t rightlevel;
  uint16_t regval;

  audvdbg("volume=%u mute=%u\n", volume, mute);

#ifndef CONFIG_AUDIO_EXCLUDE_BALANCE
  /* Calculate the left channel volume level {0..1000} */

  if (priv->balance <= 500)
    {
      leftlevel = volume;
    }
  else if (priv->balance == 1000)
    {
      leftlevel = 0;
    }
  else
    {
      leftlevel = wm8904_scalevolume(volume, b16ONE - (b16_t)priv->balance);
    }

  /* Calculate the right channel volume level {0..1000} */

  if (priv->balance >= 500)
    {
      rightlevel = volume;
    }
  else if (priv->balance == 0)
    {
      rightlevel = 0;
    }
  else
    {
      rightlevel = wm8904_scalevolume(volume, (b16_t)priv->balance);
    }
#else
  leftlevel  = priv->volume;
  rightlevel = priv->volume;
#endif

  /* Set the volume */

  regval = WM8904_HPOUTZC | WM8904_HPOUT_VOL(leftlevel);
  if (mute)
    {
      regval |= WM8904_HPOUT_MUTE;
    }

  wm8904_writereg(priv, WM8904_ANA_LEFT_OUT1, regval);

  regval = WM8904_HPOUTZC | WM8904_HPOUT_VOL(rightlevel);
  if (mute)
    {
      regval |= WM8904_HPOUT_MUTE;
    }

  wm8904_writereg(priv, WM8904_ANA_RIGHT_OUT1, regval);

  /* Remember the volume level and mute settings */

  priv->volume = volume;
  priv->mute   = mute;
}
#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */

/************************************************************************************
 * Name: wm8904_setbass
 *
 * Description:
 *   Set the bass level.
 *
 *   The level and range are in whole percentage levels (0-100).
 *
 ************************************************************************************/

#ifndef CONFIG_AUDIO_EXCLUDE_TONE
static void wm8904_setbass(FAR struct wm8904_dev_s *priv, uint8_t bass)
{
  audvdbg("bass=%u\n", bass);
#warning Missing logic
}
#endif /* CONFIG_AUDIO_EXCLUDE_TONE */

/************************************************************************************
 * Name: wm8904_settreble
 *
 * Description:
 *   Set the treble level .
 *
 *   The level and range are in whole percentage levels (0-100).
 *
 ************************************************************************************/

#ifndef CONFIG_AUDIO_EXCLUDE_TONE
static void wm8904_settreble(FAR struct wm8904_dev_s *priv, uint8_t treble)
{
  audvdbg("treble=%u\n", treble);
#warning Missing logic
}
#endif /* CONFIG_AUDIO_EXCLUDE_TONE */

/****************************************************************************
 * Name: wm8904_setdatawidth
 *
 * Description:
 *   Set the 8- or 16-bit data modes
 *
 ****************************************************************************/

static void wm8904_setdatawidth(FAR struct wm8904_dev_s *priv)
{
  uint16_t regval;

  /* "8-bit mode is selected whenever DAC_COMP=1 or ADC_COMP=1. The use of
   *  8-bit data allows samples to be passed using as few as 8 BCLK cycles
   *  per LRCLK frame. When using DSP mode B, 8-bit data words may be
   *  transferred consecutively every 8 BCLK cycles.
   *
   * "8-bit mode (without Companding) may be enabled by setting
   *  DAC_COMPMODE=1 or ADC_COMPMODE=1, when DAC_COMP=0 and ADC_COMP=0.
   */

  if (priv->bpsamp == 16)
    {
      /* Reset default default setting */

      regval = (WM8904_AIFADCR_SRC | WM8904_AIFDACR_SRC);
      wm8904_writereg(priv, WM8904_AIF0, regval);
    }
  else
    {
      /* This should select 8-bit with no companding */

      regval = (WM8904_AIFADCR_SRC  | WM8904_AIFDACR_SRC |
                WM8904_ADC_COMPMODE | WM8904_DAC_COMPMODE);
      wm8904_writereg(priv, WM8904_AIF0, regval);
    }
}

/****************************************************************************
 * Name: wm8904_setbitrate
 *
 * Description:
 *   Program the FLL to achieve the requested bitrate (fout).  Given:
 *
 *     samprate  - Samples per second
 *     nchannels - Number of channels of data
 *     bpsamp    - Bits per sample
 *
 *   Then
 *     fout = samprate * nchannels * bpsamp
 *
 *   For example:
 *     samplerate = 11,025 samples/sec
 *     nchannels  = 1
 *     bpsamp     = 16     bits
 *
 *   Then
 *     fout    = 11025 samples/sec * 1 * 16 bits/sample = 176.4 bits/sec
 *
 *   The clocking is configured like this:
 *     MCLK   is the FLL source clock
 *     Fref   is the scaled down version of MCLK
 *     Fvco   is the output frequency from the FLL
 *     Fout   is the final output from the FLL that drives the SYSCLK
 *     SYSCLK can be divided down to generate the BCLK
 *
 *   The FLL output frequency is generated at that fout by:
 *
 *     Fout = (Fvco / FLL_OUTDIV)
 *
 *   The FLL operating frequency is set according to:
 *
 *     Fvco = Fref * N.K * FLL_RATIO
 *
 *   Where Fref is the input frequency frequency as determined by
 *   FLL_CLK_REF_DIV. Fvco must be in the range of 90-100MHz.
 *
 *   As an example:
 *     FLL_CLK_REF_DIV = 16
 *     FLL_OUTDIV = 8
 *     N.K = 187.25
 *     FLL_RATIO=16
 *     Fref =32,768
 *
 *     Fvco = 32,768 * 187.25 / 16 = 383,488 Hz
 *     Fout = 383,488 / 8 = 47,936 Hz (approx. 48Khz)
 *
 ****************************************************************************/

static void wm8904_setbitrate(FAR struct wm8904_dev_s *priv)
{
  uint64_t tmp64;
  uint32_t fref;
  uint32_t fvco;
  uint32_t fout;
  uint32_t minfout;
  uint16_t regval;
  b16_t nk;
  unsigned int fllndx;
  unsigned int divndx;
  unsigned int outdiv;
  unsigned int framelen;
#ifdef WM8904_USE_FFLOCK_INT
  bool enabled;
  int retries;
#endif

  DEBUGASSERT(priv && priv->lower);

  /* First calculate the desired bitrate (fout).  This is based on
   *
   * 1. The I2S frame length (in bits)
   * 2. The number of frames per second = nchannnels * samplerate
   */

  framelen = (priv->bpsamp == 8) ? WM8904_FRAMELEN8 : WM8904_FRAMELEN16;
  fout = (uint32_t)priv->samprate * (uint32_t)priv->nchannels * framelen;

  regval = WM8904_LRCLK_DIR | WM8904_LRCLK_RATE(framelen << 1);
  wm8904_writereg(priv, WM8904_AIF3, regval);

  audvdbg("sample rate=%u nchannels=%u bpsamp=%u framelen=%d fout=%lu\n",
          priv->samprate, priv->nchannels, priv->bpsamp, framelen,
          (unsigned long)fout);

  /* Disable the SYSCLK.
   *
   * "The SYSCLK signal is enabled by register bit CLK_SYS_ENA. This bit
   * should be set to 0 when reconfiguring clock sources. ... "
   *
   * REVISIT:  This does not appear necessary if we are just reconfiguring
   * the FLL.  Disabling the FLL will stop the SYSCLK input just fine.
   */

  regval = WM8904_SYSCLK_SRCFLL | WM8904_CLK_DSP_ENA;
  wm8904_writereg(priv, WM8904_CLKRATE2, regval);

#if 0 /* Unnecessary */
  /* Unlock forced oscillator control and switch it off */

  wm8904_writereg(priv, WM8904_CTRLIF_TEST_1, WM8904_USER_KEY);
  wm8904_writereg(priv, WM8904_FLL_NCO_TEST1, 0);
  wm8904_writereg(priv, WM8904_CTRLIF_TEST_1, 0);
#endif

  /* "The FLL is enabled using the FLL_ENA register bit. Note that, when
   * changing FLL settings, it is recommended that the digital circuit be
   * disabled via FLL_ENA and then re-enabled after the other register
   * settings have been updated."
   */

  wm8904_writereg(priv, WM8904_FLL_CTRL1, 0);

  /* Determine Fref.  The source refrence clock should be the MCLK */

  fref   = priv->lower->mclk;
  regval = (WM8904_FLL_CLK_REF_SRC_MCLK | WM8904_FLL_CLK_REF_DIV1);

  /* MCLK must be divided down so that fref <=13.5MHz */

  if (fref > 4*13500000)
    {
      fref >>= 3;
      regval = (WM8904_FLL_CLK_REF_SRC_MCLK | WM8904_FLL_CLK_REF_DIV8);
    }
  else if (fref > 2*13500000)
    {
      fref >>= 2;
      regval = (WM8904_FLL_CLK_REF_SRC_MCLK | WM8904_FLL_CLK_REF_DIV4);
    }
  else if (fref > 13500000)
    {
      fref >>= 1;
      regval = (WM8904_FLL_CLK_REF_SRC_MCLK | WM8904_FLL_CLK_REF_DIV2);
    }

  wm8904_writereg(priv, WM8904_FLL_CTRL5, regval);

  /* Fvco must be between 90 and 100Mhz.  In order to meet this
   * requirement, the value of FLL_OUTDIV should be selected according
   * to the desired output Fout.  The divider, FLL_OUTDIV, must be set
   * so that Fvco is in the range 90-100MHz.  The available divisions
   * are integers from 4 to 64.
   *
   *   Fout = Fvco /FLL_OUTDIV
   *
   *
   * Is this Fout realizable?  This often happens for very low frequencies.
   * If so, we can select a different final SYSCLK scaling frequency.
   */

  minfout = WM8904_FVCO_MAX / WM8904_MAXOUTDIV;
  divndx = 0;

  for (;;)
    {
      /* Calculate the new value of Fout that we would need to provide
       * with this SYSCLK divider in place.
       */

      uint32_t newfout = (g_sysclk_scaleb1[divndx] * fout) >> 1;

      /* Is this increased Fout realizable?  Or are we just just out of
       * dividers?
       */

      if (newfout >= minfout || divndx == WM8904_BCLK_MAXDIV)
        {
          /* In either case, this is the Fout and divider that we will be
           * using.
           */

          fout = newfout;
          break;
        }

      /* We have more.. Try the next divider */

      divndx++;
    }

  /* When we get here, divndx holds the register value for the new SYSCLK
   * divider.  Set the divider value in the Audio Interface 2 register.
   */

  regval = WM8904_OPCLK_DIV1 | WM8904_BCLK_DIV(divndx);
  wm8904_writereg(priv, WM8904_AIF2, regval);

  /* Now lets make our best guess for FLL_OUTDIV
   *
   *   FLL_OUTDIV = 95000000 / Fout
   */

  outdiv = ((WM8904_FVCO_MAX + WM8904_FVCO_MAX) >> 1) / fout;
  if (outdiv < 4)
    {
      outdiv = 4;
    }
  else if (outdiv > 64)
    {
      outdiv = 64;
    }

  /* The WM8904 suggests the selecting FLL_RATIO via the following
   * range checks:
   */

  if (fref >= 1000000)
    {
      fllndx = WM8904_NFLLRATIO_DIV1;
    }
  else if (fref > 256000)
    {
      fllndx = WM8904_NFLLRATIO_DIV2;
    }
  else if (fref > 128000)
    {
      fllndx = WM8904_NFLLRATIO_DIV4;
    }
  else if (fref > 64000)
    {
      fllndx = WM8904_NFLLRATIO_DIV8;
    }
  else
    {
      fllndx = WM8904_NFLLRATIO_DIV16;
    }

  /* Finally, we need to determine the value of N.K
   *
   *   Fvco = (Fout * FLL_OUTDIV)
   *   N.K  = Fvco / (FLL_FRATIO * FREF)
   */

  fvco  = fout * outdiv;
  tmp64 = ((uint64_t)fvco << 16) / (g_fllratio[fllndx] * fref);
  nk    = (b16_t)tmp64;

  audvdbg("mclk=%lu fref=%lu fvco=%lu fout=%lu divndx=%u\n",
          (unsigned long)priv->lower->mclk, (unsigned long)fref,
          (unsigned long)fvco, (unsigned long)fout, divndx);
  audvdbg("N.K=%08lx outdiv=%u fllratio=%u\n",
          (unsigned long)nk, outdiv, g_fllratio[fllndx]);

  /* Save the actual bit rate that we are using.  This will be used by the
   * LRCLCK calculations.
   */

  priv->bitrate = fout;

  /* Now, Configure the FLL */
  /* FLL Control 1
   *
   * FLL_FRACN_ENA=1        : Enables fractional mode
   * FLL_OSC_EN=0           : FLL internal oscillator disabled
   * FLL_ENA=0              : The FLL is not enabled
   *
   * FLL_OSC_ENA must be enabled before enabling FLL_ENA (FLL_OSC_ENA is
   * only required for free-running modes).
   */

  wm8904_writereg(priv, WM8904_FLL_CTRL1, 0);
  wm8904_writereg(priv, WM8904_FLL_CTRL1, WM8904_FLL_FRACN_ENA);

  /* FLL Control 2
   *
   * FLL_OUTDIV             : FLL Fout clock divider
   *                        : Fout = Fvco / FLL_OUTDIV
   *                        : Calculated above
   * FLL_CTRL_RATE=1        : Frequency of the FLL control block,
   *                        : = Fvco / FLL_CTRL_RATE
   * FLL_FRATIO             : Fvco clock divider
   *                        : Determined by MCLK tests above
   */

  regval = WM8904_FLL_OUTDIV(outdiv) | WM8904_FLL_CTRL_RATE(1) |
           WM8904_FLL_FRATIO(fllndx);
  wm8904_writereg(priv, WM8904_FLL_CTRL2, regval);

  /* FLL Control 3
   *
   * Fractional multiply for Fref
   */

  wm8904_writereg(priv, WM8904_FLL_CTRL3, b16frac(nk));

  /* FLL Control 4
   *
   * FLL_N                  : Integer multiply for Fref
   * FLL_GAIN               : Gain applied to error
   */

  regval = WM8904_FLL_N(b16toi(nk)) | WM8904_FLL_GAIN_X1;
  wm8904_writereg(priv, WM8904_FLL_CTRL4, regval);

  /* FLL Control 5
   *
   * FLL_CLK_REF_DIV        : FLL Clock Reference Divider
   *
   * Already set above
   */

  /* Enable the FLL */

  regval = WM8904_FLL_FRACN_ENA | WM8904_FLL_ENA;
  wm8904_writereg(priv, WM8904_FLL_CTRL1, regval);

#if defined(WM8904_USE_FFLOCK_INT)
  /* Make sure that interrupts are enabled */

  enabled = WM8904_ENABLE(priv->lower);

  /* Enable the FLL lock interrupt.  Here we can be sloppy since the FLL
   * lock is the only interrupt every enabled.
   */

  priv->locked = false;
  regval = WM8904_ALL_INTS & ~WM8904_FLL_LOCK_INT;
  wm8904_writereg(priv, WM8904_INT_MASK, regval);

  /* Allow time for FLL lock.  Typical is 2 MSec.  No exotic interlock
   * here; we just poll a flag set by the interrupt handler.
   * REVISIT: Probably not necessary.
   */

  retries = 5;
  do
    {
      usleep(5*5000);
    }
  while (priv->locked == false && --retries > 0);

  /* Make sure that the FLL lock interrupt is disabled and clear any pending
   * interrupt status (again cutting* some corners).  NOTE: The interrupt
   * handler will do these things if there is no timeout.
   */

  WM8904_DISABLE(priv->lower);
  wm8904_writereg(priv, WM8904_INT_MASK, WM8904_ALL_INTS);
  wm8904_writereg(priv, WM8904_INT_STATUS, WM8904_ALL_INTS);

  /* Restore the interrupt state.  */

  WM8904_RESTORE(priv->lower, enabled)

#elif defined(WM8904_USE_FFLOCK_POLL)
  /* Allow time for FLL lock.  Typical is 2 MSec. */

  retries = 5;
  do
    {
       usleep(5*5000);
    }
  while ((wm8904_readreg(priv, WM8904_INT_STATUS) & WM8904_FLL_LOCK_INT) != 0 ||
          --retries > 0);

  /* Clear all pending status bits by writing 1's into the interrupt status
   * register.
   */

  wm8904_writereg(priv, WM8904_INT_STATUS, WM8904_ALL_INTS);

#endif /* !WM8904_USE_FFLOCK_INT && !WM8904_USE_FFLOCK_POLL */

  /* Re-enable the SYSCLK. */

  regval = WM8904_SYSCLK_SRCFLL | WM8904_CLK_SYS_ENA | WM8904_CLK_DSP_ENA;
  wm8904_writereg(priv, WM8904_CLKRATE2, regval);
}

/****************************************************************************
 * Name: wm8904_getcaps
 *
 * Description:
 *   Get the audio device capabilities
 *
 ****************************************************************************/

static int wm8904_getcaps(FAR struct audio_lowerhalf_s *dev, int type,
                          FAR struct audio_caps_s *caps)
{
  /* Validate the structure */

  DEBUGASSERT(caps && caps->ac_len >= sizeof(struct audio_caps_s));
  audvdbg("type=%d ac_type=%d\n", type, caps->ac_type);

  /* Fill in the caller's structure based on requested info */

  caps->ac_format.hw  = 0;
  caps->ac_controls.w = 0;

  switch (caps->ac_type)
    {
      /* Caller is querying for the types of units we support */

      case AUDIO_TYPE_QUERY:

        /* Provide our overall capabilities.  The interfacing software
         * must then call us back for specific info for each capability.
         */

        caps->ac_channels = 2;       /* Stereo output */

        switch (caps->ac_subtype)
          {
            case AUDIO_TYPE_QUERY:
              /* We don't decode any formats!  Only something above us in
               * the audio stream can perform decoding on our behalf.
               */

              /* The types of audio units we implement */

              caps->ac_controls.b[0] = AUDIO_TYPE_OUTPUT | AUDIO_TYPE_FEATURE |
                                     AUDIO_TYPE_PROCESSING;

              break;

            case AUDIO_FMT_MIDI:
              /* We only support Format 0 */

              caps->ac_controls.b[0] = AUDIO_SUBFMT_END;
              break;

            default:
              caps->ac_controls.b[0] = AUDIO_SUBFMT_END;
              break;
          }

        break;

      /* Provide capabilities of our OUTPUT unit */

      case AUDIO_TYPE_OUTPUT:

        caps->ac_channels = 2;

        switch (caps->ac_subtype)
          {
            case AUDIO_TYPE_QUERY:

              /* Report the Sample rates we support */

              caps->ac_controls.b[0] = AUDIO_SAMP_RATE_8K | AUDIO_SAMP_RATE_11K |
                                       AUDIO_SAMP_RATE_16K | AUDIO_SAMP_RATE_22K |
                                       AUDIO_SAMP_RATE_32K | AUDIO_SAMP_RATE_44K |
                                       AUDIO_SAMP_RATE_48K;
              break;

            case AUDIO_FMT_MP3:
            case AUDIO_FMT_WMA:
            case AUDIO_FMT_PCM:
              break;

            default:
              break;
          }

        break;

      /* Provide capabilities of our FEATURE units */

      case AUDIO_TYPE_FEATURE:

        /* If the sub-type is UNDEF, then report the Feature Units we support */

        if (caps->ac_subtype == AUDIO_FU_UNDEF)
          {
            /* Fill in the ac_controls section with the Feature Units we have */

            caps->ac_controls.b[0] = AUDIO_FU_VOLUME | AUDIO_FU_BASS | AUDIO_FU_TREBLE;
            caps->ac_controls.b[1] = AUDIO_FU_BALANCE >> 8;
          }
        else
          {
            /* TODO:  Do we need to provide specific info for the Feature Units,
             * such as volume setting ranges, etc.?
             */
          }

        break;

      /* Provide capabilities of our PROCESSING unit */

      case AUDIO_TYPE_PROCESSING:

        switch (caps->ac_subtype)
          {
            case AUDIO_PU_UNDEF:

              /* Provide the type of Processing Units we support */

              caps->ac_controls.b[0] = AUDIO_PU_STEREO_EXTENDER;
              break;

            case AUDIO_PU_STEREO_EXTENDER:

              /* Provide capabilities of our Stereo Extender */

              caps->ac_controls.b[0] = AUDIO_STEXT_ENABLE | AUDIO_STEXT_WIDTH;
              break;

            default:

              /* Other types of processing uint we don't support */

              break;
          }

        break;

      /* All others we don't support */

      default:

        /* Zero out the fields to indicate no support */

        caps->ac_subtype = 0;
        caps->ac_channels = 0;

        break;
    }

  /* Return the length of the audio_caps_s struct for validation of
   * proper Audio device type.
   */

  return caps->ac_len;
}

/****************************************************************************
 * Name: wm8904_configure
 *
 * Description:
 *   Configure the audio device for the specified  mode of operation.
 *
 ****************************************************************************/

#ifdef CONFIG_AUDIO_MULTI_SESSION
static int wm8904_configure(FAR struct audio_lowerhalf_s *dev,
                            FAR void *session,
                            FAR const struct audio_caps_s *caps)
#else
static int wm8904_configure(FAR struct audio_lowerhalf_s *dev,
                            FAR const struct audio_caps_s *caps)
#endif
{
#if !defined(CONFIG_AUDIO_EXCLUDE_VOLUME) || !defined(CONFIG_AUDIO_EXCLUDE_TONE)
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
#endif
  int ret = OK;

  DEBUGASSERT(priv && caps);
  audvdbg("ac_type: %d\n", caps->ac_type);

  /* Process the configure operation */

  switch (caps->ac_type)
    {
    case AUDIO_TYPE_FEATURE:
      audvdbg("  AUDIO_TYPE_FEATURE\n");

      /* Process based on Feature Unit */

      switch (caps->ac_format.hw)
        {
#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
        case AUDIO_FU_VOLUME:
          {
            /* Set the volume */

            uint16_t volume = caps->ac_controls.hw[0];
            audvdbg("    Volume: %d\n", volume);

            if (volume >= 0 && volume <= 1000)
              {
                /* Scale the volume setting to the range {0.. 63} */

                wm8904_setvolume(priv, (63 * volume / 1000), priv->mute);
              }
            else
              {
                ret = -EDOM;
              }
           }
          break;
#endif  /* CONFIG_AUDIO_EXCLUDE_VOLUME */

#ifndef CONFIG_AUDIO_EXCLUDE_TONE
        case AUDIO_FU_BASS:
          {
            /* Set the bass.  The percentage level (0-100) is in the
             * ac_controls.b[0] parameter.
             */

            uint8_t bass = caps->ac_controls.b[0];
            audvdbg("    Bass: %d\n", bass);

            if (bass <= 100)
              {
                wm8904_setbass(priv, bass);
              }
            else
              {
                ret = -EDOM;
              }
          }
          break;

        case AUDIO_FU_TREBLE:
          {
            /* Set the treble.  The percentage level (0-100) is in the
             * ac_controls.b[0] parameter.
             */

            uint8_t treble = caps->ac_controls.b[0];
            audvdbg("    Treble: %d\n", treble);

            if (treble <= 100)
              {
                wm8904_settreble(priv, treble);
              }
            else
              {
                ret = -EDOM;
              }
          }
          break;
#endif  /* CONFIG_AUDIO_EXCLUDE_TONE */

        default:
          auddbg("    Unrecognized feature unit\n");
          ret = -ENOTTY;
          break;
        }
        break;

    case AUDIO_TYPE_OUTPUT:
      {
        audvdbg("  AUDIO_TYPE_OUTPUT:\n");
        audvdbg("    Number of channels: %u\n", caps->ac_channels);
        audvdbg("    Sample rate:        %u\n", caps->ac_controls.hw[0]);
        audvdbg("    Sample width:       %u\n", caps->ac_controls.b[2]);

        /* Verify that all of the requested values are supported */

        ret = -ERANGE;
        if (caps->ac_channels != 1 && caps->ac_channels != 2)
          {
            auddbg("ERROR: Unsupported number of channels: %d\n",
                   caps->ac_channels);
            break;
          }

        if (caps->ac_controls.b[2] != 8 && caps->ac_controls.b[2] != 16)
          {
            auddbg("ERROR: Unsupported bits per sample: %d\n",
                   caps->ac_controls.b[2]);
            break;
          }

        /* Save the current stream configuration */

        priv->samprate  = caps->ac_controls.hw[0];
        priv->nchannels = caps->ac_channels;
        priv->bpsamp    = caps->ac_controls.b[2];

        /* Reconfigure the FLL to support the resulting number or channels,
         * bits per sample, and bitrate.
         */

        wm8904_setdatawidth(priv);
        wm8904_setbitrate(priv);
        wm8904_writereg(priv, WM8904_DUMMY, 0x55aa);

        wm8904_clock_analysis(&priv->dev, "AUDIO_TYPE_OUTPUT");
        ret = OK;
      }
      break;

    case AUDIO_TYPE_PROCESSING:
      break;
    }

  return ret;
}

/****************************************************************************
 * Name: wm8904_shutdown
 *
 * Description:
 *   Shutdown the WM8904 chip and put it in the lowest power state possible.
 *
 ****************************************************************************/

static int wm8904_shutdown(FAR struct audio_lowerhalf_s *dev)
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;

  DEBUGASSERT(priv);

  /* First disable interrupts */

  WM8904_DISABLE(priv->lower);

  /* Now issue a software reset.  This puts all WM8904 registers back in
   * their default state.
   */

  wm8904_hw_reset(priv);
  return OK;
}

/****************************************************************************
 * Name: wm8904_senddone
 *
 * Description:
 *   This is the I2S callback function that is invoked when the transfer
 *   completes.
 *
 ****************************************************************************/

static void  wm8904_senddone(FAR struct i2s_dev_s *i2s,
                             FAR struct ap_buffer_s *apb, FAR void *arg,
                             int result)
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)arg;
  struct audio_msg_s msg;
  irqstate_t flags;
  int ret;

  DEBUGASSERT(i2s && priv && priv->running && apb);
  audvdbg("apb=%p inflight=%d result=%d\n", apb, priv->inflight, result);

  /* We do not place any restriction on the context in which this function
   * is called.  It may be called from an interrupt handler.  Therefore, the
   * doneq and in-flight values might be accessed from the interrupt level.
   * Not the best design.  But we will use interrupt controls to protect
   * against that possibility.
   */

  flags = irqsave();

  /* Add the completed buffer to the end of our doneq.  We do not yet
   * decrement the reference count.
   */

  dq_addlast((FAR dq_entry_t *)apb, &priv->doneq);

  /* And decrement the number of buffers in-flight */

  DEBUGASSERT(priv->inflight > 0);
  priv->inflight--;

  /* Save the result of the transfer */
  /* REVISIT:  This can be overwritten */

  priv->result = result;
  irqrestore(flags);

  /* Now send a message to the worker thread, informing it that there are
   * buffers in the done queue that need to be cleaned up.
   */

  msg.msgId = AUDIO_MSG_COMPLETE;
  ret = mq_send(priv->mq, &msg, sizeof(msg), CONFIG_WM8904_MSG_PRIO);
  if (ret < 0)
    {
      audlldbg("ERROR: mq_send failed: %d\n", errno);
    }
}

/****************************************************************************
 * Name: wm8904_returnbuffers
 *
 * Description:
 *   This function is called after the complete of one or more data
 *   transfers.  This function will empty the done queue and release our
 *   reference to each buffer.
 *
 ****************************************************************************/

static void wm8904_returnbuffers(FAR struct wm8904_dev_s *priv)
{
  FAR struct ap_buffer_s *apb;
  irqstate_t flags;

  /* The doneq and in-flight values might be accessed from the interrupt
   * level in some implementations.  Not the best design.  But we will
   * use interrupt controls to protect against that possibility.
   */

  flags = irqsave();
  while (dq_peek(&priv->doneq) != NULL)
    {
      /* Take the next buffer from the queue of completed transfers */

      apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->doneq);
      irqrestore(flags);

      audvdbg("Returning: apb=%p curbyte=%d nbytes=%d flags=%04x\n",
              apb, apb->curbyte, apb->nbytes, apb->flags);

      /* Are we returning the final buffer in the stream? */

      if ((apb->flags & AUDIO_APB_FINAL) != 0)
        {
          /* Both the pending and the done queues should be empty and there
           * should be no buffers in-flight.
           */

          DEBUGASSERT(dq_empty(&priv->doneq) && dq_empty(&priv->pendq) &&
                      priv->inflight == 0);

          /* Set the terminating flag.  This will, eventually, cause the
           * worker thread to exit (if it is not already terminating).
           */

          audvdbg("Terminating\n");
          priv->terminating = true;
        }

      /* Release our reference to the audio buffer */

      apb_free(apb);

      /* Send the buffer back up to the previous level. */

#ifdef CONFIG_AUDIO_MULTI_SESSION
      priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK, NULL);
#else
      priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK);
#endif
      flags = irqsave();
    }

  irqrestore(flags);
}

/****************************************************************************
 * Name: wm8904_sendbuffer
 *
 * Description:
 *   Start the transfer an audio buffer to the WM8904 via I2S.  This
 *   will not wait for the transfer to complete but will return immediately.
 *   the wmd8904_senddone called will be invoked when the transfer
 *   completes, stimulating the worker thread to call this function again.
 *
 ****************************************************************************/

static int wm8904_sendbuffer(FAR struct wm8904_dev_s *priv)
{
  FAR struct ap_buffer_s *apb;
  irqstate_t flags;
  uint32_t timeout;
  int shift;
  int ret = OK;

  /* Loop while there are audio buffers to be sent and we have few than
   * CONFIG_WM8904_INFLIGHT then "in-flight"
   *
   * The 'inflight' value might be modified from the interrupt level in some
   * implementations.  We will use interrupt controls to protect against
   * that possibility.
   *
   * The 'pendq', on the other hand, is protected via a semaphore.  Let's
   * hold the semaphore while we are busy here and disable the interrupts
   * only while accessing 'inflight'.
   */

  wm8904_takesem(&priv->pendsem);
  while (priv->inflight < CONFIG_WM8904_INFLIGHT &&
         dq_peek(&priv->pendq) != NULL && !priv->paused)
    {
      /* Take next buffer from the queue of pending transfers */

      apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq);
      audvdbg("Sending apb=%p, size=%d inflight=%d\n",
              apb, apb->nbytes, priv->inflight);

      /* Increment the number of buffers in-flight before sending in order
       * to avoid a possible race condition.
       */

      flags = irqsave();
      priv->inflight++;
      irqrestore(flags);

      /* Send the entire audio buffer via I2S.  What is a reasonable timeout
       * to use?  This would depend on the bit rate and size of the buffer.
       *
       * Samples in the buffer (samples):
       *   = buffer_size * 8 / bpsamp                           samples
       * Sample rate (samples/second):
       *   = samplerate * nchannels
       * Expected transfer time (seconds):
       *   = (buffer_size * 8) / bpsamp / samplerate / nchannels
       *
       * We will set the timeout about twice that.
       *
       * NOTES:
       * - The multiplier of 8 becomes 16000 for 2x and units of
       *   milliseconds.
       * - 16000 is a approximately 16384 (1 << 14), bpsamp is either
       *   (1 << 3) or (1 << 4), and nchannels is either (1 << 0) or
       *   (1 << 1).  So this can be simplifies to (milliseconds):
       *
       *   = (buffer_size << shift) / samplerate
       */

      shift  = (priv->bpsamp == 8) ? 14 - 3 : 14 - 4;
      shift -= (priv->nchannels > 1) ? 1 : 0;

      timeout = MSEC2TICK(((uint32_t)(apb->nbytes - apb->curbyte) << shift) /
                           (uint32_t)priv->samprate);

      ret = I2S_SEND(priv->i2s, apb, wm8904_senddone, priv, timeout);
      if (ret < 0)
        {
          auddbg("ERROR: I2S_SEND failed: %d\n", ret);
          break;
        }
    }

  wm8904_givesem(&priv->pendsem);
  return ret;
}

/****************************************************************************
 * Name: wm8904_start
 *
 * Description:
 *   Start the configured operation (audio streaming, volume enabled, etc.).
 *
 ****************************************************************************/

#ifdef CONFIG_AUDIO_MULTI_SESSION
static int wm8904_start(FAR struct audio_lowerhalf_s *dev, FAR void *session)
#else
static int wm8904_start(FAR struct audio_lowerhalf_s *dev)
#endif
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  struct sched_param sparam;
  struct mq_attr attr;
  pthread_attr_t tattr;
  FAR void *value;
  int ret;

  audvdbg("Entry\n");

  /* Exit reduced power modes of operation */
  /* REVISIT */

  /* Create a message queue for the worker thread */

  snprintf(priv->mqname, sizeof(priv->mqname), "/tmp/%X", priv);

  attr.mq_maxmsg  = 16;
  attr.mq_msgsize = sizeof(struct audio_msg_s);
  attr.mq_curmsgs = 0;
  attr.mq_flags   = 0;

  priv->mq = mq_open(priv->mqname, O_RDWR | O_CREAT, 0644, &attr);
  if (priv->mq == NULL)
    {
      /* Error creating message queue! */

      auddbg("ERROR: Couldn't allocate message queue\n");
      return -ENOMEM;
    }

  /* Join any old worker thread we had created to prevent a memory leak */

  if (priv->threadid != 0)
    {
      audvdbg("Joining old thread\n");
      pthread_join(priv->threadid, &value);
    }

  /* Start our thread for sending data to the device */

  pthread_attr_init(&tattr);
  sparam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 3;
  (void)pthread_attr_setschedparam(&tattr, &sparam);
  (void)pthread_attr_setstacksize(&tattr, CONFIG_WM8904_WORKER_STACKSIZE);

  audvdbg("Starting worker thread\n");
  ret = pthread_create(&priv->threadid, &tattr, wm8904_workerthread,
                       (pthread_addr_t)priv);
  if (ret != OK)
    {
      auddbg("ERROR: pthread_create failed: %d\n", ret);
    }
  else
    {
      pthread_setname_np(priv->threadid, "wm8904");
      audvdbg("Created worker thread\n");
    }

  return ret;
}

/****************************************************************************
 * Name: wm8904_stop
 *
 * Description: Stop the configured operation (audio streaming, volume
 *              disabled, etc.).
 *
 ****************************************************************************/

#ifndef CONFIG_AUDIO_EXCLUDE_STOP
#ifdef CONFIG_AUDIO_MULTI_SESSION
static int wm8904_stop(FAR struct audio_lowerhalf_s *dev, FAR void* session)
#else
static int wm8904_stop(FAR struct audio_lowerhalf_s *dev)
#endif
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  struct audio_msg_s term_msg;
  FAR void *value;

  /* Send a message to stop all audio streaming */

  term_msg.msgId = AUDIO_MSG_STOP;
  term_msg.u.data = 0;
  mq_send(priv->mq, &term_msg, sizeof(term_msg), CONFIG_WM8904_MSG_PRIO);

  /* Join the worker thread */

  pthread_join(priv->threadid, &value);
  priv->threadid = 0;

  /* Enter into a reduced power usage mode */
  /* REVISIT: */

  return OK;
}
#endif

/****************************************************************************
 * Name: wm8904_pause
 *
 * Description: Pauses the playback.
 *
 ****************************************************************************/

#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
#ifdef CONFIG_AUDIO_MULTI_SESSION
static int wm8904_pause(FAR struct audio_lowerhalf_s *dev, FAR void* session)
#else
static int wm8904_pause(FAR struct audio_lowerhalf_s *dev)
#endif
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;

  if (priv->running && !priv->paused)
    {
      /* Disable interrupts to prevent us from suppling any more data */

      priv->paused = true;
      wm8904_setvolume(priv, priv->volume, true);
      WM8904_DISABLE(priv->lower);
    }

  return OK;
}
#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */

/****************************************************************************
 * Name: wm8904_resume
 *
 * Description: Resumes the playback.
 *
 ****************************************************************************/

#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
#ifdef CONFIG_AUDIO_MULTI_SESSION
static int wm8904_resume(FAR struct audio_lowerhalf_s *dev, FAR void* session)
#else
static int wm8904_resume(FAR struct audio_lowerhalf_s *dev)
#endif
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;

  if (priv->running && priv->paused)
    {
      priv->paused = false;
      wm8904_setvolume(priv, priv->volume, false);

      /* Enable interrupts to allow sampling data */

      wm8904_sendbuffer(priv);
#ifdef WM8904_USE_FFLOCK_INT
      WM8904_ENABLE(priv->lower);
#endif
    }

  return OK;
}
#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */

/****************************************************************************
 * Name: wm8904_enqueuebuffer
 *
 * Description: Enqueue an Audio Pipeline Buffer for playback/ processing.
 *
 ****************************************************************************/

static int wm8904_enqueuebuffer(FAR struct audio_lowerhalf_s *dev,
                                FAR struct ap_buffer_s *apb)
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  struct audio_msg_s  term_msg;
  int ret;

  audvdbg("Enqueueing: apb=%p curbyte=%d nbytes=%d flags=%04x\n",
          apb, apb->curbyte, apb->nbytes, apb->flags);

  /* Take a reference on the new audio buffer */

  apb_reference(apb);

  /* Add the new buffer to the tail of pending audio buffers */

  wm8904_takesem(&priv->pendsem);
  apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED;
  dq_addlast(&apb->dq_entry, &priv->pendq);
  wm8904_givesem(&priv->pendsem);

  /* Send a message to the worker thread indicating that a new buffer has been
   * enqueued.  If mq is NULL, then the playing has not yet started.  In that
   * case we are just "priming the pump" and we don't need to send any message.
   */

  ret = OK;
  if (priv->mq != NULL)
    {
      term_msg.msgId  = AUDIO_MSG_ENQUEUE;
      term_msg.u.data = 0;

      ret = mq_send(priv->mq, &term_msg, sizeof(term_msg), CONFIG_WM8904_MSG_PRIO);
      if (ret < 0)
        {
          int errcode = errno;
          DEBUGASSERT(errcode > 0);

          auddbg("ERROR: mq_send failed: %d\n", errcode);
          UNUSED(errcode);
        }
    }

  return ret;
}

/****************************************************************************
 * Name: wm8904_cancelbuffer
 *
 * Description: Called when an enqueued buffer is being cancelled.
 *
 ****************************************************************************/

static int wm8904_cancelbuffer(FAR struct audio_lowerhalf_s *dev,
                               FAR struct ap_buffer_s *apb)
{
  audvdbg("apb=%p\n", apb);
  return OK;
}

/****************************************************************************
 * Name: wm8904_ioctl
 *
 * Description: Perform a device ioctl
 *
 ****************************************************************************/

static int wm8904_ioctl(FAR struct audio_lowerhalf_s *dev, int cmd,
                        unsigned long arg)
{
#ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS
  FAR struct ap_buffer_info_s *bufinfo;
#endif

  /* Deal with ioctls passed from the upper-half driver */

  switch (cmd)
    {
      /* Check for AUDIOIOC_HWRESET ioctl.  This ioctl is passed straight
       * through from the upper-half audio driver.
       */

      case AUDIOIOC_HWRESET:
        {
          /* REVISIT:  Should we completely re-initialize the chip?   We
           * can't just issue a software reset; that would puts all WM8904
           * registers back in their default state.
           */

          audvdbg("AUDIOIOC_HWRESET:\n");
        }
        break;

       /* Report our preferred buffer size and quantity */

#ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS
      case AUDIOIOC_GETBUFFERINFO:
        {
          audvdbg("AUDIOIOC_GETBUFFERINFO:\n");
          bufinfo              = (FAR struct ap_buffer_info_s *) arg;
          bufinfo->buffer_size = CONFIG_WM8904_BUFFER_SIZE;
          bufinfo->nbuffers    = CONFIG_WM8904_NUM_BUFFERS;
        }
        break;
#endif

      default:
        audvdbg("Ignored\n");
        break;
    }

  return OK;
}

/****************************************************************************
 * Name: wm8904_reserve
 *
 * Description: Reserves a session (the only one we have).
 *
 ****************************************************************************/

#ifdef CONFIG_AUDIO_MULTI_SESSION
static int wm8904_reserve(FAR struct audio_lowerhalf_s *dev,
                          FAR void **session)
#else
static int wm8904_reserve(FAR struct audio_lowerhalf_s *dev)
#endif
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *) dev;
  int   ret = OK;

  /* Borrow the APBQ semaphore for thread sync */

  wm8904_takesem(&priv->pendsem);
  if (priv->reserved)
    {
      ret = -EBUSY;
    }
  else
    {
      /* Initialize the session context */

#ifdef CONFIG_AUDIO_MULTI_SESSION
     *session           = NULL;
#endif
      priv->inflight    = 0;
      priv->running     = false;
      priv->paused      = false;
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
      priv->terminating = false;
#endif
      priv->reserved    = true;
    }

  wm8904_givesem(&priv->pendsem);

  return ret;
}

/****************************************************************************
 * Name: wm8904_release
 *
 * Description: Releases the session (the only one we have).
 *
 ****************************************************************************/

#ifdef CONFIG_AUDIO_MULTI_SESSION
static int wm8904_release(FAR struct audio_lowerhalf_s *dev,
                          FAR void *session)
#else
static int wm8904_release(FAR struct audio_lowerhalf_s *dev)
#endif
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  void  *value;

  /* Join any old worker thread we had created to prevent a memory leak */

  if (priv->threadid != 0)
    {
      pthread_join(priv->threadid, &value);
      priv->threadid = 0;
    }

  /* Borrow the APBQ semaphore for thread sync */

  wm8904_takesem(&priv->pendsem);

  /* Really we should free any queued buffers here */

  priv->reserved = false;
  wm8904_givesem(&priv->pendsem);

  return OK;
}

/****************************************************************************
 * Name: wm8904_interrupt_work
 *
 * Description:
 *   WM8904 interrupt actions cannot be performed in the interrupt handler
 *   because I2C access is not possible in that context.  Instead, all I2C
 *   operations are deferred to the work queue.
 *
 * Assumptions:
 *   WM8904 interrupts were disabled in the interrupt handler.
 *
 ****************************************************************************/

#ifdef WM8904_USE_FFLOCK_INT
static void wm8904_interrupt_work(FAR void *arg)
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)arg;
  uint16_t regval;

  DEBUGASSERT(priv && priv->lower);

  /* Sample the interrupt status */

  regval = wm8904_readreg(priv, WM8904_INT_STATUS);
  audvdbg("INT_STATUS: %04x\n", regval);

  /* Check for the FLL lock interrupt.  We are sloppy here since at
   * present, only the FLL lock interrupt is used.
   */

  DEBUGASSERT((regval & WM8904_FLL_LOCK_INT) != 0 && !priv->locked);
  UNUSED(regval);

  priv->locked = true;

  /* Clear all pending interrupts by write 1's to the interrupt status
   * register.
   *
   * REVISIT: Since I2C is slow and not atomic with respect to WM8904 event,
   * could this not cause the lost of interrupts?
   */

  wm8904_writereg(priv, WM8904_INT_STATUS, WM8904_ALL_INTS);

  /* Disable further FLL lock interrupts.  We are sloppy here since at
   * present, only the FLL lock interrupt is used.
   */

  wm8904_writereg(priv, WM8904_INT_MASK, WM8904_ALL_INTS);

#ifdef WM8904_USE_FFLOCK_INT
  /* Re-enable WM8904 interrupts */

  WM8904_ENABLE(priv->lower);
#endif
}
#endif

/****************************************************************************
 * Name: wm8904_interrupt
 *
 * Description:
 *   This is the ISR that services the GPIO1/IRQ pin from the WM8904.  It
 *   signals WM8904 events such FLL lock.
 *
 ****************************************************************************/

#ifdef WM8904_USE_FFLOCK_INT
static int wm8904_interrupt(FAR const struct wm8904_lower_s *lower,
                            FAR void *arg)
{
  FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)arg;
  int ret;

  DEBUGASSERT(lower && priv);

  /* Disable further interrupts and perform all interrupt related activities
   * on the work thread.  There is nothing that we can do from the interrupt
   * handler because we cannot perform I2C operations here.
   */

  WM8904_DISABLE(priv->lower);

  DEBUGASSERT(work_available(&priv->work));
  ret = work_queue(LPWORK, &priv->work, wm8904_interrupt_work, priv, 0);
  if (ret < 0)
    {
      audlldbg("ERROR: Failed to schedule work\n");
    }

  return OK;
}
#endif

/****************************************************************************
 * Name: wm8904_workerthread
 *
 *  This is the thread that feeds data to the chip and keeps the audio
 *  stream going.
 *
 ****************************************************************************/

static void *wm8904_workerthread(pthread_addr_t pvarg)
{
  FAR struct wm8904_dev_s *priv = (struct wm8904_dev_s *) pvarg;
  struct audio_msg_s msg;
  FAR struct ap_buffer_s *apb;
  int msglen;
  int prio;

  audvdbg("Entry\n");

#ifndef CONFIG_AUDIO_EXCLUDE_STOP
  priv->terminating = false;
#endif

  /* Mark ourself as running and make sure that WM8904 interrupts are
   * enabled.
   */

  priv->running = true;
#ifdef WM8904_USE_FFLOCK_INT
  WM8904_ENABLE(priv->lower);
#endif
  wm8904_setvolume(priv, priv->volume, false);

  /* Loop as long as we are supposed to be running and as long as we have
   * buffers in-flight.
   */

  while (priv->running || priv->inflight > 0)
    {
      /* Check if we have been asked to terminate.  We have to check if we
       * still have buffers in-flight.  If we do, then we can't stop until
       * birds come back to roost.
       */

      if (priv->terminating && priv->inflight <= 0)
        {
          /* We are IDLE.  Break out of the loop and exit. */

          break;
        }
      else
        {
          /* Check if we can send more audio buffers to the WM8904 */

          wm8904_sendbuffer(priv);
        }

      /* Wait for messages from our message queue */

      msglen = mq_receive(priv->mq, &msg, sizeof(msg), &prio);

      /* Handle the case when we return with no message */

      if (msglen < sizeof(struct audio_msg_s))
        {
          auddbg("ERROR: Message too small: %d\n", msglen);
          continue;
        }

      /* Process the message */

      switch (msg.msgId)
        {
          /* The ISR has requested more data.  We will catch this case at
           * the top of the loop.
           */

          case AUDIO_MSG_DATA_REQUEST:
            audvdbg("AUDIO_MSG_DATA_REQUEST\n");
            break;

          /* Stop the playback */

#ifndef CONFIG_AUDIO_EXCLUDE_STOP
          case AUDIO_MSG_STOP:
            /* Indicate that we are terminating */

            audvdbg("AUDIO_MSG_STOP: Terminating\n");
            priv->terminating = true;
            break;
#endif

          /* We have a new buffer to send.  We will catch this case at
           * the top of the loop.
           */

          case AUDIO_MSG_ENQUEUE:
            audvdbg("AUDIO_MSG_ENQUEUE\n");
            break;

          /* We will wake up from the I2S callback with this message */

          case AUDIO_MSG_COMPLETE:
            audvdbg("AUDIO_MSG_COMPLETE\n");
            wm8904_returnbuffers(priv);
            break;

          default:
            auddbg("ERROR: Ignoring message ID %d\n", msg.msgId);
            break;
        }
    }

  /* Reset the WM8904 hardware */

  wm8904_hw_reset(priv);

  /* Return any pending buffers in our pending queue */

  wm8904_takesem(&priv->pendsem);
  while ((apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq)) != NULL)
    {
      /* Release our reference to the buffer */

      apb_free(apb);

      /* Send the buffer back up to the previous level. */

#ifdef CONFIG_AUDIO_MULTI_SESSION
      priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK, NULL);
#else
      priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK);
#endif
    }

  wm8904_givesem(&priv->pendsem);

  /* Return any pending buffers in our done queue */

  wm8904_returnbuffers(priv);

  /* Close the message queue */

  mq_close(priv->mq);
  mq_unlink(priv->mqname);
  priv->mq = NULL;

  /* Send an AUDIO_MSG_COMPLETE message to the client */

#ifdef CONFIG_AUDIO_MULTI_SESSION
  priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_COMPLETE, NULL, OK, NULL);
#else
  priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_COMPLETE, NULL, OK);
#endif

  audvdbg("Exit\n");
  return NULL;
}

/****************************************************************************
 * Name: wm8904_audio_output
 *
 * Description:
 *   Initialize and configure the WM8904 device as an audio output device.
 *
 * Input Parameters:
 *   priv - A reference to the driver state structure
 *
 * Returned Value:
 *   None.  No failures are detected.
 *
 ****************************************************************************/

static void wm8904_audio_output(FAR struct wm8904_dev_s *priv)
{
  uint16_t regval;

  /* Bias Control.
   * Preserve undocumented default bit.WM8904_DUMMY
   */

  regval = WM8904_ISEL_HIGH | WM8904_BIAS_ENA | 0x0010;
  wm8904_writereg(priv, WM8904_BIAS_CTRL, regval);

  /* VMID Control */

  regval = WM8904_VMID_BUF_ENA | WM8904_VMID_RES_NORMAL | WM8904_VMID_ENA;
  wm8904_writereg(priv, WM8904_VMID_CTRL, regval);

  /* Mic Bias Control 0 */
  /* MICDET_ENA=1, MICBIAS_ENA=1   */

  regval = WM8904_MICDET_ENA | WM8904_MICBIAS_ENA;
  wm8904_writereg(priv, WM8904_MIC_BIAS_CTRL0, regval);

  /* Mic Bias Control 1. */

  wm8904_writereg(priv, WM8904_MIC_BIAS_CTRL1, 0xc000);

  /* Power Management 0 */

  regval = WM8904_INL_ENA | WM8904_INR_ENA;
  wm8904_writereg(priv, WM8904_PM0, regval);

  /* Power Management 2 */

  regval = WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA;
  wm8904_writereg(priv, WM8904_PM2, regval);

  /* Power Management 6 */
  /* DACL_ENA=1, DACR_ENA=1, ADCL_ENA=1, ADCR_ENA=1  */

  regval = WM8904_DACL_ENA | WM8904_DACR_ENA | WM8904_ADCL_ENA | WM8904_ADCR_ENA;
  wm8904_writereg(priv, WM8904_PM6, regval);

  /* Clock Rates 0.
   *
   * This value sets TOCLK_RATE_DIV16=0, TOCLK_RATE_X4=0, and MCLK_DIV=0 while
   * preserving the state of some undocumented bits (see wm8904.h).
   *
   *   MCLK_DIV=0           : MCLK is is not divided by 2.
   */

  wm8904_writereg(priv, WM8904_CLKRATE0, 0x845e);

  /* Clock Rates 1.
   *
   * Contains settings the control the sample rate.
   */

  /* Clock Rates 2
   *
   * Contains various controls.  Some that are controlled here include:
   *
   *   WM8904_MCLK_INV=0    : MCLK is not inverted
   *   WM8904_SYSCLK_SRC=1  : SYSCLK source is FLL
   *   WM8904_TOCLK_RATE=0  :
   *   WM8904_OPCLK_ENA=0   :
   *   WM8904_CLK_SYS_ENA=1 : SYSCLK is enabled
   *   WM8904_CLK_DSP_ENA=1 : DSP clock is enabled
   *   WM8904_TOCLK_ENA=0   :
   */

  regval = WM8904_SYSCLK_SRCFLL | WM8904_CLK_SYS_ENA | WM8904_CLK_DSP_ENA;
  wm8904_writereg(priv, WM8904_CLKRATE2, regval);

  /* Audio Interface 0.
   *
   * Reset value is:
   *   No DAC invert
   *   No volume boost
   *   No loopback
   *   Left/Right ADC/DAC channels output on Left/Right
   *   Companding options set by wm8904_setdatawidth()
   */

  wm8904_setdatawidth(priv);

  /* Audio Interface 1.
   *
   * This value sets AIFADC_TDM=0, AIFADC_TDM_CHAN=0, BCLK_DIR=1 while preserving
   * the state of some undocumented bits (see wm8904.h).
   *
   *   Digital audio interface format      : I2S
   *   Digital audio interface word length : 24
   *   AIF_LRCLK_INV=0                     : LRCLK not inverted
   *   BCLK_DIR=1                          : BCLK is an output (will clock I2S).
   *   AIF_BCLK_INV=0                      : BCLK not inverted
   *   AIF_TRIS=0                          : Outputs not tri-stated
   *   AIFADC_TDM_CHAN=0                   : ADCDAT outputs data on slot 0
   *   AIFADC_TDM=0                        : Normal ADCDAT operation
   *   AIFDAC_TDM_CHAN=0                   : DACDAT data input on slot 0
   *   AIFDAC_TDM=0                        : Normal DACDAT operation
   *   Bit 14:                             : Undocumented
   */

  regval = WM8904_AIF_FMT_I2S | WM8904_AIF_WL_24BITS | WM8904_BCLK_DIR | 0x4000;
  wm8904_writereg(priv, WM8904_AIF1, regval);

  /* Audio Interface 2.
   *
   * Holds GPIO clock divider and the SYSCLK divider needed to generate BCLK.
   * This will get initialized by wm8904_setbitrate().
   */

  /* Audio Interface 3
   *
   * Set LRCLK as an output with rate = BCLK / (2*WM8904_FRAMELENn).  This is
   * a value that varies with bits per sample, n=8 or 16.  Since I2S will send
   * a word on each edge of LRCLK (after a delay), this essentially means that
   * each audio frame is WM8904_FRAMELENn bits in length.
   */

  regval = WM8904_LRCLK_DIR | WM8904_LRCLK_RATE(2*WM8904_FRAMELEN16);
  wm8904_writereg(priv, WM8904_AIF3, regval);

  /* DAC Digital 1 */

  wm8904_writereg(priv, WM8904_DAC_DIGI1, 0);

  /* Analogue Left Input 0 */
  /* Analogue Right Input 0 */

  regval =  WM8904_IN_VOL(5);
  wm8904_writereg(priv, WM8904_ANA_LEFT_IN0, regval);
  wm8904_writereg(priv, WM8904_ANA_RIGHT_IN0, regval);

  /* Analogue Left Input 1 */

  wm8904_writereg(priv, WM8904_ANA_LEFT_IN1, 0);
  wm8904_writereg(priv, WM8904_ANA_RIGHT_IN1, 0);

  /* Analogue OUT1 Left */
  /* Analogue OUT1 Right */

  wm8904_setvolume(priv, CONFIG_WM8904_INITVOLUME, true);

  /* DC Servo 0 */

  regval = WM8904_DCS_ENA_CHAN_1 | WM8904_DCS_ENA_CHAN_0;
  wm8904_writereg(priv, WM8904_DC_SERVO0, regval);

  /* Analogue HP 0 */

  regval = WM8904_HPL_RMV_SHORT | WM8904_HPL_ENA_OUTP | WM8904_HPL_ENA_DLY | WM8904_HPL_ENA |
           WM8904_HPR_RMV_SHORT | WM8904_HPR_ENA_OUTP | WM8904_HPR_ENA_DLY | WM8904_HPR_ENA;
  wm8904_writereg(priv, WM8904_ANA_HP0, regval);

  /* Charge Pump 0 */

  wm8904_writereg(priv, WM8904_CHG_PUMP0, WM8904_CP_ENA);

  /* Class W 0 */

  regval = WM8904_CP_DYN_PWR | 0x0004;
  wm8904_writereg(priv, WM8904_CLASS_W0, regval);
}

/****************************************************************************
 * Name: wm8904_audio_input
 *
 * Description:
 *   Initialize and configure the WM8904 device as an audio output device
 *   (Right input only).  wm8904_audio_output() must be called first, this
 *   function then modifies the configuration to support audio input.
 *
 * Input Parameters:
 *   priv - A reference to the driver state structure
 *
 * Returned Value:
 *   None.  No failures are detected.
 *
 ****************************************************************************/

#if 0 /* Not used */
static void wm8904_audio_input(FAR struct wm8904_dev_s *priv)
{
  /* Analogue Left Input 0  */

  wm8904_writereg(priv, WM8904_ANA_LEFT_IN0, WM8904_INMUTE);

  /* Analogue Right Input 0 */

  wm8904_writereg(priv, WM8904_ANA_RIGHT_IN0, WM8904_IN_VOL(5));

  /* Analogue Left Input 1 */

  wm8904_writereg(priv, WM8904_ANA_LEFT_IN1, 0);

  /* Analogue Right Input 1 */

  wm8904_writereg(priv, WM8904_ANA_RIGHT_IN1, WM8904_IP_SEL_N_IN2L);
}
#endif

/****************************************************************************
 * Name: wm8904_configure_ints
 *
 * Description:
 *   Configure the GPIO/IRQ interrupt
 *
 * Input Parameters:
 *   priv - A reference to the driver state structure
 *
 * Returned Value:
 *   None
 *
 ****************************************************************************/

#ifdef WM8904_USE_FFLOCK_INT
static void  wm8904_configure_ints(FAR struct wm8904_dev_s *priv)
{
  uint16_t regval;

  /* Configure GPIO1 as an IRQ
   *
   *   WM8904_GPIO1_PU=0               : No pull-up
   *   WM8904_GPIO1_PD=1               : Pulled-down
   *   WM8904_GPIO1_SEL_IRQ            : Configured as IRQ
   */

  regval = (WM8904_GPIO1_SEL_IRQ | WM8904_GPIO1_PD);
  wm8904_writereg(priv, WM8904_GPIO_CTRL1, regval);

  /* Attach our handler to the GPIO1/IRQ interrupt */

  WM8904_ATTACH(lower, wm8904_interrupt, priv);

  /* Configure interrupts.  wm8904_setbitrate() depends on FLL interrupts. */

  wm8904_writereg(priv, WM8904_INT_STATUS, WM8904_ALL_INTS);
  wm8904_writereg(priv, WM8904_INT_MASK, WM8904_ALL_INTS);
  wm8904_writereg(priv, WM8904_INT_POL, 0);
  wm8904_writereg(priv, WM8904_INT_DEBOUNCE, WM8904_ALL_INTS);
}
#endif

/****************************************************************************
 * Name: wm8904_hw_reset
 *
 * Description:
 *   Reset and re-initialize the WM8904
 *
 * Input Parameters:
 *   priv - A reference to the driver state structure
 *
 * Returned Value:
 *   None
 *
 ****************************************************************************/

static void wm8904_hw_reset(FAR struct wm8904_dev_s *priv)
{
  /* Put audio output back to its initial configuration */

  priv->samprate   = WM8904_DEFAULT_SAMPRATE;
  priv->nchannels  = WM8904_DEFAULT_NCHANNELS;
  priv->bpsamp     = WM8904_DEFAULT_BPSAMP;
#if !defined(CONFIG_AUDIO_EXCLUDE_VOLUME) && !defined(CONFIG_AUDIO_EXCLUDE_BALANCE)
  priv->balance    = b16HALF;            /* Center balance */
#endif

  /* Software reset.  This puts all WM8904 registers back in their
   * default state.
   */

  wm8904_writereg(priv, WM8904_SWRST, 0);

  /* Configure the WM8904 hardware as an audio input device */

  wm8904_audio_output(priv);

  /* Configure interrupts */

  wm8904_configure_ints(priv);

  /* Configure the FLL and the LRCLK */

  wm8904_setbitrate(priv);
  wm8904_writereg(priv, WM8904_DUMMY, 0x55aa);

  /* Dump some information and return the device instance */

  wm8904_dump_registers(&priv->dev, "After configuration");
  wm8904_clock_analysis(&priv->dev, "After configuration");
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: wm8904_initialize
 *
 * Description:
 *   Initialize the WM8904 device.
 *
 * Input Parameters:
 *   i2c     - An I2C driver instance
 *   i2s     - An I2S driver instance
 *   lower   - Persistent board configuration data
 *
 * Returned Value:
 *   A new lower half audio interface for the WM8904 device is returned on
 *   success; NULL is returned on failure.
 *
 ****************************************************************************/

FAR struct audio_lowerhalf_s *
  wm8904_initialize(FAR struct i2c_dev_s *i2c, FAR struct i2s_dev_s *i2s,
                    FAR const struct wm8904_lower_s *lower)
{
  FAR struct wm8904_dev_s *priv;
  uint16_t regval;

  /* Sanity check */

  DEBUGASSERT(i2c && i2s && lower);

  /* Allocate a WM8904 device structure */

  priv = (FAR struct wm8904_dev_s *)kmm_zalloc(sizeof(struct wm8904_dev_s));
  if (priv)
    {
      /* Initialize the WM8904 device structure.  Since we used kmm_zalloc,
       * only the non-zero elements of the structure need to be initialized.
       */

      priv->dev.ops    = &g_audioops;
      priv->lower      = lower;
      priv->i2c        = i2c;
      priv->i2s        = i2s;

      sem_init(&priv->pendsem, 0, 1);
      dq_init(&priv->pendq);
      dq_init(&priv->doneq);

      /* Initialize I2C */

      auddbg("address=%02x frequency=%d\n", lower->address, lower->frequency);
      I2C_SETFREQUENCY(i2c, lower->frequency);
      I2C_SETADDRESS(i2c, lower->address, 7);

      /* Software reset.  This puts all WM8904 registers back in their
       * default state.
       */

      wm8904_writereg(priv, WM8904_SWRST, 0);
      wm8904_dump_registers(&priv->dev, "After reset");

      /* Verify that WM8904 is present and available on this I2C */

      regval = wm8904_readreg(priv, WM8904_ID);
      if (regval != WM8904_SW_RST_DEV_ID1)
        {
          auddbg("ERROR: WM8904 not found: ID=%04x\n", regval);
          goto errout_with_dev;
        }

      /* Reset and reconfigure the WM8904 hardwaqre */

      wm8904_hw_reset(priv);
      return &priv->dev;
    }

  return NULL;

errout_with_dev:
  sem_destroy(&priv->pendsem);
  kmm_free(priv);
  return NULL;
}