1*6e54367aSthorpej /* $NetBSD: sun50i_a64_acodec.c,v 1.10 2021/01/27 03:10:20 thorpej Exp $ */
244ca330bSjmcneill
344ca330bSjmcneill /*-
444ca330bSjmcneill * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
544ca330bSjmcneill * All rights reserved.
644ca330bSjmcneill *
744ca330bSjmcneill * Redistribution and use in source and binary forms, with or without
844ca330bSjmcneill * modification, are permitted provided that the following conditions
944ca330bSjmcneill * are met:
1044ca330bSjmcneill * 1. Redistributions of source code must retain the above copyright
1144ca330bSjmcneill * notice, this list of conditions and the following disclaimer.
1244ca330bSjmcneill * 2. Redistributions in binary form must reproduce the above copyright
1344ca330bSjmcneill * notice, this list of conditions and the following disclaimer in the
1444ca330bSjmcneill * documentation and/or other materials provided with the distribution.
1544ca330bSjmcneill *
1644ca330bSjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1744ca330bSjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1844ca330bSjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1944ca330bSjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2044ca330bSjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2144ca330bSjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2244ca330bSjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2344ca330bSjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2444ca330bSjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2544ca330bSjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2644ca330bSjmcneill * SUCH DAMAGE.
2744ca330bSjmcneill */
2844ca330bSjmcneill
2944ca330bSjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: sun50i_a64_acodec.c,v 1.10 2021/01/27 03:10:20 thorpej Exp $");
3144ca330bSjmcneill
3244ca330bSjmcneill #include <sys/param.h>
3344ca330bSjmcneill #include <sys/bus.h>
3444ca330bSjmcneill #include <sys/cpu.h>
3544ca330bSjmcneill #include <sys/device.h>
3644ca330bSjmcneill #include <sys/kmem.h>
3744ca330bSjmcneill #include <sys/bitops.h>
3844ca330bSjmcneill
39e622eac4Sisaki #include <dev/audio/audio_dai.h>
4044ca330bSjmcneill
4144ca330bSjmcneill #include <dev/fdt/fdtvar.h>
4244ca330bSjmcneill
4344ca330bSjmcneill #define A64_PR_CFG 0x00
4444ca330bSjmcneill #define A64_AC_PR_RST __BIT(28)
4544ca330bSjmcneill #define A64_AC_PR_RW __BIT(24)
4644ca330bSjmcneill #define A64_AC_PR_ADDR __BITS(20,16)
4744ca330bSjmcneill #define A64_ACDA_PR_WDAT __BITS(15,8)
4844ca330bSjmcneill #define A64_ACDA_PR_RDAT __BITS(7,0)
4944ca330bSjmcneill
5044ca330bSjmcneill #define A64_HP_CTRL 0x00
5144ca330bSjmcneill #define A64_HPPA_EN __BIT(6)
5244ca330bSjmcneill #define A64_HPVOL __BITS(5,0)
5344ca330bSjmcneill #define A64_OL_MIX_CTRL 0x01
5444ca330bSjmcneill #define A64_LMIXMUTE_LDAC __BIT(1)
5544ca330bSjmcneill #define A64_OR_MIX_CTRL 0x02
5644ca330bSjmcneill #define A64_RMIXMUTE_RDAC __BIT(1)
5744ca330bSjmcneill #define A64_LINEOUT_CTRL0 0x05
5844ca330bSjmcneill #define A64_LINEOUT_LEFT_EN __BIT(7)
5944ca330bSjmcneill #define A64_LINEOUT_RIGHT_EN __BIT(6)
60254f45d9Sjmcneill #define A64_LINEOUT_EN (A64_LINEOUT_LEFT_EN|A64_LINEOUT_RIGHT_EN)
6144ca330bSjmcneill #define A64_LINEOUT_CTRL1 0x06
6244ca330bSjmcneill #define A64_LINEOUT_VOL __BITS(4,0)
6344ca330bSjmcneill #define A64_MIC1_CTRL 0x07
6444ca330bSjmcneill #define A64_MIC1G __BITS(6,4)
6544ca330bSjmcneill #define A64_MIC1AMPEN __BIT(3)
6644ca330bSjmcneill #define A64_MIC1BOOST __BITS(2,0)
6744ca330bSjmcneill #define A64_MIC2_CTRL 0x08
6844ca330bSjmcneill #define A64_MIC2_SEL __BIT(7)
6944ca330bSjmcneill #define A64_MIC2G __BITS(6,4)
7044ca330bSjmcneill #define A64_MIC2AMPEN __BIT(3)
7144ca330bSjmcneill #define A64_MIC2BOOST __BITS(2,0)
7244ca330bSjmcneill #define A64_LINEIN_CTRL 0x09
7344ca330bSjmcneill #define A64_LINEING __BITS(6,4)
7444ca330bSjmcneill #define A64_MIX_DAC_CTRL 0x0a
7544ca330bSjmcneill #define A64_DACAREN __BIT(7)
7644ca330bSjmcneill #define A64_DACALEN __BIT(6)
7744ca330bSjmcneill #define A64_RMIXEN __BIT(5)
7844ca330bSjmcneill #define A64_LMIXEN __BIT(4)
7944ca330bSjmcneill #define A64_RHPPAMUTE __BIT(3)
8044ca330bSjmcneill #define A64_LHPPAMUTE __BIT(2)
8144ca330bSjmcneill #define A64_RHPIS __BIT(1)
8244ca330bSjmcneill #define A64_LHPIS __BIT(0)
8344ca330bSjmcneill #define A64_L_ADCMIX_SRC 0x0b
8444ca330bSjmcneill #define A64_R_ADCMIX_SRC 0x0c
8544ca330bSjmcneill #define A64_ADCMIX_SRC_MIC1 __BIT(6)
8644ca330bSjmcneill #define A64_ADCMIX_SRC_MIC2 __BIT(5)
8744ca330bSjmcneill #define A64_ADCMIX_SRC_LINEIN __BIT(2)
88779f2270Sjmcneill #define A64_ADCMIX_SRC_OMIXER __BIT(1)
8944ca330bSjmcneill #define A64_ADC_CTRL 0x0d
9044ca330bSjmcneill #define A64_ADCREN __BIT(7)
9144ca330bSjmcneill #define A64_ADCLEN __BIT(6)
9244ca330bSjmcneill #define A64_ADCG __BITS(2,0)
9376bf467cSjmcneill #define A64_JACK_MIC_CTRL 0x1d
9476bf467cSjmcneill #define A64_JACKDETEN __BIT(7)
9576bf467cSjmcneill #define A64_INNERRESEN __BIT(6)
9676bf467cSjmcneill #define A64_AUTOPLEN __BIT(1)
9744ca330bSjmcneill
9844ca330bSjmcneill struct a64_acodec_softc {
9944ca330bSjmcneill device_t sc_dev;
10044ca330bSjmcneill bus_space_tag_t sc_bst;
10144ca330bSjmcneill bus_space_handle_t sc_bsh;
10244ca330bSjmcneill int sc_phandle;
10344ca330bSjmcneill
10444ca330bSjmcneill struct audio_dai_device sc_dai;
105254f45d9Sjmcneill int sc_master_dev;
10644ca330bSjmcneill };
10744ca330bSjmcneill
10844ca330bSjmcneill enum a64_acodec_mixer_ctrl {
10944ca330bSjmcneill A64_CODEC_OUTPUT_CLASS,
11044ca330bSjmcneill A64_CODEC_INPUT_CLASS,
11144ca330bSjmcneill A64_CODEC_RECORD_CLASS,
11244ca330bSjmcneill
11344ca330bSjmcneill A64_CODEC_OUTPUT_MASTER_VOLUME,
1146584c964Sjmcneill A64_CODEC_OUTPUT_MUTE,
115254f45d9Sjmcneill A64_CODEC_OUTPUT_SOURCE,
116254f45d9Sjmcneill A64_CODEC_INPUT_LINE_VOLUME,
117254f45d9Sjmcneill A64_CODEC_INPUT_HP_VOLUME,
118254f45d9Sjmcneill A64_CODEC_RECORD_LINE_VOLUME,
119254f45d9Sjmcneill A64_CODEC_RECORD_MIC1_VOLUME,
120779f2270Sjmcneill A64_CODEC_RECORD_MIC1_PREAMP,
121254f45d9Sjmcneill A64_CODEC_RECORD_MIC2_VOLUME,
122779f2270Sjmcneill A64_CODEC_RECORD_MIC2_PREAMP,
12344ca330bSjmcneill A64_CODEC_RECORD_AGC_VOLUME,
12444ca330bSjmcneill A64_CODEC_RECORD_SOURCE,
12544ca330bSjmcneill
12644ca330bSjmcneill A64_CODEC_MIXER_CTRL_LAST
12744ca330bSjmcneill };
12844ca330bSjmcneill
129254f45d9Sjmcneill #define A64_OUTPUT_SOURCE_LINE __BIT(0)
130254f45d9Sjmcneill #define A64_OUTPUT_SOURCE_HP __BIT(1)
131254f45d9Sjmcneill
13244ca330bSjmcneill static const struct a64_acodec_mixer {
13344ca330bSjmcneill const char * name;
13444ca330bSjmcneill enum a64_acodec_mixer_ctrl mixer_class;
13544ca330bSjmcneill u_int reg;
13644ca330bSjmcneill u_int mask;
13744ca330bSjmcneill } a64_acodec_mixers[A64_CODEC_MIXER_CTRL_LAST] = {
138254f45d9Sjmcneill [A64_CODEC_INPUT_LINE_VOLUME] = { AudioNline,
13944ca330bSjmcneill A64_CODEC_INPUT_CLASS, A64_LINEOUT_CTRL1, A64_LINEOUT_VOL },
140254f45d9Sjmcneill [A64_CODEC_INPUT_HP_VOLUME] = { AudioNheadphone,
141254f45d9Sjmcneill A64_CODEC_INPUT_CLASS, A64_HP_CTRL, A64_HPVOL },
142254f45d9Sjmcneill
143254f45d9Sjmcneill [A64_CODEC_RECORD_LINE_VOLUME] = { AudioNline,
144254f45d9Sjmcneill A64_CODEC_RECORD_CLASS, A64_LINEIN_CTRL, A64_LINEING },
14539e8f1a4Sjmcneill [A64_CODEC_RECORD_MIC1_VOLUME] = { AudioNmicrophone,
146254f45d9Sjmcneill A64_CODEC_RECORD_CLASS, A64_MIC1_CTRL, A64_MIC1G },
14739e8f1a4Sjmcneill [A64_CODEC_RECORD_MIC2_VOLUME] = { AudioNmicrophone "2",
148254f45d9Sjmcneill A64_CODEC_RECORD_CLASS, A64_MIC2_CTRL, A64_MIC2G },
14944ca330bSjmcneill [A64_CODEC_RECORD_AGC_VOLUME] = { AudioNagc,
15044ca330bSjmcneill A64_CODEC_RECORD_CLASS, A64_ADC_CTRL, A64_ADCG },
15144ca330bSjmcneill };
15244ca330bSjmcneill
15344ca330bSjmcneill #define RD4(sc, reg) \
15444ca330bSjmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
15544ca330bSjmcneill #define WR4(sc, reg, val) \
15644ca330bSjmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
15744ca330bSjmcneill
15844ca330bSjmcneill static u_int
a64_acodec_pr_read(struct a64_acodec_softc * sc,u_int addr)15944ca330bSjmcneill a64_acodec_pr_read(struct a64_acodec_softc *sc, u_int addr)
16044ca330bSjmcneill {
16144ca330bSjmcneill uint32_t val;
16244ca330bSjmcneill
16344ca330bSjmcneill /* Read current value */
16444ca330bSjmcneill val = RD4(sc, A64_PR_CFG);
16544ca330bSjmcneill
16644ca330bSjmcneill /* De-assert reset */
16744ca330bSjmcneill val |= A64_AC_PR_RST;
16844ca330bSjmcneill WR4(sc, A64_PR_CFG, val);
16944ca330bSjmcneill
17044ca330bSjmcneill /* Read mode */
17144ca330bSjmcneill val &= ~A64_AC_PR_RW;
17244ca330bSjmcneill WR4(sc, A64_PR_CFG, val);
17344ca330bSjmcneill
17444ca330bSjmcneill /* Set address */
17544ca330bSjmcneill val &= ~A64_AC_PR_ADDR;
17644ca330bSjmcneill val |= __SHIFTIN(addr, A64_AC_PR_ADDR);
17744ca330bSjmcneill WR4(sc, A64_PR_CFG, val);
17844ca330bSjmcneill
17944ca330bSjmcneill /* Read data */
18044ca330bSjmcneill return __SHIFTOUT(RD4(sc, A64_PR_CFG), A64_ACDA_PR_RDAT);
18144ca330bSjmcneill }
18244ca330bSjmcneill
18344ca330bSjmcneill static void
a64_acodec_pr_write(struct a64_acodec_softc * sc,u_int addr,u_int data)18444ca330bSjmcneill a64_acodec_pr_write(struct a64_acodec_softc *sc, u_int addr, u_int data)
18544ca330bSjmcneill {
18644ca330bSjmcneill uint32_t val;
18744ca330bSjmcneill
18844ca330bSjmcneill /* Read current value */
18944ca330bSjmcneill val = RD4(sc, A64_PR_CFG);
19044ca330bSjmcneill
19144ca330bSjmcneill /* De-assert reset */
19244ca330bSjmcneill val |= A64_AC_PR_RST;
19344ca330bSjmcneill WR4(sc, A64_PR_CFG, val);
19444ca330bSjmcneill
19544ca330bSjmcneill /* Set address */
19644ca330bSjmcneill val &= ~A64_AC_PR_ADDR;
19744ca330bSjmcneill val |= __SHIFTIN(addr, A64_AC_PR_ADDR);
19844ca330bSjmcneill WR4(sc, A64_PR_CFG, val);
19944ca330bSjmcneill
20044ca330bSjmcneill /* Write data */
20144ca330bSjmcneill val &= ~A64_ACDA_PR_WDAT;
20244ca330bSjmcneill val |= __SHIFTIN(data, A64_ACDA_PR_WDAT);
20344ca330bSjmcneill WR4(sc, A64_PR_CFG, val);
20444ca330bSjmcneill
20544ca330bSjmcneill /* Write mode */
20644ca330bSjmcneill val |= A64_AC_PR_RW;
20744ca330bSjmcneill WR4(sc, A64_PR_CFG, val);
20844ca330bSjmcneill
20944ca330bSjmcneill /* Clear write mode */
21044ca330bSjmcneill val &= ~A64_AC_PR_RW;
21144ca330bSjmcneill WR4(sc, A64_PR_CFG, val);
21244ca330bSjmcneill }
21344ca330bSjmcneill
21444ca330bSjmcneill static void
a64_acodec_pr_set_clear(struct a64_acodec_softc * sc,u_int addr,u_int set,u_int clr)21544ca330bSjmcneill a64_acodec_pr_set_clear(struct a64_acodec_softc *sc, u_int addr, u_int set, u_int clr)
21644ca330bSjmcneill {
21744ca330bSjmcneill u_int old, new;
21844ca330bSjmcneill
21944ca330bSjmcneill old = a64_acodec_pr_read(sc, addr);
22044ca330bSjmcneill new = set | (old & ~clr);
22144ca330bSjmcneill a64_acodec_pr_write(sc, addr, new);
22244ca330bSjmcneill }
22344ca330bSjmcneill
22444ca330bSjmcneill static int
a64_acodec_trigger_output(void * priv,void * start,void * end,int blksize,void (* intr)(void *),void * intrarg,const audio_params_t * params)22544ca330bSjmcneill a64_acodec_trigger_output(void *priv, void *start, void *end, int blksize,
22644ca330bSjmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
22744ca330bSjmcneill {
22844ca330bSjmcneill struct a64_acodec_softc * const sc = priv;
22944ca330bSjmcneill
230bd452744Sjmcneill /* Enable DAC analog l/r channels, HP PA, and output mixer */
23144ca330bSjmcneill a64_acodec_pr_set_clear(sc, A64_MIX_DAC_CTRL,
232bd452744Sjmcneill A64_DACAREN | A64_DACALEN | A64_RMIXEN | A64_LMIXEN |
233bd452744Sjmcneill A64_RHPPAMUTE | A64_LHPPAMUTE, 0);
23444ca330bSjmcneill
23544ca330bSjmcneill return 0;
23644ca330bSjmcneill }
23744ca330bSjmcneill
23844ca330bSjmcneill static int
a64_acodec_trigger_input(void * priv,void * start,void * end,int blksize,void (* intr)(void *),void * intrarg,const audio_params_t * params)23944ca330bSjmcneill a64_acodec_trigger_input(void *priv, void *start, void *end, int blksize,
24044ca330bSjmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
24144ca330bSjmcneill {
24244ca330bSjmcneill struct a64_acodec_softc * const sc = priv;
24344ca330bSjmcneill
24444ca330bSjmcneill /* Enable ADC analog l/r channels */
24544ca330bSjmcneill a64_acodec_pr_set_clear(sc, A64_ADC_CTRL,
24644ca330bSjmcneill A64_ADCREN | A64_ADCLEN, 0);
24744ca330bSjmcneill
24844ca330bSjmcneill return 0;
24944ca330bSjmcneill }
25044ca330bSjmcneill
25144ca330bSjmcneill static int
a64_acodec_halt_output(void * priv)25244ca330bSjmcneill a64_acodec_halt_output(void *priv)
25344ca330bSjmcneill {
25444ca330bSjmcneill struct a64_acodec_softc * const sc = priv;
25544ca330bSjmcneill
256bd452744Sjmcneill /* Disable DAC analog l/r channels, HP PA, and output mixer */
25744ca330bSjmcneill a64_acodec_pr_set_clear(sc, A64_MIX_DAC_CTRL,
258bd452744Sjmcneill 0, A64_DACAREN | A64_DACALEN | A64_RMIXEN | A64_LMIXEN |
259bd452744Sjmcneill A64_RHPPAMUTE | A64_LHPPAMUTE);
26044ca330bSjmcneill
26144ca330bSjmcneill return 0;
26244ca330bSjmcneill }
26344ca330bSjmcneill
26444ca330bSjmcneill static int
a64_acodec_halt_input(void * priv)26544ca330bSjmcneill a64_acodec_halt_input(void *priv)
26644ca330bSjmcneill {
26744ca330bSjmcneill struct a64_acodec_softc * const sc = priv;
26844ca330bSjmcneill
26944ca330bSjmcneill /* Disable ADC analog l/r channels */
27044ca330bSjmcneill a64_acodec_pr_set_clear(sc, A64_ADC_CTRL,
27144ca330bSjmcneill 0, A64_ADCREN | A64_ADCLEN);
27244ca330bSjmcneill
27344ca330bSjmcneill return 0;
27444ca330bSjmcneill }
27544ca330bSjmcneill
27644ca330bSjmcneill static int
a64_acodec_set_port(void * priv,mixer_ctrl_t * mc)27744ca330bSjmcneill a64_acodec_set_port(void *priv, mixer_ctrl_t *mc)
27844ca330bSjmcneill {
27944ca330bSjmcneill struct a64_acodec_softc * const sc = priv;
28044ca330bSjmcneill const struct a64_acodec_mixer *mix;
28144ca330bSjmcneill u_int val, shift;
282254f45d9Sjmcneill int nvol, dev;
28344ca330bSjmcneill
284254f45d9Sjmcneill dev = mc->dev;
285254f45d9Sjmcneill if (dev == A64_CODEC_OUTPUT_MASTER_VOLUME)
286254f45d9Sjmcneill dev = sc->sc_master_dev;
287254f45d9Sjmcneill
288254f45d9Sjmcneill switch (dev) {
289254f45d9Sjmcneill case A64_CODEC_INPUT_LINE_VOLUME:
290254f45d9Sjmcneill case A64_CODEC_INPUT_HP_VOLUME:
291254f45d9Sjmcneill case A64_CODEC_RECORD_LINE_VOLUME:
292254f45d9Sjmcneill case A64_CODEC_RECORD_MIC1_VOLUME:
293254f45d9Sjmcneill case A64_CODEC_RECORD_MIC2_VOLUME:
29444ca330bSjmcneill case A64_CODEC_RECORD_AGC_VOLUME:
295254f45d9Sjmcneill mix = &a64_acodec_mixers[dev];
29644ca330bSjmcneill val = a64_acodec_pr_read(sc, mix->reg);
29744ca330bSjmcneill shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask));
29844ca330bSjmcneill nvol = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] >> shift;
29944ca330bSjmcneill val &= ~mix->mask;
30044ca330bSjmcneill val |= __SHIFTIN(nvol, mix->mask);
30144ca330bSjmcneill a64_acodec_pr_write(sc, mix->reg, val);
30244ca330bSjmcneill return 0;
30344ca330bSjmcneill
304779f2270Sjmcneill case A64_CODEC_RECORD_MIC1_PREAMP:
305779f2270Sjmcneill if (mc->un.ord < 0 || mc->un.ord > 1)
306779f2270Sjmcneill return EINVAL;
307779f2270Sjmcneill if (mc->un.ord) {
308779f2270Sjmcneill a64_acodec_pr_set_clear(sc, A64_MIC1_CTRL, A64_MIC1AMPEN, 0);
309779f2270Sjmcneill } else {
310779f2270Sjmcneill a64_acodec_pr_set_clear(sc, A64_MIC1_CTRL, 0, A64_MIC1AMPEN);
311779f2270Sjmcneill }
312779f2270Sjmcneill return 0;
313779f2270Sjmcneill
314779f2270Sjmcneill case A64_CODEC_RECORD_MIC2_PREAMP:
315779f2270Sjmcneill if (mc->un.ord < 0 || mc->un.ord > 1)
316779f2270Sjmcneill return EINVAL;
317779f2270Sjmcneill if (mc->un.ord) {
318779f2270Sjmcneill a64_acodec_pr_set_clear(sc, A64_MIC2_CTRL, A64_MIC2AMPEN, 0);
319779f2270Sjmcneill } else {
320779f2270Sjmcneill a64_acodec_pr_set_clear(sc, A64_MIC2_CTRL, 0, A64_MIC2AMPEN);
321779f2270Sjmcneill }
322779f2270Sjmcneill return 0;
323779f2270Sjmcneill
3246584c964Sjmcneill case A64_CODEC_OUTPUT_MUTE:
3256584c964Sjmcneill if (mc->un.ord < 0 || mc->un.ord > 1)
3266584c964Sjmcneill return EINVAL;
3276584c964Sjmcneill if (mc->un.ord) {
3286584c964Sjmcneill a64_acodec_pr_set_clear(sc, A64_OL_MIX_CTRL,
3296584c964Sjmcneill 0, A64_LMIXMUTE_LDAC);
3306584c964Sjmcneill a64_acodec_pr_set_clear(sc, A64_OR_MIX_CTRL,
3316584c964Sjmcneill 0, A64_RMIXMUTE_RDAC);
3326584c964Sjmcneill } else {
3336584c964Sjmcneill a64_acodec_pr_set_clear(sc, A64_OL_MIX_CTRL,
3346584c964Sjmcneill A64_LMIXMUTE_LDAC, 0);
3356584c964Sjmcneill a64_acodec_pr_set_clear(sc, A64_OR_MIX_CTRL,
3366584c964Sjmcneill A64_RMIXMUTE_RDAC, 0);
3376584c964Sjmcneill }
3386584c964Sjmcneill return 0;
3396584c964Sjmcneill
340254f45d9Sjmcneill case A64_CODEC_OUTPUT_SOURCE:
341254f45d9Sjmcneill if (mc->un.mask & A64_OUTPUT_SOURCE_LINE)
342254f45d9Sjmcneill a64_acodec_pr_set_clear(sc, A64_LINEOUT_CTRL0,
343254f45d9Sjmcneill A64_LINEOUT_EN, 0);
344254f45d9Sjmcneill else
345254f45d9Sjmcneill a64_acodec_pr_set_clear(sc, A64_LINEOUT_CTRL0,
346254f45d9Sjmcneill 0, A64_LINEOUT_EN);
347254f45d9Sjmcneill
348254f45d9Sjmcneill if (mc->un.mask & A64_OUTPUT_SOURCE_HP)
349254f45d9Sjmcneill a64_acodec_pr_set_clear(sc, A64_HP_CTRL,
350254f45d9Sjmcneill A64_HPPA_EN, 0);
351254f45d9Sjmcneill else
352254f45d9Sjmcneill a64_acodec_pr_set_clear(sc, A64_HP_CTRL,
353254f45d9Sjmcneill 0, A64_HPPA_EN);
354254f45d9Sjmcneill return 0;
355254f45d9Sjmcneill
35644ca330bSjmcneill case A64_CODEC_RECORD_SOURCE:
35744ca330bSjmcneill a64_acodec_pr_write(sc, A64_L_ADCMIX_SRC, mc->un.mask);
35844ca330bSjmcneill a64_acodec_pr_write(sc, A64_R_ADCMIX_SRC, mc->un.mask);
35944ca330bSjmcneill return 0;
36044ca330bSjmcneill }
36144ca330bSjmcneill
36244ca330bSjmcneill return ENXIO;
36344ca330bSjmcneill }
36444ca330bSjmcneill
36544ca330bSjmcneill static int
a64_acodec_get_port(void * priv,mixer_ctrl_t * mc)36644ca330bSjmcneill a64_acodec_get_port(void *priv, mixer_ctrl_t *mc)
36744ca330bSjmcneill {
36844ca330bSjmcneill struct a64_acodec_softc * const sc = priv;
36944ca330bSjmcneill const struct a64_acodec_mixer *mix;
37044ca330bSjmcneill u_int val, shift;
371254f45d9Sjmcneill int nvol, dev;
37244ca330bSjmcneill
373254f45d9Sjmcneill dev = mc->dev;
374254f45d9Sjmcneill if (dev == A64_CODEC_OUTPUT_MASTER_VOLUME)
375254f45d9Sjmcneill dev = sc->sc_master_dev;
376254f45d9Sjmcneill
377254f45d9Sjmcneill switch (dev) {
378254f45d9Sjmcneill case A64_CODEC_INPUT_LINE_VOLUME:
379254f45d9Sjmcneill case A64_CODEC_INPUT_HP_VOLUME:
380254f45d9Sjmcneill case A64_CODEC_RECORD_LINE_VOLUME:
381254f45d9Sjmcneill case A64_CODEC_RECORD_MIC1_VOLUME:
382254f45d9Sjmcneill case A64_CODEC_RECORD_MIC2_VOLUME:
38344ca330bSjmcneill case A64_CODEC_RECORD_AGC_VOLUME:
384254f45d9Sjmcneill mix = &a64_acodec_mixers[dev];
38544ca330bSjmcneill val = a64_acodec_pr_read(sc, mix->reg);
38644ca330bSjmcneill shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask));
38744ca330bSjmcneill nvol = __SHIFTOUT(val, mix->mask) << shift;
38844ca330bSjmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = nvol;
38944ca330bSjmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = nvol;
39044ca330bSjmcneill return 0;
39144ca330bSjmcneill
392779f2270Sjmcneill case A64_CODEC_RECORD_MIC1_PREAMP:
393779f2270Sjmcneill mc->un.ord = !!(a64_acodec_pr_read(sc, A64_MIC1_CTRL) & A64_MIC1AMPEN);
394779f2270Sjmcneill return 0;
395779f2270Sjmcneill
396779f2270Sjmcneill case A64_CODEC_RECORD_MIC2_PREAMP:
397779f2270Sjmcneill mc->un.ord = !!(a64_acodec_pr_read(sc, A64_MIC2_CTRL) & A64_MIC2AMPEN);
398779f2270Sjmcneill return 0;
399779f2270Sjmcneill
4006584c964Sjmcneill case A64_CODEC_OUTPUT_MUTE:
4016584c964Sjmcneill mc->un.ord = 1;
4026584c964Sjmcneill if (a64_acodec_pr_read(sc, A64_OL_MIX_CTRL) & A64_LMIXMUTE_LDAC)
4036584c964Sjmcneill mc->un.ord = 0;
4046584c964Sjmcneill if (a64_acodec_pr_read(sc, A64_OR_MIX_CTRL) & A64_RMIXMUTE_RDAC)
4056584c964Sjmcneill mc->un.ord = 0;
4066584c964Sjmcneill return 0;
4076584c964Sjmcneill
408254f45d9Sjmcneill case A64_CODEC_OUTPUT_SOURCE:
409254f45d9Sjmcneill mc->un.mask = 0;
410254f45d9Sjmcneill if (a64_acodec_pr_read(sc, A64_LINEOUT_CTRL0) & A64_LINEOUT_EN)
411254f45d9Sjmcneill mc->un.mask |= A64_OUTPUT_SOURCE_LINE;
412254f45d9Sjmcneill if (a64_acodec_pr_read(sc, A64_HP_CTRL) & A64_HPPA_EN)
413254f45d9Sjmcneill mc->un.mask |= A64_OUTPUT_SOURCE_HP;
414254f45d9Sjmcneill return 0;
415254f45d9Sjmcneill
41644ca330bSjmcneill case A64_CODEC_RECORD_SOURCE:
41744ca330bSjmcneill mc->un.mask =
41844ca330bSjmcneill a64_acodec_pr_read(sc, A64_L_ADCMIX_SRC) |
41944ca330bSjmcneill a64_acodec_pr_read(sc, A64_R_ADCMIX_SRC);
42044ca330bSjmcneill return 0;
42144ca330bSjmcneill }
42244ca330bSjmcneill
42344ca330bSjmcneill return ENXIO;
42444ca330bSjmcneill }
42544ca330bSjmcneill
42644ca330bSjmcneill static int
a64_acodec_query_devinfo(void * priv,mixer_devinfo_t * di)42744ca330bSjmcneill a64_acodec_query_devinfo(void *priv, mixer_devinfo_t *di)
42844ca330bSjmcneill {
429254f45d9Sjmcneill struct a64_acodec_softc * const sc = priv;
43044ca330bSjmcneill const struct a64_acodec_mixer *mix;
43144ca330bSjmcneill
43244ca330bSjmcneill switch (di->index) {
43344ca330bSjmcneill case A64_CODEC_OUTPUT_CLASS:
43444ca330bSjmcneill di->mixer_class = di->index;
43544ca330bSjmcneill strcpy(di->label.name, AudioCoutputs);
43644ca330bSjmcneill di->type = AUDIO_MIXER_CLASS;
43744ca330bSjmcneill di->next = di->prev = AUDIO_MIXER_LAST;
43844ca330bSjmcneill return 0;
43944ca330bSjmcneill
44044ca330bSjmcneill case A64_CODEC_INPUT_CLASS:
44144ca330bSjmcneill di->mixer_class = di->index;
44244ca330bSjmcneill strcpy(di->label.name, AudioCinputs);
44344ca330bSjmcneill di->type = AUDIO_MIXER_CLASS;
44444ca330bSjmcneill di->next = di->prev = AUDIO_MIXER_LAST;
44544ca330bSjmcneill return 0;
44644ca330bSjmcneill
44744ca330bSjmcneill case A64_CODEC_RECORD_CLASS:
44844ca330bSjmcneill di->mixer_class = di->index;
44944ca330bSjmcneill strcpy(di->label.name, AudioCrecord);
45044ca330bSjmcneill di->type = AUDIO_MIXER_CLASS;
45144ca330bSjmcneill di->next = di->prev = AUDIO_MIXER_LAST;
45244ca330bSjmcneill return 0;
45344ca330bSjmcneill
45444ca330bSjmcneill case A64_CODEC_OUTPUT_MASTER_VOLUME:
455254f45d9Sjmcneill mix = &a64_acodec_mixers[sc->sc_master_dev];
456254f45d9Sjmcneill di->mixer_class = A64_CODEC_OUTPUT_CLASS;
457254f45d9Sjmcneill strcpy(di->label.name, AudioNmaster);
458254f45d9Sjmcneill di->un.v.delta =
459254f45d9Sjmcneill 256 / (__SHIFTOUT_MASK(mix->mask) + 1);
460254f45d9Sjmcneill di->type = AUDIO_MIXER_VALUE;
461254f45d9Sjmcneill di->next = di->prev = AUDIO_MIXER_LAST;
462254f45d9Sjmcneill di->un.v.num_channels = 2;
463254f45d9Sjmcneill strcpy(di->un.v.units.name, AudioNvolume);
464254f45d9Sjmcneill return 0;
465254f45d9Sjmcneill
466254f45d9Sjmcneill case A64_CODEC_INPUT_LINE_VOLUME:
467254f45d9Sjmcneill case A64_CODEC_INPUT_HP_VOLUME:
468254f45d9Sjmcneill case A64_CODEC_RECORD_LINE_VOLUME:
469254f45d9Sjmcneill case A64_CODEC_RECORD_MIC1_VOLUME:
470254f45d9Sjmcneill case A64_CODEC_RECORD_MIC2_VOLUME:
47144ca330bSjmcneill case A64_CODEC_RECORD_AGC_VOLUME:
47244ca330bSjmcneill mix = &a64_acodec_mixers[di->index];
47344ca330bSjmcneill di->mixer_class = mix->mixer_class;
47444ca330bSjmcneill strcpy(di->label.name, mix->name);
47544ca330bSjmcneill di->un.v.delta =
47644ca330bSjmcneill 256 / (__SHIFTOUT_MASK(mix->mask) + 1);
47744ca330bSjmcneill di->type = AUDIO_MIXER_VALUE;
478779f2270Sjmcneill di->prev = AUDIO_MIXER_LAST;
479779f2270Sjmcneill if (di->index == A64_CODEC_RECORD_MIC1_VOLUME)
480779f2270Sjmcneill di->next = A64_CODEC_RECORD_MIC1_PREAMP;
481779f2270Sjmcneill else if (di->index == A64_CODEC_RECORD_MIC2_VOLUME)
482779f2270Sjmcneill di->next = A64_CODEC_RECORD_MIC2_PREAMP;
483779f2270Sjmcneill else
484779f2270Sjmcneill di->next = AUDIO_MIXER_LAST;
48544ca330bSjmcneill di->un.v.num_channels = 2;
48644ca330bSjmcneill strcpy(di->un.v.units.name, AudioNvolume);
48744ca330bSjmcneill return 0;
48844ca330bSjmcneill
489779f2270Sjmcneill case A64_CODEC_RECORD_MIC1_PREAMP:
490779f2270Sjmcneill case A64_CODEC_RECORD_MIC2_PREAMP:
491779f2270Sjmcneill di->mixer_class = A64_CODEC_RECORD_CLASS;
492779f2270Sjmcneill strcpy(di->label.name, AudioNpreamp);
493779f2270Sjmcneill di->type = AUDIO_MIXER_ENUM;
494779f2270Sjmcneill if (di->index == A64_CODEC_RECORD_MIC1_PREAMP)
495779f2270Sjmcneill di->prev = A64_CODEC_RECORD_MIC1_VOLUME;
496779f2270Sjmcneill else
497779f2270Sjmcneill di->prev = A64_CODEC_RECORD_MIC2_VOLUME;
498779f2270Sjmcneill di->next = AUDIO_MIXER_LAST;
499779f2270Sjmcneill di->un.e.num_mem = 2;
500779f2270Sjmcneill strcpy(di->un.e.member[0].label.name, AudioNoff);
501779f2270Sjmcneill di->un.e.member[0].ord = 0;
502779f2270Sjmcneill strcpy(di->un.e.member[1].label.name, AudioNon);
503779f2270Sjmcneill di->un.e.member[1].ord = 1;
504779f2270Sjmcneill return 0;
505779f2270Sjmcneill
5066584c964Sjmcneill case A64_CODEC_OUTPUT_MUTE:
5076584c964Sjmcneill di->mixer_class = A64_CODEC_OUTPUT_CLASS;
5086584c964Sjmcneill strcpy(di->label.name, AudioNmute);
5096584c964Sjmcneill di->type = AUDIO_MIXER_ENUM;
5106584c964Sjmcneill di->next = di->prev = AUDIO_MIXER_LAST;
5116584c964Sjmcneill di->un.e.num_mem = 2;
5126584c964Sjmcneill strcpy(di->un.e.member[0].label.name, AudioNoff);
5136584c964Sjmcneill di->un.e.member[0].ord = 0;
5146584c964Sjmcneill strcpy(di->un.e.member[1].label.name, AudioNon);
5156584c964Sjmcneill di->un.e.member[1].ord = 1;
5166584c964Sjmcneill return 0;
5176584c964Sjmcneill
518254f45d9Sjmcneill case A64_CODEC_OUTPUT_SOURCE:
519254f45d9Sjmcneill di->mixer_class = A64_CODEC_OUTPUT_CLASS;
520254f45d9Sjmcneill strcpy(di->label.name, AudioNsource);
521254f45d9Sjmcneill di->type = AUDIO_MIXER_SET;
522254f45d9Sjmcneill di->next = di->prev = AUDIO_MIXER_LAST;
523254f45d9Sjmcneill di->un.s.num_mem = 2;
524254f45d9Sjmcneill strcpy(di->un.s.member[0].label.name, AudioNline);
525254f45d9Sjmcneill di->un.s.member[0].mask = A64_OUTPUT_SOURCE_LINE;
526254f45d9Sjmcneill strcpy(di->un.s.member[1].label.name, AudioNheadphone);
527254f45d9Sjmcneill di->un.s.member[1].mask = A64_OUTPUT_SOURCE_HP;
528254f45d9Sjmcneill return 0;
529254f45d9Sjmcneill
53044ca330bSjmcneill case A64_CODEC_RECORD_SOURCE:
53144ca330bSjmcneill di->mixer_class = A64_CODEC_RECORD_CLASS;
53244ca330bSjmcneill strcpy(di->label.name, AudioNsource);
53344ca330bSjmcneill di->type = AUDIO_MIXER_SET;
53444ca330bSjmcneill di->next = di->prev = AUDIO_MIXER_LAST;
53544ca330bSjmcneill di->un.s.num_mem = 4;
53644ca330bSjmcneill strcpy(di->un.s.member[0].label.name, AudioNline);
53744ca330bSjmcneill di->un.s.member[0].mask = A64_ADCMIX_SRC_LINEIN;
53839e8f1a4Sjmcneill strcpy(di->un.s.member[1].label.name, AudioNmicrophone);
53944ca330bSjmcneill di->un.s.member[1].mask = A64_ADCMIX_SRC_MIC1;
54039e8f1a4Sjmcneill strcpy(di->un.s.member[2].label.name, AudioNmicrophone "2");
54144ca330bSjmcneill di->un.s.member[2].mask = A64_ADCMIX_SRC_MIC2;
54244ca330bSjmcneill strcpy(di->un.s.member[3].label.name, AudioNdac);
54344ca330bSjmcneill di->un.s.member[3].mask = A64_ADCMIX_SRC_OMIXER;
54444ca330bSjmcneill return 0;
54544ca330bSjmcneill
54644ca330bSjmcneill }
54744ca330bSjmcneill
54844ca330bSjmcneill return ENXIO;
54944ca330bSjmcneill }
55044ca330bSjmcneill
55144ca330bSjmcneill static const struct audio_hw_if a64_acodec_hw_if = {
55244ca330bSjmcneill .trigger_output = a64_acodec_trigger_output,
55344ca330bSjmcneill .trigger_input = a64_acodec_trigger_input,
55444ca330bSjmcneill .halt_output = a64_acodec_halt_output,
55544ca330bSjmcneill .halt_input = a64_acodec_halt_input,
55644ca330bSjmcneill .set_port = a64_acodec_set_port,
55744ca330bSjmcneill .get_port = a64_acodec_get_port,
55844ca330bSjmcneill .query_devinfo = a64_acodec_query_devinfo,
55944ca330bSjmcneill };
56044ca330bSjmcneill
56144ca330bSjmcneill static audio_dai_tag_t
a64_acodec_dai_get_tag(device_t dev,const void * data,size_t len)56244ca330bSjmcneill a64_acodec_dai_get_tag(device_t dev, const void *data, size_t len)
56344ca330bSjmcneill {
56444ca330bSjmcneill struct a64_acodec_softc * const sc = device_private(dev);
56544ca330bSjmcneill
56644ca330bSjmcneill if (len != 4)
56744ca330bSjmcneill return NULL;
56844ca330bSjmcneill
56944ca330bSjmcneill return &sc->sc_dai;
57044ca330bSjmcneill }
57144ca330bSjmcneill
57244ca330bSjmcneill static struct fdtbus_dai_controller_func a64_acodec_dai_funcs = {
57344ca330bSjmcneill .get_tag = a64_acodec_dai_get_tag
57444ca330bSjmcneill };
57544ca330bSjmcneill
57676bf467cSjmcneill static int
a64_acodec_dai_jack_detect(audio_dai_tag_t dai,u_int jack,int present)57776bf467cSjmcneill a64_acodec_dai_jack_detect(audio_dai_tag_t dai, u_int jack, int present)
57876bf467cSjmcneill {
57976bf467cSjmcneill struct a64_acodec_softc * const sc = audio_dai_private(dai);
58076bf467cSjmcneill
58176bf467cSjmcneill switch (jack) {
58276bf467cSjmcneill case AUDIO_DAI_JACK_HP:
583fe043784Sjmcneill if (present) {
58476bf467cSjmcneill a64_acodec_pr_set_clear(sc, A64_LINEOUT_CTRL0,
585254f45d9Sjmcneill 0, A64_LINEOUT_EN);
586fe043784Sjmcneill a64_acodec_pr_set_clear(sc, A64_HP_CTRL,
587254f45d9Sjmcneill A64_HPPA_EN, 0);
588fe043784Sjmcneill } else {
58976bf467cSjmcneill a64_acodec_pr_set_clear(sc, A64_LINEOUT_CTRL0,
590254f45d9Sjmcneill A64_LINEOUT_EN, 0);
591fe043784Sjmcneill a64_acodec_pr_set_clear(sc, A64_HP_CTRL,
592254f45d9Sjmcneill 0, A64_HPPA_EN);
593fe043784Sjmcneill }
594254f45d9Sjmcneill
595254f45d9Sjmcneill /* Master volume controls either HP or line out */
596254f45d9Sjmcneill sc->sc_master_dev = present ?
597254f45d9Sjmcneill A64_CODEC_INPUT_HP_VOLUME : A64_CODEC_INPUT_LINE_VOLUME;
598254f45d9Sjmcneill
59976bf467cSjmcneill break;
600254f45d9Sjmcneill
60176bf467cSjmcneill case AUDIO_DAI_JACK_MIC:
60276bf467cSjmcneill /* XXX TODO */
60376bf467cSjmcneill break;
60476bf467cSjmcneill }
60576bf467cSjmcneill
60676bf467cSjmcneill return 0;
60776bf467cSjmcneill }
60876bf467cSjmcneill
609*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
610*6e54367aSthorpej { .compat = "allwinner,sun50i-a64-codec-analog" },
611*6e54367aSthorpej DEVICE_COMPAT_EOL
61244ca330bSjmcneill };
61344ca330bSjmcneill
61444ca330bSjmcneill static int
a64_acodec_match(device_t parent,cfdata_t cf,void * aux)61544ca330bSjmcneill a64_acodec_match(device_t parent, cfdata_t cf, void *aux)
61644ca330bSjmcneill {
61744ca330bSjmcneill struct fdt_attach_args * const faa = aux;
61844ca330bSjmcneill
619*6e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
62044ca330bSjmcneill }
62144ca330bSjmcneill
62244ca330bSjmcneill static void
a64_acodec_attach(device_t parent,device_t self,void * aux)62344ca330bSjmcneill a64_acodec_attach(device_t parent, device_t self, void *aux)
62444ca330bSjmcneill {
62544ca330bSjmcneill struct a64_acodec_softc * const sc = device_private(self);
62644ca330bSjmcneill struct fdt_attach_args * const faa = aux;
62744ca330bSjmcneill const int phandle = faa->faa_phandle;
62844ca330bSjmcneill bus_addr_t addr;
62944ca330bSjmcneill bus_size_t size;
63044ca330bSjmcneill
63144ca330bSjmcneill sc->sc_dev = self;
63244ca330bSjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
63344ca330bSjmcneill aprint_error(": couldn't get registers\n");
63444ca330bSjmcneill return;
63544ca330bSjmcneill }
63644ca330bSjmcneill sc->sc_bst = faa->faa_bst;
63744ca330bSjmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
63844ca330bSjmcneill aprint_error(": couldn't map registers\n");
63944ca330bSjmcneill return;
64044ca330bSjmcneill }
64144ca330bSjmcneill
64244ca330bSjmcneill sc->sc_phandle = phandle;
64344ca330bSjmcneill
64444ca330bSjmcneill aprint_naive("\n");
64544ca330bSjmcneill aprint_normal(": A64 Audio Codec (analog part)\n");
64644ca330bSjmcneill
64744ca330bSjmcneill /* Right & Left Headphone PA enable */
64844ca330bSjmcneill a64_acodec_pr_set_clear(sc, A64_HP_CTRL,
64944ca330bSjmcneill A64_HPPA_EN, 0);
650fe043784Sjmcneill
65176bf467cSjmcneill /* Jack detect enable */
652254f45d9Sjmcneill sc->sc_master_dev = A64_CODEC_INPUT_HP_VOLUME;
65376bf467cSjmcneill a64_acodec_pr_set_clear(sc, A64_JACK_MIC_CTRL,
65476bf467cSjmcneill A64_JACKDETEN | A64_INNERRESEN | A64_AUTOPLEN, 0);
65544ca330bSjmcneill
6566584c964Sjmcneill /* Unmute DAC to output mixer */
6576584c964Sjmcneill a64_acodec_pr_set_clear(sc, A64_OL_MIX_CTRL,
6586584c964Sjmcneill A64_LMIXMUTE_LDAC, 0);
6596584c964Sjmcneill a64_acodec_pr_set_clear(sc, A64_OR_MIX_CTRL,
6606584c964Sjmcneill A64_RMIXMUTE_RDAC, 0);
6616584c964Sjmcneill
66276bf467cSjmcneill sc->sc_dai.dai_jack_detect = a64_acodec_dai_jack_detect;
66344ca330bSjmcneill sc->sc_dai.dai_hw_if = &a64_acodec_hw_if;
66444ca330bSjmcneill sc->sc_dai.dai_dev = self;
66544ca330bSjmcneill sc->sc_dai.dai_priv = sc;
66644ca330bSjmcneill fdtbus_register_dai_controller(self, phandle, &a64_acodec_dai_funcs);
66744ca330bSjmcneill }
66844ca330bSjmcneill
66944ca330bSjmcneill CFATTACH_DECL_NEW(a64_acodec, sizeof(struct a64_acodec_softc),
67044ca330bSjmcneill a64_acodec_match, a64_acodec_attach, NULL, NULL);
671