wm8955.c 28 KB

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