dw-hdmi-i2s-audio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dw-hdmi-i2s-audio.c
  4. *
  5. * Copyright (c) 2017 Renesas Solutions Corp.
  6. * Kuninori Morimoto <[email protected]>
  7. */
  8. #include <linux/dma-mapping.h>
  9. #include <linux/module.h>
  10. #include <drm/bridge/dw_hdmi.h>
  11. #include <drm/drm_crtc.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->bit_clk_provider | fmt->frame_clk_provider) {
  38. dev_err(dev, "unsupported clock settings\n");
  39. return -EINVAL;
  40. }
  41. /* Reset the FIFOs before applying new params */
  42. hdmi_write(audio, HDMI_AUD_CONF0_SW_RESET, HDMI_AUD_CONF0);
  43. hdmi_write(audio, (u8)~HDMI_MC_SWRSTZ_I2SSWRST_REQ, HDMI_MC_SWRSTZ);
  44. inputclkfs = HDMI_AUD_INPUTCLKFS_64FS;
  45. conf0 = (HDMI_AUD_CONF0_I2S_SELECT | HDMI_AUD_CONF0_I2S_EN0);
  46. /* Enable the required i2s lanes */
  47. switch (hparms->channels) {
  48. case 7 ... 8:
  49. conf0 |= HDMI_AUD_CONF0_I2S_EN3;
  50. fallthrough;
  51. case 5 ... 6:
  52. conf0 |= HDMI_AUD_CONF0_I2S_EN2;
  53. fallthrough;
  54. case 3 ... 4:
  55. conf0 |= HDMI_AUD_CONF0_I2S_EN1;
  56. /* Fall-thru */
  57. }
  58. switch (hparms->sample_width) {
  59. case 16:
  60. conf1 = HDMI_AUD_CONF1_WIDTH_16;
  61. break;
  62. case 24:
  63. case 32:
  64. conf1 = HDMI_AUD_CONF1_WIDTH_24;
  65. break;
  66. }
  67. switch (fmt->fmt) {
  68. case HDMI_I2S:
  69. conf1 |= HDMI_AUD_CONF1_MODE_I2S;
  70. break;
  71. case HDMI_RIGHT_J:
  72. conf1 |= HDMI_AUD_CONF1_MODE_RIGHT_J;
  73. break;
  74. case HDMI_LEFT_J:
  75. conf1 |= HDMI_AUD_CONF1_MODE_LEFT_J;
  76. break;
  77. case HDMI_DSP_A:
  78. conf1 |= HDMI_AUD_CONF1_MODE_BURST_1;
  79. break;
  80. case HDMI_DSP_B:
  81. conf1 |= HDMI_AUD_CONF1_MODE_BURST_2;
  82. break;
  83. default:
  84. dev_err(dev, "unsupported format\n");
  85. return -EINVAL;
  86. }
  87. dw_hdmi_set_sample_rate(hdmi, hparms->sample_rate);
  88. dw_hdmi_set_channel_status(hdmi, hparms->iec.status);
  89. dw_hdmi_set_channel_count(hdmi, hparms->channels);
  90. dw_hdmi_set_channel_allocation(hdmi, hparms->cea.channel_allocation);
  91. hdmi_write(audio, inputclkfs, HDMI_AUD_INPUTCLKFS);
  92. hdmi_write(audio, conf0, HDMI_AUD_CONF0);
  93. hdmi_write(audio, conf1, HDMI_AUD_CONF1);
  94. return 0;
  95. }
  96. static int dw_hdmi_i2s_audio_startup(struct device *dev, void *data)
  97. {
  98. struct dw_hdmi_i2s_audio_data *audio = data;
  99. struct dw_hdmi *hdmi = audio->hdmi;
  100. dw_hdmi_audio_enable(hdmi);
  101. return 0;
  102. }
  103. static void dw_hdmi_i2s_audio_shutdown(struct device *dev, void *data)
  104. {
  105. struct dw_hdmi_i2s_audio_data *audio = data;
  106. struct dw_hdmi *hdmi = audio->hdmi;
  107. dw_hdmi_audio_disable(hdmi);
  108. }
  109. static int dw_hdmi_i2s_get_eld(struct device *dev, void *data, uint8_t *buf,
  110. size_t len)
  111. {
  112. struct dw_hdmi_i2s_audio_data *audio = data;
  113. u8 *eld;
  114. eld = audio->get_eld(audio->hdmi);
  115. if (eld)
  116. memcpy(buf, eld, min_t(size_t, MAX_ELD_BYTES, len));
  117. else
  118. /* Pass en empty ELD if connector not available */
  119. memset(buf, 0, len);
  120. return 0;
  121. }
  122. static int dw_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
  123. struct device_node *endpoint)
  124. {
  125. struct of_endpoint of_ep;
  126. int ret;
  127. ret = of_graph_parse_endpoint(endpoint, &of_ep);
  128. if (ret < 0)
  129. return ret;
  130. /*
  131. * HDMI sound should be located as reg = <2>
  132. * Then, it is sound port 0
  133. */
  134. if (of_ep.port == 2)
  135. return 0;
  136. return -EINVAL;
  137. }
  138. static int dw_hdmi_i2s_hook_plugged_cb(struct device *dev, void *data,
  139. hdmi_codec_plugged_cb fn,
  140. struct device *codec_dev)
  141. {
  142. struct dw_hdmi_i2s_audio_data *audio = data;
  143. struct dw_hdmi *hdmi = audio->hdmi;
  144. return dw_hdmi_set_plugged_cb(hdmi, fn, codec_dev);
  145. }
  146. static const struct hdmi_codec_ops dw_hdmi_i2s_ops = {
  147. .hw_params = dw_hdmi_i2s_hw_params,
  148. .audio_startup = dw_hdmi_i2s_audio_startup,
  149. .audio_shutdown = dw_hdmi_i2s_audio_shutdown,
  150. .get_eld = dw_hdmi_i2s_get_eld,
  151. .get_dai_id = dw_hdmi_i2s_get_dai_id,
  152. .hook_plugged_cb = dw_hdmi_i2s_hook_plugged_cb,
  153. };
  154. static int snd_dw_hdmi_probe(struct platform_device *pdev)
  155. {
  156. struct dw_hdmi_i2s_audio_data *audio = pdev->dev.platform_data;
  157. struct platform_device_info pdevinfo;
  158. struct hdmi_codec_pdata pdata;
  159. struct platform_device *platform;
  160. pdata.ops = &dw_hdmi_i2s_ops;
  161. pdata.i2s = 1;
  162. pdata.max_i2s_channels = 8;
  163. pdata.data = audio;
  164. memset(&pdevinfo, 0, sizeof(pdevinfo));
  165. pdevinfo.parent = pdev->dev.parent;
  166. pdevinfo.id = PLATFORM_DEVID_AUTO;
  167. pdevinfo.name = HDMI_CODEC_DRV_NAME;
  168. pdevinfo.data = &pdata;
  169. pdevinfo.size_data = sizeof(pdata);
  170. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  171. platform = platform_device_register_full(&pdevinfo);
  172. if (IS_ERR(platform))
  173. return PTR_ERR(platform);
  174. dev_set_drvdata(&pdev->dev, platform);
  175. return 0;
  176. }
  177. static int snd_dw_hdmi_remove(struct platform_device *pdev)
  178. {
  179. struct platform_device *platform = dev_get_drvdata(&pdev->dev);
  180. platform_device_unregister(platform);
  181. return 0;
  182. }
  183. static struct platform_driver snd_dw_hdmi_driver = {
  184. .probe = snd_dw_hdmi_probe,
  185. .remove = snd_dw_hdmi_remove,
  186. .driver = {
  187. .name = DRIVER_NAME,
  188. },
  189. };
  190. module_platform_driver(snd_dw_hdmi_driver);
  191. MODULE_AUTHOR("Kuninori Morimoto <[email protected]>");
  192. MODULE_DESCRIPTION("Synopsis Designware HDMI I2S ALSA SoC interface");
  193. MODULE_LICENSE("GPL v2");
  194. MODULE_ALIAS("platform:" DRIVER_NAME);