2 * Copyright (c) 2016 Ruslan Bukin <br@bsdpad.com>
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 /* Ingenic JZ4780 Audio Interface Controller (AIC). */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/resource.h>
47 #include <machine/bus.h>
49 #include <dev/fdt/fdt_common.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
53 #include <dev/sound/pcm/sound.h>
54 #include <dev/sound/chip.h>
57 #include <dev/extres/clk/clk.h>
58 #include <dev/xdma/xdma.h>
60 #include <mips/ingenic/jz4780_common.h>
61 #include <mips/ingenic/jz4780_aic.h>
63 #define AIC_NCHANNELS 1
67 struct resource *res[1];
69 bus_space_handle_t bsh;
72 bus_dma_tag_t dma_tag;
74 bus_addr_t buf_base_phys;
76 uintptr_t aic_fifo_paddr;
82 struct xdma_channel *xchan;
83 xdma_controller_t *xdma_tx;
87 /* Channel registers */
89 struct snd_dbuf *buffer;
90 struct pcm_channel *channel;
91 struct sc_pcminfo *parent;
93 /* Channel information */
101 /* PCM device private data */
105 struct sc_chinfo chan[AIC_NCHANNELS];
106 struct aic_softc *sc;
109 static struct resource_spec aic_spec[] = {
110 { SYS_RES_MEMORY, 0, RF_ACTIVE },
114 static int aic_probe(device_t dev);
115 static int aic_attach(device_t dev);
116 static int aic_detach(device_t dev);
117 static int setup_xdma(struct sc_pcminfo *scp);
123 static struct aic_rate rate_map[] = {
125 /* TODO: add more frequences */
133 aicmixer_init(struct snd_mixer *m)
135 struct sc_pcminfo *scp;
136 struct aic_softc *sc;
139 scp = mix_getdevinfo(m);
145 mask = SOUND_MASK_PCM;
147 snd_mtxlock(sc->lock);
148 pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL);
149 mix_setdevs(m, mask);
150 snd_mtxunlock(sc->lock);
156 aicmixer_set(struct snd_mixer *m, unsigned dev,
157 unsigned left, unsigned right)
159 struct sc_pcminfo *scp;
161 scp = mix_getdevinfo(m);
163 /* Here we can configure hardware volume on our DAC */
168 static kobj_method_t aicmixer_methods[] = {
169 KOBJMETHOD(mixer_init, aicmixer_init),
170 KOBJMETHOD(mixer_set, aicmixer_set),
173 MIXER_DECLARE(aicmixer);
179 aicchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
180 struct pcm_channel *c, int dir)
182 struct sc_pcminfo *scp;
183 struct sc_chinfo *ch;
184 struct aic_softc *sc;
186 scp = (struct sc_pcminfo *)devinfo;
189 snd_mtxlock(sc->lock);
196 snd_mtxunlock(sc->lock);
198 if (sndbuf_setup(ch->buffer, sc->buf_base, sc->dma_size) != 0) {
199 device_printf(scp->dev, "Can't setup sndbuf.\n");
207 aicchan_free(kobj_t obj, void *data)
209 struct sc_chinfo *ch = data;
210 struct sc_pcminfo *scp = ch->parent;
211 struct aic_softc *sc = scp->sc;
213 snd_mtxlock(sc->lock);
214 /* TODO: free channel buffer */
215 snd_mtxunlock(sc->lock);
221 aicchan_setformat(kobj_t obj, void *data, uint32_t format)
223 struct sc_pcminfo *scp;
224 struct sc_chinfo *ch;
235 aicchan_setspeed(kobj_t obj, void *data, uint32_t speed)
237 struct sc_pcminfo *scp;
238 struct sc_chinfo *ch;
240 struct aic_softc *sc;
250 /* First look for equal frequency. */
251 for (i = 0; rate_map[i].speed != 0; i++) {
252 if (rate_map[i].speed == speed)
256 /* If no match, just find nearest. */
258 for (i = 0; rate_map[i].speed != 0; i++) {
260 threshold = sr->speed + ((rate_map[i + 1].speed != 0) ?
261 ((rate_map[i + 1].speed - sr->speed) >> 1) : 0);
262 if (speed < threshold)
269 /* Clocks can be reconfigured here. */
275 aicchan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
277 struct sc_pcminfo *scp;
278 struct sc_chinfo *ch;
279 struct aic_softc *sc;
285 sndbuf_resize(ch->buffer, sc->dma_size / blocksize, blocksize);
287 return (sndbuf_getblksz(ch->buffer));
293 struct sc_pcminfo *scp;
294 xdma_channel_t *xchan;
295 struct sc_chinfo *ch;
296 struct aic_softc *sc;
307 bufsize = sndbuf_getsize(ch->buffer);
309 sc->pos += conf->block_len;
310 if (sc->pos >= bufsize)
314 chn_intr(ch->channel);
320 setup_xdma(struct sc_pcminfo *scp)
322 struct aic_softc *sc;
323 struct sc_chinfo *ch;
330 fmt = sndbuf_getfmt(ch->buffer);
332 KASSERT(fmt & AFMT_16BIT, ("16-bit audio supported only."));
334 err = xdma_prep_cyclic(sc->xchan,
335 XDMA_MEM_TO_DEV, /* direction */
336 sc->buf_base_phys, /* src addr */
337 sc->aic_fifo_paddr, /* dst addr */
338 sndbuf_getblksz(ch->buffer), /* block len */
339 sndbuf_getblkcnt(ch->buffer), /* block num */
340 2, /* src port width */
341 2); /* dst port width */
343 device_printf(sc->dev, "Can't configure virtual channel\n");
347 xdma_begin(sc->xchan);
353 aic_start(struct sc_pcminfo *scp)
355 struct aic_softc *sc;
360 /* Ensure clock enabled. */
361 reg = READ4(sc, I2SCR);
362 reg |= (I2SCR_ESCLK);
363 WRITE4(sc, I2SCR, reg);
367 reg = (AICCR_OSS_16 | AICCR_ISS_16);
368 reg |= (AICCR_CHANNEL_2);
371 WRITE4(sc, AICCR, reg);
377 aic_stop(struct sc_pcminfo *scp)
379 struct aic_softc *sc;
384 reg = READ4(sc, AICCR);
385 reg &= ~(AICCR_TDMS | AICCR_ERPL);
386 WRITE4(sc, AICCR, reg);
388 xdma_terminate(sc->xchan);
394 aicchan_trigger(kobj_t obj, void *data, int go)
396 struct sc_pcminfo *scp;
397 struct sc_chinfo *ch;
398 struct aic_softc *sc;
404 snd_mtxlock(sc->lock);
424 bzero(sc->buf_base, sc->dma_size);
429 snd_mtxunlock(sc->lock);
435 aicchan_getptr(kobj_t obj, void *data)
437 struct sc_pcminfo *scp;
438 struct sc_chinfo *ch;
439 struct aic_softc *sc;
448 static uint32_t aic_pfmt[] = {
449 SND_FORMAT(AFMT_S16_LE, 2, 0),
453 static struct pcmchan_caps aic_pcaps = {48000, 48000, aic_pfmt, 0};
455 static struct pcmchan_caps *
456 aicchan_getcaps(kobj_t obj, void *data)
462 static kobj_method_t aicchan_methods[] = {
463 KOBJMETHOD(channel_init, aicchan_init),
464 KOBJMETHOD(channel_free, aicchan_free),
465 KOBJMETHOD(channel_setformat, aicchan_setformat),
466 KOBJMETHOD(channel_setspeed, aicchan_setspeed),
467 KOBJMETHOD(channel_setblocksize, aicchan_setblocksize),
468 KOBJMETHOD(channel_trigger, aicchan_trigger),
469 KOBJMETHOD(channel_getptr, aicchan_getptr),
470 KOBJMETHOD(channel_getcaps, aicchan_getcaps),
473 CHANNEL_DECLARE(aicchan);
476 aic_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
483 addr = (bus_addr_t*)arg;
484 *addr = segs[0].ds_addr;
488 aic_dma_setup(struct aic_softc *sc)
495 /* DMA buffer size. */
496 sc->dma_size = 131072;
499 * Must use dma_size boundary as modulo feature required.
500 * Modulo feature allows setup circular buffer.
502 err = bus_dma_tag_create(
503 bus_get_dma_tag(sc->dev),
504 4, sc->dma_size, /* alignment, boundary */
505 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
506 BUS_SPACE_MAXADDR, /* highaddr */
507 NULL, NULL, /* filter, filterarg */
508 sc->dma_size, 1, /* maxsize, nsegments */
509 sc->dma_size, 0, /* maxsegsize, flags */
510 NULL, NULL, /* lockfunc, lockarg */
513 device_printf(dev, "cannot create bus dma tag\n");
517 err = bus_dmamem_alloc(sc->dma_tag, (void **)&sc->buf_base,
518 BUS_DMA_WAITOK | BUS_DMA_COHERENT, &sc->dma_map);
520 device_printf(dev, "cannot allocate memory\n");
524 err = bus_dmamap_load(sc->dma_tag, sc->dma_map, sc->buf_base,
525 sc->dma_size, aic_dmamap_cb, &sc->buf_base_phys, BUS_DMA_WAITOK);
527 device_printf(dev, "cannot load DMA map\n");
531 bzero(sc->buf_base, sc->dma_size);
537 aic_configure_clocks(struct aic_softc *sc)
546 err = clk_get_by_ofw_name(sc->dev, 0, "aic", &sc->clk_aic);
548 device_printf(dev, "Can't find aic clock.\n");
552 err = clk_enable(sc->clk_aic);
554 device_printf(dev, "Can't enable aic clock.\n");
558 err = clk_get_by_ofw_name(sc->dev, 0, "i2s", &sc->clk_i2s);
560 device_printf(dev, "Can't find i2s clock.\n");
564 err = clk_enable(sc->clk_i2s);
566 device_printf(dev, "Can't enable i2s clock.\n");
570 err = clk_set_freq(sc->clk_i2s, 12000000, 0);
572 device_printf(dev, "Can't set i2s frequency.\n");
576 clk_get_freq(sc->clk_aic, &aic_freq);
577 clk_get_freq(sc->clk_i2s, &i2s_freq);
579 device_printf(dev, "Frequency aic %d i2s %d\n",
580 (uint32_t)aic_freq, (uint32_t)i2s_freq);
586 aic_configure(struct aic_softc *sc)
590 WRITE4(sc, AICFR, AICFR_RST);
594 if (sc->internal_codec) {
597 reg |= (AICFR_SYNCD | AICFR_BCKD);
599 reg |= (AICFR_AUSEL); /* I2S/MSB-justified format. */
600 reg |= (AICFR_TFTH(8)); /* Transmit FIFO threshold */
601 reg |= (AICFR_RFTH(7)); /* Receive FIFO threshold */
602 WRITE4(sc, AICFR, reg);
604 reg = READ4(sc, AICFR);
605 reg |= (AICFR_ENB); /* Enable the controller. */
606 WRITE4(sc, AICFR, reg);
612 sysctl_hw_pcm_internal_codec(SYSCTL_HANDLER_ARGS)
614 struct sc_pcminfo *scp;
615 struct sc_chinfo *ch;
616 struct aic_softc *sc;
626 snd_mtxlock(sc->lock);
628 val = sc->internal_codec;
629 error = sysctl_handle_int(oidp, &val, 0, req);
630 if (error || req->newptr == NULL) {
631 snd_mtxunlock(sc->lock);
634 if (val < 0 || val > 1) {
635 snd_mtxunlock(sc->lock);
639 if (sc->internal_codec != val) {
640 sc->internal_codec = val;
648 snd_mtxunlock(sc->lock);
654 aic_probe(device_t dev)
657 if (!ofw_bus_status_okay(dev))
660 if (!ofw_bus_is_compatible(dev, "ingenic,jz4780-i2s"))
663 device_set_desc(dev, "Audio Interface Controller");
665 return (BUS_PROBE_DEFAULT);
669 aic_attach(device_t dev)
671 char status[SND_STATUSLEN];
672 struct sc_pcminfo *scp;
673 struct aic_softc *sc;
676 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
679 sc->internal_codec = 1;
681 /* Get xDMA controller */
682 sc->xdma_tx = xdma_ofw_get(sc->dev, "tx");
683 if (sc->xdma_tx == NULL) {
684 device_printf(dev, "Can't find DMA controller.\n");
688 /* Alloc xDMA virtual channel. */
689 sc->xchan = xdma_channel_alloc(sc->xdma_tx);
690 if (sc->xchan == NULL) {
691 device_printf(dev, "Can't alloc virtual DMA channel.\n");
695 /* Setup sound subsystem */
696 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "aic softc");
697 if (sc->lock == NULL) {
698 device_printf(dev, "Can't create mtx.\n");
702 if (bus_alloc_resources(dev, aic_spec, sc->res)) {
704 "could not allocate resources for device\n");
708 /* Memory interface */
709 sc->bst = rman_get_bustag(sc->res[0]);
710 sc->bsh = rman_get_bushandle(sc->res[0]);
711 sc->aic_fifo_paddr = rman_get_start(sc->res[0]) + AICDR;
714 scp = malloc(sizeof(struct sc_pcminfo), M_DEVBUF, M_WAITOK | M_ZERO);
718 /* Setup audio buffer. */
719 err = aic_dma_setup(sc);
721 device_printf(dev, "Can't setup sound buffer.\n");
726 err = aic_configure_clocks(sc);
728 device_printf(dev, "Can't configure clocks.\n");
732 err = aic_configure(sc);
734 device_printf(dev, "Can't configure AIC.\n");
738 pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
740 /* Setup interrupt handler. */
741 err = xdma_setup_intr(sc->xchan, aic_intr, scp, &sc->ih);
743 device_printf(sc->dev,
744 "Can't setup xDMA interrupt handler.\n");
748 err = pcm_register(dev, scp, 1, 0);
750 device_printf(dev, "Can't register pcm.\n");
755 pcm_addchan(dev, PCMDIR_PLAY, &aicchan_class, scp);
758 snprintf(status, SND_STATUSLEN, "at %s", ofw_bus_get_name(dev));
759 pcm_setstatus(dev, status);
761 mixer_init(dev, &aicmixer_class, scp);
763 /* Create device sysctl node. */
764 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
765 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
766 OID_AUTO, "internal_codec", CTLTYPE_INT | CTLFLAG_RW,
767 scp, 0, sysctl_hw_pcm_internal_codec, "I",
768 "use internal audio codec");
774 aic_detach(device_t dev)
776 struct aic_softc *sc;
778 sc = device_get_softc(dev);
780 xdma_channel_free(sc->xchan);
782 bus_release_resources(dev, aic_spec, sc->res);
787 static device_method_t aic_pcm_methods[] = {
788 /* Device interface */
789 DEVMETHOD(device_probe, aic_probe),
790 DEVMETHOD(device_attach, aic_attach),
791 DEVMETHOD(device_detach, aic_detach),
795 static driver_t aic_pcm_driver = {
801 DRIVER_MODULE(aic, simplebus, aic_pcm_driver, pcm_devclass, 0, 0);
802 MODULE_DEPEND(aic, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
803 MODULE_VERSION(aic, 1);