migor.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/clock.h>
  14. #include <cpu/sh7722.h>
  15. #include <sound/core.h>
  16. #include <sound/pcm.h>
  17. #include <sound/soc.h>
  18. #include <sound/soc-dapm.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. .name = "siumckb_clk",
  34. .id = -1,
  35. .ops = &siumckb_clk_ops,
  36. .rate = 0, /* initialised at run-time */
  37. };
  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->dai->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->dai->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->dai->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->dai->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_codec *codec)
  111. {
  112. snd_soc_dapm_new_controls(codec, migor_dapm_widgets,
  113. ARRAY_SIZE(migor_dapm_widgets));
  114. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  115. return 0;
  116. }
  117. /* migor digital audio interface glue - connects codec <--> CPU */
  118. static struct snd_soc_dai_link migor_dai = {
  119. .name = "wm8978",
  120. .stream_name = "WM8978",
  121. .cpu_dai = &siu_i2s_dai,
  122. .codec_dai = &wm8978_dai,
  123. .ops = &migor_dai_ops,
  124. .init = migor_dai_init,
  125. };
  126. /* migor audio machine driver */
  127. static struct snd_soc_card snd_soc_migor = {
  128. .name = "Migo-R",
  129. .platform = &siu_platform,
  130. .dai_link = &migor_dai,
  131. .num_links = 1,
  132. };
  133. /* migor audio subsystem */
  134. static struct snd_soc_device migor_snd_devdata = {
  135. .card = &snd_soc_migor,
  136. .codec_dev = &soc_codec_dev_wm8978,
  137. };
  138. static struct platform_device *migor_snd_device;
  139. static int __init migor_init(void)
  140. {
  141. int ret;
  142. ret = clk_register(&siumckb_clk);
  143. if (ret < 0)
  144. return ret;
  145. /* Port number used on this machine: port B */
  146. migor_snd_device = platform_device_alloc("soc-audio", 1);
  147. if (!migor_snd_device) {
  148. ret = -ENOMEM;
  149. goto epdevalloc;
  150. }
  151. platform_set_drvdata(migor_snd_device, &migor_snd_devdata);
  152. migor_snd_devdata.dev = &migor_snd_device->dev;
  153. ret = platform_device_add(migor_snd_device);
  154. if (ret)
  155. goto epdevadd;
  156. return 0;
  157. epdevadd:
  158. platform_device_put(migor_snd_device);
  159. epdevalloc:
  160. clk_unregister(&siumckb_clk);
  161. return ret;
  162. }
  163. static void __exit migor_exit(void)
  164. {
  165. clk_unregister(&siumckb_clk);
  166. platform_device_unregister(migor_snd_device);
  167. }
  168. module_init(migor_init);
  169. module_exit(migor_exit);
  170. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  171. MODULE_DESCRIPTION("ALSA SoC Migor");
  172. MODULE_LICENSE("GPL v2");