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