1 /* $NetBSD: ad1848.c,v 1.28 2008/04/28 18:49:27 garbled Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ken Hornstein and John Kohl.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 1994 John Brezak
33 * Copyright (c) 1991-1993 Regents of the University of California.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the Computer Systems
47 * Engineering Group at Lawrence Berkeley Laboratory.
48 * 4. Neither the name of the University nor of the Laboratory may be used
49 * to endorse or promote products derived from this software without
50 * specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * Copyright by Hannu Savolainen 1994
69 * Redistribution and use in source and binary forms, with or without
70 * modification, are permitted provided that the following conditions are
71 * met: 1. Redistributions of source code must retain the above copyright
72 * notice, this list of conditions and the following disclaimer. 2.
73 * Redistributions in binary form must reproduce the above copyright notice,
74 * this list of conditions and the following disclaimer in the documentation
75 * and/or other materials provided with the distribution.
77 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
78 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
79 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
80 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
81 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
83 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
84 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 * Portions of this code are from the VOXware support for the ad1848
92 * by Hannu Savolainen <hannu@voxware.pp.fi>
94 * Portions also supplied from the SoundBlaster driver for NetBSD.
97 #include <sys/cdefs.h>
98 __KERNEL_RCSID(0, "$NetBSD: ad1848.c,v 1.28 2008/04/28 18:49:27 garbled Exp $");
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/errno.h>
103 #include <sys/ioctl.h>
104 #include <sys/device.h>
105 #include <sys/fcntl.h>
106 /*#include <sys/syslog.h>*/
107 /*#include <sys/proc.h>*/
112 #include <sys/audioio.h>
114 #include <dev/audio_if.h>
115 #include <dev/auconv.h>
117 #include <dev/ic/ad1848reg.h>
118 #include <dev/ic/cs4231reg.h>
119 #include <dev/ic/cs4237reg.h>
120 #include <dev/ic/ad1848var.h>
122 #include <dev/isa/cs4231var.h>
126 * AD1845 on some machines don't match the AD1845 doc
127 * and defining AD1845_HACK to 1 works around the problems.
128 * options AD1845_HACK=0 should work if you have ``correct'' one.
131 #define AD1845_HACK 1 /* weird mixer, can't play slinear_be */
135 #define DPRINTF(x) if (ad1848debug) printf x
137 void ad1848_dump_regs(struct ad1848_softc
*);
143 * Initial values for the indirect registers of CS4248/AD1848.
145 static const int ad1848_init_values
[] = {
146 GAIN_12
|INPUT_MIC_GAIN_ENABLE
, /* Left Input Control */
147 GAIN_12
|INPUT_MIC_GAIN_ENABLE
, /* Right Input Control */
148 ATTEN_12
, /* Left Aux #1 Input Control */
149 ATTEN_12
, /* Right Aux #1 Input Control */
150 ATTEN_12
, /* Left Aux #2 Input Control */
151 ATTEN_12
, /* Right Aux #2 Input Control */
152 /* bits 5-0 are attenuation select */
153 ATTEN_12
, /* Left DAC output Control */
154 ATTEN_12
, /* Right DAC output Control */
155 CLOCK_XTAL1
|FMT_PCM8
, /* Clock and Data Format */
156 SINGLE_DMA
|AUTO_CAL_ENABLE
, /* Interface Config */
157 INTERRUPT_ENABLE
, /* Pin control */
158 0x00, /* Test and Init */
159 MODE2
, /* Misc control */
160 ATTEN_0
<<2, /* Digital Mix Control */
161 0, /* Upper base Count */
162 0, /* Lower base Count */
164 /* These are for CS4231 &c. only (additional registers): */
165 0, /* Alt feature 1 */
166 0, /* Alt feature 2 */
167 ATTEN_12
, /* Left line in */
168 ATTEN_12
, /* Right line in */
175 /* Mono input (a.k.a speaker) (mic) Control */
176 MONO_INPUT_MUTE
|ATTEN_6
, /* mute speaker by default */
178 0, /* record format */
179 0, /* Crystal Clock Select */
180 0, /* upper record count */
181 0 /* lower record count */
186 ad1848_to_vol(mixer_ctrl_t
*cp
, struct ad1848_volume
*vol
)
189 if (cp
->un
.value
.num_channels
== 1) {
191 vol
->right
= cp
->un
.value
.level
[AUDIO_MIXER_LEVEL_MONO
];
194 else if (cp
->un
.value
.num_channels
== 2) {
195 vol
->left
= cp
->un
.value
.level
[AUDIO_MIXER_LEVEL_LEFT
];
196 vol
->right
= cp
->un
.value
.level
[AUDIO_MIXER_LEVEL_RIGHT
];
203 ad1848_from_vol(mixer_ctrl_t
*cp
, struct ad1848_volume
*vol
)
206 if (cp
->un
.value
.num_channels
== 1) {
207 cp
->un
.value
.level
[AUDIO_MIXER_LEVEL_MONO
] = vol
->left
;
210 else if (cp
->un
.value
.num_channels
== 2) {
211 cp
->un
.value
.level
[AUDIO_MIXER_LEVEL_LEFT
] = vol
->left
;
212 cp
->un
.value
.level
[AUDIO_MIXER_LEVEL_RIGHT
] = vol
->right
;
220 ad_read(struct ad1848_softc
*sc
, int reg
)
224 ADWRITE(sc
, AD1848_IADDR
, (reg
& 0xff) | sc
->MCE_bit
);
225 x
= ADREAD(sc
, AD1848_IDATA
);
226 /* printf("(%02x<-%02x) ", reg|sc->MCE_bit, x); */
231 ad_write(struct ad1848_softc
*sc
, int reg
, int data
)
234 ADWRITE(sc
, AD1848_IADDR
, (reg
& 0xff) | sc
->MCE_bit
);
235 ADWRITE(sc
, AD1848_IDATA
, data
& 0xff);
236 /* printf("(%02x->%02x) ", reg|sc->MCE_bit, data); */
240 * extended registers (mode 3) require an additional level of
241 * indirection through CS_XREG (I23).
245 ad_xread(struct ad1848_softc
*sc
, int reg
)
249 ADWRITE(sc
, AD1848_IADDR
, CS_XREG
| sc
->MCE_bit
);
250 ADWRITE(sc
, AD1848_IDATA
, (reg
| ALT_F3_XRAE
) & 0xff);
251 x
= ADREAD(sc
, AD1848_IDATA
);
257 ad_xwrite(struct ad1848_softc
*sc
, int reg
, int val
)
260 ADWRITE(sc
, AD1848_IADDR
, CS_XREG
| sc
->MCE_bit
);
261 ADWRITE(sc
, AD1848_IDATA
, (reg
| ALT_F3_XRAE
) & 0xff);
262 ADWRITE(sc
, AD1848_IDATA
, val
& 0xff);
266 ad_set_MCE(struct ad1848_softc
*sc
, int state
)
270 sc
->MCE_bit
= MODE_CHANGE_ENABLE
;
273 ADWRITE(sc
, AD1848_IADDR
, sc
->MCE_bit
);
277 wait_for_calibration(struct ad1848_softc
*sc
)
281 DPRINTF(("ad1848: Auto calibration started.\n"));
283 * Wait until the auto calibration process has finished.
285 * 1) Wait until the chip becomes ready (reads don't return 0x80).
286 * 2) Wait until the ACI bit of I11 gets on and then off.
287 * Because newer chips are fast we may never see the ACI
288 * bit go on. Just delay a little instead.
291 while (timeout
> 0 && ADREAD(sc
, AD1848_IADDR
) == SP_IN_INIT
) {
296 DPRINTF(("ad1848: Auto calibration timed out(1).\n"));
299 /* Set register addr */
300 ADWRITE(sc
, AD1848_IADDR
, SP_TEST_AND_INIT
);
301 /* Wait for address to appear when read back. */
303 while (timeout
> 0 &&
304 (ADREAD(sc
, AD1848_IADDR
)&SP_IADDR_MASK
) != SP_TEST_AND_INIT
) {
309 DPRINTF(("ad1848: Auto calibration timed out(1.5).\n"));
312 if (!(ad_read(sc
, SP_TEST_AND_INIT
) & AUTO_CAL_IN_PROG
)) {
314 /* A new chip, just delay a little. */
315 delay(100); /* XXX what should it be? */
318 while (timeout
> 0 &&
319 !(ad_read(sc
, SP_TEST_AND_INIT
) &
325 DPRINTF(("ad1848: Auto calibration timed out(2).\n"));
331 while (timeout
> 0 &&
332 ad_read(sc
, SP_TEST_AND_INIT
) & AUTO_CAL_IN_PROG
) {
337 DPRINTF(("ad1848: Auto calibration timed out(3).\n"));
343 ad1848_dump_regs(struct ad1848_softc
*sc
)
348 printf("ad1848 status=%02x", ADREAD(sc
, AD1848_STATUS
));
350 for (i
= 0; i
< 16; i
++) {
355 for (i
= 16; i
< 32; i
++) {
362 #endif /* AUDIO_DEBUG */
366 * Attach hardware to driver, attach hardware driver to audio
367 * pseudo-device driver .
370 ad1848_attach(struct ad1848_softc
*sc
)
372 static struct ad1848_volume vol_mid
= {220, 220};
373 static struct ad1848_volume vol_0
= {0, 0};
377 /* Initialize the ad1848... */
378 for (i
= 0; i
< 0x10; i
++) {
379 ad_write(sc
, i
, ad1848_init_values
[i
]);
381 while (timeout
> 0 && ADREAD(sc
, AD1848_IADDR
) & SP_IN_INIT
)
384 /* ...and additional CS4231 stuff too */
386 ad_write(sc
, SP_INTERFACE_CONFIG
, 0); /* disable SINGLE_DMA */
387 for (i
= 0x10; i
< 0x20; i
++)
388 if (ad1848_init_values
[i
] != 0) {
389 ad_write(sc
, i
, ad1848_init_values
[i
]);
391 while (timeout
> 0 &&
392 ADREAD(sc
, AD1848_IADDR
) & SP_IN_INIT
)
398 /* Set default gains */
399 ad1848_set_rec_gain(sc
, &vol_mid
);
400 ad1848_set_channel_gain(sc
, AD1848_DAC_CHANNEL
, &vol_mid
);
401 ad1848_set_channel_gain(sc
, AD1848_MONITOR_CHANNEL
, &vol_0
);
402 ad1848_set_channel_gain(sc
, AD1848_AUX1_CHANNEL
, &vol_mid
); /* CD volume */
403 sc
->mute
[AD1848_MONITOR_CHANNEL
] = MUTE_ALL
;
406 && sc
->is_ad1845
== 0
409 ad1848_set_channel_gain(sc
, AD1848_AUX2_CHANNEL
, &vol_mid
); /* CD volume */
410 ad1848_set_channel_gain(sc
, AD1848_LINE_CHANNEL
, &vol_mid
);
411 ad1848_set_channel_gain(sc
, AD1848_MONO_CHANNEL
, &vol_0
);
412 sc
->mute
[AD1848_MONO_CHANNEL
] = MUTE_ALL
;
414 ad1848_set_channel_gain(sc
, AD1848_AUX2_CHANNEL
, &vol_0
);
416 /* Set default port */
417 ad1848_set_rec_port(sc
, MIC_IN_PORT
);
419 printf(": %s", sc
->chip_name
);
423 * Various routines to interface to higher level audio driver
425 static const struct ad1848_mixerinfo
{
430 } mixer_channel_info
[] =
432 { SP_LEFT_AUX2_CONTROL
, SP_RIGHT_AUX2_CONTROL
, AUX_INPUT_ATTEN_BITS
,
433 AUX_INPUT_ATTEN_MASK
},
434 { SP_LEFT_AUX1_CONTROL
, SP_RIGHT_AUX1_CONTROL
, AUX_INPUT_ATTEN_BITS
,
435 AUX_INPUT_ATTEN_MASK
},
436 { SP_LEFT_OUTPUT_CONTROL
, SP_RIGHT_OUTPUT_CONTROL
,
437 OUTPUT_ATTEN_BITS
, OUTPUT_ATTEN_MASK
},
438 { CS_LEFT_LINE_CONTROL
, CS_RIGHT_LINE_CONTROL
, LINE_INPUT_ATTEN_BITS
,
439 LINE_INPUT_ATTEN_MASK
},
440 { CS_MONO_IO_CONTROL
, 0, MONO_INPUT_ATTEN_BITS
, MONO_INPUT_ATTEN_MASK
},
441 { CS_MONO_IO_CONTROL
, 0, 0, 0 },
442 { SP_DIGITAL_MIX
, 0, OUTPUT_ATTEN_BITS
, MIX_ATTEN_MASK
}
446 * This function doesn't set the mute flags but does use them.
447 * The mute flags reflect the mutes that have been applied by the user.
448 * However, the driver occasionally wants to mute devices (e.g. when chaing
449 * sampling rate). These operations should not affect the mute flags.
453 ad1848_mute_channel(struct ad1848_softc
*sc
, int device
, int mute
)
457 reg
= ad_read(sc
, mixer_channel_info
[device
].left_reg
);
459 if (mute
& MUTE_LEFT
) {
460 if (device
== AD1848_MONITOR_CHANNEL
) {
461 if (sc
->open_mode
& FREAD
)
462 ad1848_mute_wave_output(sc
, WAVE_UNMUTE1
, 0);
463 ad_write(sc
, mixer_channel_info
[device
].left_reg
,
464 reg
& ~DIGITAL_MIX1_ENABLE
);
465 } else if (device
== AD1848_OUT_CHANNEL
)
466 ad_write(sc
, mixer_channel_info
[device
].left_reg
,
467 reg
| MONO_OUTPUT_MUTE
);
469 ad_write(sc
, mixer_channel_info
[device
].left_reg
,
471 } else if (!(sc
->mute
[device
] & MUTE_LEFT
)) {
472 if (device
== AD1848_MONITOR_CHANNEL
) {
473 ad_write(sc
, mixer_channel_info
[device
].left_reg
,
474 reg
| DIGITAL_MIX1_ENABLE
);
475 if (sc
->open_mode
& FREAD
)
476 ad1848_mute_wave_output(sc
, WAVE_UNMUTE1
, 1);
477 } else if (device
== AD1848_OUT_CHANNEL
)
478 ad_write(sc
, mixer_channel_info
[device
].left_reg
,
479 reg
& ~MONO_OUTPUT_MUTE
);
481 ad_write(sc
, mixer_channel_info
[device
].left_reg
,
485 if (!mixer_channel_info
[device
].right_reg
)
488 reg
= ad_read(sc
, mixer_channel_info
[device
].right_reg
);
490 if (mute
& MUTE_RIGHT
) {
491 ad_write(sc
, mixer_channel_info
[device
].right_reg
, reg
| 0x80);
492 } else if (!(sc
->mute
[device
] & MUTE_RIGHT
)) {
493 ad_write(sc
, mixer_channel_info
[device
].right_reg
, reg
&~0x80);
498 ad1848_set_channel_gain(struct ad1848_softc
*sc
, int device
,
499 struct ad1848_volume
*gp
)
501 const struct ad1848_mixerinfo
*info
;
505 info
= &mixer_channel_info
[device
];
506 sc
->gains
[device
] = *gp
;
508 atten
= (AUDIO_MAX_GAIN
- gp
->left
) * (info
->atten_bits
+ 1) /
509 (AUDIO_MAX_GAIN
+ 1);
511 reg
= ad_read(sc
, info
->left_reg
) & (info
->atten_mask
);
512 if (device
== AD1848_MONITOR_CHANNEL
)
513 reg
|= ((atten
& info
->atten_bits
) << 2);
515 reg
|= ((atten
& info
->atten_bits
));
517 ad_write(sc
, info
->left_reg
, reg
);
519 if (!info
->right_reg
)
522 atten
= (AUDIO_MAX_GAIN
- gp
->right
) * (info
->atten_bits
+ 1) /
523 (AUDIO_MAX_GAIN
+ 1);
524 reg
= ad_read(sc
, info
->right_reg
);
525 reg
&= info
->atten_mask
;
526 ad_write(sc
, info
->right_reg
, (atten
& info
->atten_bits
) | reg
);
532 ad1848_get_device_gain(struct ad1848_softc
*sc
, int device
,
533 struct ad1848_volume
*gp
)
536 *gp
= sc
->gains
[device
];
541 ad1848_get_rec_gain(struct ad1848_softc
*sc
, struct ad1848_volume
*gp
)
549 ad1848_set_rec_gain(struct ad1848_softc
*sc
, struct ad1848_volume
*gp
)
553 DPRINTF(("ad1848_set_rec_gain: %d:%d\n", gp
->left
, gp
->right
));
557 gain
= (gp
->left
* (GAIN_22_5
+ 1)) / (AUDIO_MAX_GAIN
+ 1);
558 reg
= ad_read(sc
, SP_LEFT_INPUT_CONTROL
);
559 reg
&= INPUT_GAIN_MASK
;
560 ad_write(sc
, SP_LEFT_INPUT_CONTROL
, (gain
& 0x0f) | reg
);
562 gain
= (gp
->right
* (GAIN_22_5
+ 1)) / (AUDIO_MAX_GAIN
+ 1);
563 reg
= ad_read(sc
, SP_RIGHT_INPUT_CONTROL
);
564 reg
&= INPUT_GAIN_MASK
;
565 ad_write(sc
, SP_RIGHT_INPUT_CONTROL
, (gain
& 0x0f) | reg
);
571 ad1848_mute_wave_output(struct ad1848_softc
*sc
, int mute
, int set
)
575 DPRINTF(("ad1848_mute_wave_output: %d, %d\n", mute
, set
));
577 if (mute
== WAVE_MUTE2_INIT
) {
578 sc
->wave_mute_status
= 0;
582 m
= sc
->wave_mute_status
|= mute
;
584 m
= sc
->wave_mute_status
&= ~mute
;
586 if (m
& WAVE_MUTE0
|| ((m
& WAVE_UNMUTE1
) == 0 && m
& WAVE_MUTE2
))
587 ad1848_mute_channel(sc
, AD1848_DAC_CHANNEL
, MUTE_ALL
);
589 ad1848_mute_channel(sc
, AD1848_DAC_CHANNEL
,
590 sc
->mute
[AD1848_DAC_CHANNEL
]);
594 ad1848_set_mic_gain(struct ad1848_softc
*sc
, struct ad1848_volume
*gp
)
598 DPRINTF(("cs4231_set_mic_gain: %d\n", gp
->left
));
600 if (gp
->left
> AUDIO_MAX_GAIN
/2) {
602 reg
= ad_read(sc
, SP_LEFT_INPUT_CONTROL
);
603 ad_write(sc
, SP_LEFT_INPUT_CONTROL
,
604 reg
| INPUT_MIC_GAIN_ENABLE
);
607 reg
= ad_read(sc
, SP_LEFT_INPUT_CONTROL
);
608 ad_write(sc
, SP_LEFT_INPUT_CONTROL
,
609 reg
& ~INPUT_MIC_GAIN_ENABLE
);
616 ad1848_get_mic_gain(struct ad1848_softc
*sc
, struct ad1848_volume
*gp
)
619 gp
->left
= gp
->right
= AUDIO_MAX_GAIN
;
621 gp
->left
= gp
->right
= AUDIO_MIN_GAIN
;
625 static const ad1848_devmap_t
*
626 ad1848_mixer_find_dev(const ad1848_devmap_t
*map
, int cnt
, mixer_ctrl_t
*cp
)
630 for (i
= 0; i
< cnt
; i
++) {
631 if (map
[i
].id
== cp
->dev
) {
639 ad1848_mixer_get_port(struct ad1848_softc
*ac
, const struct ad1848_devmap
*map
,
640 int cnt
, mixer_ctrl_t
*cp
)
642 const ad1848_devmap_t
*entry
;
643 struct ad1848_volume vol
;
648 if (!(entry
= ad1848_mixer_find_dev(map
, cnt
, cp
)))
653 switch (entry
->kind
) {
654 case AD1848_KIND_LVL
:
655 if (cp
->type
!= AUDIO_MIXER_VALUE
)
658 if (dev
< AD1848_AUX2_CHANNEL
||
659 dev
> AD1848_MONITOR_CHANNEL
)
662 if (cp
->un
.value
.num_channels
!= 1 &&
663 mixer_channel_info
[dev
].right_reg
== 0)
666 error
= ad1848_get_device_gain(ac
, dev
, &vol
);
668 ad1848_from_vol(cp
, &vol
);
672 case AD1848_KIND_MUTE
:
673 if (cp
->type
!= AUDIO_MIXER_ENUM
) break;
675 cp
->un
.ord
= ac
->mute
[dev
] ? 1 : 0;
679 case AD1848_KIND_RECORDGAIN
:
680 if (cp
->type
!= AUDIO_MIXER_VALUE
) break;
682 error
= ad1848_get_rec_gain(ac
, &vol
);
684 ad1848_from_vol(cp
, &vol
);
688 case AD1848_KIND_MICGAIN
:
689 if (cp
->type
!= AUDIO_MIXER_VALUE
) break;
691 error
= ad1848_get_mic_gain(ac
, &vol
);
693 ad1848_from_vol(cp
, &vol
);
697 case AD1848_KIND_RECORDSOURCE
:
698 if (cp
->type
!= AUDIO_MIXER_ENUM
) break;
699 cp
->un
.ord
= ad1848_get_rec_port(ac
);
704 printf ("Invalid kind\n");
712 ad1848_mixer_set_port(struct ad1848_softc
*ac
, const struct ad1848_devmap
*map
,
713 int cnt
, mixer_ctrl_t
*cp
)
715 const ad1848_devmap_t
*entry
;
716 struct ad1848_volume vol
;
721 if (!(entry
= ad1848_mixer_find_dev(map
, cnt
, cp
)))
726 switch (entry
->kind
) {
727 case AD1848_KIND_LVL
:
728 if (cp
->type
!= AUDIO_MIXER_VALUE
)
731 if (dev
< AD1848_AUX2_CHANNEL
||
732 dev
> AD1848_MONITOR_CHANNEL
)
735 if (cp
->un
.value
.num_channels
!= 1 &&
736 mixer_channel_info
[dev
].right_reg
== 0)
739 ad1848_to_vol(cp
, &vol
);
740 error
= ad1848_set_channel_gain(ac
, dev
, &vol
);
743 case AD1848_KIND_MUTE
:
744 if (cp
->type
!= AUDIO_MIXER_ENUM
) break;
746 ac
->mute
[dev
] = (cp
->un
.ord
? MUTE_ALL
: 0);
747 ad1848_mute_channel(ac
, dev
, ac
->mute
[dev
]);
751 case AD1848_KIND_RECORDGAIN
:
752 if (cp
->type
!= AUDIO_MIXER_VALUE
) break;
754 ad1848_to_vol(cp
, &vol
);
755 error
= ad1848_set_rec_gain(ac
, &vol
);
758 case AD1848_KIND_MICGAIN
:
759 if (cp
->type
!= AUDIO_MIXER_VALUE
) break;
761 ad1848_to_vol(cp
, &vol
);
762 error
= ad1848_set_mic_gain(ac
, &vol
);
765 case AD1848_KIND_RECORDSOURCE
:
766 if (cp
->type
!= AUDIO_MIXER_ENUM
) break;
768 error
= ad1848_set_rec_port(ac
, cp
->un
.ord
);
772 printf ("Invalid kind\n");
780 ad1848_query_encoding(void *addr
, struct audio_encoding
*fp
)
782 struct ad1848_softc
*sc
;
787 strcpy(fp
->name
, AudioEmulaw
);
788 fp
->encoding
= AUDIO_ENCODING_ULAW
;
793 strcpy(fp
->name
, AudioEalaw
);
794 fp
->encoding
= AUDIO_ENCODING_ALAW
;
799 strcpy(fp
->name
, AudioEslinear_le
);
800 fp
->encoding
= AUDIO_ENCODING_SLINEAR_LE
;
805 strcpy(fp
->name
, AudioEulinear
);
806 fp
->encoding
= AUDIO_ENCODING_ULINEAR
;
811 case 4: /* only on CS4231 */
812 strcpy(fp
->name
, AudioEslinear_be
);
813 fp
->encoding
= AUDIO_ENCODING_SLINEAR_BE
;
815 fp
->flags
= sc
->mode
== 1
819 ? AUDIO_ENCODINGFLAG_EMULATED
: 0;
822 /* emulate some modes */
824 strcpy(fp
->name
, AudioEslinear
);
825 fp
->encoding
= AUDIO_ENCODING_SLINEAR
;
827 fp
->flags
= AUDIO_ENCODINGFLAG_EMULATED
;
830 strcpy(fp
->name
, AudioEulinear_le
);
831 fp
->encoding
= AUDIO_ENCODING_ULINEAR_LE
;
833 fp
->flags
= AUDIO_ENCODINGFLAG_EMULATED
;
836 strcpy(fp
->name
, AudioEulinear_be
);
837 fp
->encoding
= AUDIO_ENCODING_ULINEAR_BE
;
839 fp
->flags
= AUDIO_ENCODINGFLAG_EMULATED
;
842 case 8: /* only on CS4231 */
843 if (sc
->mode
== 1 || sc
->is_ad1845
)
845 strcpy(fp
->name
, AudioEadpcm
);
846 fp
->encoding
= AUDIO_ENCODING_ADPCM
;
858 ad1848_set_params(void *addr
, int setmode
, int usemode
,
859 audio_params_t
*p
, audio_params_t
*r
, stream_filter_list_t
*pfil
,
860 stream_filter_list_t
*rfil
)
862 audio_params_t phw
, rhw
;
863 struct ad1848_softc
*sc
;
864 int error
, bits
, enc
;
865 stream_filter_factory_t
*pswcode
;
866 stream_filter_factory_t
*rswcode
;
868 DPRINTF(("ad1848_set_params: %u %u %u %u\n",
869 p
->encoding
, p
->precision
, p
->channels
, p
->sample_rate
));
873 pswcode
= rswcode
= 0;
877 case AUDIO_ENCODING_SLINEAR_LE
:
878 if (p
->precision
== 8) {
879 enc
= AUDIO_ENCODING_ULINEAR_LE
;
880 phw
.encoding
= AUDIO_ENCODING_ULINEAR_LE
;
881 rhw
.encoding
= AUDIO_ENCODING_ULINEAR_LE
;
882 pswcode
= rswcode
= change_sign8
;
885 case AUDIO_ENCODING_SLINEAR_BE
:
886 if (p
->precision
== 16 && (sc
->mode
== 1
891 enc
= AUDIO_ENCODING_SLINEAR_LE
;
892 phw
.encoding
= AUDIO_ENCODING_SLINEAR_LE
;
893 rhw
.encoding
= AUDIO_ENCODING_SLINEAR_LE
;
894 pswcode
= rswcode
= swap_bytes
;
897 case AUDIO_ENCODING_ULINEAR_LE
:
898 if (p
->precision
== 16) {
899 enc
= AUDIO_ENCODING_SLINEAR_LE
;
900 phw
.encoding
= AUDIO_ENCODING_SLINEAR_LE
;
901 rhw
.encoding
= AUDIO_ENCODING_SLINEAR_LE
;
902 pswcode
= rswcode
= change_sign16
;
905 case AUDIO_ENCODING_ULINEAR_BE
:
906 if (p
->precision
== 16) {
912 enc
= AUDIO_ENCODING_SLINEAR_LE
;
913 phw
.encoding
= AUDIO_ENCODING_SLINEAR_LE
;
914 rhw
.encoding
= AUDIO_ENCODING_SLINEAR_LE
;
915 pswcode
= swap_bytes_change_sign16
;
916 rswcode
= swap_bytes_change_sign16
;
918 enc
= AUDIO_ENCODING_SLINEAR_BE
;
919 phw
.encoding
= AUDIO_ENCODING_SLINEAR_BE
;
920 rhw
.encoding
= AUDIO_ENCODING_SLINEAR_BE
;
921 pswcode
= rswcode
= change_sign16
;
927 case AUDIO_ENCODING_ULAW
:
928 bits
= FMT_ULAW
>> 5;
930 case AUDIO_ENCODING_ALAW
:
931 bits
= FMT_ALAW
>> 5;
933 case AUDIO_ENCODING_ADPCM
:
934 bits
= FMT_ADPCM
>> 5;
936 case AUDIO_ENCODING_SLINEAR_LE
:
937 if (p
->precision
== 16)
938 bits
= FMT_TWOS_COMP
>> 5;
942 case AUDIO_ENCODING_SLINEAR_BE
:
943 if (p
->precision
== 16)
944 bits
= FMT_TWOS_COMP_BE
>> 5;
948 case AUDIO_ENCODING_ULINEAR_LE
:
949 if (p
->precision
== 8)
950 bits
= FMT_PCM8
>> 5;
958 if (p
->channels
< 1 || p
->channels
> 2)
961 error
= ad1848_set_speed(sc
, &p
->sample_rate
);
964 phw
.sample_rate
= p
->sample_rate
;
967 pfil
->append(pfil
, pswcode
, &phw
);
969 rfil
->append(rfil
, rswcode
, &rhw
);
971 sc
->format_bits
= bits
;
972 sc
->channels
= p
->channels
;
973 sc
->precision
= p
->precision
;
976 DPRINTF(("ad1848_set_params succeeded, bits=%x\n", bits
));
981 ad1848_set_rec_port(struct ad1848_softc
*sc
, int port
)
985 DPRINTF(("ad1848_set_rec_port: 0x%x\n", port
));
987 if (port
== MIC_IN_PORT
)
989 else if (port
== LINE_IN_PORT
)
991 else if (port
== DAC_IN_PORT
)
992 inp
= MIXED_DAC_INPUT
;
993 else if (sc
->mode
>= 2 && port
== AUX1_IN_PORT
)
998 reg
= ad_read(sc
, SP_LEFT_INPUT_CONTROL
);
999 reg
&= INPUT_SOURCE_MASK
;
1000 ad_write(sc
, SP_LEFT_INPUT_CONTROL
, (inp
|reg
));
1002 reg
= ad_read(sc
, SP_RIGHT_INPUT_CONTROL
);
1003 reg
&= INPUT_SOURCE_MASK
;
1004 ad_write(sc
, SP_RIGHT_INPUT_CONTROL
, (inp
|reg
));
1006 sc
->rec_port
= port
;
1012 ad1848_get_rec_port(struct ad1848_softc
*sc
)
1014 return sc
->rec_port
;
1018 ad1848_round_blocksize(void *addr
, int blk
,
1019 int mode
, const audio_params_t
*param
)
1022 /* Round to a multiple of the biggest sample size. */
1027 ad1848_open(void *addr
, int flags
)
1029 struct ad1848_softc
*sc
;
1033 DPRINTF(("ad1848_open: sc=%p\n", sc
));
1035 sc
->open_mode
= flags
;
1037 /* Enable interrupts */
1038 DPRINTF(("ad1848_open: enable intrs\n"));
1039 reg
= ad_read(sc
, SP_PIN_CONTROL
);
1040 ad_write(sc
, SP_PIN_CONTROL
, reg
| INTERRUPT_ENABLE
);
1042 /* If recording && monitoring, the playback part is also used. */
1043 if (flags
& FREAD
&& sc
->mute
[AD1848_MONITOR_CHANNEL
] == 0)
1044 ad1848_mute_wave_output(sc
, WAVE_UNMUTE1
, 1);
1048 ad1848_dump_regs(sc
);
1055 * Close function is called at splaudio().
1058 ad1848_close(void *addr
)
1060 struct ad1848_softc
*sc
;
1066 ad1848_mute_wave_output(sc
, WAVE_UNMUTE1
, 0);
1068 /* Disable interrupts */
1069 DPRINTF(("ad1848_close: disable intrs\n"));
1070 reg
= ad_read(sc
, SP_PIN_CONTROL
);
1071 ad_write(sc
, SP_PIN_CONTROL
, reg
& ~INTERRUPT_ENABLE
);
1075 ad1848_dump_regs(sc
);
1080 * Lower-level routines
1083 ad1848_commit_settings(void *addr
)
1085 struct ad1848_softc
*sc
;
1091 if (!sc
->need_commit
)
1096 ad1848_mute_wave_output(sc
, WAVE_MUTE0
, 1);
1098 ad_set_MCE(sc
, 1); /* Enables changes to the format select reg */
1100 fs
= sc
->speed_bits
| (sc
->format_bits
<< 5);
1102 if (sc
->channels
== 2)
1106 * OPL3-SA2 (YMF711) is sometimes busy here.
1107 * Wait until it becomes ready.
1110 timeout
< 1000 && ADREAD(sc
, AD1848_IADDR
) & SP_IN_INIT
; timeout
++)
1113 ad_write(sc
, SP_CLOCK_DATA_FORMAT
, fs
);
1116 * If mode >= 2 (CS4231), set I28 also.
1117 * It's the capture format register.
1119 if (sc
->mode
>= 2) {
1121 * Gravis Ultrasound MAX SDK sources says something about
1122 * errata sheets, with the implication that these inb()s
1125 (void)ADREAD(sc
, AD1848_IDATA
);
1126 (void)ADREAD(sc
, AD1848_IDATA
);
1127 /* Write to I8 starts resynchronization. Wait for completion. */
1129 while (timeout
> 0 && ADREAD(sc
, AD1848_IADDR
) == SP_IN_INIT
)
1132 ad_write(sc
, CS_REC_FORMAT
, fs
);
1133 (void)ADREAD(sc
, AD1848_IDATA
);
1134 (void)ADREAD(sc
, AD1848_IDATA
);
1135 /* Now wait for resync for capture side of the house */
1138 * Write to I8 starts resynchronization. Wait until it completes.
1141 while (timeout
> 0 && ADREAD(sc
, AD1848_IADDR
) == SP_IN_INIT
) {
1146 if (ADREAD(sc
, AD1848_IADDR
) == SP_IN_INIT
)
1147 printf("ad1848_commit: Auto calibration timed out\n");
1150 * Starts the calibration process and
1151 * enters playback mode after it.
1154 wait_for_calibration(sc
);
1156 ad1848_mute_wave_output(sc
, WAVE_MUTE0
, 0);
1160 sc
->need_commit
= 0;
1165 ad1848_reset(struct ad1848_softc
*sc
)
1169 DPRINTF(("ad1848_reset\n"));
1171 /* Clear the PEN and CEN bits */
1172 r
= ad_read(sc
, SP_INTERFACE_CONFIG
);
1173 r
&= ~(CAPTURE_ENABLE
| PLAYBACK_ENABLE
);
1174 ad_write(sc
, SP_INTERFACE_CONFIG
, r
);
1176 if (sc
->mode
>= 2) {
1177 ADWRITE(sc
, AD1848_IADDR
, CS_IRQ_STATUS
);
1178 ADWRITE(sc
, AD1848_IDATA
, 0);
1180 /* Clear interrupt status */
1181 ADWRITE(sc
, AD1848_STATUS
, 0);
1184 ad1848_dump_regs(sc
);
1189 ad1848_set_speed(struct ad1848_softc
*sc
, u_int
*argp
)
1192 * The sampling speed is encoded in the least significant nible of I8.
1193 * The LSB selects the clock source (0=24.576 MHz, 1=16.9344 MHz) and
1194 * other three bits select the divisor (indirectly):
1196 * The available speeds are in the following table. Keep the speeds in
1197 * the increasing order.
1205 static const speed_struct speed_table
[] = {
1206 {5510, (0 << 1) | 1},
1207 {5510, (0 << 1) | 1},
1208 {6620, (7 << 1) | 1},
1209 {8000, (0 << 1) | 0},
1210 {9600, (7 << 1) | 0},
1211 {11025, (1 << 1) | 1},
1212 {16000, (1 << 1) | 0},
1213 {18900, (2 << 1) | 1},
1214 {22050, (3 << 1) | 1},
1215 {27420, (2 << 1) | 0},
1216 {32000, (3 << 1) | 0},
1217 {33075, (6 << 1) | 1},
1218 {37800, (4 << 1) | 1},
1219 {44100, (5 << 1) | 1},
1220 {48000, (6 << 1) | 0}
1227 n
= sizeof(speed_table
) / sizeof(speed_struct
);
1229 if (arg
< speed_table
[0].speed
)
1231 if (arg
> speed_table
[n
- 1].speed
)
1234 for (i
= 1 /*really*/ ; selected
== -1 && i
< n
; i
++)
1235 if (speed_table
[i
].speed
== arg
)
1237 else if (speed_table
[i
].speed
> arg
) {
1240 diff1
= arg
- speed_table
[i
- 1].speed
;
1241 diff2
= speed_table
[i
].speed
- arg
;
1249 if (selected
== -1) {
1250 printf("ad1848: Can't find speed???\n");
1254 sc
->speed_bits
= speed_table
[selected
].bits
;
1255 sc
->need_commit
= 1;
1256 *argp
= speed_table
[selected
].speed
;
1265 ad1848_halt_output(void *addr
)
1267 struct ad1848_softc
*sc
;
1270 DPRINTF(("ad1848: ad1848_halt_output\n"));
1272 reg
= ad_read(sc
, SP_INTERFACE_CONFIG
);
1273 ad_write(sc
, SP_INTERFACE_CONFIG
, reg
& ~PLAYBACK_ENABLE
);
1279 ad1848_halt_input(void *addr
)
1281 struct ad1848_softc
*sc
;
1284 DPRINTF(("ad1848: ad1848_halt_input\n"));
1286 reg
= ad_read(sc
, SP_INTERFACE_CONFIG
);
1287 ad_write(sc
, SP_INTERFACE_CONFIG
, reg
& ~CAPTURE_ENABLE
);