wm8580.c 26 KB

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