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