tegra_alc5632.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <sound/core.h>
  20. #include <sound/jack.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include "../codecs/alc5632.h"
  25. #include "tegra_das.h"
  26. #include "tegra_i2s.h"
  27. #include "tegra_pcm.h"
  28. #include "tegra_asoc_utils.h"
  29. #define DRV_NAME "tegra-alc5632"
  30. struct tegra_alc5632 {
  31. struct tegra_asoc_utils_data util_data;
  32. struct platform_device *pcm_dev;
  33. };
  34. static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream,
  35. struct snd_pcm_hw_params *params)
  36. {
  37. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  38. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  39. struct snd_soc_codec *codec = rtd->codec;
  40. struct snd_soc_card *card = codec->card;
  41. struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
  42. int srate, mclk;
  43. int err;
  44. srate = params_rate(params);
  45. mclk = 512 * srate;
  46. err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk);
  47. if (err < 0) {
  48. dev_err(card->dev, "Can't configure clocks\n");
  49. return err;
  50. }
  51. err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  52. SND_SOC_CLOCK_IN);
  53. if (err < 0) {
  54. dev_err(card->dev, "codec_dai clock not set\n");
  55. return err;
  56. }
  57. return 0;
  58. }
  59. static struct snd_soc_ops tegra_alc5632_asoc_ops = {
  60. .hw_params = tegra_alc5632_asoc_hw_params,
  61. };
  62. static struct snd_soc_jack tegra_alc5632_hs_jack;
  63. static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
  64. {
  65. .pin = "Headset Mic",
  66. .mask = SND_JACK_MICROPHONE,
  67. },
  68. {
  69. .pin = "Headset Stereophone",
  70. .mask = SND_JACK_HEADPHONE,
  71. },
  72. };
  73. static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
  74. SND_SOC_DAPM_SPK("Int Spk", NULL),
  75. SND_SOC_DAPM_HP("Headset Stereophone", NULL),
  76. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  77. };
  78. static const struct snd_soc_dapm_route tegra_alc5632_audio_map[] = {
  79. /* Internal Speaker */
  80. {"Int Spk", NULL, "SPKOUT"},
  81. {"Int Spk", NULL, "SPKOUTN"},
  82. /* Headset Mic */
  83. {"MIC1", NULL, "MICBIAS1"},
  84. {"MICBIAS1", NULL, "Headset Mic"},
  85. /* Headset Stereophone */
  86. {"Headset Stereophone", NULL, "HPR"},
  87. {"Headset Stereophone", NULL, "HPL"},
  88. };
  89. static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
  90. SOC_DAPM_PIN_SWITCH("Int Spk"),
  91. };
  92. static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
  93. {
  94. struct snd_soc_codec *codec = rtd->codec;
  95. struct snd_soc_dapm_context *dapm = &codec->dapm;
  96. snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
  97. &tegra_alc5632_hs_jack);
  98. snd_soc_jack_add_pins(&tegra_alc5632_hs_jack,
  99. ARRAY_SIZE(tegra_alc5632_hs_jack_pins),
  100. tegra_alc5632_hs_jack_pins);
  101. snd_soc_dapm_force_enable_pin(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. .platform_name = "tegra-pcm-audio",
  108. .codec_dai_name = "alc5632-hifi",
  109. .init = tegra_alc5632_asoc_init,
  110. .ops = &tegra_alc5632_asoc_ops,
  111. .dai_fmt = SND_SOC_DAIFMT_I2S
  112. | SND_SOC_DAIFMT_NB_NF
  113. | SND_SOC_DAIFMT_CBS_CFS,
  114. };
  115. static struct snd_soc_card snd_soc_tegra_alc5632 = {
  116. .name = "tegra-alc5632",
  117. .owner = THIS_MODULE,
  118. .dai_link = &tegra_alc5632_dai,
  119. .num_links = 1,
  120. .controls = tegra_alc5632_controls,
  121. .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
  122. .dapm_widgets = tegra_alc5632_dapm_widgets,
  123. .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
  124. .dapm_routes = tegra_alc5632_audio_map,
  125. .num_dapm_routes = ARRAY_SIZE(tegra_alc5632_audio_map),
  126. .fully_routed = true,
  127. };
  128. static __devinit int tegra_alc5632_probe(struct platform_device *pdev)
  129. {
  130. struct snd_soc_card *card = &snd_soc_tegra_alc5632;
  131. struct tegra_alc5632 *alc5632;
  132. int ret;
  133. alc5632 = devm_kzalloc(&pdev->dev,
  134. sizeof(struct tegra_alc5632), GFP_KERNEL);
  135. if (!alc5632) {
  136. dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n");
  137. ret = -ENOMEM;
  138. goto err;
  139. }
  140. card->dev = &pdev->dev;
  141. platform_set_drvdata(pdev, card);
  142. snd_soc_card_set_drvdata(card, alc5632);
  143. alc5632->pcm_dev = ERR_PTR(-EINVAL);
  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. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  150. if (ret)
  151. goto err;
  152. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  153. if (ret)
  154. goto err;
  155. tegra_alc5632_dai.codec_of_node = of_parse_phandle(
  156. pdev->dev.of_node, "nvidia,audio-codec", 0);
  157. if (!tegra_alc5632_dai.codec_of_node) {
  158. dev_err(&pdev->dev,
  159. "Property 'nvidia,audio-codec' missing or invalid\n");
  160. ret = -EINVAL;
  161. goto err;
  162. }
  163. tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle(
  164. pdev->dev.of_node, "nvidia,i2s-controller", 0);
  165. if (!tegra_alc5632_dai.cpu_dai_of_node) {
  166. dev_err(&pdev->dev,
  167. "Property 'nvidia,i2s-controller' missing or invalid\n");
  168. ret = -EINVAL;
  169. goto err;
  170. }
  171. alc5632->pcm_dev = platform_device_register_simple(
  172. "tegra-pcm-audio", -1, NULL, 0);
  173. if (IS_ERR(alc5632->pcm_dev)) {
  174. dev_err(&pdev->dev,
  175. "Can't instantiate tegra-pcm-audio\n");
  176. ret = PTR_ERR(alc5632->pcm_dev);
  177. goto err;
  178. }
  179. ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
  180. if (ret)
  181. goto err_unregister;
  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(&alc5632->util_data);
  191. err_unregister:
  192. if (!IS_ERR(alc5632->pcm_dev))
  193. platform_device_unregister(alc5632->pcm_dev);
  194. err:
  195. return ret;
  196. }
  197. static int __devexit tegra_alc5632_remove(struct platform_device *pdev)
  198. {
  199. struct snd_soc_card *card = platform_get_drvdata(pdev);
  200. struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
  201. snd_soc_unregister_card(card);
  202. tegra_asoc_utils_fini(&alc5632->util_data);
  203. if (!IS_ERR(alc5632->pcm_dev))
  204. platform_device_unregister(alc5632->pcm_dev);
  205. return 0;
  206. }
  207. static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = {
  208. { .compatible = "nvidia,tegra-audio-alc5632", },
  209. {},
  210. };
  211. static struct platform_driver tegra_alc5632_driver = {
  212. .driver = {
  213. .name = DRV_NAME,
  214. .owner = THIS_MODULE,
  215. .pm = &snd_soc_pm_ops,
  216. .of_match_table = tegra_alc5632_of_match,
  217. },
  218. .probe = tegra_alc5632_probe,
  219. .remove = __devexit_p(tegra_alc5632_remove),
  220. };
  221. module_platform_driver(tegra_alc5632_driver);
  222. MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
  223. MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
  224. MODULE_LICENSE("GPL");
  225. MODULE_ALIAS("platform:" DRV_NAME);
  226. MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);