wm8580.c 25 KB

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