xref: /freebsd/sys/dev/sound/macio/i2s.c (revision aa0a1e58)
1 /*-
2  * Copyright 2008 by Marco Trillo. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 /*-
28  * Copyright (c) 2002, 2003 Tsubai Masanari.  All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions
32  * are met:
33  * 1. Redistributions of source code must retain the above copyright
34  *    notice, this list of conditions and the following disclaimer.
35  * 2. Redistributions in binary form must reproduce the above copyright
36  *    notice, this list of conditions and the following disclaimer in the
37  *    documentation and/or other materials provided with the distribution.
38  * 3. The name of the author may not be used to endorse or promote products
39  *    derived from this software without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
42  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
45  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51  *
52  * 	NetBSD: snapper.c,v 1.28 2008/05/16 03:49:54 macallan Exp
53  *	Id: snapper.c,v 1.11 2002/10/31 17:42:13 tsubai Exp
54  */
55 
56 /*
57  *	Apple I2S audio controller.
58  */
59 
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/module.h>
64 #include <sys/bus.h>
65 #include <sys/malloc.h>
66 #include <sys/lock.h>
67 #include <sys/mutex.h>
68 #include <machine/dbdma.h>
69 #include <machine/intr_machdep.h>
70 #include <machine/resource.h>
71 #include <machine/bus.h>
72 #include <machine/pio.h>
73 #include <sys/rman.h>
74 #include <dev/ofw/ofw_bus.h>
75 
76 #ifdef HAVE_KERNEL_OPTION_HEADERS
77 #include "opt_snd.h"
78 #endif
79 
80 #include <dev/sound/pcm/sound.h>
81 #include <dev/sound/macio/aoa.h>
82 
83 #include <powerpc/powermac/macgpiovar.h>
84 
85 struct i2s_softc {
86 	struct aoa_softc 	 aoa;
87 	phandle_t 		 node;
88 	phandle_t		 soundnode;
89 	struct resource 	*reg;
90 	u_int 			 output_mask;
91 	struct mtx 		 port_mtx;
92 };
93 
94 static int 	i2s_probe(device_t);
95 static int 	i2s_attach(device_t);
96 static void 	i2s_postattach(void *);
97 static int 	i2s_setup(struct i2s_softc *, u_int, u_int, u_int);
98 static void     i2s_mute_headphone (struct i2s_softc *, int);
99 static void     i2s_mute_lineout   (struct i2s_softc *, int);
100 static void     i2s_mute_speaker   (struct i2s_softc *, int);
101 static void 	i2s_set_outputs(void *, u_int);
102 
103 static struct intr_config_hook 	*i2s_delayed_attach = NULL;
104 
105 kobj_class_t	i2s_mixer_class = NULL;
106 device_t	i2s_mixer = NULL;
107 
108 static device_method_t pcm_i2s_methods[] = {
109 	/* Device interface. */
110 	DEVMETHOD(device_probe,		i2s_probe),
111 	DEVMETHOD(device_attach, 	i2s_attach),
112 
113 	{ 0, 0 }
114 };
115 
116 static driver_t pcm_i2s_driver = {
117 	"pcm",
118 	pcm_i2s_methods,
119 	PCM_SOFTC_SIZE
120 };
121 
122 DRIVER_MODULE(pcm_i2s, macio, pcm_i2s_driver, pcm_devclass, 0, 0);
123 MODULE_DEPEND(pcm_i2s, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
124 
125 static int	aoagpio_probe(device_t);
126 static int 	aoagpio_attach(device_t);
127 
128 static device_method_t aoagpio_methods[] = {
129 	/* Device interface. */
130 	DEVMETHOD(device_probe,		aoagpio_probe),
131 	DEVMETHOD(device_attach,	aoagpio_attach),
132 
133 	{ 0, 0 }
134 };
135 
136 struct aoagpio_softc {
137 	device_t 		 dev;
138 	int 			 ctrl;
139 	int			 detect_active; /* for extint-gpio */
140 	int			 level;		/* for extint-gpio */
141 	struct i2s_softc	*i2s;		/* for extint-gpio */
142 };
143 
144 static driver_t aoagpio_driver = {
145 	"aoagpio",
146 	aoagpio_methods,
147 	sizeof(struct aoagpio_softc)
148 };
149 static devclass_t aoagpio_devclass;
150 
151 DRIVER_MODULE(aoagpio, macgpio, aoagpio_driver, aoagpio_devclass, 0, 0);
152 
153 
154 /*****************************************************************************
155 			Probe and attachment routines.
156  *****************************************************************************/
157 static int
158 i2s_probe(device_t self)
159 {
160 	const char 		*name;
161 
162 	name = ofw_bus_get_name(self);
163 	if (!name)
164 		return (ENXIO);
165 
166 	if (strcmp(name, "i2s") != 0)
167 		return (ENXIO);
168 
169 	device_set_desc(self, "Apple I2S Audio Controller");
170 
171 	return (0);
172 }
173 
174 static phandle_t of_find_firstchild_byname(phandle_t, const char *);
175 
176 static int
177 i2s_attach(device_t self)
178 {
179 	struct i2s_softc 	*sc;
180 	struct resource 	*dbdma_irq;
181 	void			*dbdma_ih;
182 	int 			 rid, oirq, err;
183 	phandle_t 		 port;
184 
185 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
186 
187 	sc->aoa.sc_dev = self;
188 	sc->node = ofw_bus_get_node(self);
189 
190 	port = of_find_firstchild_byname(sc->node, "i2s-a");
191 	if (port == -1)
192 		return (ENXIO);
193 	sc->soundnode = of_find_firstchild_byname(port, "sound");
194 	if (sc->soundnode == -1)
195 		return (ENXIO);
196 
197 	mtx_init(&sc->port_mtx, "port_mtx", NULL, MTX_DEF);
198 
199 	/* Map the controller register space. */
200 	rid = 0;
201 	sc->reg = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
202 	if (sc->reg == NULL)
203 		return ENXIO;
204 
205 	/* Map the DBDMA channel register space. */
206 	rid = 1;
207 	sc->aoa.sc_odma = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
208 	    RF_ACTIVE);
209 	if (sc->aoa.sc_odma == NULL)
210 		return ENXIO;
211 
212 	/* Establish the DBDMA channel edge-triggered interrupt. */
213 	rid = 1;
214 	dbdma_irq = bus_alloc_resource_any(self, SYS_RES_IRQ,
215 	    &rid, RF_SHAREABLE | RF_ACTIVE);
216 	if (dbdma_irq == NULL)
217 		return (ENXIO);
218 
219 	/* Now initialize the controller. */
220 	err = i2s_setup(sc, 44100, 16, 64);
221 	if (err != 0)
222 		return (err);
223 
224 	snd_setup_intr(self, dbdma_irq, INTR_MPSAFE, aoa_interrupt,
225 	    sc, &dbdma_ih);
226 
227 	oirq = rman_get_start(dbdma_irq);
228 	err = powerpc_config_intr(oirq, INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
229 	if (err != 0)
230 		return (err);
231 
232 	/*
233 	 * Register a hook for delayed attach in order to allow
234 	 * the I2C controller to attach.
235 	 */
236 	if ((i2s_delayed_attach = malloc(sizeof(struct intr_config_hook),
237 	    M_TEMP, M_WAITOK | M_ZERO)) == NULL)
238 		return (ENOMEM);
239 
240 	i2s_delayed_attach->ich_func = i2s_postattach;
241 	i2s_delayed_attach->ich_arg = sc;
242 
243 	if (config_intrhook_establish(i2s_delayed_attach) != 0)
244 		return (ENOMEM);
245 
246 	return (aoa_attach(sc));
247 }
248 
249 /*****************************************************************************
250 				GPIO routines.
251  *****************************************************************************/
252 
253 enum gpio_ctrl {
254 	AMP_MUTE,
255 	HEADPHONE_MUTE,
256 	LINEOUT_MUTE,
257 	AUDIO_HW_RESET,
258 	HEADPHONE_DETECT,
259 	LINEOUT_DETECT,
260 	GPIO_CTRL_NUM
261 };
262 
263 #define GPIO_CTRL_EXTINT_SET               \
264 		((1 << HEADPHONE_DETECT) | \
265 		 (1 << LINEOUT_DETECT))
266 
267 static struct aoagpio_softc *gpio_ctrls[GPIO_CTRL_NUM] =
268 	{NULL, NULL, NULL, NULL, NULL, NULL};
269 
270 static struct gpio_match {
271 	const char	*name;
272 	enum gpio_ctrl	 ctrl;
273 } gpio_controls[] = {
274 	{"headphone-mute",      HEADPHONE_MUTE},
275 	{"lineout-mute",	LINEOUT_MUTE},
276 	{"amp-mute",	    	AMP_MUTE},
277 	{"headphone-detect",    HEADPHONE_DETECT},
278 	{"lineout-detect",      LINEOUT_DETECT},
279 	{"line-output-detect",  LINEOUT_DETECT},
280 	{"audio-hw-reset",      AUDIO_HW_RESET},
281 	{"hw-reset",	    	AUDIO_HW_RESET},
282 	{NULL,		 	GPIO_CTRL_NUM}
283 };
284 
285 static void 		i2s_cint(struct i2s_softc *);
286 
287 static void
288 aoagpio_int(void *cookie)
289 {
290 	device_t 		 self = cookie;
291 	struct aoagpio_softc	*sc;
292 
293 	sc = device_get_softc(self);
294 
295 	if (macgpio_read(self) & GPIO_LEVEL_RO)
296 		sc->level = sc->detect_active;
297 	else
298 		sc->level = !(sc->detect_active);
299 
300 	if (sc->i2s)
301 		i2s_cint(sc->i2s);
302 }
303 
304 static int
305 aoagpio_probe(device_t gpio)
306 {
307 	phandle_t	 	 node;
308 	char			 bname[32];
309 	const char 		*name;
310 	struct gpio_match	*m;
311 	struct aoagpio_softc	*sc;
312 
313 	node = ofw_bus_get_node(gpio);
314 	if (node == 0 || node == -1)
315 		return (EINVAL);
316 
317 	bzero(bname, sizeof(bname));
318 	if (OF_getprop(node, "audio-gpio", bname, sizeof(bname)) > 2)
319 		name = bname;
320 	else
321 		name = ofw_bus_get_name(gpio);
322 
323 	/* Try to find a match. */
324 	for (m = gpio_controls; m->name != NULL; m++) {
325 		if (strcmp(name, m->name) == 0) {
326 			sc = device_get_softc(gpio);
327 			gpio_ctrls[m->ctrl] = sc;
328 			sc->dev = gpio;
329 			sc->ctrl = m->ctrl;
330 			sc->level = 0;
331 			sc->detect_active = 0;
332 			sc->i2s = NULL;
333 
334 			OF_getprop(node, "audio-gpio-active-state",
335 				&sc->detect_active, sizeof(sc->detect_active));
336 
337 			if ((1 << m->ctrl) & GPIO_CTRL_EXTINT_SET)
338 				aoagpio_int(gpio);
339 
340 			device_set_desc(gpio, m->name);
341 			device_quiet(gpio);
342 			return (0);
343 		}
344 	}
345 
346 	return (ENXIO);
347 }
348 
349 static int
350 aoagpio_attach(device_t gpio)
351 {
352 	struct aoagpio_softc	*sc;
353 	struct resource		*r;
354 	void			*cookie;
355 	int			 irq, rid = 0;
356 
357 	sc = device_get_softc(gpio);
358 
359 	if ((1 << sc->ctrl) & GPIO_CTRL_EXTINT_SET) {
360 		r = bus_alloc_resource_any(gpio, SYS_RES_IRQ, &rid, RF_ACTIVE);
361 		if (r == NULL)
362 			return (ENXIO);
363 
364 		irq = rman_get_start(r);
365 		DPRINTF(("interrupting at irq %d\n", irq));
366 
367 		if (powerpc_config_intr(irq, INTR_TRIGGER_EDGE,
368 		    INTR_POLARITY_LOW) != 0)
369 			return (ENXIO);
370 
371 		bus_setup_intr(gpio, r, INTR_TYPE_MISC | INTR_MPSAFE |
372 		    INTR_ENTROPY, NULL, aoagpio_int, gpio, &cookie);
373 	}
374 
375 	return (0);
376 }
377 
378 /*
379  * I2S module registers
380  */
381 #define I2S_INT			0x00
382 #define I2S_FORMAT		0x10
383 #define I2S_FRAMECOUNT		0x40
384 #define I2S_FRAMEMATCH		0x50
385 #define I2S_WORDSIZE		0x60
386 
387 /* I2S_INT register definitions */
388 #define I2S_INT_CLKSTOPPEND     0x01000000  /* clock-stop interrupt pending */
389 
390 /* I2S_FORMAT register definitions */
391 #define CLKSRC_49MHz		0x80000000      /* Use 49152000Hz Osc. */
392 #define CLKSRC_45MHz		0x40000000      /* Use 45158400Hz Osc. */
393 #define CLKSRC_18MHz		0x00000000      /* Use 18432000Hz Osc. */
394 #define MCLK_DIV_MASK		0x1f000000      /* MCLK = SRC / DIV */
395 #define SCLK_DIV_MASK		0x00f00000      /* SCLK = MCLK / DIV */
396 #define SCLK_MASTER		0x00080000      /* Master mode */
397 #define SCLK_SLAVE		0x00000000      /* Slave mode */
398 #define SERIAL_FORMAT		0x00070000
399 #define  SERIAL_SONY		0x00000000
400 #define  SERIAL_64x		0x00010000
401 #define  SERIAL_32x		0x00020000
402 #define  SERIAL_DAV		0x00040000
403 #define  SERIAL_SILICON		0x00050000
404 
405 /* I2S_WORDSIZE register definitions */
406 #define INPUT_STEREO		(2 << 24)
407 #define INPUT_MONO		(1 << 24)
408 #define INPUT_16BIT		(0 << 16)
409 #define INPUT_24BIT		(3 << 16)
410 #define OUTPUT_STEREO		(2 << 8)
411 #define OUTPUT_MONO		(1 << 8)
412 #define OUTPUT_16BIT		(0 << 0)
413 #define OUTPUT_24BIT		(3 << 0)
414 
415 /* Master clock, needed by some codecs. We hardcode this
416    to 256 * fs as this is valid for most codecs. */
417 #define MCLK_FS		256
418 
419 /* Number of clock sources we can use. */
420 #define NCLKS	   3
421 static const struct i2s_clksrc {
422 	u_int cs_clock;
423 	u_int cs_reg;
424 } clksrc[NCLKS] = {
425 	{49152000, CLKSRC_49MHz},
426 	{45158400, CLKSRC_45MHz},
427 	{18432000, CLKSRC_18MHz}
428 };
429 
430 /* Configure the I2S controller for the required settings.
431    'rate' is the frame rate.
432    'wordsize' is the sample size (usually 16 bits).
433    'sclk_fs' is the SCLK/framerate ratio, which needs to be equal
434    or greater to the number of bits per frame. */
435 
436 static int
437 i2s_setup(struct i2s_softc *sc, u_int rate, u_int wordsize, u_int sclk_fs)
438 {
439 	u_int mclk, mdiv, sdiv;
440 	u_int reg = 0, x, wordformat;
441 	u_int i;
442 
443 	/* Make sure the settings are consistent... */
444 	if ((wordsize * 2) > sclk_fs)
445 		return (EINVAL);
446 
447 	if (sclk_fs != 32 && sclk_fs != 64)
448 		return (EINVAL);
449 
450 	/*
451 	 *	Find a clock source to derive the master clock (MCLK)
452 	 *	and the I2S bit block (SCLK) and set the divisors as
453 	 *	appropriate.
454 	 */
455 	mclk = rate * MCLK_FS;
456 	sdiv = MCLK_FS / sclk_fs;
457 
458 	for (i = 0; i < NCLKS; ++i) {
459 		if ((clksrc[i].cs_clock % mclk) == 0) {
460 			reg = clksrc[i].cs_reg;
461 			mdiv = clksrc[i].cs_clock / mclk;
462 			break;
463 		}
464 	}
465 	if (reg == 0)
466 		return (EINVAL);
467 
468 	switch (mdiv) {
469 	/* exception cases */
470 	case 1:
471 		x = 14;
472 		break;
473 	case 3:
474 		x = 13;
475 		break;
476 	case 5:
477 		x = 12;
478 		break;
479 	default:
480 		x = (mdiv / 2) - 1;
481 		break;
482 	}
483 	reg |= (x << 24) & MCLK_DIV_MASK;
484 
485 	switch (sdiv) {
486 	case 1:
487 		x = 8;
488 		break;
489 	case 3:
490 		x = 9;
491 		break;
492 	default:
493 		x = (sdiv / 2) - 1;
494 		break;
495 	}
496 	reg |= (x << 20) & SCLK_DIV_MASK;
497 
498 	/*
499 	 * 	XXX use master mode for now. This needs to be
500 	 * 	revisited if we want to add recording from SPDIF some day.
501 	 */
502 	reg |= SCLK_MASTER;
503 
504 	switch (sclk_fs) {
505 	case 64:
506 		reg |= SERIAL_64x;
507 		break;
508 	case 32:
509 		reg |= SERIAL_32x;
510 		break;
511 	}
512 
513 	/* stereo input and output */
514 	wordformat = INPUT_STEREO | OUTPUT_STEREO;
515 
516 	switch (wordsize) {
517 	case 16:
518 		wordformat |= INPUT_16BIT | OUTPUT_16BIT;
519 		break;
520 	case 24:
521 		wordformat |= INPUT_24BIT | OUTPUT_24BIT;
522 		break;
523 	default:
524 		return (EINVAL);
525 	}
526 
527 	x = bus_read_4(sc->reg, I2S_WORDSIZE);
528 	if (x != wordformat)
529 		bus_write_4(sc->reg, I2S_WORDSIZE, wordformat);
530 
531 	x = bus_read_4(sc->reg, I2S_FORMAT);
532 	if (x != reg) {
533 		/*
534 		 * 	XXX to change the format we need to stop the clock
535 		 * 	via the FCR registers. For now, rely on the firmware
536 		 *	to set sane defaults (44100).
537 		 */
538 		printf("i2s_setup: changing format not supported yet.\n");
539 		return (ENOTSUP);
540 
541 #ifdef notyet
542 		if (obio_fcr_isset(OBIO_FCR1, I2S0CLKEN)) {
543 
544 			bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_INT,
545 					  I2S_INT_CLKSTOPPEND);
546 
547 			obio_fcr_clear(OBIO_FCR1, I2S0CLKEN);
548 
549 			for (timo = 1000; timo > 0; timo--) {
550 				if (bus_space_read_4(sc->sc_tag, sc->sc_bsh,
551 				    I2S_INT) & I2S_INT_CLKSTOPPEND)
552 					break;
553 
554 				DELAY(10);
555 			}
556 
557 			if (timo == 0)
558 				printf("%s: timeout waiting for clock to stop\n",
559 					sc->sc_dev.dv_xname);
560 		}
561 
562 		bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT, reg);
563 
564 		obio_fcr_set(OBIO_FCR1, I2S0CLKEN);
565 #endif
566 	}
567 
568 	return (0);
569 }
570 
571 
572 /* XXX this does not belong here. */
573 static phandle_t
574 of_find_firstchild_byname(phandle_t node, const char *req_name)
575 {
576 	char 		name[32]; /* max name len per OF spec. */
577 	phandle_t 	n;
578 
579 	for (n = OF_child(node); n != -1; n = OF_peer(n)) {
580 		bzero(name, sizeof(name));
581 		OF_getprop(n, "name", name, sizeof(name));
582 
583 		if (strcmp(name, req_name) == 0)
584 			return (n);
585 	}
586 
587 	return (-1);
588 }
589 
590 
591 static u_int
592 gpio_read(enum gpio_ctrl ctrl)
593 {
594 	struct aoagpio_softc *sc;
595 
596 	if ((sc = gpio_ctrls[ctrl]) == NULL)
597 		return (0);
598 
599 	return (macgpio_read(sc->dev) & GPIO_DATA);
600 }
601 
602 static void
603 gpio_write(enum gpio_ctrl ctrl, u_int x)
604 {
605 	struct aoagpio_softc 	*sc;
606 	u_int 			 reg;
607 
608 	if ((sc = gpio_ctrls[ctrl]) == NULL)
609 		return;
610 
611 	reg = GPIO_DDR_OUTPUT;
612 	if (x)
613 		reg |= GPIO_DATA;
614 
615 	macgpio_write(sc->dev, reg);
616 }
617 
618 static void
619 i2s_cint(struct i2s_softc *sc)
620 {
621 	u_int mask = 0;
622 
623 	if (gpio_ctrls[HEADPHONE_DETECT] &&
624 	    gpio_ctrls[HEADPHONE_DETECT]->level)
625 		mask |= 1 << 1;
626 
627 	if (gpio_ctrls[LINEOUT_DETECT] &&
628 	    gpio_ctrls[LINEOUT_DETECT]->level)
629 		mask |= 1 << 2;
630 
631 	if (mask == 0)
632 		mask = 1 << 0; /* fall back to speakers. */
633 
634 	i2s_set_outputs(sc, mask);
635 }
636 
637 #define reset_active	    0
638 
639 /* these values are in microseconds */
640 #define RESET_SETUP_TIME	5000
641 #define RESET_HOLD_TIME		20000
642 #define RESET_RELEASE_TIME	10000
643 
644 static void
645 i2s_audio_hw_reset(struct i2s_softc *sc)
646 {
647 	if (gpio_ctrls[AUDIO_HW_RESET]) {
648 		DPRINTF(("resetting codec\n"));
649 
650 		gpio_write(AUDIO_HW_RESET, !reset_active);   /* Negate RESET */
651 		DELAY(RESET_SETUP_TIME);
652 
653 		gpio_write(AUDIO_HW_RESET, reset_active);    /* Assert RESET */
654 		DELAY(RESET_HOLD_TIME);
655 
656 		gpio_write(AUDIO_HW_RESET, !reset_active);   /* Negate RESET */
657 		DELAY(RESET_RELEASE_TIME);
658 
659 	} else {
660 		DPRINTF(("no audio_hw_reset\n"));
661 	}
662 }
663 
664 #define AMP_ACTIVE       0	      /* XXX OF */
665 #define HEADPHONE_ACTIVE 0	      /* XXX OF */
666 #define LINEOUT_ACTIVE   0	      /* XXX OF */
667 
668 #define MUTE_CONTROL(xxx, yyy)				\
669 static void 						\
670 i2s_mute_##xxx(struct i2s_softc *sc, int mute)		\
671 {							\
672 	int 		x;				\
673 							\
674 	if (gpio_ctrls[yyy##_MUTE] == NULL)		\
675 		return;					\
676 	if (mute)					\
677 		x = yyy##_ACTIVE;			\
678 	else						\
679 		x = ! yyy##_ACTIVE;			\
680 							\
681 	if (x != gpio_read(yyy##_MUTE))			\
682 		gpio_write(yyy##_MUTE, x);		\
683 }
684 
685 MUTE_CONTROL(speaker, AMP)
686 MUTE_CONTROL(headphone, HEADPHONE)
687 MUTE_CONTROL(lineout, LINEOUT)
688 
689 static void
690 i2s_set_outputs(void *ptr, u_int mask)
691 {
692 	struct i2s_softc 	*sc = ptr;
693 
694 	if (mask == sc->output_mask)
695 		return;
696 
697 	mtx_lock(&sc->port_mtx);
698 
699 	i2s_mute_speaker(sc, 1);
700 	i2s_mute_headphone(sc, 1);
701 	i2s_mute_lineout(sc, 1);
702 
703 	DPRINTF(("enabled outputs: "));
704 
705 	if (mask & (1 << 0)) {
706 		DPRINTF(("SPEAKER "));
707 		i2s_mute_speaker(sc, 0);
708 	}
709 	if (mask & (1 << 1)) {
710 		DPRINTF(("HEADPHONE "));
711 		i2s_mute_headphone(sc, 0);
712 	}
713 	if (mask & (1 << 2)) {
714 		DPRINTF(("LINEOUT "));
715 		i2s_mute_lineout(sc, 0);
716 	}
717 
718 	DPRINTF(("\n"));
719 	sc->output_mask = mask;
720 
721 	mtx_unlock(&sc->port_mtx);
722 }
723 
724 static void
725 i2s_postattach(void *xsc)
726 {
727 	struct i2s_softc 	*sc = xsc;
728 	device_t 		 self;
729 	int 			 i;
730 
731 	self = sc->aoa.sc_dev;
732 
733 	/* Reset the codec. */
734 	i2s_audio_hw_reset(sc);
735 
736 	/* If we have a codec, initialize it. */
737 	if (i2s_mixer)
738 		mixer_init(self, i2s_mixer_class, i2s_mixer);
739 
740 	/* Read initial port status. */
741 	i2s_cint(sc);
742 
743 	/* Enable GPIO interrupt callback. */
744 	for (i = 0; i < GPIO_CTRL_NUM; i++)
745 		if (gpio_ctrls[i])
746 			gpio_ctrls[i]->i2s = sc;
747 
748 	config_intrhook_disestablish(i2s_delayed_attach);
749 	free(i2s_delayed_attach, M_TEMP);
750 }
751 
752