tegra_rt5640.c 7.0 KB

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