drv_wm8978.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "drv_wm8978.h"
  13. /* Register Definitions */
  14. #define REG_SOFTWARE_RESET ((uint16_t)0)
  15. #define REG_POWER_MANAGEMENT1 ((uint16_t)(1 << 9))
  16. #define REG_POWER_MANAGEMENT2 ((uint16_t)(2 << 9))
  17. #define REG_POWER_MANAGEMENT3 ((uint16_t)(3 << 9))
  18. #define REG_AUDIO_INTERFACE ((uint16_t)(4 << 9))
  19. #define REG_COMPANDING ((uint16_t)(5 << 9))
  20. #define REG_CLOCK_GEN ((uint16_t)(6 << 9))
  21. #define REG_ADDITIONAL ((uint16_t)(7 << 9))
  22. #define REG_GPIO ((uint16_t)(8 << 9))
  23. #define REG_JACK_DETECT1 ((uint16_t)(9 << 9))
  24. #define REG_DAC ((uint16_t)(10 << 9))
  25. #define REG_LEFT_DAC_VOL ((uint16_t)(11 << 9))
  26. #define REG_RIGHT_DAC_VOL ((uint16_t)(12 << 9))
  27. #define REG_JACK_DETECT2 ((uint16_t)(13 << 9))
  28. #define REG_ADC ((uint16_t)(14 << 9))
  29. #define REG_LEFT_ADC_VOL ((uint16_t)(15 << 9))
  30. #define REG_RIGHT_ADC_VOL ((uint16_t)(16 << 9))
  31. #define REG_EQ1 ((uint16_t)(18 << 9))
  32. #define REG_EQ2 ((uint16_t)(19 << 9))
  33. #define REG_EQ3 ((uint16_t)(20 << 9))
  34. #define REG_EQ4 ((uint16_t)(21 << 9))
  35. #define REG_EQ5 ((uint16_t)(22 << 9))
  36. #define REG_DAC_LIMITER1 ((uint16_t)(24 << 9))
  37. #define REG_DAC_LIMITER2 ((uint16_t)(25 << 9))
  38. #define REG_NOTCH_FILTER1 ((uint16_t)(27 << 9))
  39. #define REG_NOTCH_FILTER2 ((uint16_t)(28 << 9))
  40. #define REG_NOTCH_FILTER3 ((uint16_t)(29 << 9))
  41. #define REG_NOTCH_FILTER4 ((uint16_t)(30 << 9))
  42. #define REG_ALC1 ((uint16_t)(32 << 9))
  43. #define REG_ALC2 ((uint16_t)(33 << 9))
  44. #define REG_ALC3 ((uint16_t)(34 << 9))
  45. #define REG_NOISE_GATE ((uint16_t)(35 << 9))
  46. #define REG_PLL_N ((uint16_t)(36 << 9))
  47. #define REG_PLL_K1 ((uint16_t)(37 << 9))
  48. #define REG_PLL_K2 ((uint16_t)(38 << 9))
  49. #define REG_PLL_K3 ((uint16_t)(39 << 9))
  50. #define REG_3D ((uint16_t)(41 << 9))
  51. #define REG_BEEP ((uint16_t)(43 << 9))
  52. #define REG_INPUT ((uint16_t)(44 << 9))
  53. #define REG_LEFT_PGA_GAIN ((uint16_t)(45 << 9))
  54. #define REG_RIGHT_PGA_GAIN ((uint16_t)(46 << 9))
  55. #define REG_LEFT_ADC_BOOST ((uint16_t)(47 << 9))
  56. #define REG_RIGHT_ADC_BOOST ((uint16_t)(48 << 9))
  57. #define REG_OUTPUT ((uint16_t)(49 << 9))
  58. #define REG_LEFT_MIXER ((uint16_t)(50 << 9))
  59. #define REG_RIGHT_MIXER ((uint16_t)(51 << 9))
  60. #define REG_LOUT1_VOL ((uint16_t)(52 << 9))
  61. #define REG_ROUT1_VOL ((uint16_t)(53 << 9))
  62. #define REG_LOUT2_VOL ((uint16_t)(54 << 9))
  63. #define REG_ROUT2_VOL ((uint16_t)(55 << 9))
  64. #define REG_OUT3_MIXER ((uint16_t)(56 << 9))
  65. #define REG_OUT4_MIXER ((uint16_t)(57 << 9))
  66. // R01 REG_POWER_MANAGEMENT1
  67. #define BUFDCOPEN (1 << 8)
  68. #define OUT4MIXEN (1 << 7)
  69. #define OUT3MIXEN (1 << 6)
  70. #define PLLEN (1 << 5)
  71. #define MICBEN (1 << 4)
  72. #define BIASEN (1 << 3)
  73. #define BUFIOEN (1 << 2)
  74. #define VMIDSEL_OFF (0)
  75. #define VMIDSEL_75K (1)
  76. #define VMIDSEL_300K (2)
  77. #define VMIDSEL_5K (3)
  78. // R02 REG_POWER_MANAGEMENT2
  79. #define ROUT1EN (1 << 8)
  80. #define LOUT1EN (1 << 7)
  81. #define SLEEP (1 << 6)
  82. #define BOOSTENR (1 << 5)
  83. #define BOOSTENL (1 << 4)
  84. #define INPPGAENR (1 << 3)
  85. #define INPPGAENL (1 << 2)
  86. #define ADCENR (1 << 1)
  87. #define ADCENL (1)
  88. // R03 REG_POWER_MANAGEMENT3
  89. #define OUT4EN (1 << 8)
  90. #define OUT3EN (1 << 7)
  91. #define LOUT2EN (1 << 6)
  92. #define ROUT2EN (1 << 5)
  93. #define RMIXEN (1 << 3)
  94. #define LMIXEN (1 << 2)
  95. #define DACENR (1 << 1)
  96. #define DACENL (1)
  97. // R04 REG_AUDIO_INTERFACE
  98. #define BCP_NORMAL (0)
  99. #define BCP_INVERTED (1 << 8)
  100. #define LRP_NORMAL (0)
  101. #define LRP_INVERTED (1 << 7)
  102. #define WL_16BITS (0)
  103. #define WL_20BITS (1 << 5)
  104. #define WL_24BITS (2 << 5) // Default value
  105. #define WL_32BITS (3 << 5)
  106. #define FMT_RIGHT_JUSTIFIED (0)
  107. #define FMT_LEFT_JUSTIFIED (1 << 3)
  108. #define FMT_I2S (2 << 3) // Default value
  109. #define FMT_PCM (3 << 3)
  110. #define DACLRSWAP (1 << 2)
  111. #define ADCLRSWAP (1 << 1)
  112. #define MONO (1)
  113. // R05 REG_COMPANDING
  114. #define WL8 (1 << 5)
  115. #define DAC_COMP_OFF (0) // Default value
  116. #define DAC_COMP_ULAW (2 << 3)
  117. #define DAC_COMP_ALAW (3 << 3)
  118. #define ADC_COMP_OFF (0) // Default value
  119. #define ADC_COMP_ULAW (2 << 1)
  120. #define ADC_COMP_ALAW (3 << 1)
  121. #define LOOPBACK (1)
  122. // R06 REG_CLOCK_GEN
  123. #define CLKSEL_MCLK (0)
  124. #define CLKSEL_PLL (1 << 8) // Default value
  125. #define MCLK_DIV1 (0)
  126. #define MCLK_DIV1_5 (1 << 5)
  127. #define MCLK_DIV2 (2 << 5) // Default value
  128. #define MCLK_DIV3 (3 << 5)
  129. #define MCLK_DIV4 (4 << 5)
  130. #define MCLK_DIV6 (5 << 5)
  131. #define MCLK_DIV8 (6 << 5)
  132. #define MCLK_DIV12 (7 << 5)
  133. #define BCLK_DIV1 (0) // Default value
  134. #define BCLK_DIV2 (1 << 2)
  135. #define BCLK_DIV4 (2 << 2)
  136. #define BCLK_DIV8 (3 << 2)
  137. #define BCLK_DIV16 (4 << 2)
  138. #define BCLK_DIV32 (5 << 2)
  139. #define MS (1)
  140. // R07 REG_ADDITIONAL
  141. #define WM_SR_48KHZ (0) // Default value
  142. #define WM_SR_32KHZ (1 << 1)
  143. #define WM_SR_24KHZ (2 << 1)
  144. #define WM_SR_16KHZ (3 << 1)
  145. #define WM_SR_12KHZ (4 << 1)
  146. #define WM_SR_8KHZ (5 << 1)
  147. #define SLOWCLKEN (1)
  148. // R08 REG_GPIO
  149. #define OPCLK_DIV1 (0) // Default value
  150. #define OPCLK_DIV2 (1 << 4)
  151. #define OPCLK_DIV3 (2 << 4)
  152. #define OPCLK_DIV4 (3 << 4)
  153. #define GPIO1POL_NONINVERTED (0) // Default value
  154. #define GPIO1POL_INVERTED (1 << 3)
  155. #define GPIO1SEL_INPUT (0) // Default value
  156. #define GPIO1SEL_TEMP_OK (2)
  157. #define GPIO1SEL_AMUTE_ACTIVE (3)
  158. #define GPIO1SEL_PLL_CLK_OP (4)
  159. #define GPIO1SEL_PLL_LOCK (5)
  160. #define GPIO1SEL_LOGIC1 (6)
  161. #define GPIO1SEL_LOGIC0 (7)
  162. // R09 REG_JACK_DETECT1
  163. #define JD_VMID_EN1 (1 << 8)
  164. #define JD_VMID_EN0 (1 << 7)
  165. #define JD_EN (1 << 6)
  166. #define JD_SEL_GPIO1 (0 << 4) // Default value
  167. #define JD_SEL_GPIO2 (1 << 4)
  168. #define JD_SEL_GPIO3 (2 << 4)
  169. // R10 REG_DAC
  170. #define SOFTMUTE (1 << 6)
  171. #define DACOSR128 (1 << 3)
  172. #define AMUTE (1 << 2)
  173. #define DACPOLR (1 << 1)
  174. #define DACPOLL (1)
  175. // R11 & R12 REG_LEFT_DAC_VOL & REG_RIGHT_DAC_VOL
  176. #define DACVU (1 << 8)
  177. #define DACVOL_POS (0)
  178. #define DACVOL_MASK (0xFF)
  179. // R13 REG_JACK_DETECT2
  180. #define JD_OUT4_EN1 (1 << 7)
  181. #define JD_OUT3_EN1 (1 << 6)
  182. #define JD_OUT2_EN1 (1 << 5)
  183. #define JD_OUT1_EN1 (1 << 4)
  184. #define JD_OUT4_EN0 (1 << 3)
  185. #define JD_OUT3_EN0 (1 << 2)
  186. #define JD_OUT2_EN0 (1 << 1)
  187. #define JD_OUT1_EN0 (1)
  188. // R14 REG_ADC
  189. #define HPFEN (1 << 8)
  190. #define HPFAPP (1 << 7)
  191. #define HPFCUT_POS (4)
  192. #define HPFCUT_MASK (7)
  193. #define HPFCUT_0 (0)
  194. #define HPFCUT_1 (1 << 4)
  195. #define HPFCUT_2 (2 << 4)
  196. #define HPFCUT_3 (3 << 4)
  197. #define HPFCUT_4 (4 << 4)
  198. #define HPFCUT_5 (5 << 4)
  199. #define HPFCUT_6 (6 << 4)
  200. #define HPFCUT_7 (7 << 4)
  201. #define ADCOSR128 (1 << 3)
  202. #define ADCRPOL (1 << 1)
  203. #define ADCLPOL (1)
  204. // R15 & R16 REG_LEFT_ADC_VOL & REG_RIGHT_ADC_VOL
  205. #define ADCVU (1 << 8)
  206. #define ADCVOL_POS (0)
  207. #define ADCVOL_MASK (0xFF)
  208. // R18 REG_EQ1
  209. #define EQ3DMODE_ADC (0)
  210. #define EQ3DMODE_DAC (1 << 8) // Default value
  211. #define EQ1C_80HZ (0)
  212. #define EQ1C_105HZ (1 << 5) // Default value
  213. #define EQ1C_135HZ (2 << 5)
  214. #define EQ1C_175HZ (3 << 5)
  215. // R19 REG_EQ2
  216. #define EQ2BW_NARROW (0) // Default value
  217. #define EQ2BW_WIDE (1 << 8)
  218. #define EQ2C_230HZ (0)
  219. #define EQ2C_300HZ (1 << 5) // Default value
  220. #define EQ2C_385HZ (2 << 5)
  221. #define EQ2C_500HZ (3 << 5)
  222. // R20 REG_EQ3
  223. #define EQ3BW_NARROW (0) // Default value
  224. #define EQ3BW_WIDE (1 << 8)
  225. #define EQ3C_650HZ (0)
  226. #define EQ3C_850HZ (1 << 5) // Default value
  227. #define EQ3C_1_1KHZ (2 << 5)
  228. #define EQ3C_1_4KHZ (3 << 5)
  229. // R21 REG_EQ4
  230. #define EQ4BW_NARROW (0) // Default value
  231. #define EQ4BW_WIDE (1 << 8)
  232. #define EQ4C_1_8KHZ (0)
  233. #define EQ4C_2_4KHZ (1 << 5) // Default value
  234. #define EQ4C_3_2KHZ (2 << 5)
  235. #define EQ4C_4_1KHZ (3 << 5)
  236. // R22 REG_EQ5
  237. #define EQ5C_5_3KHZ (0)
  238. #define EQ5C_6_9KHZ (1 << 5) // Default value
  239. #define EQ5C_9KHZ (2 << 5)
  240. #define EQ5C_11_7KHZ (3 << 5)
  241. // R18 - R22
  242. #define EQC_POS (5)
  243. #define EQC_MASK (3)
  244. #define EQG_POS (0)
  245. #define EQG_MASK (31)
  246. // R24 REG_DAC_LIMITER1
  247. #define LIMEN (1 << 8)
  248. #define LIMDCY_POS (4)
  249. #define LIMDCY_MASK (15)
  250. #define LIMDCY_750US (0)
  251. #define LIMDCY_1_5MS (1 << 4)
  252. #define LIMDCY_3MS (2 << 4)
  253. #define LIMDCY_6MS (3 << 4) // Default value
  254. #define LIMDCY_12MS (4 << 4)
  255. #define LIMDCY_24MS (5 << 4)
  256. #define LIMDCY_48MS (6 << 4)
  257. #define LIMDCY_96MS (7 << 4)
  258. #define LIMDCY_192MS (8 << 4)
  259. #define LIMDCY_384MS (9 << 4)
  260. #define LIMDCY_768MS (10 << 4)
  261. #define LIMATK_POS (0)
  262. #define LIMATK_MASK (15)
  263. #define LIMATK_94US (0)
  264. #define LIMATK_188US (1)
  265. #define LIMATK_375US (2) // Default value
  266. #define LIMATK_750US (3)
  267. #define LIMATK_1_5MS (4)
  268. #define LIMATK_3MS (5)
  269. #define LIMATK_6MS (6)
  270. #define LIMATK_12MS (7)
  271. #define LIMATK_24MS (8)
  272. #define LIMATK_48MS (9)
  273. #define LIMATK_96MS (10)
  274. #define LIMATK_192MS (11)
  275. // R25 REG_DAC_LIMITER2
  276. #define LIMLVL_POS (4)
  277. #define LIMLVL_MASK (7)
  278. #define LIMLVL_N1DB (0) // Default value
  279. #define LIMLVL_N2DB (1 << 4)
  280. #define LIMLVL_N3DB (2 << 4)
  281. #define LIMLVL_N4DB (3 << 4)
  282. #define LIMLVL_N5DB (4 << 4)
  283. #define LIMLVL_N6DB (5 << 4)
  284. #define LIMBOOST_POS (0)
  285. #define LIMBOOST_MASK (15)
  286. #define LIMBOOST_0DB (0)
  287. #define LIMBOOST_1DB (1)
  288. #define LIMBOOST_2DB (2)
  289. #define LIMBOOST_3DB (3)
  290. #define LIMBOOST_4DB (4)
  291. #define LIMBOOST_5DB (5)
  292. #define LIMBOOST_6DB (6)
  293. #define LIMBOOST_7DB (7)
  294. #define LIMBOOST_8DB (8)
  295. #define LIMBOOST_9DB (9)
  296. #define LIMBOOST_10DB (10)
  297. #define LIMBOOST_11DB (11)
  298. #define LIMBOOST_12DB (12)
  299. // R27 - R30 REG_NOTCH_FILTER1 - REG_NOTCH_FILTER4
  300. #define NFU (1 << 8)
  301. #define NFEN (1 << 7)
  302. #define NFA_POS (0)
  303. #define NFA_MASK (127)
  304. // R32 REG_ALC1
  305. #define ALCSEL_OFF (0) // Default value
  306. #define ALCSEL_RIGHT_ONLY (1 << 7)
  307. #define ALCSEL_LEFT_ONLY (2 << 7)
  308. #define ALCSEL_BOTH_ON (3 << 7)
  309. #define ALCMAXGAIN_POS (3)
  310. #define ALCMAXGAIN_MASK (7)
  311. #define ALCMAXGAIN_N6_75DB (0)
  312. #define ALCMAXGAIN_N0_75DB (1 << 3)
  313. #define ALCMAXGAIN_5_25DB (2 << 3)
  314. #define ALCMAXGAIN_11_25DB (3 << 3)
  315. #define ALCMAXGAIN_17_25DB (4 << 3)
  316. #define ALCMAXGAIN_23_25DB (5 << 3)
  317. #define ALCMAXGAIN_29_25DB (6 << 3)
  318. #define ALCMAXGAIN_35_25DB (7 << 3) // Default value
  319. #define ALCMINGAIN_POS (0)
  320. #define ALCMINGAIN_MASK (7)
  321. #define ALCMINGAIN_N12DB (0) // Default value
  322. #define ALCMINGAIN_N6DB (1)
  323. #define ALCMINGAIN_0DB (2)
  324. #define ALCMINGAIN_6DB (3)
  325. #define ALCMINGAIN_12DB (4)
  326. #define ALCMINGAIN_18DB (5)
  327. #define ALCMINGAIN_24DB (6)
  328. #define ALCMINGAIN_30DB (7)
  329. // R33 REG_ALC2
  330. #define ALCHLD_POS (4)
  331. #define ALCHLD_MASK (15)
  332. #define ALCHLD_0MS (0) // Default value
  333. #define ALCHLD_2_67MS (1 << 4)
  334. #define ALCHLD_5_33MS (2 << 4)
  335. #define ALCHLD_10_67MS (3 << 4)
  336. #define ALCHLD_21_33MS (4 << 4)
  337. #define ALCHLD_42_67MS (5 << 4)
  338. #define ALCHLD_85_33MS (6 << 4)
  339. #define ALCHLD_170_67MS (7 << 4)
  340. #define ALCHLD_341_33MS (8 << 4)
  341. #define ALCHLD_682_67MS (9 << 4)
  342. #define ALCHLD_1_36S (10 << 4)
  343. #define ALCLVL_POS (0)
  344. #define ALCLVL_MASK (15)
  345. #define ALCLVL_N22_5DBFS (0)
  346. #define ALCLVL_N21DBFS (1)
  347. #define ALCLVL_N19_5DBFS (2)
  348. #define ALCLVL_N18DBFS (3)
  349. #define ALCLVL_N16_5DBFS (4)
  350. #define ALCLVL_N15DBFS (5)
  351. #define ALCLVL_N13_5DBFS (6)
  352. #define ALCLVL_N12DBFS (7)
  353. #define ALCLVL_N10_5DBFS (8)
  354. #define ALCLVL_N9DBFS (9)
  355. #define ALCLVL_N7_5DBFS (10)
  356. #define ALCLVL_N6DBFS (11) // Default value
  357. #define ALCLVL_N4_5DBFS (12)
  358. #define ALCLVL_N3DBFS (13)
  359. #define ALCLVL_N1_5DBFS (14)
  360. // R34 REG_ALC3
  361. #define ALCMODE_ALC (0) // Default value
  362. #define ALCMODE_LIMITER (1 << 8)
  363. #define ALCDCY_POS (4)
  364. #define ALCDCY_MASK (15)
  365. #define ALCDCY_0 (0)
  366. #define ALCDCY_1 (1 << 4)
  367. #define ALCDCY_2 (2 << 4)
  368. #define ALCDCY_3 (3 << 4) // Default value
  369. #define ALCDCY_4 (4 << 4)
  370. #define ALCDCY_5 (5 << 4)
  371. #define ALCDCY_6 (6 << 4)
  372. #define ALCDCY_7 (7 << 4)
  373. #define ALCDCY_8 (8 << 4)
  374. #define ALCDCY_9 (9 << 4)
  375. #define ALCDCY_10 (10 << 4)
  376. #define ALCATK_POS (0)
  377. #define ALCATK_MASK (15)
  378. #define ALCATK_0 (0)
  379. #define ALCATK_1 (1)
  380. #define ALCATK_2 (2) // Default value
  381. #define ALCATK_3 (3)
  382. #define ALCATK_4 (4)
  383. #define ALCATK_5 (5)
  384. #define ALCATK_6 (6)
  385. #define ALCATK_7 (7)
  386. #define ALCATK_8 (8)
  387. #define ALCATK_9 (9)
  388. #define ALCATK_10 (10)
  389. // R35 REG_NOISE_GATE
  390. #define NGEN (1 << 3)
  391. #define NGTH_POS (0)
  392. #define NGTH_MASK (7)
  393. #define NGTH_N39DB (0) // Default value
  394. #define NGTH_N45DB (1)
  395. #define NGTH_N51DB (2)
  396. #define NGTH_N57DB (3)
  397. #define NGTH_N63DB (4)
  398. #define NGTH_N69DB (5)
  399. #define NGTH_N75DB (6)
  400. #define NGTH_N81DB (7)
  401. // R36 REG_PLL_N
  402. #define PLLPRESCALE (1 << 4)
  403. #define PLLN_POS (0)
  404. #define PLLN_MASK (15)
  405. // R37 - R39 REG_PLL_K1 - REG_PLL_K3
  406. #define PLLK1_POS (0)
  407. #define PLLK1_MASK (63)
  408. #define PLLK2_POS (0)
  409. #define PLLK2_MASK (511)
  410. #define PLLK3_POS (0)
  411. #define PLLK3_MASK (511)
  412. // R41 REG_3D
  413. #define DEPTH3D_POS (0)
  414. #define DEPTH3D_MASK (15)
  415. #define DEPTH3D_0 (0) // Default value
  416. #define DEPTH3D_6_67 (1)
  417. #define DEPTH3D_13_33 (2)
  418. #define DEPTH3D_20 (3)
  419. #define DEPTH3D_26_67 (4)
  420. #define DEPTH3D_33_33 (5)
  421. #define DEPTH3D_40 (6)
  422. #define DEPTH3D_46_67 (7)
  423. #define DEPTH3D_53_33 (8)
  424. #define DEPTH3D_60 (9)
  425. #define DEPTH3D_66_67 (10)
  426. #define DEPTH3D_73_33 (11)
  427. #define DEPTH3D_80 (12)
  428. #define DEPTH3D_86_67 (13)
  429. #define DEPTH3D_93_33 (14)
  430. #define DEPTH3D_100 (15)
  431. // R43 REG_BEEP
  432. #define MUTERPGA2INV (1 << 5)
  433. #define INVROUT2 (1 << 4)
  434. #define BEEPVOL_POS (1)
  435. #define BEEPVOL_MASK (7)
  436. #define BEEPVOL_N15DB (0)
  437. #define BEEPVOL_N12DB (1 << 1)
  438. #define BEEPVOL_N9DB (2 << 1)
  439. #define BEEPVOL_N6DB (3 << 1)
  440. #define BEEPVOL_N3DB (4 << 1)
  441. #define BEEPVOL_0DB (5 << 1)
  442. #define BEEPVOL_3DB (6 << 1)
  443. #define BEEPVOL_6DB (7 << 1)
  444. #define BEEPEN (1)
  445. // R44 REG_INPUT
  446. #define MBVSEL_0_9AVDD (0) // Default value
  447. #define MBVSEL_0_65AVDD (1 << 8)
  448. #define R2_2INPVGA (1 << 6)
  449. #define RIN2INPVGA (1 << 5) // Default value
  450. #define RIP2INPVGA (1 << 4) // Default value
  451. #define L2_2INPVGA (1 << 2)
  452. #define LIN2INPVGA (1 << 1) // Default value
  453. #define LIP2INPVGA (1) // Default value
  454. // R45 REG_LEFT_PGA_GAIN
  455. #define INPPGAUPDATE (1 << 8)
  456. #define INPPGAZCL (1 << 7)
  457. #define INPPGAMUTEL (1 << 6)
  458. // R46 REG_RIGHT_PGA_GAIN
  459. #define INPPGAZCR (1 << 7)
  460. #define INPPGAMUTER (1 << 6)
  461. // R45 - R46
  462. #define INPPGAVOL_POS (0)
  463. #define INPPGAVOL_MASK (63)
  464. // R47 REG_LEFT_ADC_BOOST
  465. #define PGABOOSTL (1 << 8) // Default value
  466. #define L2_2BOOSTVOL_POS (4)
  467. #define L2_2BOOSTVOL_MASK (7)
  468. #define L2_2BOOSTVOL_DISABLED (0) // Default value
  469. #define L2_2BOOSTVOL_N12DB (1 << 4)
  470. #define L2_2BOOSTVOL_N9DB (2 << 4)
  471. #define L2_2BOOSTVOL_N6DB (3 << 4)
  472. #define L2_2BOOSTVOL_N3DB (4 << 4)
  473. #define L2_2BOOSTVOL_0DB (5 << 4)
  474. #define L2_2BOOSTVOL_3DB (6 << 4)
  475. #define L2_2BOOSTVOL_6DB (7 << 4)
  476. #define AUXL2BOOSTVOL_POS (0)
  477. #define AUXL2BOOSTVOL_MASK (7)
  478. #define AUXL2BOOSTVOL_DISABLED (0) // Default value
  479. #define AUXL2BOOSTVOL_N12DB (1)
  480. #define AUXL2BOOSTVOL_N9DB (2)
  481. #define AUXL2BOOSTVOL_N6DB (3)
  482. #define AUXL2BOOSTVOL_N3DB (4)
  483. #define AUXL2BOOSTVOL_0DB (5)
  484. #define AUXL2BOOSTVOL_3DB (6)
  485. #define AUXL2BOOSTVOL_6DB (7)
  486. // R48 REG_RIGHT_ADC_BOOST
  487. #define PGABOOSTR (1 << 8) // Default value
  488. #define R2_2BOOSTVOL_POS (4)
  489. #define R2_2BOOSTVOL_MASK (7)
  490. #define R2_2BOOSTVOL_DISABLED (0) // Default value
  491. #define R2_2BOOSTVOL_N12DB (1 << 4)
  492. #define R2_2BOOSTVOL_N9DB (2 << 4)
  493. #define R2_2BOOSTVOL_N6DB (3 << 4)
  494. #define R2_2BOOSTVOL_N3DB (4 << 4)
  495. #define R2_2BOOSTVOL_0DB (5 << 4)
  496. #define R2_2BOOSTVOL_3DB (6 << 4)
  497. #define R2_2BOOSTVOL_6DB (7 << 4)
  498. #define AUXR2BOOSTVOL_POS (0)
  499. #define AUXR2BOOSTVOL_MASK (7)
  500. #define AUXR2BOOSTVOL_DISABLED (0) // Default value
  501. #define AUXR2BOOSTVOL_N12DB (1)
  502. #define AUXR2BOOSTVOL_N9DB (2)
  503. #define AUXR2BOOSTVOL_N6DB (3)
  504. #define AUXR2BOOSTVOL_N3DB (4)
  505. #define AUXR2BOOSTVOL_0DB (5)
  506. #define AUXR2BOOSTVOL_3DB (6)
  507. #define AUXR2BOOSTVOL_6DB (7)
  508. // R49 REG_OUTPUT
  509. #define DACL2RMIX (1 << 6)
  510. #define DACR2LMIX (1 << 5)
  511. #define OUT4BOOST (1 << 4)
  512. #define OUT3BOOST (1 << 3)
  513. #define SPKBOOST (1 << 2)
  514. #define TSDEN (1 << 1)
  515. #define VROI (1)
  516. // R50 REG_LEFT_MIXER
  517. #define AUXLMIXVOL_POS (6)
  518. #define AUXLMIXVOL_MASK (7)
  519. #define AUXLMIXVOL_N15DB (0) // Default value
  520. #define AUXLMIXVOL_N12DB (1 << 6)
  521. #define AUXLMIXVOL_N9DB (2 << 6)
  522. #define AUXLMIXVOL_N6DB (3 << 6)
  523. #define AUXLMIXVOL_N3DB (4 << 6)
  524. #define AUXLMIXVOL_0DB (5 << 6)
  525. #define AUXLMIXVOL_3DB (6 << 6)
  526. #define AUXLMIXVOL_6DB (7 << 6)
  527. #define AUXL2LMIX (1 << 5)
  528. #define BYPLMIXVOL_POS (2)
  529. #define BYPLMIXVOL_MASK (7)
  530. #define BYPLMIXVOL_N15DB (0) // Default value
  531. #define BYPLMIXVOL_N12DB (1 << 2)
  532. #define BYPLMIXVOL_N9DB (2 << 2)
  533. #define BYPLMIXVOL_N6DB (3 << 2)
  534. #define BYPLMIXVOL_N3DB (4 << 2)
  535. #define BYPLMIXVOL_0DB (5 << 2)
  536. #define BYPLMIXVOL_3DB (6 << 2)
  537. #define BYPLMIXVOL_6DB (7 << 2)
  538. #define BYPL2LMIX (1 << 1)
  539. #define DACL2LMIX (1)
  540. // R51 REG_RIGHT_MIXER
  541. #define AUXRMIXVOL_POS (6)
  542. #define AUXRMIXVOL_MASK (7)
  543. #define AUXRMIXVOL_N15DB (0) // Default value
  544. #define AUXRMIXVOL_N12DB (1 << 6)
  545. #define AUXRMIXVOL_N9DB (2 << 6)
  546. #define AUXRMIXVOL_N6DB (3 << 6)
  547. #define AUXRMIXVOL_N3DB (4 << 6)
  548. #define AUXRMIXVOL_0DB (5 << 6)
  549. #define AUXRMIXVOL_3DB (6 << 6)
  550. #define AUXRMIXVOL_6DB (7 << 6)
  551. #define AUXR2RMIX (1 << 5)
  552. #define BYPRMIXVOL_POS (2)
  553. #define BYPRMIXVOL_MASK (7)
  554. #define BYPRMIXVOL_N15DB (0) // Default value
  555. #define BYPRMIXVOL_N12DB (1 << 2)
  556. #define BYPRMIXVOL_N9DB (2 << 2)
  557. #define BYPRMIXVOL_N6DB (3 << 2)
  558. #define BYPRMIXVOL_N3DB (4 << 2)
  559. #define BYPRMIXVOL_0DB (5 << 2)
  560. #define BYPRMIXVOL_3DB (6 << 2)
  561. #define BYPRMIXVOL_6DB (7 << 2)
  562. #define BYPR2RMIX (1 << 1)
  563. #define DACR2RMIX (1)
  564. // R52 - R55 REG_LOUT1_VOL - REG_ROUT2_VOL
  565. #define HPVU (1 << 8)
  566. #define SPKVU (1 << 8)
  567. #define LOUT1ZC (1 << 7)
  568. #define LOUT1MUTE (1 << 6)
  569. #define ROUT1ZC (1 << 7)
  570. #define ROUT1MUTE (1 << 6)
  571. #define LOUT2ZC (1 << 7)
  572. #define LOUT2MUTE (1 << 6)
  573. #define ROUT2ZC (1 << 7)
  574. #define ROUT2MUTE (1 << 6)
  575. #define VOL_POS (0)
  576. #define VOL_MASK (63)
  577. // R56 REG_OUT3_MIXER
  578. #define OUT3MUTE (1 << 6)
  579. #define OUT4_2OUT3 (1 << 3)
  580. #define BYPL2OUT3 (1 << 2)
  581. #define LMIX2OUT3 (1 << 1)
  582. #define LDAC2OUT3 (1)
  583. // R57 REG_OUT4_MIXER
  584. #define OUT4MUTE (1 << 6)
  585. #define HALFSIG (1 << 5)
  586. #define LMIX2OUT4 (1 << 4)
  587. #define LDAC2OUT4 (1 << 3)
  588. #define BYPR2OUT4 (1 << 2)
  589. #define RMIX2OUT4 (1 << 1)
  590. #define RDAC2OUT4 (1)
  591. static void write_reg(struct rt_i2c_bus_device *dev, rt_uint16_t s_data)
  592. {
  593. struct rt_i2c_msg msg;
  594. rt_uint8_t send_buffer[2];
  595. RT_ASSERT(dev != RT_NULL);
  596. send_buffer[0] = (rt_uint8_t)(s_data >> 8);
  597. send_buffer[1] = (rt_uint8_t)(s_data);
  598. msg.addr = 0x1A;
  599. msg.flags = RT_I2C_WR;
  600. msg.len = 2;
  601. msg.buf = send_buffer;
  602. rt_i2c_transfer(dev, &msg, 1);
  603. }
  604. /**
  605. * @brief Init WM8978 Codec device.
  606. * @param dev: I2C device handle
  607. * @retval RT_EOK if correct communication, else wrong communication
  608. */
  609. int wm8978_init(struct rt_i2c_bus_device *dev)
  610. {
  611. write_reg(dev, REG_SOFTWARE_RESET);
  612. /* 1.5x boost power up sequence,Mute all outputs. */
  613. write_reg(dev, REG_LOUT1_VOL | LOUT1MUTE);
  614. write_reg(dev, REG_ROUT1_VOL | ROUT1MUTE);
  615. write_reg(dev, REG_LOUT2_VOL | LOUT2MUTE);
  616. write_reg(dev, REG_ROUT2_VOL | ROUT2MUTE);
  617. /* Enable unused output chosen from L/ROUT2, OUT3 or OUT4. */
  618. write_reg(dev, REG_POWER_MANAGEMENT3 | OUT4EN);
  619. /* Set BUFDCOPEN=1 and BUFIOEN=1 in register R1 */
  620. write_reg(dev, REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN);
  621. /* Set SPKBOOST=1 in register R49. */
  622. write_reg(dev, REG_OUTPUT | SPKBOOST);
  623. /* Set VMIDSEL[1:0] to required value in register R1. */
  624. write_reg(dev, REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K);
  625. /* Set L/RMIXEN=1 and DACENL/R=1 in register R3.*/
  626. write_reg(dev, REG_POWER_MANAGEMENT3 | LMIXEN | RMIXEN | DACENL | DACENR);
  627. /* Set BIASEN=1 in register R1. */
  628. write_reg(dev, REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K);
  629. /* Set L/ROUT2EN=1 in register R3. */
  630. write_reg(dev, REG_POWER_MANAGEMENT3 | LMIXEN | RMIXEN | DACENL | DACENR | LOUT2EN | ROUT2EN);
  631. /* Enable other mixers as required. */
  632. /* Enable other outputs as required. */
  633. write_reg(dev, REG_POWER_MANAGEMENT2 | LOUT1EN | ROUT1EN | BOOSTENL | BOOSTENR | INPPGAENL | INPPGAENR);
  634. write_reg(dev, REG_POWER_MANAGEMENT2 | LOUT1EN | ROUT1EN | BOOSTENL | BOOSTENR | INPPGAENL | INPPGAENR | ADCENL | ADCENR);
  635. /* Digital inferface setup. */
  636. write_reg(dev, REG_AUDIO_INTERFACE | BCP_NORMAL | LRP_NORMAL | WL_16BITS | FMT_I2S);
  637. write_reg(dev, REG_ADDITIONAL | WM_SR_8KHZ);
  638. write_reg(dev, REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K | MICBEN | BIASEN);
  639. write_reg(dev, REG_CLOCK_GEN | CLKSEL_MCLK | MCLK_DIV1);
  640. /* Enable DAC 128x oversampling. */
  641. write_reg(dev, REG_DAC | DACOSR128);
  642. /* Set LOUT2/ROUT2 in BTL operation. */
  643. write_reg(dev, REG_BEEP | INVROUT2);
  644. /* MIC config. */
  645. write_reg(dev, REG_INPUT | MBVSEL_0_65AVDD | RIN2INPVGA | RIP2INPVGA | LIN2INPVGA | LIP2INPVGA);
  646. /* MIC PGA -12db to 35.25db, 0.75setp default: 16(0db). */
  647. write_reg(dev, REG_LEFT_PGA_GAIN | INPPGAZCL | (36 & INPPGAVOL_MASK));
  648. write_reg(dev, REG_RIGHT_PGA_GAIN | INPPGAZCR | (36 & INPPGAVOL_MASK) | INPPGAUPDATE);
  649. write_reg(dev, REG_LEFT_ADC_BOOST | PGABOOSTL | L2_2BOOSTVOL_DISABLED | AUXL2BOOSTVOL_DISABLED);
  650. write_reg(dev, REG_RIGHT_ADC_BOOST | PGABOOSTR | R2_2BOOSTVOL_DISABLED | AUXR2BOOSTVOL_DISABLED);
  651. /* Set output volume. */
  652. wm8978_set_volume(dev, 55);
  653. return RT_EOK;
  654. }
  655. /**
  656. * @brief Set WM8978 DAC volume level.
  657. * @param dev: I2C device handle
  658. * @param vol: volume level(0 ~ 99)
  659. * @retval RT_EOK if correct communication, else wrong communication
  660. */
  661. int wm8978_set_volume(struct rt_i2c_bus_device *dev, int vol)
  662. {
  663. vol = 63 * vol / 100;
  664. vol = (vol & VOL_MASK) << VOL_POS;
  665. write_reg(dev, REG_LOUT1_VOL | vol);
  666. write_reg(dev, REG_ROUT1_VOL | HPVU | vol);
  667. write_reg(dev, REG_LOUT2_VOL | vol);
  668. write_reg(dev, REG_ROUT2_VOL | SPKVU | vol);
  669. return RT_EOK;
  670. }