tegra_rt5640.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. 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 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 int tegra_rt5640_card_remove(struct snd_soc_card *card)
  108. {
  109. struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
  110. if (gpio_is_valid(machine->gpio_hp_det)) {
  111. snd_soc_jack_free_gpios(&tegra_rt5640_hp_jack, 1,
  112. &tegra_rt5640_hp_jack_gpio);
  113. }
  114. return 0;
  115. }
  116. static struct snd_soc_dai_link tegra_rt5640_dai = {
  117. .name = "RT5640",
  118. .stream_name = "RT5640 PCM",
  119. .codec_dai_name = "rt5640-aif1",
  120. .init = tegra_rt5640_asoc_init,
  121. .ops = &tegra_rt5640_ops,
  122. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  123. SND_SOC_DAIFMT_CBS_CFS,
  124. };
  125. static struct snd_soc_card snd_soc_tegra_rt5640 = {
  126. .name = "tegra-rt5640",
  127. .owner = THIS_MODULE,
  128. .remove = tegra_rt5640_card_remove,
  129. .dai_link = &tegra_rt5640_dai,
  130. .num_links = 1,
  131. .controls = tegra_rt5640_controls,
  132. .num_controls = ARRAY_SIZE(tegra_rt5640_controls),
  133. .dapm_widgets = tegra_rt5640_dapm_widgets,
  134. .num_dapm_widgets = ARRAY_SIZE(tegra_rt5640_dapm_widgets),
  135. .fully_routed = true,
  136. };
  137. static int tegra_rt5640_probe(struct platform_device *pdev)
  138. {
  139. struct device_node *np = pdev->dev.of_node;
  140. struct snd_soc_card *card = &snd_soc_tegra_rt5640;
  141. struct tegra_rt5640 *machine;
  142. int ret;
  143. machine = devm_kzalloc(&pdev->dev,
  144. sizeof(struct tegra_rt5640), GFP_KERNEL);
  145. if (!machine) {
  146. dev_err(&pdev->dev, "Can't allocate tegra_rt5640\n");
  147. return -ENOMEM;
  148. }
  149. card->dev = &pdev->dev;
  150. platform_set_drvdata(pdev, card);
  151. snd_soc_card_set_drvdata(card, machine);
  152. machine->gpio_hp_det = of_get_named_gpio_flags(
  153. np, "nvidia,hp-det-gpios", 0, &machine->gpio_hp_det_flags);
  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. .pm = &snd_soc_pm_ops,
  210. .of_match_table = tegra_rt5640_of_match,
  211. },
  212. .probe = tegra_rt5640_probe,
  213. .remove = tegra_rt5640_remove,
  214. };
  215. module_platform_driver(tegra_rt5640_driver);
  216. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  217. MODULE_DESCRIPTION("Tegra+RT5640 machine ASoC driver");
  218. MODULE_LICENSE("GPL v2");
  219. MODULE_ALIAS("platform:" DRV_NAME);
  220. MODULE_DEVICE_TABLE(of, tegra_rt5640_of_match);