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