Home | History | Annotate | Line # | Download | only in dev
awacs.c revision 1.29.4.1
      1 /*	$NetBSD: awacs.c,v 1.29.4.1 2007/08/15 13:47:29 skrll 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.29.4.1 2007/08/15 13:47:29 skrll 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 int
    282 awacs_match(struct device *parent, struct cfdata *match, void *aux)
    283 {
    284 	struct confargs *ca;
    285 
    286 	ca = aux;
    287 	if (strcmp(ca->ca_name, "i2s") == 0)
    288 		return 1;
    289 
    290 	if (strcmp(ca->ca_name, "awacs") != 0 &&
    291 	    strcmp(ca->ca_name, "davbus") != 0)
    292 		return 0;
    293 
    294 	if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
    295 		return 0;
    296 
    297 	return 1;
    298 }
    299 
    300 static void
    301 awacs_attach(struct device *parent, struct device *self, void *aux)
    302 {
    303 	struct awacs_softc *sc;
    304 	struct confargs *ca;
    305 	int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
    306 	int len = -1, perch;
    307 	int root_node;
    308 	char compat[256];
    309 
    310 	sc = (struct awacs_softc *)self;
    311 	ca = aux;
    312 
    313 	sc->sc_reg = mapiodev(ca->ca_baseaddr + ca->ca_reg[0], ca->ca_reg[1]);
    314 
    315 	/* out */
    316 	sc->sc_odma = mapiodev(ca->ca_baseaddr + ca->ca_reg[2], ca->ca_reg[3]);
    317 	sc->sc_odmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
    318 	/* in */
    319 	sc->sc_idma = mapiodev(ca->ca_baseaddr + ca->ca_reg[4], ca->ca_reg[5]);
    320 	sc->sc_idmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
    321 
    322 	if (strcmp(ca->ca_name, "i2s") == 0) {
    323 		int node, intr[6];
    324 
    325 		node = OF_child(ca->ca_node);
    326 		if (node == 0) {
    327 			printf("no i2s-a child\n");
    328 			return;
    329 		}
    330 		if (OF_getprop(node, "interrupts", intr, sizeof(intr)) == -1) {
    331 			printf("no interrupt property\n");
    332 			return;
    333 		}
    334 
    335 		cirq = intr[0];
    336 		oirq = intr[2];
    337 		iirq = intr[4];
    338 		cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
    339 		oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
    340 		iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
    341 	} else if (ca->ca_nintr == 24) {
    342 		cirq = ca->ca_intr[0];
    343 		oirq = ca->ca_intr[2];
    344 		iirq = ca->ca_intr[4];
    345 		cirq_type = ca->ca_intr[1] ? IST_LEVEL : IST_EDGE;
    346 		oirq_type = ca->ca_intr[3] ? IST_LEVEL : IST_EDGE;
    347 		iirq_type = ca->ca_intr[5] ? IST_LEVEL : IST_EDGE;
    348 	} else {
    349 		cirq = ca->ca_intr[0];
    350 		oirq = ca->ca_intr[1];
    351 		iirq = ca->ca_intr[2];
    352 		cirq_type = oirq_type = iirq_type = IST_LEVEL;
    353 	}
    354 
    355 	intr_establish(cirq, cirq_type, IPL_BIO, awacs_status_intr, sc);
    356 	intr_establish(oirq, oirq_type, IPL_AUDIO, awacs_intr, sc);
    357 	intr_establish(iirq, iirq_type, IPL_AUDIO, awacs_intr, sc);
    358 
    359 	/* check if the chip is a screamer */
    360 	sc->sc_screamer = (of_compatible(ca->ca_node, screamer) != -1);
    361 	if (!sc->sc_screamer) {
    362 		/* look for 'sound' child node */
    363 		int sound_node;
    364 
    365 		sound_node = OF_child(ca->ca_node);
    366 		while ((sound_node != 0) && (!sc->sc_screamer)) {
    367 
    368 			sc->sc_screamer =
    369 			    (of_compatible(sound_node, screamer) != -1);
    370 			sound_node = OF_peer(sound_node);
    371 		}
    372 	}
    373 
    374 	if (sc->sc_screamer) {
    375 		printf(" Screamer");
    376 	}
    377 
    378 	printf(": irq %d,%d,%d\n", cirq, oirq, iirq);
    379 
    380 	sc->vol_l = 0;
    381 	sc->vol_r = 0;
    382 
    383 	memcpy(&sc->sc_formats, awacs_formats, sizeof(awacs_formats));
    384 
    385 	/* XXX Uni-North based models don't have byteswap capability. */
    386 	if (OF_finddevice("/uni-n") == -1) {
    387 
    388 		sc->sc_flags |= AWACS_CAP_BSWAP;
    389 	} else {
    390 
    391 		AUFMT_INVALIDATE(&sc->sc_formats[AWACS_FORMATS_LE]);
    392 	}
    393 
    394 	sc->sc_soundctl = AWACS_INPUT_SUBFRAME0 | AWACS_OUTPUT_SUBFRAME0 |
    395 		AWACS_RATE_44100 | AWACS_INTR_PORTCHG;
    396 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
    397 
    398 	sc->sc_codecctl0 = AWACS_CODEC_ADDR0 | AWACS_CODEC_EMSEL0;
    399 	sc->sc_codecctl1 = AWACS_CODEC_ADDR1 | AWACS_CODEC_EMSEL0;
    400 	sc->sc_codecctl2 = AWACS_CODEC_ADDR2 | AWACS_CODEC_EMSEL0;
    401 	sc->sc_codecctl4 = AWACS_CODEC_ADDR4 | AWACS_CODEC_EMSEL0;
    402 	sc->sc_codecctl5 = AWACS_CODEC_ADDR5 | AWACS_CODEC_EMSEL0;
    403 	sc->sc_codecctl6 = AWACS_CODEC_ADDR6 | AWACS_CODEC_EMSEL0;
    404 	sc->sc_codecctl7 = AWACS_CODEC_ADDR7 | AWACS_CODEC_EMSEL0;
    405 
    406 	sc->sc_codecctl0 |= AWACS_INPUT_CD | AWACS_DEFAULT_CD_GAIN;
    407 	awacs_write_codec(sc, sc->sc_codecctl0);
    408 
    409 	/* Set loopthrough for external mixer on beige G3 */
    410 	sc->sc_codecctl1 |= (AWACS_LOOP_THROUGH | AWACS_PARALLEL_OUTPUT);
    411 
    412         printf("%s: ", sc->sc_dev.dv_xname);
    413 
    414 	/*
    415 	 * all(?) awacs have GPIOs to detect if there's something plugged into
    416 	 * the headphone jack. The other GPIOs are either used for other jacks
    417 	 * ( the PB3400c's microphone jack for instance ) or unused.
    418 	 * The problem is that there are at least three different ways how
    419 	 * those GPIOs are wired to the actual jacks.
    420 	 * For now we bother only with headphone detection
    421 	 */
    422 	perch = OF_finddevice("/perch");
    423 	root_node = OF_finddevice("/");
    424 	if (of_compatible(root_node, detect_reversed) != -1) {
    425 
    426 		/* 0x02 is for the microphone jack, high active */
    427 		/*
    428 		 * for some reason the gpio for the headphones jack is low
    429 		 * active on the PB3400 and similar machines
    430 		 */
    431 		sc->sc_headphones_mask = 0x8;
    432 		sc->sc_headphones_in = 0x0;
    433 	} else if (perch != -1) {
    434 		/*
    435 		 * this is for the beige G3's 'personality card' which uses
    436 		 * yet another wiring of the headphone detect GPIOs
    437 		 */
    438 		sc->sc_headphones_mask = 0x04;
    439 		sc->sc_headphones_in = 0x04;
    440 	} else {
    441 		/* while on most machines it's high active as well */
    442 		sc->sc_headphones_mask = 0x8;
    443 		sc->sc_headphones_in = 0x8;
    444 	}
    445 	if (awacs_check_headphones(sc)) {
    446 
    447                 /* default output to headphones */
    448                 printf("headphones\n");
    449                 sc->sc_output_mask = OUTPUT_HEADPHONES;
    450         } else {
    451 
    452                 /* default output to speakers */
    453                 printf("speaker\n");
    454                 sc->sc_output_mask = OUTPUT_SPEAKER;
    455         }
    456 	sc->sc_output_wanted = sc->sc_output_mask;
    457 	awacs_select_output(sc, sc->sc_output_mask);
    458 
    459 	delay(100);
    460 	if (sc->sc_screamer) {
    461 		awacs_write_codec(sc, sc->sc_codecctl6);
    462 		awacs_write_codec(sc, sc->sc_codecctl5);
    463 		delay(2);
    464 		awacs_write_codec(sc, sc->sc_codecctl1);
    465 		awacs_write_codec(sc, sc->sc_codecctl7);
    466 	}
    467 
    468 	/* default input from CD */
    469 	sc->sc_record_source = 1 << 0;
    470 	sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    471 	sc->sc_codecctl0 |= AWACS_INPUT_CD;
    472 	awacs_write_codec(sc, sc->sc_codecctl0);
    473 
    474 	/* Enable interrupts and looping mode. */
    475 	/* XXX ... */
    476 
    477 	sc->sc_codecctl1 |= (AWACS_LOOP_THROUGH | AWACS_PARALLEL_OUTPUT);
    478 	awacs_write_codec(sc, sc->sc_codecctl1);
    479 
    480 #if NSGSMIX > 0
    481 	sc->sc_sgsmix = NULL;
    482 #endif
    483 	sc->sc_have_perch = 0;
    484 	if (perch != -1) {
    485 
    486 		len = OF_getprop(perch, "compatible", compat, 255);
    487 		if (len > 0) {
    488 			printf("%s: found '%s' personality card\n",
    489 			    sc->sc_dev.dv_xname, compat);
    490 			sc->sc_have_perch = 1;
    491 			config_finalize_register(&sc->sc_dev, awacs_setup_sgsmix);
    492 		}
    493 	}
    494 
    495 	/* Set initial volume[s] */
    496 	awacs_set_volume(sc, 144, 144);
    497 	awacs_set_loopthrough_volume(sc, 255, 255);
    498 
    499 	audio_attach_mi(&awacs_hw_if, sc, &sc->sc_dev);
    500 
    501 	if (kthread_create(PRI_NONE, 0, NULL, awacs_thread, sc,
    502 	    &sc->sc_thread, "%s", "awacs") != 0) {
    503 		printf("awacs: unable to create event kthread");
    504 	}
    505 }
    506 
    507 static int
    508 awacs_setup_sgsmix(struct device *cookie)
    509 {
    510 	struct awacs_softc *sc = (struct awacs_softc *)cookie;
    511 #if NSGSMIX > 0
    512 	struct device *dv;
    513 #endif
    514 
    515 	if (!sc->sc_have_perch)
    516 		return 0;
    517 #if NSGSMIX > 0
    518 	/* look for sgsmix */
    519 	for (dv = alldevs.tqh_first; dv; dv=dv->dv_list.tqe_next) {
    520 		if (device_is_a(dv, "sgsmix")) {
    521 			sc->sc_sgsmix = dv;
    522 			break;
    523 		}
    524 	}
    525 	if (sc->sc_sgsmix == NULL)
    526 		return 0;
    527 
    528 	printf("%s: using %s\n", sc->sc_dev.dv_xname,
    529 	    sc->sc_sgsmix->dv_xname);
    530 
    531 	awacs_select_output(sc, sc->sc_output_mask);
    532 	awacs_set_volume(sc, sc->vol_l, sc->vol_r);
    533 	awacs_set_bass(sc, 128);
    534 	awacs_set_treble(sc, 128);
    535 	wakeup(&sc->sc_event);
    536 #endif
    537 	return 0;
    538 }
    539 
    540 
    541 static inline u_int
    542 awacs_read_reg(struct awacs_softc *sc, int reg)
    543 {
    544 	char *addr;
    545 
    546 	addr = sc->sc_reg;
    547 	return in32rb(addr + reg);
    548 }
    549 
    550 static inline void
    551 awacs_write_reg(struct awacs_softc *sc, int reg, int val)
    552 {
    553 	char *addr;
    554 
    555 	addr = sc->sc_reg;
    556 	out32rb(addr + reg, val);
    557 }
    558 
    559 static void
    560 awacs_write_codec(struct awacs_softc *sc, int value)
    561 {
    562 
    563 	do {
    564 		delay(100);
    565 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    566 
    567 	awacs_write_reg(sc, AWACS_CODEC_CTRL, value);
    568 
    569 	do {
    570 		delay(100);
    571 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    572 }
    573 
    574 static int
    575 awacs_intr(void *v)
    576 {
    577 	struct awacs_softc *sc;
    578 	struct dbdma_command *cmd;
    579 	int count;
    580 	int status;
    581 
    582 	sc = v;
    583 	cmd = sc->sc_odmacmd;
    584 	count = sc->sc_opages;
    585 	/* Fill used buffer(s). */
    586 	while (count-- > 0) {
    587 		/* if DBDMA_INT_ALWAYS */
    588 		if (in16rb(&cmd->d_command) & 0x30) {	/* XXX */
    589 			status = in16rb(&cmd->d_status);
    590 			cmd->d_status = 0;
    591 			if (status)	/* status == 0x8400 */
    592 				if (sc->sc_ointr)
    593 					(*sc->sc_ointr)(sc->sc_oarg);
    594 		}
    595 		cmd++;
    596 	}
    597 
    598 	return 1;
    599 }
    600 
    601 /*
    602  * Close function is called at splaudio().
    603  */
    604 static void
    605 awacs_close(void *h)
    606 {
    607 	struct awacs_softc *sc;
    608 
    609 	sc = h;
    610 	awacs_halt_output(sc);
    611 	awacs_halt_input(sc);
    612 
    613 	sc->sc_ointr = 0;
    614 	sc->sc_iintr = 0;
    615 }
    616 
    617 static int
    618 awacs_query_encoding(void *h, struct audio_encoding *ae)
    619 {
    620 	struct awacs_softc *sc;
    621 
    622 	sc = h;
    623 	ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
    624 
    625 	switch (ae->index) {
    626 	case 0:
    627 		strcpy(ae->name, AudioEslinear);
    628 		ae->encoding = AUDIO_ENCODING_SLINEAR;
    629 		ae->precision = 16;
    630 		ae->flags = 0;
    631 		return 0;
    632 	case 1:
    633 		strcpy(ae->name, AudioEslinear_be);
    634 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
    635 		ae->precision = 16;
    636 		ae->flags = 0;
    637 		return 0;
    638 	case 2:
    639 		strcpy(ae->name, AudioEslinear_le);
    640 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
    641 		ae->precision = 16;
    642 		if (sc->sc_flags & AWACS_CAP_BSWAP)
    643 			ae->flags = 0;
    644 		return 0;
    645 	case 3:
    646 		strcpy(ae->name, AudioEulinear_be);
    647 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
    648 		ae->precision = 16;
    649 		return 0;
    650 	case 4:
    651 		strcpy(ae->name, AudioEulinear_le);
    652 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
    653 		ae->precision = 16;
    654 		return 0;
    655 	case 5:
    656 		strcpy(ae->name, AudioEmulaw);
    657 		ae->encoding = AUDIO_ENCODING_ULAW;
    658 		ae->precision = 8;
    659 		return 0;
    660 	case 6:
    661 		strcpy(ae->name, AudioEalaw);
    662 		ae->encoding = AUDIO_ENCODING_ALAW;
    663 		ae->precision = 8;
    664 		return 0;
    665 	default:
    666 		return EINVAL;
    667 	}
    668 }
    669 
    670 static int
    671 awacs_set_params(void *h, int setmode, int usemode,
    672 		 audio_params_t *play, audio_params_t *rec,
    673 		 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    674 {
    675 	struct awacs_softc *sc;
    676 	audio_params_t *p;
    677 	stream_filter_list_t *fil;
    678 	int mode, i;
    679 
    680 	sc = h;
    681 	p = NULL;
    682 	/*
    683 	 * This device only has one clock, so make the sample rates match.
    684 	 */
    685 	if (play->sample_rate != rec->sample_rate &&
    686 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    687 		if (setmode == AUMODE_PLAY) {
    688 			rec->sample_rate = play->sample_rate;
    689 			setmode |= AUMODE_RECORD;
    690 		} else if (setmode == AUMODE_RECORD) {
    691 			play->sample_rate = rec->sample_rate;
    692 			setmode |= AUMODE_PLAY;
    693 		} else
    694 			return EINVAL;
    695 	}
    696 
    697 	for (mode = AUMODE_RECORD; mode != -1;
    698 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    699 		if ((setmode & mode) == 0)
    700 			continue;
    701 
    702 		p = mode == AUMODE_PLAY ? play : rec;
    703 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    704 		switch (p->sample_rate) {
    705 		case 48000:	/* aurateconv */
    706 		case 44100:
    707 		case 29400:
    708 		case 22050:
    709 		case 17640:
    710 		case 14700:
    711 		case 11025:
    712 		case 8820:
    713 		case 8000:	/* aurateconv */
    714 		case 7350:
    715 			break;
    716 		default:
    717 			return EINVAL;
    718 		}
    719 		awacs_write_reg(sc, AWACS_BYTE_SWAP, 0);
    720 		i = auconv_set_converter(sc->sc_formats, AWACS_NFORMATS,
    721 					 mode, p, true, fil);
    722 		if (i < 0)
    723 			return EINVAL;
    724 		if (i == AWACS_FORMATS_LE)
    725 			awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
    726 		if (fil->req_size > 0)
    727 			p = &fil->filters[0].param;
    728 		if (awacs_set_rate(sc, p))
    729 			return EINVAL;
    730 	}
    731 	return 0;
    732 }
    733 
    734 static int
    735 awacs_round_blocksize(void *h, int size, int mode, const audio_params_t *param)
    736 {
    737 
    738 	if (size < PAGE_SIZE)
    739 		size = PAGE_SIZE;
    740 	return size & ~PGOFSET;
    741 }
    742 
    743 static int
    744 awacs_halt_output(void *h)
    745 {
    746 	struct awacs_softc *sc;
    747 
    748 	sc = h;
    749 	dbdma_stop(sc->sc_odma);
    750 	dbdma_reset(sc->sc_odma);
    751 	return 0;
    752 }
    753 
    754 static int
    755 awacs_halt_input(void *h)
    756 {
    757 	struct awacs_softc *sc;
    758 
    759 	sc = h;
    760 	dbdma_stop(sc->sc_idma);
    761 	dbdma_reset(sc->sc_idma);
    762 	return 0;
    763 }
    764 
    765 static int
    766 awacs_getdev(void *h, struct audio_device *retp)
    767 {
    768 
    769 	*retp = awacs_device;
    770 	return 0;
    771 }
    772 
    773 enum {
    774 	AWACS_MONITOR_CLASS,
    775 	AWACS_OUTPUT_CLASS,
    776 	AWACS_RECORD_CLASS,
    777 	AWACS_OUTPUT_SELECT,
    778 	AWACS_VOL_MASTER,
    779 	AWACS_INPUT_SELECT,
    780 	AWACS_VOL_INPUT,
    781 	AWACS_BASS,
    782 	AWACS_TREBLE,
    783 	AWACS_ENUM_LAST
    784 };
    785 
    786 static int
    787 awacs_set_port(void *h, mixer_ctrl_t *mc)
    788 {
    789 	struct awacs_softc *sc;
    790 	int l, r;
    791 
    792 	DPRINTF("awacs_set_port dev = %d, type = %d\n", mc->dev, mc->type);
    793 	sc = h;
    794 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    795 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    796 
    797 	switch (mc->dev) {
    798 	case AWACS_OUTPUT_SELECT:
    799 		/* No change necessary? */
    800 		if (mc->un.mask == sc->sc_output_mask)
    801 			return 0;
    802 		awacs_select_output(sc, mc->un.mask);
    803 		return 0;
    804 
    805 	case AWACS_VOL_MASTER:
    806 		awacs_set_volume(sc, l, r);
    807 		return 0;
    808 
    809 	case AWACS_INPUT_SELECT:
    810 		/* no change necessary? */
    811 		if (mc->un.mask == sc->sc_record_source)
    812 			return 0;
    813 		switch (mc->un.mask) {
    814 		case 1 << 0: /* CD */
    815 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    816 			sc->sc_codecctl0 |= AWACS_INPUT_CD;
    817 			awacs_write_codec(sc, sc->sc_codecctl0);
    818 			break;
    819 		case 1 << 1: /* microphone */
    820 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    821 			sc->sc_codecctl0 |= AWACS_INPUT_MICROPHONE;
    822 			awacs_write_codec(sc, sc->sc_codecctl0);
    823 			break;
    824 		case 1 << 2: /* line in */
    825 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    826 			sc->sc_codecctl0 |= AWACS_INPUT_LINE;
    827 			awacs_write_codec(sc, sc->sc_codecctl0);
    828 			break;
    829 		default: /* invalid argument */
    830 			return EINVAL;
    831 		}
    832 		sc->sc_record_source = mc->un.mask;
    833 		return 0;
    834 
    835 	case AWACS_VOL_INPUT:
    836 		sc->sc_codecctl0 &= ~0xff;
    837 		sc->sc_codecctl0 |= (l & 0xf0) | (r >> 4);
    838 		awacs_write_codec(sc, sc->sc_codecctl0);
    839 		return 0;
    840 
    841 #if NSGSMIX > 0
    842 	case AWACS_BASS:
    843 		awacs_set_bass(sc, l);
    844 		return 0;
    845 
    846 	case AWACS_TREBLE:
    847 		awacs_set_treble(sc, l);
    848 		return 0;
    849 #endif
    850 	}
    851 
    852 	return ENXIO;
    853 }
    854 
    855 static int
    856 awacs_get_port(void *h, mixer_ctrl_t *mc)
    857 {
    858 	struct awacs_softc *sc;
    859 	int l, r, vol;
    860 
    861 	sc = h;
    862 	switch (mc->dev) {
    863 	case AWACS_OUTPUT_SELECT:
    864 		mc->un.mask = sc->sc_output_mask;
    865 		return 0;
    866 
    867 	case AWACS_VOL_MASTER:
    868 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->vol_l;
    869 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->vol_r;
    870 		return 0;
    871 
    872 	case AWACS_INPUT_SELECT:
    873 		mc->un.mask = sc->sc_record_source;
    874 		return 0;
    875 
    876 	case AWACS_VOL_INPUT:
    877 		vol = sc->sc_codecctl0 & 0xff;
    878 		l = (vol & 0xf0);
    879 		r = (vol & 0x0f) << 4;
    880 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    881 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    882 		return 0;
    883 #if NSGSMIX > 0
    884 	case AWACS_BASS:
    885 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
    886 		return 0;
    887 
    888 	case AWACS_TREBLE:
    889 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
    890 		return 0;
    891 #endif
    892 
    893 	default:
    894 		return ENXIO;
    895 	}
    896 
    897 	return 0;
    898 }
    899 
    900 static int
    901 awacs_query_devinfo(void *h, mixer_devinfo_t *dip)
    902 {
    903 #if NSGSMIX > 0
    904 	struct awacs_softc *sc = h;
    905 #endif
    906 
    907 	switch (dip->index) {
    908 
    909 	case AWACS_OUTPUT_SELECT:
    910 		dip->mixer_class = AWACS_MONITOR_CLASS;
    911 		strcpy(dip->label.name, AudioNoutput);
    912 		dip->type = AUDIO_MIXER_SET;
    913 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    914 		dip->un.s.num_mem = 2;
    915 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
    916 		dip->un.s.member[0].mask = 1 << 0;
    917 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
    918 		dip->un.s.member[1].mask = 1 << 1;
    919 		return 0;
    920 
    921 	case AWACS_VOL_MASTER:
    922 		dip->mixer_class = AWACS_MONITOR_CLASS;
    923 		strcpy(dip->label.name, AudioNmaster);
    924 		dip->type = AUDIO_MIXER_VALUE;
    925 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    926 		dip->un.v.num_channels = 2;
    927 		strcpy(dip->un.v.units.name, AudioNvolume);
    928 		return 0;
    929 
    930 #if NSGSMIX > 0
    931 	case AWACS_BASS:
    932 		if (sc->sc_sgsmix == NULL)
    933 			return ENXIO;
    934 		dip->mixer_class = AWACS_MONITOR_CLASS;
    935 		strcpy(dip->label.name, AudioNbass);
    936 		dip->type = AUDIO_MIXER_VALUE;
    937 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    938 		dip->un.v.num_channels = 1;
    939 		strcpy(dip->un.v.units.name, AudioNbass);
    940 		return 0;
    941 
    942 	case AWACS_TREBLE:
    943 		if (sc->sc_sgsmix == NULL)
    944 			return ENXIO;
    945 		dip->mixer_class = AWACS_MONITOR_CLASS;
    946 		strcpy(dip->label.name, AudioNtreble);
    947 		dip->type = AUDIO_MIXER_VALUE;
    948 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    949 		dip->un.v.num_channels = 1;
    950 		strcpy(dip->un.v.units.name, AudioNtreble);
    951 		return 0;
    952 #endif
    953 
    954 	case AWACS_INPUT_SELECT:
    955 		dip->mixer_class = AWACS_RECORD_CLASS;
    956 		strcpy(dip->label.name, AudioNsource);
    957 		dip->type = AUDIO_MIXER_SET;
    958 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    959 		dip->un.s.num_mem = 3;
    960 		strcpy(dip->un.s.member[0].label.name, AudioNcd);
    961 		dip->un.s.member[0].mask = 1 << 0;
    962 		strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
    963 		dip->un.s.member[1].mask = 1 << 1;
    964 		strcpy(dip->un.s.member[2].label.name, AudioNline);
    965 		dip->un.s.member[2].mask = 1 << 2;
    966 		return 0;
    967 
    968 	case AWACS_VOL_INPUT:
    969 		dip->mixer_class = AWACS_RECORD_CLASS;
    970 		strcpy(dip->label.name, AudioNrecord);
    971 		dip->type = AUDIO_MIXER_VALUE;
    972 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    973 		dip->un.v.num_channels = 2;
    974 		strcpy(dip->un.v.units.name, AudioNvolume);
    975 		return 0;
    976 
    977 	case AWACS_MONITOR_CLASS:
    978 		dip->mixer_class = AWACS_MONITOR_CLASS;
    979 		strcpy(dip->label.name, AudioCmonitor);
    980 		dip->type = AUDIO_MIXER_CLASS;
    981 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    982 		return 0;
    983 
    984 	case AWACS_OUTPUT_CLASS:
    985 		dip->mixer_class = AWACS_OUTPUT_CLASS;
    986 		strcpy(dip->label.name, AudioCoutputs);
    987 		dip->type = AUDIO_MIXER_CLASS;
    988 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    989 		return 0;
    990 
    991 	case AWACS_RECORD_CLASS:
    992 		dip->mixer_class = AWACS_RECORD_CLASS;
    993 		strcpy(dip->label.name, AudioCrecord);
    994 		dip->type = AUDIO_MIXER_CLASS;
    995 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    996 		return 0;
    997 	}
    998 
    999 	return ENXIO;
   1000 }
   1001 
   1002 static size_t
   1003 awacs_round_buffersize(void *h, int dir, size_t size)
   1004 {
   1005 
   1006 	if (size > 65536)
   1007 		size = 65536;
   1008 	return size;
   1009 }
   1010 
   1011 static paddr_t
   1012 awacs_mappage(void *h, void *mem, off_t off, int prot)
   1013 {
   1014 
   1015 	if (off < 0)
   1016 		return -1;
   1017 	return -1;	/* XXX */
   1018 }
   1019 
   1020 static int
   1021 awacs_get_props(void *h)
   1022 {
   1023 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
   1024 }
   1025 
   1026 static int
   1027 awacs_trigger_output(void *h, void *start, void *end, int bsize,
   1028 		     void (*intr)(void *), void *arg,
   1029 		     const audio_params_t *param)
   1030 {
   1031 	struct awacs_softc *sc;
   1032 	struct dbdma_command *cmd;
   1033 	vaddr_t va;
   1034 	int i, len, intmode;
   1035 
   1036 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
   1037 	sc = h;
   1038 	cmd = sc->sc_odmacmd;
   1039 	sc->sc_ointr = intr;
   1040 	sc->sc_oarg = arg;
   1041 	sc->sc_opages = ((char *)end - (char *)start) / PAGE_SIZE;
   1042 
   1043 #ifdef DIAGNOSTIC
   1044 	if (sc->sc_opages > 16)
   1045 		panic("awacs_trigger_output");
   1046 #endif
   1047 
   1048 	va = (vaddr_t)start;
   1049 	len = 0;
   1050 	for (i = sc->sc_opages; i > 0; i--) {
   1051 		len += PAGE_SIZE;
   1052 		if (len < bsize)
   1053 			intmode = DBDMA_INT_NEVER;
   1054 		else {
   1055 			len = 0;
   1056 			intmode = DBDMA_INT_ALWAYS;
   1057 		}
   1058 
   1059 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, PAGE_SIZE, vtophys(va),
   1060 			intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1061 		va += PAGE_SIZE;
   1062 		cmd++;
   1063 	}
   1064 
   1065 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
   1066 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
   1067 	dbdma_st32(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
   1068 
   1069 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
   1070 
   1071 	return 0;
   1072 }
   1073 
   1074 static int
   1075 awacs_trigger_input(void *h, void *start, void *end, int bsize,
   1076 		    void (*intr)(void *), void *arg,
   1077 		    const audio_params_t *param)
   1078 {
   1079 
   1080 	DPRINTF("awacs_trigger_input called\n");
   1081 	return 1;
   1082 }
   1083 
   1084 static void
   1085 awacs_select_output(struct awacs_softc *sc, int mask)
   1086 {
   1087 
   1088 #if NSGSMIX > 0
   1089 	if (sc->sc_sgsmix) {
   1090 		if (mask & OUTPUT_HEADPHONES) {
   1091 			/* mute speakers */
   1092 			sgsmix_set_speaker_vol(sc->sc_sgsmix, 0, 0);
   1093 			sgsmix_set_headphone_vol(sc->sc_sgsmix,
   1094 			    sc->vol_l, sc->vol_r);
   1095 		}
   1096 		if (mask & OUTPUT_SPEAKER) {
   1097 			/* mute headphones */
   1098 			sgsmix_set_speaker_vol(sc->sc_sgsmix,
   1099 			    sc->vol_l, sc->vol_r);
   1100 			sgsmix_set_headphone_vol(sc->sc_sgsmix, 0, 0);
   1101 		}
   1102 	} else {
   1103 #endif
   1104 	sc->sc_codecctl1 |= AWACS_MUTE_SPEAKER | AWACS_MUTE_HEADPHONE;
   1105 	if ((sc->vol_l > 0) || (sc->vol_r > 0)) {
   1106 		if (mask & OUTPUT_SPEAKER)
   1107 			sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
   1108 		if (mask & OUTPUT_HEADPHONES)
   1109 			sc->sc_codecctl1 &= ~AWACS_MUTE_HEADPHONE;
   1110 	}
   1111 	awacs_write_codec(sc, sc->sc_codecctl1);
   1112 #if NSGSMIX > 0
   1113 	}
   1114 #endif
   1115 	sc->sc_output_mask = mask;
   1116 }
   1117 
   1118 static void
   1119 awacs_set_speaker_volume(struct awacs_softc *sc, int left, int right)
   1120 {
   1121 
   1122 #if NSGSMIX > 0
   1123 	if (sc->sc_sgsmix) {
   1124 		if (sc->sc_output_mask & OUTPUT_SPEAKER)
   1125 			sgsmix_set_speaker_vol(sc->sc_sgsmix, left, right);
   1126 	} else
   1127 #endif
   1128 	{
   1129 		int lval;
   1130 		int rval;
   1131 		uint32_t codecctl = sc->sc_codecctl1;
   1132 
   1133 		lval = 15 - ((left  & 0xf0) >> 4);
   1134 		rval = 15 - ((right & 0xf0) >> 4);
   1135 		DPRINTF("speaker_volume %d %d\n", lval, rval);
   1136 
   1137 		sc->sc_codecctl4 &= ~0x3cf;
   1138 		sc->sc_codecctl4 |= (lval << 6) | rval;
   1139 		awacs_write_codec(sc, sc->sc_codecctl4);
   1140 		if ((left == 0) && (right == 0)) {
   1141 			/*
   1142 			 * max. attenuation doesn't mean silence so we need to
   1143 			 * mute the output channel here
   1144 			 */
   1145 			codecctl |= AWACS_MUTE_SPEAKER;
   1146 		} else if (sc->sc_output_mask & OUTPUT_SPEAKER) {
   1147 			codecctl &= ~AWACS_MUTE_SPEAKER;
   1148 		}
   1149 
   1150 		if (codecctl != sc->sc_codecctl1) {
   1151 
   1152 			sc->sc_codecctl1 = codecctl;
   1153 			awacs_write_codec(sc, sc->sc_codecctl1);
   1154 		}
   1155 	}
   1156 }
   1157 
   1158 static void
   1159 awacs_set_ext_volume(struct awacs_softc *sc, int left, int right)
   1160 {
   1161 
   1162 #if NSGSMIX > 0
   1163 	if (sc->sc_sgsmix) {
   1164 		if (sc->sc_output_mask & OUTPUT_HEADPHONES)
   1165 			sgsmix_set_headphone_vol(sc->sc_sgsmix, left, right);
   1166 	} else
   1167 #endif
   1168 	{
   1169 		int lval;
   1170 		int rval;
   1171 		uint32_t codecctl = sc->sc_codecctl1;
   1172 
   1173 		lval = 15 - ((left  & 0xf0) >> 4);
   1174 		rval = 15 - ((right & 0xf0) >> 4);
   1175 		DPRINTF("ext_volume %d %d\n", lval, rval);
   1176 
   1177 		sc->sc_codecctl2 &= ~0x3cf;
   1178 		sc->sc_codecctl2 |= (lval << 6) | rval;
   1179 		awacs_write_codec(sc, sc->sc_codecctl2);
   1180 
   1181 		if ((left == 0) && (right == 0)) {
   1182 			/*
   1183 			 * max. attenuation doesn't mean silence so we need to
   1184 			 * mute the output channel here
   1185 			 */
   1186 			codecctl |= AWACS_MUTE_HEADPHONE;
   1187 		} else if (sc->sc_output_mask & OUTPUT_HEADPHONES) {
   1188 
   1189 			codecctl &= ~AWACS_MUTE_HEADPHONE;
   1190 		}
   1191 
   1192 		if (codecctl != sc->sc_codecctl1) {
   1193 
   1194 			sc->sc_codecctl1 = codecctl;
   1195 			awacs_write_codec(sc, sc->sc_codecctl1);
   1196 		}
   1197 	}
   1198 }
   1199 
   1200 static void
   1201 awacs_set_volume(struct awacs_softc *sc, int left, int right)
   1202 {
   1203 
   1204 	awacs_set_ext_volume(sc, left, right);
   1205 	awacs_set_speaker_volume(sc, left, right);
   1206 
   1207 	sc->vol_l = left;
   1208 	sc->vol_r = right;
   1209 }
   1210 
   1211 #if NSGSMIX > 0
   1212 static void
   1213 awacs_set_bass(struct awacs_softc *sc, int bass)
   1214 {
   1215 
   1216 	if (sc->sc_bass == bass)
   1217 		return;
   1218 
   1219 	sc->sc_bass = bass;
   1220 	if (sc->sc_sgsmix)
   1221 		sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass, sc->sc_treble);
   1222 }
   1223 
   1224 static void
   1225 awacs_set_treble(struct awacs_softc *sc, int treble)
   1226 {
   1227 
   1228 	if (sc->sc_treble == treble)
   1229 		return;
   1230 
   1231 	sc->sc_treble = treble;
   1232 	if (sc->sc_sgsmix)
   1233 		sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass, sc->sc_treble);
   1234 }
   1235 #endif
   1236 
   1237 void
   1238 awacs_set_loopthrough_volume(struct awacs_softc *sc, int left, int right)
   1239 {
   1240 	int lval;
   1241 	int rval;
   1242 
   1243 	lval = 15 - ((left  & 0xff) >> 4);
   1244 	rval = 15 - ((right & 0xff) >> 4);
   1245 	DPRINTF("loopthrough_volume %d %d\n", lval, rval);
   1246 
   1247 	sc->sc_codecctl5 &= ~0x3cf;
   1248 	sc->sc_codecctl5 |= (lval << 6) | rval;
   1249 	awacs_write_codec(sc, sc->sc_codecctl5);
   1250 }
   1251 
   1252 int
   1253 awacs_set_rate(struct awacs_softc *sc, const audio_params_t *p)
   1254 {
   1255 	int c;
   1256 
   1257 	switch (p->sample_rate) {
   1258 	case 44100:
   1259 		c = AWACS_RATE_44100;
   1260 		break;
   1261 	case 29400:
   1262 		c = AWACS_RATE_29400;
   1263 		break;
   1264 	case 22050:
   1265 		c = AWACS_RATE_22050;
   1266 		break;
   1267 	case 17640:
   1268 		c = AWACS_RATE_17640;
   1269 		break;
   1270 	case 14700:
   1271 		c = AWACS_RATE_14700;
   1272 		break;
   1273 	case 11025:
   1274 		c = AWACS_RATE_11025;
   1275 		break;
   1276 	case 8820:
   1277 		c = AWACS_RATE_8820;
   1278 		break;
   1279 	case 7350:
   1280 		c = AWACS_RATE_7350;
   1281 		break;
   1282 	default:
   1283 		return -1;
   1284 	}
   1285 
   1286 	sc->sc_soundctl &= ~AWACS_RATE_MASK;
   1287 	sc->sc_soundctl |= c;
   1288 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
   1289 
   1290 	return 0;
   1291 }
   1292 
   1293 static int
   1294 awacs_check_headphones(struct awacs_softc *sc)
   1295 {
   1296 	uint32_t reg;
   1297 	reg = awacs_read_reg(sc, AWACS_CODEC_STATUS);
   1298 	DPRINTF("%s: codec status reg %08x\n", sc->sc_dev.dv_xname, reg);
   1299 	return ((reg & sc->sc_headphones_mask) == sc->sc_headphones_in);
   1300 }
   1301 
   1302 static int
   1303 awacs_status_intr(void *cookie)
   1304 {
   1305 	struct awacs_softc *sc = cookie;
   1306 	int mask;
   1307 
   1308 	mask = awacs_check_headphones(sc) ? OUTPUT_HEADPHONES : OUTPUT_SPEAKER;
   1309 	if (mask != sc->sc_output_mask) {
   1310 
   1311 		sc->sc_output_wanted = mask;
   1312 		wakeup(&sc->sc_event);
   1313 	}
   1314 	/* clear the interrupt */
   1315 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl | AWACS_PORTCHG);
   1316 	return 1;
   1317 }
   1318 
   1319 static void
   1320 awacs_thread(void *cookie)
   1321 {
   1322 	struct awacs_softc *sc = cookie;
   1323 
   1324 	while (1) {
   1325 		tsleep(&sc->sc_event, PWAIT, "awacs_wait", hz);
   1326 		if (sc->sc_output_wanted == sc->sc_output_mask)
   1327 			continue;
   1328 
   1329 		awacs_select_output(sc, sc->sc_output_wanted);
   1330 		DPRINTF("%s: switching to %s\n", sc->sc_dev.dv_xname,
   1331 		    (sc->sc_output_wanted & OUTPUT_SPEAKER) ?
   1332 		    "speaker" : "headphones");
   1333 	}
   1334 }
   1335