wm8955.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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/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. enum snd_soc_control_type control_type;
  39. unsigned int mclk_rate;
  40. int deemph;
  41. int fs;
  42. struct regulator_bulk_data supplies[WM8955_NUM_SUPPLIES];
  43. };
  44. static const u16 wm8955_reg[WM8955_MAX_REGISTER + 1] = {
  45. 0x0000, /* R0 */
  46. 0x0000, /* R1 */
  47. 0x0079, /* R2 - LOUT1 volume */
  48. 0x0079, /* R3 - ROUT1 volume */
  49. 0x0000, /* R4 */
  50. 0x0008, /* R5 - DAC Control */
  51. 0x0000, /* R6 */
  52. 0x000A, /* R7 - Audio Interface */
  53. 0x0000, /* R8 - Sample Rate */
  54. 0x0000, /* R9 */
  55. 0x00FF, /* R10 - Left DAC volume */
  56. 0x00FF, /* R11 - Right DAC volume */
  57. 0x000F, /* R12 - Bass control */
  58. 0x000F, /* R13 - Treble control */
  59. 0x0000, /* R14 */
  60. 0x0000, /* R15 - Reset */
  61. 0x0000, /* R16 */
  62. 0x0000, /* R17 */
  63. 0x0000, /* R18 */
  64. 0x0000, /* R19 */
  65. 0x0000, /* R20 */
  66. 0x0000, /* R21 */
  67. 0x0000, /* R22 */
  68. 0x00C1, /* R23 - Additional control (1) */
  69. 0x0000, /* R24 - Additional control (2) */
  70. 0x0000, /* R25 - Power Management (1) */
  71. 0x0000, /* R26 - Power Management (2) */
  72. 0x0000, /* R27 - Additional Control (3) */
  73. 0x0000, /* R28 */
  74. 0x0000, /* R29 */
  75. 0x0000, /* R30 */
  76. 0x0000, /* R31 */
  77. 0x0000, /* R32 */
  78. 0x0000, /* R33 */
  79. 0x0050, /* R34 - Left out Mix (1) */
  80. 0x0050, /* R35 - Left out Mix (2) */
  81. 0x0050, /* R36 - Right out Mix (1) */
  82. 0x0050, /* R37 - Right Out Mix (2) */
  83. 0x0050, /* R38 - Mono out Mix (1) */
  84. 0x0050, /* R39 - Mono out Mix (2) */
  85. 0x0079, /* R40 - LOUT2 volume */
  86. 0x0079, /* R41 - ROUT2 volume */
  87. 0x0079, /* R42 - MONOOUT volume */
  88. 0x0000, /* R43 - Clocking / PLL */
  89. 0x0103, /* R44 - PLL Control 1 */
  90. 0x0024, /* R45 - PLL Control 2 */
  91. 0x01BA, /* R46 - PLL Control 3 */
  92. 0x0000, /* R47 */
  93. 0x0000, /* R48 */
  94. 0x0000, /* R49 */
  95. 0x0000, /* R50 */
  96. 0x0000, /* R51 */
  97. 0x0000, /* R52 */
  98. 0x0000, /* R53 */
  99. 0x0000, /* R54 */
  100. 0x0000, /* R55 */
  101. 0x0000, /* R56 */
  102. 0x0000, /* R57 */
  103. 0x0000, /* R58 */
  104. 0x0000, /* R59 - PLL Control 4 */
  105. };
  106. static int wm8955_reset(struct snd_soc_codec *codec)
  107. {
  108. return snd_soc_write(codec, WM8955_RESET, 0);
  109. }
  110. struct pll_factors {
  111. int n;
  112. int k;
  113. int outdiv;
  114. };
  115. /* The size in bits of the FLL divide multiplied by 10
  116. * to allow rounding later */
  117. #define FIXED_FLL_SIZE ((1 << 22) * 10)
  118. static int wm8995_pll_factors(struct device *dev,
  119. int Fref, int Fout, struct pll_factors *pll)
  120. {
  121. u64 Kpart;
  122. unsigned int K, Ndiv, Nmod, target;
  123. dev_dbg(dev, "Fref=%u Fout=%u\n", Fref, Fout);
  124. /* The oscilator should run at should be 90-100MHz, and
  125. * there's a divide by 4 plus an optional divide by 2 in the
  126. * output path to generate the system clock. The clock table
  127. * is sortd so we should always generate a suitable target. */
  128. target = Fout * 4;
  129. if (target < 90000000) {
  130. pll->outdiv = 1;
  131. target *= 2;
  132. } else {
  133. pll->outdiv = 0;
  134. }
  135. WARN_ON(target < 90000000 || target > 100000000);
  136. dev_dbg(dev, "Fvco=%dHz\n", target);
  137. /* Now, calculate N.K */
  138. Ndiv = target / Fref;
  139. pll->n = Ndiv;
  140. Nmod = target % Fref;
  141. dev_dbg(dev, "Nmod=%d\n", Nmod);
  142. /* Calculate fractional part - scale up so we can round. */
  143. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  144. do_div(Kpart, Fref);
  145. K = Kpart & 0xFFFFFFFF;
  146. if ((K % 10) >= 5)
  147. K += 5;
  148. /* Move down to proper range now rounding is done */
  149. pll->k = K / 10;
  150. dev_dbg(dev, "N=%x K=%x OUTDIV=%x\n", pll->n, pll->k, pll->outdiv);
  151. return 0;
  152. }
  153. /* Lookup table specifying SRATE (table 25 in datasheet); some of the
  154. * output frequencies have been rounded to the standard frequencies
  155. * they are intended to match where the error is slight. */
  156. static struct {
  157. int mclk;
  158. int fs;
  159. int usb;
  160. int sr;
  161. } clock_cfgs[] = {
  162. { 18432000, 8000, 0, 3, },
  163. { 18432000, 12000, 0, 9, },
  164. { 18432000, 16000, 0, 11, },
  165. { 18432000, 24000, 0, 29, },
  166. { 18432000, 32000, 0, 13, },
  167. { 18432000, 48000, 0, 1, },
  168. { 18432000, 96000, 0, 15, },
  169. { 16934400, 8018, 0, 19, },
  170. { 16934400, 11025, 0, 25, },
  171. { 16934400, 22050, 0, 27, },
  172. { 16934400, 44100, 0, 17, },
  173. { 16934400, 88200, 0, 31, },
  174. { 12000000, 8000, 1, 2, },
  175. { 12000000, 11025, 1, 25, },
  176. { 12000000, 12000, 1, 8, },
  177. { 12000000, 16000, 1, 10, },
  178. { 12000000, 22050, 1, 27, },
  179. { 12000000, 24000, 1, 28, },
  180. { 12000000, 32000, 1, 12, },
  181. { 12000000, 44100, 1, 17, },
  182. { 12000000, 48000, 1, 0, },
  183. { 12000000, 88200, 1, 31, },
  184. { 12000000, 96000, 1, 14, },
  185. { 12288000, 8000, 0, 2, },
  186. { 12288000, 12000, 0, 8, },
  187. { 12288000, 16000, 0, 10, },
  188. { 12288000, 24000, 0, 28, },
  189. { 12288000, 32000, 0, 12, },
  190. { 12288000, 48000, 0, 0, },
  191. { 12288000, 96000, 0, 14, },
  192. { 12289600, 8018, 0, 18, },
  193. { 12289600, 11025, 0, 24, },
  194. { 12289600, 22050, 0, 26, },
  195. { 11289600, 44100, 0, 16, },
  196. { 11289600, 88200, 0, 31, },
  197. };
  198. static int wm8955_configure_clocking(struct snd_soc_codec *codec)
  199. {
  200. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  201. int i, ret, val;
  202. int clocking = 0;
  203. int srate = 0;
  204. int sr = -1;
  205. struct pll_factors pll;
  206. /* If we're not running a sample rate currently just pick one */
  207. if (wm8955->fs == 0)
  208. wm8955->fs = 8000;
  209. /* Can we generate an exact output? */
  210. for (i = 0; i < ARRAY_SIZE(clock_cfgs); i++) {
  211. if (wm8955->fs != clock_cfgs[i].fs)
  212. continue;
  213. sr = i;
  214. if (wm8955->mclk_rate == clock_cfgs[i].mclk)
  215. break;
  216. }
  217. /* We should never get here with an unsupported sample rate */
  218. if (sr == -1) {
  219. dev_err(codec->dev, "Sample rate %dHz unsupported\n",
  220. wm8955->fs);
  221. WARN_ON(sr == -1);
  222. return -EINVAL;
  223. }
  224. if (i == ARRAY_SIZE(clock_cfgs)) {
  225. /* If we can't generate the right clock from MCLK then
  226. * we should configure the PLL to supply us with an
  227. * appropriate clock.
  228. */
  229. clocking |= WM8955_MCLKSEL;
  230. /* Use the last divider configuration we saw for the
  231. * sample rate. */
  232. ret = wm8995_pll_factors(codec->dev, wm8955->mclk_rate,
  233. clock_cfgs[sr].mclk, &pll);
  234. if (ret != 0) {
  235. dev_err(codec->dev,
  236. "Unable to generate %dHz from %dHz MCLK\n",
  237. wm8955->fs, wm8955->mclk_rate);
  238. return -EINVAL;
  239. }
  240. snd_soc_update_bits(codec, WM8955_PLL_CONTROL_1,
  241. WM8955_N_MASK | WM8955_K_21_18_MASK,
  242. (pll.n << WM8955_N_SHIFT) |
  243. pll.k >> 18);
  244. snd_soc_update_bits(codec, WM8955_PLL_CONTROL_2,
  245. WM8955_K_17_9_MASK,
  246. (pll.k >> 9) & WM8955_K_17_9_MASK);
  247. snd_soc_update_bits(codec, WM8955_PLL_CONTROL_2,
  248. WM8955_K_8_0_MASK,
  249. pll.k & WM8955_K_8_0_MASK);
  250. if (pll.k)
  251. snd_soc_update_bits(codec, WM8955_PLL_CONTROL_4,
  252. WM8955_KEN, WM8955_KEN);
  253. else
  254. snd_soc_update_bits(codec, WM8955_PLL_CONTROL_4,
  255. WM8955_KEN, 0);
  256. if (pll.outdiv)
  257. val = WM8955_PLL_RB | WM8955_PLLOUTDIV2;
  258. else
  259. val = WM8955_PLL_RB;
  260. /* Now start the PLL running */
  261. snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
  262. WM8955_PLL_RB | WM8955_PLLOUTDIV2, val);
  263. snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
  264. WM8955_PLLEN, WM8955_PLLEN);
  265. }
  266. srate = clock_cfgs[sr].usb | (clock_cfgs[sr].sr << WM8955_SR_SHIFT);
  267. snd_soc_update_bits(codec, WM8955_SAMPLE_RATE,
  268. WM8955_USB | WM8955_SR_MASK, srate);
  269. snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
  270. WM8955_MCLKSEL, clocking);
  271. return 0;
  272. }
  273. static int wm8955_sysclk(struct snd_soc_dapm_widget *w,
  274. struct snd_kcontrol *kcontrol, int event)
  275. {
  276. struct snd_soc_codec *codec = w->codec;
  277. int ret = 0;
  278. /* Always disable the clocks - if we're doing reconfiguration this
  279. * avoids misclocking.
  280. */
  281. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  282. WM8955_DIGENB, 0);
  283. snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
  284. WM8955_PLL_RB | WM8955_PLLEN, 0);
  285. switch (event) {
  286. case SND_SOC_DAPM_POST_PMD:
  287. break;
  288. case SND_SOC_DAPM_PRE_PMU:
  289. ret = wm8955_configure_clocking(codec);
  290. break;
  291. default:
  292. ret = -EINVAL;
  293. break;
  294. }
  295. return ret;
  296. }
  297. static int deemph_settings[] = { 0, 32000, 44100, 48000 };
  298. static int wm8955_set_deemph(struct snd_soc_codec *codec)
  299. {
  300. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  301. int val, i, best;
  302. /* If we're using deemphasis select the nearest available sample
  303. * rate.
  304. */
  305. if (wm8955->deemph) {
  306. best = 1;
  307. for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
  308. if (abs(deemph_settings[i] - wm8955->fs) <
  309. abs(deemph_settings[best] - wm8955->fs))
  310. best = i;
  311. }
  312. val = best << WM8955_DEEMPH_SHIFT;
  313. } else {
  314. val = 0;
  315. }
  316. dev_dbg(codec->dev, "Set deemphasis %d\n", val);
  317. return snd_soc_update_bits(codec, WM8955_DAC_CONTROL,
  318. WM8955_DEEMPH_MASK, val);
  319. }
  320. static int wm8955_get_deemph(struct snd_kcontrol *kcontrol,
  321. struct snd_ctl_elem_value *ucontrol)
  322. {
  323. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  324. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  325. ucontrol->value.enumerated.item[0] = wm8955->deemph;
  326. return 0;
  327. }
  328. static int wm8955_put_deemph(struct snd_kcontrol *kcontrol,
  329. struct snd_ctl_elem_value *ucontrol)
  330. {
  331. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  332. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  333. int deemph = ucontrol->value.enumerated.item[0];
  334. if (deemph > 1)
  335. return -EINVAL;
  336. wm8955->deemph = deemph;
  337. return wm8955_set_deemph(codec);
  338. }
  339. static const char *bass_mode_text[] = {
  340. "Linear", "Adaptive",
  341. };
  342. static const struct soc_enum bass_mode =
  343. SOC_ENUM_SINGLE(WM8955_BASS_CONTROL, 7, 2, bass_mode_text);
  344. static const char *bass_cutoff_text[] = {
  345. "Low", "High"
  346. };
  347. static const struct soc_enum bass_cutoff =
  348. SOC_ENUM_SINGLE(WM8955_BASS_CONTROL, 6, 2, bass_cutoff_text);
  349. static const char *treble_cutoff_text[] = {
  350. "High", "Low"
  351. };
  352. static const struct soc_enum treble_cutoff =
  353. SOC_ENUM_SINGLE(WM8955_TREBLE_CONTROL, 6, 2, treble_cutoff_text);
  354. static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1);
  355. static const DECLARE_TLV_DB_SCALE(atten_tlv, -600, 600, 0);
  356. static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
  357. static const DECLARE_TLV_DB_SCALE(mono_tlv, -2100, 300, 0);
  358. static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
  359. static const DECLARE_TLV_DB_SCALE(treble_tlv, -1200, 150, 1);
  360. static const struct snd_kcontrol_new wm8955_snd_controls[] = {
  361. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8955_LEFT_DAC_VOLUME,
  362. WM8955_RIGHT_DAC_VOLUME, 0, 255, 0, digital_tlv),
  363. SOC_SINGLE_TLV("Playback Attenuation Volume", WM8955_DAC_CONTROL, 7, 1, 1,
  364. atten_tlv),
  365. SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
  366. wm8955_get_deemph, wm8955_put_deemph),
  367. SOC_ENUM("Bass Mode", bass_mode),
  368. SOC_ENUM("Bass Cutoff", bass_cutoff),
  369. SOC_SINGLE("Bass Volume", WM8955_BASS_CONTROL, 0, 15, 1),
  370. SOC_ENUM("Treble Cutoff", treble_cutoff),
  371. SOC_SINGLE_TLV("Treble Volume", WM8955_TREBLE_CONTROL, 0, 14, 1, treble_tlv),
  372. SOC_SINGLE_TLV("Left Bypass Volume", WM8955_LEFT_OUT_MIX_1, 4, 7, 1,
  373. bypass_tlv),
  374. SOC_SINGLE_TLV("Left Mono Volume", WM8955_LEFT_OUT_MIX_2, 4, 7, 1,
  375. bypass_tlv),
  376. SOC_SINGLE_TLV("Right Mono Volume", WM8955_RIGHT_OUT_MIX_1, 4, 7, 1,
  377. bypass_tlv),
  378. SOC_SINGLE_TLV("Right Bypass Volume", WM8955_RIGHT_OUT_MIX_2, 4, 7, 1,
  379. bypass_tlv),
  380. /* Not a stereo pair so they line up with the DAPM switches */
  381. SOC_SINGLE_TLV("Mono Left Bypass Volume", WM8955_MONO_OUT_MIX_1, 4, 7, 1,
  382. mono_tlv),
  383. SOC_SINGLE_TLV("Mono Right Bypass Volume", WM8955_MONO_OUT_MIX_2, 4, 7, 1,
  384. mono_tlv),
  385. SOC_DOUBLE_R_TLV("Headphone Volume", WM8955_LOUT1_VOLUME,
  386. WM8955_ROUT1_VOLUME, 0, 127, 0, out_tlv),
  387. SOC_DOUBLE_R("Headphone ZC Switch", WM8955_LOUT1_VOLUME,
  388. WM8955_ROUT1_VOLUME, 7, 1, 0),
  389. SOC_DOUBLE_R_TLV("Speaker Volume", WM8955_LOUT2_VOLUME,
  390. WM8955_ROUT2_VOLUME, 0, 127, 0, out_tlv),
  391. SOC_DOUBLE_R("Speaker ZC Switch", WM8955_LOUT2_VOLUME,
  392. WM8955_ROUT2_VOLUME, 7, 1, 0),
  393. SOC_SINGLE_TLV("Mono Volume", WM8955_MONOOUT_VOLUME, 0, 127, 0, out_tlv),
  394. SOC_SINGLE("Mono ZC Switch", WM8955_MONOOUT_VOLUME, 7, 1, 0),
  395. };
  396. static const struct snd_kcontrol_new lmixer[] = {
  397. SOC_DAPM_SINGLE("Playback Switch", WM8955_LEFT_OUT_MIX_1, 8, 1, 0),
  398. SOC_DAPM_SINGLE("Bypass Switch", WM8955_LEFT_OUT_MIX_1, 7, 1, 0),
  399. SOC_DAPM_SINGLE("Right Playback Switch", WM8955_LEFT_OUT_MIX_2, 8, 1, 0),
  400. SOC_DAPM_SINGLE("Mono Switch", WM8955_LEFT_OUT_MIX_2, 7, 1, 0),
  401. };
  402. static const struct snd_kcontrol_new rmixer[] = {
  403. SOC_DAPM_SINGLE("Left Playback Switch", WM8955_RIGHT_OUT_MIX_1, 8, 1, 0),
  404. SOC_DAPM_SINGLE("Mono Switch", WM8955_RIGHT_OUT_MIX_1, 7, 1, 0),
  405. SOC_DAPM_SINGLE("Playback Switch", WM8955_RIGHT_OUT_MIX_2, 8, 1, 0),
  406. SOC_DAPM_SINGLE("Bypass Switch", WM8955_RIGHT_OUT_MIX_2, 7, 1, 0),
  407. };
  408. static const struct snd_kcontrol_new mmixer[] = {
  409. SOC_DAPM_SINGLE("Left Playback Switch", WM8955_MONO_OUT_MIX_1, 8, 1, 0),
  410. SOC_DAPM_SINGLE("Left Bypass Switch", WM8955_MONO_OUT_MIX_1, 7, 1, 0),
  411. SOC_DAPM_SINGLE("Right Playback Switch", WM8955_MONO_OUT_MIX_2, 8, 1, 0),
  412. SOC_DAPM_SINGLE("Right Bypass Switch", WM8955_MONO_OUT_MIX_2, 7, 1, 0),
  413. };
  414. static const struct snd_soc_dapm_widget wm8955_dapm_widgets[] = {
  415. SND_SOC_DAPM_INPUT("MONOIN-"),
  416. SND_SOC_DAPM_INPUT("MONOIN+"),
  417. SND_SOC_DAPM_INPUT("LINEINR"),
  418. SND_SOC_DAPM_INPUT("LINEINL"),
  419. SND_SOC_DAPM_PGA("Mono Input", SND_SOC_NOPM, 0, 0, NULL, 0),
  420. SND_SOC_DAPM_SUPPLY("SYSCLK", WM8955_POWER_MANAGEMENT_1, 0, 1, wm8955_sysclk,
  421. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  422. SND_SOC_DAPM_SUPPLY("TSDEN", WM8955_ADDITIONAL_CONTROL_1, 8, 0, NULL, 0),
  423. SND_SOC_DAPM_DAC("DACL", "Playback", WM8955_POWER_MANAGEMENT_2, 8, 0),
  424. SND_SOC_DAPM_DAC("DACR", "Playback", WM8955_POWER_MANAGEMENT_2, 7, 0),
  425. SND_SOC_DAPM_PGA("LOUT1 PGA", WM8955_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
  426. SND_SOC_DAPM_PGA("ROUT1 PGA", WM8955_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
  427. SND_SOC_DAPM_PGA("LOUT2 PGA", WM8955_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
  428. SND_SOC_DAPM_PGA("ROUT2 PGA", WM8955_POWER_MANAGEMENT_2, 3, 0, NULL, 0),
  429. SND_SOC_DAPM_PGA("MOUT PGA", WM8955_POWER_MANAGEMENT_2, 2, 0, NULL, 0),
  430. SND_SOC_DAPM_PGA("OUT3 PGA", WM8955_POWER_MANAGEMENT_2, 1, 0, NULL, 0),
  431. /* The names are chosen to make the control names nice */
  432. SND_SOC_DAPM_MIXER("Left", SND_SOC_NOPM, 0, 0,
  433. lmixer, ARRAY_SIZE(lmixer)),
  434. SND_SOC_DAPM_MIXER("Right", SND_SOC_NOPM, 0, 0,
  435. rmixer, ARRAY_SIZE(rmixer)),
  436. SND_SOC_DAPM_MIXER("Mono", SND_SOC_NOPM, 0, 0,
  437. mmixer, ARRAY_SIZE(mmixer)),
  438. SND_SOC_DAPM_OUTPUT("LOUT1"),
  439. SND_SOC_DAPM_OUTPUT("ROUT1"),
  440. SND_SOC_DAPM_OUTPUT("LOUT2"),
  441. SND_SOC_DAPM_OUTPUT("ROUT2"),
  442. SND_SOC_DAPM_OUTPUT("MONOOUT"),
  443. SND_SOC_DAPM_OUTPUT("OUT3"),
  444. };
  445. static const struct snd_soc_dapm_route wm8955_intercon[] = {
  446. { "DACL", NULL, "SYSCLK" },
  447. { "DACR", NULL, "SYSCLK" },
  448. { "Mono Input", NULL, "MONOIN-" },
  449. { "Mono Input", NULL, "MONOIN+" },
  450. { "Left", "Playback Switch", "DACL" },
  451. { "Left", "Right Playback Switch", "DACR" },
  452. { "Left", "Bypass Switch", "LINEINL" },
  453. { "Left", "Mono Switch", "Mono Input" },
  454. { "Right", "Playback Switch", "DACR" },
  455. { "Right", "Left Playback Switch", "DACL" },
  456. { "Right", "Bypass Switch", "LINEINR" },
  457. { "Right", "Mono Switch", "Mono Input" },
  458. { "Mono", "Left Playback Switch", "DACL" },
  459. { "Mono", "Right Playback Switch", "DACR" },
  460. { "Mono", "Left Bypass Switch", "LINEINL" },
  461. { "Mono", "Right Bypass Switch", "LINEINR" },
  462. { "LOUT1 PGA", NULL, "Left" },
  463. { "LOUT1", NULL, "TSDEN" },
  464. { "LOUT1", NULL, "LOUT1 PGA" },
  465. { "ROUT1 PGA", NULL, "Right" },
  466. { "ROUT1", NULL, "TSDEN" },
  467. { "ROUT1", NULL, "ROUT1 PGA" },
  468. { "LOUT2 PGA", NULL, "Left" },
  469. { "LOUT2", NULL, "TSDEN" },
  470. { "LOUT2", NULL, "LOUT2 PGA" },
  471. { "ROUT2 PGA", NULL, "Right" },
  472. { "ROUT2", NULL, "TSDEN" },
  473. { "ROUT2", NULL, "ROUT2 PGA" },
  474. { "MOUT PGA", NULL, "Mono" },
  475. { "MONOOUT", NULL, "MOUT PGA" },
  476. /* OUT3 not currently implemented */
  477. { "OUT3", NULL, "OUT3 PGA" },
  478. };
  479. static int wm8955_add_widgets(struct snd_soc_codec *codec)
  480. {
  481. struct snd_soc_dapm_context *dapm = &codec->dapm;
  482. snd_soc_add_controls(codec, wm8955_snd_controls,
  483. ARRAY_SIZE(wm8955_snd_controls));
  484. snd_soc_dapm_new_controls(dapm, wm8955_dapm_widgets,
  485. ARRAY_SIZE(wm8955_dapm_widgets));
  486. snd_soc_dapm_add_routes(dapm, 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->dapm.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->dapm.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. snd_soc_update_bits(codec, WM8955_LEFT_DAC_VOLUME,
  782. WM8955_LDVU, WM8955_LDVU);
  783. snd_soc_update_bits(codec, WM8955_RIGHT_DAC_VOLUME,
  784. WM8955_RDVU, WM8955_RDVU);
  785. snd_soc_update_bits(codec, WM8955_LOUT1_VOLUME,
  786. WM8955_LO1VU | WM8955_LO1ZC,
  787. WM8955_LO1VU | WM8955_LO1ZC);
  788. snd_soc_update_bits(codec, WM8955_ROUT1_VOLUME,
  789. WM8955_RO1VU | WM8955_RO1ZC,
  790. WM8955_RO1VU | WM8955_RO1ZC);
  791. snd_soc_update_bits(codec, WM8955_LOUT2_VOLUME,
  792. WM8955_LO2VU | WM8955_LO2ZC,
  793. WM8955_LO2VU | WM8955_LO2ZC);
  794. snd_soc_update_bits(codec, WM8955_ROUT2_VOLUME,
  795. WM8955_RO2VU | WM8955_RO2ZC,
  796. WM8955_RO2VU | WM8955_RO2ZC);
  797. snd_soc_update_bits(codec, WM8955_MONOOUT_VOLUME,
  798. WM8955_MOZC, WM8955_MOZC);
  799. /* Also enable adaptive bass boost by default */
  800. snd_soc_update_bits(codec, WM8955_BASS_CONTROL, WM8955_BB, WM8955_BB);
  801. /* Set platform data values */
  802. if (pdata) {
  803. if (pdata->out2_speaker)
  804. reg_cache[WM8955_ADDITIONAL_CONTROL_2]
  805. |= WM8955_ROUT2INV;
  806. if (pdata->monoin_diff)
  807. reg_cache[WM8955_MONO_OUT_MIX_1]
  808. |= WM8955_DMEN;
  809. }
  810. wm8955_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  811. /* Bias level configuration will have done an extra enable */
  812. regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
  813. wm8955_add_widgets(codec);
  814. return 0;
  815. err_enable:
  816. regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
  817. err_get:
  818. regulator_bulk_free(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
  819. return ret;
  820. }
  821. static int wm8955_remove(struct snd_soc_codec *codec)
  822. {
  823. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  824. wm8955_set_bias_level(codec, SND_SOC_BIAS_OFF);
  825. regulator_bulk_free(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
  826. return 0;
  827. }
  828. static struct snd_soc_codec_driver soc_codec_dev_wm8955 = {
  829. .probe = wm8955_probe,
  830. .remove = wm8955_remove,
  831. .suspend = wm8955_suspend,
  832. .resume = wm8955_resume,
  833. .set_bias_level = wm8955_set_bias_level,
  834. .reg_cache_size = ARRAY_SIZE(wm8955_reg),
  835. .reg_word_size = sizeof(u16),
  836. .reg_cache_default = wm8955_reg,
  837. };
  838. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  839. static __devinit int wm8955_i2c_probe(struct i2c_client *i2c,
  840. const struct i2c_device_id *id)
  841. {
  842. struct wm8955_priv *wm8955;
  843. int ret;
  844. wm8955 = kzalloc(sizeof(struct wm8955_priv), GFP_KERNEL);
  845. if (wm8955 == NULL)
  846. return -ENOMEM;
  847. i2c_set_clientdata(i2c, wm8955);
  848. wm8955->control_type = SND_SOC_I2C;
  849. ret = snd_soc_register_codec(&i2c->dev,
  850. &soc_codec_dev_wm8955, &wm8955_dai, 1);
  851. if (ret < 0)
  852. kfree(wm8955);
  853. return ret;
  854. }
  855. static __devexit int wm8955_i2c_remove(struct i2c_client *client)
  856. {
  857. snd_soc_unregister_codec(&client->dev);
  858. kfree(i2c_get_clientdata(client));
  859. return 0;
  860. }
  861. static const struct i2c_device_id wm8955_i2c_id[] = {
  862. { "wm8955", 0 },
  863. { }
  864. };
  865. MODULE_DEVICE_TABLE(i2c, wm8955_i2c_id);
  866. static struct i2c_driver wm8955_i2c_driver = {
  867. .driver = {
  868. .name = "wm8955-codec",
  869. .owner = THIS_MODULE,
  870. },
  871. .probe = wm8955_i2c_probe,
  872. .remove = __devexit_p(wm8955_i2c_remove),
  873. .id_table = wm8955_i2c_id,
  874. };
  875. #endif
  876. static int __init wm8955_modinit(void)
  877. {
  878. int ret = 0;
  879. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  880. ret = i2c_add_driver(&wm8955_i2c_driver);
  881. if (ret != 0) {
  882. printk(KERN_ERR "Failed to register WM8955 I2C driver: %d\n",
  883. ret);
  884. }
  885. #endif
  886. return ret;
  887. }
  888. module_init(wm8955_modinit);
  889. static void __exit wm8955_exit(void)
  890. {
  891. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  892. i2c_del_driver(&wm8955_i2c_driver);
  893. #endif
  894. }
  895. module_exit(wm8955_exit);
  896. MODULE_DESCRIPTION("ASoC WM8955 driver");
  897. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  898. MODULE_LICENSE("GPL");