2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-3-Clause
4 * Copyright 2008 by Marco Trillo. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 * Copyright (c) 2002, 2003 Tsubai Masanari. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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.
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
59 * Apple I2S audio controller.
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/module.h>
67 #include <sys/malloc.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>
76 #include <dev/ofw/ofw_bus.h>
78 #ifdef HAVE_KERNEL_OPTION_HEADERS
82 #include <dev/sound/pcm/sound.h>
83 #include <dev/sound/macio/aoa.h>
85 #include <powerpc/powermac/macgpiovar.h>
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);
105 static struct intr_config_hook *i2s_delayed_attach = NULL;
107 kobj_class_t i2s_mixer_class = NULL;
108 device_t i2s_mixer = NULL;
110 static device_method_t pcm_i2s_methods[] = {
111 /* Device interface. */
112 DEVMETHOD(device_probe, i2s_probe),
113 DEVMETHOD(device_attach, i2s_attach),
118 static driver_t pcm_i2s_driver = {
124 DRIVER_MODULE(pcm_i2s, macio, pcm_i2s_driver, pcm_devclass, 0, 0);
125 MODULE_DEPEND(pcm_i2s, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
127 static int aoagpio_probe(device_t);
128 static int aoagpio_attach(device_t);
130 static device_method_t aoagpio_methods[] = {
131 /* Device interface. */
132 DEVMETHOD(device_probe, aoagpio_probe),
133 DEVMETHOD(device_attach, aoagpio_attach),
138 struct aoagpio_softc {
141 int detect_active; /* for extint-gpio */
142 int level; /* for extint-gpio */
143 struct i2s_softc *i2s; /* for extint-gpio */
146 static driver_t aoagpio_driver = {
149 sizeof(struct aoagpio_softc)
151 static devclass_t aoagpio_devclass;
153 DRIVER_MODULE(aoagpio, macgpio, aoagpio_driver, aoagpio_devclass, 0, 0);
156 /*****************************************************************************
157 Probe and attachment routines.
158 *****************************************************************************/
160 i2s_probe(device_t self)
164 char subchildname[255];
166 name = ofw_bus_get_name(self);
170 if (strcmp(name, "i2s") != 0)
174 * Do not attach to "lightshow" I2S devices on Xserves. This controller
175 * is used there to control the LEDs on the front panel, and this
176 * driver can't handle it.
178 subchild = OF_child(OF_child(ofw_bus_get_node(self)));
179 if (subchild != 0 && OF_getprop(subchild, "name", subchildname,
180 sizeof(subchildname)) > 0 && strcmp(subchildname, "lightshow") == 0)
183 device_set_desc(self, "Apple I2S Audio Controller");
188 static phandle_t of_find_firstchild_byname(phandle_t, const char *);
191 i2s_attach(device_t self)
193 struct i2s_softc *sc;
194 struct resource *dbdma_irq;
199 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
201 sc->aoa.sc_dev = self;
202 sc->node = ofw_bus_get_node(self);
204 port = of_find_firstchild_byname(sc->node, "i2s-a");
207 sc->soundnode = of_find_firstchild_byname(port, "sound");
208 if (sc->soundnode == -1)
211 mtx_init(&sc->port_mtx, "port_mtx", NULL, MTX_DEF);
213 /* Map the controller register space. */
215 sc->reg = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
219 /* Map the DBDMA channel register space. */
221 sc->aoa.sc_odma = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
223 if (sc->aoa.sc_odma == NULL)
226 /* Establish the DBDMA channel edge-triggered interrupt. */
228 dbdma_irq = bus_alloc_resource_any(self, SYS_RES_IRQ,
229 &rid, RF_SHAREABLE | RF_ACTIVE);
230 if (dbdma_irq == NULL)
233 /* Now initialize the controller. */
234 err = i2s_setup(sc, 44100, 16, 64);
238 snd_setup_intr(self, dbdma_irq, INTR_MPSAFE, aoa_interrupt,
241 oirq = rman_get_start(dbdma_irq);
242 err = powerpc_config_intr(oirq, INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
247 * Register a hook for delayed attach in order to allow
248 * the I2C controller to attach.
250 if ((i2s_delayed_attach = malloc(sizeof(struct intr_config_hook),
251 M_TEMP, M_WAITOK | M_ZERO)) == NULL)
254 i2s_delayed_attach->ich_func = i2s_postattach;
255 i2s_delayed_attach->ich_arg = sc;
257 if (config_intrhook_establish(i2s_delayed_attach) != 0)
260 return (aoa_attach(sc));
263 /*****************************************************************************
265 *****************************************************************************/
277 #define GPIO_CTRL_EXTINT_SET \
278 ((1 << HEADPHONE_DETECT) | \
279 (1 << LINEOUT_DETECT))
281 static struct aoagpio_softc *gpio_ctrls[GPIO_CTRL_NUM] =
282 {NULL, NULL, NULL, NULL, NULL, NULL};
284 static struct gpio_match {
287 } gpio_controls[] = {
288 {"headphone-mute", HEADPHONE_MUTE},
289 {"lineout-mute", LINEOUT_MUTE},
290 {"amp-mute", AMP_MUTE},
291 {"headphone-detect", HEADPHONE_DETECT},
292 {"lineout-detect", LINEOUT_DETECT},
293 {"line-output-detect", LINEOUT_DETECT},
294 {"audio-hw-reset", AUDIO_HW_RESET},
295 {"hw-reset", AUDIO_HW_RESET},
296 {NULL, GPIO_CTRL_NUM}
299 static void i2s_cint(struct i2s_softc *);
302 aoagpio_int(void *cookie)
304 device_t self = cookie;
305 struct aoagpio_softc *sc;
307 sc = device_get_softc(self);
309 if (macgpio_read(self) & GPIO_LEVEL_RO)
310 sc->level = sc->detect_active;
312 sc->level = !(sc->detect_active);
319 aoagpio_probe(device_t gpio)
324 struct gpio_match *m;
325 struct aoagpio_softc *sc;
327 node = ofw_bus_get_node(gpio);
328 if (node == 0 || node == -1)
331 bzero(bname, sizeof(bname));
332 if (OF_getprop(node, "audio-gpio", bname, sizeof(bname)) > 2)
335 name = ofw_bus_get_name(gpio);
337 /* Try to find a match. */
338 for (m = gpio_controls; m->name != NULL; m++) {
339 if (strcmp(name, m->name) == 0) {
340 sc = device_get_softc(gpio);
341 gpio_ctrls[m->ctrl] = sc;
345 sc->detect_active = 0;
348 OF_getprop(node, "audio-gpio-active-state",
349 &sc->detect_active, sizeof(sc->detect_active));
351 if ((1 << m->ctrl) & GPIO_CTRL_EXTINT_SET)
354 device_set_desc(gpio, m->name);
364 aoagpio_attach(device_t gpio)
366 struct aoagpio_softc *sc;
371 sc = device_get_softc(gpio);
373 if ((1 << sc->ctrl) & GPIO_CTRL_EXTINT_SET) {
374 r = bus_alloc_resource_any(gpio, SYS_RES_IRQ, &rid, RF_ACTIVE);
378 irq = rman_get_start(r);
379 DPRINTF(("interrupting at irq %d\n", irq));
381 if (powerpc_config_intr(irq, INTR_TRIGGER_EDGE,
382 INTR_POLARITY_LOW) != 0)
385 bus_setup_intr(gpio, r, INTR_TYPE_MISC | INTR_MPSAFE |
386 INTR_ENTROPY, NULL, aoagpio_int, gpio, &cookie);
393 * I2S module registers
396 #define I2S_FORMAT 0x10
397 #define I2S_FRAMECOUNT 0x40
398 #define I2S_FRAMEMATCH 0x50
399 #define I2S_WORDSIZE 0x60
401 /* I2S_INT register definitions */
402 #define I2S_INT_CLKSTOPPEND 0x01000000 /* clock-stop interrupt pending */
404 /* I2S_FORMAT register definitions */
405 #define CLKSRC_49MHz 0x80000000 /* Use 49152000Hz Osc. */
406 #define CLKSRC_45MHz 0x40000000 /* Use 45158400Hz Osc. */
407 #define CLKSRC_18MHz 0x00000000 /* Use 18432000Hz Osc. */
408 #define MCLK_DIV_MASK 0x1f000000 /* MCLK = SRC / DIV */
409 #define SCLK_DIV_MASK 0x00f00000 /* SCLK = MCLK / DIV */
410 #define SCLK_MASTER 0x00080000 /* Master mode */
411 #define SCLK_SLAVE 0x00000000 /* Slave mode */
412 #define SERIAL_FORMAT 0x00070000
413 #define SERIAL_SONY 0x00000000
414 #define SERIAL_64x 0x00010000
415 #define SERIAL_32x 0x00020000
416 #define SERIAL_DAV 0x00040000
417 #define SERIAL_SILICON 0x00050000
419 /* I2S_WORDSIZE register definitions */
420 #define INPUT_STEREO (2 << 24)
421 #define INPUT_MONO (1 << 24)
422 #define INPUT_16BIT (0 << 16)
423 #define INPUT_24BIT (3 << 16)
424 #define OUTPUT_STEREO (2 << 8)
425 #define OUTPUT_MONO (1 << 8)
426 #define OUTPUT_16BIT (0 << 0)
427 #define OUTPUT_24BIT (3 << 0)
429 /* Master clock, needed by some codecs. We hardcode this
430 to 256 * fs as this is valid for most codecs. */
433 /* Number of clock sources we can use. */
435 static const struct i2s_clksrc {
439 {49152000, CLKSRC_49MHz},
440 {45158400, CLKSRC_45MHz},
441 {18432000, CLKSRC_18MHz}
444 /* Configure the I2S controller for the required settings.
445 'rate' is the frame rate.
446 'wordsize' is the sample size (usually 16 bits).
447 'sclk_fs' is the SCLK/framerate ratio, which needs to be equal
448 or greater to the number of bits per frame. */
451 i2s_setup(struct i2s_softc *sc, u_int rate, u_int wordsize, u_int sclk_fs)
453 u_int mclk, mdiv, sdiv;
454 u_int reg = 0, x, wordformat;
457 /* Make sure the settings are consistent... */
458 if ((wordsize * 2) > sclk_fs)
461 if (sclk_fs != 32 && sclk_fs != 64)
465 * Find a clock source to derive the master clock (MCLK)
466 * and the I2S bit block (SCLK) and set the divisors as
469 mclk = rate * MCLK_FS;
470 sdiv = MCLK_FS / sclk_fs;
472 for (i = 0; i < NCLKS; ++i) {
473 if ((clksrc[i].cs_clock % mclk) == 0) {
474 reg = clksrc[i].cs_reg;
475 mdiv = clksrc[i].cs_clock / mclk;
483 /* exception cases */
497 reg |= (x << 24) & MCLK_DIV_MASK;
510 reg |= (x << 20) & SCLK_DIV_MASK;
513 * XXX use master mode for now. This needs to be
514 * revisited if we want to add recording from SPDIF some day.
527 /* stereo input and output */
528 wordformat = INPUT_STEREO | OUTPUT_STEREO;
532 wordformat |= INPUT_16BIT | OUTPUT_16BIT;
535 wordformat |= INPUT_24BIT | OUTPUT_24BIT;
541 x = bus_read_4(sc->reg, I2S_WORDSIZE);
543 bus_write_4(sc->reg, I2S_WORDSIZE, wordformat);
545 x = bus_read_4(sc->reg, I2S_FORMAT);
548 * XXX to change the format we need to stop the clock
549 * via the FCR registers. For now, rely on the firmware
550 * to set sane defaults (44100).
552 printf("i2s_setup: changing format not supported yet.\n");
556 if (obio_fcr_isset(OBIO_FCR1, I2S0CLKEN)) {
558 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_INT,
559 I2S_INT_CLKSTOPPEND);
561 obio_fcr_clear(OBIO_FCR1, I2S0CLKEN);
563 for (timo = 1000; timo > 0; timo--) {
564 if (bus_space_read_4(sc->sc_tag, sc->sc_bsh,
565 I2S_INT) & I2S_INT_CLKSTOPPEND)
572 printf("%s: timeout waiting for clock to stop\n",
573 sc->sc_dev.dv_xname);
576 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT, reg);
578 obio_fcr_set(OBIO_FCR1, I2S0CLKEN);
586 /* XXX this does not belong here. */
588 of_find_firstchild_byname(phandle_t node, const char *req_name)
590 char name[32]; /* max name len per OF spec. */
593 for (n = OF_child(node); n != -1; n = OF_peer(n)) {
594 bzero(name, sizeof(name));
595 OF_getprop(n, "name", name, sizeof(name));
597 if (strcmp(name, req_name) == 0)
606 gpio_read(enum gpio_ctrl ctrl)
608 struct aoagpio_softc *sc;
610 if ((sc = gpio_ctrls[ctrl]) == NULL)
613 return (macgpio_read(sc->dev) & GPIO_DATA);
617 gpio_write(enum gpio_ctrl ctrl, u_int x)
619 struct aoagpio_softc *sc;
622 if ((sc = gpio_ctrls[ctrl]) == NULL)
625 reg = GPIO_DDR_OUTPUT;
629 macgpio_write(sc->dev, reg);
633 i2s_cint(struct i2s_softc *sc)
637 if (gpio_ctrls[HEADPHONE_DETECT] &&
638 gpio_ctrls[HEADPHONE_DETECT]->level)
641 if (gpio_ctrls[LINEOUT_DETECT] &&
642 gpio_ctrls[LINEOUT_DETECT]->level)
646 mask = 1 << 0; /* fall back to speakers. */
648 i2s_set_outputs(sc, mask);
651 #define reset_active 0
653 /* these values are in microseconds */
654 #define RESET_SETUP_TIME 5000
655 #define RESET_HOLD_TIME 20000
656 #define RESET_RELEASE_TIME 10000
659 i2s_audio_hw_reset(struct i2s_softc *sc)
661 if (gpio_ctrls[AUDIO_HW_RESET]) {
662 DPRINTF(("resetting codec\n"));
664 gpio_write(AUDIO_HW_RESET, !reset_active); /* Negate RESET */
665 DELAY(RESET_SETUP_TIME);
667 gpio_write(AUDIO_HW_RESET, reset_active); /* Assert RESET */
668 DELAY(RESET_HOLD_TIME);
670 gpio_write(AUDIO_HW_RESET, !reset_active); /* Negate RESET */
671 DELAY(RESET_RELEASE_TIME);
674 DPRINTF(("no audio_hw_reset\n"));
678 #define AMP_ACTIVE 0 /* XXX OF */
679 #define HEADPHONE_ACTIVE 0 /* XXX OF */
680 #define LINEOUT_ACTIVE 0 /* XXX OF */
682 #define MUTE_CONTROL(xxx, yyy) \
684 i2s_mute_##xxx(struct i2s_softc *sc, int mute) \
688 if (gpio_ctrls[yyy##_MUTE] == NULL) \
693 x = ! yyy##_ACTIVE; \
695 if (x != gpio_read(yyy##_MUTE)) \
696 gpio_write(yyy##_MUTE, x); \
699 MUTE_CONTROL(speaker, AMP)
700 MUTE_CONTROL(headphone, HEADPHONE)
701 MUTE_CONTROL(lineout, LINEOUT)
704 i2s_set_outputs(void *ptr, u_int mask)
706 struct i2s_softc *sc = ptr;
708 if (mask == sc->output_mask)
711 mtx_lock(&sc->port_mtx);
713 i2s_mute_speaker(sc, 1);
714 i2s_mute_headphone(sc, 1);
715 i2s_mute_lineout(sc, 1);
717 DPRINTF(("enabled outputs: "));
719 if (mask & (1 << 0)) {
720 DPRINTF(("SPEAKER "));
721 i2s_mute_speaker(sc, 0);
723 if (mask & (1 << 1)) {
724 DPRINTF(("HEADPHONE "));
725 i2s_mute_headphone(sc, 0);
727 if (mask & (1 << 2)) {
728 DPRINTF(("LINEOUT "));
729 i2s_mute_lineout(sc, 0);
733 sc->output_mask = mask;
735 mtx_unlock(&sc->port_mtx);
739 i2s_postattach(void *xsc)
741 struct i2s_softc *sc = xsc;
745 self = sc->aoa.sc_dev;
747 /* Reset the codec. */
748 i2s_audio_hw_reset(sc);
750 /* If we have a codec, initialize it. */
752 mixer_init(self, i2s_mixer_class, i2s_mixer);
754 /* Read initial port status. */
757 /* Enable GPIO interrupt callback. */
758 for (i = 0; i < GPIO_CTRL_NUM; i++)
760 gpio_ctrls[i]->i2s = sc;
762 config_intrhook_disestablish(i2s_delayed_attach);
763 free(i2s_delayed_attach, M_TEMP);