wm8955.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * wm8955.c -- WM8955 ALSA SoC Audio driver
  3. *
  4. * Copyright 2009 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm.h>
  17. #include <linux/i2c.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/slab.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/initval.h>
  26. #include <sound/tlv.h>
  27. #include <sound/wm8955.h>
  28. #include "wm8955.h"
  29. #define WM8955_NUM_SUPPLIES 4
  30. static const char *wm8955_supply_names[WM8955_NUM_SUPPLIES] = {
  31. "DCVDD",
  32. "DBVDD",
  33. "HPVDD",
  34. "AVDD",
  35. };
  36. /* codec private data */
  37. struct wm8955_priv {
  38. struct regmap *regmap;
  39. unsigned int mclk_rate;
  40. int deemph;
  41. int fs;
  42. struct regulator_bulk_data supplies[WM8955_NUM_SUPPLIES];
  43. };
  44. static const struct reg_default wm8955_reg_defaults[] = {
  45. { 2, 0x0079 }, /* R2 - LOUT1 volume */
  46. { 3, 0x0079 }, /* R3 - ROUT1 volume */
  47. { 5, 0x0008 }, /* R5 - DAC Control */
  48. { 7, 0x000A }, /* R7 - Audio Interface */
  49. { 8, 0x0000 }, /* R8 - Sample Rate */
  50. { 10, 0x00FF }, /* R10 - Left DAC volume */
  51. { 11, 0x00FF }, /* R11 - Right DAC volume */
  52. { 12, 0x000F }, /* R12 - Bass control */
  53. { 13, 0x000F }, /* R13 - Treble control */
  54. { 23, 0x00C1 }, /* R23 - Additional control (1) */
  55. { 24, 0x0000 }, /* R24 - Additional control (2) */
  56. { 25, 0x0000 }, /* R25 - Power Management (1) */
  57. { 26, 0x0000 }, /* R26 - Power Management (2) */
  58. { 27, 0x0000 }, /* R27 - Additional Control (3) */
  59. { 34, 0x0050 }, /* R34 - Left out Mix (1) */
  60. { 35, 0x0050 }, /* R35 - Left out Mix (2) */
  61. { 36, 0x0050 }, /* R36 - Right out Mix (1) */
  62. { 37, 0x0050 }, /* R37 - Right Out Mix (2) */
  63. { 38, 0x0050 }, /* R38 - Mono out Mix (1) */
  64. { 39, 0x0050 }, /* R39 - Mono out Mix (2) */
  65. { 40, 0x0079 }, /* R40 - LOUT2 volume */
  66. { 41, 0x0079 }, /* R41 - ROUT2 volume */
  67. { 42, 0x0079 }, /* R42 - MONOOUT volume */
  68. { 43, 0x0000 }, /* R43 - Clocking / PLL */
  69. { 44, 0x0103 }, /* R44 - PLL Control 1 */
  70. { 45, 0x0024 }, /* R45 - PLL Control 2 */
  71. { 46, 0x01BA }, /* R46 - PLL Control 3 */
  72. { 59, 0x0000 }, /* R59 - PLL Control 4 */
  73. };
  74. static bool wm8955_writeable(struct device *dev, unsigned int reg)
  75. {
  76. switch (reg) {
  77. case WM8955_LOUT1_VOLUME:
  78. case WM8955_ROUT1_VOLUME:
  79. case WM8955_DAC_CONTROL:
  80. case WM8955_AUDIO_INTERFACE:
  81. case WM8955_SAMPLE_RATE:
  82. case WM8955_LEFT_DAC_VOLUME:
  83. case WM8955_RIGHT_DAC_VOLUME:
  84. case WM8955_BASS_CONTROL:
  85. case WM8955_TREBLE_CONTROL:
  86. case WM8955_RESET:
  87. case WM8955_ADDITIONAL_CONTROL_1:
  88. case WM8955_ADDITIONAL_CONTROL_2:
  89. case WM8955_POWER_MANAGEMENT_1:
  90. case WM8955_POWER_MANAGEMENT_2:
  91. case WM8955_ADDITIONAL_CONTROL_3:
  92. case WM8955_LEFT_OUT_MIX_1:
  93. case WM8955_LEFT_OUT_MIX_2:
  94. case WM8955_RIGHT_OUT_MIX_1:
  95. case WM8955_RIGHT_OUT_MIX_2:
  96. case WM8955_MONO_OUT_MIX_1:
  97. case WM8955_MONO_OUT_MIX_2:
  98. case WM8955_LOUT2_VOLUME:
  99. case WM8955_ROUT2_VOLUME:
  100. case WM8955_MONOOUT_VOLUME:
  101. case WM8955_CLOCKING_PLL:
  102. case WM8955_PLL_CONTROL_1:
  103. case WM8955_PLL_CONTROL_2:
  104. case WM8955_PLL_CONTROL_3:
  105. case WM8955_PLL_CONTROL_4:
  106. return true;
  107. default:
  108. return false;
  109. }
  110. }
  111. static bool wm8955_volatile(struct device *dev, unsigned int reg)
  112. {
  113. switch (reg) {
  114. case WM8955_RESET:
  115. return true;
  116. default:
  117. return false;
  118. }
  119. }
  120. static int wm8955_reset(struct snd_soc_codec *codec)
  121. {
  122. return snd_soc_write(codec, WM8955_RESET, 0);
  123. }
  124. struct pll_factors {
  125. int n;
  126. int k;
  127. int outdiv;
  128. };
  129. /* The size in bits of the FLL divide multiplied by 10
  130. * to allow rounding later */
  131. #define FIXED_FLL_SIZE ((1 << 22) * 10)
  132. static int wm8995_pll_factors(struct device *dev,
  133. int Fref, int Fout, struct pll_factors *pll)
  134. {
  135. u64 Kpart;
  136. unsigned int K, Ndiv, Nmod, target;
  137. dev_dbg(dev, "Fref=%u Fout=%u\n", Fref, Fout);
  138. /* The oscilator should run at should be 90-100MHz, and
  139. * there's a divide by 4 plus an optional divide by 2 in the
  140. * output path to generate the system clock. The clock table
  141. * is sortd so we should always generate a suitable target. */
  142. target = Fout * 4;
  143. if (target < 90000000) {
  144. pll->outdiv = 1;
  145. target *= 2;
  146. } else {
  147. pll->outdiv = 0;
  148. }
  149. WARN_ON(target < 90000000 || target > 100000000);
  150. dev_dbg(dev, "Fvco=%dHz\n", target);
  151. /* Now, calculate N.K */
  152. Ndiv = target / Fref;
  153. pll->n = Ndiv;
  154. Nmod = target % Fref;
  155. dev_dbg(dev, "Nmod=%d\n", Nmod);
  156. /* Calculate fractional part - scale up so we can round. */
  157. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  158. do_div(Kpart, Fref);
  159. K = Kpart & 0xFFFFFFFF;
  160. if ((K % 10) >= 5)
  161. K += 5;
  162. /* Move down to proper range now rounding is done */
  163. pll->k = K / 10;
  164. dev_dbg(dev, "N=%x K=%x OUTDIV=%x\n", pll->n, pll->k, pll->outdiv);
  165. return 0;
  166. }
  167. /* Lookup table specifying SRATE (table 25 in datasheet); some of the
  168. * output frequencies have been rounded to the standard frequencies
  169. * they are intended to match where the error is slight. */
  170. static struct {
  171. int mclk;
  172. int fs;
  173. int usb;
  174. int sr;
  175. } clock_cfgs[] = {
  176. { 18432000, 8000, 0, 3, },
  177. { 18432000, 12000, 0, 9, },
  178. { 18432000, 16000, 0, 11, },
  179. { 18432000, 24000, 0, 29, },
  180. { 18432000, 32000, 0, 13, },
  181. { 18432000, 48000, 0, 1, },
  182. { 18432000, 96000, 0, 15, },
  183. { 16934400, 8018, 0, 19, },
  184. { 16934400, 11025, 0, 25, },
  185. { 16934400, 22050, 0, 27, },
  186. { 16934400, 44100, 0, 17, },
  187. { 16934400, 88200, 0, 31, },
  188. { 12000000, 8000, 1, 2, },
  189. { 12000000, 11025, 1, 25, },
  190. { 12000000, 12000, 1, 8, },
  191. { 12000000, 16000, 1, 10, },
  192. { 12000000, 22050, 1, 27, },
  193. { 12000000, 24000, 1, 28, },
  194. { 12000000, 32000, 1, 12, },
  195. { 12000000, 44100, 1, 17, },
  196. { 12000000, 48000, 1, 0, },
  197. { 12000000, 88200, 1, 31, },
  198. { 12000000, 96000, 1, 14, },
  199. { 12288000, 8000, 0, 2, },
  200. { 12288000, 12000, 0, 8, },
  201. { 12288000, 16000, 0, 10, },
  202. { 12288000, 24000, 0, 28, },
  203. { 12288000, 32000, 0, 12, },
  204. { 12288000, 48000, 0, 0, },
  205. { 12288000, 96000, 0, 14, },
  206. { 12289600, 8018, 0, 18, },
  207. { 12289600, 11025, 0, 24, },
  208. { 12289600, 22050, 0, 26, },
  209. { 11289600, 44100, 0, 16, },
  210. { 11289600, 88200, 0, 31, },
  211. };
  212. static int wm8955_configure_clocking(struct snd_soc_codec *codec)
  213. {
  214. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  215. int i, ret, val;
  216. int clocking = 0;
  217. int srate = 0;
  218. int sr = -1;
  219. struct pll_factors pll;
  220. /* If we're not running a sample rate currently just pick one */
  221. if (wm8955->fs == 0)
  222. wm8955->fs = 8000;
  223. /* Can we generate an exact output? */
  224. for (i = 0; i < ARRAY_SIZE(clock_cfgs); i++) {
  225. if (wm8955->fs != clock_cfgs[i].fs)
  226. continue;
  227. sr = i;
  228. if (wm8955->mclk_rate == clock_cfgs[i].mclk)
  229. break;
  230. }
  231. /* We should never get here with an unsupported sample rate */
  232. if (sr == -1) {
  233. dev_err(codec->dev, "Sample rate %dHz unsupported\n",
  234. wm8955->fs);
  235. WARN_ON(sr == -1);
  236. return -EINVAL;
  237. }
  238. if (i == ARRAY_SIZE(clock_cfgs)) {
  239. /* If we can't generate the right clock from MCLK then
  240. * we should configure the PLL to supply us with an
  241. * appropriate clock.
  242. */
  243. clocking |= WM8955_MCLKSEL;
  244. /* Use the last divider configuration we saw for the
  245. * sample rate. */
  246. ret = wm8995_pll_factors(codec->dev, wm8955->mclk_rate,
  247. clock_cfgs[sr].mclk, &pll);
  248. if (ret != 0) {
  249. dev_err(codec->dev,
  250. "Unable to generate %dHz from %dHz MCLK\n",
  251. wm8955->fs, wm8955->mclk_rate);
  252. return -EINVAL;
  253. }
  254. snd_soc_update_bits(codec, WM8955_PLL_CONTROL_1,
  255. WM8955_N_MASK | WM8955_K_21_18_MASK,
  256. (pll.n << WM8955_N_SHIFT) |
  257. pll.k >> 18);
  258. snd_soc_update_bits(codec, WM8955_PLL_CONTROL_2,
  259. WM8955_K_17_9_MASK,
  260. (pll.k >> 9) & WM8955_K_17_9_MASK);
  261. snd_soc_update_bits(codec, WM8955_PLL_CONTROL_3,
  262. WM8955_K_8_0_MASK,
  263. pll.k & WM8955_K_8_0_MASK);
  264. if (pll.k)
  265. snd_soc_update_bits(codec, WM8955_PLL_CONTROL_4,
  266. WM8955_KEN, WM8955_KEN);
  267. else
  268. snd_soc_update_bits(codec, WM8955_PLL_CONTROL_4,
  269. WM8955_KEN, 0);
  270. if (pll.outdiv)
  271. val = WM8955_PLL_RB | WM8955_PLLOUTDIV2;
  272. else
  273. val = WM8955_PLL_RB;
  274. /* Now start the PLL running */
  275. snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
  276. WM8955_PLL_RB | WM8955_PLLOUTDIV2, val);
  277. snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
  278. WM8955_PLLEN, WM8955_PLLEN);
  279. }
  280. srate = clock_cfgs[sr].usb | (clock_cfgs[sr].sr << WM8955_SR_SHIFT);
  281. snd_soc_update_bits(codec, WM8955_SAMPLE_RATE,
  282. WM8955_USB | WM8955_SR_MASK, srate);
  283. snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
  284. WM8955_MCLKSEL, clocking);
  285. return 0;
  286. }
  287. static int wm8955_sysclk(struct snd_soc_dapm_widget *w,
  288. struct snd_kcontrol *kcontrol, int event)
  289. {
  290. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  291. int ret = 0;
  292. /* Always disable the clocks - if we're doing reconfiguration this
  293. * avoids misclocking.
  294. */
  295. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  296. WM8955_DIGENB, 0);
  297. snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
  298. WM8955_PLL_RB | WM8955_PLLEN, 0);
  299. switch (event) {
  300. case SND_SOC_DAPM_POST_PMD:
  301. break;
  302. case SND_SOC_DAPM_PRE_PMU:
  303. ret = wm8955_configure_clocking(codec);
  304. break;
  305. default:
  306. ret = -EINVAL;
  307. break;
  308. }
  309. return ret;
  310. }
  311. static int deemph_settings[] = { 0, 32000, 44100, 48000 };
  312. static int wm8955_set_deemph(struct snd_soc_codec *codec)
  313. {
  314. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  315. int val, i, best;
  316. /* If we're using deemphasis select the nearest available sample
  317. * rate.
  318. */
  319. if (wm8955->deemph) {
  320. best = 1;
  321. for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
  322. if (abs(deemph_settings[i] - wm8955->fs) <
  323. abs(deemph_settings[best] - wm8955->fs))
  324. best = i;
  325. }
  326. val = best << WM8955_DEEMPH_SHIFT;
  327. } else {
  328. val = 0;
  329. }
  330. dev_dbg(codec->dev, "Set deemphasis %d\n", val);
  331. return snd_soc_update_bits(codec, WM8955_DAC_CONTROL,
  332. WM8955_DEEMPH_MASK, val);
  333. }
  334. static int wm8955_get_deemph(struct snd_kcontrol *kcontrol,
  335. struct snd_ctl_elem_value *ucontrol)
  336. {
  337. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  338. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  339. ucontrol->value.integer.value[0] = wm8955->deemph;
  340. return 0;
  341. }
  342. static int wm8955_put_deemph(struct snd_kcontrol *kcontrol,
  343. struct snd_ctl_elem_value *ucontrol)
  344. {
  345. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  346. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  347. unsigned int deemph = ucontrol->value.integer.value[0];
  348. if (deemph > 1)
  349. return -EINVAL;
  350. wm8955->deemph = deemph;
  351. return wm8955_set_deemph(codec);
  352. }
  353. static const char *bass_mode_text[] = {
  354. "Linear", "Adaptive",
  355. };
  356. static SOC_ENUM_SINGLE_DECL(bass_mode, WM8955_BASS_CONTROL, 7, bass_mode_text);
  357. static const char *bass_cutoff_text[] = {
  358. "Low", "High"
  359. };
  360. static SOC_ENUM_SINGLE_DECL(bass_cutoff, WM8955_BASS_CONTROL, 6,
  361. bass_cutoff_text);
  362. static const char *treble_cutoff_text[] = {
  363. "High", "Low"
  364. };
  365. static SOC_ENUM_SINGLE_DECL(treble_cutoff, WM8955_TREBLE_CONTROL, 2,
  366. treble_cutoff_text);
  367. static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1);
  368. static const DECLARE_TLV_DB_SCALE(atten_tlv, -600, 600, 0);
  369. static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
  370. static const DECLARE_TLV_DB_SCALE(mono_tlv, -2100, 300, 0);
  371. static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
  372. static const DECLARE_TLV_DB_SCALE(treble_tlv, -1200, 150, 1);
  373. static const struct snd_kcontrol_new wm8955_snd_controls[] = {
  374. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8955_LEFT_DAC_VOLUME,
  375. WM8955_RIGHT_DAC_VOLUME, 0, 255, 0, digital_tlv),
  376. SOC_SINGLE_TLV("Playback Attenuation Volume", WM8955_DAC_CONTROL, 7, 1, 1,
  377. atten_tlv),
  378. SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
  379. wm8955_get_deemph, wm8955_put_deemph),
  380. SOC_ENUM("Bass Mode", bass_mode),
  381. SOC_ENUM("Bass Cutoff", bass_cutoff),
  382. SOC_SINGLE("Bass Volume", WM8955_BASS_CONTROL, 0, 15, 1),
  383. SOC_ENUM("Treble Cutoff", treble_cutoff),
  384. SOC_SINGLE_TLV("Treble Volume", WM8955_TREBLE_CONTROL, 0, 14, 1, treble_tlv),
  385. SOC_SINGLE_TLV("Left Bypass Volume", WM8955_LEFT_OUT_MIX_1, 4, 7, 1,
  386. bypass_tlv),
  387. SOC_SINGLE_TLV("Left Mono Volume", WM8955_LEFT_OUT_MIX_2, 4, 7, 1,
  388. bypass_tlv),
  389. SOC_SINGLE_TLV("Right Mono Volume", WM8955_RIGHT_OUT_MIX_1, 4, 7, 1,
  390. bypass_tlv),
  391. SOC_SINGLE_TLV("Right Bypass Volume", WM8955_RIGHT_OUT_MIX_2, 4, 7, 1,
  392. bypass_tlv),
  393. /* Not a stereo pair so they line up with the DAPM switches */
  394. SOC_SINGLE_TLV("Mono Left Bypass Volume", WM8955_MONO_OUT_MIX_1, 4, 7, 1,
  395. mono_tlv),
  396. SOC_SINGLE_TLV("Mono Right Bypass Volume", WM8955_MONO_OUT_MIX_2, 4, 7, 1,
  397. mono_tlv),
  398. SOC_DOUBLE_R_TLV("Headphone Volume", WM8955_LOUT1_VOLUME,
  399. WM8955_ROUT1_VOLUME, 0, 127, 0, out_tlv),
  400. SOC_DOUBLE_R("Headphone ZC Switch", WM8955_LOUT1_VOLUME,
  401. WM8955_ROUT1_VOLUME, 7, 1, 0),
  402. SOC_DOUBLE_R_TLV("Speaker Volume", WM8955_LOUT2_VOLUME,
  403. WM8955_ROUT2_VOLUME, 0, 127, 0, out_tlv),
  404. SOC_DOUBLE_R("Speaker ZC Switch", WM8955_LOUT2_VOLUME,
  405. WM8955_ROUT2_VOLUME, 7, 1, 0),
  406. SOC_SINGLE_TLV("Mono Volume", WM8955_MONOOUT_VOLUME, 0, 127, 0, out_tlv),
  407. SOC_SINGLE("Mono ZC Switch", WM8955_MONOOUT_VOLUME, 7, 1, 0),
  408. };
  409. static const struct snd_kcontrol_new lmixer[] = {
  410. SOC_DAPM_SINGLE("Playback Switch", WM8955_LEFT_OUT_MIX_1, 8, 1, 0),
  411. SOC_DAPM_SINGLE("Bypass Switch", WM8955_LEFT_OUT_MIX_1, 7, 1, 0),
  412. SOC_DAPM_SINGLE("Right Playback Switch", WM8955_LEFT_OUT_MIX_2, 8, 1, 0),
  413. SOC_DAPM_SINGLE("Mono Switch", WM8955_LEFT_OUT_MIX_2, 7, 1, 0),
  414. };
  415. static const struct snd_kcontrol_new rmixer[] = {
  416. SOC_DAPM_SINGLE("Left Playback Switch", WM8955_RIGHT_OUT_MIX_1, 8, 1, 0),
  417. SOC_DAPM_SINGLE("Mono Switch", WM8955_RIGHT_OUT_MIX_1, 7, 1, 0),
  418. SOC_DAPM_SINGLE("Playback Switch", WM8955_RIGHT_OUT_MIX_2, 8, 1, 0),
  419. SOC_DAPM_SINGLE("Bypass Switch", WM8955_RIGHT_OUT_MIX_2, 7, 1, 0),
  420. };
  421. static const struct snd_kcontrol_new mmixer[] = {
  422. SOC_DAPM_SINGLE("Left Playback Switch", WM8955_MONO_OUT_MIX_1, 8, 1, 0),
  423. SOC_DAPM_SINGLE("Left Bypass Switch", WM8955_MONO_OUT_MIX_1, 7, 1, 0),
  424. SOC_DAPM_SINGLE("Right Playback Switch", WM8955_MONO_OUT_MIX_2, 8, 1, 0),
  425. SOC_DAPM_SINGLE("Right Bypass Switch", WM8955_MONO_OUT_MIX_2, 7, 1, 0),
  426. };
  427. static const struct snd_soc_dapm_widget wm8955_dapm_widgets[] = {
  428. SND_SOC_DAPM_INPUT("MONOIN-"),
  429. SND_SOC_DAPM_INPUT("MONOIN+"),
  430. SND_SOC_DAPM_INPUT("LINEINR"),
  431. SND_SOC_DAPM_INPUT("LINEINL"),
  432. SND_SOC_DAPM_PGA("Mono Input", SND_SOC_NOPM, 0, 0, NULL, 0),
  433. SND_SOC_DAPM_SUPPLY("SYSCLK", WM8955_POWER_MANAGEMENT_1, 0, 1, wm8955_sysclk,
  434. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  435. SND_SOC_DAPM_SUPPLY("TSDEN", WM8955_ADDITIONAL_CONTROL_1, 8, 0, NULL, 0),
  436. SND_SOC_DAPM_DAC("DACL", "Playback", WM8955_POWER_MANAGEMENT_2, 8, 0),
  437. SND_SOC_DAPM_DAC("DACR", "Playback", WM8955_POWER_MANAGEMENT_2, 7, 0),
  438. SND_SOC_DAPM_PGA("LOUT1 PGA", WM8955_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
  439. SND_SOC_DAPM_PGA("ROUT1 PGA", WM8955_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
  440. SND_SOC_DAPM_PGA("LOUT2 PGA", WM8955_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
  441. SND_SOC_DAPM_PGA("ROUT2 PGA", WM8955_POWER_MANAGEMENT_2, 3, 0, NULL, 0),
  442. SND_SOC_DAPM_PGA("MOUT PGA", WM8955_POWER_MANAGEMENT_2, 2, 0, NULL, 0),
  443. SND_SOC_DAPM_PGA("OUT3 PGA", WM8955_POWER_MANAGEMENT_2, 1, 0, NULL, 0),
  444. /* The names are chosen to make the control names nice */
  445. SND_SOC_DAPM_MIXER("Left", SND_SOC_NOPM, 0, 0,
  446. lmixer, ARRAY_SIZE(lmixer)),
  447. SND_SOC_DAPM_MIXER("Right", SND_SOC_NOPM, 0, 0,
  448. rmixer, ARRAY_SIZE(rmixer)),
  449. SND_SOC_DAPM_MIXER("Mono", SND_SOC_NOPM, 0, 0,
  450. mmixer, ARRAY_SIZE(mmixer)),
  451. SND_SOC_DAPM_OUTPUT("LOUT1"),
  452. SND_SOC_DAPM_OUTPUT("ROUT1"),
  453. SND_SOC_DAPM_OUTPUT("LOUT2"),
  454. SND_SOC_DAPM_OUTPUT("ROUT2"),
  455. SND_SOC_DAPM_OUTPUT("MONOOUT"),
  456. SND_SOC_DAPM_OUTPUT("OUT3"),
  457. };
  458. static const struct snd_soc_dapm_route wm8955_dapm_routes[] = {
  459. { "DACL", NULL, "SYSCLK" },
  460. { "DACR", NULL, "SYSCLK" },
  461. { "Mono Input", NULL, "MONOIN-" },
  462. { "Mono Input", NULL, "MONOIN+" },
  463. { "Left", "Playback Switch", "DACL" },
  464. { "Left", "Right Playback Switch", "DACR" },
  465. { "Left", "Bypass Switch", "LINEINL" },
  466. { "Left", "Mono Switch", "Mono Input" },
  467. { "Right", "Playback Switch", "DACR" },
  468. { "Right", "Left Playback Switch", "DACL" },
  469. { "Right", "Bypass Switch", "LINEINR" },
  470. { "Right", "Mono Switch", "Mono Input" },
  471. { "Mono", "Left Playback Switch", "DACL" },
  472. { "Mono", "Right Playback Switch", "DACR" },
  473. { "Mono", "Left Bypass Switch", "LINEINL" },
  474. { "Mono", "Right Bypass Switch", "LINEINR" },
  475. { "LOUT1 PGA", NULL, "Left" },
  476. { "LOUT1", NULL, "TSDEN" },
  477. { "LOUT1", NULL, "LOUT1 PGA" },
  478. { "ROUT1 PGA", NULL, "Right" },
  479. { "ROUT1", NULL, "TSDEN" },
  480. { "ROUT1", NULL, "ROUT1 PGA" },
  481. { "LOUT2 PGA", NULL, "Left" },
  482. { "LOUT2", NULL, "TSDEN" },
  483. { "LOUT2", NULL, "LOUT2 PGA" },
  484. { "ROUT2 PGA", NULL, "Right" },
  485. { "ROUT2", NULL, "TSDEN" },
  486. { "ROUT2", NULL, "ROUT2 PGA" },
  487. { "MOUT PGA", NULL, "Mono" },
  488. { "MONOOUT", NULL, "MOUT PGA" },
  489. /* OUT3 not currently implemented */
  490. { "OUT3", NULL, "OUT3 PGA" },
  491. };
  492. static int wm8955_hw_params(struct snd_pcm_substream *substream,
  493. struct snd_pcm_hw_params *params,
  494. struct snd_soc_dai *dai)
  495. {
  496. struct snd_soc_codec *codec = dai->codec;
  497. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  498. int ret;
  499. int wl;
  500. switch (params_width(params)) {
  501. case 16:
  502. wl = 0;
  503. break;
  504. case 20:
  505. wl = 0x4;
  506. break;
  507. case 24:
  508. wl = 0x8;
  509. break;
  510. case 32:
  511. wl = 0xc;
  512. break;
  513. default:
  514. return -EINVAL;
  515. }
  516. snd_soc_update_bits(codec, WM8955_AUDIO_INTERFACE,
  517. WM8955_WL_MASK, wl);
  518. wm8955->fs = params_rate(params);
  519. wm8955_set_deemph(codec);
  520. /* If the chip is clocked then disable the clocks and force a
  521. * reconfiguration, otherwise DAPM will power up the
  522. * clocks for us later. */
  523. ret = snd_soc_read(codec, WM8955_POWER_MANAGEMENT_1);
  524. if (ret < 0)
  525. return ret;
  526. if (ret & WM8955_DIGENB) {
  527. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  528. WM8955_DIGENB, 0);
  529. snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
  530. WM8955_PLL_RB | WM8955_PLLEN, 0);
  531. wm8955_configure_clocking(codec);
  532. }
  533. return 0;
  534. }
  535. static int wm8955_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  536. unsigned int freq, int dir)
  537. {
  538. struct snd_soc_codec *codec = dai->codec;
  539. struct wm8955_priv *priv = snd_soc_codec_get_drvdata(codec);
  540. int div;
  541. switch (clk_id) {
  542. case WM8955_CLK_MCLK:
  543. if (freq > 15000000) {
  544. priv->mclk_rate = freq /= 2;
  545. div = WM8955_MCLKDIV2;
  546. } else {
  547. priv->mclk_rate = freq;
  548. div = 0;
  549. }
  550. snd_soc_update_bits(codec, WM8955_SAMPLE_RATE,
  551. WM8955_MCLKDIV2, div);
  552. break;
  553. default:
  554. return -EINVAL;
  555. }
  556. dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
  557. return 0;
  558. }
  559. static int wm8955_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  560. {
  561. struct snd_soc_codec *codec = dai->codec;
  562. u16 aif = 0;
  563. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  564. case SND_SOC_DAIFMT_CBS_CFS:
  565. break;
  566. case SND_SOC_DAIFMT_CBM_CFM:
  567. aif |= WM8955_MS;
  568. break;
  569. default:
  570. return -EINVAL;
  571. }
  572. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  573. case SND_SOC_DAIFMT_DSP_B:
  574. aif |= WM8955_LRP;
  575. case SND_SOC_DAIFMT_DSP_A:
  576. aif |= 0x3;
  577. break;
  578. case SND_SOC_DAIFMT_I2S:
  579. aif |= 0x2;
  580. break;
  581. case SND_SOC_DAIFMT_RIGHT_J:
  582. break;
  583. case SND_SOC_DAIFMT_LEFT_J:
  584. aif |= 0x1;
  585. break;
  586. default:
  587. return -EINVAL;
  588. }
  589. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  590. case SND_SOC_DAIFMT_DSP_A:
  591. case SND_SOC_DAIFMT_DSP_B:
  592. /* frame inversion not valid for DSP modes */
  593. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  594. case SND_SOC_DAIFMT_NB_NF:
  595. break;
  596. case SND_SOC_DAIFMT_IB_NF:
  597. aif |= WM8955_BCLKINV;
  598. break;
  599. default:
  600. return -EINVAL;
  601. }
  602. break;
  603. case SND_SOC_DAIFMT_I2S:
  604. case SND_SOC_DAIFMT_RIGHT_J:
  605. case SND_SOC_DAIFMT_LEFT_J:
  606. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  607. case SND_SOC_DAIFMT_NB_NF:
  608. break;
  609. case SND_SOC_DAIFMT_IB_IF:
  610. aif |= WM8955_BCLKINV | WM8955_LRP;
  611. break;
  612. case SND_SOC_DAIFMT_IB_NF:
  613. aif |= WM8955_BCLKINV;
  614. break;
  615. case SND_SOC_DAIFMT_NB_IF:
  616. aif |= WM8955_LRP;
  617. break;
  618. default:
  619. return -EINVAL;
  620. }
  621. break;
  622. default:
  623. return -EINVAL;
  624. }
  625. snd_soc_update_bits(codec, WM8955_AUDIO_INTERFACE,
  626. WM8955_MS | WM8955_FORMAT_MASK | WM8955_BCLKINV |
  627. WM8955_LRP, aif);
  628. return 0;
  629. }
  630. static int wm8955_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  631. {
  632. struct snd_soc_codec *codec = codec_dai->codec;
  633. int val;
  634. if (mute)
  635. val = WM8955_DACMU;
  636. else
  637. val = 0;
  638. snd_soc_update_bits(codec, WM8955_DAC_CONTROL, WM8955_DACMU, val);
  639. return 0;
  640. }
  641. static int wm8955_set_bias_level(struct snd_soc_codec *codec,
  642. enum snd_soc_bias_level level)
  643. {
  644. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  645. int ret;
  646. switch (level) {
  647. case SND_SOC_BIAS_ON:
  648. break;
  649. case SND_SOC_BIAS_PREPARE:
  650. /* VMID resistance 2*50k */
  651. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  652. WM8955_VMIDSEL_MASK,
  653. 0x1 << WM8955_VMIDSEL_SHIFT);
  654. /* Default bias current */
  655. snd_soc_update_bits(codec, WM8955_ADDITIONAL_CONTROL_1,
  656. WM8955_VSEL_MASK,
  657. 0x2 << WM8955_VSEL_SHIFT);
  658. break;
  659. case SND_SOC_BIAS_STANDBY:
  660. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  661. ret = regulator_bulk_enable(ARRAY_SIZE(wm8955->supplies),
  662. wm8955->supplies);
  663. if (ret != 0) {
  664. dev_err(codec->dev,
  665. "Failed to enable supplies: %d\n",
  666. ret);
  667. return ret;
  668. }
  669. regcache_sync(wm8955->regmap);
  670. /* Enable VREF and VMID */
  671. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  672. WM8955_VREF |
  673. WM8955_VMIDSEL_MASK,
  674. WM8955_VREF |
  675. 0x3 << WM8955_VREF_SHIFT);
  676. /* Let VMID ramp */
  677. msleep(500);
  678. /* High resistance VROI to maintain outputs */
  679. snd_soc_update_bits(codec,
  680. WM8955_ADDITIONAL_CONTROL_3,
  681. WM8955_VROI, WM8955_VROI);
  682. }
  683. /* Maintain VMID with 2*250k */
  684. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  685. WM8955_VMIDSEL_MASK,
  686. 0x2 << WM8955_VMIDSEL_SHIFT);
  687. /* Minimum bias current */
  688. snd_soc_update_bits(codec, WM8955_ADDITIONAL_CONTROL_1,
  689. WM8955_VSEL_MASK, 0);
  690. break;
  691. case SND_SOC_BIAS_OFF:
  692. /* Low resistance VROI to help discharge */
  693. snd_soc_update_bits(codec,
  694. WM8955_ADDITIONAL_CONTROL_3,
  695. WM8955_VROI, 0);
  696. /* Turn off VMID and VREF */
  697. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  698. WM8955_VREF |
  699. WM8955_VMIDSEL_MASK, 0);
  700. regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies),
  701. wm8955->supplies);
  702. break;
  703. }
  704. return 0;
  705. }
  706. #define WM8955_RATES SNDRV_PCM_RATE_8000_96000
  707. #define WM8955_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  708. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  709. static const struct snd_soc_dai_ops wm8955_dai_ops = {
  710. .set_sysclk = wm8955_set_sysclk,
  711. .set_fmt = wm8955_set_fmt,
  712. .hw_params = wm8955_hw_params,
  713. .digital_mute = wm8955_digital_mute,
  714. };
  715. static struct snd_soc_dai_driver wm8955_dai = {
  716. .name = "wm8955-hifi",
  717. .playback = {
  718. .stream_name = "Playback",
  719. .channels_min = 2,
  720. .channels_max = 2,
  721. .rates = WM8955_RATES,
  722. .formats = WM8955_FORMATS,
  723. },
  724. .ops = &wm8955_dai_ops,
  725. };
  726. static int wm8955_probe(struct snd_soc_codec *codec)
  727. {
  728. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  729. struct wm8955_pdata *pdata = dev_get_platdata(codec->dev);
  730. int ret, i;
  731. for (i = 0; i < ARRAY_SIZE(wm8955->supplies); i++)
  732. wm8955->supplies[i].supply = wm8955_supply_names[i];
  733. ret = devm_regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8955->supplies),
  734. wm8955->supplies);
  735. if (ret != 0) {
  736. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  737. return ret;
  738. }
  739. ret = regulator_bulk_enable(ARRAY_SIZE(wm8955->supplies),
  740. wm8955->supplies);
  741. if (ret != 0) {
  742. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  743. return ret;
  744. }
  745. ret = wm8955_reset(codec);
  746. if (ret < 0) {
  747. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  748. goto err_enable;
  749. }
  750. /* Change some default settings - latch VU and enable ZC */
  751. snd_soc_update_bits(codec, WM8955_LEFT_DAC_VOLUME,
  752. WM8955_LDVU, WM8955_LDVU);
  753. snd_soc_update_bits(codec, WM8955_RIGHT_DAC_VOLUME,
  754. WM8955_RDVU, WM8955_RDVU);
  755. snd_soc_update_bits(codec, WM8955_LOUT1_VOLUME,
  756. WM8955_LO1VU | WM8955_LO1ZC,
  757. WM8955_LO1VU | WM8955_LO1ZC);
  758. snd_soc_update_bits(codec, WM8955_ROUT1_VOLUME,
  759. WM8955_RO1VU | WM8955_RO1ZC,
  760. WM8955_RO1VU | WM8955_RO1ZC);
  761. snd_soc_update_bits(codec, WM8955_LOUT2_VOLUME,
  762. WM8955_LO2VU | WM8955_LO2ZC,
  763. WM8955_LO2VU | WM8955_LO2ZC);
  764. snd_soc_update_bits(codec, WM8955_ROUT2_VOLUME,
  765. WM8955_RO2VU | WM8955_RO2ZC,
  766. WM8955_RO2VU | WM8955_RO2ZC);
  767. snd_soc_update_bits(codec, WM8955_MONOOUT_VOLUME,
  768. WM8955_MOZC, WM8955_MOZC);
  769. /* Also enable adaptive bass boost by default */
  770. snd_soc_update_bits(codec, WM8955_BASS_CONTROL, WM8955_BB, WM8955_BB);
  771. /* Set platform data values */
  772. if (pdata) {
  773. if (pdata->out2_speaker)
  774. snd_soc_update_bits(codec, WM8955_ADDITIONAL_CONTROL_2,
  775. WM8955_ROUT2INV, WM8955_ROUT2INV);
  776. if (pdata->monoin_diff)
  777. snd_soc_update_bits(codec, WM8955_MONO_OUT_MIX_1,
  778. WM8955_DMEN, WM8955_DMEN);
  779. }
  780. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
  781. /* Bias level configuration will have done an extra enable */
  782. regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
  783. return 0;
  784. err_enable:
  785. regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
  786. return ret;
  787. }
  788. static struct snd_soc_codec_driver soc_codec_dev_wm8955 = {
  789. .probe = wm8955_probe,
  790. .set_bias_level = wm8955_set_bias_level,
  791. .suspend_bias_off = true,
  792. .controls = wm8955_snd_controls,
  793. .num_controls = ARRAY_SIZE(wm8955_snd_controls),
  794. .dapm_widgets = wm8955_dapm_widgets,
  795. .num_dapm_widgets = ARRAY_SIZE(wm8955_dapm_widgets),
  796. .dapm_routes = wm8955_dapm_routes,
  797. .num_dapm_routes = ARRAY_SIZE(wm8955_dapm_routes),
  798. };
  799. static const struct regmap_config wm8955_regmap = {
  800. .reg_bits = 7,
  801. .val_bits = 9,
  802. .max_register = WM8955_MAX_REGISTER,
  803. .volatile_reg = wm8955_volatile,
  804. .writeable_reg = wm8955_writeable,
  805. .cache_type = REGCACHE_RBTREE,
  806. .reg_defaults = wm8955_reg_defaults,
  807. .num_reg_defaults = ARRAY_SIZE(wm8955_reg_defaults),
  808. };
  809. static int wm8955_i2c_probe(struct i2c_client *i2c,
  810. const struct i2c_device_id *id)
  811. {
  812. struct wm8955_priv *wm8955;
  813. int ret;
  814. wm8955 = devm_kzalloc(&i2c->dev, sizeof(struct wm8955_priv),
  815. GFP_KERNEL);
  816. if (wm8955 == NULL)
  817. return -ENOMEM;
  818. wm8955->regmap = devm_regmap_init_i2c(i2c, &wm8955_regmap);
  819. if (IS_ERR(wm8955->regmap)) {
  820. ret = PTR_ERR(wm8955->regmap);
  821. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  822. ret);
  823. return ret;
  824. }
  825. i2c_set_clientdata(i2c, wm8955);
  826. ret = snd_soc_register_codec(&i2c->dev,
  827. &soc_codec_dev_wm8955, &wm8955_dai, 1);
  828. return ret;
  829. }
  830. static int wm8955_i2c_remove(struct i2c_client *client)
  831. {
  832. snd_soc_unregister_codec(&client->dev);
  833. return 0;
  834. }
  835. static const struct i2c_device_id wm8955_i2c_id[] = {
  836. { "wm8955", 0 },
  837. { }
  838. };
  839. MODULE_DEVICE_TABLE(i2c, wm8955_i2c_id);
  840. static struct i2c_driver wm8955_i2c_driver = {
  841. .driver = {
  842. .name = "wm8955",
  843. },
  844. .probe = wm8955_i2c_probe,
  845. .remove = wm8955_i2c_remove,
  846. .id_table = wm8955_i2c_id,
  847. };
  848. module_i2c_driver(wm8955_i2c_driver);
  849. MODULE_DESCRIPTION("ASoC WM8955 driver");
  850. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  851. MODULE_LICENSE("GPL");