migor.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 sh_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. codec_freq = rate * 512;
  52. /*
  53. * This propagates the parent frequency change to children and
  54. * recalculates the frequency table
  55. */
  56. clk_set_rate(&siumckb_clk, codec_freq);
  57. dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq);
  58. ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, SIU_CLKB_EXT,
  59. codec_freq / 2, SND_SOC_CLOCK_IN);
  60. if (!ret)
  61. use_count++;
  62. return ret;
  63. }
  64. static int migor_hw_free(struct snd_pcm_substream *substream)
  65. {
  66. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  67. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  68. if (use_count) {
  69. use_count--;
  70. if (!use_count)
  71. snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0,
  72. SND_SOC_CLOCK_IN);
  73. } else {
  74. dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n");
  75. }
  76. return 0;
  77. }
  78. static struct snd_soc_ops migor_dai_ops = {
  79. .hw_params = migor_hw_params,
  80. .hw_free = migor_hw_free,
  81. };
  82. static const struct snd_soc_dapm_widget migor_dapm_widgets[] = {
  83. SND_SOC_DAPM_HP("Headphone", NULL),
  84. SND_SOC_DAPM_MIC("Onboard Microphone", NULL),
  85. SND_SOC_DAPM_MIC("External Microphone", NULL),
  86. };
  87. static const struct snd_soc_dapm_route audio_map[] = {
  88. /* Headphone output connected to LHP/RHP, enable OUT4 for VMID */
  89. { "Headphone", NULL, "OUT4 VMID" },
  90. { "OUT4 VMID", NULL, "LHP" },
  91. { "OUT4 VMID", NULL, "RHP" },
  92. /* On-board microphone */
  93. { "RMICN", NULL, "Mic Bias" },
  94. { "RMICP", NULL, "Mic Bias" },
  95. { "Mic Bias", NULL, "Onboard Microphone" },
  96. /* External microphone */
  97. { "LMICN", NULL, "Mic Bias" },
  98. { "LMICP", NULL, "Mic Bias" },
  99. { "Mic Bias", NULL, "External Microphone" },
  100. };
  101. /* migor digital audio interface glue - connects codec <--> CPU */
  102. static struct snd_soc_dai_link migor_dai = {
  103. .name = "wm8978",
  104. .stream_name = "WM8978",
  105. .cpu_dai_name = "siu-pcm-audio",
  106. .codec_dai_name = "wm8978-hifi",
  107. .platform_name = "siu-pcm-audio",
  108. .codec_name = "wm8978.0-001a",
  109. .dai_fmt = SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_I2S |
  110. SND_SOC_DAIFMT_CBS_CFS,
  111. .ops = &migor_dai_ops,
  112. };
  113. /* migor audio machine driver */
  114. static struct snd_soc_card snd_soc_migor = {
  115. .name = "Migo-R",
  116. .owner = THIS_MODULE,
  117. .dai_link = &migor_dai,
  118. .num_links = 1,
  119. .dapm_widgets = migor_dapm_widgets,
  120. .num_dapm_widgets = ARRAY_SIZE(migor_dapm_widgets),
  121. .dapm_routes = audio_map,
  122. .num_dapm_routes = ARRAY_SIZE(audio_map),
  123. };
  124. static struct platform_device *migor_snd_device;
  125. static int __init migor_init(void)
  126. {
  127. int ret;
  128. ret = clk_register(&siumckb_clk);
  129. if (ret < 0)
  130. return ret;
  131. siumckb_lookup = clkdev_create(&siumckb_clk, "siumckb_clk", NULL);
  132. if (!siumckb_lookup) {
  133. ret = -ENOMEM;
  134. goto eclkdevalloc;
  135. }
  136. /* Port number used on this machine: port B */
  137. migor_snd_device = platform_device_alloc("soc-audio", 1);
  138. if (!migor_snd_device) {
  139. ret = -ENOMEM;
  140. goto epdevalloc;
  141. }
  142. platform_set_drvdata(migor_snd_device, &snd_soc_migor);
  143. ret = platform_device_add(migor_snd_device);
  144. if (ret)
  145. goto epdevadd;
  146. return 0;
  147. epdevadd:
  148. platform_device_put(migor_snd_device);
  149. epdevalloc:
  150. clkdev_drop(siumckb_lookup);
  151. eclkdevalloc:
  152. clk_unregister(&siumckb_clk);
  153. return ret;
  154. }
  155. static void __exit migor_exit(void)
  156. {
  157. clkdev_drop(siumckb_lookup);
  158. clk_unregister(&siumckb_clk);
  159. platform_device_unregister(migor_snd_device);
  160. }
  161. module_init(migor_init);
  162. module_exit(migor_exit);
  163. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  164. MODULE_DESCRIPTION("ALSA SoC Migor");
  165. MODULE_LICENSE("GPL v2");