Home | History | Annotate | Line # | Download | only in dev
awacs.c revision 1.43.14.1
      1 /*	$NetBSD: awacs.c,v 1.43.14.1 2016/10/05 20:55:31 skrll 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.14.1 2016/10/05 20:55:31 skrll 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 	awacs_select_output(sc, sc->sc_output_mask);
    587 	awacs_set_volume(sc, sc->vol_l, sc->vol_r);
    588 	awacs_set_bass(sc, 128);
    589 	awacs_set_treble(sc, 128);
    590 	cv_signal(&sc->sc_event);
    591 #endif
    592 	return 0;
    593 }
    594 
    595 
    596 static inline u_int
    597 awacs_read_reg(struct awacs_softc *sc, int reg)
    598 {
    599 
    600 	return bus_space_read_4(sc->sc_tag, sc->sc_regh, reg);
    601 }
    602 
    603 static inline void
    604 awacs_write_reg(struct awacs_softc *sc, int reg, int val)
    605 {
    606 
    607 	bus_space_write_4(sc->sc_tag, sc->sc_regh, reg, val);
    608 }
    609 
    610 static void
    611 awacs_write_codec(struct awacs_softc *sc, int value)
    612 {
    613 
    614 	do {
    615 		delay(100);
    616 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    617 
    618 	awacs_write_reg(sc, AWACS_CODEC_CTRL, value);
    619 
    620 	do {
    621 		delay(100);
    622 	} while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
    623 }
    624 
    625 static int
    626 awacs_intr(void *v)
    627 {
    628 	struct awacs_softc *sc;
    629 	struct dbdma_command *cmd;
    630 	int count;
    631 	int status;
    632 
    633 	sc = v;
    634 	mutex_spin_enter(&sc->sc_intr_lock);
    635 	cmd = sc->sc_odmacmd;
    636 	count = sc->sc_opages;
    637 	/* Fill used buffer(s). */
    638 	while (count-- > 0) {
    639 		/* if DBDMA_INT_ALWAYS */
    640 		if (in16rb(&cmd->d_command) & 0x30) {	/* XXX */
    641 			status = in16rb(&cmd->d_status);
    642 			cmd->d_status = 0;
    643 			if (status)	/* status == 0x8400 */
    644 				if (sc->sc_ointr)
    645 					(*sc->sc_ointr)(sc->sc_oarg);
    646 		}
    647 		cmd++;
    648 	}
    649 	mutex_spin_exit(&sc->sc_intr_lock);
    650 
    651 	return 1;
    652 }
    653 
    654 /*
    655  * Close function is called at splaudio().
    656  */
    657 static void
    658 awacs_close(void *h)
    659 {
    660 	struct awacs_softc *sc;
    661 
    662 	sc = h;
    663 	awacs_halt_output(sc);
    664 	awacs_halt_input(sc);
    665 
    666 	sc->sc_ointr = 0;
    667 	sc->sc_iintr = 0;
    668 }
    669 
    670 static int
    671 awacs_query_encoding(void *h, struct audio_encoding *ae)
    672 {
    673 	struct awacs_softc *sc;
    674 
    675 	sc = h;
    676 	ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
    677 
    678 	switch (ae->index) {
    679 	case 0:
    680 		strcpy(ae->name, AudioEslinear);
    681 		ae->encoding = AUDIO_ENCODING_SLINEAR;
    682 		ae->precision = 16;
    683 		ae->flags = 0;
    684 		return 0;
    685 	case 1:
    686 		strcpy(ae->name, AudioEslinear_be);
    687 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
    688 		ae->precision = 16;
    689 		ae->flags = 0;
    690 		return 0;
    691 	case 2:
    692 		strcpy(ae->name, AudioEslinear_le);
    693 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
    694 		ae->precision = 16;
    695 		if (sc->sc_flags & AWACS_CAP_BSWAP)
    696 			ae->flags = 0;
    697 		return 0;
    698 	case 3:
    699 		strcpy(ae->name, AudioEulinear_be);
    700 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
    701 		ae->precision = 16;
    702 		return 0;
    703 	case 4:
    704 		strcpy(ae->name, AudioEulinear_le);
    705 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
    706 		ae->precision = 16;
    707 		return 0;
    708 	case 5:
    709 		strcpy(ae->name, AudioEmulaw);
    710 		ae->encoding = AUDIO_ENCODING_ULAW;
    711 		ae->precision = 8;
    712 		return 0;
    713 	case 6:
    714 		strcpy(ae->name, AudioEalaw);
    715 		ae->encoding = AUDIO_ENCODING_ALAW;
    716 		ae->precision = 8;
    717 		return 0;
    718 	default:
    719 		return EINVAL;
    720 	}
    721 }
    722 
    723 static int
    724 awacs_set_params(void *h, int setmode, int usemode,
    725 		 audio_params_t *play, audio_params_t *rec,
    726 		 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    727 {
    728 	struct awacs_softc *sc;
    729 	audio_params_t *p;
    730 	stream_filter_list_t *fil;
    731 	int mode, i;
    732 
    733 	sc = h;
    734 	p = NULL;
    735 	/*
    736 	 * This device only has one clock, so make the sample rates match.
    737 	 */
    738 	if (play->sample_rate != rec->sample_rate &&
    739 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    740 		if (setmode == AUMODE_PLAY) {
    741 			rec->sample_rate = play->sample_rate;
    742 			setmode |= AUMODE_RECORD;
    743 		} else if (setmode == AUMODE_RECORD) {
    744 			play->sample_rate = rec->sample_rate;
    745 			setmode |= AUMODE_PLAY;
    746 		} else
    747 			return EINVAL;
    748 	}
    749 
    750 	for (mode = AUMODE_RECORD; mode != -1;
    751 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    752 		if ((setmode & mode) == 0)
    753 			continue;
    754 
    755 		p = mode == AUMODE_PLAY ? play : rec;
    756 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    757 		switch (p->sample_rate) {
    758 		case 48000:	/* aurateconv */
    759 		case 44100:
    760 		case 29400:
    761 		case 22050:
    762 		case 17640:
    763 		case 14700:
    764 		case 11025:
    765 		case 8820:
    766 		case 8000:	/* aurateconv */
    767 		case 7350:
    768 			break;
    769 		default:
    770 			return EINVAL;
    771 		}
    772 		awacs_write_reg(sc, AWACS_BYTE_SWAP, 0);
    773 		i = auconv_set_converter(sc->sc_formats, AWACS_NFORMATS,
    774 					 mode, p, true, fil);
    775 		if (i < 0)
    776 			return EINVAL;
    777 		if (i == AWACS_FORMATS_LE)
    778 			awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
    779 		if (fil->req_size > 0)
    780 			p = &fil->filters[0].param;
    781 		if (awacs_set_rate(sc, p))
    782 			return EINVAL;
    783 	}
    784 	return 0;
    785 }
    786 
    787 static int
    788 awacs_round_blocksize(void *h, int size, int mode, const audio_params_t *param)
    789 {
    790 
    791 	if (size < PAGE_SIZE)
    792 		size = PAGE_SIZE;
    793 	return size & ~PGOFSET;
    794 }
    795 
    796 static int
    797 awacs_halt_output(void *h)
    798 {
    799 	struct awacs_softc *sc;
    800 
    801 	sc = h;
    802 	dbdma_stop(sc->sc_odma);
    803 	dbdma_reset(sc->sc_odma);
    804 	return 0;
    805 }
    806 
    807 static int
    808 awacs_halt_input(void *h)
    809 {
    810 	struct awacs_softc *sc;
    811 
    812 	sc = h;
    813 	dbdma_stop(sc->sc_idma);
    814 	dbdma_reset(sc->sc_idma);
    815 	return 0;
    816 }
    817 
    818 static int
    819 awacs_getdev(void *h, struct audio_device *retp)
    820 {
    821 
    822 	*retp = awacs_device;
    823 	return 0;
    824 }
    825 
    826 enum {
    827 	AWACS_MONITOR_CLASS,
    828 	AWACS_OUTPUT_CLASS,
    829 	AWACS_RECORD_CLASS,
    830 	AWACS_OUTPUT_SELECT,
    831 	AWACS_VOL_MASTER,
    832 	AWACS_INPUT_SELECT,
    833 	AWACS_VOL_INPUT,
    834 	AWACS_VOL_MONITOR,
    835 	AWACS_BASS,
    836 	AWACS_TREBLE,
    837 	AWACS_ENUM_LAST
    838 };
    839 
    840 static int
    841 awacs_set_port(void *h, mixer_ctrl_t *mc)
    842 {
    843 	struct awacs_softc *sc;
    844 	int l, r;
    845 
    846 	DPRINTF("awacs_set_port dev = %d, type = %d\n", mc->dev, mc->type);
    847 	sc = h;
    848 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    849 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    850 
    851 	switch (mc->dev) {
    852 	case AWACS_OUTPUT_SELECT:
    853 		/* No change necessary? */
    854 		if (mc->un.mask == sc->sc_output_mask)
    855 			return 0;
    856 		awacs_select_output(sc, mc->un.mask);
    857 		return 0;
    858 
    859 	case AWACS_VOL_MASTER:
    860 		awacs_set_volume(sc, l, r);
    861 		return 0;
    862 
    863 	case AWACS_INPUT_SELECT:
    864 		/* no change necessary? */
    865 		if (mc->un.mask == sc->sc_record_source)
    866 			return 0;
    867 		switch (mc->un.mask) {
    868 		case 1 << 0: /* CD */
    869 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    870 			sc->sc_codecctl0 |= AWACS_INPUT_CD;
    871 			awacs_write_codec(sc, sc->sc_codecctl0);
    872 			break;
    873 		case 1 << 1: /* microphone */
    874 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    875 			sc->sc_codecctl0 |= AWACS_INPUT_MICROPHONE;
    876 			awacs_write_codec(sc, sc->sc_codecctl0);
    877 			break;
    878 		case 1 << 2: /* line in */
    879 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
    880 			sc->sc_codecctl0 |= AWACS_INPUT_LINE;
    881 			awacs_write_codec(sc, sc->sc_codecctl0);
    882 			break;
    883 		default: /* invalid argument */
    884 			return EINVAL;
    885 		}
    886 		sc->sc_record_source = mc->un.mask;
    887 		return 0;
    888 
    889 	case AWACS_VOL_INPUT:
    890 		sc->sc_codecctl0 &= ~0xff;
    891 		sc->sc_codecctl0 |= (l & 0xf0) | (r >> 4);
    892 		awacs_write_codec(sc, sc->sc_codecctl0);
    893 		return 0;
    894 
    895 	case AWACS_VOL_MONITOR:
    896 		awacs_set_loopthrough_volume(sc, l, r);
    897 		return 0;
    898 
    899 #if NSGSMIX > 0
    900 	case AWACS_BASS:
    901 		awacs_set_bass(sc, l);
    902 		return 0;
    903 
    904 	case AWACS_TREBLE:
    905 		awacs_set_treble(sc, l);
    906 		return 0;
    907 #endif
    908 	}
    909 
    910 	return ENXIO;
    911 }
    912 
    913 static int
    914 awacs_get_port(void *h, mixer_ctrl_t *mc)
    915 {
    916 	struct awacs_softc *sc;
    917 	int l, r, vol;
    918 
    919 	sc = h;
    920 	switch (mc->dev) {
    921 	case AWACS_OUTPUT_SELECT:
    922 		mc->un.mask = sc->sc_output_mask;
    923 		return 0;
    924 
    925 	case AWACS_VOL_MASTER:
    926 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->vol_l;
    927 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->vol_r;
    928 		return 0;
    929 
    930 	case AWACS_INPUT_SELECT:
    931 		mc->un.mask = sc->sc_record_source;
    932 		return 0;
    933 
    934 	case AWACS_VOL_INPUT:
    935 		vol = sc->sc_codecctl0 & 0xff;
    936 		l = (vol & 0xf0);
    937 		r = (vol & 0x0f) << 4;
    938 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    939 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    940 		return 0;
    941 
    942 	case AWACS_VOL_MONITOR:
    943 		vol = sc->sc_codecctl5 & 0x3cf;
    944 		l = (vol & 0x3c0) >> 6;
    945 		r = vol & 0xf;
    946 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = (15 - l) << 4;
    947 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = (15 - r) << 4;
    948 		return 0;
    949 
    950 #if NSGSMIX > 0
    951 	case AWACS_BASS:
    952 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
    953 		return 0;
    954 
    955 	case AWACS_TREBLE:
    956 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
    957 		return 0;
    958 #endif
    959 
    960 	default:
    961 		return ENXIO;
    962 	}
    963 
    964 	return 0;
    965 }
    966 
    967 static int
    968 awacs_query_devinfo(void *h, mixer_devinfo_t *dip)
    969 {
    970 #if NSGSMIX > 0
    971 	struct awacs_softc *sc = h;
    972 #endif
    973 
    974 	switch (dip->index) {
    975 
    976 	case AWACS_OUTPUT_SELECT:
    977 		dip->mixer_class = AWACS_OUTPUT_CLASS;
    978 		strcpy(dip->label.name, AudioNoutput);
    979 		dip->type = AUDIO_MIXER_SET;
    980 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    981 		dip->un.s.num_mem = 2;
    982 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
    983 		dip->un.s.member[0].mask = 1 << 0;
    984 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
    985 		dip->un.s.member[1].mask = 1 << 1;
    986 		return 0;
    987 
    988 	case AWACS_VOL_MASTER:
    989 		dip->mixer_class = AWACS_OUTPUT_CLASS;
    990 		strcpy(dip->label.name, AudioNmaster);
    991 		dip->type = AUDIO_MIXER_VALUE;
    992 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    993 		dip->un.v.num_channels = 2;
    994 		dip->un.v.delta = 16;
    995 		strcpy(dip->un.v.units.name, AudioNvolume);
    996 		return 0;
    997 
    998 	case AWACS_VOL_MONITOR:
    999 		dip->mixer_class = AWACS_MONITOR_CLASS;
   1000 		strcpy(dip->label.name, AudioNmonitor);
   1001 		dip->type = AUDIO_MIXER_VALUE;
   1002 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1003 		dip->un.v.num_channels = 2;
   1004 		strcpy(dip->un.v.units.name, AudioNvolume);
   1005 		return 0;
   1006 
   1007 #if NSGSMIX > 0
   1008 	case AWACS_BASS:
   1009 		if (sc->sc_sgsmix == NULL)
   1010 			return ENXIO;
   1011 		dip->mixer_class = AWACS_OUTPUT_CLASS;
   1012 		strcpy(dip->label.name, AudioNbass);
   1013 		dip->type = AUDIO_MIXER_VALUE;
   1014 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1015 		dip->un.v.num_channels = 1;
   1016 		strcpy(dip->un.v.units.name, AudioNbass);
   1017 		return 0;
   1018 
   1019 	case AWACS_TREBLE:
   1020 		if (sc->sc_sgsmix == NULL)
   1021 			return ENXIO;
   1022 		dip->mixer_class = AWACS_OUTPUT_CLASS;
   1023 		strcpy(dip->label.name, AudioNtreble);
   1024 		dip->type = AUDIO_MIXER_VALUE;
   1025 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1026 		dip->un.v.num_channels = 1;
   1027 		strcpy(dip->un.v.units.name, AudioNtreble);
   1028 		return 0;
   1029 #endif
   1030 
   1031 	case AWACS_INPUT_SELECT:
   1032 		dip->mixer_class = AWACS_RECORD_CLASS;
   1033 		strcpy(dip->label.name, AudioNsource);
   1034 		dip->type = AUDIO_MIXER_SET;
   1035 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1036 		dip->un.s.num_mem = 3;
   1037 		strcpy(dip->un.s.member[0].label.name, AudioNcd);
   1038 		dip->un.s.member[0].mask = 1 << 0;
   1039 		strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
   1040 		dip->un.s.member[1].mask = 1 << 1;
   1041 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1042 		dip->un.s.member[2].mask = 1 << 2;
   1043 		return 0;
   1044 
   1045 	case AWACS_VOL_INPUT:
   1046 		dip->mixer_class = AWACS_RECORD_CLASS;
   1047 		strcpy(dip->label.name, AudioNrecord);
   1048 		dip->type = AUDIO_MIXER_VALUE;
   1049 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1050 		dip->un.v.num_channels = 2;
   1051 		strcpy(dip->un.v.units.name, AudioNvolume);
   1052 		return 0;
   1053 
   1054 	case AWACS_MONITOR_CLASS:
   1055 		dip->mixer_class = AWACS_MONITOR_CLASS;
   1056 		strcpy(dip->label.name, AudioCmonitor);
   1057 		dip->type = AUDIO_MIXER_CLASS;
   1058 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1059 		return 0;
   1060 
   1061 	case AWACS_OUTPUT_CLASS:
   1062 		dip->mixer_class = AWACS_OUTPUT_CLASS;
   1063 		strcpy(dip->label.name, AudioCoutputs);
   1064 		dip->type = AUDIO_MIXER_CLASS;
   1065 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1066 		return 0;
   1067 
   1068 	case AWACS_RECORD_CLASS:
   1069 		dip->mixer_class = AWACS_RECORD_CLASS;
   1070 		strcpy(dip->label.name, AudioCrecord);
   1071 		dip->type = AUDIO_MIXER_CLASS;
   1072 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1073 		return 0;
   1074 	}
   1075 
   1076 	return ENXIO;
   1077 }
   1078 
   1079 static size_t
   1080 awacs_round_buffersize(void *h, int dir, size_t size)
   1081 {
   1082 
   1083 	if (size > 65536)
   1084 		size = 65536;
   1085 	return size;
   1086 }
   1087 
   1088 static paddr_t
   1089 awacs_mappage(void *h, void *mem, off_t off, int prot)
   1090 {
   1091 
   1092 	if (off < 0)
   1093 		return -1;
   1094 	return -1;	/* XXX */
   1095 }
   1096 
   1097 static int
   1098 awacs_get_props(void *h)
   1099 {
   1100 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
   1101 }
   1102 
   1103 static int
   1104 awacs_trigger_output(void *h, void *start, void *end, int bsize,
   1105 		     void (*intr)(void *), void *arg,
   1106 		     const audio_params_t *param)
   1107 {
   1108 	struct awacs_softc *sc;
   1109 	struct dbdma_command *cmd;
   1110 	vaddr_t va;
   1111 	int i, len, intmode;
   1112 
   1113 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
   1114 	sc = h;
   1115 	cmd = sc->sc_odmacmd;
   1116 	sc->sc_ointr = intr;
   1117 	sc->sc_oarg = arg;
   1118 	sc->sc_opages = ((char *)end - (char *)start) / PAGE_SIZE;
   1119 
   1120 #ifdef DIAGNOSTIC
   1121 	if (sc->sc_opages > 16)
   1122 		panic("awacs_trigger_output");
   1123 #endif
   1124 
   1125 	va = (vaddr_t)start;
   1126 	len = 0;
   1127 	for (i = sc->sc_opages; i > 0; i--) {
   1128 		len += PAGE_SIZE;
   1129 		if (len < bsize)
   1130 			intmode = DBDMA_INT_NEVER;
   1131 		else {
   1132 			len = 0;
   1133 			intmode = DBDMA_INT_ALWAYS;
   1134 		}
   1135 
   1136 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, PAGE_SIZE, vtophys(va),
   1137 			intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1138 		va += PAGE_SIZE;
   1139 		cmd++;
   1140 	}
   1141 
   1142 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
   1143 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
   1144 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
   1145 
   1146 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
   1147 
   1148 	return 0;
   1149 }
   1150 
   1151 static int
   1152 awacs_trigger_input(void *h, void *start, void *end, int bsize,
   1153 		    void (*intr)(void *), void *arg,
   1154 		    const audio_params_t *param)
   1155 {
   1156 
   1157 	DPRINTF("awacs_trigger_input called\n");
   1158 	return 1;
   1159 }
   1160 
   1161 static void
   1162 awacs_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
   1163 {
   1164 	struct awacs_softc *sc = opaque;
   1165 
   1166 	*intr = &sc->sc_intr_lock;
   1167 	*thread = &sc->sc_lock;
   1168 }
   1169 
   1170 static void
   1171 awacs_select_output(struct awacs_softc *sc, int mask)
   1172 {
   1173 
   1174 #if NSGSMIX > 0
   1175 	if (sc->sc_sgsmix) {
   1176 		if (mask & OUTPUT_HEADPHONES) {
   1177 			/* mute speakers */
   1178 			sgsmix_set_speaker_vol(sc->sc_sgsmix, 0, 0);
   1179 			sgsmix_set_headphone_vol(sc->sc_sgsmix,
   1180 			    sc->vol_l, sc->vol_r);
   1181 		}
   1182 		if (mask & OUTPUT_SPEAKER) {
   1183 			/* mute headphones */
   1184 			sgsmix_set_speaker_vol(sc->sc_sgsmix,
   1185 			    sc->vol_l, sc->vol_r);
   1186 			sgsmix_set_headphone_vol(sc->sc_sgsmix, 0, 0);
   1187 		}
   1188 	} else {
   1189 #endif
   1190 	sc->sc_codecctl1 |= AWACS_MUTE_SPEAKER | AWACS_MUTE_HEADPHONE;
   1191 	if ((sc->vol_l > 0) || (sc->vol_r > 0)) {
   1192 		if (mask & OUTPUT_SPEAKER)
   1193 			sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
   1194 		if (mask & OUTPUT_HEADPHONES)
   1195 			sc->sc_codecctl1 &= ~AWACS_MUTE_HEADPHONE;
   1196 	}
   1197 	awacs_write_codec(sc, sc->sc_codecctl1);
   1198 #if NSGSMIX > 0
   1199 	}
   1200 #endif
   1201 	sc->sc_output_mask = mask;
   1202 }
   1203 
   1204 static void
   1205 awacs_set_speaker_volume(struct awacs_softc *sc, int left, int right)
   1206 {
   1207 
   1208 #if NSGSMIX > 0
   1209 	if (sc->sc_sgsmix) {
   1210 		if (sc->sc_output_mask & OUTPUT_SPEAKER)
   1211 			sgsmix_set_speaker_vol(sc->sc_sgsmix, left, right);
   1212 	} else
   1213 #endif
   1214 	{
   1215 		int lval;
   1216 		int rval;
   1217 		uint32_t codecctl = sc->sc_codecctl1;
   1218 
   1219 		lval = 15 - ((left  & 0xf0) >> 4);
   1220 		rval = 15 - ((right & 0xf0) >> 4);
   1221 		DPRINTF("speaker_volume %d %d\n", lval, rval);
   1222 
   1223 		sc->sc_codecctl4 &= ~0x3cf;
   1224 		sc->sc_codecctl4 |= (lval << 6) | rval;
   1225 		awacs_write_codec(sc, sc->sc_codecctl4);
   1226 		if ((left == 0) && (right == 0)) {
   1227 			/*
   1228 			 * max. attenuation doesn't mean silence so we need to
   1229 			 * mute the output channel here
   1230 			 */
   1231 			codecctl |= AWACS_MUTE_SPEAKER;
   1232 		} else if (sc->sc_output_mask & OUTPUT_SPEAKER) {
   1233 			codecctl &= ~AWACS_MUTE_SPEAKER;
   1234 		}
   1235 
   1236 		if (codecctl != sc->sc_codecctl1) {
   1237 
   1238 			sc->sc_codecctl1 = codecctl;
   1239 			awacs_write_codec(sc, sc->sc_codecctl1);
   1240 		}
   1241 	}
   1242 }
   1243 
   1244 static void
   1245 awacs_set_ext_volume(struct awacs_softc *sc, int left, int right)
   1246 {
   1247 
   1248 #if NSGSMIX > 0
   1249 	if (sc->sc_sgsmix) {
   1250 		if (sc->sc_output_mask & OUTPUT_HEADPHONES)
   1251 			sgsmix_set_headphone_vol(sc->sc_sgsmix, left, right);
   1252 	} else
   1253 #endif
   1254 	{
   1255 		int lval;
   1256 		int rval;
   1257 		uint32_t codecctl = sc->sc_codecctl1;
   1258 
   1259 		lval = 15 - ((left  & 0xf0) >> 4);
   1260 		rval = 15 - ((right & 0xf0) >> 4);
   1261 		DPRINTF("ext_volume %d %d\n", lval, rval);
   1262 
   1263 		sc->sc_codecctl2 &= ~0x3cf;
   1264 		sc->sc_codecctl2 |= (lval << 6) | rval;
   1265 		awacs_write_codec(sc, sc->sc_codecctl2);
   1266 
   1267 		if ((left == 0) && (right == 0)) {
   1268 			/*
   1269 			 * max. attenuation doesn't mean silence so we need to
   1270 			 * mute the output channel here
   1271 			 */
   1272 			codecctl |= AWACS_MUTE_HEADPHONE;
   1273 		} else if (sc->sc_output_mask & OUTPUT_HEADPHONES) {
   1274 
   1275 			codecctl &= ~AWACS_MUTE_HEADPHONE;
   1276 		}
   1277 
   1278 		if (codecctl != sc->sc_codecctl1) {
   1279 
   1280 			sc->sc_codecctl1 = codecctl;
   1281 			awacs_write_codec(sc, sc->sc_codecctl1);
   1282 		}
   1283 	}
   1284 }
   1285 
   1286 void
   1287 awacs_set_volume(struct awacs_softc *sc, int left, int right)
   1288 {
   1289 
   1290 	awacs_set_ext_volume(sc, left, right);
   1291 	awacs_set_speaker_volume(sc, left, right);
   1292 
   1293 	sc->vol_l = left;
   1294 	sc->vol_r = right;
   1295 }
   1296 
   1297 #if NSGSMIX > 0
   1298 static void
   1299 awacs_set_bass(struct awacs_softc *sc, int bass)
   1300 {
   1301 
   1302 	if (sc->sc_bass == bass)
   1303 		return;
   1304 
   1305 	sc->sc_bass = bass;
   1306 	if (sc->sc_sgsmix)
   1307 		sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass,
   1308 		    sc->sc_treble);
   1309 }
   1310 
   1311 static void
   1312 awacs_set_treble(struct awacs_softc *sc, int treble)
   1313 {
   1314 
   1315 	if (sc->sc_treble == treble)
   1316 		return;
   1317 
   1318 	sc->sc_treble = treble;
   1319 	if (sc->sc_sgsmix)
   1320 		sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass,
   1321 		    sc->sc_treble);
   1322 }
   1323 #endif
   1324 
   1325 void
   1326 awacs_set_loopthrough_volume(struct awacs_softc *sc, int left, int right)
   1327 {
   1328 	int lval;
   1329 	int rval;
   1330 
   1331 	lval = 15 - ((left  & 0xff) >> 4);
   1332 	rval = 15 - ((right & 0xff) >> 4);
   1333 	DPRINTF("loopthrough_volume %d %d\n", lval, rval);
   1334 
   1335 	sc->sc_codecctl5 &= ~0x3cf;
   1336 	sc->sc_codecctl5 |= (lval << 6) | rval;
   1337 	awacs_write_codec(sc, sc->sc_codecctl5);
   1338 }
   1339 
   1340 int
   1341 awacs_set_rate(struct awacs_softc *sc, const audio_params_t *p)
   1342 {
   1343 	int c;
   1344 
   1345 	switch (p->sample_rate) {
   1346 	case 44100:
   1347 		c = AWACS_RATE_44100;
   1348 		break;
   1349 	case 29400:
   1350 		c = AWACS_RATE_29400;
   1351 		break;
   1352 	case 22050:
   1353 		c = AWACS_RATE_22050;
   1354 		break;
   1355 	case 17640:
   1356 		c = AWACS_RATE_17640;
   1357 		break;
   1358 	case 14700:
   1359 		c = AWACS_RATE_14700;
   1360 		break;
   1361 	case 11025:
   1362 		c = AWACS_RATE_11025;
   1363 		break;
   1364 	case 8820:
   1365 		c = AWACS_RATE_8820;
   1366 		break;
   1367 	case 7350:
   1368 		c = AWACS_RATE_7350;
   1369 		break;
   1370 	default:
   1371 		return -1;
   1372 	}
   1373 
   1374 	sc->sc_soundctl &= ~AWACS_RATE_MASK;
   1375 	sc->sc_soundctl |= c;
   1376 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
   1377 
   1378 	return 0;
   1379 }
   1380 
   1381 static int
   1382 awacs_check_headphones(struct awacs_softc *sc)
   1383 {
   1384 	uint32_t reg;
   1385 	reg = awacs_read_reg(sc, AWACS_CODEC_STATUS);
   1386 	DPRINTF("%s: codec status reg %08x\n", device_xname(sc->sc_dev), reg);
   1387 	return ((reg & sc->sc_headphones_mask) == sc->sc_headphones_in);
   1388 }
   1389 
   1390 static int
   1391 awacs_status_intr(void *cookie)
   1392 {
   1393 	struct awacs_softc *sc = cookie;
   1394 	int mask;
   1395 
   1396 	mutex_spin_enter(&sc->sc_intr_lock);
   1397 	mask = awacs_check_headphones(sc) ? OUTPUT_HEADPHONES : OUTPUT_SPEAKER;
   1398 	if (mask != sc->sc_output_mask) {
   1399 
   1400 		sc->sc_output_wanted = mask;
   1401 		cv_signal(&sc->sc_event);
   1402 	}
   1403 	/* clear the interrupt */
   1404 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl | AWACS_PORTCHG);
   1405 	mutex_spin_exit(&sc->sc_intr_lock);
   1406 	return 1;
   1407 }
   1408 
   1409 static void
   1410 awacs_thread(void *cookie)
   1411 {
   1412 	struct awacs_softc *sc = cookie;
   1413 
   1414 	mutex_enter(&sc->sc_intr_lock);
   1415 	while (1) {
   1416 		cv_timedwait(&sc->sc_event, &sc->sc_intr_lock, hz);
   1417 		if (sc->sc_output_wanted == sc->sc_output_mask)
   1418 			continue;
   1419 
   1420 		awacs_select_output(sc, sc->sc_output_wanted);
   1421 		DPRINTF("%s: switching to %s\n", device_xname(sc->sc_dev),
   1422 		    (sc->sc_output_wanted & OUTPUT_SPEAKER) ?
   1423 		    "speaker" : "headphones");
   1424 	}
   1425 }
   1426