wm8983.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*
  2. * wm8983.c -- WM8983 ALSA SoC Audio driver
  3. *
  4. * Copyright 2011 Wolfson Microelectronics plc
  5. *
  6. * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm.h>
  17. #include <linux/i2c.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/slab.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/initval.h>
  25. #include <sound/tlv.h>
  26. #include "wm8983.h"
  27. static const u16 wm8983_reg_defs[WM8983_MAX_REGISTER + 1] = {
  28. [0x00] = 0x0000, /* R0 - Software Reset */
  29. [0x01] = 0x0000, /* R1 - Power management 1 */
  30. [0x02] = 0x0000, /* R2 - Power management 2 */
  31. [0x03] = 0x0000, /* R3 - Power management 3 */
  32. [0x04] = 0x0050, /* R4 - Audio Interface */
  33. [0x05] = 0x0000, /* R5 - Companding control */
  34. [0x06] = 0x0140, /* R6 - Clock Gen control */
  35. [0x07] = 0x0000, /* R7 - Additional control */
  36. [0x08] = 0x0000, /* R8 - GPIO Control */
  37. [0x09] = 0x0000, /* R9 - Jack Detect Control 1 */
  38. [0x0A] = 0x0000, /* R10 - DAC Control */
  39. [0x0B] = 0x00FF, /* R11 - Left DAC digital Vol */
  40. [0x0C] = 0x00FF, /* R12 - Right DAC digital vol */
  41. [0x0D] = 0x0000, /* R13 - Jack Detect Control 2 */
  42. [0x0E] = 0x0100, /* R14 - ADC Control */
  43. [0x0F] = 0x00FF, /* R15 - Left ADC Digital Vol */
  44. [0x10] = 0x00FF, /* R16 - Right ADC Digital Vol */
  45. [0x12] = 0x012C, /* R18 - EQ1 - low shelf */
  46. [0x13] = 0x002C, /* R19 - EQ2 - peak 1 */
  47. [0x14] = 0x002C, /* R20 - EQ3 - peak 2 */
  48. [0x15] = 0x002C, /* R21 - EQ4 - peak 3 */
  49. [0x16] = 0x002C, /* R22 - EQ5 - high shelf */
  50. [0x18] = 0x0032, /* R24 - DAC Limiter 1 */
  51. [0x19] = 0x0000, /* R25 - DAC Limiter 2 */
  52. [0x1B] = 0x0000, /* R27 - Notch Filter 1 */
  53. [0x1C] = 0x0000, /* R28 - Notch Filter 2 */
  54. [0x1D] = 0x0000, /* R29 - Notch Filter 3 */
  55. [0x1E] = 0x0000, /* R30 - Notch Filter 4 */
  56. [0x20] = 0x0038, /* R32 - ALC control 1 */
  57. [0x21] = 0x000B, /* R33 - ALC control 2 */
  58. [0x22] = 0x0032, /* R34 - ALC control 3 */
  59. [0x23] = 0x0000, /* R35 - Noise Gate */
  60. [0x24] = 0x0008, /* R36 - PLL N */
  61. [0x25] = 0x000C, /* R37 - PLL K 1 */
  62. [0x26] = 0x0093, /* R38 - PLL K 2 */
  63. [0x27] = 0x00E9, /* R39 - PLL K 3 */
  64. [0x29] = 0x0000, /* R41 - 3D control */
  65. [0x2A] = 0x0000, /* R42 - OUT4 to ADC */
  66. [0x2B] = 0x0000, /* R43 - Beep control */
  67. [0x2C] = 0x0033, /* R44 - Input ctrl */
  68. [0x2D] = 0x0010, /* R45 - Left INP PGA gain ctrl */
  69. [0x2E] = 0x0010, /* R46 - Right INP PGA gain ctrl */
  70. [0x2F] = 0x0100, /* R47 - Left ADC BOOST ctrl */
  71. [0x30] = 0x0100, /* R48 - Right ADC BOOST ctrl */
  72. [0x31] = 0x0002, /* R49 - Output ctrl */
  73. [0x32] = 0x0001, /* R50 - Left mixer ctrl */
  74. [0x33] = 0x0001, /* R51 - Right mixer ctrl */
  75. [0x34] = 0x0039, /* R52 - LOUT1 (HP) volume ctrl */
  76. [0x35] = 0x0039, /* R53 - ROUT1 (HP) volume ctrl */
  77. [0x36] = 0x0039, /* R54 - LOUT2 (SPK) volume ctrl */
  78. [0x37] = 0x0039, /* R55 - ROUT2 (SPK) volume ctrl */
  79. [0x38] = 0x0001, /* R56 - OUT3 mixer ctrl */
  80. [0x39] = 0x0001, /* R57 - OUT4 (MONO) mix ctrl */
  81. [0x3D] = 0x0000 /* R61 - BIAS CTRL */
  82. };
  83. static const struct wm8983_reg_access {
  84. u16 read; /* Mask of readable bits */
  85. u16 write; /* Mask of writable bits */
  86. } wm8983_access_masks[WM8983_MAX_REGISTER + 1] = {
  87. [0x00] = { 0x0000, 0x01FF }, /* R0 - Software Reset */
  88. [0x01] = { 0x0000, 0x01FF }, /* R1 - Power management 1 */
  89. [0x02] = { 0x0000, 0x01FF }, /* R2 - Power management 2 */
  90. [0x03] = { 0x0000, 0x01EF }, /* R3 - Power management 3 */
  91. [0x04] = { 0x0000, 0x01FF }, /* R4 - Audio Interface */
  92. [0x05] = { 0x0000, 0x003F }, /* R5 - Companding control */
  93. [0x06] = { 0x0000, 0x01FD }, /* R6 - Clock Gen control */
  94. [0x07] = { 0x0000, 0x000F }, /* R7 - Additional control */
  95. [0x08] = { 0x0000, 0x003F }, /* R8 - GPIO Control */
  96. [0x09] = { 0x0000, 0x0070 }, /* R9 - Jack Detect Control 1 */
  97. [0x0A] = { 0x0000, 0x004F }, /* R10 - DAC Control */
  98. [0x0B] = { 0x0000, 0x01FF }, /* R11 - Left DAC digital Vol */
  99. [0x0C] = { 0x0000, 0x01FF }, /* R12 - Right DAC digital vol */
  100. [0x0D] = { 0x0000, 0x00FF }, /* R13 - Jack Detect Control 2 */
  101. [0x0E] = { 0x0000, 0x01FB }, /* R14 - ADC Control */
  102. [0x0F] = { 0x0000, 0x01FF }, /* R15 - Left ADC Digital Vol */
  103. [0x10] = { 0x0000, 0x01FF }, /* R16 - Right ADC Digital Vol */
  104. [0x12] = { 0x0000, 0x017F }, /* R18 - EQ1 - low shelf */
  105. [0x13] = { 0x0000, 0x017F }, /* R19 - EQ2 - peak 1 */
  106. [0x14] = { 0x0000, 0x017F }, /* R20 - EQ3 - peak 2 */
  107. [0x15] = { 0x0000, 0x017F }, /* R21 - EQ4 - peak 3 */
  108. [0x16] = { 0x0000, 0x007F }, /* R22 - EQ5 - high shelf */
  109. [0x18] = { 0x0000, 0x01FF }, /* R24 - DAC Limiter 1 */
  110. [0x19] = { 0x0000, 0x007F }, /* R25 - DAC Limiter 2 */
  111. [0x1B] = { 0x0000, 0x01FF }, /* R27 - Notch Filter 1 */
  112. [0x1C] = { 0x0000, 0x017F }, /* R28 - Notch Filter 2 */
  113. [0x1D] = { 0x0000, 0x017F }, /* R29 - Notch Filter 3 */
  114. [0x1E] = { 0x0000, 0x017F }, /* R30 - Notch Filter 4 */
  115. [0x20] = { 0x0000, 0x01BF }, /* R32 - ALC control 1 */
  116. [0x21] = { 0x0000, 0x00FF }, /* R33 - ALC control 2 */
  117. [0x22] = { 0x0000, 0x01FF }, /* R34 - ALC control 3 */
  118. [0x23] = { 0x0000, 0x000F }, /* R35 - Noise Gate */
  119. [0x24] = { 0x0000, 0x001F }, /* R36 - PLL N */
  120. [0x25] = { 0x0000, 0x003F }, /* R37 - PLL K 1 */
  121. [0x26] = { 0x0000, 0x01FF }, /* R38 - PLL K 2 */
  122. [0x27] = { 0x0000, 0x01FF }, /* R39 - PLL K 3 */
  123. [0x29] = { 0x0000, 0x000F }, /* R41 - 3D control */
  124. [0x2A] = { 0x0000, 0x01E7 }, /* R42 - OUT4 to ADC */
  125. [0x2B] = { 0x0000, 0x01BF }, /* R43 - Beep control */
  126. [0x2C] = { 0x0000, 0x0177 }, /* R44 - Input ctrl */
  127. [0x2D] = { 0x0000, 0x01FF }, /* R45 - Left INP PGA gain ctrl */
  128. [0x2E] = { 0x0000, 0x01FF }, /* R46 - Right INP PGA gain ctrl */
  129. [0x2F] = { 0x0000, 0x0177 }, /* R47 - Left ADC BOOST ctrl */
  130. [0x30] = { 0x0000, 0x0177 }, /* R48 - Right ADC BOOST ctrl */
  131. [0x31] = { 0x0000, 0x007F }, /* R49 - Output ctrl */
  132. [0x32] = { 0x0000, 0x01FF }, /* R50 - Left mixer ctrl */
  133. [0x33] = { 0x0000, 0x01FF }, /* R51 - Right mixer ctrl */
  134. [0x34] = { 0x0000, 0x01FF }, /* R52 - LOUT1 (HP) volume ctrl */
  135. [0x35] = { 0x0000, 0x01FF }, /* R53 - ROUT1 (HP) volume ctrl */
  136. [0x36] = { 0x0000, 0x01FF }, /* R54 - LOUT2 (SPK) volume ctrl */
  137. [0x37] = { 0x0000, 0x01FF }, /* R55 - ROUT2 (SPK) volume ctrl */
  138. [0x38] = { 0x0000, 0x004F }, /* R56 - OUT3 mixer ctrl */
  139. [0x39] = { 0x0000, 0x00FF }, /* R57 - OUT4 (MONO) mix ctrl */
  140. [0x3D] = { 0x0000, 0x0100 } /* R61 - BIAS CTRL */
  141. };
  142. /* vol/gain update regs */
  143. static const int vol_update_regs[] = {
  144. WM8983_LEFT_DAC_DIGITAL_VOL,
  145. WM8983_RIGHT_DAC_DIGITAL_VOL,
  146. WM8983_LEFT_ADC_DIGITAL_VOL,
  147. WM8983_RIGHT_ADC_DIGITAL_VOL,
  148. WM8983_LOUT1_HP_VOLUME_CTRL,
  149. WM8983_ROUT1_HP_VOLUME_CTRL,
  150. WM8983_LOUT2_SPK_VOLUME_CTRL,
  151. WM8983_ROUT2_SPK_VOLUME_CTRL,
  152. WM8983_LEFT_INP_PGA_GAIN_CTRL,
  153. WM8983_RIGHT_INP_PGA_GAIN_CTRL
  154. };
  155. struct wm8983_priv {
  156. enum snd_soc_control_type control_type;
  157. u32 sysclk;
  158. u32 bclk;
  159. };
  160. static const struct {
  161. int div;
  162. int ratio;
  163. } fs_ratios[] = {
  164. { 10, 128 },
  165. { 15, 192 },
  166. { 20, 256 },
  167. { 30, 384 },
  168. { 40, 512 },
  169. { 60, 768 },
  170. { 80, 1024 },
  171. { 120, 1536 }
  172. };
  173. static const int srates[] = { 48000, 32000, 24000, 16000, 12000, 8000 };
  174. static const int bclk_divs[] = {
  175. 1, 2, 4, 8, 16, 32
  176. };
  177. static int eqmode_get(struct snd_kcontrol *kcontrol,
  178. struct snd_ctl_elem_value *ucontrol);
  179. static int eqmode_put(struct snd_kcontrol *kcontrol,
  180. struct snd_ctl_elem_value *ucontrol);
  181. static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1);
  182. static const DECLARE_TLV_DB_SCALE(adc_tlv, -12700, 50, 1);
  183. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  184. static const DECLARE_TLV_DB_SCALE(lim_thresh_tlv, -600, 100, 0);
  185. static const DECLARE_TLV_DB_SCALE(lim_boost_tlv, 0, 100, 0);
  186. static const DECLARE_TLV_DB_SCALE(alc_min_tlv, -1200, 600, 0);
  187. static const DECLARE_TLV_DB_SCALE(alc_max_tlv, -675, 600, 0);
  188. static const DECLARE_TLV_DB_SCALE(alc_tar_tlv, -2250, 150, 0);
  189. static const DECLARE_TLV_DB_SCALE(pga_vol_tlv, -1200, 75, 0);
  190. static const DECLARE_TLV_DB_SCALE(boost_tlv, -1200, 300, 1);
  191. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  192. static const DECLARE_TLV_DB_SCALE(aux_tlv, -1500, 300, 0);
  193. static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
  194. static const DECLARE_TLV_DB_SCALE(pga_boost_tlv, 0, 2000, 0);
  195. static const char *alc_sel_text[] = { "Off", "Right", "Left", "Stereo" };
  196. static const SOC_ENUM_SINGLE_DECL(alc_sel, WM8983_ALC_CONTROL_1, 7,
  197. alc_sel_text);
  198. static const char *alc_mode_text[] = { "ALC", "Limiter" };
  199. static const SOC_ENUM_SINGLE_DECL(alc_mode, WM8983_ALC_CONTROL_3, 8,
  200. alc_mode_text);
  201. static const char *filter_mode_text[] = { "Audio", "Application" };
  202. static const SOC_ENUM_SINGLE_DECL(filter_mode, WM8983_ADC_CONTROL, 7,
  203. filter_mode_text);
  204. static const char *eq_bw_text[] = { "Narrow", "Wide" };
  205. static const char *eqmode_text[] = { "Capture", "Playback" };
  206. static const SOC_ENUM_SINGLE_EXT_DECL(eqmode, eqmode_text);
  207. static const char *eq1_cutoff_text[] = {
  208. "80Hz", "105Hz", "135Hz", "175Hz"
  209. };
  210. static const SOC_ENUM_SINGLE_DECL(eq1_cutoff, WM8983_EQ1_LOW_SHELF, 5,
  211. eq1_cutoff_text);
  212. static const char *eq2_cutoff_text[] = {
  213. "230Hz", "300Hz", "385Hz", "500Hz"
  214. };
  215. static const SOC_ENUM_SINGLE_DECL(eq2_bw, WM8983_EQ2_PEAK_1, 8, eq_bw_text);
  216. static const SOC_ENUM_SINGLE_DECL(eq2_cutoff, WM8983_EQ2_PEAK_1, 5,
  217. eq2_cutoff_text);
  218. static const char *eq3_cutoff_text[] = {
  219. "650Hz", "850Hz", "1.1kHz", "1.4kHz"
  220. };
  221. static const SOC_ENUM_SINGLE_DECL(eq3_bw, WM8983_EQ3_PEAK_2, 8, eq_bw_text);
  222. static const SOC_ENUM_SINGLE_DECL(eq3_cutoff, WM8983_EQ3_PEAK_2, 5,
  223. eq3_cutoff_text);
  224. static const char *eq4_cutoff_text[] = {
  225. "1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz"
  226. };
  227. static const SOC_ENUM_SINGLE_DECL(eq4_bw, WM8983_EQ4_PEAK_3, 8, eq_bw_text);
  228. static const SOC_ENUM_SINGLE_DECL(eq4_cutoff, WM8983_EQ4_PEAK_3, 5,
  229. eq4_cutoff_text);
  230. static const char *eq5_cutoff_text[] = {
  231. "5.3kHz", "6.9kHz", "9kHz", "11.7kHz"
  232. };
  233. static const SOC_ENUM_SINGLE_DECL(eq5_cutoff, WM8983_EQ5_HIGH_SHELF, 5,
  234. eq5_cutoff_text);
  235. static const char *depth_3d_text[] = {
  236. "Off",
  237. "6.67%",
  238. "13.3%",
  239. "20%",
  240. "26.7%",
  241. "33.3%",
  242. "40%",
  243. "46.6%",
  244. "53.3%",
  245. "60%",
  246. "66.7%",
  247. "73.3%",
  248. "80%",
  249. "86.7%",
  250. "93.3%",
  251. "100%"
  252. };
  253. static const SOC_ENUM_SINGLE_DECL(depth_3d, WM8983_3D_CONTROL, 0,
  254. depth_3d_text);
  255. static const struct snd_kcontrol_new wm8983_snd_controls[] = {
  256. SOC_SINGLE("Digital Loopback Switch", WM8983_COMPANDING_CONTROL,
  257. 0, 1, 0),
  258. SOC_ENUM("ALC Capture Function", alc_sel),
  259. SOC_SINGLE_TLV("ALC Capture Max Volume", WM8983_ALC_CONTROL_1,
  260. 3, 7, 0, alc_max_tlv),
  261. SOC_SINGLE_TLV("ALC Capture Min Volume", WM8983_ALC_CONTROL_1,
  262. 0, 7, 0, alc_min_tlv),
  263. SOC_SINGLE_TLV("ALC Capture Target Volume", WM8983_ALC_CONTROL_2,
  264. 0, 15, 0, alc_tar_tlv),
  265. SOC_SINGLE("ALC Capture Attack", WM8983_ALC_CONTROL_3, 0, 10, 0),
  266. SOC_SINGLE("ALC Capture Hold", WM8983_ALC_CONTROL_2, 4, 10, 0),
  267. SOC_SINGLE("ALC Capture Decay", WM8983_ALC_CONTROL_3, 4, 10, 0),
  268. SOC_ENUM("ALC Mode", alc_mode),
  269. SOC_SINGLE("ALC Capture NG Switch", WM8983_NOISE_GATE,
  270. 3, 1, 0),
  271. SOC_SINGLE("ALC Capture NG Threshold", WM8983_NOISE_GATE,
  272. 0, 7, 1),
  273. SOC_DOUBLE_R_TLV("Capture Volume", WM8983_LEFT_ADC_DIGITAL_VOL,
  274. WM8983_RIGHT_ADC_DIGITAL_VOL, 0, 255, 0, adc_tlv),
  275. SOC_DOUBLE_R("Capture PGA ZC Switch", WM8983_LEFT_INP_PGA_GAIN_CTRL,
  276. WM8983_RIGHT_INP_PGA_GAIN_CTRL, 7, 1, 0),
  277. SOC_DOUBLE_R_TLV("Capture PGA Volume", WM8983_LEFT_INP_PGA_GAIN_CTRL,
  278. WM8983_RIGHT_INP_PGA_GAIN_CTRL, 0, 63, 0, pga_vol_tlv),
  279. SOC_DOUBLE_R_TLV("Capture PGA Boost Volume",
  280. WM8983_LEFT_ADC_BOOST_CTRL, WM8983_RIGHT_ADC_BOOST_CTRL,
  281. 8, 1, 0, pga_boost_tlv),
  282. SOC_DOUBLE("ADC Inversion Switch", WM8983_ADC_CONTROL, 0, 1, 1, 0),
  283. SOC_SINGLE("ADC 128x Oversampling Switch", WM8983_ADC_CONTROL, 8, 1, 0),
  284. SOC_DOUBLE_R_TLV("Playback Volume", WM8983_LEFT_DAC_DIGITAL_VOL,
  285. WM8983_RIGHT_DAC_DIGITAL_VOL, 0, 255, 0, dac_tlv),
  286. SOC_SINGLE("DAC Playback Limiter Switch", WM8983_DAC_LIMITER_1, 8, 1, 0),
  287. SOC_SINGLE("DAC Playback Limiter Decay", WM8983_DAC_LIMITER_1, 4, 10, 0),
  288. SOC_SINGLE("DAC Playback Limiter Attack", WM8983_DAC_LIMITER_1, 0, 11, 0),
  289. SOC_SINGLE_TLV("DAC Playback Limiter Threshold", WM8983_DAC_LIMITER_2,
  290. 4, 7, 1, lim_thresh_tlv),
  291. SOC_SINGLE_TLV("DAC Playback Limiter Boost Volume", WM8983_DAC_LIMITER_2,
  292. 0, 12, 0, lim_boost_tlv),
  293. SOC_DOUBLE("DAC Inversion Switch", WM8983_DAC_CONTROL, 0, 1, 1, 0),
  294. SOC_SINGLE("DAC Auto Mute Switch", WM8983_DAC_CONTROL, 2, 1, 0),
  295. SOC_SINGLE("DAC 128x Oversampling Switch", WM8983_DAC_CONTROL, 3, 1, 0),
  296. SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8983_LOUT1_HP_VOLUME_CTRL,
  297. WM8983_ROUT1_HP_VOLUME_CTRL, 0, 63, 0, out_tlv),
  298. SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8983_LOUT1_HP_VOLUME_CTRL,
  299. WM8983_ROUT1_HP_VOLUME_CTRL, 7, 1, 0),
  300. SOC_DOUBLE_R("Headphone Switch", WM8983_LOUT1_HP_VOLUME_CTRL,
  301. WM8983_ROUT1_HP_VOLUME_CTRL, 6, 1, 1),
  302. SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8983_LOUT2_SPK_VOLUME_CTRL,
  303. WM8983_ROUT2_SPK_VOLUME_CTRL, 0, 63, 0, out_tlv),
  304. SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8983_LOUT2_SPK_VOLUME_CTRL,
  305. WM8983_ROUT2_SPK_VOLUME_CTRL, 7, 1, 0),
  306. SOC_DOUBLE_R("Speaker Switch", WM8983_LOUT2_SPK_VOLUME_CTRL,
  307. WM8983_ROUT2_SPK_VOLUME_CTRL, 6, 1, 1),
  308. SOC_SINGLE("OUT3 Switch", WM8983_OUT3_MIXER_CTRL,
  309. 6, 1, 1),
  310. SOC_SINGLE("OUT4 Switch", WM8983_OUT4_MONO_MIX_CTRL,
  311. 6, 1, 1),
  312. SOC_SINGLE("High Pass Filter Switch", WM8983_ADC_CONTROL, 8, 1, 0),
  313. SOC_ENUM("High Pass Filter Mode", filter_mode),
  314. SOC_SINGLE("High Pass Filter Cutoff", WM8983_ADC_CONTROL, 4, 7, 0),
  315. SOC_DOUBLE_R_TLV("Aux Bypass Volume",
  316. WM8983_LEFT_MIXER_CTRL, WM8983_RIGHT_MIXER_CTRL, 6, 7, 0,
  317. aux_tlv),
  318. SOC_DOUBLE_R_TLV("Input PGA Bypass Volume",
  319. WM8983_LEFT_MIXER_CTRL, WM8983_RIGHT_MIXER_CTRL, 2, 7, 0,
  320. bypass_tlv),
  321. SOC_ENUM_EXT("Equalizer Function", eqmode, eqmode_get, eqmode_put),
  322. SOC_ENUM("EQ1 Cutoff", eq1_cutoff),
  323. SOC_SINGLE_TLV("EQ1 Volume", WM8983_EQ1_LOW_SHELF, 0, 24, 1, eq_tlv),
  324. SOC_ENUM("EQ2 Bandwith", eq2_bw),
  325. SOC_ENUM("EQ2 Cutoff", eq2_cutoff),
  326. SOC_SINGLE_TLV("EQ2 Volume", WM8983_EQ2_PEAK_1, 0, 24, 1, eq_tlv),
  327. SOC_ENUM("EQ3 Bandwith", eq3_bw),
  328. SOC_ENUM("EQ3 Cutoff", eq3_cutoff),
  329. SOC_SINGLE_TLV("EQ3 Volume", WM8983_EQ3_PEAK_2, 0, 24, 1, eq_tlv),
  330. SOC_ENUM("EQ4 Bandwith", eq4_bw),
  331. SOC_ENUM("EQ4 Cutoff", eq4_cutoff),
  332. SOC_SINGLE_TLV("EQ4 Volume", WM8983_EQ4_PEAK_3, 0, 24, 1, eq_tlv),
  333. SOC_ENUM("EQ5 Cutoff", eq5_cutoff),
  334. SOC_SINGLE_TLV("EQ5 Volume", WM8983_EQ5_HIGH_SHELF, 0, 24, 1, eq_tlv),
  335. SOC_ENUM("3D Depth", depth_3d),
  336. };
  337. static const struct snd_kcontrol_new left_out_mixer[] = {
  338. SOC_DAPM_SINGLE("Line Switch", WM8983_LEFT_MIXER_CTRL, 1, 1, 0),
  339. SOC_DAPM_SINGLE("Aux Switch", WM8983_LEFT_MIXER_CTRL, 5, 1, 0),
  340. SOC_DAPM_SINGLE("PCM Switch", WM8983_LEFT_MIXER_CTRL, 0, 1, 0),
  341. };
  342. static const struct snd_kcontrol_new right_out_mixer[] = {
  343. SOC_DAPM_SINGLE("Line Switch", WM8983_RIGHT_MIXER_CTRL, 1, 1, 0),
  344. SOC_DAPM_SINGLE("Aux Switch", WM8983_RIGHT_MIXER_CTRL, 5, 1, 0),
  345. SOC_DAPM_SINGLE("PCM Switch", WM8983_RIGHT_MIXER_CTRL, 0, 1, 0),
  346. };
  347. static const struct snd_kcontrol_new left_input_mixer[] = {
  348. SOC_DAPM_SINGLE("L2 Switch", WM8983_INPUT_CTRL, 2, 1, 0),
  349. SOC_DAPM_SINGLE("MicN Switch", WM8983_INPUT_CTRL, 1, 1, 0),
  350. SOC_DAPM_SINGLE("MicP Switch", WM8983_INPUT_CTRL, 0, 1, 0),
  351. };
  352. static const struct snd_kcontrol_new right_input_mixer[] = {
  353. SOC_DAPM_SINGLE("R2 Switch", WM8983_INPUT_CTRL, 6, 1, 0),
  354. SOC_DAPM_SINGLE("MicN Switch", WM8983_INPUT_CTRL, 5, 1, 0),
  355. SOC_DAPM_SINGLE("MicP Switch", WM8983_INPUT_CTRL, 4, 1, 0),
  356. };
  357. static const struct snd_kcontrol_new left_boost_mixer[] = {
  358. SOC_DAPM_SINGLE_TLV("L2 Volume", WM8983_LEFT_ADC_BOOST_CTRL,
  359. 4, 7, 0, boost_tlv),
  360. SOC_DAPM_SINGLE_TLV("AUXL Volume", WM8983_LEFT_ADC_BOOST_CTRL,
  361. 0, 7, 0, boost_tlv)
  362. };
  363. static const struct snd_kcontrol_new out3_mixer[] = {
  364. SOC_DAPM_SINGLE("LMIX2OUT3 Switch", WM8983_OUT3_MIXER_CTRL,
  365. 1, 1, 0),
  366. SOC_DAPM_SINGLE("LDAC2OUT3 Switch", WM8983_OUT3_MIXER_CTRL,
  367. 0, 1, 0),
  368. };
  369. static const struct snd_kcontrol_new out4_mixer[] = {
  370. SOC_DAPM_SINGLE("LMIX2OUT4 Switch", WM8983_OUT4_MONO_MIX_CTRL,
  371. 4, 1, 0),
  372. SOC_DAPM_SINGLE("RMIX2OUT4 Switch", WM8983_OUT4_MONO_MIX_CTRL,
  373. 1, 1, 0),
  374. SOC_DAPM_SINGLE("LDAC2OUT4 Switch", WM8983_OUT4_MONO_MIX_CTRL,
  375. 3, 1, 0),
  376. SOC_DAPM_SINGLE("RDAC2OUT4 Switch", WM8983_OUT4_MONO_MIX_CTRL,
  377. 0, 1, 0),
  378. };
  379. static const struct snd_kcontrol_new right_boost_mixer[] = {
  380. SOC_DAPM_SINGLE_TLV("R2 Volume", WM8983_RIGHT_ADC_BOOST_CTRL,
  381. 4, 7, 0, boost_tlv),
  382. SOC_DAPM_SINGLE_TLV("AUXR Volume", WM8983_RIGHT_ADC_BOOST_CTRL,
  383. 0, 7, 0, boost_tlv)
  384. };
  385. static const struct snd_soc_dapm_widget wm8983_dapm_widgets[] = {
  386. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8983_POWER_MANAGEMENT_3,
  387. 0, 0),
  388. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8983_POWER_MANAGEMENT_3,
  389. 1, 0),
  390. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8983_POWER_MANAGEMENT_2,
  391. 0, 0),
  392. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8983_POWER_MANAGEMENT_2,
  393. 1, 0),
  394. SND_SOC_DAPM_MIXER("Left Output Mixer", WM8983_POWER_MANAGEMENT_3,
  395. 2, 0, left_out_mixer, ARRAY_SIZE(left_out_mixer)),
  396. SND_SOC_DAPM_MIXER("Right Output Mixer", WM8983_POWER_MANAGEMENT_3,
  397. 3, 0, right_out_mixer, ARRAY_SIZE(right_out_mixer)),
  398. SND_SOC_DAPM_MIXER("Left Input Mixer", WM8983_POWER_MANAGEMENT_2,
  399. 2, 0, left_input_mixer, ARRAY_SIZE(left_input_mixer)),
  400. SND_SOC_DAPM_MIXER("Right Input Mixer", WM8983_POWER_MANAGEMENT_2,
  401. 3, 0, right_input_mixer, ARRAY_SIZE(right_input_mixer)),
  402. SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8983_POWER_MANAGEMENT_2,
  403. 4, 0, left_boost_mixer, ARRAY_SIZE(left_boost_mixer)),
  404. SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8983_POWER_MANAGEMENT_2,
  405. 5, 0, right_boost_mixer, ARRAY_SIZE(right_boost_mixer)),
  406. SND_SOC_DAPM_MIXER("OUT3 Mixer", WM8983_POWER_MANAGEMENT_1,
  407. 6, 0, out3_mixer, ARRAY_SIZE(out3_mixer)),
  408. SND_SOC_DAPM_MIXER("OUT4 Mixer", WM8983_POWER_MANAGEMENT_1,
  409. 7, 0, out4_mixer, ARRAY_SIZE(out4_mixer)),
  410. SND_SOC_DAPM_PGA("Left Capture PGA", WM8983_LEFT_INP_PGA_GAIN_CTRL,
  411. 6, 1, NULL, 0),
  412. SND_SOC_DAPM_PGA("Right Capture PGA", WM8983_RIGHT_INP_PGA_GAIN_CTRL,
  413. 6, 1, NULL, 0),
  414. SND_SOC_DAPM_PGA("Left Headphone Out", WM8983_POWER_MANAGEMENT_2,
  415. 7, 0, NULL, 0),
  416. SND_SOC_DAPM_PGA("Right Headphone Out", WM8983_POWER_MANAGEMENT_2,
  417. 8, 0, NULL, 0),
  418. SND_SOC_DAPM_PGA("Left Speaker Out", WM8983_POWER_MANAGEMENT_3,
  419. 5, 0, NULL, 0),
  420. SND_SOC_DAPM_PGA("Right Speaker Out", WM8983_POWER_MANAGEMENT_3,
  421. 6, 0, NULL, 0),
  422. SND_SOC_DAPM_PGA("OUT3 Out", WM8983_POWER_MANAGEMENT_3,
  423. 7, 0, NULL, 0),
  424. SND_SOC_DAPM_PGA("OUT4 Out", WM8983_POWER_MANAGEMENT_3,
  425. 8, 0, NULL, 0),
  426. SND_SOC_DAPM_SUPPLY("Mic Bias", WM8983_POWER_MANAGEMENT_1, 4, 0,
  427. NULL, 0),
  428. SND_SOC_DAPM_INPUT("LIN"),
  429. SND_SOC_DAPM_INPUT("LIP"),
  430. SND_SOC_DAPM_INPUT("RIN"),
  431. SND_SOC_DAPM_INPUT("RIP"),
  432. SND_SOC_DAPM_INPUT("AUXL"),
  433. SND_SOC_DAPM_INPUT("AUXR"),
  434. SND_SOC_DAPM_INPUT("L2"),
  435. SND_SOC_DAPM_INPUT("R2"),
  436. SND_SOC_DAPM_OUTPUT("HPL"),
  437. SND_SOC_DAPM_OUTPUT("HPR"),
  438. SND_SOC_DAPM_OUTPUT("SPKL"),
  439. SND_SOC_DAPM_OUTPUT("SPKR"),
  440. SND_SOC_DAPM_OUTPUT("OUT3"),
  441. SND_SOC_DAPM_OUTPUT("OUT4")
  442. };
  443. static const struct snd_soc_dapm_route wm8983_audio_map[] = {
  444. { "OUT3 Mixer", "LMIX2OUT3 Switch", "Left Output Mixer" },
  445. { "OUT3 Mixer", "LDAC2OUT3 Switch", "Left DAC" },
  446. { "OUT3 Out", NULL, "OUT3 Mixer" },
  447. { "OUT3", NULL, "OUT3 Out" },
  448. { "OUT4 Mixer", "LMIX2OUT4 Switch", "Left Output Mixer" },
  449. { "OUT4 Mixer", "RMIX2OUT4 Switch", "Right Output Mixer" },
  450. { "OUT4 Mixer", "LDAC2OUT4 Switch", "Left DAC" },
  451. { "OUT4 Mixer", "RDAC2OUT4 Switch", "Right DAC" },
  452. { "OUT4 Out", NULL, "OUT4 Mixer" },
  453. { "OUT4", NULL, "OUT4 Out" },
  454. { "Right Output Mixer", "PCM Switch", "Right DAC" },
  455. { "Right Output Mixer", "Aux Switch", "AUXR" },
  456. { "Right Output Mixer", "Line Switch", "Right Boost Mixer" },
  457. { "Left Output Mixer", "PCM Switch", "Left DAC" },
  458. { "Left Output Mixer", "Aux Switch", "AUXL" },
  459. { "Left Output Mixer", "Line Switch", "Left Boost Mixer" },
  460. { "Right Headphone Out", NULL, "Right Output Mixer" },
  461. { "HPR", NULL, "Right Headphone Out" },
  462. { "Left Headphone Out", NULL, "Left Output Mixer" },
  463. { "HPL", NULL, "Left Headphone Out" },
  464. { "Right Speaker Out", NULL, "Right Output Mixer" },
  465. { "SPKR", NULL, "Right Speaker Out" },
  466. { "Left Speaker Out", NULL, "Left Output Mixer" },
  467. { "SPKL", NULL, "Left Speaker Out" },
  468. { "Right ADC", NULL, "Right Boost Mixer" },
  469. { "Right Boost Mixer", "AUXR Volume", "AUXR" },
  470. { "Right Boost Mixer", NULL, "Right Capture PGA" },
  471. { "Right Boost Mixer", "R2 Volume", "R2" },
  472. { "Left ADC", NULL, "Left Boost Mixer" },
  473. { "Left Boost Mixer", "AUXL Volume", "AUXL" },
  474. { "Left Boost Mixer", NULL, "Left Capture PGA" },
  475. { "Left Boost Mixer", "L2 Volume", "L2" },
  476. { "Right Capture PGA", NULL, "Right Input Mixer" },
  477. { "Left Capture PGA", NULL, "Left Input Mixer" },
  478. { "Right Input Mixer", "R2 Switch", "R2" },
  479. { "Right Input Mixer", "MicN Switch", "RIN" },
  480. { "Right Input Mixer", "MicP Switch", "RIP" },
  481. { "Left Input Mixer", "L2 Switch", "L2" },
  482. { "Left Input Mixer", "MicN Switch", "LIN" },
  483. { "Left Input Mixer", "MicP Switch", "LIP" },
  484. };
  485. static int eqmode_get(struct snd_kcontrol *kcontrol,
  486. struct snd_ctl_elem_value *ucontrol)
  487. {
  488. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  489. unsigned int reg;
  490. reg = snd_soc_read(codec, WM8983_EQ1_LOW_SHELF);
  491. if (reg & WM8983_EQ3DMODE)
  492. ucontrol->value.integer.value[0] = 1;
  493. else
  494. ucontrol->value.integer.value[0] = 0;
  495. return 0;
  496. }
  497. static int eqmode_put(struct snd_kcontrol *kcontrol,
  498. struct snd_ctl_elem_value *ucontrol)
  499. {
  500. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  501. unsigned int regpwr2, regpwr3;
  502. unsigned int reg_eq;
  503. if (ucontrol->value.integer.value[0] != 0
  504. && ucontrol->value.integer.value[0] != 1)
  505. return -EINVAL;
  506. reg_eq = snd_soc_read(codec, WM8983_EQ1_LOW_SHELF);
  507. switch ((reg_eq & WM8983_EQ3DMODE) >> WM8983_EQ3DMODE_SHIFT) {
  508. case 0:
  509. if (!ucontrol->value.integer.value[0])
  510. return 0;
  511. break;
  512. case 1:
  513. if (ucontrol->value.integer.value[0])
  514. return 0;
  515. break;
  516. }
  517. regpwr2 = snd_soc_read(codec, WM8983_POWER_MANAGEMENT_2);
  518. regpwr3 = snd_soc_read(codec, WM8983_POWER_MANAGEMENT_3);
  519. /* disable the DACs and ADCs */
  520. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_2,
  521. WM8983_ADCENR_MASK | WM8983_ADCENL_MASK, 0);
  522. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_3,
  523. WM8983_DACENR_MASK | WM8983_DACENL_MASK, 0);
  524. /* set the desired eqmode */
  525. snd_soc_update_bits(codec, WM8983_EQ1_LOW_SHELF,
  526. WM8983_EQ3DMODE_MASK,
  527. ucontrol->value.integer.value[0]
  528. << WM8983_EQ3DMODE_SHIFT);
  529. /* restore DAC/ADC configuration */
  530. snd_soc_write(codec, WM8983_POWER_MANAGEMENT_2, regpwr2);
  531. snd_soc_write(codec, WM8983_POWER_MANAGEMENT_3, regpwr3);
  532. return 0;
  533. }
  534. static int wm8983_readable(struct snd_soc_codec *codec, unsigned int reg)
  535. {
  536. if (reg > WM8983_MAX_REGISTER)
  537. return 0;
  538. return wm8983_access_masks[reg].read != 0;
  539. }
  540. static int wm8983_dac_mute(struct snd_soc_dai *dai, int mute)
  541. {
  542. struct snd_soc_codec *codec = dai->codec;
  543. return snd_soc_update_bits(codec, WM8983_DAC_CONTROL,
  544. WM8983_SOFTMUTE_MASK,
  545. !!mute << WM8983_SOFTMUTE_SHIFT);
  546. }
  547. static int wm8983_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  548. {
  549. struct snd_soc_codec *codec = dai->codec;
  550. u16 format, master, bcp, lrp;
  551. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  552. case SND_SOC_DAIFMT_I2S:
  553. format = 0x2;
  554. break;
  555. case SND_SOC_DAIFMT_RIGHT_J:
  556. format = 0x0;
  557. break;
  558. case SND_SOC_DAIFMT_LEFT_J:
  559. format = 0x1;
  560. break;
  561. case SND_SOC_DAIFMT_DSP_A:
  562. case SND_SOC_DAIFMT_DSP_B:
  563. format = 0x3;
  564. break;
  565. default:
  566. dev_err(dai->dev, "Unknown dai format\n");
  567. return -EINVAL;
  568. }
  569. snd_soc_update_bits(codec, WM8983_AUDIO_INTERFACE,
  570. WM8983_FMT_MASK, format << WM8983_FMT_SHIFT);
  571. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  572. case SND_SOC_DAIFMT_CBM_CFM:
  573. master = 1;
  574. break;
  575. case SND_SOC_DAIFMT_CBS_CFS:
  576. master = 0;
  577. break;
  578. default:
  579. dev_err(dai->dev, "Unknown master/slave configuration\n");
  580. return -EINVAL;
  581. }
  582. snd_soc_update_bits(codec, WM8983_CLOCK_GEN_CONTROL,
  583. WM8983_MS_MASK, master << WM8983_MS_SHIFT);
  584. /* FIXME: We don't currently support DSP A/B modes */
  585. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  586. case SND_SOC_DAIFMT_DSP_A:
  587. case SND_SOC_DAIFMT_DSP_B:
  588. dev_err(dai->dev, "DSP A/B modes are not supported\n");
  589. return -EINVAL;
  590. default:
  591. break;
  592. }
  593. bcp = lrp = 0;
  594. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  595. case SND_SOC_DAIFMT_NB_NF:
  596. break;
  597. case SND_SOC_DAIFMT_IB_IF:
  598. bcp = lrp = 1;
  599. break;
  600. case SND_SOC_DAIFMT_IB_NF:
  601. bcp = 1;
  602. break;
  603. case SND_SOC_DAIFMT_NB_IF:
  604. lrp = 1;
  605. break;
  606. default:
  607. dev_err(dai->dev, "Unknown polarity configuration\n");
  608. return -EINVAL;
  609. }
  610. snd_soc_update_bits(codec, WM8983_AUDIO_INTERFACE,
  611. WM8983_LRCP_MASK, lrp << WM8983_LRCP_SHIFT);
  612. snd_soc_update_bits(codec, WM8983_AUDIO_INTERFACE,
  613. WM8983_BCP_MASK, bcp << WM8983_BCP_SHIFT);
  614. return 0;
  615. }
  616. static int wm8983_hw_params(struct snd_pcm_substream *substream,
  617. struct snd_pcm_hw_params *params,
  618. struct snd_soc_dai *dai)
  619. {
  620. int i;
  621. struct snd_soc_codec *codec = dai->codec;
  622. struct wm8983_priv *wm8983 = snd_soc_codec_get_drvdata(codec);
  623. u16 blen, srate_idx;
  624. u32 tmp;
  625. int srate_best;
  626. int ret;
  627. ret = snd_soc_params_to_bclk(params);
  628. if (ret < 0) {
  629. dev_err(codec->dev, "Failed to convert params to bclk: %d\n", ret);
  630. return ret;
  631. }
  632. wm8983->bclk = ret;
  633. switch (params_format(params)) {
  634. case SNDRV_PCM_FORMAT_S16_LE:
  635. blen = 0x0;
  636. break;
  637. case SNDRV_PCM_FORMAT_S20_3LE:
  638. blen = 0x1;
  639. break;
  640. case SNDRV_PCM_FORMAT_S24_LE:
  641. blen = 0x2;
  642. break;
  643. case SNDRV_PCM_FORMAT_S32_LE:
  644. blen = 0x3;
  645. break;
  646. default:
  647. dev_err(dai->dev, "Unsupported word length %u\n",
  648. params_format(params));
  649. return -EINVAL;
  650. }
  651. snd_soc_update_bits(codec, WM8983_AUDIO_INTERFACE,
  652. WM8983_WL_MASK, blen << WM8983_WL_SHIFT);
  653. /*
  654. * match to the nearest possible sample rate and rely
  655. * on the array index to configure the SR register
  656. */
  657. srate_idx = 0;
  658. srate_best = abs(srates[0] - params_rate(params));
  659. for (i = 1; i < ARRAY_SIZE(srates); ++i) {
  660. if (abs(srates[i] - params_rate(params)) >= srate_best)
  661. continue;
  662. srate_idx = i;
  663. srate_best = abs(srates[i] - params_rate(params));
  664. }
  665. dev_dbg(dai->dev, "Selected SRATE = %d\n", srates[srate_idx]);
  666. snd_soc_update_bits(codec, WM8983_ADDITIONAL_CONTROL,
  667. WM8983_SR_MASK, srate_idx << WM8983_SR_SHIFT);
  668. dev_dbg(dai->dev, "Target BCLK = %uHz\n", wm8983->bclk);
  669. dev_dbg(dai->dev, "SYSCLK = %uHz\n", wm8983->sysclk);
  670. for (i = 0; i < ARRAY_SIZE(fs_ratios); ++i) {
  671. if (wm8983->sysclk / params_rate(params)
  672. == fs_ratios[i].ratio)
  673. break;
  674. }
  675. if (i == ARRAY_SIZE(fs_ratios)) {
  676. dev_err(dai->dev, "Unable to configure MCLK ratio %u/%u\n",
  677. wm8983->sysclk, params_rate(params));
  678. return -EINVAL;
  679. }
  680. dev_dbg(dai->dev, "MCLK ratio = %dfs\n", fs_ratios[i].ratio);
  681. snd_soc_update_bits(codec, WM8983_CLOCK_GEN_CONTROL,
  682. WM8983_MCLKDIV_MASK, i << WM8983_MCLKDIV_SHIFT);
  683. /* select the appropriate bclk divider */
  684. tmp = (wm8983->sysclk / fs_ratios[i].div) * 10;
  685. for (i = 0; i < ARRAY_SIZE(bclk_divs); ++i) {
  686. if (wm8983->bclk == tmp / bclk_divs[i])
  687. break;
  688. }
  689. if (i == ARRAY_SIZE(bclk_divs)) {
  690. dev_err(dai->dev, "No matching BCLK divider found\n");
  691. return -EINVAL;
  692. }
  693. dev_dbg(dai->dev, "BCLK div = %d\n", i);
  694. snd_soc_update_bits(codec, WM8983_CLOCK_GEN_CONTROL,
  695. WM8983_BCLKDIV_MASK, i << WM8983_BCLKDIV_SHIFT);
  696. return 0;
  697. }
  698. struct pll_div {
  699. u32 div2:1;
  700. u32 n:4;
  701. u32 k:24;
  702. };
  703. #define FIXED_PLL_SIZE ((1ULL << 24) * 10)
  704. static int pll_factors(struct pll_div *pll_div, unsigned int target,
  705. unsigned int source)
  706. {
  707. u64 Kpart;
  708. unsigned long int K, Ndiv, Nmod;
  709. pll_div->div2 = 0;
  710. Ndiv = target / source;
  711. if (Ndiv < 6) {
  712. source >>= 1;
  713. pll_div->div2 = 1;
  714. Ndiv = target / source;
  715. }
  716. if (Ndiv < 6 || Ndiv > 12) {
  717. printk(KERN_ERR "%s: WM8983 N value is not within"
  718. " the recommended range: %lu\n", __func__, Ndiv);
  719. return -EINVAL;
  720. }
  721. pll_div->n = Ndiv;
  722. Nmod = target % source;
  723. Kpart = FIXED_PLL_SIZE * (u64)Nmod;
  724. do_div(Kpart, source);
  725. K = Kpart & 0xffffffff;
  726. if ((K % 10) >= 5)
  727. K += 5;
  728. K /= 10;
  729. pll_div->k = K;
  730. return 0;
  731. }
  732. static int wm8983_set_pll(struct snd_soc_dai *dai, int pll_id,
  733. int source, unsigned int freq_in,
  734. unsigned int freq_out)
  735. {
  736. int ret;
  737. struct snd_soc_codec *codec;
  738. struct pll_div pll_div;
  739. codec = dai->codec;
  740. if (freq_in && freq_out) {
  741. ret = pll_factors(&pll_div, freq_out * 4 * 2, freq_in);
  742. if (ret)
  743. return ret;
  744. }
  745. /* disable the PLL before re-programming it */
  746. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  747. WM8983_PLLEN_MASK, 0);
  748. if (!freq_in || !freq_out)
  749. return 0;
  750. /* set PLLN and PRESCALE */
  751. snd_soc_write(codec, WM8983_PLL_N,
  752. (pll_div.div2 << WM8983_PLL_PRESCALE_SHIFT)
  753. | pll_div.n);
  754. /* set PLLK */
  755. snd_soc_write(codec, WM8983_PLL_K_3, pll_div.k & 0x1ff);
  756. snd_soc_write(codec, WM8983_PLL_K_2, (pll_div.k >> 9) & 0x1ff);
  757. snd_soc_write(codec, WM8983_PLL_K_1, (pll_div.k >> 18));
  758. /* enable the PLL */
  759. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  760. WM8983_PLLEN_MASK, WM8983_PLLEN);
  761. return 0;
  762. }
  763. static int wm8983_set_sysclk(struct snd_soc_dai *dai,
  764. int clk_id, unsigned int freq, int dir)
  765. {
  766. struct snd_soc_codec *codec = dai->codec;
  767. struct wm8983_priv *wm8983 = snd_soc_codec_get_drvdata(codec);
  768. switch (clk_id) {
  769. case WM8983_CLKSRC_MCLK:
  770. snd_soc_update_bits(codec, WM8983_CLOCK_GEN_CONTROL,
  771. WM8983_CLKSEL_MASK, 0);
  772. break;
  773. case WM8983_CLKSRC_PLL:
  774. snd_soc_update_bits(codec, WM8983_CLOCK_GEN_CONTROL,
  775. WM8983_CLKSEL_MASK, WM8983_CLKSEL);
  776. break;
  777. default:
  778. dev_err(dai->dev, "Unknown clock source: %d\n", clk_id);
  779. return -EINVAL;
  780. }
  781. wm8983->sysclk = freq;
  782. return 0;
  783. }
  784. static int wm8983_set_bias_level(struct snd_soc_codec *codec,
  785. enum snd_soc_bias_level level)
  786. {
  787. int ret;
  788. switch (level) {
  789. case SND_SOC_BIAS_ON:
  790. case SND_SOC_BIAS_PREPARE:
  791. /* VMID at 100k */
  792. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  793. WM8983_VMIDSEL_MASK,
  794. 1 << WM8983_VMIDSEL_SHIFT);
  795. break;
  796. case SND_SOC_BIAS_STANDBY:
  797. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  798. ret = snd_soc_cache_sync(codec);
  799. if (ret < 0) {
  800. dev_err(codec->dev, "Failed to sync cache: %d\n", ret);
  801. return ret;
  802. }
  803. /* enable anti-pop features */
  804. snd_soc_update_bits(codec, WM8983_OUT4_TO_ADC,
  805. WM8983_POBCTRL_MASK | WM8983_DELEN_MASK,
  806. WM8983_POBCTRL | WM8983_DELEN);
  807. /* enable thermal shutdown */
  808. snd_soc_update_bits(codec, WM8983_OUTPUT_CTRL,
  809. WM8983_TSDEN_MASK, WM8983_TSDEN);
  810. /* enable BIASEN */
  811. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  812. WM8983_BIASEN_MASK, WM8983_BIASEN);
  813. /* VMID at 100k */
  814. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  815. WM8983_VMIDSEL_MASK,
  816. 1 << WM8983_VMIDSEL_SHIFT);
  817. msleep(250);
  818. /* disable anti-pop features */
  819. snd_soc_update_bits(codec, WM8983_OUT4_TO_ADC,
  820. WM8983_POBCTRL_MASK |
  821. WM8983_DELEN_MASK, 0);
  822. }
  823. /* VMID at 500k */
  824. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  825. WM8983_VMIDSEL_MASK,
  826. 2 << WM8983_VMIDSEL_SHIFT);
  827. break;
  828. case SND_SOC_BIAS_OFF:
  829. /* disable thermal shutdown */
  830. snd_soc_update_bits(codec, WM8983_OUTPUT_CTRL,
  831. WM8983_TSDEN_MASK, 0);
  832. /* disable VMIDSEL and BIASEN */
  833. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  834. WM8983_VMIDSEL_MASK | WM8983_BIASEN_MASK,
  835. 0);
  836. /* wait for VMID to discharge */
  837. msleep(100);
  838. snd_soc_write(codec, WM8983_POWER_MANAGEMENT_1, 0);
  839. snd_soc_write(codec, WM8983_POWER_MANAGEMENT_2, 0);
  840. snd_soc_write(codec, WM8983_POWER_MANAGEMENT_3, 0);
  841. break;
  842. }
  843. codec->dapm.bias_level = level;
  844. return 0;
  845. }
  846. #ifdef CONFIG_PM
  847. static int wm8983_suspend(struct snd_soc_codec *codec)
  848. {
  849. wm8983_set_bias_level(codec, SND_SOC_BIAS_OFF);
  850. return 0;
  851. }
  852. static int wm8983_resume(struct snd_soc_codec *codec)
  853. {
  854. wm8983_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  855. return 0;
  856. }
  857. #else
  858. #define wm8983_suspend NULL
  859. #define wm8983_resume NULL
  860. #endif
  861. static int wm8983_remove(struct snd_soc_codec *codec)
  862. {
  863. wm8983_set_bias_level(codec, SND_SOC_BIAS_OFF);
  864. return 0;
  865. }
  866. static int wm8983_probe(struct snd_soc_codec *codec)
  867. {
  868. int ret;
  869. struct wm8983_priv *wm8983 = snd_soc_codec_get_drvdata(codec);
  870. int i;
  871. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8983->control_type);
  872. if (ret < 0) {
  873. dev_err(codec->dev, "Failed to set cache i/o: %d\n", ret);
  874. return ret;
  875. }
  876. ret = snd_soc_write(codec, WM8983_SOFTWARE_RESET, 0);
  877. if (ret < 0) {
  878. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  879. return ret;
  880. }
  881. /* set the vol/gain update bits */
  882. for (i = 0; i < ARRAY_SIZE(vol_update_regs); ++i)
  883. snd_soc_update_bits(codec, vol_update_regs[i],
  884. 0x100, 0x100);
  885. /* mute all outputs and set PGAs to minimum gain */
  886. for (i = WM8983_LOUT1_HP_VOLUME_CTRL;
  887. i <= WM8983_OUT4_MONO_MIX_CTRL; ++i)
  888. snd_soc_update_bits(codec, i, 0x40, 0x40);
  889. /* enable soft mute */
  890. snd_soc_update_bits(codec, WM8983_DAC_CONTROL,
  891. WM8983_SOFTMUTE_MASK,
  892. WM8983_SOFTMUTE);
  893. /* enable BIASCUT */
  894. snd_soc_update_bits(codec, WM8983_BIAS_CTRL,
  895. WM8983_BIASCUT, WM8983_BIASCUT);
  896. return 0;
  897. }
  898. static const struct snd_soc_dai_ops wm8983_dai_ops = {
  899. .digital_mute = wm8983_dac_mute,
  900. .hw_params = wm8983_hw_params,
  901. .set_fmt = wm8983_set_fmt,
  902. .set_sysclk = wm8983_set_sysclk,
  903. .set_pll = wm8983_set_pll
  904. };
  905. #define WM8983_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  906. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  907. static struct snd_soc_dai_driver wm8983_dai = {
  908. .name = "wm8983-hifi",
  909. .playback = {
  910. .stream_name = "Playback",
  911. .channels_min = 2,
  912. .channels_max = 2,
  913. .rates = SNDRV_PCM_RATE_8000_48000,
  914. .formats = WM8983_FORMATS,
  915. },
  916. .capture = {
  917. .stream_name = "Capture",
  918. .channels_min = 2,
  919. .channels_max = 2,
  920. .rates = SNDRV_PCM_RATE_8000_48000,
  921. .formats = WM8983_FORMATS,
  922. },
  923. .ops = &wm8983_dai_ops,
  924. .symmetric_rates = 1
  925. };
  926. static struct snd_soc_codec_driver soc_codec_dev_wm8983 = {
  927. .probe = wm8983_probe,
  928. .remove = wm8983_remove,
  929. .suspend = wm8983_suspend,
  930. .resume = wm8983_resume,
  931. .set_bias_level = wm8983_set_bias_level,
  932. .reg_cache_size = ARRAY_SIZE(wm8983_reg_defs),
  933. .reg_word_size = sizeof(u16),
  934. .reg_cache_default = wm8983_reg_defs,
  935. .controls = wm8983_snd_controls,
  936. .num_controls = ARRAY_SIZE(wm8983_snd_controls),
  937. .dapm_widgets = wm8983_dapm_widgets,
  938. .num_dapm_widgets = ARRAY_SIZE(wm8983_dapm_widgets),
  939. .dapm_routes = wm8983_audio_map,
  940. .num_dapm_routes = ARRAY_SIZE(wm8983_audio_map),
  941. .readable_register = wm8983_readable
  942. };
  943. #if defined(CONFIG_SPI_MASTER)
  944. static int __devinit wm8983_spi_probe(struct spi_device *spi)
  945. {
  946. struct wm8983_priv *wm8983;
  947. int ret;
  948. wm8983 = kzalloc(sizeof *wm8983, GFP_KERNEL);
  949. if (!wm8983)
  950. return -ENOMEM;
  951. wm8983->control_type = SND_SOC_SPI;
  952. spi_set_drvdata(spi, wm8983);
  953. ret = snd_soc_register_codec(&spi->dev,
  954. &soc_codec_dev_wm8983, &wm8983_dai, 1);
  955. if (ret < 0)
  956. kfree(wm8983);
  957. return ret;
  958. }
  959. static int __devexit wm8983_spi_remove(struct spi_device *spi)
  960. {
  961. snd_soc_unregister_codec(&spi->dev);
  962. kfree(spi_get_drvdata(spi));
  963. return 0;
  964. }
  965. static struct spi_driver wm8983_spi_driver = {
  966. .driver = {
  967. .name = "wm8983",
  968. .owner = THIS_MODULE,
  969. },
  970. .probe = wm8983_spi_probe,
  971. .remove = __devexit_p(wm8983_spi_remove)
  972. };
  973. #endif
  974. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  975. static __devinit int wm8983_i2c_probe(struct i2c_client *i2c,
  976. const struct i2c_device_id *id)
  977. {
  978. struct wm8983_priv *wm8983;
  979. int ret;
  980. wm8983 = kzalloc(sizeof *wm8983, GFP_KERNEL);
  981. if (!wm8983)
  982. return -ENOMEM;
  983. wm8983->control_type = SND_SOC_I2C;
  984. i2c_set_clientdata(i2c, wm8983);
  985. ret = snd_soc_register_codec(&i2c->dev,
  986. &soc_codec_dev_wm8983, &wm8983_dai, 1);
  987. if (ret < 0)
  988. kfree(wm8983);
  989. return ret;
  990. }
  991. static __devexit int wm8983_i2c_remove(struct i2c_client *client)
  992. {
  993. snd_soc_unregister_codec(&client->dev);
  994. kfree(i2c_get_clientdata(client));
  995. return 0;
  996. }
  997. static const struct i2c_device_id wm8983_i2c_id[] = {
  998. { "wm8983", 0 },
  999. { }
  1000. };
  1001. MODULE_DEVICE_TABLE(i2c, wm8983_i2c_id);
  1002. static struct i2c_driver wm8983_i2c_driver = {
  1003. .driver = {
  1004. .name = "wm8983",
  1005. .owner = THIS_MODULE,
  1006. },
  1007. .probe = wm8983_i2c_probe,
  1008. .remove = __devexit_p(wm8983_i2c_remove),
  1009. .id_table = wm8983_i2c_id
  1010. };
  1011. #endif
  1012. static int __init wm8983_modinit(void)
  1013. {
  1014. int ret = 0;
  1015. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1016. ret = i2c_add_driver(&wm8983_i2c_driver);
  1017. if (ret) {
  1018. printk(KERN_ERR "Failed to register wm8983 I2C driver: %d\n",
  1019. ret);
  1020. }
  1021. #endif
  1022. #if defined(CONFIG_SPI_MASTER)
  1023. ret = spi_register_driver(&wm8983_spi_driver);
  1024. if (ret != 0) {
  1025. printk(KERN_ERR "Failed to register wm8983 SPI driver: %d\n",
  1026. ret);
  1027. }
  1028. #endif
  1029. return ret;
  1030. }
  1031. module_init(wm8983_modinit);
  1032. static void __exit wm8983_exit(void)
  1033. {
  1034. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1035. i2c_del_driver(&wm8983_i2c_driver);
  1036. #endif
  1037. #if defined(CONFIG_SPI_MASTER)
  1038. spi_unregister_driver(&wm8983_spi_driver);
  1039. #endif
  1040. }
  1041. module_exit(wm8983_exit);
  1042. MODULE_DESCRIPTION("ASoC WM8983 driver");
  1043. MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
  1044. MODULE_LICENSE("GPL");