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