2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2020 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
5 * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
42 #include <sys/resource.h>
43 #include <machine/bus.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/extres/clk/clk.h>
49 #include <dev/extres/hwreset/hwreset.h>
50 #include <dev/extres/regulator/regulator.h>
52 #include "syscon_if.h"
55 #include <dev/sound/pcm/sound.h>
56 #include <dev/sound/fdt/audio_dai.h>
57 #include "audio_dai_if.h"
60 #define A64_PR_CFG 0x00
61 #define A64_AC_PR_RST (1 << 28)
62 #define A64_AC_PR_RW (1 << 24)
63 #define A64_AC_PR_ADDR_MASK (0x1f << 16)
64 #define A64_AC_PR_ADDR(n) (((n) & 0x1f) << 16)
65 #define A64_ACDA_PR_WDAT_MASK (0xff << 8)
66 #define A64_ACDA_PR_WDAT(n) (((n) & 0xff) << 8)
67 #define A64_ACDA_PR_RDAT(n) ((n) & 0xff)
69 #define A64_HP_CTRL 0x00
70 #define A64_HPPA_EN (1 << 6)
71 #define A64_HPVOL_MASK 0x3f
72 #define A64_HPVOL(n) ((n) & 0x3f)
73 #define A64_OL_MIX_CTRL 0x01
74 #define A64_LMIXMUTE_LDAC (1 << 1)
75 #define A64_OR_MIX_CTRL 0x02
76 #define A64_RMIXMUTE_RDAC (1 << 1)
77 #define A64_LINEOUT_CTRL0 0x05
78 #define A64_LINEOUT_LEFT_EN (1 << 7)
79 #define A64_LINEOUT_RIGHT_EN (1 << 6)
80 #define A64_LINEOUT_EN (A64_LINEOUT_LEFT_EN|A64_LINEOUT_RIGHT_EN)
81 #define A64_LINEOUT_CTRL1 0x06
82 #define A64_LINEOUT_VOL __BITS(4,0)
83 #define A64_MIC1_CTRL 0x07
84 #define A64_MIC1G __BITS(6,4)
85 #define A64_MIC1AMPEN (1 << 3)
86 #define A64_MIC1BOOST __BITS(2,0)
87 #define A64_MIC2_CTRL 0x08
88 #define A64_MIC2_SEL (1 << 7)
89 #define A64_MIC2G_MASK (7 << 4)
90 #define A64_MIC2G(n) (((n) & 7) << 4)
91 #define A64_MIC2AMPEN (1 << 3)
92 #define A64_MIC2BOOST_MASK (7 << 0)
93 #define A64_MIC2BOOST(n) (((n) & 7) << 0)
94 #define A64_LINEIN_CTRL 0x09
95 #define A64_LINEING __BITS(6,4)
96 #define A64_MIX_DAC_CTRL 0x0a
97 #define A64_DACAREN (1 << 7)
98 #define A64_DACALEN (1 << 6)
99 #define A64_RMIXEN (1 << 5)
100 #define A64_LMIXEN (1 << 4)
101 #define A64_RHPPAMUTE (1 << 3)
102 #define A64_LHPPAMUTE (1 << 2)
103 #define A64_RHPIS (1 << 1)
104 #define A64_LHPIS (1 << 0)
105 #define A64_L_ADCMIX_SRC 0x0b
106 #define A64_R_ADCMIX_SRC 0x0c
107 #define A64_ADCMIX_SRC_MIC1 (1 << 6)
108 #define A64_ADCMIX_SRC_MIC2 (1 << 5)
109 #define A64_ADCMIX_SRC_LINEIN (1 << 2)
110 #define A64_ADCMIX_SRC_OMIXER (1 << 1)
111 #define A64_ADC_CTRL 0x0d
112 #define A64_ADCREN (1 << 7)
113 #define A64_ADCLEN (1 << 6)
114 #define A64_ADCG __BITS(2,0)
115 #define A64_JACK_MIC_CTRL 0x1d
116 #define A64_JACKDETEN (1 << 7)
117 #define A64_INNERRESEN (1 << 6)
118 #define A64_HMICBIASEN (1 << 5)
119 #define A64_AUTOPLEN (1 << 1)
121 #define A64CODEC_MIXER_DEVS ((1 << SOUND_MIXER_VOLUME) | \
122 (1 << SOUND_MIXER_MIC))
124 static struct ofw_compat_data compat_data[] = {
125 { "allwinner,sun50i-a64-codec-analog", 1},
129 struct a64codec_softc {
131 struct resource *res;
133 u_int regaddr; /* address for the sysctl */
136 #define A64CODEC_LOCK(sc) mtx_lock(&(sc)->mtx)
137 #define A64CODEC_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
138 #define A64CODEC_READ(sc, reg) bus_read_4((sc)->res, (reg))
139 #define A64CODEC_WRITE(sc, reg, val) bus_write_4((sc)->res, (reg), (val))
141 static int a64codec_probe(device_t dev);
142 static int a64codec_attach(device_t dev);
143 static int a64codec_detach(device_t dev);
146 a64_acodec_pr_read(struct a64codec_softc *sc, u_int addr)
150 /* Read current value */
151 val = A64CODEC_READ(sc, A64_PR_CFG);
153 /* De-assert reset */
154 val |= A64_AC_PR_RST;
155 A64CODEC_WRITE(sc, A64_PR_CFG, val);
158 val &= ~A64_AC_PR_RW;
159 A64CODEC_WRITE(sc, A64_PR_CFG, val);
162 val &= ~A64_AC_PR_ADDR_MASK;
163 val |= A64_AC_PR_ADDR(addr);
164 A64CODEC_WRITE(sc, A64_PR_CFG, val);
167 val = A64CODEC_READ(sc, A64_PR_CFG);
168 return A64_ACDA_PR_RDAT(val);
172 a64_acodec_pr_write(struct a64codec_softc *sc, u_int addr, u_int data)
176 /* Read current value */
177 val = A64CODEC_READ(sc, A64_PR_CFG);
179 /* De-assert reset */
180 val |= A64_AC_PR_RST;
181 A64CODEC_WRITE(sc, A64_PR_CFG, val);
184 val &= ~A64_AC_PR_ADDR_MASK;
185 val |= A64_AC_PR_ADDR(addr);
186 A64CODEC_WRITE(sc, A64_PR_CFG, val);
189 val &= ~A64_ACDA_PR_WDAT_MASK;
190 val |= A64_ACDA_PR_WDAT(data);
191 A64CODEC_WRITE(sc, A64_PR_CFG, val);
195 A64CODEC_WRITE(sc, A64_PR_CFG, val);
197 /* Clear write mode */
198 val &= ~A64_AC_PR_RW;
199 A64CODEC_WRITE(sc, A64_PR_CFG, val);
203 a64_acodec_pr_set_clear(struct a64codec_softc *sc, u_int addr, u_int set, u_int clr)
207 old = a64_acodec_pr_read(sc, addr);
208 new = set | (old & ~clr);
209 a64_acodec_pr_write(sc, addr, new);
213 a64codec_probe(device_t dev)
215 if (!ofw_bus_status_okay(dev))
218 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
221 device_set_desc(dev, "Allwinner A64 Analog Codec");
222 return (BUS_PROBE_DEFAULT);
226 a64codec_attach(device_t dev)
228 struct a64codec_softc *sc;
233 sc = device_get_softc(dev);
236 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
239 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
241 device_printf(dev, "cannot allocate resource for device\n");
246 if (regulator_get_by_ofw_property(dev, 0, "cpvdd-supply", ®) == 0) {
247 error = regulator_enable(reg);
249 device_printf(dev, "cannot enable PHY regulator\n");
254 /* Right & Left Headphone PA enable */
255 a64_acodec_pr_set_clear(sc, A64_HP_CTRL,
258 /* Microphone BIAS enable */
259 a64_acodec_pr_set_clear(sc, A64_JACK_MIC_CTRL,
260 A64_HMICBIASEN | A64_INNERRESEN, 0);
262 /* Unmute DAC to output mixer */
263 a64_acodec_pr_set_clear(sc, A64_OL_MIX_CTRL,
264 A64_LMIXMUTE_LDAC, 0);
265 a64_acodec_pr_set_clear(sc, A64_OR_MIX_CTRL,
266 A64_RMIXMUTE_RDAC, 0);
268 /* For now we work only with headphones */
269 a64_acodec_pr_set_clear(sc, A64_LINEOUT_CTRL0,
271 a64_acodec_pr_set_clear(sc, A64_HP_CTRL,
274 u_int val = a64_acodec_pr_read(sc, A64_HP_CTRL);
277 a64_acodec_pr_write(sc, A64_HP_CTRL, val);
279 a64_acodec_pr_set_clear(sc, A64_MIC2_CTRL,
280 A64_MIC2AMPEN | A64_MIC2_SEL | A64_MIC2G(0x3) | A64_MIC2BOOST(0x4),
281 A64_MIC2G_MASK | A64_MIC2BOOST_MASK);
283 a64_acodec_pr_write(sc, A64_L_ADCMIX_SRC,
284 A64_ADCMIX_SRC_MIC2);
285 a64_acodec_pr_write(sc, A64_R_ADCMIX_SRC,
286 A64_ADCMIX_SRC_MIC2);
288 /* Max out MIC2 gain */
289 val = a64_acodec_pr_read(sc, A64_MIC2_CTRL);
294 a64_acodec_pr_write(sc, A64_MIC2_CTRL, val);
296 node = ofw_bus_get_node(dev);
297 OF_device_register_xref(OF_xref_from_node(node), dev);
302 a64codec_detach(dev);
307 a64codec_detach(device_t dev)
309 struct a64codec_softc *sc;
311 sc = device_get_softc(dev);
314 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->res);
315 mtx_destroy(&sc->mtx);
321 a64codec_mixer_init(struct snd_mixer *m)
324 mix_setdevs(m, A64CODEC_MIXER_DEVS);
330 a64codec_mixer_uninit(struct snd_mixer *m)
337 a64codec_mixer_reinit(struct snd_mixer *m)
344 a64codec_mixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
346 struct a64codec_softc *sc;
347 struct mtx *mixer_lock;
351 sc = device_get_softc(mix_getdevinfo(m));
352 mixer_lock = mixer_get_lock(m);
354 if (mtx_owned(mixer_lock)) {
358 mtx_lock(mixer_lock);
365 case SOUND_MIXER_VOLUME:
366 val = a64_acodec_pr_read(sc, A64_HP_CTRL);
367 val &= ~(A64_HPVOL_MASK);
368 val |= A64_HPVOL(left * 63 / 100);
369 a64_acodec_pr_write(sc, A64_HP_CTRL, val);
372 case SOUND_MIXER_MIC:
373 val = a64_acodec_pr_read(sc, A64_MIC2_CTRL);
374 val &= ~(A64_MIC2BOOST_MASK);
375 val |= A64_MIC2BOOST(left * 7 / 100);
376 a64_acodec_pr_write(sc, A64_MIC2_CTRL, val);
384 mtx_unlock(mixer_lock);
387 return (left | (right << 8));
391 a64codec_mixer_setrecsrc(struct snd_mixer *m, unsigned src)
397 static kobj_method_t a64codec_mixer_methods[] = {
398 KOBJMETHOD(mixer_init, a64codec_mixer_init),
399 KOBJMETHOD(mixer_uninit, a64codec_mixer_uninit),
400 KOBJMETHOD(mixer_reinit, a64codec_mixer_reinit),
401 KOBJMETHOD(mixer_set, a64codec_mixer_set),
402 KOBJMETHOD(mixer_setrecsrc, a64codec_mixer_setrecsrc),
406 MIXER_DECLARE(a64codec_mixer);
409 a64codec_dai_init(device_t dev, uint32_t format)
416 a64codec_dai_trigger(device_t dev, int go, int pcm_dir)
418 struct a64codec_softc *sc = device_get_softc(dev);
420 if ((pcm_dir != PCMDIR_PLAY) && (pcm_dir != PCMDIR_REC))
425 if (pcm_dir == PCMDIR_PLAY) {
426 /* Enable DAC analog l/r channels, HP PA, and output mixer */
427 a64_acodec_pr_set_clear(sc, A64_MIX_DAC_CTRL,
428 A64_DACAREN | A64_DACALEN | A64_RMIXEN | A64_LMIXEN |
429 A64_RHPPAMUTE | A64_LHPPAMUTE, 0);
431 else if (pcm_dir == PCMDIR_REC) {
432 /* Enable ADC analog l/r channels */
433 a64_acodec_pr_set_clear(sc, A64_ADC_CTRL,
434 A64_ADCREN | A64_ADCLEN, 0);
440 if (pcm_dir == PCMDIR_PLAY) {
441 /* Disable DAC analog l/r channels, HP PA, and output mixer */
442 a64_acodec_pr_set_clear(sc, A64_MIX_DAC_CTRL,
443 0, A64_DACAREN | A64_DACALEN | A64_RMIXEN | A64_LMIXEN |
444 A64_RHPPAMUTE | A64_LHPPAMUTE);
446 else if (pcm_dir == PCMDIR_REC) {
447 /* Disable ADC analog l/r channels */
448 a64_acodec_pr_set_clear(sc, A64_ADC_CTRL,
449 0, A64_ADCREN | A64_ADCLEN);
458 a64codec_dai_setup_mixer(device_t dev, device_t pcmdev)
461 mixer_init(pcmdev, &a64codec_mixer_class, dev);
466 static device_method_t a64codec_methods[] = {
467 /* Device interface */
468 DEVMETHOD(device_probe, a64codec_probe),
469 DEVMETHOD(device_attach, a64codec_attach),
470 DEVMETHOD(device_detach, a64codec_detach),
472 DEVMETHOD(audio_dai_init, a64codec_dai_init),
473 DEVMETHOD(audio_dai_setup_mixer, a64codec_dai_setup_mixer),
474 DEVMETHOD(audio_dai_trigger, a64codec_dai_trigger),
479 static driver_t a64codec_driver = {
482 sizeof(struct a64codec_softc),
485 DRIVER_MODULE(a64codec, simplebus, a64codec_driver, 0, 0);
486 SIMPLEBUS_PNP_INFO(compat_data);