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