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