wm9705.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * wm9705.c -- ALSA Soc WM9705 codec support
  3. *
  4. * Copyright 2008 Ian Molton <spyro@f2s.com>
  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; Version 2 of the License only.
  9. *
  10. */
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/device.h>
  16. #include <sound/core.h>
  17. #include <sound/pcm.h>
  18. #include <sound/ac97_codec.h>
  19. #include <sound/initval.h>
  20. #include <sound/soc.h>
  21. #include "wm9705.h"
  22. #define WM9705_VENDOR_ID 0x574d4c05
  23. #define WM9705_VENDOR_ID_MASK 0xffffffff
  24. /*
  25. * WM9705 register cache
  26. */
  27. static const u16 wm9705_reg[] = {
  28. 0x6150, 0x8000, 0x8000, 0x8000, /* 0x0 */
  29. 0x0000, 0x8000, 0x8008, 0x8008, /* 0x8 */
  30. 0x8808, 0x8808, 0x8808, 0x8808, /* 0x10 */
  31. 0x8808, 0x0000, 0x8000, 0x0000, /* 0x18 */
  32. 0x0000, 0x0000, 0x0000, 0x000f, /* 0x20 */
  33. 0x0605, 0x0000, 0xbb80, 0x0000, /* 0x28 */
  34. 0x0000, 0xbb80, 0x0000, 0x0000, /* 0x30 */
  35. 0x0000, 0x2000, 0x0000, 0x0000, /* 0x38 */
  36. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x40 */
  37. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x48 */
  38. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x50 */
  39. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x58 */
  40. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x60 */
  41. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x68 */
  42. 0x0000, 0x0808, 0x0000, 0x0006, /* 0x70 */
  43. 0x0000, 0x0000, 0x574d, 0x4c05, /* 0x78 */
  44. };
  45. static const struct snd_kcontrol_new wm9705_snd_ac97_controls[] = {
  46. SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
  47. SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
  48. SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
  49. SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
  50. SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
  51. SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
  52. SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
  53. SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
  54. SOC_SINGLE("PCBeep Playback Volume", AC97_PC_BEEP, 1, 15, 1),
  55. SOC_SINGLE("Phone Playback Volume", AC97_PHONE, 0, 31, 1),
  56. SOC_DOUBLE("Line Playback Volume", AC97_LINE, 8, 0, 31, 1),
  57. SOC_DOUBLE("CD Playback Volume", AC97_CD, 8, 0, 31, 1),
  58. SOC_SINGLE("Mic Playback Volume", AC97_MIC, 0, 31, 1),
  59. SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 6, 1, 0),
  60. SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0),
  61. SOC_SINGLE("Capture Switch", AC97_REC_GAIN, 15, 1, 1),
  62. };
  63. static const char *wm9705_mic[] = {"Mic 1", "Mic 2"};
  64. static const char *wm9705_rec_sel[] = {"Mic", "CD", "NC", "NC",
  65. "Line", "Stereo Mix", "Mono Mix", "Phone"};
  66. static SOC_ENUM_SINGLE_DECL(wm9705_enum_mic,
  67. AC97_GENERAL_PURPOSE, 8, wm9705_mic);
  68. static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_l,
  69. AC97_REC_SEL, 8, wm9705_rec_sel);
  70. static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_r,
  71. AC97_REC_SEL, 0, wm9705_rec_sel);
  72. /* Headphone Mixer */
  73. static const struct snd_kcontrol_new wm9705_hp_mixer_controls[] = {
  74. SOC_DAPM_SINGLE("PCBeep Playback Switch", AC97_PC_BEEP, 15, 1, 1),
  75. SOC_DAPM_SINGLE("CD Playback Switch", AC97_CD, 15, 1, 1),
  76. SOC_DAPM_SINGLE("Mic Playback Switch", AC97_MIC, 15, 1, 1),
  77. SOC_DAPM_SINGLE("Phone Playback Switch", AC97_PHONE, 15, 1, 1),
  78. SOC_DAPM_SINGLE("Line Playback Switch", AC97_LINE, 15, 1, 1),
  79. };
  80. /* Mic source */
  81. static const struct snd_kcontrol_new wm9705_mic_src_controls =
  82. SOC_DAPM_ENUM("Route", wm9705_enum_mic);
  83. /* Capture source */
  84. static const struct snd_kcontrol_new wm9705_capture_selectl_controls =
  85. SOC_DAPM_ENUM("Route", wm9705_enum_rec_l);
  86. static const struct snd_kcontrol_new wm9705_capture_selectr_controls =
  87. SOC_DAPM_ENUM("Route", wm9705_enum_rec_r);
  88. /* DAPM widgets */
  89. static const struct snd_soc_dapm_widget wm9705_dapm_widgets[] = {
  90. SND_SOC_DAPM_MUX("Mic Source", SND_SOC_NOPM, 0, 0,
  91. &wm9705_mic_src_controls),
  92. SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
  93. &wm9705_capture_selectl_controls),
  94. SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
  95. &wm9705_capture_selectr_controls),
  96. SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
  97. SND_SOC_NOPM, 0, 0),
  98. SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
  99. SND_SOC_NOPM, 0, 0),
  100. SND_SOC_DAPM_MIXER_NAMED_CTL("HP Mixer", SND_SOC_NOPM, 0, 0,
  101. &wm9705_hp_mixer_controls[0],
  102. ARRAY_SIZE(wm9705_hp_mixer_controls)),
  103. SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  104. SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
  105. SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
  106. SND_SOC_DAPM_PGA("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  107. SND_SOC_DAPM_PGA("Speaker PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  108. SND_SOC_DAPM_PGA("Line PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  109. SND_SOC_DAPM_PGA("Line out PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  110. SND_SOC_DAPM_PGA("Mono PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  111. SND_SOC_DAPM_PGA("Phone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  112. SND_SOC_DAPM_PGA("Mic PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  113. SND_SOC_DAPM_PGA("PCBEEP PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  114. SND_SOC_DAPM_PGA("CD PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  115. SND_SOC_DAPM_PGA("ADC PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  116. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  117. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  118. SND_SOC_DAPM_OUTPUT("LOUT"),
  119. SND_SOC_DAPM_OUTPUT("ROUT"),
  120. SND_SOC_DAPM_OUTPUT("MONOOUT"),
  121. SND_SOC_DAPM_INPUT("PHONE"),
  122. SND_SOC_DAPM_INPUT("LINEINL"),
  123. SND_SOC_DAPM_INPUT("LINEINR"),
  124. SND_SOC_DAPM_INPUT("CDINL"),
  125. SND_SOC_DAPM_INPUT("CDINR"),
  126. SND_SOC_DAPM_INPUT("PCBEEP"),
  127. SND_SOC_DAPM_INPUT("MIC1"),
  128. SND_SOC_DAPM_INPUT("MIC2"),
  129. };
  130. /* Audio map
  131. * WM9705 has no switches to disable the route from the inputs to the HP mixer
  132. * so in order to prevent active inputs from forcing the audio outputs to be
  133. * constantly enabled, we use the mutes on those inputs to simulate such
  134. * controls.
  135. */
  136. static const struct snd_soc_dapm_route wm9705_audio_map[] = {
  137. /* HP mixer */
  138. {"HP Mixer", "PCBeep Playback Switch", "PCBEEP PGA"},
  139. {"HP Mixer", "CD Playback Switch", "CD PGA"},
  140. {"HP Mixer", "Mic Playback Switch", "Mic PGA"},
  141. {"HP Mixer", "Phone Playback Switch", "Phone PGA"},
  142. {"HP Mixer", "Line Playback Switch", "Line PGA"},
  143. {"HP Mixer", NULL, "Left DAC"},
  144. {"HP Mixer", NULL, "Right DAC"},
  145. /* mono mixer */
  146. {"Mono Mixer", NULL, "HP Mixer"},
  147. /* outputs */
  148. {"Headphone PGA", NULL, "HP Mixer"},
  149. {"HPOUTL", NULL, "Headphone PGA"},
  150. {"HPOUTR", NULL, "Headphone PGA"},
  151. {"Line out PGA", NULL, "HP Mixer"},
  152. {"LOUT", NULL, "Line out PGA"},
  153. {"ROUT", NULL, "Line out PGA"},
  154. {"Mono PGA", NULL, "Mono Mixer"},
  155. {"MONOOUT", NULL, "Mono PGA"},
  156. /* inputs */
  157. {"CD PGA", NULL, "CDINL"},
  158. {"CD PGA", NULL, "CDINR"},
  159. {"Line PGA", NULL, "LINEINL"},
  160. {"Line PGA", NULL, "LINEINR"},
  161. {"Phone PGA", NULL, "PHONE"},
  162. {"Mic Source", "Mic 1", "MIC1"},
  163. {"Mic Source", "Mic 2", "MIC2"},
  164. {"Mic PGA", NULL, "Mic Source"},
  165. {"PCBEEP PGA", NULL, "PCBEEP"},
  166. /* Left capture selector */
  167. {"Left Capture Source", "Mic", "Mic Source"},
  168. {"Left Capture Source", "CD", "CDINL"},
  169. {"Left Capture Source", "Line", "LINEINL"},
  170. {"Left Capture Source", "Stereo Mix", "HP Mixer"},
  171. {"Left Capture Source", "Mono Mix", "HP Mixer"},
  172. {"Left Capture Source", "Phone", "PHONE"},
  173. /* Right capture source */
  174. {"Right Capture Source", "Mic", "Mic Source"},
  175. {"Right Capture Source", "CD", "CDINR"},
  176. {"Right Capture Source", "Line", "LINEINR"},
  177. {"Right Capture Source", "Stereo Mix", "HP Mixer"},
  178. {"Right Capture Source", "Mono Mix", "HP Mixer"},
  179. {"Right Capture Source", "Phone", "PHONE"},
  180. {"ADC PGA", NULL, "Left Capture Source"},
  181. {"ADC PGA", NULL, "Right Capture Source"},
  182. /* ADC's */
  183. {"Left ADC", NULL, "ADC PGA"},
  184. {"Right ADC", NULL, "ADC PGA"},
  185. };
  186. /* We use a register cache to enhance read performance. */
  187. static unsigned int ac97_read(struct snd_soc_codec *codec, unsigned int reg)
  188. {
  189. struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
  190. u16 *cache = codec->reg_cache;
  191. switch (reg) {
  192. case AC97_RESET:
  193. case AC97_VENDOR_ID1:
  194. case AC97_VENDOR_ID2:
  195. return soc_ac97_ops->read(ac97, reg);
  196. default:
  197. reg = reg >> 1;
  198. if (reg >= (ARRAY_SIZE(wm9705_reg)))
  199. return -EIO;
  200. return cache[reg];
  201. }
  202. }
  203. static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
  204. unsigned int val)
  205. {
  206. struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
  207. u16 *cache = codec->reg_cache;
  208. soc_ac97_ops->write(ac97, reg, val);
  209. reg = reg >> 1;
  210. if (reg < (ARRAY_SIZE(wm9705_reg)))
  211. cache[reg] = val;
  212. return 0;
  213. }
  214. static int ac97_prepare(struct snd_pcm_substream *substream,
  215. struct snd_soc_dai *dai)
  216. {
  217. struct snd_soc_codec *codec = dai->codec;
  218. int reg;
  219. u16 vra;
  220. vra = ac97_read(codec, AC97_EXTENDED_STATUS);
  221. ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
  222. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  223. reg = AC97_PCM_FRONT_DAC_RATE;
  224. else
  225. reg = AC97_PCM_LR_ADC_RATE;
  226. return ac97_write(codec, reg, substream->runtime->rate);
  227. }
  228. #define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \
  229. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  230. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  231. SNDRV_PCM_RATE_48000)
  232. static const struct snd_soc_dai_ops wm9705_dai_ops = {
  233. .prepare = ac97_prepare,
  234. };
  235. static struct snd_soc_dai_driver wm9705_dai[] = {
  236. {
  237. .name = "wm9705-hifi",
  238. .playback = {
  239. .stream_name = "HiFi Playback",
  240. .channels_min = 1,
  241. .channels_max = 2,
  242. .rates = WM9705_AC97_RATES,
  243. .formats = SND_SOC_STD_AC97_FMTS,
  244. },
  245. .capture = {
  246. .stream_name = "HiFi Capture",
  247. .channels_min = 1,
  248. .channels_max = 2,
  249. .rates = WM9705_AC97_RATES,
  250. .formats = SND_SOC_STD_AC97_FMTS,
  251. },
  252. .ops = &wm9705_dai_ops,
  253. },
  254. {
  255. .name = "wm9705-aux",
  256. .playback = {
  257. .stream_name = "Aux Playback",
  258. .channels_min = 1,
  259. .channels_max = 1,
  260. .rates = WM9705_AC97_RATES,
  261. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  262. },
  263. }
  264. };
  265. #ifdef CONFIG_PM
  266. static int wm9705_soc_suspend(struct snd_soc_codec *codec)
  267. {
  268. struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
  269. soc_ac97_ops->write(ac97, AC97_POWERDOWN, 0xffff);
  270. return 0;
  271. }
  272. static int wm9705_soc_resume(struct snd_soc_codec *codec)
  273. {
  274. struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
  275. int i, ret;
  276. u16 *cache = codec->reg_cache;
  277. ret = snd_ac97_reset(ac97, true, WM9705_VENDOR_ID,
  278. WM9705_VENDOR_ID_MASK);
  279. if (ret < 0)
  280. return ret;
  281. for (i = 2; i < ARRAY_SIZE(wm9705_reg) << 1; i += 2) {
  282. soc_ac97_ops->write(ac97, i, cache[i>>1]);
  283. }
  284. return 0;
  285. }
  286. #else
  287. #define wm9705_soc_suspend NULL
  288. #define wm9705_soc_resume NULL
  289. #endif
  290. static int wm9705_soc_probe(struct snd_soc_codec *codec)
  291. {
  292. struct snd_ac97 *ac97;
  293. ac97 = snd_soc_new_ac97_codec(codec, WM9705_VENDOR_ID,
  294. WM9705_VENDOR_ID_MASK);
  295. if (IS_ERR(ac97)) {
  296. dev_err(codec->dev, "Failed to register AC97 codec\n");
  297. return PTR_ERR(ac97);
  298. }
  299. snd_soc_codec_set_drvdata(codec, ac97);
  300. return 0;
  301. }
  302. static int wm9705_soc_remove(struct snd_soc_codec *codec)
  303. {
  304. struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
  305. snd_soc_free_ac97_codec(ac97);
  306. return 0;
  307. }
  308. static struct snd_soc_codec_driver soc_codec_dev_wm9705 = {
  309. .probe = wm9705_soc_probe,
  310. .remove = wm9705_soc_remove,
  311. .suspend = wm9705_soc_suspend,
  312. .resume = wm9705_soc_resume,
  313. .read = ac97_read,
  314. .write = ac97_write,
  315. .reg_cache_size = ARRAY_SIZE(wm9705_reg),
  316. .reg_word_size = sizeof(u16),
  317. .reg_cache_step = 2,
  318. .reg_cache_default = wm9705_reg,
  319. .controls = wm9705_snd_ac97_controls,
  320. .num_controls = ARRAY_SIZE(wm9705_snd_ac97_controls),
  321. .dapm_widgets = wm9705_dapm_widgets,
  322. .num_dapm_widgets = ARRAY_SIZE(wm9705_dapm_widgets),
  323. .dapm_routes = wm9705_audio_map,
  324. .num_dapm_routes = ARRAY_SIZE(wm9705_audio_map),
  325. };
  326. static int wm9705_probe(struct platform_device *pdev)
  327. {
  328. return snd_soc_register_codec(&pdev->dev,
  329. &soc_codec_dev_wm9705, wm9705_dai, ARRAY_SIZE(wm9705_dai));
  330. }
  331. static int wm9705_remove(struct platform_device *pdev)
  332. {
  333. snd_soc_unregister_codec(&pdev->dev);
  334. return 0;
  335. }
  336. static struct platform_driver wm9705_codec_driver = {
  337. .driver = {
  338. .name = "wm9705-codec",
  339. },
  340. .probe = wm9705_probe,
  341. .remove = wm9705_remove,
  342. };
  343. module_platform_driver(wm9705_codec_driver);
  344. MODULE_DESCRIPTION("ASoC WM9705 driver");
  345. MODULE_AUTHOR("Ian Molton");
  346. MODULE_LICENSE("GPL v2");