wm8580.c 26 KB

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