Slava Grigorev | bfc1f97 | 2014-12-22 17:26:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Advanced Micro Devices, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | * |
| 22 | * Authors: Slava Grigorev <slava.grigorev@amd.com> |
| 23 | */ |
| 24 | |
Slava Grigorev | 64424d6e | 2014-12-06 20:19:16 -0500 | [diff] [blame] | 25 | #include <linux/gcd.h> |
Slava Grigorev | bfc1f97 | 2014-12-22 17:26:51 -0500 | [diff] [blame] | 26 | #include <drm/drmP.h> |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 27 | #include <drm/drm_crtc.h> |
Slava Grigorev | bfc1f97 | 2014-12-22 17:26:51 -0500 | [diff] [blame] | 28 | #include "radeon.h" |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 29 | #include "atom.h" |
| 30 | #include "radeon_audio.h" |
Slava Grigorev | bfc1f97 | 2014-12-22 17:26:51 -0500 | [diff] [blame] | 31 | |
| 32 | void r600_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin, |
| 33 | u8 enable_mask); |
Slava Grigorev | 8bf5982 | 2014-12-03 15:29:53 -0500 | [diff] [blame] | 34 | void dce4_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin, |
| 35 | u8 enable_mask); |
Slava Grigorev | bfc1f97 | 2014-12-22 17:26:51 -0500 | [diff] [blame] | 36 | void dce6_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin, |
| 37 | u8 enable_mask); |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 38 | u32 dce6_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg); |
| 39 | void dce6_endpoint_wreg(struct radeon_device *rdev, |
| 40 | u32 offset, u32 reg, u32 v); |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 41 | void dce3_2_afmt_write_sad_regs(struct drm_encoder *encoder, |
| 42 | struct cea_sad *sads, int sad_count); |
| 43 | void evergreen_hdmi_write_sad_regs(struct drm_encoder *encoder, |
| 44 | struct cea_sad *sads, int sad_count); |
| 45 | void dce6_afmt_write_sad_regs(struct drm_encoder *encoder, |
| 46 | struct cea_sad *sads, int sad_count); |
Slava Grigorev | 00a9d4b | 2014-12-01 18:02:57 -0500 | [diff] [blame] | 47 | void dce3_2_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder, |
| 48 | u8 *sadb, int sad_count); |
| 49 | void dce3_2_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder, |
| 50 | u8 *sadb, int sad_count); |
| 51 | void dce4_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder, |
| 52 | u8 *sadb, int sad_count); |
| 53 | void dce4_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder, |
| 54 | u8 *sadb, int sad_count); |
| 55 | void dce6_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder, |
| 56 | u8 *sadb, int sad_count); |
| 57 | void dce6_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder, |
| 58 | u8 *sadb, int sad_count); |
Slava Grigorev | 87654f8 | 2014-12-02 11:20:48 -0500 | [diff] [blame] | 59 | void dce4_afmt_write_latency_fields(struct drm_encoder *encoder, |
| 60 | struct drm_connector *connector, struct drm_display_mode *mode); |
| 61 | void dce6_afmt_write_latency_fields(struct drm_encoder *encoder, |
| 62 | struct drm_connector *connector, struct drm_display_mode *mode); |
Slava Grigorev | 3cdde02 | 2014-12-02 15:22:43 -0500 | [diff] [blame] | 63 | struct r600_audio_pin* r600_audio_get_pin(struct radeon_device *rdev); |
| 64 | struct r600_audio_pin* dce6_audio_get_pin(struct radeon_device *rdev); |
Slava Grigorev | 88252d7 | 2014-12-02 17:27:29 -0500 | [diff] [blame] | 65 | void dce6_afmt_select_pin(struct drm_encoder *encoder); |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 66 | void r600_hdmi_audio_set_dto(struct radeon_device *rdev, |
| 67 | struct radeon_crtc *crtc, unsigned int clock); |
| 68 | void dce3_2_audio_set_dto(struct radeon_device *rdev, |
| 69 | struct radeon_crtc *crtc, unsigned int clock); |
| 70 | void dce4_hdmi_audio_set_dto(struct radeon_device *rdev, |
| 71 | struct radeon_crtc *crtc, unsigned int clock); |
| 72 | void dce4_dp_audio_set_dto(struct radeon_device *rdev, |
| 73 | struct radeon_crtc *crtc, unsigned int clock); |
| 74 | void dce6_hdmi_audio_set_dto(struct radeon_device *rdev, |
| 75 | struct radeon_crtc *crtc, unsigned int clock); |
| 76 | void dce6_dp_audio_set_dto(struct radeon_device *rdev, |
| 77 | struct radeon_crtc *crtc, unsigned int clock); |
Slava Grigorev | 96ea7af | 2014-12-05 17:59:56 -0500 | [diff] [blame] | 78 | void r600_update_avi_infoframe(struct radeon_device *rdev, u32 offset, |
| 79 | unsigned char *buffer, size_t size); |
| 80 | void evergreen_update_avi_infoframe(struct radeon_device *rdev, u32 offset, |
| 81 | unsigned char *buffer, size_t size); |
Slava Grigorev | 64424d6e | 2014-12-06 20:19:16 -0500 | [diff] [blame] | 82 | void r600_hdmi_update_acr(struct drm_encoder *encoder, long offset, |
| 83 | const struct radeon_hdmi_acr *acr); |
| 84 | void dce3_2_hdmi_update_acr(struct drm_encoder *encoder, long offset, |
| 85 | const struct radeon_hdmi_acr *acr); |
| 86 | void evergreen_hdmi_update_acr(struct drm_encoder *encoder, long offset, |
| 87 | const struct radeon_hdmi_acr *acr); |
Alex Deucher | 930a978 | 2015-01-20 19:20:52 -0500 | [diff] [blame^] | 88 | void r600_set_vbi_packet(struct drm_encoder *encoder, u32 offset); |
| 89 | void dce4_set_vbi_packet(struct drm_encoder *encoder, u32 offset); |
Slava Grigorev | bfc1f97 | 2014-12-22 17:26:51 -0500 | [diff] [blame] | 90 | |
| 91 | static const u32 pin_offsets[7] = |
| 92 | { |
| 93 | (0x5e00 - 0x5e00), |
| 94 | (0x5e18 - 0x5e00), |
| 95 | (0x5e30 - 0x5e00), |
| 96 | (0x5e48 - 0x5e00), |
| 97 | (0x5e60 - 0x5e00), |
| 98 | (0x5e78 - 0x5e00), |
| 99 | (0x5e90 - 0x5e00), |
| 100 | }; |
| 101 | |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 102 | static u32 radeon_audio_rreg(struct radeon_device *rdev, u32 offset, u32 reg) |
| 103 | { |
| 104 | return RREG32(reg); |
| 105 | } |
| 106 | |
| 107 | static void radeon_audio_wreg(struct radeon_device *rdev, u32 offset, |
| 108 | u32 reg, u32 v) |
| 109 | { |
| 110 | WREG32(reg, v); |
| 111 | } |
| 112 | |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 113 | static struct radeon_audio_basic_funcs r600_funcs = { |
| 114 | .endpoint_rreg = radeon_audio_rreg, |
| 115 | .endpoint_wreg = radeon_audio_wreg, |
| 116 | .enable = r600_audio_enable, |
Slava Grigorev | 96ea7af | 2014-12-05 17:59:56 -0500 | [diff] [blame] | 117 | .update_avi_infoframe = r600_update_avi_infoframe, |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 118 | }; |
| 119 | |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 120 | static struct radeon_audio_basic_funcs dce32_funcs = { |
| 121 | .endpoint_rreg = radeon_audio_rreg, |
| 122 | .endpoint_wreg = radeon_audio_wreg, |
Slava Grigorev | 8bf5982 | 2014-12-03 15:29:53 -0500 | [diff] [blame] | 123 | .enable = r600_audio_enable, |
Slava Grigorev | 96ea7af | 2014-12-05 17:59:56 -0500 | [diff] [blame] | 124 | .update_avi_infoframe = r600_update_avi_infoframe, |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static struct radeon_audio_basic_funcs dce4_funcs = { |
| 128 | .endpoint_rreg = radeon_audio_rreg, |
| 129 | .endpoint_wreg = radeon_audio_wreg, |
Slava Grigorev | 8bf5982 | 2014-12-03 15:29:53 -0500 | [diff] [blame] | 130 | .enable = dce4_audio_enable, |
Slava Grigorev | 96ea7af | 2014-12-05 17:59:56 -0500 | [diff] [blame] | 131 | .update_avi_infoframe = evergreen_update_avi_infoframe, |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | static struct radeon_audio_basic_funcs dce6_funcs = { |
| 135 | .endpoint_rreg = dce6_endpoint_rreg, |
| 136 | .endpoint_wreg = dce6_endpoint_wreg, |
Slava Grigorev | 8bf5982 | 2014-12-03 15:29:53 -0500 | [diff] [blame] | 137 | .enable = dce6_audio_enable, |
Slava Grigorev | 96ea7af | 2014-12-05 17:59:56 -0500 | [diff] [blame] | 138 | .update_avi_infoframe = evergreen_update_avi_infoframe, |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 139 | }; |
| 140 | |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 141 | static struct radeon_audio_funcs r600_hdmi_funcs = { |
| 142 | .get_pin = r600_audio_get_pin, |
| 143 | .set_dto = r600_hdmi_audio_set_dto, |
Slava Grigorev | 64424d6e | 2014-12-06 20:19:16 -0500 | [diff] [blame] | 144 | .update_acr = r600_hdmi_update_acr, |
Alex Deucher | 930a978 | 2015-01-20 19:20:52 -0500 | [diff] [blame^] | 145 | .set_vbi_packet = r600_set_vbi_packet, |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 146 | }; |
| 147 | |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 148 | static struct radeon_audio_funcs dce32_hdmi_funcs = { |
Slava Grigorev | 3cdde02 | 2014-12-02 15:22:43 -0500 | [diff] [blame] | 149 | .get_pin = r600_audio_get_pin, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 150 | .write_sad_regs = dce3_2_afmt_write_sad_regs, |
Slava Grigorev | 00a9d4b | 2014-12-01 18:02:57 -0500 | [diff] [blame] | 151 | .write_speaker_allocation = dce3_2_afmt_hdmi_write_speaker_allocation, |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 152 | .set_dto = dce3_2_audio_set_dto, |
Slava Grigorev | 64424d6e | 2014-12-06 20:19:16 -0500 | [diff] [blame] | 153 | .update_acr = dce3_2_hdmi_update_acr, |
Alex Deucher | 930a978 | 2015-01-20 19:20:52 -0500 | [diff] [blame^] | 154 | .set_vbi_packet = r600_set_vbi_packet, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | static struct radeon_audio_funcs dce32_dp_funcs = { |
Slava Grigorev | 3cdde02 | 2014-12-02 15:22:43 -0500 | [diff] [blame] | 158 | .get_pin = r600_audio_get_pin, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 159 | .write_sad_regs = dce3_2_afmt_write_sad_regs, |
Slava Grigorev | 00a9d4b | 2014-12-01 18:02:57 -0500 | [diff] [blame] | 160 | .write_speaker_allocation = dce3_2_afmt_dp_write_speaker_allocation, |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 161 | .set_dto = dce3_2_audio_set_dto, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | static struct radeon_audio_funcs dce4_hdmi_funcs = { |
Slava Grigorev | 3cdde02 | 2014-12-02 15:22:43 -0500 | [diff] [blame] | 165 | .get_pin = r600_audio_get_pin, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 166 | .write_sad_regs = evergreen_hdmi_write_sad_regs, |
Slava Grigorev | 00a9d4b | 2014-12-01 18:02:57 -0500 | [diff] [blame] | 167 | .write_speaker_allocation = dce4_afmt_hdmi_write_speaker_allocation, |
Slava Grigorev | 87654f8 | 2014-12-02 11:20:48 -0500 | [diff] [blame] | 168 | .write_latency_fields = dce4_afmt_write_latency_fields, |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 169 | .set_dto = dce4_hdmi_audio_set_dto, |
Slava Grigorev | 64424d6e | 2014-12-06 20:19:16 -0500 | [diff] [blame] | 170 | .update_acr = evergreen_hdmi_update_acr, |
Alex Deucher | 930a978 | 2015-01-20 19:20:52 -0500 | [diff] [blame^] | 171 | .set_vbi_packet = dce4_set_vbi_packet, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | static struct radeon_audio_funcs dce4_dp_funcs = { |
Slava Grigorev | 3cdde02 | 2014-12-02 15:22:43 -0500 | [diff] [blame] | 175 | .get_pin = r600_audio_get_pin, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 176 | .write_sad_regs = evergreen_hdmi_write_sad_regs, |
Slava Grigorev | 00a9d4b | 2014-12-01 18:02:57 -0500 | [diff] [blame] | 177 | .write_speaker_allocation = dce4_afmt_dp_write_speaker_allocation, |
Slava Grigorev | 87654f8 | 2014-12-02 11:20:48 -0500 | [diff] [blame] | 178 | .write_latency_fields = dce4_afmt_write_latency_fields, |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 179 | .set_dto = dce4_dp_audio_set_dto, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static struct radeon_audio_funcs dce6_hdmi_funcs = { |
Slava Grigorev | 88252d7 | 2014-12-02 17:27:29 -0500 | [diff] [blame] | 183 | .select_pin = dce6_afmt_select_pin, |
Slava Grigorev | 3cdde02 | 2014-12-02 15:22:43 -0500 | [diff] [blame] | 184 | .get_pin = dce6_audio_get_pin, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 185 | .write_sad_regs = dce6_afmt_write_sad_regs, |
Slava Grigorev | 00a9d4b | 2014-12-01 18:02:57 -0500 | [diff] [blame] | 186 | .write_speaker_allocation = dce6_afmt_hdmi_write_speaker_allocation, |
Slava Grigorev | 87654f8 | 2014-12-02 11:20:48 -0500 | [diff] [blame] | 187 | .write_latency_fields = dce6_afmt_write_latency_fields, |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 188 | .set_dto = dce6_hdmi_audio_set_dto, |
Slava Grigorev | 64424d6e | 2014-12-06 20:19:16 -0500 | [diff] [blame] | 189 | .update_acr = evergreen_hdmi_update_acr, |
Alex Deucher | 930a978 | 2015-01-20 19:20:52 -0500 | [diff] [blame^] | 190 | .set_vbi_packet = dce4_set_vbi_packet, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | static struct radeon_audio_funcs dce6_dp_funcs = { |
Slava Grigorev | 88252d7 | 2014-12-02 17:27:29 -0500 | [diff] [blame] | 194 | .select_pin = dce6_afmt_select_pin, |
Slava Grigorev | 3cdde02 | 2014-12-02 15:22:43 -0500 | [diff] [blame] | 195 | .get_pin = dce6_audio_get_pin, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 196 | .write_sad_regs = dce6_afmt_write_sad_regs, |
Slava Grigorev | 00a9d4b | 2014-12-01 18:02:57 -0500 | [diff] [blame] | 197 | .write_speaker_allocation = dce6_afmt_dp_write_speaker_allocation, |
Slava Grigorev | 87654f8 | 2014-12-02 11:20:48 -0500 | [diff] [blame] | 198 | .write_latency_fields = dce6_afmt_write_latency_fields, |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 199 | .set_dto = dce6_dp_audio_set_dto, |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 200 | }; |
| 201 | |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 202 | static void radeon_audio_interface_init(struct radeon_device *rdev) |
| 203 | { |
| 204 | if (ASIC_IS_DCE6(rdev)) { |
| 205 | rdev->audio.funcs = &dce6_funcs; |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 206 | rdev->audio.hdmi_funcs = &dce6_hdmi_funcs; |
| 207 | rdev->audio.dp_funcs = &dce6_dp_funcs; |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 208 | } else if (ASIC_IS_DCE4(rdev)) { |
| 209 | rdev->audio.funcs = &dce4_funcs; |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 210 | rdev->audio.hdmi_funcs = &dce4_hdmi_funcs; |
| 211 | rdev->audio.dp_funcs = &dce4_dp_funcs; |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 212 | } else if (ASIC_IS_DCE32(rdev)) { |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 213 | rdev->audio.funcs = &dce32_funcs; |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 214 | rdev->audio.hdmi_funcs = &dce32_hdmi_funcs; |
| 215 | rdev->audio.dp_funcs = &dce32_dp_funcs; |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 216 | } else { |
| 217 | rdev->audio.funcs = &r600_funcs; |
| 218 | rdev->audio.hdmi_funcs = &r600_hdmi_funcs; |
| 219 | rdev->audio.dp_funcs = 0; |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Slava Grigorev | bfc1f97 | 2014-12-22 17:26:51 -0500 | [diff] [blame] | 223 | static int radeon_audio_chipset_supported(struct radeon_device *rdev) |
| 224 | { |
| 225 | return ASIC_IS_DCE2(rdev) && !ASIC_IS_NODCE(rdev); |
| 226 | } |
| 227 | |
| 228 | int radeon_audio_init(struct radeon_device *rdev) |
| 229 | { |
| 230 | int i; |
| 231 | |
| 232 | if (!radeon_audio || !radeon_audio_chipset_supported(rdev)) |
| 233 | return 0; |
| 234 | |
| 235 | rdev->audio.enabled = true; |
| 236 | |
| 237 | if (ASIC_IS_DCE83(rdev)) /* KB: 2 streams, 3 endpoints */ |
| 238 | rdev->audio.num_pins = 3; |
| 239 | else if (ASIC_IS_DCE81(rdev)) /* KV: 4 streams, 7 endpoints */ |
| 240 | rdev->audio.num_pins = 7; |
| 241 | else if (ASIC_IS_DCE8(rdev)) /* BN/HW: 6 streams, 7 endpoints */ |
| 242 | rdev->audio.num_pins = 7; |
| 243 | else if (ASIC_IS_DCE64(rdev)) /* OL: 2 streams, 2 endpoints */ |
| 244 | rdev->audio.num_pins = 2; |
| 245 | else if (ASIC_IS_DCE61(rdev)) /* TN: 4 streams, 6 endpoints */ |
| 246 | rdev->audio.num_pins = 6; |
| 247 | else if (ASIC_IS_DCE6(rdev)) /* SI: 6 streams, 6 endpoints */ |
| 248 | rdev->audio.num_pins = 6; |
| 249 | else |
| 250 | rdev->audio.num_pins = 1; |
| 251 | |
| 252 | for (i = 0; i < rdev->audio.num_pins; i++) { |
| 253 | rdev->audio.pin[i].channels = -1; |
| 254 | rdev->audio.pin[i].rate = -1; |
| 255 | rdev->audio.pin[i].bits_per_sample = -1; |
| 256 | rdev->audio.pin[i].status_bits = 0; |
| 257 | rdev->audio.pin[i].category_code = 0; |
| 258 | rdev->audio.pin[i].connected = false; |
| 259 | rdev->audio.pin[i].offset = pin_offsets[i]; |
| 260 | rdev->audio.pin[i].id = i; |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | radeon_audio_interface_init(rdev); |
| 264 | |
| 265 | /* disable audio. it will be set up later */ |
| 266 | for (i = 0; i < rdev->audio.num_pins; i++) |
Slava Grigorev | 8bf5982 | 2014-12-03 15:29:53 -0500 | [diff] [blame] | 267 | radeon_audio_enable(rdev, &rdev->audio.pin[i], false); |
Slava Grigorev | bfc1f97 | 2014-12-22 17:26:51 -0500 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |
Slava Grigorev | 1a626b6 | 2014-12-01 13:49:39 -0500 | [diff] [blame] | 271 | |
| 272 | void radeon_audio_detect(struct drm_connector *connector, |
| 273 | enum drm_connector_status status) |
| 274 | { |
| 275 | if (!connector || !connector->encoder) |
| 276 | return; |
| 277 | |
| 278 | if (status == connector_status_connected) { |
| 279 | int sink_type; |
| 280 | struct radeon_device *rdev = connector->encoder->dev->dev_private; |
| 281 | struct radeon_connector *radeon_connector; |
| 282 | struct radeon_encoder *radeon_encoder = |
| 283 | to_radeon_encoder(connector->encoder); |
| 284 | |
| 285 | if (!drm_detect_monitor_audio(radeon_connector_edid(connector))) { |
| 286 | radeon_encoder->audio = 0; |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | radeon_connector = to_radeon_connector(connector); |
| 291 | sink_type = radeon_dp_getsinktype(radeon_connector); |
| 292 | |
| 293 | if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort && |
| 294 | sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) |
| 295 | radeon_encoder->audio = rdev->audio.dp_funcs; |
| 296 | else |
| 297 | radeon_encoder->audio = rdev->audio.hdmi_funcs; |
| 298 | /* TODO: set up the sads, etc. and set the audio enable_mask */ |
| 299 | } else { |
| 300 | /* TODO: reset the audio enable_mask */ |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | u32 radeon_audio_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg) |
| 305 | { |
| 306 | if (rdev->audio.funcs->endpoint_rreg) |
| 307 | return rdev->audio.funcs->endpoint_rreg(rdev, offset, reg); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | void radeon_audio_endpoint_wreg(struct radeon_device *rdev, u32 offset, |
| 313 | u32 reg, u32 v) |
| 314 | { |
| 315 | if (rdev->audio.funcs->endpoint_wreg) |
| 316 | rdev->audio.funcs->endpoint_wreg(rdev, offset, reg, v); |
| 317 | } |
Alex Deucher | 070a2e6 | 2015-01-22 10:41:55 -0500 | [diff] [blame] | 318 | |
| 319 | void radeon_audio_write_sad_regs(struct drm_encoder *encoder) |
| 320 | { |
| 321 | struct radeon_encoder *radeon_encoder; |
| 322 | struct drm_connector *connector; |
| 323 | struct radeon_connector *radeon_connector = NULL; |
| 324 | struct cea_sad *sads; |
| 325 | int sad_count; |
| 326 | |
| 327 | list_for_each_entry(connector, |
| 328 | &encoder->dev->mode_config.connector_list, head) { |
| 329 | if (connector->encoder == encoder) { |
| 330 | radeon_connector = to_radeon_connector(connector); |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if (!radeon_connector) { |
| 336 | DRM_ERROR("Couldn't find encoder's connector\n"); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | sad_count = drm_edid_to_sad(radeon_connector_edid(connector), &sads); |
| 341 | if (sad_count <= 0) { |
| 342 | DRM_ERROR("Couldn't read SADs: %d\n", sad_count); |
| 343 | return; |
| 344 | } |
| 345 | BUG_ON(!sads); |
| 346 | |
| 347 | radeon_encoder = to_radeon_encoder(encoder); |
| 348 | |
| 349 | if (radeon_encoder->audio && radeon_encoder->audio->write_sad_regs) |
| 350 | radeon_encoder->audio->write_sad_regs(encoder, sads, sad_count); |
| 351 | |
| 352 | kfree(sads); |
| 353 | } |
Slava Grigorev | 00a9d4b | 2014-12-01 18:02:57 -0500 | [diff] [blame] | 354 | |
| 355 | void radeon_audio_write_speaker_allocation(struct drm_encoder *encoder) |
| 356 | { |
| 357 | struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); |
| 358 | struct drm_connector *connector; |
| 359 | struct radeon_connector *radeon_connector = NULL; |
| 360 | u8 *sadb = NULL; |
| 361 | int sad_count; |
| 362 | |
| 363 | list_for_each_entry(connector, |
| 364 | &encoder->dev->mode_config.connector_list, head) { |
| 365 | if (connector->encoder == encoder) { |
| 366 | radeon_connector = to_radeon_connector(connector); |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if (!radeon_connector) { |
| 372 | DRM_ERROR("Couldn't find encoder's connector\n"); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | sad_count = drm_edid_to_speaker_allocation( |
| 377 | radeon_connector_edid(connector), &sadb); |
| 378 | if (sad_count < 0) { |
| 379 | DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n", |
| 380 | sad_count); |
| 381 | sad_count = 0; |
| 382 | } |
| 383 | |
| 384 | if (radeon_encoder->audio && radeon_encoder->audio->write_speaker_allocation) |
| 385 | radeon_encoder->audio->write_speaker_allocation(encoder, sadb, sad_count); |
| 386 | |
| 387 | kfree(sadb); |
| 388 | } |
Slava Grigorev | 87654f8 | 2014-12-02 11:20:48 -0500 | [diff] [blame] | 389 | |
| 390 | void radeon_audio_write_latency_fields(struct drm_encoder *encoder, |
| 391 | struct drm_display_mode *mode) |
| 392 | { |
| 393 | struct radeon_encoder *radeon_encoder; |
| 394 | struct drm_connector *connector; |
| 395 | struct radeon_connector *radeon_connector = 0; |
| 396 | |
| 397 | list_for_each_entry(connector, |
| 398 | &encoder->dev->mode_config.connector_list, head) { |
| 399 | if (connector->encoder == encoder) { |
| 400 | radeon_connector = to_radeon_connector(connector); |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (!radeon_connector) { |
| 406 | DRM_ERROR("Couldn't find encoder's connector\n"); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | radeon_encoder = to_radeon_encoder(encoder); |
| 411 | |
| 412 | if (radeon_encoder->audio && radeon_encoder->audio->write_latency_fields) |
| 413 | radeon_encoder->audio->write_latency_fields(encoder, connector, mode); |
| 414 | } |
Slava Grigorev | 3cdde02 | 2014-12-02 15:22:43 -0500 | [diff] [blame] | 415 | |
| 416 | struct r600_audio_pin* radeon_audio_get_pin(struct drm_encoder *encoder) |
| 417 | { |
| 418 | struct radeon_device *rdev = encoder->dev->dev_private; |
| 419 | struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); |
| 420 | |
| 421 | if (radeon_encoder->audio && radeon_encoder->audio->get_pin) |
| 422 | return radeon_encoder->audio->get_pin(rdev); |
| 423 | |
| 424 | return NULL; |
| 425 | } |
Slava Grigorev | 88252d7 | 2014-12-02 17:27:29 -0500 | [diff] [blame] | 426 | |
| 427 | void radeon_audio_select_pin(struct drm_encoder *encoder) |
| 428 | { |
| 429 | struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); |
| 430 | |
| 431 | if (radeon_encoder->audio && radeon_encoder->audio->select_pin) |
| 432 | radeon_encoder->audio->select_pin(encoder); |
| 433 | } |
Slava Grigorev | 8bf5982 | 2014-12-03 15:29:53 -0500 | [diff] [blame] | 434 | |
| 435 | void radeon_audio_enable(struct radeon_device *rdev, |
| 436 | struct r600_audio_pin *pin, u8 enable_mask) |
| 437 | { |
| 438 | if (rdev->audio.funcs->enable) |
| 439 | rdev->audio.funcs->enable(rdev, pin, enable_mask); |
| 440 | } |
Slava Grigorev | 7991d66 | 2014-12-03 17:07:01 -0500 | [diff] [blame] | 441 | |
| 442 | void radeon_audio_fini(struct radeon_device *rdev) |
| 443 | { |
| 444 | int i; |
| 445 | |
| 446 | if (!rdev->audio.enabled) |
| 447 | return; |
| 448 | |
| 449 | for (i = 0; i < rdev->audio.num_pins; i++) |
| 450 | radeon_audio_enable(rdev, &rdev->audio.pin[i], false); |
| 451 | |
| 452 | rdev->audio.enabled = false; |
| 453 | } |
Slava Grigorev | a85d682 | 2014-12-05 13:38:31 -0500 | [diff] [blame] | 454 | |
| 455 | void radeon_audio_set_dto(struct drm_encoder *encoder, unsigned int clock) |
| 456 | { |
| 457 | struct radeon_device *rdev = encoder->dev->dev_private; |
| 458 | struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); |
| 459 | struct radeon_crtc *crtc = to_radeon_crtc(encoder->crtc); |
| 460 | |
| 461 | if (radeon_encoder->audio && radeon_encoder->audio->set_dto) |
| 462 | radeon_encoder->audio->set_dto(rdev, crtc, clock); |
| 463 | } |
Slava Grigorev | 96ea7af | 2014-12-05 17:59:56 -0500 | [diff] [blame] | 464 | |
| 465 | void radeon_update_avi_infoframe(struct drm_encoder *encoder, void *buffer, |
| 466 | size_t size) |
| 467 | { |
| 468 | struct radeon_device *rdev = encoder->dev->dev_private; |
| 469 | struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); |
| 470 | struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv; |
| 471 | |
| 472 | if (dig && dig->afmt && rdev->audio.funcs->update_avi_infoframe) |
| 473 | rdev->audio.funcs->update_avi_infoframe(rdev, dig->afmt->offset, |
| 474 | buffer, size); |
| 475 | } |
Slava Grigorev | 64424d6e | 2014-12-06 20:19:16 -0500 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | * calculate CTS and N values if they are not found in the table |
| 479 | */ |
| 480 | static void radeon_audio_calc_cts(unsigned int clock, int *CTS, int *N, int freq) |
| 481 | { |
| 482 | int n, cts; |
| 483 | unsigned long div, mul; |
| 484 | |
| 485 | /* Safe, but overly large values */ |
| 486 | n = 128 * freq; |
| 487 | cts = clock * 1000; |
| 488 | |
| 489 | /* Smallest valid fraction */ |
| 490 | div = gcd(n, cts); |
| 491 | |
| 492 | n /= div; |
| 493 | cts /= div; |
| 494 | |
| 495 | /* |
| 496 | * The optimal N is 128*freq/1000. Calculate the closest larger |
| 497 | * value that doesn't truncate any bits. |
| 498 | */ |
| 499 | mul = ((128*freq/1000) + (n-1))/n; |
| 500 | |
| 501 | n *= mul; |
| 502 | cts *= mul; |
| 503 | |
| 504 | /* Check that we are in spec (not always possible) */ |
| 505 | if (n < (128*freq/1500)) |
| 506 | printk(KERN_WARNING "Calculated ACR N value is too small. You may experience audio problems.\n"); |
| 507 | if (n > (128*freq/300)) |
| 508 | printk(KERN_WARNING "Calculated ACR N value is too large. You may experience audio problems.\n"); |
| 509 | |
| 510 | *N = n; |
| 511 | *CTS = cts; |
| 512 | |
| 513 | DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n", |
| 514 | *N, *CTS, freq); |
| 515 | } |
| 516 | |
| 517 | static const struct radeon_hdmi_acr* radeon_audio_acr(unsigned int clock) |
| 518 | { |
| 519 | static struct radeon_hdmi_acr res; |
| 520 | u8 i; |
| 521 | |
| 522 | static const struct radeon_hdmi_acr hdmi_predefined_acr[] = { |
| 523 | /* 32kHz 44.1kHz 48kHz */ |
| 524 | /* Clock N CTS N CTS N CTS */ |
| 525 | { 25175, 4096, 25175, 28224, 125875, 6144, 25175 }, /* 25,20/1.001 MHz */ |
| 526 | { 25200, 4096, 25200, 6272, 28000, 6144, 25200 }, /* 25.20 MHz */ |
| 527 | { 27000, 4096, 27000, 6272, 30000, 6144, 27000 }, /* 27.00 MHz */ |
| 528 | { 27027, 4096, 27027, 6272, 30030, 6144, 27027 }, /* 27.00*1.001 MHz */ |
| 529 | { 54000, 4096, 54000, 6272, 60000, 6144, 54000 }, /* 54.00 MHz */ |
| 530 | { 54054, 4096, 54054, 6272, 60060, 6144, 54054 }, /* 54.00*1.001 MHz */ |
| 531 | { 74176, 4096, 74176, 5733, 75335, 6144, 74176 }, /* 74.25/1.001 MHz */ |
| 532 | { 74250, 4096, 74250, 6272, 82500, 6144, 74250 }, /* 74.25 MHz */ |
| 533 | { 148352, 4096, 148352, 5733, 150670, 6144, 148352 }, /* 148.50/1.001 MHz */ |
| 534 | { 148500, 4096, 148500, 6272, 165000, 6144, 148500 }, /* 148.50 MHz */ |
| 535 | }; |
| 536 | |
| 537 | /* Precalculated values for common clocks */ |
| 538 | for (i = 0; i < ARRAY_SIZE(hdmi_predefined_acr); i++) |
| 539 | if (hdmi_predefined_acr[i].clock == clock) |
| 540 | return &hdmi_predefined_acr[i]; |
| 541 | |
| 542 | /* And odd clocks get manually calculated */ |
| 543 | radeon_audio_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000); |
| 544 | radeon_audio_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100); |
| 545 | radeon_audio_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000); |
| 546 | |
| 547 | return &res; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * update the N and CTS parameters for a given pixel clock rate |
| 552 | */ |
| 553 | void radeon_audio_update_acr(struct drm_encoder *encoder, unsigned int clock) |
| 554 | { |
| 555 | const struct radeon_hdmi_acr *acr = radeon_audio_acr(clock); |
| 556 | struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); |
| 557 | struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv; |
| 558 | |
| 559 | if (!dig || !dig->afmt) |
| 560 | return; |
| 561 | |
| 562 | if (radeon_encoder->audio && radeon_encoder->audio->update_acr) |
| 563 | radeon_encoder->audio->update_acr(encoder, dig->afmt->offset, acr); |
| 564 | } |
Alex Deucher | 930a978 | 2015-01-20 19:20:52 -0500 | [diff] [blame^] | 565 | |
| 566 | void radeon_audio_set_vbi_packet(struct drm_encoder *encoder) |
| 567 | { |
| 568 | struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder); |
| 569 | struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv; |
| 570 | |
| 571 | if (!dig || !dig->afmt) |
| 572 | return; |
| 573 | |
| 574 | if (radeon_encoder->audio && radeon_encoder->audio->set_vbi_packet) |
| 575 | radeon_encoder->audio->set_vbi_packet(encoder, dig->afmt->offset); |
| 576 | } |