radeon/audio: consolidate select_pin() functions
[deliverable/linux.git] / drivers / gpu / drm / radeon / radeon_audio.c
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
25 #include <drm/drmP.h>
26 #include <drm/drm_crtc.h>
27 #include "radeon.h"
28 #include "atom.h"
29 #include "radeon_audio.h"
30
31 void r600_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
32 u8 enable_mask);
33 void dce6_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
34 u8 enable_mask);
35 u32 dce6_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg);
36 void dce6_endpoint_wreg(struct radeon_device *rdev,
37 u32 offset, u32 reg, u32 v);
38 void dce3_2_afmt_write_sad_regs(struct drm_encoder *encoder,
39 struct cea_sad *sads, int sad_count);
40 void evergreen_hdmi_write_sad_regs(struct drm_encoder *encoder,
41 struct cea_sad *sads, int sad_count);
42 void dce6_afmt_write_sad_regs(struct drm_encoder *encoder,
43 struct cea_sad *sads, int sad_count);
44 void dce3_2_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
45 u8 *sadb, int sad_count);
46 void dce3_2_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
47 u8 *sadb, int sad_count);
48 void dce4_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
49 u8 *sadb, int sad_count);
50 void dce4_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
51 u8 *sadb, int sad_count);
52 void dce6_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
53 u8 *sadb, int sad_count);
54 void dce6_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
55 u8 *sadb, int sad_count);
56 void dce4_afmt_write_latency_fields(struct drm_encoder *encoder,
57 struct drm_connector *connector, struct drm_display_mode *mode);
58 void dce6_afmt_write_latency_fields(struct drm_encoder *encoder,
59 struct drm_connector *connector, struct drm_display_mode *mode);
60 struct r600_audio_pin* r600_audio_get_pin(struct radeon_device *rdev);
61 struct r600_audio_pin* dce6_audio_get_pin(struct radeon_device *rdev);
62 void dce6_afmt_select_pin(struct drm_encoder *encoder);
63
64 static const u32 pin_offsets[7] =
65 {
66 (0x5e00 - 0x5e00),
67 (0x5e18 - 0x5e00),
68 (0x5e30 - 0x5e00),
69 (0x5e48 - 0x5e00),
70 (0x5e60 - 0x5e00),
71 (0x5e78 - 0x5e00),
72 (0x5e90 - 0x5e00),
73 };
74
75 static u32 radeon_audio_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
76 {
77 return RREG32(reg);
78 }
79
80 static void radeon_audio_wreg(struct radeon_device *rdev, u32 offset,
81 u32 reg, u32 v)
82 {
83 WREG32(reg, v);
84 }
85
86 static struct radeon_audio_basic_funcs dce32_funcs = {
87 .endpoint_rreg = radeon_audio_rreg,
88 .endpoint_wreg = radeon_audio_wreg,
89 };
90
91 static struct radeon_audio_basic_funcs dce4_funcs = {
92 .endpoint_rreg = radeon_audio_rreg,
93 .endpoint_wreg = radeon_audio_wreg,
94 };
95
96 static struct radeon_audio_basic_funcs dce6_funcs = {
97 .endpoint_rreg = dce6_endpoint_rreg,
98 .endpoint_wreg = dce6_endpoint_wreg,
99 };
100
101 static struct radeon_audio_funcs dce32_hdmi_funcs = {
102 .get_pin = r600_audio_get_pin,
103 .write_sad_regs = dce3_2_afmt_write_sad_regs,
104 .write_speaker_allocation = dce3_2_afmt_hdmi_write_speaker_allocation,
105 };
106
107 static struct radeon_audio_funcs dce32_dp_funcs = {
108 .get_pin = r600_audio_get_pin,
109 .write_sad_regs = dce3_2_afmt_write_sad_regs,
110 .write_speaker_allocation = dce3_2_afmt_dp_write_speaker_allocation,
111 };
112
113 static struct radeon_audio_funcs dce4_hdmi_funcs = {
114 .get_pin = r600_audio_get_pin,
115 .write_sad_regs = evergreen_hdmi_write_sad_regs,
116 .write_speaker_allocation = dce4_afmt_hdmi_write_speaker_allocation,
117 .write_latency_fields = dce4_afmt_write_latency_fields,
118 };
119
120 static struct radeon_audio_funcs dce4_dp_funcs = {
121 .get_pin = r600_audio_get_pin,
122 .write_sad_regs = evergreen_hdmi_write_sad_regs,
123 .write_speaker_allocation = dce4_afmt_dp_write_speaker_allocation,
124 .write_latency_fields = dce4_afmt_write_latency_fields,
125 };
126
127 static struct radeon_audio_funcs dce6_hdmi_funcs = {
128 .select_pin = dce6_afmt_select_pin,
129 .get_pin = dce6_audio_get_pin,
130 .write_sad_regs = dce6_afmt_write_sad_regs,
131 .write_speaker_allocation = dce6_afmt_hdmi_write_speaker_allocation,
132 .write_latency_fields = dce6_afmt_write_latency_fields,
133 };
134
135 static struct radeon_audio_funcs dce6_dp_funcs = {
136 .select_pin = dce6_afmt_select_pin,
137 .get_pin = dce6_audio_get_pin,
138 .write_sad_regs = dce6_afmt_write_sad_regs,
139 .write_speaker_allocation = dce6_afmt_dp_write_speaker_allocation,
140 .write_latency_fields = dce6_afmt_write_latency_fields,
141 };
142
143 static void radeon_audio_interface_init(struct radeon_device *rdev)
144 {
145 if (ASIC_IS_DCE6(rdev)) {
146 rdev->audio.funcs = &dce6_funcs;
147 rdev->audio.hdmi_funcs = &dce6_hdmi_funcs;
148 rdev->audio.dp_funcs = &dce6_dp_funcs;
149 } else if (ASIC_IS_DCE4(rdev)) {
150 rdev->audio.funcs = &dce4_funcs;
151 rdev->audio.hdmi_funcs = &dce4_hdmi_funcs;
152 rdev->audio.dp_funcs = &dce4_dp_funcs;
153 } else {
154 rdev->audio.funcs = &dce32_funcs;
155 rdev->audio.hdmi_funcs = &dce32_hdmi_funcs;
156 rdev->audio.dp_funcs = &dce32_dp_funcs;
157 }
158 }
159
160 static int radeon_audio_chipset_supported(struct radeon_device *rdev)
161 {
162 return ASIC_IS_DCE2(rdev) && !ASIC_IS_NODCE(rdev);
163 }
164
165 int radeon_audio_init(struct radeon_device *rdev)
166 {
167 int i;
168
169 if (!radeon_audio || !radeon_audio_chipset_supported(rdev))
170 return 0;
171
172 rdev->audio.enabled = true;
173
174 if (ASIC_IS_DCE83(rdev)) /* KB: 2 streams, 3 endpoints */
175 rdev->audio.num_pins = 3;
176 else if (ASIC_IS_DCE81(rdev)) /* KV: 4 streams, 7 endpoints */
177 rdev->audio.num_pins = 7;
178 else if (ASIC_IS_DCE8(rdev)) /* BN/HW: 6 streams, 7 endpoints */
179 rdev->audio.num_pins = 7;
180 else if (ASIC_IS_DCE64(rdev)) /* OL: 2 streams, 2 endpoints */
181 rdev->audio.num_pins = 2;
182 else if (ASIC_IS_DCE61(rdev)) /* TN: 4 streams, 6 endpoints */
183 rdev->audio.num_pins = 6;
184 else if (ASIC_IS_DCE6(rdev)) /* SI: 6 streams, 6 endpoints */
185 rdev->audio.num_pins = 6;
186 else
187 rdev->audio.num_pins = 1;
188
189 for (i = 0; i < rdev->audio.num_pins; i++) {
190 rdev->audio.pin[i].channels = -1;
191 rdev->audio.pin[i].rate = -1;
192 rdev->audio.pin[i].bits_per_sample = -1;
193 rdev->audio.pin[i].status_bits = 0;
194 rdev->audio.pin[i].category_code = 0;
195 rdev->audio.pin[i].connected = false;
196 rdev->audio.pin[i].offset = pin_offsets[i];
197 rdev->audio.pin[i].id = i;
198 }
199
200 radeon_audio_interface_init(rdev);
201
202 /* disable audio. it will be set up later */
203 for (i = 0; i < rdev->audio.num_pins; i++)
204 if (ASIC_IS_DCE6(rdev))
205 dce6_audio_enable(rdev, &rdev->audio.pin[i], false);
206 else
207 r600_audio_enable(rdev, &rdev->audio.pin[i], false);
208
209 return 0;
210 }
211
212 void radeon_audio_detect(struct drm_connector *connector,
213 enum drm_connector_status status)
214 {
215 if (!connector || !connector->encoder)
216 return;
217
218 if (status == connector_status_connected) {
219 int sink_type;
220 struct radeon_device *rdev = connector->encoder->dev->dev_private;
221 struct radeon_connector *radeon_connector;
222 struct radeon_encoder *radeon_encoder =
223 to_radeon_encoder(connector->encoder);
224
225 if (!drm_detect_monitor_audio(radeon_connector_edid(connector))) {
226 radeon_encoder->audio = 0;
227 return;
228 }
229
230 radeon_connector = to_radeon_connector(connector);
231 sink_type = radeon_dp_getsinktype(radeon_connector);
232
233 if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort &&
234 sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT)
235 radeon_encoder->audio = rdev->audio.dp_funcs;
236 else
237 radeon_encoder->audio = rdev->audio.hdmi_funcs;
238 /* TODO: set up the sads, etc. and set the audio enable_mask */
239 } else {
240 /* TODO: reset the audio enable_mask */
241 }
242 }
243
244 u32 radeon_audio_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
245 {
246 if (rdev->audio.funcs->endpoint_rreg)
247 return rdev->audio.funcs->endpoint_rreg(rdev, offset, reg);
248
249 return 0;
250 }
251
252 void radeon_audio_endpoint_wreg(struct radeon_device *rdev, u32 offset,
253 u32 reg, u32 v)
254 {
255 if (rdev->audio.funcs->endpoint_wreg)
256 rdev->audio.funcs->endpoint_wreg(rdev, offset, reg, v);
257 }
258
259 void radeon_audio_write_sad_regs(struct drm_encoder *encoder)
260 {
261 struct radeon_encoder *radeon_encoder;
262 struct drm_connector *connector;
263 struct radeon_connector *radeon_connector = NULL;
264 struct cea_sad *sads;
265 int sad_count;
266
267 list_for_each_entry(connector,
268 &encoder->dev->mode_config.connector_list, head) {
269 if (connector->encoder == encoder) {
270 radeon_connector = to_radeon_connector(connector);
271 break;
272 }
273 }
274
275 if (!radeon_connector) {
276 DRM_ERROR("Couldn't find encoder's connector\n");
277 return;
278 }
279
280 sad_count = drm_edid_to_sad(radeon_connector_edid(connector), &sads);
281 if (sad_count <= 0) {
282 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
283 return;
284 }
285 BUG_ON(!sads);
286
287 radeon_encoder = to_radeon_encoder(encoder);
288
289 if (radeon_encoder->audio && radeon_encoder->audio->write_sad_regs)
290 radeon_encoder->audio->write_sad_regs(encoder, sads, sad_count);
291
292 kfree(sads);
293 }
294
295 void radeon_audio_write_speaker_allocation(struct drm_encoder *encoder)
296 {
297 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
298 struct drm_connector *connector;
299 struct radeon_connector *radeon_connector = NULL;
300 u8 *sadb = NULL;
301 int sad_count;
302
303 list_for_each_entry(connector,
304 &encoder->dev->mode_config.connector_list, head) {
305 if (connector->encoder == encoder) {
306 radeon_connector = to_radeon_connector(connector);
307 break;
308 }
309 }
310
311 if (!radeon_connector) {
312 DRM_ERROR("Couldn't find encoder's connector\n");
313 return;
314 }
315
316 sad_count = drm_edid_to_speaker_allocation(
317 radeon_connector_edid(connector), &sadb);
318 if (sad_count < 0) {
319 DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n",
320 sad_count);
321 sad_count = 0;
322 }
323
324 if (radeon_encoder->audio && radeon_encoder->audio->write_speaker_allocation)
325 radeon_encoder->audio->write_speaker_allocation(encoder, sadb, sad_count);
326
327 kfree(sadb);
328 }
329
330 void radeon_audio_write_latency_fields(struct drm_encoder *encoder,
331 struct drm_display_mode *mode)
332 {
333 struct radeon_encoder *radeon_encoder;
334 struct drm_connector *connector;
335 struct radeon_connector *radeon_connector = 0;
336
337 list_for_each_entry(connector,
338 &encoder->dev->mode_config.connector_list, head) {
339 if (connector->encoder == encoder) {
340 radeon_connector = to_radeon_connector(connector);
341 break;
342 }
343 }
344
345 if (!radeon_connector) {
346 DRM_ERROR("Couldn't find encoder's connector\n");
347 return;
348 }
349
350 radeon_encoder = to_radeon_encoder(encoder);
351
352 if (radeon_encoder->audio && radeon_encoder->audio->write_latency_fields)
353 radeon_encoder->audio->write_latency_fields(encoder, connector, mode);
354 }
355
356 struct r600_audio_pin* radeon_audio_get_pin(struct drm_encoder *encoder)
357 {
358 struct radeon_device *rdev = encoder->dev->dev_private;
359 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
360
361 if (radeon_encoder->audio && radeon_encoder->audio->get_pin)
362 return radeon_encoder->audio->get_pin(rdev);
363
364 return NULL;
365 }
366
367 void radeon_audio_select_pin(struct drm_encoder *encoder)
368 {
369 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
370
371 if (radeon_encoder->audio && radeon_encoder->audio->select_pin)
372 radeon_encoder->audio->select_pin(encoder);
373 }
This page took 0.042442 seconds and 6 git commands to generate.