Home | History | Annotate | Line # | Download | only in sunxi
sun4i_a10_codec.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: sun4i_a10_codec.c,v 1.1 2017/08/27 16:05:26 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 <sys/cdefs.h>
     30  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: sun4i_a10_codec.c,v 1.1 2017/08/27 16:05:26 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/cpu.h>
     35  1.1  jmcneill #include <sys/device.h>
     36  1.1  jmcneill #include <sys/kmem.h>
     37  1.1  jmcneill #include <sys/bitops.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <sys/audioio.h>
     40  1.1  jmcneill #include <dev/audio_if.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include <arm/sunxi/sunxi_codec.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #define	A10_DEFAULT_PAVOL	0x20
     45  1.1  jmcneill 
     46  1.1  jmcneill #define	A10_DAC_ACTRL		0x10
     47  1.1  jmcneill #define	 A10_DACAREN		__BIT(31)
     48  1.1  jmcneill #define	 A10_DACALEN		__BIT(30)
     49  1.1  jmcneill #define	 A10_MIXEN		__BIT(29)
     50  1.1  jmcneill #define	 A10_LNG		__BIT(26)
     51  1.1  jmcneill #define	 A10_FMG		__BITS(25,23)
     52  1.1  jmcneill #define	 A10_MICG		__BITS(22,20)
     53  1.1  jmcneill #define	 A10_LLNS		__BIT(19)
     54  1.1  jmcneill #define	 A10_RLNS		__BIT(18)
     55  1.1  jmcneill #define	 A10_LFMS		__BIT(17)
     56  1.1  jmcneill #define	 A10_RFMS		__BIT(16)
     57  1.1  jmcneill #define	 A10_LDACLMIXS		__BIT(15)
     58  1.1  jmcneill #define	 A10_RDACRMIXS		__BIT(14)
     59  1.1  jmcneill #define	 A10_LDACRMIXS		__BIT(13)
     60  1.1  jmcneill #define	 A10_MICLS		__BIT(12)
     61  1.1  jmcneill #define	 A10_MICRS		__BIT(11)
     62  1.1  jmcneill #define	 A10_DACPAS		__BIT(8)
     63  1.1  jmcneill #define	 A10_MIXPAS		__BIT(7)
     64  1.1  jmcneill #define	 A10_PAMUTE		__BIT(6)
     65  1.1  jmcneill #define	 A10_PAVOL		__BITS(5,0)
     66  1.1  jmcneill 
     67  1.1  jmcneill #define	A10_ADC_ACTRL		0x28
     68  1.1  jmcneill #define	 A10_ADCREN		__BIT(31)
     69  1.1  jmcneill #define	 A10_ADCLEN		__BIT(30)
     70  1.1  jmcneill #define	 A10_PREG1EN		__BIT(29)
     71  1.1  jmcneill #define	 A10_PREG2EN		__BIT(28)
     72  1.1  jmcneill #define	 A10_VMICEN		__BIT(27)
     73  1.1  jmcneill #define	 A10_PREG1		__BITS(26,25)
     74  1.1  jmcneill #define	 A10_PREG2		__BITS(24,23)
     75  1.1  jmcneill #define	 A10_ADCG		__BITS(22,20)
     76  1.1  jmcneill #define	 A10_ADCIS		__BITS(19,17)
     77  1.1  jmcneill #define	 A10_LNRDF		__BIT(16)
     78  1.1  jmcneill #define	 A10_LNPREG		__BITS(15,13)
     79  1.1  jmcneill #define	 A10_MIC1NEN		__BIT(12)
     80  1.1  jmcneill #define	 A10_DITHER		__BIT(8)
     81  1.1  jmcneill #define	 A10_PA_EN		__BIT(4)
     82  1.1  jmcneill #define	 A10_DDE		__BIT(3)
     83  1.1  jmcneill #define	 A10_COMPTEN		__BIT(2)
     84  1.1  jmcneill #define	 A10_PTDBS		__BITS(1,0)
     85  1.1  jmcneill 
     86  1.1  jmcneill enum a10_codec_mixer_ctrl {
     87  1.1  jmcneill 	A10_CODEC_OUTPUT_CLASS,
     88  1.1  jmcneill 	A10_CODEC_INPUT_CLASS,
     89  1.1  jmcneill 
     90  1.1  jmcneill 	A10_CODEC_OUTPUT_MASTER_VOLUME,
     91  1.1  jmcneill 	A10_CODEC_INPUT_DAC_VOLUME,
     92  1.1  jmcneill 
     93  1.1  jmcneill 	A10_CODEC_MIXER_CTRL_LAST
     94  1.1  jmcneill };
     95  1.1  jmcneill 
     96  1.1  jmcneill static const struct a10_codec_mixer {
     97  1.1  jmcneill 	const char *			name;
     98  1.1  jmcneill 	enum a10_codec_mixer_ctrl	mixer_class;
     99  1.1  jmcneill 	u_int				reg;
    100  1.1  jmcneill 	u_int				mask;
    101  1.1  jmcneill } a10_codec_mixers[A10_CODEC_MIXER_CTRL_LAST] = {
    102  1.1  jmcneill 	[A10_CODEC_OUTPUT_MASTER_VOLUME]	= { AudioNmaster,
    103  1.1  jmcneill 	    A10_CODEC_OUTPUT_CLASS, A10_DAC_ACTRL, A10_PAVOL },
    104  1.1  jmcneill 	[A10_CODEC_INPUT_DAC_VOLUME]		= { AudioNdac,
    105  1.1  jmcneill 	    A10_CODEC_INPUT_CLASS, A10_DAC_ACTRL, A10_PAVOL },
    106  1.1  jmcneill };
    107  1.1  jmcneill 
    108  1.1  jmcneill #define	RD4(sc, reg)			\
    109  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    110  1.1  jmcneill #define	WR4(sc, reg, val)		\
    111  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    112  1.1  jmcneill #define	SET4(sc, reg, mask)		\
    113  1.1  jmcneill 	WR4((sc), (reg), RD4((sc), (reg)) | (mask))
    114  1.1  jmcneill #define	CLR4(sc, reg, mask)		\
    115  1.1  jmcneill 	WR4((sc), (reg), RD4((sc), (reg)) & ~(mask))
    116  1.1  jmcneill 
    117  1.1  jmcneill static int
    118  1.1  jmcneill a10_codec_init(struct sunxi_codec_softc *sc)
    119  1.1  jmcneill {
    120  1.1  jmcneill 
    121  1.1  jmcneill 	/* Unmute PA */
    122  1.1  jmcneill 	SET4(sc, A10_DAC_ACTRL, A10_PAMUTE);
    123  1.1  jmcneill 	/* Set initial volume */
    124  1.1  jmcneill 	CLR4(sc, A10_DAC_ACTRL, A10_PAVOL);
    125  1.1  jmcneill 	SET4(sc, A10_DAC_ACTRL, __SHIFTIN(A10_DEFAULT_PAVOL, A10_PAVOL));
    126  1.1  jmcneill 	/* Enable PA */
    127  1.1  jmcneill 	SET4(sc, A10_ADC_ACTRL, A10_PA_EN);
    128  1.1  jmcneill 
    129  1.1  jmcneill 	return 0;
    130  1.1  jmcneill }
    131  1.1  jmcneill 
    132  1.1  jmcneill static void
    133  1.1  jmcneill a10_codec_mute(struct sunxi_codec_softc *sc, int mute, u_int mode)
    134  1.1  jmcneill {
    135  1.1  jmcneill 	if (mode == AUMODE_PLAY) {
    136  1.1  jmcneill 		const uint32_t pmask = A10_DACAREN|A10_DACALEN|A10_DACPAS;
    137  1.1  jmcneill 		if (mute) {
    138  1.1  jmcneill 			/* Mute DAC l/r channels to output mixer */
    139  1.1  jmcneill 			CLR4(sc, A10_DAC_ACTRL, pmask);
    140  1.1  jmcneill 		} else {
    141  1.1  jmcneill 			/* Enable DAC analog l/r channels and output mixer */
    142  1.1  jmcneill 			SET4(sc, A10_DAC_ACTRL, pmask);
    143  1.1  jmcneill 		}
    144  1.1  jmcneill 	} else {
    145  1.1  jmcneill 		const uint32_t rmask = A10_ADCREN|A10_ADCLEN;
    146  1.1  jmcneill 		if (mute) {
    147  1.1  jmcneill 			/* Disable ADC analog l/r channels */
    148  1.1  jmcneill 			CLR4(sc, A10_ADC_ACTRL, rmask);
    149  1.1  jmcneill 		} else {
    150  1.1  jmcneill 			/* Enable ADC analog l/r channels */
    151  1.1  jmcneill 			SET4(sc, A10_ADC_ACTRL, rmask);
    152  1.1  jmcneill 		}
    153  1.1  jmcneill 	}
    154  1.1  jmcneill }
    155  1.1  jmcneill 
    156  1.1  jmcneill static int
    157  1.1  jmcneill a10_codec_set_port(struct sunxi_codec_softc *sc, mixer_ctrl_t *mc)
    158  1.1  jmcneill {
    159  1.1  jmcneill 	const struct a10_codec_mixer *mix;
    160  1.1  jmcneill 	u_int val, shift;
    161  1.1  jmcneill 	int nvol;
    162  1.1  jmcneill 
    163  1.1  jmcneill 	switch (mc->dev) {
    164  1.1  jmcneill 	case A10_CODEC_OUTPUT_MASTER_VOLUME:
    165  1.1  jmcneill 	case A10_CODEC_INPUT_DAC_VOLUME:
    166  1.1  jmcneill 		mix = &a10_codec_mixers[mc->dev];
    167  1.1  jmcneill 		val = RD4(sc, mix->reg);
    168  1.1  jmcneill 		shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask));
    169  1.1  jmcneill 		nvol = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] >> shift;
    170  1.1  jmcneill 		val &= ~mix->mask;
    171  1.1  jmcneill 		val |= __SHIFTIN(nvol, mix->mask);
    172  1.1  jmcneill 		WR4(sc, mix->reg, val);
    173  1.1  jmcneill 		return 0;
    174  1.1  jmcneill 	}
    175  1.1  jmcneill 
    176  1.1  jmcneill 	return ENXIO;
    177  1.1  jmcneill }
    178  1.1  jmcneill 
    179  1.1  jmcneill static int
    180  1.1  jmcneill a10_codec_get_port(struct sunxi_codec_softc *sc, mixer_ctrl_t *mc)
    181  1.1  jmcneill {
    182  1.1  jmcneill 	const struct a10_codec_mixer *mix;
    183  1.1  jmcneill 	u_int val, shift;
    184  1.1  jmcneill 	int nvol;
    185  1.1  jmcneill 
    186  1.1  jmcneill 	switch (mc->dev) {
    187  1.1  jmcneill 	case A10_CODEC_OUTPUT_MASTER_VOLUME:
    188  1.1  jmcneill 	case A10_CODEC_INPUT_DAC_VOLUME:
    189  1.1  jmcneill 		mix = &a10_codec_mixers[mc->dev];
    190  1.1  jmcneill 		val = RD4(sc, mix->reg);
    191  1.1  jmcneill 		shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask));
    192  1.1  jmcneill 		nvol = __SHIFTOUT(val, mix->mask) << shift;
    193  1.1  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = nvol;
    194  1.1  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = nvol;
    195  1.1  jmcneill 		return 0;
    196  1.1  jmcneill 	}
    197  1.1  jmcneill 
    198  1.1  jmcneill 	return ENXIO;
    199  1.1  jmcneill }
    200  1.1  jmcneill 
    201  1.1  jmcneill static int
    202  1.1  jmcneill a10_codec_query_devinfo(struct sunxi_codec_softc *sc, mixer_devinfo_t *di)
    203  1.1  jmcneill {
    204  1.1  jmcneill 	const struct a10_codec_mixer *mix;
    205  1.1  jmcneill 
    206  1.1  jmcneill 	switch (di->index) {
    207  1.1  jmcneill 	case A10_CODEC_OUTPUT_CLASS:
    208  1.1  jmcneill 		di->mixer_class = di->index;
    209  1.1  jmcneill 		strcpy(di->label.name, AudioCoutputs);
    210  1.1  jmcneill 		di->type = AUDIO_MIXER_CLASS;
    211  1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    212  1.1  jmcneill 		return 0;
    213  1.1  jmcneill 
    214  1.1  jmcneill 	case A10_CODEC_INPUT_CLASS:
    215  1.1  jmcneill 		di->mixer_class = di->index;
    216  1.1  jmcneill 		strcpy(di->label.name, AudioCinputs);
    217  1.1  jmcneill 		di->type = AUDIO_MIXER_CLASS;
    218  1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    219  1.1  jmcneill 		return 0;
    220  1.1  jmcneill 
    221  1.1  jmcneill 	case A10_CODEC_OUTPUT_MASTER_VOLUME:
    222  1.1  jmcneill 	case A10_CODEC_INPUT_DAC_VOLUME:
    223  1.1  jmcneill 		mix = &a10_codec_mixers[di->index];
    224  1.1  jmcneill 		di->mixer_class = mix->mixer_class;
    225  1.1  jmcneill 		strcpy(di->label.name, mix->name);
    226  1.1  jmcneill 		di->un.v.delta =
    227  1.1  jmcneill 		    256 / (__SHIFTOUT_MASK(mix->mask) + 1);
    228  1.1  jmcneill 		di->type = AUDIO_MIXER_VALUE;
    229  1.1  jmcneill 		di->next = di->prev = AUDIO_MIXER_LAST;
    230  1.1  jmcneill 		di->un.v.num_channels = 2;
    231  1.1  jmcneill 		strcpy(di->un.v.units.name, AudioNvolume);
    232  1.1  jmcneill 		return 0;
    233  1.1  jmcneill 	}
    234  1.1  jmcneill 
    235  1.1  jmcneill 	return ENXIO;
    236  1.1  jmcneill }
    237  1.1  jmcneill 
    238  1.1  jmcneill const struct sunxi_codec_conf sun4i_a10_codecconf = {
    239  1.1  jmcneill 	.name = "A10 Audio Codec",
    240  1.1  jmcneill 
    241  1.1  jmcneill 	.init = a10_codec_init,
    242  1.1  jmcneill 	.mute = a10_codec_mute,
    243  1.1  jmcneill 	.set_port = a10_codec_set_port,
    244  1.1  jmcneill 	.get_port = a10_codec_get_port,
    245  1.1  jmcneill 	.query_devinfo = a10_codec_query_devinfo,
    246  1.1  jmcneill 
    247  1.1  jmcneill 	.DPC		= 0x00,
    248  1.1  jmcneill 	.DAC_FIFOC	= 0x04,
    249  1.1  jmcneill 	.DAC_FIFOS	= 0x08,
    250  1.1  jmcneill 	.DAC_TXDATA	= 0x0c,
    251  1.1  jmcneill 	.ADC_FIFOC	= 0x1c,
    252  1.1  jmcneill 	.ADC_FIFOS	= 0x20,
    253  1.1  jmcneill 	.ADC_RXDATA	= 0x24,
    254  1.1  jmcneill 	.DAC_CNT	= 0x30,
    255  1.1  jmcneill 	.ADC_CNT	= 0x34,
    256  1.1  jmcneill };
    257