2 * Copyright 2008 by Marco Trillo. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
28 * Copyright (c) 2002, 2003 Tsubai Masanari. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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.
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
57 * Apple I2S audio controller.
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/module.h>
65 #include <sys/malloc.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>
74 #include <dev/ofw/ofw_bus.h>
76 #ifdef HAVE_KERNEL_OPTION_HEADERS
80 #include <dev/sound/pcm/sound.h>
81 #include <dev/sound/macio/aoa.h>
83 #include <powerpc/powermac/macgpiovar.h>
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);
103 static struct intr_config_hook *i2s_delayed_attach = NULL;
105 kobj_class_t i2s_mixer_class = NULL;
106 device_t i2s_mixer = NULL;
108 static device_method_t pcm_i2s_methods[] = {
109 /* Device interface. */
110 DEVMETHOD(device_probe, i2s_probe),
111 DEVMETHOD(device_attach, i2s_attach),
116 static driver_t pcm_i2s_driver = {
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);
125 static int aoagpio_probe(device_t);
126 static int aoagpio_attach(device_t);
128 static device_method_t aoagpio_methods[] = {
129 /* Device interface. */
130 DEVMETHOD(device_probe, aoagpio_probe),
131 DEVMETHOD(device_attach, aoagpio_attach),
136 struct aoagpio_softc {
139 int detect_active; /* for extint-gpio */
140 int level; /* for extint-gpio */
141 struct i2s_softc *i2s; /* for extint-gpio */
144 static driver_t aoagpio_driver = {
147 sizeof(struct aoagpio_softc)
149 static devclass_t aoagpio_devclass;
151 DRIVER_MODULE(aoagpio, macgpio, aoagpio_driver, aoagpio_devclass, 0, 0);
154 /*****************************************************************************
155 Probe and attachment routines.
156 *****************************************************************************/
158 i2s_probe(device_t self)
162 char subchildname[255];
164 name = ofw_bus_get_name(self);
168 if (strcmp(name, "i2s") != 0)
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.
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)
181 device_set_desc(self, "Apple I2S Audio Controller");
186 static phandle_t of_find_firstchild_byname(phandle_t, const char *);
189 i2s_attach(device_t self)
191 struct i2s_softc *sc;
192 struct resource *dbdma_irq;
197 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
199 sc->aoa.sc_dev = self;
200 sc->node = ofw_bus_get_node(self);
202 port = of_find_firstchild_byname(sc->node, "i2s-a");
205 sc->soundnode = of_find_firstchild_byname(port, "sound");
206 if (sc->soundnode == -1)
209 mtx_init(&sc->port_mtx, "port_mtx", NULL, MTX_DEF);
211 /* Map the controller register space. */
213 sc->reg = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
217 /* Map the DBDMA channel register space. */
219 sc->aoa.sc_odma = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
221 if (sc->aoa.sc_odma == NULL)
224 /* Establish the DBDMA channel edge-triggered interrupt. */
226 dbdma_irq = bus_alloc_resource_any(self, SYS_RES_IRQ,
227 &rid, RF_SHAREABLE | RF_ACTIVE);
228 if (dbdma_irq == NULL)
231 /* Now initialize the controller. */
232 err = i2s_setup(sc, 44100, 16, 64);
236 snd_setup_intr(self, dbdma_irq, INTR_MPSAFE, aoa_interrupt,
239 oirq = rman_get_start(dbdma_irq);
240 err = powerpc_config_intr(oirq, INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
245 * Register a hook for delayed attach in order to allow
246 * the I2C controller to attach.
248 if ((i2s_delayed_attach = malloc(sizeof(struct intr_config_hook),
249 M_TEMP, M_WAITOK | M_ZERO)) == NULL)
252 i2s_delayed_attach->ich_func = i2s_postattach;
253 i2s_delayed_attach->ich_arg = sc;
255 if (config_intrhook_establish(i2s_delayed_attach) != 0)
258 return (aoa_attach(sc));
261 /*****************************************************************************
263 *****************************************************************************/
275 #define GPIO_CTRL_EXTINT_SET \
276 ((1 << HEADPHONE_DETECT) | \
277 (1 << LINEOUT_DETECT))
279 static struct aoagpio_softc *gpio_ctrls[GPIO_CTRL_NUM] =
280 {NULL, NULL, NULL, NULL, NULL, NULL};
282 static struct gpio_match {
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}
297 static void i2s_cint(struct i2s_softc *);
300 aoagpio_int(void *cookie)
302 device_t self = cookie;
303 struct aoagpio_softc *sc;
305 sc = device_get_softc(self);
307 if (macgpio_read(self) & GPIO_LEVEL_RO)
308 sc->level = sc->detect_active;
310 sc->level = !(sc->detect_active);
317 aoagpio_probe(device_t gpio)
322 struct gpio_match *m;
323 struct aoagpio_softc *sc;
325 node = ofw_bus_get_node(gpio);
326 if (node == 0 || node == -1)
329 bzero(bname, sizeof(bname));
330 if (OF_getprop(node, "audio-gpio", bname, sizeof(bname)) > 2)
333 name = ofw_bus_get_name(gpio);
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;
343 sc->detect_active = 0;
346 OF_getprop(node, "audio-gpio-active-state",
347 &sc->detect_active, sizeof(sc->detect_active));
349 if ((1 << m->ctrl) & GPIO_CTRL_EXTINT_SET)
352 device_set_desc(gpio, m->name);
362 aoagpio_attach(device_t gpio)
364 struct aoagpio_softc *sc;
369 sc = device_get_softc(gpio);
371 if ((1 << sc->ctrl) & GPIO_CTRL_EXTINT_SET) {
372 r = bus_alloc_resource_any(gpio, SYS_RES_IRQ, &rid, RF_ACTIVE);
376 irq = rman_get_start(r);
377 DPRINTF(("interrupting at irq %d\n", irq));
379 if (powerpc_config_intr(irq, INTR_TRIGGER_EDGE,
380 INTR_POLARITY_LOW) != 0)
383 bus_setup_intr(gpio, r, INTR_TYPE_MISC | INTR_MPSAFE |
384 INTR_ENTROPY, NULL, aoagpio_int, gpio, &cookie);
391 * I2S module registers
394 #define I2S_FORMAT 0x10
395 #define I2S_FRAMECOUNT 0x40
396 #define I2S_FRAMEMATCH 0x50
397 #define I2S_WORDSIZE 0x60
399 /* I2S_INT register definitions */
400 #define I2S_INT_CLKSTOPPEND 0x01000000 /* clock-stop interrupt pending */
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
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)
427 /* Master clock, needed by some codecs. We hardcode this
428 to 256 * fs as this is valid for most codecs. */
431 /* Number of clock sources we can use. */
433 static const struct i2s_clksrc {
437 {49152000, CLKSRC_49MHz},
438 {45158400, CLKSRC_45MHz},
439 {18432000, CLKSRC_18MHz}
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. */
449 i2s_setup(struct i2s_softc *sc, u_int rate, u_int wordsize, u_int sclk_fs)
451 u_int mclk, mdiv, sdiv;
452 u_int reg = 0, x, wordformat;
455 /* Make sure the settings are consistent... */
456 if ((wordsize * 2) > sclk_fs)
459 if (sclk_fs != 32 && sclk_fs != 64)
463 * Find a clock source to derive the master clock (MCLK)
464 * and the I2S bit block (SCLK) and set the divisors as
467 mclk = rate * MCLK_FS;
468 sdiv = MCLK_FS / sclk_fs;
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;
481 /* exception cases */
495 reg |= (x << 24) & MCLK_DIV_MASK;
508 reg |= (x << 20) & SCLK_DIV_MASK;
511 * XXX use master mode for now. This needs to be
512 * revisited if we want to add recording from SPDIF some day.
525 /* stereo input and output */
526 wordformat = INPUT_STEREO | OUTPUT_STEREO;
530 wordformat |= INPUT_16BIT | OUTPUT_16BIT;
533 wordformat |= INPUT_24BIT | OUTPUT_24BIT;
539 x = bus_read_4(sc->reg, I2S_WORDSIZE);
541 bus_write_4(sc->reg, I2S_WORDSIZE, wordformat);
543 x = bus_read_4(sc->reg, I2S_FORMAT);
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).
550 printf("i2s_setup: changing format not supported yet.\n");
554 if (obio_fcr_isset(OBIO_FCR1, I2S0CLKEN)) {
556 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_INT,
557 I2S_INT_CLKSTOPPEND);
559 obio_fcr_clear(OBIO_FCR1, I2S0CLKEN);
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)
570 printf("%s: timeout waiting for clock to stop\n",
571 sc->sc_dev.dv_xname);
574 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT, reg);
576 obio_fcr_set(OBIO_FCR1, I2S0CLKEN);
584 /* XXX this does not belong here. */
586 of_find_firstchild_byname(phandle_t node, const char *req_name)
588 char name[32]; /* max name len per OF spec. */
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));
595 if (strcmp(name, req_name) == 0)
604 gpio_read(enum gpio_ctrl ctrl)
606 struct aoagpio_softc *sc;
608 if ((sc = gpio_ctrls[ctrl]) == NULL)
611 return (macgpio_read(sc->dev) & GPIO_DATA);
615 gpio_write(enum gpio_ctrl ctrl, u_int x)
617 struct aoagpio_softc *sc;
620 if ((sc = gpio_ctrls[ctrl]) == NULL)
623 reg = GPIO_DDR_OUTPUT;
627 macgpio_write(sc->dev, reg);
631 i2s_cint(struct i2s_softc *sc)
635 if (gpio_ctrls[HEADPHONE_DETECT] &&
636 gpio_ctrls[HEADPHONE_DETECT]->level)
639 if (gpio_ctrls[LINEOUT_DETECT] &&
640 gpio_ctrls[LINEOUT_DETECT]->level)
644 mask = 1 << 0; /* fall back to speakers. */
646 i2s_set_outputs(sc, mask);
649 #define reset_active 0
651 /* these values are in microseconds */
652 #define RESET_SETUP_TIME 5000
653 #define RESET_HOLD_TIME 20000
654 #define RESET_RELEASE_TIME 10000
657 i2s_audio_hw_reset(struct i2s_softc *sc)
659 if (gpio_ctrls[AUDIO_HW_RESET]) {
660 DPRINTF(("resetting codec\n"));
662 gpio_write(AUDIO_HW_RESET, !reset_active); /* Negate RESET */
663 DELAY(RESET_SETUP_TIME);
665 gpio_write(AUDIO_HW_RESET, reset_active); /* Assert RESET */
666 DELAY(RESET_HOLD_TIME);
668 gpio_write(AUDIO_HW_RESET, !reset_active); /* Negate RESET */
669 DELAY(RESET_RELEASE_TIME);
672 DPRINTF(("no audio_hw_reset\n"));
676 #define AMP_ACTIVE 0 /* XXX OF */
677 #define HEADPHONE_ACTIVE 0 /* XXX OF */
678 #define LINEOUT_ACTIVE 0 /* XXX OF */
680 #define MUTE_CONTROL(xxx, yyy) \
682 i2s_mute_##xxx(struct i2s_softc *sc, int mute) \
686 if (gpio_ctrls[yyy##_MUTE] == NULL) \
691 x = ! yyy##_ACTIVE; \
693 if (x != gpio_read(yyy##_MUTE)) \
694 gpio_write(yyy##_MUTE, x); \
697 MUTE_CONTROL(speaker, AMP)
698 MUTE_CONTROL(headphone, HEADPHONE)
699 MUTE_CONTROL(lineout, LINEOUT)
702 i2s_set_outputs(void *ptr, u_int mask)
704 struct i2s_softc *sc = ptr;
706 if (mask == sc->output_mask)
709 mtx_lock(&sc->port_mtx);
711 i2s_mute_speaker(sc, 1);
712 i2s_mute_headphone(sc, 1);
713 i2s_mute_lineout(sc, 1);
715 DPRINTF(("enabled outputs: "));
717 if (mask & (1 << 0)) {
718 DPRINTF(("SPEAKER "));
719 i2s_mute_speaker(sc, 0);
721 if (mask & (1 << 1)) {
722 DPRINTF(("HEADPHONE "));
723 i2s_mute_headphone(sc, 0);
725 if (mask & (1 << 2)) {
726 DPRINTF(("LINEOUT "));
727 i2s_mute_lineout(sc, 0);
731 sc->output_mask = mask;
733 mtx_unlock(&sc->port_mtx);
737 i2s_postattach(void *xsc)
739 struct i2s_softc *sc = xsc;
743 self = sc->aoa.sc_dev;
745 /* Reset the codec. */
746 i2s_audio_hw_reset(sc);
748 /* If we have a codec, initialize it. */
750 mixer_init(self, i2s_mixer_class, i2s_mixer);
752 /* Read initial port status. */
755 /* Enable GPIO interrupt callback. */
756 for (i = 0; i < GPIO_CTRL_NUM; i++)
758 gpio_ctrls[i]->i2s = sc;
760 config_intrhook_disestablish(i2s_delayed_attach);
761 free(i2s_delayed_attach, M_TEMP);