1 /* $NetBSD: sun4i_a10_codec.c,v 1.1 2017/08/27 16:05:26 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2014-2017 Jared McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: sun4i_a10_codec.c,v 1.1 2017/08/27 16:05:26 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/cpu.h> 35 #include <sys/device.h> 36 #include <sys/kmem.h> 37 #include <sys/bitops.h> 38 39 #include <sys/audioio.h> 40 #include <dev/audio_if.h> 41 42 #include <arm/sunxi/sunxi_codec.h> 43 44 #define A10_DEFAULT_PAVOL 0x20 45 46 #define A10_DAC_ACTRL 0x10 47 #define A10_DACAREN __BIT(31) 48 #define A10_DACALEN __BIT(30) 49 #define A10_MIXEN __BIT(29) 50 #define A10_LNG __BIT(26) 51 #define A10_FMG __BITS(25,23) 52 #define A10_MICG __BITS(22,20) 53 #define A10_LLNS __BIT(19) 54 #define A10_RLNS __BIT(18) 55 #define A10_LFMS __BIT(17) 56 #define A10_RFMS __BIT(16) 57 #define A10_LDACLMIXS __BIT(15) 58 #define A10_RDACRMIXS __BIT(14) 59 #define A10_LDACRMIXS __BIT(13) 60 #define A10_MICLS __BIT(12) 61 #define A10_MICRS __BIT(11) 62 #define A10_DACPAS __BIT(8) 63 #define A10_MIXPAS __BIT(7) 64 #define A10_PAMUTE __BIT(6) 65 #define A10_PAVOL __BITS(5,0) 66 67 #define A10_ADC_ACTRL 0x28 68 #define A10_ADCREN __BIT(31) 69 #define A10_ADCLEN __BIT(30) 70 #define A10_PREG1EN __BIT(29) 71 #define A10_PREG2EN __BIT(28) 72 #define A10_VMICEN __BIT(27) 73 #define A10_PREG1 __BITS(26,25) 74 #define A10_PREG2 __BITS(24,23) 75 #define A10_ADCG __BITS(22,20) 76 #define A10_ADCIS __BITS(19,17) 77 #define A10_LNRDF __BIT(16) 78 #define A10_LNPREG __BITS(15,13) 79 #define A10_MIC1NEN __BIT(12) 80 #define A10_DITHER __BIT(8) 81 #define A10_PA_EN __BIT(4) 82 #define A10_DDE __BIT(3) 83 #define A10_COMPTEN __BIT(2) 84 #define A10_PTDBS __BITS(1,0) 85 86 enum a10_codec_mixer_ctrl { 87 A10_CODEC_OUTPUT_CLASS, 88 A10_CODEC_INPUT_CLASS, 89 90 A10_CODEC_OUTPUT_MASTER_VOLUME, 91 A10_CODEC_INPUT_DAC_VOLUME, 92 93 A10_CODEC_MIXER_CTRL_LAST 94 }; 95 96 static const struct a10_codec_mixer { 97 const char * name; 98 enum a10_codec_mixer_ctrl mixer_class; 99 u_int reg; 100 u_int mask; 101 } a10_codec_mixers[A10_CODEC_MIXER_CTRL_LAST] = { 102 [A10_CODEC_OUTPUT_MASTER_VOLUME] = { AudioNmaster, 103 A10_CODEC_OUTPUT_CLASS, A10_DAC_ACTRL, A10_PAVOL }, 104 [A10_CODEC_INPUT_DAC_VOLUME] = { AudioNdac, 105 A10_CODEC_INPUT_CLASS, A10_DAC_ACTRL, A10_PAVOL }, 106 }; 107 108 #define RD4(sc, reg) \ 109 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 110 #define WR4(sc, reg, val) \ 111 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 112 #define SET4(sc, reg, mask) \ 113 WR4((sc), (reg), RD4((sc), (reg)) | (mask)) 114 #define CLR4(sc, reg, mask) \ 115 WR4((sc), (reg), RD4((sc), (reg)) & ~(mask)) 116 117 static int 118 a10_codec_init(struct sunxi_codec_softc *sc) 119 { 120 121 /* Unmute PA */ 122 SET4(sc, A10_DAC_ACTRL, A10_PAMUTE); 123 /* Set initial volume */ 124 CLR4(sc, A10_DAC_ACTRL, A10_PAVOL); 125 SET4(sc, A10_DAC_ACTRL, __SHIFTIN(A10_DEFAULT_PAVOL, A10_PAVOL)); 126 /* Enable PA */ 127 SET4(sc, A10_ADC_ACTRL, A10_PA_EN); 128 129 return 0; 130 } 131 132 static void 133 a10_codec_mute(struct sunxi_codec_softc *sc, int mute, u_int mode) 134 { 135 if (mode == AUMODE_PLAY) { 136 const uint32_t pmask = A10_DACAREN|A10_DACALEN|A10_DACPAS; 137 if (mute) { 138 /* Mute DAC l/r channels to output mixer */ 139 CLR4(sc, A10_DAC_ACTRL, pmask); 140 } else { 141 /* Enable DAC analog l/r channels and output mixer */ 142 SET4(sc, A10_DAC_ACTRL, pmask); 143 } 144 } else { 145 const uint32_t rmask = A10_ADCREN|A10_ADCLEN; 146 if (mute) { 147 /* Disable ADC analog l/r channels */ 148 CLR4(sc, A10_ADC_ACTRL, rmask); 149 } else { 150 /* Enable ADC analog l/r channels */ 151 SET4(sc, A10_ADC_ACTRL, rmask); 152 } 153 } 154 } 155 156 static int 157 a10_codec_set_port(struct sunxi_codec_softc *sc, mixer_ctrl_t *mc) 158 { 159 const struct a10_codec_mixer *mix; 160 u_int val, shift; 161 int nvol; 162 163 switch (mc->dev) { 164 case A10_CODEC_OUTPUT_MASTER_VOLUME: 165 case A10_CODEC_INPUT_DAC_VOLUME: 166 mix = &a10_codec_mixers[mc->dev]; 167 val = RD4(sc, mix->reg); 168 shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask)); 169 nvol = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] >> shift; 170 val &= ~mix->mask; 171 val |= __SHIFTIN(nvol, mix->mask); 172 WR4(sc, mix->reg, val); 173 return 0; 174 } 175 176 return ENXIO; 177 } 178 179 static int 180 a10_codec_get_port(struct sunxi_codec_softc *sc, mixer_ctrl_t *mc) 181 { 182 const struct a10_codec_mixer *mix; 183 u_int val, shift; 184 int nvol; 185 186 switch (mc->dev) { 187 case A10_CODEC_OUTPUT_MASTER_VOLUME: 188 case A10_CODEC_INPUT_DAC_VOLUME: 189 mix = &a10_codec_mixers[mc->dev]; 190 val = RD4(sc, mix->reg); 191 shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask)); 192 nvol = __SHIFTOUT(val, mix->mask) << shift; 193 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = nvol; 194 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = nvol; 195 return 0; 196 } 197 198 return ENXIO; 199 } 200 201 static int 202 a10_codec_query_devinfo(struct sunxi_codec_softc *sc, mixer_devinfo_t *di) 203 { 204 const struct a10_codec_mixer *mix; 205 206 switch (di->index) { 207 case A10_CODEC_OUTPUT_CLASS: 208 di->mixer_class = di->index; 209 strcpy(di->label.name, AudioCoutputs); 210 di->type = AUDIO_MIXER_CLASS; 211 di->next = di->prev = AUDIO_MIXER_LAST; 212 return 0; 213 214 case A10_CODEC_INPUT_CLASS: 215 di->mixer_class = di->index; 216 strcpy(di->label.name, AudioCinputs); 217 di->type = AUDIO_MIXER_CLASS; 218 di->next = di->prev = AUDIO_MIXER_LAST; 219 return 0; 220 221 case A10_CODEC_OUTPUT_MASTER_VOLUME: 222 case A10_CODEC_INPUT_DAC_VOLUME: 223 mix = &a10_codec_mixers[di->index]; 224 di->mixer_class = mix->mixer_class; 225 strcpy(di->label.name, mix->name); 226 di->un.v.delta = 227 256 / (__SHIFTOUT_MASK(mix->mask) + 1); 228 di->type = AUDIO_MIXER_VALUE; 229 di->next = di->prev = AUDIO_MIXER_LAST; 230 di->un.v.num_channels = 2; 231 strcpy(di->un.v.units.name, AudioNvolume); 232 return 0; 233 } 234 235 return ENXIO; 236 } 237 238 const struct sunxi_codec_conf sun4i_a10_codecconf = { 239 .name = "A10 Audio Codec", 240 241 .init = a10_codec_init, 242 .mute = a10_codec_mute, 243 .set_port = a10_codec_set_port, 244 .get_port = a10_codec_get_port, 245 .query_devinfo = a10_codec_query_devinfo, 246 247 .DPC = 0x00, 248 .DAC_FIFOC = 0x04, 249 .DAC_FIFOS = 0x08, 250 .DAC_TXDATA = 0x0c, 251 .ADC_FIFOC = 0x1c, 252 .ADC_FIFOS = 0x20, 253 .ADC_RXDATA = 0x24, 254 .DAC_CNT = 0x30, 255 .ADC_CNT = 0x34, 256 }; 257