wm8580.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. /* Add non-DAPM controls */
  290. static int wm8580_add_controls(struct snd_soc_codec *codec)
  291. {
  292. int err, i;
  293. for (i = 0; i < ARRAY_SIZE(wm8580_snd_controls); i++) {
  294. err = snd_ctl_add(codec->card,
  295. snd_soc_cnew(&wm8580_snd_controls[i],
  296. codec, NULL));
  297. if (err < 0)
  298. return err;
  299. }
  300. return 0;
  301. }
  302. static const struct snd_soc_dapm_widget wm8580_dapm_widgets[] = {
  303. SND_SOC_DAPM_DAC("DAC1", "Playback", WM8580_PWRDN1, 2, 1),
  304. SND_SOC_DAPM_DAC("DAC2", "Playback", WM8580_PWRDN1, 3, 1),
  305. SND_SOC_DAPM_DAC("DAC3", "Playback", WM8580_PWRDN1, 4, 1),
  306. SND_SOC_DAPM_OUTPUT("VOUT1L"),
  307. SND_SOC_DAPM_OUTPUT("VOUT1R"),
  308. SND_SOC_DAPM_OUTPUT("VOUT2L"),
  309. SND_SOC_DAPM_OUTPUT("VOUT2R"),
  310. SND_SOC_DAPM_OUTPUT("VOUT3L"),
  311. SND_SOC_DAPM_OUTPUT("VOUT3R"),
  312. SND_SOC_DAPM_ADC("ADC", "Capture", WM8580_PWRDN1, 1, 1),
  313. SND_SOC_DAPM_INPUT("AINL"),
  314. SND_SOC_DAPM_INPUT("AINR"),
  315. };
  316. static const struct snd_soc_dapm_route audio_map[] = {
  317. { "VOUT1L", NULL, "DAC1" },
  318. { "VOUT1R", NULL, "DAC1" },
  319. { "VOUT2L", NULL, "DAC2" },
  320. { "VOUT2R", NULL, "DAC2" },
  321. { "VOUT3L", NULL, "DAC3" },
  322. { "VOUT3R", NULL, "DAC3" },
  323. { "ADC", NULL, "AINL" },
  324. { "ADC", NULL, "AINR" },
  325. };
  326. static int wm8580_add_widgets(struct snd_soc_codec *codec)
  327. {
  328. snd_soc_dapm_new_controls(codec, wm8580_dapm_widgets,
  329. ARRAY_SIZE(wm8580_dapm_widgets));
  330. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  331. snd_soc_dapm_new_widgets(codec);
  332. return 0;
  333. }
  334. /* PLL divisors */
  335. struct _pll_div {
  336. u32 prescale:1;
  337. u32 postscale:1;
  338. u32 freqmode:2;
  339. u32 n:4;
  340. u32 k:24;
  341. };
  342. /* The size in bits of the pll divide */
  343. #define FIXED_PLL_SIZE (1 << 22)
  344. /* PLL rate to output rate divisions */
  345. static struct {
  346. unsigned int div;
  347. unsigned int freqmode;
  348. unsigned int postscale;
  349. } post_table[] = {
  350. { 2, 0, 0 },
  351. { 4, 0, 1 },
  352. { 4, 1, 0 },
  353. { 8, 1, 1 },
  354. { 8, 2, 0 },
  355. { 16, 2, 1 },
  356. { 12, 3, 0 },
  357. { 24, 3, 1 }
  358. };
  359. static int pll_factors(struct _pll_div *pll_div, unsigned int target,
  360. unsigned int source)
  361. {
  362. u64 Kpart;
  363. unsigned int K, Ndiv, Nmod;
  364. int i;
  365. pr_debug("wm8580: PLL %dHz->%dHz\n", source, target);
  366. /* Scale the output frequency up; the PLL should run in the
  367. * region of 90-100MHz.
  368. */
  369. for (i = 0; i < ARRAY_SIZE(post_table); i++) {
  370. if (target * post_table[i].div >= 90000000 &&
  371. target * post_table[i].div <= 100000000) {
  372. pll_div->freqmode = post_table[i].freqmode;
  373. pll_div->postscale = post_table[i].postscale;
  374. target *= post_table[i].div;
  375. break;
  376. }
  377. }
  378. if (i == ARRAY_SIZE(post_table)) {
  379. printk(KERN_ERR "wm8580: Unable to scale output frequency "
  380. "%u\n", target);
  381. return -EINVAL;
  382. }
  383. Ndiv = target / source;
  384. if (Ndiv < 5) {
  385. source /= 2;
  386. pll_div->prescale = 1;
  387. Ndiv = target / source;
  388. } else
  389. pll_div->prescale = 0;
  390. if ((Ndiv < 5) || (Ndiv > 13)) {
  391. printk(KERN_ERR
  392. "WM8580 N=%d outside supported range\n", Ndiv);
  393. return -EINVAL;
  394. }
  395. pll_div->n = Ndiv;
  396. Nmod = target % source;
  397. Kpart = FIXED_PLL_SIZE * (long long)Nmod;
  398. do_div(Kpart, source);
  399. K = Kpart & 0xFFFFFFFF;
  400. pll_div->k = K;
  401. pr_debug("PLL %x.%x prescale %d freqmode %d postscale %d\n",
  402. pll_div->n, pll_div->k, pll_div->prescale, pll_div->freqmode,
  403. pll_div->postscale);
  404. return 0;
  405. }
  406. static int wm8580_set_dai_pll(struct snd_soc_dai *codec_dai,
  407. int pll_id, unsigned int freq_in, unsigned int freq_out)
  408. {
  409. int offset;
  410. struct snd_soc_codec *codec = codec_dai->codec;
  411. struct wm8580_priv *wm8580 = codec->private_data;
  412. struct pll_state *state;
  413. struct _pll_div pll_div;
  414. unsigned int reg;
  415. unsigned int pwr_mask;
  416. int ret;
  417. /* GCC isn't able to work out the ifs below for initialising/using
  418. * pll_div so suppress warnings.
  419. */
  420. memset(&pll_div, 0, sizeof(pll_div));
  421. switch (pll_id) {
  422. case WM8580_PLLA:
  423. state = &wm8580->a;
  424. offset = 0;
  425. pwr_mask = WM8580_PWRDN2_PLLAPD;
  426. break;
  427. case WM8580_PLLB:
  428. state = &wm8580->b;
  429. offset = 4;
  430. pwr_mask = WM8580_PWRDN2_PLLBPD;
  431. break;
  432. default:
  433. return -ENODEV;
  434. }
  435. if (freq_in && freq_out) {
  436. ret = pll_factors(&pll_div, freq_out, freq_in);
  437. if (ret != 0)
  438. return ret;
  439. }
  440. state->in = freq_in;
  441. state->out = freq_out;
  442. /* Always disable the PLL - it is not safe to leave it running
  443. * while reprogramming it.
  444. */
  445. reg = wm8580_read(codec, WM8580_PWRDN2);
  446. wm8580_write(codec, WM8580_PWRDN2, reg | pwr_mask);
  447. if (!freq_in || !freq_out)
  448. return 0;
  449. wm8580_write(codec, WM8580_PLLA1 + offset, pll_div.k & 0x1ff);
  450. wm8580_write(codec, WM8580_PLLA2 + offset, (pll_div.k >> 9) & 0xff);
  451. wm8580_write(codec, WM8580_PLLA3 + offset,
  452. (pll_div.k >> 18 & 0xf) | (pll_div.n << 4));
  453. reg = wm8580_read(codec, WM8580_PLLA4 + offset);
  454. reg &= ~0x3f;
  455. reg |= pll_div.prescale | pll_div.postscale << 1 |
  456. pll_div.freqmode << 4;
  457. wm8580_write(codec, WM8580_PLLA4 + offset, reg);
  458. /* All done, turn it on */
  459. reg = wm8580_read(codec, WM8580_PWRDN2);
  460. wm8580_write(codec, WM8580_PWRDN2, reg & ~pwr_mask);
  461. return 0;
  462. }
  463. /*
  464. * Set PCM DAI bit size and sample rate.
  465. */
  466. static int wm8580_paif_hw_params(struct snd_pcm_substream *substream,
  467. struct snd_pcm_hw_params *params,
  468. struct snd_soc_dai *dai)
  469. {
  470. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  471. struct snd_soc_device *socdev = rtd->socdev;
  472. struct snd_soc_codec *codec = socdev->codec;
  473. u16 paifb = wm8580_read(codec, WM8580_PAIF3 + dai->id);
  474. paifb &= ~WM8580_AIF_LENGTH_MASK;
  475. /* bit size */
  476. switch (params_format(params)) {
  477. case SNDRV_PCM_FORMAT_S16_LE:
  478. break;
  479. case SNDRV_PCM_FORMAT_S20_3LE:
  480. paifb |= WM8580_AIF_LENGTH_20;
  481. break;
  482. case SNDRV_PCM_FORMAT_S24_LE:
  483. paifb |= WM8580_AIF_LENGTH_24;
  484. break;
  485. case SNDRV_PCM_FORMAT_S32_LE:
  486. paifb |= WM8580_AIF_LENGTH_24;
  487. break;
  488. default:
  489. return -EINVAL;
  490. }
  491. wm8580_write(codec, WM8580_PAIF3 + dai->id, paifb);
  492. return 0;
  493. }
  494. static int wm8580_set_paif_dai_fmt(struct snd_soc_dai *codec_dai,
  495. unsigned int fmt)
  496. {
  497. struct snd_soc_codec *codec = codec_dai->codec;
  498. unsigned int aifa;
  499. unsigned int aifb;
  500. int can_invert_lrclk;
  501. aifa = wm8580_read(codec, WM8580_PAIF1 + codec_dai->id);
  502. aifb = wm8580_read(codec, WM8580_PAIF3 + codec_dai->id);
  503. aifb &= ~(WM8580_AIF_FMT_MASK | WM8580_AIF_LRP | WM8580_AIF_BCP);
  504. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  505. case SND_SOC_DAIFMT_CBS_CFS:
  506. aifa &= ~WM8580_AIF_MS;
  507. break;
  508. case SND_SOC_DAIFMT_CBM_CFM:
  509. aifa |= WM8580_AIF_MS;
  510. break;
  511. default:
  512. return -EINVAL;
  513. }
  514. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  515. case SND_SOC_DAIFMT_I2S:
  516. can_invert_lrclk = 1;
  517. aifb |= WM8580_AIF_FMT_I2S;
  518. break;
  519. case SND_SOC_DAIFMT_RIGHT_J:
  520. can_invert_lrclk = 1;
  521. aifb |= WM8580_AIF_FMT_RIGHTJ;
  522. break;
  523. case SND_SOC_DAIFMT_LEFT_J:
  524. can_invert_lrclk = 1;
  525. aifb |= WM8580_AIF_FMT_LEFTJ;
  526. break;
  527. case SND_SOC_DAIFMT_DSP_A:
  528. can_invert_lrclk = 0;
  529. aifb |= WM8580_AIF_FMT_DSP;
  530. break;
  531. case SND_SOC_DAIFMT_DSP_B:
  532. can_invert_lrclk = 0;
  533. aifb |= WM8580_AIF_FMT_DSP;
  534. aifb |= WM8580_AIF_LRP;
  535. break;
  536. default:
  537. return -EINVAL;
  538. }
  539. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  540. case SND_SOC_DAIFMT_NB_NF:
  541. break;
  542. case SND_SOC_DAIFMT_IB_IF:
  543. if (!can_invert_lrclk)
  544. return -EINVAL;
  545. aifb |= WM8580_AIF_BCP;
  546. aifb |= WM8580_AIF_LRP;
  547. break;
  548. case SND_SOC_DAIFMT_IB_NF:
  549. aifb |= WM8580_AIF_BCP;
  550. break;
  551. case SND_SOC_DAIFMT_NB_IF:
  552. if (!can_invert_lrclk)
  553. return -EINVAL;
  554. aifb |= WM8580_AIF_LRP;
  555. break;
  556. default:
  557. return -EINVAL;
  558. }
  559. wm8580_write(codec, WM8580_PAIF1 + codec_dai->id, aifa);
  560. wm8580_write(codec, WM8580_PAIF3 + codec_dai->id, aifb);
  561. return 0;
  562. }
  563. static int wm8580_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  564. int div_id, int div)
  565. {
  566. struct snd_soc_codec *codec = codec_dai->codec;
  567. unsigned int reg;
  568. switch (div_id) {
  569. case WM8580_MCLK:
  570. reg = wm8580_read(codec, WM8580_PLLB4);
  571. reg &= ~WM8580_PLLB4_MCLKOUTSRC_MASK;
  572. switch (div) {
  573. case WM8580_CLKSRC_MCLK:
  574. /* Input */
  575. break;
  576. case WM8580_CLKSRC_PLLA:
  577. reg |= WM8580_PLLB4_MCLKOUTSRC_PLLA;
  578. break;
  579. case WM8580_CLKSRC_PLLB:
  580. reg |= WM8580_PLLB4_MCLKOUTSRC_PLLB;
  581. break;
  582. case WM8580_CLKSRC_OSC:
  583. reg |= WM8580_PLLB4_MCLKOUTSRC_OSC;
  584. break;
  585. default:
  586. return -EINVAL;
  587. }
  588. wm8580_write(codec, WM8580_PLLB4, reg);
  589. break;
  590. case WM8580_DAC_CLKSEL:
  591. reg = wm8580_read(codec, WM8580_CLKSEL);
  592. reg &= ~WM8580_CLKSEL_DAC_CLKSEL_MASK;
  593. switch (div) {
  594. case WM8580_CLKSRC_MCLK:
  595. break;
  596. case WM8580_CLKSRC_PLLA:
  597. reg |= WM8580_CLKSEL_DAC_CLKSEL_PLLA;
  598. break;
  599. case WM8580_CLKSRC_PLLB:
  600. reg |= WM8580_CLKSEL_DAC_CLKSEL_PLLB;
  601. break;
  602. default:
  603. return -EINVAL;
  604. }
  605. wm8580_write(codec, WM8580_CLKSEL, reg);
  606. break;
  607. case WM8580_CLKOUTSRC:
  608. reg = wm8580_read(codec, WM8580_PLLB4);
  609. reg &= ~WM8580_PLLB4_CLKOUTSRC_MASK;
  610. switch (div) {
  611. case WM8580_CLKSRC_NONE:
  612. break;
  613. case WM8580_CLKSRC_PLLA:
  614. reg |= WM8580_PLLB4_CLKOUTSRC_PLLACLK;
  615. break;
  616. case WM8580_CLKSRC_PLLB:
  617. reg |= WM8580_PLLB4_CLKOUTSRC_PLLBCLK;
  618. break;
  619. case WM8580_CLKSRC_OSC:
  620. reg |= WM8580_PLLB4_CLKOUTSRC_OSCCLK;
  621. break;
  622. default:
  623. return -EINVAL;
  624. }
  625. wm8580_write(codec, WM8580_PLLB4, reg);
  626. break;
  627. default:
  628. return -EINVAL;
  629. }
  630. return 0;
  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 = wm8580_read(codec, WM8580_DAC_CONTROL5);
  637. if (mute)
  638. reg |= WM8580_DAC_CONTROL5_MUTEALL;
  639. else
  640. reg &= ~WM8580_DAC_CONTROL5_MUTEALL;
  641. wm8580_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. case SND_SOC_BIAS_STANDBY:
  652. break;
  653. case SND_SOC_BIAS_OFF:
  654. reg = wm8580_read(codec, WM8580_PWRDN1);
  655. wm8580_write(codec, WM8580_PWRDN1, reg | WM8580_PWRDN1_PWDN);
  656. break;
  657. }
  658. codec->bias_level = level;
  659. return 0;
  660. }
  661. #define WM8580_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  662. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  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 = {
  675. .hw_params = wm8580_paif_hw_params,
  676. .set_fmt = wm8580_set_paif_dai_fmt,
  677. .set_clkdiv = wm8580_set_dai_clkdiv,
  678. .set_pll = wm8580_set_dai_pll,
  679. .digital_mute = wm8580_digital_mute,
  680. },
  681. },
  682. {
  683. .name = "WM8580 PAIFTX",
  684. .id = 1,
  685. .capture = {
  686. .stream_name = "Capture",
  687. .channels_min = 2,
  688. .channels_max = 2,
  689. .rates = SNDRV_PCM_RATE_8000_192000,
  690. .formats = WM8580_FORMATS,
  691. },
  692. .ops = {
  693. .hw_params = wm8580_paif_hw_params,
  694. .set_fmt = wm8580_set_paif_dai_fmt,
  695. .set_clkdiv = wm8580_set_dai_clkdiv,
  696. .set_pll = wm8580_set_dai_pll,
  697. },
  698. },
  699. };
  700. EXPORT_SYMBOL_GPL(wm8580_dai);
  701. /*
  702. * initialise the WM8580 driver
  703. * register the mixer and dsp interfaces with the kernel
  704. */
  705. static int wm8580_init(struct snd_soc_device *socdev)
  706. {
  707. struct snd_soc_codec *codec = socdev->codec;
  708. int ret = 0;
  709. codec->name = "WM8580";
  710. codec->owner = THIS_MODULE;
  711. codec->read = wm8580_read_reg_cache;
  712. codec->write = wm8580_write;
  713. codec->set_bias_level = wm8580_set_bias_level;
  714. codec->dai = wm8580_dai;
  715. codec->num_dai = ARRAY_SIZE(wm8580_dai);
  716. codec->reg_cache_size = ARRAY_SIZE(wm8580_reg);
  717. codec->reg_cache = kmemdup(wm8580_reg, sizeof(wm8580_reg),
  718. GFP_KERNEL);
  719. if (codec->reg_cache == NULL)
  720. return -ENOMEM;
  721. /* Get the codec into a known state */
  722. wm8580_write(codec, WM8580_RESET, 0);
  723. /* Power up and get individual control of the DACs */
  724. wm8580_write(codec, WM8580_PWRDN1, wm8580_read(codec, WM8580_PWRDN1) &
  725. ~(WM8580_PWRDN1_PWDN | WM8580_PWRDN1_ALLDACPD));
  726. /* Make VMID high impedence */
  727. wm8580_write(codec, WM8580_ADC_CONTROL1,
  728. wm8580_read(codec, WM8580_ADC_CONTROL1) & ~0x100);
  729. /* register pcms */
  730. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1,
  731. SNDRV_DEFAULT_STR1);
  732. if (ret < 0) {
  733. printk(KERN_ERR "wm8580: failed to create pcms\n");
  734. goto pcm_err;
  735. }
  736. wm8580_add_controls(codec);
  737. wm8580_add_widgets(codec);
  738. ret = snd_soc_init_card(socdev);
  739. if (ret < 0) {
  740. printk(KERN_ERR "wm8580: failed to register card\n");
  741. goto card_err;
  742. }
  743. return ret;
  744. card_err:
  745. snd_soc_free_pcms(socdev);
  746. snd_soc_dapm_free(socdev);
  747. pcm_err:
  748. kfree(codec->reg_cache);
  749. return ret;
  750. }
  751. /* If the i2c layer weren't so broken, we could pass this kind of data
  752. around */
  753. static struct snd_soc_device *wm8580_socdev;
  754. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  755. /*
  756. * WM8580 2 wire address is determined by GPIO5
  757. * state during powerup.
  758. * low = 0x1a
  759. * high = 0x1b
  760. */
  761. static int wm8580_i2c_probe(struct i2c_client *i2c,
  762. const struct i2c_device_id *id)
  763. {
  764. struct snd_soc_device *socdev = wm8580_socdev;
  765. struct snd_soc_codec *codec = socdev->codec;
  766. int ret;
  767. i2c_set_clientdata(i2c, codec);
  768. codec->control_data = i2c;
  769. ret = wm8580_init(socdev);
  770. if (ret < 0)
  771. dev_err(&i2c->dev, "failed to initialise WM8580\n");
  772. return ret;
  773. }
  774. static int wm8580_i2c_remove(struct i2c_client *client)
  775. {
  776. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  777. kfree(codec->reg_cache);
  778. return 0;
  779. }
  780. static const struct i2c_device_id wm8580_i2c_id[] = {
  781. { "wm8580", 0 },
  782. { }
  783. };
  784. MODULE_DEVICE_TABLE(i2c, wm8580_i2c_id);
  785. static struct i2c_driver wm8580_i2c_driver = {
  786. .driver = {
  787. .name = "WM8580 I2C Codec",
  788. .owner = THIS_MODULE,
  789. },
  790. .probe = wm8580_i2c_probe,
  791. .remove = wm8580_i2c_remove,
  792. .id_table = wm8580_i2c_id,
  793. };
  794. static int wm8580_add_i2c_device(struct platform_device *pdev,
  795. const struct wm8580_setup_data *setup)
  796. {
  797. struct i2c_board_info info;
  798. struct i2c_adapter *adapter;
  799. struct i2c_client *client;
  800. int ret;
  801. ret = i2c_add_driver(&wm8580_i2c_driver);
  802. if (ret != 0) {
  803. dev_err(&pdev->dev, "can't add i2c driver\n");
  804. return ret;
  805. }
  806. memset(&info, 0, sizeof(struct i2c_board_info));
  807. info.addr = setup->i2c_address;
  808. strlcpy(info.type, "wm8580", I2C_NAME_SIZE);
  809. adapter = i2c_get_adapter(setup->i2c_bus);
  810. if (!adapter) {
  811. dev_err(&pdev->dev, "can't get i2c adapter %d\n",
  812. setup->i2c_bus);
  813. goto err_driver;
  814. }
  815. client = i2c_new_device(adapter, &info);
  816. i2c_put_adapter(adapter);
  817. if (!client) {
  818. dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
  819. (unsigned int)info.addr);
  820. goto err_driver;
  821. }
  822. return 0;
  823. err_driver:
  824. i2c_del_driver(&wm8580_i2c_driver);
  825. return -ENODEV;
  826. }
  827. #endif
  828. static int wm8580_probe(struct platform_device *pdev)
  829. {
  830. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  831. struct wm8580_setup_data *setup;
  832. struct snd_soc_codec *codec;
  833. struct wm8580_priv *wm8580;
  834. int ret = 0;
  835. pr_info("WM8580 Audio Codec %s\n", WM8580_VERSION);
  836. setup = socdev->codec_data;
  837. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  838. if (codec == NULL)
  839. return -ENOMEM;
  840. wm8580 = kzalloc(sizeof(struct wm8580_priv), GFP_KERNEL);
  841. if (wm8580 == NULL) {
  842. kfree(codec);
  843. return -ENOMEM;
  844. }
  845. codec->private_data = wm8580;
  846. socdev->codec = codec;
  847. mutex_init(&codec->mutex);
  848. INIT_LIST_HEAD(&codec->dapm_widgets);
  849. INIT_LIST_HEAD(&codec->dapm_paths);
  850. wm8580_socdev = socdev;
  851. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  852. if (setup->i2c_address) {
  853. codec->hw_write = (hw_write_t)i2c_master_send;
  854. ret = wm8580_add_i2c_device(pdev, setup);
  855. }
  856. #else
  857. /* Add other interfaces here */
  858. #endif
  859. return ret;
  860. }
  861. /* power down chip */
  862. static int wm8580_remove(struct platform_device *pdev)
  863. {
  864. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  865. struct snd_soc_codec *codec = socdev->codec;
  866. if (codec->control_data)
  867. wm8580_set_bias_level(codec, SND_SOC_BIAS_OFF);
  868. snd_soc_free_pcms(socdev);
  869. snd_soc_dapm_free(socdev);
  870. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  871. i2c_unregister_device(codec->control_data);
  872. i2c_del_driver(&wm8580_i2c_driver);
  873. #endif
  874. kfree(codec->private_data);
  875. kfree(codec);
  876. return 0;
  877. }
  878. struct snd_soc_codec_device soc_codec_dev_wm8580 = {
  879. .probe = wm8580_probe,
  880. .remove = wm8580_remove,
  881. };
  882. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8580);
  883. static int __init wm8580_modinit(void)
  884. {
  885. return snd_soc_register_dais(wm8580_dai, ARRAY_SIZE(wm8580_dai));
  886. }
  887. module_init(wm8580_modinit);
  888. static void __exit wm8580_exit(void)
  889. {
  890. snd_soc_unregister_dais(wm8580_dai, ARRAY_SIZE(wm8580_dai));
  891. }
  892. module_exit(wm8580_exit);
  893. MODULE_DESCRIPTION("ASoC WM8580 driver");
  894. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  895. MODULE_LICENSE("GPL");