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
27ba9b7163SAndrew Turner /*
2816025c35SJared McNeill * Allwinner A10/A20 and H3 Audio Codec
29ba9b7163SAndrew Turner */
30ba9b7163SAndrew Turner
31ba9b7163SAndrew Turner #include <sys/param.h>
32ba9b7163SAndrew Turner #include <sys/systm.h>
33ba9b7163SAndrew Turner #include <sys/bus.h>
34ba9b7163SAndrew Turner #include <sys/rman.h>
35ba9b7163SAndrew Turner #include <sys/condvar.h>
36ba9b7163SAndrew Turner #include <sys/kernel.h>
37ba9b7163SAndrew Turner #include <sys/module.h>
38ba9b7163SAndrew Turner #include <sys/gpio.h>
39ba9b7163SAndrew Turner
40ba9b7163SAndrew Turner #include <machine/bus.h>
41ba9b7163SAndrew Turner
42ba9b7163SAndrew Turner #include <dev/sound/pcm/sound.h>
43ba9b7163SAndrew Turner
44ba9b7163SAndrew Turner #include <dev/ofw/ofw_bus.h>
45ba9b7163SAndrew Turner #include <dev/ofw/ofw_bus_subr.h>
46ba9b7163SAndrew Turner
4716025c35SJared McNeill #include <dev/gpio/gpiobusvar.h>
4816025c35SJared McNeill
49be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
501f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
51ba9b7163SAndrew Turner
52ba9b7163SAndrew Turner #include "sunxi_dma_if.h"
53ba9b7163SAndrew Turner #include "mixer_if.h"
5416025c35SJared McNeill
5516025c35SJared McNeill struct a10codec_info;
5616025c35SJared McNeill
5716025c35SJared McNeill struct a10codec_config {
5816025c35SJared McNeill /* mixer class */
5916025c35SJared McNeill struct kobj_class *mixer_class;
6016025c35SJared McNeill
6116025c35SJared McNeill /* toggle DAC/ADC mute */
6216025c35SJared McNeill void (*mute)(struct a10codec_info *, int, int);
6316025c35SJared McNeill
6416025c35SJared McNeill /* DRQ types */
6516025c35SJared McNeill u_int drqtype_codec;
6616025c35SJared McNeill u_int drqtype_sdram;
6716025c35SJared McNeill
6816025c35SJared McNeill /* register map */
6916025c35SJared McNeill bus_size_t DPC,
7016025c35SJared McNeill DAC_FIFOC,
7116025c35SJared McNeill DAC_FIFOS,
7216025c35SJared McNeill DAC_TXDATA,
7316025c35SJared McNeill ADC_FIFOC,
7416025c35SJared McNeill ADC_FIFOS,
7516025c35SJared McNeill ADC_RXDATA,
7616025c35SJared McNeill DAC_CNT,
7716025c35SJared McNeill ADC_CNT;
7816025c35SJared McNeill };
79ba9b7163SAndrew Turner
80ba9b7163SAndrew Turner #define TX_TRIG_LEVEL 0xf
81ba9b7163SAndrew Turner #define RX_TRIG_LEVEL 0x7
82ba9b7163SAndrew Turner #define DRQ_CLR_CNT 0x3
83ba9b7163SAndrew Turner
8416025c35SJared McNeill #define AC_DAC_DPC(_sc) ((_sc)->cfg->DPC)
85ba9b7163SAndrew Turner #define DAC_DPC_EN_DA 0x80000000
8616025c35SJared McNeill #define AC_DAC_FIFOC(_sc) ((_sc)->cfg->DAC_FIFOC)
87ba9b7163SAndrew Turner #define DAC_FIFOC_FS_SHIFT 29
88ba9b7163SAndrew Turner #define DAC_FIFOC_FS_MASK (7U << DAC_FIFOC_FS_SHIFT)
89ba9b7163SAndrew Turner #define DAC_FS_48KHZ 0
90ba9b7163SAndrew Turner #define DAC_FS_32KHZ 1
91ba9b7163SAndrew Turner #define DAC_FS_24KHZ 2
92ba9b7163SAndrew Turner #define DAC_FS_16KHZ 3
93ba9b7163SAndrew Turner #define DAC_FS_12KHZ 4
94ba9b7163SAndrew Turner #define DAC_FS_8KHZ 5
95ba9b7163SAndrew Turner #define DAC_FS_192KHZ 6
96ba9b7163SAndrew Turner #define DAC_FS_96KHZ 7
97ba9b7163SAndrew Turner #define DAC_FIFOC_FIFO_MODE_SHIFT 24
98ba9b7163SAndrew Turner #define DAC_FIFOC_FIFO_MODE_MASK (3U << DAC_FIFOC_FIFO_MODE_SHIFT)
99ba9b7163SAndrew Turner #define FIFO_MODE_24_31_8 0
100ba9b7163SAndrew Turner #define FIFO_MODE_16_31_16 0
101ba9b7163SAndrew Turner #define FIFO_MODE_16_15_0 1
102ba9b7163SAndrew Turner #define DAC_FIFOC_DRQ_CLR_CNT_SHIFT 21
103ba9b7163SAndrew Turner #define DAC_FIFOC_DRQ_CLR_CNT_MASK (3U << DAC_FIFOC_DRQ_CLR_CNT_SHIFT)
104ba9b7163SAndrew Turner #define DAC_FIFOC_TX_TRIG_LEVEL_SHIFT 8
105ba9b7163SAndrew Turner #define DAC_FIFOC_TX_TRIG_LEVEL_MASK (0x7f << DAC_FIFOC_TX_TRIG_LEVEL_SHIFT)
106ba9b7163SAndrew Turner #define DAC_FIFOC_MONO_EN (1U << 6)
107ba9b7163SAndrew Turner #define DAC_FIFOC_TX_BITS (1U << 5)
108ba9b7163SAndrew Turner #define DAC_FIFOC_DRQ_EN (1U << 4)
109ba9b7163SAndrew Turner #define DAC_FIFOC_FIFO_FLUSH (1U << 0)
11016025c35SJared McNeill #define AC_DAC_FIFOS(_sc) ((_sc)->cfg->DAC_FIFOS)
11116025c35SJared McNeill #define AC_DAC_TXDATA(_sc) ((_sc)->cfg->DAC_TXDATA)
11216025c35SJared McNeill #define AC_ADC_FIFOC(_sc) ((_sc)->cfg->ADC_FIFOC)
113ba9b7163SAndrew Turner #define ADC_FIFOC_FS_SHIFT 29
114ba9b7163SAndrew Turner #define ADC_FIFOC_FS_MASK (7U << ADC_FIFOC_FS_SHIFT)
115ba9b7163SAndrew Turner #define ADC_FS_48KHZ 0
116ba9b7163SAndrew Turner #define ADC_FIFOC_EN_AD (1U << 28)
117ba9b7163SAndrew Turner #define ADC_FIFOC_RX_FIFO_MODE (1U << 24)
118ba9b7163SAndrew Turner #define ADC_FIFOC_RX_TRIG_LEVEL_SHIFT 8
119ba9b7163SAndrew Turner #define ADC_FIFOC_RX_TRIG_LEVEL_MASK (0x1f << ADC_FIFOC_RX_TRIG_LEVEL_SHIFT)
120ba9b7163SAndrew Turner #define ADC_FIFOC_MONO_EN (1U << 7)
121ba9b7163SAndrew Turner #define ADC_FIFOC_RX_BITS (1U << 6)
122ba9b7163SAndrew Turner #define ADC_FIFOC_DRQ_EN (1U << 4)
123ba9b7163SAndrew Turner #define ADC_FIFOC_FIFO_FLUSH (1U << 1)
12416025c35SJared McNeill #define AC_ADC_FIFOS(_sc) ((_sc)->cfg->ADC_FIFOS)
12516025c35SJared McNeill #define AC_ADC_RXDATA(_sc) ((_sc)->cfg->ADC_RXDATA)
12616025c35SJared McNeill #define AC_DAC_CNT(_sc) ((_sc)->cfg->DAC_CNT)
12716025c35SJared McNeill #define AC_ADC_CNT(_sc) ((_sc)->cfg->ADC_CNT)
128ba9b7163SAndrew Turner
129ba9b7163SAndrew Turner static uint32_t a10codec_fmt[] = {
130ba9b7163SAndrew Turner SND_FORMAT(AFMT_S16_LE, 1, 0),
131ba9b7163SAndrew Turner SND_FORMAT(AFMT_S16_LE, 2, 0),
132ba9b7163SAndrew Turner 0
133ba9b7163SAndrew Turner };
134ba9b7163SAndrew Turner
135ba9b7163SAndrew Turner static struct pcmchan_caps a10codec_pcaps = { 8000, 192000, a10codec_fmt, 0 };
136ba9b7163SAndrew Turner static struct pcmchan_caps a10codec_rcaps = { 8000, 48000, a10codec_fmt, 0 };
137ba9b7163SAndrew Turner
138ba9b7163SAndrew Turner struct a10codec_info;
139ba9b7163SAndrew Turner
140ba9b7163SAndrew Turner struct a10codec_chinfo {
141ba9b7163SAndrew Turner struct snd_dbuf *buffer;
142ba9b7163SAndrew Turner struct pcm_channel *channel;
143ba9b7163SAndrew Turner struct a10codec_info *parent;
144ba9b7163SAndrew Turner bus_dmamap_t dmamap;
145ba9b7163SAndrew Turner void *dmaaddr;
146ba9b7163SAndrew Turner bus_addr_t physaddr;
147ba9b7163SAndrew Turner bus_size_t fifo;
148ba9b7163SAndrew Turner device_t dmac;
149ba9b7163SAndrew Turner void *dmachan;
150ba9b7163SAndrew Turner
151ba9b7163SAndrew Turner int dir;
152ba9b7163SAndrew Turner int run;
153ba9b7163SAndrew Turner uint32_t pos;
154ba9b7163SAndrew Turner uint32_t format;
155ba9b7163SAndrew Turner uint32_t blocksize;
156ba9b7163SAndrew Turner uint32_t speed;
157ba9b7163SAndrew Turner };
158ba9b7163SAndrew Turner
159ba9b7163SAndrew Turner struct a10codec_info {
160ba9b7163SAndrew Turner device_t dev;
161bfcf888aSEmmanuel Vadot struct resource *res[2];
162ba9b7163SAndrew Turner struct mtx *lock;
163ba9b7163SAndrew Turner bus_dma_tag_t dmat;
164ba9b7163SAndrew Turner unsigned dmasize;
165ba9b7163SAndrew Turner void *ih;
166ba9b7163SAndrew Turner
16716025c35SJared McNeill struct a10codec_config *cfg;
168ba9b7163SAndrew Turner
169ba9b7163SAndrew Turner struct a10codec_chinfo play;
170ba9b7163SAndrew Turner struct a10codec_chinfo rec;
171ba9b7163SAndrew Turner };
172ba9b7163SAndrew Turner
173ba9b7163SAndrew Turner static struct resource_spec a10codec_spec[] = {
174ba9b7163SAndrew Turner { SYS_RES_MEMORY, 0, RF_ACTIVE },
175ba9b7163SAndrew Turner { -1, 0 }
176ba9b7163SAndrew Turner };
177ba9b7163SAndrew Turner
178bfcf888aSEmmanuel Vadot #define CODEC_ANALOG_READ(sc, reg) bus_read_4((sc)->res[1], (reg))
179bfcf888aSEmmanuel Vadot #define CODEC_ANALOG_WRITE(sc, reg, val) bus_write_4((sc)->res[1], (reg), (val))
180bfcf888aSEmmanuel Vadot
181ba9b7163SAndrew Turner #define CODEC_READ(sc, reg) bus_read_4((sc)->res[0], (reg))
182ba9b7163SAndrew Turner #define CODEC_WRITE(sc, reg, val) bus_write_4((sc)->res[0], (reg), (val))
183ba9b7163SAndrew Turner
184ba9b7163SAndrew Turner /*
18516025c35SJared McNeill * A10/A20 mixer interface
186ba9b7163SAndrew Turner */
187ba9b7163SAndrew Turner
18816025c35SJared McNeill #define A10_DAC_ACTL 0x10
18916025c35SJared McNeill #define A10_DACAREN (1U << 31)
19016025c35SJared McNeill #define A10_DACALEN (1U << 30)
19116025c35SJared McNeill #define A10_MIXEN (1U << 29)
19216025c35SJared McNeill #define A10_DACPAS (1U << 8)
19316025c35SJared McNeill #define A10_PAMUTE (1U << 6)
19416025c35SJared McNeill #define A10_PAVOL_SHIFT 0
19516025c35SJared McNeill #define A10_PAVOL_MASK (0x3f << A10_PAVOL_SHIFT)
19616025c35SJared McNeill #define A10_ADC_ACTL 0x28
19716025c35SJared McNeill #define A10_ADCREN (1U << 31)
19816025c35SJared McNeill #define A10_ADCLEN (1U << 30)
19916025c35SJared McNeill #define A10_PREG1EN (1U << 29)
20016025c35SJared McNeill #define A10_PREG2EN (1U << 28)
20116025c35SJared McNeill #define A10_VMICEN (1U << 27)
20216025c35SJared McNeill #define A10_ADCG_SHIFT 20
20316025c35SJared McNeill #define A10_ADCG_MASK (7U << A10_ADCG_SHIFT)
20416025c35SJared McNeill #define A10_ADCIS_SHIFT 17
20516025c35SJared McNeill #define A10_ADCIS_MASK (7U << A10_ADCIS_SHIFT)
20616025c35SJared McNeill #define A10_ADC_IS_LINEIN 0
20716025c35SJared McNeill #define A10_ADC_IS_FMIN 1
20816025c35SJared McNeill #define A10_ADC_IS_MIC1 2
20916025c35SJared McNeill #define A10_ADC_IS_MIC2 3
21016025c35SJared McNeill #define A10_ADC_IS_MIC1_L_MIC2_R 4
21116025c35SJared McNeill #define A10_ADC_IS_MIC1_LR_MIC2_LR 5
21216025c35SJared McNeill #define A10_ADC_IS_OMIX 6
21316025c35SJared McNeill #define A10_ADC_IS_LINEIN_L_MIC1_R 7
21416025c35SJared McNeill #define A10_LNRDF (1U << 16)
21516025c35SJared McNeill #define A10_LNPREG_SHIFT 13
21616025c35SJared McNeill #define A10_LNPREG_MASK (7U << A10_LNPREG_SHIFT)
21716025c35SJared McNeill #define A10_PA_EN (1U << 4)
21816025c35SJared McNeill #define A10_DDE (1U << 3)
21916025c35SJared McNeill
220ba9b7163SAndrew Turner static int
a10_mixer_init(struct snd_mixer * m)22116025c35SJared McNeill a10_mixer_init(struct snd_mixer *m)
222ba9b7163SAndrew Turner {
223ba9b7163SAndrew Turner struct a10codec_info *sc = mix_getdevinfo(m);
224ba9b7163SAndrew Turner uint32_t val;
225ba9b7163SAndrew Turner
226ba9b7163SAndrew Turner mix_setdevs(m, SOUND_MASK_VOLUME | SOUND_MASK_LINE | SOUND_MASK_RECLEV);
227ba9b7163SAndrew Turner mix_setrecdevs(m, SOUND_MASK_LINE | SOUND_MASK_LINE1 | SOUND_MASK_MIC);
228ba9b7163SAndrew Turner
229ba9b7163SAndrew Turner /* Unmute input source to PA */
23016025c35SJared McNeill val = CODEC_READ(sc, A10_DAC_ACTL);
23116025c35SJared McNeill val |= A10_PAMUTE;
23216025c35SJared McNeill CODEC_WRITE(sc, A10_DAC_ACTL, val);
233ba9b7163SAndrew Turner
234ba9b7163SAndrew Turner /* Enable PA */
23516025c35SJared McNeill val = CODEC_READ(sc, A10_ADC_ACTL);
23616025c35SJared McNeill val |= A10_PA_EN;
23716025c35SJared McNeill CODEC_WRITE(sc, A10_ADC_ACTL, val);
238ba9b7163SAndrew Turner
239ba9b7163SAndrew Turner return (0);
240ba9b7163SAndrew Turner }
241ba9b7163SAndrew Turner
24216025c35SJared McNeill static const struct a10_mixer {
243ba9b7163SAndrew Turner unsigned reg;
244ba9b7163SAndrew Turner unsigned mask;
245ba9b7163SAndrew Turner unsigned shift;
24616025c35SJared McNeill } a10_mixers[SOUND_MIXER_NRDEVICES] = {
24716025c35SJared McNeill [SOUND_MIXER_VOLUME] = { A10_DAC_ACTL, A10_PAVOL_MASK,
24816025c35SJared McNeill A10_PAVOL_SHIFT },
24916025c35SJared McNeill [SOUND_MIXER_LINE] = { A10_ADC_ACTL, A10_LNPREG_MASK,
25016025c35SJared McNeill A10_LNPREG_SHIFT },
25116025c35SJared McNeill [SOUND_MIXER_RECLEV] = { A10_ADC_ACTL, A10_ADCG_MASK,
25216025c35SJared McNeill A10_ADCG_SHIFT },
253ba9b7163SAndrew Turner };
254ba9b7163SAndrew Turner
255ba9b7163SAndrew Turner static int
a10_mixer_set(struct snd_mixer * m,unsigned dev,unsigned left,unsigned right)25616025c35SJared McNeill a10_mixer_set(struct snd_mixer *m, unsigned dev, unsigned left,
257ba9b7163SAndrew Turner unsigned right)
258ba9b7163SAndrew Turner {
259ba9b7163SAndrew Turner struct a10codec_info *sc = mix_getdevinfo(m);
260ba9b7163SAndrew Turner uint32_t val;
261ba9b7163SAndrew Turner unsigned nvol, max;
262ba9b7163SAndrew Turner
26316025c35SJared McNeill max = a10_mixers[dev].mask >> a10_mixers[dev].shift;
264ba9b7163SAndrew Turner nvol = (left * max) / 100;
265ba9b7163SAndrew Turner
26616025c35SJared McNeill val = CODEC_READ(sc, a10_mixers[dev].reg);
26716025c35SJared McNeill val &= ~a10_mixers[dev].mask;
26816025c35SJared McNeill val |= (nvol << a10_mixers[dev].shift);
26916025c35SJared McNeill CODEC_WRITE(sc, a10_mixers[dev].reg, val);
270ba9b7163SAndrew Turner
271ba9b7163SAndrew Turner left = right = (left * 100) / max;
272ba9b7163SAndrew Turner return (left | (right << 8));
273ba9b7163SAndrew Turner }
274ba9b7163SAndrew Turner
275ba9b7163SAndrew Turner static uint32_t
a10_mixer_setrecsrc(struct snd_mixer * m,uint32_t src)27616025c35SJared McNeill a10_mixer_setrecsrc(struct snd_mixer *m, uint32_t src)
277ba9b7163SAndrew Turner {
278ba9b7163SAndrew Turner struct a10codec_info *sc = mix_getdevinfo(m);
279ba9b7163SAndrew Turner uint32_t val;
280ba9b7163SAndrew Turner
28116025c35SJared McNeill val = CODEC_READ(sc, A10_ADC_ACTL);
282ba9b7163SAndrew Turner
283ba9b7163SAndrew Turner switch (src) {
284ba9b7163SAndrew Turner case SOUND_MASK_LINE: /* line-in */
28516025c35SJared McNeill val &= ~A10_ADCIS_MASK;
28616025c35SJared McNeill val |= (A10_ADC_IS_LINEIN << A10_ADCIS_SHIFT);
287ba9b7163SAndrew Turner break;
288ba9b7163SAndrew Turner case SOUND_MASK_MIC: /* MIC1 */
28916025c35SJared McNeill val &= ~A10_ADCIS_MASK;
29016025c35SJared McNeill val |= (A10_ADC_IS_MIC1 << A10_ADCIS_SHIFT);
291ba9b7163SAndrew Turner break;
292ba9b7163SAndrew Turner case SOUND_MASK_LINE1: /* MIC2 */
29316025c35SJared McNeill val &= ~A10_ADCIS_MASK;
29416025c35SJared McNeill val |= (A10_ADC_IS_MIC2 << A10_ADCIS_SHIFT);
295ba9b7163SAndrew Turner break;
296ba9b7163SAndrew Turner default:
297ba9b7163SAndrew Turner break;
298ba9b7163SAndrew Turner }
299ba9b7163SAndrew Turner
30016025c35SJared McNeill CODEC_WRITE(sc, A10_ADC_ACTL, val);
301ba9b7163SAndrew Turner
30216025c35SJared McNeill switch ((val & A10_ADCIS_MASK) >> A10_ADCIS_SHIFT) {
30316025c35SJared McNeill case A10_ADC_IS_LINEIN:
304ba9b7163SAndrew Turner return (SOUND_MASK_LINE);
30516025c35SJared McNeill case A10_ADC_IS_MIC1:
306ba9b7163SAndrew Turner return (SOUND_MASK_MIC);
30716025c35SJared McNeill case A10_ADC_IS_MIC2:
308ba9b7163SAndrew Turner return (SOUND_MASK_LINE1);
309ba9b7163SAndrew Turner default:
310ba9b7163SAndrew Turner return (0);
311ba9b7163SAndrew Turner }
312ba9b7163SAndrew Turner }
313ba9b7163SAndrew Turner
31416025c35SJared McNeill static void
a10_mute(struct a10codec_info * sc,int mute,int dir)31516025c35SJared McNeill a10_mute(struct a10codec_info *sc, int mute, int dir)
31616025c35SJared McNeill {
31716025c35SJared McNeill uint32_t val;
31816025c35SJared McNeill
31916025c35SJared McNeill if (dir == PCMDIR_PLAY) {
32016025c35SJared McNeill val = CODEC_READ(sc, A10_DAC_ACTL);
32116025c35SJared McNeill if (mute) {
32216025c35SJared McNeill /* Disable DAC analog l/r channels and output mixer */
32316025c35SJared McNeill val &= ~A10_DACAREN;
32416025c35SJared McNeill val &= ~A10_DACALEN;
32516025c35SJared McNeill val &= ~A10_DACPAS;
32616025c35SJared McNeill } else {
32716025c35SJared McNeill /* Enable DAC analog l/r channels and output mixer */
32816025c35SJared McNeill val |= A10_DACAREN;
32916025c35SJared McNeill val |= A10_DACALEN;
33016025c35SJared McNeill val |= A10_DACPAS;
33116025c35SJared McNeill }
33216025c35SJared McNeill CODEC_WRITE(sc, A10_DAC_ACTL, val);
33316025c35SJared McNeill } else {
33416025c35SJared McNeill val = CODEC_READ(sc, A10_ADC_ACTL);
33516025c35SJared McNeill if (mute) {
33616025c35SJared McNeill /* Disable ADC analog l/r channels, MIC1 preamp,
33716025c35SJared McNeill * and VMIC pin voltage
33816025c35SJared McNeill */
33916025c35SJared McNeill val &= ~A10_ADCREN;
34016025c35SJared McNeill val &= ~A10_ADCLEN;
34116025c35SJared McNeill val &= ~A10_PREG1EN;
34216025c35SJared McNeill val &= ~A10_VMICEN;
34316025c35SJared McNeill } else {
34416025c35SJared McNeill /* Enable ADC analog l/r channels, MIC1 preamp,
34516025c35SJared McNeill * and VMIC pin voltage
34616025c35SJared McNeill */
34716025c35SJared McNeill val |= A10_ADCREN;
34816025c35SJared McNeill val |= A10_ADCLEN;
34916025c35SJared McNeill val |= A10_PREG1EN;
35016025c35SJared McNeill val |= A10_VMICEN;
35116025c35SJared McNeill }
35216025c35SJared McNeill CODEC_WRITE(sc, A10_ADC_ACTL, val);
35316025c35SJared McNeill }
35416025c35SJared McNeill }
35516025c35SJared McNeill
35616025c35SJared McNeill static kobj_method_t a10_mixer_methods[] = {
35716025c35SJared McNeill KOBJMETHOD(mixer_init, a10_mixer_init),
35816025c35SJared McNeill KOBJMETHOD(mixer_set, a10_mixer_set),
35916025c35SJared McNeill KOBJMETHOD(mixer_setrecsrc, a10_mixer_setrecsrc),
360ba9b7163SAndrew Turner KOBJMETHOD_END
361ba9b7163SAndrew Turner };
36216025c35SJared McNeill MIXER_DECLARE(a10_mixer);
36316025c35SJared McNeill
36416025c35SJared McNeill /*
36516025c35SJared McNeill * H3 mixer interface
36616025c35SJared McNeill */
36716025c35SJared McNeill
36816025c35SJared McNeill #define H3_PR_CFG 0x00
369bfcf888aSEmmanuel Vadot #define H3_AC_PR_RST (1 << 28)
37016025c35SJared McNeill #define H3_AC_PR_RW (1 << 24)
37116025c35SJared McNeill #define H3_AC_PR_ADDR_SHIFT 16
37216025c35SJared McNeill #define H3_AC_PR_ADDR_MASK (0x1f << H3_AC_PR_ADDR_SHIFT)
37316025c35SJared McNeill #define H3_ACDA_PR_WDAT_SHIFT 8
37416025c35SJared McNeill #define H3_ACDA_PR_WDAT_MASK (0xff << H3_ACDA_PR_WDAT_SHIFT)
37516025c35SJared McNeill #define H3_ACDA_PR_RDAT_SHIFT 0
37616025c35SJared McNeill #define H3_ACDA_PR_RDAT_MASK (0xff << H3_ACDA_PR_RDAT_SHIFT)
37716025c35SJared McNeill
37816025c35SJared McNeill #define H3_LOMIXSC 0x01
37916025c35SJared McNeill #define H3_LOMIXSC_LDAC (1 << 1)
38016025c35SJared McNeill #define H3_ROMIXSC 0x02
38116025c35SJared McNeill #define H3_ROMIXSC_RDAC (1 << 1)
38216025c35SJared McNeill #define H3_DAC_PA_SRC 0x03
38316025c35SJared McNeill #define H3_DACAREN (1 << 7)
38416025c35SJared McNeill #define H3_DACALEN (1 << 6)
38516025c35SJared McNeill #define H3_RMIXEN (1 << 5)
38616025c35SJared McNeill #define H3_LMIXEN (1 << 4)
38716025c35SJared McNeill #define H3_LINEIN_GCTR 0x05
38816025c35SJared McNeill #define H3_LINEING_SHIFT 4
38916025c35SJared McNeill #define H3_LINEING_MASK (0x7 << H3_LINEING_SHIFT)
39016025c35SJared McNeill #define H3_MIC_GCTR 0x06
39116025c35SJared McNeill #define H3_MIC1_GAIN_SHIFT 4
39216025c35SJared McNeill #define H3_MIC1_GAIN_MASK (0x7 << H3_MIC1_GAIN_SHIFT)
39316025c35SJared McNeill #define H3_MIC2_GAIN_SHIFT 0
39416025c35SJared McNeill #define H3_MIC2_GAIN_MASK (0x7 << H3_MIC2_GAIN_SHIFT)
39516025c35SJared McNeill #define H3_PAEN_CTR 0x07
39616025c35SJared McNeill #define H3_LINEOUTEN (1 << 7)
39716025c35SJared McNeill #define H3_LINEOUT_VOLC 0x09
39816025c35SJared McNeill #define H3_LINEOUTVOL_SHIFT 3
39916025c35SJared McNeill #define H3_LINEOUTVOL_MASK (0x1f << H3_LINEOUTVOL_SHIFT)
40016025c35SJared McNeill #define H3_MIC2G_LINEOUT_CTR 0x0a
40116025c35SJared McNeill #define H3_LINEOUT_LSEL (1 << 3)
40216025c35SJared McNeill #define H3_LINEOUT_RSEL (1 << 2)
40316025c35SJared McNeill #define H3_LADCMIXSC 0x0c
40416025c35SJared McNeill #define H3_RADCMIXSC 0x0d
40516025c35SJared McNeill #define H3_ADCMIXSC_MIC1 (1 << 6)
40616025c35SJared McNeill #define H3_ADCMIXSC_MIC2 (1 << 5)
40716025c35SJared McNeill #define H3_ADCMIXSC_LINEIN (1 << 2)
40816025c35SJared McNeill #define H3_ADCMIXSC_OMIXER (3 << 0)
40916025c35SJared McNeill #define H3_ADC_AP_EN 0x0f
41016025c35SJared McNeill #define H3_ADCREN (1 << 7)
41116025c35SJared McNeill #define H3_ADCLEN (1 << 6)
41216025c35SJared McNeill #define H3_ADCG_SHIFT 0
41316025c35SJared McNeill #define H3_ADCG_MASK (0x7 << H3_ADCG_SHIFT)
41416025c35SJared McNeill
41516025c35SJared McNeill static u_int
h3_pr_read(struct a10codec_info * sc,u_int addr)41616025c35SJared McNeill h3_pr_read(struct a10codec_info *sc, u_int addr)
41716025c35SJared McNeill {
41816025c35SJared McNeill uint32_t val;
41916025c35SJared McNeill
42016025c35SJared McNeill /* Read current value */
421bfcf888aSEmmanuel Vadot val = CODEC_ANALOG_READ(sc, H3_PR_CFG);
42216025c35SJared McNeill
42316025c35SJared McNeill /* De-assert reset */
42416025c35SJared McNeill val |= H3_AC_PR_RST;
425bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val);
42616025c35SJared McNeill
42716025c35SJared McNeill /* Read mode */
42816025c35SJared McNeill val &= ~H3_AC_PR_RW;
429bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val);
43016025c35SJared McNeill
43116025c35SJared McNeill /* Set address */
43216025c35SJared McNeill val &= ~H3_AC_PR_ADDR_MASK;
43316025c35SJared McNeill val |= (addr << H3_AC_PR_ADDR_SHIFT);
434bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val);
43516025c35SJared McNeill
43616025c35SJared McNeill /* Read data */
437bfcf888aSEmmanuel Vadot return (CODEC_ANALOG_READ(sc , H3_PR_CFG) & H3_ACDA_PR_RDAT_MASK);
43816025c35SJared McNeill }
43916025c35SJared McNeill
44016025c35SJared McNeill static void
h3_pr_write(struct a10codec_info * sc,u_int addr,u_int data)44116025c35SJared McNeill h3_pr_write(struct a10codec_info *sc, u_int addr, u_int data)
44216025c35SJared McNeill {
44316025c35SJared McNeill uint32_t val;
44416025c35SJared McNeill
44516025c35SJared McNeill /* Read current value */
446bfcf888aSEmmanuel Vadot val = CODEC_ANALOG_READ(sc, H3_PR_CFG);
44716025c35SJared McNeill
44816025c35SJared McNeill /* De-assert reset */
44916025c35SJared McNeill val |= H3_AC_PR_RST;
450bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val);
45116025c35SJared McNeill
45216025c35SJared McNeill /* Set address */
45316025c35SJared McNeill val &= ~H3_AC_PR_ADDR_MASK;
45416025c35SJared McNeill val |= (addr << H3_AC_PR_ADDR_SHIFT);
455bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val);
45616025c35SJared McNeill
45716025c35SJared McNeill /* Write data */
45816025c35SJared McNeill val &= ~H3_ACDA_PR_WDAT_MASK;
45916025c35SJared McNeill val |= (data << H3_ACDA_PR_WDAT_SHIFT);
460bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val);
46116025c35SJared McNeill
46216025c35SJared McNeill /* Write mode */
46316025c35SJared McNeill val |= H3_AC_PR_RW;
464bfcf888aSEmmanuel Vadot CODEC_ANALOG_WRITE(sc, H3_PR_CFG, val);
46516025c35SJared McNeill }
46616025c35SJared McNeill
46716025c35SJared McNeill static void
h3_pr_set_clear(struct a10codec_info * sc,u_int addr,u_int set,u_int clr)46816025c35SJared McNeill h3_pr_set_clear(struct a10codec_info *sc, u_int addr, u_int set, u_int clr)
46916025c35SJared McNeill {
47016025c35SJared McNeill u_int old, new;
47116025c35SJared McNeill
47216025c35SJared McNeill old = h3_pr_read(sc, addr);
47316025c35SJared McNeill new = set | (old & ~clr);
47416025c35SJared McNeill h3_pr_write(sc, addr, new);
47516025c35SJared McNeill }
47616025c35SJared McNeill
47716025c35SJared McNeill static int
h3_mixer_init(struct snd_mixer * m)47816025c35SJared McNeill h3_mixer_init(struct snd_mixer *m)
47916025c35SJared McNeill {
480bfcf888aSEmmanuel Vadot int rid=1;
481bfcf888aSEmmanuel Vadot pcell_t reg[2];
482bfcf888aSEmmanuel Vadot phandle_t analogref;
48316025c35SJared McNeill struct a10codec_info *sc = mix_getdevinfo(m);
48416025c35SJared McNeill
485bfcf888aSEmmanuel Vadot if (OF_getencprop(ofw_bus_get_node(sc->dev), "allwinner,codec-analog-controls",
486bfcf888aSEmmanuel Vadot &analogref, sizeof(analogref)) <= 0) {
487bfcf888aSEmmanuel Vadot return (ENXIO);
488bfcf888aSEmmanuel Vadot }
489bfcf888aSEmmanuel Vadot
490bfcf888aSEmmanuel Vadot if (OF_getencprop(OF_node_from_xref(analogref), "reg",
491bfcf888aSEmmanuel Vadot reg, sizeof(reg)) <= 0) {
492bfcf888aSEmmanuel Vadot return (ENXIO);
493bfcf888aSEmmanuel Vadot }
494bfcf888aSEmmanuel Vadot
495bfcf888aSEmmanuel Vadot sc->res[1] = bus_alloc_resource(sc->dev, SYS_RES_MEMORY, &rid, reg[0],
496bfcf888aSEmmanuel Vadot reg[0]+reg[1], reg[1], RF_ACTIVE );
497bfcf888aSEmmanuel Vadot
498bfcf888aSEmmanuel Vadot if (sc->res[1] == NULL) {
499bfcf888aSEmmanuel Vadot return (ENXIO);
500bfcf888aSEmmanuel Vadot }
501bfcf888aSEmmanuel Vadot
50216025c35SJared McNeill mix_setdevs(m, SOUND_MASK_PCM | SOUND_MASK_VOLUME | SOUND_MASK_RECLEV |
50316025c35SJared McNeill SOUND_MASK_MIC | SOUND_MASK_LINE | SOUND_MASK_LINE1);
50416025c35SJared McNeill mix_setrecdevs(m, SOUND_MASK_MIC | SOUND_MASK_LINE | SOUND_MASK_LINE1 |
50516025c35SJared McNeill SOUND_MASK_IMIX);
50616025c35SJared McNeill
50716025c35SJared McNeill pcm_setflags(sc->dev, pcm_getflags(sc->dev) | SD_F_SOFTPCMVOL);
50816025c35SJared McNeill
50916025c35SJared McNeill /* Right & Left LINEOUT enable */
51016025c35SJared McNeill h3_pr_set_clear(sc, H3_PAEN_CTR, H3_LINEOUTEN, 0);
51116025c35SJared McNeill h3_pr_set_clear(sc, H3_MIC2G_LINEOUT_CTR,
51216025c35SJared McNeill H3_LINEOUT_LSEL | H3_LINEOUT_RSEL, 0);
51316025c35SJared McNeill
51416025c35SJared McNeill return (0);
51516025c35SJared McNeill }
51616025c35SJared McNeill
51716025c35SJared McNeill static const struct h3_mixer {
51816025c35SJared McNeill unsigned reg;
51916025c35SJared McNeill unsigned mask;
52016025c35SJared McNeill unsigned shift;
52116025c35SJared McNeill } h3_mixers[SOUND_MIXER_NRDEVICES] = {
52216025c35SJared McNeill [SOUND_MIXER_VOLUME] = { H3_LINEOUT_VOLC, H3_LINEOUTVOL_MASK,
52316025c35SJared McNeill H3_LINEOUTVOL_SHIFT },
52416025c35SJared McNeill [SOUND_MIXER_RECLEV] = { H3_ADC_AP_EN, H3_ADCG_MASK,
52516025c35SJared McNeill H3_ADCG_SHIFT },
52616025c35SJared McNeill [SOUND_MIXER_LINE] = { H3_LINEIN_GCTR, H3_LINEING_MASK,
52716025c35SJared McNeill H3_LINEING_SHIFT },
52816025c35SJared McNeill [SOUND_MIXER_MIC] = { H3_MIC_GCTR, H3_MIC1_GAIN_MASK,
52916025c35SJared McNeill H3_MIC1_GAIN_SHIFT },
53016025c35SJared McNeill [SOUND_MIXER_LINE1] = { H3_MIC_GCTR, H3_MIC2_GAIN_MASK,
53116025c35SJared McNeill H3_MIC2_GAIN_SHIFT },
53216025c35SJared McNeill };
53316025c35SJared McNeill
53416025c35SJared McNeill static int
h3_mixer_set(struct snd_mixer * m,unsigned dev,unsigned left,unsigned right)53516025c35SJared McNeill h3_mixer_set(struct snd_mixer *m, unsigned dev, unsigned left,
53616025c35SJared McNeill unsigned right)
53716025c35SJared McNeill {
53816025c35SJared McNeill struct a10codec_info *sc = mix_getdevinfo(m);
53916025c35SJared McNeill unsigned nvol, max;
54016025c35SJared McNeill
54116025c35SJared McNeill max = h3_mixers[dev].mask >> h3_mixers[dev].shift;
54216025c35SJared McNeill nvol = (left * max) / 100;
54316025c35SJared McNeill
54416025c35SJared McNeill h3_pr_set_clear(sc, h3_mixers[dev].reg,
54516025c35SJared McNeill nvol << h3_mixers[dev].shift, h3_mixers[dev].mask);
54616025c35SJared McNeill
54716025c35SJared McNeill left = right = (left * 100) / max;
54816025c35SJared McNeill return (left | (right << 8));
54916025c35SJared McNeill }
55016025c35SJared McNeill
55116025c35SJared McNeill static uint32_t
h3_mixer_setrecsrc(struct snd_mixer * m,uint32_t src)55216025c35SJared McNeill h3_mixer_setrecsrc(struct snd_mixer *m, uint32_t src)
55316025c35SJared McNeill {
55416025c35SJared McNeill struct a10codec_info *sc = mix_getdevinfo(m);
55516025c35SJared McNeill uint32_t val;
55616025c35SJared McNeill
55716025c35SJared McNeill val = 0;
55816025c35SJared McNeill src &= (SOUND_MASK_LINE | SOUND_MASK_MIC |
55916025c35SJared McNeill SOUND_MASK_LINE1 | SOUND_MASK_IMIX);
56016025c35SJared McNeill
56116025c35SJared McNeill if ((src & SOUND_MASK_LINE) != 0) /* line-in */
56216025c35SJared McNeill val |= H3_ADCMIXSC_LINEIN;
56316025c35SJared McNeill if ((src & SOUND_MASK_MIC) != 0) /* MIC1 */
56416025c35SJared McNeill val |= H3_ADCMIXSC_MIC1;
56516025c35SJared McNeill if ((src & SOUND_MASK_LINE1) != 0) /* MIC2 */
56616025c35SJared McNeill val |= H3_ADCMIXSC_MIC2;
56716025c35SJared McNeill if ((src & SOUND_MASK_IMIX) != 0) /* l/r output mixer */
56816025c35SJared McNeill val |= H3_ADCMIXSC_OMIXER;
56916025c35SJared McNeill
57016025c35SJared McNeill h3_pr_write(sc, H3_LADCMIXSC, val);
57116025c35SJared McNeill h3_pr_write(sc, H3_RADCMIXSC, val);
57216025c35SJared McNeill
57316025c35SJared McNeill return (src);
57416025c35SJared McNeill }
57516025c35SJared McNeill
57616025c35SJared McNeill static void
h3_mute(struct a10codec_info * sc,int mute,int dir)57716025c35SJared McNeill h3_mute(struct a10codec_info *sc, int mute, int dir)
57816025c35SJared McNeill {
57916025c35SJared McNeill if (dir == PCMDIR_PLAY) {
58016025c35SJared McNeill if (mute) {
58116025c35SJared McNeill /* Mute DAC l/r channels to output mixer */
58216025c35SJared McNeill h3_pr_set_clear(sc, H3_LOMIXSC, 0, H3_LOMIXSC_LDAC);
58316025c35SJared McNeill h3_pr_set_clear(sc, H3_ROMIXSC, 0, H3_ROMIXSC_RDAC);
58416025c35SJared McNeill /* Disable DAC analog l/r channels and output mixer */
58516025c35SJared McNeill h3_pr_set_clear(sc, H3_DAC_PA_SRC,
58616025c35SJared McNeill 0, H3_DACAREN | H3_DACALEN | H3_RMIXEN | H3_LMIXEN);
58716025c35SJared McNeill } else {
58816025c35SJared McNeill /* Enable DAC analog l/r channels and output mixer */
58916025c35SJared McNeill h3_pr_set_clear(sc, H3_DAC_PA_SRC,
59016025c35SJared McNeill H3_DACAREN | H3_DACALEN | H3_RMIXEN | H3_LMIXEN, 0);
59116025c35SJared McNeill /* Unmute DAC l/r channels to output mixer */
59216025c35SJared McNeill h3_pr_set_clear(sc, H3_LOMIXSC, H3_LOMIXSC_LDAC, 0);
59316025c35SJared McNeill h3_pr_set_clear(sc, H3_ROMIXSC, H3_ROMIXSC_RDAC, 0);
59416025c35SJared McNeill }
59516025c35SJared McNeill } else {
59616025c35SJared McNeill if (mute) {
59716025c35SJared McNeill /* Disable ADC analog l/r channels */
59816025c35SJared McNeill h3_pr_set_clear(sc, H3_ADC_AP_EN,
59916025c35SJared McNeill 0, H3_ADCREN | H3_ADCLEN);
60016025c35SJared McNeill } else {
60116025c35SJared McNeill /* Enable ADC analog l/r channels */
60216025c35SJared McNeill h3_pr_set_clear(sc, H3_ADC_AP_EN,
60316025c35SJared McNeill H3_ADCREN | H3_ADCLEN, 0);
60416025c35SJared McNeill }
60516025c35SJared McNeill }
60616025c35SJared McNeill }
60716025c35SJared McNeill
60816025c35SJared McNeill static kobj_method_t h3_mixer_methods[] = {
60916025c35SJared McNeill KOBJMETHOD(mixer_init, h3_mixer_init),
61016025c35SJared McNeill KOBJMETHOD(mixer_set, h3_mixer_set),
61116025c35SJared McNeill KOBJMETHOD(mixer_setrecsrc, h3_mixer_setrecsrc),
61216025c35SJared McNeill KOBJMETHOD_END
61316025c35SJared McNeill };
61416025c35SJared McNeill MIXER_DECLARE(h3_mixer);
615ba9b7163SAndrew Turner
616ba9b7163SAndrew Turner /*
617ba9b7163SAndrew Turner * Channel interface
618ba9b7163SAndrew Turner */
619ba9b7163SAndrew Turner
620ba9b7163SAndrew Turner static void
a10codec_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)621ba9b7163SAndrew Turner a10codec_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
622ba9b7163SAndrew Turner {
623ba9b7163SAndrew Turner struct a10codec_chinfo *ch = arg;
624ba9b7163SAndrew Turner
625ba9b7163SAndrew Turner if (error != 0)
626ba9b7163SAndrew Turner return;
627ba9b7163SAndrew Turner
628ba9b7163SAndrew Turner ch->physaddr = segs[0].ds_addr;
629ba9b7163SAndrew Turner }
630ba9b7163SAndrew Turner
631ba9b7163SAndrew Turner static void
a10codec_transfer(struct a10codec_chinfo * ch)632ba9b7163SAndrew Turner a10codec_transfer(struct a10codec_chinfo *ch)
633ba9b7163SAndrew Turner {
634ba9b7163SAndrew Turner bus_addr_t src, dst;
635ba9b7163SAndrew Turner int error;
636ba9b7163SAndrew Turner
637ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) {
638ba9b7163SAndrew Turner src = ch->physaddr + ch->pos;
639ba9b7163SAndrew Turner dst = ch->fifo;
640ba9b7163SAndrew Turner } else {
641ba9b7163SAndrew Turner src = ch->fifo;
642ba9b7163SAndrew Turner dst = ch->physaddr + ch->pos;
643ba9b7163SAndrew Turner }
644ba9b7163SAndrew Turner
645ba9b7163SAndrew Turner error = SUNXI_DMA_TRANSFER(ch->dmac, ch->dmachan, src, dst,
646ba9b7163SAndrew Turner ch->blocksize);
647ba9b7163SAndrew Turner if (error) {
648ba9b7163SAndrew Turner ch->run = 0;
649ba9b7163SAndrew Turner device_printf(ch->parent->dev, "DMA transfer failed: %d\n",
650ba9b7163SAndrew Turner error);
651ba9b7163SAndrew Turner }
652ba9b7163SAndrew Turner }
653ba9b7163SAndrew Turner
654ba9b7163SAndrew Turner static void
a10codec_dmaconfig(struct a10codec_chinfo * ch)655ba9b7163SAndrew Turner a10codec_dmaconfig(struct a10codec_chinfo *ch)
656ba9b7163SAndrew Turner {
657ba9b7163SAndrew Turner struct a10codec_info *sc = ch->parent;
658ba9b7163SAndrew Turner struct sunxi_dma_config conf;
659ba9b7163SAndrew Turner
660ba9b7163SAndrew Turner memset(&conf, 0, sizeof(conf));
661ba9b7163SAndrew Turner conf.src_width = conf.dst_width = 16;
662ba9b7163SAndrew Turner conf.src_burst_len = conf.dst_burst_len = 4;
663ba9b7163SAndrew Turner
664ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) {
665ba9b7163SAndrew Turner conf.dst_noincr = true;
66616025c35SJared McNeill conf.src_drqtype = sc->cfg->drqtype_sdram;
66716025c35SJared McNeill conf.dst_drqtype = sc->cfg->drqtype_codec;
668ba9b7163SAndrew Turner } else {
669ba9b7163SAndrew Turner conf.src_noincr = true;
67016025c35SJared McNeill conf.src_drqtype = sc->cfg->drqtype_codec;
67116025c35SJared McNeill conf.dst_drqtype = sc->cfg->drqtype_sdram;
672ba9b7163SAndrew Turner }
673ba9b7163SAndrew Turner
674ba9b7163SAndrew Turner SUNXI_DMA_SET_CONFIG(ch->dmac, ch->dmachan, &conf);
675ba9b7163SAndrew Turner }
676ba9b7163SAndrew Turner
677ba9b7163SAndrew Turner static void
a10codec_dmaintr(void * priv)678ba9b7163SAndrew Turner a10codec_dmaintr(void *priv)
679ba9b7163SAndrew Turner {
680ba9b7163SAndrew Turner struct a10codec_chinfo *ch = priv;
681ba9b7163SAndrew Turner unsigned bufsize;
682ba9b7163SAndrew Turner
683ba9b7163SAndrew Turner bufsize = sndbuf_getsize(ch->buffer);
684ba9b7163SAndrew Turner
685ba9b7163SAndrew Turner ch->pos += ch->blocksize;
686ba9b7163SAndrew Turner if (ch->pos >= bufsize)
687ba9b7163SAndrew Turner ch->pos -= bufsize;
688ba9b7163SAndrew Turner
689ba9b7163SAndrew Turner if (ch->run) {
690ba9b7163SAndrew Turner chn_intr(ch->channel);
691ba9b7163SAndrew Turner a10codec_transfer(ch);
692ba9b7163SAndrew Turner }
693ba9b7163SAndrew Turner }
694ba9b7163SAndrew Turner
695ba9b7163SAndrew Turner static unsigned
a10codec_fs(struct a10codec_chinfo * ch)696ba9b7163SAndrew Turner a10codec_fs(struct a10codec_chinfo *ch)
697ba9b7163SAndrew Turner {
698ba9b7163SAndrew Turner switch (ch->speed) {
699ba9b7163SAndrew Turner case 48000:
700ba9b7163SAndrew Turner return (DAC_FS_48KHZ);
701ba9b7163SAndrew Turner case 24000:
702ba9b7163SAndrew Turner return (DAC_FS_24KHZ);
703ba9b7163SAndrew Turner case 12000:
704ba9b7163SAndrew Turner return (DAC_FS_12KHZ);
705ba9b7163SAndrew Turner case 192000:
706ba9b7163SAndrew Turner return (DAC_FS_192KHZ);
707ba9b7163SAndrew Turner case 32000:
708ba9b7163SAndrew Turner return (DAC_FS_32KHZ);
709ba9b7163SAndrew Turner case 16000:
710ba9b7163SAndrew Turner return (DAC_FS_16KHZ);
711ba9b7163SAndrew Turner case 8000:
712ba9b7163SAndrew Turner return (DAC_FS_8KHZ);
713ba9b7163SAndrew Turner case 96000:
714ba9b7163SAndrew Turner return (DAC_FS_96KHZ);
715ba9b7163SAndrew Turner default:
716ba9b7163SAndrew Turner return (DAC_FS_48KHZ);
717ba9b7163SAndrew Turner }
718ba9b7163SAndrew Turner }
719ba9b7163SAndrew Turner
720ba9b7163SAndrew Turner static void
a10codec_start(struct a10codec_chinfo * ch)721ba9b7163SAndrew Turner a10codec_start(struct a10codec_chinfo *ch)
722ba9b7163SAndrew Turner {
723ba9b7163SAndrew Turner struct a10codec_info *sc = ch->parent;
724ba9b7163SAndrew Turner uint32_t val;
725ba9b7163SAndrew Turner
726ba9b7163SAndrew Turner ch->pos = 0;
727ba9b7163SAndrew Turner
728ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) {
729ba9b7163SAndrew Turner /* Flush DAC FIFO */
73016025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), DAC_FIFOC_FIFO_FLUSH);
731ba9b7163SAndrew Turner
732ba9b7163SAndrew Turner /* Clear DAC FIFO status */
73316025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_FIFOS(sc),
73416025c35SJared McNeill CODEC_READ(sc, AC_DAC_FIFOS(sc)));
735ba9b7163SAndrew Turner
73616025c35SJared McNeill /* Unmute output */
73716025c35SJared McNeill sc->cfg->mute(sc, 0, ch->dir);
738ba9b7163SAndrew Turner
739ba9b7163SAndrew Turner /* Configure DAC DMA channel */
740ba9b7163SAndrew Turner a10codec_dmaconfig(ch);
741ba9b7163SAndrew Turner
742ba9b7163SAndrew Turner /* Configure DAC FIFO */
74316025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_FIFOC(sc),
744ba9b7163SAndrew Turner (AFMT_CHANNEL(ch->format) == 1 ? DAC_FIFOC_MONO_EN : 0) |
745ba9b7163SAndrew Turner (a10codec_fs(ch) << DAC_FIFOC_FS_SHIFT) |
746ba9b7163SAndrew Turner (FIFO_MODE_16_15_0 << DAC_FIFOC_FIFO_MODE_SHIFT) |
747ba9b7163SAndrew Turner (DRQ_CLR_CNT << DAC_FIFOC_DRQ_CLR_CNT_SHIFT) |
748ba9b7163SAndrew Turner (TX_TRIG_LEVEL << DAC_FIFOC_TX_TRIG_LEVEL_SHIFT));
749ba9b7163SAndrew Turner
750ba9b7163SAndrew Turner /* Enable DAC DRQ */
75116025c35SJared McNeill val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
752ba9b7163SAndrew Turner val |= DAC_FIFOC_DRQ_EN;
75316025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val);
754ba9b7163SAndrew Turner } else {
755ba9b7163SAndrew Turner /* Flush ADC FIFO */
75616025c35SJared McNeill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), ADC_FIFOC_FIFO_FLUSH);
757ba9b7163SAndrew Turner
758ba9b7163SAndrew Turner /* Clear ADC FIFO status */
75916025c35SJared McNeill CODEC_WRITE(sc, AC_ADC_FIFOS(sc),
76016025c35SJared McNeill CODEC_READ(sc, AC_ADC_FIFOS(sc)));
761ba9b7163SAndrew Turner
76216025c35SJared McNeill /* Unmute input */
76316025c35SJared McNeill sc->cfg->mute(sc, 0, ch->dir);
764ba9b7163SAndrew Turner
765ba9b7163SAndrew Turner /* Configure ADC DMA channel */
766ba9b7163SAndrew Turner a10codec_dmaconfig(ch);
767ba9b7163SAndrew Turner
768ba9b7163SAndrew Turner /* Configure ADC FIFO */
76916025c35SJared McNeill CODEC_WRITE(sc, AC_ADC_FIFOC(sc),
770ba9b7163SAndrew Turner ADC_FIFOC_EN_AD |
771ba9b7163SAndrew Turner ADC_FIFOC_RX_FIFO_MODE |
772ba9b7163SAndrew Turner (AFMT_CHANNEL(ch->format) == 1 ? ADC_FIFOC_MONO_EN : 0) |
773ba9b7163SAndrew Turner (a10codec_fs(ch) << ADC_FIFOC_FS_SHIFT) |
774ba9b7163SAndrew Turner (RX_TRIG_LEVEL << ADC_FIFOC_RX_TRIG_LEVEL_SHIFT));
775ba9b7163SAndrew Turner
776ba9b7163SAndrew Turner /* Enable ADC DRQ */
77716025c35SJared McNeill val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
778ba9b7163SAndrew Turner val |= ADC_FIFOC_DRQ_EN;
77916025c35SJared McNeill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val);
780ba9b7163SAndrew Turner }
781ba9b7163SAndrew Turner
782ba9b7163SAndrew Turner /* Start DMA transfer */
783ba9b7163SAndrew Turner a10codec_transfer(ch);
784ba9b7163SAndrew Turner }
785ba9b7163SAndrew Turner
786ba9b7163SAndrew Turner static void
a10codec_stop(struct a10codec_chinfo * ch)787ba9b7163SAndrew Turner a10codec_stop(struct a10codec_chinfo *ch)
788ba9b7163SAndrew Turner {
789ba9b7163SAndrew Turner struct a10codec_info *sc = ch->parent;
790ba9b7163SAndrew Turner
791ba9b7163SAndrew Turner /* Disable DMA channel */
792ba9b7163SAndrew Turner SUNXI_DMA_HALT(ch->dmac, ch->dmachan);
793ba9b7163SAndrew Turner
79416025c35SJared McNeill sc->cfg->mute(sc, 1, ch->dir);
79516025c35SJared McNeill
796ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) {
797ba9b7163SAndrew Turner /* Disable DAC DRQ */
79816025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), 0);
799ba9b7163SAndrew Turner } else {
800ba9b7163SAndrew Turner /* Disable ADC DRQ */
80116025c35SJared McNeill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), 0);
802ba9b7163SAndrew Turner }
803ba9b7163SAndrew Turner }
804ba9b7163SAndrew Turner
805ba9b7163SAndrew Turner static void *
a10codec_chan_init(kobj_t obj,void * devinfo,struct snd_dbuf * b,struct pcm_channel * c,int dir)806ba9b7163SAndrew Turner a10codec_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
807ba9b7163SAndrew Turner struct pcm_channel *c, int dir)
808ba9b7163SAndrew Turner {
809ba9b7163SAndrew Turner struct a10codec_info *sc = devinfo;
810ba9b7163SAndrew Turner struct a10codec_chinfo *ch = dir == PCMDIR_PLAY ? &sc->play : &sc->rec;
81116025c35SJared McNeill phandle_t xref;
81216025c35SJared McNeill pcell_t *cells;
81316025c35SJared McNeill int ncells, error;
81416025c35SJared McNeill
81516025c35SJared McNeill error = ofw_bus_parse_xref_list_alloc(ofw_bus_get_node(sc->dev),
81616025c35SJared McNeill "dmas", "#dma-cells", dir == PCMDIR_PLAY ? 1 : 0,
81716025c35SJared McNeill &xref, &ncells, &cells);
81816025c35SJared McNeill if (error != 0) {
81916025c35SJared McNeill device_printf(sc->dev, "cannot parse 'dmas' property\n");
82016025c35SJared McNeill return (NULL);
82116025c35SJared McNeill }
82216025c35SJared McNeill OF_prop_free(cells);
823ba9b7163SAndrew Turner
824ba9b7163SAndrew Turner ch->parent = sc;
825ba9b7163SAndrew Turner ch->channel = c;
826ba9b7163SAndrew Turner ch->buffer = b;
827ba9b7163SAndrew Turner ch->dir = dir;
828ba9b7163SAndrew Turner ch->fifo = rman_get_start(sc->res[0]) +
82916025c35SJared McNeill (dir == PCMDIR_REC ? AC_ADC_RXDATA(sc) : AC_DAC_TXDATA(sc));
830ba9b7163SAndrew Turner
83116025c35SJared McNeill ch->dmac = OF_device_from_xref(xref);
832ba9b7163SAndrew Turner if (ch->dmac == NULL) {
833ba9b7163SAndrew Turner device_printf(sc->dev, "cannot find DMA controller\n");
83416025c35SJared McNeill device_printf(sc->dev, "xref = 0x%x\n", (u_int)xref);
835ba9b7163SAndrew Turner return (NULL);
836ba9b7163SAndrew Turner }
837ba9b7163SAndrew Turner ch->dmachan = SUNXI_DMA_ALLOC(ch->dmac, false, a10codec_dmaintr, ch);
838ba9b7163SAndrew Turner if (ch->dmachan == NULL) {
839ba9b7163SAndrew Turner device_printf(sc->dev, "cannot allocate DMA channel\n");
840ba9b7163SAndrew Turner return (NULL);
841ba9b7163SAndrew Turner }
842ba9b7163SAndrew Turner
843ba9b7163SAndrew Turner error = bus_dmamem_alloc(sc->dmat, &ch->dmaaddr,
844ba9b7163SAndrew Turner BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &ch->dmamap);
845ba9b7163SAndrew Turner if (error != 0) {
846ba9b7163SAndrew Turner device_printf(sc->dev, "cannot allocate channel buffer\n");
847ba9b7163SAndrew Turner return (NULL);
848ba9b7163SAndrew Turner }
849ba9b7163SAndrew Turner error = bus_dmamap_load(sc->dmat, ch->dmamap, ch->dmaaddr,
850ba9b7163SAndrew Turner sc->dmasize, a10codec_dmamap_cb, ch, BUS_DMA_NOWAIT);
851ba9b7163SAndrew Turner if (error != 0) {
852ba9b7163SAndrew Turner device_printf(sc->dev, "cannot load DMA map\n");
853ba9b7163SAndrew Turner return (NULL);
854ba9b7163SAndrew Turner }
855ba9b7163SAndrew Turner memset(ch->dmaaddr, 0, sc->dmasize);
856ba9b7163SAndrew Turner
857ba9b7163SAndrew Turner if (sndbuf_setup(ch->buffer, ch->dmaaddr, sc->dmasize) != 0) {
858ba9b7163SAndrew Turner device_printf(sc->dev, "cannot setup sndbuf\n");
859ba9b7163SAndrew Turner return (NULL);
860ba9b7163SAndrew Turner }
861ba9b7163SAndrew Turner
862ba9b7163SAndrew Turner return (ch);
863ba9b7163SAndrew Turner }
864ba9b7163SAndrew Turner
865ba9b7163SAndrew Turner static int
a10codec_chan_free(kobj_t obj,void * data)866ba9b7163SAndrew Turner a10codec_chan_free(kobj_t obj, void *data)
867ba9b7163SAndrew Turner {
868ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data;
869ba9b7163SAndrew Turner struct a10codec_info *sc = ch->parent;
870ba9b7163SAndrew Turner
871ba9b7163SAndrew Turner SUNXI_DMA_FREE(ch->dmac, ch->dmachan);
872ba9b7163SAndrew Turner bus_dmamap_unload(sc->dmat, ch->dmamap);
873ba9b7163SAndrew Turner bus_dmamem_free(sc->dmat, ch->dmaaddr, ch->dmamap);
874ba9b7163SAndrew Turner
875ba9b7163SAndrew Turner return (0);
876ba9b7163SAndrew Turner }
877ba9b7163SAndrew Turner
878ba9b7163SAndrew Turner static int
a10codec_chan_setformat(kobj_t obj,void * data,uint32_t format)879ba9b7163SAndrew Turner a10codec_chan_setformat(kobj_t obj, void *data, uint32_t format)
880ba9b7163SAndrew Turner {
881ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data;
882ba9b7163SAndrew Turner
883ba9b7163SAndrew Turner ch->format = format;
884ba9b7163SAndrew Turner
885ba9b7163SAndrew Turner return (0);
886ba9b7163SAndrew Turner }
887ba9b7163SAndrew Turner
888ba9b7163SAndrew Turner static uint32_t
a10codec_chan_setspeed(kobj_t obj,void * data,uint32_t speed)889ba9b7163SAndrew Turner a10codec_chan_setspeed(kobj_t obj, void *data, uint32_t speed)
890ba9b7163SAndrew Turner {
891ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data;
892ba9b7163SAndrew Turner
893ba9b7163SAndrew Turner /*
894ba9b7163SAndrew Turner * The codec supports full duplex operation but both DAC and ADC
895ba9b7163SAndrew Turner * use the same source clock (PLL2). Limit the available speeds to
896ba9b7163SAndrew Turner * those supported by a 24576000 Hz input.
897ba9b7163SAndrew Turner */
898ba9b7163SAndrew Turner switch (speed) {
899ba9b7163SAndrew Turner case 8000:
900ba9b7163SAndrew Turner case 12000:
901ba9b7163SAndrew Turner case 16000:
902ba9b7163SAndrew Turner case 24000:
903ba9b7163SAndrew Turner case 32000:
904ba9b7163SAndrew Turner case 48000:
905ba9b7163SAndrew Turner ch->speed = speed;
906ba9b7163SAndrew Turner break;
907ba9b7163SAndrew Turner case 96000:
908ba9b7163SAndrew Turner case 192000:
909ba9b7163SAndrew Turner /* 96 KHz / 192 KHz mode only supported for playback */
910ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) {
911ba9b7163SAndrew Turner ch->speed = speed;
912ba9b7163SAndrew Turner } else {
913ba9b7163SAndrew Turner ch->speed = 48000;
914ba9b7163SAndrew Turner }
915ba9b7163SAndrew Turner break;
916ba9b7163SAndrew Turner case 44100:
917ba9b7163SAndrew Turner ch->speed = 48000;
918ba9b7163SAndrew Turner break;
919ba9b7163SAndrew Turner case 22050:
920ba9b7163SAndrew Turner ch->speed = 24000;
921ba9b7163SAndrew Turner break;
922ba9b7163SAndrew Turner case 11025:
923ba9b7163SAndrew Turner ch->speed = 12000;
924ba9b7163SAndrew Turner break;
925ba9b7163SAndrew Turner default:
926ba9b7163SAndrew Turner ch->speed = 48000;
927ba9b7163SAndrew Turner break;
928ba9b7163SAndrew Turner }
929ba9b7163SAndrew Turner
930ba9b7163SAndrew Turner return (ch->speed);
931ba9b7163SAndrew Turner }
932ba9b7163SAndrew Turner
933ba9b7163SAndrew Turner static uint32_t
a10codec_chan_setblocksize(kobj_t obj,void * data,uint32_t blocksize)934ba9b7163SAndrew Turner a10codec_chan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
935ba9b7163SAndrew Turner {
936ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data;
937ba9b7163SAndrew Turner
938ba9b7163SAndrew Turner ch->blocksize = blocksize & ~3;
939ba9b7163SAndrew Turner
940ba9b7163SAndrew Turner return (ch->blocksize);
941ba9b7163SAndrew Turner }
942ba9b7163SAndrew Turner
943ba9b7163SAndrew Turner static int
a10codec_chan_trigger(kobj_t obj,void * data,int go)944ba9b7163SAndrew Turner a10codec_chan_trigger(kobj_t obj, void *data, int go)
945ba9b7163SAndrew Turner {
946ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data;
947ba9b7163SAndrew Turner struct a10codec_info *sc = ch->parent;
948ba9b7163SAndrew Turner
949ba9b7163SAndrew Turner if (!PCMTRIG_COMMON(go))
950ba9b7163SAndrew Turner return (0);
951ba9b7163SAndrew Turner
952ba9b7163SAndrew Turner snd_mtxlock(sc->lock);
953ba9b7163SAndrew Turner switch (go) {
954ba9b7163SAndrew Turner case PCMTRIG_START:
955ba9b7163SAndrew Turner ch->run = 1;
956bfcf888aSEmmanuel Vadot a10codec_stop(ch);
957ba9b7163SAndrew Turner a10codec_start(ch);
958ba9b7163SAndrew Turner break;
959ba9b7163SAndrew Turner case PCMTRIG_STOP:
960ba9b7163SAndrew Turner case PCMTRIG_ABORT:
961ba9b7163SAndrew Turner ch->run = 0;
962ba9b7163SAndrew Turner a10codec_stop(ch);
963ba9b7163SAndrew Turner break;
964ba9b7163SAndrew Turner default:
965ba9b7163SAndrew Turner break;
966ba9b7163SAndrew Turner }
967ba9b7163SAndrew Turner snd_mtxunlock(sc->lock);
968ba9b7163SAndrew Turner
969ba9b7163SAndrew Turner return (0);
970ba9b7163SAndrew Turner }
971ba9b7163SAndrew Turner
972ba9b7163SAndrew Turner static uint32_t
a10codec_chan_getptr(kobj_t obj,void * data)973ba9b7163SAndrew Turner a10codec_chan_getptr(kobj_t obj, void *data)
974ba9b7163SAndrew Turner {
975ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data;
976ba9b7163SAndrew Turner
977ba9b7163SAndrew Turner return (ch->pos);
978ba9b7163SAndrew Turner }
979ba9b7163SAndrew Turner
980ba9b7163SAndrew Turner static struct pcmchan_caps *
a10codec_chan_getcaps(kobj_t obj,void * data)981ba9b7163SAndrew Turner a10codec_chan_getcaps(kobj_t obj, void *data)
982ba9b7163SAndrew Turner {
983ba9b7163SAndrew Turner struct a10codec_chinfo *ch = data;
984ba9b7163SAndrew Turner
985ba9b7163SAndrew Turner if (ch->dir == PCMDIR_PLAY) {
986ba9b7163SAndrew Turner return (&a10codec_pcaps);
987ba9b7163SAndrew Turner } else {
988ba9b7163SAndrew Turner return (&a10codec_rcaps);
989ba9b7163SAndrew Turner }
990ba9b7163SAndrew Turner }
991ba9b7163SAndrew Turner
992ba9b7163SAndrew Turner static kobj_method_t a10codec_chan_methods[] = {
993ba9b7163SAndrew Turner KOBJMETHOD(channel_init, a10codec_chan_init),
994ba9b7163SAndrew Turner KOBJMETHOD(channel_free, a10codec_chan_free),
995ba9b7163SAndrew Turner KOBJMETHOD(channel_setformat, a10codec_chan_setformat),
996ba9b7163SAndrew Turner KOBJMETHOD(channel_setspeed, a10codec_chan_setspeed),
997ba9b7163SAndrew Turner KOBJMETHOD(channel_setblocksize, a10codec_chan_setblocksize),
998ba9b7163SAndrew Turner KOBJMETHOD(channel_trigger, a10codec_chan_trigger),
999ba9b7163SAndrew Turner KOBJMETHOD(channel_getptr, a10codec_chan_getptr),
1000ba9b7163SAndrew Turner KOBJMETHOD(channel_getcaps, a10codec_chan_getcaps),
1001ba9b7163SAndrew Turner KOBJMETHOD_END
1002ba9b7163SAndrew Turner };
1003ba9b7163SAndrew Turner CHANNEL_DECLARE(a10codec_chan);
1004ba9b7163SAndrew Turner
1005ba9b7163SAndrew Turner /*
1006ba9b7163SAndrew Turner * Device interface
1007ba9b7163SAndrew Turner */
1008ba9b7163SAndrew Turner
100916025c35SJared McNeill static const struct a10codec_config a10_config = {
101016025c35SJared McNeill .mixer_class = &a10_mixer_class,
101116025c35SJared McNeill .mute = a10_mute,
101216025c35SJared McNeill .drqtype_codec = 19,
101316025c35SJared McNeill .drqtype_sdram = 22,
101416025c35SJared McNeill .DPC = 0x00,
101516025c35SJared McNeill .DAC_FIFOC = 0x04,
101616025c35SJared McNeill .DAC_FIFOS = 0x08,
101716025c35SJared McNeill .DAC_TXDATA = 0x0c,
101816025c35SJared McNeill .ADC_FIFOC = 0x1c,
101916025c35SJared McNeill .ADC_FIFOS = 0x20,
102016025c35SJared McNeill .ADC_RXDATA = 0x24,
102116025c35SJared McNeill .DAC_CNT = 0x30,
102216025c35SJared McNeill .ADC_CNT = 0x34,
102316025c35SJared McNeill };
102416025c35SJared McNeill
102516025c35SJared McNeill static const struct a10codec_config h3_config = {
102616025c35SJared McNeill .mixer_class = &h3_mixer_class,
102716025c35SJared McNeill .mute = h3_mute,
102816025c35SJared McNeill .drqtype_codec = 15,
102916025c35SJared McNeill .drqtype_sdram = 1,
103016025c35SJared McNeill .DPC = 0x00,
103116025c35SJared McNeill .DAC_FIFOC = 0x04,
103216025c35SJared McNeill .DAC_FIFOS = 0x08,
103316025c35SJared McNeill .DAC_TXDATA = 0x20,
103416025c35SJared McNeill .ADC_FIFOC = 0x10,
103516025c35SJared McNeill .ADC_FIFOS = 0x14,
103616025c35SJared McNeill .ADC_RXDATA = 0x18,
103716025c35SJared McNeill .DAC_CNT = 0x40,
103816025c35SJared McNeill .ADC_CNT = 0x44,
103916025c35SJared McNeill };
104016025c35SJared McNeill
1041356c50adSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
104216025c35SJared McNeill { "allwinner,sun4i-a10-codec", (uintptr_t)&a10_config },
104316025c35SJared McNeill { "allwinner,sun7i-a20-codec", (uintptr_t)&a10_config },
104416025c35SJared McNeill { "allwinner,sun8i-h3-codec", (uintptr_t)&h3_config },
104516025c35SJared McNeill { NULL, 0 }
1046356c50adSEmmanuel Vadot };
1047356c50adSEmmanuel Vadot
1048ba9b7163SAndrew Turner static int
a10codec_probe(device_t dev)1049ba9b7163SAndrew Turner a10codec_probe(device_t dev)
1050ba9b7163SAndrew Turner {
1051ba9b7163SAndrew Turner if (!ofw_bus_status_okay(dev))
1052ba9b7163SAndrew Turner return (ENXIO);
1053ba9b7163SAndrew Turner
1054356c50adSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
1055ba9b7163SAndrew Turner return (ENXIO);
1056ba9b7163SAndrew Turner
1057ba9b7163SAndrew Turner device_set_desc(dev, "Allwinner Audio Codec");
1058ba9b7163SAndrew Turner return (BUS_PROBE_DEFAULT);
1059ba9b7163SAndrew Turner }
1060ba9b7163SAndrew Turner
1061ba9b7163SAndrew Turner static int
a10codec_attach(device_t dev)1062ba9b7163SAndrew Turner a10codec_attach(device_t dev)
1063ba9b7163SAndrew Turner {
1064ba9b7163SAndrew Turner struct a10codec_info *sc;
1065ba9b7163SAndrew Turner char status[SND_STATUSLEN];
106616025c35SJared McNeill struct gpiobus_pin *pa_pin;
106716025c35SJared McNeill phandle_t node;
106816025c35SJared McNeill clk_t clk_bus, clk_codec;
106916025c35SJared McNeill hwreset_t rst;
1070ba9b7163SAndrew Turner uint32_t val;
1071ba9b7163SAndrew Turner int error;
1072ba9b7163SAndrew Turner
107316025c35SJared McNeill node = ofw_bus_get_node(dev);
107416025c35SJared McNeill
1075ba9b7163SAndrew Turner sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
107616025c35SJared McNeill sc->cfg = (void *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
1077ba9b7163SAndrew Turner sc->dev = dev;
1078ba9b7163SAndrew Turner sc->lock = snd_mtxcreate(device_get_nameunit(dev), "a10codec softc");
1079ba9b7163SAndrew Turner
1080ba9b7163SAndrew Turner if (bus_alloc_resources(dev, a10codec_spec, sc->res)) {
1081ba9b7163SAndrew Turner device_printf(dev, "cannot allocate resources for device\n");
1082ba9b7163SAndrew Turner error = ENXIO;
1083ba9b7163SAndrew Turner goto fail;
1084ba9b7163SAndrew Turner }
1085ba9b7163SAndrew Turner
1086ba9b7163SAndrew Turner sc->dmasize = 131072;
1087ba9b7163SAndrew Turner error = bus_dma_tag_create(
1088ba9b7163SAndrew Turner bus_get_dma_tag(dev),
1089ba9b7163SAndrew Turner 4, sc->dmasize, /* alignment, boundary */
1090ba9b7163SAndrew Turner BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1091ba9b7163SAndrew Turner BUS_SPACE_MAXADDR, /* highaddr */
1092ba9b7163SAndrew Turner NULL, NULL, /* filter, filterarg */
1093ba9b7163SAndrew Turner sc->dmasize, 1, /* maxsize, nsegs */
1094ba9b7163SAndrew Turner sc->dmasize, 0, /* maxsegsize, flags */
1095ba9b7163SAndrew Turner NULL, NULL, /* lockfunc, lockarg */
1096ba9b7163SAndrew Turner &sc->dmat);
1097ba9b7163SAndrew Turner if (error != 0) {
1098ba9b7163SAndrew Turner device_printf(dev, "cannot create DMA tag\n");
1099ba9b7163SAndrew Turner goto fail;
1100ba9b7163SAndrew Turner }
1101ba9b7163SAndrew Turner
11026a05f063SJared McNeill /* Get clocks */
110316025c35SJared McNeill if (clk_get_by_ofw_name(dev, 0, "apb", &clk_bus) != 0 &&
110416025c35SJared McNeill clk_get_by_ofw_name(dev, 0, "ahb", &clk_bus) != 0) {
110516025c35SJared McNeill device_printf(dev, "cannot find bus clock\n");
11066a05f063SJared McNeill goto fail;
11076a05f063SJared McNeill }
110816025c35SJared McNeill if (clk_get_by_ofw_name(dev, 0, "codec", &clk_codec) != 0) {
11096a05f063SJared McNeill device_printf(dev, "cannot find codec clock\n");
11106a05f063SJared McNeill goto fail;
11116a05f063SJared McNeill }
11126a05f063SJared McNeill
111316025c35SJared McNeill /* Gating bus clock for codec */
111416025c35SJared McNeill if (clk_enable(clk_bus) != 0) {
111516025c35SJared McNeill device_printf(dev, "cannot enable bus clock\n");
11166a05f063SJared McNeill goto fail;
11176a05f063SJared McNeill }
1118ba9b7163SAndrew Turner /* Activate audio codec clock. According to the A10 and A20 user
1119ba9b7163SAndrew Turner * manuals, Audio_pll can be either 24.576MHz or 22.5792MHz. Most
1120ba9b7163SAndrew Turner * audio sampling rates require an 24.576MHz input clock with the
1121ba9b7163SAndrew Turner * exception of 44.1kHz, 22.05kHz, and 11.025kHz. Unfortunately,
1122ba9b7163SAndrew Turner * both capture and playback use the same clock source so to
1123ba9b7163SAndrew Turner * safely support independent full duplex operation, we use a fixed
1124ba9b7163SAndrew Turner * 24.576MHz clock source and don't advertise native support for
1125ba9b7163SAndrew Turner * the three sampling rates that require a 22.5792MHz input.
1126ba9b7163SAndrew Turner */
11276a05f063SJared McNeill error = clk_set_freq(clk_codec, 24576000, CLK_SET_ROUND_DOWN);
11286a05f063SJared McNeill if (error != 0) {
11296a05f063SJared McNeill device_printf(dev, "cannot set codec clock frequency\n");
11306a05f063SJared McNeill goto fail;
11316a05f063SJared McNeill }
11326a05f063SJared McNeill /* Enable audio codec clock */
11336a05f063SJared McNeill error = clk_enable(clk_codec);
11346a05f063SJared McNeill if (error != 0) {
11356a05f063SJared McNeill device_printf(dev, "cannot enable codec clock\n");
11366a05f063SJared McNeill goto fail;
11376a05f063SJared McNeill }
1138ba9b7163SAndrew Turner
113916025c35SJared McNeill /* De-assert hwreset */
1140bfcf888aSEmmanuel Vadot if (hwreset_get_by_ofw_idx(dev, 0, 0, &rst) == 0) {
114116025c35SJared McNeill error = hwreset_deassert(rst);
114216025c35SJared McNeill if (error != 0) {
114316025c35SJared McNeill device_printf(dev, "cannot de-assert reset\n");
114416025c35SJared McNeill goto fail;
114516025c35SJared McNeill }
114616025c35SJared McNeill }
114716025c35SJared McNeill
1148ba9b7163SAndrew Turner /* Enable DAC */
114916025c35SJared McNeill val = CODEC_READ(sc, AC_DAC_DPC(sc));
1150ba9b7163SAndrew Turner val |= DAC_DPC_EN_DA;
115116025c35SJared McNeill CODEC_WRITE(sc, AC_DAC_DPC(sc), val);
1152ba9b7163SAndrew Turner
115316025c35SJared McNeill if (mixer_init(dev, sc->cfg->mixer_class, sc)) {
1154ba9b7163SAndrew Turner device_printf(dev, "mixer_init failed\n");
1155ba9b7163SAndrew Turner goto fail;
1156ba9b7163SAndrew Turner }
1157ba9b7163SAndrew Turner
115816025c35SJared McNeill /* Unmute PA */
115916025c35SJared McNeill if (gpio_pin_get_by_ofw_property(dev, node, "allwinner,pa-gpios",
116016025c35SJared McNeill &pa_pin) == 0) {
116116025c35SJared McNeill error = gpio_pin_set_active(pa_pin, 1);
116216025c35SJared McNeill if (error != 0)
116316025c35SJared McNeill device_printf(dev, "failed to unmute PA\n");
116416025c35SJared McNeill }
116516025c35SJared McNeill
1166ba9b7163SAndrew Turner pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
1167ba9b7163SAndrew Turner
1168*516a9c02SChristos Margiolis pcm_init(dev, sc);
1169ba9b7163SAndrew Turner
1170ba9b7163SAndrew Turner pcm_addchan(dev, PCMDIR_PLAY, &a10codec_chan_class, sc);
1171ba9b7163SAndrew Turner pcm_addchan(dev, PCMDIR_REC, &a10codec_chan_class, sc);
1172ba9b7163SAndrew Turner
1173ba9b7163SAndrew Turner snprintf(status, SND_STATUSLEN, "at %s", ofw_bus_get_name(dev));
1174*516a9c02SChristos Margiolis if (pcm_register(dev, status)) {
1175*516a9c02SChristos Margiolis device_printf(dev, "pcm_register failed\n");
1176*516a9c02SChristos Margiolis goto fail;
1177*516a9c02SChristos Margiolis }
1178ba9b7163SAndrew Turner
1179ba9b7163SAndrew Turner return (0);
1180ba9b7163SAndrew Turner
1181ba9b7163SAndrew Turner fail:
1182ba9b7163SAndrew Turner bus_release_resources(dev, a10codec_spec, sc->res);
1183ba9b7163SAndrew Turner snd_mtxfree(sc->lock);
1184ba9b7163SAndrew Turner free(sc, M_DEVBUF);
1185ba9b7163SAndrew Turner
118616025c35SJared McNeill return (ENXIO);
1187ba9b7163SAndrew Turner }
1188ba9b7163SAndrew Turner
1189ba9b7163SAndrew Turner static device_method_t a10codec_pcm_methods[] = {
1190ba9b7163SAndrew Turner /* Device interface */
1191ba9b7163SAndrew Turner DEVMETHOD(device_probe, a10codec_probe),
1192ba9b7163SAndrew Turner DEVMETHOD(device_attach, a10codec_attach),
1193ba9b7163SAndrew Turner
1194ba9b7163SAndrew Turner DEVMETHOD_END
1195ba9b7163SAndrew Turner };
1196ba9b7163SAndrew Turner
1197ba9b7163SAndrew Turner static driver_t a10codec_pcm_driver = {
1198ba9b7163SAndrew Turner "pcm",
1199ba9b7163SAndrew Turner a10codec_pcm_methods,
1200ba9b7163SAndrew Turner PCM_SOFTC_SIZE,
1201ba9b7163SAndrew Turner };
1202ba9b7163SAndrew Turner
12032287364eSJohn Baldwin DRIVER_MODULE(a10codec, simplebus, a10codec_pcm_driver, 0, 0);
1204ba9b7163SAndrew Turner MODULE_DEPEND(a10codec, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1205ba9b7163SAndrew Turner MODULE_VERSION(a10codec, 1);
1206