tegra_sgtl5000.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 const 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. return -ENOMEM;
  108. card->dev = &pdev->dev;
  109. snd_soc_card_set_drvdata(card, machine);
  110. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  111. if (ret)
  112. goto err;
  113. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  114. if (ret)
  115. goto err;
  116. tegra_sgtl5000_dai.codec_of_node = of_parse_phandle(np,
  117. "nvidia,audio-codec", 0);
  118. if (!tegra_sgtl5000_dai.codec_of_node) {
  119. dev_err(&pdev->dev,
  120. "Property 'nvidia,audio-codec' missing or invalid\n");
  121. ret = -EINVAL;
  122. goto err;
  123. }
  124. tegra_sgtl5000_dai.cpu_of_node = of_parse_phandle(np,
  125. "nvidia,i2s-controller", 0);
  126. if (!tegra_sgtl5000_dai.cpu_of_node) {
  127. dev_err(&pdev->dev,
  128. "Property 'nvidia,i2s-controller' missing/invalid\n");
  129. ret = -EINVAL;
  130. goto err_put_codec_of_node;
  131. }
  132. tegra_sgtl5000_dai.platform_of_node = tegra_sgtl5000_dai.cpu_of_node;
  133. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  134. if (ret)
  135. goto err_put_cpu_of_node;
  136. ret = snd_soc_register_card(card);
  137. if (ret) {
  138. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  139. ret);
  140. goto err_fini_utils;
  141. }
  142. return 0;
  143. err_fini_utils:
  144. tegra_asoc_utils_fini(&machine->util_data);
  145. err_put_cpu_of_node:
  146. of_node_put(tegra_sgtl5000_dai.cpu_of_node);
  147. tegra_sgtl5000_dai.cpu_of_node = NULL;
  148. tegra_sgtl5000_dai.platform_of_node = NULL;
  149. err_put_codec_of_node:
  150. of_node_put(tegra_sgtl5000_dai.codec_of_node);
  151. tegra_sgtl5000_dai.codec_of_node = NULL;
  152. err:
  153. return ret;
  154. }
  155. static int tegra_sgtl5000_driver_remove(struct platform_device *pdev)
  156. {
  157. struct snd_soc_card *card = platform_get_drvdata(pdev);
  158. struct tegra_sgtl5000 *machine = snd_soc_card_get_drvdata(card);
  159. int ret;
  160. ret = snd_soc_unregister_card(card);
  161. tegra_asoc_utils_fini(&machine->util_data);
  162. of_node_put(tegra_sgtl5000_dai.cpu_of_node);
  163. tegra_sgtl5000_dai.cpu_of_node = NULL;
  164. tegra_sgtl5000_dai.platform_of_node = NULL;
  165. of_node_put(tegra_sgtl5000_dai.codec_of_node);
  166. tegra_sgtl5000_dai.codec_of_node = NULL;
  167. return ret;
  168. }
  169. static const struct of_device_id tegra_sgtl5000_of_match[] = {
  170. { .compatible = "nvidia,tegra-audio-sgtl5000", },
  171. { /* sentinel */ },
  172. };
  173. static struct platform_driver tegra_sgtl5000_driver = {
  174. .driver = {
  175. .name = DRV_NAME,
  176. .pm = &snd_soc_pm_ops,
  177. .of_match_table = tegra_sgtl5000_of_match,
  178. },
  179. .probe = tegra_sgtl5000_driver_probe,
  180. .remove = tegra_sgtl5000_driver_remove,
  181. };
  182. module_platform_driver(tegra_sgtl5000_driver);
  183. MODULE_AUTHOR("Marcel Ziswiler <marcel@ziswiler.com>");
  184. MODULE_DESCRIPTION("Tegra SGTL5000 machine ASoC driver");
  185. MODULE_LICENSE("GPL v2");
  186. MODULE_ALIAS("platform:" DRV_NAME);
  187. MODULE_DEVICE_TABLE(of, tegra_sgtl5000_of_match);