Home | History | Annotate | Line # | Download | only in dev
awacs.c revision 1.26.2.5
      1 /*	$NetBSD: awacs.c,v 1.26.2.5 2007/12/03 18:37:17 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: awacs.c,v 1.26.2.5 2007/12/03 18:37:17 ad Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/audioio.h>
     34 #include <sys/device.h>
     35 #include <sys/malloc.h>
     36 #include <sys/systm.h>
     37 #include <sys/kthread.h>
     38 #include <sys/kernel.h>
     39 
     40 #include <dev/auconv.h>
     41 #include <dev/audio_if.h>
     42 #include <dev/mulaw.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <machine/autoconf.h>
     47 #include <machine/pio.h>
     48 
     49 #include <dev/ofw/openfirm.h>
     50 #include <macppc/dev/dbdma.h>
     51 
     52 #include <dev/i2c/sgsmixvar.h>
     53 #include "sgsmix.h"
     54 #include "opt_awacs.h"
     55 
     56 #ifdef AWACS_DEBUG
     57 # define DPRINTF printf
     58 #else
     59 # define DPRINTF while (0) printf
     60 #endif
     61 
     62 /* sc_flags values */
     63 #define AWACS_CAP_BSWAP		0x0001
     64 
     65 struct awacs_softc {
     66 	struct device sc_dev;
     67 	int sc_flags;
     68 
     69 	void (*sc_ointr)(void *);	/* DMA completion intr handler */
     70 	void *sc_oarg;			/* arg for sc_ointr() */
     71 	int sc_opages;			/* # of output pages */
     72 
     73 	void (*sc_iintr)(void *);	/* DMA completion intr handler */
     74 	void *sc_iarg;			/* arg for sc_iintr() */
     75 
     76 	uint32_t sc_record_source;	/* recording source mask */
     77 	uint32_t sc_output_mask;	/* output mask */
     78 	uint32_t sc_headphones_mask;	/* which reading of the gpio means */
     79 	uint32_t sc_headphones_in;	/* headphones are present */
     80 
     81 	int sc_screamer;
     82 	int sc_have_perch;
     83 	int vol_l, vol_r;
     84 	int sc_bass, sc_treble;
     85 	lwp_t *sc_thread;
     86 	int sc_event;
     87 	int sc_output_wanted;
     88 #if NSGSMIX > 0
     89 	struct device *sc_sgsmix;
     90 #endif
     91 
     92 	char *sc_reg;
     93 	u_int sc_codecctl0;
     94 	u_int sc_codecctl1;
     95 	u_int sc_codecctl2;
     96 	u_int sc_codecctl4;
     97 	u_int sc_codecctl5;
     98 	u_int sc_codecctl6;
     99 	u_int sc_codecctl7;
    100 	u_int sc_soundctl;
    101 
    102 	struct dbdma_regmap *sc_odma;
    103 	struct dbdma_regmap *sc_idma;
    104 	struct dbdma_command *sc_odmacmd;
    105 	struct dbdma_command *sc_idmacmd;
    106 
    107 #define AWACS_NFORMATS	2
    108 	struct audio_format sc_formats[AWACS_NFORMATS];
    109 };
    110 
    111 static int awacs_match(struct device *, struct cfdata *, void *);
    112 static void awacs_attach(struct device *, struct device *, void *);
    113 static int awacs_intr(void *);
    114 static int awacs_status_intr(void *);
    115 
    116 static void awacs_close(void *);
    117 static int awacs_query_encoding(void *, struct audio_encoding *);
    118 static int awacs_set_params(void *, int, int, audio_params_t *, audio_params_t *,
    119 		     stream_filter_list_t *, stream_filter_list_t *);
    120 
    121 static int awacs_round_blocksize(void *, int, int, const audio_params_t *);
    122 static int awacs_trigger_output(void *, void *, void *, int, void (*)(void *),
    123 			 void *, const audio_params_t *);
    124 static int awacs_trigger_input(void *, void *, void *, int, void (*)(void *),
    125 			void *, const audio_params_t *);
    126 static int awacs_halt_output(void *);
    127 static int awacs_halt_input(void *);
    128 static int awacs_getdev(void *, struct audio_device *);
    129 static int awacs_set_port(void *, mixer_ctrl_t *);
    130 static int awacs_get_port(void *, mixer_ctrl_t *);
    131 static int awacs_query_devinfo(void *, mixer_devinfo_t *);
    132 static size_t awacs_round_buffersize(void *, int, size_t);
    133 static paddr_t awacs_mappage(void *, void *, off_t, int);
    134 static int awacs_get_props(void *);
    135 
    136 static inline u_int awacs_read_reg(struct awacs_softc *, int);
    137 static inline void awacs_write_reg(struct awacs_softc *, int, int);
    138 static void awacs_write_codec(struct awacs_softc *, int);
    139 
    140 static void awacs_set_volume(struct awacs_softc *, int, int);
    141 static void awacs_set_speaker_volume(struct awacs_softc *, int, int);
    142 static void awacs_set_ext_volume(struct awacs_softc *, int, int);
    143 static void awacs_set_loopthrough_volume(struct awacs_softc *, int, int);
    144 static int awacs_set_rate(struct awacs_softc *, const audio_params_t *);
    145 static void awacs_select_output(struct awacs_softc *, int);
    146 static int awacs_check_headphones(struct awacs_softc *);
    147 static void awacs_thread(void *);
    148 
    149 #if NSGSMIX > 0
    150 static void awacs_set_bass(struct awacs_softc *, int);
    151 static void awacs_set_treble(struct awacs_softc *, int);
    152 #endif
    153 static int awacs_setup_sgsmix(struct device *);
    154 
    155 CFATTACH_DECL(awacs, sizeof(struct awacs_softc),
    156     awacs_match, awacs_attach, NULL, NULL);
    157 
    158 const struct audio_hw_if awacs_hw_if = {
    159 	NULL,			/* open */
    160 	awacs_close,
    161 	NULL,
    162 	awacs_query_encoding,
    163 	awacs_set_params,
    164 	awacs_round_blocksize,
    165 	NULL,
    166 	NULL,
    167 	NULL,
    168 	NULL,
    169 	NULL,
    170 	awacs_halt_output,
    171 	awacs_halt_input,
    172 	NULL,
    173 	awacs_getdev,
    174 	NULL,
    175 	awacs_set_port,
    176 	awacs_get_port,
    177 	awacs_query_devinfo,
    178 	NULL,
    179 	NULL,
    180 	awacs_round_buffersize,
    181 	awacs_mappage,
    182 	awacs_get_props,
    183 	awacs_trigger_output,
    184 	awacs_trigger_input,
    185 	NULL,
    186 };
    187 
    188 struct audio_device awacs_device = {
    189 	"AWACS",
    190 	"",
    191 	"awacs"
    192 };
    193 
    194 #define AWACS_NFORMATS		2
    195 #define AWACS_FORMATS_LE	0
    196 static const struct audio_format awacs_formats[AWACS_NFORMATS] = {
    197 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    198 	 2, AUFMT_STEREO, 8, {7350, 8820, 11025, 14700, 17640, 22050, 29400, 44100}},
    199 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16,
    200 	 2, AUFMT_STEREO, 8, {7350, 8820, 11025, 14700, 17640, 22050, 29400, 44100}},
    201 };
    202 
    203 /* register offset */
    204 #define AWACS_SOUND_CTRL	0x00
    205 #define AWACS_CODEC_CTRL	0x10
    206 #define AWACS_CODEC_STATUS	0x20
    207 #define AWACS_CLIP_COUNT	0x30
    208 #define AWACS_BYTE_SWAP		0x40
    209 
    210 /* sound control */
    211 #define AWACS_INPUT_SUBFRAME0	0x00000001
    212 #define AWACS_INPUT_SUBFRAME1	0x00000002
    213 #define AWACS_INPUT_SUBFRAME2	0x00000004
    214 #define AWACS_INPUT_SUBFRAME3	0x00000008
    215 
    216 #define AWACS_OUTPUT_SUBFRAME0	0x00000010
    217 #define AWACS_OUTPUT_SUBFRAME1	0x00000020
    218 #define AWACS_OUTPUT_SUBFRAME2	0x00000040
    219 #define AWACS_OUTPUT_SUBFRAME3	0x00000080
    220 
    221 #define AWACS_RATE_44100	0x00000000
    222 #define AWACS_RATE_29400	0x00000100
    223 #define AWACS_RATE_22050	0x00000200
    224 #define AWACS_RATE_17640	0x00000300
    225 #define AWACS_RATE_14700	0x00000400
    226 #define AWACS_RATE_11025	0x00000500
    227 #define AWACS_RATE_8820		0x00000600
    228 #define AWACS_RATE_7350		0x00000700
    229 #define AWACS_RATE_MASK		0x00000700
    230 
    231 #define AWACS_ERROR		0x00000800
    232 #define AWACS_PORTCHG		0x00001000
    233 #define AWACS_INTR_ERROR	0x00002000	/* interrupt on error */
    234 #define AWACS_INTR_PORTCHG	0x00004000	/* interrupt on port change */
    235 
    236 #define AWACS_STATUS_SUBFRAME	0x00018000	/* mask */
    237 
    238 /* codec control */
    239 #define AWACS_CODEC_ADDR0	0x00000000
    240 #define AWACS_CODEC_ADDR1	0x00001000
    241 #define AWACS_CODEC_ADDR2	0x00002000
    242 #define AWACS_CODEC_ADDR4	0x00004000
    243 #define AWACS_CODEC_ADDR5	0x00005000
    244 #define AWACS_CODEC_ADDR6	0x00006000
    245 #define AWACS_CODEC_ADDR7	0x00007000
    246 #define AWACS_CODEC_EMSEL0	0x00000000
    247 #define AWACS_CODEC_EMSEL1	0x00400000
    248 #define AWACS_CODEC_EMSEL2	0x00800000
    249 #define AWACS_CODEC_EMSEL4	0x00c00000
    250 #define AWACS_CODEC_BUSY	0x01000000
    251 
    252 /* cc0 */
    253 #define AWACS_DEFAULT_CD_GAIN	0x000000bb
    254 #define AWACS_INPUT_CD		0x00000200
    255 #define AWACS_INPUT_LINE	0x00000400
    256 #define AWACS_INPUT_MICROPHONE	0x00000800
    257 #define AWACS_INPUT_MASK	0x00000e00
    258 
    259 /* cc1 */
    260 #define AWACS_LOOP_THROUGH	0x00000040
    261 #define AWACS_MUTE_SPEAKER	0x00000080
    262 #define AWACS_MUTE_HEADPHONE	0x00000200
    263 #define AWACS_PARALLEL_OUTPUT	0x00000c00
    264 
    265 /* output */
    266 #define OUTPUT_SPEAKER		1
    267 #define OUTPUT_HEADPHONES	2
    268 
    269 /* codec status */
    270 
    271 static const char *screamer[] = {"screamer", NULL};
    272 
    273 /*
    274  * list machines that have the headphone detect GPIO reversed here.
    275  * so far the only known case is the PowerBook 3400c and similar machines
    276  */
    277 static const char *detect_reversed[] = {"AAPL,3400/2400",
    278 					"AAPL,3500",
    279 					NULL};
    280 
    281 static const char *use_gpio4[] = {	"PowerMac3,3",
    282 					NULL};
    283 
    284 static int
    285 awacs_match(struct device *parent, struct cfdata *match, void *aux)
    286 {
    287 	struct confargs *ca;
    288 
    289 	ca = aux;
    290 
    291 	if (strcmp(ca->ca_name, "awacs") == 0 ||
    292 	    strcmp(ca->ca_name, "davbus") == 0)
    293 		return 100;
    294 
    295 	if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
    296 		return 0;
    297 
    298 	if (strcmp(ca->ca_name, "i2s") == 0)
    299 		return 1;
    300 
    301 	return 0;
    302 }
    303 
    304 static void
    305 awacs_attach(struct device *parent, struct device *self, void *aux)
    306 {
    307 	struct awacs_softc *sc;
    308 	struct confargs *ca;
    309 	int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
    310 	int len = -1, perch;
    311 	int root_node;
    312 	char compat[256];
    313 
    314 	sc = (struct awacs_softc *)self;
    315 	ca = aux;
    316 
    317 	sc->sc_reg = mapiodev(ca->ca_baseaddr + ca->ca_reg[0], ca->ca_reg[1]);
    318 
    319 	/* out */
    320 	sc->sc_odma = mapiodev(ca->ca_baseaddr + ca->ca_reg[2], ca->ca_reg[3]);
    321 	sc->sc_odmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
    322 	/* in */
    323 	sc->sc_idma = mapiodev(ca->ca_baseaddr + ca->ca_reg[4], ca->ca_reg[5]);
    324 	sc->sc_idmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
    325 
    326 	if (strcmp(ca->ca_name, "i2s") == 0) {
    327 		int node, intr[6];
    328 
    329 		node = OF_child(ca->ca_node);
    330 		if (node == 0) {
    331 			printf("no i2s-a child\n");
    332 			return;
    333 		}
    334 		if (OF_getprop(node, "interrupts", intr, sizeof(intr)) == -1) {
    335 			printf("no interrupt property\n");
    336 			return;
    337 		}
    338 
    339 		cirq = intr[0];
    340 		oirq = intr[2];
    341 		iirq = intr[4];
    342 		cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
    343 		oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
    344 		iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
    345 	} else if (ca->ca_nintr == 24) {
    346 		cirq = ca->ca_intr[0];
    347 		oirq = ca->ca_intr[2];
    348 		iirq = ca->ca_intr[4];
    349 		cirq_type = ca->ca_intr[1] ? IST_LEVEL : IST_EDGE;
    350 		oirq_type = ca->ca_intr[3] ? IST_LEVEL : IST_EDGE;
    351 		iirq_type = ca->ca_intr[5] ? IST_LEVEL : IST_EDGE;
    352 	} else {
    353 		cirq = ca->ca_intr[0];
    354 		oirq = ca->ca_intr[1];
    355 		iirq = ca->ca_intr[2];
    356 		cirq_type = oirq_type = iirq_type = IST_EDGE;
    357 	}
    358 
    359 	intr_establish(cirq, cirq_type, IPL_BIO, awacs_status_intr, sc);
    360 	intr_establish(oirq, oirq_type, IPL_AUDIO, awacs_intr, sc);
    361 	intr_establish(iirq, iirq_type, IPL_AUDIO, awacs_intr, sc);
    362 
    363 	/* check if the chip is a screamer */
    364 	sc->sc_screamer = (of_compatible(ca->ca_node, screamer) != -1);
    365 	if (!sc->sc_screamer) {
    366 		/* look for 'sound' child node */
    367 		int sound_node;
    368 
    369 		sound_node = OF_child(ca->ca_node);
    370 		while ((sound_node != 0) && (!sc->sc_screamer)) {
    371 
    372 			sc->sc_screamer =
    373 			    (of_compatible(sound_node, screamer) != -1);
    374 			sound_node = OF_peer(sound_node);
    375 		}
    376 	}
    377 
    378 	if (sc->sc_screamer) {
    379 		printf(" Screamer");
    380 	}
    381 
    382 	printf(": irq %d,%d,%d\n", cirq, oirq, iirq);
    383 
    384 	sc->vol_l = 0;
    385 	sc->vol_r = 0;
    386 
    387 	memcpy(&sc->sc_formats, awacs_formats, sizeof(awacs_formats));
    388 
    389 	/* XXX Uni-North based models don't have byteswap capability. */
    390 	if (OF_finddevice("/uni-n") == -1) {
    391 
    392 		sc->sc_flags |= AWACS_CAP_BSWAP;
    393 	} else {
    394 
    395 		AUFMT_INVALIDATE(&sc->sc_formats[AWACS_FORMATS_LE]);
    396 	}
    397 
    398 	sc->sc_soundctl = AWACS_INPUT_SUBFRAME0 | AWACS_OUTPUT_SUBFRAME0 |
    399 		AWACS_RATE_44100 | AWACS_INTR_PORTCHG;
    400 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
    401 
    402 	sc->sc_codecctl0 = AWACS_CODEC_ADDR0 | AWACS_CODEC_EMSEL0;
    403 	sc->sc_codecctl1 = AWACS_CODEC_ADDR1 | AWACS_CODEC_EMSEL0;
    404 	sc->sc_codecctl2 = AWACS_CODEC_ADDR2 | AWACS_CODEC_EMSEL0;
    405 	sc->sc_codecctl4 = AWACS_CODEC_ADDR4 | AWACS_CODEC_EMSEL0;
    406 	sc->sc_codecctl5 = AWACS_CODEC_ADDR5 | AWACS_CODEC_EMSEL0;
    407 	sc->sc_codecctl6 = AWACS_CODEC_ADDR6 | AWACS_CODEC_EMSEL0;
    408 	sc->sc_codecctl7 = AWACS_CODEC_ADDR7 | AWACS_CODEC_EMSEL0;
    409 
    410 	sc->sc_codecctl0 |= AWACS_INPUT_CD | AWACS_DEFAULT_CD_GAIN;
    411 	awacs_write_codec(sc, sc->sc_codecctl0);
    412 
    413 	/* Set loopthrough for external mixer on beige G3 */
    414 	sc->sc_codecctl1 |= (AWACS_LOOP_THROUGH | AWACS_PARALLEL_OUTPUT);
    415 
    416         printf("%s: ", sc->sc_dev.dv_xname);
    417 
    418 	/*
    419 	 * all(?) awacs have GPIOs to detect if there's something plugged into
    420 	 * the headphone jack. The other GPIOs are either used for other jacks
    421 	 * ( the PB3400c's microphone jack for instance ) or unused.
    422 	 * The problem is that there are at least three different ways how
    423 	 * those GPIOs are wired to the actual jacks.
    424 	 * For now we bother only with headphone detection
    425 	 */
    426 	perch = OF_finddevice("/perch");
    427 	root_node = OF_finddevice("/");
    428 	if (of_compatible(root_node, detect_reversed) != -1) {
    429 
    430 		/* 0x02 is for the microphone jack, high active */
    431 		/*
    432 		 * for some reason the gpio for the headphones jack is low
    433 		 * active on the PB3400 and similar machines
    434 		 */
    435 		sc->sc_headphones_mask = 0x8;
    436 		sc->sc_headphones_in = 0x0;
    437 	} else if ((perch != -1) ||
    438 	    (of_compatible(root_node, use_gpio4) != -1)) {
    439 		/*
    440 		 * this is for the beige G3's 'personality card' which uses
    441 		 * yet another wiring of the headphone detect GPIOs
    442 		 * some G4s use it as well
    443 		 */
    444 		sc->sc_headphones_mask = 0x04;
    445 		sc->sc_headphones_in = 0x04;
    446 	} else {
    447 		/* while on most machines it's high active as well */
    448 		sc->sc_headphones_mask = 0x8;
    449 		sc->sc_headphones_in = 0x8;
    450 	}
    451 	if (awacs_check_headphones(sc)) {
    452 
    453                 /* default output to headphones */
    454                 printf("headphones\n");
    455                 sc->sc_output_mask = OUTPUT_HEADPHONES;
    456         } else {
    457 
    458                 /* default output to speakers */
    459                 printf("speaker\n");
    460                 sc->sc_output_mask = OUTPUT_SPEAKER;
    461         }
    462 	sc->sc_output_wanted = sc->sc_output_mask;
    463 	awacs_select_output(sc, sc->sc_output_mask);
    464 
    465 	delay(100);
    466 	if (sc->sc_screamer) {
    467 		awacs_write_codec(sc, sc->sc_codecctl6);
    468 		awacs_write_codec(sc, sc->sc_codecctl5);
    469 		delay(2);
    470 		awacs_write_codec(sc, sc->sc_codecctl1);
    471 		awacs_write_codec(sc, sc->sc_codecctl7);
    472 	}
    473 
    474 	/* default input from CD */
    475 	sc->sc_record_source = 1 << 0;
    476 	sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    477 	sc->sc_codecctl0 |= AWACS_INPUT_CD;
    478 	awacs_write_codec(sc, sc->sc_codecctl0);
    479 
    480 	/* Enable interrupts and looping mode. */
    481 	/* XXX ... */
    482 
    483 	sc->sc_codecctl1 |= (AWACS_LOOP_THROUGH | AWACS_PARALLEL_OUTPUT);
    484 	awacs_write_codec(sc, sc->sc_codecctl1);
    485 
    486 #if NSGSMIX > 0
    487 	sc->sc_sgsmix = NULL;
    488 #endif
    489 	sc->sc_have_perch = 0;
    490 	if (perch != -1) {
    491 
    492 		len = OF_getprop(perch, "compatible", compat, 255);
    493 		if (len > 0) {
    494 			printf("%s: found '%s' personality card\n",
    495 			    sc->sc_dev.dv_xname, compat);
    496 			sc->sc_have_perch = 1;
    497 			config_finalize_register(&sc->sc_dev,
    498 			    awacs_setup_sgsmix);
    499 		}
    500 	}
    501 
    502 	/* Set initial volume[s] */
    503 	awacs_set_volume(sc, 144, 144);
    504 	awacs_set_loopthrough_volume(sc, 0, 0);
    505 
    506 	audio_attach_mi(&awacs_hw_if, sc, &sc->sc_dev);
    507 
    508 	if (kthread_create(PRI_NONE, 0, NULL, awacs_thread, sc,
    509 	    &sc->sc_thread, "%s", "awacs") != 0) {
    510 		printf("awacs: unable to create event kthread");
    511 	}
    512 }
    513 
    514 static int
    515 awacs_setup_sgsmix(struct device *cookie)
    516 {
    517 	struct awacs_softc *sc = (struct awacs_softc *)cookie;
    518 #if NSGSMIX > 0
    519 	struct device *dv;
    520 #endif
    521 
    522 	if (!sc->sc_have_perch)
    523 		return 0;
    524 #if NSGSMIX > 0
    525 	/* look for sgsmix */
    526 	for (dv = alldevs.tqh_first; dv; dv=dv->dv_list.tqe_next) {
    527 		if (device_is_a(dv, "sgsmix")) {
    528 			sc->sc_sgsmix = dv;
    529 			break;
    530 		}
    531 	}
    532 	if (sc->sc_sgsmix == NULL)
    533 		return 0;
    534 
    535 	printf("%s: using %s\n", sc->sc_dev.dv_xname,
    536 	    sc->sc_sgsmix->dv_xname);
    537 
    538 	awacs_select_output(sc, sc->sc_output_mask);
    539 	awacs_set_volume(sc, sc->vol_l, sc->vol_r);
    540 	awacs_set_bass(sc, 128);
    541 	awacs_set_treble(sc, 128);
    542 	wakeup(&sc->sc_event);
    543 #endif
    544 	return 0;
    545 }
    546 
    547 
    548 static inline u_int
    549 awacs_read_reg(struct awacs_softc *sc, int reg)
    550 {
    551 	char *addr;
    552 
    553 	addr = sc->sc_reg;
    554 	return in32rb(addr + reg);
    555 }
    556 
    557 static inline void
    558 awacs_write_reg(struct awacs_softc *sc, int reg, int val)
    559 {
    560 	char *addr;
    561 
    562 	addr = sc->sc_reg;
    563 	out32rb(addr + reg, val);
    564 }
    565 
    566 static void
    567 awacs_write_codec(struct awacs_softc *sc, int value)
    568 {
    569 
    570 	do {
    571 		delay(100);
    572 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    573 
    574 	awacs_write_reg(sc, AWACS_CODEC_CTRL, value);
    575 
    576 	do {
    577 		delay(100);
    578 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    579 }
    580 
    581 static int
    582 awacs_intr(void *v)
    583 {
    584 	struct awacs_softc *sc;
    585 	struct dbdma_command *cmd;
    586 	int count;
    587 	int status;
    588 
    589 	sc = v;
    590 	cmd = sc->sc_odmacmd;
    591 	count = sc->sc_opages;
    592 	/* Fill used buffer(s). */
    593 	while (count-- > 0) {
    594 		/* if DBDMA_INT_ALWAYS */
    595 		if (in16rb(&cmd->d_command) & 0x30) {	/* XXX */
    596 			status = in16rb(&cmd->d_status);
    597 			cmd->d_status = 0;
    598 			if (status)	/* status == 0x8400 */
    599 				if (sc->sc_ointr)
    600 					(*sc->sc_ointr)(sc->sc_oarg);
    601 		}
    602 		cmd++;
    603 	}
    604 
    605 	return 1;
    606 }
    607 
    608 /*
    609  * Close function is called at splaudio().
    610  */
    611 static void
    612 awacs_close(void *h)
    613 {
    614 	struct awacs_softc *sc;
    615 
    616 	sc = h;
    617 	awacs_halt_output(sc);
    618 	awacs_halt_input(sc);
    619 
    620 	sc->sc_ointr = 0;
    621 	sc->sc_iintr = 0;
    622 }
    623 
    624 static int
    625 awacs_query_encoding(void *h, struct audio_encoding *ae)
    626 {
    627 	struct awacs_softc *sc;
    628 
    629 	sc = h;
    630 	ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
    631 
    632 	switch (ae->index) {
    633 	case 0:
    634 		strcpy(ae->name, AudioEslinear);
    635 		ae->encoding = AUDIO_ENCODING_SLINEAR;
    636 		ae->precision = 16;
    637 		ae->flags = 0;
    638 		return 0;
    639 	case 1:
    640 		strcpy(ae->name, AudioEslinear_be);
    641 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
    642 		ae->precision = 16;
    643 		ae->flags = 0;
    644 		return 0;
    645 	case 2:
    646 		strcpy(ae->name, AudioEslinear_le);
    647 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
    648 		ae->precision = 16;
    649 		if (sc->sc_flags & AWACS_CAP_BSWAP)
    650 			ae->flags = 0;
    651 		return 0;
    652 	case 3:
    653 		strcpy(ae->name, AudioEulinear_be);
    654 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
    655 		ae->precision = 16;
    656 		return 0;
    657 	case 4:
    658 		strcpy(ae->name, AudioEulinear_le);
    659 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
    660 		ae->precision = 16;
    661 		return 0;
    662 	case 5:
    663 		strcpy(ae->name, AudioEmulaw);
    664 		ae->encoding = AUDIO_ENCODING_ULAW;
    665 		ae->precision = 8;
    666 		return 0;
    667 	case 6:
    668 		strcpy(ae->name, AudioEalaw);
    669 		ae->encoding = AUDIO_ENCODING_ALAW;
    670 		ae->precision = 8;
    671 		return 0;
    672 	default:
    673 		return EINVAL;
    674 	}
    675 }
    676 
    677 static int
    678 awacs_set_params(void *h, int setmode, int usemode,
    679 		 audio_params_t *play, audio_params_t *rec,
    680 		 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    681 {
    682 	struct awacs_softc *sc;
    683 	audio_params_t *p;
    684 	stream_filter_list_t *fil;
    685 	int mode, i;
    686 
    687 	sc = h;
    688 	p = NULL;
    689 	/*
    690 	 * This device only has one clock, so make the sample rates match.
    691 	 */
    692 	if (play->sample_rate != rec->sample_rate &&
    693 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    694 		if (setmode == AUMODE_PLAY) {
    695 			rec->sample_rate = play->sample_rate;
    696 			setmode |= AUMODE_RECORD;
    697 		} else if (setmode == AUMODE_RECORD) {
    698 			play->sample_rate = rec->sample_rate;
    699 			setmode |= AUMODE_PLAY;
    700 		} else
    701 			return EINVAL;
    702 	}
    703 
    704 	for (mode = AUMODE_RECORD; mode != -1;
    705 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    706 		if ((setmode & mode) == 0)
    707 			continue;
    708 
    709 		p = mode == AUMODE_PLAY ? play : rec;
    710 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    711 		switch (p->sample_rate) {
    712 		case 48000:	/* aurateconv */
    713 		case 44100:
    714 		case 29400:
    715 		case 22050:
    716 		case 17640:
    717 		case 14700:
    718 		case 11025:
    719 		case 8820:
    720 		case 8000:	/* aurateconv */
    721 		case 7350:
    722 			break;
    723 		default:
    724 			return EINVAL;
    725 		}
    726 		awacs_write_reg(sc, AWACS_BYTE_SWAP, 0);
    727 		i = auconv_set_converter(sc->sc_formats, AWACS_NFORMATS,
    728 					 mode, p, true, fil);
    729 		if (i < 0)
    730 			return EINVAL;
    731 		if (i == AWACS_FORMATS_LE)
    732 			awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
    733 		if (fil->req_size > 0)
    734 			p = &fil->filters[0].param;
    735 		if (awacs_set_rate(sc, p))
    736 			return EINVAL;
    737 	}
    738 	return 0;
    739 }
    740 
    741 static int
    742 awacs_round_blocksize(void *h, int size, int mode, const audio_params_t *param)
    743 {
    744 
    745 	if (size < PAGE_SIZE)
    746 		size = PAGE_SIZE;
    747 	return size & ~PGOFSET;
    748 }
    749 
    750 static int
    751 awacs_halt_output(void *h)
    752 {
    753 	struct awacs_softc *sc;
    754 
    755 	sc = h;
    756 	dbdma_stop(sc->sc_odma);
    757 	dbdma_reset(sc->sc_odma);
    758 	return 0;
    759 }
    760 
    761 static int
    762 awacs_halt_input(void *h)
    763 {
    764 	struct awacs_softc *sc;
    765 
    766 	sc = h;
    767 	dbdma_stop(sc->sc_idma);
    768 	dbdma_reset(sc->sc_idma);
    769 	return 0;
    770 }
    771 
    772 static int
    773 awacs_getdev(void *h, struct audio_device *retp)
    774 {
    775 
    776 	*retp = awacs_device;
    777 	return 0;
    778 }
    779 
    780 enum {
    781 	AWACS_MONITOR_CLASS,
    782 	AWACS_OUTPUT_CLASS,
    783 	AWACS_RECORD_CLASS,
    784 	AWACS_OUTPUT_SELECT,
    785 	AWACS_VOL_MASTER,
    786 	AWACS_INPUT_SELECT,
    787 	AWACS_VOL_INPUT,
    788 	AWACS_VOL_MONITOR,
    789 	AWACS_BASS,
    790 	AWACS_TREBLE,
    791 	AWACS_ENUM_LAST
    792 };
    793 
    794 static int
    795 awacs_set_port(void *h, mixer_ctrl_t *mc)
    796 {
    797 	struct awacs_softc *sc;
    798 	int l, r;
    799 
    800 	DPRINTF("awacs_set_port dev = %d, type = %d\n", mc->dev, mc->type);
    801 	sc = h;
    802 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    803 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    804 
    805 	switch (mc->dev) {
    806 	case AWACS_OUTPUT_SELECT:
    807 		/* No change necessary? */
    808 		if (mc->un.mask == sc->sc_output_mask)
    809 			return 0;
    810 		awacs_select_output(sc, mc->un.mask);
    811 		return 0;
    812 
    813 	case AWACS_VOL_MASTER:
    814 		awacs_set_volume(sc, l, r);
    815 		return 0;
    816 
    817 	case AWACS_INPUT_SELECT:
    818 		/* no change necessary? */
    819 		if (mc->un.mask == sc->sc_record_source)
    820 			return 0;
    821 		switch (mc->un.mask) {
    822 		case 1 << 0: /* CD */
    823 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    824 			sc->sc_codecctl0 |= AWACS_INPUT_CD;
    825 			awacs_write_codec(sc, sc->sc_codecctl0);
    826 			break;
    827 		case 1 << 1: /* microphone */
    828 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    829 			sc->sc_codecctl0 |= AWACS_INPUT_MICROPHONE;
    830 			awacs_write_codec(sc, sc->sc_codecctl0);
    831 			break;
    832 		case 1 << 2: /* line in */
    833 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    834 			sc->sc_codecctl0 |= AWACS_INPUT_LINE;
    835 			awacs_write_codec(sc, sc->sc_codecctl0);
    836 			break;
    837 		default: /* invalid argument */
    838 			return EINVAL;
    839 		}
    840 		sc->sc_record_source = mc->un.mask;
    841 		return 0;
    842 
    843 	case AWACS_VOL_INPUT:
    844 		sc->sc_codecctl0 &= ~0xff;
    845 		sc->sc_codecctl0 |= (l & 0xf0) | (r >> 4);
    846 		awacs_write_codec(sc, sc->sc_codecctl0);
    847 		return 0;
    848 
    849 	case AWACS_VOL_MONITOR:
    850 		awacs_set_loopthrough_volume(sc, l, r);
    851 		return 0;
    852 
    853 	case AWACS_VOL_MONITOR:
    854 		awacs_set_loopthrough_volume(sc, l, r);
    855 		return 0;
    856 
    857 #if NSGSMIX > 0
    858 	case AWACS_BASS:
    859 		awacs_set_bass(sc, l);
    860 		return 0;
    861 
    862 	case AWACS_TREBLE:
    863 		awacs_set_treble(sc, l);
    864 		return 0;
    865 #endif
    866 	}
    867 
    868 	return ENXIO;
    869 }
    870 
    871 static int
    872 awacs_get_port(void *h, mixer_ctrl_t *mc)
    873 {
    874 	struct awacs_softc *sc;
    875 	int l, r, vol;
    876 
    877 	sc = h;
    878 	switch (mc->dev) {
    879 	case AWACS_OUTPUT_SELECT:
    880 		mc->un.mask = sc->sc_output_mask;
    881 		return 0;
    882 
    883 	case AWACS_VOL_MASTER:
    884 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->vol_l;
    885 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->vol_r;
    886 		return 0;
    887 
    888 	case AWACS_INPUT_SELECT:
    889 		mc->un.mask = sc->sc_record_source;
    890 		return 0;
    891 
    892 	case AWACS_VOL_INPUT:
    893 		vol = sc->sc_codecctl0 & 0xff;
    894 		l = (vol & 0xf0);
    895 		r = (vol & 0x0f) << 4;
    896 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    897 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    898 		return 0;
    899 
    900 	case AWACS_VOL_MONITOR:
    901 		vol = sc->sc_codecctl5 & 0x3cf;
    902 		l = (vol & 0x3c0) >> 6;
    903 		r = vol & 0xf;
    904 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = (15 - l) << 4;
    905 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = (15 - r) << 4;
    906 		return 0;
    907 
    908 #if NSGSMIX > 0
    909 	case AWACS_BASS:
    910 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
    911 		return 0;
    912 
    913 	case AWACS_TREBLE:
    914 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
    915 		return 0;
    916 #endif
    917 
    918 	default:
    919 		return ENXIO;
    920 	}
    921 
    922 	return 0;
    923 }
    924 
    925 static int
    926 awacs_query_devinfo(void *h, mixer_devinfo_t *dip)
    927 {
    928 #if NSGSMIX > 0
    929 	struct awacs_softc *sc = h;
    930 #endif
    931 
    932 	switch (dip->index) {
    933 
    934 	case AWACS_OUTPUT_SELECT:
    935 		dip->mixer_class = AWACS_MONITOR_CLASS;
    936 		strcpy(dip->label.name, AudioNoutput);
    937 		dip->type = AUDIO_MIXER_SET;
    938 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    939 		dip->un.s.num_mem = 2;
    940 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
    941 		dip->un.s.member[0].mask = 1 << 0;
    942 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
    943 		dip->un.s.member[1].mask = 1 << 1;
    944 		return 0;
    945 
    946 	case AWACS_VOL_MASTER:
    947 		dip->mixer_class = AWACS_MONITOR_CLASS;
    948 		strcpy(dip->label.name, AudioNmaster);
    949 		dip->type = AUDIO_MIXER_VALUE;
    950 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    951 		dip->un.v.num_channels = 2;
    952 		strcpy(dip->un.v.units.name, AudioNvolume);
    953 		return 0;
    954 
    955 	case AWACS_VOL_MONITOR:
    956 		dip->mixer_class = AWACS_MONITOR_CLASS;
    957 		strcpy(dip->label.name, AudioNmonitor);
    958 		dip->type = AUDIO_MIXER_VALUE;
    959 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    960 		dip->un.v.num_channels = 2;
    961 		strcpy(dip->un.v.units.name, AudioNvolume);
    962 		return 0;
    963 
    964 	case AWACS_VOL_MONITOR:
    965 		dip->mixer_class = AWACS_MONITOR_CLASS;
    966 		strcpy(dip->label.name, AudioNmonitor);
    967 		dip->type = AUDIO_MIXER_VALUE;
    968 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    969 		dip->un.v.num_channels = 2;
    970 		strcpy(dip->un.v.units.name, AudioNvolume);
    971 		return 0;
    972 
    973 #if NSGSMIX > 0
    974 	case AWACS_BASS:
    975 		if (sc->sc_sgsmix == NULL)
    976 			return ENXIO;
    977 		dip->mixer_class = AWACS_MONITOR_CLASS;
    978 		strcpy(dip->label.name, AudioNbass);
    979 		dip->type = AUDIO_MIXER_VALUE;
    980 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    981 		dip->un.v.num_channels = 1;
    982 		strcpy(dip->un.v.units.name, AudioNbass);
    983 		return 0;
    984 
    985 	case AWACS_TREBLE:
    986 		if (sc->sc_sgsmix == NULL)
    987 			return ENXIO;
    988 		dip->mixer_class = AWACS_MONITOR_CLASS;
    989 		strcpy(dip->label.name, AudioNtreble);
    990 		dip->type = AUDIO_MIXER_VALUE;
    991 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    992 		dip->un.v.num_channels = 1;
    993 		strcpy(dip->un.v.units.name, AudioNtreble);
    994 		return 0;
    995 #endif
    996 
    997 	case AWACS_INPUT_SELECT:
    998 		dip->mixer_class = AWACS_RECORD_CLASS;
    999 		strcpy(dip->label.name, AudioNsource);
   1000 		dip->type = AUDIO_MIXER_SET;
   1001 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1002 		dip->un.s.num_mem = 3;
   1003 		strcpy(dip->un.s.member[0].label.name, AudioNcd);
   1004 		dip->un.s.member[0].mask = 1 << 0;
   1005 		strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
   1006 		dip->un.s.member[1].mask = 1 << 1;
   1007 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1008 		dip->un.s.member[2].mask = 1 << 2;
   1009 		return 0;
   1010 
   1011 	case AWACS_VOL_INPUT:
   1012 		dip->mixer_class = AWACS_RECORD_CLASS;
   1013 		strcpy(dip->label.name, AudioNrecord);
   1014 		dip->type = AUDIO_MIXER_VALUE;
   1015 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1016 		dip->un.v.num_channels = 2;
   1017 		strcpy(dip->un.v.units.name, AudioNvolume);
   1018 		return 0;
   1019 
   1020 	case AWACS_MONITOR_CLASS:
   1021 		dip->mixer_class = AWACS_MONITOR_CLASS;
   1022 		strcpy(dip->label.name, AudioCmonitor);
   1023 		dip->type = AUDIO_MIXER_CLASS;
   1024 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1025 		return 0;
   1026 
   1027 	case AWACS_OUTPUT_CLASS:
   1028 		dip->mixer_class = AWACS_OUTPUT_CLASS;
   1029 		strcpy(dip->label.name, AudioCoutputs);
   1030 		dip->type = AUDIO_MIXER_CLASS;
   1031 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1032 		return 0;
   1033 
   1034 	case AWACS_RECORD_CLASS:
   1035 		dip->mixer_class = AWACS_RECORD_CLASS;
   1036 		strcpy(dip->label.name, AudioCrecord);
   1037 		dip->type = AUDIO_MIXER_CLASS;
   1038 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1039 		return 0;
   1040 	}
   1041 
   1042 	return ENXIO;
   1043 }
   1044 
   1045 static size_t
   1046 awacs_round_buffersize(void *h, int dir, size_t size)
   1047 {
   1048 
   1049 	if (size > 65536)
   1050 		size = 65536;
   1051 	return size;
   1052 }
   1053 
   1054 static paddr_t
   1055 awacs_mappage(void *h, void *mem, off_t off, int prot)
   1056 {
   1057 
   1058 	if (off < 0)
   1059 		return -1;
   1060 	return -1;	/* XXX */
   1061 }
   1062 
   1063 static int
   1064 awacs_get_props(void *h)
   1065 {
   1066 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
   1067 }
   1068 
   1069 static int
   1070 awacs_trigger_output(void *h, void *start, void *end, int bsize,
   1071 		     void (*intr)(void *), void *arg,
   1072 		     const audio_params_t *param)
   1073 {
   1074 	struct awacs_softc *sc;
   1075 	struct dbdma_command *cmd;
   1076 	vaddr_t va;
   1077 	int i, len, intmode;
   1078 
   1079 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
   1080 	sc = h;
   1081 	cmd = sc->sc_odmacmd;
   1082 	sc->sc_ointr = intr;
   1083 	sc->sc_oarg = arg;
   1084 	sc->sc_opages = ((char *)end - (char *)start) / PAGE_SIZE;
   1085 
   1086 #ifdef DIAGNOSTIC
   1087 	if (sc->sc_opages > 16)
   1088 		panic("awacs_trigger_output");
   1089 #endif
   1090 
   1091 	va = (vaddr_t)start;
   1092 	len = 0;
   1093 	for (i = sc->sc_opages; i > 0; i--) {
   1094 		len += PAGE_SIZE;
   1095 		if (len < bsize)
   1096 			intmode = DBDMA_INT_NEVER;
   1097 		else {
   1098 			len = 0;
   1099 			intmode = DBDMA_INT_ALWAYS;
   1100 		}
   1101 
   1102 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, PAGE_SIZE, vtophys(va),
   1103 			intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1104 		va += PAGE_SIZE;
   1105 		cmd++;
   1106 	}
   1107 
   1108 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
   1109 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
   1110 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
   1111 
   1112 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
   1113 
   1114 	return 0;
   1115 }
   1116 
   1117 static int
   1118 awacs_trigger_input(void *h, void *start, void *end, int bsize,
   1119 		    void (*intr)(void *), void *arg,
   1120 		    const audio_params_t *param)
   1121 {
   1122 
   1123 	DPRINTF("awacs_trigger_input called\n");
   1124 	return 1;
   1125 }
   1126 
   1127 static void
   1128 awacs_select_output(struct awacs_softc *sc, int mask)
   1129 {
   1130 
   1131 #if NSGSMIX > 0
   1132 	if (sc->sc_sgsmix) {
   1133 		if (mask & OUTPUT_HEADPHONES) {
   1134 			/* mute speakers */
   1135 			sgsmix_set_speaker_vol(sc->sc_sgsmix, 0, 0);
   1136 			sgsmix_set_headphone_vol(sc->sc_sgsmix,
   1137 			    sc->vol_l, sc->vol_r);
   1138 		}
   1139 		if (mask & OUTPUT_SPEAKER) {
   1140 			/* mute headphones */
   1141 			sgsmix_set_speaker_vol(sc->sc_sgsmix,
   1142 			    sc->vol_l, sc->vol_r);
   1143 			sgsmix_set_headphone_vol(sc->sc_sgsmix, 0, 0);
   1144 		}
   1145 	} else {
   1146 #endif
   1147 	sc->sc_codecctl1 |= AWACS_MUTE_SPEAKER | AWACS_MUTE_HEADPHONE;
   1148 	if ((sc->vol_l > 0) || (sc->vol_r > 0)) {
   1149 		if (mask & OUTPUT_SPEAKER)
   1150 			sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
   1151 		if (mask & OUTPUT_HEADPHONES)
   1152 			sc->sc_codecctl1 &= ~AWACS_MUTE_HEADPHONE;
   1153 	}
   1154 	awacs_write_codec(sc, sc->sc_codecctl1);
   1155 #if NSGSMIX > 0
   1156 	}
   1157 #endif
   1158 	sc->sc_output_mask = mask;
   1159 }
   1160 
   1161 static void
   1162 awacs_set_speaker_volume(struct awacs_softc *sc, int left, int right)
   1163 {
   1164 
   1165 #if NSGSMIX > 0
   1166 	if (sc->sc_sgsmix) {
   1167 		if (sc->sc_output_mask & OUTPUT_SPEAKER)
   1168 			sgsmix_set_speaker_vol(sc->sc_sgsmix, left, right);
   1169 	} else
   1170 #endif
   1171 	{
   1172 		int lval;
   1173 		int rval;
   1174 		uint32_t codecctl = sc->sc_codecctl1;
   1175 
   1176 		lval = 15 - ((left  & 0xf0) >> 4);
   1177 		rval = 15 - ((right & 0xf0) >> 4);
   1178 		DPRINTF("speaker_volume %d %d\n", lval, rval);
   1179 
   1180 		sc->sc_codecctl4 &= ~0x3cf;
   1181 		sc->sc_codecctl4 |= (lval << 6) | rval;
   1182 		awacs_write_codec(sc, sc->sc_codecctl4);
   1183 		if ((left == 0) && (right == 0)) {
   1184 			/*
   1185 			 * max. attenuation doesn't mean silence so we need to
   1186 			 * mute the output channel here
   1187 			 */
   1188 			codecctl |= AWACS_MUTE_SPEAKER;
   1189 		} else if (sc->sc_output_mask & OUTPUT_SPEAKER) {
   1190 			codecctl &= ~AWACS_MUTE_SPEAKER;
   1191 		}
   1192 
   1193 		if (codecctl != sc->sc_codecctl1) {
   1194 
   1195 			sc->sc_codecctl1 = codecctl;
   1196 			awacs_write_codec(sc, sc->sc_codecctl1);
   1197 		}
   1198 	}
   1199 }
   1200 
   1201 static void
   1202 awacs_set_ext_volume(struct awacs_softc *sc, int left, int right)
   1203 {
   1204 
   1205 #if NSGSMIX > 0
   1206 	if (sc->sc_sgsmix) {
   1207 		if (sc->sc_output_mask & OUTPUT_HEADPHONES)
   1208 			sgsmix_set_headphone_vol(sc->sc_sgsmix, left, right);
   1209 	} else
   1210 #endif
   1211 	{
   1212 		int lval;
   1213 		int rval;
   1214 		uint32_t codecctl = sc->sc_codecctl1;
   1215 
   1216 		lval = 15 - ((left  & 0xf0) >> 4);
   1217 		rval = 15 - ((right & 0xf0) >> 4);
   1218 		DPRINTF("ext_volume %d %d\n", lval, rval);
   1219 
   1220 		sc->sc_codecctl2 &= ~0x3cf;
   1221 		sc->sc_codecctl2 |= (lval << 6) | rval;
   1222 		awacs_write_codec(sc, sc->sc_codecctl2);
   1223 
   1224 		if ((left == 0) && (right == 0)) {
   1225 			/*
   1226 			 * max. attenuation doesn't mean silence so we need to
   1227 			 * mute the output channel here
   1228 			 */
   1229 			codecctl |= AWACS_MUTE_HEADPHONE;
   1230 		} else if (sc->sc_output_mask & OUTPUT_HEADPHONES) {
   1231 
   1232 			codecctl &= ~AWACS_MUTE_HEADPHONE;
   1233 		}
   1234 
   1235 		if (codecctl != sc->sc_codecctl1) {
   1236 
   1237 			sc->sc_codecctl1 = codecctl;
   1238 			awacs_write_codec(sc, sc->sc_codecctl1);
   1239 		}
   1240 	}
   1241 }
   1242 
   1243 static void
   1244 awacs_set_volume(struct awacs_softc *sc, int left, int right)
   1245 {
   1246 
   1247 	awacs_set_ext_volume(sc, left, right);
   1248 	awacs_set_speaker_volume(sc, left, right);
   1249 
   1250 	sc->vol_l = left;
   1251 	sc->vol_r = right;
   1252 }
   1253 
   1254 #if NSGSMIX > 0
   1255 static void
   1256 awacs_set_bass(struct awacs_softc *sc, int bass)
   1257 {
   1258 
   1259 	if (sc->sc_bass == bass)
   1260 		return;
   1261 
   1262 	sc->sc_bass = bass;
   1263 	if (sc->sc_sgsmix)
   1264 		sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass, sc->sc_treble);
   1265 }
   1266 
   1267 static void
   1268 awacs_set_treble(struct awacs_softc *sc, int treble)
   1269 {
   1270 
   1271 	if (sc->sc_treble == treble)
   1272 		return;
   1273 
   1274 	sc->sc_treble = treble;
   1275 	if (sc->sc_sgsmix)
   1276 		sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass, sc->sc_treble);
   1277 }
   1278 #endif
   1279 
   1280 void
   1281 awacs_set_loopthrough_volume(struct awacs_softc *sc, int left, int right)
   1282 {
   1283 	int lval;
   1284 	int rval;
   1285 
   1286 	lval = 15 - ((left  & 0xff) >> 4);
   1287 	rval = 15 - ((right & 0xff) >> 4);
   1288 	DPRINTF("loopthrough_volume %d %d\n", lval, rval);
   1289 
   1290 	sc->sc_codecctl5 &= ~0x3cf;
   1291 	sc->sc_codecctl5 |= (lval << 6) | rval;
   1292 	awacs_write_codec(sc, sc->sc_codecctl5);
   1293 }
   1294 
   1295 int
   1296 awacs_set_rate(struct awacs_softc *sc, const audio_params_t *p)
   1297 {
   1298 	int c;
   1299 
   1300 	switch (p->sample_rate) {
   1301 	case 44100:
   1302 		c = AWACS_RATE_44100;
   1303 		break;
   1304 	case 29400:
   1305 		c = AWACS_RATE_29400;
   1306 		break;
   1307 	case 22050:
   1308 		c = AWACS_RATE_22050;
   1309 		break;
   1310 	case 17640:
   1311 		c = AWACS_RATE_17640;
   1312 		break;
   1313 	case 14700:
   1314 		c = AWACS_RATE_14700;
   1315 		break;
   1316 	case 11025:
   1317 		c = AWACS_RATE_11025;
   1318 		break;
   1319 	case 8820:
   1320 		c = AWACS_RATE_8820;
   1321 		break;
   1322 	case 7350:
   1323 		c = AWACS_RATE_7350;
   1324 		break;
   1325 	default:
   1326 		return -1;
   1327 	}
   1328 
   1329 	sc->sc_soundctl &= ~AWACS_RATE_MASK;
   1330 	sc->sc_soundctl |= c;
   1331 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
   1332 
   1333 	return 0;
   1334 }
   1335 
   1336 static int
   1337 awacs_check_headphones(struct awacs_softc *sc)
   1338 {
   1339 	uint32_t reg;
   1340 	reg = awacs_read_reg(sc, AWACS_CODEC_STATUS);
   1341 	DPRINTF("%s: codec status reg %08x\n", sc->sc_dev.dv_xname, reg);
   1342 	return ((reg & sc->sc_headphones_mask) == sc->sc_headphones_in);
   1343 }
   1344 
   1345 static int
   1346 awacs_status_intr(void *cookie)
   1347 {
   1348 	struct awacs_softc *sc = cookie;
   1349 	int mask;
   1350 
   1351 	mask = awacs_check_headphones(sc) ? OUTPUT_HEADPHONES : OUTPUT_SPEAKER;
   1352 	if (mask != sc->sc_output_mask) {
   1353 
   1354 		sc->sc_output_wanted = mask;
   1355 		wakeup(&sc->sc_event);
   1356 	}
   1357 	/* clear the interrupt */
   1358 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl | AWACS_PORTCHG);
   1359 	return 1;
   1360 }
   1361 
   1362 static void
   1363 awacs_thread(void *cookie)
   1364 {
   1365 	struct awacs_softc *sc = cookie;
   1366 
   1367 	while (1) {
   1368 		tsleep(&sc->sc_event, PWAIT, "awacs_wait", hz);
   1369 		if (sc->sc_output_wanted == sc->sc_output_mask)
   1370 			continue;
   1371 
   1372 		awacs_select_output(sc, sc->sc_output_wanted);
   1373 		DPRINTF("%s: switching to %s\n", sc->sc_dev.dv_xname,
   1374 		    (sc->sc_output_wanted & OUTPUT_SPEAKER) ?
   1375 		    "speaker" : "headphones");
   1376 	}
   1377 }
   1378