2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Slava Grigorev <slava.grigorev@amd.com>
26 #include <drm/drm_crtc.h>
29 #include "radeon_audio.h"
31 void r600_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
33 void dce4_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
35 void dce6_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
37 u32 dce6_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg);
38 void dce6_endpoint_wreg(struct radeon_device *rdev,
39 u32 offset, u32 reg, u32 v);
40 void dce3_2_afmt_write_sad_regs(struct drm_encoder *encoder,
41 struct cea_sad *sads, int sad_count);
42 void evergreen_hdmi_write_sad_regs(struct drm_encoder *encoder,
43 struct cea_sad *sads, int sad_count);
44 void dce6_afmt_write_sad_regs(struct drm_encoder *encoder,
45 struct cea_sad *sads, int sad_count);
46 void dce3_2_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
47 u8 *sadb, int sad_count);
48 void dce3_2_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
49 u8 *sadb, int sad_count);
50 void dce4_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
51 u8 *sadb, int sad_count);
52 void dce4_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
53 u8 *sadb, int sad_count);
54 void dce6_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
55 u8 *sadb, int sad_count);
56 void dce6_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
57 u8 *sadb, int sad_count);
58 void dce4_afmt_write_latency_fields(struct drm_encoder *encoder,
59 struct drm_connector *connector, struct drm_display_mode *mode);
60 void dce6_afmt_write_latency_fields(struct drm_encoder *encoder,
61 struct drm_connector *connector, struct drm_display_mode *mode);
62 struct r600_audio_pin* r600_audio_get_pin(struct radeon_device *rdev);
63 struct r600_audio_pin* dce6_audio_get_pin(struct radeon_device *rdev);
64 void dce6_afmt_select_pin(struct drm_encoder *encoder);
65 void r600_hdmi_audio_set_dto(struct radeon_device *rdev,
66 struct radeon_crtc *crtc, unsigned int clock);
67 void dce3_2_audio_set_dto(struct radeon_device *rdev,
68 struct radeon_crtc *crtc, unsigned int clock);
69 void dce4_hdmi_audio_set_dto(struct radeon_device *rdev,
70 struct radeon_crtc *crtc, unsigned int clock);
71 void dce4_dp_audio_set_dto(struct radeon_device *rdev,
72 struct radeon_crtc *crtc, unsigned int clock);
73 void dce6_hdmi_audio_set_dto(struct radeon_device *rdev,
74 struct radeon_crtc *crtc, unsigned int clock);
75 void dce6_dp_audio_set_dto(struct radeon_device *rdev,
76 struct radeon_crtc *crtc, unsigned int clock);
77 void r600_update_avi_infoframe(struct radeon_device *rdev, u32 offset,
78 unsigned char *buffer, size_t size);
79 void evergreen_update_avi_infoframe(struct radeon_device *rdev, u32 offset,
80 unsigned char *buffer, size_t size);
82 static const u32 pin_offsets[7] =
93 static u32 radeon_audio_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
98 static void radeon_audio_wreg(struct radeon_device *rdev, u32 offset,
104 static struct radeon_audio_basic_funcs r600_funcs = {
105 .endpoint_rreg = radeon_audio_rreg,
106 .endpoint_wreg = radeon_audio_wreg,
107 .enable = r600_audio_enable,
108 .update_avi_infoframe = r600_update_avi_infoframe,
111 static struct radeon_audio_basic_funcs dce32_funcs = {
112 .endpoint_rreg = radeon_audio_rreg,
113 .endpoint_wreg = radeon_audio_wreg,
114 .enable = r600_audio_enable,
115 .update_avi_infoframe = r600_update_avi_infoframe,
118 static struct radeon_audio_basic_funcs dce4_funcs = {
119 .endpoint_rreg = radeon_audio_rreg,
120 .endpoint_wreg = radeon_audio_wreg,
121 .enable = dce4_audio_enable,
122 .update_avi_infoframe = evergreen_update_avi_infoframe,
125 static struct radeon_audio_basic_funcs dce6_funcs = {
126 .endpoint_rreg = dce6_endpoint_rreg,
127 .endpoint_wreg = dce6_endpoint_wreg,
128 .enable = dce6_audio_enable,
129 .update_avi_infoframe = evergreen_update_avi_infoframe,
132 static struct radeon_audio_funcs r600_hdmi_funcs = {
133 .get_pin = r600_audio_get_pin,
134 .set_dto = r600_hdmi_audio_set_dto,
137 static struct radeon_audio_funcs dce32_hdmi_funcs = {
138 .get_pin = r600_audio_get_pin,
139 .write_sad_regs = dce3_2_afmt_write_sad_regs,
140 .write_speaker_allocation = dce3_2_afmt_hdmi_write_speaker_allocation,
141 .set_dto = dce3_2_audio_set_dto,
144 static struct radeon_audio_funcs dce32_dp_funcs = {
145 .get_pin = r600_audio_get_pin,
146 .write_sad_regs = dce3_2_afmt_write_sad_regs,
147 .write_speaker_allocation = dce3_2_afmt_dp_write_speaker_allocation,
148 .set_dto = dce3_2_audio_set_dto,
151 static struct radeon_audio_funcs dce4_hdmi_funcs = {
152 .get_pin = r600_audio_get_pin,
153 .write_sad_regs = evergreen_hdmi_write_sad_regs,
154 .write_speaker_allocation = dce4_afmt_hdmi_write_speaker_allocation,
155 .write_latency_fields = dce4_afmt_write_latency_fields,
156 .set_dto = dce4_hdmi_audio_set_dto,
159 static struct radeon_audio_funcs dce4_dp_funcs = {
160 .get_pin = r600_audio_get_pin,
161 .write_sad_regs = evergreen_hdmi_write_sad_regs,
162 .write_speaker_allocation = dce4_afmt_dp_write_speaker_allocation,
163 .write_latency_fields = dce4_afmt_write_latency_fields,
164 .set_dto = dce4_dp_audio_set_dto,
167 static struct radeon_audio_funcs dce6_hdmi_funcs = {
168 .select_pin = dce6_afmt_select_pin,
169 .get_pin = dce6_audio_get_pin,
170 .write_sad_regs = dce6_afmt_write_sad_regs,
171 .write_speaker_allocation = dce6_afmt_hdmi_write_speaker_allocation,
172 .write_latency_fields = dce6_afmt_write_latency_fields,
173 .set_dto = dce6_hdmi_audio_set_dto,
176 static struct radeon_audio_funcs dce6_dp_funcs = {
177 .select_pin = dce6_afmt_select_pin,
178 .get_pin = dce6_audio_get_pin,
179 .write_sad_regs = dce6_afmt_write_sad_regs,
180 .write_speaker_allocation = dce6_afmt_dp_write_speaker_allocation,
181 .write_latency_fields = dce6_afmt_write_latency_fields,
182 .set_dto = dce6_dp_audio_set_dto,
185 static void radeon_audio_interface_init(struct radeon_device *rdev)
187 if (ASIC_IS_DCE6(rdev)) {
188 rdev->audio.funcs = &dce6_funcs;
189 rdev->audio.hdmi_funcs = &dce6_hdmi_funcs;
190 rdev->audio.dp_funcs = &dce6_dp_funcs;
191 } else if (ASIC_IS_DCE4(rdev)) {
192 rdev->audio.funcs = &dce4_funcs;
193 rdev->audio.hdmi_funcs = &dce4_hdmi_funcs;
194 rdev->audio.dp_funcs = &dce4_dp_funcs;
195 } else if (ASIC_IS_DCE32(rdev)) {
196 rdev->audio.funcs = &dce32_funcs;
197 rdev->audio.hdmi_funcs = &dce32_hdmi_funcs;
198 rdev->audio.dp_funcs = &dce32_dp_funcs;
200 rdev->audio.funcs = &r600_funcs;
201 rdev->audio.hdmi_funcs = &r600_hdmi_funcs;
202 rdev->audio.dp_funcs = 0;
206 static int radeon_audio_chipset_supported(struct radeon_device *rdev)
208 return ASIC_IS_DCE2(rdev) && !ASIC_IS_NODCE(rdev);
211 int radeon_audio_init(struct radeon_device *rdev)
215 if (!radeon_audio || !radeon_audio_chipset_supported(rdev))
218 rdev->audio.enabled = true;
220 if (ASIC_IS_DCE83(rdev)) /* KB: 2 streams, 3 endpoints */
221 rdev->audio.num_pins = 3;
222 else if (ASIC_IS_DCE81(rdev)) /* KV: 4 streams, 7 endpoints */
223 rdev->audio.num_pins = 7;
224 else if (ASIC_IS_DCE8(rdev)) /* BN/HW: 6 streams, 7 endpoints */
225 rdev->audio.num_pins = 7;
226 else if (ASIC_IS_DCE64(rdev)) /* OL: 2 streams, 2 endpoints */
227 rdev->audio.num_pins = 2;
228 else if (ASIC_IS_DCE61(rdev)) /* TN: 4 streams, 6 endpoints */
229 rdev->audio.num_pins = 6;
230 else if (ASIC_IS_DCE6(rdev)) /* SI: 6 streams, 6 endpoints */
231 rdev->audio.num_pins = 6;
233 rdev->audio.num_pins = 1;
235 for (i = 0; i < rdev->audio.num_pins; i++) {
236 rdev->audio.pin[i].channels = -1;
237 rdev->audio.pin[i].rate = -1;
238 rdev->audio.pin[i].bits_per_sample = -1;
239 rdev->audio.pin[i].status_bits = 0;
240 rdev->audio.pin[i].category_code = 0;
241 rdev->audio.pin[i].connected = false;
242 rdev->audio.pin[i].offset = pin_offsets[i];
243 rdev->audio.pin[i].id = i;
246 radeon_audio_interface_init(rdev);
248 /* disable audio. it will be set up later */
249 for (i = 0; i < rdev->audio.num_pins; i++)
250 radeon_audio_enable(rdev, &rdev->audio.pin[i], false);
255 void radeon_audio_detect(struct drm_connector *connector,
256 enum drm_connector_status status)
258 if (!connector || !connector->encoder)
261 if (status == connector_status_connected) {
263 struct radeon_device *rdev = connector->encoder->dev->dev_private;
264 struct radeon_connector *radeon_connector;
265 struct radeon_encoder *radeon_encoder =
266 to_radeon_encoder(connector->encoder);
268 if (!drm_detect_monitor_audio(radeon_connector_edid(connector))) {
269 radeon_encoder->audio = 0;
273 radeon_connector = to_radeon_connector(connector);
274 sink_type = radeon_dp_getsinktype(radeon_connector);
276 if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort &&
277 sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT)
278 radeon_encoder->audio = rdev->audio.dp_funcs;
280 radeon_encoder->audio = rdev->audio.hdmi_funcs;
281 /* TODO: set up the sads, etc. and set the audio enable_mask */
283 /* TODO: reset the audio enable_mask */
287 u32 radeon_audio_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
289 if (rdev->audio.funcs->endpoint_rreg)
290 return rdev->audio.funcs->endpoint_rreg(rdev, offset, reg);
295 void radeon_audio_endpoint_wreg(struct radeon_device *rdev, u32 offset,
298 if (rdev->audio.funcs->endpoint_wreg)
299 rdev->audio.funcs->endpoint_wreg(rdev, offset, reg, v);
302 void radeon_audio_write_sad_regs(struct drm_encoder *encoder)
304 struct radeon_encoder *radeon_encoder;
305 struct drm_connector *connector;
306 struct radeon_connector *radeon_connector = NULL;
307 struct cea_sad *sads;
310 list_for_each_entry(connector,
311 &encoder->dev->mode_config.connector_list, head) {
312 if (connector->encoder == encoder) {
313 radeon_connector = to_radeon_connector(connector);
318 if (!radeon_connector) {
319 DRM_ERROR("Couldn't find encoder's connector\n");
323 sad_count = drm_edid_to_sad(radeon_connector_edid(connector), &sads);
324 if (sad_count <= 0) {
325 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
330 radeon_encoder = to_radeon_encoder(encoder);
332 if (radeon_encoder->audio && radeon_encoder->audio->write_sad_regs)
333 radeon_encoder->audio->write_sad_regs(encoder, sads, sad_count);
338 void radeon_audio_write_speaker_allocation(struct drm_encoder *encoder)
340 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
341 struct drm_connector *connector;
342 struct radeon_connector *radeon_connector = NULL;
346 list_for_each_entry(connector,
347 &encoder->dev->mode_config.connector_list, head) {
348 if (connector->encoder == encoder) {
349 radeon_connector = to_radeon_connector(connector);
354 if (!radeon_connector) {
355 DRM_ERROR("Couldn't find encoder's connector\n");
359 sad_count = drm_edid_to_speaker_allocation(
360 radeon_connector_edid(connector), &sadb);
362 DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n",
367 if (radeon_encoder->audio && radeon_encoder->audio->write_speaker_allocation)
368 radeon_encoder->audio->write_speaker_allocation(encoder, sadb, sad_count);
373 void radeon_audio_write_latency_fields(struct drm_encoder *encoder,
374 struct drm_display_mode *mode)
376 struct radeon_encoder *radeon_encoder;
377 struct drm_connector *connector;
378 struct radeon_connector *radeon_connector = 0;
380 list_for_each_entry(connector,
381 &encoder->dev->mode_config.connector_list, head) {
382 if (connector->encoder == encoder) {
383 radeon_connector = to_radeon_connector(connector);
388 if (!radeon_connector) {
389 DRM_ERROR("Couldn't find encoder's connector\n");
393 radeon_encoder = to_radeon_encoder(encoder);
395 if (radeon_encoder->audio && radeon_encoder->audio->write_latency_fields)
396 radeon_encoder->audio->write_latency_fields(encoder, connector, mode);
399 struct r600_audio_pin* radeon_audio_get_pin(struct drm_encoder *encoder)
401 struct radeon_device *rdev = encoder->dev->dev_private;
402 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
404 if (radeon_encoder->audio && radeon_encoder->audio->get_pin)
405 return radeon_encoder->audio->get_pin(rdev);
410 void radeon_audio_select_pin(struct drm_encoder *encoder)
412 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
414 if (radeon_encoder->audio && radeon_encoder->audio->select_pin)
415 radeon_encoder->audio->select_pin(encoder);
418 void radeon_audio_enable(struct radeon_device *rdev,
419 struct r600_audio_pin *pin, u8 enable_mask)
421 if (rdev->audio.funcs->enable)
422 rdev->audio.funcs->enable(rdev, pin, enable_mask);
425 void radeon_audio_fini(struct radeon_device *rdev)
429 if (!rdev->audio.enabled)
432 for (i = 0; i < rdev->audio.num_pins; i++)
433 radeon_audio_enable(rdev, &rdev->audio.pin[i], false);
435 rdev->audio.enabled = false;
438 void radeon_audio_set_dto(struct drm_encoder *encoder, unsigned int clock)
440 struct radeon_device *rdev = encoder->dev->dev_private;
441 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
442 struct radeon_crtc *crtc = to_radeon_crtc(encoder->crtc);
444 if (radeon_encoder->audio && radeon_encoder->audio->set_dto)
445 radeon_encoder->audio->set_dto(rdev, crtc, clock);
448 void radeon_update_avi_infoframe(struct drm_encoder *encoder, void *buffer,
451 struct radeon_device *rdev = encoder->dev->dev_private;
452 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
453 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
455 if (dig && dig->afmt && rdev->audio.funcs->update_avi_infoframe)
456 rdev->audio.funcs->update_avi_infoframe(rdev, dig->afmt->offset,