hdmi_audio.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. */
  6. #include <linux/hdmi.h>
  7. #include "hdmi.h"
  8. /* maps MSM_HDMI_AUDIO_CHANNEL_n consts used by audio driver to # of channels: */
  9. static int nchannels[] = { 2, 4, 6, 8 };
  10. /* Supported HDMI Audio sample rates */
  11. #define MSM_HDMI_SAMPLE_RATE_32KHZ 0
  12. #define MSM_HDMI_SAMPLE_RATE_44_1KHZ 1
  13. #define MSM_HDMI_SAMPLE_RATE_48KHZ 2
  14. #define MSM_HDMI_SAMPLE_RATE_88_2KHZ 3
  15. #define MSM_HDMI_SAMPLE_RATE_96KHZ 4
  16. #define MSM_HDMI_SAMPLE_RATE_176_4KHZ 5
  17. #define MSM_HDMI_SAMPLE_RATE_192KHZ 6
  18. #define MSM_HDMI_SAMPLE_RATE_MAX 7
  19. struct hdmi_msm_audio_acr {
  20. uint32_t n; /* N parameter for clock regeneration */
  21. uint32_t cts; /* CTS parameter for clock regeneration */
  22. };
  23. struct hdmi_msm_audio_arcs {
  24. unsigned long int pixclock;
  25. struct hdmi_msm_audio_acr lut[MSM_HDMI_SAMPLE_RATE_MAX];
  26. };
  27. #define HDMI_MSM_AUDIO_ARCS(pclk, ...) { (1000 * (pclk)), __VA_ARGS__ }
  28. /* Audio constants lookup table for hdmi_msm_audio_acr_setup */
  29. /* Valid Pixel-Clock rates: 25.2MHz, 27MHz, 27.03MHz, 74.25MHz, 148.5MHz */
  30. static const struct hdmi_msm_audio_arcs acr_lut[] = {
  31. /* 25.200MHz */
  32. HDMI_MSM_AUDIO_ARCS(25200, {
  33. {4096, 25200}, {6272, 28000}, {6144, 25200}, {12544, 28000},
  34. {12288, 25200}, {25088, 28000}, {24576, 25200} }),
  35. /* 27.000MHz */
  36. HDMI_MSM_AUDIO_ARCS(27000, {
  37. {4096, 27000}, {6272, 30000}, {6144, 27000}, {12544, 30000},
  38. {12288, 27000}, {25088, 30000}, {24576, 27000} }),
  39. /* 27.027MHz */
  40. HDMI_MSM_AUDIO_ARCS(27030, {
  41. {4096, 27027}, {6272, 30030}, {6144, 27027}, {12544, 30030},
  42. {12288, 27027}, {25088, 30030}, {24576, 27027} }),
  43. /* 74.250MHz */
  44. HDMI_MSM_AUDIO_ARCS(74250, {
  45. {4096, 74250}, {6272, 82500}, {6144, 74250}, {12544, 82500},
  46. {12288, 74250}, {25088, 82500}, {24576, 74250} }),
  47. /* 148.500MHz */
  48. HDMI_MSM_AUDIO_ARCS(148500, {
  49. {4096, 148500}, {6272, 165000}, {6144, 148500}, {12544, 165000},
  50. {12288, 148500}, {25088, 165000}, {24576, 148500} }),
  51. };
  52. static const struct hdmi_msm_audio_arcs *get_arcs(unsigned long int pixclock)
  53. {
  54. int i;
  55. for (i = 0; i < ARRAY_SIZE(acr_lut); i++) {
  56. const struct hdmi_msm_audio_arcs *arcs = &acr_lut[i];
  57. if (arcs->pixclock == pixclock)
  58. return arcs;
  59. }
  60. return NULL;
  61. }
  62. int msm_hdmi_audio_update(struct hdmi *hdmi)
  63. {
  64. struct hdmi_audio *audio = &hdmi->audio;
  65. struct hdmi_audio_infoframe *info = &audio->infoframe;
  66. const struct hdmi_msm_audio_arcs *arcs = NULL;
  67. bool enabled = audio->enabled;
  68. uint32_t acr_pkt_ctrl, vbi_pkt_ctrl, aud_pkt_ctrl;
  69. uint32_t infofrm_ctrl, audio_config;
  70. DBG("audio: enabled=%d, channels=%d, channel_allocation=0x%x, "
  71. "level_shift_value=%d, downmix_inhibit=%d, rate=%d",
  72. audio->enabled, info->channels, info->channel_allocation,
  73. info->level_shift_value, info->downmix_inhibit, audio->rate);
  74. DBG("video: power_on=%d, pixclock=%lu", hdmi->power_on, hdmi->pixclock);
  75. if (enabled && !(hdmi->power_on && hdmi->pixclock)) {
  76. DBG("disabling audio: no video");
  77. enabled = false;
  78. }
  79. if (enabled) {
  80. arcs = get_arcs(hdmi->pixclock);
  81. if (!arcs) {
  82. DBG("disabling audio: unsupported pixclock: %lu",
  83. hdmi->pixclock);
  84. enabled = false;
  85. }
  86. }
  87. /* Read first before writing */
  88. acr_pkt_ctrl = hdmi_read(hdmi, REG_HDMI_ACR_PKT_CTRL);
  89. vbi_pkt_ctrl = hdmi_read(hdmi, REG_HDMI_VBI_PKT_CTRL);
  90. aud_pkt_ctrl = hdmi_read(hdmi, REG_HDMI_AUDIO_PKT_CTRL1);
  91. infofrm_ctrl = hdmi_read(hdmi, REG_HDMI_INFOFRAME_CTRL0);
  92. audio_config = hdmi_read(hdmi, REG_HDMI_AUDIO_CFG);
  93. /* Clear N/CTS selection bits */
  94. acr_pkt_ctrl &= ~HDMI_ACR_PKT_CTRL_SELECT__MASK;
  95. if (enabled) {
  96. uint32_t n, cts, multiplier;
  97. enum hdmi_acr_cts select;
  98. uint8_t buf[14];
  99. n = arcs->lut[audio->rate].n;
  100. cts = arcs->lut[audio->rate].cts;
  101. if ((MSM_HDMI_SAMPLE_RATE_192KHZ == audio->rate) ||
  102. (MSM_HDMI_SAMPLE_RATE_176_4KHZ == audio->rate)) {
  103. multiplier = 4;
  104. n >>= 2; /* divide N by 4 and use multiplier */
  105. } else if ((MSM_HDMI_SAMPLE_RATE_96KHZ == audio->rate) ||
  106. (MSM_HDMI_SAMPLE_RATE_88_2KHZ == audio->rate)) {
  107. multiplier = 2;
  108. n >>= 1; /* divide N by 2 and use multiplier */
  109. } else {
  110. multiplier = 1;
  111. }
  112. DBG("n=%u, cts=%u, multiplier=%u", n, cts, multiplier);
  113. acr_pkt_ctrl |= HDMI_ACR_PKT_CTRL_SOURCE;
  114. acr_pkt_ctrl |= HDMI_ACR_PKT_CTRL_AUDIO_PRIORITY;
  115. acr_pkt_ctrl |= HDMI_ACR_PKT_CTRL_N_MULTIPLIER(multiplier);
  116. if ((MSM_HDMI_SAMPLE_RATE_48KHZ == audio->rate) ||
  117. (MSM_HDMI_SAMPLE_RATE_96KHZ == audio->rate) ||
  118. (MSM_HDMI_SAMPLE_RATE_192KHZ == audio->rate))
  119. select = ACR_48;
  120. else if ((MSM_HDMI_SAMPLE_RATE_44_1KHZ == audio->rate) ||
  121. (MSM_HDMI_SAMPLE_RATE_88_2KHZ == audio->rate) ||
  122. (MSM_HDMI_SAMPLE_RATE_176_4KHZ == audio->rate))
  123. select = ACR_44;
  124. else /* default to 32k */
  125. select = ACR_32;
  126. acr_pkt_ctrl |= HDMI_ACR_PKT_CTRL_SELECT(select);
  127. hdmi_write(hdmi, REG_HDMI_ACR_0(select - 1),
  128. HDMI_ACR_0_CTS(cts));
  129. hdmi_write(hdmi, REG_HDMI_ACR_1(select - 1),
  130. HDMI_ACR_1_N(n));
  131. hdmi_write(hdmi, REG_HDMI_AUDIO_PKT_CTRL2,
  132. COND(info->channels != 2, HDMI_AUDIO_PKT_CTRL2_LAYOUT) |
  133. HDMI_AUDIO_PKT_CTRL2_OVERRIDE);
  134. acr_pkt_ctrl |= HDMI_ACR_PKT_CTRL_CONT;
  135. acr_pkt_ctrl |= HDMI_ACR_PKT_CTRL_SEND;
  136. /* configure infoframe: */
  137. hdmi_audio_infoframe_pack(info, buf, sizeof(buf));
  138. hdmi_write(hdmi, REG_HDMI_AUDIO_INFO0,
  139. (buf[3] << 0) | (buf[4] << 8) |
  140. (buf[5] << 16) | (buf[6] << 24));
  141. hdmi_write(hdmi, REG_HDMI_AUDIO_INFO1,
  142. (buf[7] << 0) | (buf[8] << 8));
  143. hdmi_write(hdmi, REG_HDMI_GC, 0);
  144. vbi_pkt_ctrl |= HDMI_VBI_PKT_CTRL_GC_ENABLE;
  145. vbi_pkt_ctrl |= HDMI_VBI_PKT_CTRL_GC_EVERY_FRAME;
  146. aud_pkt_ctrl |= HDMI_AUDIO_PKT_CTRL1_AUDIO_SAMPLE_SEND;
  147. infofrm_ctrl |= HDMI_INFOFRAME_CTRL0_AUDIO_INFO_SEND;
  148. infofrm_ctrl |= HDMI_INFOFRAME_CTRL0_AUDIO_INFO_CONT;
  149. infofrm_ctrl |= HDMI_INFOFRAME_CTRL0_AUDIO_INFO_SOURCE;
  150. infofrm_ctrl |= HDMI_INFOFRAME_CTRL0_AUDIO_INFO_UPDATE;
  151. audio_config &= ~HDMI_AUDIO_CFG_FIFO_WATERMARK__MASK;
  152. audio_config |= HDMI_AUDIO_CFG_FIFO_WATERMARK(4);
  153. audio_config |= HDMI_AUDIO_CFG_ENGINE_ENABLE;
  154. } else {
  155. acr_pkt_ctrl &= ~HDMI_ACR_PKT_CTRL_CONT;
  156. acr_pkt_ctrl &= ~HDMI_ACR_PKT_CTRL_SEND;
  157. vbi_pkt_ctrl &= ~HDMI_VBI_PKT_CTRL_GC_ENABLE;
  158. vbi_pkt_ctrl &= ~HDMI_VBI_PKT_CTRL_GC_EVERY_FRAME;
  159. aud_pkt_ctrl &= ~HDMI_AUDIO_PKT_CTRL1_AUDIO_SAMPLE_SEND;
  160. infofrm_ctrl &= ~HDMI_INFOFRAME_CTRL0_AUDIO_INFO_SEND;
  161. infofrm_ctrl &= ~HDMI_INFOFRAME_CTRL0_AUDIO_INFO_CONT;
  162. infofrm_ctrl &= ~HDMI_INFOFRAME_CTRL0_AUDIO_INFO_SOURCE;
  163. infofrm_ctrl &= ~HDMI_INFOFRAME_CTRL0_AUDIO_INFO_UPDATE;
  164. audio_config &= ~HDMI_AUDIO_CFG_ENGINE_ENABLE;
  165. }
  166. hdmi_write(hdmi, REG_HDMI_ACR_PKT_CTRL, acr_pkt_ctrl);
  167. hdmi_write(hdmi, REG_HDMI_VBI_PKT_CTRL, vbi_pkt_ctrl);
  168. hdmi_write(hdmi, REG_HDMI_AUDIO_PKT_CTRL1, aud_pkt_ctrl);
  169. hdmi_write(hdmi, REG_HDMI_INFOFRAME_CTRL0, infofrm_ctrl);
  170. hdmi_write(hdmi, REG_HDMI_AUD_INT,
  171. COND(enabled, HDMI_AUD_INT_AUD_FIFO_URUN_INT) |
  172. COND(enabled, HDMI_AUD_INT_AUD_SAM_DROP_INT));
  173. hdmi_write(hdmi, REG_HDMI_AUDIO_CFG, audio_config);
  174. DBG("audio %sabled", enabled ? "en" : "dis");
  175. return 0;
  176. }
  177. int msm_hdmi_audio_info_setup(struct hdmi *hdmi, bool enabled,
  178. uint32_t num_of_channels, uint32_t channel_allocation,
  179. uint32_t level_shift, bool down_mix)
  180. {
  181. struct hdmi_audio *audio;
  182. if (!hdmi)
  183. return -ENXIO;
  184. audio = &hdmi->audio;
  185. if (num_of_channels >= ARRAY_SIZE(nchannels))
  186. return -EINVAL;
  187. audio->enabled = enabled;
  188. audio->infoframe.channels = nchannels[num_of_channels];
  189. audio->infoframe.channel_allocation = channel_allocation;
  190. audio->infoframe.level_shift_value = level_shift;
  191. audio->infoframe.downmix_inhibit = down_mix;
  192. return msm_hdmi_audio_update(hdmi);
  193. }
  194. void msm_hdmi_audio_set_sample_rate(struct hdmi *hdmi, int rate)
  195. {
  196. struct hdmi_audio *audio;
  197. if (!hdmi)
  198. return;
  199. audio = &hdmi->audio;
  200. if ((rate < 0) || (rate >= MSM_HDMI_SAMPLE_RATE_MAX))
  201. return;
  202. audio->rate = rate;
  203. msm_hdmi_audio_update(hdmi);
  204. }