1ba9b7163SAndrew Turner /*- 2ba9b7163SAndrew Turner * Copyright (c) 2014-2016 Jared D. McNeill <jmcneill@invisible.ca> 3ba9b7163SAndrew Turner * All rights reserved. 4ba9b7163SAndrew Turner * 5ba9b7163SAndrew Turner * Redistribution and use in source and binary forms, with or without 6ba9b7163SAndrew Turner * modification, are permitted provided that the following conditions 7ba9b7163SAndrew Turner * are met: 8ba9b7163SAndrew Turner * 1. Redistributions of source code must retain the above copyright 9ba9b7163SAndrew Turner * notice, this list of conditions and the following disclaimer. 10ba9b7163SAndrew Turner * 2. Redistributions in binary form must reproduce the above copyright 11ba9b7163SAndrew Turner * notice, this list of conditions and the following disclaimer in the 12ba9b7163SAndrew Turner * documentation and/or other materials provided with the distribution. 13ba9b7163SAndrew Turner * 14ba9b7163SAndrew Turner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15ba9b7163SAndrew Turner * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16ba9b7163SAndrew Turner * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17ba9b7163SAndrew Turner * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18ba9b7163SAndrew Turner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 19ba9b7163SAndrew Turner * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20ba9b7163SAndrew Turner * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 21ba9b7163SAndrew Turner * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22ba9b7163SAndrew Turner * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23ba9b7163SAndrew Turner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24ba9b7163SAndrew Turner * SUCH DAMAGE. 25ba9b7163SAndrew Turner * 26ba9b7163SAndrew Turner * $FreeBSD$ 27ba9b7163SAndrew Turner */ 28ba9b7163SAndrew Turner 29ba9b7163SAndrew Turner /* 3016025c35SJared McNeill * Allwinner A10/A20 and H3 Audio Codec 31ba9b7163SAndrew Turner */ 32ba9b7163SAndrew Turner 33ba9b7163SAndrew Turner #include <sys/cdefs.h> 34ba9b7163SAndrew Turner __FBSDID("$FreeBSD$"); 35ba9b7163SAndrew Turner 36ba9b7163SAndrew Turner #include <sys/param.h> 37ba9b7163SAndrew Turner #include <sys/systm.h> 38ba9b7163SAndrew Turner #include <sys/bus.h> 39ba9b7163SAndrew Turner #include <sys/rman.h> 40ba9b7163SAndrew Turner #include <sys/condvar.h> 41ba9b7163SAndrew Turner #include <sys/kernel.h> 42ba9b7163SAndrew Turner #include <sys/module.h> 43ba9b7163SAndrew Turner #include <sys/gpio.h> 44ba9b7163SAndrew Turner 45ba9b7163SAndrew Turner #include <machine/bus.h> 46ba9b7163SAndrew Turner 47ba9b7163SAndrew Turner #include <dev/sound/pcm/sound.h> 48ba9b7163SAndrew Turner #include <dev/sound/chip.h> 49ba9b7163SAndrew Turner 50ba9b7163SAndrew Turner #include <dev/ofw/ofw_bus.h> 51ba9b7163SAndrew Turner #include <dev/ofw/ofw_bus_subr.h> 52ba9b7163SAndrew Turner 5316025c35SJared McNeill #include <dev/gpio/gpiobusvar.h> 5416025c35SJared McNeill 556a05f063SJared McNeill #include <dev/extres/clk/clk.h> 5616025c35SJared McNeill #include <dev/extres/hwreset/hwreset.h> 57ba9b7163SAndrew Turner 58ba9b7163SAndrew Turner #include "sunxi_dma_if.h" 59ba9b7163SAndrew Turner #include "mixer_if.h" 6016025c35SJared McNeill 6116025c35SJared McNeill struct a10codec_info; 6216025c35SJared McNeill 6316025c35SJared McNeill struct a10codec_config { 6416025c35SJared McNeill /* mixer class */ 6516025c35SJared McNeill struct kobj_class *mixer_class; 6616025c35SJared McNeill 6716025c35SJared McNeill /* toggle DAC/ADC mute */ 6816025c35SJared McNeill void (*mute)(struct a10codec_info *, int, int); 6916025c35SJared McNeill 7016025c35SJared McNeill /* DRQ types */ 7116025c35SJared McNeill u_int drqtype_codec; 7216025c35SJared McNeill u_int drqtype_sdram; 7316025c35SJared McNeill 7416025c35SJared McNeill /* register map */ 7516025c35SJared McNeill bus_size_t DPC, 7616025c35SJared McNeill DAC_FIFOC, 7716025c35SJared McNeill DAC_FIFOS, 7816025c35SJared McNeill DAC_TXDATA, 7916025c35SJared McNeill ADC_FIFOC, 8016025c35SJared McNeill ADC_FIFOS, 8116025c35SJared McNeill ADC_RXDATA, 8216025c35SJared McNeill DAC_CNT, 8316025c35SJared McNeill ADC_CNT; 8416025c35SJared McNeill }; 85ba9b7163SAndrew Turner 86ba9b7163SAndrew Turner #define TX_TRIG_LEVEL 0xf 87ba9b7163SAndrew Turner #define RX_TRIG_LEVEL 0x7 88ba9b7163SAndrew Turner #define DRQ_CLR_CNT 0x3 89ba9b7163SAndrew Turner 9016025c35SJared McNeill #define AC_DAC_DPC(_sc) ((_sc)->cfg->DPC) 91ba9b7163SAndrew Turner #define DAC_DPC_EN_DA 0x80000000 9216025c35SJared McNeill #define AC_DAC_FIFOC(_sc) ((_sc)->cfg->DAC_FIFOC) 93ba9b7163SAndrew Turner #define DAC_FIFOC_FS_SHIFT 29 94ba9b7163SAndrew Turner #define DAC_FIFOC_FS_MASK (7U << DAC_FIFOC_FS_SHIFT) 95ba9b7163SAndrew Turner #define DAC_FS_48KHZ 0 96ba9b7163SAndrew Turner #define DAC_FS_32KHZ 1 97ba9b7163SAndrew Turner #define DAC_FS_24KHZ 2 98ba9b7163SAndrew Turner #define DAC_FS_16KHZ 3 99ba9b7163SAndrew Turner #define DAC_FS_12KHZ 4 100ba9b7163SAndrew Turner #define DAC_FS_8KHZ 5 101ba9b7163SAndrew Turner #define DAC_FS_192KHZ 6 102ba9b7163SAndrew Turner #define DAC_FS_96KHZ 7 103ba9b7163SAndrew Turner #define DAC_FIFOC_FIFO_MODE_SHIFT 24 104ba9b7163SAndrew Turner #define DAC_FIFOC_FIFO_MODE_MASK (3U << DAC_FIFOC_FIFO_MODE_SHIFT) 105ba9b7163SAndrew Turner #define FIFO_MODE_24_31_8 0 106ba9b7163SAndrew Turner #define FIFO_MODE_16_31_16 0 107ba9b7163SAndrew Turner #define FIFO_MODE_16_15_0 1 108ba9b7163SAndrew Turner #define DAC_FIFOC_DRQ_CLR_CNT_SHIFT 21 109ba9b7163SAndrew Turner #define DAC_FIFOC_DRQ_CLR_CNT_MASK (3U << DAC_FIFOC_DRQ_CLR_CNT_SHIFT) 110ba9b7163SAndrew Turner #define DAC_FIFOC_TX_TRIG_LEVEL_SHIFT 8 111ba9b7163SAndrew Turner #define DAC_FIFOC_TX_TRIG_LEVEL_MASK (0x7f << DAC_FIFOC_TX_TRIG_LEVEL_SHIFT) 112ba9b7163SAndrew Turner #define DAC_FIFOC_MONO_EN (1U << 6) 113ba9b7163SAndrew Turner #define DAC_FIFOC_TX_BITS (1U << 5) 114ba9b7163SAndrew Turner #define DAC_FIFOC_DRQ_EN (1U << 4) 115ba9b7163SAndrew Turner #define DAC_FIFOC_FIFO_FLUSH (1U << 0) 11616025c35SJared McNeill #define AC_DAC_FIFOS(_sc) ((_sc)->cfg->DAC_FIFOS) 11716025c35SJared McNeill #define AC_DAC_TXDATA(_sc) ((_sc)->cfg->DAC_TXDATA) 11816025c35SJared McNeill #define AC_ADC_FIFOC(_sc) ((_sc)->cfg->ADC_FIFOC) 119ba9b7163SAndrew Turner #define ADC_FIFOC_FS_SHIFT 29 120ba9b7163SAndrew Turner #define ADC_FIFOC_FS_MASK (7U << ADC_FIFOC_FS_SHIFT) 121ba9b7163SAndrew Turner #define ADC_FS_48KHZ 0 122ba9b7163SAndrew Turner #define ADC_FIFOC_EN_AD (1U << 28) 123ba9b7163SAndrew Turner #define ADC_FIFOC_RX_FIFO_MODE (1U << 24) 124ba9b7163SAndrew Turner #define ADC_FIFOC_RX_TRIG_LEVEL_SHIFT 8 125ba9b7163SAndrew Turner #define ADC_FIFOC_RX_TRIG_LEVEL_MASK (0x1f << ADC_FIFOC_RX_TRIG_LEVEL_SHIFT) 126ba9b7163SAndrew Turner #define ADC_FIFOC_MONO_EN (1U << 7) 127ba9b7163SAndrew Turner #define ADC_FIFOC_RX_BITS (1U << 6) 128ba9b7163SAndrew Turner #define ADC_FIFOC_DRQ_EN (1U << 4) 129ba9b7163SAndrew Turner #define ADC_FIFOC_FIFO_FLUSH (1U << 1) 13016025c35SJared McNeill #define AC_ADC_FIFOS(_sc) ((_sc)->cfg->ADC_FIFOS) 13116025c35SJared McNeill #define AC_ADC_RXDATA(_sc) ((_sc)->cfg->ADC_RXDATA) 13216025c35SJared McNeill #define AC_DAC_CNT(_sc) ((_sc)->cfg->DAC_CNT) 13316025c35SJared McNeill #define AC_ADC_CNT(_sc) ((_sc)->cfg->ADC_CNT) 134ba9b7163SAndrew Turner 135ba9b7163SAndrew Turner static uint32_t a10codec_fmt[] = { 136ba9b7163SAndrew Turner SND_FORMAT(AFMT_S16_LE, 1, 0), 137ba9b7163SAndrew Turner SND_FORMAT(AFMT_S16_LE, 2, 0), 138ba9b7163SAndrew Turner 0 139ba9b7163SAndrew Turner }; 140ba9b7163SAndrew Turner 141ba9b7163SAndrew Turner static struct pcmchan_caps a10codec_pcaps = { 8000, 192000, a10codec_fmt, 0 }; 142ba9b7163SAndrew Turner static struct pcmchan_caps a10codec_rcaps = { 8000, 48000, a10codec_fmt, 0 }; 143ba9b7163SAndrew Turner 144ba9b7163SAndrew Turner struct a10codec_info; 145ba9b7163SAndrew Turner 146ba9b7163SAndrew Turner struct a10codec_chinfo { 147ba9b7163SAndrew Turner struct snd_dbuf *buffer; 148ba9b7163SAndrew Turner struct pcm_channel *channel; 149ba9b7163SAndrew Turner struct a10codec_info *parent; 150ba9b7163SAndrew Turner bus_dmamap_t dmamap; 151ba9b7163SAndrew Turner void *dmaaddr; 152ba9b7163SAndrew Turner bus_addr_t physaddr; 153ba9b7163SAndrew Turner bus_size_t fifo; 154ba9b7163SAndrew Turner device_t dmac; 155ba9b7163SAndrew Turner void *dmachan; 156ba9b7163SAndrew Turner 157ba9b7163SAndrew Turner int dir; 158ba9b7163SAndrew Turner int run; 159ba9b7163SAndrew Turner uint32_t pos; 160ba9b7163SAndrew Turner uint32_t format; 161ba9b7163SAndrew Turner uint32_t blocksize; 162ba9b7163SAndrew Turner uint32_t speed; 163ba9b7163SAndrew Turner }; 164ba9b7163SAndrew Turner 165ba9b7163SAndrew Turner struct a10codec_info { 166ba9b7163SAndrew Turner device_t dev; 167*bfcf888aSEmmanuel Vadot struct resource *res[2]; 168ba9b7163SAndrew Turner struct mtx *lock; 169ba9b7163SAndrew Turner bus_dma_tag_t dmat; 170ba9b7163SAndrew Turner unsigned dmasize; 171ba9b7163SAndrew Turner void *ih; 172ba9b7163SAndrew Turner 17316025c35SJared McNeill struct a10codec_config *cfg; 174ba9b7163SAndrew Turner 175ba9b7163SAndrew Turner struct a10codec_chinfo play; 176ba9b7163SAndrew Turner struct a10codec_chinfo rec; 177ba9b7163SAndrew Turner }; 178ba9b7163SAndrew Turner 179ba9b7163SAndrew Turner static struct resource_spec a10codec_spec[] = { 180ba9b7163SAndrew Turner { SYS_RES_MEMORY, 0, RF_ACTIVE }, 181ba9b7163SAndrew Turner { -1, 0 } 182ba9b7163SAndrew Turner }; 183ba9b7163SAndrew Turner 184*bfcf888aSEmmanuel Vadot #define CODEC_ANALOG_READ(sc, reg) bus_read_4((sc)->res[1], (reg)) 185*bfcf888aSEmmanuel Vadot #define CODEC_ANALOG_WRITE(sc, reg, val) bus_write_4((sc)->res[1], (reg), (val)) 186*bfcf888aSEmmanuel Vadot 187ba9b7163SAndrew Turner #define CODEC_READ(sc, reg) bus_read_4((sc)->res[0], (reg)) 188ba9b7163SAndrew Turner #define CODEC_WRITE(sc, reg, val) bus_write_4((sc)->res[0], (reg), (val)) 189ba9b7163SAndrew Turner 190ba9b7163SAndrew Turner /* 19116025c35SJared McNeill * A10/A20 mixer interface 192ba9b7163SAndrew Turner */ 193ba9b7163SAndrew Turner 19416025c35SJared McNeill #define A10_DAC_ACTL 0x10 19516025c35SJared McNeill #define A10_DACAREN (1U << 31) 19616025c35SJared McNeill #define A10_DACALEN (1U << 30) 19716025c35SJared McNeill #define A10_MIXEN (1U << 29) 19816025c35SJared McNeill #define A10_DACPAS (1U << 8) 19916025c35SJared McNeill #define A10_PAMUTE (1U << 6) 20016025c35SJared McNeill #define A10_PAVOL_SHIFT 0 20116025c35SJared McNeill #define A10_PAVOL_MASK (0x3f << A10_PAVOL_SHIFT) 20216025c35SJared McNeill #define A10_ADC_ACTL 0x28 20316025c35SJared McNeill #define A10_ADCREN (1U << 31) 20416025c35SJared McNeill #define A10_ADCLEN (1U << 30) 20516025c35SJared McNeill #define A10_PREG1EN (1U << 29) 20616025c35SJared McNeill #define A10_PREG2EN (1U << 28) 20716025c35SJared McNeill #define A10_VMICEN (1U << 27) 20816025c35SJared McNeill #define A10_ADCG_SHIFT 20 20916025c35SJared McNeill #define A10_ADCG_MASK (7U << A10_ADCG_SHIFT) 21016025c35SJared McNeill #define A10_ADCIS_SHIFT 17 21116025c35SJared McNeill #define A10_ADCIS_MASK (7U << A10_ADCIS_SHIFT) 21216025c35SJared McNeill #define A10_ADC_IS_LINEIN 0 21316025c35SJared McNeill #define A10_ADC_IS_FMIN 1 21416025c35SJared McNeill #define A10_ADC_IS_MIC1 2 21516025c35SJared McNeill #define A10_ADC_IS_MIC2 3 21616025c35SJared McNeill #define A10_ADC_IS_MIC1_L_MIC2_R 4 21716025c35SJared McNeill #define A10_ADC_IS_MIC1_LR_MIC2_LR 5 21816025c35SJared McNeill #define A10_ADC_IS_OMIX 6 21916025c35SJared McNeill #define A10_ADC_IS_LINEIN_L_MIC1_R 7 22016025c35SJared McNeill #define A10_LNRDF (1U << 16) 22116025c35SJared McNeill #define A10_LNPREG_SHIFT 13 22216025c35SJared McNeill #define A10_LNPREG_MASK (7U << A10_LNPREG_SHIFT) 22316025c35SJared McNeill #define A10_PA_EN (1U << 4) 22416025c35SJared McNeill #define A10_DDE (1U << 3) 22516025c35SJared McNeill 226ba9b7163SAndrew Turner static int 22716025c35SJared McNeill a10_mixer_init(struct snd_mixer *m) 228ba9b7163SAndrew Turner { 229ba9b7163SAndrew Turner struct a10codec_info *sc = mix_getdevinfo(m); 230ba9b7163SAndrew Turner uint32_t val; 231ba9b7163SAndrew Turner 232ba9b7163SAndrew Turner mix_setdevs(m, SOUND_MASK_VOLUME | SOUND_MASK_LINE | SOUND_MASK_RECLEV); 233ba9b7163SAndrew Turner mix_setrecdevs(m, SOUND_MASK_LINE | SOUND_MASK_LINE1 | SOUND_MASK_MIC); 234ba9b7163SAndrew Turner 235ba9b7163SAndrew Turner /* Unmute input source to PA */ 23616025c35SJared McNeill val = CODEC_READ(sc, A10_DAC_ACTL); 23716025c35SJared McNeill val |= A10_PAMUTE; 23816025c35SJared McNeill CODEC_WRITE(sc, A10_DAC_ACTL, val); 239ba9b7163SAndrew Turner 240ba9b7163SAndrew Turner /* Enable PA */ 24116025c35SJared McNeill val = CODEC_READ(sc, A10_ADC_ACTL); 24216025c35SJared McNeill val |= A10_PA_EN; 24316025c35SJared McNeill CODEC_WRITE(sc, A10_ADC_ACTL, val); 244ba9b7163SAndrew Turner 245ba9b7163SAndrew Turner return (0); 246ba9b7163SAndrew Turner } 247ba9b7163SAndrew Turner 24816025c35SJared McNeill static const struct a10_mixer { 249ba9b7163SAndrew Turner unsigned reg; 250ba9b7163SAndrew Turner unsigned mask; 251ba9b7163SAndrew Turner unsigned shift; 25216025c35SJared McNeill } a10_mixers[SOUND_MIXER_NRDEVICES] = { 25316025c35SJared McNeill [SOUND_MIXER_VOLUME] = { A10_DAC_ACTL, A10_PAVOL_MASK, 25416025c35SJared McNeill A10_PAVOL_SHIFT }, 25516025c35SJared McNeill [SOUND_MIXER_LINE] = { A10_ADC_ACTL, A10_LNPREG_MASK, 25616025c35SJared McNeill A10_LNPREG_SHIFT }, 25716025c35SJared McNeill [SOUND_MIXER_RECLEV] = { A10_ADC_ACTL, A10_ADCG_MASK, 25816025c35SJared McNeill A10_ADCG_SHIFT }, 259ba9b7163SAndrew Turner }; 260ba9b7163SAndrew Turner 261ba9b7163SAndrew Turner static int 26216025c35SJared McNeill a10_mixer_set(struct snd_mixer *m, unsigned dev, unsigned left, 263ba9b7163SAndrew Turner unsigned right) 264ba9b7163SAndrew Turner { 265ba9b7163SAndrew Turner struct a10codec_info *sc = mix_getdevinfo(m); 266ba9b7163SAndrew Turner uint32_t val; 267ba9b7163SAndrew Turner unsigned nvol, max; 268ba9b7163SAndrew Turner 26916025c35SJared McNeill max = a10_mixers[dev].mask >> a10_mixers[dev].shift; 270ba9b7163SAndrew Turner nvol = (left * max) / 100; 271ba9b7163SAndrew Turner 27216025c35SJared McNeill val = CODEC_READ(sc, a10_mixers[dev].reg); 27316025c35SJared McNeill val &= ~a10_mixers[dev].mask; 27416025c35SJared McNeill val |= (nvol << a10_mixers[dev].shift); 27516025c35SJared McNeill CODEC_WRITE(sc, a10_mixers[dev].reg, val); 276ba9b7163SAndrew Turner 277ba9b7163SAndrew Turner left = right = (left * 100) / max; 278ba9b7163SAndrew Turner return (left | (right << 8)); 279ba9b7163SAndrew Turner } 280ba9b7163SAndrew Turner 281ba9b7163SAndrew Turner static uint32_t 28216025c35SJared McNeill a10_mixer_setrecsrc(struct snd_mixer *m, uint32_t src) 283ba9b7163SAndrew Turner { 284ba9b7163SAndrew Turner struct a10codec_info *sc = mix_getdevinfo(m); 285ba9b7163SAndrew Turner uint32_t val; 286ba9b7163SAndrew Turner 28716025c35SJared McNeill val = CODEC_READ(sc, A10_ADC_ACTL); 288ba9b7163SAndrew Turner 289ba9b7163SAndrew Turner switch (src) { 290ba9b7163SAndrew Turner case SOUND_MASK_LINE: /* line-in */ 29116025c35SJared McNeill val &= ~A10_ADCIS_MASK; 29216025c35SJared McNeill val |= (A10_ADC_IS_LINEIN << A10_ADCIS_SHIFT); 293ba9b7163SAndrew Turner break; 294ba9b7163SAndrew Turner case SOUND_MASK_MIC: /* MIC1 */ 29516025c35SJared McNeill val &= ~A10_ADCIS_MASK; 29616025c35SJared McNeill val |= (A10_ADC_IS_MIC1 << A10_ADCIS_SHIFT); 297ba9b7163SAndrew Turner break; 298ba9b7163SAndrew Turner case SOUND_MASK_LINE1: /* MIC2 */ 29916025c35SJared McNeill val &= ~A10_ADCIS_MASK; 30016025c35SJared McNeill val |= (A10_ADC_IS_MIC2 << A10_ADCIS_SHIFT); 301ba9b7163SAndrew Turner break; 302ba9b7163SAndrew Turner default: 303ba9b7163SAndrew Turner break; 304ba9b7163SAndrew Turner } 305ba9b7163SAndrew Turner 30616025c35SJared McNeill CODEC_WRITE(sc, A10_ADC_ACTL, val); 307ba9b7163SAndrew Turner 30816025c35SJared McNeill switch ((val & A10_ADCIS_MASK) >> A10_ADCIS_SHIFT) { 30916025c35SJared McNeill case A10_ADC_IS_LINEIN: 310ba9b7163SAndrew Turner return (SOUND_MASK_LINE); 31116025c35SJared McNeill case A10_ADC_IS_MIC1: 312ba9b7163SAndrew Turner return (SOUND_MASK_MIC); 31316025c35SJared McNeill case A10_ADC_IS_MIC2: 314ba9b7163SAndrew Turner return (SOUND_MASK_LINE1); 315ba9b7163SAndrew Turner default: 316ba9b7163SAndrew Turner return (0); 317ba9b7163SAndrew Turner } 318ba9b7163SAndrew Turner } 319ba9b7163SAndrew Turner 32016025c35SJared McNeill static void 32116025c35SJared McNeill a10_mute(struct a10codec_info *sc, int mute, int dir) 32216025c35SJared McNeill { 32316025c35SJared McNeill uint32_t val; 32416025c35SJared McNeill 32516025c35SJared McNeill if (dir == PCMDIR_PLAY) { 32616025c35SJared McNeill val = CODEC_READ(sc, A10_DAC_ACTL); 32716025c35SJared McNeill if (mute) { 32816025c35SJared McNeill /* Disable DAC analog l/r channels and output mixer */ 32916025c35SJared McNeill val &= ~A10_DACAREN; 33016025c35SJared McNeill val &= ~A10_DACALEN; 33116025c35SJared McNeill val &= ~A10_DACPAS; 33216025c35SJared McNeill } else { 33316025c35SJared McNeill /* Enable DAC analog l/r channels and output mixer */ 33416025c35SJared McNeill val |= A10_DACAREN; 33516025c35SJared McNeill val |= A10_DACALEN; 33616025c35SJared McNeill val |= A10_DACPAS; 33716025c35SJared McNeill } 33816025c35SJared McNeill CODEC_WRITE(sc, A10_DAC_ACTL, val); 33916025c35SJared McNeill } else { 34016025c35SJared McNeill val = CODEC_READ(sc, A10_ADC_ACTL); 34116025c35SJared McNeill if (mute) { 34216025c35SJared McNeill /* Disable ADC analog l/r channels, MIC1 preamp, 34316025c35SJared McNeill * and VMIC pin voltage 34416025c35SJared McNeill */ 34516025c35SJared McNeill val &= ~A10_ADCREN; 34616025c35SJared McNeill val &= ~A10_ADCLEN; 34716025c35SJared McNeill val &= ~A10_PREG1EN; 34816025c35SJared McNeill val &= ~A10_VMICEN; 34916025c35SJared McNeill } else { 35016025c35SJared McNeill /* Enable ADC analog l/r channels, MIC1 preamp, 35116025c35SJared McNeill * and VMIC pin voltage 35216025c35SJared McNeill */ 35316025c35SJared McNeill val |= A10_ADCREN; 35416025c35SJared McNeill val |= A10_ADCLEN; 35516025c35SJared McNeill val |= A10_PREG1EN; 35616025c35SJared McNeill val |= A10_VMICEN; 35716025c35SJared McNeill } 35816025c35SJared McNeill CODEC_WRITE(sc, A10_ADC_ACTL, val); 35916025c35SJared McNeill } 36016025c35SJared McNeill } 36116025c35SJared McNeill 36216025c35SJared McNeill static kobj_method_t a10_mixer_methods[] = { 36316025c35SJared McNeill KOBJMETHOD(mixer_init, a10_mixer_init), 36416025c35SJared McNeill KOBJMETHOD(mixer_set, a10_mixer_set), 36516025c35SJared McNeill KOBJMETHOD(mixer_setrecsrc, a10_mixer_setrecsrc), 366ba9b7163SAndrew Turner KOBJMETHOD_END 367ba9b7163SAndrew Turner }; 36816025c35SJared McNeill MIXER_DECLARE(a10_mixer); 36916025c35SJared McNeill 37016025c35SJared McNeill 37116025c35SJared McNeill /* 37216025c35SJared McNeill * H3 mixer interface 37316025c35SJared McNeill */ 37416025c35SJared McNeill 37516025c35SJared McNeill #define H3_PR_CFG 0x00 376*bfcf888aSEmmanuel Vadot #define H3_AC_PR_RST (1 << 28) 37716025c35SJared McNeill #define H3_AC_PR_RW (1 << 24) 37816025c35SJared McNeill #define H3_AC_PR_ADDR_SHIFT 16 37916025c35SJared McNeill #define H3_AC_PR_ADDR_MASK (0x1f << H3_AC_PR_ADDR_SHIFT) 38016025c35SJared McNeill #define H3_ACDA_PR_WDAT_SHIFT 8 38116025c35SJared McNeill #define H3_ACDA_PR_WDAT_MASK (0xff << H3_ACDA_PR_WDAT_SHIFT) 38216025c35SJared McNeill #define H3_ACDA_PR_RDAT_SHIFT 0 38316025c35SJared McNeill #define H3_ACDA_PR_RDAT_MASK (0xff << H3_ACDA_PR_RDAT_SHIFT) 38416025c35SJared McNeill 38516025c35SJared McNeill #define H3_LOMIXSC 0x01 38616025c35SJared McNeill #define H3_LOMIXSC_LDAC (1 << 1) 38716025c35SJared McNeill #define H3_ROMIXSC 0x02 38816025c35SJared McNeill #define H3_ROMIXSC_RDAC (1 << 1) 38916025c35SJared McNeill #define H3_DAC_PA_SRC 0x03 39016025c35SJared McNeill #define H3_DACAREN (1 << 7) 39116025c35SJared McNeill #define H3_DACALEN (1 << 6) 39216025c35SJared McNeill #define H3_RMIXEN (1 << 5) 39316025c35SJared McNeill #define H3_LMIXEN (1 << 4) 39416025c35SJared McNeill #define H3_LINEIN_GCTR 0x05 39516025c35SJared McNeill #define H3_LINEING_SHIFT 4 39616025c35SJared McNeill #define H3_LINEING_MASK (0x7 << H3_LINEING_SHIFT) 39716025c35SJared McNeill #define H3_MIC_GCTR 0x06 39816025c35SJared McNeill #define H3_MIC1_GAIN_SHIFT 4 39916025c35SJared McNeill #define H3_MIC1_GAIN_MASK (0x7 << H3_MIC1_GAIN_SHIFT) 40016025c35SJared McNeill #define H3_MIC2_GAIN_SHIFT 0 40116025c35SJared McNeill #define H3_MIC2_GAIN_MASK (0x7 << H3_MIC2_GAIN_SHIFT) 40216025c35SJared McNeill #define H3_PAEN_CTR 0x07 40316025c35SJared McNeill #define H3_LINEOUTEN (1 << 7) 40416025c35SJared McNeill #define H3_LINEOUT_VOLC 0x09 40516025c35SJared McNeill #define H3_LINEOUTVOL_SHIFT 3 40616025c35SJared McNeill #define H3_LINEOUTVOL_MASK (0x1f << H3_LINEOUTVOL_SHIFT) 40716025c35SJared McNeill #define H3_MIC2G_LINEOUT_CTR 0x0a 40816025c35SJared McNeill #define H3_LINEOUT_LSEL (1 << 3) 40916025c35SJared McNeill #define H3_LINEOUT_RSEL (1 << 2) 41016025c35SJared McNeill #define H3_LADCMIXSC 0x0c 41116025c35SJared McNeill #define H3_RADCMIXSC 0x0d 41216025c35SJared McNeill #define H3_ADCMIXSC_MIC1 (1 << 6) 41316025c35SJared McNeill #define H3_ADCMIXSC_MIC2 (1 << 5) 41416025c35SJared McNeill #define H3_ADCMIXSC_LINEIN (1 << 2) 41516025c35SJared McNeill #define H3_ADCMIXSC_OMIXER (3 << 0) 41616025c35SJared McNeill #define H3_ADC_AP_EN 0x0f 41716025c35SJared McNeill #define H3_ADCREN (1 << 7) 41816025c35SJared McNeill #define H3_ADCLEN (1 << 6) 41916025c35SJared McNeill #define H3_ADCG_SHIFT 0 42016025c35SJared McNeill #define H3_ADCG_MASK (0x7 << H3_ADCG_SHIFT) 42116025c35SJared McNeill 42216025c35SJared McNeill static u_int 42316025c35SJared McNeill h3_pr_read(struct a10codec_info *sc, u_int addr) 42416025c35SJared McNeill { 42516025c35SJared McNeill uint32_t val; 42616025c35SJared McNeill 42716025c35SJared McNeill /* Read current value */ 428*bfcf888aSEmmanuel Vadot val = CODEC_ANALOG_READ(sc, H3_PR_CFG); 42916025c35SJared McNeill 43016025c35SJared McNeill /* De-assert reset */ 43116025c35SJared McNeill val |= H3_AC_PR_RST; 432*bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val); 43316025c35SJared McNeill 43416025c35SJared McNeill /* Read mode */ 43516025c35SJared McNeill val &= ~H3_AC_PR_RW; 436*bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val); 43716025c35SJared McNeill 43816025c35SJared McNeill /* Set address */ 43916025c35SJared McNeill val &= ~H3_AC_PR_ADDR_MASK; 44016025c35SJared McNeill val |= (addr << H3_AC_PR_ADDR_SHIFT); 441*bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val); 44216025c35SJared McNeill 44316025c35SJared McNeill /* Read data */ 444*bfcf888aSEmmanuel Vadot return (CODEC_ANALOG_READ(sc , H3_PR_CFG) & H3_ACDA_PR_RDAT_MASK); 44516025c35SJared McNeill } 44616025c35SJared McNeill 44716025c35SJared McNeill static void 44816025c35SJared McNeill h3_pr_write(struct a10codec_info *sc, u_int addr, u_int data) 44916025c35SJared McNeill { 45016025c35SJared McNeill uint32_t val; 45116025c35SJared McNeill 45216025c35SJared McNeill /* Read current value */ 453*bfcf888aSEmmanuel Vadot val = CODEC_ANALOG_READ(sc, H3_PR_CFG); 45416025c35SJared McNeill 45516025c35SJared McNeill /* De-assert reset */ 45616025c35SJared McNeill val |= H3_AC_PR_RST; 457*bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val); 45816025c35SJared McNeill 45916025c35SJared McNeill /* Set address */ 46016025c35SJared McNeill val &= ~H3_AC_PR_ADDR_MASK; 46116025c35SJared McNeill val |= (addr << H3_AC_PR_ADDR_SHIFT); 462*bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val); 46316025c35SJared McNeill 46416025c35SJared McNeill /* Write data */ 46516025c35SJared McNeill val &= ~H3_ACDA_PR_WDAT_MASK; 46616025c35SJared McNeill val |= (data << H3_ACDA_PR_WDAT_SHIFT); 467*bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val); 46816025c35SJared McNeill 46916025c35SJared McNeill /* Write mode */ 47016025c35SJared McNeill val |= H3_AC_PR_RW; 471*bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val); 47216025c35SJared McNeill } 47316025c35SJared McNeill 47416025c35SJared McNeill static void 47516025c35SJared McNeill h3_pr_set_clear(struct a10codec_info *sc, u_int addr, u_int set, u_int clr) 47616025c35SJared McNeill { 47716025c35SJared McNeill u_int old, new; 47816025c35SJared McNeill 47916025c35SJared McNeill old = h3_pr_read(sc, addr); 48016025c35SJared McNeill new = set | (old & ~clr); 48116025c35SJared McNeill h3_pr_write(sc, addr, new); 48216025c35SJared McNeill } 48316025c35SJared McNeill 48416025c35SJared McNeill static int 48516025c35SJared McNeill h3_mixer_init(struct snd_mixer *m) 48616025c35SJared McNeill { 487*bfcf888aSEmmanuel Vadot int rid=1; 488*bfcf888aSEmmanuel Vadot pcell_t reg[2]; 489*bfcf888aSEmmanuel Vadot phandle_t analogref; 49016025c35SJared McNeill struct a10codec_info *sc = mix_getdevinfo(m); 49116025c35SJared McNeill 492*bfcf888aSEmmanuel Vadot if (OF_getencprop(ofw_bus_get_node(sc->dev), "allwinner,codec-analog-controls", 493*bfcf888aSEmmanuel Vadot &analogref, sizeof(analogref)) <= 0) { 494*bfcf888aSEmmanuel Vadot return (ENXIO); 495*bfcf888aSEmmanuel Vadot } 496*bfcf888aSEmmanuel Vadot 497*bfcf888aSEmmanuel Vadot if (OF_getencprop(OF_node_from_xref(analogref), "reg", 498*bfcf888aSEmmanuel Vadot reg, sizeof(reg)) <= 0) { 499*bfcf888aSEmmanuel Vadot return (ENXIO); 500*bfcf888aSEmmanuel Vadot } 501*bfcf888aSEmmanuel Vadot 502*bfcf888aSEmmanuel Vadot sc->res[1] = bus_alloc_resource(sc->dev, SYS_RES_MEMORY, &rid, reg[0], 503*bfcf888aSEmmanuel Vadot reg[0]+reg[1], reg[1], RF_ACTIVE ); 504*bfcf888aSEmmanuel Vadot 505*bfcf888aSEmmanuel Vadot if (sc->res[1] == NULL) { 506*bfcf888aSEmmanuel Vadot return (ENXIO); 507*bfcf888aSEmmanuel Vadot } 508*bfcf888aSEmmanuel Vadot 50916025c35SJared McNeill mix_setdevs(m, SOUND_MASK_PCM | SOUND_MASK_VOLUME | SOUND_MASK_RECLEV | 51016025c35SJared McNeill SOUND_MASK_MIC | SOUND_MASK_LINE | SOUND_MASK_LINE1); 51116025c35SJared McNeill mix_setrecdevs(m, SOUND_MASK_MIC | SOUND_MASK_LINE | SOUND_MASK_LINE1 | 51216025c35SJared McNeill SOUND_MASK_IMIX); 51316025c35SJared McNeill 51416025c35SJared McNeill pcm_setflags(sc->dev, pcm_getflags(sc->dev) | SD_F_SOFTPCMVOL); 51516025c35SJared McNeill 51616025c35SJared McNeill /* Right & Left LINEOUT enable */ 51716025c35SJared McNeill h3_pr_set_clear(sc, H3_PAEN_CTR, H3_LINEOUTEN, 0); 51816025c35SJared McNeill h3_pr_set_clear(sc, H3_MIC2G_LINEOUT_CTR, 51916025c35SJared McNeill H3_LINEOUT_LSEL | H3_LINEOUT_RSEL, 0); 52016025c35SJared McNeill 52116025c35SJared McNeill return (0); 52216025c35SJared McNeill } 52316025c35SJared McNeill 52416025c35SJared McNeill static const struct h3_mixer { 52516025c35SJared McNeill unsigned reg; 52616025c35SJared McNeill unsigned mask; 52716025c35SJared McNeill unsigned shift; 52816025c35SJared McNeill } h3_mixers[SOUND_MIXER_NRDEVICES] = { 52916025c35SJared McNeill [SOUND_MIXER_VOLUME] = { H3_LINEOUT_VOLC, H3_LINEOUTVOL_MASK, 53016025c35SJared McNeill H3_LINEOUTVOL_SHIFT }, 53116025c35SJared McNeill [SOUND_MIXER_RECLEV] = { H3_ADC_AP_EN, H3_ADCG_MASK, 53216025c35SJared McNeill H3_ADCG_SHIFT }, 53316025c35SJared McNeill [SOUND_MIXER_LINE] = { H3_LINEIN_GCTR, H3_LINEING_MASK, 53416025c35SJared McNeill H3_LINEING_SHIFT }, 53516025c35SJared McNeill [SOUND_MIXER_MIC] = { H3_MIC_GCTR, H3_MIC1_GAIN_MASK, 53616025c35SJared McNeill H3_MIC1_GAIN_SHIFT }, 53716025c35SJared McNeill [SOUND_MIXER_LINE1] = { H3_MIC_GCTR, H3_MIC2_GAIN_MASK, 53816025c35SJared McNeill H3_MIC2_GAIN_SHIFT }, 53916025c35SJared McNeill }; 54016025c35SJared McNeill 54116025c35SJared McNeill static int 54216025c35SJared McNeill h3_mixer_set(struct snd_mixer *m, unsigned dev, unsigned left, 54316025c35SJared McNeill unsigned right) 54416025c35SJared McNeill { 54516025c35SJared McNeill struct a10codec_info *sc = mix_getdevinfo(m); 54616025c35SJared McNeill unsigned nvol, max; 54716025c35SJared McNeill 54816025c35SJared McNeill max = h3_mixers[dev].mask >> h3_mixers[dev].shift; 54916025c35SJared McNeill nvol = (left * max) / 100; 55016025c35SJared McNeill 55116025c35SJared McNeill h3_pr_set_clear(sc, h3_mixers[dev].reg, 55216025c35SJared McNeill nvol << h3_mixers[dev].shift, h3_mixers[dev].mask); 55316025c35SJared McNeill 55416025c35SJared McNeill left = right = (left * 100) / max; 55516025c35SJared McNeill return (left | (right << 8)); 55616025c35SJared McNeill } 55716025c35SJared McNeill 55816025c35SJared McNeill static uint32_t 55916025c35SJared McNeill h3_mixer_setrecsrc(struct snd_mixer *m, uint32_t src) 56016025c35SJared McNeill { 56116025c35SJared McNeill struct a10codec_info *sc = mix_getdevinfo(m); 56216025c35SJared McNeill uint32_t val; 56316025c35SJared McNeill 56416025c35SJared McNeill val = 0; 56516025c35SJared McNeill src &= (SOUND_MASK_LINE | SOUND_MASK_MIC | 56616025c35SJared McNeill SOUND_MASK_LINE1 | SOUND_MASK_IMIX); 56716025c35SJared McNeill 56816025c35SJared McNeill if ((src & SOUND_MASK_LINE) != 0) /* line-in */ 56916025c35SJared McNeill val |= H3_ADCMIXSC_LINEIN; 57016025c35SJared McNeill if ((src & SOUND_MASK_MIC) != 0) /* MIC1 */ 57116025c35SJared McNeill val |= H3_ADCMIXSC_MIC1; 57216025c35SJared McNeill if ((src & SOUND_MASK_LINE1) != 0) /* MIC2 */ 57316025c35SJared McNeill val |= H3_ADCMIXSC_MIC2; 57416025c35SJared McNeill if ((src & SOUND_MASK_IMIX) != 0) /* l/r output mixer */ 57516025c35SJared McNeill val |= H3_ADCMIXSC_OMIXER; 57616025c35SJared McNeill 57716025c35SJared McNeill h3_pr_write(sc, H3_LADCMIXSC, val); 57816025c35SJared McNeill h3_pr_write(sc, H3_RADCMIXSC, val); 57916025c35SJared McNeill 58016025c35SJared McNeill return (src); 58116025c35SJared McNeill } 58216025c35SJared McNeill 58316025c35SJared McNeill static void 58416025c35SJared McNeill h3_mute(struct a10codec_info *sc, int mute, int dir) 58516025c35SJared McNeill { 58616025c35SJared McNeill if (dir == PCMDIR_PLAY) { 58716025c35SJared McNeill if (mute) { 58816025c35SJared McNeill /* Mute DAC l/r channels to output mixer */ 58916025c35SJared McNeill h3_pr_set_clear(sc, H3_LOMIXSC, 0, H3_LOMIXSC_LDAC); 59016025c35SJared McNeill h3_pr_set_clear(sc, H3_ROMIXSC, 0, H3_ROMIXSC_RDAC); 59116025c35SJared McNeill /* Disable DAC analog l/r channels and output mixer */ 59216025c35SJared McNeill h3_pr_set_clear(sc, H3_DAC_PA_SRC, 59316025c35SJared McNeill 0, H3_DACAREN | H3_DACALEN | H3_RMIXEN | H3_LMIXEN); 59416025c35SJared McNeill } else { 59516025c35SJared McNeill /* Enable DAC analog l/r channels and output mixer */ 59616025c35SJared McNeill h3_pr_set_clear(sc, H3_DAC_PA_SRC, 59716025c35SJared McNeill H3_DACAREN | H3_DACALEN | H3_RMIXEN | H3_LMIXEN, 0); 59816025c35SJared McNeill /* Unmute DAC l/r channels to output mixer */ 59916025c35SJared McNeill h3_pr_set_clear(sc, H3_LOMIXSC, H3_LOMIXSC_LDAC, 0); 60016025c35SJared McNeill h3_pr_set_clear(sc, H3_ROMIXSC, H3_ROMIXSC_RDAC, 0); 60116025c35SJared McNeill } 60216025c35SJared McNeill } else { 60316025c35SJared McNeill if (mute) { 60416025c35SJared McNeill /* Disable ADC analog l/r channels */ 60516025c35SJared McNeill h3_pr_set_clear(sc, H3_ADC_AP_EN, 60616025c35SJared McNeill 0, H3_ADCREN | H3_ADCLEN); 60716025c35SJared McNeill } else { 60816025c35SJared McNeill /* Enable ADC analog l/r channels */ 60916025c35SJared McNeill h3_pr_set_clear(sc, H3_ADC_AP_EN, 61016025c35SJared McNeill H3_ADCREN | H3_ADCLEN, 0); 61116025c35SJared McNeill } 61216025c35SJared McNeill } 61316025c35SJared McNeill } 61416025c35SJared McNeill 61516025c35SJared McNeill static kobj_method_t h3_mixer_methods[] = { 61616025c35SJared McNeill KOBJMETHOD(mixer_init, h3_mixer_init), 61716025c35SJared McNeill KOBJMETHOD(mixer_set, h3_mixer_set), 61816025c35SJared McNeill KOBJMETHOD(mixer_setrecsrc, h3_mixer_setrecsrc), 61916025c35SJared McNeill KOBJMETHOD_END 62016025c35SJared McNeill }; 62116025c35SJared McNeill MIXER_DECLARE(h3_mixer); 622ba9b7163SAndrew Turner 623ba9b7163SAndrew Turner 624ba9b7163SAndrew Turner /* 625ba9b7163SAndrew Turner * Channel interface 626ba9b7163SAndrew Turner */ 627ba9b7163SAndrew Turner 628ba9b7163SAndrew Turner static void 629ba9b7163SAndrew Turner a10codec_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 630ba9b7163SAndrew Turner { 631ba9b7163SAndrew Turner struct a10codec_chinfo *ch = arg; 632ba9b7163SAndrew Turner 633ba9b7163SAndrew Turner if (error != 0) 634ba9b7163SAndrew Turner return; 635ba9b7163SAndrew Turner 636ba9b7163SAndrew Turner ch->physaddr = segs[0].ds_addr; 637ba9b7163SAndrew Turner } 638ba9b7163SAndrew Turner 639ba9b7163SAndrew Turner static void 640ba9b7163SAndrew Turner a10codec_transfer(struct a10codec_chinfo *ch) 641ba9b7163SAndrew Turner { 642ba9b7163SAndrew Turner bus_addr_t src, dst; 643ba9b7163SAndrew Turner int error; 644ba9b7163SAndrew Turner 645ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) { 646ba9b7163SAndrew Turner src = ch->physaddr + ch->pos; 647ba9b7163SAndrew Turner dst = ch->fifo; 648ba9b7163SAndrew Turner } else { 649ba9b7163SAndrew Turner src = ch->fifo; 650ba9b7163SAndrew Turner dst = ch->physaddr + ch->pos; 651ba9b7163SAndrew Turner } 652ba9b7163SAndrew Turner 653ba9b7163SAndrew Turner error = SUNXI_DMA_TRANSFER(ch->dmac, ch->dmachan, src, dst, 654ba9b7163SAndrew Turner ch->blocksize); 655ba9b7163SAndrew Turner if (error) { 656ba9b7163SAndrew Turner ch->run = 0; 657ba9b7163SAndrew Turner device_printf(ch->parent->dev, "DMA transfer failed: %d\n", 658ba9b7163SAndrew Turner error); 659ba9b7163SAndrew Turner } 660ba9b7163SAndrew Turner } 661ba9b7163SAndrew Turner 662ba9b7163SAndrew Turner static void 663ba9b7163SAndrew Turner a10codec_dmaconfig(struct a10codec_chinfo *ch) 664ba9b7163SAndrew Turner { 665ba9b7163SAndrew Turner struct a10codec_info *sc = ch->parent; 666ba9b7163SAndrew Turner struct sunxi_dma_config conf; 667ba9b7163SAndrew Turner 668ba9b7163SAndrew Turner memset(&conf, 0, sizeof(conf)); 669ba9b7163SAndrew Turner conf.src_width = conf.dst_width = 16; 670ba9b7163SAndrew Turner conf.src_burst_len = conf.dst_burst_len = 4; 671ba9b7163SAndrew Turner 672ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) { 673ba9b7163SAndrew Turner conf.dst_noincr = true; 67416025c35SJared McNeill conf.src_drqtype = sc->cfg->drqtype_sdram; 67516025c35SJared McNeill conf.dst_drqtype = sc->cfg->drqtype_codec; 676ba9b7163SAndrew Turner } else { 677ba9b7163SAndrew Turner conf.src_noincr = true; 67816025c35SJared McNeill conf.src_drqtype = sc->cfg->drqtype_codec; 67916025c35SJared McNeill conf.dst_drqtype = sc->cfg->drqtype_sdram; 680ba9b7163SAndrew Turner } 681ba9b7163SAndrew Turner 682ba9b7163SAndrew Turner SUNXI_DMA_SET_CONFIG(ch->dmac, ch->dmachan, &conf); 683ba9b7163SAndrew Turner } 684ba9b7163SAndrew Turner 685ba9b7163SAndrew Turner static void 686ba9b7163SAndrew Turner a10codec_dmaintr(void *priv) 687ba9b7163SAndrew Turner { 688ba9b7163SAndrew Turner struct a10codec_chinfo *ch = priv; 689ba9b7163SAndrew Turner unsigned bufsize; 690ba9b7163SAndrew Turner 691ba9b7163SAndrew Turner bufsize = sndbuf_getsize(ch->buffer); 692ba9b7163SAndrew Turner 693ba9b7163SAndrew Turner ch->pos += ch->blocksize; 694ba9b7163SAndrew Turner if (ch->pos >= bufsize) 695ba9b7163SAndrew Turner ch->pos -= bufsize; 696ba9b7163SAndrew Turner 697ba9b7163SAndrew Turner if (ch->run) { 698ba9b7163SAndrew Turner chn_intr(ch->channel); 699ba9b7163SAndrew Turner a10codec_transfer(ch); 700ba9b7163SAndrew Turner } 701ba9b7163SAndrew Turner } 702ba9b7163SAndrew Turner 703ba9b7163SAndrew Turner static unsigned 704ba9b7163SAndrew Turner a10codec_fs(struct a10codec_chinfo *ch) 705ba9b7163SAndrew Turner { 706ba9b7163SAndrew Turner switch (ch->speed) { 707ba9b7163SAndrew Turner case 48000: 708ba9b7163SAndrew Turner return (DAC_FS_48KHZ); 709ba9b7163SAndrew Turner case 24000: 710ba9b7163SAndrew Turner return (DAC_FS_24KHZ); 711ba9b7163SAndrew Turner case 12000: 712ba9b7163SAndrew Turner return (DAC_FS_12KHZ); 713ba9b7163SAndrew Turner case 192000: 714ba9b7163SAndrew Turner return (DAC_FS_192KHZ); 715ba9b7163SAndrew Turner case 32000: 716ba9b7163SAndrew Turner return (DAC_FS_32KHZ); 717ba9b7163SAndrew Turner case 16000: 718ba9b7163SAndrew Turner return (DAC_FS_16KHZ); 719ba9b7163SAndrew Turner case 8000: 720ba9b7163SAndrew Turner return (DAC_FS_8KHZ); 721ba9b7163SAndrew Turner case 96000: 722ba9b7163SAndrew Turner return (DAC_FS_96KHZ); 723ba9b7163SAndrew Turner default: 724ba9b7163SAndrew Turner return (DAC_FS_48KHZ); 725ba9b7163SAndrew Turner } 726ba9b7163SAndrew Turner } 727ba9b7163SAndrew Turner 728ba9b7163SAndrew Turner static void 729ba9b7163SAndrew Turner a10codec_start(struct a10codec_chinfo *ch) 730ba9b7163SAndrew Turner { 731ba9b7163SAndrew Turner struct a10codec_info *sc = ch->parent; 732ba9b7163SAndrew Turner uint32_t val; 733ba9b7163SAndrew Turner 734ba9b7163SAndrew Turner ch->pos = 0; 735ba9b7163SAndrew Turner 736ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) { 737ba9b7163SAndrew Turner /* Flush DAC FIFO */ 73816025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), DAC_FIFOC_FIFO_FLUSH); 739ba9b7163SAndrew Turner 740ba9b7163SAndrew Turner /* Clear DAC FIFO status */ 74116025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_FIFOS(sc), 74216025c35SJared McNeill CODEC_READ(sc, AC_DAC_FIFOS(sc))); 743ba9b7163SAndrew Turner 74416025c35SJared McNeill /* Unmute output */ 74516025c35SJared McNeill sc->cfg->mute(sc, 0, ch->dir); 746ba9b7163SAndrew Turner 747ba9b7163SAndrew Turner /* Configure DAC DMA channel */ 748ba9b7163SAndrew Turner a10codec_dmaconfig(ch); 749ba9b7163SAndrew Turner 750ba9b7163SAndrew Turner /* Configure DAC FIFO */ 75116025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), 752ba9b7163SAndrew Turner (AFMT_CHANNEL(ch->format) == 1 ? DAC_FIFOC_MONO_EN : 0) | 753ba9b7163SAndrew Turner (a10codec_fs(ch) << DAC_FIFOC_FS_SHIFT) | 754ba9b7163SAndrew Turner (FIFO_MODE_16_15_0 << DAC_FIFOC_FIFO_MODE_SHIFT) | 755ba9b7163SAndrew Turner (DRQ_CLR_CNT << DAC_FIFOC_DRQ_CLR_CNT_SHIFT) | 756ba9b7163SAndrew Turner (TX_TRIG_LEVEL << DAC_FIFOC_TX_TRIG_LEVEL_SHIFT)); 757ba9b7163SAndrew Turner 758ba9b7163SAndrew Turner /* Enable DAC DRQ */ 75916025c35SJared McNeill val = CODEC_READ(sc, AC_DAC_FIFOC(sc)); 760ba9b7163SAndrew Turner val |= DAC_FIFOC_DRQ_EN; 76116025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val); 762ba9b7163SAndrew Turner } else { 763ba9b7163SAndrew Turner /* Flush ADC FIFO */ 76416025c35SJared McNeill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), ADC_FIFOC_FIFO_FLUSH); 765ba9b7163SAndrew Turner 766ba9b7163SAndrew Turner /* Clear ADC FIFO status */ 76716025c35SJared McNeill CODEC_WRITE(sc, AC_ADC_FIFOS(sc), 76816025c35SJared McNeill CODEC_READ(sc, AC_ADC_FIFOS(sc))); 769ba9b7163SAndrew Turner 77016025c35SJared McNeill /* Unmute input */ 77116025c35SJared McNeill sc->cfg->mute(sc, 0, ch->dir); 772ba9b7163SAndrew Turner 773ba9b7163SAndrew Turner /* Configure ADC DMA channel */ 774ba9b7163SAndrew Turner a10codec_dmaconfig(ch); 775ba9b7163SAndrew Turner 776ba9b7163SAndrew Turner /* Configure ADC FIFO */ 77716025c35SJared McNeill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), 778ba9b7163SAndrew Turner ADC_FIFOC_EN_AD | 779ba9b7163SAndrew Turner ADC_FIFOC_RX_FIFO_MODE | 780ba9b7163SAndrew Turner (AFMT_CHANNEL(ch->format) == 1 ? ADC_FIFOC_MONO_EN : 0) | 781ba9b7163SAndrew Turner (a10codec_fs(ch) << ADC_FIFOC_FS_SHIFT) | 782ba9b7163SAndrew Turner (RX_TRIG_LEVEL << ADC_FIFOC_RX_TRIG_LEVEL_SHIFT)); 783ba9b7163SAndrew Turner 784ba9b7163SAndrew Turner /* Enable ADC DRQ */ 78516025c35SJared McNeill val = CODEC_READ(sc, AC_ADC_FIFOC(sc)); 786ba9b7163SAndrew Turner val |= ADC_FIFOC_DRQ_EN; 78716025c35SJared McNeill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val); 788ba9b7163SAndrew Turner } 789ba9b7163SAndrew Turner 790ba9b7163SAndrew Turner /* Start DMA transfer */ 791ba9b7163SAndrew Turner a10codec_transfer(ch); 792ba9b7163SAndrew Turner } 793ba9b7163SAndrew Turner 794ba9b7163SAndrew Turner static void 795ba9b7163SAndrew Turner a10codec_stop(struct a10codec_chinfo *ch) 796ba9b7163SAndrew Turner { 797ba9b7163SAndrew Turner struct a10codec_info *sc = ch->parent; 798ba9b7163SAndrew Turner 799ba9b7163SAndrew Turner /* Disable DMA channel */ 800ba9b7163SAndrew Turner SUNXI_DMA_HALT(ch->dmac, ch->dmachan); 801ba9b7163SAndrew Turner 80216025c35SJared McNeill sc->cfg->mute(sc, 1, ch->dir); 80316025c35SJared McNeill 804ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) { 805ba9b7163SAndrew Turner /* Disable DAC DRQ */ 80616025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), 0); 807ba9b7163SAndrew Turner } else { 808ba9b7163SAndrew Turner /* Disable ADC DRQ */ 80916025c35SJared McNeill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), 0); 810ba9b7163SAndrew Turner } 811ba9b7163SAndrew Turner } 812ba9b7163SAndrew Turner 813ba9b7163SAndrew Turner static void * 814ba9b7163SAndrew Turner a10codec_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, 815ba9b7163SAndrew Turner struct pcm_channel *c, int dir) 816ba9b7163SAndrew Turner { 817ba9b7163SAndrew Turner struct a10codec_info *sc = devinfo; 818ba9b7163SAndrew Turner struct a10codec_chinfo *ch = dir == PCMDIR_PLAY ? &sc->play : &sc->rec; 81916025c35SJared McNeill phandle_t xref; 82016025c35SJared McNeill pcell_t *cells; 82116025c35SJared McNeill int ncells, error; 82216025c35SJared McNeill 82316025c35SJared McNeill error = ofw_bus_parse_xref_list_alloc(ofw_bus_get_node(sc->dev), 82416025c35SJared McNeill "dmas", "#dma-cells", dir == PCMDIR_PLAY ? 1 : 0, 82516025c35SJared McNeill &xref, &ncells, &cells); 82616025c35SJared McNeill if (error != 0) { 82716025c35SJared McNeill device_printf(sc->dev, "cannot parse 'dmas' property\n"); 82816025c35SJared McNeill return (NULL); 82916025c35SJared McNeill } 83016025c35SJared McNeill OF_prop_free(cells); 831ba9b7163SAndrew Turner 832ba9b7163SAndrew Turner ch->parent = sc; 833ba9b7163SAndrew Turner ch->channel = c; 834ba9b7163SAndrew Turner ch->buffer = b; 835ba9b7163SAndrew Turner ch->dir = dir; 836ba9b7163SAndrew Turner ch->fifo = rman_get_start(sc->res[0]) + 83716025c35SJared McNeill (dir == PCMDIR_REC ? AC_ADC_RXDATA(sc) : AC_DAC_TXDATA(sc)); 838ba9b7163SAndrew Turner 83916025c35SJared McNeill ch->dmac = OF_device_from_xref(xref); 840ba9b7163SAndrew Turner if (ch->dmac == NULL) { 841ba9b7163SAndrew Turner device_printf(sc->dev, "cannot find DMA controller\n"); 84216025c35SJared McNeill device_printf(sc->dev, "xref = 0x%x\n", (u_int)xref); 843ba9b7163SAndrew Turner return (NULL); 844ba9b7163SAndrew Turner } 845ba9b7163SAndrew Turner ch->dmachan = SUNXI_DMA_ALLOC(ch->dmac, false, a10codec_dmaintr, ch); 846ba9b7163SAndrew Turner if (ch->dmachan == NULL) { 847ba9b7163SAndrew Turner device_printf(sc->dev, "cannot allocate DMA channel\n"); 848ba9b7163SAndrew Turner return (NULL); 849ba9b7163SAndrew Turner } 850ba9b7163SAndrew Turner 851ba9b7163SAndrew Turner error = bus_dmamem_alloc(sc->dmat, &ch->dmaaddr, 852ba9b7163SAndrew Turner BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &ch->dmamap); 853ba9b7163SAndrew Turner if (error != 0) { 854ba9b7163SAndrew Turner device_printf(sc->dev, "cannot allocate channel buffer\n"); 855ba9b7163SAndrew Turner return (NULL); 856ba9b7163SAndrew Turner } 857ba9b7163SAndrew Turner error = bus_dmamap_load(sc->dmat, ch->dmamap, ch->dmaaddr, 858ba9b7163SAndrew Turner sc->dmasize, a10codec_dmamap_cb, ch, BUS_DMA_NOWAIT); 859ba9b7163SAndrew Turner if (error != 0) { 860ba9b7163SAndrew Turner device_printf(sc->dev, "cannot load DMA map\n"); 861ba9b7163SAndrew Turner return (NULL); 862ba9b7163SAndrew Turner } 863ba9b7163SAndrew Turner memset(ch->dmaaddr, 0, sc->dmasize); 864ba9b7163SAndrew Turner 865ba9b7163SAndrew Turner if (sndbuf_setup(ch->buffer, ch->dmaaddr, sc->dmasize) != 0) { 866ba9b7163SAndrew Turner device_printf(sc->dev, "cannot setup sndbuf\n"); 867ba9b7163SAndrew Turner return (NULL); 868ba9b7163SAndrew Turner } 869ba9b7163SAndrew Turner 870ba9b7163SAndrew Turner return (ch); 871ba9b7163SAndrew Turner } 872ba9b7163SAndrew Turner 873ba9b7163SAndrew Turner static int 874ba9b7163SAndrew Turner a10codec_chan_free(kobj_t obj, void *data) 875ba9b7163SAndrew Turner { 876ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data; 877ba9b7163SAndrew Turner struct a10codec_info *sc = ch->parent; 878ba9b7163SAndrew Turner 879ba9b7163SAndrew Turner SUNXI_DMA_FREE(ch->dmac, ch->dmachan); 880ba9b7163SAndrew Turner bus_dmamap_unload(sc->dmat, ch->dmamap); 881ba9b7163SAndrew Turner bus_dmamem_free(sc->dmat, ch->dmaaddr, ch->dmamap); 882ba9b7163SAndrew Turner 883ba9b7163SAndrew Turner return (0); 884ba9b7163SAndrew Turner } 885ba9b7163SAndrew Turner 886ba9b7163SAndrew Turner static int 887ba9b7163SAndrew Turner a10codec_chan_setformat(kobj_t obj, void *data, uint32_t format) 888ba9b7163SAndrew Turner { 889ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data; 890ba9b7163SAndrew Turner 891ba9b7163SAndrew Turner ch->format = format; 892ba9b7163SAndrew Turner 893ba9b7163SAndrew Turner return (0); 894ba9b7163SAndrew Turner } 895ba9b7163SAndrew Turner 896ba9b7163SAndrew Turner static uint32_t 897ba9b7163SAndrew Turner a10codec_chan_setspeed(kobj_t obj, void *data, uint32_t speed) 898ba9b7163SAndrew Turner { 899ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data; 900ba9b7163SAndrew Turner 901ba9b7163SAndrew Turner /* 902ba9b7163SAndrew Turner * The codec supports full duplex operation but both DAC and ADC 903ba9b7163SAndrew Turner * use the same source clock (PLL2). Limit the available speeds to 904ba9b7163SAndrew Turner * those supported by a 24576000 Hz input. 905ba9b7163SAndrew Turner */ 906ba9b7163SAndrew Turner switch (speed) { 907ba9b7163SAndrew Turner case 8000: 908ba9b7163SAndrew Turner case 12000: 909ba9b7163SAndrew Turner case 16000: 910ba9b7163SAndrew Turner case 24000: 911ba9b7163SAndrew Turner case 32000: 912ba9b7163SAndrew Turner case 48000: 913ba9b7163SAndrew Turner ch->speed = speed; 914ba9b7163SAndrew Turner break; 915ba9b7163SAndrew Turner case 96000: 916ba9b7163SAndrew Turner case 192000: 917ba9b7163SAndrew Turner /* 96 KHz / 192 KHz mode only supported for playback */ 918ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) { 919ba9b7163SAndrew Turner ch->speed = speed; 920ba9b7163SAndrew Turner } else { 921ba9b7163SAndrew Turner ch->speed = 48000; 922ba9b7163SAndrew Turner } 923ba9b7163SAndrew Turner break; 924ba9b7163SAndrew Turner case 44100: 925ba9b7163SAndrew Turner ch->speed = 48000; 926ba9b7163SAndrew Turner break; 927ba9b7163SAndrew Turner case 22050: 928ba9b7163SAndrew Turner ch->speed = 24000; 929ba9b7163SAndrew Turner break; 930ba9b7163SAndrew Turner case 11025: 931ba9b7163SAndrew Turner ch->speed = 12000; 932ba9b7163SAndrew Turner break; 933ba9b7163SAndrew Turner default: 934ba9b7163SAndrew Turner ch->speed = 48000; 935ba9b7163SAndrew Turner break; 936ba9b7163SAndrew Turner } 937ba9b7163SAndrew Turner 938ba9b7163SAndrew Turner return (ch->speed); 939ba9b7163SAndrew Turner } 940ba9b7163SAndrew Turner 941ba9b7163SAndrew Turner static uint32_t 942ba9b7163SAndrew Turner a10codec_chan_setblocksize(kobj_t obj, void *data, uint32_t blocksize) 943ba9b7163SAndrew Turner { 944ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data; 945ba9b7163SAndrew Turner 946ba9b7163SAndrew Turner ch->blocksize = blocksize & ~3; 947ba9b7163SAndrew Turner 948ba9b7163SAndrew Turner return (ch->blocksize); 949ba9b7163SAndrew Turner } 950ba9b7163SAndrew Turner 951ba9b7163SAndrew Turner static int 952ba9b7163SAndrew Turner a10codec_chan_trigger(kobj_t obj, void *data, int go) 953ba9b7163SAndrew Turner { 954ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data; 955ba9b7163SAndrew Turner struct a10codec_info *sc = ch->parent; 956ba9b7163SAndrew Turner 957ba9b7163SAndrew Turner if (!PCMTRIG_COMMON(go)) 958ba9b7163SAndrew Turner return (0); 959ba9b7163SAndrew Turner 960ba9b7163SAndrew Turner snd_mtxlock(sc->lock); 961ba9b7163SAndrew Turner switch (go) { 962ba9b7163SAndrew Turner case PCMTRIG_START: 963ba9b7163SAndrew Turner ch->run = 1; 964*bfcf888aSEmmanuel Vadot a10codec_stop(ch); 965ba9b7163SAndrew Turner a10codec_start(ch); 966ba9b7163SAndrew Turner break; 967ba9b7163SAndrew Turner case PCMTRIG_STOP: 968ba9b7163SAndrew Turner case PCMTRIG_ABORT: 969ba9b7163SAndrew Turner ch->run = 0; 970ba9b7163SAndrew Turner a10codec_stop(ch); 971ba9b7163SAndrew Turner break; 972ba9b7163SAndrew Turner default: 973ba9b7163SAndrew Turner break; 974ba9b7163SAndrew Turner } 975ba9b7163SAndrew Turner snd_mtxunlock(sc->lock); 976ba9b7163SAndrew Turner 977ba9b7163SAndrew Turner return (0); 978ba9b7163SAndrew Turner } 979ba9b7163SAndrew Turner 980ba9b7163SAndrew Turner static uint32_t 981ba9b7163SAndrew Turner a10codec_chan_getptr(kobj_t obj, void *data) 982ba9b7163SAndrew Turner { 983ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data; 984ba9b7163SAndrew Turner 985ba9b7163SAndrew Turner return (ch->pos); 986ba9b7163SAndrew Turner } 987ba9b7163SAndrew Turner 988ba9b7163SAndrew Turner static struct pcmchan_caps * 989ba9b7163SAndrew Turner a10codec_chan_getcaps(kobj_t obj, void *data) 990ba9b7163SAndrew Turner { 991ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data; 992ba9b7163SAndrew Turner 993ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) { 994ba9b7163SAndrew Turner return (&a10codec_pcaps); 995ba9b7163SAndrew Turner } else { 996ba9b7163SAndrew Turner return (&a10codec_rcaps); 997ba9b7163SAndrew Turner } 998ba9b7163SAndrew Turner } 999ba9b7163SAndrew Turner 1000ba9b7163SAndrew Turner static kobj_method_t a10codec_chan_methods[] = { 1001ba9b7163SAndrew Turner KOBJMETHOD(channel_init, a10codec_chan_init), 1002ba9b7163SAndrew Turner KOBJMETHOD(channel_free, a10codec_chan_free), 1003ba9b7163SAndrew Turner KOBJMETHOD(channel_setformat, a10codec_chan_setformat), 1004ba9b7163SAndrew Turner KOBJMETHOD(channel_setspeed, a10codec_chan_setspeed), 1005ba9b7163SAndrew Turner KOBJMETHOD(channel_setblocksize, a10codec_chan_setblocksize), 1006ba9b7163SAndrew Turner KOBJMETHOD(channel_trigger, a10codec_chan_trigger), 1007ba9b7163SAndrew Turner KOBJMETHOD(channel_getptr, a10codec_chan_getptr), 1008ba9b7163SAndrew Turner KOBJMETHOD(channel_getcaps, a10codec_chan_getcaps), 1009ba9b7163SAndrew Turner KOBJMETHOD_END 1010ba9b7163SAndrew Turner }; 1011ba9b7163SAndrew Turner CHANNEL_DECLARE(a10codec_chan); 1012ba9b7163SAndrew Turner 1013ba9b7163SAndrew Turner 1014ba9b7163SAndrew Turner /* 1015ba9b7163SAndrew Turner * Device interface 1016ba9b7163SAndrew Turner */ 1017ba9b7163SAndrew Turner 101816025c35SJared McNeill static const struct a10codec_config a10_config = { 101916025c35SJared McNeill .mixer_class = &a10_mixer_class, 102016025c35SJared McNeill .mute = a10_mute, 102116025c35SJared McNeill .drqtype_codec = 19, 102216025c35SJared McNeill .drqtype_sdram = 22, 102316025c35SJared McNeill .DPC = 0x00, 102416025c35SJared McNeill .DAC_FIFOC = 0x04, 102516025c35SJared McNeill .DAC_FIFOS = 0x08, 102616025c35SJared McNeill .DAC_TXDATA = 0x0c, 102716025c35SJared McNeill .ADC_FIFOC = 0x1c, 102816025c35SJared McNeill .ADC_FIFOS = 0x20, 102916025c35SJared McNeill .ADC_RXDATA = 0x24, 103016025c35SJared McNeill .DAC_CNT = 0x30, 103116025c35SJared McNeill .ADC_CNT = 0x34, 103216025c35SJared McNeill }; 103316025c35SJared McNeill 103416025c35SJared McNeill static const struct a10codec_config h3_config = { 103516025c35SJared McNeill .mixer_class = &h3_mixer_class, 103616025c35SJared McNeill .mute = h3_mute, 103716025c35SJared McNeill .drqtype_codec = 15, 103816025c35SJared McNeill .drqtype_sdram = 1, 103916025c35SJared McNeill .DPC = 0x00, 104016025c35SJared McNeill .DAC_FIFOC = 0x04, 104116025c35SJared McNeill .DAC_FIFOS = 0x08, 104216025c35SJared McNeill .DAC_TXDATA = 0x20, 104316025c35SJared McNeill .ADC_FIFOC = 0x10, 104416025c35SJared McNeill .ADC_FIFOS = 0x14, 104516025c35SJared McNeill .ADC_RXDATA = 0x18, 104616025c35SJared McNeill .DAC_CNT = 0x40, 104716025c35SJared McNeill .ADC_CNT = 0x44, 104816025c35SJared McNeill }; 104916025c35SJared McNeill 1050356c50adSEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 105116025c35SJared McNeill { "allwinner,sun4i-a10-codec", (uintptr_t)&a10_config }, 105216025c35SJared McNeill { "allwinner,sun7i-a20-codec", (uintptr_t)&a10_config }, 105316025c35SJared McNeill { "allwinner,sun8i-h3-codec", (uintptr_t)&h3_config }, 105416025c35SJared McNeill { NULL, 0 } 1055356c50adSEmmanuel Vadot }; 1056356c50adSEmmanuel Vadot 1057ba9b7163SAndrew Turner static int 1058ba9b7163SAndrew Turner a10codec_probe(device_t dev) 1059ba9b7163SAndrew Turner { 1060ba9b7163SAndrew Turner if (!ofw_bus_status_okay(dev)) 1061ba9b7163SAndrew Turner return (ENXIO); 1062ba9b7163SAndrew Turner 1063356c50adSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 1064ba9b7163SAndrew Turner return (ENXIO); 1065ba9b7163SAndrew Turner 1066ba9b7163SAndrew Turner device_set_desc(dev, "Allwinner Audio Codec"); 1067ba9b7163SAndrew Turner return (BUS_PROBE_DEFAULT); 1068ba9b7163SAndrew Turner } 1069ba9b7163SAndrew Turner 1070ba9b7163SAndrew Turner static int 1071ba9b7163SAndrew Turner a10codec_attach(device_t dev) 1072ba9b7163SAndrew Turner { 1073ba9b7163SAndrew Turner struct a10codec_info *sc; 1074ba9b7163SAndrew Turner char status[SND_STATUSLEN]; 107516025c35SJared McNeill struct gpiobus_pin *pa_pin; 107616025c35SJared McNeill phandle_t node; 107716025c35SJared McNeill clk_t clk_bus, clk_codec; 107816025c35SJared McNeill hwreset_t rst; 1079ba9b7163SAndrew Turner uint32_t val; 1080ba9b7163SAndrew Turner int error; 1081ba9b7163SAndrew Turner 108216025c35SJared McNeill node = ofw_bus_get_node(dev); 108316025c35SJared McNeill 1084ba9b7163SAndrew Turner sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO); 108516025c35SJared McNeill sc->cfg = (void *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 1086ba9b7163SAndrew Turner sc->dev = dev; 1087ba9b7163SAndrew Turner sc->lock = snd_mtxcreate(device_get_nameunit(dev), "a10codec softc"); 1088ba9b7163SAndrew Turner 1089ba9b7163SAndrew Turner if (bus_alloc_resources(dev, a10codec_spec, sc->res)) { 1090ba9b7163SAndrew Turner device_printf(dev, "cannot allocate resources for device\n"); 1091ba9b7163SAndrew Turner error = ENXIO; 1092ba9b7163SAndrew Turner goto fail; 1093ba9b7163SAndrew Turner } 1094ba9b7163SAndrew Turner 1095ba9b7163SAndrew Turner sc->dmasize = 131072; 1096ba9b7163SAndrew Turner error = bus_dma_tag_create( 1097ba9b7163SAndrew Turner bus_get_dma_tag(dev), 1098ba9b7163SAndrew Turner 4, sc->dmasize, /* alignment, boundary */ 1099ba9b7163SAndrew Turner BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1100ba9b7163SAndrew Turner BUS_SPACE_MAXADDR, /* highaddr */ 1101ba9b7163SAndrew Turner NULL, NULL, /* filter, filterarg */ 1102ba9b7163SAndrew Turner sc->dmasize, 1, /* maxsize, nsegs */ 1103ba9b7163SAndrew Turner sc->dmasize, 0, /* maxsegsize, flags */ 1104ba9b7163SAndrew Turner NULL, NULL, /* lockfunc, lockarg */ 1105ba9b7163SAndrew Turner &sc->dmat); 1106ba9b7163SAndrew Turner if (error != 0) { 1107ba9b7163SAndrew Turner device_printf(dev, "cannot create DMA tag\n"); 1108ba9b7163SAndrew Turner goto fail; 1109ba9b7163SAndrew Turner } 1110ba9b7163SAndrew Turner 11116a05f063SJared McNeill /* Get clocks */ 111216025c35SJared McNeill if (clk_get_by_ofw_name(dev, 0, "apb", &clk_bus) != 0 && 111316025c35SJared McNeill clk_get_by_ofw_name(dev, 0, "ahb", &clk_bus) != 0) { 111416025c35SJared McNeill device_printf(dev, "cannot find bus clock\n"); 11156a05f063SJared McNeill goto fail; 11166a05f063SJared McNeill } 111716025c35SJared McNeill if (clk_get_by_ofw_name(dev, 0, "codec", &clk_codec) != 0) { 11186a05f063SJared McNeill device_printf(dev, "cannot find codec clock\n"); 11196a05f063SJared McNeill goto fail; 11206a05f063SJared McNeill } 11216a05f063SJared McNeill 112216025c35SJared McNeill /* Gating bus clock for codec */ 112316025c35SJared McNeill if (clk_enable(clk_bus) != 0) { 112416025c35SJared McNeill device_printf(dev, "cannot enable bus clock\n"); 11256a05f063SJared McNeill goto fail; 11266a05f063SJared McNeill } 1127ba9b7163SAndrew Turner /* Activate audio codec clock. According to the A10 and A20 user 1128ba9b7163SAndrew Turner * manuals, Audio_pll can be either 24.576MHz or 22.5792MHz. Most 1129ba9b7163SAndrew Turner * audio sampling rates require an 24.576MHz input clock with the 1130ba9b7163SAndrew Turner * exception of 44.1kHz, 22.05kHz, and 11.025kHz. Unfortunately, 1131ba9b7163SAndrew Turner * both capture and playback use the same clock source so to 1132ba9b7163SAndrew Turner * safely support independent full duplex operation, we use a fixed 1133ba9b7163SAndrew Turner * 24.576MHz clock source and don't advertise native support for 1134ba9b7163SAndrew Turner * the three sampling rates that require a 22.5792MHz input. 1135ba9b7163SAndrew Turner */ 11366a05f063SJared McNeill error = clk_set_freq(clk_codec, 24576000, CLK_SET_ROUND_DOWN); 11376a05f063SJared McNeill if (error != 0) { 11386a05f063SJared McNeill device_printf(dev, "cannot set codec clock frequency\n"); 11396a05f063SJared McNeill goto fail; 11406a05f063SJared McNeill } 11416a05f063SJared McNeill /* Enable audio codec clock */ 11426a05f063SJared McNeill error = clk_enable(clk_codec); 11436a05f063SJared McNeill if (error != 0) { 11446a05f063SJared McNeill device_printf(dev, "cannot enable codec clock\n"); 11456a05f063SJared McNeill goto fail; 11466a05f063SJared McNeill } 1147ba9b7163SAndrew Turner 114816025c35SJared McNeill /* De-assert hwreset */ 1149*bfcf888aSEmmanuel Vadot if (hwreset_get_by_ofw_idx(dev, 0, 0, &rst) == 0) { 115016025c35SJared McNeill error = hwreset_deassert(rst); 115116025c35SJared McNeill if (error != 0) { 115216025c35SJared McNeill device_printf(dev, "cannot de-assert reset\n"); 115316025c35SJared McNeill goto fail; 115416025c35SJared McNeill } 115516025c35SJared McNeill } 115616025c35SJared McNeill 1157ba9b7163SAndrew Turner /* Enable DAC */ 115816025c35SJared McNeill val = CODEC_READ(sc, AC_DAC_DPC(sc)); 1159ba9b7163SAndrew Turner val |= DAC_DPC_EN_DA; 116016025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_DPC(sc), val); 1161ba9b7163SAndrew Turner 116216025c35SJared McNeill if (mixer_init(dev, sc->cfg->mixer_class, sc)) { 1163ba9b7163SAndrew Turner device_printf(dev, "mixer_init failed\n"); 1164ba9b7163SAndrew Turner goto fail; 1165ba9b7163SAndrew Turner } 1166ba9b7163SAndrew Turner 116716025c35SJared McNeill /* Unmute PA */ 116816025c35SJared McNeill if (gpio_pin_get_by_ofw_property(dev, node, "allwinner,pa-gpios", 116916025c35SJared McNeill &pa_pin) == 0) { 117016025c35SJared McNeill error = gpio_pin_set_active(pa_pin, 1); 117116025c35SJared McNeill if (error != 0) 117216025c35SJared McNeill device_printf(dev, "failed to unmute PA\n"); 117316025c35SJared McNeill } 117416025c35SJared McNeill 1175ba9b7163SAndrew Turner pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE); 1176ba9b7163SAndrew Turner 1177ba9b7163SAndrew Turner if (pcm_register(dev, sc, 1, 1)) { 1178ba9b7163SAndrew Turner device_printf(dev, "pcm_register failed\n"); 1179ba9b7163SAndrew Turner goto fail; 1180ba9b7163SAndrew Turner } 1181ba9b7163SAndrew Turner 1182ba9b7163SAndrew Turner pcm_addchan(dev, PCMDIR_PLAY, &a10codec_chan_class, sc); 1183ba9b7163SAndrew Turner pcm_addchan(dev, PCMDIR_REC, &a10codec_chan_class, sc); 1184ba9b7163SAndrew Turner 1185ba9b7163SAndrew Turner snprintf(status, SND_STATUSLEN, "at %s", ofw_bus_get_name(dev)); 1186ba9b7163SAndrew Turner pcm_setstatus(dev, status); 1187ba9b7163SAndrew Turner 1188ba9b7163SAndrew Turner return (0); 1189ba9b7163SAndrew Turner 1190ba9b7163SAndrew Turner fail: 1191ba9b7163SAndrew Turner bus_release_resources(dev, a10codec_spec, sc->res); 1192ba9b7163SAndrew Turner snd_mtxfree(sc->lock); 1193ba9b7163SAndrew Turner free(sc, M_DEVBUF); 1194ba9b7163SAndrew Turner 119516025c35SJared McNeill return (ENXIO); 1196ba9b7163SAndrew Turner } 1197ba9b7163SAndrew Turner 1198ba9b7163SAndrew Turner static device_method_t a10codec_pcm_methods[] = { 1199ba9b7163SAndrew Turner /* Device interface */ 1200ba9b7163SAndrew Turner DEVMETHOD(device_probe, a10codec_probe), 1201ba9b7163SAndrew Turner DEVMETHOD(device_attach, a10codec_attach), 1202ba9b7163SAndrew Turner 1203ba9b7163SAndrew Turner DEVMETHOD_END 1204ba9b7163SAndrew Turner }; 1205ba9b7163SAndrew Turner 1206ba9b7163SAndrew Turner static driver_t a10codec_pcm_driver = { 1207ba9b7163SAndrew Turner "pcm", 1208ba9b7163SAndrew Turner a10codec_pcm_methods, 1209ba9b7163SAndrew Turner PCM_SOFTC_SIZE, 1210ba9b7163SAndrew Turner }; 1211ba9b7163SAndrew Turner 1212ba9b7163SAndrew Turner DRIVER_MODULE(a10codec, simplebus, a10codec_pcm_driver, pcm_devclass, 0, 0); 1213ba9b7163SAndrew Turner MODULE_DEPEND(a10codec, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 1214ba9b7163SAndrew Turner MODULE_VERSION(a10codec, 1); 1215