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