harmony.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * harmony.c - Harmony machine ASoC driver
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2010-2011 - NVIDIA, Inc.
  6. *
  7. * Based on code copyright/by:
  8. *
  9. * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
  10. *
  11. * Copyright 2007 Wolfson Microelectronics PLC.
  12. * Author: Graeme Gregory
  13. * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  27. * 02110-1301 USA
  28. *
  29. */
  30. #include <asm/mach-types.h>
  31. #include <linux/module.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/slab.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/soc.h>
  38. #include "tegra_das.h"
  39. #include "tegra_i2s.h"
  40. #include "tegra_pcm.h"
  41. #include "tegra_asoc_utils.h"
  42. #define DRV_NAME "tegra-snd-harmony"
  43. #define PREFIX DRV_NAME ": "
  44. struct tegra_harmony {
  45. };
  46. static int harmony_asoc_hw_params(struct snd_pcm_substream *substream,
  47. struct snd_pcm_hw_params *params)
  48. {
  49. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  50. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  51. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  52. int srate, mclk, mclk_change;
  53. int err;
  54. srate = params_rate(params);
  55. switch (srate) {
  56. case 64000:
  57. case 88200:
  58. case 96000:
  59. mclk = 128 * srate;
  60. break;
  61. default:
  62. mclk = 256 * srate;
  63. break;
  64. }
  65. /* FIXME: Codec only requires >= 3MHz if OSR==0 */
  66. while (mclk < 6000000)
  67. mclk *= 2;
  68. err = tegra_asoc_utils_set_rate(srate, mclk, &mclk_change);
  69. if (err < 0) {
  70. pr_err(PREFIX "Can't configure clocks\n");
  71. return err;
  72. }
  73. err = snd_soc_dai_set_fmt(codec_dai,
  74. SND_SOC_DAIFMT_I2S |
  75. SND_SOC_DAIFMT_NB_NF |
  76. SND_SOC_DAIFMT_CBS_CFS);
  77. if (err < 0) {
  78. pr_err(PREFIX "codec_dai fmt not set\n");
  79. return err;
  80. }
  81. err = snd_soc_dai_set_fmt(cpu_dai,
  82. SND_SOC_DAIFMT_I2S |
  83. SND_SOC_DAIFMT_NB_NF |
  84. SND_SOC_DAIFMT_CBS_CFS);
  85. if (err < 0) {
  86. pr_err(PREFIX "cpu_dai fmt not set\n");
  87. return err;
  88. }
  89. if (mclk_change) {
  90. err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, SND_SOC_CLOCK_IN);
  91. if (err < 0) {
  92. pr_err(PREFIX "codec_dai clock not set\n");
  93. return err;
  94. }
  95. }
  96. return 0;
  97. }
  98. static struct snd_soc_ops harmony_asoc_ops = {
  99. .hw_params = harmony_asoc_hw_params,
  100. };
  101. static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
  102. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  103. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  104. };
  105. static const struct snd_soc_dapm_route harmony_audio_map[] = {
  106. {"Headphone Jack", NULL, "HPOUTR"},
  107. {"Headphone Jack", NULL, "HPOUTL"},
  108. {"Mic Bias", NULL, "Mic Jack"},
  109. {"IN1L", NULL, "Mic Bias"},
  110. };
  111. static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
  112. {
  113. struct snd_soc_codec *codec = rtd->codec;
  114. struct snd_soc_dapm_context *dapm = &codec->dapm;
  115. snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
  116. ARRAY_SIZE(harmony_dapm_widgets));
  117. snd_soc_dapm_add_routes(dapm, harmony_audio_map,
  118. ARRAY_SIZE(harmony_audio_map));
  119. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  120. snd_soc_dapm_enable_pin(dapm, "Mic Jack");
  121. snd_soc_dapm_sync(dapm);
  122. return 0;
  123. }
  124. static struct snd_soc_dai_link harmony_wm8903_dai = {
  125. .name = "WM8903",
  126. .stream_name = "WM8903 PCM",
  127. .codec_name = "wm8903-codec.0-001a",
  128. .platform_name = "tegra-pcm-audio",
  129. .cpu_dai_name = "tegra-i2s.0",
  130. .codec_dai_name = "wm8903-hifi",
  131. .init = harmony_asoc_init,
  132. .ops = &harmony_asoc_ops,
  133. };
  134. static struct snd_soc_card snd_soc_harmony = {
  135. .name = "tegra-harmony",
  136. .dai_link = &harmony_wm8903_dai,
  137. .num_links = 1,
  138. };
  139. static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
  140. {
  141. struct snd_soc_card *card = &snd_soc_harmony;
  142. struct tegra_harmony *harmony;
  143. int ret;
  144. if (!machine_is_harmony()) {
  145. dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
  146. return -ENODEV;
  147. }
  148. harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
  149. if (!harmony) {
  150. dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
  151. return -ENOMEM;
  152. }
  153. ret = tegra_asoc_utils_init();
  154. if (ret)
  155. goto err_free_harmony;
  156. card->dev = &pdev->dev;
  157. platform_set_drvdata(pdev, card);
  158. snd_soc_card_set_drvdata(card, harmony);
  159. ret = snd_soc_register_card(card);
  160. if (ret) {
  161. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  162. ret);
  163. goto err_clear_drvdata;
  164. }
  165. return 0;
  166. err_clear_drvdata:
  167. snd_soc_card_set_drvdata(card, NULL);
  168. platform_set_drvdata(pdev, NULL);
  169. card->dev = NULL;
  170. tegra_asoc_utils_fini();
  171. err_free_harmony:
  172. kfree(harmony);
  173. return ret;
  174. }
  175. static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
  176. {
  177. struct snd_soc_card *card = platform_get_drvdata(pdev);
  178. struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
  179. snd_soc_unregister_card(card);
  180. snd_soc_card_set_drvdata(card, NULL);
  181. platform_set_drvdata(pdev, NULL);
  182. card->dev = NULL;
  183. tegra_asoc_utils_fini();
  184. kfree(harmony);
  185. return 0;
  186. }
  187. static struct platform_driver tegra_snd_harmony_driver = {
  188. .driver = {
  189. .name = DRV_NAME,
  190. .owner = THIS_MODULE,
  191. },
  192. .probe = tegra_snd_harmony_probe,
  193. .remove = __devexit_p(tegra_snd_harmony_remove),
  194. };
  195. static int __init snd_tegra_harmony_init(void)
  196. {
  197. return platform_driver_register(&tegra_snd_harmony_driver);
  198. }
  199. module_init(snd_tegra_harmony_init);
  200. static void __exit snd_tegra_harmony_exit(void)
  201. {
  202. platform_driver_unregister(&tegra_snd_harmony_driver);
  203. }
  204. module_exit(snd_tegra_harmony_exit);
  205. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  206. MODULE_DESCRIPTION("Harmony machine ASoC driver");
  207. MODULE_LICENSE("GPL");