2 * Copyright (c) 2016-2018 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;
85 struct xdma_channel *xchan;
86 xdma_controller_t *xdma_tx;
87 struct xdma_request req;
90 /* Channel registers */
92 struct snd_dbuf *buffer;
93 struct pcm_channel *channel;
94 struct sc_pcminfo *parent;
96 /* Channel information */
104 /* PCM device private data */
108 struct sc_chinfo chan[AIC_NCHANNELS];
109 struct aic_softc *sc;
112 static struct resource_spec aic_spec[] = {
113 { SYS_RES_MEMORY, 0, RF_ACTIVE },
117 static int aic_probe(device_t dev);
118 static int aic_attach(device_t dev);
119 static int aic_detach(device_t dev);
120 static int setup_xdma(struct sc_pcminfo *scp);
126 static struct aic_rate rate_map[] = {
128 /* TODO: add more frequences */
136 aicmixer_init(struct snd_mixer *m)
138 struct sc_pcminfo *scp;
139 struct aic_softc *sc;
142 scp = mix_getdevinfo(m);
148 mask = SOUND_MASK_PCM;
150 snd_mtxlock(sc->lock);
151 pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL);
152 mix_setdevs(m, mask);
153 snd_mtxunlock(sc->lock);
159 aicmixer_set(struct snd_mixer *m, unsigned dev,
160 unsigned left, unsigned right)
162 struct sc_pcminfo *scp;
164 scp = mix_getdevinfo(m);
166 /* Here we can configure hardware volume on our DAC */
171 static kobj_method_t aicmixer_methods[] = {
172 KOBJMETHOD(mixer_init, aicmixer_init),
173 KOBJMETHOD(mixer_set, aicmixer_set),
176 MIXER_DECLARE(aicmixer);
182 aicchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
183 struct pcm_channel *c, int dir)
185 struct sc_pcminfo *scp;
186 struct sc_chinfo *ch;
187 struct aic_softc *sc;
189 scp = (struct sc_pcminfo *)devinfo;
192 snd_mtxlock(sc->lock);
199 snd_mtxunlock(sc->lock);
201 if (sndbuf_setup(ch->buffer, sc->buf_base, sc->dma_size) != 0) {
202 device_printf(scp->dev, "Can't setup sndbuf.\n");
210 aicchan_free(kobj_t obj, void *data)
212 struct sc_chinfo *ch = data;
213 struct sc_pcminfo *scp = ch->parent;
214 struct aic_softc *sc = scp->sc;
216 snd_mtxlock(sc->lock);
217 /* TODO: free channel buffer */
218 snd_mtxunlock(sc->lock);
224 aicchan_setformat(kobj_t obj, void *data, uint32_t format)
226 struct sc_pcminfo *scp;
227 struct sc_chinfo *ch;
238 aicchan_setspeed(kobj_t obj, void *data, uint32_t speed)
240 struct sc_pcminfo *scp;
241 struct sc_chinfo *ch;
243 struct aic_softc *sc;
253 /* First look for equal frequency. */
254 for (i = 0; rate_map[i].speed != 0; i++) {
255 if (rate_map[i].speed == speed)
259 /* If no match, just find nearest. */
261 for (i = 0; rate_map[i].speed != 0; i++) {
263 threshold = sr->speed + ((rate_map[i + 1].speed != 0) ?
264 ((rate_map[i + 1].speed - sr->speed) >> 1) : 0);
265 if (speed < threshold)
272 /* Clocks can be reconfigured here. */
278 aicchan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
280 struct sc_pcminfo *scp;
281 struct sc_chinfo *ch;
282 struct aic_softc *sc;
288 sndbuf_resize(ch->buffer, sc->dma_size / blocksize, blocksize);
290 return (sndbuf_getblksz(ch->buffer));
294 aic_intr(void *arg, xdma_transfer_status_t *status)
296 struct sc_pcminfo *scp;
297 struct xdma_request *req;
298 xdma_channel_t *xchan;
299 struct sc_chinfo *ch;
300 struct aic_softc *sc;
310 bufsize = sndbuf_getsize(ch->buffer);
312 sc->pos += req->block_len;
313 if (sc->pos >= bufsize)
317 chn_intr(ch->channel);
323 setup_xdma(struct sc_pcminfo *scp)
325 struct aic_softc *sc;
326 struct sc_chinfo *ch;
333 fmt = sndbuf_getfmt(ch->buffer);
335 KASSERT(fmt & AFMT_16BIT, ("16-bit audio supported only."));
337 sc->req.operation = XDMA_CYCLIC;
338 sc->req.req_type = XR_TYPE_PHYS;
339 sc->req.direction = XDMA_MEM_TO_DEV;
340 sc->req.src_addr = sc->buf_base_phys;
341 sc->req.dst_addr = sc->aic_fifo_paddr;
342 sc->req.src_width = 2;
343 sc->req.dst_width = 2;
344 sc->req.block_len = sndbuf_getblksz(ch->buffer);
345 sc->req.block_num = sndbuf_getblkcnt(ch->buffer);
347 err = xdma_request(sc->xchan, &sc->req);
349 device_printf(sc->dev, "Can't configure virtual channel\n");
353 xdma_control(sc->xchan, XDMA_CMD_BEGIN);
359 aic_start(struct sc_pcminfo *scp)
361 struct aic_softc *sc;
366 /* Ensure clock enabled. */
367 reg = READ4(sc, I2SCR);
368 reg |= (I2SCR_ESCLK);
369 WRITE4(sc, I2SCR, reg);
373 reg = (AICCR_OSS_16 | AICCR_ISS_16);
374 reg |= (AICCR_CHANNEL_2);
377 WRITE4(sc, AICCR, reg);
383 aic_stop(struct sc_pcminfo *scp)
385 struct aic_softc *sc;
390 reg = READ4(sc, AICCR);
391 reg &= ~(AICCR_TDMS | AICCR_ERPL);
392 WRITE4(sc, AICCR, reg);
394 xdma_control(sc->xchan, XDMA_CMD_TERMINATE);
400 aicchan_trigger(kobj_t obj, void *data, int go)
402 struct sc_pcminfo *scp;
403 struct sc_chinfo *ch;
404 struct aic_softc *sc;
410 snd_mtxlock(sc->lock);
430 bzero(sc->buf_base, sc->dma_size);
435 snd_mtxunlock(sc->lock);
441 aicchan_getptr(kobj_t obj, void *data)
443 struct sc_pcminfo *scp;
444 struct sc_chinfo *ch;
445 struct aic_softc *sc;
454 static uint32_t aic_pfmt[] = {
455 SND_FORMAT(AFMT_S16_LE, 2, 0),
459 static struct pcmchan_caps aic_pcaps = {48000, 48000, aic_pfmt, 0};
461 static struct pcmchan_caps *
462 aicchan_getcaps(kobj_t obj, void *data)
468 static kobj_method_t aicchan_methods[] = {
469 KOBJMETHOD(channel_init, aicchan_init),
470 KOBJMETHOD(channel_free, aicchan_free),
471 KOBJMETHOD(channel_setformat, aicchan_setformat),
472 KOBJMETHOD(channel_setspeed, aicchan_setspeed),
473 KOBJMETHOD(channel_setblocksize, aicchan_setblocksize),
474 KOBJMETHOD(channel_trigger, aicchan_trigger),
475 KOBJMETHOD(channel_getptr, aicchan_getptr),
476 KOBJMETHOD(channel_getcaps, aicchan_getcaps),
479 CHANNEL_DECLARE(aicchan);
482 aic_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
489 addr = (bus_addr_t*)arg;
490 *addr = segs[0].ds_addr;
494 aic_dma_setup(struct aic_softc *sc)
501 /* DMA buffer size. */
502 sc->dma_size = 131072;
505 * Must use dma_size boundary as modulo feature required.
506 * Modulo feature allows setup circular buffer.
508 err = bus_dma_tag_create(
509 bus_get_dma_tag(sc->dev),
510 4, sc->dma_size, /* alignment, boundary */
511 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
512 BUS_SPACE_MAXADDR, /* highaddr */
513 NULL, NULL, /* filter, filterarg */
514 sc->dma_size, 1, /* maxsize, nsegments */
515 sc->dma_size, 0, /* maxsegsize, flags */
516 NULL, NULL, /* lockfunc, lockarg */
519 device_printf(dev, "cannot create bus dma tag\n");
523 err = bus_dmamem_alloc(sc->dma_tag, (void **)&sc->buf_base,
524 BUS_DMA_WAITOK | BUS_DMA_COHERENT, &sc->dma_map);
526 device_printf(dev, "cannot allocate memory\n");
530 err = bus_dmamap_load(sc->dma_tag, sc->dma_map, sc->buf_base,
531 sc->dma_size, aic_dmamap_cb, &sc->buf_base_phys, BUS_DMA_WAITOK);
533 device_printf(dev, "cannot load DMA map\n");
537 bzero(sc->buf_base, sc->dma_size);
543 aic_configure_clocks(struct aic_softc *sc)
552 err = clk_get_by_ofw_name(sc->dev, 0, "aic", &sc->clk_aic);
554 device_printf(dev, "Can't find aic clock.\n");
558 err = clk_enable(sc->clk_aic);
560 device_printf(dev, "Can't enable aic clock.\n");
564 err = clk_get_by_ofw_name(sc->dev, 0, "i2s", &sc->clk_i2s);
566 device_printf(dev, "Can't find i2s clock.\n");
570 err = clk_enable(sc->clk_i2s);
572 device_printf(dev, "Can't enable i2s clock.\n");
576 err = clk_set_freq(sc->clk_i2s, 12000000, 0);
578 device_printf(dev, "Can't set i2s frequency.\n");
582 clk_get_freq(sc->clk_aic, &aic_freq);
583 clk_get_freq(sc->clk_i2s, &i2s_freq);
585 device_printf(dev, "Frequency aic %d i2s %d\n",
586 (uint32_t)aic_freq, (uint32_t)i2s_freq);
592 aic_configure(struct aic_softc *sc)
596 WRITE4(sc, AICFR, AICFR_RST);
600 if (sc->internal_codec) {
603 reg |= (AICFR_SYNCD | AICFR_BCKD);
605 reg |= (AICFR_AUSEL); /* I2S/MSB-justified format. */
606 reg |= (AICFR_TFTH(8)); /* Transmit FIFO threshold */
607 reg |= (AICFR_RFTH(7)); /* Receive FIFO threshold */
608 WRITE4(sc, AICFR, reg);
610 reg = READ4(sc, AICFR);
611 reg |= (AICFR_ENB); /* Enable the controller. */
612 WRITE4(sc, AICFR, reg);
618 sysctl_hw_pcm_internal_codec(SYSCTL_HANDLER_ARGS)
620 struct sc_pcminfo *scp;
621 struct sc_chinfo *ch;
622 struct aic_softc *sc;
632 snd_mtxlock(sc->lock);
634 val = sc->internal_codec;
635 error = sysctl_handle_int(oidp, &val, 0, req);
636 if (error || req->newptr == NULL) {
637 snd_mtxunlock(sc->lock);
640 if (val < 0 || val > 1) {
641 snd_mtxunlock(sc->lock);
645 if (sc->internal_codec != val) {
646 sc->internal_codec = val;
654 snd_mtxunlock(sc->lock);
660 aic_probe(device_t dev)
663 if (!ofw_bus_status_okay(dev))
666 if (!ofw_bus_is_compatible(dev, "ingenic,jz4780-i2s"))
669 device_set_desc(dev, "Audio Interface Controller");
671 return (BUS_PROBE_DEFAULT);
675 aic_attach(device_t dev)
677 char status[SND_STATUSLEN];
678 struct sc_pcminfo *scp;
679 struct aic_softc *sc;
682 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
685 sc->internal_codec = 1;
687 /* Get xDMA controller */
688 sc->xdma_tx = xdma_ofw_get(sc->dev, "tx");
689 if (sc->xdma_tx == NULL) {
690 device_printf(dev, "Can't find DMA controller.\n");
694 /* Alloc xDMA virtual channel. */
695 sc->xchan = xdma_channel_alloc(sc->xdma_tx, 0);
696 if (sc->xchan == NULL) {
697 device_printf(dev, "Can't alloc virtual DMA channel.\n");
701 /* Setup sound subsystem */
702 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "aic softc");
703 if (sc->lock == NULL) {
704 device_printf(dev, "Can't create mtx.\n");
708 if (bus_alloc_resources(dev, aic_spec, sc->res)) {
710 "could not allocate resources for device\n");
714 /* Memory interface */
715 sc->bst = rman_get_bustag(sc->res[0]);
716 sc->bsh = rman_get_bushandle(sc->res[0]);
717 sc->aic_fifo_paddr = rman_get_start(sc->res[0]) + AICDR;
720 scp = malloc(sizeof(struct sc_pcminfo), M_DEVBUF, M_WAITOK | M_ZERO);
724 /* Setup audio buffer. */
725 err = aic_dma_setup(sc);
727 device_printf(dev, "Can't setup sound buffer.\n");
732 err = aic_configure_clocks(sc);
734 device_printf(dev, "Can't configure clocks.\n");
738 err = aic_configure(sc);
740 device_printf(dev, "Can't configure AIC.\n");
744 pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
746 /* Setup interrupt handler. */
747 err = xdma_setup_intr(sc->xchan, 0, aic_intr, scp, &sc->ih);
749 device_printf(sc->dev,
750 "Can't setup xDMA interrupt handler.\n");
754 err = pcm_register(dev, scp, 1, 0);
756 device_printf(dev, "Can't register pcm.\n");
761 pcm_addchan(dev, PCMDIR_PLAY, &aicchan_class, scp);
764 snprintf(status, SND_STATUSLEN, "at %s", ofw_bus_get_name(dev));
765 pcm_setstatus(dev, status);
767 mixer_init(dev, &aicmixer_class, scp);
769 /* Create device sysctl node. */
770 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
771 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
772 OID_AUTO, "internal_codec",
773 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
774 scp, 0, sysctl_hw_pcm_internal_codec, "I",
775 "use internal audio codec");
781 aic_detach(device_t dev)
783 struct aic_softc *sc;
785 sc = device_get_softc(dev);
787 xdma_channel_free(sc->xchan);
789 bus_release_resources(dev, aic_spec, sc->res);
794 static device_method_t aic_pcm_methods[] = {
795 /* Device interface */
796 DEVMETHOD(device_probe, aic_probe),
797 DEVMETHOD(device_attach, aic_attach),
798 DEVMETHOD(device_detach, aic_detach),
802 static driver_t aic_pcm_driver = {
808 DRIVER_MODULE(aic, simplebus, aic_pcm_driver, pcm_devclass, 0, 0);
809 MODULE_DEPEND(aic, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
810 MODULE_VERSION(aic, 1);