wm8985.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * wm8985.c -- WM8985 ALSA SoC Audio driver
  3. *
  4. * Copyright 2010 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. * TODO:
  13. * o Add OUT3/OUT4 mixer controls.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/pm.h>
  20. #include <linux/i2c.h>
  21. #include <linux/regmap.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/slab.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/initval.h>
  30. #include <sound/tlv.h>
  31. #include "wm8985.h"
  32. #define WM8985_NUM_SUPPLIES 4
  33. static const char *wm8985_supply_names[WM8985_NUM_SUPPLIES] = {
  34. "DCVDD",
  35. "DBVDD",
  36. "AVDD1",
  37. "AVDD2"
  38. };
  39. static const struct reg_default wm8985_reg_defaults[] = {
  40. { 1, 0x0000 }, /* R1 - Power management 1 */
  41. { 2, 0x0000 }, /* R2 - Power management 2 */
  42. { 3, 0x0000 }, /* R3 - Power management 3 */
  43. { 4, 0x0050 }, /* R4 - Audio Interface */
  44. { 5, 0x0000 }, /* R5 - Companding control */
  45. { 6, 0x0140 }, /* R6 - Clock Gen control */
  46. { 7, 0x0000 }, /* R7 - Additional control */
  47. { 8, 0x0000 }, /* R8 - GPIO Control */
  48. { 9, 0x0000 }, /* R9 - Jack Detect Control 1 */
  49. { 10, 0x0000 }, /* R10 - DAC Control */
  50. { 11, 0x00FF }, /* R11 - Left DAC digital Vol */
  51. { 12, 0x00FF }, /* R12 - Right DAC digital vol */
  52. { 13, 0x0000 }, /* R13 - Jack Detect Control 2 */
  53. { 14, 0x0100 }, /* R14 - ADC Control */
  54. { 15, 0x00FF }, /* R15 - Left ADC Digital Vol */
  55. { 16, 0x00FF }, /* R16 - Right ADC Digital Vol */
  56. { 18, 0x012C }, /* R18 - EQ1 - low shelf */
  57. { 19, 0x002C }, /* R19 - EQ2 - peak 1 */
  58. { 20, 0x002C }, /* R20 - EQ3 - peak 2 */
  59. { 21, 0x002C }, /* R21 - EQ4 - peak 3 */
  60. { 22, 0x002C }, /* R22 - EQ5 - high shelf */
  61. { 24, 0x0032 }, /* R24 - DAC Limiter 1 */
  62. { 25, 0x0000 }, /* R25 - DAC Limiter 2 */
  63. { 27, 0x0000 }, /* R27 - Notch Filter 1 */
  64. { 28, 0x0000 }, /* R28 - Notch Filter 2 */
  65. { 29, 0x0000 }, /* R29 - Notch Filter 3 */
  66. { 30, 0x0000 }, /* R30 - Notch Filter 4 */
  67. { 32, 0x0038 }, /* R32 - ALC control 1 */
  68. { 33, 0x000B }, /* R33 - ALC control 2 */
  69. { 34, 0x0032 }, /* R34 - ALC control 3 */
  70. { 35, 0x0000 }, /* R35 - Noise Gate */
  71. { 36, 0x0008 }, /* R36 - PLL N */
  72. { 37, 0x000C }, /* R37 - PLL K 1 */
  73. { 38, 0x0093 }, /* R38 - PLL K 2 */
  74. { 39, 0x00E9 }, /* R39 - PLL K 3 */
  75. { 41, 0x0000 }, /* R41 - 3D control */
  76. { 42, 0x0000 }, /* R42 - OUT4 to ADC */
  77. { 43, 0x0000 }, /* R43 - Beep control */
  78. { 44, 0x0033 }, /* R44 - Input ctrl */
  79. { 45, 0x0010 }, /* R45 - Left INP PGA gain ctrl */
  80. { 46, 0x0010 }, /* R46 - Right INP PGA gain ctrl */
  81. { 47, 0x0100 }, /* R47 - Left ADC BOOST ctrl */
  82. { 48, 0x0100 }, /* R48 - Right ADC BOOST ctrl */
  83. { 49, 0x0002 }, /* R49 - Output ctrl */
  84. { 50, 0x0001 }, /* R50 - Left mixer ctrl */
  85. { 51, 0x0001 }, /* R51 - Right mixer ctrl */
  86. { 52, 0x0039 }, /* R52 - LOUT1 (HP) volume ctrl */
  87. { 53, 0x0039 }, /* R53 - ROUT1 (HP) volume ctrl */
  88. { 54, 0x0039 }, /* R54 - LOUT2 (SPK) volume ctrl */
  89. { 55, 0x0039 }, /* R55 - ROUT2 (SPK) volume ctrl */
  90. { 56, 0x0001 }, /* R56 - OUT3 mixer ctrl */
  91. { 57, 0x0001 }, /* R57 - OUT4 (MONO) mix ctrl */
  92. { 60, 0x0004 }, /* R60 - OUTPUT ctrl */
  93. { 61, 0x0000 }, /* R61 - BIAS CTRL */
  94. };
  95. static bool wm8985_writeable(struct device *dev, unsigned int reg)
  96. {
  97. switch (reg) {
  98. case WM8985_SOFTWARE_RESET:
  99. case WM8985_POWER_MANAGEMENT_1:
  100. case WM8985_POWER_MANAGEMENT_2:
  101. case WM8985_POWER_MANAGEMENT_3:
  102. case WM8985_AUDIO_INTERFACE:
  103. case WM8985_COMPANDING_CONTROL:
  104. case WM8985_CLOCK_GEN_CONTROL:
  105. case WM8985_ADDITIONAL_CONTROL:
  106. case WM8985_GPIO_CONTROL:
  107. case WM8985_JACK_DETECT_CONTROL_1:
  108. case WM8985_DAC_CONTROL:
  109. case WM8985_LEFT_DAC_DIGITAL_VOL:
  110. case WM8985_RIGHT_DAC_DIGITAL_VOL:
  111. case WM8985_JACK_DETECT_CONTROL_2:
  112. case WM8985_ADC_CONTROL:
  113. case WM8985_LEFT_ADC_DIGITAL_VOL:
  114. case WM8985_RIGHT_ADC_DIGITAL_VOL:
  115. case WM8985_EQ1_LOW_SHELF:
  116. case WM8985_EQ2_PEAK_1:
  117. case WM8985_EQ3_PEAK_2:
  118. case WM8985_EQ4_PEAK_3:
  119. case WM8985_EQ5_HIGH_SHELF:
  120. case WM8985_DAC_LIMITER_1:
  121. case WM8985_DAC_LIMITER_2:
  122. case WM8985_NOTCH_FILTER_1:
  123. case WM8985_NOTCH_FILTER_2:
  124. case WM8985_NOTCH_FILTER_3:
  125. case WM8985_NOTCH_FILTER_4:
  126. case WM8985_ALC_CONTROL_1:
  127. case WM8985_ALC_CONTROL_2:
  128. case WM8985_ALC_CONTROL_3:
  129. case WM8985_NOISE_GATE:
  130. case WM8985_PLL_N:
  131. case WM8985_PLL_K_1:
  132. case WM8985_PLL_K_2:
  133. case WM8985_PLL_K_3:
  134. case WM8985_3D_CONTROL:
  135. case WM8985_OUT4_TO_ADC:
  136. case WM8985_BEEP_CONTROL:
  137. case WM8985_INPUT_CTRL:
  138. case WM8985_LEFT_INP_PGA_GAIN_CTRL:
  139. case WM8985_RIGHT_INP_PGA_GAIN_CTRL:
  140. case WM8985_LEFT_ADC_BOOST_CTRL:
  141. case WM8985_RIGHT_ADC_BOOST_CTRL:
  142. case WM8985_OUTPUT_CTRL0:
  143. case WM8985_LEFT_MIXER_CTRL:
  144. case WM8985_RIGHT_MIXER_CTRL:
  145. case WM8985_LOUT1_HP_VOLUME_CTRL:
  146. case WM8985_ROUT1_HP_VOLUME_CTRL:
  147. case WM8985_LOUT2_SPK_VOLUME_CTRL:
  148. case WM8985_ROUT2_SPK_VOLUME_CTRL:
  149. case WM8985_OUT3_MIXER_CTRL:
  150. case WM8985_OUT4_MONO_MIX_CTRL:
  151. case WM8985_OUTPUT_CTRL1:
  152. case WM8985_BIAS_CTRL:
  153. return true;
  154. default:
  155. return false;
  156. }
  157. }
  158. /*
  159. * latch bit 8 of these registers to ensure instant
  160. * volume updates
  161. */
  162. static const int volume_update_regs[] = {
  163. WM8985_LEFT_DAC_DIGITAL_VOL,
  164. WM8985_RIGHT_DAC_DIGITAL_VOL,
  165. WM8985_LEFT_ADC_DIGITAL_VOL,
  166. WM8985_RIGHT_ADC_DIGITAL_VOL,
  167. WM8985_LOUT2_SPK_VOLUME_CTRL,
  168. WM8985_ROUT2_SPK_VOLUME_CTRL,
  169. WM8985_LOUT1_HP_VOLUME_CTRL,
  170. WM8985_ROUT1_HP_VOLUME_CTRL,
  171. WM8985_LEFT_INP_PGA_GAIN_CTRL,
  172. WM8985_RIGHT_INP_PGA_GAIN_CTRL
  173. };
  174. struct wm8985_priv {
  175. struct regmap *regmap;
  176. struct regulator_bulk_data supplies[WM8985_NUM_SUPPLIES];
  177. unsigned int sysclk;
  178. unsigned int bclk;
  179. };
  180. static const struct {
  181. int div;
  182. int ratio;
  183. } fs_ratios[] = {
  184. { 10, 128 },
  185. { 15, 192 },
  186. { 20, 256 },
  187. { 30, 384 },
  188. { 40, 512 },
  189. { 60, 768 },
  190. { 80, 1024 },
  191. { 120, 1536 }
  192. };
  193. static const int srates[] = { 48000, 32000, 24000, 16000, 12000, 8000 };
  194. static const int bclk_divs[] = {
  195. 1, 2, 4, 8, 16, 32
  196. };
  197. static int eqmode_get(struct snd_kcontrol *kcontrol,
  198. struct snd_ctl_elem_value *ucontrol);
  199. static int eqmode_put(struct snd_kcontrol *kcontrol,
  200. struct snd_ctl_elem_value *ucontrol);
  201. static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1);
  202. static const DECLARE_TLV_DB_SCALE(adc_tlv, -12700, 50, 1);
  203. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  204. static const DECLARE_TLV_DB_SCALE(lim_thresh_tlv, -600, 100, 0);
  205. static const DECLARE_TLV_DB_SCALE(lim_boost_tlv, 0, 100, 0);
  206. static const DECLARE_TLV_DB_SCALE(alc_min_tlv, -1200, 600, 0);
  207. static const DECLARE_TLV_DB_SCALE(alc_max_tlv, -675, 600, 0);
  208. static const DECLARE_TLV_DB_SCALE(alc_tar_tlv, -2250, 150, 0);
  209. static const DECLARE_TLV_DB_SCALE(pga_vol_tlv, -1200, 75, 0);
  210. static const DECLARE_TLV_DB_SCALE(boost_tlv, -1200, 300, 1);
  211. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  212. static const DECLARE_TLV_DB_SCALE(aux_tlv, -1500, 300, 0);
  213. static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
  214. static const DECLARE_TLV_DB_SCALE(pga_boost_tlv, 0, 2000, 0);
  215. static const char *alc_sel_text[] = { "Off", "Right", "Left", "Stereo" };
  216. static const SOC_ENUM_SINGLE_DECL(alc_sel, WM8985_ALC_CONTROL_1, 7,
  217. alc_sel_text);
  218. static const char *alc_mode_text[] = { "ALC", "Limiter" };
  219. static const SOC_ENUM_SINGLE_DECL(alc_mode, WM8985_ALC_CONTROL_3, 8,
  220. alc_mode_text);
  221. static const char *filter_mode_text[] = { "Audio", "Application" };
  222. static const SOC_ENUM_SINGLE_DECL(filter_mode, WM8985_ADC_CONTROL, 7,
  223. filter_mode_text);
  224. static const char *eq_bw_text[] = { "Narrow", "Wide" };
  225. static const char *eqmode_text[] = { "Capture", "Playback" };
  226. static const SOC_ENUM_SINGLE_EXT_DECL(eqmode, eqmode_text);
  227. static const char *eq1_cutoff_text[] = {
  228. "80Hz", "105Hz", "135Hz", "175Hz"
  229. };
  230. static const SOC_ENUM_SINGLE_DECL(eq1_cutoff, WM8985_EQ1_LOW_SHELF, 5,
  231. eq1_cutoff_text);
  232. static const char *eq2_cutoff_text[] = {
  233. "230Hz", "300Hz", "385Hz", "500Hz"
  234. };
  235. static const SOC_ENUM_SINGLE_DECL(eq2_bw, WM8985_EQ2_PEAK_1, 8, eq_bw_text);
  236. static const SOC_ENUM_SINGLE_DECL(eq2_cutoff, WM8985_EQ2_PEAK_1, 5,
  237. eq2_cutoff_text);
  238. static const char *eq3_cutoff_text[] = {
  239. "650Hz", "850Hz", "1.1kHz", "1.4kHz"
  240. };
  241. static const SOC_ENUM_SINGLE_DECL(eq3_bw, WM8985_EQ3_PEAK_2, 8, eq_bw_text);
  242. static const SOC_ENUM_SINGLE_DECL(eq3_cutoff, WM8985_EQ3_PEAK_2, 5,
  243. eq3_cutoff_text);
  244. static const char *eq4_cutoff_text[] = {
  245. "1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz"
  246. };
  247. static const SOC_ENUM_SINGLE_DECL(eq4_bw, WM8985_EQ4_PEAK_3, 8, eq_bw_text);
  248. static const SOC_ENUM_SINGLE_DECL(eq4_cutoff, WM8985_EQ4_PEAK_3, 5,
  249. eq4_cutoff_text);
  250. static const char *eq5_cutoff_text[] = {
  251. "5.3kHz", "6.9kHz", "9kHz", "11.7kHz"
  252. };
  253. static const SOC_ENUM_SINGLE_DECL(eq5_cutoff, WM8985_EQ5_HIGH_SHELF, 5,
  254. eq5_cutoff_text);
  255. static const char *speaker_mode_text[] = { "Class A/B", "Class D" };
  256. static const SOC_ENUM_SINGLE_DECL(speaker_mode, 0x17, 8, speaker_mode_text);
  257. static const char *depth_3d_text[] = {
  258. "Off",
  259. "6.67%",
  260. "13.3%",
  261. "20%",
  262. "26.7%",
  263. "33.3%",
  264. "40%",
  265. "46.6%",
  266. "53.3%",
  267. "60%",
  268. "66.7%",
  269. "73.3%",
  270. "80%",
  271. "86.7%",
  272. "93.3%",
  273. "100%"
  274. };
  275. static const SOC_ENUM_SINGLE_DECL(depth_3d, WM8985_3D_CONTROL, 0,
  276. depth_3d_text);
  277. static const struct snd_kcontrol_new wm8985_snd_controls[] = {
  278. SOC_SINGLE("Digital Loopback Switch", WM8985_COMPANDING_CONTROL,
  279. 0, 1, 0),
  280. SOC_ENUM("ALC Capture Function", alc_sel),
  281. SOC_SINGLE_TLV("ALC Capture Max Volume", WM8985_ALC_CONTROL_1,
  282. 3, 7, 0, alc_max_tlv),
  283. SOC_SINGLE_TLV("ALC Capture Min Volume", WM8985_ALC_CONTROL_1,
  284. 0, 7, 0, alc_min_tlv),
  285. SOC_SINGLE_TLV("ALC Capture Target Volume", WM8985_ALC_CONTROL_2,
  286. 0, 15, 0, alc_tar_tlv),
  287. SOC_SINGLE("ALC Capture Attack", WM8985_ALC_CONTROL_3, 0, 10, 0),
  288. SOC_SINGLE("ALC Capture Hold", WM8985_ALC_CONTROL_2, 4, 10, 0),
  289. SOC_SINGLE("ALC Capture Decay", WM8985_ALC_CONTROL_3, 4, 10, 0),
  290. SOC_ENUM("ALC Mode", alc_mode),
  291. SOC_SINGLE("ALC Capture NG Switch", WM8985_NOISE_GATE,
  292. 3, 1, 0),
  293. SOC_SINGLE("ALC Capture NG Threshold", WM8985_NOISE_GATE,
  294. 0, 7, 1),
  295. SOC_DOUBLE_R_TLV("Capture Volume", WM8985_LEFT_ADC_DIGITAL_VOL,
  296. WM8985_RIGHT_ADC_DIGITAL_VOL, 0, 255, 0, adc_tlv),
  297. SOC_DOUBLE_R("Capture PGA ZC Switch", WM8985_LEFT_INP_PGA_GAIN_CTRL,
  298. WM8985_RIGHT_INP_PGA_GAIN_CTRL, 7, 1, 0),
  299. SOC_DOUBLE_R_TLV("Capture PGA Volume", WM8985_LEFT_INP_PGA_GAIN_CTRL,
  300. WM8985_RIGHT_INP_PGA_GAIN_CTRL, 0, 63, 0, pga_vol_tlv),
  301. SOC_DOUBLE_R_TLV("Capture PGA Boost Volume",
  302. WM8985_LEFT_ADC_BOOST_CTRL, WM8985_RIGHT_ADC_BOOST_CTRL,
  303. 8, 1, 0, pga_boost_tlv),
  304. SOC_DOUBLE("ADC Inversion Switch", WM8985_ADC_CONTROL, 0, 1, 1, 0),
  305. SOC_SINGLE("ADC 128x Oversampling Switch", WM8985_ADC_CONTROL, 8, 1, 0),
  306. SOC_DOUBLE_R_TLV("Playback Volume", WM8985_LEFT_DAC_DIGITAL_VOL,
  307. WM8985_RIGHT_DAC_DIGITAL_VOL, 0, 255, 0, dac_tlv),
  308. SOC_SINGLE("DAC Playback Limiter Switch", WM8985_DAC_LIMITER_1, 8, 1, 0),
  309. SOC_SINGLE("DAC Playback Limiter Decay", WM8985_DAC_LIMITER_1, 4, 10, 0),
  310. SOC_SINGLE("DAC Playback Limiter Attack", WM8985_DAC_LIMITER_1, 0, 11, 0),
  311. SOC_SINGLE_TLV("DAC Playback Limiter Threshold", WM8985_DAC_LIMITER_2,
  312. 4, 7, 1, lim_thresh_tlv),
  313. SOC_SINGLE_TLV("DAC Playback Limiter Boost Volume", WM8985_DAC_LIMITER_2,
  314. 0, 12, 0, lim_boost_tlv),
  315. SOC_DOUBLE("DAC Inversion Switch", WM8985_DAC_CONTROL, 0, 1, 1, 0),
  316. SOC_SINGLE("DAC Auto Mute Switch", WM8985_DAC_CONTROL, 2, 1, 0),
  317. SOC_SINGLE("DAC 128x Oversampling Switch", WM8985_DAC_CONTROL, 3, 1, 0),
  318. SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8985_LOUT1_HP_VOLUME_CTRL,
  319. WM8985_ROUT1_HP_VOLUME_CTRL, 0, 63, 0, out_tlv),
  320. SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8985_LOUT1_HP_VOLUME_CTRL,
  321. WM8985_ROUT1_HP_VOLUME_CTRL, 7, 1, 0),
  322. SOC_DOUBLE_R("Headphone Switch", WM8985_LOUT1_HP_VOLUME_CTRL,
  323. WM8985_ROUT1_HP_VOLUME_CTRL, 6, 1, 1),
  324. SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8985_LOUT2_SPK_VOLUME_CTRL,
  325. WM8985_ROUT2_SPK_VOLUME_CTRL, 0, 63, 0, out_tlv),
  326. SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8985_LOUT2_SPK_VOLUME_CTRL,
  327. WM8985_ROUT2_SPK_VOLUME_CTRL, 7, 1, 0),
  328. SOC_DOUBLE_R("Speaker Switch", WM8985_LOUT2_SPK_VOLUME_CTRL,
  329. WM8985_ROUT2_SPK_VOLUME_CTRL, 6, 1, 1),
  330. SOC_SINGLE("High Pass Filter Switch", WM8985_ADC_CONTROL, 8, 1, 0),
  331. SOC_ENUM("High Pass Filter Mode", filter_mode),
  332. SOC_SINGLE("High Pass Filter Cutoff", WM8985_ADC_CONTROL, 4, 7, 0),
  333. SOC_DOUBLE_R_TLV("Aux Bypass Volume",
  334. WM8985_LEFT_MIXER_CTRL, WM8985_RIGHT_MIXER_CTRL, 6, 7, 0,
  335. aux_tlv),
  336. SOC_DOUBLE_R_TLV("Input PGA Bypass Volume",
  337. WM8985_LEFT_MIXER_CTRL, WM8985_RIGHT_MIXER_CTRL, 2, 7, 0,
  338. bypass_tlv),
  339. SOC_ENUM_EXT("Equalizer Function", eqmode, eqmode_get, eqmode_put),
  340. SOC_ENUM("EQ1 Cutoff", eq1_cutoff),
  341. SOC_SINGLE_TLV("EQ1 Volume", WM8985_EQ1_LOW_SHELF, 0, 24, 1, eq_tlv),
  342. SOC_ENUM("EQ2 Bandwith", eq2_bw),
  343. SOC_ENUM("EQ2 Cutoff", eq2_cutoff),
  344. SOC_SINGLE_TLV("EQ2 Volume", WM8985_EQ2_PEAK_1, 0, 24, 1, eq_tlv),
  345. SOC_ENUM("EQ3 Bandwith", eq3_bw),
  346. SOC_ENUM("EQ3 Cutoff", eq3_cutoff),
  347. SOC_SINGLE_TLV("EQ3 Volume", WM8985_EQ3_PEAK_2, 0, 24, 1, eq_tlv),
  348. SOC_ENUM("EQ4 Bandwith", eq4_bw),
  349. SOC_ENUM("EQ4 Cutoff", eq4_cutoff),
  350. SOC_SINGLE_TLV("EQ4 Volume", WM8985_EQ4_PEAK_3, 0, 24, 1, eq_tlv),
  351. SOC_ENUM("EQ5 Cutoff", eq5_cutoff),
  352. SOC_SINGLE_TLV("EQ5 Volume", WM8985_EQ5_HIGH_SHELF, 0, 24, 1, eq_tlv),
  353. SOC_ENUM("3D Depth", depth_3d),
  354. SOC_ENUM("Speaker Mode", speaker_mode)
  355. };
  356. static const struct snd_kcontrol_new left_out_mixer[] = {
  357. SOC_DAPM_SINGLE("Line Switch", WM8985_LEFT_MIXER_CTRL, 1, 1, 0),
  358. SOC_DAPM_SINGLE("Aux Switch", WM8985_LEFT_MIXER_CTRL, 5, 1, 0),
  359. SOC_DAPM_SINGLE("PCM Switch", WM8985_LEFT_MIXER_CTRL, 0, 1, 0),
  360. };
  361. static const struct snd_kcontrol_new right_out_mixer[] = {
  362. SOC_DAPM_SINGLE("Line Switch", WM8985_RIGHT_MIXER_CTRL, 1, 1, 0),
  363. SOC_DAPM_SINGLE("Aux Switch", WM8985_RIGHT_MIXER_CTRL, 5, 1, 0),
  364. SOC_DAPM_SINGLE("PCM Switch", WM8985_RIGHT_MIXER_CTRL, 0, 1, 0),
  365. };
  366. static const struct snd_kcontrol_new left_input_mixer[] = {
  367. SOC_DAPM_SINGLE("L2 Switch", WM8985_INPUT_CTRL, 2, 1, 0),
  368. SOC_DAPM_SINGLE("MicN Switch", WM8985_INPUT_CTRL, 1, 1, 0),
  369. SOC_DAPM_SINGLE("MicP Switch", WM8985_INPUT_CTRL, 0, 1, 0),
  370. };
  371. static const struct snd_kcontrol_new right_input_mixer[] = {
  372. SOC_DAPM_SINGLE("R2 Switch", WM8985_INPUT_CTRL, 6, 1, 0),
  373. SOC_DAPM_SINGLE("MicN Switch", WM8985_INPUT_CTRL, 5, 1, 0),
  374. SOC_DAPM_SINGLE("MicP Switch", WM8985_INPUT_CTRL, 4, 1, 0),
  375. };
  376. static const struct snd_kcontrol_new left_boost_mixer[] = {
  377. SOC_DAPM_SINGLE_TLV("L2 Volume", WM8985_LEFT_ADC_BOOST_CTRL,
  378. 4, 7, 0, boost_tlv),
  379. SOC_DAPM_SINGLE_TLV("AUXL Volume", WM8985_LEFT_ADC_BOOST_CTRL,
  380. 0, 7, 0, boost_tlv)
  381. };
  382. static const struct snd_kcontrol_new right_boost_mixer[] = {
  383. SOC_DAPM_SINGLE_TLV("R2 Volume", WM8985_RIGHT_ADC_BOOST_CTRL,
  384. 4, 7, 0, boost_tlv),
  385. SOC_DAPM_SINGLE_TLV("AUXR Volume", WM8985_RIGHT_ADC_BOOST_CTRL,
  386. 0, 7, 0, boost_tlv)
  387. };
  388. static const struct snd_soc_dapm_widget wm8985_dapm_widgets[] = {
  389. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8985_POWER_MANAGEMENT_3,
  390. 0, 0),
  391. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8985_POWER_MANAGEMENT_3,
  392. 1, 0),
  393. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8985_POWER_MANAGEMENT_2,
  394. 0, 0),
  395. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8985_POWER_MANAGEMENT_2,
  396. 1, 0),
  397. SND_SOC_DAPM_MIXER("Left Output Mixer", WM8985_POWER_MANAGEMENT_3,
  398. 2, 0, left_out_mixer, ARRAY_SIZE(left_out_mixer)),
  399. SND_SOC_DAPM_MIXER("Right Output Mixer", WM8985_POWER_MANAGEMENT_3,
  400. 3, 0, right_out_mixer, ARRAY_SIZE(right_out_mixer)),
  401. SND_SOC_DAPM_MIXER("Left Input Mixer", WM8985_POWER_MANAGEMENT_2,
  402. 2, 0, left_input_mixer, ARRAY_SIZE(left_input_mixer)),
  403. SND_SOC_DAPM_MIXER("Right Input Mixer", WM8985_POWER_MANAGEMENT_2,
  404. 3, 0, right_input_mixer, ARRAY_SIZE(right_input_mixer)),
  405. SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8985_POWER_MANAGEMENT_2,
  406. 4, 0, left_boost_mixer, ARRAY_SIZE(left_boost_mixer)),
  407. SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8985_POWER_MANAGEMENT_2,
  408. 5, 0, right_boost_mixer, ARRAY_SIZE(right_boost_mixer)),
  409. SND_SOC_DAPM_PGA("Left Capture PGA", WM8985_LEFT_INP_PGA_GAIN_CTRL,
  410. 6, 1, NULL, 0),
  411. SND_SOC_DAPM_PGA("Right Capture PGA", WM8985_RIGHT_INP_PGA_GAIN_CTRL,
  412. 6, 1, NULL, 0),
  413. SND_SOC_DAPM_PGA("Left Headphone Out", WM8985_POWER_MANAGEMENT_2,
  414. 7, 0, NULL, 0),
  415. SND_SOC_DAPM_PGA("Right Headphone Out", WM8985_POWER_MANAGEMENT_2,
  416. 8, 0, NULL, 0),
  417. SND_SOC_DAPM_PGA("Left Speaker Out", WM8985_POWER_MANAGEMENT_3,
  418. 5, 0, NULL, 0),
  419. SND_SOC_DAPM_PGA("Right Speaker Out", WM8985_POWER_MANAGEMENT_3,
  420. 6, 0, NULL, 0),
  421. SND_SOC_DAPM_SUPPLY("Mic Bias", WM8985_POWER_MANAGEMENT_1, 4, 0,
  422. NULL, 0),
  423. SND_SOC_DAPM_INPUT("LIN"),
  424. SND_SOC_DAPM_INPUT("LIP"),
  425. SND_SOC_DAPM_INPUT("RIN"),
  426. SND_SOC_DAPM_INPUT("RIP"),
  427. SND_SOC_DAPM_INPUT("AUXL"),
  428. SND_SOC_DAPM_INPUT("AUXR"),
  429. SND_SOC_DAPM_INPUT("L2"),
  430. SND_SOC_DAPM_INPUT("R2"),
  431. SND_SOC_DAPM_OUTPUT("HPL"),
  432. SND_SOC_DAPM_OUTPUT("HPR"),
  433. SND_SOC_DAPM_OUTPUT("SPKL"),
  434. SND_SOC_DAPM_OUTPUT("SPKR")
  435. };
  436. static const struct snd_soc_dapm_route wm8985_dapm_routes[] = {
  437. { "Right Output Mixer", "PCM Switch", "Right DAC" },
  438. { "Right Output Mixer", "Aux Switch", "AUXR" },
  439. { "Right Output Mixer", "Line Switch", "Right Boost Mixer" },
  440. { "Left Output Mixer", "PCM Switch", "Left DAC" },
  441. { "Left Output Mixer", "Aux Switch", "AUXL" },
  442. { "Left Output Mixer", "Line Switch", "Left Boost Mixer" },
  443. { "Right Headphone Out", NULL, "Right Output Mixer" },
  444. { "HPR", NULL, "Right Headphone Out" },
  445. { "Left Headphone Out", NULL, "Left Output Mixer" },
  446. { "HPL", NULL, "Left Headphone Out" },
  447. { "Right Speaker Out", NULL, "Right Output Mixer" },
  448. { "SPKR", NULL, "Right Speaker Out" },
  449. { "Left Speaker Out", NULL, "Left Output Mixer" },
  450. { "SPKL", NULL, "Left Speaker Out" },
  451. { "Right ADC", NULL, "Right Boost Mixer" },
  452. { "Right Boost Mixer", "AUXR Volume", "AUXR" },
  453. { "Right Boost Mixer", NULL, "Right Capture PGA" },
  454. { "Right Boost Mixer", "R2 Volume", "R2" },
  455. { "Left ADC", NULL, "Left Boost Mixer" },
  456. { "Left Boost Mixer", "AUXL Volume", "AUXL" },
  457. { "Left Boost Mixer", NULL, "Left Capture PGA" },
  458. { "Left Boost Mixer", "L2 Volume", "L2" },
  459. { "Right Capture PGA", NULL, "Right Input Mixer" },
  460. { "Left Capture PGA", NULL, "Left Input Mixer" },
  461. { "Right Input Mixer", "R2 Switch", "R2" },
  462. { "Right Input Mixer", "MicN Switch", "RIN" },
  463. { "Right Input Mixer", "MicP Switch", "RIP" },
  464. { "Left Input Mixer", "L2 Switch", "L2" },
  465. { "Left Input Mixer", "MicN Switch", "LIN" },
  466. { "Left Input Mixer", "MicP Switch", "LIP" },
  467. };
  468. static int eqmode_get(struct snd_kcontrol *kcontrol,
  469. struct snd_ctl_elem_value *ucontrol)
  470. {
  471. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  472. unsigned int reg;
  473. reg = snd_soc_read(codec, WM8985_EQ1_LOW_SHELF);
  474. if (reg & WM8985_EQ3DMODE)
  475. ucontrol->value.integer.value[0] = 1;
  476. else
  477. ucontrol->value.integer.value[0] = 0;
  478. return 0;
  479. }
  480. static int eqmode_put(struct snd_kcontrol *kcontrol,
  481. struct snd_ctl_elem_value *ucontrol)
  482. {
  483. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  484. unsigned int regpwr2, regpwr3;
  485. unsigned int reg_eq;
  486. if (ucontrol->value.integer.value[0] != 0
  487. && ucontrol->value.integer.value[0] != 1)
  488. return -EINVAL;
  489. reg_eq = snd_soc_read(codec, WM8985_EQ1_LOW_SHELF);
  490. switch ((reg_eq & WM8985_EQ3DMODE) >> WM8985_EQ3DMODE_SHIFT) {
  491. case 0:
  492. if (!ucontrol->value.integer.value[0])
  493. return 0;
  494. break;
  495. case 1:
  496. if (ucontrol->value.integer.value[0])
  497. return 0;
  498. break;
  499. }
  500. regpwr2 = snd_soc_read(codec, WM8985_POWER_MANAGEMENT_2);
  501. regpwr3 = snd_soc_read(codec, WM8985_POWER_MANAGEMENT_3);
  502. /* disable the DACs and ADCs */
  503. snd_soc_update_bits(codec, WM8985_POWER_MANAGEMENT_2,
  504. WM8985_ADCENR_MASK | WM8985_ADCENL_MASK, 0);
  505. snd_soc_update_bits(codec, WM8985_POWER_MANAGEMENT_3,
  506. WM8985_DACENR_MASK | WM8985_DACENL_MASK, 0);
  507. snd_soc_update_bits(codec, WM8985_ADDITIONAL_CONTROL,
  508. WM8985_M128ENB_MASK, WM8985_M128ENB);
  509. /* set the desired eqmode */
  510. snd_soc_update_bits(codec, WM8985_EQ1_LOW_SHELF,
  511. WM8985_EQ3DMODE_MASK,
  512. ucontrol->value.integer.value[0]
  513. << WM8985_EQ3DMODE_SHIFT);
  514. /* restore DAC/ADC configuration */
  515. snd_soc_write(codec, WM8985_POWER_MANAGEMENT_2, regpwr2);
  516. snd_soc_write(codec, WM8985_POWER_MANAGEMENT_3, regpwr3);
  517. return 0;
  518. }
  519. static int wm8985_reset(struct snd_soc_codec *codec)
  520. {
  521. return snd_soc_write(codec, WM8985_SOFTWARE_RESET, 0x0);
  522. }
  523. static int wm8985_dac_mute(struct snd_soc_dai *dai, int mute)
  524. {
  525. struct snd_soc_codec *codec = dai->codec;
  526. return snd_soc_update_bits(codec, WM8985_DAC_CONTROL,
  527. WM8985_SOFTMUTE_MASK,
  528. !!mute << WM8985_SOFTMUTE_SHIFT);
  529. }
  530. static int wm8985_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  531. {
  532. struct snd_soc_codec *codec;
  533. u16 format, master, bcp, lrp;
  534. codec = dai->codec;
  535. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  536. case SND_SOC_DAIFMT_I2S:
  537. format = 0x2;
  538. break;
  539. case SND_SOC_DAIFMT_RIGHT_J:
  540. format = 0x0;
  541. break;
  542. case SND_SOC_DAIFMT_LEFT_J:
  543. format = 0x1;
  544. break;
  545. case SND_SOC_DAIFMT_DSP_A:
  546. case SND_SOC_DAIFMT_DSP_B:
  547. format = 0x3;
  548. break;
  549. default:
  550. dev_err(dai->dev, "Unknown dai format\n");
  551. return -EINVAL;
  552. }
  553. snd_soc_update_bits(codec, WM8985_AUDIO_INTERFACE,
  554. WM8985_FMT_MASK, format << WM8985_FMT_SHIFT);
  555. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  556. case SND_SOC_DAIFMT_CBM_CFM:
  557. master = 1;
  558. break;
  559. case SND_SOC_DAIFMT_CBS_CFS:
  560. master = 0;
  561. break;
  562. default:
  563. dev_err(dai->dev, "Unknown master/slave configuration\n");
  564. return -EINVAL;
  565. }
  566. snd_soc_update_bits(codec, WM8985_CLOCK_GEN_CONTROL,
  567. WM8985_MS_MASK, master << WM8985_MS_SHIFT);
  568. /* frame inversion is not valid for dsp modes */
  569. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  570. case SND_SOC_DAIFMT_DSP_A:
  571. case SND_SOC_DAIFMT_DSP_B:
  572. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  573. case SND_SOC_DAIFMT_IB_IF:
  574. case SND_SOC_DAIFMT_NB_IF:
  575. return -EINVAL;
  576. default:
  577. break;
  578. }
  579. break;
  580. default:
  581. break;
  582. }
  583. bcp = lrp = 0;
  584. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  585. case SND_SOC_DAIFMT_NB_NF:
  586. break;
  587. case SND_SOC_DAIFMT_IB_IF:
  588. bcp = lrp = 1;
  589. break;
  590. case SND_SOC_DAIFMT_IB_NF:
  591. bcp = 1;
  592. break;
  593. case SND_SOC_DAIFMT_NB_IF:
  594. lrp = 1;
  595. break;
  596. default:
  597. dev_err(dai->dev, "Unknown polarity configuration\n");
  598. return -EINVAL;
  599. }
  600. snd_soc_update_bits(codec, WM8985_AUDIO_INTERFACE,
  601. WM8985_LRP_MASK, lrp << WM8985_LRP_SHIFT);
  602. snd_soc_update_bits(codec, WM8985_AUDIO_INTERFACE,
  603. WM8985_BCP_MASK, bcp << WM8985_BCP_SHIFT);
  604. return 0;
  605. }
  606. static int wm8985_hw_params(struct snd_pcm_substream *substream,
  607. struct snd_pcm_hw_params *params,
  608. struct snd_soc_dai *dai)
  609. {
  610. int i;
  611. struct snd_soc_codec *codec;
  612. struct wm8985_priv *wm8985;
  613. u16 blen, srate_idx;
  614. unsigned int tmp;
  615. int srate_best;
  616. codec = dai->codec;
  617. wm8985 = snd_soc_codec_get_drvdata(codec);
  618. wm8985->bclk = snd_soc_params_to_bclk(params);
  619. if ((int)wm8985->bclk < 0)
  620. return wm8985->bclk;
  621. switch (params_format(params)) {
  622. case SNDRV_PCM_FORMAT_S16_LE:
  623. blen = 0x0;
  624. break;
  625. case SNDRV_PCM_FORMAT_S20_3LE:
  626. blen = 0x1;
  627. break;
  628. case SNDRV_PCM_FORMAT_S24_LE:
  629. blen = 0x2;
  630. break;
  631. case SNDRV_PCM_FORMAT_S32_LE:
  632. blen = 0x3;
  633. break;
  634. default:
  635. dev_err(dai->dev, "Unsupported word length %u\n",
  636. params_format(params));
  637. return -EINVAL;
  638. }
  639. snd_soc_update_bits(codec, WM8985_AUDIO_INTERFACE,
  640. WM8985_WL_MASK, blen << WM8985_WL_SHIFT);
  641. /*
  642. * match to the nearest possible sample rate and rely
  643. * on the array index to configure the SR register
  644. */
  645. srate_idx = 0;
  646. srate_best = abs(srates[0] - params_rate(params));
  647. for (i = 1; i < ARRAY_SIZE(srates); ++i) {
  648. if (abs(srates[i] - params_rate(params)) >= srate_best)
  649. continue;
  650. srate_idx = i;
  651. srate_best = abs(srates[i] - params_rate(params));
  652. }
  653. dev_dbg(dai->dev, "Selected SRATE = %d\n", srates[srate_idx]);
  654. snd_soc_update_bits(codec, WM8985_ADDITIONAL_CONTROL,
  655. WM8985_SR_MASK, srate_idx << WM8985_SR_SHIFT);
  656. dev_dbg(dai->dev, "Target BCLK = %uHz\n", wm8985->bclk);
  657. dev_dbg(dai->dev, "SYSCLK = %uHz\n", wm8985->sysclk);
  658. for (i = 0; i < ARRAY_SIZE(fs_ratios); ++i) {
  659. if (wm8985->sysclk / params_rate(params)
  660. == fs_ratios[i].ratio)
  661. break;
  662. }
  663. if (i == ARRAY_SIZE(fs_ratios)) {
  664. dev_err(dai->dev, "Unable to configure MCLK ratio %u/%u\n",
  665. wm8985->sysclk, params_rate(params));
  666. return -EINVAL;
  667. }
  668. dev_dbg(dai->dev, "MCLK ratio = %dfs\n", fs_ratios[i].ratio);
  669. snd_soc_update_bits(codec, WM8985_CLOCK_GEN_CONTROL,
  670. WM8985_MCLKDIV_MASK, i << WM8985_MCLKDIV_SHIFT);
  671. /* select the appropriate bclk divider */
  672. tmp = (wm8985->sysclk / fs_ratios[i].div) * 10;
  673. for (i = 0; i < ARRAY_SIZE(bclk_divs); ++i) {
  674. if (wm8985->bclk == tmp / bclk_divs[i])
  675. break;
  676. }
  677. if (i == ARRAY_SIZE(bclk_divs)) {
  678. dev_err(dai->dev, "No matching BCLK divider found\n");
  679. return -EINVAL;
  680. }
  681. dev_dbg(dai->dev, "BCLK div = %d\n", i);
  682. snd_soc_update_bits(codec, WM8985_CLOCK_GEN_CONTROL,
  683. WM8985_BCLKDIV_MASK, i << WM8985_BCLKDIV_SHIFT);
  684. return 0;
  685. }
  686. struct pll_div {
  687. u32 div2:1;
  688. u32 n:4;
  689. u32 k:24;
  690. };
  691. #define FIXED_PLL_SIZE ((1ULL << 24) * 10)
  692. static int pll_factors(struct pll_div *pll_div, unsigned int target,
  693. unsigned int source)
  694. {
  695. u64 Kpart;
  696. unsigned long int K, Ndiv, Nmod;
  697. pll_div->div2 = 0;
  698. Ndiv = target / source;
  699. if (Ndiv < 6) {
  700. source >>= 1;
  701. pll_div->div2 = 1;
  702. Ndiv = target / source;
  703. }
  704. if (Ndiv < 6 || Ndiv > 12) {
  705. printk(KERN_ERR "%s: WM8985 N value is not within"
  706. " the recommended range: %lu\n", __func__, Ndiv);
  707. return -EINVAL;
  708. }
  709. pll_div->n = Ndiv;
  710. Nmod = target % source;
  711. Kpart = FIXED_PLL_SIZE * (u64)Nmod;
  712. do_div(Kpart, source);
  713. K = Kpart & 0xffffffff;
  714. if ((K % 10) >= 5)
  715. K += 5;
  716. K /= 10;
  717. pll_div->k = K;
  718. return 0;
  719. }
  720. static int wm8985_set_pll(struct snd_soc_dai *dai, int pll_id,
  721. int source, unsigned int freq_in,
  722. unsigned int freq_out)
  723. {
  724. int ret;
  725. struct snd_soc_codec *codec;
  726. struct pll_div pll_div;
  727. codec = dai->codec;
  728. if (freq_in && freq_out) {
  729. ret = pll_factors(&pll_div, freq_out * 4 * 2, freq_in);
  730. if (ret)
  731. return ret;
  732. }
  733. /* disable the PLL before reprogramming it */
  734. snd_soc_update_bits(codec, WM8985_POWER_MANAGEMENT_1,
  735. WM8985_PLLEN_MASK, 0);
  736. if (!freq_in || !freq_out)
  737. return 0;
  738. /* set PLLN and PRESCALE */
  739. snd_soc_write(codec, WM8985_PLL_N,
  740. (pll_div.div2 << WM8985_PLL_PRESCALE_SHIFT)
  741. | pll_div.n);
  742. /* set PLLK */
  743. snd_soc_write(codec, WM8985_PLL_K_3, pll_div.k & 0x1ff);
  744. snd_soc_write(codec, WM8985_PLL_K_2, (pll_div.k >> 9) & 0x1ff);
  745. snd_soc_write(codec, WM8985_PLL_K_1, (pll_div.k >> 18));
  746. /* set the source of the clock to be the PLL */
  747. snd_soc_update_bits(codec, WM8985_CLOCK_GEN_CONTROL,
  748. WM8985_CLKSEL_MASK, WM8985_CLKSEL);
  749. /* enable the PLL */
  750. snd_soc_update_bits(codec, WM8985_POWER_MANAGEMENT_1,
  751. WM8985_PLLEN_MASK, WM8985_PLLEN);
  752. return 0;
  753. }
  754. static int wm8985_set_sysclk(struct snd_soc_dai *dai,
  755. int clk_id, unsigned int freq, int dir)
  756. {
  757. struct snd_soc_codec *codec;
  758. struct wm8985_priv *wm8985;
  759. codec = dai->codec;
  760. wm8985 = snd_soc_codec_get_drvdata(codec);
  761. switch (clk_id) {
  762. case WM8985_CLKSRC_MCLK:
  763. snd_soc_update_bits(codec, WM8985_CLOCK_GEN_CONTROL,
  764. WM8985_CLKSEL_MASK, 0);
  765. snd_soc_update_bits(codec, WM8985_POWER_MANAGEMENT_1,
  766. WM8985_PLLEN_MASK, 0);
  767. break;
  768. case WM8985_CLKSRC_PLL:
  769. snd_soc_update_bits(codec, WM8985_CLOCK_GEN_CONTROL,
  770. WM8985_CLKSEL_MASK, WM8985_CLKSEL);
  771. break;
  772. default:
  773. dev_err(dai->dev, "Unknown clock source %d\n", clk_id);
  774. return -EINVAL;
  775. }
  776. wm8985->sysclk = freq;
  777. return 0;
  778. }
  779. static int wm8985_set_bias_level(struct snd_soc_codec *codec,
  780. enum snd_soc_bias_level level)
  781. {
  782. int ret;
  783. struct wm8985_priv *wm8985;
  784. wm8985 = snd_soc_codec_get_drvdata(codec);
  785. switch (level) {
  786. case SND_SOC_BIAS_ON:
  787. case SND_SOC_BIAS_PREPARE:
  788. /* VMID at 75k */
  789. snd_soc_update_bits(codec, WM8985_POWER_MANAGEMENT_1,
  790. WM8985_VMIDSEL_MASK,
  791. 1 << WM8985_VMIDSEL_SHIFT);
  792. break;
  793. case SND_SOC_BIAS_STANDBY:
  794. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  795. ret = regulator_bulk_enable(ARRAY_SIZE(wm8985->supplies),
  796. wm8985->supplies);
  797. if (ret) {
  798. dev_err(codec->dev,
  799. "Failed to enable supplies: %d\n",
  800. ret);
  801. return ret;
  802. }
  803. regcache_sync(wm8985->regmap);
  804. /* enable anti-pop features */
  805. snd_soc_update_bits(codec, WM8985_OUT4_TO_ADC,
  806. WM8985_POBCTRL_MASK,
  807. WM8985_POBCTRL);
  808. /* enable thermal shutdown */
  809. snd_soc_update_bits(codec, WM8985_OUTPUT_CTRL0,
  810. WM8985_TSDEN_MASK, WM8985_TSDEN);
  811. snd_soc_update_bits(codec, WM8985_OUTPUT_CTRL0,
  812. WM8985_TSOPCTRL_MASK,
  813. WM8985_TSOPCTRL);
  814. /* enable BIASEN */
  815. snd_soc_update_bits(codec, WM8985_POWER_MANAGEMENT_1,
  816. WM8985_BIASEN_MASK, WM8985_BIASEN);
  817. /* VMID at 75k */
  818. snd_soc_update_bits(codec, WM8985_POWER_MANAGEMENT_1,
  819. WM8985_VMIDSEL_MASK,
  820. 1 << WM8985_VMIDSEL_SHIFT);
  821. msleep(500);
  822. /* disable anti-pop features */
  823. snd_soc_update_bits(codec, WM8985_OUT4_TO_ADC,
  824. WM8985_POBCTRL_MASK, 0);
  825. }
  826. /* VMID at 300k */
  827. snd_soc_update_bits(codec, WM8985_POWER_MANAGEMENT_1,
  828. WM8985_VMIDSEL_MASK,
  829. 2 << WM8985_VMIDSEL_SHIFT);
  830. break;
  831. case SND_SOC_BIAS_OFF:
  832. /* disable thermal shutdown */
  833. snd_soc_update_bits(codec, WM8985_OUTPUT_CTRL0,
  834. WM8985_TSOPCTRL_MASK, 0);
  835. snd_soc_update_bits(codec, WM8985_OUTPUT_CTRL0,
  836. WM8985_TSDEN_MASK, 0);
  837. /* disable VMIDSEL and BIASEN */
  838. snd_soc_update_bits(codec, WM8985_POWER_MANAGEMENT_1,
  839. WM8985_VMIDSEL_MASK | WM8985_BIASEN_MASK,
  840. 0);
  841. snd_soc_write(codec, WM8985_POWER_MANAGEMENT_1, 0);
  842. snd_soc_write(codec, WM8985_POWER_MANAGEMENT_2, 0);
  843. snd_soc_write(codec, WM8985_POWER_MANAGEMENT_3, 0);
  844. regcache_mark_dirty(wm8985->regmap);
  845. regulator_bulk_disable(ARRAY_SIZE(wm8985->supplies),
  846. wm8985->supplies);
  847. break;
  848. }
  849. codec->dapm.bias_level = level;
  850. return 0;
  851. }
  852. #ifdef CONFIG_PM
  853. static int wm8985_suspend(struct snd_soc_codec *codec)
  854. {
  855. wm8985_set_bias_level(codec, SND_SOC_BIAS_OFF);
  856. return 0;
  857. }
  858. static int wm8985_resume(struct snd_soc_codec *codec)
  859. {
  860. wm8985_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  861. return 0;
  862. }
  863. #else
  864. #define wm8985_suspend NULL
  865. #define wm8985_resume NULL
  866. #endif
  867. static int wm8985_remove(struct snd_soc_codec *codec)
  868. {
  869. struct wm8985_priv *wm8985;
  870. wm8985 = snd_soc_codec_get_drvdata(codec);
  871. wm8985_set_bias_level(codec, SND_SOC_BIAS_OFF);
  872. regulator_bulk_free(ARRAY_SIZE(wm8985->supplies), wm8985->supplies);
  873. return 0;
  874. }
  875. static int wm8985_probe(struct snd_soc_codec *codec)
  876. {
  877. size_t i;
  878. struct wm8985_priv *wm8985;
  879. int ret;
  880. wm8985 = snd_soc_codec_get_drvdata(codec);
  881. codec->control_data = wm8985->regmap;
  882. ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_REGMAP);
  883. if (ret < 0) {
  884. dev_err(codec->dev, "Failed to set cache i/o: %d\n", ret);
  885. return ret;
  886. }
  887. for (i = 0; i < ARRAY_SIZE(wm8985->supplies); i++)
  888. wm8985->supplies[i].supply = wm8985_supply_names[i];
  889. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8985->supplies),
  890. wm8985->supplies);
  891. if (ret) {
  892. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  893. return ret;
  894. }
  895. ret = regulator_bulk_enable(ARRAY_SIZE(wm8985->supplies),
  896. wm8985->supplies);
  897. if (ret) {
  898. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  899. goto err_reg_get;
  900. }
  901. ret = wm8985_reset(codec);
  902. if (ret < 0) {
  903. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  904. goto err_reg_enable;
  905. }
  906. /* latch volume update bits */
  907. for (i = 0; i < ARRAY_SIZE(volume_update_regs); ++i)
  908. snd_soc_update_bits(codec, volume_update_regs[i],
  909. 0x100, 0x100);
  910. /* enable BIASCUT */
  911. snd_soc_update_bits(codec, WM8985_BIAS_CTRL, WM8985_BIASCUT,
  912. WM8985_BIASCUT);
  913. wm8985_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  914. return 0;
  915. err_reg_enable:
  916. regulator_bulk_disable(ARRAY_SIZE(wm8985->supplies), wm8985->supplies);
  917. err_reg_get:
  918. regulator_bulk_free(ARRAY_SIZE(wm8985->supplies), wm8985->supplies);
  919. return ret;
  920. }
  921. static const struct snd_soc_dai_ops wm8985_dai_ops = {
  922. .digital_mute = wm8985_dac_mute,
  923. .hw_params = wm8985_hw_params,
  924. .set_fmt = wm8985_set_fmt,
  925. .set_sysclk = wm8985_set_sysclk,
  926. .set_pll = wm8985_set_pll
  927. };
  928. #define WM8985_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  929. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  930. static struct snd_soc_dai_driver wm8985_dai = {
  931. .name = "wm8985-hifi",
  932. .playback = {
  933. .stream_name = "Playback",
  934. .channels_min = 2,
  935. .channels_max = 2,
  936. .rates = SNDRV_PCM_RATE_8000_48000,
  937. .formats = WM8985_FORMATS,
  938. },
  939. .capture = {
  940. .stream_name = "Capture",
  941. .channels_min = 2,
  942. .channels_max = 2,
  943. .rates = SNDRV_PCM_RATE_8000_48000,
  944. .formats = WM8985_FORMATS,
  945. },
  946. .ops = &wm8985_dai_ops,
  947. .symmetric_rates = 1
  948. };
  949. static struct snd_soc_codec_driver soc_codec_dev_wm8985 = {
  950. .probe = wm8985_probe,
  951. .remove = wm8985_remove,
  952. .suspend = wm8985_suspend,
  953. .resume = wm8985_resume,
  954. .set_bias_level = wm8985_set_bias_level,
  955. .controls = wm8985_snd_controls,
  956. .num_controls = ARRAY_SIZE(wm8985_snd_controls),
  957. .dapm_widgets = wm8985_dapm_widgets,
  958. .num_dapm_widgets = ARRAY_SIZE(wm8985_dapm_widgets),
  959. .dapm_routes = wm8985_dapm_routes,
  960. .num_dapm_routes = ARRAY_SIZE(wm8985_dapm_routes),
  961. };
  962. static const struct regmap_config wm8985_regmap = {
  963. .reg_bits = 7,
  964. .val_bits = 9,
  965. .max_register = WM8985_MAX_REGISTER,
  966. .writeable_reg = wm8985_writeable,
  967. .cache_type = REGCACHE_RBTREE,
  968. .reg_defaults = wm8985_reg_defaults,
  969. .num_reg_defaults = ARRAY_SIZE(wm8985_reg_defaults),
  970. };
  971. #if defined(CONFIG_SPI_MASTER)
  972. static int __devinit wm8985_spi_probe(struct spi_device *spi)
  973. {
  974. struct wm8985_priv *wm8985;
  975. int ret;
  976. wm8985 = devm_kzalloc(&spi->dev, sizeof *wm8985, GFP_KERNEL);
  977. if (!wm8985)
  978. return -ENOMEM;
  979. spi_set_drvdata(spi, wm8985);
  980. wm8985->regmap = regmap_init_spi(spi, &wm8985_regmap);
  981. if (IS_ERR(wm8985->regmap)) {
  982. ret = PTR_ERR(wm8985->regmap);
  983. dev_err(&spi->dev, "Failed to allocate register map: %d\n",
  984. ret);
  985. goto err;
  986. }
  987. ret = snd_soc_register_codec(&spi->dev,
  988. &soc_codec_dev_wm8985, &wm8985_dai, 1);
  989. if (ret != 0)
  990. goto err;
  991. return 0;
  992. err:
  993. regmap_exit(wm8985->regmap);
  994. return ret;
  995. }
  996. static int __devexit wm8985_spi_remove(struct spi_device *spi)
  997. {
  998. struct wm8985_priv *wm8985 = spi_get_drvdata(spi);
  999. snd_soc_unregister_codec(&spi->dev);
  1000. regmap_exit(wm8985->regmap);
  1001. return 0;
  1002. }
  1003. static struct spi_driver wm8985_spi_driver = {
  1004. .driver = {
  1005. .name = "wm8985",
  1006. .owner = THIS_MODULE,
  1007. },
  1008. .probe = wm8985_spi_probe,
  1009. .remove = __devexit_p(wm8985_spi_remove)
  1010. };
  1011. #endif
  1012. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1013. static __devinit int wm8985_i2c_probe(struct i2c_client *i2c,
  1014. const struct i2c_device_id *id)
  1015. {
  1016. struct wm8985_priv *wm8985;
  1017. int ret;
  1018. wm8985 = devm_kzalloc(&i2c->dev, sizeof *wm8985, GFP_KERNEL);
  1019. if (!wm8985)
  1020. return -ENOMEM;
  1021. i2c_set_clientdata(i2c, wm8985);
  1022. wm8985->regmap = regmap_init_i2c(i2c, &wm8985_regmap);
  1023. if (IS_ERR(wm8985->regmap)) {
  1024. ret = PTR_ERR(wm8985->regmap);
  1025. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  1026. ret);
  1027. goto err;
  1028. }
  1029. ret = snd_soc_register_codec(&i2c->dev,
  1030. &soc_codec_dev_wm8985, &wm8985_dai, 1);
  1031. if (ret != 0)
  1032. goto err;
  1033. return 0;
  1034. err:
  1035. regmap_exit(wm8985->regmap);
  1036. return ret;
  1037. }
  1038. static __devexit int wm8985_i2c_remove(struct i2c_client *i2c)
  1039. {
  1040. struct wm8985_priv *wm8985 = i2c_get_clientdata(i2c);
  1041. snd_soc_unregister_codec(&i2c->dev);
  1042. regmap_exit(wm8985->regmap);
  1043. return 0;
  1044. }
  1045. static const struct i2c_device_id wm8985_i2c_id[] = {
  1046. { "wm8985", 0 },
  1047. { }
  1048. };
  1049. MODULE_DEVICE_TABLE(i2c, wm8985_i2c_id);
  1050. static struct i2c_driver wm8985_i2c_driver = {
  1051. .driver = {
  1052. .name = "wm8985",
  1053. .owner = THIS_MODULE,
  1054. },
  1055. .probe = wm8985_i2c_probe,
  1056. .remove = __devexit_p(wm8985_i2c_remove),
  1057. .id_table = wm8985_i2c_id
  1058. };
  1059. #endif
  1060. static int __init wm8985_modinit(void)
  1061. {
  1062. int ret = 0;
  1063. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1064. ret = i2c_add_driver(&wm8985_i2c_driver);
  1065. if (ret) {
  1066. printk(KERN_ERR "Failed to register wm8985 I2C driver: %d\n",
  1067. ret);
  1068. }
  1069. #endif
  1070. #if defined(CONFIG_SPI_MASTER)
  1071. ret = spi_register_driver(&wm8985_spi_driver);
  1072. if (ret != 0) {
  1073. printk(KERN_ERR "Failed to register wm8985 SPI driver: %d\n",
  1074. ret);
  1075. }
  1076. #endif
  1077. return ret;
  1078. }
  1079. module_init(wm8985_modinit);
  1080. static void __exit wm8985_exit(void)
  1081. {
  1082. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1083. i2c_del_driver(&wm8985_i2c_driver);
  1084. #endif
  1085. #if defined(CONFIG_SPI_MASTER)
  1086. spi_unregister_driver(&wm8985_spi_driver);
  1087. #endif
  1088. }
  1089. module_exit(wm8985_exit);
  1090. MODULE_DESCRIPTION("ASoC WM8985 driver");
  1091. MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
  1092. MODULE_LICENSE("GPL");