hdmi_audio.c 8.3 KB

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