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