migor.c 4.9 KB

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