tegra_wm8753.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * tegra_wm8753.c - Tegra machine ASoC driver for boards using WM8753 codec.
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2010-2012 - 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 <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/slab.h>
  33. #include <linux/gpio.h>
  34. #include <linux/of_gpio.h>
  35. #include <sound/core.h>
  36. #include <sound/jack.h>
  37. #include <sound/pcm.h>
  38. #include <sound/pcm_params.h>
  39. #include <sound/soc.h>
  40. #include "../codecs/wm8753.h"
  41. #include "tegra_asoc_utils.h"
  42. #define DRV_NAME "tegra-snd-wm8753"
  43. struct tegra_wm8753 {
  44. struct tegra_asoc_utils_data util_data;
  45. };
  46. static int tegra_wm8753_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_codec *codec = codec_dai->codec;
  52. struct snd_soc_card *card = codec->card;
  53. struct tegra_wm8753 *machine = snd_soc_card_get_drvdata(card);
  54. int srate, mclk;
  55. int err;
  56. srate = params_rate(params);
  57. switch (srate) {
  58. case 11025:
  59. case 22050:
  60. case 44100:
  61. case 88200:
  62. mclk = 11289600;
  63. break;
  64. default:
  65. mclk = 12288000;
  66. break;
  67. }
  68. err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
  69. if (err < 0) {
  70. dev_err(card->dev, "Can't configure clocks\n");
  71. return err;
  72. }
  73. err = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, mclk,
  74. SND_SOC_CLOCK_IN);
  75. if (err < 0) {
  76. dev_err(card->dev, "codec_dai clock not set\n");
  77. return err;
  78. }
  79. return 0;
  80. }
  81. static struct snd_soc_ops tegra_wm8753_ops = {
  82. .hw_params = tegra_wm8753_hw_params,
  83. };
  84. static const struct snd_soc_dapm_widget tegra_wm8753_dapm_widgets[] = {
  85. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  86. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  87. };
  88. static struct snd_soc_dai_link tegra_wm8753_dai = {
  89. .name = "WM8753",
  90. .stream_name = "WM8753 PCM",
  91. .codec_dai_name = "wm8753-hifi",
  92. .ops = &tegra_wm8753_ops,
  93. .dai_fmt = SND_SOC_DAIFMT_I2S |
  94. SND_SOC_DAIFMT_NB_NF |
  95. SND_SOC_DAIFMT_CBS_CFS,
  96. };
  97. static struct snd_soc_card snd_soc_tegra_wm8753 = {
  98. .name = "tegra-wm8753",
  99. .owner = THIS_MODULE,
  100. .dai_link = &tegra_wm8753_dai,
  101. .num_links = 1,
  102. .dapm_widgets = tegra_wm8753_dapm_widgets,
  103. .num_dapm_widgets = ARRAY_SIZE(tegra_wm8753_dapm_widgets),
  104. .fully_routed = true,
  105. };
  106. static int tegra_wm8753_driver_probe(struct platform_device *pdev)
  107. {
  108. struct device_node *np = pdev->dev.of_node;
  109. struct snd_soc_card *card = &snd_soc_tegra_wm8753;
  110. struct tegra_wm8753 *machine;
  111. int ret;
  112. machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8753),
  113. GFP_KERNEL);
  114. if (!machine) {
  115. dev_err(&pdev->dev, "Can't allocate tegra_wm8753 struct\n");
  116. return -ENOMEM;
  117. }
  118. card->dev = &pdev->dev;
  119. platform_set_drvdata(pdev, card);
  120. snd_soc_card_set_drvdata(card, machine);
  121. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  122. if (ret)
  123. goto err;
  124. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  125. if (ret)
  126. goto err;
  127. tegra_wm8753_dai.codec_of_node = of_parse_phandle(np,
  128. "nvidia,audio-codec", 0);
  129. if (!tegra_wm8753_dai.codec_of_node) {
  130. dev_err(&pdev->dev,
  131. "Property 'nvidia,audio-codec' missing or invalid\n");
  132. ret = -EINVAL;
  133. goto err;
  134. }
  135. tegra_wm8753_dai.cpu_of_node = of_parse_phandle(np,
  136. "nvidia,i2s-controller", 0);
  137. if (!tegra_wm8753_dai.cpu_of_node) {
  138. dev_err(&pdev->dev,
  139. "Property 'nvidia,i2s-controller' missing or invalid\n");
  140. ret = -EINVAL;
  141. goto err;
  142. }
  143. tegra_wm8753_dai.platform_of_node = tegra_wm8753_dai.cpu_of_node;
  144. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  145. if (ret)
  146. goto err;
  147. ret = snd_soc_register_card(card);
  148. if (ret) {
  149. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  150. ret);
  151. goto err_fini_utils;
  152. }
  153. return 0;
  154. err_fini_utils:
  155. tegra_asoc_utils_fini(&machine->util_data);
  156. err:
  157. return ret;
  158. }
  159. static int tegra_wm8753_driver_remove(struct platform_device *pdev)
  160. {
  161. struct snd_soc_card *card = platform_get_drvdata(pdev);
  162. struct tegra_wm8753 *machine = snd_soc_card_get_drvdata(card);
  163. snd_soc_unregister_card(card);
  164. tegra_asoc_utils_fini(&machine->util_data);
  165. return 0;
  166. }
  167. static const struct of_device_id tegra_wm8753_of_match[] = {
  168. { .compatible = "nvidia,tegra-audio-wm8753", },
  169. {},
  170. };
  171. static struct platform_driver tegra_wm8753_driver = {
  172. .driver = {
  173. .name = DRV_NAME,
  174. .owner = THIS_MODULE,
  175. .pm = &snd_soc_pm_ops,
  176. .of_match_table = tegra_wm8753_of_match,
  177. },
  178. .probe = tegra_wm8753_driver_probe,
  179. .remove = tegra_wm8753_driver_remove,
  180. };
  181. module_platform_driver(tegra_wm8753_driver);
  182. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  183. MODULE_DESCRIPTION("Tegra+WM8753 machine ASoC driver");
  184. MODULE_LICENSE("GPL");
  185. MODULE_ALIAS("platform:" DRV_NAME);
  186. MODULE_DEVICE_TABLE(of, tegra_wm8753_of_match);