wm9705.c 12 KB

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