Home | History | Annotate | Line # | Download | only in dev
awacs.c revision 1.33.20.2
      1 /*	$NetBSD: awacs.c,v 1.33.20.2 2010/03/11 15:02:36 yamt 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.20.2 2010/03/11 15:02:36 yamt 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 }
    533 
    534 static int
    535 awacs_setup_sgsmix(device_t cookie)
    536 {
    537 	struct awacs_softc *sc = device_private(cookie);
    538 #if NSGSMIX > 0
    539 	device_t dv;
    540 	deviter_t di;
    541 #endif
    542 
    543 	if (!sc->sc_have_perch)
    544 		return 0;
    545 #if NSGSMIX > 0
    546 	/* look for sgsmix */
    547 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    548 	     dv != NULL;
    549 	     dv = deviter_next(&di)) {
    550 		if (device_is_a(dv, "sgsmix")) {
    551 			sc->sc_sgsmix = dv;
    552 			break;
    553 		}
    554 	}
    555 	deviter_release(&di);
    556 	if (sc->sc_sgsmix == NULL)
    557 		return 0;
    558 
    559 	printf("%s: using %s\n", device_xname(sc->sc_dev),
    560 	    device_xname(sc->sc_sgsmix));
    561 
    562 	awacs_select_output(sc, sc->sc_output_mask);
    563 	awacs_set_volume(sc, sc->vol_l, sc->vol_r);
    564 	awacs_set_bass(sc, 128);
    565 	awacs_set_treble(sc, 128);
    566 	wakeup(&sc->sc_event);
    567 #endif
    568 	return 0;
    569 }
    570 
    571 
    572 static inline u_int
    573 awacs_read_reg(struct awacs_softc *sc, int reg)
    574 {
    575 	char *addr;
    576 
    577 	addr = sc->sc_reg;
    578 	return in32rb(addr + reg);
    579 }
    580 
    581 static inline void
    582 awacs_write_reg(struct awacs_softc *sc, int reg, int val)
    583 {
    584 	char *addr;
    585 
    586 	addr = sc->sc_reg;
    587 	out32rb(addr + reg, val);
    588 }
    589 
    590 static void
    591 awacs_write_codec(struct awacs_softc *sc, int value)
    592 {
    593 
    594 	do {
    595 		delay(100);
    596 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    597 
    598 	awacs_write_reg(sc, AWACS_CODEC_CTRL, value);
    599 
    600 	do {
    601 		delay(100);
    602 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    603 }
    604 
    605 static int
    606 awacs_intr(void *v)
    607 {
    608 	struct awacs_softc *sc;
    609 	struct dbdma_command *cmd;
    610 	int count;
    611 	int status;
    612 
    613 	sc = v;
    614 	cmd = sc->sc_odmacmd;
    615 	count = sc->sc_opages;
    616 	/* Fill used buffer(s). */
    617 	while (count-- > 0) {
    618 		/* if DBDMA_INT_ALWAYS */
    619 		if (in16rb(&cmd->d_command) & 0x30) {	/* XXX */
    620 			status = in16rb(&cmd->d_status);
    621 			cmd->d_status = 0;
    622 			if (status)	/* status == 0x8400 */
    623 				if (sc->sc_ointr)
    624 					(*sc->sc_ointr)(sc->sc_oarg);
    625 		}
    626 		cmd++;
    627 	}
    628 
    629 	return 1;
    630 }
    631 
    632 /*
    633  * Close function is called at splaudio().
    634  */
    635 static void
    636 awacs_close(void *h)
    637 {
    638 	struct awacs_softc *sc;
    639 
    640 	sc = h;
    641 	awacs_halt_output(sc);
    642 	awacs_halt_input(sc);
    643 
    644 	sc->sc_ointr = 0;
    645 	sc->sc_iintr = 0;
    646 }
    647 
    648 static int
    649 awacs_query_encoding(void *h, struct audio_encoding *ae)
    650 {
    651 	struct awacs_softc *sc;
    652 
    653 	sc = h;
    654 	ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
    655 
    656 	switch (ae->index) {
    657 	case 0:
    658 		strcpy(ae->name, AudioEslinear);
    659 		ae->encoding = AUDIO_ENCODING_SLINEAR;
    660 		ae->precision = 16;
    661 		ae->flags = 0;
    662 		return 0;
    663 	case 1:
    664 		strcpy(ae->name, AudioEslinear_be);
    665 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
    666 		ae->precision = 16;
    667 		ae->flags = 0;
    668 		return 0;
    669 	case 2:
    670 		strcpy(ae->name, AudioEslinear_le);
    671 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
    672 		ae->precision = 16;
    673 		if (sc->sc_flags & AWACS_CAP_BSWAP)
    674 			ae->flags = 0;
    675 		return 0;
    676 	case 3:
    677 		strcpy(ae->name, AudioEulinear_be);
    678 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
    679 		ae->precision = 16;
    680 		return 0;
    681 	case 4:
    682 		strcpy(ae->name, AudioEulinear_le);
    683 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
    684 		ae->precision = 16;
    685 		return 0;
    686 	case 5:
    687 		strcpy(ae->name, AudioEmulaw);
    688 		ae->encoding = AUDIO_ENCODING_ULAW;
    689 		ae->precision = 8;
    690 		return 0;
    691 	case 6:
    692 		strcpy(ae->name, AudioEalaw);
    693 		ae->encoding = AUDIO_ENCODING_ALAW;
    694 		ae->precision = 8;
    695 		return 0;
    696 	default:
    697 		return EINVAL;
    698 	}
    699 }
    700 
    701 static int
    702 awacs_set_params(void *h, int setmode, int usemode,
    703 		 audio_params_t *play, audio_params_t *rec,
    704 		 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    705 {
    706 	struct awacs_softc *sc;
    707 	audio_params_t *p;
    708 	stream_filter_list_t *fil;
    709 	int mode, i;
    710 
    711 	sc = h;
    712 	p = NULL;
    713 	/*
    714 	 * This device only has one clock, so make the sample rates match.
    715 	 */
    716 	if (play->sample_rate != rec->sample_rate &&
    717 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    718 		if (setmode == AUMODE_PLAY) {
    719 			rec->sample_rate = play->sample_rate;
    720 			setmode |= AUMODE_RECORD;
    721 		} else if (setmode == AUMODE_RECORD) {
    722 			play->sample_rate = rec->sample_rate;
    723 			setmode |= AUMODE_PLAY;
    724 		} else
    725 			return EINVAL;
    726 	}
    727 
    728 	for (mode = AUMODE_RECORD; mode != -1;
    729 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    730 		if ((setmode & mode) == 0)
    731 			continue;
    732 
    733 		p = mode == AUMODE_PLAY ? play : rec;
    734 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    735 		switch (p->sample_rate) {
    736 		case 48000:	/* aurateconv */
    737 		case 44100:
    738 		case 29400:
    739 		case 22050:
    740 		case 17640:
    741 		case 14700:
    742 		case 11025:
    743 		case 8820:
    744 		case 8000:	/* aurateconv */
    745 		case 7350:
    746 			break;
    747 		default:
    748 			return EINVAL;
    749 		}
    750 		awacs_write_reg(sc, AWACS_BYTE_SWAP, 0);
    751 		i = auconv_set_converter(sc->sc_formats, AWACS_NFORMATS,
    752 					 mode, p, true, fil);
    753 		if (i < 0)
    754 			return EINVAL;
    755 		if (i == AWACS_FORMATS_LE)
    756 			awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
    757 		if (fil->req_size > 0)
    758 			p = &fil->filters[0].param;
    759 		if (awacs_set_rate(sc, p))
    760 			return EINVAL;
    761 	}
    762 	return 0;
    763 }
    764 
    765 static int
    766 awacs_round_blocksize(void *h, int size, int mode, const audio_params_t *param)
    767 {
    768 
    769 	if (size < PAGE_SIZE)
    770 		size = PAGE_SIZE;
    771 	return size & ~PGOFSET;
    772 }
    773 
    774 static int
    775 awacs_halt_output(void *h)
    776 {
    777 	struct awacs_softc *sc;
    778 
    779 	sc = h;
    780 	dbdma_stop(sc->sc_odma);
    781 	dbdma_reset(sc->sc_odma);
    782 	return 0;
    783 }
    784 
    785 static int
    786 awacs_halt_input(void *h)
    787 {
    788 	struct awacs_softc *sc;
    789 
    790 	sc = h;
    791 	dbdma_stop(sc->sc_idma);
    792 	dbdma_reset(sc->sc_idma);
    793 	return 0;
    794 }
    795 
    796 static int
    797 awacs_getdev(void *h, struct audio_device *retp)
    798 {
    799 
    800 	*retp = awacs_device;
    801 	return 0;
    802 }
    803 
    804 enum {
    805 	AWACS_MONITOR_CLASS,
    806 	AWACS_OUTPUT_CLASS,
    807 	AWACS_RECORD_CLASS,
    808 	AWACS_OUTPUT_SELECT,
    809 	AWACS_VOL_MASTER,
    810 	AWACS_INPUT_SELECT,
    811 	AWACS_VOL_INPUT,
    812 	AWACS_VOL_MONITOR,
    813 	AWACS_BASS,
    814 	AWACS_TREBLE,
    815 	AWACS_ENUM_LAST
    816 };
    817 
    818 static int
    819 awacs_set_port(void *h, mixer_ctrl_t *mc)
    820 {
    821 	struct awacs_softc *sc;
    822 	int l, r;
    823 
    824 	DPRINTF("awacs_set_port dev = %d, type = %d\n", mc->dev, mc->type);
    825 	sc = h;
    826 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    827 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    828 
    829 	switch (mc->dev) {
    830 	case AWACS_OUTPUT_SELECT:
    831 		/* No change necessary? */
    832 		if (mc->un.mask == sc->sc_output_mask)
    833 			return 0;
    834 		awacs_select_output(sc, mc->un.mask);
    835 		return 0;
    836 
    837 	case AWACS_VOL_MASTER:
    838 		awacs_set_volume(sc, l, r);
    839 		return 0;
    840 
    841 	case AWACS_INPUT_SELECT:
    842 		/* no change necessary? */
    843 		if (mc->un.mask == sc->sc_record_source)
    844 			return 0;
    845 		switch (mc->un.mask) {
    846 		case 1 << 0: /* CD */
    847 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    848 			sc->sc_codecctl0 |= AWACS_INPUT_CD;
    849 			awacs_write_codec(sc, sc->sc_codecctl0);
    850 			break;
    851 		case 1 << 1: /* microphone */
    852 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    853 			sc->sc_codecctl0 |= AWACS_INPUT_MICROPHONE;
    854 			awacs_write_codec(sc, sc->sc_codecctl0);
    855 			break;
    856 		case 1 << 2: /* line in */
    857 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    858 			sc->sc_codecctl0 |= AWACS_INPUT_LINE;
    859 			awacs_write_codec(sc, sc->sc_codecctl0);
    860 			break;
    861 		default: /* invalid argument */
    862 			return EINVAL;
    863 		}
    864 		sc->sc_record_source = mc->un.mask;
    865 		return 0;
    866 
    867 	case AWACS_VOL_INPUT:
    868 		sc->sc_codecctl0 &= ~0xff;
    869 		sc->sc_codecctl0 |= (l & 0xf0) | (r >> 4);
    870 		awacs_write_codec(sc, sc->sc_codecctl0);
    871 		return 0;
    872 
    873 	case AWACS_VOL_MONITOR:
    874 		awacs_set_loopthrough_volume(sc, l, r);
    875 		return 0;
    876 
    877 #if NSGSMIX > 0
    878 	case AWACS_BASS:
    879 		awacs_set_bass(sc, l);
    880 		return 0;
    881 
    882 	case AWACS_TREBLE:
    883 		awacs_set_treble(sc, l);
    884 		return 0;
    885 #endif
    886 	}
    887 
    888 	return ENXIO;
    889 }
    890 
    891 static int
    892 awacs_get_port(void *h, mixer_ctrl_t *mc)
    893 {
    894 	struct awacs_softc *sc;
    895 	int l, r, vol;
    896 
    897 	sc = h;
    898 	switch (mc->dev) {
    899 	case AWACS_OUTPUT_SELECT:
    900 		mc->un.mask = sc->sc_output_mask;
    901 		return 0;
    902 
    903 	case AWACS_VOL_MASTER:
    904 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->vol_l;
    905 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->vol_r;
    906 		return 0;
    907 
    908 	case AWACS_INPUT_SELECT:
    909 		mc->un.mask = sc->sc_record_source;
    910 		return 0;
    911 
    912 	case AWACS_VOL_INPUT:
    913 		vol = sc->sc_codecctl0 & 0xff;
    914 		l = (vol & 0xf0);
    915 		r = (vol & 0x0f) << 4;
    916 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    917 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    918 		return 0;
    919 
    920 	case AWACS_VOL_MONITOR:
    921 		vol = sc->sc_codecctl5 & 0x3cf;
    922 		l = (vol & 0x3c0) >> 6;
    923 		r = vol & 0xf;
    924 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = (15 - l) << 4;
    925 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = (15 - r) << 4;
    926 		return 0;
    927 
    928 #if NSGSMIX > 0
    929 	case AWACS_BASS:
    930 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
    931 		return 0;
    932 
    933 	case AWACS_TREBLE:
    934 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
    935 		return 0;
    936 #endif
    937 
    938 	default:
    939 		return ENXIO;
    940 	}
    941 
    942 	return 0;
    943 }
    944 
    945 static int
    946 awacs_query_devinfo(void *h, mixer_devinfo_t *dip)
    947 {
    948 #if NSGSMIX > 0
    949 	struct awacs_softc *sc = h;
    950 #endif
    951 
    952 	switch (dip->index) {
    953 
    954 	case AWACS_OUTPUT_SELECT:
    955 		dip->mixer_class = AWACS_MONITOR_CLASS;
    956 		strcpy(dip->label.name, AudioNoutput);
    957 		dip->type = AUDIO_MIXER_SET;
    958 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    959 		dip->un.s.num_mem = 2;
    960 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
    961 		dip->un.s.member[0].mask = 1 << 0;
    962 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
    963 		dip->un.s.member[1].mask = 1 << 1;
    964 		return 0;
    965 
    966 	case AWACS_VOL_MASTER:
    967 		dip->mixer_class = AWACS_MONITOR_CLASS;
    968 		strcpy(dip->label.name, AudioNmaster);
    969 		dip->type = AUDIO_MIXER_VALUE;
    970 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    971 		dip->un.v.num_channels = 2;
    972 		strcpy(dip->un.v.units.name, AudioNvolume);
    973 		return 0;
    974 
    975 	case AWACS_VOL_MONITOR:
    976 		dip->mixer_class = AWACS_MONITOR_CLASS;
    977 		strcpy(dip->label.name, AudioNmonitor);
    978 		dip->type = AUDIO_MIXER_VALUE;
    979 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    980 		dip->un.v.num_channels = 2;
    981 		strcpy(dip->un.v.units.name, AudioNvolume);
    982 		return 0;
    983 
    984 #if NSGSMIX > 0
    985 	case AWACS_BASS:
    986 		if (sc->sc_sgsmix == NULL)
    987 			return ENXIO;
    988 		dip->mixer_class = AWACS_MONITOR_CLASS;
    989 		strcpy(dip->label.name, AudioNbass);
    990 		dip->type = AUDIO_MIXER_VALUE;
    991 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    992 		dip->un.v.num_channels = 1;
    993 		strcpy(dip->un.v.units.name, AudioNbass);
    994 		return 0;
    995 
    996 	case AWACS_TREBLE:
    997 		if (sc->sc_sgsmix == NULL)
    998 			return ENXIO;
    999 		dip->mixer_class = AWACS_MONITOR_CLASS;
   1000 		strcpy(dip->label.name, AudioNtreble);
   1001 		dip->type = AUDIO_MIXER_VALUE;
   1002 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1003 		dip->un.v.num_channels = 1;
   1004 		strcpy(dip->un.v.units.name, AudioNtreble);
   1005 		return 0;
   1006 #endif
   1007 
   1008 	case AWACS_INPUT_SELECT:
   1009 		dip->mixer_class = AWACS_RECORD_CLASS;
   1010 		strcpy(dip->label.name, AudioNsource);
   1011 		dip->type = AUDIO_MIXER_SET;
   1012 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1013 		dip->un.s.num_mem = 3;
   1014 		strcpy(dip->un.s.member[0].label.name, AudioNcd);
   1015 		dip->un.s.member[0].mask = 1 << 0;
   1016 		strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
   1017 		dip->un.s.member[1].mask = 1 << 1;
   1018 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1019 		dip->un.s.member[2].mask = 1 << 2;
   1020 		return 0;
   1021 
   1022 	case AWACS_VOL_INPUT:
   1023 		dip->mixer_class = AWACS_RECORD_CLASS;
   1024 		strcpy(dip->label.name, AudioNrecord);
   1025 		dip->type = AUDIO_MIXER_VALUE;
   1026 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1027 		dip->un.v.num_channels = 2;
   1028 		strcpy(dip->un.v.units.name, AudioNvolume);
   1029 		return 0;
   1030 
   1031 	case AWACS_MONITOR_CLASS:
   1032 		dip->mixer_class = AWACS_MONITOR_CLASS;
   1033 		strcpy(dip->label.name, AudioCmonitor);
   1034 		dip->type = AUDIO_MIXER_CLASS;
   1035 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1036 		return 0;
   1037 
   1038 	case AWACS_OUTPUT_CLASS:
   1039 		dip->mixer_class = AWACS_OUTPUT_CLASS;
   1040 		strcpy(dip->label.name, AudioCoutputs);
   1041 		dip->type = AUDIO_MIXER_CLASS;
   1042 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1043 		return 0;
   1044 
   1045 	case AWACS_RECORD_CLASS:
   1046 		dip->mixer_class = AWACS_RECORD_CLASS;
   1047 		strcpy(dip->label.name, AudioCrecord);
   1048 		dip->type = AUDIO_MIXER_CLASS;
   1049 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1050 		return 0;
   1051 	}
   1052 
   1053 	return ENXIO;
   1054 }
   1055 
   1056 static size_t
   1057 awacs_round_buffersize(void *h, int dir, size_t size)
   1058 {
   1059 
   1060 	if (size > 65536)
   1061 		size = 65536;
   1062 	return size;
   1063 }
   1064 
   1065 static paddr_t
   1066 awacs_mappage(void *h, void *mem, off_t off, int prot)
   1067 {
   1068 
   1069 	if (off < 0)
   1070 		return -1;
   1071 	return -1;	/* XXX */
   1072 }
   1073 
   1074 static int
   1075 awacs_get_props(void *h)
   1076 {
   1077 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
   1078 }
   1079 
   1080 static int
   1081 awacs_trigger_output(void *h, void *start, void *end, int bsize,
   1082 		     void (*intr)(void *), void *arg,
   1083 		     const audio_params_t *param)
   1084 {
   1085 	struct awacs_softc *sc;
   1086 	struct dbdma_command *cmd;
   1087 	vaddr_t va;
   1088 	int i, len, intmode;
   1089 
   1090 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
   1091 	sc = h;
   1092 	cmd = sc->sc_odmacmd;
   1093 	sc->sc_ointr = intr;
   1094 	sc->sc_oarg = arg;
   1095 	sc->sc_opages = ((char *)end - (char *)start) / PAGE_SIZE;
   1096 
   1097 #ifdef DIAGNOSTIC
   1098 	if (sc->sc_opages > 16)
   1099 		panic("awacs_trigger_output");
   1100 #endif
   1101 
   1102 	va = (vaddr_t)start;
   1103 	len = 0;
   1104 	for (i = sc->sc_opages; i > 0; i--) {
   1105 		len += PAGE_SIZE;
   1106 		if (len < bsize)
   1107 			intmode = DBDMA_INT_NEVER;
   1108 		else {
   1109 			len = 0;
   1110 			intmode = DBDMA_INT_ALWAYS;
   1111 		}
   1112 
   1113 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, PAGE_SIZE, vtophys(va),
   1114 			intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1115 		va += PAGE_SIZE;
   1116 		cmd++;
   1117 	}
   1118 
   1119 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
   1120 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
   1121 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
   1122 
   1123 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
   1124 
   1125 	return 0;
   1126 }
   1127 
   1128 static int
   1129 awacs_trigger_input(void *h, void *start, void *end, int bsize,
   1130 		    void (*intr)(void *), void *arg,
   1131 		    const audio_params_t *param)
   1132 {
   1133 
   1134 	DPRINTF("awacs_trigger_input called\n");
   1135 	return 1;
   1136 }
   1137 
   1138 static void
   1139 awacs_select_output(struct awacs_softc *sc, int mask)
   1140 {
   1141 
   1142 #if NSGSMIX > 0
   1143 	if (sc->sc_sgsmix) {
   1144 		if (mask & OUTPUT_HEADPHONES) {
   1145 			/* mute speakers */
   1146 			sgsmix_set_speaker_vol(sc->sc_sgsmix, 0, 0);
   1147 			sgsmix_set_headphone_vol(sc->sc_sgsmix,
   1148 			    sc->vol_l, sc->vol_r);
   1149 		}
   1150 		if (mask & OUTPUT_SPEAKER) {
   1151 			/* mute headphones */
   1152 			sgsmix_set_speaker_vol(sc->sc_sgsmix,
   1153 			    sc->vol_l, sc->vol_r);
   1154 			sgsmix_set_headphone_vol(sc->sc_sgsmix, 0, 0);
   1155 		}
   1156 	} else {
   1157 #endif
   1158 	sc->sc_codecctl1 |= AWACS_MUTE_SPEAKER | AWACS_MUTE_HEADPHONE;
   1159 	if ((sc->vol_l > 0) || (sc->vol_r > 0)) {
   1160 		if (mask & OUTPUT_SPEAKER)
   1161 			sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
   1162 		if (mask & OUTPUT_HEADPHONES)
   1163 			sc->sc_codecctl1 &= ~AWACS_MUTE_HEADPHONE;
   1164 	}
   1165 	awacs_write_codec(sc, sc->sc_codecctl1);
   1166 #if NSGSMIX > 0
   1167 	}
   1168 #endif
   1169 	sc->sc_output_mask = mask;
   1170 }
   1171 
   1172 static void
   1173 awacs_set_speaker_volume(struct awacs_softc *sc, int left, int right)
   1174 {
   1175 
   1176 #if NSGSMIX > 0
   1177 	if (sc->sc_sgsmix) {
   1178 		if (sc->sc_output_mask & OUTPUT_SPEAKER)
   1179 			sgsmix_set_speaker_vol(sc->sc_sgsmix, left, right);
   1180 	} else
   1181 #endif
   1182 	{
   1183 		int lval;
   1184 		int rval;
   1185 		uint32_t codecctl = sc->sc_codecctl1;
   1186 
   1187 		lval = 15 - ((left  & 0xf0) >> 4);
   1188 		rval = 15 - ((right & 0xf0) >> 4);
   1189 		DPRINTF("speaker_volume %d %d\n", lval, rval);
   1190 
   1191 		sc->sc_codecctl4 &= ~0x3cf;
   1192 		sc->sc_codecctl4 |= (lval << 6) | rval;
   1193 		awacs_write_codec(sc, sc->sc_codecctl4);
   1194 		if ((left == 0) && (right == 0)) {
   1195 			/*
   1196 			 * max. attenuation doesn't mean silence so we need to
   1197 			 * mute the output channel here
   1198 			 */
   1199 			codecctl |= AWACS_MUTE_SPEAKER;
   1200 		} else if (sc->sc_output_mask & OUTPUT_SPEAKER) {
   1201 			codecctl &= ~AWACS_MUTE_SPEAKER;
   1202 		}
   1203 
   1204 		if (codecctl != sc->sc_codecctl1) {
   1205 
   1206 			sc->sc_codecctl1 = codecctl;
   1207 			awacs_write_codec(sc, sc->sc_codecctl1);
   1208 		}
   1209 	}
   1210 }
   1211 
   1212 static void
   1213 awacs_set_ext_volume(struct awacs_softc *sc, int left, int right)
   1214 {
   1215 
   1216 #if NSGSMIX > 0
   1217 	if (sc->sc_sgsmix) {
   1218 		if (sc->sc_output_mask & OUTPUT_HEADPHONES)
   1219 			sgsmix_set_headphone_vol(sc->sc_sgsmix, left, right);
   1220 	} else
   1221 #endif
   1222 	{
   1223 		int lval;
   1224 		int rval;
   1225 		uint32_t codecctl = sc->sc_codecctl1;
   1226 
   1227 		lval = 15 - ((left  & 0xf0) >> 4);
   1228 		rval = 15 - ((right & 0xf0) >> 4);
   1229 		DPRINTF("ext_volume %d %d\n", lval, rval);
   1230 
   1231 		sc->sc_codecctl2 &= ~0x3cf;
   1232 		sc->sc_codecctl2 |= (lval << 6) | rval;
   1233 		awacs_write_codec(sc, sc->sc_codecctl2);
   1234 
   1235 		if ((left == 0) && (right == 0)) {
   1236 			/*
   1237 			 * max. attenuation doesn't mean silence so we need to
   1238 			 * mute the output channel here
   1239 			 */
   1240 			codecctl |= AWACS_MUTE_HEADPHONE;
   1241 		} else if (sc->sc_output_mask & OUTPUT_HEADPHONES) {
   1242 
   1243 			codecctl &= ~AWACS_MUTE_HEADPHONE;
   1244 		}
   1245 
   1246 		if (codecctl != sc->sc_codecctl1) {
   1247 
   1248 			sc->sc_codecctl1 = codecctl;
   1249 			awacs_write_codec(sc, sc->sc_codecctl1);
   1250 		}
   1251 	}
   1252 }
   1253 
   1254 void
   1255 awacs_set_volume(struct awacs_softc *sc, int left, int right)
   1256 {
   1257 
   1258 	awacs_set_ext_volume(sc, left, right);
   1259 	awacs_set_speaker_volume(sc, left, right);
   1260 
   1261 	sc->vol_l = left;
   1262 	sc->vol_r = right;
   1263 }
   1264 
   1265 #if NSGSMIX > 0
   1266 static void
   1267 awacs_set_bass(struct awacs_softc *sc, int bass)
   1268 {
   1269 
   1270 	if (sc->sc_bass == bass)
   1271 		return;
   1272 
   1273 	sc->sc_bass = bass;
   1274 	if (sc->sc_sgsmix)
   1275 		sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass,
   1276 		    sc->sc_treble);
   1277 }
   1278 
   1279 static void
   1280 awacs_set_treble(struct awacs_softc *sc, int treble)
   1281 {
   1282 
   1283 	if (sc->sc_treble == treble)
   1284 		return;
   1285 
   1286 	sc->sc_treble = treble;
   1287 	if (sc->sc_sgsmix)
   1288 		sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass,
   1289 		    sc->sc_treble);
   1290 }
   1291 #endif
   1292 
   1293 void
   1294 awacs_set_loopthrough_volume(struct awacs_softc *sc, int left, int right)
   1295 {
   1296 	int lval;
   1297 	int rval;
   1298 
   1299 	lval = 15 - ((left  & 0xff) >> 4);
   1300 	rval = 15 - ((right & 0xff) >> 4);
   1301 	DPRINTF("loopthrough_volume %d %d\n", lval, rval);
   1302 
   1303 	sc->sc_codecctl5 &= ~0x3cf;
   1304 	sc->sc_codecctl5 |= (lval << 6) | rval;
   1305 	awacs_write_codec(sc, sc->sc_codecctl5);
   1306 }
   1307 
   1308 int
   1309 awacs_set_rate(struct awacs_softc *sc, const audio_params_t *p)
   1310 {
   1311 	int c;
   1312 
   1313 	switch (p->sample_rate) {
   1314 	case 44100:
   1315 		c = AWACS_RATE_44100;
   1316 		break;
   1317 	case 29400:
   1318 		c = AWACS_RATE_29400;
   1319 		break;
   1320 	case 22050:
   1321 		c = AWACS_RATE_22050;
   1322 		break;
   1323 	case 17640:
   1324 		c = AWACS_RATE_17640;
   1325 		break;
   1326 	case 14700:
   1327 		c = AWACS_RATE_14700;
   1328 		break;
   1329 	case 11025:
   1330 		c = AWACS_RATE_11025;
   1331 		break;
   1332 	case 8820:
   1333 		c = AWACS_RATE_8820;
   1334 		break;
   1335 	case 7350:
   1336 		c = AWACS_RATE_7350;
   1337 		break;
   1338 	default:
   1339 		return -1;
   1340 	}
   1341 
   1342 	sc->sc_soundctl &= ~AWACS_RATE_MASK;
   1343 	sc->sc_soundctl |= c;
   1344 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
   1345 
   1346 	return 0;
   1347 }
   1348 
   1349 static int
   1350 awacs_check_headphones(struct awacs_softc *sc)
   1351 {
   1352 	uint32_t reg;
   1353 	reg = awacs_read_reg(sc, AWACS_CODEC_STATUS);
   1354 	DPRINTF("%s: codec status reg %08x\n", device_xname(sc->sc_dev), reg);
   1355 	return ((reg & sc->sc_headphones_mask) == sc->sc_headphones_in);
   1356 }
   1357 
   1358 static int
   1359 awacs_status_intr(void *cookie)
   1360 {
   1361 	struct awacs_softc *sc = cookie;
   1362 	int mask;
   1363 
   1364 	mask = awacs_check_headphones(sc) ? OUTPUT_HEADPHONES : OUTPUT_SPEAKER;
   1365 	if (mask != sc->sc_output_mask) {
   1366 
   1367 		sc->sc_output_wanted = mask;
   1368 		wakeup(&sc->sc_event);
   1369 	}
   1370 	/* clear the interrupt */
   1371 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl | AWACS_PORTCHG);
   1372 	return 1;
   1373 }
   1374 
   1375 static void
   1376 awacs_thread(void *cookie)
   1377 {
   1378 	struct awacs_softc *sc = cookie;
   1379 
   1380 	while (1) {
   1381 		tsleep(&sc->sc_event, PWAIT, "awacs_wait", hz);
   1382 		if (sc->sc_output_wanted == sc->sc_output_mask)
   1383 			continue;
   1384 
   1385 		awacs_select_output(sc, sc->sc_output_wanted);
   1386 		DPRINTF("%s: switching to %s\n", device_xname(sc->sc_dev),
   1387 		    (sc->sc_output_wanted & OUTPUT_SPEAKER) ?
   1388 		    "speaker" : "headphones");
   1389 	}
   1390 }
   1391