sunxi_codec.c revision 1.12 1 1.12 thorpej /* $NetBSD: sunxi_codec.c,v 1.12 2021/01/27 03:10:20 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2014-2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include "opt_ddb.h"
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.12 thorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_codec.c,v 1.12 2021/01/27 03:10:20 thorpej Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill #include <sys/cpu.h>
37 1.1 jmcneill #include <sys/device.h>
38 1.1 jmcneill #include <sys/kmem.h>
39 1.1 jmcneill #include <sys/gpio.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <sys/audioio.h>
42 1.6 isaki #include <dev/audio/audio_if.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <dev/fdt/fdtvar.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <arm/sunxi/sunxi_codec.h>
47 1.1 jmcneill
48 1.1 jmcneill #define TX_TRIG_LEVEL 0xf
49 1.1 jmcneill #define RX_TRIG_LEVEL 0x7
50 1.1 jmcneill #define DRQ_CLR_CNT 0x3
51 1.1 jmcneill
52 1.1 jmcneill #define AC_DAC_DPC(_sc) ((_sc)->sc_cfg->DPC)
53 1.1 jmcneill #define DAC_DPC_EN_DA 0x80000000
54 1.1 jmcneill #define AC_DAC_FIFOC(_sc) ((_sc)->sc_cfg->DAC_FIFOC)
55 1.1 jmcneill #define DAC_FIFOC_FS __BITS(31,29)
56 1.1 jmcneill #define DAC_FS_48KHZ 0
57 1.1 jmcneill #define DAC_FS_32KHZ 1
58 1.1 jmcneill #define DAC_FS_24KHZ 2
59 1.1 jmcneill #define DAC_FS_16KHZ 3
60 1.1 jmcneill #define DAC_FS_12KHZ 4
61 1.1 jmcneill #define DAC_FS_8KHZ 5
62 1.1 jmcneill #define DAC_FS_192KHZ 6
63 1.1 jmcneill #define DAC_FS_96KHZ 7
64 1.1 jmcneill #define DAC_FIFOC_FIFO_MODE __BITS(25,24)
65 1.1 jmcneill #define FIFO_MODE_24_31_8 0
66 1.1 jmcneill #define FIFO_MODE_16_31_16 0
67 1.1 jmcneill #define FIFO_MODE_16_15_0 1
68 1.1 jmcneill #define DAC_FIFOC_DRQ_CLR_CNT __BITS(22,21)
69 1.1 jmcneill #define DAC_FIFOC_TX_TRIG_LEVEL __BITS(14,8)
70 1.1 jmcneill #define DAC_FIFOC_MONO_EN __BIT(6)
71 1.1 jmcneill #define DAC_FIFOC_TX_BITS __BIT(5)
72 1.1 jmcneill #define DAC_FIFOC_DRQ_EN __BIT(4)
73 1.1 jmcneill #define DAC_FIFOC_FIFO_FLUSH __BIT(0)
74 1.1 jmcneill #define AC_DAC_FIFOS(_sc) ((_sc)->sc_cfg->DAC_FIFOS)
75 1.1 jmcneill #define AC_DAC_TXDATA(_sc) ((_sc)->sc_cfg->DAC_TXDATA)
76 1.1 jmcneill #define AC_ADC_FIFOC(_sc) ((_sc)->sc_cfg->ADC_FIFOC)
77 1.1 jmcneill #define ADC_FIFOC_FS __BITS(31,29)
78 1.1 jmcneill #define ADC_FS_48KHZ 0
79 1.1 jmcneill #define ADC_FIFOC_EN_AD __BIT(28)
80 1.1 jmcneill #define ADC_FIFOC_RX_FIFO_MODE __BIT(24)
81 1.1 jmcneill #define ADC_FIFOC_RX_TRIG_LEVEL __BITS(12,8)
82 1.1 jmcneill #define ADC_FIFOC_MONO_EN __BIT(7)
83 1.1 jmcneill #define ADC_FIFOC_RX_BITS __BIT(6)
84 1.1 jmcneill #define ADC_FIFOC_DRQ_EN __BIT(4)
85 1.1 jmcneill #define ADC_FIFOC_FIFO_FLUSH __BIT(0)
86 1.1 jmcneill #define AC_ADC_FIFOS(_sc) ((_sc)->sc_cfg->ADC_FIFOS)
87 1.1 jmcneill #define AC_ADC_RXDATA(_sc) ((_sc)->sc_cfg->ADC_RXDATA)
88 1.1 jmcneill #define AC_DAC_CNT(_sc) ((_sc)->sc_cfg->DAC_CNT)
89 1.1 jmcneill #define AC_ADC_CNT(_sc) ((_sc)->sc_cfg->ADC_CNT)
90 1.1 jmcneill
91 1.9 thorpej static const struct device_compatible_entry compat_data[] = {
92 1.2 jmcneill A10_CODEC_COMPATDATA,
93 1.3 jmcneill A31_CODEC_COMPATDATA,
94 1.1 jmcneill H3_CODEC_COMPATDATA,
95 1.9 thorpej
96 1.11 thorpej DEVICE_COMPAT_EOL
97 1.1 jmcneill };
98 1.1 jmcneill
99 1.1 jmcneill #define CODEC_READ(sc, reg) \
100 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
101 1.1 jmcneill #define CODEC_WRITE(sc, reg, val) \
102 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
103 1.1 jmcneill
104 1.1 jmcneill static int
105 1.1 jmcneill sunxi_codec_allocdma(struct sunxi_codec_softc *sc, size_t size,
106 1.1 jmcneill size_t align, struct sunxi_codec_dma *dma)
107 1.1 jmcneill {
108 1.1 jmcneill int error;
109 1.1 jmcneill
110 1.1 jmcneill dma->dma_size = size;
111 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, dma->dma_size, align, 0,
112 1.1 jmcneill dma->dma_segs, 1, &dma->dma_nsegs, BUS_DMA_WAITOK);
113 1.1 jmcneill if (error)
114 1.1 jmcneill return error;
115 1.1 jmcneill
116 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs,
117 1.1 jmcneill dma->dma_size, &dma->dma_addr, BUS_DMA_WAITOK | BUS_DMA_COHERENT);
118 1.1 jmcneill if (error)
119 1.1 jmcneill goto free;
120 1.1 jmcneill
121 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, dma->dma_size, dma->dma_nsegs,
122 1.1 jmcneill dma->dma_size, 0, BUS_DMA_WAITOK, &dma->dma_map);
123 1.1 jmcneill if (error)
124 1.1 jmcneill goto unmap;
125 1.1 jmcneill
126 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_addr,
127 1.1 jmcneill dma->dma_size, NULL, BUS_DMA_WAITOK);
128 1.1 jmcneill if (error)
129 1.1 jmcneill goto destroy;
130 1.1 jmcneill
131 1.1 jmcneill return 0;
132 1.1 jmcneill
133 1.1 jmcneill destroy:
134 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
135 1.1 jmcneill unmap:
136 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
137 1.1 jmcneill free:
138 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
139 1.1 jmcneill
140 1.1 jmcneill return error;
141 1.1 jmcneill }
142 1.1 jmcneill
143 1.1 jmcneill static void
144 1.1 jmcneill sunxi_codec_freedma(struct sunxi_codec_softc *sc, struct sunxi_codec_dma *dma)
145 1.1 jmcneill {
146 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
147 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
148 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
149 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
150 1.1 jmcneill }
151 1.1 jmcneill
152 1.1 jmcneill static int
153 1.1 jmcneill sunxi_codec_transfer(struct sunxi_codec_chan *ch)
154 1.1 jmcneill {
155 1.1 jmcneill bus_dma_segment_t seg;
156 1.1 jmcneill
157 1.1 jmcneill seg.ds_addr = ch->ch_cur_phys;
158 1.1 jmcneill seg.ds_len = ch->ch_blksize;
159 1.1 jmcneill ch->ch_req.dreq_segs = &seg;
160 1.1 jmcneill ch->ch_req.dreq_nsegs = 1;
161 1.1 jmcneill
162 1.1 jmcneill return fdtbus_dma_transfer(ch->ch_dma, &ch->ch_req);
163 1.1 jmcneill }
164 1.1 jmcneill
165 1.1 jmcneill static int
166 1.6 isaki sunxi_codec_query_format(void *priv, audio_format_query_t *afp)
167 1.1 jmcneill {
168 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
169 1.1 jmcneill
170 1.6 isaki return audio_query_format(&sc->sc_format, 1, afp);
171 1.1 jmcneill }
172 1.1 jmcneill
173 1.1 jmcneill static int
174 1.6 isaki sunxi_codec_set_format(void *priv, int setmode,
175 1.6 isaki const audio_params_t *play, const audio_params_t *rec,
176 1.6 isaki audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
177 1.1 jmcneill {
178 1.1 jmcneill
179 1.1 jmcneill return 0;
180 1.1 jmcneill }
181 1.1 jmcneill
182 1.1 jmcneill static int
183 1.1 jmcneill sunxi_codec_set_port(void *priv, mixer_ctrl_t *mc)
184 1.1 jmcneill {
185 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
186 1.1 jmcneill
187 1.1 jmcneill return sc->sc_cfg->set_port(sc, mc);
188 1.1 jmcneill }
189 1.1 jmcneill
190 1.1 jmcneill static int
191 1.1 jmcneill sunxi_codec_get_port(void *priv, mixer_ctrl_t *mc)
192 1.1 jmcneill {
193 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
194 1.1 jmcneill
195 1.1 jmcneill return sc->sc_cfg->get_port(sc, mc);
196 1.1 jmcneill }
197 1.1 jmcneill
198 1.1 jmcneill static int
199 1.1 jmcneill sunxi_codec_query_devinfo(void *priv, mixer_devinfo_t *di)
200 1.1 jmcneill {
201 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
202 1.1 jmcneill
203 1.1 jmcneill return sc->sc_cfg->query_devinfo(sc, di);
204 1.1 jmcneill }
205 1.1 jmcneill
206 1.1 jmcneill static void *
207 1.1 jmcneill sunxi_codec_allocm(void *priv, int dir, size_t size)
208 1.1 jmcneill {
209 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
210 1.1 jmcneill struct sunxi_codec_dma *dma;
211 1.1 jmcneill int error;
212 1.1 jmcneill
213 1.1 jmcneill dma = kmem_alloc(sizeof(*dma), KM_SLEEP);
214 1.1 jmcneill
215 1.1 jmcneill error = sunxi_codec_allocdma(sc, size, 16, dma);
216 1.1 jmcneill if (error) {
217 1.1 jmcneill kmem_free(dma, sizeof(*dma));
218 1.1 jmcneill device_printf(sc->sc_dev, "couldn't allocate DMA memory (%d)\n",
219 1.1 jmcneill error);
220 1.1 jmcneill return NULL;
221 1.1 jmcneill }
222 1.1 jmcneill
223 1.1 jmcneill LIST_INSERT_HEAD(&sc->sc_dmalist, dma, dma_list);
224 1.1 jmcneill
225 1.1 jmcneill return dma->dma_addr;
226 1.1 jmcneill }
227 1.1 jmcneill
228 1.1 jmcneill static void
229 1.1 jmcneill sunxi_codec_freem(void *priv, void *addr, size_t size)
230 1.1 jmcneill {
231 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
232 1.1 jmcneill struct sunxi_codec_dma *dma;
233 1.1 jmcneill
234 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
235 1.1 jmcneill if (dma->dma_addr == addr) {
236 1.1 jmcneill sunxi_codec_freedma(sc, dma);
237 1.1 jmcneill LIST_REMOVE(dma, dma_list);
238 1.1 jmcneill kmem_free(dma, sizeof(*dma));
239 1.1 jmcneill break;
240 1.1 jmcneill }
241 1.1 jmcneill }
242 1.1 jmcneill
243 1.1 jmcneill static int
244 1.1 jmcneill sunxi_codec_getdev(void *priv, struct audio_device *adev)
245 1.1 jmcneill {
246 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
247 1.1 jmcneill
248 1.1 jmcneill snprintf(adev->name, sizeof(adev->name), "Allwinner");
249 1.1 jmcneill snprintf(adev->version, sizeof(adev->version), "%s",
250 1.1 jmcneill sc->sc_cfg->name);
251 1.1 jmcneill snprintf(adev->config, sizeof(adev->config), "sunxicodec");
252 1.1 jmcneill
253 1.1 jmcneill return 0;
254 1.1 jmcneill }
255 1.1 jmcneill
256 1.1 jmcneill static int
257 1.1 jmcneill sunxi_codec_get_props(void *priv)
258 1.1 jmcneill {
259 1.7 isaki
260 1.7 isaki return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE|
261 1.7 isaki AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
262 1.1 jmcneill }
263 1.1 jmcneill
264 1.1 jmcneill static int
265 1.1 jmcneill sunxi_codec_trigger_output(void *priv, void *start, void *end, int blksize,
266 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
267 1.1 jmcneill {
268 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
269 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_pchan;
270 1.1 jmcneill struct sunxi_codec_dma *dma;
271 1.1 jmcneill bus_addr_t pstart;
272 1.1 jmcneill bus_size_t psize;
273 1.1 jmcneill uint32_t val;
274 1.1 jmcneill int error;
275 1.1 jmcneill
276 1.1 jmcneill pstart = 0;
277 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start;
278 1.1 jmcneill
279 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
280 1.1 jmcneill if (dma->dma_addr == start) {
281 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr;
282 1.1 jmcneill break;
283 1.1 jmcneill }
284 1.1 jmcneill if (pstart == 0) {
285 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start);
286 1.1 jmcneill return EINVAL;
287 1.1 jmcneill }
288 1.1 jmcneill
289 1.1 jmcneill ch->ch_intr = intr;
290 1.1 jmcneill ch->ch_intrarg = intrarg;
291 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart;
292 1.1 jmcneill ch->ch_end_phys = pstart + psize;
293 1.1 jmcneill ch->ch_blksize = blksize;
294 1.1 jmcneill
295 1.1 jmcneill /* Flush DAC FIFO */
296 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
297 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val | DAC_FIFOC_FIFO_FLUSH);
298 1.1 jmcneill
299 1.1 jmcneill /* Clear DAC FIFO status */
300 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOS(sc));
301 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOS(sc), val);
302 1.1 jmcneill
303 1.1 jmcneill /* Unmute output */
304 1.1 jmcneill if (sc->sc_cfg->mute)
305 1.1 jmcneill sc->sc_cfg->mute(sc, 0, ch->ch_mode);
306 1.1 jmcneill
307 1.1 jmcneill /* Configure DAC FIFO */
308 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc),
309 1.1 jmcneill __SHIFTIN(DAC_FS_48KHZ, DAC_FIFOC_FS) |
310 1.1 jmcneill __SHIFTIN(FIFO_MODE_16_15_0, DAC_FIFOC_FIFO_MODE) |
311 1.1 jmcneill __SHIFTIN(DRQ_CLR_CNT, DAC_FIFOC_DRQ_CLR_CNT) |
312 1.1 jmcneill __SHIFTIN(TX_TRIG_LEVEL, DAC_FIFOC_TX_TRIG_LEVEL));
313 1.1 jmcneill
314 1.1 jmcneill /* Enable DAC DRQ */
315 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
316 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val | DAC_FIFOC_DRQ_EN);
317 1.1 jmcneill
318 1.1 jmcneill /* Start DMA transfer */
319 1.1 jmcneill error = sunxi_codec_transfer(ch);
320 1.1 jmcneill if (error != 0) {
321 1.1 jmcneill aprint_error_dev(sc->sc_dev,
322 1.1 jmcneill "failed to start DMA transfer: %d\n", error);
323 1.1 jmcneill return error;
324 1.1 jmcneill }
325 1.1 jmcneill
326 1.1 jmcneill return 0;
327 1.1 jmcneill }
328 1.1 jmcneill
329 1.1 jmcneill static int
330 1.1 jmcneill sunxi_codec_trigger_input(void *priv, void *start, void *end, int blksize,
331 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
332 1.1 jmcneill {
333 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
334 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_rchan;
335 1.1 jmcneill struct sunxi_codec_dma *dma;
336 1.1 jmcneill bus_addr_t pstart;
337 1.1 jmcneill bus_size_t psize;
338 1.1 jmcneill uint32_t val;
339 1.1 jmcneill int error;
340 1.1 jmcneill
341 1.1 jmcneill pstart = 0;
342 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start;
343 1.1 jmcneill
344 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
345 1.1 jmcneill if (dma->dma_addr == start) {
346 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr;
347 1.1 jmcneill break;
348 1.1 jmcneill }
349 1.1 jmcneill if (pstart == 0) {
350 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start);
351 1.1 jmcneill return EINVAL;
352 1.1 jmcneill }
353 1.1 jmcneill
354 1.1 jmcneill ch->ch_intr = intr;
355 1.1 jmcneill ch->ch_intrarg = intrarg;
356 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart;
357 1.1 jmcneill ch->ch_end_phys = pstart + psize;
358 1.1 jmcneill ch->ch_blksize = blksize;
359 1.1 jmcneill
360 1.1 jmcneill /* Flush ADC FIFO */
361 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
362 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val | ADC_FIFOC_FIFO_FLUSH);
363 1.1 jmcneill
364 1.1 jmcneill /* Clear ADC FIFO status */
365 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOS(sc));
366 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOS(sc), val);
367 1.1 jmcneill
368 1.1 jmcneill /* Unmute input */
369 1.1 jmcneill if (sc->sc_cfg->mute)
370 1.1 jmcneill sc->sc_cfg->mute(sc, 0, ch->ch_mode);
371 1.1 jmcneill
372 1.1 jmcneill /* Configure ADC FIFO */
373 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc),
374 1.1 jmcneill __SHIFTIN(ADC_FS_48KHZ, ADC_FIFOC_FS) |
375 1.1 jmcneill __SHIFTIN(RX_TRIG_LEVEL, ADC_FIFOC_RX_TRIG_LEVEL) |
376 1.1 jmcneill ADC_FIFOC_EN_AD | ADC_FIFOC_RX_FIFO_MODE);
377 1.1 jmcneill
378 1.1 jmcneill /* Enable ADC DRQ */
379 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
380 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val | ADC_FIFOC_DRQ_EN);
381 1.1 jmcneill
382 1.1 jmcneill /* Start DMA transfer */
383 1.1 jmcneill error = sunxi_codec_transfer(ch);
384 1.1 jmcneill if (error != 0) {
385 1.1 jmcneill aprint_error_dev(sc->sc_dev,
386 1.1 jmcneill "failed to start DMA transfer: %d\n", error);
387 1.1 jmcneill return error;
388 1.1 jmcneill }
389 1.1 jmcneill
390 1.1 jmcneill return 0;
391 1.1 jmcneill }
392 1.1 jmcneill
393 1.1 jmcneill static int
394 1.1 jmcneill sunxi_codec_halt_output(void *priv)
395 1.1 jmcneill {
396 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
397 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_pchan;
398 1.1 jmcneill uint32_t val;
399 1.1 jmcneill
400 1.1 jmcneill /* Disable DMA channel */
401 1.1 jmcneill fdtbus_dma_halt(ch->ch_dma);
402 1.1 jmcneill
403 1.5 bouyer /* flush fifo */
404 1.5 bouyer val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
405 1.5 bouyer CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val | DAC_FIFOC_FIFO_FLUSH);
406 1.5 bouyer while (val & DAC_FIFOC_FIFO_FLUSH)
407 1.5 bouyer val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
408 1.5 bouyer
409 1.1 jmcneill /* Mute output */
410 1.1 jmcneill if (sc->sc_cfg->mute)
411 1.1 jmcneill sc->sc_cfg->mute(sc, 1, ch->ch_mode);
412 1.1 jmcneill
413 1.1 jmcneill /* Disable DAC DRQ */
414 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
415 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val & ~DAC_FIFOC_DRQ_EN);
416 1.1 jmcneill
417 1.1 jmcneill ch->ch_intr = NULL;
418 1.1 jmcneill ch->ch_intrarg = NULL;
419 1.1 jmcneill
420 1.1 jmcneill return 0;
421 1.1 jmcneill }
422 1.1 jmcneill
423 1.1 jmcneill static int
424 1.1 jmcneill sunxi_codec_halt_input(void *priv)
425 1.1 jmcneill {
426 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
427 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_rchan;
428 1.1 jmcneill uint32_t val;
429 1.1 jmcneill
430 1.1 jmcneill /* Mute output */
431 1.1 jmcneill if (sc->sc_cfg->mute)
432 1.1 jmcneill sc->sc_cfg->mute(sc, 1, ch->ch_mode);
433 1.1 jmcneill
434 1.5 bouyer /* flush fifo */
435 1.5 bouyer val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
436 1.5 bouyer CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val | ADC_FIFOC_FIFO_FLUSH);
437 1.5 bouyer while (val & ADC_FIFOC_FIFO_FLUSH)
438 1.5 bouyer val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
439 1.5 bouyer
440 1.5 bouyer /* Disable DMA channel */
441 1.5 bouyer fdtbus_dma_halt(ch->ch_dma);
442 1.5 bouyer
443 1.1 jmcneill /* Disable ADC DRQ */
444 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
445 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val & ~ADC_FIFOC_DRQ_EN);
446 1.1 jmcneill
447 1.1 jmcneill return 0;
448 1.1 jmcneill }
449 1.1 jmcneill
450 1.1 jmcneill static void
451 1.1 jmcneill sunxi_codec_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
452 1.1 jmcneill {
453 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
454 1.1 jmcneill
455 1.1 jmcneill *intr = &sc->sc_intr_lock;
456 1.1 jmcneill *thread = &sc->sc_lock;
457 1.1 jmcneill }
458 1.1 jmcneill
459 1.1 jmcneill static const struct audio_hw_if sunxi_codec_hw_if = {
460 1.6 isaki .query_format = sunxi_codec_query_format,
461 1.6 isaki .set_format = sunxi_codec_set_format,
462 1.1 jmcneill .allocm = sunxi_codec_allocm,
463 1.1 jmcneill .freem = sunxi_codec_freem,
464 1.1 jmcneill .getdev = sunxi_codec_getdev,
465 1.1 jmcneill .set_port = sunxi_codec_set_port,
466 1.1 jmcneill .get_port = sunxi_codec_get_port,
467 1.1 jmcneill .query_devinfo = sunxi_codec_query_devinfo,
468 1.1 jmcneill .get_props = sunxi_codec_get_props,
469 1.1 jmcneill .trigger_output = sunxi_codec_trigger_output,
470 1.1 jmcneill .trigger_input = sunxi_codec_trigger_input,
471 1.1 jmcneill .halt_output = sunxi_codec_halt_output,
472 1.1 jmcneill .halt_input = sunxi_codec_halt_input,
473 1.1 jmcneill .get_locks = sunxi_codec_get_locks,
474 1.1 jmcneill };
475 1.1 jmcneill
476 1.1 jmcneill static void
477 1.1 jmcneill sunxi_codec_dmaintr(void *priv)
478 1.1 jmcneill {
479 1.1 jmcneill struct sunxi_codec_chan * const ch = priv;
480 1.4 bouyer struct sunxi_codec_softc * const sc = ch->ch_sc;
481 1.1 jmcneill
482 1.4 bouyer mutex_enter(&sc->sc_intr_lock);
483 1.1 jmcneill ch->ch_cur_phys += ch->ch_blksize;
484 1.1 jmcneill if (ch->ch_cur_phys >= ch->ch_end_phys)
485 1.1 jmcneill ch->ch_cur_phys = ch->ch_start_phys;
486 1.1 jmcneill
487 1.1 jmcneill if (ch->ch_intr) {
488 1.1 jmcneill ch->ch_intr(ch->ch_intrarg);
489 1.1 jmcneill sunxi_codec_transfer(ch);
490 1.1 jmcneill }
491 1.4 bouyer mutex_exit(&sc->sc_intr_lock);
492 1.1 jmcneill }
493 1.1 jmcneill
494 1.1 jmcneill static int
495 1.1 jmcneill sunxi_codec_chan_init(struct sunxi_codec_softc *sc,
496 1.1 jmcneill struct sunxi_codec_chan *ch, u_int mode, const char *dmaname)
497 1.1 jmcneill {
498 1.1 jmcneill ch->ch_sc = sc;
499 1.1 jmcneill ch->ch_mode = mode;
500 1.1 jmcneill ch->ch_dma = fdtbus_dma_get(sc->sc_phandle, dmaname, sunxi_codec_dmaintr, ch);
501 1.1 jmcneill if (ch->ch_dma == NULL) {
502 1.1 jmcneill aprint_error(": couldn't get dma channel \"%s\"\n", dmaname);
503 1.1 jmcneill return ENXIO;
504 1.1 jmcneill }
505 1.1 jmcneill
506 1.1 jmcneill if (mode == AUMODE_PLAY) {
507 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_WRITE;
508 1.1 jmcneill ch->ch_req.dreq_dev_phys =
509 1.1 jmcneill sc->sc_baseaddr + AC_DAC_TXDATA(sc);
510 1.1 jmcneill } else {
511 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_READ;
512 1.1 jmcneill ch->ch_req.dreq_dev_phys =
513 1.1 jmcneill sc->sc_baseaddr + AC_ADC_RXDATA(sc);
514 1.1 jmcneill }
515 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_bus_width = 16;
516 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_burst_len = 4;
517 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_bus_width = 16;
518 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_burst_len = 4;
519 1.1 jmcneill
520 1.1 jmcneill return 0;
521 1.1 jmcneill }
522 1.1 jmcneill
523 1.1 jmcneill static int
524 1.1 jmcneill sunxi_codec_clock_init(int phandle)
525 1.1 jmcneill {
526 1.1 jmcneill struct fdtbus_reset *rst;
527 1.1 jmcneill struct clk *clk;
528 1.1 jmcneill int error;
529 1.1 jmcneill
530 1.1 jmcneill /* Set codec clock to 24.576MHz, suitable for 48 kHz sampling rates */
531 1.1 jmcneill clk = fdtbus_clock_get(phandle, "codec");
532 1.1 jmcneill if (clk == NULL) {
533 1.1 jmcneill aprint_error(": couldn't find codec clock\n");
534 1.1 jmcneill return ENXIO;
535 1.1 jmcneill }
536 1.1 jmcneill error = clk_set_rate(clk, 24576000);
537 1.1 jmcneill if (error != 0) {
538 1.1 jmcneill aprint_error(": couldn't set codec clock rate: %d\n", error);
539 1.1 jmcneill return error;
540 1.1 jmcneill }
541 1.1 jmcneill error = clk_enable(clk);
542 1.1 jmcneill if (error != 0) {
543 1.1 jmcneill aprint_error(": couldn't enable codec clock: %d\n", error);
544 1.1 jmcneill return error;
545 1.1 jmcneill }
546 1.1 jmcneill
547 1.1 jmcneill /* Enable APB clock */
548 1.1 jmcneill clk = fdtbus_clock_get(phandle, "apb");
549 1.1 jmcneill if (clk == NULL) {
550 1.1 jmcneill aprint_error(": couldn't find apb clock\n");
551 1.1 jmcneill return ENXIO;
552 1.1 jmcneill }
553 1.1 jmcneill error = clk_enable(clk);
554 1.1 jmcneill if (error != 0) {
555 1.1 jmcneill aprint_error(": couldn't enable apb clock: %d\n", error);
556 1.1 jmcneill return error;
557 1.1 jmcneill }
558 1.1 jmcneill
559 1.1 jmcneill /* De-assert reset */
560 1.1 jmcneill rst = fdtbus_reset_get_index(phandle, 0);
561 1.2 jmcneill if (rst != NULL) {
562 1.2 jmcneill error = fdtbus_reset_deassert(rst);
563 1.2 jmcneill if (error != 0) {
564 1.2 jmcneill aprint_error(": couldn't de-assert reset: %d\n", error);
565 1.2 jmcneill return error;
566 1.2 jmcneill }
567 1.1 jmcneill }
568 1.1 jmcneill
569 1.1 jmcneill return 0;
570 1.1 jmcneill }
571 1.1 jmcneill
572 1.1 jmcneill static int
573 1.1 jmcneill sunxi_codec_match(device_t parent, cfdata_t cf, void *aux)
574 1.1 jmcneill {
575 1.1 jmcneill struct fdt_attach_args * const faa = aux;
576 1.1 jmcneill
577 1.12 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
578 1.1 jmcneill }
579 1.1 jmcneill
580 1.1 jmcneill static void
581 1.1 jmcneill sunxi_codec_attach(device_t parent, device_t self, void *aux)
582 1.1 jmcneill {
583 1.1 jmcneill struct sunxi_codec_softc * const sc = device_private(self);
584 1.1 jmcneill struct fdt_attach_args * const faa = aux;
585 1.1 jmcneill const int phandle = faa->faa_phandle;
586 1.1 jmcneill bus_addr_t addr;
587 1.1 jmcneill bus_size_t size;
588 1.1 jmcneill uint32_t val;
589 1.1 jmcneill
590 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
591 1.1 jmcneill aprint_error(": couldn't get registers\n");
592 1.1 jmcneill return;
593 1.1 jmcneill }
594 1.1 jmcneill
595 1.1 jmcneill if (sunxi_codec_clock_init(phandle) != 0)
596 1.1 jmcneill return;
597 1.1 jmcneill
598 1.1 jmcneill sc->sc_dev = self;
599 1.1 jmcneill sc->sc_phandle = phandle;
600 1.1 jmcneill sc->sc_baseaddr = addr;
601 1.1 jmcneill sc->sc_bst = faa->faa_bst;
602 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
603 1.1 jmcneill aprint_error(": couldn't map registers\n");
604 1.1 jmcneill return;
605 1.1 jmcneill }
606 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
607 1.1 jmcneill LIST_INIT(&sc->sc_dmalist);
608 1.12 thorpej sc->sc_cfg = of_compatible_lookup(phandle, compat_data)->data;
609 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
610 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
611 1.1 jmcneill
612 1.1 jmcneill if (sunxi_codec_chan_init(sc, &sc->sc_pchan, AUMODE_PLAY, "tx") != 0 ||
613 1.1 jmcneill sunxi_codec_chan_init(sc, &sc->sc_rchan, AUMODE_RECORD, "rx") != 0) {
614 1.1 jmcneill aprint_error(": couldn't setup channels\n");
615 1.1 jmcneill return;
616 1.1 jmcneill }
617 1.1 jmcneill
618 1.1 jmcneill /* Optional PA mute GPIO */
619 1.1 jmcneill sc->sc_pin_pa = fdtbus_gpio_acquire(phandle, "allwinner,pa-gpios", GPIO_PIN_OUTPUT);
620 1.1 jmcneill
621 1.1 jmcneill aprint_naive("\n");
622 1.1 jmcneill aprint_normal(": %s\n", sc->sc_cfg->name);
623 1.1 jmcneill
624 1.1 jmcneill /* Enable DAC */
625 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_DPC(sc));
626 1.1 jmcneill val |= DAC_DPC_EN_DA;
627 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_DPC(sc), val);
628 1.1 jmcneill
629 1.1 jmcneill /* Initialize codec */
630 1.1 jmcneill if (sc->sc_cfg->init(sc) != 0) {
631 1.1 jmcneill aprint_error_dev(self, "couldn't initialize codec\n");
632 1.1 jmcneill return;
633 1.1 jmcneill }
634 1.1 jmcneill
635 1.1 jmcneill sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
636 1.1 jmcneill sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
637 1.1 jmcneill sc->sc_format.validbits = 16;
638 1.1 jmcneill sc->sc_format.precision = 16;
639 1.1 jmcneill sc->sc_format.channels = 2;
640 1.1 jmcneill sc->sc_format.channel_mask = AUFMT_STEREO;
641 1.6 isaki sc->sc_format.frequency_type = 1;
642 1.6 isaki sc->sc_format.frequency[0] = 48000;
643 1.1 jmcneill
644 1.1 jmcneill audio_attach_mi(&sunxi_codec_hw_if, sc, self);
645 1.1 jmcneill }
646 1.1 jmcneill
647 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_codec, sizeof(struct sunxi_codec_softc),
648 1.1 jmcneill sunxi_codec_match, sunxi_codec_attach, NULL, NULL);
649 1.1 jmcneill
650 1.1 jmcneill #ifdef DDB
651 1.1 jmcneill void sunxicodec_dump(void);
652 1.1 jmcneill
653 1.1 jmcneill void
654 1.1 jmcneill sunxicodec_dump(void)
655 1.1 jmcneill {
656 1.1 jmcneill struct sunxi_codec_softc *sc;
657 1.1 jmcneill device_t dev;
658 1.1 jmcneill
659 1.1 jmcneill dev = device_find_by_driver_unit("sunxicodec", 0);
660 1.1 jmcneill if (dev == NULL)
661 1.1 jmcneill return;
662 1.1 jmcneill sc = device_private(dev);
663 1.1 jmcneill
664 1.1 jmcneill device_printf(dev, "AC_DAC_DPC: %08x\n", CODEC_READ(sc, AC_DAC_DPC(sc)));
665 1.1 jmcneill device_printf(dev, "AC_DAC_FIFOC: %08x\n", CODEC_READ(sc, AC_DAC_FIFOC(sc)));
666 1.1 jmcneill device_printf(dev, "AC_DAC_FIFOS: %08x\n", CODEC_READ(sc, AC_DAC_FIFOS(sc)));
667 1.1 jmcneill device_printf(dev, "AC_ADC_FIFOC: %08x\n", CODEC_READ(sc, AC_ADC_FIFOC(sc)));
668 1.1 jmcneill device_printf(dev, "AC_ADC_FIFOS: %08x\n", CODEC_READ(sc, AC_ADC_FIFOS(sc)));
669 1.1 jmcneill device_printf(dev, "AC_DAC_CNT: %08x\n", CODEC_READ(sc, AC_DAC_CNT(sc)));
670 1.1 jmcneill device_printf(dev, "AC_ADC_CNT: %08x\n", CODEC_READ(sc, AC_ADC_CNT(sc)));
671 1.1 jmcneill }
672 1.1 jmcneill #endif
673