blob: 846fae5763998a7ed7ab0140f68deb7b3dae40b8 [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"
Daniel Vetter3574dda2011-02-18 17:59:19 +010029#include "radeon_asic.h"
Christian Koenigdafc3bd2009-10-11 23:49:13 +020030#include "atom.h"
31
32#define AUDIO_TIMER_INTERVALL 100 /* 1/10 sekund should be enough */
33
34/*
35 * check if the chipset is supported
36 */
37static int r600_audio_chipset_supported(struct radeon_device *rdev)
38{
Alex Deucher16823d12010-04-16 11:35:30 -040039 return (rdev->family >= CHIP_R600 && rdev->family < CHIP_CEDAR)
Christian Koenigdafc3bd2009-10-11 23:49:13 +020040 || rdev->family == CHIP_RS600
41 || rdev->family == CHIP_RS690
42 || rdev->family == CHIP_RS740;
43}
44
45/*
46 * current number of channels
47 */
Christian König58bd0862010-04-05 22:14:55 +020048int r600_audio_channels(struct radeon_device *rdev)
Christian Koenigdafc3bd2009-10-11 23:49:13 +020049{
50 return (RREG32(R600_AUDIO_RATE_BPS_CHANNEL) & 0x7) + 1;
51}
52
53/*
54 * current bits per sample
55 */
Christian König58bd0862010-04-05 22:14:55 +020056int r600_audio_bits_per_sample(struct radeon_device *rdev)
Christian Koenigdafc3bd2009-10-11 23:49:13 +020057{
58 uint32_t value = (RREG32(R600_AUDIO_RATE_BPS_CHANNEL) & 0xF0) >> 4;
59 switch (value) {
60 case 0x0: return 8;
61 case 0x1: return 16;
62 case 0x2: return 20;
63 case 0x3: return 24;
64 case 0x4: return 32;
65 }
66
Rafał Miłecki219de622010-06-25 21:24:20 +020067 dev_err(rdev->dev, "Unknown bits per sample 0x%x using 16 instead\n",
68 (int)value);
Christian Koenigdafc3bd2009-10-11 23:49:13 +020069
70 return 16;
71}
72
73/*
74 * current sampling rate in HZ
75 */
Christian König58bd0862010-04-05 22:14:55 +020076int r600_audio_rate(struct radeon_device *rdev)
Christian Koenigdafc3bd2009-10-11 23:49:13 +020077{
78 uint32_t value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);
79 uint32_t result;
80
81 if (value & 0x4000)
82 result = 44100;
83 else
84 result = 48000;
85
86 result *= ((value >> 11) & 0x7) + 1;
87 result /= ((value >> 8) & 0x7) + 1;
88
89 return result;
90}
91
92/*
93 * iec 60958 status bits
94 */
Christian König58bd0862010-04-05 22:14:55 +020095uint8_t r600_audio_status_bits(struct radeon_device *rdev)
Christian Koenigdafc3bd2009-10-11 23:49:13 +020096{
97 return RREG32(R600_AUDIO_STATUS_BITS) & 0xff;
98}
99
100/*
101 * iec 60958 category code
102 */
Christian König58bd0862010-04-05 22:14:55 +0200103uint8_t r600_audio_category_code(struct radeon_device *rdev)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200104{
105 return (RREG32(R600_AUDIO_STATUS_BITS) >> 8) & 0xff;
106}
107
108/*
Christian Koenigf2594932010-04-10 03:13:16 +0200109 * schedule next audio update event
110 */
111void r600_audio_schedule_polling(struct radeon_device *rdev)
112{
113 mod_timer(&rdev->audio_timer,
114 jiffies + msecs_to_jiffies(AUDIO_TIMER_INTERVALL));
115}
116
117/*
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200118 * update all hdmi interfaces with current audio parameters
119 */
120static void r600_audio_update_hdmi(unsigned long param)
121{
122 struct radeon_device *rdev = (struct radeon_device *)param;
123 struct drm_device *dev = rdev->ddev;
124
125 int channels = r600_audio_channels(rdev);
126 int rate = r600_audio_rate(rdev);
127 int bps = r600_audio_bits_per_sample(rdev);
128 uint8_t status_bits = r600_audio_status_bits(rdev);
129 uint8_t category_code = r600_audio_category_code(rdev);
130
131 struct drm_encoder *encoder;
Christian König58bd0862010-04-05 22:14:55 +0200132 int changes = 0, still_going = 0;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200133
134 changes |= channels != rdev->audio_channels;
135 changes |= rate != rdev->audio_rate;
136 changes |= bps != rdev->audio_bits_per_sample;
137 changes |= status_bits != rdev->audio_status_bits;
138 changes |= category_code != rdev->audio_category_code;
139
140 if (changes) {
141 rdev->audio_channels = channels;
142 rdev->audio_rate = rate;
143 rdev->audio_bits_per_sample = bps;
144 rdev->audio_status_bits = status_bits;
145 rdev->audio_category_code = category_code;
146 }
147
148 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Christian König58bd0862010-04-05 22:14:55 +0200149 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Christian Koenigf2594932010-04-10 03:13:16 +0200150 still_going |= radeon_encoder->audio_polling_active;
151 if (changes || r600_hdmi_buffer_status_changed(encoder))
152 r600_hdmi_update_audio_settings(encoder);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200153 }
154
Rafał Miłecki219de622010-06-25 21:24:20 +0200155 if (still_going)
156 r600_audio_schedule_polling(rdev);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200157}
158
159/*
Rafał Miłecki5230aea2010-02-11 22:58:52 +0100160 * turn on/off audio engine
161 */
162static void r600_audio_engine_enable(struct radeon_device *rdev, bool enable)
163{
Rafał Miłecki219de622010-06-25 21:24:20 +0200164 DRM_INFO("%s audio support\n", enable ? "Enabling" : "Disabling");
Rafał Miłecki5230aea2010-02-11 22:58:52 +0100165 WREG32_P(R600_AUDIO_ENABLE, enable ? 0x81000000 : 0x0, ~0x81000000);
Rafał Miłecki7eea7e92010-06-19 12:24:56 +0200166 rdev->audio_enabled = enable;
Rafał Miłecki5230aea2010-02-11 22:58:52 +0100167}
168
169/*
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200170 * initialize the audio vars and register the update timer
171 */
172int r600_audio_init(struct radeon_device *rdev)
173{
Rafał Miłeckic8792d52010-02-26 08:46:33 +0000174 if (!radeon_audio || !r600_audio_chipset_supported(rdev))
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200175 return 0;
176
Rafał Miłeckic8792d52010-02-26 08:46:33 +0000177 r600_audio_engine_enable(rdev, true);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200178
179 rdev->audio_channels = -1;
180 rdev->audio_rate = -1;
181 rdev->audio_bits_per_sample = -1;
182 rdev->audio_status_bits = 0;
183 rdev->audio_category_code = 0;
184
185 setup_timer(
186 &rdev->audio_timer,
187 r600_audio_update_hdmi,
188 (unsigned long)rdev);
189
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200190 return 0;
191}
192
193/*
Christian König58bd0862010-04-05 22:14:55 +0200194 * enable the polling timer, to check for status changes
195 */
196void r600_audio_enable_polling(struct drm_encoder *encoder)
197{
198 struct drm_device *dev = encoder->dev;
199 struct radeon_device *rdev = dev->dev_private;
200 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
201
Rafał Miłecki219de622010-06-25 21:24:20 +0200202 DRM_DEBUG("r600_audio_enable_polling: %d\n",
203 radeon_encoder->audio_polling_active);
Christian König58bd0862010-04-05 22:14:55 +0200204 if (radeon_encoder->audio_polling_active)
205 return;
206
207 radeon_encoder->audio_polling_active = 1;
Rafał Miłecki7eea7e92010-06-19 12:24:56 +0200208 if (rdev->audio_enabled)
209 mod_timer(&rdev->audio_timer, jiffies + 1);
Christian König58bd0862010-04-05 22:14:55 +0200210}
211
212/*
213 * disable the polling timer, so we get no more status updates
214 */
215void r600_audio_disable_polling(struct drm_encoder *encoder)
216{
217 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłecki219de622010-06-25 21:24:20 +0200218 DRM_DEBUG("r600_audio_disable_polling: %d\n",
219 radeon_encoder->audio_polling_active);
Christian König58bd0862010-04-05 22:14:55 +0200220 radeon_encoder->audio_polling_active = 0;
221}
222
223/*
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200224 * atach the audio codec to the clock source of the encoder
225 */
226void r600_audio_set_clock(struct drm_encoder *encoder, int clock)
227{
228 struct drm_device *dev = encoder->dev;
229 struct radeon_device *rdev = dev->dev_private;
230 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000231 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200232 int base_rate = 48000;
233
234 switch (radeon_encoder->encoder_id) {
235 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
236 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
237 WREG32_P(R600_AUDIO_TIMING, 0, ~0x301);
238 break;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200239 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
240 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
241 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
242 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
243 WREG32_P(R600_AUDIO_TIMING, 0x100, ~0x301);
244 break;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200245 default:
Rafał Miłecki219de622010-06-25 21:24:20 +0200246 dev_err(rdev->dev, "Unsupported encoder type 0x%02X\n",
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200247 radeon_encoder->encoder_id);
248 return;
249 }
250
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000251 switch (dig->dig_encoder) {
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200252 case 0:
Rafał Miłecki3fe373d2010-03-06 13:03:38 +0000253 WREG32(R600_AUDIO_PLL1_MUL, base_rate * 50);
254 WREG32(R600_AUDIO_PLL1_DIV, clock * 100);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200255 WREG32(R600_AUDIO_CLK_SRCSEL, 0);
256 break;
257
258 case 1:
Rafał Miłecki3fe373d2010-03-06 13:03:38 +0000259 WREG32(R600_AUDIO_PLL2_MUL, base_rate * 50);
260 WREG32(R600_AUDIO_PLL2_DIV, clock * 100);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200261 WREG32(R600_AUDIO_CLK_SRCSEL, 1);
262 break;
Rafał Miłecki2cd6218c2010-03-08 22:14:01 +0000263 default:
264 dev_err(rdev->dev, "Unsupported DIG on encoder 0x%02X\n",
265 radeon_encoder->encoder_id);
266 return;
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200267 }
268}
269
270/*
271 * release the audio timer
272 * TODO: How to do this correctly on SMP systems?
273 */
274void r600_audio_fini(struct radeon_device *rdev)
275{
Rafał Miłecki7eea7e92010-06-19 12:24:56 +0200276 if (!rdev->audio_enabled)
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200277 return;
278
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200279 del_timer(&rdev->audio_timer);
Rafał Miłecki5230aea2010-02-11 22:58:52 +0100280
281 r600_audio_engine_enable(rdev, false);
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200282}