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