tegra_rt5640.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * tegra_rt5640.c - Tegra machine ASoC driver for boards using RT5640 codec.
  3. *
  4. * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
  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. * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
  22. * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
  23. * Copyright 2007 Wolfson Microelectronics PLC.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/gpio.h>
  29. #include <linux/of_gpio.h>
  30. #include <sound/core.h>
  31. #include <sound/jack.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #include "../codecs/rt5640.h"
  36. #include "tegra_asoc_utils.h"
  37. #define DRV_NAME "tegra-snd-rt5640"
  38. struct tegra_rt5640 {
  39. struct tegra_asoc_utils_data util_data;
  40. int gpio_hp_det;
  41. enum of_gpio_flags gpio_hp_det_flags;
  42. };
  43. static int tegra_rt5640_asoc_hw_params(struct snd_pcm_substream *substream,
  44. struct snd_pcm_hw_params *params)
  45. {
  46. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  47. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  48. struct snd_soc_card *card = rtd->card;
  49. struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
  50. int srate, mclk;
  51. int err;
  52. srate = params_rate(params);
  53. mclk = 256 * srate;
  54. err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
  55. if (err < 0) {
  56. dev_err(card->dev, "Can't configure clocks\n");
  57. return err;
  58. }
  59. err = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_MCLK, mclk,
  60. SND_SOC_CLOCK_IN);
  61. if (err < 0) {
  62. dev_err(card->dev, "codec_dai clock not set\n");
  63. return err;
  64. }
  65. return 0;
  66. }
  67. static const struct snd_soc_ops tegra_rt5640_ops = {
  68. .hw_params = tegra_rt5640_asoc_hw_params,
  69. };
  70. static struct snd_soc_jack tegra_rt5640_hp_jack;
  71. static struct snd_soc_jack_pin tegra_rt5640_hp_jack_pins[] = {
  72. {
  73. .pin = "Headphones",
  74. .mask = SND_JACK_HEADPHONE,
  75. },
  76. };
  77. static struct snd_soc_jack_gpio tegra_rt5640_hp_jack_gpio = {
  78. .name = "Headphone detection",
  79. .report = SND_JACK_HEADPHONE,
  80. .debounce_time = 150,
  81. .invert = 1,
  82. };
  83. static const struct snd_soc_dapm_widget tegra_rt5640_dapm_widgets[] = {
  84. SND_SOC_DAPM_HP("Headphones", NULL),
  85. SND_SOC_DAPM_SPK("Speakers", NULL),
  86. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  87. };
  88. static const struct snd_kcontrol_new tegra_rt5640_controls[] = {
  89. SOC_DAPM_PIN_SWITCH("Speakers"),
  90. };
  91. static int tegra_rt5640_asoc_init(struct snd_soc_pcm_runtime *rtd)
  92. {
  93. struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(rtd->card);
  94. snd_soc_card_jack_new(rtd->card, "Headphones", SND_JACK_HEADPHONE,
  95. &tegra_rt5640_hp_jack, tegra_rt5640_hp_jack_pins,
  96. ARRAY_SIZE(tegra_rt5640_hp_jack_pins));
  97. if (gpio_is_valid(machine->gpio_hp_det)) {
  98. tegra_rt5640_hp_jack_gpio.gpio = machine->gpio_hp_det;
  99. tegra_rt5640_hp_jack_gpio.invert =
  100. !!(machine->gpio_hp_det_flags & OF_GPIO_ACTIVE_LOW);
  101. snd_soc_jack_add_gpios(&tegra_rt5640_hp_jack,
  102. 1,
  103. &tegra_rt5640_hp_jack_gpio);
  104. }
  105. return 0;
  106. }
  107. static struct snd_soc_dai_link tegra_rt5640_dai = {
  108. .name = "RT5640",
  109. .stream_name = "RT5640 PCM",
  110. .codec_dai_name = "rt5640-aif1",
  111. .init = tegra_rt5640_asoc_init,
  112. .ops = &tegra_rt5640_ops,
  113. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  114. SND_SOC_DAIFMT_CBS_CFS,
  115. };
  116. static struct snd_soc_card snd_soc_tegra_rt5640 = {
  117. .name = "tegra-rt5640",
  118. .owner = THIS_MODULE,
  119. .dai_link = &tegra_rt5640_dai,
  120. .num_links = 1,
  121. .controls = tegra_rt5640_controls,
  122. .num_controls = ARRAY_SIZE(tegra_rt5640_controls),
  123. .dapm_widgets = tegra_rt5640_dapm_widgets,
  124. .num_dapm_widgets = ARRAY_SIZE(tegra_rt5640_dapm_widgets),
  125. .fully_routed = true,
  126. };
  127. static int tegra_rt5640_probe(struct platform_device *pdev)
  128. {
  129. struct device_node *np = pdev->dev.of_node;
  130. struct snd_soc_card *card = &snd_soc_tegra_rt5640;
  131. struct tegra_rt5640 *machine;
  132. int ret;
  133. machine = devm_kzalloc(&pdev->dev,
  134. sizeof(struct tegra_rt5640), GFP_KERNEL);
  135. if (!machine)
  136. return -ENOMEM;
  137. card->dev = &pdev->dev;
  138. snd_soc_card_set_drvdata(card, machine);
  139. machine->gpio_hp_det = of_get_named_gpio_flags(
  140. np, "nvidia,hp-det-gpios", 0, &machine->gpio_hp_det_flags);
  141. if (machine->gpio_hp_det == -EPROBE_DEFER)
  142. return -EPROBE_DEFER;
  143. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  144. if (ret)
  145. goto err;
  146. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  147. if (ret)
  148. goto err;
  149. tegra_rt5640_dai.codec_of_node = of_parse_phandle(np,
  150. "nvidia,audio-codec", 0);
  151. if (!tegra_rt5640_dai.codec_of_node) {
  152. dev_err(&pdev->dev,
  153. "Property 'nvidia,audio-codec' missing or invalid\n");
  154. ret = -EINVAL;
  155. goto err;
  156. }
  157. tegra_rt5640_dai.cpu_of_node = of_parse_phandle(np,
  158. "nvidia,i2s-controller", 0);
  159. if (!tegra_rt5640_dai.cpu_of_node) {
  160. dev_err(&pdev->dev,
  161. "Property 'nvidia,i2s-controller' missing or invalid\n");
  162. ret = -EINVAL;
  163. goto err;
  164. }
  165. tegra_rt5640_dai.platform_of_node = tegra_rt5640_dai.cpu_of_node;
  166. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  167. if (ret)
  168. goto err;
  169. ret = snd_soc_register_card(card);
  170. if (ret) {
  171. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  172. ret);
  173. goto err_fini_utils;
  174. }
  175. return 0;
  176. err_fini_utils:
  177. tegra_asoc_utils_fini(&machine->util_data);
  178. err:
  179. return ret;
  180. }
  181. static int tegra_rt5640_remove(struct platform_device *pdev)
  182. {
  183. struct snd_soc_card *card = platform_get_drvdata(pdev);
  184. struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
  185. snd_soc_unregister_card(card);
  186. tegra_asoc_utils_fini(&machine->util_data);
  187. return 0;
  188. }
  189. static const struct of_device_id tegra_rt5640_of_match[] = {
  190. { .compatible = "nvidia,tegra-audio-rt5640", },
  191. {},
  192. };
  193. static struct platform_driver tegra_rt5640_driver = {
  194. .driver = {
  195. .name = DRV_NAME,
  196. .pm = &snd_soc_pm_ops,
  197. .of_match_table = tegra_rt5640_of_match,
  198. },
  199. .probe = tegra_rt5640_probe,
  200. .remove = tegra_rt5640_remove,
  201. };
  202. module_platform_driver(tegra_rt5640_driver);
  203. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  204. MODULE_DESCRIPTION("Tegra+RT5640 machine ASoC driver");
  205. MODULE_LICENSE("GPL v2");
  206. MODULE_ALIAS("platform:" DRV_NAME);
  207. MODULE_DEVICE_TABLE(of, tegra_rt5640_of_match);