Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / drm / radeon / r600_audio.c
1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  * Copyright 2009 Christian König.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Christian König
25  *
26  * $FreeBSD: head/sys/dev/drm2/radeon/r600_audio.c 254885 2013-08-25 19:37:15Z dumbbell $
27  */
28
29 #include <drm/drmP.h>
30 #include "radeon.h"
31 #include "radeon_reg.h"
32 #include "radeon_asic.h"
33 #include "atom.h"
34
35 /*
36  * check if enc_priv stores radeon_encoder_atom_dig
37  */
38 static bool radeon_dig_encoder(struct drm_encoder *encoder)
39 {
40         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
41         switch (radeon_encoder->encoder_id) {
42         case ENCODER_OBJECT_ID_INTERNAL_LVDS:
43         case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
44         case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
45         case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
46         case ENCODER_OBJECT_ID_INTERNAL_DVO1:
47         case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
48         case ENCODER_OBJECT_ID_INTERNAL_DDI:
49         case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
50         case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
51         case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
52         case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
53                 return true;
54         }
55         return false;
56 }
57
58 /*
59  * check if the chipset is supported
60  */
61 static int r600_audio_chipset_supported(struct radeon_device *rdev)
62 {
63         return (rdev->family >= CHIP_R600 && !ASIC_IS_DCE6(rdev))
64                 || rdev->family == CHIP_RS600
65                 || rdev->family == CHIP_RS690
66                 || rdev->family == CHIP_RS740;
67 }
68
69 struct r600_audio r600_audio_status(struct radeon_device *rdev)
70 {
71         struct r600_audio status;
72         uint32_t value;
73
74         value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);
75
76         /* number of channels */
77         status.channels = (value & 0x7) + 1;
78
79         /* bits per sample */
80         switch ((value & 0xF0) >> 4) {
81         case 0x0:
82                 status.bits_per_sample = 8;
83                 break;
84         case 0x1:
85                 status.bits_per_sample = 16;
86                 break;
87         case 0x2:
88                 status.bits_per_sample = 20;
89                 break;
90         case 0x3:
91                 status.bits_per_sample = 24;
92                 break;
93         case 0x4:
94                 status.bits_per_sample = 32;
95                 break;
96         default:
97                 dev_err(rdev->dev, "Unknown bits per sample 0x%x, using 16\n",
98                         (int)value);
99                 status.bits_per_sample = 16;
100         }
101
102         /* current sampling rate in HZ */
103         if (value & 0x4000)
104                 status.rate = 44100;
105         else
106                 status.rate = 48000;
107         status.rate *= ((value >> 11) & 0x7) + 1;
108         status.rate /= ((value >> 8) & 0x7) + 1;
109
110         value = RREG32(R600_AUDIO_STATUS_BITS);
111
112         /* iec 60958 status bits */
113         status.status_bits = value & 0xff;
114
115         /* iec 60958 category code */
116         status.category_code = (value >> 8) & 0xff;
117
118         return status;
119 }
120
121 /*
122  * update all hdmi interfaces with current audio parameters
123  */
124 void r600_audio_update_hdmi(void *arg, int pending)
125 {
126         struct radeon_device *rdev = arg;
127         struct drm_device *dev = rdev->ddev;
128         struct r600_audio audio_status = r600_audio_status(rdev);
129         struct drm_encoder *encoder;
130         bool changed = false;
131
132         if (rdev->audio_status.channels != audio_status.channels ||
133             rdev->audio_status.rate != audio_status.rate ||
134             rdev->audio_status.bits_per_sample != audio_status.bits_per_sample ||
135             rdev->audio_status.status_bits != audio_status.status_bits ||
136             rdev->audio_status.category_code != audio_status.category_code) {
137                 rdev->audio_status = audio_status;
138                 changed = true;
139         }
140
141         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
142                 if (!radeon_dig_encoder(encoder))
143                         continue;
144                 if (changed || r600_hdmi_buffer_status_changed(encoder))
145                         r600_hdmi_update_audio_settings(encoder);
146         }
147 }
148
149 /*
150  * turn on/off audio engine
151  */
152 static void r600_audio_engine_enable(struct radeon_device *rdev, bool enable)
153 {
154         u32 value = 0;
155         DRM_INFO("%s audio support\n", enable ? "Enabling" : "Disabling");
156         if (ASIC_IS_DCE4(rdev)) {
157                 if (enable) {
158                         value |= 0x81000000; /* Required to enable audio */
159                         value |= 0x0e1000f0; /* fglrx sets that too */
160                 }
161                 WREG32(EVERGREEN_AUDIO_ENABLE, value);
162         } else {
163                 WREG32_P(R600_AUDIO_ENABLE,
164                          enable ? 0x81000000 : 0x0, ~0x81000000);
165         }
166         rdev->audio_enabled = enable;
167 }
168
169 /*
170  * initialize the audio vars
171  */
172 int r600_audio_init(struct radeon_device *rdev)
173 {
174         if (!radeon_audio || !r600_audio_chipset_supported(rdev))
175                 return 0;
176
177         r600_audio_engine_enable(rdev, true);
178
179         rdev->audio_status.channels = -1;
180         rdev->audio_status.rate = -1;
181         rdev->audio_status.bits_per_sample = -1;
182         rdev->audio_status.status_bits = 0;
183         rdev->audio_status.category_code = 0;
184
185         return 0;
186 }
187
188 /*
189  * atach the audio codec to the clock source of the encoder
190  */
191 void r600_audio_set_clock(struct drm_encoder *encoder, int clock)
192 {
193         struct drm_device *dev = encoder->dev;
194         struct radeon_device *rdev = dev->dev_private;
195         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
196         struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
197         struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
198         int base_rate = 48000;
199
200         switch (radeon_encoder->encoder_id) {
201         case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
202         case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
203                 WREG32_P(R600_AUDIO_TIMING, 0, ~0x301);
204                 break;
205         case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
206         case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
207         case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
208         case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
209                 WREG32_P(R600_AUDIO_TIMING, 0x100, ~0x301);
210                 break;
211         default:
212                 dev_err(rdev->dev, "Unsupported encoder type 0x%02X\n",
213                           radeon_encoder->encoder_id);
214                 return;
215         }
216
217         if (ASIC_IS_DCE4(rdev)) {
218                 /* TODO: other PLLs? */
219                 WREG32(EVERGREEN_AUDIO_PLL1_MUL, base_rate * 10);
220                 WREG32(EVERGREEN_AUDIO_PLL1_DIV, clock * 10);
221                 WREG32(EVERGREEN_AUDIO_PLL1_UNK, 0x00000071);
222
223                 /* Select DTO source */
224                 WREG32(0x5ac, radeon_crtc->crtc_id);
225         } else {
226                 switch (dig->dig_encoder) {
227                 case 0:
228                         WREG32(R600_AUDIO_PLL1_MUL, base_rate * 50);
229                         WREG32(R600_AUDIO_PLL1_DIV, clock * 100);
230                         WREG32(R600_AUDIO_CLK_SRCSEL, 0);
231                         break;
232
233                 case 1:
234                         WREG32(R600_AUDIO_PLL2_MUL, base_rate * 50);
235                         WREG32(R600_AUDIO_PLL2_DIV, clock * 100);
236                         WREG32(R600_AUDIO_CLK_SRCSEL, 1);
237                         break;
238                 default:
239                         dev_err(rdev->dev,
240                                 "Unsupported DIG on encoder 0x%02X\n",
241                                 radeon_encoder->encoder_id);
242                         return;
243                 }
244         }
245 }
246
247 /*
248  * release the audio timer
249  * TODO: How to do this correctly on SMP systems?
250  */
251 void r600_audio_fini(struct radeon_device *rdev)
252 {
253         if (!rdev->audio_enabled)
254                 return;
255
256         r600_audio_engine_enable(rdev, false);
257 }