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