wm8955.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 specifiying 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");