wm8904.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539
  1. /****************************************************************************
  2. * drivers/audio/wm8904.c
  3. *
  4. * Audio device driver for Wolfson Microelectronics WM8904 Audio codec.
  5. *
  6. * Copyright (C) 2014, 2016-2018 Gregory Nutt. All rights reserved.
  7. * Author: Gregory Nutt <gnutt@nuttx.org>
  8. *
  9. * References:
  10. * - "WM8904 Ultra Low Power CODEC for Portable Audio Applications, Pre-
  11. * Production", September 2012, Rev 3.3, Wolfson Microelectronics
  12. *
  13. * - The framework for this driver is based on Ken Pettit's VS1053 driver.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in
  23. * the documentation and/or other materials provided with the
  24. * distribution.
  25. * 3. Neither the name NuttX nor the names of its contributors may be
  26. * used to endorse or promote products derived from this software
  27. * without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  32. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  33. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  34. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  35. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  36. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  37. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  39. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. ****************************************************************************/
  43. /****************************************************************************
  44. * Included Files
  45. ****************************************************************************/
  46. #include <nuttx/config.h>
  47. #include <sys/types.h>
  48. #include <sys/ioctl.h>
  49. #include <stdint.h>
  50. #include <stdio.h>
  51. #include <fcntl.h>
  52. #include <string.h>
  53. #include <errno.h>
  54. #include <fixedmath.h>
  55. #include <queue.h>
  56. #include <debug.h>
  57. #include <nuttx/irq.h>
  58. #include <nuttx/kmalloc.h>
  59. #include <nuttx/clock.h>
  60. #include <nuttx/wqueue.h>
  61. #include <nuttx/signal.h>
  62. #include <nuttx/mqueue.h>
  63. #include <nuttx/i2c/i2c_master.h>
  64. #include <nuttx/fs/fs.h>
  65. #include <nuttx/fs/ioctl.h>
  66. #include <nuttx/audio/i2s.h>
  67. #include <nuttx/audio/audio.h>
  68. #include <nuttx/audio/wm8904.h>
  69. #include <nuttx/lib/math.h>
  70. #include "wm8904.h"
  71. /****************************************************************************
  72. * Pre-processor Definitions
  73. ****************************************************************************/
  74. /* Maximum number of retries */
  75. #define MAX_RETRIES 3
  76. /****************************************************************************
  77. * Private Function Prototypes
  78. ****************************************************************************/
  79. #if !defined(CONFIG_WM8904_REGDUMP) && !defined(CONFIG_WM8904_CLKDEBUG)
  80. static
  81. #endif
  82. uint16_t wm8904_readreg(FAR struct wm8904_dev_s *priv,
  83. uint8_t regaddr);
  84. static void wm8904_writereg(FAR struct wm8904_dev_s *priv,
  85. uint8_t regaddr, uint16_t regval);
  86. static void wm8904_takesem(sem_t *sem);
  87. #define wm8904_givesem(s) nxsem_post(s)
  88. #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
  89. static inline uint16_t wm8904_scalevolume(uint16_t volume, b16_t scale);
  90. static void wm8904_setvolume(FAR struct wm8904_dev_s *priv,
  91. uint16_t volume, bool mute);
  92. #endif
  93. #ifndef CONFIG_AUDIO_EXCLUDE_TONE
  94. static void wm8904_setbass(FAR struct wm8904_dev_s *priv, uint8_t bass);
  95. static void wm8904_settreble(FAR struct wm8904_dev_s *priv, uint8_t treble);
  96. #endif
  97. static void wm8904_setdatawidth(FAR struct wm8904_dev_s *priv);
  98. static void wm8904_setbitrate(FAR struct wm8904_dev_s *priv);
  99. /* Audio lower half methods (and close friends) */
  100. static int wm8904_getcaps(FAR struct audio_lowerhalf_s *dev, int type,
  101. FAR struct audio_caps_s *caps);
  102. #ifdef CONFIG_AUDIO_MULTI_SESSION
  103. static int wm8904_configure(FAR struct audio_lowerhalf_s *dev,
  104. FAR void *session, FAR const struct audio_caps_s *caps);
  105. #else
  106. static int wm8904_configure(FAR struct audio_lowerhalf_s *dev,
  107. FAR const struct audio_caps_s *caps);
  108. #endif
  109. static int wm8904_shutdown(FAR struct audio_lowerhalf_s *dev);
  110. static void wm8904_senddone(FAR struct i2s_dev_s *i2s,
  111. FAR struct ap_buffer_s *apb, FAR void *arg, int result);
  112. static void wm8904_returnbuffers(FAR struct wm8904_dev_s *priv);
  113. static int wm8904_sendbuffer(FAR struct wm8904_dev_s *priv);
  114. #ifdef CONFIG_AUDIO_MULTI_SESSION
  115. static int wm8904_start(FAR struct audio_lowerhalf_s *dev,
  116. FAR void *session);
  117. #else
  118. static int wm8904_start(FAR struct audio_lowerhalf_s *dev);
  119. #endif
  120. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  121. #ifdef CONFIG_AUDIO_MULTI_SESSION
  122. static int wm8904_stop(FAR struct audio_lowerhalf_s *dev,
  123. FAR void *session);
  124. #else
  125. static int wm8904_stop(FAR struct audio_lowerhalf_s *dev);
  126. #endif
  127. #endif
  128. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  129. #ifdef CONFIG_AUDIO_MULTI_SESSION
  130. static int wm8904_pause(FAR struct audio_lowerhalf_s *dev,
  131. FAR void *session);
  132. static int wm8904_resume(FAR struct audio_lowerhalf_s *dev,
  133. FAR void *session);
  134. #else
  135. static int wm8904_pause(FAR struct audio_lowerhalf_s *dev);
  136. static int wm8904_resume(FAR struct audio_lowerhalf_s *dev);
  137. #endif
  138. #endif
  139. static int wm8904_enqueuebuffer(FAR struct audio_lowerhalf_s *dev,
  140. FAR struct ap_buffer_s *apb);
  141. static int wm8904_cancelbuffer(FAR struct audio_lowerhalf_s *dev,
  142. FAR struct ap_buffer_s *apb);
  143. static int wm8904_ioctl(FAR struct audio_lowerhalf_s *dev, int cmd,
  144. unsigned long arg);
  145. #ifdef CONFIG_AUDIO_MULTI_SESSION
  146. static int wm8904_reserve(FAR struct audio_lowerhalf_s *dev,
  147. FAR void **session);
  148. #else
  149. static int wm8904_reserve(FAR struct audio_lowerhalf_s *dev);
  150. #endif
  151. #ifdef CONFIG_AUDIO_MULTI_SESSION
  152. static int wm8904_release(FAR struct audio_lowerhalf_s *dev,
  153. FAR void *session);
  154. #else
  155. static int wm8904_release(FAR struct audio_lowerhalf_s *dev);
  156. #endif
  157. /* Interrupt handling an worker thread */
  158. #ifdef WM8904_USE_FFLOCK_INT
  159. static void wm8904_interrupt_work(FAR void *arg);
  160. static int wm8904_interrupt(FAR const struct wm8904_lower_s *lower,
  161. FAR void *arg);
  162. #endif
  163. static void *wm8904_workerthread(pthread_addr_t pvarg);
  164. /* Initialization */
  165. static void wm8904_audio_output(FAR struct wm8904_dev_s *priv);
  166. #if 0 /* Not used */
  167. static void wm8904_audio_input(FAR struct wm8904_dev_s *priv);
  168. #endif
  169. #ifdef WM8904_USE_FFLOCK_INT
  170. static void wm8904_configure_ints(FAR struct wm8904_dev_s *priv);
  171. #else
  172. # define wm8904_configure_ints(p)
  173. #endif
  174. static void wm8904_hw_reset(FAR struct wm8904_dev_s *priv);
  175. /****************************************************************************
  176. * Private Data
  177. ****************************************************************************/
  178. static const struct audio_ops_s g_audioops =
  179. {
  180. wm8904_getcaps, /* getcaps */
  181. wm8904_configure, /* configure */
  182. wm8904_shutdown, /* shutdown */
  183. wm8904_start, /* start */
  184. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  185. wm8904_stop, /* stop */
  186. #endif
  187. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  188. wm8904_pause, /* pause */
  189. wm8904_resume, /* resume */
  190. #endif
  191. NULL, /* allocbuffer */
  192. NULL, /* freebuffer */
  193. wm8904_enqueuebuffer, /* enqueue_buffer */
  194. wm8904_cancelbuffer, /* cancel_buffer */
  195. wm8904_ioctl, /* ioctl */
  196. NULL, /* read */
  197. NULL, /* write */
  198. wm8904_reserve, /* reserve */
  199. wm8904_release /* release */
  200. };
  201. #ifndef CONFIG_WM8904_CLKDEBUG
  202. static
  203. #endif
  204. const uint8_t g_sysclk_scaleb1[WM8904_BCLK_MAXDIV+1] =
  205. {
  206. 2, 3, 4, 6, 8, 10, 11, /* 1, 1.5, 2, 3, 4, 5, 5.5 */
  207. 12, 16, 20, 22, 24, 32, 40, /* 6, 8, 10, 11, 12, 16, 20 */
  208. 44, 48, 50, 60, 64, 88, 96 /* 22, 24, 25, 30, 32, 44, 48 */
  209. };
  210. #ifndef CONFIG_WM8904_CLKDEBUG
  211. static
  212. #endif
  213. const uint8_t g_fllratio[WM8904_NFLLRATIO] =
  214. {
  215. 1, 2, 4, 8, 16
  216. };
  217. /****************************************************************************
  218. * Private Functions
  219. ****************************************************************************/
  220. /****************************************************************************
  221. * Name: wm8904_readreg
  222. *
  223. * Description:
  224. * Read the specified 16-bit register from the WM8904 device.
  225. *
  226. ****************************************************************************/
  227. #if !defined(CONFIG_WM8904_REGDUMP) && !defined(CONFIG_WM8904_CLKDEBUG)
  228. static
  229. #endif
  230. uint16_t wm8904_readreg(FAR struct wm8904_dev_s *priv, uint8_t regaddr)
  231. {
  232. int retries;
  233. /* Try up to three times to read the register */
  234. for (retries = 1; retries <= MAX_RETRIES; retries++)
  235. {
  236. struct i2c_msg_s msg[2];
  237. uint8_t data[2];
  238. int ret;
  239. /* Set up to write the address */
  240. msg[0].frequency = priv->lower->frequency;
  241. msg[0].addr = priv->lower->address;
  242. msg[0].flags = 0;
  243. msg[0].buffer = &regaddr;
  244. msg[0].length = 1;
  245. /* Followed by the read data */
  246. msg[1].frequency = priv->lower->frequency;
  247. msg[1].addr = priv->lower->address;
  248. msg[1].flags = I2C_M_READ;
  249. msg[1].buffer = data;
  250. msg[1].length = 2;
  251. /* Read the register data. The returned value is the number messages
  252. * completed.
  253. */
  254. ret = I2C_TRANSFER(priv->i2c, msg, 2);
  255. if (ret < 0)
  256. {
  257. #ifdef CONFIG_I2C_RESET
  258. /* Perhaps the I2C bus is locked up? Try to shake the bus free.
  259. * Don't bother with the reset if this was the last attempt.
  260. */
  261. if (retries < MAX_RETRIES)
  262. {
  263. audwarn("WARNING: I2C_TRANSFER failed: %d ... Resetting\n", ret);
  264. ret = I2C_RESET(priv->i2c);
  265. if (ret < 0)
  266. {
  267. auderr("ERROR: I2C_RESET failed: %d\n", ret);
  268. break;
  269. }
  270. }
  271. #else
  272. auderr("ERROR: I2C_TRANSFER failed: %d\n", ret);
  273. #endif
  274. }
  275. else
  276. {
  277. uint16_t regval;
  278. /* The I2C transfer was successful... break out of the loop and
  279. * return the value read.
  280. */
  281. regval = ((uint16_t)data[0] << 8) | (uint16_t)data[1];
  282. audinfo("Read: %02x -> %04x\n", regaddr, regval);
  283. return regval;
  284. }
  285. audinfo("retries=%d regaddr=%02x\n", retries, regaddr);
  286. }
  287. /* No error indication is returned on a failure... just return zero */
  288. return 0;
  289. }
  290. /************************************************************************************
  291. * Name: wm8904_writereg
  292. *
  293. * Description:
  294. * Write the specified 16-bit register to the WM8904 device.
  295. *
  296. ************************************************************************************/
  297. static void wm8904_writereg(FAR struct wm8904_dev_s *priv, uint8_t regaddr,
  298. uint16_t regval)
  299. {
  300. struct i2c_config_s config;
  301. int retries;
  302. /* Setup up the I2C configuration */
  303. config.frequency = priv->lower->frequency;
  304. config.address = priv->lower->address;
  305. config.addrlen = 7;
  306. /* Try up to three times to read the register */
  307. for (retries = 1; retries <= MAX_RETRIES; retries++)
  308. {
  309. uint8_t data[3];
  310. int ret;
  311. /* Set up the data to write */
  312. data[0] = regaddr;
  313. data[1] = regval >> 8;
  314. data[2] = regval & 0xff;
  315. /* Read the register data. The returned value is the number messages
  316. * completed.
  317. */
  318. ret = i2c_write(priv->i2c, &config, data, 3);
  319. if (ret < 0)
  320. {
  321. #ifdef CONFIG_I2C_RESET
  322. /* Perhaps the I2C bus is locked up? Try to shake the bus free.
  323. * Don't bother with the reset if this was the last attempt.
  324. */
  325. if (retries < MAX_RETRIES)
  326. {
  327. audwarn("WARNING: i2c_write failed: %d ... Resetting\n", ret);
  328. ret = I2C_RESET(priv->i2c);
  329. if (ret < 0)
  330. {
  331. auderr("ERROR: I2C_RESET failed: %d\n", ret);
  332. break;
  333. }
  334. }
  335. #else
  336. auderr("ERROR: I2C_TRANSFER failed: %d\n", ret);
  337. #endif
  338. }
  339. else
  340. {
  341. /* The I2C transfer was successful... break out of the loop and
  342. * return the value read.
  343. */
  344. audinfo("Write: %02x <- %04x\n", regaddr, regval);
  345. return;
  346. }
  347. audinfo("retries=%d regaddr=%02x\n", retries, regaddr);
  348. }
  349. }
  350. /************************************************************************************
  351. * Name: wm8904_takesem
  352. *
  353. * Description:
  354. * Take a semaphore count, handling the nasty EINTR return if we are interrupted
  355. * by a signal.
  356. *
  357. ************************************************************************************/
  358. static void wm8904_takesem(sem_t *sem)
  359. {
  360. int ret;
  361. do
  362. {
  363. ret = nxsem_wait(sem);
  364. DEBUGASSERT(ret == 0 || ret == -EINTR);
  365. }
  366. while (ret == -EINTR);
  367. }
  368. /************************************************************************************
  369. * Name: wm8904_scalevolume
  370. *
  371. * Description:
  372. * Set the right and left volume values in the WM8904 device based on the current
  373. * volume and balance settings.
  374. *
  375. ************************************************************************************/
  376. #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
  377. static inline uint16_t wm8904_scalevolume(uint16_t volume, b16_t scale)
  378. {
  379. return b16toi((b16_t)volume * scale);
  380. }
  381. #endif
  382. /************************************************************************************
  383. * Name: wm8904_setvolume
  384. *
  385. * Description:
  386. * Set the right and left volume values in the WM8904 device based on the current
  387. * volume and balance settings.
  388. *
  389. ************************************************************************************/
  390. #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
  391. static void wm8904_setvolume(FAR struct wm8904_dev_s *priv, uint16_t volume,
  392. bool mute)
  393. {
  394. uint32_t leftlevel;
  395. uint32_t rightlevel;
  396. uint16_t regval;
  397. audinfo("volume=%u mute=%u\n", volume, mute);
  398. #ifndef CONFIG_AUDIO_EXCLUDE_BALANCE
  399. /* Calculate the left channel volume level {0..1000} */
  400. if (priv->balance <= 500)
  401. {
  402. leftlevel = volume;
  403. }
  404. else if (priv->balance == 1000)
  405. {
  406. leftlevel = 0;
  407. }
  408. else
  409. {
  410. leftlevel = wm8904_scalevolume(volume, b16ONE - (b16_t)priv->balance);
  411. }
  412. /* Calculate the right channel volume level {0..1000} */
  413. if (priv->balance >= 500)
  414. {
  415. rightlevel = volume;
  416. }
  417. else if (priv->balance == 0)
  418. {
  419. rightlevel = 0;
  420. }
  421. else
  422. {
  423. rightlevel = wm8904_scalevolume(volume, (b16_t)priv->balance);
  424. }
  425. #else
  426. leftlevel = priv->volume;
  427. rightlevel = priv->volume;
  428. #endif
  429. /* Set the volume */
  430. regval = WM8904_HPOUTZC | WM8904_HPOUT_VOL(leftlevel);
  431. if (mute)
  432. {
  433. regval |= WM8904_HPOUT_MUTE;
  434. }
  435. wm8904_writereg(priv, WM8904_ANA_LEFT_OUT1, regval);
  436. regval = WM8904_HPOUTZC | WM8904_HPOUT_VOL(rightlevel);
  437. if (mute)
  438. {
  439. regval |= WM8904_HPOUT_MUTE;
  440. }
  441. wm8904_writereg(priv, WM8904_ANA_RIGHT_OUT1, regval);
  442. /* Remember the volume level and mute settings */
  443. priv->volume = volume;
  444. priv->mute = mute;
  445. }
  446. #endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
  447. /************************************************************************************
  448. * Name: wm8904_setbass
  449. *
  450. * Description:
  451. * Set the bass level.
  452. *
  453. * The level and range are in whole percentage levels (0-100).
  454. *
  455. ************************************************************************************/
  456. #ifndef CONFIG_AUDIO_EXCLUDE_TONE
  457. static void wm8904_setbass(FAR struct wm8904_dev_s *priv, uint8_t bass)
  458. {
  459. audinfo("bass=%u\n", bass);
  460. #warning Missing logic
  461. }
  462. #endif /* CONFIG_AUDIO_EXCLUDE_TONE */
  463. /************************************************************************************
  464. * Name: wm8904_settreble
  465. *
  466. * Description:
  467. * Set the treble level .
  468. *
  469. * The level and range are in whole percentage levels (0-100).
  470. *
  471. ************************************************************************************/
  472. #ifndef CONFIG_AUDIO_EXCLUDE_TONE
  473. static void wm8904_settreble(FAR struct wm8904_dev_s *priv, uint8_t treble)
  474. {
  475. audinfo("treble=%u\n", treble);
  476. #warning Missing logic
  477. }
  478. #endif /* CONFIG_AUDIO_EXCLUDE_TONE */
  479. /****************************************************************************
  480. * Name: wm8904_setdatawidth
  481. *
  482. * Description:
  483. * Set the 8- or 16-bit data modes
  484. *
  485. ****************************************************************************/
  486. static void wm8904_setdatawidth(FAR struct wm8904_dev_s *priv)
  487. {
  488. uint16_t regval;
  489. /* "8-bit mode is selected whenever DAC_COMP=1 or ADC_COMP=1. The use of
  490. * 8-bit data allows samples to be passed using as few as 8 BCLK cycles
  491. * per LRCLK frame. When using DSP mode B, 8-bit data words may be
  492. * transferred consecutively every 8 BCLK cycles.
  493. *
  494. * "8-bit mode (without Companding) may be enabled by setting
  495. * DAC_COMPMODE=1 or ADC_COMPMODE=1, when DAC_COMP=0 and ADC_COMP=0.
  496. */
  497. if (priv->bpsamp == 16)
  498. {
  499. /* Reset default default setting */
  500. regval = (WM8904_AIFADCR_SRC | WM8904_AIFDACR_SRC);
  501. wm8904_writereg(priv, WM8904_AIF0, regval);
  502. }
  503. else
  504. {
  505. /* This should select 8-bit with no companding */
  506. regval = (WM8904_AIFADCR_SRC | WM8904_AIFDACR_SRC |
  507. WM8904_ADC_COMPMODE | WM8904_DAC_COMPMODE);
  508. wm8904_writereg(priv, WM8904_AIF0, regval);
  509. }
  510. }
  511. /****************************************************************************
  512. * Name: wm8904_setbitrate
  513. *
  514. * Description:
  515. * Program the FLL to achieve the requested bitrate (fout). Given:
  516. *
  517. * samprate - Samples per second
  518. * nchannels - Number of channels of data
  519. * bpsamp - Bits per sample
  520. *
  521. * Then
  522. * fout = samprate * nchannels * bpsamp
  523. *
  524. * For example:
  525. * samplerate = 11,025 samples/sec
  526. * nchannels = 1
  527. * bpsamp = 16 bits
  528. *
  529. * Then
  530. * fout = 11025 samples/sec * 1 * 16 bits/sample = 176.4 bits/sec
  531. *
  532. * The clocking is configured like this:
  533. * MCLK is the FLL source clock
  534. * Fref is the scaled down version of MCLK
  535. * Fvco is the output frequency from the FLL
  536. * Fout is the final output from the FLL that drives the SYSCLK
  537. * SYSCLK can be divided down to generate the BCLK
  538. *
  539. * The FLL output frequency is generated at that fout by:
  540. *
  541. * Fout = (Fvco / FLL_OUTDIV)
  542. *
  543. * The FLL operating frequency is set according to:
  544. *
  545. * Fvco = Fref * N.K * FLL_RATIO
  546. *
  547. * Where Fref is the input frequency frequency as determined by
  548. * FLL_CLK_REF_DIV. Fvco must be in the range of 90-100MHz.
  549. *
  550. * As an example:
  551. * FLL_CLK_REF_DIV = 16
  552. * FLL_OUTDIV = 8
  553. * N.K = 187.25
  554. * FLL_RATIO=16
  555. * Fref =32,768
  556. *
  557. * Fvco = 32,768 * 187.25 / 16 = 383,488 Hz
  558. * Fout = 383,488 / 8 = 47,936 Hz (approx. 48Khz)
  559. *
  560. ****************************************************************************/
  561. static void wm8904_setbitrate(FAR struct wm8904_dev_s *priv)
  562. {
  563. uint64_t tmp64;
  564. uint32_t fref;
  565. uint32_t fvco;
  566. uint32_t fout;
  567. uint32_t minfout;
  568. uint16_t regval;
  569. b16_t nk;
  570. unsigned int fllndx;
  571. unsigned int divndx;
  572. unsigned int outdiv;
  573. unsigned int framelen;
  574. #ifdef WM8904_USE_FFLOCK_INT
  575. bool enabled;
  576. int retries;
  577. #endif
  578. DEBUGASSERT(priv && priv->lower);
  579. /* First calculate the desired bitrate (fout). This is based on
  580. *
  581. * 1. The I2S frame length (in bits)
  582. * 2. The number of frames per second = nchannnels * samplerate
  583. */
  584. framelen = (priv->bpsamp == 8) ? WM8904_FRAMELEN8 : WM8904_FRAMELEN16;
  585. fout = (uint32_t)priv->samprate * (uint32_t)priv->nchannels * framelen;
  586. regval = WM8904_LRCLK_DIR | WM8904_LRCLK_RATE(framelen << 1);
  587. wm8904_writereg(priv, WM8904_AIF3, regval);
  588. audinfo("sample rate=%u nchannels=%u bpsamp=%u framelen=%d fout=%lu\n",
  589. priv->samprate, priv->nchannels, priv->bpsamp, framelen,
  590. (unsigned long)fout);
  591. /* Disable the SYSCLK.
  592. *
  593. * "The SYSCLK signal is enabled by register bit CLK_SYS_ENA. This bit
  594. * should be set to 0 when reconfiguring clock sources. ... "
  595. *
  596. * REVISIT: This does not appear necessary if we are just reconfiguring
  597. * the FLL. Disabling the FLL will stop the SYSCLK input just fine.
  598. */
  599. regval = WM8904_SYSCLK_SRCFLL | WM8904_CLK_DSP_ENA;
  600. wm8904_writereg(priv, WM8904_CLKRATE2, regval);
  601. #if 0 /* Unnecessary */
  602. /* Unlock forced oscillator control and switch it off */
  603. wm8904_writereg(priv, WM8904_CTRLIF_TEST_1, WM8904_USER_KEY);
  604. wm8904_writereg(priv, WM8904_FLL_NCO_TEST1, 0);
  605. wm8904_writereg(priv, WM8904_CTRLIF_TEST_1, 0);
  606. #endif
  607. /* "The FLL is enabled using the FLL_ENA register bit. Note that, when
  608. * changing FLL settings, it is recommended that the digital circuit be
  609. * disabled via FLL_ENA and then re-enabled after the other register
  610. * settings have been updated."
  611. */
  612. wm8904_writereg(priv, WM8904_FLL_CTRL1, 0);
  613. /* Determine Fref. The source refrence clock should be the MCLK */
  614. fref = priv->lower->mclk;
  615. regval = (WM8904_FLL_CLK_REF_SRC_MCLK | WM8904_FLL_CLK_REF_DIV1);
  616. /* MCLK must be divided down so that fref <=13.5MHz */
  617. if (fref > 4*13500000)
  618. {
  619. fref >>= 3;
  620. regval = (WM8904_FLL_CLK_REF_SRC_MCLK | WM8904_FLL_CLK_REF_DIV8);
  621. }
  622. else if (fref > 2*13500000)
  623. {
  624. fref >>= 2;
  625. regval = (WM8904_FLL_CLK_REF_SRC_MCLK | WM8904_FLL_CLK_REF_DIV4);
  626. }
  627. else if (fref > 13500000)
  628. {
  629. fref >>= 1;
  630. regval = (WM8904_FLL_CLK_REF_SRC_MCLK | WM8904_FLL_CLK_REF_DIV2);
  631. }
  632. wm8904_writereg(priv, WM8904_FLL_CTRL5, regval);
  633. /* Fvco must be between 90 and 100Mhz. In order to meet this
  634. * requirement, the value of FLL_OUTDIV should be selected according
  635. * to the desired output Fout. The divider, FLL_OUTDIV, must be set
  636. * so that Fvco is in the range 90-100MHz. The available divisions
  637. * are integers from 4 to 64.
  638. *
  639. * Fout = Fvco /FLL_OUTDIV
  640. *
  641. *
  642. * Is this Fout realizable? This often happens for very low frequencies.
  643. * If so, we can select a different final SYSCLK scaling frequency.
  644. */
  645. minfout = WM8904_FVCO_MAX / WM8904_MAXOUTDIV;
  646. divndx = 0;
  647. for (; ; )
  648. {
  649. /* Calculate the new value of Fout that we would need to provide
  650. * with this SYSCLK divider in place.
  651. */
  652. uint32_t newfout = (g_sysclk_scaleb1[divndx] * fout) >> 1;
  653. /* Is this increased Fout realizable? Or are we just just out of
  654. * dividers?
  655. */
  656. if (newfout >= minfout || divndx == WM8904_BCLK_MAXDIV)
  657. {
  658. /* In either case, this is the Fout and divider that we will be
  659. * using.
  660. */
  661. fout = newfout;
  662. break;
  663. }
  664. /* We have more.. Try the next divider */
  665. divndx++;
  666. }
  667. /* When we get here, divndx holds the register value for the new SYSCLK
  668. * divider. Set the divider value in the Audio Interface 2 register.
  669. */
  670. regval = WM8904_OPCLK_DIV1 | WM8904_BCLK_DIV(divndx);
  671. wm8904_writereg(priv, WM8904_AIF2, regval);
  672. /* Now lets make our best guess for FLL_OUTDIV
  673. *
  674. * FLL_OUTDIV = 95000000 / Fout
  675. */
  676. outdiv = ((WM8904_FVCO_MAX + WM8904_FVCO_MAX) >> 1) / fout;
  677. if (outdiv < 4)
  678. {
  679. outdiv = 4;
  680. }
  681. else if (outdiv > 64)
  682. {
  683. outdiv = 64;
  684. }
  685. /* The WM8904 suggests the selecting FLL_RATIO via the following
  686. * range checks:
  687. */
  688. if (fref >= 1000000)
  689. {
  690. fllndx = WM8904_NFLLRATIO_DIV1;
  691. }
  692. else if (fref > 256000)
  693. {
  694. fllndx = WM8904_NFLLRATIO_DIV2;
  695. }
  696. else if (fref > 128000)
  697. {
  698. fllndx = WM8904_NFLLRATIO_DIV4;
  699. }
  700. else if (fref > 64000)
  701. {
  702. fllndx = WM8904_NFLLRATIO_DIV8;
  703. }
  704. else
  705. {
  706. fllndx = WM8904_NFLLRATIO_DIV16;
  707. }
  708. /* Finally, we need to determine the value of N.K
  709. *
  710. * Fvco = (Fout * FLL_OUTDIV)
  711. * N.K = Fvco / (FLL_FRATIO * FREF)
  712. */
  713. fvco = fout * outdiv;
  714. tmp64 = ((uint64_t)fvco << 16) / (g_fllratio[fllndx] * fref);
  715. nk = (b16_t)tmp64;
  716. audinfo("mclk=%lu fref=%lu fvco=%lu fout=%lu divndx=%u\n",
  717. (unsigned long)priv->lower->mclk, (unsigned long)fref,
  718. (unsigned long)fvco, (unsigned long)fout, divndx);
  719. audinfo("N.K=%08lx outdiv=%u fllratio=%u\n",
  720. (unsigned long)nk, outdiv, g_fllratio[fllndx]);
  721. /* Save the actual bit rate that we are using. This will be used by the
  722. * LRCLCK calculations.
  723. */
  724. priv->bitrate = fout;
  725. /* Now, Configure the FLL */
  726. /* FLL Control 1
  727. *
  728. * FLL_FRACN_ENA=1 : Enables fractional mode
  729. * FLL_OSC_EN=0 : FLL internal oscillator disabled
  730. * FLL_ENA=0 : The FLL is not enabled
  731. *
  732. * FLL_OSC_ENA must be enabled before enabling FLL_ENA (FLL_OSC_ENA is
  733. * only required for free-running modes).
  734. */
  735. wm8904_writereg(priv, WM8904_FLL_CTRL1, 0);
  736. wm8904_writereg(priv, WM8904_FLL_CTRL1, WM8904_FLL_FRACN_ENA);
  737. /* FLL Control 2
  738. *
  739. * FLL_OUTDIV : FLL Fout clock divider
  740. * : Fout = Fvco / FLL_OUTDIV
  741. * : Calculated above
  742. * FLL_CTRL_RATE=1 : Frequency of the FLL control block,
  743. * : = Fvco / FLL_CTRL_RATE
  744. * FLL_FRATIO : Fvco clock divider
  745. * : Determined by MCLK tests above
  746. */
  747. regval = WM8904_FLL_OUTDIV(outdiv) | WM8904_FLL_CTRL_RATE(1) |
  748. WM8904_FLL_FRATIO(fllndx);
  749. wm8904_writereg(priv, WM8904_FLL_CTRL2, regval);
  750. /* FLL Control 3
  751. *
  752. * Fractional multiply for Fref
  753. */
  754. wm8904_writereg(priv, WM8904_FLL_CTRL3, b16frac(nk));
  755. /* FLL Control 4
  756. *
  757. * FLL_N : Integer multiply for Fref
  758. * FLL_GAIN : Gain applied to error
  759. */
  760. regval = WM8904_FLL_N(b16toi(nk)) | WM8904_FLL_GAIN_X1;
  761. wm8904_writereg(priv, WM8904_FLL_CTRL4, regval);
  762. /* FLL Control 5
  763. *
  764. * FLL_CLK_REF_DIV : FLL Clock Reference Divider
  765. *
  766. * Already set above
  767. */
  768. /* Enable the FLL */
  769. regval = WM8904_FLL_FRACN_ENA | WM8904_FLL_ENA;
  770. wm8904_writereg(priv, WM8904_FLL_CTRL1, regval);
  771. #if defined(WM8904_USE_FFLOCK_INT)
  772. /* Make sure that interrupts are enabled */
  773. enabled = WM8904_ENABLE(priv->lower);
  774. /* Enable the FLL lock interrupt. Here we can be sloppy since the FLL
  775. * lock is the only interrupt every enabled.
  776. */
  777. priv->locked = false;
  778. regval = WM8904_ALL_INTS & ~WM8904_FLL_LOCK_INT;
  779. wm8904_writereg(priv, WM8904_INT_MASK, regval);
  780. /* Allow time for FLL lock. Typical is 2 MSec. No exotic interlock
  781. * here; we just poll a flag set by the interrupt handler.
  782. * REVISIT: Probably not necessary.
  783. */
  784. retries = 5;
  785. do
  786. {
  787. nxsig_usleep(5*5000);
  788. }
  789. while (priv->locked == false && --retries > 0);
  790. /* Make sure that the FLL lock interrupt is disabled and clear any pending
  791. * interrupt status (again cutting* some corners). NOTE: The interrupt
  792. * handler will do these things if there is no timeout.
  793. */
  794. WM8904_DISABLE(priv->lower);
  795. wm8904_writereg(priv, WM8904_INT_MASK, WM8904_ALL_INTS);
  796. wm8904_writereg(priv, WM8904_INT_STATUS, WM8904_ALL_INTS);
  797. /* Restore the interrupt state. */
  798. WM8904_RESTORE(priv->lower, enabled)
  799. #elif defined(WM8904_USE_FFLOCK_POLL)
  800. /* Allow time for FLL lock. Typical is 2 MSec. */
  801. retries = 5;
  802. do
  803. {
  804. nxsig_usleep(5*5000);
  805. }
  806. while ((wm8904_readreg(priv, WM8904_INT_STATUS) & WM8904_FLL_LOCK_INT) != 0 ||
  807. --retries > 0);
  808. /* Clear all pending status bits by writing 1's into the interrupt status
  809. * register.
  810. */
  811. wm8904_writereg(priv, WM8904_INT_STATUS, WM8904_ALL_INTS);
  812. #endif /* !WM8904_USE_FFLOCK_INT && !WM8904_USE_FFLOCK_POLL */
  813. /* Re-enable the SYSCLK. */
  814. regval = WM8904_SYSCLK_SRCFLL | WM8904_CLK_SYS_ENA | WM8904_CLK_DSP_ENA;
  815. wm8904_writereg(priv, WM8904_CLKRATE2, regval);
  816. }
  817. /****************************************************************************
  818. * Name: wm8904_getcaps
  819. *
  820. * Description:
  821. * Get the audio device capabilities
  822. *
  823. ****************************************************************************/
  824. static int wm8904_getcaps(FAR struct audio_lowerhalf_s *dev, int type,
  825. FAR struct audio_caps_s *caps)
  826. {
  827. /* Validate the structure */
  828. DEBUGASSERT(caps && caps->ac_len >= sizeof(struct audio_caps_s));
  829. audinfo("type=%d ac_type=%d\n", type, caps->ac_type);
  830. /* Fill in the caller's structure based on requested info */
  831. caps->ac_format.hw = 0;
  832. caps->ac_controls.w = 0;
  833. switch (caps->ac_type)
  834. {
  835. /* Caller is querying for the types of units we support */
  836. case AUDIO_TYPE_QUERY:
  837. /* Provide our overall capabilities. The interfacing software
  838. * must then call us back for specific info for each capability.
  839. */
  840. caps->ac_channels = 2; /* Stereo output */
  841. switch (caps->ac_subtype)
  842. {
  843. case AUDIO_TYPE_QUERY:
  844. /* We don't decode any formats! Only something above us in
  845. * the audio stream can perform decoding on our behalf.
  846. */
  847. /* The types of audio units we implement */
  848. caps->ac_controls.b[0] = AUDIO_TYPE_OUTPUT | AUDIO_TYPE_FEATURE |
  849. AUDIO_TYPE_PROCESSING;
  850. break;
  851. case AUDIO_FMT_MIDI:
  852. /* We only support Format 0 */
  853. caps->ac_controls.b[0] = AUDIO_SUBFMT_END;
  854. break;
  855. default:
  856. caps->ac_controls.b[0] = AUDIO_SUBFMT_END;
  857. break;
  858. }
  859. break;
  860. /* Provide capabilities of our OUTPUT unit */
  861. case AUDIO_TYPE_OUTPUT:
  862. caps->ac_channels = 2;
  863. switch (caps->ac_subtype)
  864. {
  865. case AUDIO_TYPE_QUERY:
  866. /* Report the Sample rates we support */
  867. caps->ac_controls.b[0] = AUDIO_SAMP_RATE_8K | AUDIO_SAMP_RATE_11K |
  868. AUDIO_SAMP_RATE_16K | AUDIO_SAMP_RATE_22K |
  869. AUDIO_SAMP_RATE_32K | AUDIO_SAMP_RATE_44K |
  870. AUDIO_SAMP_RATE_48K;
  871. break;
  872. case AUDIO_FMT_MP3:
  873. case AUDIO_FMT_WMA:
  874. case AUDIO_FMT_PCM:
  875. break;
  876. default:
  877. break;
  878. }
  879. break;
  880. /* Provide capabilities of our FEATURE units */
  881. case AUDIO_TYPE_FEATURE:
  882. /* If the sub-type is UNDEF, then report the Feature Units we support */
  883. if (caps->ac_subtype == AUDIO_FU_UNDEF)
  884. {
  885. /* Fill in the ac_controls section with the Feature Units we have */
  886. caps->ac_controls.b[0] = AUDIO_FU_VOLUME | AUDIO_FU_BASS | AUDIO_FU_TREBLE;
  887. caps->ac_controls.b[1] = AUDIO_FU_BALANCE >> 8;
  888. }
  889. else
  890. {
  891. /* TODO: Do we need to provide specific info for the Feature Units,
  892. * such as volume setting ranges, etc.?
  893. */
  894. }
  895. break;
  896. /* Provide capabilities of our PROCESSING unit */
  897. case AUDIO_TYPE_PROCESSING:
  898. switch (caps->ac_subtype)
  899. {
  900. case AUDIO_PU_UNDEF:
  901. /* Provide the type of Processing Units we support */
  902. caps->ac_controls.b[0] = AUDIO_PU_STEREO_EXTENDER;
  903. break;
  904. case AUDIO_PU_STEREO_EXTENDER:
  905. /* Provide capabilities of our Stereo Extender */
  906. caps->ac_controls.b[0] = AUDIO_STEXT_ENABLE | AUDIO_STEXT_WIDTH;
  907. break;
  908. default:
  909. /* Other types of processing uint we don't support */
  910. break;
  911. }
  912. break;
  913. /* All others we don't support */
  914. default:
  915. /* Zero out the fields to indicate no support */
  916. caps->ac_subtype = 0;
  917. caps->ac_channels = 0;
  918. break;
  919. }
  920. /* Return the length of the audio_caps_s struct for validation of
  921. * proper Audio device type.
  922. */
  923. return caps->ac_len;
  924. }
  925. /****************************************************************************
  926. * Name: wm8904_configure
  927. *
  928. * Description:
  929. * Configure the audio device for the specified mode of operation.
  930. *
  931. ****************************************************************************/
  932. #ifdef CONFIG_AUDIO_MULTI_SESSION
  933. static int wm8904_configure(FAR struct audio_lowerhalf_s *dev,
  934. FAR void *session,
  935. FAR const struct audio_caps_s *caps)
  936. #else
  937. static int wm8904_configure(FAR struct audio_lowerhalf_s *dev,
  938. FAR const struct audio_caps_s *caps)
  939. #endif
  940. {
  941. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  942. int ret = OK;
  943. DEBUGASSERT(priv != NULL && caps != NULL);
  944. audinfo("ac_type: %d\n", caps->ac_type);
  945. /* Process the configure operation */
  946. switch (caps->ac_type)
  947. {
  948. case AUDIO_TYPE_FEATURE:
  949. audinfo(" AUDIO_TYPE_FEATURE\n");
  950. /* Process based on Feature Unit */
  951. switch (caps->ac_format.hw)
  952. {
  953. #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
  954. case AUDIO_FU_VOLUME:
  955. {
  956. /* Set the volume */
  957. uint16_t volume = caps->ac_controls.hw[0];
  958. audinfo(" Volume: %d\n", volume);
  959. if (volume >= 0 && volume <= 1000)
  960. {
  961. /* Scale the volume setting to the range {0.. 63} */
  962. wm8904_setvolume(priv, (63 * volume / 1000), priv->mute);
  963. }
  964. else
  965. {
  966. ret = -EDOM;
  967. }
  968. }
  969. break;
  970. #endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
  971. #ifndef CONFIG_AUDIO_EXCLUDE_TONE
  972. case AUDIO_FU_BASS:
  973. {
  974. /* Set the bass. The percentage level (0-100) is in the
  975. * ac_controls.b[0] parameter.
  976. */
  977. uint8_t bass = caps->ac_controls.b[0];
  978. audinfo(" Bass: %d\n", bass);
  979. if (bass <= 100)
  980. {
  981. wm8904_setbass(priv, bass);
  982. }
  983. else
  984. {
  985. ret = -EDOM;
  986. }
  987. }
  988. break;
  989. case AUDIO_FU_TREBLE:
  990. {
  991. /* Set the treble. The percentage level (0-100) is in the
  992. * ac_controls.b[0] parameter.
  993. */
  994. uint8_t treble = caps->ac_controls.b[0];
  995. audinfo(" Treble: %d\n", treble);
  996. if (treble <= 100)
  997. {
  998. wm8904_settreble(priv, treble);
  999. }
  1000. else
  1001. {
  1002. ret = -EDOM;
  1003. }
  1004. }
  1005. break;
  1006. #endif /* CONFIG_AUDIO_EXCLUDE_TONE */
  1007. default:
  1008. auderr(" ERROR: Unrecognized feature unit\n");
  1009. ret = -ENOTTY;
  1010. break;
  1011. }
  1012. break;
  1013. case AUDIO_TYPE_OUTPUT:
  1014. {
  1015. audinfo(" AUDIO_TYPE_OUTPUT:\n");
  1016. audinfo(" Number of channels: %u\n", caps->ac_channels);
  1017. audinfo(" Sample rate: %u\n", caps->ac_controls.hw[0]);
  1018. audinfo(" Sample width: %u\n", caps->ac_controls.b[2]);
  1019. /* Verify that all of the requested values are supported */
  1020. ret = -ERANGE;
  1021. if (caps->ac_channels != 1 && caps->ac_channels != 2)
  1022. {
  1023. auderr("ERROR: Unsupported number of channels: %d\n",
  1024. caps->ac_channels);
  1025. break;
  1026. }
  1027. if (caps->ac_controls.b[2] != 8 && caps->ac_controls.b[2] != 16)
  1028. {
  1029. auderr("ERROR: Unsupported bits per sample: %d\n",
  1030. caps->ac_controls.b[2]);
  1031. break;
  1032. }
  1033. /* Save the current stream configuration */
  1034. priv->samprate = caps->ac_controls.hw[0];
  1035. priv->nchannels = caps->ac_channels;
  1036. priv->bpsamp = caps->ac_controls.b[2];
  1037. /* Reconfigure the FLL to support the resulting number or channels,
  1038. * bits per sample, and bitrate.
  1039. */
  1040. wm8904_setdatawidth(priv);
  1041. wm8904_setbitrate(priv);
  1042. wm8904_writereg(priv, WM8904_DUMMY, 0x55aa);
  1043. wm8904_clock_analysis(&priv->dev, "AUDIO_TYPE_OUTPUT");
  1044. ret = OK;
  1045. }
  1046. break;
  1047. case AUDIO_TYPE_PROCESSING:
  1048. break;
  1049. }
  1050. return ret;
  1051. }
  1052. /****************************************************************************
  1053. * Name: wm8904_shutdown
  1054. *
  1055. * Description:
  1056. * Shutdown the WM8904 chip and put it in the lowest power state possible.
  1057. *
  1058. ****************************************************************************/
  1059. static int wm8904_shutdown(FAR struct audio_lowerhalf_s *dev)
  1060. {
  1061. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  1062. DEBUGASSERT(priv);
  1063. /* First disable interrupts */
  1064. WM8904_DISABLE(priv->lower);
  1065. /* Now issue a software reset. This puts all WM8904 registers back in
  1066. * their default state.
  1067. */
  1068. wm8904_hw_reset(priv);
  1069. return OK;
  1070. }
  1071. /****************************************************************************
  1072. * Name: wm8904_senddone
  1073. *
  1074. * Description:
  1075. * This is the I2S callback function that is invoked when the transfer
  1076. * completes.
  1077. *
  1078. ****************************************************************************/
  1079. static void wm8904_senddone(FAR struct i2s_dev_s *i2s,
  1080. FAR struct ap_buffer_s *apb, FAR void *arg,
  1081. int result)
  1082. {
  1083. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)arg;
  1084. struct audio_msg_s msg;
  1085. irqstate_t flags;
  1086. int ret;
  1087. DEBUGASSERT(i2s && priv && priv->running && apb);
  1088. audinfo("apb=%p inflight=%d result=%d\n", apb, priv->inflight, result);
  1089. /* We do not place any restriction on the context in which this function
  1090. * is called. It may be called from an interrupt handler. Therefore, the
  1091. * doneq and in-flight values might be accessed from the interrupt level.
  1092. * Not the best design. But we will use interrupt controls to protect
  1093. * against that possibility.
  1094. */
  1095. flags = enter_critical_section();
  1096. /* Add the completed buffer to the end of our doneq. We do not yet
  1097. * decrement the reference count.
  1098. */
  1099. dq_addlast((FAR dq_entry_t *)apb, &priv->doneq);
  1100. /* And decrement the number of buffers in-flight */
  1101. DEBUGASSERT(priv->inflight > 0);
  1102. priv->inflight--;
  1103. /* Save the result of the transfer */
  1104. /* REVISIT: This can be overwritten */
  1105. priv->result = result;
  1106. leave_critical_section(flags);
  1107. /* Now send a message to the worker thread, informing it that there are
  1108. * buffers in the done queue that need to be cleaned up.
  1109. */
  1110. msg.msgId = AUDIO_MSG_COMPLETE;
  1111. ret = nxmq_send(priv->mq, (FAR const char *)&msg, sizeof(msg),
  1112. CONFIG_WM8904_MSG_PRIO);
  1113. if (ret < 0)
  1114. {
  1115. auderr("ERROR: nxmq_send failed: %d\n", ret);
  1116. }
  1117. }
  1118. /****************************************************************************
  1119. * Name: wm8904_returnbuffers
  1120. *
  1121. * Description:
  1122. * This function is called after the complete of one or more data
  1123. * transfers. This function will empty the done queue and release our
  1124. * reference to each buffer.
  1125. *
  1126. ****************************************************************************/
  1127. static void wm8904_returnbuffers(FAR struct wm8904_dev_s *priv)
  1128. {
  1129. FAR struct ap_buffer_s *apb;
  1130. irqstate_t flags;
  1131. /* The doneq and in-flight values might be accessed from the interrupt
  1132. * level in some implementations. Not the best design. But we will
  1133. * use interrupt controls to protect against that possibility.
  1134. */
  1135. flags = enter_critical_section();
  1136. while (dq_peek(&priv->doneq) != NULL)
  1137. {
  1138. /* Take the next buffer from the queue of completed transfers */
  1139. apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->doneq);
  1140. leave_critical_section(flags);
  1141. audinfo("Returning: apb=%p curbyte=%d nbytes=%d flags=%04x\n",
  1142. apb, apb->curbyte, apb->nbytes, apb->flags);
  1143. /* Are we returning the final buffer in the stream? */
  1144. if ((apb->flags & AUDIO_APB_FINAL) != 0)
  1145. {
  1146. /* Both the pending and the done queues should be empty and there
  1147. * should be no buffers in-flight.
  1148. */
  1149. DEBUGASSERT(dq_empty(&priv->doneq) && dq_empty(&priv->pendq) &&
  1150. priv->inflight == 0);
  1151. /* Set the terminating flag. This will, eventually, cause the
  1152. * worker thread to exit (if it is not already terminating).
  1153. */
  1154. audinfo("Terminating\n");
  1155. priv->terminating = true;
  1156. }
  1157. /* Release our reference to the audio buffer */
  1158. apb_free(apb);
  1159. /* Send the buffer back up to the previous level. */
  1160. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1161. priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK, NULL);
  1162. #else
  1163. priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK);
  1164. #endif
  1165. flags = enter_critical_section();
  1166. }
  1167. leave_critical_section(flags);
  1168. }
  1169. /****************************************************************************
  1170. * Name: wm8904_sendbuffer
  1171. *
  1172. * Description:
  1173. * Start the transfer an audio buffer to the WM8904 via I2S. This
  1174. * will not wait for the transfer to complete but will return immediately.
  1175. * the wmd8904_senddone called will be invoked when the transfer
  1176. * completes, stimulating the worker thread to call this function again.
  1177. *
  1178. ****************************************************************************/
  1179. static int wm8904_sendbuffer(FAR struct wm8904_dev_s *priv)
  1180. {
  1181. FAR struct ap_buffer_s *apb;
  1182. irqstate_t flags;
  1183. uint32_t timeout;
  1184. int shift;
  1185. int ret = OK;
  1186. /* Loop while there are audio buffers to be sent and we have few than
  1187. * CONFIG_WM8904_INFLIGHT then "in-flight"
  1188. *
  1189. * The 'inflight' value might be modified from the interrupt level in some
  1190. * implementations. We will use interrupt controls to protect against
  1191. * that possibility.
  1192. *
  1193. * The 'pendq', on the other hand, is protected via a semaphore. Let's
  1194. * hold the semaphore while we are busy here and disable the interrupts
  1195. * only while accessing 'inflight'.
  1196. */
  1197. wm8904_takesem(&priv->pendsem);
  1198. while (priv->inflight < CONFIG_WM8904_INFLIGHT &&
  1199. dq_peek(&priv->pendq) != NULL && !priv->paused)
  1200. {
  1201. /* Take next buffer from the queue of pending transfers */
  1202. apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq);
  1203. audinfo("Sending apb=%p, size=%d inflight=%d\n",
  1204. apb, apb->nbytes, priv->inflight);
  1205. /* Increment the number of buffers in-flight before sending in order
  1206. * to avoid a possible race condition.
  1207. */
  1208. flags = enter_critical_section();
  1209. priv->inflight++;
  1210. leave_critical_section(flags);
  1211. /* Send the entire audio buffer via I2S. What is a reasonable timeout
  1212. * to use? This would depend on the bit rate and size of the buffer.
  1213. *
  1214. * Samples in the buffer (samples):
  1215. * = buffer_size * 8 / bpsamp samples
  1216. * Sample rate (samples/second):
  1217. * = samplerate * nchannels
  1218. * Expected transfer time (seconds):
  1219. * = (buffer_size * 8) / bpsamp / samplerate / nchannels
  1220. *
  1221. * We will set the timeout about twice that.
  1222. *
  1223. * NOTES:
  1224. * - The multiplier of 8 becomes 16000 for 2x and units of
  1225. * milliseconds.
  1226. * - 16000 is a approximately 16384 (1 << 14), bpsamp is either
  1227. * (1 << 3) or (1 << 4), and nchannels is either (1 << 0) or
  1228. * (1 << 1). So this can be simplifies to (milliseconds):
  1229. *
  1230. * = (buffer_size << shift) / samplerate
  1231. */
  1232. shift = (priv->bpsamp == 8) ? 14 - 3 : 14 - 4;
  1233. shift -= (priv->nchannels > 1) ? 1 : 0;
  1234. timeout = MSEC2TICK(((uint32_t)(apb->nbytes - apb->curbyte) << shift) /
  1235. (uint32_t)priv->samprate);
  1236. ret = I2S_SEND(priv->i2s, apb, wm8904_senddone, priv, timeout);
  1237. if (ret < 0)
  1238. {
  1239. auderr("ERROR: I2S_SEND failed: %d\n", ret);
  1240. break;
  1241. }
  1242. }
  1243. wm8904_givesem(&priv->pendsem);
  1244. return ret;
  1245. }
  1246. /****************************************************************************
  1247. * Name: wm8904_start
  1248. *
  1249. * Description:
  1250. * Start the configured operation (audio streaming, volume enabled, etc.).
  1251. *
  1252. ****************************************************************************/
  1253. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1254. static int wm8904_start(FAR struct audio_lowerhalf_s *dev, FAR void *session)
  1255. #else
  1256. static int wm8904_start(FAR struct audio_lowerhalf_s *dev)
  1257. #endif
  1258. {
  1259. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  1260. struct sched_param sparam;
  1261. struct mq_attr attr;
  1262. pthread_attr_t tattr;
  1263. FAR void *value;
  1264. int ret;
  1265. audinfo("Entry\n");
  1266. /* Exit reduced power modes of operation */
  1267. /* REVISIT */
  1268. /* Create a message queue for the worker thread */
  1269. snprintf(priv->mqname, sizeof(priv->mqname), "/tmp/%X", priv);
  1270. attr.mq_maxmsg = 16;
  1271. attr.mq_msgsize = sizeof(struct audio_msg_s);
  1272. attr.mq_curmsgs = 0;
  1273. attr.mq_flags = 0;
  1274. priv->mq = mq_open(priv->mqname, O_RDWR | O_CREAT, 0644, &attr);
  1275. if (priv->mq == NULL)
  1276. {
  1277. /* Error creating message queue! */
  1278. auderr("ERROR: Couldn't allocate message queue\n");
  1279. return -ENOMEM;
  1280. }
  1281. /* Join any old worker thread we had created to prevent a memory leak */
  1282. if (priv->threadid != 0)
  1283. {
  1284. audinfo("Joining old thread\n");
  1285. pthread_join(priv->threadid, &value);
  1286. }
  1287. /* Start our thread for sending data to the device */
  1288. pthread_attr_init(&tattr);
  1289. sparam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 3;
  1290. (void)pthread_attr_setschedparam(&tattr, &sparam);
  1291. (void)pthread_attr_setstacksize(&tattr, CONFIG_WM8904_WORKER_STACKSIZE);
  1292. audinfo("Starting worker thread\n");
  1293. ret = pthread_create(&priv->threadid, &tattr, wm8904_workerthread,
  1294. (pthread_addr_t)priv);
  1295. if (ret != OK)
  1296. {
  1297. auderr("ERROR: pthread_create failed: %d\n", ret);
  1298. }
  1299. else
  1300. {
  1301. pthread_setname_np(priv->threadid, "wm8904");
  1302. audinfo("Created worker thread\n");
  1303. }
  1304. return ret;
  1305. }
  1306. /****************************************************************************
  1307. * Name: wm8904_stop
  1308. *
  1309. * Description: Stop the configured operation (audio streaming, volume
  1310. * disabled, etc.).
  1311. *
  1312. ****************************************************************************/
  1313. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  1314. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1315. static int wm8904_stop(FAR struct audio_lowerhalf_s *dev, FAR void *session)
  1316. #else
  1317. static int wm8904_stop(FAR struct audio_lowerhalf_s *dev)
  1318. #endif
  1319. {
  1320. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  1321. struct audio_msg_s term_msg;
  1322. FAR void *value;
  1323. /* Send a message to stop all audio streaming */
  1324. term_msg.msgId = AUDIO_MSG_STOP;
  1325. term_msg.u.data = 0;
  1326. (void)nxmq_send(priv->mq, (FAR const char *)&term_msg, sizeof(term_msg),
  1327. CONFIG_WM8904_MSG_PRIO);
  1328. /* Join the worker thread */
  1329. pthread_join(priv->threadid, &value);
  1330. priv->threadid = 0;
  1331. /* Enter into a reduced power usage mode */
  1332. /* REVISIT: */
  1333. return OK;
  1334. }
  1335. #endif
  1336. /****************************************************************************
  1337. * Name: wm8904_pause
  1338. *
  1339. * Description: Pauses the playback.
  1340. *
  1341. ****************************************************************************/
  1342. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  1343. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1344. static int wm8904_pause(FAR struct audio_lowerhalf_s *dev, FAR void *session)
  1345. #else
  1346. static int wm8904_pause(FAR struct audio_lowerhalf_s *dev)
  1347. #endif
  1348. {
  1349. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  1350. if (priv->running && !priv->paused)
  1351. {
  1352. /* Disable interrupts to prevent us from suppling any more data */
  1353. priv->paused = true;
  1354. wm8904_setvolume(priv, priv->volume, true);
  1355. WM8904_DISABLE(priv->lower);
  1356. }
  1357. return OK;
  1358. }
  1359. #endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
  1360. /****************************************************************************
  1361. * Name: wm8904_resume
  1362. *
  1363. * Description: Resumes the playback.
  1364. *
  1365. ****************************************************************************/
  1366. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  1367. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1368. static int wm8904_resume(FAR struct audio_lowerhalf_s *dev, FAR void *session)
  1369. #else
  1370. static int wm8904_resume(FAR struct audio_lowerhalf_s *dev)
  1371. #endif
  1372. {
  1373. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  1374. if (priv->running && priv->paused)
  1375. {
  1376. priv->paused = false;
  1377. wm8904_setvolume(priv, priv->volume, false);
  1378. /* Enable interrupts to allow sampling data */
  1379. wm8904_sendbuffer(priv);
  1380. #ifdef WM8904_USE_FFLOCK_INT
  1381. WM8904_ENABLE(priv->lower);
  1382. #endif
  1383. }
  1384. return OK;
  1385. }
  1386. #endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
  1387. /****************************************************************************
  1388. * Name: wm8904_enqueuebuffer
  1389. *
  1390. * Description: Enqueue an Audio Pipeline Buffer for playback/ processing.
  1391. *
  1392. ****************************************************************************/
  1393. static int wm8904_enqueuebuffer(FAR struct audio_lowerhalf_s *dev,
  1394. FAR struct ap_buffer_s *apb)
  1395. {
  1396. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  1397. struct audio_msg_s term_msg;
  1398. int ret;
  1399. audinfo("Enqueueing: apb=%p curbyte=%d nbytes=%d flags=%04x\n",
  1400. apb, apb->curbyte, apb->nbytes, apb->flags);
  1401. /* Take a reference on the new audio buffer */
  1402. apb_reference(apb);
  1403. /* Add the new buffer to the tail of pending audio buffers */
  1404. wm8904_takesem(&priv->pendsem);
  1405. apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED;
  1406. dq_addlast(&apb->dq_entry, &priv->pendq);
  1407. wm8904_givesem(&priv->pendsem);
  1408. /* Send a message to the worker thread indicating that a new buffer has been
  1409. * enqueued. If mq is NULL, then the playing has not yet started. In that
  1410. * case we are just "priming the pump" and we don't need to send any message.
  1411. */
  1412. ret = OK;
  1413. if (priv->mq != NULL)
  1414. {
  1415. term_msg.msgId = AUDIO_MSG_ENQUEUE;
  1416. term_msg.u.data = 0;
  1417. ret = nxmq_send(priv->mq, (FAR const char *)&term_msg,
  1418. sizeof(term_msg), CONFIG_WM8904_MSG_PRIO);
  1419. if (ret < 0)
  1420. {
  1421. auderr("ERROR: nxmq_send failed: %d\n", ret);
  1422. }
  1423. }
  1424. return ret;
  1425. }
  1426. /****************************************************************************
  1427. * Name: wm8904_cancelbuffer
  1428. *
  1429. * Description: Called when an enqueued buffer is being cancelled.
  1430. *
  1431. ****************************************************************************/
  1432. static int wm8904_cancelbuffer(FAR struct audio_lowerhalf_s *dev,
  1433. FAR struct ap_buffer_s *apb)
  1434. {
  1435. audinfo("apb=%p\n", apb);
  1436. return OK;
  1437. }
  1438. /****************************************************************************
  1439. * Name: wm8904_ioctl
  1440. *
  1441. * Description: Perform a device ioctl
  1442. *
  1443. ****************************************************************************/
  1444. static int wm8904_ioctl(FAR struct audio_lowerhalf_s *dev, int cmd,
  1445. unsigned long arg)
  1446. {
  1447. #ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS
  1448. FAR struct ap_buffer_info_s *bufinfo;
  1449. #endif
  1450. /* Deal with ioctls passed from the upper-half driver */
  1451. switch (cmd)
  1452. {
  1453. /* Check for AUDIOIOC_HWRESET ioctl. This ioctl is passed straight
  1454. * through from the upper-half audio driver.
  1455. */
  1456. case AUDIOIOC_HWRESET:
  1457. {
  1458. /* REVISIT: Should we completely re-initialize the chip? We
  1459. * can't just issue a software reset; that would puts all WM8904
  1460. * registers back in their default state.
  1461. */
  1462. audinfo("AUDIOIOC_HWRESET:\n");
  1463. }
  1464. break;
  1465. /* Report our preferred buffer size and quantity */
  1466. #ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS
  1467. case AUDIOIOC_GETBUFFERINFO:
  1468. {
  1469. audinfo("AUDIOIOC_GETBUFFERINFO:\n");
  1470. bufinfo = (FAR struct ap_buffer_info_s *) arg;
  1471. bufinfo->buffer_size = CONFIG_WM8904_BUFFER_SIZE;
  1472. bufinfo->nbuffers = CONFIG_WM8904_NUM_BUFFERS;
  1473. }
  1474. break;
  1475. #endif
  1476. default:
  1477. audinfo("Ignored\n");
  1478. break;
  1479. }
  1480. return OK;
  1481. }
  1482. /****************************************************************************
  1483. * Name: wm8904_reserve
  1484. *
  1485. * Description: Reserves a session (the only one we have).
  1486. *
  1487. ****************************************************************************/
  1488. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1489. static int wm8904_reserve(FAR struct audio_lowerhalf_s *dev,
  1490. FAR void **session)
  1491. #else
  1492. static int wm8904_reserve(FAR struct audio_lowerhalf_s *dev)
  1493. #endif
  1494. {
  1495. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *) dev;
  1496. int ret = OK;
  1497. /* Borrow the APBQ semaphore for thread sync */
  1498. wm8904_takesem(&priv->pendsem);
  1499. if (priv->reserved)
  1500. {
  1501. ret = -EBUSY;
  1502. }
  1503. else
  1504. {
  1505. /* Initialize the session context */
  1506. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1507. *session = NULL;
  1508. #endif
  1509. priv->inflight = 0;
  1510. priv->running = false;
  1511. priv->paused = false;
  1512. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  1513. priv->terminating = false;
  1514. #endif
  1515. priv->reserved = true;
  1516. }
  1517. wm8904_givesem(&priv->pendsem);
  1518. return ret;
  1519. }
  1520. /****************************************************************************
  1521. * Name: wm8904_release
  1522. *
  1523. * Description: Releases the session (the only one we have).
  1524. *
  1525. ****************************************************************************/
  1526. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1527. static int wm8904_release(FAR struct audio_lowerhalf_s *dev,
  1528. FAR void *session)
  1529. #else
  1530. static int wm8904_release(FAR struct audio_lowerhalf_s *dev)
  1531. #endif
  1532. {
  1533. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)dev;
  1534. void *value;
  1535. /* Join any old worker thread we had created to prevent a memory leak */
  1536. if (priv->threadid != 0)
  1537. {
  1538. pthread_join(priv->threadid, &value);
  1539. priv->threadid = 0;
  1540. }
  1541. /* Borrow the APBQ semaphore for thread sync */
  1542. wm8904_takesem(&priv->pendsem);
  1543. /* Really we should free any queued buffers here */
  1544. priv->reserved = false;
  1545. wm8904_givesem(&priv->pendsem);
  1546. return OK;
  1547. }
  1548. /****************************************************************************
  1549. * Name: wm8904_interrupt_work
  1550. *
  1551. * Description:
  1552. * WM8904 interrupt actions cannot be performed in the interrupt handler
  1553. * because I2C access is not possible in that context. Instead, all I2C
  1554. * operations are deferred to the work queue.
  1555. *
  1556. * Assumptions:
  1557. * WM8904 interrupts were disabled in the interrupt handler.
  1558. *
  1559. ****************************************************************************/
  1560. #ifdef WM8904_USE_FFLOCK_INT
  1561. static void wm8904_interrupt_work(FAR void *arg)
  1562. {
  1563. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)arg;
  1564. uint16_t regval;
  1565. DEBUGASSERT(priv && priv->lower);
  1566. /* Sample the interrupt status */
  1567. regval = wm8904_readreg(priv, WM8904_INT_STATUS);
  1568. audinfo("INT_STATUS: %04x\n", regval);
  1569. /* Check for the FLL lock interrupt. We are sloppy here since at
  1570. * present, only the FLL lock interrupt is used.
  1571. */
  1572. DEBUGASSERT((regval & WM8904_FLL_LOCK_INT) != 0 && !priv->locked);
  1573. UNUSED(regval);
  1574. priv->locked = true;
  1575. /* Clear all pending interrupts by write 1's to the interrupt status
  1576. * register.
  1577. *
  1578. * REVISIT: Since I2C is slow and not atomic with respect to WM8904 event,
  1579. * could this not cause the lost of interrupts?
  1580. */
  1581. wm8904_writereg(priv, WM8904_INT_STATUS, WM8904_ALL_INTS);
  1582. /* Disable further FLL lock interrupts. We are sloppy here since at
  1583. * present, only the FLL lock interrupt is used.
  1584. */
  1585. wm8904_writereg(priv, WM8904_INT_MASK, WM8904_ALL_INTS);
  1586. #ifdef WM8904_USE_FFLOCK_INT
  1587. /* Re-enable WM8904 interrupts */
  1588. WM8904_ENABLE(priv->lower);
  1589. #endif
  1590. }
  1591. #endif
  1592. /****************************************************************************
  1593. * Name: wm8904_interrupt
  1594. *
  1595. * Description:
  1596. * This is the ISR that services the GPIO1/IRQ pin from the WM8904. It
  1597. * signals WM8904 events such FLL lock.
  1598. *
  1599. ****************************************************************************/
  1600. #ifdef WM8904_USE_FFLOCK_INT
  1601. static int wm8904_interrupt(FAR const struct wm8904_lower_s *lower,
  1602. FAR void *arg)
  1603. {
  1604. FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *)arg;
  1605. int ret;
  1606. DEBUGASSERT(lower && priv);
  1607. /* Disable further interrupts and perform all interrupt related activities
  1608. * on the work thread. There is nothing that we can do from the interrupt
  1609. * handler because we cannot perform I2C operations here.
  1610. */
  1611. WM8904_DISABLE(priv->lower);
  1612. DEBUGASSERT(work_available(&priv->work));
  1613. ret = work_queue(LPWORK, &priv->work, wm8904_interrupt_work, priv, 0);
  1614. if (ret < 0)
  1615. {
  1616. auderr("ERROR: Failed to schedule work\n");
  1617. }
  1618. return OK;
  1619. }
  1620. #endif
  1621. /****************************************************************************
  1622. * Name: wm8904_workerthread
  1623. *
  1624. * This is the thread that feeds data to the chip and keeps the audio
  1625. * stream going.
  1626. *
  1627. ****************************************************************************/
  1628. static void *wm8904_workerthread(pthread_addr_t pvarg)
  1629. {
  1630. FAR struct wm8904_dev_s *priv = (struct wm8904_dev_s *) pvarg;
  1631. struct audio_msg_s msg;
  1632. FAR struct ap_buffer_s *apb;
  1633. int msglen;
  1634. int prio;
  1635. audinfo("Entry\n");
  1636. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  1637. priv->terminating = false;
  1638. #endif
  1639. /* Mark ourself as running and make sure that WM8904 interrupts are
  1640. * enabled.
  1641. */
  1642. priv->running = true;
  1643. #ifdef WM8904_USE_FFLOCK_INT
  1644. WM8904_ENABLE(priv->lower);
  1645. #endif
  1646. wm8904_setvolume(priv, priv->volume, false);
  1647. /* Loop as long as we are supposed to be running and as long as we have
  1648. * buffers in-flight.
  1649. */
  1650. while (priv->running || priv->inflight > 0)
  1651. {
  1652. /* Check if we have been asked to terminate. We have to check if we
  1653. * still have buffers in-flight. If we do, then we can't stop until
  1654. * birds come back to roost.
  1655. */
  1656. if (priv->terminating && priv->inflight <= 0)
  1657. {
  1658. /* We are IDLE. Break out of the loop and exit. */
  1659. break;
  1660. }
  1661. else
  1662. {
  1663. /* Check if we can send more audio buffers to the WM8904 */
  1664. wm8904_sendbuffer(priv);
  1665. }
  1666. /* Wait for messages from our message queue */
  1667. msglen = nxmq_receive(priv->mq, (FAR char *)&msg, sizeof(msg), &prio);
  1668. /* Handle the case when we return with no message */
  1669. if (msglen < sizeof(struct audio_msg_s))
  1670. {
  1671. auderr("ERROR: Message too small: %d\n", msglen);
  1672. continue;
  1673. }
  1674. /* Process the message */
  1675. switch (msg.msgId)
  1676. {
  1677. /* The ISR has requested more data. We will catch this case at
  1678. * the top of the loop.
  1679. */
  1680. case AUDIO_MSG_DATA_REQUEST:
  1681. audinfo("AUDIO_MSG_DATA_REQUEST\n");
  1682. break;
  1683. /* Stop the playback */
  1684. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  1685. case AUDIO_MSG_STOP:
  1686. /* Indicate that we are terminating */
  1687. audinfo("AUDIO_MSG_STOP: Terminating\n");
  1688. priv->terminating = true;
  1689. break;
  1690. #endif
  1691. /* We have a new buffer to send. We will catch this case at
  1692. * the top of the loop.
  1693. */
  1694. case AUDIO_MSG_ENQUEUE:
  1695. audinfo("AUDIO_MSG_ENQUEUE\n");
  1696. break;
  1697. /* We will wake up from the I2S callback with this message */
  1698. case AUDIO_MSG_COMPLETE:
  1699. audinfo("AUDIO_MSG_COMPLETE\n");
  1700. wm8904_returnbuffers(priv);
  1701. break;
  1702. default:
  1703. auderr("ERROR: Ignoring message ID %d\n", msg.msgId);
  1704. break;
  1705. }
  1706. }
  1707. /* Reset the WM8904 hardware */
  1708. wm8904_hw_reset(priv);
  1709. /* Return any pending buffers in our pending queue */
  1710. wm8904_takesem(&priv->pendsem);
  1711. while ((apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq)) != NULL)
  1712. {
  1713. /* Release our reference to the buffer */
  1714. apb_free(apb);
  1715. /* Send the buffer back up to the previous level. */
  1716. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1717. priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK, NULL);
  1718. #else
  1719. priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb, OK);
  1720. #endif
  1721. }
  1722. wm8904_givesem(&priv->pendsem);
  1723. /* Return any pending buffers in our done queue */
  1724. wm8904_returnbuffers(priv);
  1725. /* Close the message queue */
  1726. mq_close(priv->mq);
  1727. mq_unlink(priv->mqname);
  1728. priv->mq = NULL;
  1729. /* Send an AUDIO_MSG_COMPLETE message to the client */
  1730. #ifdef CONFIG_AUDIO_MULTI_SESSION
  1731. priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_COMPLETE, NULL, OK, NULL);
  1732. #else
  1733. priv->dev.upper(priv->dev.priv, AUDIO_CALLBACK_COMPLETE, NULL, OK);
  1734. #endif
  1735. audinfo("Exit\n");
  1736. return NULL;
  1737. }
  1738. /****************************************************************************
  1739. * Name: wm8904_audio_output
  1740. *
  1741. * Description:
  1742. * Initialize and configure the WM8904 device as an audio output device.
  1743. *
  1744. * Input Parameters:
  1745. * priv - A reference to the driver state structure
  1746. *
  1747. * Returned Value:
  1748. * None. No failures are detected.
  1749. *
  1750. ****************************************************************************/
  1751. static void wm8904_audio_output(FAR struct wm8904_dev_s *priv)
  1752. {
  1753. uint16_t regval;
  1754. /* Bias Control.
  1755. * Preserve undocumented default bit.WM8904_DUMMY
  1756. */
  1757. regval = WM8904_ISEL_HIGH | WM8904_BIAS_ENA | 0x0010;
  1758. wm8904_writereg(priv, WM8904_BIAS_CTRL, regval);
  1759. /* VMID Control */
  1760. regval = WM8904_VMID_BUF_ENA | WM8904_VMID_RES_NORMAL | WM8904_VMID_ENA;
  1761. wm8904_writereg(priv, WM8904_VMID_CTRL, regval);
  1762. /* Mic Bias Control 0 */
  1763. /* MICDET_ENA=1, MICBIAS_ENA=1 */
  1764. regval = WM8904_MICDET_ENA | WM8904_MICBIAS_ENA;
  1765. wm8904_writereg(priv, WM8904_MIC_BIAS_CTRL0, regval);
  1766. /* Mic Bias Control 1. */
  1767. wm8904_writereg(priv, WM8904_MIC_BIAS_CTRL1, 0xc000);
  1768. /* Power Management 0 */
  1769. regval = WM8904_INL_ENA | WM8904_INR_ENA;
  1770. wm8904_writereg(priv, WM8904_PM0, regval);
  1771. /* Power Management 2 */
  1772. regval = WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA;
  1773. wm8904_writereg(priv, WM8904_PM2, regval);
  1774. /* Power Management 6 */
  1775. /* DACL_ENA=1, DACR_ENA=1, ADCL_ENA=1, ADCR_ENA=1 */
  1776. regval = WM8904_DACL_ENA | WM8904_DACR_ENA | WM8904_ADCL_ENA | WM8904_ADCR_ENA;
  1777. wm8904_writereg(priv, WM8904_PM6, regval);
  1778. /* Clock Rates 0.
  1779. *
  1780. * This value sets TOCLK_RATE_DIV16=0, TOCLK_RATE_X4=0, and MCLK_DIV=0 while
  1781. * preserving the state of some undocumented bits (see wm8904.h).
  1782. *
  1783. * MCLK_DIV=0 : MCLK is is not divided by 2.
  1784. */
  1785. wm8904_writereg(priv, WM8904_CLKRATE0, 0x845e);
  1786. /* Clock Rates 1.
  1787. *
  1788. * Contains settings the control the sample rate.
  1789. */
  1790. /* Clock Rates 2
  1791. *
  1792. * Contains various controls. Some that are controlled here include:
  1793. *
  1794. * WM8904_MCLK_INV=0 : MCLK is not inverted
  1795. * WM8904_SYSCLK_SRC=1 : SYSCLK source is FLL
  1796. * WM8904_TOCLK_RATE=0 :
  1797. * WM8904_OPCLK_ENA=0 :
  1798. * WM8904_CLK_SYS_ENA=1 : SYSCLK is enabled
  1799. * WM8904_CLK_DSP_ENA=1 : DSP clock is enabled
  1800. * WM8904_TOCLK_ENA=0 :
  1801. */
  1802. regval = WM8904_SYSCLK_SRCFLL | WM8904_CLK_SYS_ENA | WM8904_CLK_DSP_ENA;
  1803. wm8904_writereg(priv, WM8904_CLKRATE2, regval);
  1804. /* Audio Interface 0.
  1805. *
  1806. * Reset value is:
  1807. * No DAC invert
  1808. * No volume boost
  1809. * No loopback
  1810. * Left/Right ADC/DAC channels output on Left/Right
  1811. * Companding options set by wm8904_setdatawidth()
  1812. */
  1813. wm8904_setdatawidth(priv);
  1814. /* Audio Interface 1.
  1815. *
  1816. * This value sets AIFADC_TDM=0, AIFADC_TDM_CHAN=0, BCLK_DIR=1 while preserving
  1817. * the state of some undocumented bits (see wm8904.h).
  1818. *
  1819. * Digital audio interface format : I2S
  1820. * Digital audio interface word length : 24
  1821. * AIF_LRCLK_INV=0 : LRCLK not inverted
  1822. * BCLK_DIR=1 : BCLK is an output (will clock I2S).
  1823. * AIF_BCLK_INV=0 : BCLK not inverted
  1824. * AIF_TRIS=0 : Outputs not tri-stated
  1825. * AIFADC_TDM_CHAN=0 : ADCDAT outputs data on slot 0
  1826. * AIFADC_TDM=0 : Normal ADCDAT operation
  1827. * AIFDAC_TDM_CHAN=0 : DACDAT data input on slot 0
  1828. * AIFDAC_TDM=0 : Normal DACDAT operation
  1829. * Bit 14: : Undocumented
  1830. */
  1831. regval = WM8904_AIF_FMT_I2S | WM8904_AIF_WL_24BITS | WM8904_BCLK_DIR | 0x4000;
  1832. wm8904_writereg(priv, WM8904_AIF1, regval);
  1833. /* Audio Interface 2.
  1834. *
  1835. * Holds GPIO clock divider and the SYSCLK divider needed to generate BCLK.
  1836. * This will get initialized by wm8904_setbitrate().
  1837. */
  1838. /* Audio Interface 3
  1839. *
  1840. * Set LRCLK as an output with rate = BCLK / (2*WM8904_FRAMELENn). This is
  1841. * a value that varies with bits per sample, n=8 or 16. Since I2S will send
  1842. * a word on each edge of LRCLK (after a delay), this essentially means that
  1843. * each audio frame is WM8904_FRAMELENn bits in length.
  1844. */
  1845. regval = WM8904_LRCLK_DIR | WM8904_LRCLK_RATE(2*WM8904_FRAMELEN16);
  1846. wm8904_writereg(priv, WM8904_AIF3, regval);
  1847. /* DAC Digital 1 */
  1848. wm8904_writereg(priv, WM8904_DAC_DIGI1, 0);
  1849. /* Analogue Left Input 0 */
  1850. /* Analogue Right Input 0 */
  1851. regval = WM8904_IN_VOL(5);
  1852. wm8904_writereg(priv, WM8904_ANA_LEFT_IN0, regval);
  1853. wm8904_writereg(priv, WM8904_ANA_RIGHT_IN0, regval);
  1854. /* Analogue Left Input 1 */
  1855. wm8904_writereg(priv, WM8904_ANA_LEFT_IN1, 0);
  1856. wm8904_writereg(priv, WM8904_ANA_RIGHT_IN1, 0);
  1857. /* Analogue OUT1 Left */
  1858. /* Analogue OUT1 Right */
  1859. wm8904_setvolume(priv, CONFIG_WM8904_INITVOLUME, true);
  1860. /* DC Servo 0 */
  1861. regval = WM8904_DCS_ENA_CHAN_1 | WM8904_DCS_ENA_CHAN_0;
  1862. wm8904_writereg(priv, WM8904_DC_SERVO0, regval);
  1863. /* Analogue HP 0 */
  1864. regval = WM8904_HPL_RMV_SHORT | WM8904_HPL_ENA_OUTP | WM8904_HPL_ENA_DLY | WM8904_HPL_ENA |
  1865. WM8904_HPR_RMV_SHORT | WM8904_HPR_ENA_OUTP | WM8904_HPR_ENA_DLY | WM8904_HPR_ENA;
  1866. wm8904_writereg(priv, WM8904_ANA_HP0, regval);
  1867. /* Charge Pump 0 */
  1868. wm8904_writereg(priv, WM8904_CHG_PUMP0, WM8904_CP_ENA);
  1869. /* Class W 0 */
  1870. regval = WM8904_CP_DYN_PWR | 0x0004;
  1871. wm8904_writereg(priv, WM8904_CLASS_W0, regval);
  1872. }
  1873. /****************************************************************************
  1874. * Name: wm8904_audio_input
  1875. *
  1876. * Description:
  1877. * Initialize and configure the WM8904 device as an audio output device
  1878. * (Right input only). wm8904_audio_output() must be called first, this
  1879. * function then modifies the configuration to support audio input.
  1880. *
  1881. * Input Parameters:
  1882. * priv - A reference to the driver state structure
  1883. *
  1884. * Returned Value:
  1885. * None. No failures are detected.
  1886. *
  1887. ****************************************************************************/
  1888. #if 0 /* Not used */
  1889. static void wm8904_audio_input(FAR struct wm8904_dev_s *priv)
  1890. {
  1891. /* Analogue Left Input 0 */
  1892. wm8904_writereg(priv, WM8904_ANA_LEFT_IN0, WM8904_INMUTE);
  1893. /* Analogue Right Input 0 */
  1894. wm8904_writereg(priv, WM8904_ANA_RIGHT_IN0, WM8904_IN_VOL(5));
  1895. /* Analogue Left Input 1 */
  1896. wm8904_writereg(priv, WM8904_ANA_LEFT_IN1, 0);
  1897. /* Analogue Right Input 1 */
  1898. wm8904_writereg(priv, WM8904_ANA_RIGHT_IN1, WM8904_IP_SEL_N_IN2L);
  1899. }
  1900. #endif
  1901. /****************************************************************************
  1902. * Name: wm8904_configure_ints
  1903. *
  1904. * Description:
  1905. * Configure the GPIO/IRQ interrupt
  1906. *
  1907. * Input Parameters:
  1908. * priv - A reference to the driver state structure
  1909. *
  1910. * Returned Value:
  1911. * None
  1912. *
  1913. ****************************************************************************/
  1914. #ifdef WM8904_USE_FFLOCK_INT
  1915. static void wm8904_configure_ints(FAR struct wm8904_dev_s *priv)
  1916. {
  1917. uint16_t regval;
  1918. /* Configure GPIO1 as an IRQ
  1919. *
  1920. * WM8904_GPIO1_PU=0 : No pull-up
  1921. * WM8904_GPIO1_PD=1 : Pulled-down
  1922. * WM8904_GPIO1_SEL_IRQ : Configured as IRQ
  1923. */
  1924. regval = (WM8904_GPIO1_SEL_IRQ | WM8904_GPIO1_PD);
  1925. wm8904_writereg(priv, WM8904_GPIO_CTRL1, regval);
  1926. /* Attach our handler to the GPIO1/IRQ interrupt */
  1927. WM8904_ATTACH(lower, wm8904_interrupt, priv);
  1928. /* Configure interrupts. wm8904_setbitrate() depends on FLL interrupts. */
  1929. wm8904_writereg(priv, WM8904_INT_STATUS, WM8904_ALL_INTS);
  1930. wm8904_writereg(priv, WM8904_INT_MASK, WM8904_ALL_INTS);
  1931. wm8904_writereg(priv, WM8904_INT_POL, 0);
  1932. wm8904_writereg(priv, WM8904_INT_DEBOUNCE, WM8904_ALL_INTS);
  1933. }
  1934. #endif
  1935. /****************************************************************************
  1936. * Name: wm8904_hw_reset
  1937. *
  1938. * Description:
  1939. * Reset and re-initialize the WM8904
  1940. *
  1941. * Input Parameters:
  1942. * priv - A reference to the driver state structure
  1943. *
  1944. * Returned Value:
  1945. * None
  1946. *
  1947. ****************************************************************************/
  1948. static void wm8904_hw_reset(FAR struct wm8904_dev_s *priv)
  1949. {
  1950. /* Put audio output back to its initial configuration */
  1951. priv->samprate = WM8904_DEFAULT_SAMPRATE;
  1952. priv->nchannels = WM8904_DEFAULT_NCHANNELS;
  1953. priv->bpsamp = WM8904_DEFAULT_BPSAMP;
  1954. #if !defined(CONFIG_AUDIO_EXCLUDE_VOLUME) && !defined(CONFIG_AUDIO_EXCLUDE_BALANCE)
  1955. priv->balance = b16HALF; /* Center balance */
  1956. #endif
  1957. /* Software reset. This puts all WM8904 registers back in their
  1958. * default state.
  1959. */
  1960. wm8904_writereg(priv, WM8904_SWRST, 0);
  1961. /* Configure the WM8904 hardware as an audio input device */
  1962. wm8904_audio_output(priv);
  1963. /* Configure interrupts */
  1964. wm8904_configure_ints(priv);
  1965. /* Configure the FLL and the LRCLK */
  1966. wm8904_setbitrate(priv);
  1967. wm8904_writereg(priv, WM8904_DUMMY, 0x55aa);
  1968. /* Dump some information and return the device instance */
  1969. wm8904_dump_registers(&priv->dev, "After configuration");
  1970. wm8904_clock_analysis(&priv->dev, "After configuration");
  1971. }
  1972. /****************************************************************************
  1973. * Public Functions
  1974. ****************************************************************************/
  1975. /****************************************************************************
  1976. * Name: wm8904_initialize
  1977. *
  1978. * Description:
  1979. * Initialize the WM8904 device.
  1980. *
  1981. * Input Parameters:
  1982. * i2c - An I2C driver instance
  1983. * i2s - An I2S driver instance
  1984. * lower - Persistent board configuration data
  1985. *
  1986. * Returned Value:
  1987. * A new lower half audio interface for the WM8904 device is returned on
  1988. * success; NULL is returned on failure.
  1989. *
  1990. ****************************************************************************/
  1991. FAR struct audio_lowerhalf_s *
  1992. wm8904_initialize(FAR struct i2c_master_s *i2c, FAR struct i2s_dev_s *i2s,
  1993. FAR const struct wm8904_lower_s *lower)
  1994. {
  1995. FAR struct wm8904_dev_s *priv;
  1996. uint16_t regval;
  1997. /* Sanity check */
  1998. DEBUGASSERT(i2c && i2s && lower);
  1999. /* Allocate a WM8904 device structure */
  2000. priv = (FAR struct wm8904_dev_s *)kmm_zalloc(sizeof(struct wm8904_dev_s));
  2001. if (priv)
  2002. {
  2003. /* Initialize the WM8904 device structure. Since we used kmm_zalloc,
  2004. * only the non-zero elements of the structure need to be initialized.
  2005. */
  2006. priv->dev.ops = &g_audioops;
  2007. priv->lower = lower;
  2008. priv->i2c = i2c;
  2009. priv->i2s = i2s;
  2010. nxsem_init(&priv->pendsem, 0, 1);
  2011. dq_init(&priv->pendq);
  2012. dq_init(&priv->doneq);
  2013. /* Software reset. This puts all WM8904 registers back in their
  2014. * default state.
  2015. */
  2016. wm8904_writereg(priv, WM8904_SWRST, 0);
  2017. wm8904_dump_registers(&priv->dev, "After reset");
  2018. /* Verify that WM8904 is present and available on this I2C */
  2019. regval = wm8904_readreg(priv, WM8904_ID);
  2020. if (regval != WM8904_SW_RST_DEV_ID1)
  2021. {
  2022. auderr("ERROR: WM8904 not found: ID=%04x\n", regval);
  2023. goto errout_with_dev;
  2024. }
  2025. /* Reset and reconfigure the WM8904 hardwaqre */
  2026. wm8904_hw_reset(priv);
  2027. return &priv->dev;
  2028. }
  2029. return NULL;
  2030. errout_with_dev:
  2031. nxsem_destroy(&priv->pendsem);
  2032. kmm_free(priv);
  2033. return NULL;
  2034. }