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