wm8974.c 19 KB

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