imx-spdif.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  3. *
  4. * The code contained herein is licensed under the GNU General Public
  5. * License. You may obtain a copy of the GNU General Public License
  6. * Version 2 or later at the following locations:
  7. *
  8. * http://www.opensource.org/licenses/gpl-license.html
  9. * http://www.gnu.org/copyleft/gpl.html
  10. */
  11. #include <linux/module.h>
  12. #include <linux/of_platform.h>
  13. #include <sound/soc.h>
  14. struct imx_spdif_data {
  15. struct snd_soc_dai_link dai[2];
  16. struct snd_soc_card card;
  17. struct platform_device *txdev;
  18. struct platform_device *rxdev;
  19. };
  20. static int imx_spdif_audio_probe(struct platform_device *pdev)
  21. {
  22. struct device_node *spdif_np, *np = pdev->dev.of_node;
  23. struct imx_spdif_data *data;
  24. int ret = 0, num_links = 0;
  25. spdif_np = of_parse_phandle(np, "spdif-controller", 0);
  26. if (!spdif_np) {
  27. dev_err(&pdev->dev, "failed to find spdif-controller\n");
  28. ret = -EINVAL;
  29. goto end;
  30. }
  31. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  32. if (!data) {
  33. dev_err(&pdev->dev, "failed to allocate memory\n");
  34. ret = -ENOMEM;
  35. goto end;
  36. }
  37. if (of_property_read_bool(np, "spdif-out")) {
  38. data->dai[num_links].name = "S/PDIF TX";
  39. data->dai[num_links].stream_name = "S/PDIF PCM Playback";
  40. data->dai[num_links].codec_dai_name = "dit-hifi";
  41. data->dai[num_links].codec_name = "spdif-dit";
  42. data->dai[num_links].cpu_of_node = spdif_np;
  43. data->dai[num_links].platform_of_node = spdif_np;
  44. num_links++;
  45. data->txdev = platform_device_register_simple("spdif-dit", -1, NULL, 0);
  46. if (IS_ERR(data->txdev)) {
  47. ret = PTR_ERR(data->txdev);
  48. dev_err(&pdev->dev, "register dit failed: %d\n", ret);
  49. goto end;
  50. }
  51. }
  52. if (of_property_read_bool(np, "spdif-in")) {
  53. data->dai[num_links].name = "S/PDIF RX";
  54. data->dai[num_links].stream_name = "S/PDIF PCM Capture";
  55. data->dai[num_links].codec_dai_name = "dir-hifi";
  56. data->dai[num_links].codec_name = "spdif-dir";
  57. data->dai[num_links].cpu_of_node = spdif_np;
  58. data->dai[num_links].platform_of_node = spdif_np;
  59. num_links++;
  60. data->rxdev = platform_device_register_simple("spdif-dir", -1, NULL, 0);
  61. if (IS_ERR(data->rxdev)) {
  62. ret = PTR_ERR(data->rxdev);
  63. dev_err(&pdev->dev, "register dir failed: %d\n", ret);
  64. goto error_dit;
  65. }
  66. }
  67. if (!num_links) {
  68. dev_err(&pdev->dev, "no enabled S/PDIF DAI link\n");
  69. goto error_dir;
  70. }
  71. data->card.dev = &pdev->dev;
  72. data->card.num_links = num_links;
  73. data->card.dai_link = data->dai;
  74. ret = snd_soc_of_parse_card_name(&data->card, "model");
  75. if (ret)
  76. goto error_dir;
  77. ret = snd_soc_register_card(&data->card);
  78. if (ret) {
  79. dev_err(&pdev->dev, "snd_soc_register_card failed: %d\n", ret);
  80. goto error_dir;
  81. }
  82. platform_set_drvdata(pdev, data);
  83. goto end;
  84. error_dir:
  85. if (data->rxdev)
  86. platform_device_unregister(data->rxdev);
  87. error_dit:
  88. if (data->txdev)
  89. platform_device_unregister(data->txdev);
  90. end:
  91. if (spdif_np)
  92. of_node_put(spdif_np);
  93. return ret;
  94. }
  95. static int imx_spdif_audio_remove(struct platform_device *pdev)
  96. {
  97. struct imx_spdif_data *data = platform_get_drvdata(pdev);
  98. if (data->rxdev)
  99. platform_device_unregister(data->rxdev);
  100. if (data->txdev)
  101. platform_device_unregister(data->txdev);
  102. snd_soc_unregister_card(&data->card);
  103. return 0;
  104. }
  105. static const struct of_device_id imx_spdif_dt_ids[] = {
  106. { .compatible = "fsl,imx-audio-spdif", },
  107. { /* sentinel */ }
  108. };
  109. MODULE_DEVICE_TABLE(of, imx_spdif_dt_ids);
  110. static struct platform_driver imx_spdif_driver = {
  111. .driver = {
  112. .name = "imx-spdif",
  113. .owner = THIS_MODULE,
  114. .of_match_table = imx_spdif_dt_ids,
  115. },
  116. .probe = imx_spdif_audio_probe,
  117. .remove = imx_spdif_audio_remove,
  118. };
  119. module_platform_driver(imx_spdif_driver);
  120. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  121. MODULE_DESCRIPTION("Freescale i.MX S/PDIF machine driver");
  122. MODULE_LICENSE("GPL v2");
  123. MODULE_ALIAS("platform:imx-spdif");