wm8974.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*
  2. * wm8974.c -- WM8974 ALSA Soc Audio driver
  3. *
  4. * Copyright 2006-2009 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Liam Girdwood <linux@wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/i2c.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/initval.h>
  26. #include <sound/tlv.h>
  27. #include "wm8974.h"
  28. static const u16 wm8974_reg[WM8974_CACHEREGNUM] = {
  29. 0x0000, 0x0000, 0x0000, 0x0000,
  30. 0x0050, 0x0000, 0x0140, 0x0000,
  31. 0x0000, 0x0000, 0x0000, 0x00ff,
  32. 0x0000, 0x0000, 0x0100, 0x00ff,
  33. 0x0000, 0x0000, 0x012c, 0x002c,
  34. 0x002c, 0x002c, 0x002c, 0x0000,
  35. 0x0032, 0x0000, 0x0000, 0x0000,
  36. 0x0000, 0x0000, 0x0000, 0x0000,
  37. 0x0038, 0x000b, 0x0032, 0x0000,
  38. 0x0008, 0x000c, 0x0093, 0x00e9,
  39. 0x0000, 0x0000, 0x0000, 0x0000,
  40. 0x0003, 0x0010, 0x0000, 0x0000,
  41. 0x0000, 0x0002, 0x0000, 0x0000,
  42. 0x0000, 0x0000, 0x0039, 0x0000,
  43. 0x0000,
  44. };
  45. #define WM8974_POWER1_BIASEN 0x08
  46. #define WM8974_POWER1_BUFIOEN 0x04
  47. struct wm8974_priv {
  48. enum snd_soc_control_type control_type;
  49. };
  50. #define wm8974_reset(c) snd_soc_write(c, WM8974_RESET, 0)
  51. static const char *wm8974_companding[] = {"Off", "NC", "u-law", "A-law" };
  52. static const char *wm8974_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz" };
  53. static const char *wm8974_eqmode[] = {"Capture", "Playback" };
  54. static const char *wm8974_bw[] = {"Narrow", "Wide" };
  55. static const char *wm8974_eq1[] = {"80Hz", "105Hz", "135Hz", "175Hz" };
  56. static const char *wm8974_eq2[] = {"230Hz", "300Hz", "385Hz", "500Hz" };
  57. static const char *wm8974_eq3[] = {"650Hz", "850Hz", "1.1kHz", "1.4kHz" };
  58. static const char *wm8974_eq4[] = {"1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz" };
  59. static const char *wm8974_eq5[] = {"5.3kHz", "6.9kHz", "9kHz", "11.7kHz" };
  60. static const char *wm8974_alc[] = {"ALC", "Limiter" };
  61. static const struct soc_enum wm8974_enum[] = {
  62. SOC_ENUM_SINGLE(WM8974_COMP, 1, 4, wm8974_companding), /* adc */
  63. SOC_ENUM_SINGLE(WM8974_COMP, 3, 4, wm8974_companding), /* dac */
  64. SOC_ENUM_SINGLE(WM8974_DAC, 4, 4, wm8974_deemp),
  65. SOC_ENUM_SINGLE(WM8974_EQ1, 8, 2, wm8974_eqmode),
  66. SOC_ENUM_SINGLE(WM8974_EQ1, 5, 4, wm8974_eq1),
  67. SOC_ENUM_SINGLE(WM8974_EQ2, 8, 2, wm8974_bw),
  68. SOC_ENUM_SINGLE(WM8974_EQ2, 5, 4, wm8974_eq2),
  69. SOC_ENUM_SINGLE(WM8974_EQ3, 8, 2, wm8974_bw),
  70. SOC_ENUM_SINGLE(WM8974_EQ3, 5, 4, wm8974_eq3),
  71. SOC_ENUM_SINGLE(WM8974_EQ4, 8, 2, wm8974_bw),
  72. SOC_ENUM_SINGLE(WM8974_EQ4, 5, 4, wm8974_eq4),
  73. SOC_ENUM_SINGLE(WM8974_EQ5, 8, 2, wm8974_bw),
  74. SOC_ENUM_SINGLE(WM8974_EQ5, 5, 4, wm8974_eq5),
  75. SOC_ENUM_SINGLE(WM8974_ALC3, 8, 2, wm8974_alc),
  76. };
  77. static const char *wm8974_auxmode_text[] = { "Buffer", "Mixer" };
  78. static const struct soc_enum wm8974_auxmode =
  79. SOC_ENUM_SINGLE(WM8974_INPUT, 3, 2, wm8974_auxmode_text);
  80. static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1);
  81. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  82. static const DECLARE_TLV_DB_SCALE(inpga_tlv, -1200, 75, 0);
  83. static const DECLARE_TLV_DB_SCALE(spk_tlv, -5700, 100, 0);
  84. static const struct snd_kcontrol_new wm8974_snd_controls[] = {
  85. SOC_SINGLE("Digital Loopback Switch", WM8974_COMP, 0, 1, 0),
  86. SOC_ENUM("DAC Companding", wm8974_enum[1]),
  87. SOC_ENUM("ADC Companding", wm8974_enum[0]),
  88. SOC_ENUM("Playback De-emphasis", wm8974_enum[2]),
  89. SOC_SINGLE("DAC Inversion Switch", WM8974_DAC, 0, 1, 0),
  90. SOC_SINGLE_TLV("PCM Volume", WM8974_DACVOL, 0, 255, 0, digital_tlv),
  91. SOC_SINGLE("High Pass Filter Switch", WM8974_ADC, 8, 1, 0),
  92. SOC_SINGLE("High Pass Cut Off", WM8974_ADC, 4, 7, 0),
  93. SOC_SINGLE("ADC Inversion Switch", WM8974_ADC, 0, 1, 0),
  94. SOC_SINGLE_TLV("Capture Volume", WM8974_ADCVOL, 0, 255, 0, digital_tlv),
  95. SOC_ENUM("Equaliser Function", wm8974_enum[3]),
  96. SOC_ENUM("EQ1 Cut Off", wm8974_enum[4]),
  97. SOC_SINGLE_TLV("EQ1 Volume", WM8974_EQ1, 0, 24, 1, eq_tlv),
  98. SOC_ENUM("Equaliser EQ2 Bandwith", wm8974_enum[5]),
  99. SOC_ENUM("EQ2 Cut Off", wm8974_enum[6]),
  100. SOC_SINGLE_TLV("EQ2 Volume", WM8974_EQ2, 0, 24, 1, eq_tlv),
  101. SOC_ENUM("Equaliser EQ3 Bandwith", wm8974_enum[7]),
  102. SOC_ENUM("EQ3 Cut Off", wm8974_enum[8]),
  103. SOC_SINGLE_TLV("EQ3 Volume", WM8974_EQ3, 0, 24, 1, eq_tlv),
  104. SOC_ENUM("Equaliser EQ4 Bandwith", wm8974_enum[9]),
  105. SOC_ENUM("EQ4 Cut Off", wm8974_enum[10]),
  106. SOC_SINGLE_TLV("EQ4 Volume", WM8974_EQ4, 0, 24, 1, eq_tlv),
  107. SOC_ENUM("Equaliser EQ5 Bandwith", wm8974_enum[11]),
  108. SOC_ENUM("EQ5 Cut Off", wm8974_enum[12]),
  109. SOC_SINGLE_TLV("EQ5 Volume", WM8974_EQ5, 0, 24, 1, eq_tlv),
  110. SOC_SINGLE("DAC Playback Limiter Switch", WM8974_DACLIM1, 8, 1, 0),
  111. SOC_SINGLE("DAC Playback Limiter Decay", WM8974_DACLIM1, 4, 15, 0),
  112. SOC_SINGLE("DAC Playback Limiter Attack", WM8974_DACLIM1, 0, 15, 0),
  113. SOC_SINGLE("DAC Playback Limiter Threshold", WM8974_DACLIM2, 4, 7, 0),
  114. SOC_SINGLE("DAC Playback Limiter Boost", WM8974_DACLIM2, 0, 15, 0),
  115. SOC_SINGLE("ALC Enable Switch", WM8974_ALC1, 8, 1, 0),
  116. SOC_SINGLE("ALC Capture Max Gain", WM8974_ALC1, 3, 7, 0),
  117. SOC_SINGLE("ALC Capture Min Gain", WM8974_ALC1, 0, 7, 0),
  118. SOC_SINGLE("ALC Capture ZC Switch", WM8974_ALC2, 8, 1, 0),
  119. SOC_SINGLE("ALC Capture Hold", WM8974_ALC2, 4, 7, 0),
  120. SOC_SINGLE("ALC Capture Target", WM8974_ALC2, 0, 15, 0),
  121. SOC_ENUM("ALC Capture Mode", wm8974_enum[13]),
  122. SOC_SINGLE("ALC Capture Decay", WM8974_ALC3, 4, 15, 0),
  123. SOC_SINGLE("ALC Capture Attack", WM8974_ALC3, 0, 15, 0),
  124. SOC_SINGLE("ALC Capture Noise Gate Switch", WM8974_NGATE, 3, 1, 0),
  125. SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8974_NGATE, 0, 7, 0),
  126. SOC_SINGLE("Capture PGA ZC Switch", WM8974_INPPGA, 7, 1, 0),
  127. SOC_SINGLE_TLV("Capture PGA Volume", WM8974_INPPGA, 0, 63, 0, inpga_tlv),
  128. SOC_SINGLE("Speaker Playback ZC Switch", WM8974_SPKVOL, 7, 1, 0),
  129. SOC_SINGLE("Speaker Playback Switch", WM8974_SPKVOL, 6, 1, 1),
  130. SOC_SINGLE_TLV("Speaker Playback Volume", WM8974_SPKVOL, 0, 63, 0, spk_tlv),
  131. SOC_ENUM("Aux Mode", wm8974_auxmode),
  132. SOC_SINGLE("Capture Boost(+20dB)", WM8974_ADCBOOST, 8, 1, 0),
  133. SOC_SINGLE("Mono Playback Switch", WM8974_MONOMIX, 6, 1, 1),
  134. /* DAC / ADC oversampling */
  135. SOC_SINGLE("DAC 128x Oversampling Switch", WM8974_DAC, 8, 1, 0),
  136. SOC_SINGLE("ADC 128x Oversampling Switch", WM8974_ADC, 8, 1, 0),
  137. };
  138. /* Speaker Output Mixer */
  139. static const struct snd_kcontrol_new wm8974_speaker_mixer_controls[] = {
  140. SOC_DAPM_SINGLE("Line Bypass Switch", WM8974_SPKMIX, 1, 1, 0),
  141. SOC_DAPM_SINGLE("Aux Playback Switch", WM8974_SPKMIX, 5, 1, 0),
  142. SOC_DAPM_SINGLE("PCM Playback Switch", WM8974_SPKMIX, 0, 1, 0),
  143. };
  144. /* Mono Output Mixer */
  145. static const struct snd_kcontrol_new wm8974_mono_mixer_controls[] = {
  146. SOC_DAPM_SINGLE("Line Bypass Switch", WM8974_MONOMIX, 1, 1, 0),
  147. SOC_DAPM_SINGLE("Aux Playback Switch", WM8974_MONOMIX, 2, 1, 0),
  148. SOC_DAPM_SINGLE("PCM Playback Switch", WM8974_MONOMIX, 0, 1, 0),
  149. };
  150. /* Boost mixer */
  151. static const struct snd_kcontrol_new wm8974_boost_mixer[] = {
  152. SOC_DAPM_SINGLE("Aux Switch", WM8974_INPPGA, 6, 1, 0),
  153. };
  154. /* Input PGA */
  155. static const struct snd_kcontrol_new wm8974_inpga[] = {
  156. SOC_DAPM_SINGLE("Aux Switch", WM8974_INPUT, 2, 1, 0),
  157. SOC_DAPM_SINGLE("MicN Switch", WM8974_INPUT, 1, 1, 0),
  158. SOC_DAPM_SINGLE("MicP Switch", WM8974_INPUT, 0, 1, 0),
  159. };
  160. /* AUX Input boost vol */
  161. static const struct snd_kcontrol_new wm8974_aux_boost_controls =
  162. SOC_DAPM_SINGLE("Aux Volume", WM8974_ADCBOOST, 0, 7, 0);
  163. /* Mic Input boost vol */
  164. static const struct snd_kcontrol_new wm8974_mic_boost_controls =
  165. SOC_DAPM_SINGLE("Mic Volume", WM8974_ADCBOOST, 4, 7, 0);
  166. static const struct snd_soc_dapm_widget wm8974_dapm_widgets[] = {
  167. SND_SOC_DAPM_MIXER("Speaker Mixer", WM8974_POWER3, 2, 0,
  168. &wm8974_speaker_mixer_controls[0],
  169. ARRAY_SIZE(wm8974_speaker_mixer_controls)),
  170. SND_SOC_DAPM_MIXER("Mono Mixer", WM8974_POWER3, 3, 0,
  171. &wm8974_mono_mixer_controls[0],
  172. ARRAY_SIZE(wm8974_mono_mixer_controls)),
  173. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8974_POWER3, 0, 0),
  174. SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8974_POWER2, 0, 0),
  175. SND_SOC_DAPM_PGA("Aux Input", WM8974_POWER1, 6, 0, NULL, 0),
  176. SND_SOC_DAPM_PGA("SpkN Out", WM8974_POWER3, 5, 0, NULL, 0),
  177. SND_SOC_DAPM_PGA("SpkP Out", WM8974_POWER3, 6, 0, NULL, 0),
  178. SND_SOC_DAPM_PGA("Mono Out", WM8974_POWER3, 7, 0, NULL, 0),
  179. SND_SOC_DAPM_MIXER("Input PGA", WM8974_POWER2, 2, 0, wm8974_inpga,
  180. ARRAY_SIZE(wm8974_inpga)),
  181. SND_SOC_DAPM_MIXER("Boost Mixer", WM8974_POWER2, 4, 0,
  182. wm8974_boost_mixer, ARRAY_SIZE(wm8974_boost_mixer)),
  183. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8974_POWER1, 4, 0),
  184. SND_SOC_DAPM_INPUT("MICN"),
  185. SND_SOC_DAPM_INPUT("MICP"),
  186. SND_SOC_DAPM_INPUT("AUX"),
  187. SND_SOC_DAPM_OUTPUT("MONOOUT"),
  188. SND_SOC_DAPM_OUTPUT("SPKOUTP"),
  189. SND_SOC_DAPM_OUTPUT("SPKOUTN"),
  190. };
  191. static const struct snd_soc_dapm_route audio_map[] = {
  192. /* Mono output mixer */
  193. {"Mono Mixer", "PCM Playback Switch", "DAC"},
  194. {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
  195. {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
  196. /* Speaker output mixer */
  197. {"Speaker Mixer", "PCM Playback Switch", "DAC"},
  198. {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
  199. {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
  200. /* Outputs */
  201. {"Mono Out", NULL, "Mono Mixer"},
  202. {"MONOOUT", NULL, "Mono Out"},
  203. {"SpkN Out", NULL, "Speaker Mixer"},
  204. {"SpkP Out", NULL, "Speaker Mixer"},
  205. {"SPKOUTN", NULL, "SpkN Out"},
  206. {"SPKOUTP", NULL, "SpkP Out"},
  207. /* Boost Mixer */
  208. {"ADC", NULL, "Boost Mixer"},
  209. {"Boost Mixer", "Aux Switch", "Aux Input"},
  210. {"Boost Mixer", NULL, "Input PGA"},
  211. {"Boost Mixer", NULL, "MICP"},
  212. /* Input PGA */
  213. {"Input PGA", "Aux Switch", "Aux Input"},
  214. {"Input PGA", "MicN Switch", "MICN"},
  215. {"Input PGA", "MicP Switch", "MICP"},
  216. /* Inputs */
  217. {"Aux Input", NULL, "AUX"},
  218. };
  219. static int wm8974_add_widgets(struct snd_soc_codec *codec)
  220. {
  221. struct snd_soc_dapm_context *dapm = &codec->dapm;
  222. snd_soc_dapm_new_controls(dapm, wm8974_dapm_widgets,
  223. ARRAY_SIZE(wm8974_dapm_widgets));
  224. snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
  225. return 0;
  226. }
  227. struct pll_ {
  228. unsigned int pre_div:1;
  229. unsigned int n:4;
  230. unsigned int k;
  231. };
  232. /* The size in bits of the pll divide multiplied by 10
  233. * to allow rounding later */
  234. #define FIXED_PLL_SIZE ((1 << 24) * 10)
  235. static void pll_factors(struct pll_ *pll_div,
  236. unsigned int target, unsigned int source)
  237. {
  238. unsigned long long Kpart;
  239. unsigned int K, Ndiv, Nmod;
  240. /* There is a fixed divide by 4 in the output path */
  241. target *= 4;
  242. Ndiv = target / source;
  243. if (Ndiv < 6) {
  244. source /= 2;
  245. pll_div->pre_div = 1;
  246. Ndiv = target / source;
  247. } else
  248. pll_div->pre_div = 0;
  249. if ((Ndiv < 6) || (Ndiv > 12))
  250. printk(KERN_WARNING
  251. "WM8974 N value %u outwith recommended range!\n",
  252. Ndiv);
  253. pll_div->n = Ndiv;
  254. Nmod = target % source;
  255. Kpart = FIXED_PLL_SIZE * (long long)Nmod;
  256. do_div(Kpart, source);
  257. K = Kpart & 0xFFFFFFFF;
  258. /* Check if we need to round */
  259. if ((K % 10) >= 5)
  260. K += 5;
  261. /* Move down to proper range now rounding is done */
  262. K /= 10;
  263. pll_div->k = K;
  264. }
  265. static int wm8974_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  266. int source, unsigned int freq_in, unsigned int freq_out)
  267. {
  268. struct snd_soc_codec *codec = codec_dai->codec;
  269. struct pll_ pll_div;
  270. u16 reg;
  271. if (freq_in == 0 || freq_out == 0) {
  272. /* Clock CODEC directly from MCLK */
  273. reg = snd_soc_read(codec, WM8974_CLOCK);
  274. snd_soc_write(codec, WM8974_CLOCK, reg & 0x0ff);
  275. /* Turn off PLL */
  276. reg = snd_soc_read(codec, WM8974_POWER1);
  277. snd_soc_write(codec, WM8974_POWER1, reg & 0x1df);
  278. return 0;
  279. }
  280. pll_factors(&pll_div, freq_out, freq_in);
  281. snd_soc_write(codec, WM8974_PLLN, (pll_div.pre_div << 4) | pll_div.n);
  282. snd_soc_write(codec, WM8974_PLLK1, pll_div.k >> 18);
  283. snd_soc_write(codec, WM8974_PLLK2, (pll_div.k >> 9) & 0x1ff);
  284. snd_soc_write(codec, WM8974_PLLK3, pll_div.k & 0x1ff);
  285. reg = snd_soc_read(codec, WM8974_POWER1);
  286. snd_soc_write(codec, WM8974_POWER1, reg | 0x020);
  287. /* Run CODEC from PLL instead of MCLK */
  288. reg = snd_soc_read(codec, WM8974_CLOCK);
  289. snd_soc_write(codec, WM8974_CLOCK, reg | 0x100);
  290. return 0;
  291. }
  292. /*
  293. * Configure WM8974 clock dividers.
  294. */
  295. static int wm8974_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  296. int div_id, int div)
  297. {
  298. struct snd_soc_codec *codec = codec_dai->codec;
  299. u16 reg;
  300. switch (div_id) {
  301. case WM8974_OPCLKDIV:
  302. reg = snd_soc_read(codec, WM8974_GPIO) & 0x1cf;
  303. snd_soc_write(codec, WM8974_GPIO, reg | div);
  304. break;
  305. case WM8974_MCLKDIV:
  306. reg = snd_soc_read(codec, WM8974_CLOCK) & 0x11f;
  307. snd_soc_write(codec, WM8974_CLOCK, reg | div);
  308. break;
  309. case WM8974_BCLKDIV:
  310. reg = snd_soc_read(codec, WM8974_CLOCK) & 0x1e3;
  311. snd_soc_write(codec, WM8974_CLOCK, reg | div);
  312. break;
  313. default:
  314. return -EINVAL;
  315. }
  316. return 0;
  317. }
  318. static int wm8974_set_dai_fmt(struct snd_soc_dai *codec_dai,
  319. unsigned int fmt)
  320. {
  321. struct snd_soc_codec *codec = codec_dai->codec;
  322. u16 iface = 0;
  323. u16 clk = snd_soc_read(codec, WM8974_CLOCK) & 0x1fe;
  324. /* set master/slave audio interface */
  325. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  326. case SND_SOC_DAIFMT_CBM_CFM:
  327. clk |= 0x0001;
  328. break;
  329. case SND_SOC_DAIFMT_CBS_CFS:
  330. break;
  331. default:
  332. return -EINVAL;
  333. }
  334. /* interface format */
  335. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  336. case SND_SOC_DAIFMT_I2S:
  337. iface |= 0x0010;
  338. break;
  339. case SND_SOC_DAIFMT_RIGHT_J:
  340. break;
  341. case SND_SOC_DAIFMT_LEFT_J:
  342. iface |= 0x0008;
  343. break;
  344. case SND_SOC_DAIFMT_DSP_A:
  345. iface |= 0x00018;
  346. break;
  347. default:
  348. return -EINVAL;
  349. }
  350. /* clock inversion */
  351. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  352. case SND_SOC_DAIFMT_NB_NF:
  353. break;
  354. case SND_SOC_DAIFMT_IB_IF:
  355. iface |= 0x0180;
  356. break;
  357. case SND_SOC_DAIFMT_IB_NF:
  358. iface |= 0x0100;
  359. break;
  360. case SND_SOC_DAIFMT_NB_IF:
  361. iface |= 0x0080;
  362. break;
  363. default:
  364. return -EINVAL;
  365. }
  366. snd_soc_write(codec, WM8974_IFACE, iface);
  367. snd_soc_write(codec, WM8974_CLOCK, clk);
  368. return 0;
  369. }
  370. static int wm8974_pcm_hw_params(struct snd_pcm_substream *substream,
  371. struct snd_pcm_hw_params *params,
  372. struct snd_soc_dai *dai)
  373. {
  374. struct snd_soc_codec *codec = dai->codec;
  375. u16 iface = snd_soc_read(codec, WM8974_IFACE) & 0x19f;
  376. u16 adn = snd_soc_read(codec, WM8974_ADD) & 0x1f1;
  377. /* bit size */
  378. switch (params_format(params)) {
  379. case SNDRV_PCM_FORMAT_S16_LE:
  380. break;
  381. case SNDRV_PCM_FORMAT_S20_3LE:
  382. iface |= 0x0020;
  383. break;
  384. case SNDRV_PCM_FORMAT_S24_LE:
  385. iface |= 0x0040;
  386. break;
  387. case SNDRV_PCM_FORMAT_S32_LE:
  388. iface |= 0x0060;
  389. break;
  390. }
  391. /* filter coefficient */
  392. switch (params_rate(params)) {
  393. case 8000:
  394. adn |= 0x5 << 1;
  395. break;
  396. case 11025:
  397. adn |= 0x4 << 1;
  398. break;
  399. case 16000:
  400. adn |= 0x3 << 1;
  401. break;
  402. case 22050:
  403. adn |= 0x2 << 1;
  404. break;
  405. case 32000:
  406. adn |= 0x1 << 1;
  407. break;
  408. case 44100:
  409. case 48000:
  410. break;
  411. }
  412. snd_soc_write(codec, WM8974_IFACE, iface);
  413. snd_soc_write(codec, WM8974_ADD, adn);
  414. return 0;
  415. }
  416. static int wm8974_mute(struct snd_soc_dai *dai, int mute)
  417. {
  418. struct snd_soc_codec *codec = dai->codec;
  419. u16 mute_reg = snd_soc_read(codec, WM8974_DAC) & 0xffbf;
  420. if (mute)
  421. snd_soc_write(codec, WM8974_DAC, mute_reg | 0x40);
  422. else
  423. snd_soc_write(codec, WM8974_DAC, mute_reg);
  424. return 0;
  425. }
  426. /* liam need to make this lower power with dapm */
  427. static int wm8974_set_bias_level(struct snd_soc_codec *codec,
  428. enum snd_soc_bias_level level)
  429. {
  430. u16 power1 = snd_soc_read(codec, WM8974_POWER1) & ~0x3;
  431. switch (level) {
  432. case SND_SOC_BIAS_ON:
  433. case SND_SOC_BIAS_PREPARE:
  434. power1 |= 0x1; /* VMID 50k */
  435. snd_soc_write(codec, WM8974_POWER1, power1);
  436. break;
  437. case SND_SOC_BIAS_STANDBY:
  438. power1 |= WM8974_POWER1_BIASEN | WM8974_POWER1_BUFIOEN;
  439. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  440. /* Initial cap charge at VMID 5k */
  441. snd_soc_write(codec, WM8974_POWER1, power1 | 0x3);
  442. mdelay(100);
  443. }
  444. power1 |= 0x2; /* VMID 500k */
  445. snd_soc_write(codec, WM8974_POWER1, power1);
  446. break;
  447. case SND_SOC_BIAS_OFF:
  448. snd_soc_write(codec, WM8974_POWER1, 0);
  449. snd_soc_write(codec, WM8974_POWER2, 0);
  450. snd_soc_write(codec, WM8974_POWER3, 0);
  451. break;
  452. }
  453. codec->dapm.bias_level = level;
  454. return 0;
  455. }
  456. #define WM8974_RATES (SNDRV_PCM_RATE_8000_48000)
  457. #define WM8974_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  458. SNDRV_PCM_FMTBIT_S24_LE)
  459. static struct snd_soc_dai_ops wm8974_ops = {
  460. .hw_params = wm8974_pcm_hw_params,
  461. .digital_mute = wm8974_mute,
  462. .set_fmt = wm8974_set_dai_fmt,
  463. .set_clkdiv = wm8974_set_dai_clkdiv,
  464. .set_pll = wm8974_set_dai_pll,
  465. };
  466. static struct snd_soc_dai_driver wm8974_dai = {
  467. .name = "wm8974-hifi",
  468. .playback = {
  469. .stream_name = "Playback",
  470. .channels_min = 1,
  471. .channels_max = 2, /* Only 1 channel of data */
  472. .rates = WM8974_RATES,
  473. .formats = WM8974_FORMATS,},
  474. .capture = {
  475. .stream_name = "Capture",
  476. .channels_min = 1,
  477. .channels_max = 2, /* Only 1 channel of data */
  478. .rates = WM8974_RATES,
  479. .formats = WM8974_FORMATS,},
  480. .ops = &wm8974_ops,
  481. .symmetric_rates = 1,
  482. };
  483. static int wm8974_suspend(struct snd_soc_codec *codec, pm_message_t state)
  484. {
  485. wm8974_set_bias_level(codec, SND_SOC_BIAS_OFF);
  486. return 0;
  487. }
  488. static int wm8974_resume(struct snd_soc_codec *codec)
  489. {
  490. int i;
  491. u8 data[2];
  492. u16 *cache = codec->reg_cache;
  493. /* Sync reg_cache with the hardware */
  494. for (i = 0; i < ARRAY_SIZE(wm8974_reg); i++) {
  495. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  496. data[1] = cache[i] & 0x00ff;
  497. codec->hw_write(codec->control_data, data, 2);
  498. }
  499. wm8974_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  500. return 0;
  501. }
  502. static int wm8974_probe(struct snd_soc_codec *codec)
  503. {
  504. int ret = 0;
  505. ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_I2C);
  506. if (ret < 0) {
  507. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  508. return ret;
  509. }
  510. ret = wm8974_reset(codec);
  511. if (ret < 0) {
  512. dev_err(codec->dev, "Failed to issue reset\n");
  513. return ret;
  514. }
  515. wm8974_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  516. snd_soc_add_controls(codec, wm8974_snd_controls,
  517. ARRAY_SIZE(wm8974_snd_controls));
  518. wm8974_add_widgets(codec);
  519. return ret;
  520. }
  521. /* power down chip */
  522. static int wm8974_remove(struct snd_soc_codec *codec)
  523. {
  524. wm8974_set_bias_level(codec, SND_SOC_BIAS_OFF);
  525. return 0;
  526. }
  527. static struct snd_soc_codec_driver soc_codec_dev_wm8974 = {
  528. .probe = wm8974_probe,
  529. .remove = wm8974_remove,
  530. .suspend = wm8974_suspend,
  531. .resume = wm8974_resume,
  532. .set_bias_level = wm8974_set_bias_level,
  533. .reg_cache_size = ARRAY_SIZE(wm8974_reg),
  534. .reg_word_size = sizeof(u16),
  535. .reg_cache_default = wm8974_reg,
  536. };
  537. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  538. static __devinit int wm8974_i2c_probe(struct i2c_client *i2c,
  539. const struct i2c_device_id *id)
  540. {
  541. struct wm8974_priv *wm8974;
  542. int ret;
  543. wm8974 = kzalloc(sizeof(struct wm8974_priv), GFP_KERNEL);
  544. if (wm8974 == NULL)
  545. return -ENOMEM;
  546. i2c_set_clientdata(i2c, wm8974);
  547. ret = snd_soc_register_codec(&i2c->dev,
  548. &soc_codec_dev_wm8974, &wm8974_dai, 1);
  549. if (ret < 0)
  550. kfree(wm8974);
  551. return ret;
  552. }
  553. static __devexit int wm8974_i2c_remove(struct i2c_client *client)
  554. {
  555. snd_soc_unregister_codec(&client->dev);
  556. kfree(i2c_get_clientdata(client));
  557. return 0;
  558. }
  559. static const struct i2c_device_id wm8974_i2c_id[] = {
  560. { "wm8974", 0 },
  561. { }
  562. };
  563. MODULE_DEVICE_TABLE(i2c, wm8974_i2c_id);
  564. static struct i2c_driver wm8974_i2c_driver = {
  565. .driver = {
  566. .name = "wm8974-codec",
  567. .owner = THIS_MODULE,
  568. },
  569. .probe = wm8974_i2c_probe,
  570. .remove = __devexit_p(wm8974_i2c_remove),
  571. .id_table = wm8974_i2c_id,
  572. };
  573. #endif
  574. static int __init wm8974_modinit(void)
  575. {
  576. int ret = 0;
  577. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  578. ret = i2c_add_driver(&wm8974_i2c_driver);
  579. if (ret != 0) {
  580. printk(KERN_ERR "Failed to register wm8974 I2C driver: %d\n",
  581. ret);
  582. }
  583. #endif
  584. return ret;
  585. }
  586. module_init(wm8974_modinit);
  587. static void __exit wm8974_exit(void)
  588. {
  589. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  590. i2c_del_driver(&wm8974_i2c_driver);
  591. #endif
  592. }
  593. module_exit(wm8974_exit);
  594. MODULE_DESCRIPTION("ASoC WM8974 driver");
  595. MODULE_AUTHOR("Liam Girdwood");
  596. MODULE_LICENSE("GPL");