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