Home | History | Annotate | Line # | Download | only in dev
awacs.c revision 1.38
      1 /*	$NetBSD: awacs.c,v 1.38 2010/12/20 00:25:37 matt 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.38 2010/12/20 00:25:37 matt 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 <machine/autoconf.h>
     45 #include <machine/pio.h>
     46 
     47 #include <dev/ofw/openfirm.h>
     48 #include <macppc/dev/dbdma.h>
     49 
     50 #include <dev/i2c/sgsmixvar.h>
     51 #include "sgsmix.h"
     52 #include "opt_awacs.h"
     53 
     54 #ifdef AWACS_DEBUG
     55 # define DPRINTF printf
     56 #else
     57 # define DPRINTF while (0) printf
     58 #endif
     59 
     60 /* sc_flags values */
     61 #define AWACS_CAP_BSWAP		0x0001
     62 
     63 struct awacs_softc {
     64 	device_t sc_dev;
     65 	int sc_flags;
     66 
     67 	void (*sc_ointr)(void *);	/* DMA completion intr handler */
     68 	void *sc_oarg;			/* arg for sc_ointr() */
     69 	int sc_opages;			/* # of output pages */
     70 
     71 	void (*sc_iintr)(void *);	/* DMA completion intr handler */
     72 	void *sc_iarg;			/* arg for sc_iintr() */
     73 
     74 	uint32_t sc_record_source;	/* recording source mask */
     75 	uint32_t sc_output_mask;	/* output mask */
     76 	uint32_t sc_headphones_mask;	/* which reading of the gpio means */
     77 	uint32_t sc_headphones_in;	/* headphones are present */
     78 
     79 	int sc_screamer;
     80 	int sc_have_perch;
     81 	int vol_l, vol_r;
     82 	int sc_bass, sc_treble;
     83 	lwp_t *sc_thread;
     84 	int sc_event;
     85 	int sc_output_wanted;
     86 	int sc_need_parallel_output;
     87 #if NSGSMIX > 0
     88 	device_t sc_sgsmix;
     89 #endif
     90 
     91 	char *sc_reg;
     92 	u_int sc_codecctl0;
     93 	u_int sc_codecctl1;
     94 	u_int sc_codecctl2;
     95 	u_int sc_codecctl4;
     96 	u_int sc_codecctl5;
     97 	u_int sc_codecctl6;
     98 	u_int sc_codecctl7;
     99 	u_int sc_soundctl;
    100 
    101 	struct dbdma_regmap *sc_odma;
    102 	struct dbdma_regmap *sc_idma;
    103 	struct dbdma_command *sc_odmacmd;
    104 	struct dbdma_command *sc_idmacmd;
    105 
    106 #define AWACS_NFORMATS	2
    107 	struct audio_format sc_formats[AWACS_NFORMATS];
    108 };
    109 
    110 static int awacs_match(device_t, struct cfdata *, void *);
    111 static void awacs_attach(device_t, device_t, void *);
    112 static int awacs_intr(void *);
    113 static int awacs_status_intr(void *);
    114 
    115 static void awacs_close(void *);
    116 static int awacs_query_encoding(void *, struct audio_encoding *);
    117 static int awacs_set_params(void *, int, int, audio_params_t *, audio_params_t *,
    118 		     stream_filter_list_t *, stream_filter_list_t *);
    119 
    120 static int awacs_round_blocksize(void *, int, int, const audio_params_t *);
    121 static int awacs_trigger_output(void *, void *, void *, int, void (*)(void *),
    122 			 void *, const audio_params_t *);
    123 static int awacs_trigger_input(void *, void *, void *, int, void (*)(void *),
    124 			void *, const audio_params_t *);
    125 static int awacs_halt_output(void *);
    126 static int awacs_halt_input(void *);
    127 static int awacs_getdev(void *, struct audio_device *);
    128 static int awacs_set_port(void *, mixer_ctrl_t *);
    129 static int awacs_get_port(void *, mixer_ctrl_t *);
    130 static int awacs_query_devinfo(void *, mixer_devinfo_t *);
    131 static size_t awacs_round_buffersize(void *, int, size_t);
    132 static paddr_t awacs_mappage(void *, void *, off_t, int);
    133 static int awacs_get_props(void *);
    134 
    135 static inline u_int awacs_read_reg(struct awacs_softc *, int);
    136 static inline void awacs_write_reg(struct awacs_softc *, int, int);
    137 static void awacs_write_codec(struct awacs_softc *, int);
    138 
    139 void awacs_set_volume(struct awacs_softc *, int, int);
    140 static void awacs_set_speaker_volume(struct awacs_softc *, int, int);
    141 static void awacs_set_ext_volume(struct awacs_softc *, int, int);
    142 static void awacs_set_loopthrough_volume(struct awacs_softc *, int, int);
    143 static int awacs_set_rate(struct awacs_softc *, const audio_params_t *);
    144 static void awacs_select_output(struct awacs_softc *, int);
    145 static int awacs_check_headphones(struct awacs_softc *);
    146 static void awacs_thread(void *);
    147 
    148 #if NSGSMIX > 0
    149 static void awacs_set_bass(struct awacs_softc *, int);
    150 static void awacs_set_treble(struct awacs_softc *, int);
    151 #endif
    152 static int awacs_setup_sgsmix(device_t);
    153 
    154 CFATTACH_DECL_NEW(awacs, sizeof(struct awacs_softc),
    155     awacs_match, awacs_attach, NULL, NULL);
    156 
    157 const struct audio_hw_if awacs_hw_if = {
    158 	NULL,			/* open */
    159 	awacs_close,
    160 	NULL,
    161 	awacs_query_encoding,
    162 	awacs_set_params,
    163 	awacs_round_blocksize,
    164 	NULL,
    165 	NULL,
    166 	NULL,
    167 	NULL,
    168 	NULL,
    169 	awacs_halt_output,
    170 	awacs_halt_input,
    171 	NULL,
    172 	awacs_getdev,
    173 	NULL,
    174 	awacs_set_port,
    175 	awacs_get_port,
    176 	awacs_query_devinfo,
    177 	NULL,
    178 	NULL,
    179 	awacs_round_buffersize,
    180 	awacs_mappage,
    181 	awacs_get_props,
    182 	awacs_trigger_output,
    183 	awacs_trigger_input,
    184 	NULL,
    185 };
    186 
    187 struct audio_device awacs_device = {
    188 	"AWACS",
    189 	"",
    190 	"awacs"
    191 };
    192 
    193 #define AWACS_NFORMATS		2
    194 #define AWACS_FORMATS_LE	0
    195 static const struct audio_format awacs_formats[AWACS_NFORMATS] = {
    196 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    197 	 2, AUFMT_STEREO, 8,
    198 	 {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,
    201 	 {7350, 8820, 11025, 14700, 17640, 22050, 29400, 44100}},
    202 };
    203 
    204 /* register offset */
    205 #define AWACS_SOUND_CTRL	0x00
    206 #define AWACS_CODEC_CTRL	0x10
    207 #define AWACS_CODEC_STATUS	0x20
    208 #define AWACS_CLIP_COUNT	0x30
    209 #define AWACS_BYTE_SWAP		0x40
    210 
    211 /* sound control */
    212 #define AWACS_INPUT_SUBFRAME0	0x00000001
    213 #define AWACS_INPUT_SUBFRAME1	0x00000002
    214 #define AWACS_INPUT_SUBFRAME2	0x00000004
    215 #define AWACS_INPUT_SUBFRAME3	0x00000008
    216 
    217 #define AWACS_OUTPUT_SUBFRAME0	0x00000010
    218 #define AWACS_OUTPUT_SUBFRAME1	0x00000020
    219 #define AWACS_OUTPUT_SUBFRAME2	0x00000040
    220 #define AWACS_OUTPUT_SUBFRAME3	0x00000080
    221 
    222 #define AWACS_RATE_44100	0x00000000
    223 #define AWACS_RATE_29400	0x00000100
    224 #define AWACS_RATE_22050	0x00000200
    225 #define AWACS_RATE_17640	0x00000300
    226 #define AWACS_RATE_14700	0x00000400
    227 #define AWACS_RATE_11025	0x00000500
    228 #define AWACS_RATE_8820		0x00000600
    229 #define AWACS_RATE_7350		0x00000700
    230 #define AWACS_RATE_MASK		0x00000700
    231 
    232 #define AWACS_ERROR		0x00000800
    233 #define AWACS_PORTCHG		0x00001000
    234 #define AWACS_INTR_ERROR	0x00002000	/* interrupt on error */
    235 #define AWACS_INTR_PORTCHG	0x00004000	/* interrupt on port change */
    236 
    237 #define AWACS_STATUS_SUBFRAME	0x00018000	/* mask */
    238 
    239 /* codec control */
    240 #define AWACS_CODEC_ADDR0	0x00000000
    241 #define AWACS_CODEC_ADDR1	0x00001000
    242 #define AWACS_CODEC_ADDR2	0x00002000
    243 #define AWACS_CODEC_ADDR4	0x00004000
    244 #define AWACS_CODEC_ADDR5	0x00005000
    245 #define AWACS_CODEC_ADDR6	0x00006000
    246 #define AWACS_CODEC_ADDR7	0x00007000
    247 #define AWACS_CODEC_EMSEL0	0x00000000
    248 #define AWACS_CODEC_EMSEL1	0x00400000
    249 #define AWACS_CODEC_EMSEL2	0x00800000
    250 #define AWACS_CODEC_EMSEL4	0x00c00000
    251 #define AWACS_CODEC_BUSY	0x01000000
    252 
    253 /* cc0 */
    254 #define AWACS_DEFAULT_CD_GAIN	0x000000bb
    255 #define AWACS_INPUT_CD		0x00000200
    256 #define AWACS_INPUT_LINE	0x00000400
    257 #define AWACS_INPUT_MICROPHONE	0x00000800
    258 #define AWACS_INPUT_MASK	0x00000e00
    259 
    260 /* cc1 */
    261 #define AWACS_LOOP_THROUGH	0x00000040
    262 #define AWACS_MUTE_SPEAKER	0x00000080
    263 #define AWACS_MUTE_HEADPHONE	0x00000200
    264 #define AWACS_PARALLEL_OUTPUT	0x00000c00
    265 
    266 /* output */
    267 #define OUTPUT_SPEAKER		1
    268 #define OUTPUT_HEADPHONES	2
    269 
    270 /* codec status */
    271 
    272 static const char *screamer[] = {"screamer", NULL};
    273 
    274 /*
    275  * list machines that have the headphone detect GPIO reversed here.
    276  * so far the only known case is the PowerBook 3400c and similar machines
    277  */
    278 static const char *detect_reversed[] = {"AAPL,3400/2400",
    279 					"AAPL,3500",
    280 					NULL};
    281 
    282 static const char *use_gpio4[] = {	"PowerMac3,3",
    283 					NULL};
    284 
    285 /*
    286  * list of machines that do not require AWACS_PARALLEL_OUTPUT
    287  */
    288 static const char *no_parallel_output[] = {	"PowerBook3,1",
    289 						NULL};
    290 
    291 static int
    292 awacs_match(device_t parent, struct cfdata *match, void *aux)
    293 {
    294 	struct confargs *ca;
    295 
    296 	ca = aux;
    297 
    298 	if (strcmp(ca->ca_name, "awacs") == 0 ||
    299 	    strcmp(ca->ca_name, "davbus") == 0)
    300 		return 100;
    301 
    302 	if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
    303 		return 0;
    304 
    305 	if (strcmp(ca->ca_name, "i2s") == 0)
    306 		return 1;
    307 
    308 	return 0;
    309 }
    310 
    311 static void
    312 awacs_attach(device_t parent, device_t self, void *aux)
    313 {
    314 	struct awacs_softc *sc;
    315 	struct confargs *ca;
    316 	int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
    317 	int len = -1, perch;
    318 	int root_node;
    319 	char compat[256];
    320 
    321 	sc = device_private(self);
    322 	sc->sc_dev = self;
    323 	ca = aux;
    324 
    325 	sc->sc_reg = mapiodev(ca->ca_baseaddr + ca->ca_reg[0], ca->ca_reg[1]);
    326 
    327 	/* out */
    328 	sc->sc_odma = mapiodev(ca->ca_baseaddr + ca->ca_reg[2], ca->ca_reg[3]);
    329 	sc->sc_odmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
    330 	/* in */
    331 	sc->sc_idma = mapiodev(ca->ca_baseaddr + ca->ca_reg[4], ca->ca_reg[5]);
    332 	sc->sc_idmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
    333 
    334 	if (strcmp(ca->ca_name, "i2s") == 0) {
    335 		int node, intr[6];
    336 
    337 		node = OF_child(ca->ca_node);
    338 		if (node == 0) {
    339 			printf("no i2s-a child\n");
    340 			return;
    341 		}
    342 		if (OF_getprop(node, "interrupts", intr, sizeof(intr)) == -1) {
    343 			printf("no interrupt property\n");
    344 			return;
    345 		}
    346 
    347 		cirq = intr[0];
    348 		oirq = intr[2];
    349 		iirq = intr[4];
    350 		cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
    351 		oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
    352 		iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
    353 	} else if (ca->ca_nintr == 24) {
    354 		cirq = ca->ca_intr[0];
    355 		oirq = ca->ca_intr[2];
    356 		iirq = ca->ca_intr[4];
    357 		cirq_type = ca->ca_intr[1] ? IST_LEVEL : IST_EDGE;
    358 		oirq_type = ca->ca_intr[3] ? IST_LEVEL : IST_EDGE;
    359 		iirq_type = ca->ca_intr[5] ? IST_LEVEL : IST_EDGE;
    360 	} else {
    361 		cirq = ca->ca_intr[0];
    362 		oirq = ca->ca_intr[1];
    363 		iirq = ca->ca_intr[2];
    364 		cirq_type = oirq_type = iirq_type = IST_EDGE;
    365 	}
    366 
    367 	intr_establish(cirq, cirq_type, IPL_BIO, awacs_status_intr, sc);
    368 	intr_establish(oirq, oirq_type, IPL_AUDIO, awacs_intr, sc);
    369 	intr_establish(iirq, iirq_type, IPL_AUDIO, awacs_intr, sc);
    370 
    371 	/* check if the chip is a screamer */
    372 	sc->sc_screamer = (of_compatible(ca->ca_node, screamer) != -1);
    373 	if (!sc->sc_screamer) {
    374 		/* look for 'sound' child node */
    375 		int sound_node;
    376 
    377 		sound_node = OF_child(ca->ca_node);
    378 		while ((sound_node != 0) && (!sc->sc_screamer)) {
    379 
    380 			sc->sc_screamer =
    381 			    (of_compatible(sound_node, screamer) != -1);
    382 			sound_node = OF_peer(sound_node);
    383 		}
    384 	}
    385 
    386 	if (sc->sc_screamer) {
    387 		printf(" Screamer");
    388 	}
    389 
    390 	printf(": irq %d,%d,%d\n", cirq, oirq, iirq);
    391 
    392 	sc->vol_l = 0;
    393 	sc->vol_r = 0;
    394 
    395 	memcpy(&sc->sc_formats, awacs_formats, sizeof(awacs_formats));
    396 
    397 	/* XXX Uni-North based models don't have byteswap capability. */
    398 	if (OF_finddevice("/uni-n") == -1) {
    399 
    400 		sc->sc_flags |= AWACS_CAP_BSWAP;
    401 	} else {
    402 
    403 		AUFMT_INVALIDATE(&sc->sc_formats[AWACS_FORMATS_LE]);
    404 	}
    405 
    406 	sc->sc_soundctl = AWACS_INPUT_SUBFRAME0 | AWACS_OUTPUT_SUBFRAME0 |
    407 		AWACS_RATE_44100 | AWACS_INTR_PORTCHG;
    408 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
    409 
    410 	sc->sc_codecctl0 = AWACS_CODEC_ADDR0 | AWACS_CODEC_EMSEL0;
    411 	sc->sc_codecctl1 = AWACS_CODEC_ADDR1 | AWACS_CODEC_EMSEL0;
    412 	sc->sc_codecctl2 = AWACS_CODEC_ADDR2 | AWACS_CODEC_EMSEL0;
    413 	sc->sc_codecctl4 = AWACS_CODEC_ADDR4 | AWACS_CODEC_EMSEL0;
    414 	sc->sc_codecctl5 = AWACS_CODEC_ADDR5 | AWACS_CODEC_EMSEL0;
    415 	sc->sc_codecctl6 = AWACS_CODEC_ADDR6 | AWACS_CODEC_EMSEL0;
    416 	sc->sc_codecctl7 = AWACS_CODEC_ADDR7 | AWACS_CODEC_EMSEL0;
    417 
    418 	sc->sc_codecctl0 |= AWACS_INPUT_CD | AWACS_DEFAULT_CD_GAIN;
    419 	awacs_write_codec(sc, sc->sc_codecctl0);
    420 
    421 	/* Set loopthrough for external mixer on beige G3 */
    422 	sc->sc_codecctl1 |= AWACS_LOOP_THROUGH;
    423 
    424         printf("%s: ", device_xname(sc->sc_dev));
    425 
    426 	/*
    427 	 * all(?) awacs have GPIOs to detect if there's something plugged into
    428 	 * the headphone jack. The other GPIOs are either used for other jacks
    429 	 * ( the PB3400c's microphone jack for instance ) or unused.
    430 	 * The problem is that there are at least three different ways how
    431 	 * those GPIOs are wired to the actual jacks.
    432 	 * For now we bother only with headphone detection
    433 	 */
    434 	perch = OF_finddevice("/perch");
    435 	root_node = OF_finddevice("/");
    436 	if (of_compatible(root_node, detect_reversed) != -1) {
    437 
    438 		/* 0x02 is for the microphone jack, high active */
    439 		/*
    440 		 * for some reason the gpio for the headphones jack is low
    441 		 * active on the PB3400 and similar machines
    442 		 */
    443 		sc->sc_headphones_mask = 0x8;
    444 		sc->sc_headphones_in = 0x0;
    445 	} else if ((perch != -1) ||
    446 	    (of_compatible(root_node, use_gpio4) != -1)) {
    447 		/*
    448 		 * this is for the beige G3's 'personality card' which uses
    449 		 * yet another wiring of the headphone detect GPIOs
    450 		 * some G4s use it as well
    451 		 */
    452 		sc->sc_headphones_mask = 0x04;
    453 		sc->sc_headphones_in = 0x04;
    454 	} else {
    455 		/* while on most machines it's high active as well */
    456 		sc->sc_headphones_mask = 0x8;
    457 		sc->sc_headphones_in = 0x8;
    458 	}
    459 
    460 	if (of_compatible(root_node, no_parallel_output) != -1)
    461 		sc->sc_need_parallel_output = 0;
    462 	else {
    463 		sc->sc_need_parallel_output = 1;
    464 		sc->sc_codecctl1 |= AWACS_PARALLEL_OUTPUT;
    465 	}
    466 
    467 	if (awacs_check_headphones(sc)) {
    468 
    469                 /* default output to headphones */
    470                 printf("headphones\n");
    471                 sc->sc_output_mask = OUTPUT_HEADPHONES;
    472         } else {
    473 
    474                 /* default output to speakers */
    475                 printf("speaker\n");
    476                 sc->sc_output_mask = OUTPUT_SPEAKER;
    477         }
    478 	sc->sc_output_wanted = sc->sc_output_mask;
    479 	awacs_select_output(sc, sc->sc_output_mask);
    480 
    481 	delay(100);
    482 	if (sc->sc_screamer) {
    483 		awacs_write_codec(sc, sc->sc_codecctl6);
    484 		awacs_write_codec(sc, sc->sc_codecctl5);
    485 		delay(2);
    486 		awacs_write_codec(sc, sc->sc_codecctl1);
    487 		awacs_write_codec(sc, sc->sc_codecctl7);
    488 	}
    489 
    490 	/* default input from CD */
    491 	sc->sc_record_source = 1 << 0;
    492 	sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    493 	sc->sc_codecctl0 |= AWACS_INPUT_CD;
    494 	awacs_write_codec(sc, sc->sc_codecctl0);
    495 
    496 	/* Enable interrupts and looping mode. */
    497 	/* XXX ... */
    498 
    499 	sc->sc_codecctl1 |= AWACS_LOOP_THROUGH;
    500 	if (sc->sc_need_parallel_output)
    501 		sc->sc_codecctl1 |= AWACS_PARALLEL_OUTPUT;
    502 	awacs_write_codec(sc, sc->sc_codecctl1);
    503 
    504 #if NSGSMIX > 0
    505 	sc->sc_sgsmix = NULL;
    506 #endif
    507 	sc->sc_have_perch = 0;
    508 	if (perch != -1) {
    509 
    510 		len = OF_getprop(perch, "compatible", compat, 255);
    511 		if (len > 0) {
    512 			printf("%s: found '%s' personality card\n",
    513 			    device_xname(sc->sc_dev), compat);
    514 			sc->sc_have_perch = 1;
    515 			config_finalize_register(sc->sc_dev,
    516 			    awacs_setup_sgsmix);
    517 		}
    518 	}
    519 
    520 	/* Set initial volume[s] */
    521 	awacs_set_volume(sc, 144, 144);
    522 	awacs_set_loopthrough_volume(sc, 0, 0);
    523 
    524 	audio_attach_mi(&awacs_hw_if, sc, sc->sc_dev);
    525 
    526 	if (kthread_create(PRI_NONE, 0, NULL, awacs_thread, sc,
    527 	    &sc->sc_thread, "%s", "awacs") != 0) {
    528 		printf("awacs: unable to create event kthread");
    529 	}
    530 	pmf_device_register(sc->sc_dev, NULL, NULL);
    531 }
    532 
    533 static int
    534 awacs_setup_sgsmix(device_t cookie)
    535 {
    536 	struct awacs_softc *sc = device_private(cookie);
    537 #if NSGSMIX > 0
    538 	device_t dv;
    539 	deviter_t di;
    540 #endif
    541 
    542 	if (!sc->sc_have_perch)
    543 		return 0;
    544 #if NSGSMIX > 0
    545 	/* look for sgsmix */
    546 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    547 	     dv != NULL;
    548 	     dv = deviter_next(&di)) {
    549 		if (device_is_a(dv, "sgsmix")) {
    550 			sc->sc_sgsmix = dv;
    551 			break;
    552 		}
    553 	}
    554 	deviter_release(&di);
    555 	if (sc->sc_sgsmix == NULL)
    556 		return 0;
    557 
    558 	printf("%s: using %s\n", device_xname(sc->sc_dev),
    559 	    device_xname(sc->sc_sgsmix));
    560 
    561 	awacs_select_output(sc, sc->sc_output_mask);
    562 	awacs_set_volume(sc, sc->vol_l, sc->vol_r);
    563 	awacs_set_bass(sc, 128);
    564 	awacs_set_treble(sc, 128);
    565 	wakeup(&sc->sc_event);
    566 #endif
    567 	return 0;
    568 }
    569 
    570 
    571 static inline u_int
    572 awacs_read_reg(struct awacs_softc *sc, int reg)
    573 {
    574 	char *addr;
    575 
    576 	addr = sc->sc_reg;
    577 	return in32rb(addr + reg);
    578 }
    579 
    580 static inline void
    581 awacs_write_reg(struct awacs_softc *sc, int reg, int val)
    582 {
    583 	char *addr;
    584 
    585 	addr = sc->sc_reg;
    586 	out32rb(addr + reg, val);
    587 }
    588 
    589 static void
    590 awacs_write_codec(struct awacs_softc *sc, int value)
    591 {
    592 
    593 	do {
    594 		delay(100);
    595 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    596 
    597 	awacs_write_reg(sc, AWACS_CODEC_CTRL, value);
    598 
    599 	do {
    600 		delay(100);
    601 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    602 }
    603 
    604 static int
    605 awacs_intr(void *v)
    606 {
    607 	struct awacs_softc *sc;
    608 	struct dbdma_command *cmd;
    609 	int count;
    610 	int status;
    611 
    612 	sc = v;
    613 	cmd = sc->sc_odmacmd;
    614 	count = sc->sc_opages;
    615 	/* Fill used buffer(s). */
    616 	while (count-- > 0) {
    617 		/* if DBDMA_INT_ALWAYS */
    618 		if (in16rb(&cmd->d_command) & 0x30) {	/* XXX */
    619 			status = in16rb(&cmd->d_status);
    620 			cmd->d_status = 0;
    621 			if (status)	/* status == 0x8400 */
    622 				if (sc->sc_ointr)
    623 					(*sc->sc_ointr)(sc->sc_oarg);
    624 		}
    625 		cmd++;
    626 	}
    627 
    628 	return 1;
    629 }
    630 
    631 /*
    632  * Close function is called at splaudio().
    633  */
    634 static void
    635 awacs_close(void *h)
    636 {
    637 	struct awacs_softc *sc;
    638 
    639 	sc = h;
    640 	awacs_halt_output(sc);
    641 	awacs_halt_input(sc);
    642 
    643 	sc->sc_ointr = 0;
    644 	sc->sc_iintr = 0;
    645 }
    646 
    647 static int
    648 awacs_query_encoding(void *h, struct audio_encoding *ae)
    649 {
    650 	struct awacs_softc *sc;
    651 
    652 	sc = h;
    653 	ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
    654 
    655 	switch (ae->index) {
    656 	case 0:
    657 		strcpy(ae->name, AudioEslinear);
    658 		ae->encoding = AUDIO_ENCODING_SLINEAR;
    659 		ae->precision = 16;
    660 		ae->flags = 0;
    661 		return 0;
    662 	case 1:
    663 		strcpy(ae->name, AudioEslinear_be);
    664 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
    665 		ae->precision = 16;
    666 		ae->flags = 0;
    667 		return 0;
    668 	case 2:
    669 		strcpy(ae->name, AudioEslinear_le);
    670 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
    671 		ae->precision = 16;
    672 		if (sc->sc_flags & AWACS_CAP_BSWAP)
    673 			ae->flags = 0;
    674 		return 0;
    675 	case 3:
    676 		strcpy(ae->name, AudioEulinear_be);
    677 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
    678 		ae->precision = 16;
    679 		return 0;
    680 	case 4:
    681 		strcpy(ae->name, AudioEulinear_le);
    682 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
    683 		ae->precision = 16;
    684 		return 0;
    685 	case 5:
    686 		strcpy(ae->name, AudioEmulaw);
    687 		ae->encoding = AUDIO_ENCODING_ULAW;
    688 		ae->precision = 8;
    689 		return 0;
    690 	case 6:
    691 		strcpy(ae->name, AudioEalaw);
    692 		ae->encoding = AUDIO_ENCODING_ALAW;
    693 		ae->precision = 8;
    694 		return 0;
    695 	default:
    696 		return EINVAL;
    697 	}
    698 }
    699 
    700 static int
    701 awacs_set_params(void *h, int setmode, int usemode,
    702 		 audio_params_t *play, audio_params_t *rec,
    703 		 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    704 {
    705 	struct awacs_softc *sc;
    706 	audio_params_t *p;
    707 	stream_filter_list_t *fil;
    708 	int mode, i;
    709 
    710 	sc = h;
    711 	p = NULL;
    712 	/*
    713 	 * This device only has one clock, so make the sample rates match.
    714 	 */
    715 	if (play->sample_rate != rec->sample_rate &&
    716 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    717 		if (setmode == AUMODE_PLAY) {
    718 			rec->sample_rate = play->sample_rate;
    719 			setmode |= AUMODE_RECORD;
    720 		} else if (setmode == AUMODE_RECORD) {
    721 			play->sample_rate = rec->sample_rate;
    722 			setmode |= AUMODE_PLAY;
    723 		} else
    724 			return EINVAL;
    725 	}
    726 
    727 	for (mode = AUMODE_RECORD; mode != -1;
    728 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    729 		if ((setmode & mode) == 0)
    730 			continue;
    731 
    732 		p = mode == AUMODE_PLAY ? play : rec;
    733 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    734 		switch (p->sample_rate) {
    735 		case 48000:	/* aurateconv */
    736 		case 44100:
    737 		case 29400:
    738 		case 22050:
    739 		case 17640:
    740 		case 14700:
    741 		case 11025:
    742 		case 8820:
    743 		case 8000:	/* aurateconv */
    744 		case 7350:
    745 			break;
    746 		default:
    747 			return EINVAL;
    748 		}
    749 		awacs_write_reg(sc, AWACS_BYTE_SWAP, 0);
    750 		i = auconv_set_converter(sc->sc_formats, AWACS_NFORMATS,
    751 					 mode, p, true, fil);
    752 		if (i < 0)
    753 			return EINVAL;
    754 		if (i == AWACS_FORMATS_LE)
    755 			awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
    756 		if (fil->req_size > 0)
    757 			p = &fil->filters[0].param;
    758 		if (awacs_set_rate(sc, p))
    759 			return EINVAL;
    760 	}
    761 	return 0;
    762 }
    763 
    764 static int
    765 awacs_round_blocksize(void *h, int size, int mode, const audio_params_t *param)
    766 {
    767 
    768 	if (size < PAGE_SIZE)
    769 		size = PAGE_SIZE;
    770 	return size & ~PGOFSET;
    771 }
    772 
    773 static int
    774 awacs_halt_output(void *h)
    775 {
    776 	struct awacs_softc *sc;
    777 
    778 	sc = h;
    779 	dbdma_stop(sc->sc_odma);
    780 	dbdma_reset(sc->sc_odma);
    781 	return 0;
    782 }
    783 
    784 static int
    785 awacs_halt_input(void *h)
    786 {
    787 	struct awacs_softc *sc;
    788 
    789 	sc = h;
    790 	dbdma_stop(sc->sc_idma);
    791 	dbdma_reset(sc->sc_idma);
    792 	return 0;
    793 }
    794 
    795 static int
    796 awacs_getdev(void *h, struct audio_device *retp)
    797 {
    798 
    799 	*retp = awacs_device;
    800 	return 0;
    801 }
    802 
    803 enum {
    804 	AWACS_MONITOR_CLASS,
    805 	AWACS_OUTPUT_CLASS,
    806 	AWACS_RECORD_CLASS,
    807 	AWACS_OUTPUT_SELECT,
    808 	AWACS_VOL_MASTER,
    809 	AWACS_INPUT_SELECT,
    810 	AWACS_VOL_INPUT,
    811 	AWACS_VOL_MONITOR,
    812 	AWACS_BASS,
    813 	AWACS_TREBLE,
    814 	AWACS_ENUM_LAST
    815 };
    816 
    817 static int
    818 awacs_set_port(void *h, mixer_ctrl_t *mc)
    819 {
    820 	struct awacs_softc *sc;
    821 	int l, r;
    822 
    823 	DPRINTF("awacs_set_port dev = %d, type = %d\n", mc->dev, mc->type);
    824 	sc = h;
    825 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    826 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    827 
    828 	switch (mc->dev) {
    829 	case AWACS_OUTPUT_SELECT:
    830 		/* No change necessary? */
    831 		if (mc->un.mask == sc->sc_output_mask)
    832 			return 0;
    833 		awacs_select_output(sc, mc->un.mask);
    834 		return 0;
    835 
    836 	case AWACS_VOL_MASTER:
    837 		awacs_set_volume(sc, l, r);
    838 		return 0;
    839 
    840 	case AWACS_INPUT_SELECT:
    841 		/* no change necessary? */
    842 		if (mc->un.mask == sc->sc_record_source)
    843 			return 0;
    844 		switch (mc->un.mask) {
    845 		case 1 << 0: /* CD */
    846 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    847 			sc->sc_codecctl0 |= AWACS_INPUT_CD;
    848 			awacs_write_codec(sc, sc->sc_codecctl0);
    849 			break;
    850 		case 1 << 1: /* microphone */
    851 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    852 			sc->sc_codecctl0 |= AWACS_INPUT_MICROPHONE;
    853 			awacs_write_codec(sc, sc->sc_codecctl0);
    854 			break;
    855 		case 1 << 2: /* line in */
    856 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    857 			sc->sc_codecctl0 |= AWACS_INPUT_LINE;
    858 			awacs_write_codec(sc, sc->sc_codecctl0);
    859 			break;
    860 		default: /* invalid argument */
    861 			return EINVAL;
    862 		}
    863 		sc->sc_record_source = mc->un.mask;
    864 		return 0;
    865 
    866 	case AWACS_VOL_INPUT:
    867 		sc->sc_codecctl0 &= ~0xff;
    868 		sc->sc_codecctl0 |= (l & 0xf0) | (r >> 4);
    869 		awacs_write_codec(sc, sc->sc_codecctl0);
    870 		return 0;
    871 
    872 	case AWACS_VOL_MONITOR:
    873 		awacs_set_loopthrough_volume(sc, l, r);
    874 		return 0;
    875 
    876 #if NSGSMIX > 0
    877 	case AWACS_BASS:
    878 		awacs_set_bass(sc, l);
    879 		return 0;
    880 
    881 	case AWACS_TREBLE:
    882 		awacs_set_treble(sc, l);
    883 		return 0;
    884 #endif
    885 	}
    886 
    887 	return ENXIO;
    888 }
    889 
    890 static int
    891 awacs_get_port(void *h, mixer_ctrl_t *mc)
    892 {
    893 	struct awacs_softc *sc;
    894 	int l, r, vol;
    895 
    896 	sc = h;
    897 	switch (mc->dev) {
    898 	case AWACS_OUTPUT_SELECT:
    899 		mc->un.mask = sc->sc_output_mask;
    900 		return 0;
    901 
    902 	case AWACS_VOL_MASTER:
    903 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->vol_l;
    904 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->vol_r;
    905 		return 0;
    906 
    907 	case AWACS_INPUT_SELECT:
    908 		mc->un.mask = sc->sc_record_source;
    909 		return 0;
    910 
    911 	case AWACS_VOL_INPUT:
    912 		vol = sc->sc_codecctl0 & 0xff;
    913 		l = (vol & 0xf0);
    914 		r = (vol & 0x0f) << 4;
    915 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    916 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    917 		return 0;
    918 
    919 	case AWACS_VOL_MONITOR:
    920 		vol = sc->sc_codecctl5 & 0x3cf;
    921 		l = (vol & 0x3c0) >> 6;
    922 		r = vol & 0xf;
    923 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = (15 - l) << 4;
    924 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = (15 - r) << 4;
    925 		return 0;
    926 
    927 #if NSGSMIX > 0
    928 	case AWACS_BASS:
    929 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
    930 		return 0;
    931 
    932 	case AWACS_TREBLE:
    933 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
    934 		return 0;
    935 #endif
    936 
    937 	default:
    938 		return ENXIO;
    939 	}
    940 
    941 	return 0;
    942 }
    943 
    944 static int
    945 awacs_query_devinfo(void *h, mixer_devinfo_t *dip)
    946 {
    947 #if NSGSMIX > 0
    948 	struct awacs_softc *sc = h;
    949 #endif
    950 
    951 	switch (dip->index) {
    952 
    953 	case AWACS_OUTPUT_SELECT:
    954 		dip->mixer_class = AWACS_OUTPUT_CLASS;
    955 		strcpy(dip->label.name, AudioNoutput);
    956 		dip->type = AUDIO_MIXER_SET;
    957 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    958 		dip->un.s.num_mem = 2;
    959 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
    960 		dip->un.s.member[0].mask = 1 << 0;
    961 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
    962 		dip->un.s.member[1].mask = 1 << 1;
    963 		return 0;
    964 
    965 	case AWACS_VOL_MASTER:
    966 		dip->mixer_class = AWACS_OUTPUT_CLASS;
    967 		strcpy(dip->label.name, AudioNmaster);
    968 		dip->type = AUDIO_MIXER_VALUE;
    969 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    970 		dip->un.v.num_channels = 2;
    971 		dip->un.v.delta = 16;
    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_OUTPUT_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_OUTPUT_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