wm8955.c 28 KB

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