tegra_rt5640.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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_card *card = rtd->card;
  48. struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
  49. int srate, mclk;
  50. int err;
  51. srate = params_rate(params);
  52. mclk = 256 * srate;
  53. err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
  54. if (err < 0) {
  55. dev_err(card->dev, "Can't configure clocks\n");
  56. return err;
  57. }
  58. err = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_MCLK, mclk,
  59. SND_SOC_CLOCK_IN);
  60. if (err < 0) {
  61. dev_err(card->dev, "codec_dai clock not set\n");
  62. return err;
  63. }
  64. return 0;
  65. }
  66. static struct snd_soc_ops tegra_rt5640_ops = {
  67. .hw_params = tegra_rt5640_asoc_hw_params,
  68. };
  69. static struct snd_soc_jack tegra_rt5640_hp_jack;
  70. static struct snd_soc_jack_pin tegra_rt5640_hp_jack_pins[] = {
  71. {
  72. .pin = "Headphones",
  73. .mask = SND_JACK_HEADPHONE,
  74. },
  75. };
  76. static struct snd_soc_jack_gpio tegra_rt5640_hp_jack_gpio = {
  77. .name = "Headphone detection",
  78. .report = SND_JACK_HEADPHONE,
  79. .debounce_time = 150,
  80. .invert = 1,
  81. };
  82. static const struct snd_soc_dapm_widget tegra_rt5640_dapm_widgets[] = {
  83. SND_SOC_DAPM_HP("Headphones", NULL),
  84. SND_SOC_DAPM_SPK("Speakers", NULL),
  85. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  86. };
  87. static const struct snd_kcontrol_new tegra_rt5640_controls[] = {
  88. SOC_DAPM_PIN_SWITCH("Speakers"),
  89. };
  90. static int tegra_rt5640_asoc_init(struct snd_soc_pcm_runtime *rtd)
  91. {
  92. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  93. struct snd_soc_codec *codec = codec_dai->codec;
  94. struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(rtd->card);
  95. snd_soc_jack_new(codec, "Headphones", SND_JACK_HEADPHONE,
  96. &tegra_rt5640_hp_jack);
  97. snd_soc_jack_add_pins(&tegra_rt5640_hp_jack,
  98. ARRAY_SIZE(tegra_rt5640_hp_jack_pins),
  99. tegra_rt5640_hp_jack_pins);
  100. if (gpio_is_valid(machine->gpio_hp_det)) {
  101. tegra_rt5640_hp_jack_gpio.gpio = machine->gpio_hp_det;
  102. snd_soc_jack_add_gpios(&tegra_rt5640_hp_jack,
  103. 1,
  104. &tegra_rt5640_hp_jack_gpio);
  105. }
  106. return 0;
  107. }
  108. static int tegra_rt5640_card_remove(struct snd_soc_card *card)
  109. {
  110. struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
  111. if (gpio_is_valid(machine->gpio_hp_det)) {
  112. snd_soc_jack_free_gpios(&tegra_rt5640_hp_jack, 1,
  113. &tegra_rt5640_hp_jack_gpio);
  114. }
  115. return 0;
  116. }
  117. static struct snd_soc_dai_link tegra_rt5640_dai = {
  118. .name = "RT5640",
  119. .stream_name = "RT5640 PCM",
  120. .codec_dai_name = "rt5640-aif1",
  121. .init = tegra_rt5640_asoc_init,
  122. .ops = &tegra_rt5640_ops,
  123. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  124. SND_SOC_DAIFMT_CBS_CFS,
  125. };
  126. static struct snd_soc_card snd_soc_tegra_rt5640 = {
  127. .name = "tegra-rt5640",
  128. .owner = THIS_MODULE,
  129. .remove = tegra_rt5640_card_remove,
  130. .dai_link = &tegra_rt5640_dai,
  131. .num_links = 1,
  132. .controls = tegra_rt5640_controls,
  133. .num_controls = ARRAY_SIZE(tegra_rt5640_controls),
  134. .dapm_widgets = tegra_rt5640_dapm_widgets,
  135. .num_dapm_widgets = ARRAY_SIZE(tegra_rt5640_dapm_widgets),
  136. .fully_routed = true,
  137. };
  138. static int tegra_rt5640_probe(struct platform_device *pdev)
  139. {
  140. struct device_node *np = pdev->dev.of_node;
  141. struct snd_soc_card *card = &snd_soc_tegra_rt5640;
  142. struct tegra_rt5640 *machine;
  143. int ret;
  144. machine = devm_kzalloc(&pdev->dev,
  145. sizeof(struct tegra_rt5640), GFP_KERNEL);
  146. if (!machine) {
  147. dev_err(&pdev->dev, "Can't allocate tegra_rt5640\n");
  148. return -ENOMEM;
  149. }
  150. card->dev = &pdev->dev;
  151. platform_set_drvdata(pdev, card);
  152. snd_soc_card_set_drvdata(card, machine);
  153. machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
  154. if (machine->gpio_hp_det == -EPROBE_DEFER)
  155. return -EPROBE_DEFER;
  156. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  157. if (ret)
  158. goto err;
  159. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  160. if (ret)
  161. goto err;
  162. tegra_rt5640_dai.codec_of_node = of_parse_phandle(np,
  163. "nvidia,audio-codec", 0);
  164. if (!tegra_rt5640_dai.codec_of_node) {
  165. dev_err(&pdev->dev,
  166. "Property 'nvidia,audio-codec' missing or invalid\n");
  167. ret = -EINVAL;
  168. goto err;
  169. }
  170. tegra_rt5640_dai.cpu_of_node = of_parse_phandle(np,
  171. "nvidia,i2s-controller", 0);
  172. if (!tegra_rt5640_dai.cpu_of_node) {
  173. dev_err(&pdev->dev,
  174. "Property 'nvidia,i2s-controller' missing or invalid\n");
  175. ret = -EINVAL;
  176. goto err;
  177. }
  178. tegra_rt5640_dai.platform_of_node = tegra_rt5640_dai.cpu_of_node;
  179. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  180. if (ret)
  181. goto err;
  182. ret = snd_soc_register_card(card);
  183. if (ret) {
  184. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  185. ret);
  186. goto err_fini_utils;
  187. }
  188. return 0;
  189. err_fini_utils:
  190. tegra_asoc_utils_fini(&machine->util_data);
  191. err:
  192. return ret;
  193. }
  194. static int tegra_rt5640_remove(struct platform_device *pdev)
  195. {
  196. struct snd_soc_card *card = platform_get_drvdata(pdev);
  197. struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
  198. snd_soc_unregister_card(card);
  199. tegra_asoc_utils_fini(&machine->util_data);
  200. return 0;
  201. }
  202. static const struct of_device_id tegra_rt5640_of_match[] = {
  203. { .compatible = "nvidia,tegra-audio-rt5640", },
  204. {},
  205. };
  206. static struct platform_driver tegra_rt5640_driver = {
  207. .driver = {
  208. .name = DRV_NAME,
  209. .owner = THIS_MODULE,
  210. .pm = &snd_soc_pm_ops,
  211. .of_match_table = tegra_rt5640_of_match,
  212. },
  213. .probe = tegra_rt5640_probe,
  214. .remove = tegra_rt5640_remove,
  215. };
  216. module_platform_driver(tegra_rt5640_driver);
  217. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  218. MODULE_DESCRIPTION("Tegra+RT5640 machine ASoC driver");
  219. MODULE_LICENSE("GPL v2");
  220. MODULE_ALIAS("platform:" DRV_NAME);
  221. MODULE_DEVICE_TABLE(of, tegra_rt5640_of_match);