wm8985.c 37 KB

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