drm/radeon: fall back to GTT if bo creation/validation in VRAM fails.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / gpu / 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 #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 */
36 static int r600_audio_chipset_supported(struct radeon_device *rdev)
37 {
38 return (rdev->family >= CHIP_R600 && rdev->family < CHIP_CEDAR)
39 || rdev->family == CHIP_RS600
40 || rdev->family == CHIP_RS690
41 || rdev->family == CHIP_RS740;
42 }
43
44 /*
45 * current number of channels
46 */
47 int r600_audio_channels(struct radeon_device *rdev)
48 {
49 return (RREG32(R600_AUDIO_RATE_BPS_CHANNEL) & 0x7) + 1;
50 }
51
52 /*
53 * current bits per sample
54 */
55 int r600_audio_bits_per_sample(struct radeon_device *rdev)
56 {
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 */
74 int r600_audio_rate(struct radeon_device *rdev)
75 {
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 */
93 uint8_t r600_audio_status_bits(struct radeon_device *rdev)
94 {
95 return RREG32(R600_AUDIO_STATUS_BITS) & 0xff;
96 }
97
98 /*
99 * iec 60958 category code
100 */
101 uint8_t r600_audio_category_code(struct radeon_device *rdev)
102 {
103 return (RREG32(R600_AUDIO_STATUS_BITS) >> 8) & 0xff;
104 }
105
106 /*
107 * schedule next audio update event
108 */
109 void r600_audio_schedule_polling(struct radeon_device *rdev)
110 {
111 mod_timer(&rdev->audio_timer,
112 jiffies + msecs_to_jiffies(AUDIO_TIMER_INTERVALL));
113 }
114
115 /*
116 * update all hdmi interfaces with current audio parameters
117 */
118 static void r600_audio_update_hdmi(unsigned long param)
119 {
120 struct radeon_device *rdev = (struct radeon_device *)param;
121 struct drm_device *dev = rdev->ddev;
122
123 int channels = r600_audio_channels(rdev);
124 int rate = r600_audio_rate(rdev);
125 int bps = r600_audio_bits_per_sample(rdev);
126 uint8_t status_bits = r600_audio_status_bits(rdev);
127 uint8_t category_code = r600_audio_category_code(rdev);
128
129 struct drm_encoder *encoder;
130 int changes = 0, still_going = 0;
131
132 changes |= channels != rdev->audio_channels;
133 changes |= rate != rdev->audio_rate;
134 changes |= bps != rdev->audio_bits_per_sample;
135 changes |= status_bits != rdev->audio_status_bits;
136 changes |= category_code != rdev->audio_category_code;
137
138 if (changes) {
139 rdev->audio_channels = channels;
140 rdev->audio_rate = rate;
141 rdev->audio_bits_per_sample = bps;
142 rdev->audio_status_bits = status_bits;
143 rdev->audio_category_code = category_code;
144 }
145
146 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
147 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
148 still_going |= radeon_encoder->audio_polling_active;
149 if (changes || r600_hdmi_buffer_status_changed(encoder))
150 r600_hdmi_update_audio_settings(encoder);
151 }
152
153 if(still_going) r600_audio_schedule_polling(rdev);
154 }
155
156 /*
157 * turn on/off audio engine
158 */
159 static void r600_audio_engine_enable(struct radeon_device *rdev, bool enable)
160 {
161 DRM_INFO("%s audio support", enable ? "Enabling" : "Disabling");
162 WREG32_P(R600_AUDIO_ENABLE, enable ? 0x81000000 : 0x0, ~0x81000000);
163 rdev->audio_enabled = enable;
164 }
165
166 /*
167 * initialize the audio vars and register the update timer
168 */
169 int r600_audio_init(struct radeon_device *rdev)
170 {
171 if (!radeon_audio || !r600_audio_chipset_supported(rdev))
172 return 0;
173
174 r600_audio_engine_enable(rdev, true);
175
176 rdev->audio_channels = -1;
177 rdev->audio_rate = -1;
178 rdev->audio_bits_per_sample = -1;
179 rdev->audio_status_bits = 0;
180 rdev->audio_category_code = 0;
181
182 setup_timer(
183 &rdev->audio_timer,
184 r600_audio_update_hdmi,
185 (unsigned long)rdev);
186
187 return 0;
188 }
189
190 /*
191 * enable the polling timer, to check for status changes
192 */
193 void r600_audio_enable_polling(struct drm_encoder *encoder)
194 {
195 struct drm_device *dev = encoder->dev;
196 struct radeon_device *rdev = dev->dev_private;
197 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
198
199 DRM_DEBUG("r600_audio_enable_polling: %d", radeon_encoder->audio_polling_active);
200 if (radeon_encoder->audio_polling_active)
201 return;
202
203 radeon_encoder->audio_polling_active = 1;
204 if (rdev->audio_enabled)
205 mod_timer(&rdev->audio_timer, jiffies + 1);
206 }
207
208 /*
209 * disable the polling timer, so we get no more status updates
210 */
211 void r600_audio_disable_polling(struct drm_encoder *encoder)
212 {
213 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
214 DRM_DEBUG("r600_audio_disable_polling: %d", radeon_encoder->audio_polling_active);
215 radeon_encoder->audio_polling_active = 0;
216 }
217
218 /*
219 * atach the audio codec to the clock source of the encoder
220 */
221 void r600_audio_set_clock(struct drm_encoder *encoder, int clock)
222 {
223 struct drm_device *dev = encoder->dev;
224 struct radeon_device *rdev = dev->dev_private;
225 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
226 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
227 int base_rate = 48000;
228
229 switch (radeon_encoder->encoder_id) {
230 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
231 case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
232 WREG32_P(R600_AUDIO_TIMING, 0, ~0x301);
233 break;
234 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
235 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
236 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
237 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
238 WREG32_P(R600_AUDIO_TIMING, 0x100, ~0x301);
239 break;
240 default:
241 DRM_ERROR("Unsupported encoder type 0x%02X\n",
242 radeon_encoder->encoder_id);
243 return;
244 }
245
246 switch (dig->dig_encoder) {
247 case 0:
248 WREG32(R600_AUDIO_PLL1_MUL, base_rate * 50);
249 WREG32(R600_AUDIO_PLL1_DIV, clock * 100);
250 WREG32(R600_AUDIO_CLK_SRCSEL, 0);
251 break;
252
253 case 1:
254 WREG32(R600_AUDIO_PLL2_MUL, base_rate * 50);
255 WREG32(R600_AUDIO_PLL2_DIV, clock * 100);
256 WREG32(R600_AUDIO_CLK_SRCSEL, 1);
257 break;
258 default:
259 dev_err(rdev->dev, "Unsupported DIG on encoder 0x%02X\n",
260 radeon_encoder->encoder_id);
261 return;
262 }
263 }
264
265 /*
266 * release the audio timer
267 * TODO: How to do this correctly on SMP systems?
268 */
269 void r600_audio_fini(struct radeon_device *rdev)
270 {
271 if (!rdev->audio_enabled)
272 return;
273
274 del_timer(&rdev->audio_timer);
275
276 r600_audio_engine_enable(rdev, false);
277 }