zylonite.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * zylonite.c -- SoC audio for Zylonite
  3. *
  4. * Copyright 2008 Wolfson Microelectronics PLC.
  5. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/device.h>
  16. #include <linux/clk.h>
  17. #include <linux/i2c.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dapm.h>
  23. #include "../codecs/wm9713.h"
  24. #include "pxa2xx-ac97.h"
  25. #include "pxa-ssp.h"
  26. /*
  27. * There is a physical switch SW15 on the board which changes the MCLK
  28. * for the WM9713 between the standard AC97 master clock and the
  29. * output of the CLK_POUT signal from the PXA.
  30. */
  31. static int clk_pout;
  32. module_param(clk_pout, int, 0);
  33. MODULE_PARM_DESC(clk_pout, "Use CLK_POUT as WM9713 MCLK (SW15 on board).");
  34. static struct clk *pout;
  35. static struct snd_soc_card zylonite;
  36. static const struct snd_soc_dapm_widget zylonite_dapm_widgets[] = {
  37. SND_SOC_DAPM_HP("Headphone", NULL),
  38. SND_SOC_DAPM_MIC("Headset Microphone", NULL),
  39. SND_SOC_DAPM_MIC("Handset Microphone", NULL),
  40. SND_SOC_DAPM_SPK("Multiactor", NULL),
  41. SND_SOC_DAPM_SPK("Headset Earpiece", NULL),
  42. };
  43. /* Currently supported audio map */
  44. static const struct snd_soc_dapm_route audio_map[] = {
  45. /* Headphone output connected to HPL/HPR */
  46. { "Headphone", NULL, "HPL" },
  47. { "Headphone", NULL, "HPR" },
  48. /* On-board earpiece */
  49. { "Headset Earpiece", NULL, "OUT3" },
  50. /* Headphone mic */
  51. { "MIC2A", NULL, "Mic Bias" },
  52. { "Mic Bias", NULL, "Headset Microphone" },
  53. /* On-board mic */
  54. { "MIC1", NULL, "Mic Bias" },
  55. { "Mic Bias", NULL, "Handset Microphone" },
  56. /* Multiactor differentially connected over SPKL/SPKR */
  57. { "Multiactor", NULL, "SPKL" },
  58. { "Multiactor", NULL, "SPKR" },
  59. };
  60. static int zylonite_wm9713_init(struct snd_soc_pcm_runtime *rtd)
  61. {
  62. struct snd_soc_codec *codec = rtd->codec;
  63. if (clk_pout)
  64. snd_soc_dai_set_pll(rtd->codec_dai, 0, 0,
  65. clk_get_rate(pout), 0);
  66. snd_soc_dapm_new_controls(codec, zylonite_dapm_widgets,
  67. ARRAY_SIZE(zylonite_dapm_widgets));
  68. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  69. /* Static setup for now */
  70. snd_soc_dapm_enable_pin(codec, "Headphone");
  71. snd_soc_dapm_enable_pin(codec, "Headset Earpiece");
  72. snd_soc_dapm_sync(codec);
  73. return 0;
  74. }
  75. static int zylonite_voice_hw_params(struct snd_pcm_substream *substream,
  76. struct snd_pcm_hw_params *params)
  77. {
  78. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  79. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  80. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  81. unsigned int pll_out = 0;
  82. unsigned int wm9713_div = 0;
  83. int ret = 0;
  84. int rate = params_rate(params);
  85. int width = snd_pcm_format_physical_width(params_format(params));
  86. /* Only support ratios that we can generate neatly from the AC97
  87. * based master clock - in particular, this excludes 44.1kHz.
  88. * In most applications the voice DAC will be used for telephony
  89. * data so multiples of 8kHz will be the common case.
  90. */
  91. switch (rate) {
  92. case 8000:
  93. wm9713_div = 12;
  94. break;
  95. case 16000:
  96. wm9713_div = 6;
  97. break;
  98. case 48000:
  99. wm9713_div = 2;
  100. break;
  101. default:
  102. /* Don't support OSS emulation */
  103. return -EINVAL;
  104. }
  105. /* Add 1 to the width for the leading clock cycle */
  106. pll_out = rate * (width + 1) * 8;
  107. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0, 1);
  108. if (ret < 0)
  109. return ret;
  110. ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, pll_out);
  111. if (ret < 0)
  112. return ret;
  113. if (clk_pout)
  114. ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_PLL_DIV,
  115. WM9713_PCMDIV(wm9713_div));
  116. else
  117. ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_DIV,
  118. WM9713_PCMDIV(wm9713_div));
  119. if (ret < 0)
  120. return ret;
  121. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  122. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  123. if (ret < 0)
  124. return ret;
  125. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  126. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  127. if (ret < 0)
  128. return ret;
  129. return 0;
  130. }
  131. static struct snd_soc_ops zylonite_voice_ops = {
  132. .hw_params = zylonite_voice_hw_params,
  133. };
  134. static struct snd_soc_dai_link zylonite_dai[] = {
  135. {
  136. .name = "AC97",
  137. .stream_name = "AC97 HiFi",
  138. .codec_name = "wm9713-codec",
  139. .platform_name = "pxa-pcm-audio",
  140. .cpu_dai_name = "pxa-ac97.0",
  141. .codec_name = "wm9713-hifi",
  142. .init = zylonite_wm9713_init,
  143. },
  144. {
  145. .name = "AC97 Aux",
  146. .stream_name = "AC97 Aux",
  147. .codec_name = "wm9713-codec",
  148. .platform_name = "pxa-pcm-audio",
  149. .cpu_dai_name = "pxa-ac97.1",
  150. .codec_name = "wm9713-aux",
  151. },
  152. {
  153. .name = "WM9713 Voice",
  154. .stream_name = "WM9713 Voice",
  155. .codec_name = "wm9713-codec",
  156. .platform_name = "pxa-pcm-audio",
  157. .cpu_dai_name = "pxa-ssp-dai.2",
  158. .codec_name = "wm9713-voice",
  159. .ops = &zylonite_voice_ops,
  160. },
  161. };
  162. static int zylonite_probe(struct platform_device *pdev)
  163. {
  164. int ret;
  165. if (clk_pout) {
  166. pout = clk_get(NULL, "CLK_POUT");
  167. if (IS_ERR(pout)) {
  168. dev_err(&pdev->dev, "Unable to obtain CLK_POUT: %ld\n",
  169. PTR_ERR(pout));
  170. return PTR_ERR(pout);
  171. }
  172. ret = clk_enable(pout);
  173. if (ret != 0) {
  174. dev_err(&pdev->dev, "Unable to enable CLK_POUT: %d\n",
  175. ret);
  176. clk_put(pout);
  177. return ret;
  178. }
  179. dev_dbg(&pdev->dev, "MCLK enabled at %luHz\n",
  180. clk_get_rate(pout));
  181. }
  182. return 0;
  183. }
  184. static int zylonite_remove(struct platform_device *pdev)
  185. {
  186. if (clk_pout) {
  187. clk_disable(pout);
  188. clk_put(pout);
  189. }
  190. return 0;
  191. }
  192. static int zylonite_suspend_post(struct platform_device *pdev,
  193. pm_message_t state)
  194. {
  195. if (clk_pout)
  196. clk_disable(pout);
  197. return 0;
  198. }
  199. static int zylonite_resume_pre(struct platform_device *pdev)
  200. {
  201. int ret = 0;
  202. if (clk_pout) {
  203. ret = clk_enable(pout);
  204. if (ret != 0)
  205. dev_err(&pdev->dev, "Unable to enable CLK_POUT: %d\n",
  206. ret);
  207. }
  208. return ret;
  209. }
  210. static struct snd_soc_card zylonite = {
  211. .name = "Zylonite",
  212. .probe = &zylonite_probe,
  213. .remove = &zylonite_remove,
  214. .suspend_post = &zylonite_suspend_post,
  215. .resume_pre = &zylonite_resume_pre,
  216. .dai_link = zylonite_dai,
  217. .num_links = ARRAY_SIZE(zylonite_dai),
  218. .owner = THIS_MODULE,
  219. };
  220. static struct platform_device *zylonite_snd_ac97_device;
  221. static int __init zylonite_init(void)
  222. {
  223. int ret;
  224. zylonite_snd_ac97_device = platform_device_alloc("soc-audio", -1);
  225. if (!zylonite_snd_ac97_device)
  226. return -ENOMEM;
  227. platform_set_drvdata(zylonite_snd_ac97_device, &zylonite);
  228. ret = platform_device_add(zylonite_snd_ac97_device);
  229. if (ret != 0)
  230. platform_device_put(zylonite_snd_ac97_device);
  231. return ret;
  232. }
  233. static void __exit zylonite_exit(void)
  234. {
  235. platform_device_unregister(zylonite_snd_ac97_device);
  236. }
  237. module_init(zylonite_init);
  238. module_exit(zylonite_exit);
  239. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  240. MODULE_DESCRIPTION("ALSA SoC WM9713 Zylonite");
  241. MODULE_LICENSE("GPL");