tegra_rt5677.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * tegra_rt5677.c - Tegra machine ASoC driver for boards using RT5677 codec.
  3. *
  4. * Copyright (c) 2014, The Chromium OS Authors. 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/rt5677.h"
  36. #include "tegra_asoc_utils.h"
  37. #define DRV_NAME "tegra-snd-rt5677"
  38. struct tegra_rt5677 {
  39. struct tegra_asoc_utils_data util_data;
  40. int gpio_hp_det;
  41. int gpio_hp_en;
  42. int gpio_mic_present;
  43. int gpio_dmic_clk_en;
  44. };
  45. static int tegra_rt5677_asoc_hw_params(struct snd_pcm_substream *substream,
  46. struct snd_pcm_hw_params *params)
  47. {
  48. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  49. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  50. struct snd_soc_card *card = rtd->card;
  51. struct tegra_rt5677 *machine = snd_soc_card_get_drvdata(card);
  52. int srate, mclk, err;
  53. srate = params_rate(params);
  54. mclk = 256 * srate;
  55. err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
  56. if (err < 0) {
  57. dev_err(card->dev, "Can't configure clocks\n");
  58. return err;
  59. }
  60. err = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, mclk,
  61. SND_SOC_CLOCK_IN);
  62. if (err < 0) {
  63. dev_err(card->dev, "codec_dai clock not set\n");
  64. return err;
  65. }
  66. return 0;
  67. }
  68. static int tegra_rt5677_event_hp(struct snd_soc_dapm_widget *w,
  69. struct snd_kcontrol *k, int event)
  70. {
  71. struct snd_soc_dapm_context *dapm = w->dapm;
  72. struct snd_soc_card *card = dapm->card;
  73. struct tegra_rt5677 *machine = snd_soc_card_get_drvdata(card);
  74. if (!gpio_is_valid(machine->gpio_hp_en))
  75. return 0;
  76. gpio_set_value_cansleep(machine->gpio_hp_en,
  77. SND_SOC_DAPM_EVENT_ON(event));
  78. return 0;
  79. }
  80. static struct snd_soc_ops tegra_rt5677_ops = {
  81. .hw_params = tegra_rt5677_asoc_hw_params,
  82. };
  83. static struct snd_soc_jack tegra_rt5677_hp_jack;
  84. static struct snd_soc_jack_pin tegra_rt5677_hp_jack_pins = {
  85. .pin = "Headphone",
  86. .mask = SND_JACK_HEADPHONE,
  87. };
  88. static struct snd_soc_jack_gpio tegra_rt5677_hp_jack_gpio = {
  89. .name = "Headphone detection",
  90. .report = SND_JACK_HEADPHONE,
  91. .debounce_time = 150,
  92. };
  93. static struct snd_soc_jack tegra_rt5677_mic_jack;
  94. static struct snd_soc_jack_pin tegra_rt5677_mic_jack_pins = {
  95. .pin = "Headset Mic",
  96. .mask = SND_JACK_MICROPHONE,
  97. };
  98. static struct snd_soc_jack_gpio tegra_rt5677_mic_jack_gpio = {
  99. .name = "Headset Mic detection",
  100. .report = SND_JACK_MICROPHONE,
  101. .debounce_time = 150,
  102. .invert = 1
  103. };
  104. static const struct snd_soc_dapm_widget tegra_rt5677_dapm_widgets[] = {
  105. SND_SOC_DAPM_SPK("Speaker", NULL),
  106. SND_SOC_DAPM_HP("Headphone", tegra_rt5677_event_hp),
  107. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  108. SND_SOC_DAPM_MIC("Internal Mic 1", NULL),
  109. SND_SOC_DAPM_MIC("Internal Mic 2", NULL),
  110. };
  111. static const struct snd_kcontrol_new tegra_rt5677_controls[] = {
  112. SOC_DAPM_PIN_SWITCH("Speaker"),
  113. SOC_DAPM_PIN_SWITCH("Headphone"),
  114. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  115. SOC_DAPM_PIN_SWITCH("Internal Mic 1"),
  116. SOC_DAPM_PIN_SWITCH("Internal Mic 2"),
  117. };
  118. static int tegra_rt5677_asoc_init(struct snd_soc_pcm_runtime *rtd)
  119. {
  120. struct tegra_rt5677 *machine = snd_soc_card_get_drvdata(rtd->card);
  121. snd_soc_card_jack_new(rtd->card, "Headphone Jack", SND_JACK_HEADPHONE,
  122. &tegra_rt5677_hp_jack,
  123. &tegra_rt5677_hp_jack_pins, 1);
  124. if (gpio_is_valid(machine->gpio_hp_det)) {
  125. tegra_rt5677_hp_jack_gpio.gpio = machine->gpio_hp_det;
  126. snd_soc_jack_add_gpios(&tegra_rt5677_hp_jack, 1,
  127. &tegra_rt5677_hp_jack_gpio);
  128. }
  129. snd_soc_card_jack_new(rtd->card, "Mic Jack", SND_JACK_MICROPHONE,
  130. &tegra_rt5677_mic_jack,
  131. &tegra_rt5677_mic_jack_pins, 1);
  132. if (gpio_is_valid(machine->gpio_mic_present)) {
  133. tegra_rt5677_mic_jack_gpio.gpio = machine->gpio_mic_present;
  134. snd_soc_jack_add_gpios(&tegra_rt5677_mic_jack, 1,
  135. &tegra_rt5677_mic_jack_gpio);
  136. }
  137. snd_soc_dapm_force_enable_pin(&rtd->card->dapm, "MICBIAS1");
  138. return 0;
  139. }
  140. static int tegra_rt5677_card_remove(struct snd_soc_card *card)
  141. {
  142. struct tegra_rt5677 *machine = snd_soc_card_get_drvdata(card);
  143. if (gpio_is_valid(machine->gpio_hp_det)) {
  144. snd_soc_jack_free_gpios(&tegra_rt5677_hp_jack, 1,
  145. &tegra_rt5677_hp_jack_gpio);
  146. }
  147. if (gpio_is_valid(machine->gpio_mic_present)) {
  148. snd_soc_jack_free_gpios(&tegra_rt5677_mic_jack, 1,
  149. &tegra_rt5677_mic_jack_gpio);
  150. }
  151. return 0;
  152. }
  153. static struct snd_soc_dai_link tegra_rt5677_dai = {
  154. .name = "RT5677",
  155. .stream_name = "RT5677 PCM",
  156. .codec_dai_name = "rt5677-aif1",
  157. .init = tegra_rt5677_asoc_init,
  158. .ops = &tegra_rt5677_ops,
  159. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  160. SND_SOC_DAIFMT_CBS_CFS,
  161. };
  162. static struct snd_soc_card snd_soc_tegra_rt5677 = {
  163. .name = "tegra-rt5677",
  164. .owner = THIS_MODULE,
  165. .remove = tegra_rt5677_card_remove,
  166. .dai_link = &tegra_rt5677_dai,
  167. .num_links = 1,
  168. .controls = tegra_rt5677_controls,
  169. .num_controls = ARRAY_SIZE(tegra_rt5677_controls),
  170. .dapm_widgets = tegra_rt5677_dapm_widgets,
  171. .num_dapm_widgets = ARRAY_SIZE(tegra_rt5677_dapm_widgets),
  172. .fully_routed = true,
  173. };
  174. static int tegra_rt5677_probe(struct platform_device *pdev)
  175. {
  176. struct device_node *np = pdev->dev.of_node;
  177. struct snd_soc_card *card = &snd_soc_tegra_rt5677;
  178. struct tegra_rt5677 *machine;
  179. int ret;
  180. machine = devm_kzalloc(&pdev->dev,
  181. sizeof(struct tegra_rt5677), GFP_KERNEL);
  182. if (!machine)
  183. return -ENOMEM;
  184. card->dev = &pdev->dev;
  185. platform_set_drvdata(pdev, card);
  186. snd_soc_card_set_drvdata(card, machine);
  187. machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
  188. if (machine->gpio_hp_det == -EPROBE_DEFER)
  189. return -EPROBE_DEFER;
  190. machine->gpio_mic_present = of_get_named_gpio(np,
  191. "nvidia,mic-present-gpios", 0);
  192. if (machine->gpio_mic_present == -EPROBE_DEFER)
  193. return -EPROBE_DEFER;
  194. machine->gpio_hp_en = of_get_named_gpio(np, "nvidia,hp-en-gpios", 0);
  195. if (machine->gpio_hp_en == -EPROBE_DEFER)
  196. return -EPROBE_DEFER;
  197. if (gpio_is_valid(machine->gpio_hp_en)) {
  198. ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_en,
  199. GPIOF_OUT_INIT_LOW, "hp_en");
  200. if (ret) {
  201. dev_err(card->dev, "cannot get hp_en gpio\n");
  202. return ret;
  203. }
  204. }
  205. machine->gpio_dmic_clk_en = of_get_named_gpio(np,
  206. "nvidia,dmic-clk-en-gpios", 0);
  207. if (machine->gpio_dmic_clk_en == -EPROBE_DEFER)
  208. return -EPROBE_DEFER;
  209. if (gpio_is_valid(machine->gpio_dmic_clk_en)) {
  210. ret = devm_gpio_request_one(&pdev->dev,
  211. machine->gpio_dmic_clk_en,
  212. GPIOF_OUT_INIT_HIGH, "dmic_clk_en");
  213. if (ret) {
  214. dev_err(card->dev, "cannot get dmic_clk_en gpio\n");
  215. return ret;
  216. }
  217. }
  218. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  219. if (ret)
  220. goto err;
  221. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  222. if (ret)
  223. goto err;
  224. tegra_rt5677_dai.codec_of_node = of_parse_phandle(np,
  225. "nvidia,audio-codec", 0);
  226. if (!tegra_rt5677_dai.codec_of_node) {
  227. dev_err(&pdev->dev,
  228. "Property 'nvidia,audio-codec' missing or invalid\n");
  229. ret = -EINVAL;
  230. goto err;
  231. }
  232. tegra_rt5677_dai.cpu_of_node = of_parse_phandle(np,
  233. "nvidia,i2s-controller", 0);
  234. if (!tegra_rt5677_dai.cpu_of_node) {
  235. dev_err(&pdev->dev,
  236. "Property 'nvidia,i2s-controller' missing or invalid\n");
  237. ret = -EINVAL;
  238. goto err;
  239. }
  240. tegra_rt5677_dai.platform_of_node = tegra_rt5677_dai.cpu_of_node;
  241. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  242. if (ret)
  243. goto err;
  244. ret = snd_soc_register_card(card);
  245. if (ret) {
  246. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  247. ret);
  248. goto err_fini_utils;
  249. }
  250. return 0;
  251. err_fini_utils:
  252. tegra_asoc_utils_fini(&machine->util_data);
  253. err:
  254. return ret;
  255. }
  256. static int tegra_rt5677_remove(struct platform_device *pdev)
  257. {
  258. struct snd_soc_card *card = platform_get_drvdata(pdev);
  259. struct tegra_rt5677 *machine = snd_soc_card_get_drvdata(card);
  260. snd_soc_unregister_card(card);
  261. tegra_asoc_utils_fini(&machine->util_data);
  262. return 0;
  263. }
  264. static const struct of_device_id tegra_rt5677_of_match[] = {
  265. { .compatible = "nvidia,tegra-audio-rt5677", },
  266. {},
  267. };
  268. static struct platform_driver tegra_rt5677_driver = {
  269. .driver = {
  270. .name = DRV_NAME,
  271. .pm = &snd_soc_pm_ops,
  272. .of_match_table = tegra_rt5677_of_match,
  273. },
  274. .probe = tegra_rt5677_probe,
  275. .remove = tegra_rt5677_remove,
  276. };
  277. module_platform_driver(tegra_rt5677_driver);
  278. MODULE_AUTHOR("Anatol Pomozov <anatol@google.com>");
  279. MODULE_DESCRIPTION("Tegra+RT5677 machine ASoC driver");
  280. MODULE_LICENSE("GPL v2");
  281. MODULE_ALIAS("platform:" DRV_NAME);
  282. MODULE_DEVICE_TABLE(of, tegra_rt5677_of_match);