drv_wm8978.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-14 ZeroFree first implementation
  9. * 2019-07-28 Ernest perfect player, add record funciton and other APIs
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "drv_wm8978.h"
  14. #include <stdlib.h>
  15. /* Register Definitions */
  16. #define REG_SOFTWARE_RESET ((uint16_t)0)
  17. #define REG_POWER_MANAGEMENT1 ((uint16_t)(1 << 9))
  18. #define REG_POWER_MANAGEMENT2 ((uint16_t)(2 << 9))
  19. #define REG_POWER_MANAGEMENT3 ((uint16_t)(3 << 9))
  20. #define REG_AUDIO_INTERFACE ((uint16_t)(4 << 9))
  21. #define REG_COMPANDING ((uint16_t)(5 << 9))
  22. #define REG_CLOCK_GEN ((uint16_t)(6 << 9))
  23. #define REG_ADDITIONAL ((uint16_t)(7 << 9))
  24. #define REG_GPIO ((uint16_t)(8 << 9))
  25. #define REG_JACK_DETECT1 ((uint16_t)(9 << 9))
  26. #define REG_DAC ((uint16_t)(10 << 9))
  27. #define REG_LEFT_DAC_VOL ((uint16_t)(11 << 9))
  28. #define REG_RIGHT_DAC_VOL ((uint16_t)(12 << 9))
  29. #define REG_JACK_DETECT2 ((uint16_t)(13 << 9))
  30. #define REG_ADC ((uint16_t)(14 << 9))
  31. #define REG_LEFT_ADC_VOL ((uint16_t)(15 << 9))
  32. #define REG_RIGHT_ADC_VOL ((uint16_t)(16 << 9))
  33. #define REG_EQ1 ((uint16_t)(18 << 9))
  34. #define REG_EQ2 ((uint16_t)(19 << 9))
  35. #define REG_EQ3 ((uint16_t)(20 << 9))
  36. #define REG_EQ4 ((uint16_t)(21 << 9))
  37. #define REG_EQ5 ((uint16_t)(22 << 9))
  38. #define REG_DAC_LIMITER1 ((uint16_t)(24 << 9))
  39. #define REG_DAC_LIMITER2 ((uint16_t)(25 << 9))
  40. #define REG_NOTCH_FILTER1 ((uint16_t)(27 << 9))
  41. #define REG_NOTCH_FILTER2 ((uint16_t)(28 << 9))
  42. #define REG_NOTCH_FILTER3 ((uint16_t)(29 << 9))
  43. #define REG_NOTCH_FILTER4 ((uint16_t)(30 << 9))
  44. #define REG_ALC1 ((uint16_t)(32 << 9))
  45. #define REG_ALC2 ((uint16_t)(33 << 9))
  46. #define REG_ALC3 ((uint16_t)(34 << 9))
  47. #define REG_NOISE_GATE ((uint16_t)(35 << 9))
  48. #define REG_PLL_N ((uint16_t)(36 << 9))
  49. #define REG_PLL_K1 ((uint16_t)(37 << 9))
  50. #define REG_PLL_K2 ((uint16_t)(38 << 9))
  51. #define REG_PLL_K3 ((uint16_t)(39 << 9))
  52. #define REG_3D ((uint16_t)(41 << 9))
  53. #define REG_BEEP ((uint16_t)(43 << 9))
  54. #define REG_INPUT ((uint16_t)(44 << 9))
  55. #define REG_LEFT_PGA_GAIN ((uint16_t)(45 << 9))
  56. #define REG_RIGHT_PGA_GAIN ((uint16_t)(46 << 9))
  57. #define REG_LEFT_ADC_BOOST ((uint16_t)(47 << 9))
  58. #define REG_RIGHT_ADC_BOOST ((uint16_t)(48 << 9))
  59. #define REG_OUTPUT ((uint16_t)(49 << 9))
  60. #define REG_LEFT_MIXER ((uint16_t)(50 << 9))
  61. #define REG_RIGHT_MIXER ((uint16_t)(51 << 9))
  62. #define REG_LOUT1_VOL ((uint16_t)(52 << 9))
  63. #define REG_ROUT1_VOL ((uint16_t)(53 << 9))
  64. #define REG_LOUT2_VOL ((uint16_t)(54 << 9))
  65. #define REG_ROUT2_VOL ((uint16_t)(55 << 9))
  66. #define REG_OUT3_MIXER ((uint16_t)(56 << 9))
  67. #define REG_OUT4_MIXER ((uint16_t)(57 << 9))
  68. // R01 REG_POWER_MANAGEMENT1
  69. #define BUFDCOPEN (1 << 8)
  70. #define OUT4MIXEN (1 << 7)
  71. #define OUT3MIXEN (1 << 6)
  72. #define PLLEN (1 << 5)
  73. #define MICBEN (1 << 4)
  74. #define BIASEN (1 << 3)
  75. #define BUFIOEN (1 << 2)
  76. #define VMIDSEL_OFF (0)
  77. #define VMIDSEL_75K (1)
  78. #define VMIDSEL_300K (2)
  79. #define VMIDSEL_5K (3)
  80. // R02 REG_POWER_MANAGEMENT2
  81. #define ROUT1EN (1 << 8)
  82. #define LOUT1EN (1 << 7)
  83. #define SLEEP (1 << 6)
  84. #define BOOSTENR (1 << 5)
  85. #define BOOSTENL (1 << 4)
  86. #define INPPGAENR (1 << 3)
  87. #define INPPGAENL (1 << 2)
  88. #define ADCENR (1 << 1)
  89. #define ADCENL (1)
  90. // R03 REG_POWER_MANAGEMENT3
  91. #define OUT4EN (1 << 8)
  92. #define OUT3EN (1 << 7)
  93. #define LOUT2EN (1 << 6)
  94. #define ROUT2EN (1 << 5)
  95. #define RMIXEN (1 << 3)
  96. #define LMIXEN (1 << 2)
  97. #define DACENR (1 << 1)
  98. #define DACENL (1)
  99. // R04 REG_AUDIO_INTERFACE
  100. #define BCP_NORMAL (0)
  101. #define BCP_INVERTED (1 << 8)
  102. #define LRP_NORMAL (0)
  103. #define LRP_INVERTED (1 << 7)
  104. #define WL_16BITS (0)
  105. #define WL_20BITS (1 << 5)
  106. #define WL_24BITS (2 << 5) // Default value
  107. #define WL_32BITS (3 << 5)
  108. #define FMT_RIGHT_JUSTIFIED (0)
  109. #define FMT_LEFT_JUSTIFIED (1 << 3)
  110. #define FMT_I2S (2 << 3) // Default value
  111. #define FMT_PCM (3 << 3)
  112. #define DACLRSWAP (1 << 2)
  113. #define ADCLRSWAP (1 << 1)
  114. #define MONO (1)
  115. // R05 REG_COMPANDING
  116. #define WL8 (1 << 5)
  117. #define DAC_COMP_OFF (0) // Default value
  118. #define DAC_COMP_ULAW (2 << 3)
  119. #define DAC_COMP_ALAW (3 << 3)
  120. #define ADC_COMP_OFF (0) // Default value
  121. #define ADC_COMP_ULAW (2 << 1)
  122. #define ADC_COMP_ALAW (3 << 1)
  123. #define LOOPBACK (1)
  124. // R06 REG_CLOCK_GEN
  125. #define CLKSEL_MCLK (0)
  126. #define CLKSEL_PLL (1 << 8) // Default value
  127. #define MCLK_DIV1 (0)
  128. #define MCLK_DIV1_5 (1 << 5)
  129. #define MCLK_DIV2 (2 << 5) // Default value
  130. #define MCLK_DIV3 (3 << 5)
  131. #define MCLK_DIV4 (4 << 5)
  132. #define MCLK_DIV6 (5 << 5)
  133. #define MCLK_DIV8 (6 << 5)
  134. #define MCLK_DIV12 (7 << 5)
  135. #define BCLK_DIV1 (0) // Default value
  136. #define BCLK_DIV2 (1 << 2)
  137. #define BCLK_DIV4 (2 << 2)
  138. #define BCLK_DIV8 (3 << 2)
  139. #define BCLK_DIV16 (4 << 2)
  140. #define BCLK_DIV32 (5 << 2)
  141. #define MS (1)
  142. // R07 REG_ADDITIONAL
  143. #define WM_SR_48KHZ (0) // Default value
  144. #define WM_SR_32KHZ (1 << 1)
  145. #define WM_SR_24KHZ (2 << 1)
  146. #define WM_SR_16KHZ (3 << 1)
  147. #define WM_SR_12KHZ (4 << 1)
  148. #define WM_SR_8KHZ (5 << 1)
  149. #define SLOWCLKEN (1)
  150. // R08 REG_GPIO
  151. #define OPCLK_DIV1 (0) // Default value
  152. #define OPCLK_DIV2 (1 << 4)
  153. #define OPCLK_DIV3 (2 << 4)
  154. #define OPCLK_DIV4 (3 << 4)
  155. #define GPIO1POL_NONINVERTED (0) // Default value
  156. #define GPIO1POL_INVERTED (1 << 3)
  157. #define GPIO1SEL_INPUT (0) // Default value
  158. #define GPIO1SEL_TEMP_OK (2)
  159. #define GPIO1SEL_AMUTE_ACTIVE (3)
  160. #define GPIO1SEL_PLL_CLK_OP (4)
  161. #define GPIO1SEL_PLL_LOCK (5)
  162. #define GPIO1SEL_LOGIC1 (6)
  163. #define GPIO1SEL_LOGIC0 (7)
  164. // R09 REG_JACK_DETECT1
  165. #define JD_VMID_EN1 (1 << 8)
  166. #define JD_VMID_EN0 (1 << 7)
  167. #define JD_EN (1 << 6)
  168. #define JD_SEL_GPIO1 (0 << 4) // Default value
  169. #define JD_SEL_GPIO2 (1 << 4)
  170. #define JD_SEL_GPIO3 (2 << 4)
  171. // R10 REG_DAC
  172. #define SOFTMUTE (1 << 6)
  173. #define DACOSR128 (1 << 3)
  174. #define AMUTE (1 << 2)
  175. #define DACPOLR (1 << 1)
  176. #define DACPOLL (1)
  177. // R11 & R12 REG_LEFT_DAC_VOL & REG_RIGHT_DAC_VOL
  178. #define DACVU (1 << 8)
  179. #define DACVOL_POS (0)
  180. #define DACVOL_MASK (0xFF)
  181. // R13 REG_JACK_DETECT2
  182. #define JD_OUT4_EN1 (1 << 7)
  183. #define JD_OUT3_EN1 (1 << 6)
  184. #define JD_OUT2_EN1 (1 << 5)
  185. #define JD_OUT1_EN1 (1 << 4)
  186. #define JD_OUT4_EN0 (1 << 3)
  187. #define JD_OUT3_EN0 (1 << 2)
  188. #define JD_OUT2_EN0 (1 << 1)
  189. #define JD_OUT1_EN0 (1)
  190. // R14 REG_ADC
  191. #define HPFEN (1 << 8)
  192. #define HPFAPP (1 << 7)
  193. #define HPFCUT_POS (4)
  194. #define HPFCUT_MASK (7)
  195. #define HPFCUT_0 (0)
  196. #define HPFCUT_1 (1 << 4)
  197. #define HPFCUT_2 (2 << 4)
  198. #define HPFCUT_3 (3 << 4)
  199. #define HPFCUT_4 (4 << 4)
  200. #define HPFCUT_5 (5 << 4)
  201. #define HPFCUT_6 (6 << 4)
  202. #define HPFCUT_7 (7 << 4)
  203. #define ADCOSR128 (1 << 3)
  204. #define ADCRPOL (1 << 1)
  205. #define ADCLPOL (1)
  206. // R15 & R16 REG_LEFT_ADC_VOL & REG_RIGHT_ADC_VOL
  207. #define ADCVU (1 << 8)
  208. #define ADCVOL_POS (0)
  209. #define ADCVOL_MASK (0xFF)
  210. // R18 REG_EQ1
  211. #define EQ3DMODE_ADC (0)
  212. #define EQ3DMODE_DAC (1 << 8) // Default value
  213. #define EQ1C_80HZ (0)
  214. #define EQ1C_105HZ (1 << 5) // Default value
  215. #define EQ1C_135HZ (2 << 5)
  216. #define EQ1C_175HZ (3 << 5)
  217. // R19 REG_EQ2
  218. #define EQ2BW_NARROW (0) // Default value
  219. #define EQ2BW_WIDE (1 << 8)
  220. #define EQ2C_230HZ (0)
  221. #define EQ2C_300HZ (1 << 5) // Default value
  222. #define EQ2C_385HZ (2 << 5)
  223. #define EQ2C_500HZ (3 << 5)
  224. // R20 REG_EQ3
  225. #define EQ3BW_NARROW (0) // Default value
  226. #define EQ3BW_WIDE (1 << 8)
  227. #define EQ3C_650HZ (0)
  228. #define EQ3C_850HZ (1 << 5) // Default value
  229. #define EQ3C_1_1KHZ (2 << 5)
  230. #define EQ3C_1_4KHZ (3 << 5)
  231. // R21 REG_EQ4
  232. #define EQ4BW_NARROW (0) // Default value
  233. #define EQ4BW_WIDE (1 << 8)
  234. #define EQ4C_1_8KHZ (0)
  235. #define EQ4C_2_4KHZ (1 << 5) // Default value
  236. #define EQ4C_3_2KHZ (2 << 5)
  237. #define EQ4C_4_1KHZ (3 << 5)
  238. // R22 REG_EQ5
  239. #define EQ5C_5_3KHZ (0)
  240. #define EQ5C_6_9KHZ (1 << 5) // Default value
  241. #define EQ5C_9KHZ (2 << 5)
  242. #define EQ5C_11_7KHZ (3 << 5)
  243. // R18 - R22
  244. #define EQC_POS (5)
  245. #define EQC_MASK (3)
  246. #define EQG_POS (0)
  247. #define EQG_MASK (31)
  248. // R24 REG_DAC_LIMITER1
  249. #define LIMEN (1 << 8)
  250. #define LIMDCY_POS (4)
  251. #define LIMDCY_MASK (15)
  252. #define LIMDCY_750US (0)
  253. #define LIMDCY_1_5MS (1 << 4)
  254. #define LIMDCY_3MS (2 << 4)
  255. #define LIMDCY_6MS (3 << 4) // Default value
  256. #define LIMDCY_12MS (4 << 4)
  257. #define LIMDCY_24MS (5 << 4)
  258. #define LIMDCY_48MS (6 << 4)
  259. #define LIMDCY_96MS (7 << 4)
  260. #define LIMDCY_192MS (8 << 4)
  261. #define LIMDCY_384MS (9 << 4)
  262. #define LIMDCY_768MS (10 << 4)
  263. #define LIMATK_POS (0)
  264. #define LIMATK_MASK (15)
  265. #define LIMATK_94US (0)
  266. #define LIMATK_188US (1)
  267. #define LIMATK_375US (2) // Default value
  268. #define LIMATK_750US (3)
  269. #define LIMATK_1_5MS (4)
  270. #define LIMATK_3MS (5)
  271. #define LIMATK_6MS (6)
  272. #define LIMATK_12MS (7)
  273. #define LIMATK_24MS (8)
  274. #define LIMATK_48MS (9)
  275. #define LIMATK_96MS (10)
  276. #define LIMATK_192MS (11)
  277. // R25 REG_DAC_LIMITER2
  278. #define LIMLVL_POS (4)
  279. #define LIMLVL_MASK (7)
  280. #define LIMLVL_N1DB (0) // Default value
  281. #define LIMLVL_N2DB (1 << 4)
  282. #define LIMLVL_N3DB (2 << 4)
  283. #define LIMLVL_N4DB (3 << 4)
  284. #define LIMLVL_N5DB (4 << 4)
  285. #define LIMLVL_N6DB (5 << 4)
  286. #define LIMBOOST_POS (0)
  287. #define LIMBOOST_MASK (15)
  288. #define LIMBOOST_0DB (0)
  289. #define LIMBOOST_1DB (1)
  290. #define LIMBOOST_2DB (2)
  291. #define LIMBOOST_3DB (3)
  292. #define LIMBOOST_4DB (4)
  293. #define LIMBOOST_5DB (5)
  294. #define LIMBOOST_6DB (6)
  295. #define LIMBOOST_7DB (7)
  296. #define LIMBOOST_8DB (8)
  297. #define LIMBOOST_9DB (9)
  298. #define LIMBOOST_10DB (10)
  299. #define LIMBOOST_11DB (11)
  300. #define LIMBOOST_12DB (12)
  301. // R27 - R30 REG_NOTCH_FILTER1 - REG_NOTCH_FILTER4
  302. #define NFU (1 << 8)
  303. #define NFEN (1 << 7)
  304. #define NFA_POS (0)
  305. #define NFA_MASK (127)
  306. // R32 REG_ALC1
  307. #define ALCSEL_OFF (0) // Default value
  308. #define ALCSEL_RIGHT_ONLY (1 << 7)
  309. #define ALCSEL_LEFT_ONLY (2 << 7)
  310. #define ALCSEL_BOTH_ON (3 << 7)
  311. #define ALCMAXGAIN_POS (3)
  312. #define ALCMAXGAIN_MASK (7)
  313. #define ALCMAXGAIN_N6_75DB (0)
  314. #define ALCMAXGAIN_N0_75DB (1 << 3)
  315. #define ALCMAXGAIN_5_25DB (2 << 3)
  316. #define ALCMAXGAIN_11_25DB (3 << 3)
  317. #define ALCMAXGAIN_17_25DB (4 << 3)
  318. #define ALCMAXGAIN_23_25DB (5 << 3)
  319. #define ALCMAXGAIN_29_25DB (6 << 3)
  320. #define ALCMAXGAIN_35_25DB (7 << 3) // Default value
  321. #define ALCMINGAIN_POS (0)
  322. #define ALCMINGAIN_MASK (7)
  323. #define ALCMINGAIN_N12DB (0) // Default value
  324. #define ALCMINGAIN_N6DB (1)
  325. #define ALCMINGAIN_0DB (2)
  326. #define ALCMINGAIN_6DB (3)
  327. #define ALCMINGAIN_12DB (4)
  328. #define ALCMINGAIN_18DB (5)
  329. #define ALCMINGAIN_24DB (6)
  330. #define ALCMINGAIN_30DB (7)
  331. // R33 REG_ALC2
  332. #define ALCHLD_POS (4)
  333. #define ALCHLD_MASK (15)
  334. #define ALCHLD_0MS (0) // Default value
  335. #define ALCHLD_2_67MS (1 << 4)
  336. #define ALCHLD_5_33MS (2 << 4)
  337. #define ALCHLD_10_67MS (3 << 4)
  338. #define ALCHLD_21_33MS (4 << 4)
  339. #define ALCHLD_42_67MS (5 << 4)
  340. #define ALCHLD_85_33MS (6 << 4)
  341. #define ALCHLD_170_67MS (7 << 4)
  342. #define ALCHLD_341_33MS (8 << 4)
  343. #define ALCHLD_682_67MS (9 << 4)
  344. #define ALCHLD_1_36S (10 << 4)
  345. #define ALCLVL_POS (0)
  346. #define ALCLVL_MASK (15)
  347. #define ALCLVL_N22_5DBFS (0)
  348. #define ALCLVL_N21DBFS (1)
  349. #define ALCLVL_N19_5DBFS (2)
  350. #define ALCLVL_N18DBFS (3)
  351. #define ALCLVL_N16_5DBFS (4)
  352. #define ALCLVL_N15DBFS (5)
  353. #define ALCLVL_N13_5DBFS (6)
  354. #define ALCLVL_N12DBFS (7)
  355. #define ALCLVL_N10_5DBFS (8)
  356. #define ALCLVL_N9DBFS (9)
  357. #define ALCLVL_N7_5DBFS (10)
  358. #define ALCLVL_N6DBFS (11) // Default value
  359. #define ALCLVL_N4_5DBFS (12)
  360. #define ALCLVL_N3DBFS (13)
  361. #define ALCLVL_N1_5DBFS (14)
  362. // R34 REG_ALC3
  363. #define ALCMODE_ALC (0) // Default value
  364. #define ALCMODE_LIMITER (1 << 8)
  365. #define ALCDCY_POS (4)
  366. #define ALCDCY_MASK (15)
  367. #define ALCDCY_0 (0)
  368. #define ALCDCY_1 (1 << 4)
  369. #define ALCDCY_2 (2 << 4)
  370. #define ALCDCY_3 (3 << 4) // Default value
  371. #define ALCDCY_4 (4 << 4)
  372. #define ALCDCY_5 (5 << 4)
  373. #define ALCDCY_6 (6 << 4)
  374. #define ALCDCY_7 (7 << 4)
  375. #define ALCDCY_8 (8 << 4)
  376. #define ALCDCY_9 (9 << 4)
  377. #define ALCDCY_10 (10 << 4)
  378. #define ALCATK_POS (0)
  379. #define ALCATK_MASK (15)
  380. #define ALCATK_0 (0)
  381. #define ALCATK_1 (1)
  382. #define ALCATK_2 (2) // Default value
  383. #define ALCATK_3 (3)
  384. #define ALCATK_4 (4)
  385. #define ALCATK_5 (5)
  386. #define ALCATK_6 (6)
  387. #define ALCATK_7 (7)
  388. #define ALCATK_8 (8)
  389. #define ALCATK_9 (9)
  390. #define ALCATK_10 (10)
  391. // R35 REG_NOISE_GATE
  392. #define NGEN (1 << 3)
  393. #define NGTH_POS (0)
  394. #define NGTH_MASK (7)
  395. #define NGTH_N39DB (0) // Default value
  396. #define NGTH_N45DB (1)
  397. #define NGTH_N51DB (2)
  398. #define NGTH_N57DB (3)
  399. #define NGTH_N63DB (4)
  400. #define NGTH_N69DB (5)
  401. #define NGTH_N75DB (6)
  402. #define NGTH_N81DB (7)
  403. // R36 REG_PLL_N
  404. #define PLLPRESCALE (1 << 4)
  405. #define PLLN_POS (0)
  406. #define PLLN_MASK (15)
  407. // R37 - R39 REG_PLL_K1 - REG_PLL_K3
  408. #define PLLK1_POS (0)
  409. #define PLLK1_MASK (63)
  410. #define PLLK2_POS (0)
  411. #define PLLK2_MASK (511)
  412. #define PLLK3_POS (0)
  413. #define PLLK3_MASK (511)
  414. // R41 REG_3D
  415. #define DEPTH3D_POS (0)
  416. #define DEPTH3D_MASK (15)
  417. #define DEPTH3D_0 (0) // Default value
  418. #define DEPTH3D_6_67 (1)
  419. #define DEPTH3D_13_33 (2)
  420. #define DEPTH3D_20 (3)
  421. #define DEPTH3D_26_67 (4)
  422. #define DEPTH3D_33_33 (5)
  423. #define DEPTH3D_40 (6)
  424. #define DEPTH3D_46_67 (7)
  425. #define DEPTH3D_53_33 (8)
  426. #define DEPTH3D_60 (9)
  427. #define DEPTH3D_66_67 (10)
  428. #define DEPTH3D_73_33 (11)
  429. #define DEPTH3D_80 (12)
  430. #define DEPTH3D_86_67 (13)
  431. #define DEPTH3D_93_33 (14)
  432. #define DEPTH3D_100 (15)
  433. // R43 REG_BEEP
  434. #define MUTERPGA2INV (1 << 5)
  435. #define INVROUT2 (1 << 4)
  436. #define BEEPVOL_POS (1)
  437. #define BEEPVOL_MASK (7)
  438. #define BEEPVOL_N15DB (0)
  439. #define BEEPVOL_N12DB (1 << 1)
  440. #define BEEPVOL_N9DB (2 << 1)
  441. #define BEEPVOL_N6DB (3 << 1)
  442. #define BEEPVOL_N3DB (4 << 1)
  443. #define BEEPVOL_0DB (5 << 1)
  444. #define BEEPVOL_3DB (6 << 1)
  445. #define BEEPVOL_6DB (7 << 1)
  446. #define BEEPEN (1)
  447. // R44 REG_INPUT
  448. #define MBVSEL_0_9AVDD (0) // Default value
  449. #define MBVSEL_0_65AVDD (1 << 8)
  450. #define R2_2INPVGA (1 << 6)
  451. #define RIN2INPVGA (1 << 5) // Default value
  452. #define RIP2INPVGA (1 << 4) // Default value
  453. #define L2_2INPVGA (1 << 2)
  454. #define LIN2INPVGA (1 << 1) // Default value
  455. #define LIP2INPVGA (1) // Default value
  456. // R45 REG_LEFT_PGA_GAIN
  457. #define INPPGAUPDATE (1 << 8)
  458. #define INPPGAZCL (1 << 7)
  459. #define INPPGAMUTEL (1 << 6)
  460. // R46 REG_RIGHT_PGA_GAIN
  461. #define INPPGAZCR (1 << 7)
  462. #define INPPGAMUTER (1 << 6)
  463. // R45 - R46
  464. #define INPPGAVOL_POS (0)
  465. #define INPPGAVOL_MASK (63)
  466. // R47 REG_LEFT_ADC_BOOST
  467. #define PGABOOSTL (1 << 8) // Default value
  468. #define L2_2BOOSTVOL_POS (4)
  469. #define L2_2BOOSTVOL_MASK (7)
  470. #define L2_2BOOSTVOL_DISABLED (0) // Default value
  471. #define L2_2BOOSTVOL_N12DB (1 << 4)
  472. #define L2_2BOOSTVOL_N9DB (2 << 4)
  473. #define L2_2BOOSTVOL_N6DB (3 << 4)
  474. #define L2_2BOOSTVOL_N3DB (4 << 4)
  475. #define L2_2BOOSTVOL_0DB (5 << 4)
  476. #define L2_2BOOSTVOL_3DB (6 << 4)
  477. #define L2_2BOOSTVOL_6DB (7 << 4)
  478. #define AUXL2BOOSTVOL_POS (0)
  479. #define AUXL2BOOSTVOL_MASK (7)
  480. #define AUXL2BOOSTVOL_DISABLED (0) // Default value
  481. #define AUXL2BOOSTVOL_N12DB (1)
  482. #define AUXL2BOOSTVOL_N9DB (2)
  483. #define AUXL2BOOSTVOL_N6DB (3)
  484. #define AUXL2BOOSTVOL_N3DB (4)
  485. #define AUXL2BOOSTVOL_0DB (5)
  486. #define AUXL2BOOSTVOL_3DB (6)
  487. #define AUXL2BOOSTVOL_6DB (7)
  488. // R48 REG_RIGHT_ADC_BOOST
  489. #define PGABOOSTR (1 << 8) // Default value
  490. #define R2_2BOOSTVOL_POS (4)
  491. #define R2_2BOOSTVOL_MASK (7)
  492. #define R2_2BOOSTVOL_DISABLED (0) // Default value
  493. #define R2_2BOOSTVOL_N12DB (1 << 4)
  494. #define R2_2BOOSTVOL_N9DB (2 << 4)
  495. #define R2_2BOOSTVOL_N6DB (3 << 4)
  496. #define R2_2BOOSTVOL_N3DB (4 << 4)
  497. #define R2_2BOOSTVOL_0DB (5 << 4)
  498. #define R2_2BOOSTVOL_3DB (6 << 4)
  499. #define R2_2BOOSTVOL_6DB (7 << 4)
  500. #define AUXR2BOOSTVOL_POS (0)
  501. #define AUXR2BOOSTVOL_MASK (7)
  502. #define AUXR2BOOSTVOL_DISABLED (0) // Default value
  503. #define AUXR2BOOSTVOL_N12DB (1)
  504. #define AUXR2BOOSTVOL_N9DB (2)
  505. #define AUXR2BOOSTVOL_N6DB (3)
  506. #define AUXR2BOOSTVOL_N3DB (4)
  507. #define AUXR2BOOSTVOL_0DB (5)
  508. #define AUXR2BOOSTVOL_3DB (6)
  509. #define AUXR2BOOSTVOL_6DB (7)
  510. // R49 REG_OUTPUT
  511. #define DACL2RMIX (1 << 6)
  512. #define DACR2LMIX (1 << 5)
  513. #define OUT4BOOST (1 << 4)
  514. #define OUT3BOOST (1 << 3)
  515. #define SPKBOOST (1 << 2)
  516. #define TSDEN (1 << 1)
  517. #define VROI (1)
  518. // R50 REG_LEFT_MIXER
  519. #define AUXLMIXVOL_POS (6)
  520. #define AUXLMIXVOL_MASK (7)
  521. #define AUXLMIXVOL_N15DB (0) // Default value
  522. #define AUXLMIXVOL_N12DB (1 << 6)
  523. #define AUXLMIXVOL_N9DB (2 << 6)
  524. #define AUXLMIXVOL_N6DB (3 << 6)
  525. #define AUXLMIXVOL_N3DB (4 << 6)
  526. #define AUXLMIXVOL_0DB (5 << 6)
  527. #define AUXLMIXVOL_3DB (6 << 6)
  528. #define AUXLMIXVOL_6DB (7 << 6)
  529. #define AUXL2LMIX (1 << 5)
  530. #define BYPLMIXVOL_POS (2)
  531. #define BYPLMIXVOL_MASK (7)
  532. #define BYPLMIXVOL_N15DB (0) // Default value
  533. #define BYPLMIXVOL_N12DB (1 << 2)
  534. #define BYPLMIXVOL_N9DB (2 << 2)
  535. #define BYPLMIXVOL_N6DB (3 << 2)
  536. #define BYPLMIXVOL_N3DB (4 << 2)
  537. #define BYPLMIXVOL_0DB (5 << 2)
  538. #define BYPLMIXVOL_3DB (6 << 2)
  539. #define BYPLMIXVOL_6DB (7 << 2)
  540. #define BYPL2LMIX (1 << 1)
  541. #define DACL2LMIX (1)
  542. // R51 REG_RIGHT_MIXER
  543. #define AUXRMIXVOL_POS (6)
  544. #define AUXRMIXVOL_MASK (7)
  545. #define AUXRMIXVOL_N15DB (0) // Default value
  546. #define AUXRMIXVOL_N12DB (1 << 6)
  547. #define AUXRMIXVOL_N9DB (2 << 6)
  548. #define AUXRMIXVOL_N6DB (3 << 6)
  549. #define AUXRMIXVOL_N3DB (4 << 6)
  550. #define AUXRMIXVOL_0DB (5 << 6)
  551. #define AUXRMIXVOL_3DB (6 << 6)
  552. #define AUXRMIXVOL_6DB (7 << 6)
  553. #define AUXR2RMIX (1 << 5)
  554. #define BYPRMIXVOL_POS (2)
  555. #define BYPRMIXVOL_MASK (7)
  556. #define BYPRMIXVOL_N15DB (0) // Default value
  557. #define BYPRMIXVOL_N12DB (1 << 2)
  558. #define BYPRMIXVOL_N9DB (2 << 2)
  559. #define BYPRMIXVOL_N6DB (3 << 2)
  560. #define BYPRMIXVOL_N3DB (4 << 2)
  561. #define BYPRMIXVOL_0DB (5 << 2)
  562. #define BYPRMIXVOL_3DB (6 << 2)
  563. #define BYPRMIXVOL_6DB (7 << 2)
  564. #define BYPR2RMIX (1 << 1)
  565. #define DACR2RMIX (1)
  566. // R52 - R55 REG_LOUT1_VOL - REG_ROUT2_VOL
  567. #define HPVU (1 << 8)
  568. #define SPKVU (1 << 8)
  569. #define LOUT1ZC (1 << 7)
  570. #define LOUT1MUTE (1 << 6)
  571. #define ROUT1ZC (1 << 7)
  572. #define ROUT1MUTE (1 << 6)
  573. #define LOUT2ZC (1 << 7)
  574. #define LOUT2MUTE (1 << 6)
  575. #define ROUT2ZC (1 << 7)
  576. #define ROUT2MUTE (1 << 6)
  577. #define VOL_POS (0)
  578. #define VOL_MASK (63)
  579. // R56 REG_OUT3_MIXER
  580. #define OUT3MUTE (1 << 6)
  581. #define OUT4_2OUT3 (1 << 3)
  582. #define BYPL2OUT3 (1 << 2)
  583. #define LMIX2OUT3 (1 << 1)
  584. #define LDAC2OUT3 (1)
  585. // R57 REG_OUT4_MIXER
  586. #define OUT4MUTE (1 << 6)
  587. #define HALFSIG (1 << 5)
  588. #define LMIX2OUT4 (1 << 4)
  589. #define LDAC2OUT4 (1 << 3)
  590. #define BYPR2OUT4 (1 << 2)
  591. #define RMIX2OUT4 (1 << 1)
  592. #define RDAC2OUT4 (1)
  593. static rt_uint16_t wm8978_regval_tbl[58] =
  594. {
  595. 0X0000, 0X0000, 0X0000, 0X0000, 0X0050, 0X0000, 0X0140, 0X0000,
  596. 0X0000, 0X0000, 0X0000, 0X00FF, 0X00FF, 0X0000, 0X0100, 0X00FF,
  597. 0X00FF, 0X0000, 0X012C, 0X002C, 0X002C, 0X002C, 0X002C, 0X0000,
  598. 0X0032, 0X0000, 0X0000, 0X0000, 0X0000, 0X0000, 0X0000, 0X0000,
  599. 0X0038, 0X000B, 0X0032, 0X0000, 0X0008, 0X000C, 0X0093, 0X00E9,
  600. 0X0000, 0X0000, 0X0000, 0X0000, 0X0003, 0X0010, 0X0010, 0X0100,
  601. 0X0100, 0X0002, 0X0001, 0X0001, 0X0039, 0X0039, 0X0039, 0X0039,
  602. 0X0001, 0X0001
  603. };
  604. static void wm8978_write_reg(struct rt_i2c_bus_device *dev, rt_uint16_t s_data)
  605. {
  606. struct rt_i2c_msg msg;
  607. rt_uint8_t send_buffer[2];
  608. RT_ASSERT(dev != RT_NULL);
  609. /* store temp */
  610. wm8978_regval_tbl[s_data >> 9] = s_data & 0x1FF;
  611. send_buffer[0] = (rt_uint8_t)(s_data >> 8);
  612. send_buffer[1] = (rt_uint8_t)(s_data);
  613. msg.addr = 0x1A;
  614. msg.flags = RT_I2C_WR;
  615. msg.len = 2;
  616. msg.buf = send_buffer;
  617. rt_i2c_transfer(dev, &msg, 1);
  618. }
  619. static rt_uint16_t wm8978_read_reg(struct rt_i2c_bus_device *dev, rt_uint16_t reg)
  620. {
  621. return wm8978_regval_tbl[reg >> 9];
  622. }
  623. /**
  624. * @brief Init WM8978 Codec device.
  625. * @param dev: I2C device handle
  626. * @retval RT_EOK if correct communication, else wrong communication
  627. */
  628. void wm8978_player_start(struct rt_i2c_bus_device *dev)
  629. {
  630. wm8978_reset(dev);
  631. /* 1.5x boost power up sequence,Mute all outputs. */
  632. wm8978_write_reg(dev, REG_LOUT1_VOL | LOUT1MUTE);
  633. wm8978_write_reg(dev, REG_ROUT1_VOL | ROUT1MUTE);
  634. wm8978_write_reg(dev, REG_LOUT2_VOL | LOUT2MUTE);
  635. wm8978_write_reg(dev, REG_ROUT2_VOL | ROUT2MUTE);
  636. /* Enable unused output chosen from L/ROUT2, OUT3 or OUT4. */
  637. wm8978_write_reg(dev, REG_POWER_MANAGEMENT3 | OUT4EN);
  638. /* Set BUFDCOPEN=1, BIASEN=1 and BUFIOEN=1 in register R1 */
  639. wm8978_write_reg(dev, REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | BIASEN);
  640. /* Set SPKBOOST=1 in register R49. */
  641. wm8978_write_reg(dev, REG_OUTPUT | SPKBOOST);
  642. /* Set VMIDSEL[1:0] to required value in register R1. */
  643. wm8978_write_reg(dev, REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K);
  644. /* Set L/RMIXEN=1 and DACENL/R=1 in register R3.*/
  645. wm8978_write_reg(dev, REG_POWER_MANAGEMENT3 | LMIXEN | RMIXEN | DACENL | DACENR);
  646. /* Set BIASEN=1 in register R1. */
  647. wm8978_write_reg(dev, REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K);
  648. /* Set L/ROUT2EN=1 in register R3. */
  649. wm8978_write_reg(dev, REG_POWER_MANAGEMENT3 | LMIXEN | RMIXEN | DACENL | DACENR | LOUT2EN | ROUT2EN);
  650. /* Enable other outputs as required. */
  651. wm8978_write_reg(dev, REG_POWER_MANAGEMENT2 | LOUT1EN | ROUT1EN | BOOSTENL | BOOSTENR | INPPGAENL | INPPGAENR);
  652. wm8978_write_reg(dev, REG_POWER_MANAGEMENT2 | LOUT1EN | ROUT1EN | BOOSTENL | BOOSTENR | INPPGAENL | INPPGAENR | ADCENL | ADCENR);
  653. /* Digital inferface setup. */
  654. wm8978_write_reg(dev, REG_AUDIO_INTERFACE | BCP_NORMAL | LRP_NORMAL | WL_16BITS | FMT_I2S);
  655. wm8978_write_reg(dev, REG_ADDITIONAL | WM_SR_8KHZ);
  656. wm8978_write_reg(dev, REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K | MICBEN | BIASEN);
  657. wm8978_write_reg(dev, REG_CLOCK_GEN | CLKSEL_MCLK | MCLK_DIV1);
  658. /* Enable DAC 128x oversampling. */
  659. wm8978_write_reg(dev, REG_DAC | DACOSR128);
  660. /* Set LOUT2/ROUT2 in BTL operation. */
  661. wm8978_write_reg(dev, REG_BEEP | INVROUT2);
  662. wm8978_DAC_enabled(dev, 1);
  663. /* Set output volume. */
  664. wm8978_set_volume(dev, 50);
  665. }
  666. void wm8978_record_start(struct rt_i2c_bus_device *dev)
  667. {
  668. wm8978_write_reg(dev, REG_POWER_MANAGEMENT1 | MICBEN | BIASEN | VMIDSEL_5K);
  669. wm8978_write_reg(dev, REG_POWER_MANAGEMENT2 | ROUT1EN | LOUT1EN | BOOSTENR | BOOSTENL);
  670. wm8978_write_reg(dev, REG_POWER_MANAGEMENT3 | LOUT2EN | ROUT2EN | RMIXEN | LMIXEN);
  671. /* mclk be supplied by outside */
  672. wm8978_write_reg(dev, REG_CLOCK_GEN);
  673. wm8978_write_reg(dev, (REG_BEEP | BEEPVOL_N3DB));
  674. wm8978_write_reg(dev, (REG_LEFT_ADC_BOOST | PGABOOSTL));
  675. wm8978_write_reg(dev, (REG_RIGHT_ADC_BOOST | PGABOOSTR));
  676. wm8978_write_reg(dev, (REG_OUTPUT | TSDEN | SPKBOOST));
  677. wm8978_write_reg(dev, (REG_DAC | RMIXEN));
  678. wm8978_write_reg(dev, (REG_ADC | ADCOSR128));
  679. wm8978_ADC_enabled(dev, 1);
  680. wm8978_DAC_enabled(dev, 0);
  681. wm8978_mic_enabled(dev, 1);
  682. wm8978_linein_enabled(dev, 1);
  683. wm8978_aux_enabled(dev, 0);
  684. wm8978_output_set(dev, 0, 0);
  685. wm8978_aux_gain(dev, 5);
  686. wm8978_mic_gain(dev, 50);
  687. }
  688. rt_err_t wm8978_init(struct rt_i2c_bus_device *dev)
  689. {
  690. wm8978_reset(dev);
  691. wm8978_write_reg(dev, REG_POWER_MANAGEMENT1 | MICBEN | BIASEN | VMIDSEL_5K);
  692. wm8978_write_reg(dev, REG_POWER_MANAGEMENT2 | ROUT1EN | LOUT1EN | BOOSTENR | BOOSTENL);
  693. wm8978_write_reg(dev, REG_POWER_MANAGEMENT3 | LOUT2EN | ROUT2EN | RMIXEN | LMIXEN);
  694. /* mclk be supplied by outside */
  695. wm8978_write_reg(dev, REG_CLOCK_GEN);
  696. wm8978_write_reg(dev, (REG_BEEP | BEEPVOL_N3DB));
  697. wm8978_write_reg(dev, (REG_LEFT_ADC_BOOST | PGABOOSTL));
  698. wm8978_write_reg(dev, (REG_RIGHT_ADC_BOOST | PGABOOSTR));
  699. wm8978_write_reg(dev, (REG_OUTPUT | TSDEN | SPKBOOST));
  700. wm8978_write_reg(dev, (REG_DAC | RMIXEN));
  701. wm8978_write_reg(dev, (REG_ADC | ADCOSR128));
  702. wm8978_interface_cfg(dev, I2S_FOMAT_SELECT, 16);
  703. wm8978_mic_enabled(dev, 0);
  704. return RT_EOK;
  705. }
  706. void wm8978_DAC_enabled(struct rt_i2c_bus_device *dev, rt_bool_t bool)
  707. {
  708. rt_uint16_t value;
  709. value = wm8978_read_reg(dev, REG_POWER_MANAGEMENT3);
  710. bool ? (value |= 3) : (value &= ~3);
  711. wm8978_write_reg(dev, REG_POWER_MANAGEMENT3 | value);
  712. }
  713. void wm8978_ADC_enabled(struct rt_i2c_bus_device *dev, rt_bool_t bool)
  714. {
  715. rt_uint16_t value;
  716. value = wm8978_read_reg(dev, REG_POWER_MANAGEMENT2);
  717. bool ? (value |= 3) : (value &= ~3);
  718. wm8978_write_reg(dev, REG_POWER_MANAGEMENT2 | value);
  719. }
  720. void wm8978_mic_enabled(struct rt_i2c_bus_device *dev, rt_bool_t bool)
  721. {
  722. rt_uint16_t value;
  723. value = wm8978_read_reg(dev, REG_POWER_MANAGEMENT2);
  724. bool ? (value |= 3 << 2) : (value &= ~(3 << 2));
  725. wm8978_write_reg(dev, REG_POWER_MANAGEMENT2 | value);
  726. value = wm8978_read_reg(dev, REG_INPUT);
  727. bool ? (value |= 3 << 4 | 3) : (value &= ~(3 << 4 | 3));
  728. wm8978_write_reg(dev, REG_INPUT | value);
  729. }
  730. void wm8978_linein_gain(struct rt_i2c_bus_device *dev, rt_uint8_t value)
  731. {
  732. rt_uint16_t regval;
  733. value &= 0x7;
  734. /* set left boost */
  735. regval = wm8978_read_reg(dev, REG_LEFT_ADC_BOOST);
  736. regval &= ~(7 << 4);
  737. wm8978_write_reg(dev, REG_LEFT_ADC_BOOST | regval | value << 4);
  738. /* set right boost */
  739. regval = wm8978_read_reg(dev, REG_RIGHT_ADC_BOOST);
  740. regval &= ~(7 << 4);
  741. wm8978_write_reg(dev, REG_RIGHT_ADC_BOOST | regval | value << 4);
  742. }
  743. void wm8978_aux_gain(struct rt_i2c_bus_device *dev, rt_uint8_t value)
  744. {
  745. rt_uint16_t regval;
  746. value &= 0x7;
  747. /* set left boost */
  748. regval = wm8978_read_reg(dev, REG_LEFT_ADC_BOOST);
  749. regval &= ~(7 << 0);
  750. wm8978_write_reg(dev, REG_LEFT_ADC_BOOST | regval | value << 0);
  751. /* set right boost */
  752. regval = wm8978_read_reg(dev, REG_RIGHT_ADC_BOOST);
  753. regval &= ~(7 << 0);
  754. wm8978_write_reg(dev, REG_RIGHT_ADC_BOOST | regval | value << 0);
  755. }
  756. void wm8978_mic_gain(struct rt_i2c_bus_device *dev, rt_uint8_t gain)
  757. {
  758. gain &= 0X3F;
  759. wm8978_write_reg(dev, REG_LEFT_PGA_GAIN | gain);
  760. wm8978_write_reg(dev, REG_RIGHT_PGA_GAIN | gain | 1 << 8);
  761. }
  762. void wm8978_linein_enabled(struct rt_i2c_bus_device *dev, rt_bool_t bool)
  763. {
  764. wm8978_linein_gain(dev, (bool ? L2_2BOOSTVOL_0DB : 0));
  765. }
  766. void wm8978_aux_enabled(struct rt_i2c_bus_device *dev, rt_bool_t bool)
  767. {
  768. wm8978_linein_gain(dev, (bool ? AUXL2BOOSTVOL_6DB : 0));
  769. }
  770. void wm8978_output_set(struct rt_i2c_bus_device *dev, rt_bool_t dacen, rt_bool_t bypass)
  771. {
  772. rt_uint16_t regval = 0;
  773. if (dacen)
  774. {
  775. regval |= 1 << 0;
  776. }
  777. if (bypass)
  778. {
  779. regval |= 1 << 1;
  780. regval |= 5 << 2;
  781. }
  782. wm8978_write_reg(dev, REG_LEFT_MIXER | regval);
  783. wm8978_write_reg(dev, REG_RIGHT_MIXER | regval);
  784. }
  785. void wm8978_hpvol_set(struct rt_i2c_bus_device *dev, rt_uint8_t volume)
  786. {
  787. volume &= 0X3F;
  788. if (volume == 0)
  789. {
  790. volume |= 1 << 6;//mute mode
  791. }
  792. wm8978_write_reg(dev, REG_LOUT1_VOL | volume);
  793. wm8978_write_reg(dev, REG_ROUT1_VOL | volume | (1 << 8));
  794. }
  795. void wm8978_spkvol_set(struct rt_i2c_bus_device *dev, rt_uint8_t volume)
  796. {
  797. volume &= 0X3F;
  798. if (volume == 0)
  799. {
  800. volume |= 1 << 6;//mute mode
  801. }
  802. wm8978_write_reg(dev, REG_LOUT2_VOL | volume);
  803. wm8978_write_reg(dev, REG_ROUT2_VOL | volume | (1 << 8));
  804. }
  805. /**
  806. * @brief Set WM8978 DAC volume level.
  807. * @param dev: I2C device handle
  808. * @param vol: volume level(0 ~ 99)
  809. * @retval RT_EOK if correct communication, else wrong communication
  810. */
  811. int wm8978_set_volume(struct rt_i2c_bus_device *dev, int vol)
  812. {
  813. vol = 63 * vol / 100;
  814. vol = (vol & VOL_MASK) << VOL_POS;
  815. wm8978_write_reg(dev, REG_LOUT1_VOL | vol);
  816. wm8978_write_reg(dev, REG_ROUT1_VOL | HPVU | vol);
  817. wm8978_write_reg(dev, REG_LOUT2_VOL | vol);
  818. wm8978_write_reg(dev, REG_ROUT2_VOL | SPKVU | vol);
  819. return RT_EOK;
  820. }
  821. void wm8978_interface_cfg(struct rt_i2c_bus_device *dev, enum data_fomat_select fmt, rt_uint32_t bitBand)
  822. {
  823. rt_uint16_t temp = 0;
  824. switch (fmt)
  825. {
  826. case RIGHT_FOMAT_SELECT:
  827. temp = FMT_RIGHT_JUSTIFIED;
  828. break;
  829. case LEFT_FOMAT_SELECT:
  830. temp = FMT_LEFT_JUSTIFIED;
  831. break;
  832. case I2S_FOMAT_SELECT:
  833. temp = FMT_I2S;
  834. break;
  835. case PCM_FOMAT_SELECT:
  836. temp = FMT_PCM;
  837. break;
  838. default:
  839. break;
  840. }
  841. switch (bitBand)
  842. {
  843. case 16:
  844. temp |= WL_16BITS;
  845. break;
  846. case 20:
  847. temp |= WL_20BITS;
  848. break;
  849. case 24:
  850. temp |= WL_24BITS;
  851. break;
  852. case 32:
  853. temp |= WL_32BITS;
  854. break;
  855. default:
  856. break;
  857. }
  858. wm8978_write_reg(dev, REG_AUDIO_INTERFACE | temp);
  859. }
  860. void wm8978_reset(struct rt_i2c_bus_device *dev)
  861. {
  862. wm8978_write_reg(dev, REG_SOFTWARE_RESET);
  863. }
  864. void wm8978_mute_enabled(struct rt_i2c_bus_device *dev, rt_bool_t enable)
  865. {
  866. wm8978_write_reg(dev, REG_POWER_MANAGEMENT1 | (enable ? BIASEN : 0));
  867. }
  868. rt_err_t wm8978_set_EQ1(struct rt_i2c_bus_device *dev, rt_uint8_t freq, rt_uint8_t gain)
  869. {
  870. rt_uint16_t temp = 0;
  871. if (freq > 3 || gain > 24)
  872. {
  873. return -RT_ERROR;
  874. }
  875. switch (freq)
  876. {
  877. case 0:
  878. temp = EQ1C_80HZ;
  879. break;
  880. case 1:
  881. temp = EQ1C_105HZ;
  882. break;
  883. case 2:
  884. temp = EQ1C_135HZ;
  885. break;
  886. case 3:
  887. temp = EQ1C_175HZ;
  888. break;
  889. default:
  890. break;
  891. }
  892. /* 0 - 24 as -12~+12dB */
  893. gain = 24 - gain;
  894. temp |= gain;
  895. wm8978_write_reg(dev, REG_EQ1 | temp);
  896. return RT_EOK;
  897. }
  898. rt_err_t wm8978_set_EQ2(struct rt_i2c_bus_device *dev, rt_uint8_t freq, rt_uint8_t gain)
  899. {
  900. rt_uint16_t temp = 0;
  901. if (freq > 3 || gain > 24)
  902. {
  903. return -RT_ERROR;
  904. }
  905. switch (freq)
  906. {
  907. case 0:
  908. temp = EQ2C_230HZ;
  909. break;
  910. case 1:
  911. temp = EQ2C_300HZ;
  912. break;
  913. case 2:
  914. temp = EQ2C_385HZ;
  915. break;
  916. case 3:
  917. temp = EQ2C_500HZ;
  918. break;
  919. default:
  920. break;
  921. }
  922. /* 0 - 24 as -12~+12dB */
  923. gain = 24 - gain;
  924. temp |= gain;
  925. wm8978_write_reg(dev, REG_EQ2 | temp);
  926. return RT_EOK;
  927. }
  928. rt_err_t wm8978_set_EQ3(struct rt_i2c_bus_device *dev, rt_uint8_t freq, rt_uint8_t gain)
  929. {
  930. rt_uint16_t temp = 0;
  931. if (freq > 3 || gain > 24)
  932. {
  933. return -RT_ERROR;
  934. }
  935. switch (freq)
  936. {
  937. case 0:
  938. temp = EQ3C_650HZ;
  939. break;
  940. case 1:
  941. temp = EQ3C_850HZ;
  942. break;
  943. case 2:
  944. temp = EQ3C_1_1KHZ;
  945. break;
  946. case 3:
  947. temp = EQ3C_1_4KHZ;
  948. break;
  949. default:
  950. break;
  951. }
  952. /* 0 - 24 as -12~+12dB */
  953. gain = 24 - gain;
  954. temp |= gain;
  955. wm8978_write_reg(dev, REG_EQ3 | temp);
  956. return RT_EOK;
  957. }
  958. rt_err_t wm8978_set_EQ4(struct rt_i2c_bus_device *dev, rt_uint8_t freq, rt_uint8_t gain)
  959. {
  960. rt_uint16_t temp = 0;
  961. if (freq > 3 || gain > 24)
  962. {
  963. return -RT_ERROR;
  964. }
  965. switch (freq)
  966. {
  967. case 0:
  968. temp = EQ4C_1_8KHZ;
  969. break;
  970. case 1:
  971. temp = EQ4C_2_4KHZ;
  972. break;
  973. case 2:
  974. temp = EQ4C_3_2KHZ;
  975. break;
  976. case 3:
  977. temp = EQ4C_4_1KHZ;
  978. break;
  979. default:
  980. break;
  981. }
  982. /* 0 - 24 as -12~+12dB */
  983. gain = 24 - gain;
  984. temp |= gain;
  985. wm8978_write_reg(dev, REG_EQ4 | temp);
  986. return RT_EOK;
  987. }
  988. rt_err_t wm8978_set_EQ5(struct rt_i2c_bus_device *dev, rt_uint8_t freq, rt_uint8_t gain)
  989. {
  990. rt_uint16_t temp = 0;
  991. if (freq > 3 || gain > 24)
  992. {
  993. return -RT_ERROR;
  994. }
  995. switch (freq)
  996. {
  997. case 0:
  998. temp = EQ5C_5_3KHZ;
  999. break;
  1000. case 1:
  1001. temp = EQ5C_6_9KHZ;
  1002. break;
  1003. case 2:
  1004. temp = EQ5C_9KHZ;
  1005. break;
  1006. case 3:
  1007. temp = EQ5C_11_7KHZ;
  1008. break;
  1009. default:
  1010. break;
  1011. }
  1012. /* 0 - 24 as -12~+12dB */
  1013. gain = 24 - gain;
  1014. temp |= gain;
  1015. wm8978_write_reg(dev, REG_EQ5 | temp);
  1016. return RT_EOK;
  1017. }
  1018. void wm8978_3D_Set(struct rt_i2c_bus_device *dev, rt_uint8_t depth)
  1019. {
  1020. wm8978_write_reg(dev, REG_3D | (depth & 0xf));
  1021. }