wm8983.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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/regmap.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/slab.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/initval.h>
  26. #include <sound/tlv.h>
  27. #include "wm8983.h"
  28. static const struct reg_default wm8983_defaults[] = {
  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. /* vol/gain update regs */
  84. static const int vol_update_regs[] = {
  85. WM8983_LEFT_DAC_DIGITAL_VOL,
  86. WM8983_RIGHT_DAC_DIGITAL_VOL,
  87. WM8983_LEFT_ADC_DIGITAL_VOL,
  88. WM8983_RIGHT_ADC_DIGITAL_VOL,
  89. WM8983_LOUT1_HP_VOLUME_CTRL,
  90. WM8983_ROUT1_HP_VOLUME_CTRL,
  91. WM8983_LOUT2_SPK_VOLUME_CTRL,
  92. WM8983_ROUT2_SPK_VOLUME_CTRL,
  93. WM8983_LEFT_INP_PGA_GAIN_CTRL,
  94. WM8983_RIGHT_INP_PGA_GAIN_CTRL
  95. };
  96. struct wm8983_priv {
  97. struct regmap *regmap;
  98. u32 sysclk;
  99. u32 bclk;
  100. };
  101. static const struct {
  102. int div;
  103. int ratio;
  104. } fs_ratios[] = {
  105. { 10, 128 },
  106. { 15, 192 },
  107. { 20, 256 },
  108. { 30, 384 },
  109. { 40, 512 },
  110. { 60, 768 },
  111. { 80, 1024 },
  112. { 120, 1536 }
  113. };
  114. static const int srates[] = { 48000, 32000, 24000, 16000, 12000, 8000 };
  115. static const int bclk_divs[] = {
  116. 1, 2, 4, 8, 16, 32
  117. };
  118. static int eqmode_get(struct snd_kcontrol *kcontrol,
  119. struct snd_ctl_elem_value *ucontrol);
  120. static int eqmode_put(struct snd_kcontrol *kcontrol,
  121. struct snd_ctl_elem_value *ucontrol);
  122. static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1);
  123. static const DECLARE_TLV_DB_SCALE(adc_tlv, -12700, 50, 1);
  124. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  125. static const DECLARE_TLV_DB_SCALE(lim_thresh_tlv, -600, 100, 0);
  126. static const DECLARE_TLV_DB_SCALE(lim_boost_tlv, 0, 100, 0);
  127. static const DECLARE_TLV_DB_SCALE(alc_min_tlv, -1200, 600, 0);
  128. static const DECLARE_TLV_DB_SCALE(alc_max_tlv, -675, 600, 0);
  129. static const DECLARE_TLV_DB_SCALE(alc_tar_tlv, -2250, 150, 0);
  130. static const DECLARE_TLV_DB_SCALE(pga_vol_tlv, -1200, 75, 0);
  131. static const DECLARE_TLV_DB_SCALE(boost_tlv, -1200, 300, 1);
  132. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  133. static const DECLARE_TLV_DB_SCALE(aux_tlv, -1500, 300, 0);
  134. static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
  135. static const DECLARE_TLV_DB_SCALE(pga_boost_tlv, 0, 2000, 0);
  136. static const char *alc_sel_text[] = { "Off", "Right", "Left", "Stereo" };
  137. static SOC_ENUM_SINGLE_DECL(alc_sel, WM8983_ALC_CONTROL_1, 7, alc_sel_text);
  138. static const char *alc_mode_text[] = { "ALC", "Limiter" };
  139. static SOC_ENUM_SINGLE_DECL(alc_mode, WM8983_ALC_CONTROL_3, 8, alc_mode_text);
  140. static const char *filter_mode_text[] = { "Audio", "Application" };
  141. static SOC_ENUM_SINGLE_DECL(filter_mode, WM8983_ADC_CONTROL, 7,
  142. filter_mode_text);
  143. static const char *eq_bw_text[] = { "Narrow", "Wide" };
  144. static const char *eqmode_text[] = { "Capture", "Playback" };
  145. static SOC_ENUM_SINGLE_EXT_DECL(eqmode, eqmode_text);
  146. static const char *eq1_cutoff_text[] = {
  147. "80Hz", "105Hz", "135Hz", "175Hz"
  148. };
  149. static SOC_ENUM_SINGLE_DECL(eq1_cutoff, WM8983_EQ1_LOW_SHELF, 5,
  150. eq1_cutoff_text);
  151. static const char *eq2_cutoff_text[] = {
  152. "230Hz", "300Hz", "385Hz", "500Hz"
  153. };
  154. static SOC_ENUM_SINGLE_DECL(eq2_bw, WM8983_EQ2_PEAK_1, 8, eq_bw_text);
  155. static SOC_ENUM_SINGLE_DECL(eq2_cutoff, WM8983_EQ2_PEAK_1, 5, eq2_cutoff_text);
  156. static const char *eq3_cutoff_text[] = {
  157. "650Hz", "850Hz", "1.1kHz", "1.4kHz"
  158. };
  159. static SOC_ENUM_SINGLE_DECL(eq3_bw, WM8983_EQ3_PEAK_2, 8, eq_bw_text);
  160. static SOC_ENUM_SINGLE_DECL(eq3_cutoff, WM8983_EQ3_PEAK_2, 5, eq3_cutoff_text);
  161. static const char *eq4_cutoff_text[] = {
  162. "1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz"
  163. };
  164. static SOC_ENUM_SINGLE_DECL(eq4_bw, WM8983_EQ4_PEAK_3, 8, eq_bw_text);
  165. static SOC_ENUM_SINGLE_DECL(eq4_cutoff, WM8983_EQ4_PEAK_3, 5, eq4_cutoff_text);
  166. static const char *eq5_cutoff_text[] = {
  167. "5.3kHz", "6.9kHz", "9kHz", "11.7kHz"
  168. };
  169. static SOC_ENUM_SINGLE_DECL(eq5_cutoff, WM8983_EQ5_HIGH_SHELF, 5,
  170. eq5_cutoff_text);
  171. static const char *depth_3d_text[] = {
  172. "Off",
  173. "6.67%",
  174. "13.3%",
  175. "20%",
  176. "26.7%",
  177. "33.3%",
  178. "40%",
  179. "46.6%",
  180. "53.3%",
  181. "60%",
  182. "66.7%",
  183. "73.3%",
  184. "80%",
  185. "86.7%",
  186. "93.3%",
  187. "100%"
  188. };
  189. static SOC_ENUM_SINGLE_DECL(depth_3d, WM8983_3D_CONTROL, 0,
  190. depth_3d_text);
  191. static const struct snd_kcontrol_new wm8983_snd_controls[] = {
  192. SOC_SINGLE("Digital Loopback Switch", WM8983_COMPANDING_CONTROL,
  193. 0, 1, 0),
  194. SOC_ENUM("ALC Capture Function", alc_sel),
  195. SOC_SINGLE_TLV("ALC Capture Max Volume", WM8983_ALC_CONTROL_1,
  196. 3, 7, 0, alc_max_tlv),
  197. SOC_SINGLE_TLV("ALC Capture Min Volume", WM8983_ALC_CONTROL_1,
  198. 0, 7, 0, alc_min_tlv),
  199. SOC_SINGLE_TLV("ALC Capture Target Volume", WM8983_ALC_CONTROL_2,
  200. 0, 15, 0, alc_tar_tlv),
  201. SOC_SINGLE("ALC Capture Attack", WM8983_ALC_CONTROL_3, 0, 10, 0),
  202. SOC_SINGLE("ALC Capture Hold", WM8983_ALC_CONTROL_2, 4, 10, 0),
  203. SOC_SINGLE("ALC Capture Decay", WM8983_ALC_CONTROL_3, 4, 10, 0),
  204. SOC_ENUM("ALC Mode", alc_mode),
  205. SOC_SINGLE("ALC Capture NG Switch", WM8983_NOISE_GATE,
  206. 3, 1, 0),
  207. SOC_SINGLE("ALC Capture NG Threshold", WM8983_NOISE_GATE,
  208. 0, 7, 1),
  209. SOC_DOUBLE_R_TLV("Capture Volume", WM8983_LEFT_ADC_DIGITAL_VOL,
  210. WM8983_RIGHT_ADC_DIGITAL_VOL, 0, 255, 0, adc_tlv),
  211. SOC_DOUBLE_R("Capture PGA ZC Switch", WM8983_LEFT_INP_PGA_GAIN_CTRL,
  212. WM8983_RIGHT_INP_PGA_GAIN_CTRL, 7, 1, 0),
  213. SOC_DOUBLE_R_TLV("Capture PGA Volume", WM8983_LEFT_INP_PGA_GAIN_CTRL,
  214. WM8983_RIGHT_INP_PGA_GAIN_CTRL, 0, 63, 0, pga_vol_tlv),
  215. SOC_DOUBLE_R_TLV("Capture PGA Boost Volume",
  216. WM8983_LEFT_ADC_BOOST_CTRL, WM8983_RIGHT_ADC_BOOST_CTRL,
  217. 8, 1, 0, pga_boost_tlv),
  218. SOC_DOUBLE("ADC Inversion Switch", WM8983_ADC_CONTROL, 0, 1, 1, 0),
  219. SOC_SINGLE("ADC 128x Oversampling Switch", WM8983_ADC_CONTROL, 8, 1, 0),
  220. SOC_DOUBLE_R_TLV("Playback Volume", WM8983_LEFT_DAC_DIGITAL_VOL,
  221. WM8983_RIGHT_DAC_DIGITAL_VOL, 0, 255, 0, dac_tlv),
  222. SOC_SINGLE("DAC Playback Limiter Switch", WM8983_DAC_LIMITER_1, 8, 1, 0),
  223. SOC_SINGLE("DAC Playback Limiter Decay", WM8983_DAC_LIMITER_1, 4, 10, 0),
  224. SOC_SINGLE("DAC Playback Limiter Attack", WM8983_DAC_LIMITER_1, 0, 11, 0),
  225. SOC_SINGLE_TLV("DAC Playback Limiter Threshold", WM8983_DAC_LIMITER_2,
  226. 4, 7, 1, lim_thresh_tlv),
  227. SOC_SINGLE_TLV("DAC Playback Limiter Boost Volume", WM8983_DAC_LIMITER_2,
  228. 0, 12, 0, lim_boost_tlv),
  229. SOC_DOUBLE("DAC Inversion Switch", WM8983_DAC_CONTROL, 0, 1, 1, 0),
  230. SOC_SINGLE("DAC Auto Mute Switch", WM8983_DAC_CONTROL, 2, 1, 0),
  231. SOC_SINGLE("DAC 128x Oversampling Switch", WM8983_DAC_CONTROL, 3, 1, 0),
  232. SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8983_LOUT1_HP_VOLUME_CTRL,
  233. WM8983_ROUT1_HP_VOLUME_CTRL, 0, 63, 0, out_tlv),
  234. SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8983_LOUT1_HP_VOLUME_CTRL,
  235. WM8983_ROUT1_HP_VOLUME_CTRL, 7, 1, 0),
  236. SOC_DOUBLE_R("Headphone Switch", WM8983_LOUT1_HP_VOLUME_CTRL,
  237. WM8983_ROUT1_HP_VOLUME_CTRL, 6, 1, 1),
  238. SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8983_LOUT2_SPK_VOLUME_CTRL,
  239. WM8983_ROUT2_SPK_VOLUME_CTRL, 0, 63, 0, out_tlv),
  240. SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8983_LOUT2_SPK_VOLUME_CTRL,
  241. WM8983_ROUT2_SPK_VOLUME_CTRL, 7, 1, 0),
  242. SOC_DOUBLE_R("Speaker Switch", WM8983_LOUT2_SPK_VOLUME_CTRL,
  243. WM8983_ROUT2_SPK_VOLUME_CTRL, 6, 1, 1),
  244. SOC_SINGLE("OUT3 Switch", WM8983_OUT3_MIXER_CTRL,
  245. 6, 1, 1),
  246. SOC_SINGLE("OUT4 Switch", WM8983_OUT4_MONO_MIX_CTRL,
  247. 6, 1, 1),
  248. SOC_SINGLE("High Pass Filter Switch", WM8983_ADC_CONTROL, 8, 1, 0),
  249. SOC_ENUM("High Pass Filter Mode", filter_mode),
  250. SOC_SINGLE("High Pass Filter Cutoff", WM8983_ADC_CONTROL, 4, 7, 0),
  251. SOC_DOUBLE_R_TLV("Aux Bypass Volume",
  252. WM8983_LEFT_MIXER_CTRL, WM8983_RIGHT_MIXER_CTRL, 6, 7, 0,
  253. aux_tlv),
  254. SOC_DOUBLE_R_TLV("Input PGA Bypass Volume",
  255. WM8983_LEFT_MIXER_CTRL, WM8983_RIGHT_MIXER_CTRL, 2, 7, 0,
  256. bypass_tlv),
  257. SOC_ENUM_EXT("Equalizer Function", eqmode, eqmode_get, eqmode_put),
  258. SOC_ENUM("EQ1 Cutoff", eq1_cutoff),
  259. SOC_SINGLE_TLV("EQ1 Volume", WM8983_EQ1_LOW_SHELF, 0, 24, 1, eq_tlv),
  260. SOC_ENUM("EQ2 Bandwidth", eq2_bw),
  261. SOC_ENUM("EQ2 Cutoff", eq2_cutoff),
  262. SOC_SINGLE_TLV("EQ2 Volume", WM8983_EQ2_PEAK_1, 0, 24, 1, eq_tlv),
  263. SOC_ENUM("EQ3 Bandwidth", eq3_bw),
  264. SOC_ENUM("EQ3 Cutoff", eq3_cutoff),
  265. SOC_SINGLE_TLV("EQ3 Volume", WM8983_EQ3_PEAK_2, 0, 24, 1, eq_tlv),
  266. SOC_ENUM("EQ4 Bandwidth", eq4_bw),
  267. SOC_ENUM("EQ4 Cutoff", eq4_cutoff),
  268. SOC_SINGLE_TLV("EQ4 Volume", WM8983_EQ4_PEAK_3, 0, 24, 1, eq_tlv),
  269. SOC_ENUM("EQ5 Cutoff", eq5_cutoff),
  270. SOC_SINGLE_TLV("EQ5 Volume", WM8983_EQ5_HIGH_SHELF, 0, 24, 1, eq_tlv),
  271. SOC_ENUM("3D Depth", depth_3d),
  272. };
  273. static const struct snd_kcontrol_new left_out_mixer[] = {
  274. SOC_DAPM_SINGLE("Line Switch", WM8983_LEFT_MIXER_CTRL, 1, 1, 0),
  275. SOC_DAPM_SINGLE("Aux Switch", WM8983_LEFT_MIXER_CTRL, 5, 1, 0),
  276. SOC_DAPM_SINGLE("PCM Switch", WM8983_LEFT_MIXER_CTRL, 0, 1, 0),
  277. };
  278. static const struct snd_kcontrol_new right_out_mixer[] = {
  279. SOC_DAPM_SINGLE("Line Switch", WM8983_RIGHT_MIXER_CTRL, 1, 1, 0),
  280. SOC_DAPM_SINGLE("Aux Switch", WM8983_RIGHT_MIXER_CTRL, 5, 1, 0),
  281. SOC_DAPM_SINGLE("PCM Switch", WM8983_RIGHT_MIXER_CTRL, 0, 1, 0),
  282. };
  283. static const struct snd_kcontrol_new left_input_mixer[] = {
  284. SOC_DAPM_SINGLE("L2 Switch", WM8983_INPUT_CTRL, 2, 1, 0),
  285. SOC_DAPM_SINGLE("MicN Switch", WM8983_INPUT_CTRL, 1, 1, 0),
  286. SOC_DAPM_SINGLE("MicP Switch", WM8983_INPUT_CTRL, 0, 1, 0),
  287. };
  288. static const struct snd_kcontrol_new right_input_mixer[] = {
  289. SOC_DAPM_SINGLE("R2 Switch", WM8983_INPUT_CTRL, 6, 1, 0),
  290. SOC_DAPM_SINGLE("MicN Switch", WM8983_INPUT_CTRL, 5, 1, 0),
  291. SOC_DAPM_SINGLE("MicP Switch", WM8983_INPUT_CTRL, 4, 1, 0),
  292. };
  293. static const struct snd_kcontrol_new left_boost_mixer[] = {
  294. SOC_DAPM_SINGLE_TLV("L2 Volume", WM8983_LEFT_ADC_BOOST_CTRL,
  295. 4, 7, 0, boost_tlv),
  296. SOC_DAPM_SINGLE_TLV("AUXL Volume", WM8983_LEFT_ADC_BOOST_CTRL,
  297. 0, 7, 0, boost_tlv)
  298. };
  299. static const struct snd_kcontrol_new out3_mixer[] = {
  300. SOC_DAPM_SINGLE("LMIX2OUT3 Switch", WM8983_OUT3_MIXER_CTRL,
  301. 1, 1, 0),
  302. SOC_DAPM_SINGLE("LDAC2OUT3 Switch", WM8983_OUT3_MIXER_CTRL,
  303. 0, 1, 0),
  304. };
  305. static const struct snd_kcontrol_new out4_mixer[] = {
  306. SOC_DAPM_SINGLE("LMIX2OUT4 Switch", WM8983_OUT4_MONO_MIX_CTRL,
  307. 4, 1, 0),
  308. SOC_DAPM_SINGLE("RMIX2OUT4 Switch", WM8983_OUT4_MONO_MIX_CTRL,
  309. 1, 1, 0),
  310. SOC_DAPM_SINGLE("LDAC2OUT4 Switch", WM8983_OUT4_MONO_MIX_CTRL,
  311. 3, 1, 0),
  312. SOC_DAPM_SINGLE("RDAC2OUT4 Switch", WM8983_OUT4_MONO_MIX_CTRL,
  313. 0, 1, 0),
  314. };
  315. static const struct snd_kcontrol_new right_boost_mixer[] = {
  316. SOC_DAPM_SINGLE_TLV("R2 Volume", WM8983_RIGHT_ADC_BOOST_CTRL,
  317. 4, 7, 0, boost_tlv),
  318. SOC_DAPM_SINGLE_TLV("AUXR Volume", WM8983_RIGHT_ADC_BOOST_CTRL,
  319. 0, 7, 0, boost_tlv)
  320. };
  321. static const struct snd_soc_dapm_widget wm8983_dapm_widgets[] = {
  322. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8983_POWER_MANAGEMENT_3,
  323. 0, 0),
  324. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8983_POWER_MANAGEMENT_3,
  325. 1, 0),
  326. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8983_POWER_MANAGEMENT_2,
  327. 0, 0),
  328. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8983_POWER_MANAGEMENT_2,
  329. 1, 0),
  330. SND_SOC_DAPM_MIXER("Left Output Mixer", WM8983_POWER_MANAGEMENT_3,
  331. 2, 0, left_out_mixer, ARRAY_SIZE(left_out_mixer)),
  332. SND_SOC_DAPM_MIXER("Right Output Mixer", WM8983_POWER_MANAGEMENT_3,
  333. 3, 0, right_out_mixer, ARRAY_SIZE(right_out_mixer)),
  334. SND_SOC_DAPM_MIXER("Left Input Mixer", WM8983_POWER_MANAGEMENT_2,
  335. 2, 0, left_input_mixer, ARRAY_SIZE(left_input_mixer)),
  336. SND_SOC_DAPM_MIXER("Right Input Mixer", WM8983_POWER_MANAGEMENT_2,
  337. 3, 0, right_input_mixer, ARRAY_SIZE(right_input_mixer)),
  338. SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8983_POWER_MANAGEMENT_2,
  339. 4, 0, left_boost_mixer, ARRAY_SIZE(left_boost_mixer)),
  340. SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8983_POWER_MANAGEMENT_2,
  341. 5, 0, right_boost_mixer, ARRAY_SIZE(right_boost_mixer)),
  342. SND_SOC_DAPM_MIXER("OUT3 Mixer", WM8983_POWER_MANAGEMENT_1,
  343. 6, 0, out3_mixer, ARRAY_SIZE(out3_mixer)),
  344. SND_SOC_DAPM_MIXER("OUT4 Mixer", WM8983_POWER_MANAGEMENT_1,
  345. 7, 0, out4_mixer, ARRAY_SIZE(out4_mixer)),
  346. SND_SOC_DAPM_PGA("Left Capture PGA", WM8983_LEFT_INP_PGA_GAIN_CTRL,
  347. 6, 1, NULL, 0),
  348. SND_SOC_DAPM_PGA("Right Capture PGA", WM8983_RIGHT_INP_PGA_GAIN_CTRL,
  349. 6, 1, NULL, 0),
  350. SND_SOC_DAPM_PGA("Left Headphone Out", WM8983_POWER_MANAGEMENT_2,
  351. 7, 0, NULL, 0),
  352. SND_SOC_DAPM_PGA("Right Headphone Out", WM8983_POWER_MANAGEMENT_2,
  353. 8, 0, NULL, 0),
  354. SND_SOC_DAPM_PGA("Left Speaker Out", WM8983_POWER_MANAGEMENT_3,
  355. 5, 0, NULL, 0),
  356. SND_SOC_DAPM_PGA("Right Speaker Out", WM8983_POWER_MANAGEMENT_3,
  357. 6, 0, NULL, 0),
  358. SND_SOC_DAPM_PGA("OUT3 Out", WM8983_POWER_MANAGEMENT_3,
  359. 7, 0, NULL, 0),
  360. SND_SOC_DAPM_PGA("OUT4 Out", WM8983_POWER_MANAGEMENT_3,
  361. 8, 0, NULL, 0),
  362. SND_SOC_DAPM_SUPPLY("Mic Bias", WM8983_POWER_MANAGEMENT_1, 4, 0,
  363. NULL, 0),
  364. SND_SOC_DAPM_INPUT("LIN"),
  365. SND_SOC_DAPM_INPUT("LIP"),
  366. SND_SOC_DAPM_INPUT("RIN"),
  367. SND_SOC_DAPM_INPUT("RIP"),
  368. SND_SOC_DAPM_INPUT("AUXL"),
  369. SND_SOC_DAPM_INPUT("AUXR"),
  370. SND_SOC_DAPM_INPUT("L2"),
  371. SND_SOC_DAPM_INPUT("R2"),
  372. SND_SOC_DAPM_OUTPUT("HPL"),
  373. SND_SOC_DAPM_OUTPUT("HPR"),
  374. SND_SOC_DAPM_OUTPUT("SPKL"),
  375. SND_SOC_DAPM_OUTPUT("SPKR"),
  376. SND_SOC_DAPM_OUTPUT("OUT3"),
  377. SND_SOC_DAPM_OUTPUT("OUT4")
  378. };
  379. static const struct snd_soc_dapm_route wm8983_audio_map[] = {
  380. { "OUT3 Mixer", "LMIX2OUT3 Switch", "Left Output Mixer" },
  381. { "OUT3 Mixer", "LDAC2OUT3 Switch", "Left DAC" },
  382. { "OUT3 Out", NULL, "OUT3 Mixer" },
  383. { "OUT3", NULL, "OUT3 Out" },
  384. { "OUT4 Mixer", "LMIX2OUT4 Switch", "Left Output Mixer" },
  385. { "OUT4 Mixer", "RMIX2OUT4 Switch", "Right Output Mixer" },
  386. { "OUT4 Mixer", "LDAC2OUT4 Switch", "Left DAC" },
  387. { "OUT4 Mixer", "RDAC2OUT4 Switch", "Right DAC" },
  388. { "OUT4 Out", NULL, "OUT4 Mixer" },
  389. { "OUT4", NULL, "OUT4 Out" },
  390. { "Right Output Mixer", "PCM Switch", "Right DAC" },
  391. { "Right Output Mixer", "Aux Switch", "AUXR" },
  392. { "Right Output Mixer", "Line Switch", "Right Boost Mixer" },
  393. { "Left Output Mixer", "PCM Switch", "Left DAC" },
  394. { "Left Output Mixer", "Aux Switch", "AUXL" },
  395. { "Left Output Mixer", "Line Switch", "Left Boost Mixer" },
  396. { "Right Headphone Out", NULL, "Right Output Mixer" },
  397. { "HPR", NULL, "Right Headphone Out" },
  398. { "Left Headphone Out", NULL, "Left Output Mixer" },
  399. { "HPL", NULL, "Left Headphone Out" },
  400. { "Right Speaker Out", NULL, "Right Output Mixer" },
  401. { "SPKR", NULL, "Right Speaker Out" },
  402. { "Left Speaker Out", NULL, "Left Output Mixer" },
  403. { "SPKL", NULL, "Left Speaker Out" },
  404. { "Right ADC", NULL, "Right Boost Mixer" },
  405. { "Right Boost Mixer", "AUXR Volume", "AUXR" },
  406. { "Right Boost Mixer", NULL, "Right Capture PGA" },
  407. { "Right Boost Mixer", "R2 Volume", "R2" },
  408. { "Left ADC", NULL, "Left Boost Mixer" },
  409. { "Left Boost Mixer", "AUXL Volume", "AUXL" },
  410. { "Left Boost Mixer", NULL, "Left Capture PGA" },
  411. { "Left Boost Mixer", "L2 Volume", "L2" },
  412. { "Right Capture PGA", NULL, "Right Input Mixer" },
  413. { "Left Capture PGA", NULL, "Left Input Mixer" },
  414. { "Right Input Mixer", "R2 Switch", "R2" },
  415. { "Right Input Mixer", "MicN Switch", "RIN" },
  416. { "Right Input Mixer", "MicP Switch", "RIP" },
  417. { "Left Input Mixer", "L2 Switch", "L2" },
  418. { "Left Input Mixer", "MicN Switch", "LIN" },
  419. { "Left Input Mixer", "MicP Switch", "LIP" },
  420. };
  421. static int eqmode_get(struct snd_kcontrol *kcontrol,
  422. struct snd_ctl_elem_value *ucontrol)
  423. {
  424. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  425. unsigned int reg;
  426. reg = snd_soc_read(codec, WM8983_EQ1_LOW_SHELF);
  427. if (reg & WM8983_EQ3DMODE)
  428. ucontrol->value.enumerated.item[0] = 1;
  429. else
  430. ucontrol->value.enumerated.item[0] = 0;
  431. return 0;
  432. }
  433. static int eqmode_put(struct snd_kcontrol *kcontrol,
  434. struct snd_ctl_elem_value *ucontrol)
  435. {
  436. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  437. unsigned int regpwr2, regpwr3;
  438. unsigned int reg_eq;
  439. if (ucontrol->value.enumerated.item[0] != 0
  440. && ucontrol->value.enumerated.item[0] != 1)
  441. return -EINVAL;
  442. reg_eq = snd_soc_read(codec, WM8983_EQ1_LOW_SHELF);
  443. switch ((reg_eq & WM8983_EQ3DMODE) >> WM8983_EQ3DMODE_SHIFT) {
  444. case 0:
  445. if (!ucontrol->value.enumerated.item[0])
  446. return 0;
  447. break;
  448. case 1:
  449. if (ucontrol->value.enumerated.item[0])
  450. return 0;
  451. break;
  452. }
  453. regpwr2 = snd_soc_read(codec, WM8983_POWER_MANAGEMENT_2);
  454. regpwr3 = snd_soc_read(codec, WM8983_POWER_MANAGEMENT_3);
  455. /* disable the DACs and ADCs */
  456. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_2,
  457. WM8983_ADCENR_MASK | WM8983_ADCENL_MASK, 0);
  458. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_3,
  459. WM8983_DACENR_MASK | WM8983_DACENL_MASK, 0);
  460. /* set the desired eqmode */
  461. snd_soc_update_bits(codec, WM8983_EQ1_LOW_SHELF,
  462. WM8983_EQ3DMODE_MASK,
  463. ucontrol->value.enumerated.item[0]
  464. << WM8983_EQ3DMODE_SHIFT);
  465. /* restore DAC/ADC configuration */
  466. snd_soc_write(codec, WM8983_POWER_MANAGEMENT_2, regpwr2);
  467. snd_soc_write(codec, WM8983_POWER_MANAGEMENT_3, regpwr3);
  468. return 0;
  469. }
  470. static bool wm8983_writeable(struct device *dev, unsigned int reg)
  471. {
  472. switch (reg) {
  473. case WM8983_SOFTWARE_RESET ... WM8983_RIGHT_ADC_DIGITAL_VOL:
  474. case WM8983_EQ1_LOW_SHELF ... WM8983_DAC_LIMITER_2:
  475. case WM8983_NOTCH_FILTER_1 ... WM8983_NOTCH_FILTER_4:
  476. case WM8983_ALC_CONTROL_1 ... WM8983_PLL_K_3:
  477. case WM8983_3D_CONTROL ... WM8983_OUT4_MONO_MIX_CTRL:
  478. case WM8983_BIAS_CTRL:
  479. return true;
  480. default:
  481. return false;
  482. }
  483. }
  484. static int wm8983_dac_mute(struct snd_soc_dai *dai, int mute)
  485. {
  486. struct snd_soc_codec *codec = dai->codec;
  487. return snd_soc_update_bits(codec, WM8983_DAC_CONTROL,
  488. WM8983_SOFTMUTE_MASK,
  489. !!mute << WM8983_SOFTMUTE_SHIFT);
  490. }
  491. static int wm8983_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  492. {
  493. struct snd_soc_codec *codec = dai->codec;
  494. u16 format, master, bcp, lrp;
  495. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  496. case SND_SOC_DAIFMT_I2S:
  497. format = 0x2;
  498. break;
  499. case SND_SOC_DAIFMT_RIGHT_J:
  500. format = 0x0;
  501. break;
  502. case SND_SOC_DAIFMT_LEFT_J:
  503. format = 0x1;
  504. break;
  505. case SND_SOC_DAIFMT_DSP_A:
  506. case SND_SOC_DAIFMT_DSP_B:
  507. format = 0x3;
  508. break;
  509. default:
  510. dev_err(dai->dev, "Unknown dai format\n");
  511. return -EINVAL;
  512. }
  513. snd_soc_update_bits(codec, WM8983_AUDIO_INTERFACE,
  514. WM8983_FMT_MASK, format << WM8983_FMT_SHIFT);
  515. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  516. case SND_SOC_DAIFMT_CBM_CFM:
  517. master = 1;
  518. break;
  519. case SND_SOC_DAIFMT_CBS_CFS:
  520. master = 0;
  521. break;
  522. default:
  523. dev_err(dai->dev, "Unknown master/slave configuration\n");
  524. return -EINVAL;
  525. }
  526. snd_soc_update_bits(codec, WM8983_CLOCK_GEN_CONTROL,
  527. WM8983_MS_MASK, master << WM8983_MS_SHIFT);
  528. /* FIXME: We don't currently support DSP A/B modes */
  529. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  530. case SND_SOC_DAIFMT_DSP_A:
  531. case SND_SOC_DAIFMT_DSP_B:
  532. dev_err(dai->dev, "DSP A/B modes are not supported\n");
  533. return -EINVAL;
  534. default:
  535. break;
  536. }
  537. bcp = lrp = 0;
  538. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  539. case SND_SOC_DAIFMT_NB_NF:
  540. break;
  541. case SND_SOC_DAIFMT_IB_IF:
  542. bcp = lrp = 1;
  543. break;
  544. case SND_SOC_DAIFMT_IB_NF:
  545. bcp = 1;
  546. break;
  547. case SND_SOC_DAIFMT_NB_IF:
  548. lrp = 1;
  549. break;
  550. default:
  551. dev_err(dai->dev, "Unknown polarity configuration\n");
  552. return -EINVAL;
  553. }
  554. snd_soc_update_bits(codec, WM8983_AUDIO_INTERFACE,
  555. WM8983_LRCP_MASK, lrp << WM8983_LRCP_SHIFT);
  556. snd_soc_update_bits(codec, WM8983_AUDIO_INTERFACE,
  557. WM8983_BCP_MASK, bcp << WM8983_BCP_SHIFT);
  558. return 0;
  559. }
  560. static int wm8983_hw_params(struct snd_pcm_substream *substream,
  561. struct snd_pcm_hw_params *params,
  562. struct snd_soc_dai *dai)
  563. {
  564. int i;
  565. struct snd_soc_codec *codec = dai->codec;
  566. struct wm8983_priv *wm8983 = snd_soc_codec_get_drvdata(codec);
  567. u16 blen, srate_idx;
  568. u32 tmp;
  569. int srate_best;
  570. int ret;
  571. ret = snd_soc_params_to_bclk(params);
  572. if (ret < 0) {
  573. dev_err(codec->dev, "Failed to convert params to bclk: %d\n", ret);
  574. return ret;
  575. }
  576. wm8983->bclk = ret;
  577. switch (params_width(params)) {
  578. case 16:
  579. blen = 0x0;
  580. break;
  581. case 20:
  582. blen = 0x1;
  583. break;
  584. case 24:
  585. blen = 0x2;
  586. break;
  587. case 32:
  588. blen = 0x3;
  589. break;
  590. default:
  591. dev_err(dai->dev, "Unsupported word length %u\n",
  592. params_width(params));
  593. return -EINVAL;
  594. }
  595. snd_soc_update_bits(codec, WM8983_AUDIO_INTERFACE,
  596. WM8983_WL_MASK, blen << WM8983_WL_SHIFT);
  597. /*
  598. * match to the nearest possible sample rate and rely
  599. * on the array index to configure the SR register
  600. */
  601. srate_idx = 0;
  602. srate_best = abs(srates[0] - params_rate(params));
  603. for (i = 1; i < ARRAY_SIZE(srates); ++i) {
  604. if (abs(srates[i] - params_rate(params)) >= srate_best)
  605. continue;
  606. srate_idx = i;
  607. srate_best = abs(srates[i] - params_rate(params));
  608. }
  609. dev_dbg(dai->dev, "Selected SRATE = %d\n", srates[srate_idx]);
  610. snd_soc_update_bits(codec, WM8983_ADDITIONAL_CONTROL,
  611. WM8983_SR_MASK, srate_idx << WM8983_SR_SHIFT);
  612. dev_dbg(dai->dev, "Target BCLK = %uHz\n", wm8983->bclk);
  613. dev_dbg(dai->dev, "SYSCLK = %uHz\n", wm8983->sysclk);
  614. for (i = 0; i < ARRAY_SIZE(fs_ratios); ++i) {
  615. if (wm8983->sysclk / params_rate(params)
  616. == fs_ratios[i].ratio)
  617. break;
  618. }
  619. if (i == ARRAY_SIZE(fs_ratios)) {
  620. dev_err(dai->dev, "Unable to configure MCLK ratio %u/%u\n",
  621. wm8983->sysclk, params_rate(params));
  622. return -EINVAL;
  623. }
  624. dev_dbg(dai->dev, "MCLK ratio = %dfs\n", fs_ratios[i].ratio);
  625. snd_soc_update_bits(codec, WM8983_CLOCK_GEN_CONTROL,
  626. WM8983_MCLKDIV_MASK, i << WM8983_MCLKDIV_SHIFT);
  627. /* select the appropriate bclk divider */
  628. tmp = (wm8983->sysclk / fs_ratios[i].div) * 10;
  629. for (i = 0; i < ARRAY_SIZE(bclk_divs); ++i) {
  630. if (wm8983->bclk == tmp / bclk_divs[i])
  631. break;
  632. }
  633. if (i == ARRAY_SIZE(bclk_divs)) {
  634. dev_err(dai->dev, "No matching BCLK divider found\n");
  635. return -EINVAL;
  636. }
  637. dev_dbg(dai->dev, "BCLK div = %d\n", i);
  638. snd_soc_update_bits(codec, WM8983_CLOCK_GEN_CONTROL,
  639. WM8983_BCLKDIV_MASK, i << WM8983_BCLKDIV_SHIFT);
  640. return 0;
  641. }
  642. struct pll_div {
  643. u32 div2:1;
  644. u32 n:4;
  645. u32 k:24;
  646. };
  647. #define FIXED_PLL_SIZE ((1ULL << 24) * 10)
  648. static int pll_factors(struct pll_div *pll_div, unsigned int target,
  649. unsigned int source)
  650. {
  651. u64 Kpart;
  652. unsigned long int K, Ndiv, Nmod;
  653. pll_div->div2 = 0;
  654. Ndiv = target / source;
  655. if (Ndiv < 6) {
  656. source >>= 1;
  657. pll_div->div2 = 1;
  658. Ndiv = target / source;
  659. }
  660. if (Ndiv < 6 || Ndiv > 12) {
  661. printk(KERN_ERR "%s: WM8983 N value is not within"
  662. " the recommended range: %lu\n", __func__, Ndiv);
  663. return -EINVAL;
  664. }
  665. pll_div->n = Ndiv;
  666. Nmod = target % source;
  667. Kpart = FIXED_PLL_SIZE * (u64)Nmod;
  668. do_div(Kpart, source);
  669. K = Kpart & 0xffffffff;
  670. if ((K % 10) >= 5)
  671. K += 5;
  672. K /= 10;
  673. pll_div->k = K;
  674. return 0;
  675. }
  676. static int wm8983_set_pll(struct snd_soc_dai *dai, int pll_id,
  677. int source, unsigned int freq_in,
  678. unsigned int freq_out)
  679. {
  680. int ret;
  681. struct snd_soc_codec *codec;
  682. struct pll_div pll_div;
  683. codec = dai->codec;
  684. if (!freq_in || !freq_out) {
  685. /* disable the PLL */
  686. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  687. WM8983_PLLEN_MASK, 0);
  688. return 0;
  689. } else {
  690. ret = pll_factors(&pll_div, freq_out * 4 * 2, freq_in);
  691. if (ret)
  692. return ret;
  693. /* disable the PLL before re-programming it */
  694. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  695. WM8983_PLLEN_MASK, 0);
  696. /* set PLLN and PRESCALE */
  697. snd_soc_write(codec, WM8983_PLL_N,
  698. (pll_div.div2 << WM8983_PLL_PRESCALE_SHIFT)
  699. | pll_div.n);
  700. /* set PLLK */
  701. snd_soc_write(codec, WM8983_PLL_K_3, pll_div.k & 0x1ff);
  702. snd_soc_write(codec, WM8983_PLL_K_2, (pll_div.k >> 9) & 0x1ff);
  703. snd_soc_write(codec, WM8983_PLL_K_1, (pll_div.k >> 18));
  704. /* enable the PLL */
  705. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  706. WM8983_PLLEN_MASK, WM8983_PLLEN);
  707. }
  708. return 0;
  709. }
  710. static int wm8983_set_sysclk(struct snd_soc_dai *dai,
  711. int clk_id, unsigned int freq, int dir)
  712. {
  713. struct snd_soc_codec *codec = dai->codec;
  714. struct wm8983_priv *wm8983 = snd_soc_codec_get_drvdata(codec);
  715. switch (clk_id) {
  716. case WM8983_CLKSRC_MCLK:
  717. snd_soc_update_bits(codec, WM8983_CLOCK_GEN_CONTROL,
  718. WM8983_CLKSEL_MASK, 0);
  719. break;
  720. case WM8983_CLKSRC_PLL:
  721. snd_soc_update_bits(codec, WM8983_CLOCK_GEN_CONTROL,
  722. WM8983_CLKSEL_MASK, WM8983_CLKSEL);
  723. break;
  724. default:
  725. dev_err(dai->dev, "Unknown clock source: %d\n", clk_id);
  726. return -EINVAL;
  727. }
  728. wm8983->sysclk = freq;
  729. return 0;
  730. }
  731. static int wm8983_set_bias_level(struct snd_soc_codec *codec,
  732. enum snd_soc_bias_level level)
  733. {
  734. struct wm8983_priv *wm8983 = snd_soc_codec_get_drvdata(codec);
  735. int ret;
  736. switch (level) {
  737. case SND_SOC_BIAS_ON:
  738. case SND_SOC_BIAS_PREPARE:
  739. /* VMID at 100k */
  740. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  741. WM8983_VMIDSEL_MASK,
  742. 1 << WM8983_VMIDSEL_SHIFT);
  743. break;
  744. case SND_SOC_BIAS_STANDBY:
  745. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  746. ret = regcache_sync(wm8983->regmap);
  747. if (ret < 0) {
  748. dev_err(codec->dev, "Failed to sync cache: %d\n", ret);
  749. return ret;
  750. }
  751. /* enable anti-pop features */
  752. snd_soc_update_bits(codec, WM8983_OUT4_TO_ADC,
  753. WM8983_POBCTRL_MASK | WM8983_DELEN_MASK,
  754. WM8983_POBCTRL | WM8983_DELEN);
  755. /* enable thermal shutdown */
  756. snd_soc_update_bits(codec, WM8983_OUTPUT_CTRL,
  757. WM8983_TSDEN_MASK, WM8983_TSDEN);
  758. /* enable BIASEN */
  759. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  760. WM8983_BIASEN_MASK, WM8983_BIASEN);
  761. /* VMID at 100k */
  762. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  763. WM8983_VMIDSEL_MASK,
  764. 1 << WM8983_VMIDSEL_SHIFT);
  765. msleep(250);
  766. /* disable anti-pop features */
  767. snd_soc_update_bits(codec, WM8983_OUT4_TO_ADC,
  768. WM8983_POBCTRL_MASK |
  769. WM8983_DELEN_MASK, 0);
  770. }
  771. /* VMID at 500k */
  772. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  773. WM8983_VMIDSEL_MASK,
  774. 2 << WM8983_VMIDSEL_SHIFT);
  775. break;
  776. case SND_SOC_BIAS_OFF:
  777. /* disable thermal shutdown */
  778. snd_soc_update_bits(codec, WM8983_OUTPUT_CTRL,
  779. WM8983_TSDEN_MASK, 0);
  780. /* disable VMIDSEL and BIASEN */
  781. snd_soc_update_bits(codec, WM8983_POWER_MANAGEMENT_1,
  782. WM8983_VMIDSEL_MASK | WM8983_BIASEN_MASK,
  783. 0);
  784. /* wait for VMID to discharge */
  785. msleep(100);
  786. snd_soc_write(codec, WM8983_POWER_MANAGEMENT_1, 0);
  787. snd_soc_write(codec, WM8983_POWER_MANAGEMENT_2, 0);
  788. snd_soc_write(codec, WM8983_POWER_MANAGEMENT_3, 0);
  789. break;
  790. }
  791. return 0;
  792. }
  793. static int wm8983_probe(struct snd_soc_codec *codec)
  794. {
  795. int ret;
  796. int i;
  797. ret = snd_soc_write(codec, WM8983_SOFTWARE_RESET, 0);
  798. if (ret < 0) {
  799. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  800. return ret;
  801. }
  802. /* set the vol/gain update bits */
  803. for (i = 0; i < ARRAY_SIZE(vol_update_regs); ++i)
  804. snd_soc_update_bits(codec, vol_update_regs[i],
  805. 0x100, 0x100);
  806. /* mute all outputs and set PGAs to minimum gain */
  807. for (i = WM8983_LOUT1_HP_VOLUME_CTRL;
  808. i <= WM8983_OUT4_MONO_MIX_CTRL; ++i)
  809. snd_soc_update_bits(codec, i, 0x40, 0x40);
  810. /* enable soft mute */
  811. snd_soc_update_bits(codec, WM8983_DAC_CONTROL,
  812. WM8983_SOFTMUTE_MASK,
  813. WM8983_SOFTMUTE);
  814. /* enable BIASCUT */
  815. snd_soc_update_bits(codec, WM8983_BIAS_CTRL,
  816. WM8983_BIASCUT, WM8983_BIASCUT);
  817. return 0;
  818. }
  819. static const struct snd_soc_dai_ops wm8983_dai_ops = {
  820. .digital_mute = wm8983_dac_mute,
  821. .hw_params = wm8983_hw_params,
  822. .set_fmt = wm8983_set_fmt,
  823. .set_sysclk = wm8983_set_sysclk,
  824. .set_pll = wm8983_set_pll
  825. };
  826. #define WM8983_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  827. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  828. static struct snd_soc_dai_driver wm8983_dai = {
  829. .name = "wm8983-hifi",
  830. .playback = {
  831. .stream_name = "Playback",
  832. .channels_min = 2,
  833. .channels_max = 2,
  834. .rates = SNDRV_PCM_RATE_8000_48000,
  835. .formats = WM8983_FORMATS,
  836. },
  837. .capture = {
  838. .stream_name = "Capture",
  839. .channels_min = 2,
  840. .channels_max = 2,
  841. .rates = SNDRV_PCM_RATE_8000_48000,
  842. .formats = WM8983_FORMATS,
  843. },
  844. .ops = &wm8983_dai_ops,
  845. .symmetric_rates = 1
  846. };
  847. static const struct snd_soc_codec_driver soc_codec_dev_wm8983 = {
  848. .probe = wm8983_probe,
  849. .set_bias_level = wm8983_set_bias_level,
  850. .suspend_bias_off = true,
  851. .component_driver = {
  852. .controls = wm8983_snd_controls,
  853. .num_controls = ARRAY_SIZE(wm8983_snd_controls),
  854. .dapm_widgets = wm8983_dapm_widgets,
  855. .num_dapm_widgets = ARRAY_SIZE(wm8983_dapm_widgets),
  856. .dapm_routes = wm8983_audio_map,
  857. .num_dapm_routes = ARRAY_SIZE(wm8983_audio_map),
  858. },
  859. };
  860. static const struct regmap_config wm8983_regmap = {
  861. .reg_bits = 7,
  862. .val_bits = 9,
  863. .reg_defaults = wm8983_defaults,
  864. .num_reg_defaults = ARRAY_SIZE(wm8983_defaults),
  865. .cache_type = REGCACHE_RBTREE,
  866. .max_register = WM8983_MAX_REGISTER,
  867. .writeable_reg = wm8983_writeable,
  868. };
  869. #if defined(CONFIG_SPI_MASTER)
  870. static int wm8983_spi_probe(struct spi_device *spi)
  871. {
  872. struct wm8983_priv *wm8983;
  873. int ret;
  874. wm8983 = devm_kzalloc(&spi->dev, sizeof *wm8983, GFP_KERNEL);
  875. if (!wm8983)
  876. return -ENOMEM;
  877. wm8983->regmap = devm_regmap_init_spi(spi, &wm8983_regmap);
  878. if (IS_ERR(wm8983->regmap)) {
  879. ret = PTR_ERR(wm8983->regmap);
  880. dev_err(&spi->dev, "Failed to init regmap: %d\n", ret);
  881. return ret;
  882. }
  883. spi_set_drvdata(spi, wm8983);
  884. ret = snd_soc_register_codec(&spi->dev,
  885. &soc_codec_dev_wm8983, &wm8983_dai, 1);
  886. return ret;
  887. }
  888. static int wm8983_spi_remove(struct spi_device *spi)
  889. {
  890. snd_soc_unregister_codec(&spi->dev);
  891. return 0;
  892. }
  893. static struct spi_driver wm8983_spi_driver = {
  894. .driver = {
  895. .name = "wm8983",
  896. },
  897. .probe = wm8983_spi_probe,
  898. .remove = wm8983_spi_remove
  899. };
  900. #endif
  901. #if IS_ENABLED(CONFIG_I2C)
  902. static int wm8983_i2c_probe(struct i2c_client *i2c,
  903. const struct i2c_device_id *id)
  904. {
  905. struct wm8983_priv *wm8983;
  906. int ret;
  907. wm8983 = devm_kzalloc(&i2c->dev, sizeof *wm8983, GFP_KERNEL);
  908. if (!wm8983)
  909. return -ENOMEM;
  910. wm8983->regmap = devm_regmap_init_i2c(i2c, &wm8983_regmap);
  911. if (IS_ERR(wm8983->regmap)) {
  912. ret = PTR_ERR(wm8983->regmap);
  913. dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
  914. return ret;
  915. }
  916. i2c_set_clientdata(i2c, wm8983);
  917. ret = snd_soc_register_codec(&i2c->dev,
  918. &soc_codec_dev_wm8983, &wm8983_dai, 1);
  919. return ret;
  920. }
  921. static int wm8983_i2c_remove(struct i2c_client *client)
  922. {
  923. snd_soc_unregister_codec(&client->dev);
  924. return 0;
  925. }
  926. static const struct i2c_device_id wm8983_i2c_id[] = {
  927. { "wm8983", 0 },
  928. { }
  929. };
  930. MODULE_DEVICE_TABLE(i2c, wm8983_i2c_id);
  931. static struct i2c_driver wm8983_i2c_driver = {
  932. .driver = {
  933. .name = "wm8983",
  934. },
  935. .probe = wm8983_i2c_probe,
  936. .remove = wm8983_i2c_remove,
  937. .id_table = wm8983_i2c_id
  938. };
  939. #endif
  940. static int __init wm8983_modinit(void)
  941. {
  942. int ret = 0;
  943. #if IS_ENABLED(CONFIG_I2C)
  944. ret = i2c_add_driver(&wm8983_i2c_driver);
  945. if (ret) {
  946. printk(KERN_ERR "Failed to register wm8983 I2C driver: %d\n",
  947. ret);
  948. }
  949. #endif
  950. #if defined(CONFIG_SPI_MASTER)
  951. ret = spi_register_driver(&wm8983_spi_driver);
  952. if (ret != 0) {
  953. printk(KERN_ERR "Failed to register wm8983 SPI driver: %d\n",
  954. ret);
  955. }
  956. #endif
  957. return ret;
  958. }
  959. module_init(wm8983_modinit);
  960. static void __exit wm8983_exit(void)
  961. {
  962. #if IS_ENABLED(CONFIG_I2C)
  963. i2c_del_driver(&wm8983_i2c_driver);
  964. #endif
  965. #if defined(CONFIG_SPI_MASTER)
  966. spi_unregister_driver(&wm8983_spi_driver);
  967. #endif
  968. }
  969. module_exit(wm8983_exit);
  970. MODULE_DESCRIPTION("ASoC WM8983 driver");
  971. MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
  972. MODULE_LICENSE("GPL");