1 1.14 jmcneill /* $NetBSD: sunxi_codec.c,v 1.14 2021/05/05 20:58:03 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2014-2017 Jared McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jmcneill * SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include "opt_ddb.h" 30 1.1 jmcneill 31 1.1 jmcneill #include <sys/cdefs.h> 32 1.14 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_codec.c,v 1.14 2021/05/05 20:58:03 jmcneill Exp $"); 33 1.1 jmcneill 34 1.1 jmcneill #include <sys/param.h> 35 1.1 jmcneill #include <sys/bus.h> 36 1.1 jmcneill #include <sys/cpu.h> 37 1.1 jmcneill #include <sys/device.h> 38 1.1 jmcneill #include <sys/kmem.h> 39 1.1 jmcneill #include <sys/gpio.h> 40 1.1 jmcneill 41 1.1 jmcneill #include <sys/audioio.h> 42 1.6 isaki #include <dev/audio/audio_if.h> 43 1.1 jmcneill 44 1.1 jmcneill #include <dev/fdt/fdtvar.h> 45 1.1 jmcneill 46 1.1 jmcneill #include <arm/sunxi/sunxi_codec.h> 47 1.1 jmcneill 48 1.1 jmcneill #define TX_TRIG_LEVEL 0xf 49 1.1 jmcneill #define RX_TRIG_LEVEL 0x7 50 1.1 jmcneill #define DRQ_CLR_CNT 0x3 51 1.1 jmcneill 52 1.1 jmcneill #define AC_DAC_DPC(_sc) ((_sc)->sc_cfg->DPC) 53 1.1 jmcneill #define DAC_DPC_EN_DA 0x80000000 54 1.1 jmcneill #define AC_DAC_FIFOC(_sc) ((_sc)->sc_cfg->DAC_FIFOC) 55 1.1 jmcneill #define DAC_FIFOC_FS __BITS(31,29) 56 1.1 jmcneill #define DAC_FS_48KHZ 0 57 1.1 jmcneill #define DAC_FS_32KHZ 1 58 1.1 jmcneill #define DAC_FS_24KHZ 2 59 1.1 jmcneill #define DAC_FS_16KHZ 3 60 1.1 jmcneill #define DAC_FS_12KHZ 4 61 1.1 jmcneill #define DAC_FS_8KHZ 5 62 1.1 jmcneill #define DAC_FS_192KHZ 6 63 1.1 jmcneill #define DAC_FS_96KHZ 7 64 1.1 jmcneill #define DAC_FIFOC_FIFO_MODE __BITS(25,24) 65 1.1 jmcneill #define FIFO_MODE_24_31_8 0 66 1.1 jmcneill #define FIFO_MODE_16_31_16 0 67 1.1 jmcneill #define FIFO_MODE_16_15_0 1 68 1.1 jmcneill #define DAC_FIFOC_DRQ_CLR_CNT __BITS(22,21) 69 1.1 jmcneill #define DAC_FIFOC_TX_TRIG_LEVEL __BITS(14,8) 70 1.1 jmcneill #define DAC_FIFOC_MONO_EN __BIT(6) 71 1.1 jmcneill #define DAC_FIFOC_TX_BITS __BIT(5) 72 1.1 jmcneill #define DAC_FIFOC_DRQ_EN __BIT(4) 73 1.1 jmcneill #define DAC_FIFOC_FIFO_FLUSH __BIT(0) 74 1.1 jmcneill #define AC_DAC_FIFOS(_sc) ((_sc)->sc_cfg->DAC_FIFOS) 75 1.1 jmcneill #define AC_DAC_TXDATA(_sc) ((_sc)->sc_cfg->DAC_TXDATA) 76 1.1 jmcneill #define AC_ADC_FIFOC(_sc) ((_sc)->sc_cfg->ADC_FIFOC) 77 1.1 jmcneill #define ADC_FIFOC_FS __BITS(31,29) 78 1.1 jmcneill #define ADC_FS_48KHZ 0 79 1.1 jmcneill #define ADC_FIFOC_EN_AD __BIT(28) 80 1.1 jmcneill #define ADC_FIFOC_RX_FIFO_MODE __BIT(24) 81 1.1 jmcneill #define ADC_FIFOC_RX_TRIG_LEVEL __BITS(12,8) 82 1.1 jmcneill #define ADC_FIFOC_MONO_EN __BIT(7) 83 1.1 jmcneill #define ADC_FIFOC_RX_BITS __BIT(6) 84 1.1 jmcneill #define ADC_FIFOC_DRQ_EN __BIT(4) 85 1.1 jmcneill #define ADC_FIFOC_FIFO_FLUSH __BIT(0) 86 1.1 jmcneill #define AC_ADC_FIFOS(_sc) ((_sc)->sc_cfg->ADC_FIFOS) 87 1.1 jmcneill #define AC_ADC_RXDATA(_sc) ((_sc)->sc_cfg->ADC_RXDATA) 88 1.1 jmcneill #define AC_DAC_CNT(_sc) ((_sc)->sc_cfg->DAC_CNT) 89 1.1 jmcneill #define AC_ADC_CNT(_sc) ((_sc)->sc_cfg->ADC_CNT) 90 1.1 jmcneill 91 1.9 thorpej static const struct device_compatible_entry compat_data[] = { 92 1.14 jmcneill A10_CODEC_COMPATDATA 93 1.14 jmcneill A31_CODEC_COMPATDATA 94 1.14 jmcneill H3_CODEC_COMPATDATA 95 1.14 jmcneill V3S_CODEC_COMPATDATA 96 1.9 thorpej 97 1.11 thorpej DEVICE_COMPAT_EOL 98 1.1 jmcneill }; 99 1.1 jmcneill 100 1.1 jmcneill #define CODEC_READ(sc, reg) \ 101 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 102 1.1 jmcneill #define CODEC_WRITE(sc, reg, val) \ 103 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 104 1.1 jmcneill 105 1.1 jmcneill static int 106 1.1 jmcneill sunxi_codec_allocdma(struct sunxi_codec_softc *sc, size_t size, 107 1.1 jmcneill size_t align, struct sunxi_codec_dma *dma) 108 1.1 jmcneill { 109 1.1 jmcneill int error; 110 1.1 jmcneill 111 1.1 jmcneill dma->dma_size = size; 112 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, dma->dma_size, align, 0, 113 1.1 jmcneill dma->dma_segs, 1, &dma->dma_nsegs, BUS_DMA_WAITOK); 114 1.1 jmcneill if (error) 115 1.1 jmcneill return error; 116 1.1 jmcneill 117 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs, 118 1.1 jmcneill dma->dma_size, &dma->dma_addr, BUS_DMA_WAITOK | BUS_DMA_COHERENT); 119 1.1 jmcneill if (error) 120 1.1 jmcneill goto free; 121 1.1 jmcneill 122 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, dma->dma_size, dma->dma_nsegs, 123 1.1 jmcneill dma->dma_size, 0, BUS_DMA_WAITOK, &dma->dma_map); 124 1.1 jmcneill if (error) 125 1.1 jmcneill goto unmap; 126 1.1 jmcneill 127 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_addr, 128 1.1 jmcneill dma->dma_size, NULL, BUS_DMA_WAITOK); 129 1.1 jmcneill if (error) 130 1.1 jmcneill goto destroy; 131 1.1 jmcneill 132 1.1 jmcneill return 0; 133 1.1 jmcneill 134 1.1 jmcneill destroy: 135 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, dma->dma_map); 136 1.1 jmcneill unmap: 137 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size); 138 1.1 jmcneill free: 139 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs); 140 1.1 jmcneill 141 1.1 jmcneill return error; 142 1.1 jmcneill } 143 1.1 jmcneill 144 1.1 jmcneill static void 145 1.1 jmcneill sunxi_codec_freedma(struct sunxi_codec_softc *sc, struct sunxi_codec_dma *dma) 146 1.1 jmcneill { 147 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, dma->dma_map); 148 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, dma->dma_map); 149 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size); 150 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs); 151 1.1 jmcneill } 152 1.1 jmcneill 153 1.1 jmcneill static int 154 1.1 jmcneill sunxi_codec_transfer(struct sunxi_codec_chan *ch) 155 1.1 jmcneill { 156 1.1 jmcneill bus_dma_segment_t seg; 157 1.1 jmcneill 158 1.1 jmcneill seg.ds_addr = ch->ch_cur_phys; 159 1.1 jmcneill seg.ds_len = ch->ch_blksize; 160 1.1 jmcneill ch->ch_req.dreq_segs = &seg; 161 1.1 jmcneill ch->ch_req.dreq_nsegs = 1; 162 1.1 jmcneill 163 1.1 jmcneill return fdtbus_dma_transfer(ch->ch_dma, &ch->ch_req); 164 1.1 jmcneill } 165 1.1 jmcneill 166 1.1 jmcneill static int 167 1.6 isaki sunxi_codec_query_format(void *priv, audio_format_query_t *afp) 168 1.1 jmcneill { 169 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 170 1.1 jmcneill 171 1.6 isaki return audio_query_format(&sc->sc_format, 1, afp); 172 1.1 jmcneill } 173 1.1 jmcneill 174 1.1 jmcneill static int 175 1.6 isaki sunxi_codec_set_format(void *priv, int setmode, 176 1.6 isaki const audio_params_t *play, const audio_params_t *rec, 177 1.6 isaki audio_filter_reg_t *pfil, audio_filter_reg_t *rfil) 178 1.1 jmcneill { 179 1.1 jmcneill 180 1.1 jmcneill return 0; 181 1.1 jmcneill } 182 1.1 jmcneill 183 1.1 jmcneill static int 184 1.1 jmcneill sunxi_codec_set_port(void *priv, mixer_ctrl_t *mc) 185 1.1 jmcneill { 186 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 187 1.1 jmcneill 188 1.1 jmcneill return sc->sc_cfg->set_port(sc, mc); 189 1.1 jmcneill } 190 1.1 jmcneill 191 1.1 jmcneill static int 192 1.1 jmcneill sunxi_codec_get_port(void *priv, mixer_ctrl_t *mc) 193 1.1 jmcneill { 194 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 195 1.1 jmcneill 196 1.1 jmcneill return sc->sc_cfg->get_port(sc, mc); 197 1.1 jmcneill } 198 1.1 jmcneill 199 1.1 jmcneill static int 200 1.1 jmcneill sunxi_codec_query_devinfo(void *priv, mixer_devinfo_t *di) 201 1.1 jmcneill { 202 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 203 1.1 jmcneill 204 1.1 jmcneill return sc->sc_cfg->query_devinfo(sc, di); 205 1.1 jmcneill } 206 1.1 jmcneill 207 1.1 jmcneill static void * 208 1.1 jmcneill sunxi_codec_allocm(void *priv, int dir, size_t size) 209 1.1 jmcneill { 210 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 211 1.1 jmcneill struct sunxi_codec_dma *dma; 212 1.1 jmcneill int error; 213 1.1 jmcneill 214 1.1 jmcneill dma = kmem_alloc(sizeof(*dma), KM_SLEEP); 215 1.1 jmcneill 216 1.1 jmcneill error = sunxi_codec_allocdma(sc, size, 16, dma); 217 1.1 jmcneill if (error) { 218 1.1 jmcneill kmem_free(dma, sizeof(*dma)); 219 1.1 jmcneill device_printf(sc->sc_dev, "couldn't allocate DMA memory (%d)\n", 220 1.1 jmcneill error); 221 1.1 jmcneill return NULL; 222 1.1 jmcneill } 223 1.1 jmcneill 224 1.1 jmcneill LIST_INSERT_HEAD(&sc->sc_dmalist, dma, dma_list); 225 1.1 jmcneill 226 1.1 jmcneill return dma->dma_addr; 227 1.1 jmcneill } 228 1.1 jmcneill 229 1.1 jmcneill static void 230 1.1 jmcneill sunxi_codec_freem(void *priv, void *addr, size_t size) 231 1.1 jmcneill { 232 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 233 1.1 jmcneill struct sunxi_codec_dma *dma; 234 1.1 jmcneill 235 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list) 236 1.1 jmcneill if (dma->dma_addr == addr) { 237 1.1 jmcneill sunxi_codec_freedma(sc, dma); 238 1.1 jmcneill LIST_REMOVE(dma, dma_list); 239 1.1 jmcneill kmem_free(dma, sizeof(*dma)); 240 1.1 jmcneill break; 241 1.1 jmcneill } 242 1.1 jmcneill } 243 1.1 jmcneill 244 1.1 jmcneill static int 245 1.1 jmcneill sunxi_codec_getdev(void *priv, struct audio_device *adev) 246 1.1 jmcneill { 247 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 248 1.1 jmcneill 249 1.1 jmcneill snprintf(adev->name, sizeof(adev->name), "Allwinner"); 250 1.1 jmcneill snprintf(adev->version, sizeof(adev->version), "%s", 251 1.1 jmcneill sc->sc_cfg->name); 252 1.1 jmcneill snprintf(adev->config, sizeof(adev->config), "sunxicodec"); 253 1.1 jmcneill 254 1.1 jmcneill return 0; 255 1.1 jmcneill } 256 1.1 jmcneill 257 1.1 jmcneill static int 258 1.1 jmcneill sunxi_codec_get_props(void *priv) 259 1.1 jmcneill { 260 1.7 isaki 261 1.7 isaki return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE| 262 1.7 isaki AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX; 263 1.1 jmcneill } 264 1.1 jmcneill 265 1.1 jmcneill static int 266 1.1 jmcneill sunxi_codec_trigger_output(void *priv, void *start, void *end, int blksize, 267 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params) 268 1.1 jmcneill { 269 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 270 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_pchan; 271 1.1 jmcneill struct sunxi_codec_dma *dma; 272 1.1 jmcneill bus_addr_t pstart; 273 1.1 jmcneill bus_size_t psize; 274 1.1 jmcneill uint32_t val; 275 1.1 jmcneill int error; 276 1.1 jmcneill 277 1.1 jmcneill pstart = 0; 278 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start; 279 1.1 jmcneill 280 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list) 281 1.1 jmcneill if (dma->dma_addr == start) { 282 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr; 283 1.1 jmcneill break; 284 1.1 jmcneill } 285 1.1 jmcneill if (pstart == 0) { 286 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start); 287 1.1 jmcneill return EINVAL; 288 1.1 jmcneill } 289 1.1 jmcneill 290 1.1 jmcneill ch->ch_intr = intr; 291 1.1 jmcneill ch->ch_intrarg = intrarg; 292 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart; 293 1.1 jmcneill ch->ch_end_phys = pstart + psize; 294 1.1 jmcneill ch->ch_blksize = blksize; 295 1.1 jmcneill 296 1.1 jmcneill /* Flush DAC FIFO */ 297 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOC(sc)); 298 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val | DAC_FIFOC_FIFO_FLUSH); 299 1.1 jmcneill 300 1.1 jmcneill /* Clear DAC FIFO status */ 301 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOS(sc)); 302 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOS(sc), val); 303 1.1 jmcneill 304 1.1 jmcneill /* Unmute output */ 305 1.1 jmcneill if (sc->sc_cfg->mute) 306 1.1 jmcneill sc->sc_cfg->mute(sc, 0, ch->ch_mode); 307 1.1 jmcneill 308 1.1 jmcneill /* Configure DAC FIFO */ 309 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), 310 1.1 jmcneill __SHIFTIN(DAC_FS_48KHZ, DAC_FIFOC_FS) | 311 1.1 jmcneill __SHIFTIN(FIFO_MODE_16_15_0, DAC_FIFOC_FIFO_MODE) | 312 1.1 jmcneill __SHIFTIN(DRQ_CLR_CNT, DAC_FIFOC_DRQ_CLR_CNT) | 313 1.1 jmcneill __SHIFTIN(TX_TRIG_LEVEL, DAC_FIFOC_TX_TRIG_LEVEL)); 314 1.1 jmcneill 315 1.1 jmcneill /* Enable DAC DRQ */ 316 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOC(sc)); 317 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val | DAC_FIFOC_DRQ_EN); 318 1.1 jmcneill 319 1.1 jmcneill /* Start DMA transfer */ 320 1.1 jmcneill error = sunxi_codec_transfer(ch); 321 1.1 jmcneill if (error != 0) { 322 1.1 jmcneill aprint_error_dev(sc->sc_dev, 323 1.1 jmcneill "failed to start DMA transfer: %d\n", error); 324 1.1 jmcneill return error; 325 1.1 jmcneill } 326 1.1 jmcneill 327 1.1 jmcneill return 0; 328 1.1 jmcneill } 329 1.1 jmcneill 330 1.1 jmcneill static int 331 1.1 jmcneill sunxi_codec_trigger_input(void *priv, void *start, void *end, int blksize, 332 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params) 333 1.1 jmcneill { 334 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 335 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_rchan; 336 1.1 jmcneill struct sunxi_codec_dma *dma; 337 1.1 jmcneill bus_addr_t pstart; 338 1.1 jmcneill bus_size_t psize; 339 1.1 jmcneill uint32_t val; 340 1.1 jmcneill int error; 341 1.1 jmcneill 342 1.1 jmcneill pstart = 0; 343 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start; 344 1.1 jmcneill 345 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list) 346 1.1 jmcneill if (dma->dma_addr == start) { 347 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr; 348 1.1 jmcneill break; 349 1.1 jmcneill } 350 1.1 jmcneill if (pstart == 0) { 351 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start); 352 1.1 jmcneill return EINVAL; 353 1.1 jmcneill } 354 1.1 jmcneill 355 1.1 jmcneill ch->ch_intr = intr; 356 1.1 jmcneill ch->ch_intrarg = intrarg; 357 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart; 358 1.1 jmcneill ch->ch_end_phys = pstart + psize; 359 1.1 jmcneill ch->ch_blksize = blksize; 360 1.1 jmcneill 361 1.1 jmcneill /* Flush ADC FIFO */ 362 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOC(sc)); 363 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val | ADC_FIFOC_FIFO_FLUSH); 364 1.1 jmcneill 365 1.1 jmcneill /* Clear ADC FIFO status */ 366 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOS(sc)); 367 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOS(sc), val); 368 1.1 jmcneill 369 1.1 jmcneill /* Unmute input */ 370 1.1 jmcneill if (sc->sc_cfg->mute) 371 1.1 jmcneill sc->sc_cfg->mute(sc, 0, ch->ch_mode); 372 1.1 jmcneill 373 1.1 jmcneill /* Configure ADC FIFO */ 374 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), 375 1.1 jmcneill __SHIFTIN(ADC_FS_48KHZ, ADC_FIFOC_FS) | 376 1.1 jmcneill __SHIFTIN(RX_TRIG_LEVEL, ADC_FIFOC_RX_TRIG_LEVEL) | 377 1.1 jmcneill ADC_FIFOC_EN_AD | ADC_FIFOC_RX_FIFO_MODE); 378 1.1 jmcneill 379 1.1 jmcneill /* Enable ADC DRQ */ 380 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOC(sc)); 381 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val | ADC_FIFOC_DRQ_EN); 382 1.1 jmcneill 383 1.1 jmcneill /* Start DMA transfer */ 384 1.1 jmcneill error = sunxi_codec_transfer(ch); 385 1.1 jmcneill if (error != 0) { 386 1.1 jmcneill aprint_error_dev(sc->sc_dev, 387 1.1 jmcneill "failed to start DMA transfer: %d\n", error); 388 1.1 jmcneill return error; 389 1.1 jmcneill } 390 1.1 jmcneill 391 1.1 jmcneill return 0; 392 1.1 jmcneill } 393 1.1 jmcneill 394 1.1 jmcneill static int 395 1.1 jmcneill sunxi_codec_halt_output(void *priv) 396 1.1 jmcneill { 397 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 398 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_pchan; 399 1.1 jmcneill uint32_t val; 400 1.1 jmcneill 401 1.1 jmcneill /* Disable DMA channel */ 402 1.1 jmcneill fdtbus_dma_halt(ch->ch_dma); 403 1.1 jmcneill 404 1.5 bouyer /* flush fifo */ 405 1.5 bouyer val = CODEC_READ(sc, AC_DAC_FIFOC(sc)); 406 1.5 bouyer CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val | DAC_FIFOC_FIFO_FLUSH); 407 1.5 bouyer while (val & DAC_FIFOC_FIFO_FLUSH) 408 1.5 bouyer val = CODEC_READ(sc, AC_DAC_FIFOC(sc)); 409 1.5 bouyer 410 1.1 jmcneill /* Mute output */ 411 1.1 jmcneill if (sc->sc_cfg->mute) 412 1.1 jmcneill sc->sc_cfg->mute(sc, 1, ch->ch_mode); 413 1.1 jmcneill 414 1.1 jmcneill /* Disable DAC DRQ */ 415 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOC(sc)); 416 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val & ~DAC_FIFOC_DRQ_EN); 417 1.1 jmcneill 418 1.1 jmcneill ch->ch_intr = NULL; 419 1.1 jmcneill ch->ch_intrarg = NULL; 420 1.1 jmcneill 421 1.1 jmcneill return 0; 422 1.1 jmcneill } 423 1.1 jmcneill 424 1.1 jmcneill static int 425 1.1 jmcneill sunxi_codec_halt_input(void *priv) 426 1.1 jmcneill { 427 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 428 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_rchan; 429 1.1 jmcneill uint32_t val; 430 1.1 jmcneill 431 1.1 jmcneill /* Mute output */ 432 1.1 jmcneill if (sc->sc_cfg->mute) 433 1.1 jmcneill sc->sc_cfg->mute(sc, 1, ch->ch_mode); 434 1.1 jmcneill 435 1.5 bouyer /* flush fifo */ 436 1.5 bouyer val = CODEC_READ(sc, AC_ADC_FIFOC(sc)); 437 1.5 bouyer CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val | ADC_FIFOC_FIFO_FLUSH); 438 1.5 bouyer while (val & ADC_FIFOC_FIFO_FLUSH) 439 1.5 bouyer val = CODEC_READ(sc, AC_ADC_FIFOC(sc)); 440 1.5 bouyer 441 1.5 bouyer /* Disable DMA channel */ 442 1.5 bouyer fdtbus_dma_halt(ch->ch_dma); 443 1.5 bouyer 444 1.1 jmcneill /* Disable ADC DRQ */ 445 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOC(sc)); 446 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val & ~ADC_FIFOC_DRQ_EN); 447 1.1 jmcneill 448 1.1 jmcneill return 0; 449 1.1 jmcneill } 450 1.1 jmcneill 451 1.1 jmcneill static void 452 1.1 jmcneill sunxi_codec_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread) 453 1.1 jmcneill { 454 1.1 jmcneill struct sunxi_codec_softc * const sc = priv; 455 1.1 jmcneill 456 1.1 jmcneill *intr = &sc->sc_intr_lock; 457 1.1 jmcneill *thread = &sc->sc_lock; 458 1.1 jmcneill } 459 1.1 jmcneill 460 1.1 jmcneill static const struct audio_hw_if sunxi_codec_hw_if = { 461 1.6 isaki .query_format = sunxi_codec_query_format, 462 1.6 isaki .set_format = sunxi_codec_set_format, 463 1.1 jmcneill .allocm = sunxi_codec_allocm, 464 1.1 jmcneill .freem = sunxi_codec_freem, 465 1.1 jmcneill .getdev = sunxi_codec_getdev, 466 1.1 jmcneill .set_port = sunxi_codec_set_port, 467 1.1 jmcneill .get_port = sunxi_codec_get_port, 468 1.1 jmcneill .query_devinfo = sunxi_codec_query_devinfo, 469 1.1 jmcneill .get_props = sunxi_codec_get_props, 470 1.1 jmcneill .trigger_output = sunxi_codec_trigger_output, 471 1.1 jmcneill .trigger_input = sunxi_codec_trigger_input, 472 1.1 jmcneill .halt_output = sunxi_codec_halt_output, 473 1.1 jmcneill .halt_input = sunxi_codec_halt_input, 474 1.1 jmcneill .get_locks = sunxi_codec_get_locks, 475 1.1 jmcneill }; 476 1.1 jmcneill 477 1.1 jmcneill static void 478 1.1 jmcneill sunxi_codec_dmaintr(void *priv) 479 1.1 jmcneill { 480 1.1 jmcneill struct sunxi_codec_chan * const ch = priv; 481 1.4 bouyer struct sunxi_codec_softc * const sc = ch->ch_sc; 482 1.1 jmcneill 483 1.4 bouyer mutex_enter(&sc->sc_intr_lock); 484 1.1 jmcneill ch->ch_cur_phys += ch->ch_blksize; 485 1.1 jmcneill if (ch->ch_cur_phys >= ch->ch_end_phys) 486 1.1 jmcneill ch->ch_cur_phys = ch->ch_start_phys; 487 1.1 jmcneill 488 1.1 jmcneill if (ch->ch_intr) { 489 1.1 jmcneill ch->ch_intr(ch->ch_intrarg); 490 1.1 jmcneill sunxi_codec_transfer(ch); 491 1.1 jmcneill } 492 1.4 bouyer mutex_exit(&sc->sc_intr_lock); 493 1.1 jmcneill } 494 1.1 jmcneill 495 1.1 jmcneill static int 496 1.1 jmcneill sunxi_codec_chan_init(struct sunxi_codec_softc *sc, 497 1.1 jmcneill struct sunxi_codec_chan *ch, u_int mode, const char *dmaname) 498 1.1 jmcneill { 499 1.1 jmcneill ch->ch_sc = sc; 500 1.1 jmcneill ch->ch_mode = mode; 501 1.1 jmcneill ch->ch_dma = fdtbus_dma_get(sc->sc_phandle, dmaname, sunxi_codec_dmaintr, ch); 502 1.1 jmcneill if (ch->ch_dma == NULL) { 503 1.1 jmcneill aprint_error(": couldn't get dma channel \"%s\"\n", dmaname); 504 1.1 jmcneill return ENXIO; 505 1.1 jmcneill } 506 1.1 jmcneill 507 1.1 jmcneill if (mode == AUMODE_PLAY) { 508 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_WRITE; 509 1.1 jmcneill ch->ch_req.dreq_dev_phys = 510 1.1 jmcneill sc->sc_baseaddr + AC_DAC_TXDATA(sc); 511 1.1 jmcneill } else { 512 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_READ; 513 1.1 jmcneill ch->ch_req.dreq_dev_phys = 514 1.1 jmcneill sc->sc_baseaddr + AC_ADC_RXDATA(sc); 515 1.1 jmcneill } 516 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_bus_width = 16; 517 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_burst_len = 4; 518 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_bus_width = 16; 519 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_burst_len = 4; 520 1.1 jmcneill 521 1.1 jmcneill return 0; 522 1.1 jmcneill } 523 1.1 jmcneill 524 1.1 jmcneill static int 525 1.1 jmcneill sunxi_codec_clock_init(int phandle) 526 1.1 jmcneill { 527 1.1 jmcneill struct fdtbus_reset *rst; 528 1.1 jmcneill struct clk *clk; 529 1.1 jmcneill int error; 530 1.1 jmcneill 531 1.1 jmcneill /* Set codec clock to 24.576MHz, suitable for 48 kHz sampling rates */ 532 1.1 jmcneill clk = fdtbus_clock_get(phandle, "codec"); 533 1.1 jmcneill if (clk == NULL) { 534 1.1 jmcneill aprint_error(": couldn't find codec clock\n"); 535 1.1 jmcneill return ENXIO; 536 1.1 jmcneill } 537 1.1 jmcneill error = clk_set_rate(clk, 24576000); 538 1.1 jmcneill if (error != 0) { 539 1.1 jmcneill aprint_error(": couldn't set codec clock rate: %d\n", error); 540 1.1 jmcneill return error; 541 1.1 jmcneill } 542 1.1 jmcneill error = clk_enable(clk); 543 1.1 jmcneill if (error != 0) { 544 1.1 jmcneill aprint_error(": couldn't enable codec clock: %d\n", error); 545 1.1 jmcneill return error; 546 1.1 jmcneill } 547 1.1 jmcneill 548 1.1 jmcneill /* Enable APB clock */ 549 1.1 jmcneill clk = fdtbus_clock_get(phandle, "apb"); 550 1.1 jmcneill if (clk == NULL) { 551 1.1 jmcneill aprint_error(": couldn't find apb clock\n"); 552 1.1 jmcneill return ENXIO; 553 1.1 jmcneill } 554 1.1 jmcneill error = clk_enable(clk); 555 1.1 jmcneill if (error != 0) { 556 1.1 jmcneill aprint_error(": couldn't enable apb clock: %d\n", error); 557 1.1 jmcneill return error; 558 1.1 jmcneill } 559 1.1 jmcneill 560 1.1 jmcneill /* De-assert reset */ 561 1.1 jmcneill rst = fdtbus_reset_get_index(phandle, 0); 562 1.2 jmcneill if (rst != NULL) { 563 1.2 jmcneill error = fdtbus_reset_deassert(rst); 564 1.2 jmcneill if (error != 0) { 565 1.2 jmcneill aprint_error(": couldn't de-assert reset: %d\n", error); 566 1.2 jmcneill return error; 567 1.2 jmcneill } 568 1.1 jmcneill } 569 1.1 jmcneill 570 1.1 jmcneill return 0; 571 1.1 jmcneill } 572 1.1 jmcneill 573 1.1 jmcneill static int 574 1.1 jmcneill sunxi_codec_match(device_t parent, cfdata_t cf, void *aux) 575 1.1 jmcneill { 576 1.1 jmcneill struct fdt_attach_args * const faa = aux; 577 1.1 jmcneill 578 1.12 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 579 1.1 jmcneill } 580 1.1 jmcneill 581 1.1 jmcneill static void 582 1.1 jmcneill sunxi_codec_attach(device_t parent, device_t self, void *aux) 583 1.1 jmcneill { 584 1.1 jmcneill struct sunxi_codec_softc * const sc = device_private(self); 585 1.1 jmcneill struct fdt_attach_args * const faa = aux; 586 1.1 jmcneill const int phandle = faa->faa_phandle; 587 1.1 jmcneill bus_addr_t addr; 588 1.1 jmcneill bus_size_t size; 589 1.1 jmcneill uint32_t val; 590 1.1 jmcneill 591 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 592 1.1 jmcneill aprint_error(": couldn't get registers\n"); 593 1.1 jmcneill return; 594 1.1 jmcneill } 595 1.1 jmcneill 596 1.1 jmcneill if (sunxi_codec_clock_init(phandle) != 0) 597 1.1 jmcneill return; 598 1.1 jmcneill 599 1.1 jmcneill sc->sc_dev = self; 600 1.1 jmcneill sc->sc_phandle = phandle; 601 1.1 jmcneill sc->sc_baseaddr = addr; 602 1.1 jmcneill sc->sc_bst = faa->faa_bst; 603 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 604 1.1 jmcneill aprint_error(": couldn't map registers\n"); 605 1.1 jmcneill return; 606 1.1 jmcneill } 607 1.1 jmcneill sc->sc_dmat = faa->faa_dmat; 608 1.1 jmcneill LIST_INIT(&sc->sc_dmalist); 609 1.12 thorpej sc->sc_cfg = of_compatible_lookup(phandle, compat_data)->data; 610 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 611 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED); 612 1.1 jmcneill 613 1.1 jmcneill if (sunxi_codec_chan_init(sc, &sc->sc_pchan, AUMODE_PLAY, "tx") != 0 || 614 1.1 jmcneill sunxi_codec_chan_init(sc, &sc->sc_rchan, AUMODE_RECORD, "rx") != 0) { 615 1.1 jmcneill aprint_error(": couldn't setup channels\n"); 616 1.1 jmcneill return; 617 1.1 jmcneill } 618 1.1 jmcneill 619 1.1 jmcneill /* Optional PA mute GPIO */ 620 1.1 jmcneill sc->sc_pin_pa = fdtbus_gpio_acquire(phandle, "allwinner,pa-gpios", GPIO_PIN_OUTPUT); 621 1.1 jmcneill 622 1.1 jmcneill aprint_naive("\n"); 623 1.1 jmcneill aprint_normal(": %s\n", sc->sc_cfg->name); 624 1.1 jmcneill 625 1.1 jmcneill /* Enable DAC */ 626 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_DPC(sc)); 627 1.1 jmcneill val |= DAC_DPC_EN_DA; 628 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_DPC(sc), val); 629 1.1 jmcneill 630 1.1 jmcneill /* Initialize codec */ 631 1.1 jmcneill if (sc->sc_cfg->init(sc) != 0) { 632 1.1 jmcneill aprint_error_dev(self, "couldn't initialize codec\n"); 633 1.1 jmcneill return; 634 1.1 jmcneill } 635 1.1 jmcneill 636 1.1 jmcneill sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD; 637 1.1 jmcneill sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE; 638 1.1 jmcneill sc->sc_format.validbits = 16; 639 1.1 jmcneill sc->sc_format.precision = 16; 640 1.1 jmcneill sc->sc_format.channels = 2; 641 1.1 jmcneill sc->sc_format.channel_mask = AUFMT_STEREO; 642 1.6 isaki sc->sc_format.frequency_type = 1; 643 1.6 isaki sc->sc_format.frequency[0] = 48000; 644 1.1 jmcneill 645 1.1 jmcneill audio_attach_mi(&sunxi_codec_hw_if, sc, self); 646 1.1 jmcneill } 647 1.1 jmcneill 648 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_codec, sizeof(struct sunxi_codec_softc), 649 1.1 jmcneill sunxi_codec_match, sunxi_codec_attach, NULL, NULL); 650 1.1 jmcneill 651 1.1 jmcneill #ifdef DDB 652 1.1 jmcneill void sunxicodec_dump(void); 653 1.1 jmcneill 654 1.1 jmcneill void 655 1.1 jmcneill sunxicodec_dump(void) 656 1.1 jmcneill { 657 1.1 jmcneill struct sunxi_codec_softc *sc; 658 1.1 jmcneill device_t dev; 659 1.1 jmcneill 660 1.1 jmcneill dev = device_find_by_driver_unit("sunxicodec", 0); 661 1.1 jmcneill if (dev == NULL) 662 1.1 jmcneill return; 663 1.1 jmcneill sc = device_private(dev); 664 1.1 jmcneill 665 1.1 jmcneill device_printf(dev, "AC_DAC_DPC: %08x\n", CODEC_READ(sc, AC_DAC_DPC(sc))); 666 1.1 jmcneill device_printf(dev, "AC_DAC_FIFOC: %08x\n", CODEC_READ(sc, AC_DAC_FIFOC(sc))); 667 1.1 jmcneill device_printf(dev, "AC_DAC_FIFOS: %08x\n", CODEC_READ(sc, AC_DAC_FIFOS(sc))); 668 1.1 jmcneill device_printf(dev, "AC_ADC_FIFOC: %08x\n", CODEC_READ(sc, AC_ADC_FIFOC(sc))); 669 1.1 jmcneill device_printf(dev, "AC_ADC_FIFOS: %08x\n", CODEC_READ(sc, AC_ADC_FIFOS(sc))); 670 1.1 jmcneill device_printf(dev, "AC_DAC_CNT: %08x\n", CODEC_READ(sc, AC_DAC_CNT(sc))); 671 1.1 jmcneill device_printf(dev, "AC_ADC_CNT: %08x\n", CODEC_READ(sc, AC_ADC_CNT(sc))); 672 1.1 jmcneill } 673 1.1 jmcneill #endif 674