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