wm8955.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. return wm8955->deemph;
  328. }
  329. static int wm8955_put_deemph(struct snd_kcontrol *kcontrol,
  330. struct snd_ctl_elem_value *ucontrol)
  331. {
  332. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  333. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  334. int deemph = ucontrol->value.enumerated.item[0];
  335. if (deemph > 1)
  336. return -EINVAL;
  337. wm8955->deemph = deemph;
  338. return wm8955_set_deemph(codec);
  339. }
  340. static const char *bass_mode_text[] = {
  341. "Linear", "Adaptive",
  342. };
  343. static const struct soc_enum bass_mode =
  344. SOC_ENUM_SINGLE(WM8955_BASS_CONTROL, 7, 2, bass_mode_text);
  345. static const char *bass_cutoff_text[] = {
  346. "Low", "High"
  347. };
  348. static const struct soc_enum bass_cutoff =
  349. SOC_ENUM_SINGLE(WM8955_BASS_CONTROL, 6, 2, bass_cutoff_text);
  350. static const char *treble_cutoff_text[] = {
  351. "High", "Low"
  352. };
  353. static const struct soc_enum treble_cutoff =
  354. SOC_ENUM_SINGLE(WM8955_TREBLE_CONTROL, 6, 2, treble_cutoff_text);
  355. static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1);
  356. static const DECLARE_TLV_DB_SCALE(atten_tlv, -600, 600, 0);
  357. static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
  358. static const DECLARE_TLV_DB_SCALE(mono_tlv, -2100, 300, 0);
  359. static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
  360. static const DECLARE_TLV_DB_SCALE(treble_tlv, -1200, 150, 1);
  361. static const struct snd_kcontrol_new wm8955_snd_controls[] = {
  362. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8955_LEFT_DAC_VOLUME,
  363. WM8955_RIGHT_DAC_VOLUME, 0, 255, 0, digital_tlv),
  364. SOC_SINGLE_TLV("Playback Attenuation Volume", WM8955_DAC_CONTROL, 7, 1, 1,
  365. atten_tlv),
  366. SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
  367. wm8955_get_deemph, wm8955_put_deemph),
  368. SOC_ENUM("Bass Mode", bass_mode),
  369. SOC_ENUM("Bass Cutoff", bass_cutoff),
  370. SOC_SINGLE("Bass Volume", WM8955_BASS_CONTROL, 0, 15, 1),
  371. SOC_ENUM("Treble Cutoff", treble_cutoff),
  372. SOC_SINGLE_TLV("Treble Volume", WM8955_TREBLE_CONTROL, 0, 14, 1, treble_tlv),
  373. SOC_SINGLE_TLV("Left Bypass Volume", WM8955_LEFT_OUT_MIX_1, 4, 7, 1,
  374. bypass_tlv),
  375. SOC_SINGLE_TLV("Left Mono Volume", WM8955_LEFT_OUT_MIX_2, 4, 7, 1,
  376. bypass_tlv),
  377. SOC_SINGLE_TLV("Right Mono Volume", WM8955_RIGHT_OUT_MIX_1, 4, 7, 1,
  378. bypass_tlv),
  379. SOC_SINGLE_TLV("Right Bypass Volume", WM8955_RIGHT_OUT_MIX_2, 4, 7, 1,
  380. bypass_tlv),
  381. /* Not a stereo pair so they line up with the DAPM switches */
  382. SOC_SINGLE_TLV("Mono Left Bypass Volume", WM8955_MONO_OUT_MIX_1, 4, 7, 1,
  383. mono_tlv),
  384. SOC_SINGLE_TLV("Mono Right Bypass Volume", WM8955_MONO_OUT_MIX_2, 4, 7, 1,
  385. mono_tlv),
  386. SOC_DOUBLE_R_TLV("Headphone Volume", WM8955_LOUT1_VOLUME,
  387. WM8955_ROUT1_VOLUME, 0, 127, 0, out_tlv),
  388. SOC_DOUBLE_R("Headphone ZC Switch", WM8955_LOUT1_VOLUME,
  389. WM8955_ROUT1_VOLUME, 7, 1, 0),
  390. SOC_DOUBLE_R_TLV("Speaker Volume", WM8955_LOUT2_VOLUME,
  391. WM8955_ROUT2_VOLUME, 0, 127, 0, out_tlv),
  392. SOC_DOUBLE_R("Speaker ZC Switch", WM8955_LOUT2_VOLUME,
  393. WM8955_ROUT2_VOLUME, 7, 1, 0),
  394. SOC_SINGLE_TLV("Mono Volume", WM8955_MONOOUT_VOLUME, 0, 127, 0, out_tlv),
  395. SOC_SINGLE("Mono ZC Switch", WM8955_MONOOUT_VOLUME, 7, 1, 0),
  396. };
  397. static const struct snd_kcontrol_new lmixer[] = {
  398. SOC_DAPM_SINGLE("Playback Switch", WM8955_LEFT_OUT_MIX_1, 8, 1, 0),
  399. SOC_DAPM_SINGLE("Bypass Switch", WM8955_LEFT_OUT_MIX_1, 7, 1, 0),
  400. SOC_DAPM_SINGLE("Right Playback Switch", WM8955_LEFT_OUT_MIX_2, 8, 1, 0),
  401. SOC_DAPM_SINGLE("Mono Switch", WM8955_LEFT_OUT_MIX_2, 7, 1, 0),
  402. };
  403. static const struct snd_kcontrol_new rmixer[] = {
  404. SOC_DAPM_SINGLE("Left Playback Switch", WM8955_RIGHT_OUT_MIX_1, 8, 1, 0),
  405. SOC_DAPM_SINGLE("Mono Switch", WM8955_RIGHT_OUT_MIX_1, 7, 1, 0),
  406. SOC_DAPM_SINGLE("Playback Switch", WM8955_RIGHT_OUT_MIX_2, 8, 1, 0),
  407. SOC_DAPM_SINGLE("Bypass Switch", WM8955_RIGHT_OUT_MIX_2, 7, 1, 0),
  408. };
  409. static const struct snd_kcontrol_new mmixer[] = {
  410. SOC_DAPM_SINGLE("Left Playback Switch", WM8955_MONO_OUT_MIX_1, 8, 1, 0),
  411. SOC_DAPM_SINGLE("Left Bypass Switch", WM8955_MONO_OUT_MIX_1, 7, 1, 0),
  412. SOC_DAPM_SINGLE("Right Playback Switch", WM8955_MONO_OUT_MIX_2, 8, 1, 0),
  413. SOC_DAPM_SINGLE("Right Bypass Switch", WM8955_MONO_OUT_MIX_2, 7, 1, 0),
  414. };
  415. static const struct snd_soc_dapm_widget wm8955_dapm_widgets[] = {
  416. SND_SOC_DAPM_INPUT("MONOIN-"),
  417. SND_SOC_DAPM_INPUT("MONOIN+"),
  418. SND_SOC_DAPM_INPUT("LINEINR"),
  419. SND_SOC_DAPM_INPUT("LINEINL"),
  420. SND_SOC_DAPM_PGA("Mono Input", SND_SOC_NOPM, 0, 0, NULL, 0),
  421. SND_SOC_DAPM_SUPPLY("SYSCLK", WM8955_POWER_MANAGEMENT_1, 0, 1, wm8955_sysclk,
  422. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  423. SND_SOC_DAPM_SUPPLY("TSDEN", WM8955_ADDITIONAL_CONTROL_1, 8, 0, NULL, 0),
  424. SND_SOC_DAPM_DAC("DACL", "Playback", WM8955_POWER_MANAGEMENT_2, 8, 0),
  425. SND_SOC_DAPM_DAC("DACR", "Playback", WM8955_POWER_MANAGEMENT_2, 7, 0),
  426. SND_SOC_DAPM_PGA("LOUT1 PGA", WM8955_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
  427. SND_SOC_DAPM_PGA("ROUT1 PGA", WM8955_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
  428. SND_SOC_DAPM_PGA("LOUT2 PGA", WM8955_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
  429. SND_SOC_DAPM_PGA("ROUT2 PGA", WM8955_POWER_MANAGEMENT_2, 3, 0, NULL, 0),
  430. SND_SOC_DAPM_PGA("MOUT PGA", WM8955_POWER_MANAGEMENT_2, 2, 0, NULL, 0),
  431. SND_SOC_DAPM_PGA("OUT3 PGA", WM8955_POWER_MANAGEMENT_2, 1, 0, NULL, 0),
  432. /* The names are chosen to make the control names nice */
  433. SND_SOC_DAPM_MIXER("Left", SND_SOC_NOPM, 0, 0,
  434. lmixer, ARRAY_SIZE(lmixer)),
  435. SND_SOC_DAPM_MIXER("Right", SND_SOC_NOPM, 0, 0,
  436. rmixer, ARRAY_SIZE(rmixer)),
  437. SND_SOC_DAPM_MIXER("Mono", SND_SOC_NOPM, 0, 0,
  438. mmixer, ARRAY_SIZE(mmixer)),
  439. SND_SOC_DAPM_OUTPUT("LOUT1"),
  440. SND_SOC_DAPM_OUTPUT("ROUT1"),
  441. SND_SOC_DAPM_OUTPUT("LOUT2"),
  442. SND_SOC_DAPM_OUTPUT("ROUT2"),
  443. SND_SOC_DAPM_OUTPUT("MONOOUT"),
  444. SND_SOC_DAPM_OUTPUT("OUT3"),
  445. };
  446. static const struct snd_soc_dapm_route wm8955_intercon[] = {
  447. { "DACL", NULL, "SYSCLK" },
  448. { "DACR", NULL, "SYSCLK" },
  449. { "Mono Input", NULL, "MONOIN-" },
  450. { "Mono Input", NULL, "MONOIN+" },
  451. { "Left", "Playback Switch", "DACL" },
  452. { "Left", "Right Playback Switch", "DACR" },
  453. { "Left", "Bypass Switch", "LINEINL" },
  454. { "Left", "Mono Switch", "Mono Input" },
  455. { "Right", "Playback Switch", "DACR" },
  456. { "Right", "Left Playback Switch", "DACL" },
  457. { "Right", "Bypass Switch", "LINEINR" },
  458. { "Right", "Mono Switch", "Mono Input" },
  459. { "Mono", "Left Playback Switch", "DACL" },
  460. { "Mono", "Right Playback Switch", "DACR" },
  461. { "Mono", "Left Bypass Switch", "LINEINL" },
  462. { "Mono", "Right Bypass Switch", "LINEINR" },
  463. { "LOUT1 PGA", NULL, "Left" },
  464. { "LOUT1", NULL, "TSDEN" },
  465. { "LOUT1", NULL, "LOUT1 PGA" },
  466. { "ROUT1 PGA", NULL, "Right" },
  467. { "ROUT1", NULL, "TSDEN" },
  468. { "ROUT1", NULL, "ROUT1 PGA" },
  469. { "LOUT2 PGA", NULL, "Left" },
  470. { "LOUT2", NULL, "TSDEN" },
  471. { "LOUT2", NULL, "LOUT2 PGA" },
  472. { "ROUT2 PGA", NULL, "Right" },
  473. { "ROUT2", NULL, "TSDEN" },
  474. { "ROUT2", NULL, "ROUT2 PGA" },
  475. { "MOUT PGA", NULL, "Mono" },
  476. { "MONOOUT", NULL, "MOUT PGA" },
  477. /* OUT3 not currently implemented */
  478. { "OUT3", NULL, "OUT3 PGA" },
  479. };
  480. static int wm8955_add_widgets(struct snd_soc_codec *codec)
  481. {
  482. snd_soc_add_controls(codec, wm8955_snd_controls,
  483. ARRAY_SIZE(wm8955_snd_controls));
  484. snd_soc_dapm_new_controls(codec, wm8955_dapm_widgets,
  485. ARRAY_SIZE(wm8955_dapm_widgets));
  486. snd_soc_dapm_add_routes(codec, wm8955_intercon,
  487. ARRAY_SIZE(wm8955_intercon));
  488. return 0;
  489. }
  490. static int wm8955_hw_params(struct snd_pcm_substream *substream,
  491. struct snd_pcm_hw_params *params,
  492. struct snd_soc_dai *dai)
  493. {
  494. struct snd_soc_codec *codec = dai->codec;
  495. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  496. int ret;
  497. int wl;
  498. switch (params_format(params)) {
  499. case SNDRV_PCM_FORMAT_S16_LE:
  500. wl = 0;
  501. break;
  502. case SNDRV_PCM_FORMAT_S20_3LE:
  503. wl = 0x4;
  504. break;
  505. case SNDRV_PCM_FORMAT_S24_LE:
  506. wl = 0x8;
  507. break;
  508. case SNDRV_PCM_FORMAT_S32_LE:
  509. wl = 0xc;
  510. break;
  511. default:
  512. return -EINVAL;
  513. }
  514. snd_soc_update_bits(codec, WM8955_AUDIO_INTERFACE,
  515. WM8955_WL_MASK, wl);
  516. wm8955->fs = params_rate(params);
  517. wm8955_set_deemph(codec);
  518. /* If the chip is clocked then disable the clocks and force a
  519. * reconfiguration, otherwise DAPM will power up the
  520. * clocks for us later. */
  521. ret = snd_soc_read(codec, WM8955_POWER_MANAGEMENT_1);
  522. if (ret < 0)
  523. return ret;
  524. if (ret & WM8955_DIGENB) {
  525. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  526. WM8955_DIGENB, 0);
  527. snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
  528. WM8955_PLL_RB | WM8955_PLLEN, 0);
  529. wm8955_configure_clocking(codec);
  530. }
  531. return 0;
  532. }
  533. static int wm8955_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  534. unsigned int freq, int dir)
  535. {
  536. struct snd_soc_codec *codec = dai->codec;
  537. struct wm8955_priv *priv = snd_soc_codec_get_drvdata(codec);
  538. int div;
  539. switch (clk_id) {
  540. case WM8955_CLK_MCLK:
  541. if (freq > 15000000) {
  542. priv->mclk_rate = freq /= 2;
  543. div = WM8955_MCLKDIV2;
  544. } else {
  545. priv->mclk_rate = freq;
  546. div = 0;
  547. }
  548. snd_soc_update_bits(codec, WM8955_SAMPLE_RATE,
  549. WM8955_MCLKDIV2, div);
  550. break;
  551. default:
  552. return -EINVAL;
  553. }
  554. dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
  555. return 0;
  556. }
  557. static int wm8955_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  558. {
  559. struct snd_soc_codec *codec = dai->codec;
  560. u16 aif = 0;
  561. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  562. case SND_SOC_DAIFMT_CBS_CFS:
  563. break;
  564. case SND_SOC_DAIFMT_CBM_CFM:
  565. aif |= WM8955_MS;
  566. break;
  567. default:
  568. return -EINVAL;
  569. }
  570. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  571. case SND_SOC_DAIFMT_DSP_B:
  572. aif |= WM8955_LRP;
  573. case SND_SOC_DAIFMT_DSP_A:
  574. aif |= 0x3;
  575. break;
  576. case SND_SOC_DAIFMT_I2S:
  577. aif |= 0x2;
  578. break;
  579. case SND_SOC_DAIFMT_RIGHT_J:
  580. break;
  581. case SND_SOC_DAIFMT_LEFT_J:
  582. aif |= 0x1;
  583. break;
  584. default:
  585. return -EINVAL;
  586. }
  587. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  588. case SND_SOC_DAIFMT_DSP_A:
  589. case SND_SOC_DAIFMT_DSP_B:
  590. /* frame inversion not valid for DSP modes */
  591. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  592. case SND_SOC_DAIFMT_NB_NF:
  593. break;
  594. case SND_SOC_DAIFMT_IB_NF:
  595. aif |= WM8955_BCLKINV;
  596. break;
  597. default:
  598. return -EINVAL;
  599. }
  600. break;
  601. case SND_SOC_DAIFMT_I2S:
  602. case SND_SOC_DAIFMT_RIGHT_J:
  603. case SND_SOC_DAIFMT_LEFT_J:
  604. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  605. case SND_SOC_DAIFMT_NB_NF:
  606. break;
  607. case SND_SOC_DAIFMT_IB_IF:
  608. aif |= WM8955_BCLKINV | WM8955_LRP;
  609. break;
  610. case SND_SOC_DAIFMT_IB_NF:
  611. aif |= WM8955_BCLKINV;
  612. break;
  613. case SND_SOC_DAIFMT_NB_IF:
  614. aif |= WM8955_LRP;
  615. break;
  616. default:
  617. return -EINVAL;
  618. }
  619. break;
  620. default:
  621. return -EINVAL;
  622. }
  623. snd_soc_update_bits(codec, WM8955_AUDIO_INTERFACE,
  624. WM8955_MS | WM8955_FORMAT_MASK | WM8955_BCLKINV |
  625. WM8955_LRP, aif);
  626. return 0;
  627. }
  628. static int wm8955_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  629. {
  630. struct snd_soc_codec *codec = codec_dai->codec;
  631. int val;
  632. if (mute)
  633. val = WM8955_DACMU;
  634. else
  635. val = 0;
  636. snd_soc_update_bits(codec, WM8955_DAC_CONTROL, WM8955_DACMU, val);
  637. return 0;
  638. }
  639. static int wm8955_set_bias_level(struct snd_soc_codec *codec,
  640. enum snd_soc_bias_level level)
  641. {
  642. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  643. int ret, i;
  644. switch (level) {
  645. case SND_SOC_BIAS_ON:
  646. break;
  647. case SND_SOC_BIAS_PREPARE:
  648. /* VMID resistance 2*50k */
  649. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  650. WM8955_VMIDSEL_MASK,
  651. 0x1 << WM8955_VMIDSEL_SHIFT);
  652. /* Default bias current */
  653. snd_soc_update_bits(codec, WM8955_ADDITIONAL_CONTROL_1,
  654. WM8955_VSEL_MASK,
  655. 0x2 << WM8955_VSEL_SHIFT);
  656. break;
  657. case SND_SOC_BIAS_STANDBY:
  658. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  659. ret = regulator_bulk_enable(ARRAY_SIZE(wm8955->supplies),
  660. wm8955->supplies);
  661. if (ret != 0) {
  662. dev_err(codec->dev,
  663. "Failed to enable supplies: %d\n",
  664. ret);
  665. return ret;
  666. }
  667. /* Sync back cached values if they're
  668. * different from the hardware default.
  669. */
  670. for (i = 0; i < ARRAY_SIZE(wm8955->reg_cache); i++) {
  671. if (i == WM8955_RESET)
  672. continue;
  673. if (wm8955->reg_cache[i] == wm8955_reg[i])
  674. continue;
  675. snd_soc_write(codec, i, wm8955->reg_cache[i]);
  676. }
  677. /* Enable VREF and VMID */
  678. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  679. WM8955_VREF |
  680. WM8955_VMIDSEL_MASK,
  681. WM8955_VREF |
  682. 0x3 << WM8955_VREF_SHIFT);
  683. /* Let VMID ramp */
  684. msleep(500);
  685. /* High resistance VROI to maintain outputs */
  686. snd_soc_update_bits(codec,
  687. WM8955_ADDITIONAL_CONTROL_3,
  688. WM8955_VROI, WM8955_VROI);
  689. }
  690. /* Maintain VMID with 2*250k */
  691. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  692. WM8955_VMIDSEL_MASK,
  693. 0x2 << WM8955_VMIDSEL_SHIFT);
  694. /* Minimum bias current */
  695. snd_soc_update_bits(codec, WM8955_ADDITIONAL_CONTROL_1,
  696. WM8955_VSEL_MASK, 0);
  697. break;
  698. case SND_SOC_BIAS_OFF:
  699. /* Low resistance VROI to help discharge */
  700. snd_soc_update_bits(codec,
  701. WM8955_ADDITIONAL_CONTROL_3,
  702. WM8955_VROI, 0);
  703. /* Turn off VMID and VREF */
  704. snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
  705. WM8955_VREF |
  706. WM8955_VMIDSEL_MASK, 0);
  707. regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies),
  708. wm8955->supplies);
  709. break;
  710. }
  711. codec->bias_level = level;
  712. return 0;
  713. }
  714. #define WM8955_RATES SNDRV_PCM_RATE_8000_96000
  715. #define WM8955_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  716. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  717. static struct snd_soc_dai_ops wm8955_dai_ops = {
  718. .set_sysclk = wm8955_set_sysclk,
  719. .set_fmt = wm8955_set_fmt,
  720. .hw_params = wm8955_hw_params,
  721. .digital_mute = wm8955_digital_mute,
  722. };
  723. static struct snd_soc_dai_driver wm8955_dai = {
  724. .name = "wm8955-hifi",
  725. .playback = {
  726. .stream_name = "Playback",
  727. .channels_min = 2,
  728. .channels_max = 2,
  729. .rates = WM8955_RATES,
  730. .formats = WM8955_FORMATS,
  731. },
  732. .ops = &wm8955_dai_ops,
  733. };
  734. #ifdef CONFIG_PM
  735. static int wm8955_suspend(struct snd_soc_codec *codec, pm_message_t state)
  736. {
  737. wm8955_set_bias_level(codec, SND_SOC_BIAS_OFF);
  738. return 0;
  739. }
  740. static int wm8955_resume(struct snd_soc_codec *codec)
  741. {
  742. wm8955_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  743. return 0;
  744. }
  745. #else
  746. #define wm8955_suspend NULL
  747. #define wm8955_resume NULL
  748. #endif
  749. static int wm8955_probe(struct snd_soc_codec *codec)
  750. {
  751. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  752. struct wm8955_pdata *pdata = dev_get_platdata(codec->dev);
  753. int ret, i;
  754. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8955->control_type);
  755. if (ret != 0) {
  756. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  757. return ret;
  758. }
  759. for (i = 0; i < ARRAY_SIZE(wm8955->supplies); i++)
  760. wm8955->supplies[i].supply = wm8955_supply_names[i];
  761. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8955->supplies),
  762. wm8955->supplies);
  763. if (ret != 0) {
  764. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  765. return ret;
  766. }
  767. ret = regulator_bulk_enable(ARRAY_SIZE(wm8955->supplies),
  768. wm8955->supplies);
  769. if (ret != 0) {
  770. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  771. goto err_get;
  772. }
  773. ret = wm8955_reset(codec);
  774. if (ret < 0) {
  775. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  776. goto err_enable;
  777. }
  778. /* Change some default settings - latch VU and enable ZC */
  779. wm8955->reg_cache[WM8955_LEFT_DAC_VOLUME] |= WM8955_LDVU;
  780. wm8955->reg_cache[WM8955_RIGHT_DAC_VOLUME] |= WM8955_RDVU;
  781. wm8955->reg_cache[WM8955_LOUT1_VOLUME] |= WM8955_LO1VU | WM8955_LO1ZC;
  782. wm8955->reg_cache[WM8955_ROUT1_VOLUME] |= WM8955_RO1VU | WM8955_RO1ZC;
  783. wm8955->reg_cache[WM8955_LOUT2_VOLUME] |= WM8955_LO2VU | WM8955_LO2ZC;
  784. wm8955->reg_cache[WM8955_ROUT2_VOLUME] |= WM8955_RO2VU | WM8955_RO2ZC;
  785. wm8955->reg_cache[WM8955_MONOOUT_VOLUME] |= WM8955_MOZC;
  786. /* Also enable adaptive bass boost by default */
  787. wm8955->reg_cache[WM8955_BASS_CONTROL] |= WM8955_BB;
  788. /* Set platform data values */
  789. if (pdata) {
  790. if (pdata->out2_speaker)
  791. wm8955->reg_cache[WM8955_ADDITIONAL_CONTROL_2]
  792. |= WM8955_ROUT2INV;
  793. if (pdata->monoin_diff)
  794. wm8955->reg_cache[WM8955_MONO_OUT_MIX_1]
  795. |= WM8955_DMEN;
  796. }
  797. wm8955_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  798. /* Bias level configuration will have done an extra enable */
  799. regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
  800. wm8955_add_widgets(codec);
  801. return 0;
  802. err_enable:
  803. regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
  804. err_get:
  805. regulator_bulk_free(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
  806. return ret;
  807. }
  808. static int wm8955_remove(struct snd_soc_codec *codec)
  809. {
  810. struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
  811. wm8955_set_bias_level(codec, SND_SOC_BIAS_OFF);
  812. regulator_bulk_free(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
  813. return 0;
  814. }
  815. static struct snd_soc_codec_driver soc_codec_dev_wm8955 = {
  816. .probe = wm8955_probe,
  817. .remove = wm8955_remove,
  818. .suspend = wm8955_suspend,
  819. .resume = wm8955_resume,
  820. .set_bias_level = wm8955_set_bias_level,
  821. .reg_cache_size = ARRAY_SIZE(wm8955_reg),
  822. .reg_word_size = sizeof(u16),
  823. .reg_cache_default = wm8955_reg,
  824. };
  825. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  826. static __devinit int wm8955_i2c_probe(struct i2c_client *i2c,
  827. const struct i2c_device_id *id)
  828. {
  829. struct wm8955_priv *wm8955;
  830. int ret;
  831. wm8955 = kzalloc(sizeof(struct wm8955_priv), GFP_KERNEL);
  832. if (wm8955 == NULL)
  833. return -ENOMEM;
  834. i2c_set_clientdata(i2c, wm8955);
  835. ret = snd_soc_register_codec(&i2c->dev,
  836. &soc_codec_dev_wm8955, &wm8955_dai, 1);
  837. if (ret < 0)
  838. kfree(wm8955);
  839. return ret;
  840. }
  841. static __devexit int wm8955_i2c_remove(struct i2c_client *client)
  842. {
  843. snd_soc_unregister_codec(&client->dev);
  844. kfree(i2c_get_clientdata(client));
  845. return 0;
  846. }
  847. static const struct i2c_device_id wm8955_i2c_id[] = {
  848. { "wm8955", 0 },
  849. { }
  850. };
  851. MODULE_DEVICE_TABLE(i2c, wm8955_i2c_id);
  852. static struct i2c_driver wm8955_i2c_driver = {
  853. .driver = {
  854. .name = "wm8955-codec",
  855. .owner = THIS_MODULE,
  856. },
  857. .probe = wm8955_i2c_probe,
  858. .remove = __devexit_p(wm8955_i2c_remove),
  859. .id_table = wm8955_i2c_id,
  860. };
  861. #endif
  862. static int __init wm8955_modinit(void)
  863. {
  864. int ret = 0;
  865. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  866. ret = i2c_add_driver(&wm8955_i2c_driver);
  867. if (ret != 0) {
  868. printk(KERN_ERR "Failed to register WM8955 I2C driver: %d\n",
  869. ret);
  870. }
  871. #endif
  872. return ret;
  873. }
  874. module_init(wm8955_modinit);
  875. static void __exit wm8955_exit(void)
  876. {
  877. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  878. i2c_del_driver(&wm8955_i2c_driver);
  879. #endif
  880. }
  881. module_exit(wm8955_exit);
  882. MODULE_DESCRIPTION("ASoC WM8955 driver");
  883. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  884. MODULE_LICENSE("GPL");