dw-hdmi-i2s-audio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * dw-hdmi-i2s-audio.c
  3. *
  4. * Copyright (c) 2017 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <drm/bridge/dw_hdmi.h>
  12. #include <sound/hdmi-codec.h>
  13. #include "dw-hdmi.h"
  14. #include "dw-hdmi-audio.h"
  15. #define DRIVER_NAME "dw-hdmi-i2s-audio"
  16. static inline void hdmi_write(struct dw_hdmi_i2s_audio_data *audio,
  17. u8 val, int offset)
  18. {
  19. struct dw_hdmi *hdmi = audio->hdmi;
  20. audio->write(hdmi, val, offset);
  21. }
  22. static inline u8 hdmi_read(struct dw_hdmi_i2s_audio_data *audio, int offset)
  23. {
  24. struct dw_hdmi *hdmi = audio->hdmi;
  25. return audio->read(hdmi, offset);
  26. }
  27. static int dw_hdmi_i2s_hw_params(struct device *dev, void *data,
  28. struct hdmi_codec_daifmt *fmt,
  29. struct hdmi_codec_params *hparms)
  30. {
  31. struct dw_hdmi_i2s_audio_data *audio = data;
  32. struct dw_hdmi *hdmi = audio->hdmi;
  33. u8 conf0 = 0;
  34. u8 conf1 = 0;
  35. u8 inputclkfs = 0;
  36. /* it cares I2S only */
  37. if ((fmt->fmt != HDMI_I2S) ||
  38. (fmt->bit_clk_master | fmt->frame_clk_master)) {
  39. dev_err(dev, "unsupported format/settings\n");
  40. return -EINVAL;
  41. }
  42. inputclkfs = HDMI_AUD_INPUTCLKFS_64FS;
  43. conf0 = HDMI_AUD_CONF0_I2S_ALL_ENABLE;
  44. switch (hparms->sample_width) {
  45. case 16:
  46. conf1 = HDMI_AUD_CONF1_WIDTH_16;
  47. break;
  48. case 24:
  49. case 32:
  50. conf1 = HDMI_AUD_CONF1_WIDTH_24;
  51. break;
  52. }
  53. dw_hdmi_set_sample_rate(hdmi, hparms->sample_rate);
  54. hdmi_write(audio, inputclkfs, HDMI_AUD_INPUTCLKFS);
  55. hdmi_write(audio, conf0, HDMI_AUD_CONF0);
  56. hdmi_write(audio, conf1, HDMI_AUD_CONF1);
  57. dw_hdmi_audio_enable(hdmi);
  58. return 0;
  59. }
  60. static void dw_hdmi_i2s_audio_shutdown(struct device *dev, void *data)
  61. {
  62. struct dw_hdmi_i2s_audio_data *audio = data;
  63. struct dw_hdmi *hdmi = audio->hdmi;
  64. dw_hdmi_audio_disable(hdmi);
  65. hdmi_write(audio, HDMI_AUD_CONF0_SW_RESET, HDMI_AUD_CONF0);
  66. }
  67. static int dw_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
  68. struct device_node *endpoint)
  69. {
  70. struct of_endpoint of_ep;
  71. int ret;
  72. ret = of_graph_parse_endpoint(endpoint, &of_ep);
  73. if (ret < 0)
  74. return ret;
  75. /*
  76. * HDMI sound should be located as reg = <2>
  77. * Then, it is sound port 0
  78. */
  79. if (of_ep.port == 2)
  80. return 0;
  81. return -EINVAL;
  82. }
  83. static struct hdmi_codec_ops dw_hdmi_i2s_ops = {
  84. .hw_params = dw_hdmi_i2s_hw_params,
  85. .audio_shutdown = dw_hdmi_i2s_audio_shutdown,
  86. .get_dai_id = dw_hdmi_i2s_get_dai_id,
  87. };
  88. static int snd_dw_hdmi_probe(struct platform_device *pdev)
  89. {
  90. struct dw_hdmi_i2s_audio_data *audio = pdev->dev.platform_data;
  91. struct platform_device_info pdevinfo;
  92. struct hdmi_codec_pdata pdata;
  93. struct platform_device *platform;
  94. pdata.ops = &dw_hdmi_i2s_ops;
  95. pdata.i2s = 1;
  96. pdata.max_i2s_channels = 6;
  97. pdata.data = audio;
  98. memset(&pdevinfo, 0, sizeof(pdevinfo));
  99. pdevinfo.parent = pdev->dev.parent;
  100. pdevinfo.id = PLATFORM_DEVID_AUTO;
  101. pdevinfo.name = HDMI_CODEC_DRV_NAME;
  102. pdevinfo.data = &pdata;
  103. pdevinfo.size_data = sizeof(pdata);
  104. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  105. platform = platform_device_register_full(&pdevinfo);
  106. if (IS_ERR(platform))
  107. return PTR_ERR(platform);
  108. dev_set_drvdata(&pdev->dev, platform);
  109. return 0;
  110. }
  111. static int snd_dw_hdmi_remove(struct platform_device *pdev)
  112. {
  113. struct platform_device *platform = dev_get_drvdata(&pdev->dev);
  114. platform_device_unregister(platform);
  115. return 0;
  116. }
  117. static struct platform_driver snd_dw_hdmi_driver = {
  118. .probe = snd_dw_hdmi_probe,
  119. .remove = snd_dw_hdmi_remove,
  120. .driver = {
  121. .name = DRIVER_NAME,
  122. .owner = THIS_MODULE,
  123. },
  124. };
  125. module_platform_driver(snd_dw_hdmi_driver);
  126. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  127. MODULE_DESCRIPTION("Synopsis Designware HDMI I2S ALSA SoC interface");
  128. MODULE_LICENSE("GPL v2");
  129. MODULE_ALIAS("platform:" DRIVER_NAME);