wm8955.c 28 KB

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