blob: ed8d3989f172a6d037ae3bd2c1f6c7663244bbb2 [file] [log] [blame]
Christian Koenigdafc3bd2009-10-11 23:49:13 +02001/*
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#include "drmP.h"
27#include "radeon.h"
28#include "radeon_reg.h"
29#include "atom.h"
30
31#define AUDIO_TIMER_INTERVALL 100 /* 1/10 sekund should be enough */
32
33/*
34 * check if the chipset is supported
35 */
36static int r600_audio_chipset_supported(struct radeon_device *rdev)
37{
Alex Deucher16823d12010-04-16 11:35:30 -040038 return (rdev->family >= CHIP_R600 && rdev->family < CHIP_CEDAR)
Christian Koenigdafc3bd2009-10-11 23:49:13 +020039 || rdev->family == CHIP_RS600
40 || rdev->family == CHIP_RS690
41 || rdev->family == CHIP_RS740;
42}
43
44/*
45 * current number of channels
46 */
Christian König58bd0862010-04-05 22:14:55 +020047int r600_audio_channels(struct radeon_device *rdev)
Christian Koenigdafc3bd2009-10-11 23:49:13 +020048{
49 return (RREG32(R600_AUDIO_RATE_BPS_CHANNEL) & 0x7) + 1;
50}
51
52/*
53 * current bits per sample
54 */
Christian König58bd0862010-04-05 22:14:55 +020055int r600_audio_bits_per_sample(struct radeon_device *rdev)
Christian Koenigdafc3bd2009-10-11 23:49:13 +020056{
57 uint32_t value = (RREG32(R600_AUDIO_RATE_BPS_CHANNEL) & 0xF0) >> 4;
58 switch (value) {
59 case 0x0: return 8;
60 case 0x1: return 16;
61 case 0x2: return 20;
62 case 0x3: return 24;
63 case 0x4: return 32;
64 }
65
66 DRM_ERROR("Unknown bits per sample 0x%x using 16 instead.\n", (int)value);
67
68 return 16;
69}
70
71/*
72 * current sampling rate in HZ
73 */
Christian König58bd0862010-04-05 22:14:55 +020074int r600_audio_rate(struct radeon_device *rdev)
Christian Koenigdafc3bd2009-10-11 23:49:13 +020075{
76 uint32_t value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);
77 uint32_t result;
78
79 if (value & 0x4000)
80 result = 44100;
81 else
82 result = 48000;
83
84 result *= ((value >> 11) & 0x7) + 1;
85 result /= ((value >> 8) & 0x7) + 1;
86
87 return result;
88}
89
90/*
91 * iec 60958 status bits
92 */
Christian König58bd0862010-04-05 22:14:55 +020093uint8_t r600_audio_status_bits(struct radeon_device *rdev)
Christian Koenigdafc3bd2009-10-11 23:49:13 +020094{
95 return RREG32(R600_AUDIO_STATUS_BITS) & 0xff;
96}
97
98/*
99 * iec 60958 category code
100 */
Christian König58bd0862010-04-05 22:14:55 +0200101uint8_t r600_audio_category_code(struct radeon_device *rdev)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200102{
103 return (RREG32(R600_AUDIO_STATUS_BITS) >> 8) & 0xff;
104}
105
106/*
107 * update all hdmi interfaces with current audio parameters
108 */
109static void r600_audio_update_hdmi(unsigned long param)
110{
111 struct radeon_device *rdev = (struct radeon_device *)param;
112 struct drm_device *dev = rdev->ddev;
113
114 int channels = r600_audio_channels(rdev);
115 int rate = r600_audio_rate(rdev);
116 int bps = r600_audio_bits_per_sample(rdev);
117 uint8_t status_bits = r600_audio_status_bits(rdev);
118 uint8_t category_code = r600_audio_category_code(rdev);
119
120 struct drm_encoder *encoder;
Christian König58bd0862010-04-05 22:14:55 +0200121 int changes = 0, still_going = 0;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200122
123 changes |= channels != rdev->audio_channels;
124 changes |= rate != rdev->audio_rate;
125 changes |= bps != rdev->audio_bits_per_sample;
126 changes |= status_bits != rdev->audio_status_bits;
127 changes |= category_code != rdev->audio_category_code;
128
129 if (changes) {
130 rdev->audio_channels = channels;
131 rdev->audio_rate = rate;
132 rdev->audio_bits_per_sample = bps;
133 rdev->audio_status_bits = status_bits;
134 rdev->audio_category_code = category_code;
135 }
136
137 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Christian König58bd0862010-04-05 22:14:55 +0200138 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
139 if (radeon_encoder->audio_polling_active) {
140 still_going = 1;
141 if (changes || r600_hdmi_buffer_status_changed(encoder))
142 r600_hdmi_update_audio_settings(encoder);
143 }
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200144 }
145
Christian König58bd0862010-04-05 22:14:55 +0200146 if(still_going)
147 mod_timer(&rdev->audio_timer,
148 jiffies + msecs_to_jiffies(AUDIO_TIMER_INTERVALL));
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200149}
150
151/*
Rafał Miłecki5230aea2010-02-11 22:58:52 +0100152 * turn on/off audio engine
153 */
154static void r600_audio_engine_enable(struct radeon_device *rdev, bool enable)
155{
156 DRM_INFO("%s audio support", enable ? "Enabling" : "Disabling");
157 WREG32_P(R600_AUDIO_ENABLE, enable ? 0x81000000 : 0x0, ~0x81000000);
158}
159
160/*
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200161 * initialize the audio vars and register the update timer
162 */
163int r600_audio_init(struct radeon_device *rdev)
164{
Rafał Miłeckic8792d52010-02-26 08:46:33 +0000165 if (!radeon_audio || !r600_audio_chipset_supported(rdev))
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200166 return 0;
167
Rafał Miłeckic8792d52010-02-26 08:46:33 +0000168 r600_audio_engine_enable(rdev, true);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200169
170 rdev->audio_channels = -1;
171 rdev->audio_rate = -1;
172 rdev->audio_bits_per_sample = -1;
173 rdev->audio_status_bits = 0;
174 rdev->audio_category_code = 0;
175
176 setup_timer(
177 &rdev->audio_timer,
178 r600_audio_update_hdmi,
179 (unsigned long)rdev);
180
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200181 return 0;
182}
183
184/*
Christian König58bd0862010-04-05 22:14:55 +0200185 * enable the polling timer, to check for status changes
186 */
187void r600_audio_enable_polling(struct drm_encoder *encoder)
188{
189 struct drm_device *dev = encoder->dev;
190 struct radeon_device *rdev = dev->dev_private;
191 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
192
193 DRM_DEBUG("r600_audio_enable_polling: %d", radeon_encoder->audio_polling_active);
194 if (radeon_encoder->audio_polling_active)
195 return;
196
197 radeon_encoder->audio_polling_active = 1;
198 mod_timer(&rdev->audio_timer, jiffies + 1);
199}
200
201/*
202 * disable the polling timer, so we get no more status updates
203 */
204void r600_audio_disable_polling(struct drm_encoder *encoder)
205{
206 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
207 DRM_DEBUG("r600_audio_disable_polling: %d", radeon_encoder->audio_polling_active);
208 radeon_encoder->audio_polling_active = 0;
209}
210
211/*
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200212 * atach the audio codec to the clock source of the encoder
213 */
214void r600_audio_set_clock(struct drm_encoder *encoder, int clock)
215{
216 struct drm_device *dev = encoder->dev;
217 struct radeon_device *rdev = dev->dev_private;
218 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000219 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200220 int base_rate = 48000;
221
222 switch (radeon_encoder->encoder_id) {
223 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
224 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
225 WREG32_P(R600_AUDIO_TIMING, 0, ~0x301);
226 break;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200227 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
228 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
229 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
230 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
231 WREG32_P(R600_AUDIO_TIMING, 0x100, ~0x301);
232 break;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200233 default:
234 DRM_ERROR("Unsupported encoder type 0x%02X\n",
235 radeon_encoder->encoder_id);
236 return;
237 }
238
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000239 switch (dig->dig_encoder) {
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200240 case 0:
Rafał Miłecki3fe373d2010-03-06 13:03:38 +0000241 WREG32(R600_AUDIO_PLL1_MUL, base_rate * 50);
242 WREG32(R600_AUDIO_PLL1_DIV, clock * 100);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200243 WREG32(R600_AUDIO_CLK_SRCSEL, 0);
244 break;
245
246 case 1:
Rafał Miłecki3fe373d2010-03-06 13:03:38 +0000247 WREG32(R600_AUDIO_PLL2_MUL, base_rate * 50);
248 WREG32(R600_AUDIO_PLL2_DIV, clock * 100);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200249 WREG32(R600_AUDIO_CLK_SRCSEL, 1);
250 break;
Rafał Miłecki2cd62182010-03-08 22:14:01 +0000251 default:
252 dev_err(rdev->dev, "Unsupported DIG on encoder 0x%02X\n",
253 radeon_encoder->encoder_id);
254 return;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200255 }
256}
257
258/*
259 * release the audio timer
260 * TODO: How to do this correctly on SMP systems?
261 */
262void r600_audio_fini(struct radeon_device *rdev)
263{
Rafał Miłeckic8792d52010-02-26 08:46:33 +0000264 if (!radeon_audio || !r600_audio_chipset_supported(rdev))
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200265 return;
266
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200267 del_timer(&rdev->audio_timer);
Rafał Miłecki5230aea2010-02-11 22:58:52 +0100268
269 r600_audio_engine_enable(rdev, false);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200270}