tegra_alc5632.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. *
  6. * Authors: Leon Romanovsky <leon@leon.nu>
  7. * Andrey Danin <danindrey@mail.ru>
  8. * Marc Dietrich <marvin24@gmx.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <asm/mach-types.h>
  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_das.h"
  27. #include "tegra_i2s.h"
  28. #include "tegra_pcm.h"
  29. #include "tegra_asoc_utils.h"
  30. #define DRV_NAME "tegra-alc5632"
  31. #define GPIO_HP_DET BIT(0)
  32. struct tegra_alc5632 {
  33. struct tegra_asoc_utils_data util_data;
  34. struct platform_device *pcm_dev;
  35. int gpio_requested;
  36. int gpio_hp_det;
  37. };
  38. static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream,
  39. struct snd_pcm_hw_params *params)
  40. {
  41. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  42. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  43. struct snd_soc_codec *codec = rtd->codec;
  44. struct snd_soc_card *card = codec->card;
  45. struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
  46. int srate, mclk;
  47. int err;
  48. srate = params_rate(params);
  49. mclk = 512 * srate;
  50. err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk);
  51. if (err < 0) {
  52. dev_err(card->dev, "Can't configure clocks\n");
  53. return err;
  54. }
  55. err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  56. SND_SOC_CLOCK_IN);
  57. if (err < 0) {
  58. dev_err(card->dev, "codec_dai clock not set\n");
  59. return err;
  60. }
  61. return 0;
  62. }
  63. static struct snd_soc_ops tegra_alc5632_asoc_ops = {
  64. .hw_params = tegra_alc5632_asoc_hw_params,
  65. };
  66. static struct snd_soc_jack tegra_alc5632_hs_jack;
  67. static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
  68. {
  69. .pin = "Headset Mic",
  70. .mask = SND_JACK_MICROPHONE,
  71. },
  72. {
  73. .pin = "Headset Stereophone",
  74. .mask = SND_JACK_HEADPHONE,
  75. },
  76. };
  77. static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = {
  78. .name = "Headset detection",
  79. .report = SND_JACK_HEADSET,
  80. .debounce_time = 150,
  81. .invert = 1,
  82. };
  83. static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
  84. SND_SOC_DAPM_SPK("Int Spk", NULL),
  85. SND_SOC_DAPM_HP("Headset Stereophone", NULL),
  86. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  87. };
  88. static const struct snd_soc_dapm_route tegra_alc5632_audio_map[] = {
  89. /* Internal Speaker */
  90. {"Int Spk", NULL, "SPKOUT"},
  91. {"Int Spk", NULL, "SPKOUTN"},
  92. /* Headset Mic */
  93. {"MIC1", NULL, "MICBIAS1"},
  94. {"MICBIAS1", NULL, "Headset Mic"},
  95. /* Headset Stereophone */
  96. {"Headset Stereophone", NULL, "HPR"},
  97. {"Headset Stereophone", NULL, "HPL"},
  98. };
  99. static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
  100. SOC_DAPM_PIN_SWITCH("Int Spk"),
  101. };
  102. static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
  103. {
  104. struct snd_soc_codec *codec = rtd->codec;
  105. struct snd_soc_dapm_context *dapm = &codec->dapm;
  106. struct device_node *np = codec->card->dev->of_node;
  107. struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(codec->card);
  108. int ret;
  109. snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
  110. &tegra_alc5632_hs_jack);
  111. snd_soc_jack_add_pins(&tegra_alc5632_hs_jack,
  112. ARRAY_SIZE(tegra_alc5632_hs_jack_pins),
  113. tegra_alc5632_hs_jack_pins);
  114. machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
  115. if (gpio_is_valid(machine->gpio_hp_det)) {
  116. tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
  117. snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
  118. 1,
  119. &tegra_alc5632_hp_jack_gpio);
  120. machine->gpio_requested |= GPIO_HP_DET;
  121. }
  122. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
  123. return 0;
  124. }
  125. static struct snd_soc_dai_link tegra_alc5632_dai = {
  126. .name = "ALC5632",
  127. .stream_name = "ALC5632 PCM",
  128. .platform_name = "tegra-pcm-audio",
  129. .codec_dai_name = "alc5632-hifi",
  130. .init = tegra_alc5632_asoc_init,
  131. .ops = &tegra_alc5632_asoc_ops,
  132. .dai_fmt = SND_SOC_DAIFMT_I2S
  133. | SND_SOC_DAIFMT_NB_NF
  134. | SND_SOC_DAIFMT_CBS_CFS,
  135. };
  136. static struct snd_soc_card snd_soc_tegra_alc5632 = {
  137. .name = "tegra-alc5632",
  138. .owner = THIS_MODULE,
  139. .dai_link = &tegra_alc5632_dai,
  140. .num_links = 1,
  141. .controls = tegra_alc5632_controls,
  142. .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
  143. .dapm_widgets = tegra_alc5632_dapm_widgets,
  144. .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
  145. .dapm_routes = tegra_alc5632_audio_map,
  146. .num_dapm_routes = ARRAY_SIZE(tegra_alc5632_audio_map),
  147. .fully_routed = true,
  148. };
  149. static __devinit int tegra_alc5632_probe(struct platform_device *pdev)
  150. {
  151. struct snd_soc_card *card = &snd_soc_tegra_alc5632;
  152. struct tegra_alc5632 *alc5632;
  153. int ret;
  154. alc5632 = devm_kzalloc(&pdev->dev,
  155. sizeof(struct tegra_alc5632), GFP_KERNEL);
  156. if (!alc5632) {
  157. dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n");
  158. ret = -ENOMEM;
  159. goto err;
  160. }
  161. card->dev = &pdev->dev;
  162. platform_set_drvdata(pdev, card);
  163. snd_soc_card_set_drvdata(card, alc5632);
  164. alc5632->pcm_dev = ERR_PTR(-EINVAL);
  165. if (!(pdev->dev.of_node)) {
  166. dev_err(&pdev->dev, "Must be instantiated using device tree\n");
  167. ret = -EINVAL;
  168. goto err;
  169. }
  170. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  171. if (ret)
  172. goto err;
  173. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  174. if (ret)
  175. goto err;
  176. tegra_alc5632_dai.codec_of_node = of_parse_phandle(
  177. pdev->dev.of_node, "nvidia,audio-codec", 0);
  178. if (!tegra_alc5632_dai.codec_of_node) {
  179. dev_err(&pdev->dev,
  180. "Property 'nvidia,audio-codec' missing or invalid\n");
  181. ret = -EINVAL;
  182. goto err;
  183. }
  184. tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle(
  185. pdev->dev.of_node, "nvidia,i2s-controller", 0);
  186. if (!tegra_alc5632_dai.cpu_dai_of_node) {
  187. dev_err(&pdev->dev,
  188. "Property 'nvidia,i2s-controller' missing or invalid\n");
  189. ret = -EINVAL;
  190. goto err;
  191. }
  192. alc5632->pcm_dev = platform_device_register_simple(
  193. "tegra-pcm-audio", -1, NULL, 0);
  194. if (IS_ERR(alc5632->pcm_dev)) {
  195. dev_err(&pdev->dev,
  196. "Can't instantiate tegra-pcm-audio\n");
  197. ret = PTR_ERR(alc5632->pcm_dev);
  198. goto err;
  199. }
  200. ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
  201. if (ret)
  202. goto err_unregister;
  203. ret = snd_soc_register_card(card);
  204. if (ret) {
  205. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  206. ret);
  207. goto err_fini_utils;
  208. }
  209. return 0;
  210. err_fini_utils:
  211. tegra_asoc_utils_fini(&alc5632->util_data);
  212. err_unregister:
  213. if (!IS_ERR(alc5632->pcm_dev))
  214. platform_device_unregister(alc5632->pcm_dev);
  215. err:
  216. return ret;
  217. }
  218. static int __devexit tegra_alc5632_remove(struct platform_device *pdev)
  219. {
  220. struct snd_soc_card *card = platform_get_drvdata(pdev);
  221. struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
  222. if (machine->gpio_requested & GPIO_HP_DET)
  223. snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack,
  224. 1,
  225. &tegra_alc5632_hp_jack_gpio);
  226. machine->gpio_requested = 0;
  227. snd_soc_unregister_card(card);
  228. tegra_asoc_utils_fini(&machine->util_data);
  229. if (!IS_ERR(machine->pcm_dev))
  230. platform_device_unregister(machine->pcm_dev);
  231. return 0;
  232. }
  233. static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = {
  234. { .compatible = "nvidia,tegra-audio-alc5632", },
  235. {},
  236. };
  237. static struct platform_driver tegra_alc5632_driver = {
  238. .driver = {
  239. .name = DRV_NAME,
  240. .owner = THIS_MODULE,
  241. .pm = &snd_soc_pm_ops,
  242. .of_match_table = tegra_alc5632_of_match,
  243. },
  244. .probe = tegra_alc5632_probe,
  245. .remove = __devexit_p(tegra_alc5632_remove),
  246. };
  247. module_platform_driver(tegra_alc5632_driver);
  248. MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
  249. MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
  250. MODULE_LICENSE("GPL");
  251. MODULE_ALIAS("platform:" DRV_NAME);
  252. MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);