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