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