1 1.1 riastrad /* $NetBSD: radeon_dce6_afmt.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2013 Advanced Micro Devices, Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad */ 25 1.1 riastrad #include <sys/cdefs.h> 26 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_dce6_afmt.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $"); 27 1.1 riastrad 28 1.1 riastrad #include <linux/hdmi.h> 29 1.2 riastrad 30 1.1 riastrad #include "radeon.h" 31 1.1 riastrad #include "radeon_audio.h" 32 1.1 riastrad #include "sid.h" 33 1.1 riastrad 34 1.1 riastrad #define DCE8_DCCG_AUDIO_DTO1_PHASE 0x05b8 35 1.1 riastrad #define DCE8_DCCG_AUDIO_DTO1_MODULE 0x05bc 36 1.1 riastrad 37 1.1 riastrad u32 dce6_endpoint_rreg(struct radeon_device *rdev, 38 1.1 riastrad u32 block_offset, u32 reg) 39 1.1 riastrad { 40 1.1 riastrad unsigned long flags; 41 1.1 riastrad u32 r; 42 1.1 riastrad 43 1.1 riastrad spin_lock_irqsave(&rdev->end_idx_lock, flags); 44 1.1 riastrad WREG32(AZ_F0_CODEC_ENDPOINT_INDEX + block_offset, reg); 45 1.1 riastrad r = RREG32(AZ_F0_CODEC_ENDPOINT_DATA + block_offset); 46 1.1 riastrad spin_unlock_irqrestore(&rdev->end_idx_lock, flags); 47 1.1 riastrad 48 1.1 riastrad return r; 49 1.1 riastrad } 50 1.1 riastrad 51 1.1 riastrad void dce6_endpoint_wreg(struct radeon_device *rdev, 52 1.1 riastrad u32 block_offset, u32 reg, u32 v) 53 1.1 riastrad { 54 1.1 riastrad unsigned long flags; 55 1.1 riastrad 56 1.1 riastrad spin_lock_irqsave(&rdev->end_idx_lock, flags); 57 1.1 riastrad if (ASIC_IS_DCE8(rdev)) 58 1.1 riastrad WREG32(AZ_F0_CODEC_ENDPOINT_INDEX + block_offset, reg); 59 1.1 riastrad else 60 1.1 riastrad WREG32(AZ_F0_CODEC_ENDPOINT_INDEX + block_offset, 61 1.1 riastrad AZ_ENDPOINT_REG_WRITE_EN | AZ_ENDPOINT_REG_INDEX(reg)); 62 1.1 riastrad WREG32(AZ_F0_CODEC_ENDPOINT_DATA + block_offset, v); 63 1.1 riastrad spin_unlock_irqrestore(&rdev->end_idx_lock, flags); 64 1.1 riastrad } 65 1.1 riastrad 66 1.1 riastrad static void dce6_afmt_get_connected_pins(struct radeon_device *rdev) 67 1.1 riastrad { 68 1.1 riastrad int i; 69 1.1 riastrad u32 offset, tmp; 70 1.1 riastrad 71 1.1 riastrad for (i = 0; i < rdev->audio.num_pins; i++) { 72 1.1 riastrad offset = rdev->audio.pin[i].offset; 73 1.1 riastrad tmp = RREG32_ENDPOINT(offset, 74 1.1 riastrad AZ_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT); 75 1.1 riastrad if (((tmp & PORT_CONNECTIVITY_MASK) >> PORT_CONNECTIVITY_SHIFT) == 1) 76 1.1 riastrad rdev->audio.pin[i].connected = false; 77 1.1 riastrad else 78 1.1 riastrad rdev->audio.pin[i].connected = true; 79 1.1 riastrad } 80 1.1 riastrad } 81 1.1 riastrad 82 1.1 riastrad struct r600_audio_pin *dce6_audio_get_pin(struct radeon_device *rdev) 83 1.1 riastrad { 84 1.1 riastrad struct drm_encoder *encoder; 85 1.1 riastrad struct radeon_encoder *radeon_encoder; 86 1.1 riastrad struct radeon_encoder_atom_dig *dig; 87 1.1 riastrad struct r600_audio_pin *pin = NULL; 88 1.1 riastrad int i, pin_count; 89 1.1 riastrad 90 1.1 riastrad dce6_afmt_get_connected_pins(rdev); 91 1.1 riastrad 92 1.1 riastrad for (i = 0; i < rdev->audio.num_pins; i++) { 93 1.1 riastrad if (rdev->audio.pin[i].connected) { 94 1.1 riastrad pin = &rdev->audio.pin[i]; 95 1.1 riastrad pin_count = 0; 96 1.1 riastrad 97 1.1 riastrad list_for_each_entry(encoder, &rdev->ddev->mode_config.encoder_list, head) { 98 1.1 riastrad if (radeon_encoder_is_digital(encoder)) { 99 1.1 riastrad radeon_encoder = to_radeon_encoder(encoder); 100 1.1 riastrad dig = radeon_encoder->enc_priv; 101 1.1 riastrad if (dig->pin == pin) 102 1.1 riastrad pin_count++; 103 1.1 riastrad } 104 1.1 riastrad } 105 1.1 riastrad 106 1.1 riastrad if (pin_count == 0) 107 1.1 riastrad return pin; 108 1.1 riastrad } 109 1.1 riastrad } 110 1.1 riastrad if (!pin) 111 1.1 riastrad DRM_ERROR("No connected audio pins found!\n"); 112 1.1 riastrad return pin; 113 1.1 riastrad } 114 1.1 riastrad 115 1.1 riastrad void dce6_afmt_select_pin(struct drm_encoder *encoder) 116 1.1 riastrad { 117 1.1 riastrad struct radeon_device *rdev = encoder->dev->dev_private; 118 1.1 riastrad struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); 119 1.1 riastrad struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv; 120 1.1 riastrad 121 1.1 riastrad if (!dig || !dig->afmt || !dig->pin) 122 1.1 riastrad return; 123 1.1 riastrad 124 1.1 riastrad WREG32(AFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, 125 1.1 riastrad AFMT_AUDIO_SRC_SELECT(dig->pin->id)); 126 1.1 riastrad } 127 1.1 riastrad 128 1.1 riastrad void dce6_afmt_write_latency_fields(struct drm_encoder *encoder, 129 1.1 riastrad struct drm_connector *connector, 130 1.1 riastrad struct drm_display_mode *mode) 131 1.1 riastrad { 132 1.1 riastrad struct radeon_device *rdev = encoder->dev->dev_private; 133 1.1 riastrad struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); 134 1.1 riastrad struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv; 135 1.1 riastrad u32 tmp = 0; 136 1.1 riastrad 137 1.1 riastrad if (!dig || !dig->afmt || !dig->pin) 138 1.1 riastrad return; 139 1.1 riastrad 140 1.1 riastrad if (mode->flags & DRM_MODE_FLAG_INTERLACE) { 141 1.1 riastrad if (connector->latency_present[1]) 142 1.1 riastrad tmp = VIDEO_LIPSYNC(connector->video_latency[1]) | 143 1.1 riastrad AUDIO_LIPSYNC(connector->audio_latency[1]); 144 1.1 riastrad else 145 1.1 riastrad tmp = VIDEO_LIPSYNC(0) | AUDIO_LIPSYNC(0); 146 1.1 riastrad } else { 147 1.1 riastrad if (connector->latency_present[0]) 148 1.1 riastrad tmp = VIDEO_LIPSYNC(connector->video_latency[0]) | 149 1.1 riastrad AUDIO_LIPSYNC(connector->audio_latency[0]); 150 1.1 riastrad else 151 1.1 riastrad tmp = VIDEO_LIPSYNC(0) | AUDIO_LIPSYNC(0); 152 1.1 riastrad } 153 1.1 riastrad WREG32_ENDPOINT(dig->pin->offset, 154 1.1 riastrad AZ_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp); 155 1.1 riastrad } 156 1.1 riastrad 157 1.1 riastrad void dce6_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder, 158 1.1 riastrad u8 *sadb, int sad_count) 159 1.1 riastrad { 160 1.1 riastrad struct radeon_device *rdev = encoder->dev->dev_private; 161 1.1 riastrad struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); 162 1.1 riastrad struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv; 163 1.1 riastrad u32 tmp; 164 1.1 riastrad 165 1.1 riastrad if (!dig || !dig->afmt || !dig->pin) 166 1.1 riastrad return; 167 1.1 riastrad 168 1.1 riastrad /* program the speaker allocation */ 169 1.1 riastrad tmp = RREG32_ENDPOINT(dig->pin->offset, 170 1.1 riastrad AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER); 171 1.1 riastrad tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK); 172 1.1 riastrad /* set HDMI mode */ 173 1.1 riastrad tmp |= HDMI_CONNECTION; 174 1.1 riastrad if (sad_count) 175 1.1 riastrad tmp |= SPEAKER_ALLOCATION(sadb[0]); 176 1.1 riastrad else 177 1.1 riastrad tmp |= SPEAKER_ALLOCATION(5); /* stereo */ 178 1.1 riastrad WREG32_ENDPOINT(dig->pin->offset, 179 1.1 riastrad AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp); 180 1.1 riastrad } 181 1.1 riastrad 182 1.1 riastrad void dce6_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder, 183 1.1 riastrad u8 *sadb, int sad_count) 184 1.1 riastrad { 185 1.1 riastrad struct radeon_device *rdev = encoder->dev->dev_private; 186 1.1 riastrad struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); 187 1.1 riastrad struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv; 188 1.1 riastrad u32 tmp; 189 1.1 riastrad 190 1.1 riastrad if (!dig || !dig->afmt || !dig->pin) 191 1.1 riastrad return; 192 1.1 riastrad 193 1.1 riastrad /* program the speaker allocation */ 194 1.1 riastrad tmp = RREG32_ENDPOINT(dig->pin->offset, 195 1.1 riastrad AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER); 196 1.1 riastrad tmp &= ~(HDMI_CONNECTION | SPEAKER_ALLOCATION_MASK); 197 1.1 riastrad /* set DP mode */ 198 1.1 riastrad tmp |= DP_CONNECTION; 199 1.1 riastrad if (sad_count) 200 1.1 riastrad tmp |= SPEAKER_ALLOCATION(sadb[0]); 201 1.1 riastrad else 202 1.1 riastrad tmp |= SPEAKER_ALLOCATION(5); /* stereo */ 203 1.1 riastrad WREG32_ENDPOINT(dig->pin->offset, 204 1.1 riastrad AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp); 205 1.1 riastrad } 206 1.1 riastrad 207 1.1 riastrad void dce6_afmt_write_sad_regs(struct drm_encoder *encoder, 208 1.1 riastrad struct cea_sad *sads, int sad_count) 209 1.1 riastrad { 210 1.1 riastrad int i; 211 1.1 riastrad struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); 212 1.1 riastrad struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv; 213 1.1 riastrad struct radeon_device *rdev = encoder->dev->dev_private; 214 1.1 riastrad static const u16 eld_reg_to_type[][2] = { 215 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM }, 216 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 }, 217 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 }, 218 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 }, 219 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 }, 220 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC }, 221 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS }, 222 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC }, 223 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 }, 224 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD }, 225 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP }, 226 1.1 riastrad { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO }, 227 1.1 riastrad }; 228 1.1 riastrad 229 1.1 riastrad if (!dig || !dig->afmt || !dig->pin) 230 1.1 riastrad return; 231 1.1 riastrad 232 1.1 riastrad for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) { 233 1.1 riastrad u32 value = 0; 234 1.1 riastrad u8 stereo_freqs = 0; 235 1.1 riastrad int max_channels = -1; 236 1.1 riastrad int j; 237 1.1 riastrad 238 1.1 riastrad for (j = 0; j < sad_count; j++) { 239 1.1 riastrad struct cea_sad *sad = &sads[j]; 240 1.1 riastrad 241 1.1 riastrad if (sad->format == eld_reg_to_type[i][1]) { 242 1.1 riastrad if (sad->channels > max_channels) { 243 1.1 riastrad value = MAX_CHANNELS(sad->channels) | 244 1.1 riastrad DESCRIPTOR_BYTE_2(sad->byte2) | 245 1.1 riastrad SUPPORTED_FREQUENCIES(sad->freq); 246 1.1 riastrad max_channels = sad->channels; 247 1.1 riastrad } 248 1.1 riastrad 249 1.1 riastrad if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM) 250 1.1 riastrad stereo_freqs |= sad->freq; 251 1.1 riastrad else 252 1.1 riastrad break; 253 1.1 riastrad } 254 1.1 riastrad } 255 1.1 riastrad 256 1.1 riastrad value |= SUPPORTED_FREQUENCIES_STEREO(stereo_freqs); 257 1.1 riastrad 258 1.1 riastrad WREG32_ENDPOINT(dig->pin->offset, eld_reg_to_type[i][0], value); 259 1.1 riastrad } 260 1.1 riastrad } 261 1.1 riastrad 262 1.1 riastrad void dce6_audio_enable(struct radeon_device *rdev, 263 1.1 riastrad struct r600_audio_pin *pin, 264 1.1 riastrad u8 enable_mask) 265 1.1 riastrad { 266 1.1 riastrad if (!pin) 267 1.1 riastrad return; 268 1.1 riastrad 269 1.1 riastrad WREG32_ENDPOINT(pin->offset, AZ_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL, 270 1.1 riastrad enable_mask ? AUDIO_ENABLED : 0); 271 1.1 riastrad } 272 1.1 riastrad 273 1.1 riastrad void dce6_hdmi_audio_set_dto(struct radeon_device *rdev, 274 1.1 riastrad struct radeon_crtc *crtc, unsigned int clock) 275 1.1 riastrad { 276 1.1 riastrad /* Two dtos; generally use dto0 for HDMI */ 277 1.1 riastrad u32 value = 0; 278 1.1 riastrad 279 1.1 riastrad if (crtc) 280 1.1 riastrad value |= DCCG_AUDIO_DTO0_SOURCE_SEL(crtc->crtc_id); 281 1.1 riastrad 282 1.1 riastrad WREG32(DCCG_AUDIO_DTO_SOURCE, value); 283 1.1 riastrad 284 1.1 riastrad /* Express [24MHz / target pixel clock] as an exact rational 285 1.1 riastrad * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE 286 1.1 riastrad * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator 287 1.1 riastrad */ 288 1.1 riastrad WREG32(DCCG_AUDIO_DTO0_PHASE, 24000); 289 1.1 riastrad WREG32(DCCG_AUDIO_DTO0_MODULE, clock); 290 1.1 riastrad } 291 1.1 riastrad 292 1.1 riastrad void dce6_dp_audio_set_dto(struct radeon_device *rdev, 293 1.1 riastrad struct radeon_crtc *crtc, unsigned int clock) 294 1.1 riastrad { 295 1.1 riastrad /* Two dtos; generally use dto1 for DP */ 296 1.1 riastrad u32 value = 0; 297 1.1 riastrad value |= DCCG_AUDIO_DTO_SEL; 298 1.1 riastrad 299 1.1 riastrad if (crtc) 300 1.1 riastrad value |= DCCG_AUDIO_DTO0_SOURCE_SEL(crtc->crtc_id); 301 1.1 riastrad 302 1.1 riastrad WREG32(DCCG_AUDIO_DTO_SOURCE, value); 303 1.1 riastrad 304 1.1 riastrad /* Express [24MHz / target pixel clock] as an exact rational 305 1.1 riastrad * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE 306 1.1 riastrad * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator 307 1.1 riastrad */ 308 1.1 riastrad if (ASIC_IS_DCE8(rdev)) { 309 1.1 riastrad unsigned int div = (RREG32(DENTIST_DISPCLK_CNTL) & 310 1.1 riastrad DENTIST_DPREFCLK_WDIVIDER_MASK) >> 311 1.1 riastrad DENTIST_DPREFCLK_WDIVIDER_SHIFT; 312 1.1 riastrad div = radeon_audio_decode_dfs_div(div); 313 1.1 riastrad 314 1.1 riastrad if (div) 315 1.1 riastrad clock = clock * 100 / div; 316 1.1 riastrad 317 1.1 riastrad WREG32(DCE8_DCCG_AUDIO_DTO1_PHASE, 24000); 318 1.1 riastrad WREG32(DCE8_DCCG_AUDIO_DTO1_MODULE, clock); 319 1.1 riastrad } else { 320 1.1 riastrad WREG32(DCCG_AUDIO_DTO1_PHASE, 24000); 321 1.1 riastrad WREG32(DCCG_AUDIO_DTO1_MODULE, clock); 322 1.1 riastrad } 323 1.1 riastrad } 324