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