imx-hdmi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright 2017-2020 NXP
  3. #include <linux/module.h>
  4. #include <linux/of_platform.h>
  5. #include <sound/jack.h>
  6. #include <sound/pcm_params.h>
  7. #include <sound/hdmi-codec.h>
  8. #include "fsl_sai.h"
  9. /**
  10. * struct cpu_priv - CPU private data
  11. * @sysclk_id: SYSCLK ids for set_sysclk()
  12. * @slot_width: Slot width of each frame
  13. *
  14. * Note: [1] for tx and [0] for rx
  15. */
  16. struct cpu_priv {
  17. u32 sysclk_id[2];
  18. u32 slot_width;
  19. };
  20. struct imx_hdmi_data {
  21. struct snd_soc_dai_link dai;
  22. struct snd_soc_card card;
  23. struct snd_soc_jack hdmi_jack;
  24. struct snd_soc_jack_pin hdmi_jack_pin;
  25. struct cpu_priv cpu_priv;
  26. u32 dai_fmt;
  27. };
  28. static int imx_hdmi_hw_params(struct snd_pcm_substream *substream,
  29. struct snd_pcm_hw_params *params)
  30. {
  31. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  32. struct imx_hdmi_data *data = snd_soc_card_get_drvdata(rtd->card);
  33. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  34. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  35. struct snd_soc_card *card = rtd->card;
  36. struct device *dev = card->dev;
  37. u32 slot_width = data->cpu_priv.slot_width;
  38. int ret;
  39. /* MCLK always is (256 or 192) * rate. */
  40. ret = snd_soc_dai_set_sysclk(cpu_dai, data->cpu_priv.sysclk_id[tx],
  41. 8 * slot_width * params_rate(params),
  42. tx ? SND_SOC_CLOCK_OUT : SND_SOC_CLOCK_IN);
  43. if (ret && ret != -ENOTSUPP) {
  44. dev_err(dev, "failed to set cpu sysclk: %d\n", ret);
  45. return ret;
  46. }
  47. ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0, 0, 2, slot_width);
  48. if (ret && ret != -ENOTSUPP) {
  49. dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
  50. return ret;
  51. }
  52. return 0;
  53. }
  54. static const struct snd_soc_ops imx_hdmi_ops = {
  55. .hw_params = imx_hdmi_hw_params,
  56. };
  57. static const struct snd_soc_dapm_widget imx_hdmi_widgets[] = {
  58. SND_SOC_DAPM_LINE("HDMI Jack", NULL),
  59. };
  60. static int imx_hdmi_init(struct snd_soc_pcm_runtime *rtd)
  61. {
  62. struct snd_soc_card *card = rtd->card;
  63. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  64. struct snd_soc_component *component = codec_dai->component;
  65. struct imx_hdmi_data *data = snd_soc_card_get_drvdata(card);
  66. int ret;
  67. data->hdmi_jack_pin.pin = "HDMI Jack";
  68. data->hdmi_jack_pin.mask = SND_JACK_LINEOUT;
  69. /* enable jack detection */
  70. ret = snd_soc_card_jack_new_pins(card, "HDMI Jack", SND_JACK_LINEOUT,
  71. &data->hdmi_jack,
  72. &data->hdmi_jack_pin, 1);
  73. if (ret) {
  74. dev_err(card->dev, "Can't new HDMI Jack %d\n", ret);
  75. return ret;
  76. }
  77. ret = snd_soc_component_set_jack(component, &data->hdmi_jack, NULL);
  78. if (ret && ret != -ENOTSUPP) {
  79. dev_err(card->dev, "Can't set HDMI Jack %d\n", ret);
  80. return ret;
  81. }
  82. return 0;
  83. };
  84. static int imx_hdmi_probe(struct platform_device *pdev)
  85. {
  86. struct device_node *np = pdev->dev.of_node;
  87. bool hdmi_out = of_property_read_bool(np, "hdmi-out");
  88. bool hdmi_in = of_property_read_bool(np, "hdmi-in");
  89. struct snd_soc_dai_link_component *dlc;
  90. struct platform_device *cpu_pdev;
  91. struct device_node *cpu_np;
  92. struct imx_hdmi_data *data;
  93. int ret;
  94. dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL);
  95. if (!dlc)
  96. return -ENOMEM;
  97. cpu_np = of_parse_phandle(np, "audio-cpu", 0);
  98. if (!cpu_np) {
  99. dev_err(&pdev->dev, "cpu dai phandle missing or invalid\n");
  100. ret = -EINVAL;
  101. goto fail;
  102. }
  103. cpu_pdev = of_find_device_by_node(cpu_np);
  104. if (!cpu_pdev) {
  105. dev_err(&pdev->dev, "failed to find SAI platform device\n");
  106. ret = -EINVAL;
  107. goto fail;
  108. }
  109. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  110. if (!data) {
  111. ret = -ENOMEM;
  112. put_device(&cpu_pdev->dev);
  113. goto fail;
  114. }
  115. data->dai.cpus = &dlc[0];
  116. data->dai.num_cpus = 1;
  117. data->dai.platforms = &dlc[1];
  118. data->dai.num_platforms = 1;
  119. data->dai.codecs = &dlc[2];
  120. data->dai.num_codecs = 1;
  121. data->dai.name = "i.MX HDMI";
  122. data->dai.stream_name = "i.MX HDMI";
  123. data->dai.cpus->dai_name = dev_name(&cpu_pdev->dev);
  124. data->dai.platforms->of_node = cpu_np;
  125. data->dai.ops = &imx_hdmi_ops;
  126. data->dai.playback_only = true;
  127. data->dai.capture_only = false;
  128. data->dai.init = imx_hdmi_init;
  129. put_device(&cpu_pdev->dev);
  130. if (of_node_name_eq(cpu_np, "sai")) {
  131. data->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
  132. data->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
  133. }
  134. if (of_device_is_compatible(np, "fsl,imx-audio-sii902x")) {
  135. data->dai_fmt = SND_SOC_DAIFMT_LEFT_J;
  136. data->cpu_priv.slot_width = 24;
  137. } else {
  138. data->dai_fmt = SND_SOC_DAIFMT_I2S;
  139. data->cpu_priv.slot_width = 32;
  140. }
  141. if ((hdmi_out && hdmi_in) || (!hdmi_out && !hdmi_in)) {
  142. dev_err(&pdev->dev, "Invalid HDMI DAI link\n");
  143. ret = -EINVAL;
  144. goto fail;
  145. }
  146. if (hdmi_out) {
  147. data->dai.playback_only = true;
  148. data->dai.capture_only = false;
  149. data->dai.codecs->dai_name = "i2s-hifi";
  150. data->dai.codecs->name = "hdmi-audio-codec.1";
  151. data->dai.dai_fmt = data->dai_fmt |
  152. SND_SOC_DAIFMT_NB_NF |
  153. SND_SOC_DAIFMT_CBC_CFC;
  154. }
  155. if (hdmi_in) {
  156. data->dai.playback_only = false;
  157. data->dai.capture_only = true;
  158. data->dai.codecs->dai_name = "i2s-hifi";
  159. data->dai.codecs->name = "hdmi-audio-codec.2";
  160. data->dai.dai_fmt = data->dai_fmt |
  161. SND_SOC_DAIFMT_NB_NF |
  162. SND_SOC_DAIFMT_CBP_CFP;
  163. }
  164. data->card.dapm_widgets = imx_hdmi_widgets;
  165. data->card.num_dapm_widgets = ARRAY_SIZE(imx_hdmi_widgets);
  166. data->card.dev = &pdev->dev;
  167. data->card.owner = THIS_MODULE;
  168. ret = snd_soc_of_parse_card_name(&data->card, "model");
  169. if (ret)
  170. goto fail;
  171. data->card.num_links = 1;
  172. data->card.dai_link = &data->dai;
  173. snd_soc_card_set_drvdata(&data->card, data);
  174. ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
  175. if (ret) {
  176. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
  177. goto fail;
  178. }
  179. fail:
  180. of_node_put(cpu_np);
  181. return ret;
  182. }
  183. static const struct of_device_id imx_hdmi_dt_ids[] = {
  184. { .compatible = "fsl,imx-audio-hdmi", },
  185. { .compatible = "fsl,imx-audio-sii902x", },
  186. { /* sentinel */ }
  187. };
  188. MODULE_DEVICE_TABLE(of, imx_hdmi_dt_ids);
  189. static struct platform_driver imx_hdmi_driver = {
  190. .driver = {
  191. .name = "imx-hdmi",
  192. .pm = &snd_soc_pm_ops,
  193. .of_match_table = imx_hdmi_dt_ids,
  194. },
  195. .probe = imx_hdmi_probe,
  196. };
  197. module_platform_driver(imx_hdmi_driver);
  198. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  199. MODULE_DESCRIPTION("Freescale i.MX hdmi audio ASoC machine driver");
  200. MODULE_LICENSE("GPL v2");
  201. MODULE_ALIAS("platform:imx-hdmi");