migor.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/device.h>
  11. #include <linux/firmware.h>
  12. #include <linux/module.h>
  13. #include <asm/clkdev.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 <sound/soc-dapm.h>
  20. #include "../codecs/wm8978.h"
  21. #include "siu.h"
  22. /* Default 8000Hz sampling frequency */
  23. static unsigned long codec_freq = 8000 * 512;
  24. static unsigned int use_count;
  25. /* External clock, sourced from the codec at the SIUMCKB pin */
  26. static unsigned long siumckb_recalc(struct clk *clk)
  27. {
  28. return codec_freq;
  29. }
  30. static struct clk_ops siumckb_clk_ops = {
  31. .recalc = siumckb_recalc,
  32. };
  33. static struct clk siumckb_clk = {
  34. .ops = &siumckb_clk_ops,
  35. .rate = 0, /* initialised at run-time */
  36. };
  37. static struct clk_lookup *siumckb_lookup;
  38. static int migor_hw_params(struct snd_pcm_substream *substream,
  39. struct snd_pcm_hw_params *params)
  40. {
  41. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  42. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  43. int ret;
  44. unsigned int rate = params_rate(params);
  45. ret = snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 13000000,
  46. SND_SOC_CLOCK_IN);
  47. if (ret < 0)
  48. return ret;
  49. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_OPCLKRATE, rate * 512);
  50. if (ret < 0)
  51. return ret;
  52. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_NB_IF |
  53. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS);
  54. if (ret < 0)
  55. return ret;
  56. ret = snd_soc_dai_set_fmt(rtd->cpu_dai, SND_SOC_DAIFMT_NB_IF |
  57. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS);
  58. if (ret < 0)
  59. return ret;
  60. codec_freq = rate * 512;
  61. /*
  62. * This propagates the parent frequency change to children and
  63. * recalculates the frequency table
  64. */
  65. clk_set_rate(&siumckb_clk, codec_freq);
  66. dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq);
  67. ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, SIU_CLKB_EXT,
  68. codec_freq / 2, SND_SOC_CLOCK_IN);
  69. if (!ret)
  70. use_count++;
  71. return ret;
  72. }
  73. static int migor_hw_free(struct snd_pcm_substream *substream)
  74. {
  75. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  76. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  77. if (use_count) {
  78. use_count--;
  79. if (!use_count)
  80. snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0,
  81. SND_SOC_CLOCK_IN);
  82. } else {
  83. dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n");
  84. }
  85. return 0;
  86. }
  87. static struct snd_soc_ops migor_dai_ops = {
  88. .hw_params = migor_hw_params,
  89. .hw_free = migor_hw_free,
  90. };
  91. static const struct snd_soc_dapm_widget migor_dapm_widgets[] = {
  92. SND_SOC_DAPM_HP("Headphone", NULL),
  93. SND_SOC_DAPM_MIC("Onboard Microphone", NULL),
  94. SND_SOC_DAPM_MIC("External Microphone", NULL),
  95. };
  96. static const struct snd_soc_dapm_route audio_map[] = {
  97. /* Headphone output connected to LHP/RHP, enable OUT4 for VMID */
  98. { "Headphone", NULL, "OUT4 VMID" },
  99. { "OUT4 VMID", NULL, "LHP" },
  100. { "OUT4 VMID", NULL, "RHP" },
  101. /* On-board microphone */
  102. { "RMICN", NULL, "Mic Bias" },
  103. { "RMICP", NULL, "Mic Bias" },
  104. { "Mic Bias", NULL, "Onboard Microphone" },
  105. /* External microphone */
  106. { "LMICN", NULL, "Mic Bias" },
  107. { "LMICP", NULL, "Mic Bias" },
  108. { "Mic Bias", NULL, "External Microphone" },
  109. };
  110. static int migor_dai_init(struct snd_soc_pcm_runtime *rtd)
  111. {
  112. struct snd_soc_codec *codec = rtd->codec;
  113. snd_soc_dapm_new_controls(codec, migor_dapm_widgets,
  114. ARRAY_SIZE(migor_dapm_widgets));
  115. snd_soc_dapm_add_routes(codec, 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");