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