tegra_alc5632.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * tegra_alc5632.c -- Toshiba AC100(PAZ00) machine ASoC driver
  3. *
  4. * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
  5. * Copyright (C) 2012 - NVIDIA, Inc.
  6. *
  7. * Authors: Leon Romanovsky <leon@leon.nu>
  8. * Andrey Danin <danindrey@mail.ru>
  9. * Marc Dietrich <marvin24@gmx.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/gpio.h>
  19. #include <linux/of_gpio.h>
  20. #include <sound/core.h>
  21. #include <sound/jack.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include "../codecs/alc5632.h"
  26. #include "tegra_asoc_utils.h"
  27. #define DRV_NAME "tegra-alc5632"
  28. struct tegra_alc5632 {
  29. struct tegra_asoc_utils_data util_data;
  30. int gpio_hp_det;
  31. };
  32. static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream,
  33. struct snd_pcm_hw_params *params)
  34. {
  35. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  36. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  37. struct snd_soc_card *card = rtd->card;
  38. struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
  39. int srate, mclk;
  40. int err;
  41. srate = params_rate(params);
  42. mclk = 512 * srate;
  43. err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk);
  44. if (err < 0) {
  45. dev_err(card->dev, "Can't configure clocks\n");
  46. return err;
  47. }
  48. err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  49. SND_SOC_CLOCK_IN);
  50. if (err < 0) {
  51. dev_err(card->dev, "codec_dai clock not set\n");
  52. return err;
  53. }
  54. return 0;
  55. }
  56. static const struct snd_soc_ops tegra_alc5632_asoc_ops = {
  57. .hw_params = tegra_alc5632_asoc_hw_params,
  58. };
  59. static struct snd_soc_jack tegra_alc5632_hs_jack;
  60. static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
  61. {
  62. .pin = "Headset Mic",
  63. .mask = SND_JACK_MICROPHONE,
  64. },
  65. {
  66. .pin = "Headset Stereophone",
  67. .mask = SND_JACK_HEADPHONE,
  68. },
  69. };
  70. static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = {
  71. .name = "Headset detection",
  72. .report = SND_JACK_HEADSET,
  73. .debounce_time = 150,
  74. };
  75. static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
  76. SND_SOC_DAPM_SPK("Int Spk", NULL),
  77. SND_SOC_DAPM_HP("Headset Stereophone", NULL),
  78. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  79. SND_SOC_DAPM_MIC("Digital Mic", NULL),
  80. };
  81. static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
  82. SOC_DAPM_PIN_SWITCH("Int Spk"),
  83. };
  84. static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
  85. {
  86. int ret;
  87. struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(rtd->card);
  88. ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
  89. SND_JACK_HEADSET,
  90. &tegra_alc5632_hs_jack,
  91. tegra_alc5632_hs_jack_pins,
  92. ARRAY_SIZE(tegra_alc5632_hs_jack_pins));
  93. if (ret)
  94. return ret;
  95. if (gpio_is_valid(machine->gpio_hp_det)) {
  96. tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
  97. snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
  98. 1,
  99. &tegra_alc5632_hp_jack_gpio);
  100. }
  101. snd_soc_dapm_force_enable_pin(&rtd->card->dapm, "MICBIAS1");
  102. return 0;
  103. }
  104. static struct snd_soc_dai_link tegra_alc5632_dai = {
  105. .name = "ALC5632",
  106. .stream_name = "ALC5632 PCM",
  107. .codec_dai_name = "alc5632-hifi",
  108. .init = tegra_alc5632_asoc_init,
  109. .ops = &tegra_alc5632_asoc_ops,
  110. .dai_fmt = SND_SOC_DAIFMT_I2S
  111. | SND_SOC_DAIFMT_NB_NF
  112. | SND_SOC_DAIFMT_CBS_CFS,
  113. };
  114. static struct snd_soc_card snd_soc_tegra_alc5632 = {
  115. .name = "tegra-alc5632",
  116. .owner = THIS_MODULE,
  117. .dai_link = &tegra_alc5632_dai,
  118. .num_links = 1,
  119. .controls = tegra_alc5632_controls,
  120. .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
  121. .dapm_widgets = tegra_alc5632_dapm_widgets,
  122. .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
  123. .fully_routed = true,
  124. };
  125. static int tegra_alc5632_probe(struct platform_device *pdev)
  126. {
  127. struct device_node *np = pdev->dev.of_node;
  128. struct snd_soc_card *card = &snd_soc_tegra_alc5632;
  129. struct tegra_alc5632 *alc5632;
  130. int ret;
  131. alc5632 = devm_kzalloc(&pdev->dev,
  132. sizeof(struct tegra_alc5632), GFP_KERNEL);
  133. if (!alc5632)
  134. return -ENOMEM;
  135. card->dev = &pdev->dev;
  136. snd_soc_card_set_drvdata(card, alc5632);
  137. alc5632->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
  138. if (alc5632->gpio_hp_det == -EPROBE_DEFER)
  139. return -EPROBE_DEFER;
  140. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  141. if (ret)
  142. goto err;
  143. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  144. if (ret)
  145. goto err;
  146. tegra_alc5632_dai.codec_of_node = of_parse_phandle(
  147. pdev->dev.of_node, "nvidia,audio-codec", 0);
  148. if (!tegra_alc5632_dai.codec_of_node) {
  149. dev_err(&pdev->dev,
  150. "Property 'nvidia,audio-codec' missing or invalid\n");
  151. ret = -EINVAL;
  152. goto err;
  153. }
  154. tegra_alc5632_dai.cpu_of_node = of_parse_phandle(np,
  155. "nvidia,i2s-controller", 0);
  156. if (!tegra_alc5632_dai.cpu_of_node) {
  157. dev_err(&pdev->dev,
  158. "Property 'nvidia,i2s-controller' missing or invalid\n");
  159. ret = -EINVAL;
  160. goto err_put_codec_of_node;
  161. }
  162. tegra_alc5632_dai.platform_of_node = tegra_alc5632_dai.cpu_of_node;
  163. ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
  164. if (ret)
  165. goto err_put_cpu_of_node;
  166. ret = snd_soc_register_card(card);
  167. if (ret) {
  168. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  169. ret);
  170. goto err_fini_utils;
  171. }
  172. return 0;
  173. err_fini_utils:
  174. tegra_asoc_utils_fini(&alc5632->util_data);
  175. err_put_cpu_of_node:
  176. of_node_put(tegra_alc5632_dai.cpu_of_node);
  177. tegra_alc5632_dai.cpu_of_node = NULL;
  178. tegra_alc5632_dai.platform_of_node = NULL;
  179. err_put_codec_of_node:
  180. of_node_put(tegra_alc5632_dai.codec_of_node);
  181. tegra_alc5632_dai.codec_of_node = NULL;
  182. err:
  183. return ret;
  184. }
  185. static int tegra_alc5632_remove(struct platform_device *pdev)
  186. {
  187. struct snd_soc_card *card = platform_get_drvdata(pdev);
  188. struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
  189. snd_soc_unregister_card(card);
  190. tegra_asoc_utils_fini(&machine->util_data);
  191. of_node_put(tegra_alc5632_dai.cpu_of_node);
  192. tegra_alc5632_dai.cpu_of_node = NULL;
  193. tegra_alc5632_dai.platform_of_node = NULL;
  194. of_node_put(tegra_alc5632_dai.codec_of_node);
  195. tegra_alc5632_dai.codec_of_node = NULL;
  196. return 0;
  197. }
  198. static const struct of_device_id tegra_alc5632_of_match[] = {
  199. { .compatible = "nvidia,tegra-audio-alc5632", },
  200. {},
  201. };
  202. static struct platform_driver tegra_alc5632_driver = {
  203. .driver = {
  204. .name = DRV_NAME,
  205. .pm = &snd_soc_pm_ops,
  206. .of_match_table = tegra_alc5632_of_match,
  207. },
  208. .probe = tegra_alc5632_probe,
  209. .remove = tegra_alc5632_remove,
  210. };
  211. module_platform_driver(tegra_alc5632_driver);
  212. MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
  213. MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
  214. MODULE_LICENSE("GPL");
  215. MODULE_ALIAS("platform:" DRV_NAME);
  216. MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);