blob: 8fad9af92ec8df4c1dec9da17edbce87c032fc3e [file] [log] [blame]
Slava Grigorevbfc1f972014-12-22 17:26:51 -05001/*
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 Grigorev64424d6e2014-12-06 20:19:16 -050025#include <linux/gcd.h>
Slava Grigorevbfc1f972014-12-22 17:26:51 -050026#include <drm/drmP.h>
Slava Grigorev1a626b62014-12-01 13:49:39 -050027#include <drm/drm_crtc.h>
Slava Grigorevbfc1f972014-12-22 17:26:51 -050028#include "radeon.h"
Slava Grigorev1a626b62014-12-01 13:49:39 -050029#include "atom.h"
30#include "radeon_audio.h"
Slava Grigorevbfc1f972014-12-22 17:26:51 -050031
32void r600_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
33 u8 enable_mask);
Slava Grigorev8bf59822014-12-03 15:29:53 -050034void dce4_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
35 u8 enable_mask);
Slava Grigorevbfc1f972014-12-22 17:26:51 -050036void dce6_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
37 u8 enable_mask);
Slava Grigorev1a626b62014-12-01 13:49:39 -050038u32 dce6_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg);
39void dce6_endpoint_wreg(struct radeon_device *rdev,
40 u32 offset, u32 reg, u32 v);
Alex Deucher070a2e62015-01-22 10:41:55 -050041void dce3_2_afmt_write_sad_regs(struct drm_encoder *encoder,
42 struct cea_sad *sads, int sad_count);
43void evergreen_hdmi_write_sad_regs(struct drm_encoder *encoder,
44 struct cea_sad *sads, int sad_count);
45void dce6_afmt_write_sad_regs(struct drm_encoder *encoder,
46 struct cea_sad *sads, int sad_count);
Slava Grigorev00a9d4b2014-12-01 18:02:57 -050047void dce3_2_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
48 u8 *sadb, int sad_count);
49void dce3_2_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
50 u8 *sadb, int sad_count);
51void dce4_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
52 u8 *sadb, int sad_count);
53void dce4_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
54 u8 *sadb, int sad_count);
55void dce6_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
56 u8 *sadb, int sad_count);
57void dce6_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
58 u8 *sadb, int sad_count);
Slava Grigorev87654f82014-12-02 11:20:48 -050059void dce4_afmt_write_latency_fields(struct drm_encoder *encoder,
60 struct drm_connector *connector, struct drm_display_mode *mode);
61void dce6_afmt_write_latency_fields(struct drm_encoder *encoder,
62 struct drm_connector *connector, struct drm_display_mode *mode);
Slava Grigorev3cdde022014-12-02 15:22:43 -050063struct r600_audio_pin* r600_audio_get_pin(struct radeon_device *rdev);
64struct r600_audio_pin* dce6_audio_get_pin(struct radeon_device *rdev);
Slava Grigorev88252d72014-12-02 17:27:29 -050065void dce6_afmt_select_pin(struct drm_encoder *encoder);
Slava Grigoreva85d6822014-12-05 13:38:31 -050066void r600_hdmi_audio_set_dto(struct radeon_device *rdev,
67 struct radeon_crtc *crtc, unsigned int clock);
68void dce3_2_audio_set_dto(struct radeon_device *rdev,
69 struct radeon_crtc *crtc, unsigned int clock);
70void dce4_hdmi_audio_set_dto(struct radeon_device *rdev,
71 struct radeon_crtc *crtc, unsigned int clock);
72void dce4_dp_audio_set_dto(struct radeon_device *rdev,
73 struct radeon_crtc *crtc, unsigned int clock);
74void dce6_hdmi_audio_set_dto(struct radeon_device *rdev,
75 struct radeon_crtc *crtc, unsigned int clock);
76void dce6_dp_audio_set_dto(struct radeon_device *rdev,
77 struct radeon_crtc *crtc, unsigned int clock);
Slava Grigorev96ea7af2014-12-05 17:59:56 -050078void r600_update_avi_infoframe(struct radeon_device *rdev, u32 offset,
79 unsigned char *buffer, size_t size);
80void evergreen_update_avi_infoframe(struct radeon_device *rdev, u32 offset,
81 unsigned char *buffer, size_t size);
Slava Grigorev64424d6e2014-12-06 20:19:16 -050082void r600_hdmi_update_acr(struct drm_encoder *encoder, long offset,
83 const struct radeon_hdmi_acr *acr);
84void dce3_2_hdmi_update_acr(struct drm_encoder *encoder, long offset,
85 const struct radeon_hdmi_acr *acr);
86void evergreen_hdmi_update_acr(struct drm_encoder *encoder, long offset,
87 const struct radeon_hdmi_acr *acr);
Alex Deucher930a9782015-01-20 19:20:52 -050088void r600_set_vbi_packet(struct drm_encoder *encoder, u32 offset);
89void dce4_set_vbi_packet(struct drm_encoder *encoder, u32 offset);
Slava Grigorevbfc1f972014-12-22 17:26:51 -050090
91static 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 Grigorev1a626b62014-12-01 13:49:39 -0500102static u32 radeon_audio_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
103{
104 return RREG32(reg);
105}
106
107static void radeon_audio_wreg(struct radeon_device *rdev, u32 offset,
108 u32 reg, u32 v)
109{
110 WREG32(reg, v);
111}
112
Slava Grigoreva85d6822014-12-05 13:38:31 -0500113static 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 Grigorev96ea7af2014-12-05 17:59:56 -0500117 .update_avi_infoframe = r600_update_avi_infoframe,
Slava Grigoreva85d6822014-12-05 13:38:31 -0500118};
119
Slava Grigorev1a626b62014-12-01 13:49:39 -0500120static struct radeon_audio_basic_funcs dce32_funcs = {
121 .endpoint_rreg = radeon_audio_rreg,
122 .endpoint_wreg = radeon_audio_wreg,
Slava Grigorev8bf59822014-12-03 15:29:53 -0500123 .enable = r600_audio_enable,
Slava Grigorev96ea7af2014-12-05 17:59:56 -0500124 .update_avi_infoframe = r600_update_avi_infoframe,
Slava Grigorev1a626b62014-12-01 13:49:39 -0500125};
126
127static struct radeon_audio_basic_funcs dce4_funcs = {
128 .endpoint_rreg = radeon_audio_rreg,
129 .endpoint_wreg = radeon_audio_wreg,
Slava Grigorev8bf59822014-12-03 15:29:53 -0500130 .enable = dce4_audio_enable,
Slava Grigorev96ea7af2014-12-05 17:59:56 -0500131 .update_avi_infoframe = evergreen_update_avi_infoframe,
Slava Grigorev1a626b62014-12-01 13:49:39 -0500132};
133
134static struct radeon_audio_basic_funcs dce6_funcs = {
135 .endpoint_rreg = dce6_endpoint_rreg,
136 .endpoint_wreg = dce6_endpoint_wreg,
Slava Grigorev8bf59822014-12-03 15:29:53 -0500137 .enable = dce6_audio_enable,
Slava Grigorev96ea7af2014-12-05 17:59:56 -0500138 .update_avi_infoframe = evergreen_update_avi_infoframe,
Slava Grigorev1a626b62014-12-01 13:49:39 -0500139};
140
Slava Grigoreva85d6822014-12-05 13:38:31 -0500141static struct radeon_audio_funcs r600_hdmi_funcs = {
142 .get_pin = r600_audio_get_pin,
143 .set_dto = r600_hdmi_audio_set_dto,
Slava Grigorev64424d6e2014-12-06 20:19:16 -0500144 .update_acr = r600_hdmi_update_acr,
Alex Deucher930a9782015-01-20 19:20:52 -0500145 .set_vbi_packet = r600_set_vbi_packet,
Slava Grigoreva85d6822014-12-05 13:38:31 -0500146};
147
Alex Deucher070a2e62015-01-22 10:41:55 -0500148static struct radeon_audio_funcs dce32_hdmi_funcs = {
Slava Grigorev3cdde022014-12-02 15:22:43 -0500149 .get_pin = r600_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500150 .write_sad_regs = dce3_2_afmt_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500151 .write_speaker_allocation = dce3_2_afmt_hdmi_write_speaker_allocation,
Slava Grigoreva85d6822014-12-05 13:38:31 -0500152 .set_dto = dce3_2_audio_set_dto,
Slava Grigorev64424d6e2014-12-06 20:19:16 -0500153 .update_acr = dce3_2_hdmi_update_acr,
Alex Deucher930a9782015-01-20 19:20:52 -0500154 .set_vbi_packet = r600_set_vbi_packet,
Alex Deucher070a2e62015-01-22 10:41:55 -0500155};
156
157static struct radeon_audio_funcs dce32_dp_funcs = {
Slava Grigorev3cdde022014-12-02 15:22:43 -0500158 .get_pin = r600_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500159 .write_sad_regs = dce3_2_afmt_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500160 .write_speaker_allocation = dce3_2_afmt_dp_write_speaker_allocation,
Slava Grigoreva85d6822014-12-05 13:38:31 -0500161 .set_dto = dce3_2_audio_set_dto,
Alex Deucher070a2e62015-01-22 10:41:55 -0500162};
163
164static struct radeon_audio_funcs dce4_hdmi_funcs = {
Slava Grigorev3cdde022014-12-02 15:22:43 -0500165 .get_pin = r600_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500166 .write_sad_regs = evergreen_hdmi_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500167 .write_speaker_allocation = dce4_afmt_hdmi_write_speaker_allocation,
Slava Grigorev87654f82014-12-02 11:20:48 -0500168 .write_latency_fields = dce4_afmt_write_latency_fields,
Slava Grigoreva85d6822014-12-05 13:38:31 -0500169 .set_dto = dce4_hdmi_audio_set_dto,
Slava Grigorev64424d6e2014-12-06 20:19:16 -0500170 .update_acr = evergreen_hdmi_update_acr,
Alex Deucher930a9782015-01-20 19:20:52 -0500171 .set_vbi_packet = dce4_set_vbi_packet,
Alex Deucher070a2e62015-01-22 10:41:55 -0500172};
173
174static struct radeon_audio_funcs dce4_dp_funcs = {
Slava Grigorev3cdde022014-12-02 15:22:43 -0500175 .get_pin = r600_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500176 .write_sad_regs = evergreen_hdmi_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500177 .write_speaker_allocation = dce4_afmt_dp_write_speaker_allocation,
Slava Grigorev87654f82014-12-02 11:20:48 -0500178 .write_latency_fields = dce4_afmt_write_latency_fields,
Slava Grigoreva85d6822014-12-05 13:38:31 -0500179 .set_dto = dce4_dp_audio_set_dto,
Alex Deucher070a2e62015-01-22 10:41:55 -0500180};
181
182static struct radeon_audio_funcs dce6_hdmi_funcs = {
Slava Grigorev88252d72014-12-02 17:27:29 -0500183 .select_pin = dce6_afmt_select_pin,
Slava Grigorev3cdde022014-12-02 15:22:43 -0500184 .get_pin = dce6_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500185 .write_sad_regs = dce6_afmt_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500186 .write_speaker_allocation = dce6_afmt_hdmi_write_speaker_allocation,
Slava Grigorev87654f82014-12-02 11:20:48 -0500187 .write_latency_fields = dce6_afmt_write_latency_fields,
Slava Grigoreva85d6822014-12-05 13:38:31 -0500188 .set_dto = dce6_hdmi_audio_set_dto,
Slava Grigorev64424d6e2014-12-06 20:19:16 -0500189 .update_acr = evergreen_hdmi_update_acr,
Alex Deucher930a9782015-01-20 19:20:52 -0500190 .set_vbi_packet = dce4_set_vbi_packet,
Alex Deucher070a2e62015-01-22 10:41:55 -0500191};
192
193static struct radeon_audio_funcs dce6_dp_funcs = {
Slava Grigorev88252d72014-12-02 17:27:29 -0500194 .select_pin = dce6_afmt_select_pin,
Slava Grigorev3cdde022014-12-02 15:22:43 -0500195 .get_pin = dce6_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500196 .write_sad_regs = dce6_afmt_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500197 .write_speaker_allocation = dce6_afmt_dp_write_speaker_allocation,
Slava Grigorev87654f82014-12-02 11:20:48 -0500198 .write_latency_fields = dce6_afmt_write_latency_fields,
Slava Grigoreva85d6822014-12-05 13:38:31 -0500199 .set_dto = dce6_dp_audio_set_dto,
Alex Deucher070a2e62015-01-22 10:41:55 -0500200};
201
Slava Grigorev1a626b62014-12-01 13:49:39 -0500202static void radeon_audio_interface_init(struct radeon_device *rdev)
203{
204 if (ASIC_IS_DCE6(rdev)) {
205 rdev->audio.funcs = &dce6_funcs;
Alex Deucher070a2e62015-01-22 10:41:55 -0500206 rdev->audio.hdmi_funcs = &dce6_hdmi_funcs;
207 rdev->audio.dp_funcs = &dce6_dp_funcs;
Slava Grigorev1a626b62014-12-01 13:49:39 -0500208 } else if (ASIC_IS_DCE4(rdev)) {
209 rdev->audio.funcs = &dce4_funcs;
Alex Deucher070a2e62015-01-22 10:41:55 -0500210 rdev->audio.hdmi_funcs = &dce4_hdmi_funcs;
211 rdev->audio.dp_funcs = &dce4_dp_funcs;
Slava Grigoreva85d6822014-12-05 13:38:31 -0500212 } else if (ASIC_IS_DCE32(rdev)) {
Slava Grigorev1a626b62014-12-01 13:49:39 -0500213 rdev->audio.funcs = &dce32_funcs;
Alex Deucher070a2e62015-01-22 10:41:55 -0500214 rdev->audio.hdmi_funcs = &dce32_hdmi_funcs;
215 rdev->audio.dp_funcs = &dce32_dp_funcs;
Slava Grigoreva85d6822014-12-05 13:38:31 -0500216 } else {
217 rdev->audio.funcs = &r600_funcs;
218 rdev->audio.hdmi_funcs = &r600_hdmi_funcs;
219 rdev->audio.dp_funcs = 0;
Slava Grigorev1a626b62014-12-01 13:49:39 -0500220 }
221}
222
Slava Grigorevbfc1f972014-12-22 17:26:51 -0500223static int radeon_audio_chipset_supported(struct radeon_device *rdev)
224{
225 return ASIC_IS_DCE2(rdev) && !ASIC_IS_NODCE(rdev);
226}
227
228int 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 Grigorev1a626b62014-12-01 13:49:39 -0500261 }
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 Grigorev8bf59822014-12-03 15:29:53 -0500267 radeon_audio_enable(rdev, &rdev->audio.pin[i], false);
Slava Grigorevbfc1f972014-12-22 17:26:51 -0500268
269 return 0;
270}
Slava Grigorev1a626b62014-12-01 13:49:39 -0500271
272void 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
304u32 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
312void 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 Deucher070a2e62015-01-22 10:41:55 -0500318
319void 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 Grigorev00a9d4b2014-12-01 18:02:57 -0500354
355void 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 Grigorev87654f82014-12-02 11:20:48 -0500389
390void 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 Grigorev3cdde022014-12-02 15:22:43 -0500415
416struct 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 Grigorev88252d72014-12-02 17:27:29 -0500426
427void 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 Grigorev8bf59822014-12-03 15:29:53 -0500434
435void 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 Grigorev7991d662014-12-03 17:07:01 -0500441
442void 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 Grigoreva85d6822014-12-05 13:38:31 -0500454
455void 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 Grigorev96ea7af2014-12-05 17:59:56 -0500464
465void 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 Grigorev64424d6e2014-12-06 20:19:16 -0500476
477/*
478 * calculate CTS and N values if they are not found in the table
479 */
480static 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
517static 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 */
553void 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 Deucher930a9782015-01-20 19:20:52 -0500565
566void 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}