wm8580.c 26 KB

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