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