zylonite.c 7.0 KB

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