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. put_device(&ssi_pdev->dev);
  106. codec_dev = of_find_i2c_device_by_node(codec_np);
  107. if (!codec_dev) {
  108. dev_err(&pdev->dev, "failed to find codec platform device\n");
  109. return -EPROBE_DEFER;
  110. }
  111. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  112. if (!data) {
  113. ret = -ENOMEM;
  114. goto fail;
  115. }
  116. data->codec_clk = clk_get(&codec_dev->dev, NULL);
  117. if (IS_ERR(data->codec_clk)) {
  118. ret = PTR_ERR(data->codec_clk);
  119. goto fail;
  120. }
  121. data->clk_frequency = clk_get_rate(data->codec_clk);
  122. data->dai.name = "HiFi";
  123. data->dai.stream_name = "HiFi";
  124. data->dai.codec_dai_name = "sgtl5000";
  125. data->dai.codec_of_node = codec_np;
  126. data->dai.cpu_of_node = ssi_np;
  127. data->dai.platform_of_node = ssi_np;
  128. data->dai.init = &imx_sgtl5000_dai_init;
  129. data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  130. SND_SOC_DAIFMT_CBM_CFM;
  131. data->card.dev = &pdev->dev;
  132. ret = snd_soc_of_parse_card_name(&data->card, "model");
  133. if (ret)
  134. goto fail;
  135. ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
  136. if (ret)
  137. goto fail;
  138. data->card.num_links = 1;
  139. data->card.owner = THIS_MODULE;
  140. data->card.dai_link = &data->dai;
  141. data->card.dapm_widgets = imx_sgtl5000_dapm_widgets;
  142. data->card.num_dapm_widgets = ARRAY_SIZE(imx_sgtl5000_dapm_widgets);
  143. platform_set_drvdata(pdev, &data->card);
  144. snd_soc_card_set_drvdata(&data->card, data);
  145. ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
  146. if (ret) {
  147. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
  148. goto fail;
  149. }
  150. of_node_put(ssi_np);
  151. of_node_put(codec_np);
  152. return 0;
  153. fail:
  154. if (data && !IS_ERR(data->codec_clk))
  155. clk_put(data->codec_clk);
  156. of_node_put(ssi_np);
  157. of_node_put(codec_np);
  158. return ret;
  159. }
  160. static int imx_sgtl5000_remove(struct platform_device *pdev)
  161. {
  162. struct snd_soc_card *card = platform_get_drvdata(pdev);
  163. struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(card);
  164. clk_put(data->codec_clk);
  165. return 0;
  166. }
  167. static const struct of_device_id imx_sgtl5000_dt_ids[] = {
  168. { .compatible = "fsl,imx-audio-sgtl5000", },
  169. { /* sentinel */ }
  170. };
  171. MODULE_DEVICE_TABLE(of, imx_sgtl5000_dt_ids);
  172. static struct platform_driver imx_sgtl5000_driver = {
  173. .driver = {
  174. .name = "imx-sgtl5000",
  175. .pm = &snd_soc_pm_ops,
  176. .of_match_table = imx_sgtl5000_dt_ids,
  177. },
  178. .probe = imx_sgtl5000_probe,
  179. .remove = imx_sgtl5000_remove,
  180. };
  181. module_platform_driver(imx_sgtl5000_driver);
  182. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  183. MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver");
  184. MODULE_LICENSE("GPL v2");
  185. MODULE_ALIAS("platform:imx-sgtl5000");