ad1848.c revision 1.11 1 /* $NetBSD: ad1848.c,v 1.11 2001/11/04 08:08:25 itohy Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ken Hornstein and John Kohl.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1994 John Brezak
40 * Copyright (c) 1991-1993 Regents of the University of California.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the Computer Systems
54 * Engineering Group at Lawrence Berkeley Laboratory.
55 * 4. Neither the name of the University nor of the Laboratory may be used
56 * to endorse or promote products derived from this software without
57 * specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 */
72
73 /*
74 * Copyright by Hannu Savolainen 1994
75 *
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions are
78 * met: 1. Redistributions of source code must retain the above copyright
79 * notice, this list of conditions and the following disclaimer. 2.
80 * Redistributions in binary form must reproduce the above copyright notice,
81 * this list of conditions and the following disclaimer in the documentation
82 * and/or other materials provided with the distribution.
83 *
84 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
85 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
86 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
87 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
88 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
90 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
91 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 * SUCH DAMAGE.
95 *
96 */
97 /*
98 * Portions of this code are from the VOXware support for the ad1848
99 * by Hannu Savolainen <hannu (at) voxware.pp.fi>
100 *
101 * Portions also supplied from the SoundBlaster driver for NetBSD.
102 */
103
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/errno.h>
107 #include <sys/ioctl.h>
108 #include <sys/device.h>
109 #include <sys/fcntl.h>
110 /*#include <sys/syslog.h>*/
111 /*#include <sys/proc.h>*/
112
113 #include <machine/cpu.h>
114 #include <machine/bus.h>
115
116 #include <sys/audioio.h>
117
118 #include <dev/audio_if.h>
119 #include <dev/auconv.h>
120
121 #include <dev/ic/ad1848reg.h>
122 #include <dev/ic/cs4231reg.h>
123 #include <dev/ic/cs4237reg.h>
124 #include <dev/ic/ad1848var.h>
125 #if 0
126 #include <dev/isa/cs4231var.h>
127 #endif
128
129 /*
130 * AD1845 on some machines don't match the AD1845 doc
131 * and defining AD1845_HACK to 1 works around the problems.
132 * options AD1845_HACK=0 should work if you have ``correct'' one.
133 */
134 #ifndef AD1845_HACK
135 #define AD1845_HACK 1 /* weird mixer, can't play slinear_be */
136 #endif
137
138 #ifdef AUDIO_DEBUG
139 #define DPRINTF(x) if (ad1848debug) printf x
140 int ad1848debug = 0;
141 #else
142 #define DPRINTF(x)
143 #endif
144
145 /*
146 * Initial values for the indirect registers of CS4248/AD1848.
147 */
148 static const int ad1848_init_values[] = {
149 GAIN_12|INPUT_MIC_GAIN_ENABLE, /* Left Input Control */
150 GAIN_12|INPUT_MIC_GAIN_ENABLE, /* Right Input Control */
151 ATTEN_12, /* Left Aux #1 Input Control */
152 ATTEN_12, /* Right Aux #1 Input Control */
153 ATTEN_12, /* Left Aux #2 Input Control */
154 ATTEN_12, /* Right Aux #2 Input Control */
155 /* bits 5-0 are attenuation select */
156 ATTEN_12, /* Left DAC output Control */
157 ATTEN_12, /* Right DAC output Control */
158 CLOCK_XTAL1|FMT_PCM8, /* Clock and Data Format */
159 SINGLE_DMA|AUTO_CAL_ENABLE, /* Interface Config */
160 INTERRUPT_ENABLE, /* Pin control */
161 0x00, /* Test and Init */
162 MODE2, /* Misc control */
163 ATTEN_0<<2, /* Digital Mix Control */
164 0, /* Upper base Count */
165 0, /* Lower base Count */
166
167 /* These are for CS4231 &c. only (additional registers): */
168 0, /* Alt feature 1 */
169 0, /* Alt feature 2 */
170 ATTEN_12, /* Left line in */
171 ATTEN_12, /* Right line in */
172 0, /* Timer low */
173 0, /* Timer high */
174 0, /* unused */
175 0, /* unused */
176 0, /* IRQ status */
177 0, /* unused */
178 /* Mono input (a.k.a speaker) (mic) Control */
179 MONO_INPUT_MUTE|ATTEN_6, /* mute speaker by default */
180 0, /* unused */
181 0, /* record format */
182 0, /* Crystal Clock Select */
183 0, /* upper record count */
184 0 /* lower record count */
185 };
186
187
188 __inline int ad_read __P((struct ad1848_softc *, int));
189 __inline void ad_write __P((struct ad1848_softc *, int, int));
190 static void ad_set_MCE __P((struct ad1848_softc *, int));
191 static void wait_for_calibration __P((struct ad1848_softc *));
192
193
194 int
195 ad1848_to_vol(cp, vol)
196 mixer_ctrl_t *cp;
197 struct ad1848_volume *vol;
198 {
199 if (cp->un.value.num_channels == 1) {
200 vol->left =
201 vol->right = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
202 return(1);
203 }
204 else if (cp->un.value.num_channels == 2) {
205 vol->left = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
206 vol->right = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
207 return(1);
208 }
209 return(0);
210 }
211
212 int
213 ad1848_from_vol(cp, vol)
214 mixer_ctrl_t *cp;
215 struct ad1848_volume *vol;
216 {
217 if (cp->un.value.num_channels == 1) {
218 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = vol->left;
219 return(1);
220 }
221 else if (cp->un.value.num_channels == 2) {
222 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = vol->left;
223 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = vol->right;
224 return(1);
225 }
226 return(0);
227 }
228
229
230 __inline int
231 ad_read(sc, reg)
232 struct ad1848_softc *sc;
233 int reg;
234 {
235 int x;
236
237 ADWRITE(sc, AD1848_IADDR, (reg & 0xff) | sc->MCE_bit);
238 x = ADREAD(sc, AD1848_IDATA);
239 /* printf("(%02x<-%02x) ", reg|sc->MCE_bit, x); */
240 return x;
241 }
242
243 __inline void
244 ad_write(sc, reg, data)
245 struct ad1848_softc *sc;
246 int reg;
247 int data;
248 {
249 ADWRITE(sc, AD1848_IADDR, (reg & 0xff) | sc->MCE_bit);
250 ADWRITE(sc, AD1848_IDATA, data & 0xff);
251 /* printf("(%02x->%02x) ", reg|sc->MCE_bit, data); */
252 }
253
254 /*
255 * extended registers (mode 3) require an additional level of
256 * indirection through CS_XREG (I23).
257 */
258
259 __inline int
260 ad_xread(sc, reg)
261 struct ad1848_softc *sc;
262 int reg;
263 {
264 int x;
265
266 ADWRITE(sc, AD1848_IADDR, CS_XREG | sc->MCE_bit);
267 ADWRITE(sc, AD1848_IDATA, (reg | ALT_F3_XRAE) & 0xff);
268 x = ADREAD(sc, AD1848_IDATA);
269
270 return x;
271 }
272
273 __inline void
274 ad_xwrite(sc, reg, val)
275 struct ad1848_softc *sc;
276 int reg, val;
277 {
278 ADWRITE(sc, AD1848_IADDR, CS_XREG | sc->MCE_bit);
279 ADWRITE(sc, AD1848_IDATA, (reg | ALT_F3_XRAE) & 0xff);
280 ADWRITE(sc, AD1848_IDATA, val & 0xff);
281 }
282
283 static void
284 ad_set_MCE(sc, state)
285 struct ad1848_softc *sc;
286 int state;
287 {
288 if (state)
289 sc->MCE_bit = MODE_CHANGE_ENABLE;
290 else
291 sc->MCE_bit = 0;
292 ADWRITE(sc, AD1848_IADDR, sc->MCE_bit);
293 }
294
295 static void
296 wait_for_calibration(sc)
297 struct ad1848_softc *sc;
298 {
299 int timeout;
300
301 DPRINTF(("ad1848: Auto calibration started.\n"));
302 /*
303 * Wait until the auto calibration process has finished.
304 *
305 * 1) Wait until the chip becomes ready (reads don't return 0x80).
306 * 2) Wait until the ACI bit of I11 gets on and then off.
307 * Because newer chips are fast we may never see the ACI
308 * bit go on. Just delay a little instead.
309 */
310 timeout = 10000;
311 while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT) {
312 delay(10);
313 timeout--;
314 }
315 if (timeout <= 0)
316 DPRINTF(("ad1848: Auto calibration timed out(1).\n"));
317
318 /* Set register addr */
319 ADWRITE(sc, AD1848_IADDR, SP_TEST_AND_INIT);
320 /* Wait for address to appear when read back. */
321 timeout = 100000;
322 while (timeout > 0 &&
323 (ADREAD(sc, AD1848_IADDR)&SP_IADDR_MASK) != SP_TEST_AND_INIT) {
324 delay(10);
325 timeout--;
326 }
327 if (timeout <= 0)
328 DPRINTF(("ad1848: Auto calibration timed out(1.5).\n"));
329
330 if (!(ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG)) {
331 if (sc->mode > 1) {
332 /* A new chip, just delay a little. */
333 delay(100); /* XXX what should it be? */
334 } else {
335 timeout = 10000;
336 while (timeout > 0 &&
337 !(ad_read(sc, SP_TEST_AND_INIT) &
338 AUTO_CAL_IN_PROG)) {
339 delay(10);
340 timeout--;
341 }
342 if (timeout <= 0)
343 DPRINTF(("ad1848: Auto calibration timed out(2).\n"));
344 }
345 }
346
347 timeout = 10000;
348 while (timeout > 0 &&
349 ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG) {
350 delay(10);
351 timeout--;
352 }
353 if (timeout <= 0)
354 DPRINTF(("ad1848: Auto calibration timed out(3).\n"));
355 }
356
357 #ifdef AUDIO_DEBUG
358 void
359 ad1848_dump_regs(sc)
360 struct ad1848_softc *sc;
361 {
362 int i;
363 u_char r;
364
365 printf("ad1848 status=%02x", ADREAD(sc, AD1848_STATUS));
366 printf(" regs: ");
367 for (i = 0; i < 16; i++) {
368 r = ad_read(sc, i);
369 printf("%02x ", r);
370 }
371 if (sc->mode >= 2) {
372 for (i = 16; i < 32; i++) {
373 r = ad_read(sc, i);
374 printf("%02x ", r);
375 }
376 }
377 printf("\n");
378 }
379 #endif
380
381
382 /*
383 * Attach hardware to driver, attach hardware driver to audio
384 * pseudo-device driver .
385 */
386 void
387 ad1848_attach(sc)
388 struct ad1848_softc *sc;
389 {
390 int i;
391 static struct ad1848_volume vol_mid = {220, 220};
392 static struct ad1848_volume vol_0 = {0, 0};
393 struct audio_params pparams, rparams;
394 int timeout;
395
396 /* Initialize the ad1848... */
397 for (i = 0; i < 0x10; i++) {
398 ad_write(sc, i, ad1848_init_values[i]);
399 timeout = 100000;
400 while (timeout > 0 && ADREAD(sc, AD1848_IADDR) & SP_IN_INIT)
401 timeout--;
402 }
403 /* ...and additional CS4231 stuff too */
404 if (sc->mode >= 2) {
405 ad_write(sc, SP_INTERFACE_CONFIG, 0); /* disable SINGLE_DMA */
406 for (i = 0x10; i < 0x20; i++)
407 if (ad1848_init_values[i] != 0) {
408 ad_write(sc, i, ad1848_init_values[i]);
409 timeout = 100000;
410 while (timeout > 0 &&
411 ADREAD(sc, AD1848_IADDR) & SP_IN_INIT)
412 timeout--;
413 }
414 }
415 ad1848_reset(sc);
416
417 pparams = audio_default;
418 rparams = audio_default;
419 ad1848_set_params(sc, AUMODE_RECORD|AUMODE_PLAY, 0, &pparams, &rparams);
420
421 /* Set default gains */
422 ad1848_set_rec_gain(sc, &vol_mid);
423 ad1848_set_channel_gain(sc, AD1848_DAC_CHANNEL, &vol_mid);
424 ad1848_set_channel_gain(sc, AD1848_MONITOR_CHANNEL, &vol_0);
425 ad1848_set_channel_gain(sc, AD1848_AUX1_CHANNEL, &vol_mid); /* CD volume */
426 sc->mute[AD1848_MONITOR_CHANNEL] = MUTE_ALL;
427 if (sc->mode >= 2
428 #if AD1845_HACK
429 && sc->is_ad1845 == 0
430 #endif
431 ) {
432 ad1848_set_channel_gain(sc, AD1848_AUX2_CHANNEL, &vol_mid); /* CD volume */
433 ad1848_set_channel_gain(sc, AD1848_LINE_CHANNEL, &vol_mid);
434 ad1848_set_channel_gain(sc, AD1848_MONO_CHANNEL, &vol_0);
435 sc->mute[AD1848_MONO_CHANNEL] = MUTE_ALL;
436 } else
437 ad1848_set_channel_gain(sc, AD1848_AUX2_CHANNEL, &vol_0);
438
439 /* Set default port */
440 ad1848_set_rec_port(sc, MIC_IN_PORT);
441
442 printf(": %s", sc->chip_name);
443 }
444
445 /*
446 * Various routines to interface to higher level audio driver
447 */
448 const struct ad1848_mixerinfo {
449 int left_reg;
450 int right_reg;
451 int atten_bits;
452 int atten_mask;
453 } mixer_channel_info[] =
454 {
455 { SP_LEFT_AUX2_CONTROL, SP_RIGHT_AUX2_CONTROL, AUX_INPUT_ATTEN_BITS,
456 AUX_INPUT_ATTEN_MASK },
457 { SP_LEFT_AUX1_CONTROL, SP_RIGHT_AUX1_CONTROL, AUX_INPUT_ATTEN_BITS,
458 AUX_INPUT_ATTEN_MASK },
459 { SP_LEFT_OUTPUT_CONTROL, SP_RIGHT_OUTPUT_CONTROL,
460 OUTPUT_ATTEN_BITS, OUTPUT_ATTEN_MASK },
461 { CS_LEFT_LINE_CONTROL, CS_RIGHT_LINE_CONTROL, LINE_INPUT_ATTEN_BITS,
462 LINE_INPUT_ATTEN_MASK },
463 { CS_MONO_IO_CONTROL, 0, MONO_INPUT_ATTEN_BITS, MONO_INPUT_ATTEN_MASK },
464 { SP_DIGITAL_MIX, 0, OUTPUT_ATTEN_BITS, MIX_ATTEN_MASK }
465 };
466
467 /*
468 * This function doesn't set the mute flags but does use them.
469 * The mute flags reflect the mutes that have been applied by the user.
470 * However, the driver occasionally wants to mute devices (e.g. when chaing
471 * sampling rate). These operations should not affect the mute flags.
472 */
473
474 void
475 ad1848_mute_channel(sc, device, mute)
476 struct ad1848_softc *sc;
477 int device;
478 int mute;
479 {
480 u_char reg;
481
482 reg = ad_read(sc, mixer_channel_info[device].left_reg);
483
484 if (mute & MUTE_LEFT) {
485 if (device == AD1848_MONITOR_CHANNEL) {
486 if (sc->open_mode & FREAD)
487 ad1848_mute_wave_output(sc, WAVE_UNMUTE1, 0);
488 ad_write(sc, mixer_channel_info[device].left_reg,
489 reg & ~DIGITAL_MIX1_ENABLE);
490 } else
491 ad_write(sc, mixer_channel_info[device].left_reg,
492 reg | 0x80);
493 } else if (!(sc->mute[device] & MUTE_LEFT)) {
494 if (device == AD1848_MONITOR_CHANNEL) {
495 ad_write(sc, mixer_channel_info[device].left_reg,
496 reg | DIGITAL_MIX1_ENABLE);
497 if (sc->open_mode & FREAD)
498 ad1848_mute_wave_output(sc, WAVE_UNMUTE1, 1);
499 } else
500 ad_write(sc, mixer_channel_info[device].left_reg,
501 reg & ~0x80);
502 }
503
504 if (!mixer_channel_info[device].right_reg)
505 return;
506
507 reg = ad_read(sc, mixer_channel_info[device].right_reg);
508
509 if (mute & MUTE_RIGHT) {
510 ad_write(sc, mixer_channel_info[device].right_reg, reg | 0x80);
511 } else if (!(sc->mute[device] & MUTE_RIGHT)) {
512 ad_write(sc, mixer_channel_info[device].right_reg, reg &~0x80);
513 }
514 }
515
516
517 int
518 ad1848_set_channel_gain(sc, device, gp)
519 struct ad1848_softc *sc;
520 int device;
521 struct ad1848_volume *gp;
522 {
523 const struct ad1848_mixerinfo *info = &mixer_channel_info[device];
524 u_char reg;
525 u_int atten;
526
527 sc->gains[device] = *gp;
528
529 atten = (AUDIO_MAX_GAIN - gp->left) * (info->atten_bits + 1) /
530 (AUDIO_MAX_GAIN + 1);
531
532 reg = ad_read(sc, info->left_reg) & (info->atten_mask);
533 if (device == AD1848_MONITOR_CHANNEL)
534 reg |= ((atten & info->atten_bits) << 2);
535 else
536 reg |= ((atten & info->atten_bits));
537
538 ad_write(sc, info->left_reg, reg);
539
540 if (!info->right_reg)
541 return (0);
542
543 atten = (AUDIO_MAX_GAIN - gp->right) * (info->atten_bits + 1) /
544 (AUDIO_MAX_GAIN + 1);
545 reg = ad_read(sc, info->right_reg);
546 reg &= info->atten_mask;
547 ad_write(sc, info->right_reg, (atten & info->atten_bits) | reg);
548
549 return(0);
550 }
551
552
553 int
554 ad1848_get_device_gain(sc, device, gp)
555 struct ad1848_softc *sc;
556 int device;
557 struct ad1848_volume *gp;
558 {
559 *gp = sc->gains[device];
560 return(0);
561 }
562
563 int
564 ad1848_get_rec_gain(sc, gp)
565 struct ad1848_softc *sc;
566 struct ad1848_volume *gp;
567 {
568 *gp = sc->rec_gain;
569 return(0);
570 }
571
572 int
573 ad1848_set_rec_gain(sc, gp)
574 struct ad1848_softc *sc;
575 struct ad1848_volume *gp;
576 {
577 u_char reg, gain;
578
579 DPRINTF(("ad1848_set_rec_gain: %d:%d\n", gp->left, gp->right));
580
581 sc->rec_gain = *gp;
582
583 gain = (gp->left * (GAIN_22_5 + 1)) / (AUDIO_MAX_GAIN + 1);
584 reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
585 reg &= INPUT_GAIN_MASK;
586 ad_write(sc, SP_LEFT_INPUT_CONTROL, (gain & 0x0f) | reg);
587
588 gain = (gp->right * (GAIN_22_5 + 1)) / (AUDIO_MAX_GAIN + 1);
589 reg = ad_read(sc, SP_RIGHT_INPUT_CONTROL);
590 reg &= INPUT_GAIN_MASK;
591 ad_write(sc, SP_RIGHT_INPUT_CONTROL, (gain & 0x0f) | reg);
592
593 return(0);
594 }
595
596
597 void
598 ad1848_mute_wave_output(sc, mute, set)
599 struct ad1848_softc *sc;
600 int mute, set;
601 {
602 int m;
603
604 DPRINTF(("ad1848_mute_wave_output: %d, %d\n", mute, set));
605
606 if (mute == WAVE_MUTE2_INIT) {
607 sc->wave_mute_status = 0;
608 mute = WAVE_MUTE2;
609 }
610 if (set)
611 m = sc->wave_mute_status |= mute;
612 else
613 m = sc->wave_mute_status &= ~mute;
614
615 if (m & WAVE_MUTE0 || ((m & WAVE_UNMUTE1) == 0 && m & WAVE_MUTE2))
616 ad1848_mute_channel(sc, AD1848_DAC_CHANNEL, MUTE_ALL);
617 else
618 ad1848_mute_channel(sc, AD1848_DAC_CHANNEL,
619 sc->mute[AD1848_DAC_CHANNEL]);
620 }
621
622 int
623 ad1848_set_mic_gain(sc, gp)
624 struct ad1848_softc *sc;
625 struct ad1848_volume *gp;
626 {
627 u_char reg;
628
629 DPRINTF(("cs4231_set_mic_gain: %d\n", gp->left));
630
631 if (gp->left > AUDIO_MAX_GAIN/2) {
632 sc->mic_gain_on = 1;
633 reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
634 ad_write(sc, SP_LEFT_INPUT_CONTROL,
635 reg | INPUT_MIC_GAIN_ENABLE);
636 } else {
637 sc->mic_gain_on = 0;
638 reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
639 ad_write(sc, SP_LEFT_INPUT_CONTROL,
640 reg & ~INPUT_MIC_GAIN_ENABLE);
641 }
642
643 return(0);
644 }
645
646 int
647 ad1848_get_mic_gain(sc, gp)
648 struct ad1848_softc *sc;
649 struct ad1848_volume *gp;
650 {
651 if (sc->mic_gain_on)
652 gp->left = gp->right = AUDIO_MAX_GAIN;
653 else
654 gp->left = gp->right = AUDIO_MIN_GAIN;
655 return(0);
656 }
657
658
659 static ad1848_devmap_t *
660 ad1848_mixer_find_dev __P((ad1848_devmap_t *, int, mixer_ctrl_t *));
661
662 static ad1848_devmap_t *
663 ad1848_mixer_find_dev(map, cnt, cp)
664 ad1848_devmap_t *map;
665 int cnt;
666 mixer_ctrl_t *cp;
667 {
668 int i;
669
670 for (i = 0; i < cnt; i++) {
671 if (map[i].id == cp->dev) {
672 return (&map[i]);
673 }
674 }
675 return (0);
676 }
677
678 int
679 ad1848_mixer_get_port(ac, map, cnt, cp)
680 struct ad1848_softc *ac;
681 struct ad1848_devmap *map;
682 int cnt;
683 mixer_ctrl_t *cp;
684 {
685 ad1848_devmap_t *entry;
686 struct ad1848_volume vol;
687 int error = EINVAL;
688 int dev;
689
690 if (!(entry = ad1848_mixer_find_dev(map, cnt, cp)))
691 return (ENXIO);
692
693 dev = entry->dev;
694
695 switch (entry->kind) {
696 case AD1848_KIND_LVL:
697 if (cp->type != AUDIO_MIXER_VALUE)
698 break;
699
700 if (dev < AD1848_AUX2_CHANNEL ||
701 dev > AD1848_MONITOR_CHANNEL)
702 break;
703
704 if (cp->un.value.num_channels != 1 &&
705 mixer_channel_info[dev].right_reg == 0)
706 break;
707
708 error = ad1848_get_device_gain(ac, dev, &vol);
709 if (!error)
710 ad1848_from_vol(cp, &vol);
711
712 break;
713
714 case AD1848_KIND_MUTE:
715 if (cp->type != AUDIO_MIXER_ENUM) break;
716
717 cp->un.ord = ac->mute[dev] ? 1 : 0;
718 error = 0;
719 break;
720
721 case AD1848_KIND_RECORDGAIN:
722 if (cp->type != AUDIO_MIXER_VALUE) break;
723
724 error = ad1848_get_rec_gain(ac, &vol);
725 if (!error)
726 ad1848_from_vol(cp, &vol);
727
728 break;
729
730 case AD1848_KIND_MICGAIN:
731 if (cp->type != AUDIO_MIXER_VALUE) break;
732
733 error = ad1848_get_mic_gain(ac, &vol);
734 if (!error)
735 ad1848_from_vol(cp, &vol);
736
737 break;
738
739 case AD1848_KIND_RECORDSOURCE:
740 if (cp->type != AUDIO_MIXER_ENUM) break;
741 cp->un.ord = ad1848_get_rec_port(ac);
742 error = 0;
743 break;
744
745 default:
746 printf ("Invalid kind\n");
747 break;
748 }
749
750 return (error);
751 }
752
753 int
754 ad1848_mixer_set_port(ac, map, cnt, cp)
755 struct ad1848_softc *ac;
756 struct ad1848_devmap *map;
757 int cnt;
758 mixer_ctrl_t *cp;
759 {
760 ad1848_devmap_t *entry;
761 struct ad1848_volume vol;
762 int error = EINVAL;
763 int dev;
764
765 if (!(entry = ad1848_mixer_find_dev(map, cnt, cp)))
766 return (ENXIO);
767
768 dev = entry->dev;
769
770 switch (entry->kind) {
771 case AD1848_KIND_LVL:
772 if (cp->type != AUDIO_MIXER_VALUE)
773 break;
774
775 if (dev < AD1848_AUX2_CHANNEL ||
776 dev > AD1848_MONITOR_CHANNEL)
777 break;
778
779 if (cp->un.value.num_channels != 1 &&
780 mixer_channel_info[dev].right_reg == 0)
781 break;
782
783 ad1848_to_vol(cp, &vol);
784 error = ad1848_set_channel_gain(ac, dev, &vol);
785 break;
786
787 case AD1848_KIND_MUTE:
788 if (cp->type != AUDIO_MIXER_ENUM) break;
789
790 ac->mute[dev] = (cp->un.ord ? MUTE_ALL : 0);
791 ad1848_mute_channel(ac, dev, ac->mute[dev]);
792 error = 0;
793 break;
794
795 case AD1848_KIND_RECORDGAIN:
796 if (cp->type != AUDIO_MIXER_VALUE) break;
797
798 ad1848_to_vol(cp, &vol);
799 error = ad1848_set_rec_gain(ac, &vol);
800 break;
801
802 case AD1848_KIND_MICGAIN:
803 if (cp->type != AUDIO_MIXER_VALUE) break;
804
805 ad1848_to_vol(cp, &vol);
806 error = ad1848_set_mic_gain(ac, &vol);
807 break;
808
809 case AD1848_KIND_RECORDSOURCE:
810 if (cp->type != AUDIO_MIXER_ENUM) break;
811
812 error = ad1848_set_rec_port(ac, cp->un.ord);
813 break;
814
815 default:
816 printf ("Invalid kind\n");
817 break;
818 }
819
820 return (error);
821 }
822
823
824 int
825 ad1848_query_encoding(addr, fp)
826 void *addr;
827 struct audio_encoding *fp;
828 {
829 struct ad1848_softc *sc = addr;
830
831 switch (fp->index) {
832 case 0:
833 strcpy(fp->name, AudioEmulaw);
834 fp->encoding = AUDIO_ENCODING_ULAW;
835 fp->precision = 8;
836 fp->flags = 0;
837 break;
838 case 1:
839 strcpy(fp->name, AudioEalaw);
840 fp->encoding = AUDIO_ENCODING_ALAW;
841 fp->precision = 8;
842 fp->flags = 0;
843 break;
844 case 2:
845 strcpy(fp->name, AudioEslinear_le);
846 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
847 fp->precision = 16;
848 fp->flags = 0;
849 break;
850 case 3:
851 strcpy(fp->name, AudioEulinear);
852 fp->encoding = AUDIO_ENCODING_ULINEAR;
853 fp->precision = 8;
854 fp->flags = 0;
855 break;
856
857 case 4: /* only on CS4231 */
858 strcpy(fp->name, AudioEslinear_be);
859 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
860 fp->precision = 16;
861 fp->flags = sc->mode == 1
862 #if AD1845_HACK
863 || sc->is_ad1845
864 #endif
865 ? AUDIO_ENCODINGFLAG_EMULATED : 0;
866 break;
867
868 /* emulate some modes */
869 case 5:
870 strcpy(fp->name, AudioEslinear);
871 fp->encoding = AUDIO_ENCODING_SLINEAR;
872 fp->precision = 8;
873 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
874 break;
875 case 6:
876 strcpy(fp->name, AudioEulinear_le);
877 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
878 fp->precision = 16;
879 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
880 break;
881 case 7:
882 strcpy(fp->name, AudioEulinear_be);
883 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
884 fp->precision = 16;
885 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
886 break;
887
888 case 8: /* only on CS4231 */
889 if (sc->mode == 1 || sc->is_ad1845)
890 return EINVAL;
891 strcpy(fp->name, AudioEadpcm);
892 fp->encoding = AUDIO_ENCODING_ADPCM;
893 fp->precision = 8;
894 fp->flags = 0;
895 break;
896 default:
897 return EINVAL;
898 /*NOTREACHED*/
899 }
900 return (0);
901 }
902
903 int
904 ad1848_set_params(addr, setmode, usemode, p, r)
905 void *addr;
906 int setmode, usemode;
907 struct audio_params *p, *r;
908 {
909 struct ad1848_softc *sc = addr;
910 int error, bits, enc;
911 void (*pswcode) __P((void *, u_char *buf, int cnt));
912 void (*rswcode) __P((void *, u_char *buf, int cnt));
913
914 DPRINTF(("ad1848_set_params: %d %d %d %ld\n",
915 p->encoding, p->precision, p->channels, p->sample_rate));
916
917 enc = p->encoding;
918 pswcode = rswcode = 0;
919 switch (enc) {
920 case AUDIO_ENCODING_SLINEAR_LE:
921 if (p->precision == 8) {
922 enc = AUDIO_ENCODING_ULINEAR_LE;
923 pswcode = rswcode = change_sign8;
924 }
925 break;
926 case AUDIO_ENCODING_SLINEAR_BE:
927 if (p->precision == 16 && (sc->mode == 1
928 #if AD1845_HACK
929 || sc->is_ad1845
930 #endif
931 )) {
932 enc = AUDIO_ENCODING_SLINEAR_LE;
933 pswcode = rswcode = swap_bytes;
934 }
935 break;
936 case AUDIO_ENCODING_ULINEAR_LE:
937 if (p->precision == 16) {
938 enc = AUDIO_ENCODING_SLINEAR_LE;
939 pswcode = rswcode = change_sign16_le;
940 }
941 break;
942 case AUDIO_ENCODING_ULINEAR_BE:
943 if (p->precision == 16) {
944 if (sc->mode == 1
945 #if AD1845_HACK
946 || sc->is_ad1845
947 #endif
948 ) {
949 enc = AUDIO_ENCODING_SLINEAR_LE;
950 pswcode = swap_bytes_change_sign16_le;
951 rswcode = change_sign16_swap_bytes_le;
952 } else {
953 enc = AUDIO_ENCODING_SLINEAR_BE;
954 pswcode = rswcode = change_sign16_be;
955 }
956 }
957 break;
958 }
959 switch (enc) {
960 case AUDIO_ENCODING_ULAW:
961 bits = FMT_ULAW >> 5;
962 break;
963 case AUDIO_ENCODING_ALAW:
964 bits = FMT_ALAW >> 5;
965 break;
966 case AUDIO_ENCODING_ADPCM:
967 bits = FMT_ADPCM >> 5;
968 break;
969 case AUDIO_ENCODING_SLINEAR_LE:
970 if (p->precision == 16)
971 bits = FMT_TWOS_COMP >> 5;
972 else
973 return EINVAL;
974 break;
975 case AUDIO_ENCODING_SLINEAR_BE:
976 if (p->precision == 16)
977 bits = FMT_TWOS_COMP_BE >> 5;
978 else
979 return EINVAL;
980 break;
981 case AUDIO_ENCODING_ULINEAR_LE:
982 if (p->precision == 8)
983 bits = FMT_PCM8 >> 5;
984 else
985 return EINVAL;
986 break;
987 default:
988 return EINVAL;
989 }
990
991 if (p->channels < 1 || p->channels > 2)
992 return EINVAL;
993
994 error = ad1848_set_speed(sc, &p->sample_rate);
995 if (error)
996 return error;
997
998 p->sw_code = pswcode;
999 r->sw_code = rswcode;
1000
1001 sc->format_bits = bits;
1002 sc->channels = p->channels;
1003 sc->precision = p->precision;
1004 sc->need_commit = 1;
1005
1006 DPRINTF(("ad1848_set_params succeeded, bits=%x\n", bits));
1007 return (0);
1008 }
1009
1010 int
1011 ad1848_set_rec_port(sc, port)
1012 struct ad1848_softc *sc;
1013 int port;
1014 {
1015 u_char inp, reg;
1016
1017 DPRINTF(("ad1848_set_rec_port: 0x%x\n", port));
1018
1019 if (port == MIC_IN_PORT)
1020 inp = MIC_INPUT;
1021 else if (port == LINE_IN_PORT)
1022 inp = LINE_INPUT;
1023 else if (port == DAC_IN_PORT)
1024 inp = MIXED_DAC_INPUT;
1025 else if (sc->mode >= 2 && port == AUX1_IN_PORT)
1026 inp = AUX_INPUT;
1027 else
1028 return(EINVAL);
1029
1030 reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
1031 reg &= INPUT_SOURCE_MASK;
1032 ad_write(sc, SP_LEFT_INPUT_CONTROL, (inp|reg));
1033
1034 reg = ad_read(sc, SP_RIGHT_INPUT_CONTROL);
1035 reg &= INPUT_SOURCE_MASK;
1036 ad_write(sc, SP_RIGHT_INPUT_CONTROL, (inp|reg));
1037
1038 sc->rec_port = port;
1039
1040 return (0);
1041 }
1042
1043 int
1044 ad1848_get_rec_port(sc)
1045 struct ad1848_softc *sc;
1046 {
1047 return (sc->rec_port);
1048 }
1049
1050 int
1051 ad1848_round_blocksize(addr, blk)
1052 void *addr;
1053 int blk;
1054 {
1055
1056 /* Round to a multiple of the biggest sample size. */
1057 return (blk &= -4);
1058 }
1059
1060 int
1061 ad1848_open(addr, flags)
1062 void *addr;
1063 int flags;
1064 {
1065 struct ad1848_softc *sc = addr;
1066 u_char reg;
1067
1068 DPRINTF(("ad1848_open: sc=%p\n", sc));
1069
1070 sc->open_mode = flags;
1071
1072 /* Enable interrupts */
1073 DPRINTF(("ad1848_open: enable intrs\n"));
1074 reg = ad_read(sc, SP_PIN_CONTROL);
1075 ad_write(sc, SP_PIN_CONTROL, reg | INTERRUPT_ENABLE);
1076
1077 /* If recording && monitoring, the playback part is also used. */
1078 if (flags & FREAD && sc->mute[AD1848_MONITOR_CHANNEL] == 0)
1079 ad1848_mute_wave_output(sc, WAVE_UNMUTE1, 1);
1080
1081 #ifdef AUDIO_DEBUG
1082 if (ad1848debug)
1083 ad1848_dump_regs(sc);
1084 #endif
1085
1086 return 0;
1087 }
1088
1089 /*
1090 * Close function is called at splaudio().
1091 */
1092 void
1093 ad1848_close(addr)
1094 void *addr;
1095 {
1096 struct ad1848_softc *sc = addr;
1097 u_char reg;
1098
1099 sc->open_mode = 0;
1100
1101 ad1848_mute_wave_output(sc, WAVE_UNMUTE1, 0);
1102
1103 /* Disable interrupts */
1104 DPRINTF(("ad1848_close: disable intrs\n"));
1105 reg = ad_read(sc, SP_PIN_CONTROL);
1106 ad_write(sc, SP_PIN_CONTROL, reg & ~INTERRUPT_ENABLE);
1107
1108 #ifdef AUDIO_DEBUG
1109 if (ad1848debug)
1110 ad1848_dump_regs(sc);
1111 #endif
1112 }
1113
1114 /*
1115 * Lower-level routines
1116 */
1117 int
1118 ad1848_commit_settings(addr)
1119 void *addr;
1120 {
1121 struct ad1848_softc *sc = addr;
1122 int timeout;
1123 u_char fs;
1124 int s;
1125
1126 if (!sc->need_commit)
1127 return 0;
1128
1129 s = splaudio();
1130
1131 ad1848_mute_wave_output(sc, WAVE_MUTE0, 1);
1132
1133 ad_set_MCE(sc, 1); /* Enables changes to the format select reg */
1134
1135 fs = sc->speed_bits | (sc->format_bits << 5);
1136
1137 if (sc->channels == 2)
1138 fs |= FMT_STEREO;
1139
1140 ad_write(sc, SP_CLOCK_DATA_FORMAT, fs);
1141
1142 /*
1143 * If mode >= 2 (CS4231), set I28 also.
1144 * It's the capture format register.
1145 */
1146 if (sc->mode >= 2) {
1147 /*
1148 * Gravis Ultrasound MAX SDK sources says something about
1149 * errata sheets, with the implication that these inb()s
1150 * are necessary.
1151 */
1152 (void)ADREAD(sc, AD1848_IDATA);
1153 (void)ADREAD(sc, AD1848_IDATA);
1154 /* Write to I8 starts resyncronization. Wait for completion. */
1155 timeout = 100000;
1156 while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
1157 timeout--;
1158
1159 ad_write(sc, CS_REC_FORMAT, fs);
1160 (void)ADREAD(sc, AD1848_IDATA);
1161 (void)ADREAD(sc, AD1848_IDATA);
1162 /* Now wait for resync for capture side of the house */
1163 }
1164 /*
1165 * Write to I8 starts resyncronization. Wait until it completes.
1166 */
1167 timeout = 100000;
1168 while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT) {
1169 delay(10);
1170 timeout--;
1171 }
1172
1173 if (ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
1174 printf("ad1848_commit: Auto calibration timed out\n");
1175
1176 /*
1177 * Starts the calibration process and
1178 * enters playback mode after it.
1179 */
1180 ad_set_MCE(sc, 0);
1181 wait_for_calibration(sc);
1182
1183 ad1848_mute_wave_output(sc, WAVE_MUTE0, 0);
1184
1185 splx(s);
1186
1187 sc->need_commit = 0;
1188 return 0;
1189 }
1190
1191 void
1192 ad1848_reset(sc)
1193 struct ad1848_softc *sc;
1194 {
1195 u_char r;
1196
1197 DPRINTF(("ad1848_reset\n"));
1198
1199 /* Clear the PEN and CEN bits */
1200 r = ad_read(sc, SP_INTERFACE_CONFIG);
1201 r &= ~(CAPTURE_ENABLE | PLAYBACK_ENABLE);
1202 ad_write(sc, SP_INTERFACE_CONFIG, r);
1203
1204 if (sc->mode >= 2) {
1205 ADWRITE(sc, AD1848_IADDR, CS_IRQ_STATUS);
1206 ADWRITE(sc, AD1848_IDATA, 0);
1207 }
1208 /* Clear interrupt status */
1209 ADWRITE(sc, AD1848_STATUS, 0);
1210 #ifdef AUDIO_DEBUG
1211 if (ad1848debug)
1212 ad1848_dump_regs(sc);
1213 #endif
1214 }
1215
1216 int
1217 ad1848_set_speed(sc, argp)
1218 struct ad1848_softc *sc;
1219 u_long *argp;
1220 {
1221 /*
1222 * The sampling speed is encoded in the least significant nible of I8.
1223 * The LSB selects the clock source (0=24.576 MHz, 1=16.9344 Mhz) and
1224 * other three bits select the divisor (indirectly):
1225 *
1226 * The available speeds are in the following table. Keep the speeds in
1227 * the increasing order.
1228 */
1229 typedef struct {
1230 int speed;
1231 u_char bits;
1232 } speed_struct;
1233 u_long arg = *argp;
1234
1235 static const speed_struct speed_table[] = {
1236 {5510, (0 << 1) | 1},
1237 {5510, (0 << 1) | 1},
1238 {6620, (7 << 1) | 1},
1239 {8000, (0 << 1) | 0},
1240 {9600, (7 << 1) | 0},
1241 {11025, (1 << 1) | 1},
1242 {16000, (1 << 1) | 0},
1243 {18900, (2 << 1) | 1},
1244 {22050, (3 << 1) | 1},
1245 {27420, (2 << 1) | 0},
1246 {32000, (3 << 1) | 0},
1247 {33075, (6 << 1) | 1},
1248 {37800, (4 << 1) | 1},
1249 {44100, (5 << 1) | 1},
1250 {48000, (6 << 1) | 0}
1251 };
1252
1253 int i, n, selected = -1;
1254
1255 n = sizeof(speed_table) / sizeof(speed_struct);
1256
1257 if (arg < speed_table[0].speed)
1258 selected = 0;
1259 if (arg > speed_table[n - 1].speed)
1260 selected = n - 1;
1261
1262 for (i = 1 /*really*/ ; selected == -1 && i < n; i++)
1263 if (speed_table[i].speed == arg)
1264 selected = i;
1265 else if (speed_table[i].speed > arg) {
1266 int diff1, diff2;
1267
1268 diff1 = arg - speed_table[i - 1].speed;
1269 diff2 = speed_table[i].speed - arg;
1270
1271 if (diff1 < diff2)
1272 selected = i - 1;
1273 else
1274 selected = i;
1275 }
1276
1277 if (selected == -1) {
1278 printf("ad1848: Can't find speed???\n");
1279 selected = 3;
1280 }
1281
1282 sc->speed_bits = speed_table[selected].bits;
1283 sc->need_commit = 1;
1284 *argp = speed_table[selected].speed;
1285
1286 return (0);
1287 }
1288
1289 /*
1290 * Halt I/O
1291 */
1292 int
1293 ad1848_halt_output(addr)
1294 void *addr;
1295 {
1296 struct ad1848_softc *sc = addr;
1297 u_char reg;
1298
1299 DPRINTF(("ad1848: ad1848_halt_output\n"));
1300
1301 reg = ad_read(sc, SP_INTERFACE_CONFIG);
1302 ad_write(sc, SP_INTERFACE_CONFIG, reg & ~PLAYBACK_ENABLE);
1303
1304 return(0);
1305 }
1306
1307 int
1308 ad1848_halt_input(addr)
1309 void *addr;
1310 {
1311 struct ad1848_softc *sc = addr;
1312 u_char reg;
1313
1314 DPRINTF(("ad1848: ad1848_halt_input\n"));
1315
1316 reg = ad_read(sc, SP_INTERFACE_CONFIG);
1317 ad_write(sc, SP_INTERFACE_CONFIG, reg & ~CAPTURE_ENABLE);
1318
1319 return(0);
1320 }
1321