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