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