wm8580.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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->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. struct snd_soc_dai wm8580_dai[] = {
  651. {
  652. .name = "WM8580 PAIFRX",
  653. .id = 0,
  654. .playback = {
  655. .stream_name = "Playback",
  656. .channels_min = 1,
  657. .channels_max = 6,
  658. .rates = SNDRV_PCM_RATE_8000_192000,
  659. .formats = WM8580_FORMATS,
  660. },
  661. .ops = {
  662. .hw_params = wm8580_paif_hw_params,
  663. .set_fmt = wm8580_set_paif_dai_fmt,
  664. .set_clkdiv = wm8580_set_dai_clkdiv,
  665. .set_pll = wm8580_set_dai_pll,
  666. .digital_mute = wm8580_digital_mute,
  667. },
  668. },
  669. {
  670. .name = "WM8580 PAIFTX",
  671. .id = 1,
  672. .capture = {
  673. .stream_name = "Capture",
  674. .channels_min = 2,
  675. .channels_max = 2,
  676. .rates = SNDRV_PCM_RATE_8000_192000,
  677. .formats = WM8580_FORMATS,
  678. },
  679. .ops = {
  680. .hw_params = wm8580_paif_hw_params,
  681. .set_fmt = wm8580_set_paif_dai_fmt,
  682. .set_clkdiv = wm8580_set_dai_clkdiv,
  683. .set_pll = wm8580_set_dai_pll,
  684. },
  685. },
  686. };
  687. EXPORT_SYMBOL_GPL(wm8580_dai);
  688. /*
  689. * initialise the WM8580 driver
  690. * register the mixer and dsp interfaces with the kernel
  691. */
  692. static int wm8580_init(struct snd_soc_device *socdev)
  693. {
  694. struct snd_soc_codec *codec = socdev->codec;
  695. int ret = 0;
  696. codec->name = "WM8580";
  697. codec->owner = THIS_MODULE;
  698. codec->read = wm8580_read_reg_cache;
  699. codec->write = wm8580_write;
  700. codec->set_bias_level = wm8580_set_bias_level;
  701. codec->dai = wm8580_dai;
  702. codec->num_dai = ARRAY_SIZE(wm8580_dai);
  703. codec->reg_cache_size = ARRAY_SIZE(wm8580_reg);
  704. codec->reg_cache = kmemdup(wm8580_reg, sizeof(wm8580_reg),
  705. GFP_KERNEL);
  706. if (codec->reg_cache == NULL)
  707. return -ENOMEM;
  708. /* Get the codec into a known state */
  709. wm8580_write(codec, WM8580_RESET, 0);
  710. /* Power up and get individual control of the DACs */
  711. wm8580_write(codec, WM8580_PWRDN1, wm8580_read(codec, WM8580_PWRDN1) &
  712. ~(WM8580_PWRDN1_PWDN | WM8580_PWRDN1_ALLDACPD));
  713. /* Make VMID high impedence */
  714. wm8580_write(codec, WM8580_ADC_CONTROL1,
  715. wm8580_read(codec, WM8580_ADC_CONTROL1) & ~0x100);
  716. /* register pcms */
  717. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1,
  718. SNDRV_DEFAULT_STR1);
  719. if (ret < 0) {
  720. printk(KERN_ERR "wm8580: failed to create pcms\n");
  721. goto pcm_err;
  722. }
  723. snd_soc_add_controls(codec, wm8580_snd_controls,
  724. ARRAY_SIZE(wm8580_snd_controls));
  725. wm8580_add_widgets(codec);
  726. ret = snd_soc_init_card(socdev);
  727. if (ret < 0) {
  728. printk(KERN_ERR "wm8580: failed to register card\n");
  729. goto card_err;
  730. }
  731. return ret;
  732. card_err:
  733. snd_soc_free_pcms(socdev);
  734. snd_soc_dapm_free(socdev);
  735. pcm_err:
  736. kfree(codec->reg_cache);
  737. return ret;
  738. }
  739. /* If the i2c layer weren't so broken, we could pass this kind of data
  740. around */
  741. static struct snd_soc_device *wm8580_socdev;
  742. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  743. /*
  744. * WM8580 2 wire address is determined by GPIO5
  745. * state during powerup.
  746. * low = 0x1a
  747. * high = 0x1b
  748. */
  749. static int wm8580_i2c_probe(struct i2c_client *i2c,
  750. const struct i2c_device_id *id)
  751. {
  752. struct snd_soc_device *socdev = wm8580_socdev;
  753. struct snd_soc_codec *codec = socdev->codec;
  754. int ret;
  755. i2c_set_clientdata(i2c, codec);
  756. codec->control_data = i2c;
  757. ret = wm8580_init(socdev);
  758. if (ret < 0)
  759. dev_err(&i2c->dev, "failed to initialise WM8580\n");
  760. return ret;
  761. }
  762. static int wm8580_i2c_remove(struct i2c_client *client)
  763. {
  764. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  765. kfree(codec->reg_cache);
  766. return 0;
  767. }
  768. static const struct i2c_device_id wm8580_i2c_id[] = {
  769. { "wm8580", 0 },
  770. { }
  771. };
  772. MODULE_DEVICE_TABLE(i2c, wm8580_i2c_id);
  773. static struct i2c_driver wm8580_i2c_driver = {
  774. .driver = {
  775. .name = "WM8580 I2C Codec",
  776. .owner = THIS_MODULE,
  777. },
  778. .probe = wm8580_i2c_probe,
  779. .remove = wm8580_i2c_remove,
  780. .id_table = wm8580_i2c_id,
  781. };
  782. static int wm8580_add_i2c_device(struct platform_device *pdev,
  783. const struct wm8580_setup_data *setup)
  784. {
  785. struct i2c_board_info info;
  786. struct i2c_adapter *adapter;
  787. struct i2c_client *client;
  788. int ret;
  789. ret = i2c_add_driver(&wm8580_i2c_driver);
  790. if (ret != 0) {
  791. dev_err(&pdev->dev, "can't add i2c driver\n");
  792. return ret;
  793. }
  794. memset(&info, 0, sizeof(struct i2c_board_info));
  795. info.addr = setup->i2c_address;
  796. strlcpy(info.type, "wm8580", I2C_NAME_SIZE);
  797. adapter = i2c_get_adapter(setup->i2c_bus);
  798. if (!adapter) {
  799. dev_err(&pdev->dev, "can't get i2c adapter %d\n",
  800. setup->i2c_bus);
  801. goto err_driver;
  802. }
  803. client = i2c_new_device(adapter, &info);
  804. i2c_put_adapter(adapter);
  805. if (!client) {
  806. dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
  807. (unsigned int)info.addr);
  808. goto err_driver;
  809. }
  810. return 0;
  811. err_driver:
  812. i2c_del_driver(&wm8580_i2c_driver);
  813. return -ENODEV;
  814. }
  815. #endif
  816. static int wm8580_probe(struct platform_device *pdev)
  817. {
  818. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  819. struct wm8580_setup_data *setup;
  820. struct snd_soc_codec *codec;
  821. struct wm8580_priv *wm8580;
  822. int ret = 0;
  823. pr_info("WM8580 Audio Codec %s\n", WM8580_VERSION);
  824. setup = socdev->codec_data;
  825. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  826. if (codec == NULL)
  827. return -ENOMEM;
  828. wm8580 = kzalloc(sizeof(struct wm8580_priv), GFP_KERNEL);
  829. if (wm8580 == NULL) {
  830. kfree(codec);
  831. return -ENOMEM;
  832. }
  833. codec->private_data = wm8580;
  834. socdev->codec = codec;
  835. mutex_init(&codec->mutex);
  836. INIT_LIST_HEAD(&codec->dapm_widgets);
  837. INIT_LIST_HEAD(&codec->dapm_paths);
  838. wm8580_socdev = socdev;
  839. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  840. if (setup->i2c_address) {
  841. codec->hw_write = (hw_write_t)i2c_master_send;
  842. ret = wm8580_add_i2c_device(pdev, setup);
  843. }
  844. #else
  845. /* Add other interfaces here */
  846. #endif
  847. return ret;
  848. }
  849. /* power down chip */
  850. static int wm8580_remove(struct platform_device *pdev)
  851. {
  852. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  853. struct snd_soc_codec *codec = socdev->codec;
  854. if (codec->control_data)
  855. wm8580_set_bias_level(codec, SND_SOC_BIAS_OFF);
  856. snd_soc_free_pcms(socdev);
  857. snd_soc_dapm_free(socdev);
  858. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  859. i2c_unregister_device(codec->control_data);
  860. i2c_del_driver(&wm8580_i2c_driver);
  861. #endif
  862. kfree(codec->private_data);
  863. kfree(codec);
  864. return 0;
  865. }
  866. struct snd_soc_codec_device soc_codec_dev_wm8580 = {
  867. .probe = wm8580_probe,
  868. .remove = wm8580_remove,
  869. };
  870. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8580);
  871. static int __init wm8580_modinit(void)
  872. {
  873. return snd_soc_register_dais(wm8580_dai, ARRAY_SIZE(wm8580_dai));
  874. }
  875. module_init(wm8580_modinit);
  876. static void __exit wm8580_exit(void)
  877. {
  878. snd_soc_unregister_dais(wm8580_dai, ARRAY_SIZE(wm8580_dai));
  879. }
  880. module_exit(wm8580_exit);
  881. MODULE_DESCRIPTION("ASoC WM8580 driver");
  882. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  883. MODULE_LICENSE("GPL");