tegra_rt5640.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * tegra_rt5640.c - Tegra machine ASoC driver for boards using WM8903 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. };
  42. static int tegra_rt5640_asoc_hw_params(struct snd_pcm_substream *substream,
  43. struct snd_pcm_hw_params *params)
  44. {
  45. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  46. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  47. struct snd_soc_codec *codec = codec_dai->codec;
  48. struct snd_soc_card *card = codec->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 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 snd_soc_dai *codec_dai = rtd->codec_dai;
  94. struct snd_soc_codec *codec = codec_dai->codec;
  95. struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(codec->card);
  96. snd_soc_jack_new(codec, "Headphones", SND_JACK_HEADPHONE,
  97. &tegra_rt5640_hp_jack);
  98. snd_soc_jack_add_pins(&tegra_rt5640_hp_jack,
  99. ARRAY_SIZE(tegra_rt5640_hp_jack_pins),
  100. tegra_rt5640_hp_jack_pins);
  101. if (gpio_is_valid(machine->gpio_hp_det)) {
  102. tegra_rt5640_hp_jack_gpio.gpio = machine->gpio_hp_det;
  103. snd_soc_jack_add_gpios(&tegra_rt5640_hp_jack,
  104. 1,
  105. &tegra_rt5640_hp_jack_gpio);
  106. }
  107. return 0;
  108. }
  109. static struct snd_soc_dai_link tegra_rt5640_dai = {
  110. .name = "RT5640",
  111. .stream_name = "RT5640 PCM",
  112. .codec_dai_name = "rt5640-aif1",
  113. .init = tegra_rt5640_asoc_init,
  114. .ops = &tegra_rt5640_ops,
  115. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  116. SND_SOC_DAIFMT_CBS_CFS,
  117. };
  118. static struct snd_soc_card snd_soc_tegra_rt5640 = {
  119. .name = "tegra-rt5640",
  120. .owner = THIS_MODULE,
  121. .dai_link = &tegra_rt5640_dai,
  122. .num_links = 1,
  123. .controls = tegra_rt5640_controls,
  124. .num_controls = ARRAY_SIZE(tegra_rt5640_controls),
  125. .dapm_widgets = tegra_rt5640_dapm_widgets,
  126. .num_dapm_widgets = ARRAY_SIZE(tegra_rt5640_dapm_widgets),
  127. .fully_routed = true,
  128. };
  129. static int tegra_rt5640_probe(struct platform_device *pdev)
  130. {
  131. struct device_node *np = pdev->dev.of_node;
  132. struct snd_soc_card *card = &snd_soc_tegra_rt5640;
  133. struct tegra_rt5640 *machine;
  134. int ret;
  135. machine = devm_kzalloc(&pdev->dev,
  136. sizeof(struct tegra_rt5640), GFP_KERNEL);
  137. if (!machine) {
  138. dev_err(&pdev->dev, "Can't allocate tegra_rt5640\n");
  139. return -ENOMEM;
  140. }
  141. card->dev = &pdev->dev;
  142. platform_set_drvdata(pdev, card);
  143. snd_soc_card_set_drvdata(card, machine);
  144. machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
  145. if (machine->gpio_hp_det == -EPROBE_DEFER)
  146. return -EPROBE_DEFER;
  147. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  148. if (ret)
  149. goto err;
  150. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  151. if (ret)
  152. goto err;
  153. tegra_rt5640_dai.codec_of_node = of_parse_phandle(np,
  154. "nvidia,audio-codec", 0);
  155. if (!tegra_rt5640_dai.codec_of_node) {
  156. dev_err(&pdev->dev,
  157. "Property 'nvidia,audio-codec' missing or invalid\n");
  158. ret = -EINVAL;
  159. goto err;
  160. }
  161. tegra_rt5640_dai.cpu_of_node = of_parse_phandle(np,
  162. "nvidia,i2s-controller", 0);
  163. if (!tegra_rt5640_dai.cpu_of_node) {
  164. dev_err(&pdev->dev,
  165. "Property 'nvidia,i2s-controller' missing or invalid\n");
  166. ret = -EINVAL;
  167. goto err;
  168. }
  169. tegra_rt5640_dai.platform_of_node = tegra_rt5640_dai.cpu_of_node;
  170. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  171. if (ret)
  172. goto err;
  173. ret = snd_soc_register_card(card);
  174. if (ret) {
  175. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  176. ret);
  177. goto err_fini_utils;
  178. }
  179. return 0;
  180. err_fini_utils:
  181. tegra_asoc_utils_fini(&machine->util_data);
  182. err:
  183. return ret;
  184. }
  185. static int tegra_rt5640_remove(struct platform_device *pdev)
  186. {
  187. struct snd_soc_card *card = platform_get_drvdata(pdev);
  188. struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
  189. snd_soc_jack_free_gpios(&tegra_rt5640_hp_jack, 1,
  190. &tegra_rt5640_hp_jack_gpio);
  191. snd_soc_unregister_card(card);
  192. tegra_asoc_utils_fini(&machine->util_data);
  193. return 0;
  194. }
  195. static const struct of_device_id tegra_rt5640_of_match[] = {
  196. { .compatible = "nvidia,tegra-audio-rt5640", },
  197. {},
  198. };
  199. static struct platform_driver tegra_rt5640_driver = {
  200. .driver = {
  201. .name = DRV_NAME,
  202. .owner = THIS_MODULE,
  203. .pm = &snd_soc_pm_ops,
  204. .of_match_table = tegra_rt5640_of_match,
  205. },
  206. .probe = tegra_rt5640_probe,
  207. .remove = tegra_rt5640_remove,
  208. };
  209. module_platform_driver(tegra_rt5640_driver);
  210. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  211. MODULE_DESCRIPTION("Tegra+RT5640 machine ASoC driver");
  212. MODULE_LICENSE("GPL v2");
  213. MODULE_ALIAS("platform:" DRV_NAME);
  214. MODULE_DEVICE_TABLE(of, tegra_rt5640_of_match);