migor.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * ALSA SoC driver for Migo-R
  3. *
  4. * Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clkdev.h>
  11. #include <linux/device.h>
  12. #include <linux/firmware.h>
  13. #include <linux/module.h>
  14. #include <asm/clock.h>
  15. #include <cpu/sh7722.h>
  16. #include <sound/core.h>
  17. #include <sound/pcm.h>
  18. #include <sound/soc.h>
  19. #include "../codecs/wm8978.h"
  20. #include "siu.h"
  21. /* Default 8000Hz sampling frequency */
  22. static unsigned long codec_freq = 8000 * 512;
  23. static unsigned int use_count;
  24. /* External clock, sourced from the codec at the SIUMCKB pin */
  25. static unsigned long siumckb_recalc(struct clk *clk)
  26. {
  27. return codec_freq;
  28. }
  29. static struct clk_ops siumckb_clk_ops = {
  30. .recalc = siumckb_recalc,
  31. };
  32. static struct clk siumckb_clk = {
  33. .ops = &siumckb_clk_ops,
  34. .rate = 0, /* initialised at run-time */
  35. };
  36. static struct clk_lookup *siumckb_lookup;
  37. static int migor_hw_params(struct snd_pcm_substream *substream,
  38. struct snd_pcm_hw_params *params)
  39. {
  40. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  41. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  42. int ret;
  43. unsigned int rate = params_rate(params);
  44. ret = snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 13000000,
  45. SND_SOC_CLOCK_IN);
  46. if (ret < 0)
  47. return ret;
  48. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_OPCLKRATE, rate * 512);
  49. if (ret < 0)
  50. return ret;
  51. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_NB_IF |
  52. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS);
  53. if (ret < 0)
  54. return ret;
  55. ret = snd_soc_dai_set_fmt(rtd->cpu_dai, SND_SOC_DAIFMT_NB_IF |
  56. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS);
  57. if (ret < 0)
  58. return ret;
  59. codec_freq = rate * 512;
  60. /*
  61. * This propagates the parent frequency change to children and
  62. * recalculates the frequency table
  63. */
  64. clk_set_rate(&siumckb_clk, codec_freq);
  65. dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq);
  66. ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, SIU_CLKB_EXT,
  67. codec_freq / 2, SND_SOC_CLOCK_IN);
  68. if (!ret)
  69. use_count++;
  70. return ret;
  71. }
  72. static int migor_hw_free(struct snd_pcm_substream *substream)
  73. {
  74. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  75. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  76. if (use_count) {
  77. use_count--;
  78. if (!use_count)
  79. snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0,
  80. SND_SOC_CLOCK_IN);
  81. } else {
  82. dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n");
  83. }
  84. return 0;
  85. }
  86. static struct snd_soc_ops migor_dai_ops = {
  87. .hw_params = migor_hw_params,
  88. .hw_free = migor_hw_free,
  89. };
  90. static const struct snd_soc_dapm_widget migor_dapm_widgets[] = {
  91. SND_SOC_DAPM_HP("Headphone", NULL),
  92. SND_SOC_DAPM_MIC("Onboard Microphone", NULL),
  93. SND_SOC_DAPM_MIC("External Microphone", NULL),
  94. };
  95. static const struct snd_soc_dapm_route audio_map[] = {
  96. /* Headphone output connected to LHP/RHP, enable OUT4 for VMID */
  97. { "Headphone", NULL, "OUT4 VMID" },
  98. { "OUT4 VMID", NULL, "LHP" },
  99. { "OUT4 VMID", NULL, "RHP" },
  100. /* On-board microphone */
  101. { "RMICN", NULL, "Mic Bias" },
  102. { "RMICP", NULL, "Mic Bias" },
  103. { "Mic Bias", NULL, "Onboard Microphone" },
  104. /* External microphone */
  105. { "LMICN", NULL, "Mic Bias" },
  106. { "LMICP", NULL, "Mic Bias" },
  107. { "Mic Bias", NULL, "External Microphone" },
  108. };
  109. static int migor_dai_init(struct snd_soc_pcm_runtime *rtd)
  110. {
  111. struct snd_soc_codec *codec = rtd->codec;
  112. struct snd_soc_dapm_context *dapm = &codec->dapm;
  113. snd_soc_dapm_new_controls(dapm, migor_dapm_widgets,
  114. ARRAY_SIZE(migor_dapm_widgets));
  115. snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
  116. return 0;
  117. }
  118. /* migor digital audio interface glue - connects codec <--> CPU */
  119. static struct snd_soc_dai_link migor_dai = {
  120. .name = "wm8978",
  121. .stream_name = "WM8978",
  122. .cpu_dai_name = "siu-i2s-dai",
  123. .codec_dai_name = "wm8978-hifi",
  124. .platform_name = "siu-pcm-audio",
  125. .codec_name = "wm8978.0-001a",
  126. .ops = &migor_dai_ops,
  127. .init = migor_dai_init,
  128. };
  129. /* migor audio machine driver */
  130. static struct snd_soc_card snd_soc_migor = {
  131. .name = "Migo-R",
  132. .dai_link = &migor_dai,
  133. .num_links = 1,
  134. };
  135. static struct platform_device *migor_snd_device;
  136. static int __init migor_init(void)
  137. {
  138. int ret;
  139. ret = clk_register(&siumckb_clk);
  140. if (ret < 0)
  141. return ret;
  142. siumckb_lookup = clkdev_alloc(&siumckb_clk, "siumckb_clk", NULL);
  143. if (!siumckb_lookup) {
  144. ret = -ENOMEM;
  145. goto eclkdevalloc;
  146. }
  147. clkdev_add(siumckb_lookup);
  148. /* Port number used on this machine: port B */
  149. migor_snd_device = platform_device_alloc("soc-audio", 1);
  150. if (!migor_snd_device) {
  151. ret = -ENOMEM;
  152. goto epdevalloc;
  153. }
  154. platform_set_drvdata(migor_snd_device, &snd_soc_migor);
  155. ret = platform_device_add(migor_snd_device);
  156. if (ret)
  157. goto epdevadd;
  158. return 0;
  159. epdevadd:
  160. platform_device_put(migor_snd_device);
  161. epdevalloc:
  162. clkdev_drop(siumckb_lookup);
  163. eclkdevalloc:
  164. clk_unregister(&siumckb_clk);
  165. return ret;
  166. }
  167. static void __exit migor_exit(void)
  168. {
  169. clkdev_drop(siumckb_lookup);
  170. clk_unregister(&siumckb_clk);
  171. platform_device_unregister(migor_snd_device);
  172. }
  173. module_init(migor_init);
  174. module_exit(migor_exit);
  175. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  176. MODULE_DESCRIPTION("ALSA SoC Migor");
  177. MODULE_LICENSE("GPL v2");