wm8580.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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 <sound/core.h>
  29. #include <sound/pcm.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/soc.h>
  32. #include <sound/soc-dapm.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. 0x001c, 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. u16 reg_cache[WM8580_MAX_REGISTER + 1];
  173. struct pll_state a;
  174. struct pll_state b;
  175. int sysclk[2];
  176. };
  177. static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
  178. static int wm8580_out_vu(struct snd_kcontrol *kcontrol,
  179. struct snd_ctl_elem_value *ucontrol)
  180. {
  181. struct soc_mixer_control *mc =
  182. (struct soc_mixer_control *)kcontrol->private_value;
  183. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  184. u16 *reg_cache = codec->reg_cache;
  185. unsigned int reg = mc->reg;
  186. unsigned int reg2 = mc->rreg;
  187. int ret;
  188. /* Clear the register cache so we write without VU set */
  189. reg_cache[reg] = 0;
  190. reg_cache[reg2] = 0;
  191. ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
  192. if (ret < 0)
  193. return ret;
  194. /* Now write again with the volume update bit set */
  195. snd_soc_update_bits(codec, reg, 0x100, 0x100);
  196. snd_soc_update_bits(codec, reg2, 0x100, 0x100);
  197. return 0;
  198. }
  199. #define SOC_WM8580_OUT_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax, \
  200. xinvert, tlv_array) \
  201. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
  202. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
  203. SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  204. .tlv.p = (tlv_array), \
  205. .info = snd_soc_info_volsw_2r, \
  206. .get = snd_soc_get_volsw_2r, .put = wm8580_out_vu, \
  207. .private_value = (unsigned long)&(struct soc_mixer_control) \
  208. {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
  209. .max = xmax, .invert = xinvert} }
  210. static const struct snd_kcontrol_new wm8580_snd_controls[] = {
  211. SOC_WM8580_OUT_DOUBLE_R_TLV("DAC1 Playback Volume",
  212. WM8580_DIGITAL_ATTENUATION_DACL1,
  213. WM8580_DIGITAL_ATTENUATION_DACR1,
  214. 0, 0xff, 0, dac_tlv),
  215. SOC_WM8580_OUT_DOUBLE_R_TLV("DAC2 Playback Volume",
  216. WM8580_DIGITAL_ATTENUATION_DACL2,
  217. WM8580_DIGITAL_ATTENUATION_DACR2,
  218. 0, 0xff, 0, dac_tlv),
  219. SOC_WM8580_OUT_DOUBLE_R_TLV("DAC3 Playback Volume",
  220. WM8580_DIGITAL_ATTENUATION_DACL3,
  221. WM8580_DIGITAL_ATTENUATION_DACR3,
  222. 0, 0xff, 0, dac_tlv),
  223. SOC_SINGLE("DAC1 Deemphasis Switch", WM8580_DAC_CONTROL3, 0, 1, 0),
  224. SOC_SINGLE("DAC2 Deemphasis Switch", WM8580_DAC_CONTROL3, 1, 1, 0),
  225. SOC_SINGLE("DAC3 Deemphasis Switch", WM8580_DAC_CONTROL3, 2, 1, 0),
  226. SOC_DOUBLE("DAC1 Invert Switch", WM8580_DAC_CONTROL4, 0, 1, 1, 0),
  227. SOC_DOUBLE("DAC2 Invert Switch", WM8580_DAC_CONTROL4, 2, 3, 1, 0),
  228. SOC_DOUBLE("DAC3 Invert Switch", WM8580_DAC_CONTROL4, 4, 5, 1, 0),
  229. SOC_SINGLE("DAC ZC Switch", WM8580_DAC_CONTROL5, 5, 1, 0),
  230. SOC_SINGLE("DAC1 Switch", WM8580_DAC_CONTROL5, 0, 1, 1),
  231. SOC_SINGLE("DAC2 Switch", WM8580_DAC_CONTROL5, 1, 1, 1),
  232. SOC_SINGLE("DAC3 Switch", WM8580_DAC_CONTROL5, 2, 1, 1),
  233. SOC_DOUBLE("Capture Switch", WM8580_ADC_CONTROL1, 0, 1, 1, 1),
  234. SOC_SINGLE("Capture High-Pass Filter Switch", WM8580_ADC_CONTROL1, 4, 1, 0),
  235. };
  236. static const struct snd_soc_dapm_widget wm8580_dapm_widgets[] = {
  237. SND_SOC_DAPM_DAC("DAC1", "Playback", WM8580_PWRDN1, 2, 1),
  238. SND_SOC_DAPM_DAC("DAC2", "Playback", WM8580_PWRDN1, 3, 1),
  239. SND_SOC_DAPM_DAC("DAC3", "Playback", WM8580_PWRDN1, 4, 1),
  240. SND_SOC_DAPM_OUTPUT("VOUT1L"),
  241. SND_SOC_DAPM_OUTPUT("VOUT1R"),
  242. SND_SOC_DAPM_OUTPUT("VOUT2L"),
  243. SND_SOC_DAPM_OUTPUT("VOUT2R"),
  244. SND_SOC_DAPM_OUTPUT("VOUT3L"),
  245. SND_SOC_DAPM_OUTPUT("VOUT3R"),
  246. SND_SOC_DAPM_ADC("ADC", "Capture", WM8580_PWRDN1, 1, 1),
  247. SND_SOC_DAPM_INPUT("AINL"),
  248. SND_SOC_DAPM_INPUT("AINR"),
  249. };
  250. static const struct snd_soc_dapm_route audio_map[] = {
  251. { "VOUT1L", NULL, "DAC1" },
  252. { "VOUT1R", NULL, "DAC1" },
  253. { "VOUT2L", NULL, "DAC2" },
  254. { "VOUT2R", NULL, "DAC2" },
  255. { "VOUT3L", NULL, "DAC3" },
  256. { "VOUT3R", NULL, "DAC3" },
  257. { "ADC", NULL, "AINL" },
  258. { "ADC", NULL, "AINR" },
  259. };
  260. static int wm8580_add_widgets(struct snd_soc_codec *codec)
  261. {
  262. snd_soc_dapm_new_controls(codec, wm8580_dapm_widgets,
  263. ARRAY_SIZE(wm8580_dapm_widgets));
  264. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  265. return 0;
  266. }
  267. /* PLL divisors */
  268. struct _pll_div {
  269. u32 prescale:1;
  270. u32 postscale:1;
  271. u32 freqmode:2;
  272. u32 n:4;
  273. u32 k:24;
  274. };
  275. /* The size in bits of the pll divide */
  276. #define FIXED_PLL_SIZE (1 << 22)
  277. /* PLL rate to output rate divisions */
  278. static struct {
  279. unsigned int div;
  280. unsigned int freqmode;
  281. unsigned int postscale;
  282. } post_table[] = {
  283. { 2, 0, 0 },
  284. { 4, 0, 1 },
  285. { 4, 1, 0 },
  286. { 8, 1, 1 },
  287. { 8, 2, 0 },
  288. { 16, 2, 1 },
  289. { 12, 3, 0 },
  290. { 24, 3, 1 }
  291. };
  292. static int pll_factors(struct _pll_div *pll_div, unsigned int target,
  293. unsigned int source)
  294. {
  295. u64 Kpart;
  296. unsigned int K, Ndiv, Nmod;
  297. int i;
  298. pr_debug("wm8580: PLL %uHz->%uHz\n", source, target);
  299. /* Scale the output frequency up; the PLL should run in the
  300. * region of 90-100MHz.
  301. */
  302. for (i = 0; i < ARRAY_SIZE(post_table); i++) {
  303. if (target * post_table[i].div >= 90000000 &&
  304. target * post_table[i].div <= 100000000) {
  305. pll_div->freqmode = post_table[i].freqmode;
  306. pll_div->postscale = post_table[i].postscale;
  307. target *= post_table[i].div;
  308. break;
  309. }
  310. }
  311. if (i == ARRAY_SIZE(post_table)) {
  312. printk(KERN_ERR "wm8580: Unable to scale output frequency "
  313. "%u\n", target);
  314. return -EINVAL;
  315. }
  316. Ndiv = target / source;
  317. if (Ndiv < 5) {
  318. source /= 2;
  319. pll_div->prescale = 1;
  320. Ndiv = target / source;
  321. } else
  322. pll_div->prescale = 0;
  323. if ((Ndiv < 5) || (Ndiv > 13)) {
  324. printk(KERN_ERR
  325. "WM8580 N=%u outside supported range\n", Ndiv);
  326. return -EINVAL;
  327. }
  328. pll_div->n = Ndiv;
  329. Nmod = target % source;
  330. Kpart = FIXED_PLL_SIZE * (long long)Nmod;
  331. do_div(Kpart, source);
  332. K = Kpart & 0xFFFFFFFF;
  333. pll_div->k = K;
  334. pr_debug("PLL %x.%x prescale %d freqmode %d postscale %d\n",
  335. pll_div->n, pll_div->k, pll_div->prescale, pll_div->freqmode,
  336. pll_div->postscale);
  337. return 0;
  338. }
  339. static int wm8580_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  340. int source, unsigned int freq_in, unsigned int freq_out)
  341. {
  342. int offset;
  343. struct snd_soc_codec *codec = codec_dai->codec;
  344. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  345. struct pll_state *state;
  346. struct _pll_div pll_div;
  347. unsigned int reg;
  348. unsigned int pwr_mask;
  349. int ret;
  350. /* GCC isn't able to work out the ifs below for initialising/using
  351. * pll_div so suppress warnings.
  352. */
  353. memset(&pll_div, 0, sizeof(pll_div));
  354. switch (pll_id) {
  355. case WM8580_PLLA:
  356. state = &wm8580->a;
  357. offset = 0;
  358. pwr_mask = WM8580_PWRDN2_PLLAPD;
  359. break;
  360. case WM8580_PLLB:
  361. state = &wm8580->b;
  362. offset = 4;
  363. pwr_mask = WM8580_PWRDN2_PLLBPD;
  364. break;
  365. default:
  366. return -ENODEV;
  367. }
  368. if (freq_in && freq_out) {
  369. ret = pll_factors(&pll_div, freq_out, freq_in);
  370. if (ret != 0)
  371. return ret;
  372. }
  373. state->in = freq_in;
  374. state->out = freq_out;
  375. /* Always disable the PLL - it is not safe to leave it running
  376. * while reprogramming it.
  377. */
  378. reg = snd_soc_read(codec, WM8580_PWRDN2);
  379. snd_soc_write(codec, WM8580_PWRDN2, reg | pwr_mask);
  380. if (!freq_in || !freq_out)
  381. return 0;
  382. snd_soc_write(codec, WM8580_PLLA1 + offset, pll_div.k & 0x1ff);
  383. snd_soc_write(codec, WM8580_PLLA2 + offset, (pll_div.k >> 9) & 0x1ff);
  384. snd_soc_write(codec, WM8580_PLLA3 + offset,
  385. (pll_div.k >> 18 & 0xf) | (pll_div.n << 4));
  386. reg = snd_soc_read(codec, WM8580_PLLA4 + offset);
  387. reg &= ~0x1b;
  388. reg |= pll_div.prescale | pll_div.postscale << 1 |
  389. pll_div.freqmode << 3;
  390. snd_soc_write(codec, WM8580_PLLA4 + offset, reg);
  391. /* All done, turn it on */
  392. reg = snd_soc_read(codec, WM8580_PWRDN2);
  393. snd_soc_write(codec, WM8580_PWRDN2, reg & ~pwr_mask);
  394. return 0;
  395. }
  396. static const int wm8580_sysclk_ratios[] = {
  397. 128, 192, 256, 384, 512, 768, 1152,
  398. };
  399. /*
  400. * Set PCM DAI bit size and sample rate.
  401. */
  402. static int wm8580_paif_hw_params(struct snd_pcm_substream *substream,
  403. struct snd_pcm_hw_params *params,
  404. struct snd_soc_dai *dai)
  405. {
  406. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  407. struct snd_soc_codec *codec = rtd->codec;
  408. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  409. u16 paifa = 0;
  410. u16 paifb = 0;
  411. int i, ratio, osr;
  412. /* bit size */
  413. switch (params_format(params)) {
  414. case SNDRV_PCM_FORMAT_S16_LE:
  415. paifa |= 0x8;
  416. break;
  417. case SNDRV_PCM_FORMAT_S20_3LE:
  418. paifa |= 0x10;
  419. paifb |= WM8580_AIF_LENGTH_20;
  420. break;
  421. case SNDRV_PCM_FORMAT_S24_LE:
  422. paifa |= 0x10;
  423. paifb |= WM8580_AIF_LENGTH_24;
  424. break;
  425. case SNDRV_PCM_FORMAT_S32_LE:
  426. paifa |= 0x10;
  427. paifb |= WM8580_AIF_LENGTH_24;
  428. break;
  429. default:
  430. return -EINVAL;
  431. }
  432. /* Look up the SYSCLK ratio; accept only exact matches */
  433. ratio = wm8580->sysclk[dai->id] / params_rate(params);
  434. for (i = 0; i < ARRAY_SIZE(wm8580_sysclk_ratios); i++)
  435. if (ratio == wm8580_sysclk_ratios[i])
  436. break;
  437. if (i == ARRAY_SIZE(wm8580_sysclk_ratios)) {
  438. dev_err(codec->dev, "Invalid clock ratio %d/%d\n",
  439. wm8580->sysclk[dai->id], params_rate(params));
  440. return -EINVAL;
  441. }
  442. paifa |= i;
  443. dev_dbg(codec->dev, "Running at %dfs with %dHz clock\n",
  444. wm8580_sysclk_ratios[i], wm8580->sysclk[dai->driver->id]);
  445. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  446. switch (ratio) {
  447. case 128:
  448. case 192:
  449. osr = WM8580_DACOSR;
  450. dev_dbg(codec->dev, "Selecting 64x OSR\n");
  451. break;
  452. default:
  453. osr = 0;
  454. dev_dbg(codec->dev, "Selecting 128x OSR\n");
  455. break;
  456. }
  457. snd_soc_update_bits(codec, WM8580_PAIF3, WM8580_DACOSR, osr);
  458. }
  459. snd_soc_update_bits(codec, WM8580_PAIF1 + dai->driver->id,
  460. WM8580_AIF_RATE_MASK | WM8580_AIF_BCLKSEL_MASK,
  461. paifa);
  462. snd_soc_update_bits(codec, WM8580_PAIF3 + dai->driver->id,
  463. WM8580_AIF_LENGTH_MASK, paifb);
  464. return 0;
  465. }
  466. static int wm8580_set_paif_dai_fmt(struct snd_soc_dai *codec_dai,
  467. unsigned int fmt)
  468. {
  469. struct snd_soc_codec *codec = codec_dai->codec;
  470. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  471. unsigned int aifa;
  472. unsigned int aifb;
  473. int can_invert_lrclk;
  474. int sysclk;
  475. aifa = snd_soc_read(codec, WM8580_PAIF1 + codec_dai->driver->id);
  476. aifb = snd_soc_read(codec, WM8580_PAIF3 + codec_dai->driver->id);
  477. aifb &= ~(WM8580_AIF_FMT_MASK | WM8580_AIF_LRP | WM8580_AIF_BCP);
  478. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  479. case SND_SOC_DAIFMT_CBS_CFS:
  480. aifa &= ~WM8580_AIF_MS;
  481. break;
  482. case SND_SOC_DAIFMT_CBM_CFM:
  483. aifa |= WM8580_AIF_MS;
  484. break;
  485. default:
  486. return -EINVAL;
  487. }
  488. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  489. case SND_SOC_DAIFMT_I2S:
  490. can_invert_lrclk = 1;
  491. aifb |= WM8580_AIF_FMT_I2S;
  492. break;
  493. case SND_SOC_DAIFMT_RIGHT_J:
  494. can_invert_lrclk = 1;
  495. aifb |= WM8580_AIF_FMT_RIGHTJ;
  496. break;
  497. case SND_SOC_DAIFMT_LEFT_J:
  498. can_invert_lrclk = 1;
  499. aifb |= WM8580_AIF_FMT_LEFTJ;
  500. break;
  501. case SND_SOC_DAIFMT_DSP_A:
  502. can_invert_lrclk = 0;
  503. aifb |= WM8580_AIF_FMT_DSP;
  504. break;
  505. case SND_SOC_DAIFMT_DSP_B:
  506. can_invert_lrclk = 0;
  507. aifb |= WM8580_AIF_FMT_DSP;
  508. aifb |= WM8580_AIF_LRP;
  509. break;
  510. default:
  511. return -EINVAL;
  512. }
  513. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  514. case SND_SOC_DAIFMT_NB_NF:
  515. break;
  516. case SND_SOC_DAIFMT_IB_IF:
  517. if (!can_invert_lrclk)
  518. return -EINVAL;
  519. aifb |= WM8580_AIF_BCP;
  520. aifb |= WM8580_AIF_LRP;
  521. break;
  522. case SND_SOC_DAIFMT_IB_NF:
  523. aifb |= WM8580_AIF_BCP;
  524. break;
  525. case SND_SOC_DAIFMT_NB_IF:
  526. if (!can_invert_lrclk)
  527. return -EINVAL;
  528. aifb |= WM8580_AIF_LRP;
  529. break;
  530. default:
  531. return -EINVAL;
  532. }
  533. sysclk = wm8580->sysclk[codec_dai->driver->id];
  534. snd_soc_write(codec, WM8580_PAIF1 + codec_dai->driver->id, aifa);
  535. snd_soc_write(codec, WM8580_PAIF3 + codec_dai->driver->id, aifb);
  536. return 0;
  537. }
  538. static int wm8580_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  539. int div_id, int div)
  540. {
  541. struct snd_soc_codec *codec = codec_dai->codec;
  542. unsigned int reg;
  543. switch (div_id) {
  544. case WM8580_MCLK:
  545. reg = snd_soc_read(codec, WM8580_PLLB4);
  546. reg &= ~WM8580_PLLB4_MCLKOUTSRC_MASK;
  547. switch (div) {
  548. case WM8580_CLKSRC_MCLK:
  549. /* Input */
  550. break;
  551. case WM8580_CLKSRC_PLLA:
  552. reg |= WM8580_PLLB4_MCLKOUTSRC_PLLA;
  553. break;
  554. case WM8580_CLKSRC_PLLB:
  555. reg |= WM8580_PLLB4_MCLKOUTSRC_PLLB;
  556. break;
  557. case WM8580_CLKSRC_OSC:
  558. reg |= WM8580_PLLB4_MCLKOUTSRC_OSC;
  559. break;
  560. default:
  561. return -EINVAL;
  562. }
  563. snd_soc_write(codec, WM8580_PLLB4, reg);
  564. break;
  565. case WM8580_CLKOUTSRC:
  566. reg = snd_soc_read(codec, WM8580_PLLB4);
  567. reg &= ~WM8580_PLLB4_CLKOUTSRC_MASK;
  568. switch (div) {
  569. case WM8580_CLKSRC_NONE:
  570. break;
  571. case WM8580_CLKSRC_PLLA:
  572. reg |= WM8580_PLLB4_CLKOUTSRC_PLLACLK;
  573. break;
  574. case WM8580_CLKSRC_PLLB:
  575. reg |= WM8580_PLLB4_CLKOUTSRC_PLLBCLK;
  576. break;
  577. case WM8580_CLKSRC_OSC:
  578. reg |= WM8580_PLLB4_CLKOUTSRC_OSCCLK;
  579. break;
  580. default:
  581. return -EINVAL;
  582. }
  583. snd_soc_write(codec, WM8580_PLLB4, reg);
  584. break;
  585. default:
  586. return -EINVAL;
  587. }
  588. return 0;
  589. }
  590. static int wm8580_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  591. unsigned int freq, int dir)
  592. {
  593. struct snd_soc_codec *codec = dai->codec;
  594. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  595. int sel, sel_mask, sel_shift;
  596. switch (dai->driver->id) {
  597. case WM8580_DAI_PAIFRX:
  598. sel_mask = 0x3;
  599. sel_shift = 0;
  600. break;
  601. case WM8580_DAI_PAIFTX:
  602. sel_mask = 0xc;
  603. sel_shift = 2;
  604. break;
  605. default:
  606. BUG_ON("Unknown DAI driver ID\n");
  607. return -EINVAL;
  608. }
  609. switch (clk_id) {
  610. case WM8580_CLKSRC_ADCMCLK:
  611. if (dai->id != WM8580_DAI_PAIFTX)
  612. return -EINVAL;
  613. sel = 0 << sel_shift;
  614. break;
  615. case WM8580_CLKSRC_PLLA:
  616. sel = 1 << sel_shift;
  617. break;
  618. case WM8580_CLKSRC_PLLB:
  619. sel = 2 << sel_shift;
  620. break;
  621. case WM8580_CLKSRC_MCLK:
  622. sel = 3 << sel_shift;
  623. break;
  624. default:
  625. dev_err(codec->dev, "Unknown clock %d\n", clk_id);
  626. return -EINVAL;
  627. }
  628. /* We really should validate PLL settings but not yet */
  629. wm8580->sysclk[dai->id] = freq;
  630. return snd_soc_update_bits(codec, WM8580_CLKSEL, sel_mask, sel);
  631. }
  632. static int wm8580_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  633. {
  634. struct snd_soc_codec *codec = codec_dai->codec;
  635. unsigned int reg;
  636. reg = snd_soc_read(codec, WM8580_DAC_CONTROL5);
  637. if (mute)
  638. reg |= WM8580_DAC_CONTROL5_MUTEALL;
  639. else
  640. reg &= ~WM8580_DAC_CONTROL5_MUTEALL;
  641. snd_soc_write(codec, WM8580_DAC_CONTROL5, reg);
  642. return 0;
  643. }
  644. static int wm8580_set_bias_level(struct snd_soc_codec *codec,
  645. enum snd_soc_bias_level level)
  646. {
  647. u16 reg;
  648. switch (level) {
  649. case SND_SOC_BIAS_ON:
  650. case SND_SOC_BIAS_PREPARE:
  651. break;
  652. case SND_SOC_BIAS_STANDBY:
  653. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  654. /* Power up and get individual control of the DACs */
  655. reg = snd_soc_read(codec, WM8580_PWRDN1);
  656. reg &= ~(WM8580_PWRDN1_PWDN | WM8580_PWRDN1_ALLDACPD);
  657. snd_soc_write(codec, WM8580_PWRDN1, reg);
  658. /* Make VMID high impedence */
  659. reg = snd_soc_read(codec, WM8580_ADC_CONTROL1);
  660. reg &= ~0x100;
  661. snd_soc_write(codec, WM8580_ADC_CONTROL1, reg);
  662. }
  663. break;
  664. case SND_SOC_BIAS_OFF:
  665. reg = snd_soc_read(codec, WM8580_PWRDN1);
  666. snd_soc_write(codec, WM8580_PWRDN1, reg | WM8580_PWRDN1_PWDN);
  667. break;
  668. }
  669. codec->bias_level = level;
  670. return 0;
  671. }
  672. #define WM8580_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  673. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  674. static struct snd_soc_dai_ops wm8580_dai_ops_playback = {
  675. .set_sysclk = wm8580_set_sysclk,
  676. .hw_params = wm8580_paif_hw_params,
  677. .set_fmt = wm8580_set_paif_dai_fmt,
  678. .set_clkdiv = wm8580_set_dai_clkdiv,
  679. .set_pll = wm8580_set_dai_pll,
  680. .digital_mute = wm8580_digital_mute,
  681. };
  682. static struct snd_soc_dai_ops wm8580_dai_ops_capture = {
  683. .set_sysclk = wm8580_set_sysclk,
  684. .hw_params = wm8580_paif_hw_params,
  685. .set_fmt = wm8580_set_paif_dai_fmt,
  686. .set_clkdiv = wm8580_set_dai_clkdiv,
  687. .set_pll = wm8580_set_dai_pll,
  688. };
  689. static struct snd_soc_dai_driver wm8580_dai[] = {
  690. {
  691. .name = "wm8580-hifi-playback",
  692. .id = WM8580_DAI_PAIFRX,
  693. .playback = {
  694. .stream_name = "Playback",
  695. .channels_min = 1,
  696. .channels_max = 6,
  697. .rates = SNDRV_PCM_RATE_8000_192000,
  698. .formats = WM8580_FORMATS,
  699. },
  700. .ops = &wm8580_dai_ops_playback,
  701. },
  702. {
  703. .name = "wm8580-hifi-capture",
  704. .id = WM8580_DAI_PAIFTX,
  705. .capture = {
  706. .stream_name = "Capture",
  707. .channels_min = 2,
  708. .channels_max = 2,
  709. .rates = SNDRV_PCM_RATE_8000_192000,
  710. .formats = WM8580_FORMATS,
  711. },
  712. .ops = &wm8580_dai_ops_capture,
  713. },
  714. };
  715. static int wm8580_probe(struct snd_soc_codec *codec)
  716. {
  717. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  718. int ret = 0,i;
  719. ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8580->control_type);
  720. if (ret < 0) {
  721. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  722. return ret;
  723. }
  724. for (i = 0; i < ARRAY_SIZE(wm8580->supplies); i++)
  725. wm8580->supplies[i].supply = wm8580_supply_names[i];
  726. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8580->supplies),
  727. wm8580->supplies);
  728. if (ret != 0) {
  729. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  730. return ret;
  731. }
  732. ret = regulator_bulk_enable(ARRAY_SIZE(wm8580->supplies),
  733. wm8580->supplies);
  734. if (ret != 0) {
  735. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  736. goto err_regulator_get;
  737. }
  738. /* Get the codec into a known state */
  739. ret = snd_soc_write(codec, WM8580_RESET, 0);
  740. if (ret != 0) {
  741. dev_err(codec->dev, "Failed to reset codec: %d\n", ret);
  742. goto err_regulator_enable;
  743. }
  744. wm8580_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  745. snd_soc_add_controls(codec, wm8580_snd_controls,
  746. ARRAY_SIZE(wm8580_snd_controls));
  747. wm8580_add_widgets(codec);
  748. return 0;
  749. err_regulator_enable:
  750. regulator_bulk_disable(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
  751. err_regulator_get:
  752. regulator_bulk_free(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
  753. return ret;
  754. }
  755. /* power down chip */
  756. static int wm8580_remove(struct snd_soc_codec *codec)
  757. {
  758. struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
  759. wm8580_set_bias_level(codec, SND_SOC_BIAS_OFF);
  760. regulator_bulk_disable(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
  761. regulator_bulk_free(ARRAY_SIZE(wm8580->supplies), wm8580->supplies);
  762. return 0;
  763. }
  764. static struct snd_soc_codec_driver soc_codec_dev_wm8580 = {
  765. .probe = wm8580_probe,
  766. .remove = wm8580_remove,
  767. .set_bias_level = wm8580_set_bias_level,
  768. .reg_cache_size = ARRAY_SIZE(wm8580_reg),
  769. .reg_word_size = sizeof(u16),
  770. .reg_cache_default = &wm8580_reg,
  771. };
  772. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  773. static int wm8580_i2c_probe(struct i2c_client *i2c,
  774. const struct i2c_device_id *id)
  775. {
  776. struct wm8580_priv *wm8580;
  777. int ret;
  778. wm8580 = kzalloc(sizeof(struct wm8580_priv), GFP_KERNEL);
  779. if (wm8580 == NULL)
  780. return -ENOMEM;
  781. i2c_set_clientdata(i2c, wm8580);
  782. wm8580->control_type = SND_SOC_I2C;
  783. ret = snd_soc_register_codec(&i2c->dev,
  784. &soc_codec_dev_wm8580, wm8580_dai, ARRAY_SIZE(wm8580_dai));
  785. if (ret < 0)
  786. kfree(wm8580);
  787. return ret;
  788. }
  789. static int wm8580_i2c_remove(struct i2c_client *client)
  790. {
  791. snd_soc_unregister_codec(&client->dev);
  792. kfree(i2c_get_clientdata(client));
  793. return 0;
  794. }
  795. static const struct i2c_device_id wm8580_i2c_id[] = {
  796. { "wm8580", 0 },
  797. { }
  798. };
  799. MODULE_DEVICE_TABLE(i2c, wm8580_i2c_id);
  800. static struct i2c_driver wm8580_i2c_driver = {
  801. .driver = {
  802. .name = "wm8580-codec",
  803. .owner = THIS_MODULE,
  804. },
  805. .probe = wm8580_i2c_probe,
  806. .remove = wm8580_i2c_remove,
  807. .id_table = wm8580_i2c_id,
  808. };
  809. #endif
  810. static int __init wm8580_modinit(void)
  811. {
  812. int ret = 0;
  813. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  814. ret = i2c_add_driver(&wm8580_i2c_driver);
  815. if (ret != 0) {
  816. pr_err("Failed to register WM8580 I2C driver: %d\n", ret);
  817. }
  818. #endif
  819. return ret;
  820. }
  821. module_init(wm8580_modinit);
  822. static void __exit wm8580_exit(void)
  823. {
  824. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  825. i2c_del_driver(&wm8580_i2c_driver);
  826. #endif
  827. }
  828. module_exit(wm8580_exit);
  829. MODULE_DESCRIPTION("ASoC WM8580 driver");
  830. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  831. MODULE_LICENSE("GPL");