ac97.c revision 1.32 1 1.32 kent /* $NetBSD: ac97.c,v 1.32 2002/10/11 02:32:23 kent Exp $ */
2 1.14 thorpej /* $OpenBSD: ac97.c,v 1.8 2000/07/19 09:01:35 csapuntz Exp $ */
3 1.1 augustss
4 1.1 augustss /*
5 1.14 thorpej * Copyright (c) 1999, 2000 Constantine Sapuntzakis
6 1.1 augustss *
7 1.1 augustss * Author: Constantine Sapuntzakis <csapuntz (at) stanford.edu>
8 1.1 augustss *
9 1.1 augustss * Redistribution and use in source and binary forms, with or without
10 1.1 augustss * modification, are permitted provided that the following conditions
11 1.1 augustss * are met:
12 1.1 augustss * 1. Redistributions of source code must retain the above copyright
13 1.1 augustss * notice, this list of conditions and the following disclaimer.
14 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer in the
16 1.1 augustss * documentation and/or other materials provided with the distribution.
17 1.14 thorpej * 3. The name of the author may not be used to endorse or promote
18 1.14 thorpej * products derived from this software without specific prior written
19 1.14 thorpej * permission.
20 1.14 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
21 1.14 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 1.14 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.14 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
24 1.14 thorpej * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.14 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 1.14 thorpej * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 1.14 thorpej * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 1.14 thorpej * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.14 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 1.14 thorpej * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 1.14 thorpej * DAMAGE
32 1.1 augustss */
33 1.1 augustss
34 1.1 augustss /* Partially inspired by FreeBSD's sys/dev/pcm/ac97.c. It came with
35 1.1 augustss the following copyright */
36 1.1 augustss
37 1.1 augustss /*
38 1.1 augustss * Copyright (c) 1999 Cameron Grant <gandalf (at) vilnya.demon.co.uk>
39 1.1 augustss * All rights reserved.
40 1.1 augustss *
41 1.1 augustss * Redistribution and use in source and binary forms, with or without
42 1.1 augustss * modification, are permitted provided that the following conditions
43 1.1 augustss * are met:
44 1.1 augustss * 1. Redistributions of source code must retain the above copyright
45 1.1 augustss * notice, this list of conditions and the following disclaimer.
46 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
47 1.1 augustss * notice, this list of conditions and the following disclaimer in the
48 1.1 augustss * documentation and/or other materials provided with the distribution.
49 1.1 augustss *
50 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51 1.1 augustss * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 1.1 augustss * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 1.1 augustss * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
54 1.1 augustss * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 1.1 augustss * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 1.1 augustss * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 1.1 augustss * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 1.1 augustss * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 1.1 augustss * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 1.1 augustss * SUCH DAMAGE.
61 1.1 augustss *
62 1.1 augustss * $FreeBSD$
63 1.1 augustss */
64 1.21 lukem
65 1.21 lukem #include <sys/cdefs.h>
66 1.32 kent __KERNEL_RCSID(0, "$NetBSD: ac97.c,v 1.32 2002/10/11 02:32:23 kent Exp $");
67 1.1 augustss
68 1.1 augustss #include <sys/param.h>
69 1.1 augustss #include <sys/systm.h>
70 1.1 augustss #include <sys/kernel.h>
71 1.1 augustss #include <sys/malloc.h>
72 1.1 augustss #include <sys/device.h>
73 1.1 augustss
74 1.1 augustss #include <sys/audioio.h>
75 1.1 augustss #include <dev/audio_if.h>
76 1.10 thorpej
77 1.10 thorpej #include <dev/ic/ac97reg.h>
78 1.9 thorpej #include <dev/ic/ac97var.h>
79 1.1 augustss
80 1.18 jdolecek static const struct audio_mixer_enum ac97_on_off = { 2,
81 1.1 augustss { { { AudioNoff } , 0 },
82 1.1 augustss { { AudioNon } , 1 } }};
83 1.1 augustss
84 1.1 augustss
85 1.18 jdolecek static const struct audio_mixer_enum ac97_mic_select = { 2,
86 1.1 augustss { { { AudioNmicrophone "0" },
87 1.1 augustss 0 },
88 1.1 augustss { { AudioNmicrophone "1" },
89 1.1 augustss 1 } }};
90 1.1 augustss
91 1.18 jdolecek static const struct audio_mixer_enum ac97_mono_select = { 2,
92 1.1 augustss { { { AudioNmixerout },
93 1.1 augustss 0 },
94 1.1 augustss { { AudioNmicrophone },
95 1.1 augustss 1 } }};
96 1.1 augustss
97 1.18 jdolecek static const struct audio_mixer_enum ac97_source = { 8,
98 1.1 augustss { { { AudioNmicrophone } , 0 },
99 1.1 augustss { { AudioNcd }, 1 },
100 1.1 augustss { { "video" }, 2 },
101 1.1 augustss { { AudioNaux }, 3 },
102 1.1 augustss { { AudioNline }, 4 },
103 1.1 augustss { { AudioNmixerout }, 5 },
104 1.1 augustss { { AudioNmixerout AudioNmono }, 6 },
105 1.1 augustss { { "phone" }, 7 }}};
106 1.1 augustss
107 1.19 erh /*
108 1.19 erh * Due to different values for each source that uses these structures,
109 1.19 erh * the ac97_query_devinfo function sets delta in mixer_devinfo_t using
110 1.19 erh * ac97_source_info.bits.
111 1.19 erh */
112 1.18 jdolecek static const struct audio_mixer_value ac97_volume_stereo = { { AudioNvolume },
113 1.1 augustss 2 };
114 1.1 augustss
115 1.18 jdolecek static const struct audio_mixer_value ac97_volume_mono = { { AudioNvolume },
116 1.1 augustss 1 };
117 1.1 augustss
118 1.1 augustss #define WRAP(a) &a, sizeof(a)
119 1.1 augustss
120 1.18 jdolecek const struct ac97_source_info {
121 1.18 jdolecek const char *class;
122 1.18 jdolecek const char *device;
123 1.18 jdolecek const char *qualifier;
124 1.1 augustss int type;
125 1.1 augustss
126 1.18 jdolecek const void *info;
127 1.1 augustss int info_size;
128 1.1 augustss
129 1.1 augustss u_int8_t reg;
130 1.14 thorpej u_int16_t default_value;
131 1.1 augustss u_int8_t bits:3;
132 1.1 augustss u_int8_t ofs:4;
133 1.1 augustss u_int8_t mute:1;
134 1.1 augustss u_int8_t polarity:1; /* Does 0 == MAX or MIN */
135 1.1 augustss
136 1.1 augustss int prev;
137 1.1 augustss int next;
138 1.1 augustss int mixer_class;
139 1.1 augustss } source_info[] = {
140 1.1 augustss { AudioCinputs , NULL, NULL, AUDIO_MIXER_CLASS,
141 1.1 augustss },
142 1.1 augustss { AudioCoutputs, NULL, NULL, AUDIO_MIXER_CLASS,
143 1.1 augustss },
144 1.1 augustss { AudioCrecord , NULL, NULL, AUDIO_MIXER_CLASS,
145 1.1 augustss },
146 1.1 augustss /* Stereo master volume*/
147 1.1 augustss { AudioCoutputs, AudioNmaster, NULL, AUDIO_MIXER_VALUE,
148 1.1 augustss WRAP(ac97_volume_stereo),
149 1.14 thorpej AC97_REG_MASTER_VOLUME, 0x8000, 5, 0, 1,
150 1.1 augustss },
151 1.1 augustss /* Mono volume */
152 1.1 augustss { AudioCoutputs, AudioNmono, NULL, AUDIO_MIXER_VALUE,
153 1.1 augustss WRAP(ac97_volume_mono),
154 1.14 thorpej AC97_REG_MASTER_VOLUME_MONO, 0x8000, 6, 0, 1,
155 1.1 augustss },
156 1.1 augustss { AudioCoutputs, AudioNmono,AudioNsource, AUDIO_MIXER_ENUM,
157 1.1 augustss WRAP(ac97_mono_select),
158 1.14 thorpej AC97_REG_GP, 0x0000, 1, 9, 0,
159 1.1 augustss },
160 1.1 augustss /* Headphone volume */
161 1.1 augustss { AudioCoutputs, AudioNheadphone, NULL, AUDIO_MIXER_VALUE,
162 1.1 augustss WRAP(ac97_volume_stereo),
163 1.14 thorpej AC97_REG_HEADPHONE_VOLUME, 0x8000, 6, 0, 1,
164 1.1 augustss },
165 1.1 augustss /* Tone */
166 1.1 augustss { AudioCoutputs, "tone", NULL, AUDIO_MIXER_VALUE,
167 1.1 augustss WRAP(ac97_volume_stereo),
168 1.14 thorpej AC97_REG_MASTER_TONE, 0x0f0f, 4, 0, 0,
169 1.1 augustss },
170 1.1 augustss /* PC Beep Volume */
171 1.1 augustss { AudioCinputs, AudioNspeaker, NULL, AUDIO_MIXER_VALUE,
172 1.1 augustss WRAP(ac97_volume_mono),
173 1.14 thorpej AC97_REG_PCBEEP_VOLUME, 0x0000, 4, 1, 1,
174 1.1 augustss },
175 1.1 augustss /* Phone */
176 1.1 augustss { AudioCinputs, "phone", NULL, AUDIO_MIXER_VALUE,
177 1.1 augustss WRAP(ac97_volume_mono),
178 1.14 thorpej AC97_REG_PHONE_VOLUME, 0x8008, 5, 0, 1,
179 1.1 augustss },
180 1.1 augustss /* Mic Volume */
181 1.1 augustss { AudioCinputs, AudioNmicrophone, NULL, AUDIO_MIXER_VALUE,
182 1.1 augustss WRAP(ac97_volume_mono),
183 1.14 thorpej AC97_REG_MIC_VOLUME, 0x8008, 5, 0, 1,
184 1.1 augustss },
185 1.1 augustss { AudioCinputs, AudioNmicrophone, AudioNpreamp, AUDIO_MIXER_ENUM,
186 1.1 augustss WRAP(ac97_on_off),
187 1.14 thorpej AC97_REG_MIC_VOLUME, 0x8008, 1, 6, 0,
188 1.1 augustss },
189 1.1 augustss { AudioCinputs, AudioNmicrophone, AudioNsource, AUDIO_MIXER_ENUM,
190 1.1 augustss WRAP(ac97_mic_select),
191 1.14 thorpej AC97_REG_GP, 0x0000, 1, 8, 0,
192 1.1 augustss },
193 1.1 augustss /* Line in Volume */
194 1.1 augustss { AudioCinputs, AudioNline, NULL, AUDIO_MIXER_VALUE,
195 1.1 augustss WRAP(ac97_volume_stereo),
196 1.14 thorpej AC97_REG_LINEIN_VOLUME, 0x8808, 5, 0, 1,
197 1.1 augustss },
198 1.1 augustss /* CD Volume */
199 1.1 augustss { AudioCinputs, AudioNcd, NULL, AUDIO_MIXER_VALUE,
200 1.1 augustss WRAP(ac97_volume_stereo),
201 1.14 thorpej AC97_REG_CD_VOLUME, 0x8808, 5, 0, 1,
202 1.1 augustss },
203 1.1 augustss /* Video Volume */
204 1.1 augustss { AudioCinputs, "video", NULL, AUDIO_MIXER_VALUE,
205 1.1 augustss WRAP(ac97_volume_stereo),
206 1.14 thorpej AC97_REG_VIDEO_VOLUME, 0x8808, 5, 0, 1,
207 1.1 augustss },
208 1.1 augustss /* AUX volume */
209 1.1 augustss { AudioCinputs, AudioNaux, NULL, AUDIO_MIXER_VALUE,
210 1.1 augustss WRAP(ac97_volume_stereo),
211 1.14 thorpej AC97_REG_AUX_VOLUME, 0x8808, 5, 0, 1,
212 1.1 augustss },
213 1.1 augustss /* PCM out volume */
214 1.1 augustss { AudioCinputs, AudioNdac, NULL, AUDIO_MIXER_VALUE,
215 1.1 augustss WRAP(ac97_volume_stereo),
216 1.14 thorpej AC97_REG_PCMOUT_VOLUME, 0x8808, 5, 0, 1,
217 1.1 augustss },
218 1.1 augustss /* Record Source - some logic for this is hard coded - see below */
219 1.1 augustss { AudioCrecord, AudioNsource, NULL, AUDIO_MIXER_ENUM,
220 1.1 augustss WRAP(ac97_source),
221 1.14 thorpej AC97_REG_RECORD_SELECT, 0x0000, 3, 0, 0,
222 1.1 augustss },
223 1.1 augustss /* Record Gain */
224 1.1 augustss { AudioCrecord, AudioNvolume, NULL, AUDIO_MIXER_VALUE,
225 1.1 augustss WRAP(ac97_volume_stereo),
226 1.14 thorpej AC97_REG_RECORD_GAIN, 0x8000, 4, 0, 1,
227 1.1 augustss },
228 1.1 augustss /* Record Gain mic */
229 1.1 augustss { AudioCrecord, AudioNmicrophone, NULL, AUDIO_MIXER_VALUE,
230 1.1 augustss WRAP(ac97_volume_mono),
231 1.14 thorpej AC97_REG_RECORD_GAIN_MIC, 0x8000, 4, 0, 1, 1,
232 1.1 augustss },
233 1.1 augustss /* */
234 1.1 augustss { AudioCoutputs, AudioNloudness, NULL, AUDIO_MIXER_ENUM,
235 1.1 augustss WRAP(ac97_on_off),
236 1.14 thorpej AC97_REG_GP, 0x0000, 1, 12, 0,
237 1.1 augustss },
238 1.1 augustss { AudioCoutputs, AudioNspatial, NULL, AUDIO_MIXER_ENUM,
239 1.1 augustss WRAP(ac97_on_off),
240 1.14 thorpej AC97_REG_GP, 0x0000, 1, 13, 0,
241 1.1 augustss },
242 1.1 augustss { AudioCoutputs, AudioNspatial, "center", AUDIO_MIXER_VALUE,
243 1.1 augustss WRAP(ac97_volume_mono),
244 1.14 thorpej AC97_REG_3D_CONTROL, 0x0000, 4, 8, 0, 1,
245 1.1 augustss },
246 1.1 augustss { AudioCoutputs, AudioNspatial, "depth", AUDIO_MIXER_VALUE,
247 1.1 augustss WRAP(ac97_volume_mono),
248 1.14 thorpej AC97_REG_3D_CONTROL, 0x0000, 4, 0, 0, 1,
249 1.1 augustss },
250 1.1 augustss
251 1.1 augustss /* Missing features: Simulated Stereo, POP, Loopback mode */
252 1.1 augustss } ;
253 1.1 augustss
254 1.1 augustss #define SOURCE_INFO_SIZE (sizeof(source_info)/sizeof(source_info[0]))
255 1.1 augustss
256 1.1 augustss /*
257 1.1 augustss * Check out http://developer.intel.com/pc-supp/platform/ac97/ for
258 1.1 augustss * information on AC-97
259 1.1 augustss */
260 1.1 augustss
261 1.1 augustss struct ac97_softc {
262 1.28 kent /* ac97_codec_if must be at the first of ac97_softc. */
263 1.14 thorpej struct ac97_codec_if codec_if;
264 1.1 augustss
265 1.14 thorpej struct ac97_host_if *host_if;
266 1.1 augustss
267 1.1 augustss struct ac97_source_info source_info[2 * SOURCE_INFO_SIZE];
268 1.1 augustss int num_source_info;
269 1.14 thorpej
270 1.14 thorpej enum ac97_host_flags host_flags;
271 1.28 kent unsigned int ac97_clock; /* usually 48000 */
272 1.28 kent #define AC97_STANDARD_CLOCK 48000U
273 1.28 kent u_int16_t caps; /* -> AC97_REG_RESET */
274 1.28 kent u_int16_t ext_id; /* -> AC97_REG_EXT_AUDIO_ID */
275 1.14 thorpej u_int16_t shadow_reg[128];
276 1.1 augustss };
277 1.1 augustss
278 1.1 augustss int ac97_mixer_get_port __P((struct ac97_codec_if *self, mixer_ctrl_t *cp));
279 1.1 augustss int ac97_mixer_set_port __P((struct ac97_codec_if *self, mixer_ctrl_t *));
280 1.1 augustss int ac97_query_devinfo __P((struct ac97_codec_if *self, mixer_devinfo_t *));
281 1.1 augustss int ac97_get_portnum_by_name __P((struct ac97_codec_if *, char *, char *,
282 1.1 augustss char *));
283 1.14 thorpej void ac97_restore_shadow __P((struct ac97_codec_if *self));
284 1.28 kent int ac97_set_rate(struct ac97_codec_if *codec_if, int target, u_long *rate);
285 1.28 kent void ac97_set_clock(struct ac97_codec_if *codec_if, unsigned int clock);
286 1.28 kent u_int16_t ac97_get_extcaps(struct ac97_codec_if *codec_if);
287 1.1 augustss
288 1.1 augustss struct ac97_codec_if_vtbl ac97civ = {
289 1.28 kent ac97_mixer_get_port,
290 1.1 augustss ac97_mixer_set_port,
291 1.1 augustss ac97_query_devinfo,
292 1.14 thorpej ac97_get_portnum_by_name,
293 1.14 thorpej ac97_restore_shadow,
294 1.28 kent ac97_get_extcaps,
295 1.28 kent ac97_set_rate,
296 1.28 kent ac97_set_clock,
297 1.1 augustss };
298 1.7 thorpej
299 1.7 thorpej static const struct ac97_codecid {
300 1.1 augustss u_int32_t id;
301 1.31 kent u_int32_t mask;
302 1.7 thorpej const char *name;
303 1.1 augustss } ac97codecid[] = {
304 1.31 kent { AC97_CODEC_ID('A', 'D', 'S', 3),
305 1.31 kent 0xffffffff, "Analog Devices AD1819B" },
306 1.31 kent { AC97_CODEC_ID('A', 'D', 'S', 64),
307 1.31 kent 0xffffffff, "Analog Devices AD1881" },
308 1.31 kent { AC97_CODEC_ID('A', 'D', 'S', 72),
309 1.31 kent 0xffffffff, "Analog Devices AD1881A" },
310 1.31 kent { AC97_CODEC_ID('A', 'D', 'S', 96),
311 1.31 kent 0xffffffff, "Analog Devices AD1885" },
312 1.31 kent { AC97_CODEC_ID('A', 'D', 'S', 99),
313 1.31 kent 0xffffffff, "Analog Devices AD1886A" },
314 1.31 kent
315 1.31 kent { AC97_CODEC_ID('A', 'K', 'M', 0),
316 1.31 kent 0xffffffff, "Asahi Kasei AK4540" },
317 1.31 kent { AC97_CODEC_ID('A', 'K', 'M', 2),
318 1.31 kent 0xffffffff, "Asahi Kasei AK4543" },
319 1.31 kent
320 1.31 kent { AC97_CODEC_ID('A', 'L', 'G', 0x10),
321 1.31 kent 0xffffffff, "Advance Logic ALC200" },
322 1.31 kent { AC97_CODEC_ID('A', 'L', 'G', 0x20),
323 1.31 kent 0xffffffff, "Advance Logic ALC650" },
324 1.31 kent { AC97_CODEC_ID('A', 'L', 'G', 0x30),
325 1.31 kent 0xffffffff, "Advance Logic ALC101" },
326 1.31 kent { AC97_CODEC_ID('A', 'L', 'G', 0x40),
327 1.31 kent 0xffffffff, "Advance Logic ALC202" },
328 1.31 kent { AC97_CODEC_ID('A', 'L', 'G', 0x50),
329 1.31 kent 0xffffffff, "Advance Logic ALC250" },
330 1.31 kent
331 1.31 kent /* Cirrus Logic, Crystal series:
332 1.31 kent * 'C' 'R' 'Y' 0x0[0-7] - CS4297
333 1.31 kent * 0x1[0-7] - CS4297A
334 1.31 kent * 0x2[0-7] - CS4298
335 1.31 kent * 0x2[8-f] - CS4294
336 1.31 kent * 0x3[0-7] - CS4299
337 1.31 kent * 0x4[8-f] - CS4201
338 1.31 kent * 0x5[8-f] - CS4205
339 1.31 kent * 0x6[0-7] - CS4291
340 1.31 kent * 0x7[0-7] - CS4202
341 1.31 kent * Datasheets:
342 1.31 kent * http://www.cirrus.com/pubs/cs4297A-5.pdf?DocumentID=593
343 1.31 kent * http://www.cirrus.com/pubs/cs4294.pdf?DocumentID=32
344 1.31 kent * http://www.cirrus.com/pubs/cs4299-5.pdf?DocumentID=594
345 1.31 kent * http://www.cirrus.com/pubs/cs4201-2.pdf?DocumentID=492
346 1.31 kent * http://www.cirrus.com/pubs/cs4205-2.pdf?DocumentID=492
347 1.31 kent * http://www.cirrus.com/pubs/cs4202-1.pdf?DocumentID=852
348 1.31 kent */
349 1.31 kent { AC97_CODEC_ID('C', 'R', 'Y', 0x00),
350 1.31 kent 0xfffffff8, "Crystal CS4297", },
351 1.31 kent { AC97_CODEC_ID('C', 'R', 'Y', 0x10),
352 1.31 kent 0xfffffff8, "Crystal CS4297A", },
353 1.31 kent { AC97_CODEC_ID('C', 'R', 'Y', 0x20),
354 1.31 kent 0xfffffff8, "Crystal CS4298", },
355 1.31 kent { AC97_CODEC_ID('C', 'R', 'Y', 0x28),
356 1.31 kent 0xfffffff8, "Crystal CS4294", },
357 1.31 kent { AC97_CODEC_ID('C', 'R', 'Y', 0x30),
358 1.31 kent 0xfffffff8, "Crystal CS4299", },
359 1.31 kent { AC97_CODEC_ID('C', 'R', 'Y', 0x48),
360 1.31 kent 0xfffffff8, "Crystal CS4201", },
361 1.31 kent { AC97_CODEC_ID('C', 'R', 'Y', 0x58),
362 1.31 kent 0xfffffff8, "Crystal CS4205", },
363 1.31 kent { AC97_CODEC_ID('C', 'R', 'Y', 0x60),
364 1.31 kent 0xfffffff8, "Crystal CS4291", },
365 1.31 kent { AC97_CODEC_ID('C', 'R', 'Y', 0x70),
366 1.31 kent 0xfffffff8, "Crystal CS4202", },
367 1.31 kent
368 1.31 kent { AC97_CODEC_ID('N', 'S', 'C', 49),
369 1.31 kent 0xffffffff, "National Semiconductor LM4549", },
370 1.31 kent { AC97_CODEC_ID('S', 'I', 'L', 34),
371 1.31 kent 0xffffffff, "Silicon Laboratory Si3036", },
372 1.31 kent { AC97_CODEC_ID('S', 'I', 'L', 35),
373 1.31 kent 0xffffffff, "Silicon Laboratory Si3038", },
374 1.31 kent { AC97_CODEC_ID('T', 'R', 'A', 2),
375 1.31 kent 0xffffffff, "TriTech TR28022", },
376 1.31 kent { AC97_CODEC_ID('T', 'R', 'A', 3),
377 1.31 kent 0xffffffff, "TriTech TR28023", },
378 1.31 kent { AC97_CODEC_ID('T', 'R', 'A', 6),
379 1.31 kent 0xffffffff, "TriTech TR28026", },
380 1.31 kent { AC97_CODEC_ID('T', 'R', 'A', 8),
381 1.31 kent 0xffffffff, "TriTech TR28028", },
382 1.31 kent { AC97_CODEC_ID('T', 'R', 'A', 35),
383 1.31 kent 0xffffffff, "TriTech TR28602", },
384 1.31 kent { AC97_CODEC_ID('W', 'M', 'L', 0),
385 1.31 kent 0xffffffff, "Wolfson WM9704", },
386 1.31 kent { AC97_CODEC_ID('W', 'M', 'L', 3),
387 1.31 kent 0xffffffff, "Wolfson WM9707", },
388 1.31 kent { AC97_CODEC_ID('Y', 'M', 'H', 0),
389 1.31 kent 0xffffffff, "Yamaha YMF743-S", },
390 1.31 kent { AC97_CODEC_ID('Y', 'M', 'H', 3),
391 1.31 kent 0xffffffff, "Yamaha YMF753-S", },
392 1.31 kent { 0x45838308, 0xffffffff, "ESS Technology ES1921", },
393 1.31 kent { 0x83847600, 0xffffffff, "SigmaTel STAC9700", },
394 1.31 kent { 0x83847604, 0xffffffff, "SigmaTel STAC9701/3/4/5", },
395 1.31 kent { 0x83847605, 0xffffffff, "SigmaTel STAC9704", },
396 1.31 kent { 0x83847608, 0xffffffff, "SigmaTel STAC9708", },
397 1.31 kent { 0x83847609, 0xffffffff, "SigmaTel STAC9721/23", },
398 1.31 kent { 0x83847644, 0xffffffff, "SigmaTel STAC9744/45", },
399 1.31 kent { 0x83847684, 0xffffffff, "SigmaTel STAC9783/84", },
400 1.31 kent { 0,
401 1.31 kent 0, NULL, }
402 1.1 augustss };
403 1.1 augustss
404 1.18 jdolecek static const char * const ac97enhancement[] = {
405 1.2 soren "no 3D stereo",
406 1.1 augustss "Analog Devices Phat Stereo",
407 1.24 ichiro "Creative",
408 1.1 augustss "National Semi 3D",
409 1.1 augustss "Yamaha Ymersion",
410 1.1 augustss "BBE 3D",
411 1.24 ichiro "Crystal Semi 3D",
412 1.1 augustss "Qsound QXpander",
413 1.1 augustss "Spatializer 3D",
414 1.1 augustss "SRS 3D",
415 1.1 augustss "Platform Tech 3D",
416 1.1 augustss "AKM 3D",
417 1.1 augustss "Aureal",
418 1.1 augustss "AZTECH 3D",
419 1.1 augustss "Binaura 3D",
420 1.1 augustss "ESS Technology",
421 1.1 augustss "Harman International VMAx",
422 1.1 augustss "Nvidea 3D",
423 1.1 augustss "Philips Incredible Sound",
424 1.1 augustss "Texas Instruments' 3D",
425 1.1 augustss "VLSI Technology 3D",
426 1.1 augustss "TriTech 3D",
427 1.1 augustss "Realtek 3D",
428 1.1 augustss "Samsung 3D",
429 1.1 augustss "Wolfson Microelectronics 3D",
430 1.1 augustss "Delta Integration 3D",
431 1.1 augustss "SigmaTel 3D",
432 1.1 augustss "Unknown 3D",
433 1.1 augustss "Rockwell 3D",
434 1.1 augustss "Unknown 3D",
435 1.1 augustss "Unknown 3D",
436 1.1 augustss "Unknown 3D",
437 1.1 augustss };
438 1.1 augustss
439 1.18 jdolecek static const char * const ac97feature[] = {
440 1.27 kent "dedicated mic channel",
441 1.1 augustss "reserved",
442 1.1 augustss "tone",
443 1.1 augustss "simulated stereo",
444 1.1 augustss "headphone",
445 1.1 augustss "bass boost",
446 1.1 augustss "18 bit DAC",
447 1.1 augustss "20 bit DAC",
448 1.1 augustss "18 bit ADC",
449 1.1 augustss "20 bit ADC"
450 1.1 augustss };
451 1.1 augustss
452 1.1 augustss
453 1.18 jdolecek int ac97_str_equal __P((const char *, const char *));
454 1.1 augustss void ac97_setup_source_info __P((struct ac97_softc *));
455 1.14 thorpej void ac97_read __P((struct ac97_softc *, u_int8_t, u_int16_t *));
456 1.14 thorpej void ac97_setup_defaults __P((struct ac97_softc *));
457 1.14 thorpej int ac97_write __P((struct ac97_softc *, u_int8_t, u_int16_t));
458 1.1 augustss
459 1.1 augustss /* #define AC97_DEBUG 10 */
460 1.1 augustss
461 1.1 augustss #ifdef AUDIO_DEBUG
462 1.1 augustss #define DPRINTF(x) if (ac97debug) printf x
463 1.1 augustss #define DPRINTFN(n,x) if (ac97debug>(n)) printf x
464 1.1 augustss #ifdef AC97_DEBUG
465 1.1 augustss int ac97debug = AC97_DEBUG;
466 1.1 augustss #else
467 1.1 augustss int ac97debug = 0;
468 1.1 augustss #endif
469 1.1 augustss #else
470 1.1 augustss #define DPRINTF(x)
471 1.1 augustss #define DPRINTFN(n,x)
472 1.1 augustss #endif
473 1.1 augustss
474 1.14 thorpej void
475 1.14 thorpej ac97_read(as, reg, val)
476 1.14 thorpej struct ac97_softc *as;
477 1.14 thorpej u_int8_t reg;
478 1.14 thorpej u_int16_t *val;
479 1.14 thorpej {
480 1.14 thorpej int error;
481 1.14 thorpej
482 1.14 thorpej if (as->host_flags & AC97_HOST_DONT_READ &&
483 1.14 thorpej (reg != AC97_REG_VENDOR_ID1 && reg != AC97_REG_VENDOR_ID2 &&
484 1.14 thorpej reg != AC97_REG_RESET)) {
485 1.14 thorpej *val = as->shadow_reg[reg >> 1];
486 1.14 thorpej return;
487 1.14 thorpej }
488 1.14 thorpej
489 1.14 thorpej if ((error = as->host_if->read(as->host_if->arg, reg, val))) {
490 1.14 thorpej *val = as->shadow_reg[reg >> 1];
491 1.14 thorpej }
492 1.14 thorpej }
493 1.14 thorpej
494 1.14 thorpej int
495 1.14 thorpej ac97_write(as, reg, val)
496 1.14 thorpej struct ac97_softc *as;
497 1.14 thorpej u_int8_t reg;
498 1.14 thorpej u_int16_t val;
499 1.14 thorpej {
500 1.14 thorpej
501 1.14 thorpej as->shadow_reg[reg >> 1] = val;
502 1.14 thorpej
503 1.14 thorpej return (as->host_if->write(as->host_if->arg, reg, val));
504 1.14 thorpej }
505 1.14 thorpej
506 1.14 thorpej void
507 1.14 thorpej ac97_setup_defaults(as)
508 1.14 thorpej struct ac97_softc *as;
509 1.14 thorpej {
510 1.14 thorpej int idx;
511 1.18 jdolecek const struct ac97_source_info *si;
512 1.14 thorpej
513 1.14 thorpej memset(as->shadow_reg, 0, sizeof(as->shadow_reg));
514 1.14 thorpej
515 1.14 thorpej for (idx = 0; idx < SOURCE_INFO_SIZE; idx++) {
516 1.14 thorpej si = &source_info[idx];
517 1.14 thorpej ac97_write(as, si->reg, si->default_value);
518 1.14 thorpej }
519 1.14 thorpej }
520 1.14 thorpej
521 1.14 thorpej void
522 1.14 thorpej ac97_restore_shadow(self)
523 1.14 thorpej struct ac97_codec_if *self;
524 1.14 thorpej {
525 1.14 thorpej struct ac97_softc *as = (struct ac97_softc *) self;
526 1.14 thorpej int idx;
527 1.18 jdolecek const struct ac97_source_info *si;
528 1.14 thorpej
529 1.14 thorpej for (idx = 0; idx < SOURCE_INFO_SIZE; idx++) {
530 1.14 thorpej si = &source_info[idx];
531 1.14 thorpej ac97_write(as, si->reg, as->shadow_reg[si->reg >> 1]);
532 1.14 thorpej }
533 1.14 thorpej }
534 1.1 augustss
535 1.1 augustss int
536 1.1 augustss ac97_str_equal(a, b)
537 1.18 jdolecek const char *a, *b;
538 1.1 augustss {
539 1.1 augustss return ((a == b) || (a && b && (!strcmp(a, b))));
540 1.1 augustss }
541 1.1 augustss
542 1.1 augustss void
543 1.1 augustss ac97_setup_source_info(as)
544 1.1 augustss struct ac97_softc *as;
545 1.1 augustss {
546 1.1 augustss int idx, ouridx;
547 1.1 augustss struct ac97_source_info *si, *si2;
548 1.1 augustss
549 1.1 augustss for (idx = 0, ouridx = 0; idx < SOURCE_INFO_SIZE; idx++) {
550 1.1 augustss si = &as->source_info[ouridx];
551 1.1 augustss
552 1.20 thorpej memcpy(si, &source_info[idx], sizeof(*si));
553 1.1 augustss
554 1.1 augustss switch (si->type) {
555 1.1 augustss case AUDIO_MIXER_CLASS:
556 1.1 augustss si->mixer_class = ouridx;
557 1.1 augustss ouridx++;
558 1.1 augustss break;
559 1.1 augustss case AUDIO_MIXER_VALUE:
560 1.1 augustss /* Todo - Test to see if it works */
561 1.1 augustss ouridx++;
562 1.1 augustss
563 1.1 augustss /* Add an entry for mute, if necessary */
564 1.1 augustss if (si->mute) {
565 1.1 augustss si = &as->source_info[ouridx];
566 1.20 thorpej memcpy(si, &source_info[idx], sizeof(*si));
567 1.1 augustss si->qualifier = AudioNmute;
568 1.1 augustss si->type = AUDIO_MIXER_ENUM;
569 1.1 augustss si->info = &ac97_on_off;
570 1.1 augustss si->info_size = sizeof(ac97_on_off);
571 1.1 augustss si->bits = 1;
572 1.1 augustss si->ofs = 15;
573 1.1 augustss si->mute = 0;
574 1.1 augustss si->polarity = 0;
575 1.1 augustss ouridx++;
576 1.1 augustss }
577 1.1 augustss break;
578 1.1 augustss case AUDIO_MIXER_ENUM:
579 1.1 augustss /* Todo - Test to see if it works */
580 1.1 augustss ouridx++;
581 1.1 augustss break;
582 1.1 augustss default:
583 1.1 augustss printf ("ac97: shouldn't get here\n");
584 1.1 augustss break;
585 1.1 augustss }
586 1.1 augustss }
587 1.1 augustss
588 1.1 augustss as->num_source_info = ouridx;
589 1.1 augustss
590 1.1 augustss for (idx = 0; idx < as->num_source_info; idx++) {
591 1.1 augustss int idx2, previdx;
592 1.1 augustss
593 1.1 augustss si = &as->source_info[idx];
594 1.1 augustss
595 1.1 augustss /* Find mixer class */
596 1.1 augustss for (idx2 = 0; idx2 < as->num_source_info; idx2++) {
597 1.1 augustss si2 = &as->source_info[idx2];
598 1.1 augustss
599 1.1 augustss if (si2->type == AUDIO_MIXER_CLASS &&
600 1.1 augustss ac97_str_equal(si->class,
601 1.1 augustss si2->class)) {
602 1.1 augustss si->mixer_class = idx2;
603 1.1 augustss }
604 1.1 augustss }
605 1.1 augustss
606 1.1 augustss
607 1.1 augustss /* Setup prev and next pointers */
608 1.1 augustss if (si->prev != 0)
609 1.1 augustss continue;
610 1.1 augustss
611 1.1 augustss if (si->qualifier)
612 1.1 augustss continue;
613 1.1 augustss
614 1.1 augustss si->prev = AUDIO_MIXER_LAST;
615 1.1 augustss previdx = idx;
616 1.1 augustss
617 1.1 augustss for (idx2 = 0; idx2 < as->num_source_info; idx2++) {
618 1.1 augustss if (idx2 == idx)
619 1.1 augustss continue;
620 1.1 augustss
621 1.1 augustss si2 = &as->source_info[idx2];
622 1.1 augustss
623 1.1 augustss if (!si2->prev &&
624 1.1 augustss ac97_str_equal(si->class, si2->class) &&
625 1.1 augustss ac97_str_equal(si->device, si2->device)) {
626 1.1 augustss as->source_info[previdx].next = idx2;
627 1.1 augustss as->source_info[idx2].prev = previdx;
628 1.1 augustss
629 1.1 augustss previdx = idx2;
630 1.1 augustss }
631 1.1 augustss }
632 1.1 augustss
633 1.1 augustss as->source_info[previdx].next = AUDIO_MIXER_LAST;
634 1.1 augustss }
635 1.1 augustss }
636 1.1 augustss
637 1.28 kent int
638 1.14 thorpej ac97_attach(host_if)
639 1.14 thorpej struct ac97_host_if *host_if;
640 1.1 augustss {
641 1.1 augustss struct ac97_softc *as;
642 1.14 thorpej struct device *sc_dev = (struct device *)host_if->arg;
643 1.1 augustss int error, i, j;
644 1.1 augustss u_int32_t id;
645 1.28 kent u_int16_t id1, id2;
646 1.28 kent u_int16_t extstat, rate;
647 1.8 augustss mixer_ctrl_t ctl;
648 1.27 kent const char *delim;
649 1.28 kent
650 1.23 tsutsui as = malloc(sizeof(struct ac97_softc), M_DEVBUF, M_WAITOK|M_ZERO);
651 1.1 augustss
652 1.2 soren if (as == NULL)
653 1.2 soren return (ENOMEM);
654 1.14 thorpej
655 1.14 thorpej as->codec_if.vtbl = &ac97civ;
656 1.14 thorpej as->host_if = host_if;
657 1.1 augustss
658 1.14 thorpej if ((error = host_if->attach(host_if->arg, &as->codec_if))) {
659 1.1 augustss free (as, M_DEVBUF);
660 1.1 augustss return (error);
661 1.1 augustss }
662 1.1 augustss
663 1.14 thorpej host_if->reset(host_if->arg);
664 1.1 augustss
665 1.14 thorpej host_if->write(host_if->arg, AC97_REG_POWER, 0);
666 1.14 thorpej host_if->write(host_if->arg, AC97_REG_RESET, 0);
667 1.1 augustss
668 1.14 thorpej if (host_if->flags)
669 1.14 thorpej as->host_flags = host_if->flags(host_if->arg);
670 1.1 augustss
671 1.14 thorpej ac97_setup_defaults(as);
672 1.15 thorpej ac97_read(as, AC97_REG_VENDOR_ID1, &id1);
673 1.15 thorpej ac97_read(as, AC97_REG_VENDOR_ID2, &id2);
674 1.28 kent ac97_read(as, AC97_REG_RESET, &as->caps);
675 1.1 augustss
676 1.1 augustss id = (id1 << 16) | id2;
677 1.1 augustss
678 1.2 soren printf("%s: ", sc_dev->dv_xname);
679 1.2 soren
680 1.2 soren for (i = 0; ; i++) {
681 1.2 soren if (ac97codecid[i].id == 0) {
682 1.4 augustss char pnp[4];
683 1.7 thorpej
684 1.7 thorpej AC97_GET_CODEC_ID(id, pnp);
685 1.4 augustss #define ISASCII(c) ((c) >= ' ' && (c) < 0x7f)
686 1.4 augustss if (ISASCII(pnp[0]) && ISASCII(pnp[1]) &&
687 1.4 augustss ISASCII(pnp[2]))
688 1.7 thorpej printf("%c%c%c%d", pnp[0], pnp[1], pnp[2],
689 1.7 thorpej pnp[3]);
690 1.4 augustss else
691 1.12 soren printf("unknown (0x%08x)", id);
692 1.11 soren break;
693 1.11 soren }
694 1.31 kent if (ac97codecid[i].id == (id & ac97codecid[i].mask)) {
695 1.11 soren printf("%s", ac97codecid[i].name);
696 1.2 soren break;
697 1.2 soren }
698 1.1 augustss }
699 1.2 soren printf(" codec; ");
700 1.1 augustss for (i = j = 0; i < 10; i++) {
701 1.28 kent if (as->caps & (1 << i)) {
702 1.1 augustss printf("%s%s", j? ", " : "", ac97feature[i]);
703 1.1 augustss j++;
704 1.1 augustss }
705 1.1 augustss }
706 1.28 kent printf("%s%s\n", j ? ", " : "",
707 1.28 kent ac97enhancement[(as->caps >> 10) & 0x1f]);
708 1.1 augustss
709 1.28 kent as->ac97_clock = AC97_STANDARD_CLOCK;
710 1.28 kent ac97_read(as, AC97_REG_EXT_AUDIO_ID, &as->ext_id);
711 1.28 kent if (as->ext_id & (AC97_EXT_AUDIO_VRA | AC97_EXT_AUDIO_DRA
712 1.28 kent | AC97_EXT_AUDIO_SPDIF | AC97_EXT_AUDIO_VRM
713 1.28 kent | AC97_EXT_AUDIO_CDAC | AC97_EXT_AUDIO_SDAC
714 1.28 kent | AC97_EXT_AUDIO_LDAC)) {
715 1.27 kent printf("%s:", sc_dev->dv_xname);
716 1.27 kent delim = "";
717 1.27 kent
718 1.28 kent if (as->ext_id & AC97_EXT_AUDIO_VRA) {
719 1.27 kent printf("%s variable rate audio", delim);
720 1.27 kent delim = ",";
721 1.27 kent }
722 1.28 kent if (as->ext_id & AC97_EXT_AUDIO_DRA) {
723 1.27 kent printf("%s double rate output", delim);
724 1.27 kent delim = ",";
725 1.27 kent }
726 1.28 kent if (as->ext_id & AC97_EXT_AUDIO_SPDIF) {
727 1.27 kent printf("%s S/PDIF", delim);
728 1.27 kent delim = ",";
729 1.27 kent }
730 1.28 kent if (as->ext_id & AC97_EXT_AUDIO_VRM) {
731 1.27 kent printf("%s variable rate dedicated mic", delim);
732 1.27 kent delim = ",";
733 1.27 kent }
734 1.28 kent if (as->ext_id & AC97_EXT_AUDIO_CDAC) {
735 1.27 kent printf("%s center DAC", delim);
736 1.27 kent delim = ",";
737 1.27 kent }
738 1.28 kent if (as->ext_id & AC97_EXT_AUDIO_SDAC) {
739 1.27 kent printf("%s surround DAC", delim);
740 1.27 kent delim = ",";
741 1.27 kent }
742 1.28 kent if (as->ext_id & AC97_EXT_AUDIO_LDAC) {
743 1.27 kent printf("%s LFE DAC", delim);
744 1.27 kent }
745 1.27 kent printf("\n");
746 1.28 kent
747 1.28 kent /* If VRA and/or VRM capablities, enable them. */
748 1.28 kent if (as->ext_id & (AC97_EXT_AUDIO_VRA | AC97_EXT_AUDIO_VRM)) {
749 1.28 kent ac97_read(as, AC97_REG_EXT_AUDIO_CTRL, &extstat);
750 1.28 kent if (as->ext_id & AC97_EXT_AUDIO_VRA) {
751 1.28 kent extstat |= AC97_EXT_AUDIO_VRA;
752 1.28 kent }
753 1.28 kent if (as->ext_id & AC97_EXT_AUDIO_VRM) {
754 1.28 kent extstat |= AC97_EXT_AUDIO_VRM;
755 1.28 kent }
756 1.28 kent ac97_write(as, AC97_REG_EXT_AUDIO_CTRL, extstat);
757 1.28 kent
758 1.28 kent /* so it claims to do variable rate, let's make sure */
759 1.28 kent ac97_write(as, AC97_REG_PCM_FRONT_DAC_RATE, 44100);
760 1.28 kent ac97_read(as, AC97_REG_PCM_FRONT_DAC_RATE, &rate);
761 1.28 kent if (rate != 44100) {
762 1.28 kent /* We can't believe ext_id */
763 1.28 kent as->ext_id = 0;
764 1.28 kent printf("%s: Ignore these capabilities.\n",
765 1.28 kent sc_dev->dv_xname);
766 1.28 kent }
767 1.32 kent /* restore the default value */
768 1.32 kent ac97_write(as, AC97_REG_PCM_FRONT_DAC_RATE,
769 1.32 kent AC97_SINGLE_RATE);
770 1.28 kent }
771 1.27 kent }
772 1.1 augustss
773 1.1 augustss ac97_setup_source_info(as);
774 1.8 augustss
775 1.8 augustss /* Just enable the DAC and master volumes by default */
776 1.8 augustss memset(&ctl, 0, sizeof(ctl));
777 1.8 augustss
778 1.8 augustss ctl.type = AUDIO_MIXER_ENUM;
779 1.8 augustss ctl.un.ord = 0; /* off */
780 1.14 thorpej ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCoutputs,
781 1.8 augustss AudioNmaster, AudioNmute);
782 1.14 thorpej ac97_mixer_set_port(&as->codec_if, &ctl);
783 1.14 thorpej ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCinputs,
784 1.8 augustss AudioNdac, AudioNmute);
785 1.8 augustss
786 1.14 thorpej ac97_mixer_set_port(&as->codec_if, &ctl);
787 1.14 thorpej ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCrecord,
788 1.8 augustss AudioNvolume, AudioNmute);
789 1.14 thorpej ac97_mixer_set_port(&as->codec_if, &ctl);
790 1.8 augustss
791 1.14 thorpej ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCrecord,
792 1.8 augustss AudioNsource, NULL);
793 1.8 augustss ctl.type = AUDIO_MIXER_ENUM;
794 1.8 augustss ctl.un.ord = 0;
795 1.14 thorpej ac97_mixer_set_port(&as->codec_if, &ctl);
796 1.1 augustss
797 1.1 augustss return (0);
798 1.1 augustss }
799 1.1 augustss
800 1.1 augustss
801 1.1 augustss int
802 1.1 augustss ac97_query_devinfo(codec_if, dip)
803 1.1 augustss struct ac97_codec_if *codec_if;
804 1.1 augustss mixer_devinfo_t *dip;
805 1.1 augustss {
806 1.1 augustss struct ac97_softc *as = (struct ac97_softc *)codec_if;
807 1.1 augustss
808 1.1 augustss if (dip->index < as->num_source_info) {
809 1.1 augustss struct ac97_source_info *si = &as->source_info[dip->index];
810 1.18 jdolecek const char *name;
811 1.1 augustss
812 1.1 augustss dip->type = si->type;
813 1.1 augustss dip->mixer_class = si->mixer_class;
814 1.1 augustss dip->prev = si->prev;
815 1.1 augustss dip->next = si->next;
816 1.1 augustss
817 1.1 augustss if (si->qualifier)
818 1.1 augustss name = si->qualifier;
819 1.1 augustss else if (si->device)
820 1.1 augustss name = si->device;
821 1.1 augustss else if (si->class)
822 1.1 augustss name = si->class;
823 1.6 augustss else
824 1.6 augustss name = 0;
825 1.1 augustss
826 1.1 augustss if (name)
827 1.1 augustss strcpy(dip->label.name, name);
828 1.1 augustss
829 1.20 thorpej memcpy(&dip->un, si->info, si->info_size);
830 1.19 erh
831 1.19 erh /* Set the delta for volume sources */
832 1.19 erh if (dip->type == AUDIO_MIXER_VALUE)
833 1.19 erh dip->un.v.delta = 1 << (8 - si->bits);
834 1.19 erh
835 1.1 augustss return (0);
836 1.1 augustss }
837 1.1 augustss
838 1.1 augustss return (ENXIO);
839 1.1 augustss }
840 1.1 augustss
841 1.1 augustss
842 1.1 augustss
843 1.1 augustss int
844 1.1 augustss ac97_mixer_set_port(codec_if, cp)
845 1.1 augustss struct ac97_codec_if *codec_if;
846 1.1 augustss mixer_ctrl_t *cp;
847 1.1 augustss {
848 1.1 augustss struct ac97_softc *as = (struct ac97_softc *)codec_if;
849 1.1 augustss struct ac97_source_info *si = &as->source_info[cp->dev];
850 1.1 augustss u_int16_t mask;
851 1.1 augustss u_int16_t val, newval;
852 1.1 augustss int error;
853 1.1 augustss
854 1.1 augustss if (cp->dev < 0 || cp->dev >= as->num_source_info)
855 1.1 augustss return (EINVAL);
856 1.1 augustss
857 1.1 augustss if (cp->type != si->type)
858 1.1 augustss return (EINVAL);
859 1.1 augustss
860 1.15 thorpej ac97_read(as, si->reg, &val);
861 1.1 augustss
862 1.1 augustss DPRINTFN(5, ("read(%x) = %x\n", si->reg, val));
863 1.1 augustss
864 1.1 augustss mask = (1 << si->bits) - 1;
865 1.1 augustss
866 1.1 augustss switch (cp->type) {
867 1.1 augustss case AUDIO_MIXER_ENUM:
868 1.1 augustss if (cp->un.ord > mask || cp->un.ord < 0)
869 1.1 augustss return (EINVAL);
870 1.1 augustss
871 1.1 augustss newval = (cp->un.ord << si->ofs);
872 1.1 augustss if (si->reg == AC97_REG_RECORD_SELECT) {
873 1.1 augustss newval |= (newval << (8 + si->ofs));
874 1.1 augustss mask |= (mask << 8);
875 1.1 augustss }
876 1.1 augustss break;
877 1.1 augustss case AUDIO_MIXER_VALUE:
878 1.1 augustss {
879 1.18 jdolecek const struct audio_mixer_value *value = si->info;
880 1.1 augustss u_int16_t l, r;
881 1.1 augustss
882 1.1 augustss if ((cp->un.value.num_channels <= 0) ||
883 1.1 augustss (cp->un.value.num_channels > value->num_channels))
884 1.1 augustss return (EINVAL);
885 1.1 augustss
886 1.1 augustss if (cp->un.value.num_channels == 1) {
887 1.1 augustss l = r = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
888 1.1 augustss } else {
889 1.17 rh if (!(as->host_flags & AC97_HOST_SWAPPED_CHANNELS)) {
890 1.17 rh l = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
891 1.17 rh r = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
892 1.17 rh } else { /* left/right is reversed here */
893 1.17 rh r = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
894 1.17 rh l = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
895 1.17 rh }
896 1.17 rh
897 1.1 augustss }
898 1.1 augustss
899 1.1 augustss if (!si->polarity) {
900 1.1 augustss l = 255 - l;
901 1.1 augustss r = 255 - r;
902 1.1 augustss }
903 1.1 augustss
904 1.1 augustss l = l >> (8 - si->bits);
905 1.1 augustss r = r >> (8 - si->bits);
906 1.1 augustss
907 1.1 augustss newval = ((l & mask) << si->ofs);
908 1.1 augustss if (value->num_channels == 2) {
909 1.1 augustss newval |= ((r & mask) << (si->ofs + 8));
910 1.1 augustss mask |= (mask << 8);
911 1.1 augustss }
912 1.1 augustss
913 1.1 augustss break;
914 1.1 augustss }
915 1.1 augustss default:
916 1.1 augustss return (EINVAL);
917 1.1 augustss }
918 1.1 augustss
919 1.1 augustss mask = mask << si->ofs;
920 1.15 thorpej error = ac97_write(as, si->reg, (val & ~mask) | newval);
921 1.1 augustss if (error)
922 1.1 augustss return (error);
923 1.1 augustss
924 1.1 augustss return (0);
925 1.1 augustss }
926 1.1 augustss
927 1.1 augustss int
928 1.1 augustss ac97_get_portnum_by_name(codec_if, class, device, qualifier)
929 1.1 augustss struct ac97_codec_if *codec_if;
930 1.1 augustss char *class, *device, *qualifier;
931 1.1 augustss {
932 1.1 augustss struct ac97_softc *as = (struct ac97_softc *)codec_if;
933 1.1 augustss int idx;
934 1.1 augustss
935 1.1 augustss for (idx = 0; idx < as->num_source_info; idx++) {
936 1.1 augustss struct ac97_source_info *si = &as->source_info[idx];
937 1.1 augustss if (ac97_str_equal(class, si->class) &&
938 1.1 augustss ac97_str_equal(device, si->device) &&
939 1.1 augustss ac97_str_equal(qualifier, si->qualifier))
940 1.1 augustss return (idx);
941 1.1 augustss }
942 1.1 augustss
943 1.1 augustss return (-1);
944 1.1 augustss }
945 1.1 augustss
946 1.1 augustss int
947 1.1 augustss ac97_mixer_get_port(codec_if, cp)
948 1.1 augustss struct ac97_codec_if *codec_if;
949 1.1 augustss mixer_ctrl_t *cp;
950 1.1 augustss {
951 1.1 augustss struct ac97_softc *as = (struct ac97_softc *)codec_if;
952 1.1 augustss struct ac97_source_info *si = &as->source_info[cp->dev];
953 1.1 augustss u_int16_t mask;
954 1.1 augustss u_int16_t val;
955 1.1 augustss
956 1.1 augustss if (cp->dev < 0 || cp->dev >= as->num_source_info)
957 1.1 augustss return (EINVAL);
958 1.1 augustss
959 1.1 augustss if (cp->type != si->type)
960 1.1 augustss return (EINVAL);
961 1.1 augustss
962 1.15 thorpej ac97_read(as, si->reg, &val);
963 1.1 augustss
964 1.1 augustss DPRINTFN(5, ("read(%x) = %x\n", si->reg, val));
965 1.1 augustss
966 1.1 augustss mask = (1 << si->bits) - 1;
967 1.1 augustss
968 1.1 augustss switch (cp->type) {
969 1.1 augustss case AUDIO_MIXER_ENUM:
970 1.1 augustss cp->un.ord = (val >> si->ofs) & mask;
971 1.1 augustss DPRINTFN(4, ("AUDIO_MIXER_ENUM: %x %d %x %d\n", val, si->ofs, mask, cp->un.ord));
972 1.1 augustss break;
973 1.1 augustss case AUDIO_MIXER_VALUE:
974 1.1 augustss {
975 1.18 jdolecek const struct audio_mixer_value *value = si->info;
976 1.1 augustss u_int16_t l, r;
977 1.1 augustss
978 1.1 augustss if ((cp->un.value.num_channels <= 0) ||
979 1.1 augustss (cp->un.value.num_channels > value->num_channels))
980 1.1 augustss return (EINVAL);
981 1.1 augustss
982 1.1 augustss if (value->num_channels == 1) {
983 1.1 augustss l = r = (val >> si->ofs) & mask;
984 1.1 augustss } else {
985 1.17 rh if (!(as->host_flags & AC97_HOST_SWAPPED_CHANNELS)) {
986 1.17 rh l = (val >> si->ofs) & mask;
987 1.17 rh r = (val >> (si->ofs + 8)) & mask;
988 1.17 rh } else { /* host has reversed channels */
989 1.17 rh r = (val >> si->ofs) & mask;
990 1.17 rh l = (val >> (si->ofs + 8)) & mask;
991 1.17 rh }
992 1.1 augustss }
993 1.1 augustss
994 1.1 augustss l = (l << (8 - si->bits));
995 1.1 augustss r = (r << (8 - si->bits));
996 1.1 augustss if (!si->polarity) {
997 1.1 augustss l = 255 - l;
998 1.1 augustss r = 255 - r;
999 1.1 augustss }
1000 1.1 augustss
1001 1.1 augustss /* The EAP driver averages l and r for stereo
1002 1.1 augustss channels that are requested in MONO mode. Does this
1003 1.1 augustss make sense? */
1004 1.1 augustss if (cp->un.value.num_channels == 1) {
1005 1.1 augustss cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = l;
1006 1.1 augustss } else if (cp->un.value.num_channels == 2) {
1007 1.1 augustss cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
1008 1.1 augustss cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
1009 1.1 augustss }
1010 1.1 augustss
1011 1.1 augustss break;
1012 1.1 augustss }
1013 1.1 augustss default:
1014 1.1 augustss return (EINVAL);
1015 1.1 augustss }
1016 1.1 augustss
1017 1.1 augustss return (0);
1018 1.1 augustss }
1019 1.1 augustss
1020 1.28 kent
1021 1.28 kent int
1022 1.28 kent ac97_set_rate(struct ac97_codec_if *codec_if, int target, u_long *rate)
1023 1.28 kent {
1024 1.28 kent struct ac97_softc *as;
1025 1.28 kent u_long value;
1026 1.28 kent u_int16_t ext_stat;
1027 1.28 kent u_int16_t actual;
1028 1.28 kent u_int16_t power;
1029 1.28 kent u_int16_t power_bit;
1030 1.28 kent
1031 1.28 kent as = (struct ac97_softc *)codec_if;
1032 1.29 kent if (target == AC97_REG_PCM_MIC_ADC_RATE) {
1033 1.29 kent if (!(as->ext_id & AC97_EXT_AUDIO_VRM)) {
1034 1.29 kent *rate = AC97_SINGLE_RATE;
1035 1.29 kent return 0;
1036 1.29 kent }
1037 1.29 kent } else {
1038 1.29 kent if (!(as->ext_id & AC97_EXT_AUDIO_VRA)) {
1039 1.29 kent *rate = AC97_SINGLE_RATE;
1040 1.29 kent return 0;
1041 1.29 kent }
1042 1.29 kent }
1043 1.28 kent value = *rate * AC97_STANDARD_CLOCK / as->ac97_clock;
1044 1.28 kent ext_stat = 0;
1045 1.28 kent /*
1046 1.28 kent * PCM_FRONT_DAC_RATE/PCM_SURR_DAC_RATE/PCM_LFE_DAC_RATE
1047 1.28 kent * Check VRA, DRA
1048 1.28 kent * PCM_LR_ADC_RATE
1049 1.28 kent * Check VRA
1050 1.28 kent * PCM_MIC_ADC_RATE
1051 1.28 kent * Check VRM
1052 1.28 kent */
1053 1.28 kent switch (target) {
1054 1.28 kent case AC97_REG_PCM_FRONT_DAC_RATE:
1055 1.28 kent case AC97_REG_PCM_SURR_DAC_RATE:
1056 1.28 kent case AC97_REG_PCM_LFE_DAC_RATE:
1057 1.28 kent power_bit = AC97_POWER_OUT;
1058 1.28 kent if (!(as->ext_id & AC97_EXT_AUDIO_VRA)) {
1059 1.28 kent *rate = AC97_SINGLE_RATE;
1060 1.28 kent return 0;
1061 1.28 kent }
1062 1.28 kent if (as->ext_id & AC97_EXT_AUDIO_DRA) {
1063 1.28 kent ac97_read(as, AC97_REG_EXT_AUDIO_CTRL, &ext_stat);
1064 1.28 kent if (value > 0x1ffff) {
1065 1.28 kent return EINVAL;
1066 1.28 kent } else if (value > 0xffff) {
1067 1.28 kent /* Enable DRA */
1068 1.28 kent ext_stat |= AC97_EXT_AUDIO_DRA;
1069 1.28 kent ac97_write(as, AC97_REG_EXT_AUDIO_CTRL, ext_stat);
1070 1.28 kent value /= 2;
1071 1.28 kent } else {
1072 1.28 kent /* Disable DRA */
1073 1.28 kent ext_stat &= ~AC97_EXT_AUDIO_DRA;
1074 1.28 kent ac97_write(as, AC97_REG_EXT_AUDIO_CTRL, ext_stat);
1075 1.28 kent }
1076 1.28 kent } else {
1077 1.28 kent if (value > 0xffff)
1078 1.28 kent return EINVAL;
1079 1.28 kent }
1080 1.28 kent break;
1081 1.28 kent case AC97_REG_PCM_LR_ADC_RATE:
1082 1.28 kent power_bit = AC97_POWER_IN;
1083 1.28 kent if (!(as->ext_id & AC97_EXT_AUDIO_VRA)) {
1084 1.28 kent *rate = AC97_SINGLE_RATE;
1085 1.28 kent return 0;
1086 1.28 kent }
1087 1.28 kent if (value > 0xffff)
1088 1.28 kent return EINVAL;
1089 1.28 kent break;
1090 1.28 kent case AC97_REG_PCM_MIC_ADC_RATE:
1091 1.28 kent power_bit = AC97_POWER_IN;
1092 1.28 kent if (!(as->ext_id & AC97_EXT_AUDIO_VRM)) {
1093 1.28 kent *rate = AC97_SINGLE_RATE;
1094 1.28 kent return 0;
1095 1.28 kent }
1096 1.28 kent if (value > 0xffff)
1097 1.28 kent return EINVAL;
1098 1.28 kent break;
1099 1.28 kent default:
1100 1.28 kent printf("%s: Unknown register: 0x%x\n", __func__, target);
1101 1.28 kent return EINVAL;
1102 1.28 kent }
1103 1.28 kent
1104 1.28 kent ac97_read(as, AC97_REG_POWER, &power);
1105 1.28 kent ac97_write(as, AC97_REG_POWER, power | power_bit);
1106 1.28 kent
1107 1.28 kent ac97_write(as, target, (u_int16_t)value);
1108 1.28 kent ac97_read(as, target, &actual);
1109 1.28 kent actual = (u_int32_t)actual * as->ac97_clock / AC97_STANDARD_CLOCK;
1110 1.28 kent
1111 1.28 kent ac97_write(as, AC97_REG_POWER, power);
1112 1.28 kent if (ext_stat & AC97_EXT_AUDIO_DRA) {
1113 1.28 kent *rate = actual * 2;
1114 1.28 kent } else {
1115 1.28 kent *rate = actual;
1116 1.28 kent }
1117 1.28 kent return 0;
1118 1.28 kent }
1119 1.28 kent
1120 1.28 kent void
1121 1.28 kent ac97_set_clock(struct ac97_codec_if *codec_if, unsigned int clock)
1122 1.28 kent {
1123 1.28 kent struct ac97_softc *as;
1124 1.28 kent
1125 1.28 kent as = (struct ac97_softc *)codec_if;
1126 1.28 kent as->ac97_clock = clock;
1127 1.28 kent }
1128 1.28 kent
1129 1.28 kent u_int16_t
1130 1.28 kent ac97_get_extcaps(struct ac97_codec_if *codec_if)
1131 1.28 kent {
1132 1.28 kent struct ac97_softc *as;
1133 1.28 kent
1134 1.28 kent as = (struct ac97_softc *)codec_if;
1135 1.28 kent return as->ext_id;
1136 1.28 kent }
1137