migor.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // ALSA SoC driver for Migo-R
  4. //
  5. // Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  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 = substream->private_data;
  37. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  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(rtd->cpu_dai, 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 = substream->private_data;
  63. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  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. static struct snd_soc_dai_link migor_dai = {
  99. .name = "wm8978",
  100. .stream_name = "WM8978",
  101. .cpu_dai_name = "siu-pcm-audio",
  102. .codec_dai_name = "wm8978-hifi",
  103. .platform_name = "siu-pcm-audio",
  104. .codec_name = "wm8978.0-001a",
  105. .dai_fmt = SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_I2S |
  106. SND_SOC_DAIFMT_CBS_CFS,
  107. .ops = &migor_dai_ops,
  108. };
  109. /* migor audio machine driver */
  110. static struct snd_soc_card snd_soc_migor = {
  111. .name = "Migo-R",
  112. .owner = THIS_MODULE,
  113. .dai_link = &migor_dai,
  114. .num_links = 1,
  115. .dapm_widgets = migor_dapm_widgets,
  116. .num_dapm_widgets = ARRAY_SIZE(migor_dapm_widgets),
  117. .dapm_routes = audio_map,
  118. .num_dapm_routes = ARRAY_SIZE(audio_map),
  119. };
  120. static struct platform_device *migor_snd_device;
  121. static int __init migor_init(void)
  122. {
  123. int ret;
  124. ret = clk_register(&siumckb_clk);
  125. if (ret < 0)
  126. return ret;
  127. siumckb_lookup = clkdev_create(&siumckb_clk, "siumckb_clk", NULL);
  128. if (!siumckb_lookup) {
  129. ret = -ENOMEM;
  130. goto eclkdevalloc;
  131. }
  132. /* Port number used on this machine: port B */
  133. migor_snd_device = platform_device_alloc("soc-audio", 1);
  134. if (!migor_snd_device) {
  135. ret = -ENOMEM;
  136. goto epdevalloc;
  137. }
  138. platform_set_drvdata(migor_snd_device, &snd_soc_migor);
  139. ret = platform_device_add(migor_snd_device);
  140. if (ret)
  141. goto epdevadd;
  142. return 0;
  143. epdevadd:
  144. platform_device_put(migor_snd_device);
  145. epdevalloc:
  146. clkdev_drop(siumckb_lookup);
  147. eclkdevalloc:
  148. clk_unregister(&siumckb_clk);
  149. return ret;
  150. }
  151. static void __exit migor_exit(void)
  152. {
  153. clkdev_drop(siumckb_lookup);
  154. clk_unregister(&siumckb_clk);
  155. platform_device_unregister(migor_snd_device);
  156. }
  157. module_init(migor_init);
  158. module_exit(migor_exit);
  159. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  160. MODULE_DESCRIPTION("ALSA SoC Migor");
  161. MODULE_LICENSE("GPL v2");