imx-sgtl5000.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Copyright 2012 Linaro Ltd.
  4. *
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * http://www.opensource.org/licenses/gpl-license.html
  10. * http://www.gnu.org/copyleft/gpl.html
  11. */
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/i2c.h>
  16. #include <linux/clk.h>
  17. #include <sound/soc.h>
  18. #include "../codecs/sgtl5000.h"
  19. #include "imx-audmux.h"
  20. #define DAI_NAME_SIZE 32
  21. struct imx_sgtl5000_data {
  22. struct snd_soc_dai_link dai;
  23. struct snd_soc_card card;
  24. char codec_dai_name[DAI_NAME_SIZE];
  25. char platform_name[DAI_NAME_SIZE];
  26. struct clk *codec_clk;
  27. unsigned int clk_frequency;
  28. };
  29. static int imx_sgtl5000_dai_init(struct snd_soc_pcm_runtime *rtd)
  30. {
  31. struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(rtd->card);
  32. struct device *dev = rtd->card->dev;
  33. int ret;
  34. ret = snd_soc_dai_set_sysclk(rtd->codec_dai, SGTL5000_SYSCLK,
  35. data->clk_frequency, SND_SOC_CLOCK_IN);
  36. if (ret) {
  37. dev_err(dev, "could not set codec driver clock params\n");
  38. return ret;
  39. }
  40. return 0;
  41. }
  42. static const struct snd_soc_dapm_widget imx_sgtl5000_dapm_widgets[] = {
  43. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  44. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  45. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  46. SND_SOC_DAPM_SPK("Line Out Jack", NULL),
  47. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  48. };
  49. static int imx_sgtl5000_probe(struct platform_device *pdev)
  50. {
  51. struct device_node *np = pdev->dev.of_node;
  52. struct device_node *ssi_np, *codec_np;
  53. struct platform_device *ssi_pdev;
  54. struct i2c_client *codec_dev;
  55. struct imx_sgtl5000_data *data = NULL;
  56. int int_port, ext_port;
  57. int ret;
  58. ret = of_property_read_u32(np, "mux-int-port", &int_port);
  59. if (ret) {
  60. dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
  61. return ret;
  62. }
  63. ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
  64. if (ret) {
  65. dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
  66. return ret;
  67. }
  68. /*
  69. * The port numbering in the hardware manual starts at 1, while
  70. * the audmux API expects it starts at 0.
  71. */
  72. int_port--;
  73. ext_port--;
  74. ret = imx_audmux_v2_configure_port(int_port,
  75. IMX_AUDMUX_V2_PTCR_SYN |
  76. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  77. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  78. IMX_AUDMUX_V2_PTCR_TFSDIR |
  79. IMX_AUDMUX_V2_PTCR_TCLKDIR,
  80. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
  81. if (ret) {
  82. dev_err(&pdev->dev, "audmux internal port setup failed\n");
  83. return ret;
  84. }
  85. ret = imx_audmux_v2_configure_port(ext_port,
  86. IMX_AUDMUX_V2_PTCR_SYN,
  87. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
  88. if (ret) {
  89. dev_err(&pdev->dev, "audmux external port setup failed\n");
  90. return ret;
  91. }
  92. ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
  93. codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
  94. if (!ssi_np || !codec_np) {
  95. dev_err(&pdev->dev, "phandle missing or invalid\n");
  96. ret = -EINVAL;
  97. goto fail;
  98. }
  99. ssi_pdev = of_find_device_by_node(ssi_np);
  100. if (!ssi_pdev) {
  101. dev_err(&pdev->dev, "failed to find SSI platform device\n");
  102. ret = -EPROBE_DEFER;
  103. goto fail;
  104. }
  105. codec_dev = of_find_i2c_device_by_node(codec_np);
  106. if (!codec_dev) {
  107. dev_err(&pdev->dev, "failed to find codec platform device\n");
  108. return -EPROBE_DEFER;
  109. }
  110. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  111. if (!data) {
  112. ret = -ENOMEM;
  113. goto fail;
  114. }
  115. data->codec_clk = clk_get(&codec_dev->dev, NULL);
  116. if (IS_ERR(data->codec_clk)) {
  117. ret = PTR_ERR(data->codec_clk);
  118. goto fail;
  119. }
  120. data->clk_frequency = clk_get_rate(data->codec_clk);
  121. data->dai.name = "HiFi";
  122. data->dai.stream_name = "HiFi";
  123. data->dai.codec_dai_name = "sgtl5000";
  124. data->dai.codec_of_node = codec_np;
  125. data->dai.cpu_of_node = ssi_np;
  126. data->dai.platform_of_node = ssi_np;
  127. data->dai.init = &imx_sgtl5000_dai_init;
  128. data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  129. SND_SOC_DAIFMT_CBM_CFM;
  130. data->card.dev = &pdev->dev;
  131. ret = snd_soc_of_parse_card_name(&data->card, "model");
  132. if (ret)
  133. goto fail;
  134. ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
  135. if (ret)
  136. goto fail;
  137. data->card.num_links = 1;
  138. data->card.owner = THIS_MODULE;
  139. data->card.dai_link = &data->dai;
  140. data->card.dapm_widgets = imx_sgtl5000_dapm_widgets;
  141. data->card.num_dapm_widgets = ARRAY_SIZE(imx_sgtl5000_dapm_widgets);
  142. platform_set_drvdata(pdev, &data->card);
  143. snd_soc_card_set_drvdata(&data->card, data);
  144. ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
  145. if (ret) {
  146. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
  147. goto fail;
  148. }
  149. of_node_put(ssi_np);
  150. of_node_put(codec_np);
  151. return 0;
  152. fail:
  153. if (data && !IS_ERR(data->codec_clk))
  154. clk_put(data->codec_clk);
  155. of_node_put(ssi_np);
  156. of_node_put(codec_np);
  157. return ret;
  158. }
  159. static int imx_sgtl5000_remove(struct platform_device *pdev)
  160. {
  161. struct snd_soc_card *card = platform_get_drvdata(pdev);
  162. struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(card);
  163. clk_put(data->codec_clk);
  164. return 0;
  165. }
  166. static const struct of_device_id imx_sgtl5000_dt_ids[] = {
  167. { .compatible = "fsl,imx-audio-sgtl5000", },
  168. { /* sentinel */ }
  169. };
  170. MODULE_DEVICE_TABLE(of, imx_sgtl5000_dt_ids);
  171. static struct platform_driver imx_sgtl5000_driver = {
  172. .driver = {
  173. .name = "imx-sgtl5000",
  174. .pm = &snd_soc_pm_ops,
  175. .of_match_table = imx_sgtl5000_dt_ids,
  176. },
  177. .probe = imx_sgtl5000_probe,
  178. .remove = imx_sgtl5000_remove,
  179. };
  180. module_platform_driver(imx_sgtl5000_driver);
  181. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  182. MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver");
  183. MODULE_LICENSE("GPL v2");
  184. MODULE_ALIAS("platform:imx-sgtl5000");