2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
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
29 * Allwinner A10/A20 HDMI Audio
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/condvar.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <dev/sound/pcm/sound.h>
44 #include <dev/sound/chip.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include "sunxi_dma_if.h"
52 #define DRQTYPE_HDMIAUDIO 24
53 #define DRQTYPE_SDRAM 1
56 #define DMA_BURST_LEN 8
57 #define DDMA_BLKSIZE 32
58 #define DDMA_WAIT_CYC 8
60 #define DMABUF_MIN 4096
61 #define DMABUF_DEFAULT 65536
62 #define DMABUF_MAX 131072
64 #define HDMI_SAMPLERATE 48000
66 #define TX_FIFO 0x01c16400
68 static uint32_t a10hdmiaudio_fmt[] = {
69 SND_FORMAT(AFMT_S16_LE, 2, 0),
73 static struct pcmchan_caps a10hdmiaudio_pcaps = {
74 HDMI_SAMPLERATE, HDMI_SAMPLERATE, a10hdmiaudio_fmt, 0
77 struct a10hdmiaudio_info;
79 struct a10hdmiaudio_chinfo {
80 struct snd_dbuf *buffer;
81 struct pcm_channel *channel;
82 struct a10hdmiaudio_info *parent;
94 struct a10hdmiaudio_info {
100 struct a10hdmiaudio_chinfo play;
108 a10hdmiaudio_mixer_init(struct snd_mixer *m)
110 mix_setdevs(m, SOUND_MASK_PCM);
116 a10hdmiaudio_mixer_set(struct snd_mixer *m, unsigned dev, unsigned left,
122 static kobj_method_t a10hdmiaudio_mixer_methods[] = {
123 KOBJMETHOD(mixer_init, a10hdmiaudio_mixer_init),
124 KOBJMETHOD(mixer_set, a10hdmiaudio_mixer_set),
127 MIXER_DECLARE(a10hdmiaudio_mixer);
135 a10hdmiaudio_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
137 struct a10hdmiaudio_chinfo *ch = arg;
142 ch->physaddr = segs[0].ds_addr;
146 a10hdmiaudio_transfer(struct a10hdmiaudio_chinfo *ch)
150 error = SUNXI_DMA_TRANSFER(ch->dmac, ch->dmachan,
151 ch->physaddr + ch->pos, TX_FIFO, ch->blocksize);
154 device_printf(ch->parent->dev, "DMA transfer failed: %d\n",
160 a10hdmiaudio_dmaconfig(struct a10hdmiaudio_chinfo *ch)
162 struct sunxi_dma_config conf;
164 memset(&conf, 0, sizeof(conf));
165 conf.src_width = conf.dst_width = DMA_WIDTH;
166 conf.src_burst_len = conf.dst_burst_len = DMA_BURST_LEN;
167 conf.src_blksize = conf.dst_blksize = DDMA_BLKSIZE;
168 conf.src_wait_cyc = conf.dst_wait_cyc = DDMA_WAIT_CYC;
169 conf.src_drqtype = DRQTYPE_SDRAM;
170 conf.dst_drqtype = DRQTYPE_HDMIAUDIO;
171 conf.dst_noincr = true;
173 SUNXI_DMA_SET_CONFIG(ch->dmac, ch->dmachan, &conf);
177 a10hdmiaudio_dmaintr(void *priv)
179 struct a10hdmiaudio_chinfo *ch = priv;
182 bufsize = sndbuf_getsize(ch->buffer);
184 ch->pos += ch->blocksize;
185 if (ch->pos >= bufsize)
189 chn_intr(ch->channel);
190 a10hdmiaudio_transfer(ch);
195 a10hdmiaudio_start(struct a10hdmiaudio_chinfo *ch)
199 /* Configure DMA channel */
200 a10hdmiaudio_dmaconfig(ch);
202 /* Start DMA transfer */
203 a10hdmiaudio_transfer(ch);
207 a10hdmiaudio_stop(struct a10hdmiaudio_chinfo *ch)
209 /* Disable DMA channel */
210 SUNXI_DMA_HALT(ch->dmac, ch->dmachan);
214 a10hdmiaudio_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
215 struct pcm_channel *c, int dir)
217 struct a10hdmiaudio_info *sc = devinfo;
218 struct a10hdmiaudio_chinfo *ch = &sc->play;
225 ch->dmac = devclass_get_device(devclass_find("a10dmac"), 0);
226 if (ch->dmac == NULL) {
227 device_printf(sc->dev, "cannot find DMA controller\n");
230 ch->dmachan = SUNXI_DMA_ALLOC(ch->dmac, true, a10hdmiaudio_dmaintr, ch);
231 if (ch->dmachan == NULL) {
232 device_printf(sc->dev, "cannot allocate DMA channel\n");
236 error = bus_dmamem_alloc(sc->dmat, &ch->dmaaddr,
237 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &ch->dmamap);
239 device_printf(sc->dev, "cannot allocate channel buffer\n");
242 error = bus_dmamap_load(sc->dmat, ch->dmamap, ch->dmaaddr,
243 sc->dmasize, a10hdmiaudio_dmamap_cb, ch, BUS_DMA_NOWAIT);
245 device_printf(sc->dev, "cannot load DMA map\n");
248 memset(ch->dmaaddr, 0, sc->dmasize);
250 if (sndbuf_setup(ch->buffer, ch->dmaaddr, sc->dmasize) != 0) {
251 device_printf(sc->dev, "cannot setup sndbuf\n");
259 a10hdmiaudio_chan_free(kobj_t obj, void *data)
261 struct a10hdmiaudio_chinfo *ch = data;
262 struct a10hdmiaudio_info *sc = ch->parent;
264 SUNXI_DMA_FREE(ch->dmac, ch->dmachan);
265 bus_dmamap_unload(sc->dmat, ch->dmamap);
266 bus_dmamem_free(sc->dmat, ch->dmaaddr, ch->dmamap);
272 a10hdmiaudio_chan_setformat(kobj_t obj, void *data, uint32_t format)
278 a10hdmiaudio_chan_setspeed(kobj_t obj, void *data, uint32_t speed)
280 return (HDMI_SAMPLERATE);
284 a10hdmiaudio_chan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
286 struct a10hdmiaudio_chinfo *ch = data;
288 ch->blocksize = blocksize & ~3;
290 return (ch->blocksize);
294 a10hdmiaudio_chan_trigger(kobj_t obj, void *data, int go)
296 struct a10hdmiaudio_chinfo *ch = data;
297 struct a10hdmiaudio_info *sc = ch->parent;
299 if (!PCMTRIG_COMMON(go))
302 snd_mtxlock(sc->lock);
306 a10hdmiaudio_start(ch);
311 a10hdmiaudio_stop(ch);
316 snd_mtxunlock(sc->lock);
322 a10hdmiaudio_chan_getptr(kobj_t obj, void *data)
324 struct a10hdmiaudio_chinfo *ch = data;
329 static struct pcmchan_caps *
330 a10hdmiaudio_chan_getcaps(kobj_t obj, void *data)
332 return (&a10hdmiaudio_pcaps);
335 static kobj_method_t a10hdmiaudio_chan_methods[] = {
336 KOBJMETHOD(channel_init, a10hdmiaudio_chan_init),
337 KOBJMETHOD(channel_free, a10hdmiaudio_chan_free),
338 KOBJMETHOD(channel_setformat, a10hdmiaudio_chan_setformat),
339 KOBJMETHOD(channel_setspeed, a10hdmiaudio_chan_setspeed),
340 KOBJMETHOD(channel_setblocksize, a10hdmiaudio_chan_setblocksize),
341 KOBJMETHOD(channel_trigger, a10hdmiaudio_chan_trigger),
342 KOBJMETHOD(channel_getptr, a10hdmiaudio_chan_getptr),
343 KOBJMETHOD(channel_getcaps, a10hdmiaudio_chan_getcaps),
346 CHANNEL_DECLARE(a10hdmiaudio_chan);
354 a10hdmiaudio_probe(device_t dev)
356 if (!ofw_bus_status_okay(dev))
359 if (!ofw_bus_is_compatible(dev, "allwinner,sun7i-a20-hdmiaudio"))
362 device_set_desc(dev, "Allwinner HDMI Audio");
363 return (BUS_PROBE_DEFAULT);
367 a10hdmiaudio_attach(device_t dev)
369 struct a10hdmiaudio_info *sc;
370 char status[SND_STATUSLEN];
373 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
375 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "a10hdmiaudio softc");
377 sc->dmasize = pcm_getbuffersize(dev, DMABUF_MIN,
378 DMABUF_DEFAULT, DMABUF_MAX);
379 error = bus_dma_tag_create(
380 bus_get_dma_tag(dev),
381 4, sc->dmasize, /* alignment, boundary */
382 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
383 BUS_SPACE_MAXADDR, /* highaddr */
384 NULL, NULL, /* filter, filterarg */
385 sc->dmasize, 1, /* maxsize, nsegs */
386 sc->dmasize, 0, /* maxsegsize, flags */
387 NULL, NULL, /* lockfunc, lockarg */
390 device_printf(dev, "cannot create DMA tag\n");
394 if (mixer_init(dev, &a10hdmiaudio_mixer_class, sc)) {
395 device_printf(dev, "mixer_init failed\n");
399 pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
400 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SOFTPCMVOL);
402 if (pcm_register(dev, sc, 1, 0)) {
403 device_printf(dev, "pcm_register failed\n");
407 pcm_addchan(dev, PCMDIR_PLAY, &a10hdmiaudio_chan_class, sc);
409 snprintf(status, SND_STATUSLEN, "at %s", ofw_bus_get_name(dev));
410 pcm_setstatus(dev, status);
415 snd_mtxfree(sc->lock);
421 static device_method_t a10hdmiaudio_pcm_methods[] = {
422 /* Device interface */
423 DEVMETHOD(device_probe, a10hdmiaudio_probe),
424 DEVMETHOD(device_attach, a10hdmiaudio_attach),
429 static driver_t a10hdmiaudio_pcm_driver = {
431 a10hdmiaudio_pcm_methods,
435 DRIVER_MODULE(a10hdmiaudio, simplebus, a10hdmiaudio_pcm_driver, pcm_devclass, 0, 0);
436 MODULE_DEPEND(a10hdmiaudio, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
437 MODULE_VERSION(a10hdmiaudio, 1);