xref: /openbsd-src/sys/arch/macppc/dev/i2s.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: i2s.c,v 1.12 2008/08/24 23:44:44 todd Exp $	*/
2 /*	$NetBSD: i2s.c,v 1.1 2003/12/27 02:19:34 grant Exp $	*/
3 
4 /*-
5  * Copyright (c) 2002 Tsubai Masanari.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/audioio.h>
32 #include <sys/device.h>
33 #include <sys/malloc.h>
34 #include <sys/systm.h>
35 
36 #include <dev/auconv.h>
37 #include <dev/audio_if.h>
38 #include <dev/mulaw.h>
39 #include <dev/ofw/openfirm.h>
40 #include <macppc/dev/dbdma.h>
41 
42 #include <uvm/uvm_extern.h>
43 
44 #include <machine/autoconf.h>
45 #include <machine/pio.h>
46 
47 #include <macppc/dev/i2svar.h>
48 #include <macppc/dev/i2sreg.h>
49 
50 #ifdef I2S_DEBUG
51 # define DPRINTF(x) printf x
52 #else
53 # define DPRINTF(x)
54 #endif
55 
56 struct i2s_mode *i2s_find_mode(u_int, u_int, u_int);
57 
58 static int gpio_read(char *);
59 static void gpio_write(char *, int);
60 void i2s_mute_speaker(struct i2s_softc *, int);
61 void i2s_mute_headphone(struct i2s_softc *, int);
62 void i2s_mute_lineout(struct i2s_softc *, int);
63 int i2s_cint(void *);
64 u_char *i2s_gpio_map(struct i2s_softc *, char *, int *);
65 void i2s_init(struct i2s_softc *, int);
66 
67 int i2s_intr(void *);
68 int i2s_iintr(void *);
69 
70 /* XXX */
71 void keylargo_fcr_enable(int, u_int32_t);
72 void keylargo_fcr_disable(int, u_int32_t);
73 
74 struct cfdriver i2s_cd = {
75 	NULL, "i2s", DV_DULL
76 };
77 
78 static u_char *amp_mute;
79 static u_char *headphone_mute;
80 static u_char *lineout_mute;
81 static u_char *audio_hw_reset;
82 static u_char *headphone_detect;
83 static int headphone_detect_active;
84 static u_char *lineout_detect;
85 static int lineout_detect_active;
86 
87 /* GPIO bits */
88 #define GPIO_OUTSEL	0xf0	/* Output select */
89 		/*	0x00	GPIO bit0 is output
90 			0x10	media-bay power
91 			0x20	reserved
92 			0x30	MPIC */
93 
94 #define GPIO_ALTOE	0x08	/* Alternate output enable */
95 		/*	0x00	Use DDR
96 			0x08	Use output select */
97 
98 #define GPIO_DDR	0x04	/* Data direction */
99 #define GPIO_DDR_OUTPUT	0x04	/* Output */
100 #define GPIO_DDR_INPUT	0x00	/* Input */
101 
102 #define GPIO_LEVEL	0x02	/* Pin level (RO) */
103 
104 #define	GPIO_DATA	0x01	/* Data */
105 
106 void
107 i2s_attach(struct device *parent, struct i2s_softc *sc, struct confargs *ca)
108 {
109 	int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
110 	u_int32_t reg[6], intr[6];
111 
112 	sc->sc_node = OF_child(ca->ca_node);
113 	sc->sc_baseaddr = ca->ca_baseaddr;
114 
115 	OF_getprop(sc->sc_node, "reg", reg, sizeof reg);
116 	reg[0] += sc->sc_baseaddr;
117 	reg[2] += sc->sc_baseaddr;
118 	reg[4] += sc->sc_baseaddr;
119 
120 	sc->sc_reg = mapiodev(reg[0], reg[1]);
121 
122 	sc->sc_dmat = ca->ca_dmat;
123 	sc->sc_odma = mapiodev(reg[2], reg[3]); /* out */
124 	sc->sc_idma = mapiodev(reg[4], reg[5]); /* in */
125 	sc->sc_odbdma = dbdma_alloc(sc->sc_dmat, I2S_DMALIST_MAX);
126 	sc->sc_odmacmd = sc->sc_odbdma->d_addr;
127 	sc->sc_idbdma = dbdma_alloc(sc->sc_dmat, I2S_DMALIST_MAX);
128 	sc->sc_idmacmd = sc->sc_idbdma->d_addr;
129 
130 	OF_getprop(sc->sc_node, "interrupts", intr, sizeof intr);
131 	cirq = intr[0];
132 	oirq = intr[2];
133 	iirq = intr[4];
134 	cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
135 	oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
136 	iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
137 
138 	/* intr_establish(cirq, cirq_type, IPL_AUDIO, i2s_intr, sc); */
139 	mac_intr_establish(parent, oirq, oirq_type, IPL_AUDIO, i2s_intr,
140 	    sc, sc->sc_dev.dv_xname);
141 	mac_intr_establish(parent, iirq, iirq_type, IPL_AUDIO, i2s_iintr,
142 	    sc, sc->sc_dev.dv_xname);
143 
144 	printf(": irq %d,%d,%d\n", cirq, oirq, iirq);
145 
146 	i2s_set_rate(sc, 44100);
147 	i2s_gpio_init(sc, ca->ca_node, parent);
148 }
149 
150 int
151 i2s_intr(v)
152 	void *v;
153 {
154 	struct i2s_softc *sc = v;
155 	struct dbdma_command *cmd = sc->sc_odmap;
156 	u_int16_t c, status;
157 
158 	/* if not set we are not running */
159 	if (!cmd)
160 		return (0);
161 	DPRINTF(("i2s_intr: cmd %x\n", cmd));
162 
163 	c = in16rb(&cmd->d_command);
164 	status = in16rb(&cmd->d_status);
165 
166 	if (c >> 12 == DBDMA_CMD_OUT_LAST)
167 		sc->sc_odmap = sc->sc_odmacmd;
168 	else
169 		sc->sc_odmap++;
170 
171 	if (c & (DBDMA_INT_ALWAYS << 4)) {
172 		cmd->d_status = 0;
173 		if (status)	/* status == 0x8400 */
174 			if (sc->sc_ointr)
175 				(*sc->sc_ointr)(sc->sc_oarg);
176 	}
177 
178 	return 1;
179 }
180 
181 int
182 i2s_iintr(v)
183 	void *v;
184 {
185 	struct i2s_softc *sc = v;
186 	struct dbdma_command *cmd = sc->sc_idmap;
187 	u_int16_t c, status;
188 
189 	/* if not set we are not running */
190 	if (!cmd)
191 		return (0);
192 	DPRINTF(("i2s_intr: cmd %x\n", cmd));
193 
194 	c = in16rb(&cmd->d_command);
195 	status = in16rb(&cmd->d_status);
196 
197 	if (c >> 12 == DBDMA_CMD_IN_LAST)
198 		sc->sc_idmap = sc->sc_idmacmd;
199 	else
200 		sc->sc_idmap++;
201 
202 	if (c & (DBDMA_INT_ALWAYS << 4)) {
203 		cmd->d_status = 0;
204 		if (status)	/* status == 0x8400 */
205 			if (sc->sc_iintr)
206 				(*sc->sc_iintr)(sc->sc_iarg);
207 	}
208 
209 	return 1;
210 }
211 
212 int
213 i2s_open(h, flags)
214 	void *h;
215 	int flags;
216 {
217 	return 0;
218 }
219 
220 /*
221  * Close function is called at splaudio().
222  */
223 void
224 i2s_close(h)
225 	void *h;
226 {
227 	struct i2s_softc *sc = h;
228 
229 	i2s_halt_output(sc);
230 	i2s_halt_input(sc);
231 
232 	sc->sc_ointr = 0;
233 	sc->sc_iintr = 0;
234 }
235 
236 int
237 i2s_query_encoding(h, ae)
238 	void *h;
239 	struct audio_encoding *ae;
240 {
241 	int err = 0;
242 
243 	switch (ae->index) {
244 	case 0:
245 		strlcpy(ae->name, AudioEslinear, sizeof(ae->name));
246 		ae->encoding = AUDIO_ENCODING_SLINEAR;
247 		ae->precision = 16;
248 		ae->flags = 0;
249 		break;
250 	case 1:
251 		strlcpy(ae->name, AudioEslinear_be, sizeof(ae->name));
252 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
253 		ae->precision = 16;
254 		ae->flags = 0;
255 		break;
256 	case 2:
257 		strlcpy(ae->name, AudioEslinear_le, sizeof(ae->name));
258 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
259 		ae->precision = 16;
260 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
261 		break;
262 	case 3:
263 		strlcpy(ae->name, AudioEulinear_be, sizeof(ae->name));
264 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
265 		ae->precision = 16;
266 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
267 		break;
268 	case 4:
269 		strlcpy(ae->name, AudioEulinear_le, sizeof(ae->name));
270 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
271 		ae->precision = 16;
272 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
273 		break;
274 	case 5:
275 		strlcpy(ae->name, AudioEmulaw, sizeof(ae->name));
276 		ae->encoding = AUDIO_ENCODING_ULAW;
277 		ae->precision = 8;
278 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
279 		break;
280 	case 6:
281 		strlcpy(ae->name, AudioEalaw, sizeof(ae->name));
282 		ae->encoding = AUDIO_ENCODING_ALAW;
283 		ae->precision = 8;
284 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
285 		break;
286 	case 7:
287 		strlcpy(ae->name, AudioEslinear, sizeof(ae->name));
288 		ae->encoding = AUDIO_ENCODING_SLINEAR;
289 		ae->precision = 8;
290 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
291 		break;
292 	case 8:
293 		strlcpy(ae->name, AudioEulinear, sizeof(ae->name));
294 		ae->encoding = AUDIO_ENCODING_ULINEAR;
295 		ae->precision = 8;
296 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
297 		break;
298 	default:
299 		err = EINVAL;
300 		break;
301 	}
302 	return (err);
303 }
304 
305 
306 struct i2s_mode {
307 	u_int encoding;
308 	u_int precision;
309 	u_int channels;
310 	void (*sw_code)(void *, u_char *, int);
311 	int factor;
312 } i2s_modes[] = {
313 	{ AUDIO_ENCODING_SLINEAR_LE,  8, 1, linear8_to_linear16_be_mts, 4 },
314 	{ AUDIO_ENCODING_SLINEAR_LE,  8, 2, linear8_to_linear16_be, 2 },
315 	{ AUDIO_ENCODING_SLINEAR_LE, 16, 1, swap_bytes_mts, 2 },
316 	{ AUDIO_ENCODING_SLINEAR_LE, 16, 2, swap_bytes, 1 },
317 	{ AUDIO_ENCODING_SLINEAR_BE,  8, 1, linear8_to_linear16_be_mts, 4 },
318 	{ AUDIO_ENCODING_SLINEAR_BE,  8, 2, linear8_to_linear16_be, 2 },
319 	{ AUDIO_ENCODING_SLINEAR_BE, 16, 1, noswap_bytes_mts, 2 },
320 	{ AUDIO_ENCODING_SLINEAR_BE, 16, 2, NULL, 1 },
321 	{ AUDIO_ENCODING_ULINEAR_LE,  8, 1, ulinear8_to_linear16_be_mts, 4 },
322 	{ AUDIO_ENCODING_ULINEAR_LE,  8, 2, ulinear8_to_linear16_be, 2 },
323 	{ AUDIO_ENCODING_ULINEAR_LE, 16, 1, change_sign16_swap_bytes_le_mts, 2 },
324 	{ AUDIO_ENCODING_ULINEAR_LE, 16, 2, swap_bytes_change_sign16_be, 1 },
325 	{ AUDIO_ENCODING_ULINEAR_BE,  8, 1, ulinear8_to_linear16_be_mts, 4 },
326 	{ AUDIO_ENCODING_ULINEAR_BE,  8, 2, ulinear8_to_linear16_be, 2 },
327 	{ AUDIO_ENCODING_ULINEAR_BE, 16, 1, change_sign16_be_mts, 2 },
328 	{ AUDIO_ENCODING_ULINEAR_BE, 16, 2, change_sign16_be, 1 }
329 };
330 
331 
332 struct i2s_mode *
333 i2s_find_mode(u_int encoding, u_int precision, u_int channels)
334 {
335 	struct i2s_mode *m;
336 	int i;
337 
338 	for (i = 0; i < sizeof(i2s_modes)/sizeof(i2s_modes[0]); i++) {
339 		m = &i2s_modes[i];
340 		if (m->encoding == encoding &&
341 		    m->precision == precision &&
342 		    m->channels == channels)
343 			return (m);
344 	}
345 	return (NULL);
346 }
347 
348 int
349 i2s_set_params(h, setmode, usemode, play, rec)
350 	void *h;
351 	int setmode, usemode;
352 	struct audio_params *play, *rec;
353 {
354 	struct i2s_mode *m;
355 	struct i2s_softc *sc = h;
356 	struct audio_params *p;
357 	int mode;
358 
359 	p = play; /* default to play */
360 
361 	/*
362 	 * This device only has one clock, so make the sample rates match.
363 	 */
364 	if (play->sample_rate != rec->sample_rate &&
365 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
366 		if (setmode == AUMODE_PLAY) {
367 			rec->sample_rate = play->sample_rate;
368 			setmode |= AUMODE_RECORD;
369 		} else if (setmode == AUMODE_RECORD) {
370 			play->sample_rate = rec->sample_rate;
371 			setmode |= AUMODE_PLAY;
372 		} else
373 			return EINVAL;
374 	}
375 
376 	for (mode = AUMODE_RECORD; mode != -1;
377 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
378 		if ((setmode & mode) == 0)
379 			continue;
380 
381 		p = mode == AUMODE_PLAY ? play : rec;
382 
383 		if (p->sample_rate < 4000 || p->sample_rate > 50000 ||
384 		    (p->precision != 8 && p->precision != 16) ||
385 		    (p->channels != 1 && p->channels != 2))
386 			return EINVAL;
387 
388 		switch (p->encoding) {
389 		case AUDIO_ENCODING_SLINEAR_LE:
390 		case AUDIO_ENCODING_SLINEAR_BE:
391 		case AUDIO_ENCODING_ULINEAR_LE:
392 		case AUDIO_ENCODING_ULINEAR_BE:
393 			m = i2s_find_mode(p->encoding, p->precision,
394 			    p->channels);
395 			if (m == NULL) {
396 				printf("mode not found: %u/%u/%u\n",
397 				    p->encoding, p->precision, p->channels);
398 				return (EINVAL);
399 			}
400 			p->factor = m->factor;
401 			p->sw_code = m->sw_code;
402 			break;
403 
404 		case AUDIO_ENCODING_ULAW:
405 			if (mode == AUMODE_PLAY) {
406 				if (p->channels == 1) {
407 					p->factor = 4;
408 					p->sw_code = mulaw_to_slinear16_be_mts;
409 					break;
410 				}
411 				if (p->channels == 2) {
412 					p->factor = 2;
413 					p->sw_code = mulaw_to_slinear16_be;
414 					break;
415 				}
416 			} else
417 				break; /* XXX */
418 			return (EINVAL);
419 
420 		case AUDIO_ENCODING_ALAW:
421 			if (mode == AUMODE_PLAY) {
422 				if (p->channels == 1) {
423 					p->factor = 4;
424 					p->sw_code = alaw_to_slinear16_be_mts;
425 					break;
426 				}
427 				if (p->channels == 2) {
428 					p->factor = 2;
429 					p->sw_code = alaw_to_slinear16_be;
430 					break;
431 				}
432 			} else
433 				break; /* XXX */
434 			return (EINVAL);
435 
436 		default:
437 			return (EINVAL);
438 		}
439 	}
440 
441 	/* Set the speed */
442 	if (i2s_set_rate(sc, play->sample_rate))
443 		return EINVAL;
444 
445 	p->sample_rate = sc->sc_rate;
446 
447 	return 0;
448 }
449 
450 int
451 i2s_round_blocksize(h, size)
452 	void *h;
453 	int size;
454 {
455 	if (size < NBPG)
456 		size = NBPG;
457 	return size & ~PGOFSET;
458 }
459 
460 int
461 i2s_halt_output(h)
462 	void *h;
463 {
464 	struct i2s_softc *sc = h;
465 
466 	dbdma_stop(sc->sc_odma);
467 	dbdma_reset(sc->sc_odma);
468 	return 0;
469 }
470 
471 int
472 i2s_halt_input(h)
473 	void *h;
474 {
475 	struct i2s_softc *sc = h;
476 
477 	dbdma_stop(sc->sc_idma);
478 	dbdma_reset(sc->sc_idma);
479 	return 0;
480 }
481 
482 enum {
483 	I2S_OUTPUT_CLASS,
484 	I2S_RECORD_CLASS,
485 	I2S_OUTPUT_SELECT,
486 	I2S_VOL_OUTPUT,
487 	I2S_INPUT_SELECT,
488 	I2S_VOL_INPUT,
489 	I2S_BASS,
490 	I2S_TREBLE,
491 	I2S_ENUM_LAST
492 };
493 
494 int
495 i2s_set_port(h, mc)
496 	void *h;
497 	mixer_ctrl_t *mc;
498 {
499 	struct i2s_softc *sc = h;
500 	int l, r;
501 
502 	DPRINTF(("i2s_set_port dev = %d, type = %d\n", mc->dev, mc->type));
503 
504 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
505 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
506 
507 	switch (mc->dev) {
508 	case I2S_OUTPUT_SELECT:
509 		/* No change necessary? */
510 		if (mc->un.mask == sc->sc_output_mask)
511 			return 0;
512 
513 		i2s_mute_speaker(sc, 1);
514 		i2s_mute_headphone(sc, 1);
515 		i2s_mute_lineout(sc, 1);
516 		if (mc->un.mask & 1 << 0)
517 			i2s_mute_speaker(sc, 0);
518 		if (mc->un.mask & 1 << 1)
519 			i2s_mute_headphone(sc, 0);
520 		if (mc->un.mask & 1 << 2)
521 			i2s_mute_lineout(sc, 0);
522 
523 		sc->sc_output_mask = mc->un.mask;
524 		return 0;
525 
526 	case I2S_VOL_OUTPUT:
527 		(*sc->sc_setvolume)(sc, l, r);
528 		return 0;
529 
530 	case I2S_BASS:
531 		if (sc->sc_setbass != NULL)
532 			(*sc->sc_setbass)(sc, l);
533 		return (0);
534 
535 	case I2S_TREBLE:
536 		if (sc->sc_settreble != NULL)
537 			(*sc->sc_settreble)(sc, l);
538 		return (0);
539 
540 	case I2S_INPUT_SELECT:
541 		/* no change necessary? */
542 		if (mc->un.mask == sc->sc_record_source)
543 			return 0;
544 		switch (mc->un.mask) {
545 		case 1 << 0: /* microphone */
546 		case 1 << 1: /* line in */
547 			/* XXX TO BE DONE */
548 			break;
549 		default: /* invalid argument */
550 			return EINVAL;
551 		}
552 		if (sc->sc_setinput != NULL)
553 			(*sc->sc_setinput)(sc, mc->un.mask);
554 		sc->sc_record_source = mc->un.mask;
555 		return 0;
556 
557 	case I2S_VOL_INPUT:
558 		/* XXX TO BE DONE */
559 		return 0;
560 	}
561 
562 	return ENXIO;
563 }
564 
565 int
566 i2s_get_port(h, mc)
567 	void *h;
568 	mixer_ctrl_t *mc;
569 {
570 	struct i2s_softc *sc = h;
571 
572 	DPRINTF(("i2s_get_port dev = %d, type = %d\n", mc->dev, mc->type));
573 
574 	switch (mc->dev) {
575 	case I2S_OUTPUT_SELECT:
576 		mc->un.mask = sc->sc_output_mask;
577 		return 0;
578 
579 	case I2S_VOL_OUTPUT:
580 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_vol_l;
581 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_vol_r;
582 		return 0;
583 
584 	case I2S_INPUT_SELECT:
585 		mc->un.mask = sc->sc_record_source;
586 		return 0;
587 
588 	case I2S_BASS:
589 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
590 		return (0);
591 
592 	case I2S_TREBLE:
593 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
594 		return (0);
595 
596 	case I2S_VOL_INPUT:
597 		/* XXX TO BE DONE */
598 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 0;
599 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 0;
600 		return 0;
601 
602 	default:
603 		return ENXIO;
604 	}
605 
606 	return 0;
607 }
608 
609 int
610 i2s_query_devinfo(h, dip)
611 	void *h;
612 	mixer_devinfo_t *dip;
613 {
614 	struct i2s_softc *sc = h;
615 	int n = 0;
616 
617 	switch (dip->index) {
618 
619 	case I2S_OUTPUT_SELECT:
620 		dip->mixer_class = I2S_OUTPUT_CLASS;
621 		strlcpy(dip->label.name, AudioNselect, sizeof(dip->label.name));
622 		dip->type = AUDIO_MIXER_SET;
623 		dip->prev = dip->next = AUDIO_MIXER_LAST;
624 		strlcpy(dip->un.s.member[n].label.name, AudioNspeaker,
625 		    sizeof(dip->un.s.member[n].label.name));
626 		dip->un.s.member[n++].mask = 1 << 0;
627 		if (headphone_mute) {
628 			strlcpy(dip->un.s.member[n].label.name,
629 			    AudioNheadphone,
630 			    sizeof(dip->un.s.member[n].label.name));
631 			dip->un.s.member[n++].mask = 1 << 1;
632 		}
633 		if (lineout_mute) {
634 			strlcpy(dip->un.s.member[n].label.name,	AudioNline,
635 			    sizeof(dip->un.s.member[n].label.name));
636 			dip->un.s.member[n++].mask = 1 << 2;
637 		}
638 		dip->un.s.num_mem = n;
639 		return 0;
640 
641 	case I2S_VOL_OUTPUT:
642 		dip->mixer_class = I2S_OUTPUT_CLASS;
643 		strlcpy(dip->label.name, AudioNmaster, sizeof(dip->label.name));
644 		dip->type = AUDIO_MIXER_VALUE;
645 		dip->prev = dip->next = AUDIO_MIXER_LAST;
646 		dip->un.v.num_channels = 2;
647 		strlcpy(dip->un.v.units.name, AudioNvolume,
648 		    sizeof(dip->un.v.units.name));
649 		return 0;
650 
651 	case I2S_INPUT_SELECT:
652 		dip->mixer_class = I2S_RECORD_CLASS;
653 		strlcpy(dip->label.name, AudioNsource, sizeof(dip->label.name));
654 		dip->type = AUDIO_MIXER_SET;
655 		dip->prev = dip->next = AUDIO_MIXER_LAST;
656 		dip->un.s.num_mem = 2;
657 		strlcpy(dip->un.s.member[0].label.name, AudioNmicrophone,
658 		    sizeof(dip->un.s.member[0].label.name));
659 		dip->un.s.member[0].mask = 1 << 0;
660 		strlcpy(dip->un.s.member[1].label.name, AudioNline,
661 		    sizeof(dip->un.s.member[1].label.name));
662 		dip->un.s.member[1].mask = 1 << 1;
663 		return 0;
664 
665 	case I2S_VOL_INPUT:
666 		dip->mixer_class = I2S_RECORD_CLASS;
667 		strlcpy(dip->label.name, AudioNrecord, sizeof(dip->label.name));
668 		dip->type = AUDIO_MIXER_VALUE;
669 		dip->prev = dip->next = AUDIO_MIXER_LAST;
670 		dip->un.v.num_channels = 2;
671 		strlcpy(dip->un.v.units.name, AudioNvolume,
672 		    sizeof(dip->un.v.units.name));
673 		return 0;
674 
675 	case I2S_OUTPUT_CLASS:
676 		dip->mixer_class = I2S_OUTPUT_CLASS;
677 		strlcpy(dip->label.name, AudioCoutputs,
678 		    sizeof(dip->label.name));
679 		dip->type = AUDIO_MIXER_CLASS;
680 		dip->next = dip->prev = AUDIO_MIXER_LAST;
681 		return 0;
682 
683 	case I2S_RECORD_CLASS:
684 		dip->mixer_class = I2S_RECORD_CLASS;
685 		strlcpy(dip->label.name, AudioCrecord, sizeof(dip->label.name));
686 		dip->type = AUDIO_MIXER_CLASS;
687 		dip->next = dip->prev = AUDIO_MIXER_LAST;
688 		return 0;
689 
690 	case I2S_BASS:
691 		if (sc->sc_setbass == NULL)
692 			return (ENXIO);
693 		dip->mixer_class = I2S_OUTPUT_CLASS;
694 		strlcpy(dip->label.name, AudioNbass, sizeof(dip->label.name));
695 		dip->type = AUDIO_MIXER_VALUE;
696 		dip->prev = dip->next = AUDIO_MIXER_LAST;
697 		dip->un.v.num_channels = 1;
698 		return (0);
699 
700 	case I2S_TREBLE:
701 		if (sc->sc_settreble == NULL)
702 			return (ENXIO);
703 		dip->mixer_class = I2S_OUTPUT_CLASS;
704 		strlcpy(dip->label.name, AudioNtreble, sizeof(dip->label.name));
705 		dip->type = AUDIO_MIXER_VALUE;
706 		dip->prev = dip->next = AUDIO_MIXER_LAST;
707 		dip->un.v.num_channels = 1;
708 		return (0);
709 	}
710 
711 	return ENXIO;
712 }
713 
714 size_t
715 i2s_round_buffersize(h, dir, size)
716 	void *h;
717 	int dir;
718 	size_t size;
719 {
720 	if (size > 65536)
721 		size = 65536;
722 	return size;
723 }
724 
725 paddr_t
726 i2s_mappage(h, mem, off, prot)
727 	void *h;
728 	void *mem;
729 	off_t off;
730 	int prot;
731 {
732 	if (off < 0)
733 		return -1;
734 	return -1;	/* XXX */
735 }
736 
737 int
738 i2s_get_props(h)
739 	void *h;
740 {
741 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
742 }
743 
744 int
745 i2s_trigger_output(h, start, end, bsize, intr, arg, param)
746 	void *h;
747 	void *start, *end;
748 	int bsize;
749 	void (*intr)(void *);
750 	void *arg;
751 	struct audio_params *param;
752 {
753 	struct i2s_softc *sc = h;
754 	struct i2s_dma *p;
755 	struct dbdma_command *cmd = sc->sc_odmacmd;
756 	vaddr_t spa, pa, epa;
757 	int c;
758 
759 	DPRINTF(("trigger_output %p %p 0x%x\n", start, end, bsize));
760 
761 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next);
762 	if (!p)
763 		return -1;
764 
765 	sc->sc_ointr = intr;
766 	sc->sc_oarg = arg;
767 	sc->sc_odmap = sc->sc_odmacmd;
768 
769 	spa = p->segs[0].ds_addr;
770 	c = DBDMA_CMD_OUT_MORE;
771 	for (pa = spa, epa = spa + (end - start);
772 	    pa < epa; pa += bsize, cmd++) {
773 
774 		if (pa + bsize == epa)
775 			c = DBDMA_CMD_OUT_LAST;
776 
777 		DBDMA_BUILD(cmd, c, 0, bsize, pa, DBDMA_INT_ALWAYS,
778 			DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
779 	}
780 
781 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
782 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
783 	dbdma_st32(&cmd->d_cmddep, sc->sc_odbdma->d_paddr);
784 
785 	dbdma_start(sc->sc_odma, sc->sc_odbdma);
786 
787 	return 0;
788 }
789 
790 int
791 i2s_trigger_input(h, start, end, bsize, intr, arg, param)
792 	void *h;
793 	void *start, *end;
794 	int bsize;
795 	void (*intr)(void *);
796 	void *arg;
797 	struct audio_params *param;
798 {
799 	struct i2s_softc *sc = h;
800 	struct i2s_dma *p;
801 	struct dbdma_command *cmd = sc->sc_idmacmd;
802 	vaddr_t spa, pa, epa;
803 	int c;
804 
805 	DPRINTF(("trigger_input %p %p 0x%x\n", start, end, bsize));
806 
807 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next);
808 	if (!p)
809 		return -1;
810 
811 	sc->sc_iintr = intr;
812 	sc->sc_iarg = arg;
813 	sc->sc_idmap = sc->sc_idmacmd;
814 
815 	spa = p->segs[0].ds_addr;
816 	c = DBDMA_CMD_IN_MORE;
817 	for (pa = spa, epa = spa + (end - start);
818 	    pa < epa; pa += bsize, cmd++) {
819 
820 		if (pa + bsize == epa)
821 			c = DBDMA_CMD_IN_LAST;
822 
823 		DBDMA_BUILD(cmd, c, 0, bsize, pa, DBDMA_INT_ALWAYS,
824 			DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
825 	}
826 
827 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
828 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
829 	dbdma_st32(&cmd->d_cmddep, sc->sc_idbdma->d_paddr);
830 
831 	dbdma_start(sc->sc_idma, sc->sc_idbdma);
832 
833 	return 0;
834 }
835 
836 
837 /* rate = fs = LRCLK
838  * SCLK = 64*LRCLK (I2S)
839  * MCLK = 256fs (typ. -- changeable)
840  * MCLK = clksrc / mdiv
841  *  SCLK = MCLK / sdiv
842  * rate = SCLK / 64    ( = LRCLK = fs)
843  */
844 int
845 i2s_set_rate(sc, rate)
846 	struct i2s_softc *sc;
847 	int rate;
848 {
849 	u_int reg = 0;
850 	int MCLK;
851 	int clksrc, mdiv, sdiv;
852 	int mclk_fs;
853 	int timo;
854 
855 	/* sanify */
856 	if (rate > 48000)
857 		rate = 48000;
858 	else if (rate < 44100)
859 		rate = 44100;
860 
861 	switch (rate) {
862 	case 44100:
863 		clksrc = 45158400;		/* 45MHz */
864 		reg = CLKSRC_45MHz;
865 		mclk_fs = 256;
866 		break;
867 
868 	case 48000:
869 		clksrc = 49152000;		/* 49MHz */
870 		reg = CLKSRC_49MHz;
871 		mclk_fs = 256;
872 		break;
873 
874 	default:
875 		return EINVAL;
876 	}
877 
878 	MCLK = rate * mclk_fs;
879 	mdiv = clksrc / MCLK;			/* 4 */
880 	sdiv = mclk_fs / 64;			/* 4 */
881 
882 	switch (mdiv) {
883 	case 1:
884 		reg |= MCLK_DIV1;
885 		break;
886 	case 3:
887 		reg |= MCLK_DIV3;
888 		break;
889 	case 5:
890 		reg |= MCLK_DIV5;
891 		break;
892 	default:
893 		reg |= ((mdiv / 2 - 1) << 24) & 0x1f000000;
894 		break;
895 	}
896 
897 	switch (sdiv) {
898 	case 1:
899 		reg |= SCLK_DIV1;
900 		break;
901 	case 3:
902 		reg |= SCLK_DIV3;
903 		break;
904 	default:
905 		reg |= ((sdiv / 2 - 1) << 20) & 0x00f00000;
906 		break;
907 	}
908 
909 	reg |= SCLK_MASTER;	/* XXX master mode */
910 
911 	reg |= SERIAL_64x;
912 
913 	if (sc->sc_rate == rate)
914 		return (0);
915 
916 	/* stereo input and output */
917 	DPRINTF(("I2SSetDataWordSizeReg 0x%08x -> 0x%08x\n",
918 	    in32rb(sc->sc_reg + I2S_WORDSIZE), 0x02000200));
919 	out32rb(sc->sc_reg + I2S_WORDSIZE, 0x02000200);
920 
921 	/* Clear CLKSTOPPEND */
922 	out32rb(sc->sc_reg + I2S_INT, I2S_INT_CLKSTOPPEND);
923 
924 	keylargo_fcr_disable(I2SClockOffset, I2S0CLKEN);
925 
926 	/* Wait until clock is stopped */
927 	for (timo = 1000; timo > 0; timo--) {
928 		if (in32rb(sc->sc_reg + I2S_INT) & I2S_INT_CLKSTOPPEND)
929 			goto done;
930 		delay(1);
931 	}
932 
933 	printf("i2s_set_rate: timeout\n");
934 
935 done:
936 	DPRINTF(("I2SSetSerialFormatReg 0x%x -> 0x%x\n",
937 	    in32rb(sc->sc_reg + I2S_FORMAT), reg));
938 	out32rb(sc->sc_reg + I2S_FORMAT, reg);
939 
940 	keylargo_fcr_enable(I2SClockOffset, I2S0CLKEN);
941 
942 	sc->sc_rate = rate;
943 
944 	return 0;
945 }
946 
947 int
948 gpio_read(addr)
949 	char *addr;
950 {
951 	if (*addr & GPIO_DATA)
952 		return 1;
953 	return 0;
954 }
955 
956 void
957 gpio_write(addr, val)
958 	char *addr;
959 	int val;
960 {
961 	u_int data = GPIO_DDR_OUTPUT;
962 
963 	if (val)
964 		data |= GPIO_DATA;
965 	*addr = data;
966 	asm volatile ("eieio" ::: "memory");
967 }
968 
969 #define amp_active 0		/* XXX OF */
970 #define headphone_active 0	/* XXX OF */
971 #define lineout_active 0	/* XXX OF */
972 
973 void
974 i2s_mute_speaker(sc, mute)
975 	struct i2s_softc *sc;
976 	int mute;
977 {
978 	u_int x;
979 
980 	if (amp_mute == NULL)
981 		return;
982 
983 	DPRINTF(("ampmute %d --> ", gpio_read(amp_mute)));
984 
985 	if (mute)
986 		x = amp_active;		/* mute */
987 	else
988 		x = !amp_active;	/* unmute */
989 	if (x != gpio_read(amp_mute))
990 		gpio_write(amp_mute, x);
991 
992 	DPRINTF(("%d\n", gpio_read(amp_mute)));
993 }
994 
995 void
996 i2s_mute_headphone(sc, mute)
997 	struct i2s_softc *sc;
998 	int mute;
999 {
1000 	u_int x;
1001 
1002 	if (headphone_mute == NULL)
1003 		return;
1004 
1005 	DPRINTF(("headphonemute %d --> ", gpio_read(headphone_mute)));
1006 
1007 	if (mute)
1008 		x = headphone_active;	/* mute */
1009 	else
1010 		x = !headphone_active;	/* unmute */
1011 	if (x != gpio_read(headphone_mute))
1012 		gpio_write(headphone_mute, x);
1013 
1014 	DPRINTF(("%d\n", gpio_read(headphone_mute)));
1015 }
1016 
1017 void
1018 i2s_mute_lineout(sc, mute)
1019 	struct i2s_softc *sc;
1020 	int mute;
1021 {
1022 	u_int x;
1023 
1024 	if (lineout_mute == NULL)
1025 		return;
1026 
1027 	DPRINTF(("lineout %d --> ", gpio_read(lineout_mute)));
1028 
1029 	if (mute)
1030 		x = lineout_active;	/* mute */
1031 	else
1032 		x = !lineout_active;	/* unmute */
1033 	if (x != gpio_read(lineout_mute))
1034 		gpio_write(lineout_mute, x);
1035 
1036 	DPRINTF(("%d\n", gpio_read(lineout_mute)));
1037 }
1038 
1039 int
1040 i2s_cint(v)
1041 	void *v;
1042 {
1043 	struct i2s_softc *sc = v;
1044 	u_int sense;
1045 
1046 	sc->sc_output_mask = 0;
1047 	i2s_mute_speaker(sc, 1);
1048 	i2s_mute_headphone(sc, 1);
1049 	i2s_mute_lineout(sc, 1);
1050 
1051 	if (headphone_detect)
1052 		sense = *headphone_detect;
1053 	else
1054 		sense = !headphone_detect_active << 1;
1055 	DPRINTF(("headphone detect = 0x%x\n", sense));
1056 
1057 	if (((sense & 0x02) >> 1) == headphone_detect_active) {
1058 		DPRINTF(("headphone is inserted\n"));
1059 		sc->sc_output_mask |= 1 << 1;
1060 		i2s_mute_headphone(sc, 0);
1061 	} else {
1062 		DPRINTF(("headphone is NOT inserted\n"));
1063 	}
1064 
1065 	if (lineout_detect)
1066 		sense = *lineout_detect;
1067 	else
1068 		sense = !lineout_detect_active << 1;
1069 	DPRINTF(("lineout detect = 0x%x\n", sense));
1070 
1071 	if (((sense & 0x02) >> 1) == lineout_detect_active) {
1072 		DPRINTF(("lineout is inserted\n"));
1073 		sc->sc_output_mask |= 1 << 2;
1074 		i2s_mute_lineout(sc, 0);
1075 	} else {
1076 		DPRINTF(("lineout is NOT inserted\n"));
1077 	}
1078 
1079 	if (sc->sc_output_mask == 0) {
1080 		sc->sc_output_mask |= 1 << 0;
1081 		i2s_mute_speaker(sc, 0);
1082 	}
1083 
1084 	return 1;
1085 }
1086 
1087 u_char *
1088 i2s_gpio_map(struct i2s_softc *sc, char *name, int *irq)
1089 {
1090 	u_int32_t reg[2];
1091 	u_int32_t intr[2];
1092 	int gpio;
1093 
1094 	if (OF_getprop(sc->sc_node, name, &gpio,
1095             sizeof(gpio)) != sizeof(gpio) ||
1096 	    OF_getprop(gpio, "reg", &reg[0],
1097 	    sizeof(reg[0])) != sizeof(reg[0]) ||
1098 	    OF_getprop(OF_parent(gpio), "reg", &reg[1],
1099 	    sizeof(reg[1])) != sizeof(reg[1]))
1100 		return NULL;
1101 
1102 	if (irq && OF_getprop(gpio, "interrupts",
1103 	    intr, sizeof(intr)) == sizeof(intr)) {
1104 		*irq = intr[0];
1105 	}
1106 
1107 	return mapiodev(sc->sc_baseaddr + reg[0] + reg[1], 1);
1108 }
1109 
1110 void
1111 i2s_gpio_init(sc, node, parent)
1112 	struct i2s_softc *sc;
1113 	int node;
1114 	struct device *parent;
1115 {
1116 	int gpio;
1117 	int headphone_detect_intr = -1, headphone_detect_intrtype;
1118 	int lineout_detect_intr = -1;
1119 
1120 	/* Map gpios. */
1121 	amp_mute = i2s_gpio_map(sc, "platform-amp-mute", NULL);
1122 	headphone_mute = i2s_gpio_map(sc, "platform-headphone-mute", NULL);
1123 	headphone_detect = i2s_gpio_map(sc, "platform-headphone-detect",
1124 	    &headphone_detect_intr);
1125 	lineout_mute = i2s_gpio_map(sc, "platform-lineout-mute", NULL);
1126 	lineout_detect = i2s_gpio_map(sc, "platform-lineout-detect",
1127 	    &lineout_detect_intr);
1128 	audio_hw_reset = i2s_gpio_map(sc, "platform-hw-reset", NULL);
1129 
1130 	gpio = OF_getnodebyname(OF_parent(node), "gpio");
1131 	DPRINTF((" /gpio 0x%x\n", gpio));
1132 	gpio = OF_child(gpio);
1133 	while (gpio) {
1134 		char name[64], audio_gpio[64];
1135 		int intr[2];
1136 		paddr_t addr;
1137 
1138 		bzero(name, sizeof name);
1139 		bzero(audio_gpio, sizeof audio_gpio);
1140 		addr = 0;
1141 		OF_getprop(gpio, "name", name, sizeof name);
1142 		OF_getprop(gpio, "audio-gpio", audio_gpio, sizeof audio_gpio);
1143 		OF_getprop(gpio, "AAPL,address", &addr, sizeof addr);
1144 		/* printf("0x%x %s %s\n", gpio, name, audio_gpio); */
1145 
1146 		/* gpio5 */
1147 		if (headphone_mute == NULL &&
1148 		    strcmp(audio_gpio, "headphone-mute") == 0)
1149 			headphone_mute = mapiodev(addr,1);
1150 
1151 		/* gpio6 */
1152 		if (amp_mute == NULL &&
1153 		    strcmp(audio_gpio, "amp-mute") == 0)
1154 			amp_mute = mapiodev(addr,1);
1155 
1156 		/* extint-gpio15 */
1157 		if (headphone_detect == NULL &&
1158 		    strcmp(audio_gpio, "headphone-detect") == 0) {
1159 			headphone_detect = mapiodev(addr,1);
1160 			OF_getprop(gpio, "audio-gpio-active-state",
1161 			    &headphone_detect_active, 4);
1162 			OF_getprop(gpio, "interrupts", intr, 8);
1163 			headphone_detect_intr = intr[0];
1164 			headphone_detect_intrtype = intr[1];
1165 		}
1166 
1167 		/* gpio11 (keywest-11) */
1168 		if (audio_hw_reset == NULL &&
1169 		    strcmp(audio_gpio, "audio-hw-reset") == 0)
1170 			audio_hw_reset = mapiodev(addr,1);
1171 
1172 		gpio = OF_peer(gpio);
1173 	}
1174 	DPRINTF((" amp-mute %p\n", amp_mute));
1175 	DPRINTF((" headphone-mute %p\n", headphone_mute));
1176 	DPRINTF((" headphone-detect %p\n", headphone_detect));
1177 	DPRINTF((" headphone-detect active %x\n", headphone_detect_active));
1178 	DPRINTF((" headphone-detect intr %x\n", headphone_detect_intr));
1179 	DPRINTF((" lineout-mute %p\n", lineout_mute));
1180 	DPRINTF((" lineout-detect %p\n", lineout_detect));
1181 	DPRINTF((" lineout-detect active %x\n", lineout_detect_active));
1182 	DPRINTF((" lineout-detect intr %x\n", lineout_detect_intr));
1183 	DPRINTF((" audio-hw-reset %p\n", audio_hw_reset));
1184 
1185 	if (headphone_detect_intr != -1)
1186 		mac_intr_establish(parent, headphone_detect_intr, IST_EDGE,
1187 		    IPL_AUDIO, i2s_cint, sc, sc->sc_dev.dv_xname);
1188 
1189 	if (lineout_detect_intr != -1)
1190 		mac_intr_establish(parent, lineout_detect_intr, IST_EDGE,
1191 		    IPL_AUDIO, i2s_cint, sc, sc->sc_dev.dv_xname);
1192 
1193 	/* Enable headphone interrupt? */
1194 	*headphone_detect |= 0x80;
1195 	asm volatile("eieio");
1196 
1197 	/* Update headphone status. */
1198 	i2s_cint(sc);
1199 }
1200 
1201 void *
1202 i2s_allocm(void *h, int dir, size_t size, int type, int flags)
1203 {
1204 	struct i2s_softc *sc = h;
1205 	struct i2s_dma *p;
1206 	int error;
1207 
1208 	if (size > I2S_DMALIST_MAX * I2S_DMASEG_MAX)
1209 		return (NULL);
1210 
1211 	p = malloc(sizeof(*p), type, flags | M_ZERO);
1212 	if (!p)
1213 		return (NULL);
1214 
1215 	/* convert to the bus.h style, not used otherwise */
1216 	if (flags & M_NOWAIT)
1217 		flags = BUS_DMA_NOWAIT;
1218 
1219 	p->size = size;
1220 	if ((error = bus_dmamem_alloc(sc->sc_dmat, p->size, NBPG, 0, p->segs,
1221 	    1, &p->nsegs, flags)) != 0) {
1222 		printf("%s: unable to allocate dma, error = %d\n",
1223 		    sc->sc_dev.dv_xname, error);
1224 		free(p, type);
1225 		return NULL;
1226 	}
1227 
1228 	if ((error = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size,
1229 	    &p->addr, flags | BUS_DMA_COHERENT)) != 0) {
1230 		printf("%s: unable to map dma, error = %d\n",
1231 		    sc->sc_dev.dv_xname, error);
1232 		bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
1233 		free(p, type);
1234 		return NULL;
1235 	}
1236 
1237 	if ((error = bus_dmamap_create(sc->sc_dmat, p->size, 1,
1238 	    p->size, 0, flags, &p->map)) != 0) {
1239 		printf("%s: unable to create dma map, error = %d\n",
1240 		    sc->sc_dev.dv_xname, error);
1241 		bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
1242 		bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
1243 		free(p, type);
1244 		return NULL;
1245 	}
1246 
1247 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size,
1248 	    NULL, flags)) != 0) {
1249 		printf("%s: unable to load dma map, error = %d\n",
1250 		    sc->sc_dev.dv_xname, error);
1251 		bus_dmamap_destroy(sc->sc_dmat, p->map);
1252 		bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
1253 		bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
1254 		free(p, type);
1255 		return NULL;
1256 	}
1257 
1258 	p->next = sc->sc_dmas;
1259 	sc->sc_dmas = p;
1260 
1261 	return p->addr;
1262 }
1263 
1264 #define reset_active 0
1265 
1266 int
1267 deq_reset(struct i2s_softc *sc)
1268 {
1269 	if (audio_hw_reset == NULL)
1270 		return (-1);
1271 
1272 	gpio_write(audio_hw_reset, !reset_active);
1273 	delay(1000000);
1274 
1275 	gpio_write(audio_hw_reset, reset_active);
1276 	delay(1);
1277 
1278 	gpio_write(audio_hw_reset, !reset_active);
1279 	delay(10000);
1280 
1281 	return (0);
1282 }
1283