blob: e6c73f8206b218ed5edb59bb3cc039597f9fb657 [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
25#include <drm/drmP.h>
Slava Grigorev1a626b62014-12-01 13:49:39 -050026#include <drm/drm_crtc.h>
Slava Grigorevbfc1f972014-12-22 17:26:51 -050027#include "radeon.h"
Slava Grigorev1a626b62014-12-01 13:49:39 -050028#include "atom.h"
29#include "radeon_audio.h"
Slava Grigorevbfc1f972014-12-22 17:26:51 -050030
31void r600_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
32 u8 enable_mask);
Slava Grigorev8bf59822014-12-03 15:29:53 -050033void dce4_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
34 u8 enable_mask);
Slava Grigorevbfc1f972014-12-22 17:26:51 -050035void dce6_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
36 u8 enable_mask);
Slava Grigorev1a626b62014-12-01 13:49:39 -050037u32 dce6_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg);
38void dce6_endpoint_wreg(struct radeon_device *rdev,
39 u32 offset, u32 reg, u32 v);
Alex Deucher070a2e62015-01-22 10:41:55 -050040void dce3_2_afmt_write_sad_regs(struct drm_encoder *encoder,
41 struct cea_sad *sads, int sad_count);
42void evergreen_hdmi_write_sad_regs(struct drm_encoder *encoder,
43 struct cea_sad *sads, int sad_count);
44void dce6_afmt_write_sad_regs(struct drm_encoder *encoder,
45 struct cea_sad *sads, int sad_count);
Slava Grigorev00a9d4b2014-12-01 18:02:57 -050046void dce3_2_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
47 u8 *sadb, int sad_count);
48void dce3_2_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
49 u8 *sadb, int sad_count);
50void dce4_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
51 u8 *sadb, int sad_count);
52void dce4_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
53 u8 *sadb, int sad_count);
54void dce6_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
55 u8 *sadb, int sad_count);
56void dce6_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
57 u8 *sadb, int sad_count);
Slava Grigorev87654f82014-12-02 11:20:48 -050058void dce4_afmt_write_latency_fields(struct drm_encoder *encoder,
59 struct drm_connector *connector, struct drm_display_mode *mode);
60void dce6_afmt_write_latency_fields(struct drm_encoder *encoder,
61 struct drm_connector *connector, struct drm_display_mode *mode);
Slava Grigorev3cdde022014-12-02 15:22:43 -050062struct r600_audio_pin* r600_audio_get_pin(struct radeon_device *rdev);
63struct r600_audio_pin* dce6_audio_get_pin(struct radeon_device *rdev);
Slava Grigorev88252d72014-12-02 17:27:29 -050064void dce6_afmt_select_pin(struct drm_encoder *encoder);
Slava Grigorevbfc1f972014-12-22 17:26:51 -050065
66static const u32 pin_offsets[7] =
67{
68 (0x5e00 - 0x5e00),
69 (0x5e18 - 0x5e00),
70 (0x5e30 - 0x5e00),
71 (0x5e48 - 0x5e00),
72 (0x5e60 - 0x5e00),
73 (0x5e78 - 0x5e00),
74 (0x5e90 - 0x5e00),
75};
76
Slava Grigorev1a626b62014-12-01 13:49:39 -050077static u32 radeon_audio_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
78{
79 return RREG32(reg);
80}
81
82static void radeon_audio_wreg(struct radeon_device *rdev, u32 offset,
83 u32 reg, u32 v)
84{
85 WREG32(reg, v);
86}
87
88static struct radeon_audio_basic_funcs dce32_funcs = {
89 .endpoint_rreg = radeon_audio_rreg,
90 .endpoint_wreg = radeon_audio_wreg,
Slava Grigorev8bf59822014-12-03 15:29:53 -050091 .enable = r600_audio_enable,
Slava Grigorev1a626b62014-12-01 13:49:39 -050092};
93
94static struct radeon_audio_basic_funcs dce4_funcs = {
95 .endpoint_rreg = radeon_audio_rreg,
96 .endpoint_wreg = radeon_audio_wreg,
Slava Grigorev8bf59822014-12-03 15:29:53 -050097 .enable = dce4_audio_enable,
Slava Grigorev1a626b62014-12-01 13:49:39 -050098};
99
100static struct radeon_audio_basic_funcs dce6_funcs = {
101 .endpoint_rreg = dce6_endpoint_rreg,
102 .endpoint_wreg = dce6_endpoint_wreg,
Slava Grigorev8bf59822014-12-03 15:29:53 -0500103 .enable = dce6_audio_enable,
Slava Grigorev1a626b62014-12-01 13:49:39 -0500104};
105
Alex Deucher070a2e62015-01-22 10:41:55 -0500106static struct radeon_audio_funcs dce32_hdmi_funcs = {
Slava Grigorev3cdde022014-12-02 15:22:43 -0500107 .get_pin = r600_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500108 .write_sad_regs = dce3_2_afmt_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500109 .write_speaker_allocation = dce3_2_afmt_hdmi_write_speaker_allocation,
Alex Deucher070a2e62015-01-22 10:41:55 -0500110};
111
112static struct radeon_audio_funcs dce32_dp_funcs = {
Slava Grigorev3cdde022014-12-02 15:22:43 -0500113 .get_pin = r600_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500114 .write_sad_regs = dce3_2_afmt_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500115 .write_speaker_allocation = dce3_2_afmt_dp_write_speaker_allocation,
Alex Deucher070a2e62015-01-22 10:41:55 -0500116};
117
118static struct radeon_audio_funcs dce4_hdmi_funcs = {
Slava Grigorev3cdde022014-12-02 15:22:43 -0500119 .get_pin = r600_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500120 .write_sad_regs = evergreen_hdmi_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500121 .write_speaker_allocation = dce4_afmt_hdmi_write_speaker_allocation,
Slava Grigorev87654f82014-12-02 11:20:48 -0500122 .write_latency_fields = dce4_afmt_write_latency_fields,
Alex Deucher070a2e62015-01-22 10:41:55 -0500123};
124
125static struct radeon_audio_funcs dce4_dp_funcs = {
Slava Grigorev3cdde022014-12-02 15:22:43 -0500126 .get_pin = r600_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500127 .write_sad_regs = evergreen_hdmi_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500128 .write_speaker_allocation = dce4_afmt_dp_write_speaker_allocation,
Slava Grigorev87654f82014-12-02 11:20:48 -0500129 .write_latency_fields = dce4_afmt_write_latency_fields,
Alex Deucher070a2e62015-01-22 10:41:55 -0500130};
131
132static struct radeon_audio_funcs dce6_hdmi_funcs = {
Slava Grigorev88252d72014-12-02 17:27:29 -0500133 .select_pin = dce6_afmt_select_pin,
Slava Grigorev3cdde022014-12-02 15:22:43 -0500134 .get_pin = dce6_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500135 .write_sad_regs = dce6_afmt_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500136 .write_speaker_allocation = dce6_afmt_hdmi_write_speaker_allocation,
Slava Grigorev87654f82014-12-02 11:20:48 -0500137 .write_latency_fields = dce6_afmt_write_latency_fields,
Alex Deucher070a2e62015-01-22 10:41:55 -0500138};
139
140static struct radeon_audio_funcs dce6_dp_funcs = {
Slava Grigorev88252d72014-12-02 17:27:29 -0500141 .select_pin = dce6_afmt_select_pin,
Slava Grigorev3cdde022014-12-02 15:22:43 -0500142 .get_pin = dce6_audio_get_pin,
Alex Deucher070a2e62015-01-22 10:41:55 -0500143 .write_sad_regs = dce6_afmt_write_sad_regs,
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500144 .write_speaker_allocation = dce6_afmt_dp_write_speaker_allocation,
Slava Grigorev87654f82014-12-02 11:20:48 -0500145 .write_latency_fields = dce6_afmt_write_latency_fields,
Alex Deucher070a2e62015-01-22 10:41:55 -0500146};
147
Slava Grigorev1a626b62014-12-01 13:49:39 -0500148static void radeon_audio_interface_init(struct radeon_device *rdev)
149{
150 if (ASIC_IS_DCE6(rdev)) {
151 rdev->audio.funcs = &dce6_funcs;
Alex Deucher070a2e62015-01-22 10:41:55 -0500152 rdev->audio.hdmi_funcs = &dce6_hdmi_funcs;
153 rdev->audio.dp_funcs = &dce6_dp_funcs;
Slava Grigorev1a626b62014-12-01 13:49:39 -0500154 } else if (ASIC_IS_DCE4(rdev)) {
155 rdev->audio.funcs = &dce4_funcs;
Alex Deucher070a2e62015-01-22 10:41:55 -0500156 rdev->audio.hdmi_funcs = &dce4_hdmi_funcs;
157 rdev->audio.dp_funcs = &dce4_dp_funcs;
Slava Grigorev1a626b62014-12-01 13:49:39 -0500158 } else {
159 rdev->audio.funcs = &dce32_funcs;
Alex Deucher070a2e62015-01-22 10:41:55 -0500160 rdev->audio.hdmi_funcs = &dce32_hdmi_funcs;
161 rdev->audio.dp_funcs = &dce32_dp_funcs;
Slava Grigorev1a626b62014-12-01 13:49:39 -0500162 }
163}
164
Slava Grigorevbfc1f972014-12-22 17:26:51 -0500165static int radeon_audio_chipset_supported(struct radeon_device *rdev)
166{
167 return ASIC_IS_DCE2(rdev) && !ASIC_IS_NODCE(rdev);
168}
169
170int radeon_audio_init(struct radeon_device *rdev)
171{
172 int i;
173
174 if (!radeon_audio || !radeon_audio_chipset_supported(rdev))
175 return 0;
176
177 rdev->audio.enabled = true;
178
179 if (ASIC_IS_DCE83(rdev)) /* KB: 2 streams, 3 endpoints */
180 rdev->audio.num_pins = 3;
181 else if (ASIC_IS_DCE81(rdev)) /* KV: 4 streams, 7 endpoints */
182 rdev->audio.num_pins = 7;
183 else if (ASIC_IS_DCE8(rdev)) /* BN/HW: 6 streams, 7 endpoints */
184 rdev->audio.num_pins = 7;
185 else if (ASIC_IS_DCE64(rdev)) /* OL: 2 streams, 2 endpoints */
186 rdev->audio.num_pins = 2;
187 else if (ASIC_IS_DCE61(rdev)) /* TN: 4 streams, 6 endpoints */
188 rdev->audio.num_pins = 6;
189 else if (ASIC_IS_DCE6(rdev)) /* SI: 6 streams, 6 endpoints */
190 rdev->audio.num_pins = 6;
191 else
192 rdev->audio.num_pins = 1;
193
194 for (i = 0; i < rdev->audio.num_pins; i++) {
195 rdev->audio.pin[i].channels = -1;
196 rdev->audio.pin[i].rate = -1;
197 rdev->audio.pin[i].bits_per_sample = -1;
198 rdev->audio.pin[i].status_bits = 0;
199 rdev->audio.pin[i].category_code = 0;
200 rdev->audio.pin[i].connected = false;
201 rdev->audio.pin[i].offset = pin_offsets[i];
202 rdev->audio.pin[i].id = i;
Slava Grigorev1a626b62014-12-01 13:49:39 -0500203 }
204
205 radeon_audio_interface_init(rdev);
206
207 /* disable audio. it will be set up later */
208 for (i = 0; i < rdev->audio.num_pins; i++)
Slava Grigorev8bf59822014-12-03 15:29:53 -0500209 radeon_audio_enable(rdev, &rdev->audio.pin[i], false);
Slava Grigorevbfc1f972014-12-22 17:26:51 -0500210
211 return 0;
212}
Slava Grigorev1a626b62014-12-01 13:49:39 -0500213
214void radeon_audio_detect(struct drm_connector *connector,
215 enum drm_connector_status status)
216{
217 if (!connector || !connector->encoder)
218 return;
219
220 if (status == connector_status_connected) {
221 int sink_type;
222 struct radeon_device *rdev = connector->encoder->dev->dev_private;
223 struct radeon_connector *radeon_connector;
224 struct radeon_encoder *radeon_encoder =
225 to_radeon_encoder(connector->encoder);
226
227 if (!drm_detect_monitor_audio(radeon_connector_edid(connector))) {
228 radeon_encoder->audio = 0;
229 return;
230 }
231
232 radeon_connector = to_radeon_connector(connector);
233 sink_type = radeon_dp_getsinktype(radeon_connector);
234
235 if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort &&
236 sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT)
237 radeon_encoder->audio = rdev->audio.dp_funcs;
238 else
239 radeon_encoder->audio = rdev->audio.hdmi_funcs;
240 /* TODO: set up the sads, etc. and set the audio enable_mask */
241 } else {
242 /* TODO: reset the audio enable_mask */
243 }
244}
245
246u32 radeon_audio_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
247{
248 if (rdev->audio.funcs->endpoint_rreg)
249 return rdev->audio.funcs->endpoint_rreg(rdev, offset, reg);
250
251 return 0;
252}
253
254void radeon_audio_endpoint_wreg(struct radeon_device *rdev, u32 offset,
255 u32 reg, u32 v)
256{
257 if (rdev->audio.funcs->endpoint_wreg)
258 rdev->audio.funcs->endpoint_wreg(rdev, offset, reg, v);
259}
Alex Deucher070a2e62015-01-22 10:41:55 -0500260
261void radeon_audio_write_sad_regs(struct drm_encoder *encoder)
262{
263 struct radeon_encoder *radeon_encoder;
264 struct drm_connector *connector;
265 struct radeon_connector *radeon_connector = NULL;
266 struct cea_sad *sads;
267 int sad_count;
268
269 list_for_each_entry(connector,
270 &encoder->dev->mode_config.connector_list, head) {
271 if (connector->encoder == encoder) {
272 radeon_connector = to_radeon_connector(connector);
273 break;
274 }
275 }
276
277 if (!radeon_connector) {
278 DRM_ERROR("Couldn't find encoder's connector\n");
279 return;
280 }
281
282 sad_count = drm_edid_to_sad(radeon_connector_edid(connector), &sads);
283 if (sad_count <= 0) {
284 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
285 return;
286 }
287 BUG_ON(!sads);
288
289 radeon_encoder = to_radeon_encoder(encoder);
290
291 if (radeon_encoder->audio && radeon_encoder->audio->write_sad_regs)
292 radeon_encoder->audio->write_sad_regs(encoder, sads, sad_count);
293
294 kfree(sads);
295}
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500296
297void radeon_audio_write_speaker_allocation(struct drm_encoder *encoder)
298{
299 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
300 struct drm_connector *connector;
301 struct radeon_connector *radeon_connector = NULL;
302 u8 *sadb = NULL;
303 int sad_count;
304
305 list_for_each_entry(connector,
306 &encoder->dev->mode_config.connector_list, head) {
307 if (connector->encoder == encoder) {
308 radeon_connector = to_radeon_connector(connector);
309 break;
310 }
311 }
312
313 if (!radeon_connector) {
314 DRM_ERROR("Couldn't find encoder's connector\n");
315 return;
316 }
317
318 sad_count = drm_edid_to_speaker_allocation(
319 radeon_connector_edid(connector), &sadb);
320 if (sad_count < 0) {
321 DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n",
322 sad_count);
323 sad_count = 0;
324 }
325
326 if (radeon_encoder->audio && radeon_encoder->audio->write_speaker_allocation)
327 radeon_encoder->audio->write_speaker_allocation(encoder, sadb, sad_count);
328
329 kfree(sadb);
330}
Slava Grigorev87654f82014-12-02 11:20:48 -0500331
332void radeon_audio_write_latency_fields(struct drm_encoder *encoder,
333 struct drm_display_mode *mode)
334{
335 struct radeon_encoder *radeon_encoder;
336 struct drm_connector *connector;
337 struct radeon_connector *radeon_connector = 0;
338
339 list_for_each_entry(connector,
340 &encoder->dev->mode_config.connector_list, head) {
341 if (connector->encoder == encoder) {
342 radeon_connector = to_radeon_connector(connector);
343 break;
344 }
345 }
346
347 if (!radeon_connector) {
348 DRM_ERROR("Couldn't find encoder's connector\n");
349 return;
350 }
351
352 radeon_encoder = to_radeon_encoder(encoder);
353
354 if (radeon_encoder->audio && radeon_encoder->audio->write_latency_fields)
355 radeon_encoder->audio->write_latency_fields(encoder, connector, mode);
356}
Slava Grigorev3cdde022014-12-02 15:22:43 -0500357
358struct r600_audio_pin* radeon_audio_get_pin(struct drm_encoder *encoder)
359{
360 struct radeon_device *rdev = encoder->dev->dev_private;
361 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
362
363 if (radeon_encoder->audio && radeon_encoder->audio->get_pin)
364 return radeon_encoder->audio->get_pin(rdev);
365
366 return NULL;
367}
Slava Grigorev88252d72014-12-02 17:27:29 -0500368
369void radeon_audio_select_pin(struct drm_encoder *encoder)
370{
371 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
372
373 if (radeon_encoder->audio && radeon_encoder->audio->select_pin)
374 radeon_encoder->audio->select_pin(encoder);
375}
Slava Grigorev8bf59822014-12-03 15:29:53 -0500376
377void radeon_audio_enable(struct radeon_device *rdev,
378 struct r600_audio_pin *pin, u8 enable_mask)
379{
380 if (rdev->audio.funcs->enable)
381 rdev->audio.funcs->enable(rdev, pin, enable_mask);
382}
Slava Grigorev7991d662014-12-03 17:07:01 -0500383
384void radeon_audio_fini(struct radeon_device *rdev)
385{
386 int i;
387
388 if (!rdev->audio.enabled)
389 return;
390
391 for (i = 0; i < rdev->audio.num_pins; i++)
392 radeon_audio_enable(rdev, &rdev->audio.pin[i], false);
393
394 rdev->audio.enabled = false;
395}