tegra_alc5632.c 6.8 KB

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