tegra_sgtl5000.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * tegra_sgtl5000.c - Tegra machine ASoC driver for boards using SGTL5000 codec
  3. *
  4. * Author: Marcel Ziswiler <marcel@ziswiler.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Based on code copyright/by:
  19. *
  20. * Copyright (C) 2010-2012 - NVIDIA, Inc.
  21. * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
  22. * Copyright 2007 Wolfson Microelectronics PLC.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/gpio.h>
  28. #include <linux/of_gpio.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc.h>
  33. #include "../codecs/sgtl5000.h"
  34. #include "tegra_asoc_utils.h"
  35. #define DRV_NAME "tegra-snd-sgtl5000"
  36. struct tegra_sgtl5000 {
  37. struct tegra_asoc_utils_data util_data;
  38. };
  39. static int tegra_sgtl5000_hw_params(struct snd_pcm_substream *substream,
  40. struct snd_pcm_hw_params *params)
  41. {
  42. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  43. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  44. struct snd_soc_card *card = rtd->card;
  45. struct tegra_sgtl5000 *machine = snd_soc_card_get_drvdata(card);
  46. int srate, mclk;
  47. int err;
  48. srate = params_rate(params);
  49. switch (srate) {
  50. case 11025:
  51. case 22050:
  52. case 44100:
  53. case 88200:
  54. mclk = 11289600;
  55. break;
  56. default:
  57. mclk = 12288000;
  58. break;
  59. }
  60. err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
  61. if (err < 0) {
  62. dev_err(card->dev, "Can't configure clocks\n");
  63. return err;
  64. }
  65. err = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk,
  66. SND_SOC_CLOCK_IN);
  67. if (err < 0) {
  68. dev_err(card->dev, "codec_dai clock not set\n");
  69. return err;
  70. }
  71. return 0;
  72. }
  73. static struct snd_soc_ops tegra_sgtl5000_ops = {
  74. .hw_params = tegra_sgtl5000_hw_params,
  75. };
  76. static const struct snd_soc_dapm_widget tegra_sgtl5000_dapm_widgets[] = {
  77. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  78. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  79. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  80. };
  81. static struct snd_soc_dai_link tegra_sgtl5000_dai = {
  82. .name = "sgtl5000",
  83. .stream_name = "HiFi",
  84. .codec_dai_name = "sgtl5000",
  85. .ops = &tegra_sgtl5000_ops,
  86. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  87. SND_SOC_DAIFMT_CBS_CFS,
  88. };
  89. static struct snd_soc_card snd_soc_tegra_sgtl5000 = {
  90. .name = "tegra-sgtl5000",
  91. .owner = THIS_MODULE,
  92. .dai_link = &tegra_sgtl5000_dai,
  93. .num_links = 1,
  94. .dapm_widgets = tegra_sgtl5000_dapm_widgets,
  95. .num_dapm_widgets = ARRAY_SIZE(tegra_sgtl5000_dapm_widgets),
  96. .fully_routed = true,
  97. };
  98. static int tegra_sgtl5000_driver_probe(struct platform_device *pdev)
  99. {
  100. struct device_node *np = pdev->dev.of_node;
  101. struct snd_soc_card *card = &snd_soc_tegra_sgtl5000;
  102. struct tegra_sgtl5000 *machine;
  103. int ret;
  104. machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_sgtl5000),
  105. GFP_KERNEL);
  106. if (!machine) {
  107. dev_err(&pdev->dev, "Can't allocate tegra_sgtl5000 struct\n");
  108. return -ENOMEM;
  109. }
  110. card->dev = &pdev->dev;
  111. platform_set_drvdata(pdev, card);
  112. snd_soc_card_set_drvdata(card, machine);
  113. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  114. if (ret)
  115. goto err;
  116. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  117. if (ret)
  118. goto err;
  119. tegra_sgtl5000_dai.codec_of_node = of_parse_phandle(np,
  120. "nvidia,audio-codec", 0);
  121. if (!tegra_sgtl5000_dai.codec_of_node) {
  122. dev_err(&pdev->dev,
  123. "Property 'nvidia,audio-codec' missing or invalid\n");
  124. ret = -EINVAL;
  125. goto err;
  126. }
  127. tegra_sgtl5000_dai.cpu_of_node = of_parse_phandle(np,
  128. "nvidia,i2s-controller", 0);
  129. if (!tegra_sgtl5000_dai.cpu_of_node) {
  130. dev_err(&pdev->dev,
  131. "Property 'nvidia,i2s-controller' missing/invalid\n");
  132. ret = -EINVAL;
  133. goto err;
  134. }
  135. tegra_sgtl5000_dai.platform_of_node = tegra_sgtl5000_dai.cpu_of_node;
  136. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  137. if (ret)
  138. goto err;
  139. ret = snd_soc_register_card(card);
  140. if (ret) {
  141. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  142. ret);
  143. goto err_fini_utils;
  144. }
  145. return 0;
  146. err_fini_utils:
  147. tegra_asoc_utils_fini(&machine->util_data);
  148. err:
  149. return ret;
  150. }
  151. static int tegra_sgtl5000_driver_remove(struct platform_device *pdev)
  152. {
  153. struct snd_soc_card *card = platform_get_drvdata(pdev);
  154. struct tegra_sgtl5000 *machine = snd_soc_card_get_drvdata(card);
  155. int ret;
  156. ret = snd_soc_unregister_card(card);
  157. tegra_asoc_utils_fini(&machine->util_data);
  158. return ret;
  159. }
  160. static const struct of_device_id tegra_sgtl5000_of_match[] = {
  161. { .compatible = "nvidia,tegra-audio-sgtl5000", },
  162. { /* sentinel */ },
  163. };
  164. static struct platform_driver tegra_sgtl5000_driver = {
  165. .driver = {
  166. .name = DRV_NAME,
  167. .pm = &snd_soc_pm_ops,
  168. .of_match_table = tegra_sgtl5000_of_match,
  169. },
  170. .probe = tegra_sgtl5000_driver_probe,
  171. .remove = tegra_sgtl5000_driver_remove,
  172. };
  173. module_platform_driver(tegra_sgtl5000_driver);
  174. MODULE_AUTHOR("Marcel Ziswiler <marcel@ziswiler.com>");
  175. MODULE_DESCRIPTION("Tegra SGTL5000 machine ASoC driver");
  176. MODULE_LICENSE("GPL v2");
  177. MODULE_ALIAS("platform:" DRV_NAME);
  178. MODULE_DEVICE_TABLE(of, tegra_sgtl5000_of_match);