xref: /netbsd-src/sys/arch/macppc/dev/awacs.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: awacs.c,v 1.18 2003/05/03 18:10:51 wiz 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/param.h>
30 #include <sys/audioio.h>
31 #include <sys/device.h>
32 #include <sys/malloc.h>
33 #include <sys/systm.h>
34 
35 #include <dev/auconv.h>
36 #include <dev/audio_if.h>
37 #include <dev/mulaw.h>
38 
39 #include <uvm/uvm_extern.h>
40 
41 #include <machine/autoconf.h>
42 #include <machine/pio.h>
43 
44 #include <dev/ofw/openfirm.h>
45 #include <macppc/dev/dbdma.h>
46 
47 #ifdef AWACS_DEBUG
48 # define DPRINTF printf
49 #else
50 # define DPRINTF while (0) printf
51 #endif
52 
53 /* sc_flags values */
54 #define AWACS_CAP_BSWAP		0x0001
55 
56 struct awacs_softc {
57 	struct device sc_dev;
58 	int sc_flags;
59 
60 	void (*sc_ointr)(void *);	/* DMA completion intr handler */
61 	void *sc_oarg;			/* arg for sc_ointr() */
62 	int sc_opages;			/* # of output pages */
63 
64 	void (*sc_iintr)(void *);	/* DMA completion intr handler */
65 	void *sc_iarg;			/* arg for sc_iintr() */
66 
67 	u_int sc_record_source;		/* recording source mask */
68 	u_int sc_output_mask;		/* output source mask */
69 
70 	char *sc_reg;
71 	u_int sc_codecctl0;
72 	u_int sc_codecctl1;
73 	u_int sc_codecctl2;
74 	u_int sc_codecctl4;
75 	u_int sc_soundctl;
76 
77 	struct dbdma_regmap *sc_odma;
78 	struct dbdma_regmap *sc_idma;
79 	struct dbdma_command *sc_odmacmd;
80 	struct dbdma_command *sc_idmacmd;
81 };
82 
83 int awacs_match(struct device *, struct cfdata *, void *);
84 void awacs_attach(struct device *, struct device *, void *);
85 int awacs_intr(void *);
86 
87 int awacs_open(void *, int);
88 void awacs_close(void *);
89 int awacs_query_encoding(void *, struct audio_encoding *);
90 int awacs_set_params(void *, int, int, struct audio_params *,
91 			 struct audio_params *);
92 int awacs_round_blocksize(void *, int);
93 int awacs_trigger_output(void *, void *, void *, int, void (*)(void *),
94 			     void *, struct audio_params *);
95 int awacs_trigger_input(void *, void *, void *, int, void (*)(void *),
96 			    void *, struct audio_params *);
97 int awacs_halt_output(void *);
98 int awacs_halt_input(void *);
99 int awacs_getdev(void *, struct audio_device *);
100 int awacs_set_port(void *, mixer_ctrl_t *);
101 int awacs_get_port(void *, mixer_ctrl_t *);
102 int awacs_query_devinfo(void *, mixer_devinfo_t *);
103 size_t awacs_round_buffersize(void *, int, size_t);
104 paddr_t awacs_mappage(void *, void *, off_t, int);
105 int awacs_get_props(void *);
106 
107 static inline u_int awacs_read_reg(struct awacs_softc *, int);
108 static inline void awacs_write_reg(struct awacs_softc *, int, int);
109 void awacs_write_codec(struct awacs_softc *, int);
110 void awacs_set_speaker_volume(struct awacs_softc *, int, int);
111 void awacs_set_ext_volume(struct awacs_softc *, int, int);
112 int awacs_set_rate(struct awacs_softc *, struct audio_params *);
113 
114 static void mono16_to_stereo16(void *, u_char *, int);
115 static void swap_bytes_mono16_to_stereo16(void *, u_char *, int);
116 
117 CFATTACH_DECL(awacs, sizeof(struct awacs_softc),
118     awacs_match, awacs_attach, NULL, NULL);
119 
120 struct audio_hw_if awacs_hw_if = {
121 	awacs_open,
122 	awacs_close,
123 	NULL,
124 	awacs_query_encoding,
125 	awacs_set_params,
126 	awacs_round_blocksize,
127 	NULL,
128 	NULL,
129 	NULL,
130 	NULL,
131 	NULL,
132 	awacs_halt_output,
133 	awacs_halt_input,
134 	NULL,
135 	awacs_getdev,
136 	NULL,
137 	awacs_set_port,
138 	awacs_get_port,
139 	awacs_query_devinfo,
140 	NULL,
141 	NULL,
142 	awacs_round_buffersize,
143 	awacs_mappage,
144 	awacs_get_props,
145 	awacs_trigger_output,
146 	awacs_trigger_input,
147 	NULL,
148 };
149 
150 struct audio_device awacs_device = {
151 	"AWACS",
152 	"",
153 	"awacs"
154 };
155 
156 /* register offset */
157 #define AWACS_SOUND_CTRL	0x00
158 #define AWACS_CODEC_CTRL	0x10
159 #define AWACS_CODEC_STATUS	0x20
160 #define AWACS_CLIP_COUNT	0x30
161 #define AWACS_BYTE_SWAP		0x40
162 
163 /* sound control */
164 #define AWACS_INPUT_SUBFRAME0	0x00000001
165 #define AWACS_INPUT_SUBFRAME1	0x00000002
166 #define AWACS_INPUT_SUBFRAME2	0x00000004
167 #define AWACS_INPUT_SUBFRAME3	0x00000008
168 
169 #define AWACS_OUTPUT_SUBFRAME0	0x00000010
170 #define AWACS_OUTPUT_SUBFRAME1	0x00000020
171 #define AWACS_OUTPUT_SUBFRAME2	0x00000040
172 #define AWACS_OUTPUT_SUBFRAME3	0x00000080
173 
174 #define AWACS_RATE_44100	0x00000000
175 #define AWACS_RATE_29400	0x00000100
176 #define AWACS_RATE_22050	0x00000200
177 #define AWACS_RATE_17640	0x00000300
178 #define AWACS_RATE_14700	0x00000400
179 #define AWACS_RATE_11025	0x00000500
180 #define AWACS_RATE_8820		0x00000600
181 #define AWACS_RATE_7350		0x00000700
182 #define AWACS_RATE_MASK		0x00000700
183 
184 /* codec control */
185 #define AWACS_CODEC_ADDR0	0x00000000
186 #define AWACS_CODEC_ADDR1	0x00001000
187 #define AWACS_CODEC_ADDR2	0x00002000
188 #define AWACS_CODEC_ADDR4	0x00004000
189 #define AWACS_CODEC_EMSEL0	0x00000000
190 #define AWACS_CODEC_EMSEL1	0x00400000
191 #define AWACS_CODEC_EMSEL2	0x00800000
192 #define AWACS_CODEC_EMSEL4	0x00c00000
193 #define AWACS_CODEC_BUSY	0x01000000
194 
195 /* cc0 */
196 #define AWACS_DEFAULT_CD_GAIN	0x000000bb
197 #define AWACS_INPUT_CD		0x00000200
198 #define AWACS_INPUT_LINE	0x00000400
199 #define AWACS_INPUT_MICROPHONE	0x00000800
200 #define AWACS_INPUT_MASK	0x00000e00
201 
202 /* cc1 */
203 #define AWACS_MUTE_SPEAKER	0x00000080
204 #define AWACS_MUTE_HEADPHONE	0x00000200
205 
206 int
207 awacs_match(parent, match, aux)
208 	struct device *parent;
209 	struct cfdata *match;
210 	void *aux;
211 {
212 	struct confargs *ca = aux;
213 
214 	if (strcmp(ca->ca_name, "i2s") == 0)
215 		return 1;
216 
217 	if (strcmp(ca->ca_name, "awacs") != 0 &&
218 	    strcmp(ca->ca_name, "davbus") != 0)
219 		return 0;
220 
221 	if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
222 		return 0;
223 
224 	return 1;
225 }
226 
227 void
228 awacs_attach(parent, self, aux)
229 	struct device *parent;
230 	struct device *self;
231 	void *aux;
232 {
233 	struct awacs_softc *sc = (struct awacs_softc *)self;
234 	struct confargs *ca = aux;
235 	int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
236 
237 	ca->ca_reg[0] += ca->ca_baseaddr;
238 	ca->ca_reg[2] += ca->ca_baseaddr;
239 	ca->ca_reg[4] += ca->ca_baseaddr;
240 
241 	sc->sc_reg = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
242 
243 	sc->sc_odma = mapiodev(ca->ca_reg[2], ca->ca_reg[3]); /* out */
244 	sc->sc_idma = mapiodev(ca->ca_reg[4], ca->ca_reg[5]); /* in */
245 	sc->sc_odmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
246 	sc->sc_idmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command));
247 
248 	if (strcmp(ca->ca_name, "i2s") == 0) {
249 		int node, intr[6];
250 
251 		node = OF_child(ca->ca_node);
252 		if (node == NULL) {
253 			printf("no i2s-a child\n");
254 			return;
255 		}
256 		if (OF_getprop(node, "interrupts", intr, sizeof(intr)) == -1) {
257 			printf("no interrupt property\n");
258 			return;
259 		}
260 
261 		cirq = intr[0];
262 		oirq = intr[2];
263 		iirq = intr[4];
264 		cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
265 		oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
266 		iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
267 	} else if (ca->ca_nintr == 24) {
268 		cirq = ca->ca_intr[0];
269 		oirq = ca->ca_intr[2];
270 		iirq = ca->ca_intr[4];
271 		cirq_type = ca->ca_intr[1] ? IST_LEVEL : IST_EDGE;
272 		oirq_type = ca->ca_intr[3] ? IST_LEVEL : IST_EDGE;
273 		iirq_type = ca->ca_intr[5] ? IST_LEVEL : IST_EDGE;
274 	} else {
275 		cirq = ca->ca_intr[0];
276 		oirq = ca->ca_intr[1];
277 		iirq = ca->ca_intr[2];
278 		cirq_type = oirq_type = iirq_type = IST_LEVEL;
279 	}
280 
281 	intr_establish(cirq, cirq_type, IPL_AUDIO, awacs_intr, sc);
282 	intr_establish(oirq, oirq_type, IPL_AUDIO, awacs_intr, sc);
283 	/* intr_establish(iirq, iirq_type, IPL_AUDIO, awacs_intr, sc); */
284 
285 	printf(": irq %d,%d,%d\n", cirq, oirq, iirq);
286 
287 	/* XXX Uni-North based models don't have byteswap capability. */
288 	if (OF_finddevice("/uni-n") == -1)
289 		sc->sc_flags |= AWACS_CAP_BSWAP;
290 
291 	sc->sc_soundctl = AWACS_INPUT_SUBFRAME0 | AWACS_OUTPUT_SUBFRAME0 |
292 		AWACS_RATE_44100;
293 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
294 
295 	sc->sc_codecctl0 = AWACS_CODEC_ADDR0 | AWACS_CODEC_EMSEL0;
296 	sc->sc_codecctl1 = AWACS_CODEC_ADDR1 | AWACS_CODEC_EMSEL0;
297 	sc->sc_codecctl2 = AWACS_CODEC_ADDR2 | AWACS_CODEC_EMSEL0;
298 	sc->sc_codecctl4 = AWACS_CODEC_ADDR4 | AWACS_CODEC_EMSEL0;
299 
300 	sc->sc_codecctl0 |= AWACS_INPUT_CD | AWACS_DEFAULT_CD_GAIN;
301 	awacs_write_codec(sc, sc->sc_codecctl0);
302 
303 	/* Set initial volume[s] */
304 	awacs_set_speaker_volume(sc, 80, 80);
305 
306 	/* Set loopback (for CD?) */
307 	/* sc->sc_codecctl1 |= 0x440; */
308 	sc->sc_codecctl1 |= 0x40;
309 	awacs_write_codec(sc, sc->sc_codecctl1);
310 
311 	/* default output to speakers */
312 	sc->sc_output_mask = 1 << 0;
313 	sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
314 	sc->sc_codecctl1 |= AWACS_MUTE_HEADPHONE;
315 	awacs_write_codec(sc, sc->sc_codecctl1);
316 
317 	/* default input from CD */
318 	sc->sc_record_source = 1 << 0;
319 	sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
320 	sc->sc_codecctl0 |= AWACS_INPUT_CD;
321 	awacs_write_codec(sc, sc->sc_codecctl0);
322 
323 	/* Enable interrupts and looping mode. */
324 	/* XXX ... */
325 
326 	audio_attach_mi(&awacs_hw_if, sc, &sc->sc_dev);
327 }
328 
329 static inline u_int
330 awacs_read_reg(sc, reg)
331 	struct awacs_softc *sc;
332 	int reg;
333 {
334 	char *addr = sc->sc_reg;
335 
336 	return in32rb(addr + reg);
337 }
338 
339 static inline void
340 awacs_write_reg(sc, reg, val)
341 	struct awacs_softc *sc;
342 	int reg, val;
343 {
344 	char *addr = sc->sc_reg;
345 
346 	out32rb(addr + reg, val);
347 }
348 
349 void
350 awacs_write_codec(sc, value)
351 	struct awacs_softc *sc;
352 	int value;
353 {
354 	awacs_write_reg(sc, AWACS_CODEC_CTRL, value);
355 	while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
356 }
357 
358 int
359 awacs_intr(v)
360 	void *v;
361 {
362 	struct awacs_softc *sc = v;
363 	struct dbdma_command *cmd = sc->sc_odmacmd;
364 	int count = sc->sc_opages;
365 	int status;
366 
367 	/* Fill used buffer(s). */
368 	while (count-- > 0) {
369 		/* if DBDMA_INT_ALWAYS */
370 		if (in16rb(&cmd->d_command) & 0x30) {	/* XXX */
371 			status = in16rb(&cmd->d_status);
372 			cmd->d_status = 0;
373 			if (status)	/* status == 0x8400 */
374 				if (sc->sc_ointr)
375 					(*sc->sc_ointr)(sc->sc_oarg);
376 		}
377 		cmd++;
378 	}
379 
380 	return 1;
381 }
382 
383 int
384 awacs_open(h, flags)
385 	void *h;
386 	int flags;
387 {
388 	return 0;
389 }
390 
391 /*
392  * Close function is called at splaudio().
393  */
394 void
395 awacs_close(h)
396 	void *h;
397 {
398 	struct awacs_softc *sc = h;
399 
400 	awacs_halt_output(sc);
401 	awacs_halt_input(sc);
402 
403 	sc->sc_ointr = 0;
404 	sc->sc_iintr = 0;
405 }
406 
407 int
408 awacs_query_encoding(h, ae)
409 	void *h;
410 	struct audio_encoding *ae;
411 {
412 	struct awacs_softc *sc = h;
413 
414 	ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
415 
416 	switch (ae->index) {
417 	case 0:
418 		strcpy(ae->name, AudioEslinear);
419 		ae->encoding = AUDIO_ENCODING_SLINEAR;
420 		ae->precision = 16;
421 		ae->flags = 0;
422 		return 0;
423 	case 1:
424 		strcpy(ae->name, AudioEslinear_be);
425 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
426 		ae->precision = 16;
427 		ae->flags = 0;
428 		return 0;
429 	case 2:
430 		strcpy(ae->name, AudioEslinear_le);
431 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
432 		ae->precision = 16;
433 		if (sc->sc_flags & AWACS_CAP_BSWAP)
434 			ae->flags = 0;
435 		return 0;
436 	case 3:
437 		strcpy(ae->name, AudioEulinear_be);
438 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
439 		ae->precision = 16;
440 		return 0;
441 	case 4:
442 		strcpy(ae->name, AudioEulinear_le);
443 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
444 		ae->precision = 16;
445 		return 0;
446 	case 5:
447 		strcpy(ae->name, AudioEmulaw);
448 		ae->encoding = AUDIO_ENCODING_ULAW;
449 		ae->precision = 8;
450 		return 0;
451 	case 6:
452 		strcpy(ae->name, AudioEalaw);
453 		ae->encoding = AUDIO_ENCODING_ALAW;
454 		ae->precision = 8;
455 		return 0;
456 	default:
457 		return EINVAL;
458 	}
459 }
460 
461 static void
462 mono16_to_stereo16(v, p, cc)
463 	void *v;
464 	u_char *p;
465 	int cc;
466 {
467 	int x;
468 	int16_t *src, *dst;
469 
470 	src = (void *)(p + cc);
471 	dst = (void *)(p + cc * 2);
472 	while (cc > 0) {
473 		x = *--src;
474 		*--dst = x;
475 		*--dst = x;
476 		cc -= 2;
477 	}
478 }
479 
480 static void
481 swap_bytes_mono16_to_stereo16(v, p, cc)
482 	void *v;
483 	u_char *p;
484 	int cc;
485 {
486 	swap_bytes(v, p, cc);
487 	mono16_to_stereo16(v, p, cc);
488 }
489 
490 int
491 awacs_set_params(h, setmode, usemode, play, rec)
492 	void *h;
493 	int setmode, usemode;
494 	struct audio_params *play, *rec;
495 {
496 	struct awacs_softc *sc = h;
497 	struct audio_params *p;
498 	int mode;
499 
500 	/*
501 	 * This device only has one clock, so make the sample rates match.
502 	 */
503 	if (play->sample_rate != rec->sample_rate &&
504 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
505 		if (setmode == AUMODE_PLAY) {
506 			rec->sample_rate = play->sample_rate;
507 			setmode |= AUMODE_RECORD;
508 		} else if (setmode == AUMODE_RECORD) {
509 			play->sample_rate = rec->sample_rate;
510 			setmode |= AUMODE_PLAY;
511 		} else
512 			return EINVAL;
513 	}
514 
515 	for (mode = AUMODE_RECORD; mode != -1;
516 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
517 		if ((setmode & mode) == 0)
518 			continue;
519 
520 		p = mode == AUMODE_PLAY ? play : rec;
521 
522 		if (p->sample_rate < 4000 || p->sample_rate > 50000 ||
523 		    (p->precision != 8 && p->precision != 16) ||
524 		    (p->channels != 1 && p->channels != 2))
525 			return EINVAL;
526 
527 		p->factor = 1;
528 		p->sw_code = 0;
529 		awacs_write_reg(sc, AWACS_BYTE_SWAP, 0);
530 
531 		switch (p->encoding) {
532 
533 		case AUDIO_ENCODING_SLINEAR_LE:
534 			if (sc->sc_flags & AWACS_CAP_BSWAP)
535 				awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
536 			else {
537 				if (p->channels == 2 && p->precision == 16) {
538 					p->sw_code = swap_bytes;
539 					break;
540 				}
541 				if (p->channels == 1 && p->precision == 16) {
542 					p->factor = 2;
543 					p->sw_code =
544 					    swap_bytes_mono16_to_stereo16;
545 					break;
546 				}
547 				return EINVAL;
548 			}
549 		case AUDIO_ENCODING_SLINEAR_BE:
550 			if (p->channels == 1 && p->precision == 16) {
551 				p->factor = 2;
552 				p->sw_code = mono16_to_stereo16;
553 				break;
554 			}
555 			if (p->channels == 2 && p->precision == 16)
556 				break;
557 
558 			return EINVAL;
559 
560 		case AUDIO_ENCODING_ULINEAR_LE:
561 			if (p->channels == 2 && p->precision == 16) {
562 				p->sw_code = swap_bytes_change_sign16_be;
563 				break;
564 			}
565 			return EINVAL;
566 
567 		case AUDIO_ENCODING_ULINEAR_BE:
568 			if (p->channels == 2 && p->precision == 16) {
569 				p->sw_code = change_sign16_be;
570 				break;
571 			}
572 			return EINVAL;
573 
574 		case AUDIO_ENCODING_ULAW:
575 			if (mode == AUMODE_PLAY) {
576 				p->factor = 2;
577 				p->sw_code = mulaw_to_slinear16_be;
578 				break;
579 			} else
580 				break;		/* XXX */
581 
582 			return EINVAL;
583 
584 		case AUDIO_ENCODING_ALAW:
585 			if (mode == AUMODE_PLAY) {
586 				p->factor = 2;
587 				p->sw_code = alaw_to_slinear16_be;
588 				break;
589 			}
590 			return EINVAL;
591 
592 		default:
593 			return EINVAL;
594 		}
595 	}
596 
597 	/* Set the speed */
598 	if (awacs_set_rate(sc, p))
599 		return EINVAL;
600 
601 	return 0;
602 }
603 
604 int
605 awacs_round_blocksize(h, size)
606 	void *h;
607 	int size;
608 {
609 	if (size < PAGE_SIZE)
610 		size = PAGE_SIZE;
611 	return size & ~PGOFSET;
612 }
613 
614 int
615 awacs_halt_output(h)
616 	void *h;
617 {
618 	struct awacs_softc *sc = h;
619 
620 	dbdma_stop(sc->sc_odma);
621 	dbdma_reset(sc->sc_odma);
622 	return 0;
623 }
624 
625 int
626 awacs_halt_input(h)
627 	void *h;
628 {
629 	struct awacs_softc *sc = h;
630 
631 	dbdma_stop(sc->sc_idma);
632 	dbdma_reset(sc->sc_idma);
633 	return 0;
634 }
635 
636 int
637 awacs_getdev(h, retp)
638 	void *h;
639 	struct audio_device *retp;
640 {
641 	*retp = awacs_device;
642 	return 0;
643 }
644 
645 enum {
646 	AWACS_MONITOR_CLASS,
647 	AWACS_OUTPUT_CLASS,
648 	AWACS_RECORD_CLASS,
649 	AWACS_OUTPUT_SELECT,
650 	AWACS_VOL_SPEAKER,
651 	AWACS_VOL_HEADPHONE,
652 	AWACS_INPUT_SELECT,
653 	AWACS_VOL_INPUT,
654 	AWACS_ENUM_LAST
655 };
656 
657 int
658 awacs_set_port(h, mc)
659 	void *h;
660 	mixer_ctrl_t *mc;
661 {
662 	struct awacs_softc *sc = h;
663 	int l, r;
664 
665 	DPRINTF("awacs_set_port dev = %d, type = %d\n", mc->dev, mc->type);
666 
667 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
668 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
669 
670 	switch (mc->dev) {
671 	case AWACS_OUTPUT_SELECT:
672 		/* No change necessary? */
673 		if (mc->un.mask == sc->sc_output_mask)
674 			return 0;
675 
676 		sc->sc_codecctl1 |= AWACS_MUTE_SPEAKER | AWACS_MUTE_HEADPHONE;
677 		if (mc->un.mask & 1 << 0)
678 			sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
679 		if (mc->un.mask & 1 << 1)
680 			sc->sc_codecctl1 &= ~AWACS_MUTE_HEADPHONE;
681 
682 		awacs_write_codec(sc, sc->sc_codecctl1);
683 		sc->sc_output_mask = mc->un.mask;
684 		return 0;
685 
686 	case AWACS_VOL_SPEAKER:
687 		awacs_set_speaker_volume(sc, l, r);
688 		return 0;
689 
690 	case AWACS_VOL_HEADPHONE:
691 		awacs_set_ext_volume(sc, l, r);
692 		return 0;
693 
694 	case AWACS_INPUT_SELECT:
695 		/* no change necessary? */
696 		if (mc->un.mask == sc->sc_record_source)
697 			return 0;
698 		switch (mc->un.mask) {
699 		case 1 << 0: /* CD */
700 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
701 			sc->sc_codecctl0 |= AWACS_INPUT_CD;
702 			awacs_write_codec(sc, sc->sc_codecctl0);
703 			break;
704 		case 1 << 1: /* microphone */
705 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
706 			sc->sc_codecctl0 |= AWACS_INPUT_MICROPHONE;
707 			awacs_write_codec(sc, sc->sc_codecctl0);
708 			break;
709 		case 1 << 2: /* line in */
710 			sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
711 			sc->sc_codecctl0 |= AWACS_INPUT_LINE;
712 			awacs_write_codec(sc, sc->sc_codecctl0);
713 			break;
714 		default: /* invalid argument */
715 			return EINVAL;
716 		}
717 		sc->sc_record_source = mc->un.mask;
718 		return 0;
719 
720 	case AWACS_VOL_INPUT:
721 		sc->sc_codecctl0 &= ~0xff;
722 		sc->sc_codecctl0 |= (l & 0xf0) | (r >> 4);
723 		awacs_write_codec(sc, sc->sc_codecctl0);
724 		return 0;
725 	}
726 
727 	return ENXIO;
728 }
729 
730 int
731 awacs_get_port(h, mc)
732 	void *h;
733 	mixer_ctrl_t *mc;
734 {
735 	struct awacs_softc *sc = h;
736 	int vol, l, r;
737 
738 	DPRINTF("awacs_get_port dev = %d, type = %d\n", mc->dev, mc->type);
739 
740 	switch (mc->dev) {
741 	case AWACS_OUTPUT_SELECT:
742 		mc->un.mask = sc->sc_output_mask;
743 		return 0;
744 
745 	case AWACS_VOL_SPEAKER:
746 		vol = sc->sc_codecctl4;
747 		l = (15 - ((vol & 0x3c0) >> 6)) * 16;
748 		r = (15 - (vol & 0x0f)) * 16;
749 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
750 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
751 		return 0;
752 
753 	case AWACS_VOL_HEADPHONE:
754 		vol = sc->sc_codecctl2;
755 		l = (15 - ((vol & 0x3c0) >> 6)) * 16;
756 		r = (15 - (vol & 0x0f)) * 16;
757 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
758 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
759 		return 0;
760 
761 	case AWACS_INPUT_SELECT:
762 		mc->un.mask = sc->sc_record_source;
763 		return 0;
764 
765 	case AWACS_VOL_INPUT:
766 		vol = sc->sc_codecctl0 & 0xff;
767 		l = (vol & 0xf0);
768 		r = (vol & 0x0f) << 4;
769 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
770 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
771 		return 0;
772 
773 	default:
774 		return ENXIO;
775 	}
776 
777 	return 0;
778 }
779 
780 int
781 awacs_query_devinfo(h, dip)
782 	void *h;
783 	mixer_devinfo_t *dip;
784 {
785 
786 	DPRINTF("query_devinfo %d\n", dip->index);
787 
788 	switch (dip->index) {
789 
790 	case AWACS_OUTPUT_SELECT:
791 		dip->mixer_class = AWACS_MONITOR_CLASS;
792 		strcpy(dip->label.name, AudioNoutput);
793 		dip->type = AUDIO_MIXER_SET;
794 		dip->prev = dip->next = AUDIO_MIXER_LAST;
795 		dip->un.s.num_mem = 2;
796 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
797 		dip->un.s.member[0].mask = 1 << 0;
798 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
799 		dip->un.s.member[1].mask = 1 << 1;
800 		return 0;
801 
802 	case AWACS_VOL_SPEAKER:
803 		dip->mixer_class = AWACS_OUTPUT_CLASS;
804 		strcpy(dip->label.name, AudioNspeaker);
805 		dip->type = AUDIO_MIXER_VALUE;
806 		dip->prev = dip->next = AUDIO_MIXER_LAST;
807 		dip->un.v.num_channels = 2;
808 		strcpy(dip->un.v.units.name, AudioNvolume);
809 		return 0;
810 
811 	case AWACS_VOL_HEADPHONE:
812 		dip->mixer_class = AWACS_OUTPUT_CLASS;
813 		strcpy(dip->label.name, AudioNheadphone);
814 		dip->type = AUDIO_MIXER_VALUE;
815 		dip->prev = dip->next = AUDIO_MIXER_LAST;
816 		dip->un.v.num_channels = 2;
817 		strcpy(dip->un.v.units.name, AudioNvolume);
818 		return 0;
819 
820 	case AWACS_INPUT_SELECT:
821 		dip->mixer_class = AWACS_RECORD_CLASS;
822 		strcpy(dip->label.name, AudioNsource);
823 		dip->type = AUDIO_MIXER_SET;
824 		dip->prev = dip->next = AUDIO_MIXER_LAST;
825 		dip->un.s.num_mem = 3;
826 		strcpy(dip->un.s.member[0].label.name, AudioNcd);
827 		dip->un.s.member[0].mask = 1 << 0;
828 		strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
829 		dip->un.s.member[1].mask = 1 << 1;
830 		strcpy(dip->un.s.member[2].label.name, AudioNline);
831 		dip->un.s.member[2].mask = 1 << 2;
832 		return 0;
833 
834 	case AWACS_VOL_INPUT:
835 		dip->mixer_class = AWACS_RECORD_CLASS;
836 		strcpy(dip->label.name, AudioNrecord);
837 		dip->type = AUDIO_MIXER_VALUE;
838 		dip->prev = dip->next = AUDIO_MIXER_LAST;
839 		dip->un.v.num_channels = 2;
840 		strcpy(dip->un.v.units.name, AudioNvolume);
841 		return 0;
842 
843 	case AWACS_MONITOR_CLASS:
844 		dip->mixer_class = AWACS_MONITOR_CLASS;
845 		strcpy(dip->label.name, AudioCmonitor);
846 		dip->type = AUDIO_MIXER_CLASS;
847 		dip->next = dip->prev = AUDIO_MIXER_LAST;
848 		return 0;
849 
850 	case AWACS_OUTPUT_CLASS:
851 		dip->mixer_class = AWACS_OUTPUT_CLASS;
852 		strcpy(dip->label.name, AudioCoutputs);
853 		dip->type = AUDIO_MIXER_CLASS;
854 		dip->next = dip->prev = AUDIO_MIXER_LAST;
855 		return 0;
856 
857 	case AWACS_RECORD_CLASS:
858 		dip->mixer_class = AWACS_RECORD_CLASS;
859 		strcpy(dip->label.name, AudioCrecord);
860 		dip->type = AUDIO_MIXER_CLASS;
861 		dip->next = dip->prev = AUDIO_MIXER_LAST;
862 		return 0;
863 	}
864 
865 	return ENXIO;
866 }
867 
868 size_t
869 awacs_round_buffersize(h, dir, size)
870 	void *h;
871 	int dir;
872 	size_t size;
873 {
874 	if (size > 65536)
875 		size = 65536;
876 	return size;
877 }
878 
879 paddr_t
880 awacs_mappage(h, mem, off, prot)
881 	void *h;
882 	void *mem;
883 	off_t off;
884 	int prot;
885 {
886 	if (off < 0)
887 		return -1;
888 	return -1;	/* XXX */
889 }
890 
891 int
892 awacs_get_props(h)
893 	void *h;
894 {
895 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
896 }
897 
898 int
899 awacs_trigger_output(h, start, end, bsize, intr, arg, param)
900 	void *h;
901 	void *start, *end;
902 	int bsize;
903 	void (*intr)(void *);
904 	void *arg;
905 	struct audio_params *param;
906 {
907 	struct awacs_softc *sc = h;
908 	struct dbdma_command *cmd = sc->sc_odmacmd;
909 	vaddr_t va;
910 	int i, len, intmode;
911 
912 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
913 
914 	sc->sc_ointr = intr;
915 	sc->sc_oarg = arg;
916 	sc->sc_opages = ((char *)end - (char *)start) / PAGE_SIZE;
917 
918 #ifdef DIAGNOSTIC
919 	if (sc->sc_opages > 16)
920 		panic("awacs_trigger_output");
921 #endif
922 
923 	va = (vaddr_t)start;
924 	len = 0;
925 	for (i = sc->sc_opages; i > 0; i--) {
926 		len += PAGE_SIZE;
927 		if (len < bsize)
928 			intmode = DBDMA_INT_NEVER;
929 		else {
930 			len = 0;
931 			intmode = DBDMA_INT_ALWAYS;
932 		}
933 
934 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, PAGE_SIZE, vtophys(va),
935 			intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
936 		va += PAGE_SIZE;
937 		cmd++;
938 	}
939 
940 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
941 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
942 	dbdma_st32(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
943 
944 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
945 
946 	return 0;
947 }
948 
949 int
950 awacs_trigger_input(h, start, end, bsize, intr, arg, param)
951 	void *h;
952 	void *start, *end;
953 	int bsize;
954 	void (*intr)(void *);
955 	void *arg;
956 	struct audio_params *param;
957 {
958 	printf("awacs_trigger_input called\n");
959 
960 	return 1;
961 }
962 
963 void
964 awacs_set_speaker_volume(sc, left, right)
965 	struct awacs_softc *sc;
966 	int left, right;
967 {
968 	int lval = 15 - (left  & 0xff) / 16;
969 	int rval = 15 - (right & 0xff) / 16;
970 
971 	DPRINTF("speaker_volume %d %d\n", lval, rval);
972 
973 	sc->sc_codecctl4 &= ~0x3cf;
974 	sc->sc_codecctl4 |= (lval << 6) | rval;
975 	awacs_write_codec(sc, sc->sc_codecctl4);
976 }
977 
978 void
979 awacs_set_ext_volume(sc, left, right)
980 	struct awacs_softc *sc;
981 	int left, right;
982 {
983 	int lval = 15 - (left  & 0xff) / 16;
984 	int rval = 15 - (right & 0xff) / 16;
985 
986 	DPRINTF("ext_volume %d %d\n", lval, rval);
987 
988 	sc->sc_codecctl2 &= ~0x3cf;
989 	sc->sc_codecctl2 |= (lval << 6) | rval;
990 	awacs_write_codec(sc, sc->sc_codecctl2);
991 }
992 
993 int
994 awacs_set_rate(sc, p)
995 	struct awacs_softc *sc;
996 	struct audio_params *p;
997 {
998 	int c;
999 
1000 	switch (p->sample_rate) {
1001 	case 48000:
1002 		p->hw_sample_rate = 44100;
1003 		c = AWACS_RATE_44100;
1004 		break;
1005 	case 44100:
1006 		c = AWACS_RATE_44100;
1007 		break;
1008 	case 29400:
1009 		c = AWACS_RATE_29400;
1010 		break;
1011 	case 22050:
1012 		c = AWACS_RATE_22050;
1013 		break;
1014 	case 17640:
1015 		c = AWACS_RATE_17640;
1016 		break;
1017 	case 14700:
1018 		c = AWACS_RATE_14700;
1019 		break;
1020 	case 11025:
1021 		c = AWACS_RATE_11025;
1022 		break;
1023 	case 8820:
1024 		c = AWACS_RATE_8820;
1025 		break;
1026 	case 8000: /* XXX */
1027 	case 7350:
1028 		c = AWACS_RATE_7350;
1029 		break;
1030 	default:
1031 		return -1;
1032 	}
1033 
1034 	sc->sc_soundctl &= ~AWACS_RATE_MASK;
1035 	sc->sc_soundctl |= c;
1036 	awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
1037 
1038 	return 0;
1039 }
1040