wm8580.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. * wm8580.c -- WM8580 ALSA Soc Audio driver
  3. *
  4. * Copyright 2008, 2009 Wolfson Microelectronics PLC.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * Notes:
  12. * The WM8580 is a multichannel codec with S/PDIF support, featuring six
  13. * DAC channels and two ADC channels.
  14. *
  15. * Currently only the primary audio interface is supported - S/PDIF and
  16. * the secondary audio interfaces are not.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/pm.h>
  24. #include <linux/i2c.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/slab.h>
  27. #include <linux/of_device.h>
  28. #include <sound/core.h>
  29. #include <sound/pcm.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/soc.h>
  32. #include <sound/tlv.h>
  33. #include <sound/initval.h>
  34. #include <asm/div64.h>
  35. #include "wm8580.h"
  36. /* WM8580 register space */
  37. #define WM8580_PLLA1 0x00
  38. #define WM8580_PLLA2 0x01
  39. #define WM8580_PLLA3 0x02
  40. #define WM8580_PLLA4 0x03
  41. #define WM8580_PLLB1 0x04
  42. #define WM8580_PLLB2 0x05
  43. #define WM8580_PLLB3 0x06
  44. #define WM8580_PLLB4 0x07
  45. #define WM8580_CLKSEL 0x08
  46. #define WM8580_PAIF1 0x09
  47. #define WM8580_PAIF2 0x0A
  48. #define WM8580_SAIF1 0x0B
  49. #define WM8580_PAIF3 0x0C
  50. #define WM8580_PAIF4 0x0D
  51. #define WM8580_SAIF2 0x0E
  52. #define WM8580_DAC_CONTROL1 0x0F
  53. #define WM8580_DAC_CONTROL2 0x10
  54. #define WM8580_DAC_CONTROL3 0x11
  55. #define WM8580_DAC_CONTROL4 0x12
  56. #define WM8580_DAC_CONTROL5 0x13
  57. #define WM8580_DIGITAL_ATTENUATION_DACL1 0x14
  58. #define WM8580_DIGITAL_ATTENUATION_DACR1 0x15
  59. #define WM8580_DIGITAL_ATTENUATION_DACL2 0x16
  60. #define WM8580_DIGITAL_ATTENUATION_DACR2 0x17
  61. #define WM8580_DIGITAL_ATTENUATION_DACL3 0x18
  62. #define WM8580_DIGITAL_ATTENUATION_DACR3 0x19
  63. #define WM8580_MASTER_DIGITAL_ATTENUATION 0x1C
  64. #define WM8580_ADC_CONTROL1 0x1D
  65. #define WM8580_SPDTXCHAN0 0x1E
  66. #define WM8580_SPDTXCHAN1 0x1F
  67. #define WM8580_SPDTXCHAN2 0x20
  68. #define WM8580_SPDTXCHAN3 0x21
  69. #define WM8580_SPDTXCHAN4 0x22
  70. #define WM8580_SPDTXCHAN5 0x23
  71. #define WM8580_SPDMODE 0x24
  72. #define WM8580_INTMASK 0x25
  73. #define WM8580_GPO1 0x26
  74. #define WM8580_GPO2 0x27
  75. #define WM8580_GPO3 0x28
  76. #define WM8580_GPO4 0x29
  77. #define WM8580_GPO5 0x2A
  78. #define WM8580_INTSTAT 0x2B
  79. #define WM8580_SPDRXCHAN1 0x2C
  80. #define WM8580_SPDRXCHAN2 0x2D
  81. #define WM8580_SPDRXCHAN3 0x2E
  82. #define WM8580_SPDRXCHAN4 0x2F
  83. #define WM8580_SPDRXCHAN5 0x30
  84. #define WM8580_SPDSTAT 0x31
  85. #define WM8580_PWRDN1 0x32
  86. #define WM8580_PWRDN2 0x33
  87. #define WM8580_READBACK 0x34
  88. #define WM8580_RESET 0x35
  89. #define WM8580_MAX_REGISTER 0x35
  90. #define WM8580_DACOSR 0x40
  91. /* PLLB4 (register 7h) */
  92. #define WM8580_PLLB4_MCLKOUTSRC_MASK 0x60
  93. #define WM8580_PLLB4_MCLKOUTSRC_PLLA 0x20
  94. #define WM8580_PLLB4_MCLKOUTSRC_PLLB 0x40
  95. #define WM8580_PLLB4_MCLKOUTSRC_OSC 0x60
  96. #define WM8580_PLLB4_CLKOUTSRC_MASK 0x180
  97. #define WM8580_PLLB4_CLKOUTSRC_PLLACLK 0x080
  98. #define WM8580_PLLB4_CLKOUTSRC_PLLBCLK 0x100
  99. #define WM8580_PLLB4_CLKOUTSRC_OSCCLK 0x180
  100. /* CLKSEL (register 8h) */
  101. #define WM8580_CLKSEL_DAC_CLKSEL_MASK 0x03
  102. #define WM8580_CLKSEL_DAC_CLKSEL_PLLA 0x01
  103. #define WM8580_CLKSEL_DAC_CLKSEL_PLLB 0x02
  104. /* AIF control 1 (registers 9h-bh) */
  105. #define WM8580_AIF_RATE_MASK 0x7
  106. #define WM8580_AIF_BCLKSEL_MASK 0x18
  107. #define WM8580_AIF_MS 0x20
  108. #define WM8580_AIF_CLKSRC_MASK 0xc0
  109. #define WM8580_AIF_CLKSRC_PLLA 0x40
  110. #define WM8580_AIF_CLKSRC_PLLB 0x40
  111. #define WM8580_AIF_CLKSRC_MCLK 0xc0
  112. /* AIF control 2 (registers ch-eh) */
  113. #define WM8580_AIF_FMT_MASK 0x03
  114. #define WM8580_AIF_FMT_RIGHTJ 0x00
  115. #define WM8580_AIF_FMT_LEFTJ 0x01
  116. #define WM8580_AIF_FMT_I2S 0x02
  117. #define WM8580_AIF_FMT_DSP 0x03
  118. #define WM8580_AIF_LENGTH_MASK 0x0c
  119. #define WM8580_AIF_LENGTH_16 0x00
  120. #define WM8580_AIF_LENGTH_20 0x04
  121. #define WM8580_AIF_LENGTH_24 0x08
  122. #define WM8580_AIF_LENGTH_32 0x0c
  123. #define WM8580_AIF_LRP 0x10
  124. #define WM8580_AIF_BCP 0x20
  125. /* Powerdown Register 1 (register 32h) */
  126. #define WM8580_PWRDN1_PWDN 0x001
  127. #define WM8580_PWRDN1_ALLDACPD 0x040
  128. /* Powerdown Register 2 (register 33h) */
  129. #define WM8580_PWRDN2_OSSCPD 0x001
  130. #define WM8580_PWRDN2_PLLAPD 0x002
  131. #define WM8580_PWRDN2_PLLBPD 0x004
  132. #define WM8580_PWRDN2_SPDIFPD 0x008
  133. #define WM8580_PWRDN2_SPDIFTXD 0x010
  134. #define WM8580_PWRDN2_SPDIFRXD 0x020
  135. #define WM8580_DAC_CONTROL5_MUTEALL 0x10
  136. /*
  137. * wm8580 register cache
  138. * We can't read the WM8580 register space when we
  139. * are using 2 wire for device control, so we cache them instead.
  140. */
  141. static const u16 wm8580_reg[] = {
  142. 0x0121, 0x017e, 0x007d, 0x0014, /*R3*/
  143. 0x0121, 0x017e, 0x007d, 0x0194, /*R7*/
  144. 0x0010, 0x0002, 0x0002, 0x00c2, /*R11*/
  145. 0x0182, 0x0082, 0x000a, 0x0024, /*R15*/
  146. 0x0009, 0x0000, 0x00ff, 0x0000, /*R19*/
  147. 0x00ff, 0x00ff, 0x00ff, 0x00ff, /*R23*/
  148. 0x00ff, 0x00ff, 0x00ff, 0x00ff, /*R27*/
  149. 0x01f0, 0x0040, 0x0000, 0x0000, /*R31(0x1F)*/
  150. 0x0000, 0x0000, 0x0031, 0x000b, /*R35*/
  151. 0x0039, 0x0000, 0x0010, 0x0032, /*R39*/
  152. 0x0054, 0x0076, 0x0098, 0x0000, /*R43(0x2B)*/
  153. 0x0000, 0x0000, 0x0000, 0x0000, /*R47*/
  154. 0x0000, 0x0000, 0x005e, 0x003e, /*R51(0x33)*/
  155. 0x0000, 0x0000 /*R53*/
  156. };
  157. struct pll_state {
  158. unsigned int in;
  159. unsigned int out;
  160. };
  161. #define WM8580_NUM_SUPPLIES 3
  162. static const char *wm8580_supply_names[WM8580_NUM_SUPPLIES] = {
  163. "AVDD",
  164. "DVDD",
  165. "PVDD",
  166. };
  167. /* codec private data */
  168. struct wm8580_priv {
  169. enum snd_soc_control_type control_type;
  170. struct regulator_bulk_data supplies[WM8580_NUM_SUPPLIES];
  171. struct pll_state a;
  172. struct pll_state b;
  173. int sysclk[2];
  174. };
  175. static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
  176. static int wm8580_out_vu(struct snd_kcontrol *kcontrol,
  177. struct snd_ctl_elem_value *ucontrol)
  178. {
  179. struct soc_mixer_control *mc =
  180. (struct soc_mixer_control *)kcontrol->private_value;
  181. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  182. u16 *reg_cache = codec->reg_cache;
  183. unsigned int reg = mc->reg;
  184. unsigned int reg2 = mc->rreg;
  185. int ret;
  186. /* Clear the register cache so we write without VU set */
  187. reg_cache[reg] = 0;
  188. reg_cache[reg2] = 0;
  189. ret = snd_soc_put_volsw(kcontrol, ucontrol);
  190. if (ret < 0)
  191. return ret;
  192. /* Now write again with the volume update bit set */
  193. snd_soc_update_bits(codec, reg, 0x100, 0x100);
  194. snd_soc_update_bits(codec, reg2, 0x100, 0x100);
  195. return 0;
  196. }
  197. static const struct snd_kcontrol_new wm8580_snd_controls[] = {
  198. SOC_DOUBLE_R_EXT_TLV("DAC1 Playback Volume",
  199. WM8580_DIGITAL_ATTENUATION_DACL1,
  200. WM8580_DIGITAL_ATTENUATION_DACR1,
  201. 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv),
  202. SOC_DOUBLE_R_EXT_TLV("DAC2 Playback Volume",
  203. WM8580_DIGITAL_ATTENUATION_DACL2,
  204. WM8580_DIGITAL_ATTENUATION_DACR2,
  205. 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv),
  206. SOC_DOUBLE_R_EXT_TLV("DAC3 Playback Volume",
  207. WM8580_DIGITAL_ATTENUATION_DACL3,
  208. WM8580_DIGITAL_ATTENUATION_DACR3,
  209. 0, 0xff, 0, snd_soc_get_volsw, wm8580_out_vu, dac_tlv),
  210. SOC_SINGLE("DAC1 Deemphasis Switch", WM8580_DAC_CONTROL3, 0, 1, 0),
  211. SOC_SINGLE("DAC2 Deemphasis Switch", WM8580_DAC_CONTROL3, 1, 1, 0),
  212. SOC_SINGLE("DAC3 Deemphasis Switch", WM8580_DAC_CONTROL3, 2, 1, 0),
  213. SOC_DOUBLE("DAC1 Invert Switch", WM8580_DAC_CONTROL4, 0, 1, 1, 0),
  214. SOC_DOUBLE("DAC2 Invert Switch", WM8580_DAC_CONTROL4, 2, 3, 1, 0),
  215. SOC_DOUBLE("DAC3 Invert Switch", WM8580_DAC_CONTROL4, 4, 5, 1, 0),
  216. SOC_SINGLE("DAC ZC Switch", WM8580_DAC_CONTROL5, 5, 1, 0),
  217. SOC_SINGLE("DAC1 Switch", WM8580_DAC_CONTROL5, 0, 1, 1),
  218. SOC_SINGLE("DAC2 Switch", WM8580_DAC_CONTROL5, 1, 1, 1),
  219. SOC_SINGLE("DAC3 Switch", WM8580_DAC_CONTROL5, 2, 1, 1),
  220. SOC_DOUBLE("Capture Switch", WM8580_ADC_CONTROL1, 0, 1, 1, 1),
  221. SOC_SINGLE("Capture High-Pass Filter Switch", WM8580_ADC_CONTROL1, 4, 1, 0),
  222. };
  223. static const struct snd_soc_dapm_widget wm8580_dapm_widgets[] = {
  224. SND_SOC_DAPM_DAC("DAC1", "Playback", WM8580_PWRDN1, 2, 1),
  225. SND_SOC_DAPM_DAC("DAC2", "Playback", WM8580_PWRDN1, 3, 1),
  226. SND_SOC_DAPM_DAC("DAC3", "Playback", WM8580_PWRDN1, 4, 1),
  227. SND_SOC_DAPM_OUTPUT("VOUT1L"),
  228. SND_SOC_DAPM_OUTPUT("VOUT1R"),
  229. SND_SOC_DAPM_OUTPUT("VOUT2L"),
  230. SND_SOC_DAPM_OUTPUT("VOUT2R"),
  231. SND_SOC_DAPM_OUTPUT("VOUT3L"),
  232. SND_SOC_DAPM_OUTPUT("VOUT3R"),
  233. SND_SOC_DAPM_ADC("ADC", "Capture", WM8580_PWRDN1, 1, 1),
  234. SND_SOC_DAPM_INPUT("AINL"),
  235. SND_SOC_DAPM_INPUT("AINR"),
  236. };
  237. static const struct snd_soc_dapm_route wm8580_dapm_routes[] = {
  238. { "VOUT1L", NULL, "DAC1" },
  239. { "VOUT1R", NULL, "DAC1" },
  240. { "VOUT2L", NULL, "DAC2" },
  241. { "VOUT2R", NULL, "DAC2" },
  242. { "VOUT3L", NULL, "DAC3" },
  243. { "VOUT3R", NULL, "DAC3" },
  244. { "ADC", NULL, "AINL" },
  245. { "ADC", NULL, "AINR" },
  246. };
  247. /* PLL divisors */
  248. struct _pll_div {
  249. u32 prescale:1;
  250. u32 postscale:1;
  251. u32 freqmode:2;
  252. u32 n:4;
  253. u32 k:24;
  254. };
  255. /* The size in bits of the pll divide */
  256. #define FIXED_PLL_SIZE (1 << 22)
  257. /* PLL rate to output rate divisions */
  258. static struct {
  259. unsigned int div;
  260. unsigned int freqmode;
  261. unsigned int postscale;
  262. } post_table[] = {
  263. { 2, 0, 0 },
  264. { 4, 0, 1 },
  265. { 4, 1, 0 },
  266. { 8, 1, 1 },
  267. { 8, 2, 0 },
  268. { 16, 2, 1 },
  269. { 12, 3, 0 },
  270. { 24, 3, 1 }
  271. };
  272. static int pll_factors(struct _pll_div *pll_div, unsigned int target,
  273. unsigned int source)
  274. {
  275. u64 Kpart;
  276. unsigned int K, Ndiv, Nmod;
  277. int i;
  278. pr_debug("wm8580: PLL %uHz->%uHz\n", source, target);
  279. /* Scale the output frequency up; the PLL should run in the
  280. * region of 90-100MHz.
  281. */
  282. for (i = 0; i < ARRAY_SIZE(post_table); i++) {
  283. if (target * post_table[i].div >= 90000000 &&
  284. target * post_table[i].div <= 100000000) {
  285. pll_div->freqmode = post_table[i].freqmode;
  286. pll_div->postscale = post_table[i].postscale;
  287. target *= post_table[i].div;
  288. break;
  289. }
  290. }
  291. if (i == ARRAY_SIZE(post_table)) {
  292. printk(KERN_ERR "wm8580: Unable to scale output frequency "
  293. "%u\n", target);
  294. return -EINVAL;
  295. }
  296. Ndiv = target / source;
  297. if (Ndiv < 5) {
  298. source /= 2;
  299. pll_div->prescale = 1;
  300. Ndiv = target / source;
  301. } else
  302. pll_div->prescale = 0;
  303. if ((Ndiv < 5) || (Ndiv > 13)) {
  304. printk(KERN_ERR
  305. "WM8580 N=%u outside supported range\n", Ndiv);
  306. return -EINVAL;
  307. }
  308. pll_div->n = Ndiv;
  309. Nmod = target % source;
  310. Kpart = FIXED_PLL_SIZE * (long long)Nmod;
  311. do_div(Kpart, source);
  312. K = Kpart & 0xFFFFFFFF;
  313. pll_div->k = K;
  314. pr_debug("PLL %x.%x prescale %d freqmode %d postscale %d\n",
  315. pll_div->n, pll_div->k, pll_div->prescale, pll_div->freqmode,
  316. pll_div->postscale);
  317. return 0;
  318. }
  319. static int wm8580_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  320. int source, unsigned int freq_in, unsigned int freq_out)
  321. {
  322. int offset;
  323. struct snd_soc_codec *codec = codec_dai->codec;
  324. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  325. struct pll_state *state;
  326. struct _pll_div pll_div;
  327. unsigned int reg;
  328. unsigned int pwr_mask;
  329. int ret;
  330. /* GCC isn't able to work out the ifs below for initialising/using
  331. * pll_div so suppress warnings.
  332. */
  333. memset(&pll_div, 0, sizeof(pll_div));
  334. switch (pll_id) {
  335. case WM8580_PLLA:
  336. state = &wm8580->a;
  337. offset = 0;
  338. pwr_mask = WM8580_PWRDN2_PLLAPD;
  339. break;
  340. case WM8580_PLLB:
  341. state = &wm8580->b;
  342. offset = 4;
  343. pwr_mask = WM8580_PWRDN2_PLLBPD;
  344. break;
  345. default:
  346. return -ENODEV;
  347. }
  348. if (freq_in && freq_out) {
  349. ret = pll_factors(&pll_div, freq_out, freq_in);
  350. if (ret != 0)
  351. return ret;
  352. }
  353. state->in = freq_in;
  354. state->out = freq_out;
  355. /* Always disable the PLL - it is not safe to leave it running
  356. * while reprogramming it.
  357. */
  358. snd_soc_update_bits(codec, WM8580_PWRDN2, pwr_mask, pwr_mask);
  359. if (!freq_in || !freq_out)
  360. return 0;
  361. snd_soc_write(codec, WM8580_PLLA1 + offset, pll_div.k & 0x1ff);
  362. snd_soc_write(codec, WM8580_PLLA2 + offset, (pll_div.k >> 9) & 0x1ff);
  363. snd_soc_write(codec, WM8580_PLLA3 + offset,
  364. (pll_div.k >> 18 & 0xf) | (pll_div.n << 4));
  365. reg = snd_soc_read(codec, WM8580_PLLA4 + offset);
  366. reg &= ~0x1b;
  367. reg |= pll_div.prescale | pll_div.postscale << 1 |
  368. pll_div.freqmode << 3;
  369. snd_soc_write(codec, WM8580_PLLA4 + offset, reg);
  370. /* All done, turn it on */
  371. snd_soc_update_bits(codec, WM8580_PWRDN2, pwr_mask, 0);
  372. return 0;
  373. }
  374. static const int wm8580_sysclk_ratios[] = {
  375. 128, 192, 256, 384, 512, 768, 1152,
  376. };
  377. /*
  378. * Set PCM DAI bit size and sample rate.
  379. */
  380. static int wm8580_paif_hw_params(struct snd_pcm_substream *substream,
  381. struct snd_pcm_hw_params *params,
  382. struct snd_soc_dai *dai)
  383. {
  384. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  385. struct snd_soc_codec *codec = rtd->codec;
  386. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  387. u16 paifa = 0;
  388. u16 paifb = 0;
  389. int i, ratio, osr;
  390. /* bit size */
  391. switch (params_format(params)) {
  392. case SNDRV_PCM_FORMAT_S16_LE:
  393. paifa |= 0x8;
  394. break;
  395. case SNDRV_PCM_FORMAT_S20_3LE:
  396. paifa |= 0x0;
  397. paifb |= WM8580_AIF_LENGTH_20;
  398. break;
  399. case SNDRV_PCM_FORMAT_S24_LE:
  400. paifa |= 0x0;
  401. paifb |= WM8580_AIF_LENGTH_24;
  402. break;
  403. case SNDRV_PCM_FORMAT_S32_LE:
  404. paifa |= 0x0;
  405. paifb |= WM8580_AIF_LENGTH_32;
  406. break;
  407. default:
  408. return -EINVAL;
  409. }
  410. /* Look up the SYSCLK ratio; accept only exact matches */
  411. ratio = wm8580->sysclk[dai->driver->id] / params_rate(params);
  412. for (i = 0; i < ARRAY_SIZE(wm8580_sysclk_ratios); i++)
  413. if (ratio == wm8580_sysclk_ratios[i])
  414. break;
  415. if (i == ARRAY_SIZE(wm8580_sysclk_ratios)) {
  416. dev_err(codec->dev, "Invalid clock ratio %d/%d\n",
  417. wm8580->sysclk[dai->driver->id], params_rate(params));
  418. return -EINVAL;
  419. }
  420. paifa |= i;
  421. dev_dbg(codec->dev, "Running at %dfs with %dHz clock\n",
  422. wm8580_sysclk_ratios[i], wm8580->sysclk[dai->driver->id]);
  423. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  424. switch (ratio) {
  425. case 128:
  426. case 192:
  427. osr = WM8580_DACOSR;
  428. dev_dbg(codec->dev, "Selecting 64x OSR\n");
  429. break;
  430. default:
  431. osr = 0;
  432. dev_dbg(codec->dev, "Selecting 128x OSR\n");
  433. break;
  434. }
  435. snd_soc_update_bits(codec, WM8580_PAIF3, WM8580_DACOSR, osr);
  436. }
  437. snd_soc_update_bits(codec, WM8580_PAIF1 + dai->driver->id,
  438. WM8580_AIF_RATE_MASK | WM8580_AIF_BCLKSEL_MASK,
  439. paifa);
  440. snd_soc_update_bits(codec, WM8580_PAIF3 + dai->driver->id,
  441. WM8580_AIF_LENGTH_MASK, paifb);
  442. return 0;
  443. }
  444. static int wm8580_set_paif_dai_fmt(struct snd_soc_dai *codec_dai,
  445. unsigned int fmt)
  446. {
  447. struct snd_soc_codec *codec = codec_dai->codec;
  448. unsigned int aifa;
  449. unsigned int aifb;
  450. int can_invert_lrclk;
  451. aifa = snd_soc_read(codec, WM8580_PAIF1 + codec_dai->driver->id);
  452. aifb = snd_soc_read(codec, WM8580_PAIF3 + codec_dai->driver->id);
  453. aifb &= ~(WM8580_AIF_FMT_MASK | WM8580_AIF_LRP | WM8580_AIF_BCP);
  454. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  455. case SND_SOC_DAIFMT_CBS_CFS:
  456. aifa &= ~WM8580_AIF_MS;
  457. break;
  458. case SND_SOC_DAIFMT_CBM_CFM:
  459. aifa |= WM8580_AIF_MS;
  460. break;
  461. default:
  462. return -EINVAL;
  463. }
  464. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  465. case SND_SOC_DAIFMT_I2S:
  466. can_invert_lrclk = 1;
  467. aifb |= WM8580_AIF_FMT_I2S;
  468. break;
  469. case SND_SOC_DAIFMT_RIGHT_J:
  470. can_invert_lrclk = 1;
  471. aifb |= WM8580_AIF_FMT_RIGHTJ;
  472. break;
  473. case SND_SOC_DAIFMT_LEFT_J:
  474. can_invert_lrclk = 1;
  475. aifb |= WM8580_AIF_FMT_LEFTJ;
  476. break;
  477. case SND_SOC_DAIFMT_DSP_A:
  478. can_invert_lrclk = 0;
  479. aifb |= WM8580_AIF_FMT_DSP;
  480. break;
  481. case SND_SOC_DAIFMT_DSP_B:
  482. can_invert_lrclk = 0;
  483. aifb |= WM8580_AIF_FMT_DSP;
  484. aifb |= WM8580_AIF_LRP;
  485. break;
  486. default:
  487. return -EINVAL;
  488. }
  489. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  490. case SND_SOC_DAIFMT_NB_NF:
  491. break;
  492. case SND_SOC_DAIFMT_IB_IF:
  493. if (!can_invert_lrclk)
  494. return -EINVAL;
  495. aifb |= WM8580_AIF_BCP;
  496. aifb |= WM8580_AIF_LRP;
  497. break;
  498. case SND_SOC_DAIFMT_IB_NF:
  499. aifb |= WM8580_AIF_BCP;
  500. break;
  501. case SND_SOC_DAIFMT_NB_IF:
  502. if (!can_invert_lrclk)
  503. return -EINVAL;
  504. aifb |= WM8580_AIF_LRP;
  505. break;
  506. default:
  507. return -EINVAL;
  508. }
  509. snd_soc_write(codec, WM8580_PAIF1 + codec_dai->driver->id, aifa);
  510. snd_soc_write(codec, WM8580_PAIF3 + codec_dai->driver->id, aifb);
  511. return 0;
  512. }
  513. static int wm8580_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  514. int div_id, int div)
  515. {
  516. struct snd_soc_codec *codec = codec_dai->codec;
  517. unsigned int reg;
  518. switch (div_id) {
  519. case WM8580_MCLK:
  520. reg = snd_soc_read(codec, WM8580_PLLB4);
  521. reg &= ~WM8580_PLLB4_MCLKOUTSRC_MASK;
  522. switch (div) {
  523. case WM8580_CLKSRC_MCLK:
  524. /* Input */
  525. break;
  526. case WM8580_CLKSRC_PLLA:
  527. reg |= WM8580_PLLB4_MCLKOUTSRC_PLLA;
  528. break;
  529. case WM8580_CLKSRC_PLLB:
  530. reg |= WM8580_PLLB4_MCLKOUTSRC_PLLB;
  531. break;
  532. case WM8580_CLKSRC_OSC:
  533. reg |= WM8580_PLLB4_MCLKOUTSRC_OSC;
  534. break;
  535. default:
  536. return -EINVAL;
  537. }
  538. snd_soc_write(codec, WM8580_PLLB4, reg);
  539. break;
  540. case WM8580_CLKOUTSRC:
  541. reg = snd_soc_read(codec, WM8580_PLLB4);
  542. reg &= ~WM8580_PLLB4_CLKOUTSRC_MASK;
  543. switch (div) {
  544. case WM8580_CLKSRC_NONE:
  545. break;
  546. case WM8580_CLKSRC_PLLA:
  547. reg |= WM8580_PLLB4_CLKOUTSRC_PLLACLK;
  548. break;
  549. case WM8580_CLKSRC_PLLB:
  550. reg |= WM8580_PLLB4_CLKOUTSRC_PLLBCLK;
  551. break;
  552. case WM8580_CLKSRC_OSC:
  553. reg |= WM8580_PLLB4_CLKOUTSRC_OSCCLK;
  554. break;
  555. default:
  556. return -EINVAL;
  557. }
  558. snd_soc_write(codec, WM8580_PLLB4, reg);
  559. break;
  560. default:
  561. return -EINVAL;
  562. }
  563. return 0;
  564. }
  565. static int wm8580_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  566. unsigned int freq, int dir)
  567. {
  568. struct snd_soc_codec *codec = dai->codec;
  569. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  570. int ret, sel, sel_mask, sel_shift;
  571. switch (dai->driver->id) {
  572. case WM8580_DAI_PAIFRX:
  573. sel_mask = 0x3;
  574. sel_shift = 0;
  575. break;
  576. case WM8580_DAI_PAIFTX:
  577. sel_mask = 0xc;
  578. sel_shift = 2;
  579. break;
  580. default:
  581. BUG_ON("Unknown DAI driver ID\n");
  582. return -EINVAL;
  583. }
  584. switch (clk_id) {
  585. case WM8580_CLKSRC_ADCMCLK:
  586. if (dai->driver->id != WM8580_DAI_PAIFTX)
  587. return -EINVAL;
  588. sel = 0 << sel_shift;
  589. break;
  590. case WM8580_CLKSRC_PLLA:
  591. sel = 1 << sel_shift;
  592. break;
  593. case WM8580_CLKSRC_PLLB:
  594. sel = 2 << sel_shift;
  595. break;
  596. case WM8580_CLKSRC_MCLK:
  597. sel = 3 << sel_shift;
  598. break;
  599. default:
  600. dev_err(codec->dev, "Unknown clock %d\n", clk_id);
  601. return -EINVAL;
  602. }
  603. /* We really should validate PLL settings but not yet */
  604. wm8580->sysclk[dai->driver->id] = freq;
  605. ret = snd_soc_update_bits(codec, WM8580_CLKSEL, sel_mask, sel);
  606. if (ret < 0)
  607. return ret;
  608. return 0;
  609. }
  610. static int wm8580_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  611. {
  612. struct snd_soc_codec *codec = codec_dai->codec;
  613. unsigned int reg;
  614. reg = snd_soc_read(codec, WM8580_DAC_CONTROL5);
  615. if (mute)
  616. reg |= WM8580_DAC_CONTROL5_MUTEALL;
  617. else
  618. reg &= ~WM8580_DAC_CONTROL5_MUTEALL;
  619. snd_soc_write(codec, WM8580_DAC_CONTROL5, reg);
  620. return 0;
  621. }
  622. static int wm8580_set_bias_level(struct snd_soc_codec *codec,
  623. enum snd_soc_bias_level level)
  624. {
  625. switch (level) {
  626. case SND_SOC_BIAS_ON:
  627. case SND_SOC_BIAS_PREPARE:
  628. break;
  629. case SND_SOC_BIAS_STANDBY:
  630. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  631. /* Power up and get individual control of the DACs */
  632. snd_soc_update_bits(codec, WM8580_PWRDN1,
  633. WM8580_PWRDN1_PWDN |
  634. WM8580_PWRDN1_ALLDACPD, 0);
  635. /* Make VMID high impedance */
  636. snd_soc_update_bits(codec, WM8580_ADC_CONTROL1,
  637. 0x100, 0);
  638. }
  639. break;
  640. case SND_SOC_BIAS_OFF:
  641. snd_soc_update_bits(codec, WM8580_PWRDN1,
  642. WM8580_PWRDN1_PWDN, WM8580_PWRDN1_PWDN);
  643. break;
  644. }
  645. codec->dapm.bias_level = level;
  646. return 0;
  647. }
  648. #define WM8580_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  649. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  650. static const struct snd_soc_dai_ops wm8580_dai_ops_playback = {
  651. .set_sysclk = wm8580_set_sysclk,
  652. .hw_params = wm8580_paif_hw_params,
  653. .set_fmt = wm8580_set_paif_dai_fmt,
  654. .set_clkdiv = wm8580_set_dai_clkdiv,
  655. .set_pll = wm8580_set_dai_pll,
  656. .digital_mute = wm8580_digital_mute,
  657. };
  658. static const struct snd_soc_dai_ops wm8580_dai_ops_capture = {
  659. .set_sysclk = wm8580_set_sysclk,
  660. .hw_params = wm8580_paif_hw_params,
  661. .set_fmt = wm8580_set_paif_dai_fmt,
  662. .set_clkdiv = wm8580_set_dai_clkdiv,
  663. .set_pll = wm8580_set_dai_pll,
  664. };
  665. static struct snd_soc_dai_driver wm8580_dai[] = {
  666. {
  667. .name = "wm8580-hifi-playback",
  668. .id = WM8580_DAI_PAIFRX,
  669. .playback = {
  670. .stream_name = "Playback",
  671. .channels_min = 1,
  672. .channels_max = 6,
  673. .rates = SNDRV_PCM_RATE_8000_192000,
  674. .formats = WM8580_FORMATS,
  675. },
  676. .ops = &wm8580_dai_ops_playback,
  677. },
  678. {
  679. .name = "wm8580-hifi-capture",
  680. .id = WM8580_DAI_PAIFTX,
  681. .capture = {
  682. .stream_name = "Capture",
  683. .channels_min = 2,
  684. .channels_max = 2,
  685. .rates = SNDRV_PCM_RATE_8000_192000,
  686. .formats = WM8580_FORMATS,
  687. },
  688. .ops = &wm8580_dai_ops_capture,
  689. },
  690. };
  691. static int wm8580_probe(struct snd_soc_codec *codec)
  692. {
  693. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  694. int ret = 0,i;
  695. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8580->control_type);
  696. if (ret < 0) {
  697. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  698. return ret;
  699. }
  700. for (i = 0; i < ARRAY_SIZE(wm8580->supplies); i++)
  701. wm8580->supplies[i].supply = wm8580_supply_names[i];
  702. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8580->supplies),
  703. wm8580->supplies);
  704. if (ret != 0) {
  705. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  706. return ret;
  707. }
  708. ret = regulator_bulk_enable(ARRAY_SIZE(wm8580->supplies),
  709. wm8580->supplies);
  710. if (ret != 0) {
  711. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  712. goto err_regulator_get;
  713. }
  714. /* Get the codec into a known state */
  715. ret = snd_soc_write(codec, WM8580_RESET, 0);
  716. if (ret != 0) {
  717. dev_err(codec->dev, "Failed to reset codec: %d\n", ret);
  718. goto err_regulator_enable;
  719. }
  720. wm8580_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  721. return 0;
  722. err_regulator_enable:
  723. regulator_bulk_disable(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
  724. err_regulator_get:
  725. regulator_bulk_free(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
  726. return ret;
  727. }
  728. /* power down chip */
  729. static int wm8580_remove(struct snd_soc_codec *codec)
  730. {
  731. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  732. wm8580_set_bias_level(codec, SND_SOC_BIAS_OFF);
  733. regulator_bulk_disable(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
  734. regulator_bulk_free(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
  735. return 0;
  736. }
  737. static struct snd_soc_codec_driver soc_codec_dev_wm8580 = {
  738. .probe = wm8580_probe,
  739. .remove = wm8580_remove,
  740. .set_bias_level = wm8580_set_bias_level,
  741. .reg_cache_size = ARRAY_SIZE(wm8580_reg),
  742. .reg_word_size = sizeof(u16),
  743. .reg_cache_default = wm8580_reg,
  744. .controls = wm8580_snd_controls,
  745. .num_controls = ARRAY_SIZE(wm8580_snd_controls),
  746. .dapm_widgets = wm8580_dapm_widgets,
  747. .num_dapm_widgets = ARRAY_SIZE(wm8580_dapm_widgets),
  748. .dapm_routes = wm8580_dapm_routes,
  749. .num_dapm_routes = ARRAY_SIZE(wm8580_dapm_routes),
  750. };
  751. static const struct of_device_id wm8580_of_match[] = {
  752. { .compatible = "wlf,wm8580" },
  753. { },
  754. };
  755. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  756. static int wm8580_i2c_probe(struct i2c_client *i2c,
  757. const struct i2c_device_id *id)
  758. {
  759. struct wm8580_priv *wm8580;
  760. int ret;
  761. wm8580 = kzalloc(sizeof(struct wm8580_priv), GFP_KERNEL);
  762. if (wm8580 == NULL)
  763. return -ENOMEM;
  764. i2c_set_clientdata(i2c, wm8580);
  765. wm8580->control_type = SND_SOC_I2C;
  766. ret = snd_soc_register_codec(&i2c->dev,
  767. &soc_codec_dev_wm8580, wm8580_dai, ARRAY_SIZE(wm8580_dai));
  768. if (ret < 0)
  769. kfree(wm8580);
  770. return ret;
  771. }
  772. static int wm8580_i2c_remove(struct i2c_client *client)
  773. {
  774. snd_soc_unregister_codec(&client->dev);
  775. kfree(i2c_get_clientdata(client));
  776. return 0;
  777. }
  778. static const struct i2c_device_id wm8580_i2c_id[] = {
  779. { "wm8580", 0 },
  780. { }
  781. };
  782. MODULE_DEVICE_TABLE(i2c, wm8580_i2c_id);
  783. static struct i2c_driver wm8580_i2c_driver = {
  784. .driver = {
  785. .name = "wm8580",
  786. .owner = THIS_MODULE,
  787. .of_match_table = wm8580_of_match,
  788. },
  789. .probe = wm8580_i2c_probe,
  790. .remove = wm8580_i2c_remove,
  791. .id_table = wm8580_i2c_id,
  792. };
  793. #endif
  794. static int __init wm8580_modinit(void)
  795. {
  796. int ret = 0;
  797. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  798. ret = i2c_add_driver(&wm8580_i2c_driver);
  799. if (ret != 0) {
  800. pr_err("Failed to register WM8580 I2C driver: %d\n", ret);
  801. }
  802. #endif
  803. return ret;
  804. }
  805. module_init(wm8580_modinit);
  806. static void __exit wm8580_exit(void)
  807. {
  808. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  809. i2c_del_driver(&wm8580_i2c_driver);
  810. #endif
  811. }
  812. module_exit(wm8580_exit);
  813. MODULE_DESCRIPTION("ASoC WM8580 driver");
  814. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  815. MODULE_LICENSE("GPL");