sunxi_codec.c revision 1.5 1 1.5 bouyer /* $NetBSD: sunxi_codec.c,v 1.5 2018/04/20 18:07:40 bouyer 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.5 bouyer __KERNEL_RCSID(0, "$NetBSD: sunxi_codec.c,v 1.5 2018/04/20 18:07:40 bouyer 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.1 jmcneill #include <dev/audio_if.h>
43 1.1 jmcneill #include <dev/auconv.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <arm/sunxi/sunxi_codec.h>
48 1.1 jmcneill
49 1.1 jmcneill #define TX_TRIG_LEVEL 0xf
50 1.1 jmcneill #define RX_TRIG_LEVEL 0x7
51 1.1 jmcneill #define DRQ_CLR_CNT 0x3
52 1.1 jmcneill
53 1.1 jmcneill #define AC_DAC_DPC(_sc) ((_sc)->sc_cfg->DPC)
54 1.1 jmcneill #define DAC_DPC_EN_DA 0x80000000
55 1.1 jmcneill #define AC_DAC_FIFOC(_sc) ((_sc)->sc_cfg->DAC_FIFOC)
56 1.1 jmcneill #define DAC_FIFOC_FS __BITS(31,29)
57 1.1 jmcneill #define DAC_FS_48KHZ 0
58 1.1 jmcneill #define DAC_FS_32KHZ 1
59 1.1 jmcneill #define DAC_FS_24KHZ 2
60 1.1 jmcneill #define DAC_FS_16KHZ 3
61 1.1 jmcneill #define DAC_FS_12KHZ 4
62 1.1 jmcneill #define DAC_FS_8KHZ 5
63 1.1 jmcneill #define DAC_FS_192KHZ 6
64 1.1 jmcneill #define DAC_FS_96KHZ 7
65 1.1 jmcneill #define DAC_FIFOC_FIFO_MODE __BITS(25,24)
66 1.1 jmcneill #define FIFO_MODE_24_31_8 0
67 1.1 jmcneill #define FIFO_MODE_16_31_16 0
68 1.1 jmcneill #define FIFO_MODE_16_15_0 1
69 1.1 jmcneill #define DAC_FIFOC_DRQ_CLR_CNT __BITS(22,21)
70 1.1 jmcneill #define DAC_FIFOC_TX_TRIG_LEVEL __BITS(14,8)
71 1.1 jmcneill #define DAC_FIFOC_MONO_EN __BIT(6)
72 1.1 jmcneill #define DAC_FIFOC_TX_BITS __BIT(5)
73 1.1 jmcneill #define DAC_FIFOC_DRQ_EN __BIT(4)
74 1.1 jmcneill #define DAC_FIFOC_FIFO_FLUSH __BIT(0)
75 1.1 jmcneill #define AC_DAC_FIFOS(_sc) ((_sc)->sc_cfg->DAC_FIFOS)
76 1.1 jmcneill #define AC_DAC_TXDATA(_sc) ((_sc)->sc_cfg->DAC_TXDATA)
77 1.1 jmcneill #define AC_ADC_FIFOC(_sc) ((_sc)->sc_cfg->ADC_FIFOC)
78 1.1 jmcneill #define ADC_FIFOC_FS __BITS(31,29)
79 1.1 jmcneill #define ADC_FS_48KHZ 0
80 1.1 jmcneill #define ADC_FIFOC_EN_AD __BIT(28)
81 1.1 jmcneill #define ADC_FIFOC_RX_FIFO_MODE __BIT(24)
82 1.1 jmcneill #define ADC_FIFOC_RX_TRIG_LEVEL __BITS(12,8)
83 1.1 jmcneill #define ADC_FIFOC_MONO_EN __BIT(7)
84 1.1 jmcneill #define ADC_FIFOC_RX_BITS __BIT(6)
85 1.1 jmcneill #define ADC_FIFOC_DRQ_EN __BIT(4)
86 1.1 jmcneill #define ADC_FIFOC_FIFO_FLUSH __BIT(0)
87 1.1 jmcneill #define AC_ADC_FIFOS(_sc) ((_sc)->sc_cfg->ADC_FIFOS)
88 1.1 jmcneill #define AC_ADC_RXDATA(_sc) ((_sc)->sc_cfg->ADC_RXDATA)
89 1.1 jmcneill #define AC_DAC_CNT(_sc) ((_sc)->sc_cfg->DAC_CNT)
90 1.1 jmcneill #define AC_ADC_CNT(_sc) ((_sc)->sc_cfg->ADC_CNT)
91 1.1 jmcneill
92 1.1 jmcneill static const struct of_compat_data compat_data[] = {
93 1.2 jmcneill A10_CODEC_COMPATDATA,
94 1.3 jmcneill A31_CODEC_COMPATDATA,
95 1.1 jmcneill H3_CODEC_COMPATDATA,
96 1.1 jmcneill { NULL }
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.1 jmcneill sunxi_codec_open(void *priv, int flags)
167 1.1 jmcneill {
168 1.1 jmcneill return 0;
169 1.1 jmcneill }
170 1.1 jmcneill
171 1.1 jmcneill static void
172 1.1 jmcneill sunxi_codec_close(void *priv)
173 1.1 jmcneill {
174 1.1 jmcneill }
175 1.1 jmcneill
176 1.1 jmcneill static int
177 1.1 jmcneill sunxi_codec_query_encoding(void *priv, struct audio_encoding *ae)
178 1.1 jmcneill {
179 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
180 1.1 jmcneill
181 1.1 jmcneill return auconv_query_encoding(sc->sc_encodings, ae);
182 1.1 jmcneill }
183 1.1 jmcneill
184 1.1 jmcneill static int
185 1.1 jmcneill sunxi_codec_set_params(void *priv, int setmode, int usemode,
186 1.1 jmcneill audio_params_t *play, audio_params_t *rec,
187 1.1 jmcneill stream_filter_list_t *pfil, stream_filter_list_t *rfil)
188 1.1 jmcneill {
189 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
190 1.1 jmcneill int index;
191 1.1 jmcneill
192 1.1 jmcneill if (play && (setmode & AUMODE_PLAY)) {
193 1.1 jmcneill index = auconv_set_converter(&sc->sc_format, 1,
194 1.1 jmcneill AUMODE_PLAY, play, true, pfil);
195 1.1 jmcneill if (index < 0)
196 1.1 jmcneill return EINVAL;
197 1.1 jmcneill sc->sc_pchan.ch_params = pfil->req_size > 0 ?
198 1.1 jmcneill pfil->filters[0].param : *play;
199 1.1 jmcneill }
200 1.1 jmcneill if (rec && (setmode & AUMODE_RECORD)) {
201 1.1 jmcneill index = auconv_set_converter(&sc->sc_format, 1,
202 1.1 jmcneill AUMODE_RECORD, rec, true, rfil);
203 1.1 jmcneill if (index < 0)
204 1.1 jmcneill return EINVAL;
205 1.1 jmcneill sc->sc_rchan.ch_params = rfil->req_size > 0 ?
206 1.1 jmcneill rfil->filters[0].param : *rec;
207 1.1 jmcneill }
208 1.1 jmcneill
209 1.1 jmcneill return 0;
210 1.1 jmcneill }
211 1.1 jmcneill
212 1.1 jmcneill static int
213 1.1 jmcneill sunxi_codec_set_port(void *priv, mixer_ctrl_t *mc)
214 1.1 jmcneill {
215 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
216 1.1 jmcneill
217 1.1 jmcneill return sc->sc_cfg->set_port(sc, mc);
218 1.1 jmcneill }
219 1.1 jmcneill
220 1.1 jmcneill static int
221 1.1 jmcneill sunxi_codec_get_port(void *priv, mixer_ctrl_t *mc)
222 1.1 jmcneill {
223 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
224 1.1 jmcneill
225 1.1 jmcneill return sc->sc_cfg->get_port(sc, mc);
226 1.1 jmcneill }
227 1.1 jmcneill
228 1.1 jmcneill static int
229 1.1 jmcneill sunxi_codec_query_devinfo(void *priv, mixer_devinfo_t *di)
230 1.1 jmcneill {
231 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
232 1.1 jmcneill
233 1.1 jmcneill return sc->sc_cfg->query_devinfo(sc, di);
234 1.1 jmcneill }
235 1.1 jmcneill
236 1.1 jmcneill static void *
237 1.1 jmcneill sunxi_codec_allocm(void *priv, int dir, size_t size)
238 1.1 jmcneill {
239 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
240 1.1 jmcneill struct sunxi_codec_dma *dma;
241 1.1 jmcneill int error;
242 1.1 jmcneill
243 1.1 jmcneill dma = kmem_alloc(sizeof(*dma), KM_SLEEP);
244 1.1 jmcneill
245 1.1 jmcneill error = sunxi_codec_allocdma(sc, size, 16, dma);
246 1.1 jmcneill if (error) {
247 1.1 jmcneill kmem_free(dma, sizeof(*dma));
248 1.1 jmcneill device_printf(sc->sc_dev, "couldn't allocate DMA memory (%d)\n",
249 1.1 jmcneill error);
250 1.1 jmcneill return NULL;
251 1.1 jmcneill }
252 1.1 jmcneill
253 1.1 jmcneill LIST_INSERT_HEAD(&sc->sc_dmalist, dma, dma_list);
254 1.1 jmcneill
255 1.1 jmcneill return dma->dma_addr;
256 1.1 jmcneill }
257 1.1 jmcneill
258 1.1 jmcneill static void
259 1.1 jmcneill sunxi_codec_freem(void *priv, void *addr, size_t size)
260 1.1 jmcneill {
261 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
262 1.1 jmcneill struct sunxi_codec_dma *dma;
263 1.1 jmcneill
264 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
265 1.1 jmcneill if (dma->dma_addr == addr) {
266 1.1 jmcneill sunxi_codec_freedma(sc, dma);
267 1.1 jmcneill LIST_REMOVE(dma, dma_list);
268 1.1 jmcneill kmem_free(dma, sizeof(*dma));
269 1.1 jmcneill break;
270 1.1 jmcneill }
271 1.1 jmcneill }
272 1.1 jmcneill
273 1.1 jmcneill static paddr_t
274 1.1 jmcneill sunxi_codec_mappage(void *priv, void *addr, off_t off, int prot)
275 1.1 jmcneill {
276 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
277 1.1 jmcneill struct sunxi_codec_dma *dma;
278 1.1 jmcneill
279 1.1 jmcneill if (off < 0)
280 1.1 jmcneill return -1;
281 1.1 jmcneill
282 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
283 1.1 jmcneill if (dma->dma_addr == addr) {
284 1.1 jmcneill return bus_dmamem_mmap(sc->sc_dmat, dma->dma_segs,
285 1.1 jmcneill dma->dma_nsegs, off, prot, BUS_DMA_WAITOK);
286 1.1 jmcneill }
287 1.1 jmcneill
288 1.1 jmcneill return -1;
289 1.1 jmcneill }
290 1.1 jmcneill
291 1.1 jmcneill static int
292 1.1 jmcneill sunxi_codec_getdev(void *priv, struct audio_device *adev)
293 1.1 jmcneill {
294 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
295 1.1 jmcneill
296 1.1 jmcneill snprintf(adev->name, sizeof(adev->name), "Allwinner");
297 1.1 jmcneill snprintf(adev->version, sizeof(adev->version), "%s",
298 1.1 jmcneill sc->sc_cfg->name);
299 1.1 jmcneill snprintf(adev->config, sizeof(adev->config), "sunxicodec");
300 1.1 jmcneill
301 1.1 jmcneill return 0;
302 1.1 jmcneill }
303 1.1 jmcneill
304 1.1 jmcneill static int
305 1.1 jmcneill sunxi_codec_get_props(void *priv)
306 1.1 jmcneill {
307 1.1 jmcneill return AUDIO_PROP_PLAYBACK|AUDIO_PROP_CAPTURE|
308 1.1 jmcneill AUDIO_PROP_INDEPENDENT|AUDIO_PROP_MMAP|
309 1.1 jmcneill AUDIO_PROP_FULLDUPLEX;
310 1.1 jmcneill }
311 1.1 jmcneill
312 1.1 jmcneill static int
313 1.1 jmcneill sunxi_codec_round_blocksize(void *priv, int bs, int mode,
314 1.1 jmcneill const audio_params_t *params)
315 1.1 jmcneill {
316 1.1 jmcneill bs &= ~3;
317 1.1 jmcneill if (bs == 0)
318 1.1 jmcneill bs = 4;
319 1.1 jmcneill return bs;
320 1.1 jmcneill }
321 1.1 jmcneill
322 1.1 jmcneill static size_t
323 1.1 jmcneill sunxi_codec_round_buffersize(void *priv, int dir, size_t bufsize)
324 1.1 jmcneill {
325 1.1 jmcneill return bufsize;
326 1.1 jmcneill }
327 1.1 jmcneill
328 1.1 jmcneill static int
329 1.1 jmcneill sunxi_codec_trigger_output(void *priv, void *start, void *end, int blksize,
330 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
331 1.1 jmcneill {
332 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
333 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_pchan;
334 1.1 jmcneill struct sunxi_codec_dma *dma;
335 1.1 jmcneill bus_addr_t pstart;
336 1.1 jmcneill bus_size_t psize;
337 1.1 jmcneill uint32_t val;
338 1.1 jmcneill int error;
339 1.1 jmcneill
340 1.1 jmcneill pstart = 0;
341 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start;
342 1.1 jmcneill
343 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
344 1.1 jmcneill if (dma->dma_addr == start) {
345 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr;
346 1.1 jmcneill break;
347 1.1 jmcneill }
348 1.1 jmcneill if (pstart == 0) {
349 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start);
350 1.1 jmcneill return EINVAL;
351 1.1 jmcneill }
352 1.1 jmcneill
353 1.1 jmcneill ch->ch_intr = intr;
354 1.1 jmcneill ch->ch_intrarg = intrarg;
355 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart;
356 1.1 jmcneill ch->ch_end_phys = pstart + psize;
357 1.1 jmcneill ch->ch_blksize = blksize;
358 1.1 jmcneill
359 1.1 jmcneill /* Flush DAC FIFO */
360 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
361 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val | DAC_FIFOC_FIFO_FLUSH);
362 1.1 jmcneill
363 1.1 jmcneill /* Clear DAC FIFO status */
364 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOS(sc));
365 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOS(sc), val);
366 1.1 jmcneill
367 1.1 jmcneill /* Unmute output */
368 1.1 jmcneill if (sc->sc_cfg->mute)
369 1.1 jmcneill sc->sc_cfg->mute(sc, 0, ch->ch_mode);
370 1.1 jmcneill
371 1.1 jmcneill /* Configure DAC FIFO */
372 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc),
373 1.1 jmcneill __SHIFTIN(DAC_FS_48KHZ, DAC_FIFOC_FS) |
374 1.1 jmcneill __SHIFTIN(FIFO_MODE_16_15_0, DAC_FIFOC_FIFO_MODE) |
375 1.1 jmcneill __SHIFTIN(DRQ_CLR_CNT, DAC_FIFOC_DRQ_CLR_CNT) |
376 1.1 jmcneill __SHIFTIN(TX_TRIG_LEVEL, DAC_FIFOC_TX_TRIG_LEVEL));
377 1.1 jmcneill
378 1.1 jmcneill /* Enable DAC DRQ */
379 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
380 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val | DAC_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_trigger_input(void *priv, void *start, void *end, int blksize,
395 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
396 1.1 jmcneill {
397 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
398 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_rchan;
399 1.1 jmcneill struct sunxi_codec_dma *dma;
400 1.1 jmcneill bus_addr_t pstart;
401 1.1 jmcneill bus_size_t psize;
402 1.1 jmcneill uint32_t val;
403 1.1 jmcneill int error;
404 1.1 jmcneill
405 1.1 jmcneill pstart = 0;
406 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start;
407 1.1 jmcneill
408 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
409 1.1 jmcneill if (dma->dma_addr == start) {
410 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr;
411 1.1 jmcneill break;
412 1.1 jmcneill }
413 1.1 jmcneill if (pstart == 0) {
414 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start);
415 1.1 jmcneill return EINVAL;
416 1.1 jmcneill }
417 1.1 jmcneill
418 1.1 jmcneill ch->ch_intr = intr;
419 1.1 jmcneill ch->ch_intrarg = intrarg;
420 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart;
421 1.1 jmcneill ch->ch_end_phys = pstart + psize;
422 1.1 jmcneill ch->ch_blksize = blksize;
423 1.1 jmcneill
424 1.1 jmcneill /* Flush ADC FIFO */
425 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
426 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val | ADC_FIFOC_FIFO_FLUSH);
427 1.1 jmcneill
428 1.1 jmcneill /* Clear ADC FIFO status */
429 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOS(sc));
430 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOS(sc), val);
431 1.1 jmcneill
432 1.1 jmcneill /* Unmute input */
433 1.1 jmcneill if (sc->sc_cfg->mute)
434 1.1 jmcneill sc->sc_cfg->mute(sc, 0, ch->ch_mode);
435 1.1 jmcneill
436 1.1 jmcneill /* Configure ADC FIFO */
437 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc),
438 1.1 jmcneill __SHIFTIN(ADC_FS_48KHZ, ADC_FIFOC_FS) |
439 1.1 jmcneill __SHIFTIN(RX_TRIG_LEVEL, ADC_FIFOC_RX_TRIG_LEVEL) |
440 1.1 jmcneill ADC_FIFOC_EN_AD | ADC_FIFOC_RX_FIFO_MODE);
441 1.1 jmcneill
442 1.1 jmcneill /* Enable ADC DRQ */
443 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
444 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val | ADC_FIFOC_DRQ_EN);
445 1.1 jmcneill
446 1.1 jmcneill /* Start DMA transfer */
447 1.1 jmcneill error = sunxi_codec_transfer(ch);
448 1.1 jmcneill if (error != 0) {
449 1.1 jmcneill aprint_error_dev(sc->sc_dev,
450 1.1 jmcneill "failed to start DMA transfer: %d\n", error);
451 1.1 jmcneill return error;
452 1.1 jmcneill }
453 1.1 jmcneill
454 1.1 jmcneill return 0;
455 1.1 jmcneill }
456 1.1 jmcneill
457 1.1 jmcneill static int
458 1.1 jmcneill sunxi_codec_halt_output(void *priv)
459 1.1 jmcneill {
460 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
461 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_pchan;
462 1.1 jmcneill uint32_t val;
463 1.1 jmcneill
464 1.1 jmcneill /* Disable DMA channel */
465 1.1 jmcneill fdtbus_dma_halt(ch->ch_dma);
466 1.1 jmcneill
467 1.5 bouyer /* flush fifo */
468 1.5 bouyer val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
469 1.5 bouyer CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val | DAC_FIFOC_FIFO_FLUSH);
470 1.5 bouyer while (val & DAC_FIFOC_FIFO_FLUSH)
471 1.5 bouyer val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
472 1.5 bouyer
473 1.1 jmcneill /* Mute output */
474 1.1 jmcneill if (sc->sc_cfg->mute)
475 1.1 jmcneill sc->sc_cfg->mute(sc, 1, ch->ch_mode);
476 1.1 jmcneill
477 1.1 jmcneill /* Disable DAC DRQ */
478 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_FIFOC(sc));
479 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_FIFOC(sc), val & ~DAC_FIFOC_DRQ_EN);
480 1.1 jmcneill
481 1.1 jmcneill ch->ch_intr = NULL;
482 1.1 jmcneill ch->ch_intrarg = NULL;
483 1.1 jmcneill
484 1.1 jmcneill return 0;
485 1.1 jmcneill }
486 1.1 jmcneill
487 1.1 jmcneill static int
488 1.1 jmcneill sunxi_codec_halt_input(void *priv)
489 1.1 jmcneill {
490 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
491 1.1 jmcneill struct sunxi_codec_chan *ch = &sc->sc_rchan;
492 1.1 jmcneill uint32_t val;
493 1.1 jmcneill
494 1.1 jmcneill /* Mute output */
495 1.1 jmcneill if (sc->sc_cfg->mute)
496 1.1 jmcneill sc->sc_cfg->mute(sc, 1, ch->ch_mode);
497 1.1 jmcneill
498 1.5 bouyer /* flush fifo */
499 1.5 bouyer val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
500 1.5 bouyer CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val | ADC_FIFOC_FIFO_FLUSH);
501 1.5 bouyer while (val & ADC_FIFOC_FIFO_FLUSH)
502 1.5 bouyer val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
503 1.5 bouyer
504 1.5 bouyer /* Disable DMA channel */
505 1.5 bouyer fdtbus_dma_halt(ch->ch_dma);
506 1.5 bouyer
507 1.1 jmcneill /* Disable ADC DRQ */
508 1.1 jmcneill val = CODEC_READ(sc, AC_ADC_FIFOC(sc));
509 1.1 jmcneill CODEC_WRITE(sc, AC_ADC_FIFOC(sc), val & ~ADC_FIFOC_DRQ_EN);
510 1.1 jmcneill
511 1.1 jmcneill return 0;
512 1.1 jmcneill }
513 1.1 jmcneill
514 1.1 jmcneill static void
515 1.1 jmcneill sunxi_codec_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
516 1.1 jmcneill {
517 1.1 jmcneill struct sunxi_codec_softc * const sc = priv;
518 1.1 jmcneill
519 1.1 jmcneill *intr = &sc->sc_intr_lock;
520 1.1 jmcneill *thread = &sc->sc_lock;
521 1.1 jmcneill }
522 1.1 jmcneill
523 1.1 jmcneill static const struct audio_hw_if sunxi_codec_hw_if = {
524 1.1 jmcneill .open = sunxi_codec_open,
525 1.1 jmcneill .close = sunxi_codec_close,
526 1.5 bouyer .drain = NULL,
527 1.1 jmcneill .query_encoding = sunxi_codec_query_encoding,
528 1.1 jmcneill .set_params = sunxi_codec_set_params,
529 1.1 jmcneill .allocm = sunxi_codec_allocm,
530 1.1 jmcneill .freem = sunxi_codec_freem,
531 1.1 jmcneill .mappage = sunxi_codec_mappage,
532 1.1 jmcneill .getdev = sunxi_codec_getdev,
533 1.1 jmcneill .set_port = sunxi_codec_set_port,
534 1.1 jmcneill .get_port = sunxi_codec_get_port,
535 1.1 jmcneill .query_devinfo = sunxi_codec_query_devinfo,
536 1.1 jmcneill .get_props = sunxi_codec_get_props,
537 1.1 jmcneill .round_blocksize = sunxi_codec_round_blocksize,
538 1.1 jmcneill .round_buffersize = sunxi_codec_round_buffersize,
539 1.1 jmcneill .trigger_output = sunxi_codec_trigger_output,
540 1.1 jmcneill .trigger_input = sunxi_codec_trigger_input,
541 1.1 jmcneill .halt_output = sunxi_codec_halt_output,
542 1.1 jmcneill .halt_input = sunxi_codec_halt_input,
543 1.1 jmcneill .get_locks = sunxi_codec_get_locks,
544 1.1 jmcneill };
545 1.1 jmcneill
546 1.1 jmcneill static void
547 1.1 jmcneill sunxi_codec_dmaintr(void *priv)
548 1.1 jmcneill {
549 1.1 jmcneill struct sunxi_codec_chan * const ch = priv;
550 1.4 bouyer struct sunxi_codec_softc * const sc = ch->ch_sc;
551 1.1 jmcneill
552 1.4 bouyer mutex_enter(&sc->sc_intr_lock);
553 1.1 jmcneill ch->ch_cur_phys += ch->ch_blksize;
554 1.1 jmcneill if (ch->ch_cur_phys >= ch->ch_end_phys)
555 1.1 jmcneill ch->ch_cur_phys = ch->ch_start_phys;
556 1.1 jmcneill
557 1.1 jmcneill if (ch->ch_intr) {
558 1.1 jmcneill ch->ch_intr(ch->ch_intrarg);
559 1.1 jmcneill sunxi_codec_transfer(ch);
560 1.1 jmcneill }
561 1.4 bouyer mutex_exit(&sc->sc_intr_lock);
562 1.1 jmcneill }
563 1.1 jmcneill
564 1.1 jmcneill static int
565 1.1 jmcneill sunxi_codec_chan_init(struct sunxi_codec_softc *sc,
566 1.1 jmcneill struct sunxi_codec_chan *ch, u_int mode, const char *dmaname)
567 1.1 jmcneill {
568 1.1 jmcneill ch->ch_sc = sc;
569 1.1 jmcneill ch->ch_mode = mode;
570 1.1 jmcneill ch->ch_dma = fdtbus_dma_get(sc->sc_phandle, dmaname, sunxi_codec_dmaintr, ch);
571 1.1 jmcneill if (ch->ch_dma == NULL) {
572 1.1 jmcneill aprint_error(": couldn't get dma channel \"%s\"\n", dmaname);
573 1.1 jmcneill return ENXIO;
574 1.1 jmcneill }
575 1.1 jmcneill
576 1.1 jmcneill if (mode == AUMODE_PLAY) {
577 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_WRITE;
578 1.1 jmcneill ch->ch_req.dreq_dev_phys =
579 1.1 jmcneill sc->sc_baseaddr + AC_DAC_TXDATA(sc);
580 1.1 jmcneill } else {
581 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_READ;
582 1.1 jmcneill ch->ch_req.dreq_dev_phys =
583 1.1 jmcneill sc->sc_baseaddr + AC_ADC_RXDATA(sc);
584 1.1 jmcneill }
585 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_bus_width = 16;
586 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_burst_len = 4;
587 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_bus_width = 16;
588 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_burst_len = 4;
589 1.1 jmcneill
590 1.1 jmcneill return 0;
591 1.1 jmcneill }
592 1.1 jmcneill
593 1.1 jmcneill static int
594 1.1 jmcneill sunxi_codec_clock_init(int phandle)
595 1.1 jmcneill {
596 1.1 jmcneill struct fdtbus_reset *rst;
597 1.1 jmcneill struct clk *clk;
598 1.1 jmcneill int error;
599 1.1 jmcneill
600 1.1 jmcneill /* Set codec clock to 24.576MHz, suitable for 48 kHz sampling rates */
601 1.1 jmcneill clk = fdtbus_clock_get(phandle, "codec");
602 1.1 jmcneill if (clk == NULL) {
603 1.1 jmcneill aprint_error(": couldn't find codec clock\n");
604 1.1 jmcneill return ENXIO;
605 1.1 jmcneill }
606 1.1 jmcneill error = clk_set_rate(clk, 24576000);
607 1.1 jmcneill if (error != 0) {
608 1.1 jmcneill aprint_error(": couldn't set codec clock rate: %d\n", error);
609 1.1 jmcneill return error;
610 1.1 jmcneill }
611 1.1 jmcneill error = clk_enable(clk);
612 1.1 jmcneill if (error != 0) {
613 1.1 jmcneill aprint_error(": couldn't enable codec clock: %d\n", error);
614 1.1 jmcneill return error;
615 1.1 jmcneill }
616 1.1 jmcneill
617 1.1 jmcneill /* Enable APB clock */
618 1.1 jmcneill clk = fdtbus_clock_get(phandle, "apb");
619 1.1 jmcneill if (clk == NULL) {
620 1.1 jmcneill aprint_error(": couldn't find apb clock\n");
621 1.1 jmcneill return ENXIO;
622 1.1 jmcneill }
623 1.1 jmcneill error = clk_enable(clk);
624 1.1 jmcneill if (error != 0) {
625 1.1 jmcneill aprint_error(": couldn't enable apb clock: %d\n", error);
626 1.1 jmcneill return error;
627 1.1 jmcneill }
628 1.1 jmcneill
629 1.1 jmcneill /* De-assert reset */
630 1.1 jmcneill rst = fdtbus_reset_get_index(phandle, 0);
631 1.2 jmcneill if (rst != NULL) {
632 1.2 jmcneill error = fdtbus_reset_deassert(rst);
633 1.2 jmcneill if (error != 0) {
634 1.2 jmcneill aprint_error(": couldn't de-assert reset: %d\n", error);
635 1.2 jmcneill return error;
636 1.2 jmcneill }
637 1.1 jmcneill }
638 1.1 jmcneill
639 1.1 jmcneill return 0;
640 1.1 jmcneill }
641 1.1 jmcneill
642 1.1 jmcneill static int
643 1.1 jmcneill sunxi_codec_match(device_t parent, cfdata_t cf, void *aux)
644 1.1 jmcneill {
645 1.1 jmcneill struct fdt_attach_args * const faa = aux;
646 1.1 jmcneill
647 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
648 1.1 jmcneill }
649 1.1 jmcneill
650 1.1 jmcneill static void
651 1.1 jmcneill sunxi_codec_attach(device_t parent, device_t self, void *aux)
652 1.1 jmcneill {
653 1.1 jmcneill struct sunxi_codec_softc * const sc = device_private(self);
654 1.1 jmcneill struct fdt_attach_args * const faa = aux;
655 1.1 jmcneill const int phandle = faa->faa_phandle;
656 1.1 jmcneill bus_addr_t addr;
657 1.1 jmcneill bus_size_t size;
658 1.1 jmcneill uint32_t val;
659 1.1 jmcneill int error;
660 1.1 jmcneill
661 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
662 1.1 jmcneill aprint_error(": couldn't get registers\n");
663 1.1 jmcneill return;
664 1.1 jmcneill }
665 1.1 jmcneill
666 1.1 jmcneill if (sunxi_codec_clock_init(phandle) != 0)
667 1.1 jmcneill return;
668 1.1 jmcneill
669 1.1 jmcneill sc->sc_dev = self;
670 1.1 jmcneill sc->sc_phandle = phandle;
671 1.1 jmcneill sc->sc_baseaddr = addr;
672 1.1 jmcneill sc->sc_bst = faa->faa_bst;
673 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
674 1.1 jmcneill aprint_error(": couldn't map registers\n");
675 1.1 jmcneill return;
676 1.1 jmcneill }
677 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
678 1.1 jmcneill LIST_INIT(&sc->sc_dmalist);
679 1.1 jmcneill sc->sc_cfg = (void *)of_search_compatible(phandle, compat_data)->data;
680 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
681 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
682 1.1 jmcneill
683 1.1 jmcneill if (sunxi_codec_chan_init(sc, &sc->sc_pchan, AUMODE_PLAY, "tx") != 0 ||
684 1.1 jmcneill sunxi_codec_chan_init(sc, &sc->sc_rchan, AUMODE_RECORD, "rx") != 0) {
685 1.1 jmcneill aprint_error(": couldn't setup channels\n");
686 1.1 jmcneill return;
687 1.1 jmcneill }
688 1.1 jmcneill
689 1.1 jmcneill /* Optional PA mute GPIO */
690 1.1 jmcneill sc->sc_pin_pa = fdtbus_gpio_acquire(phandle, "allwinner,pa-gpios", GPIO_PIN_OUTPUT);
691 1.1 jmcneill
692 1.1 jmcneill aprint_naive("\n");
693 1.1 jmcneill aprint_normal(": %s\n", sc->sc_cfg->name);
694 1.1 jmcneill
695 1.1 jmcneill /* Enable DAC */
696 1.1 jmcneill val = CODEC_READ(sc, AC_DAC_DPC(sc));
697 1.1 jmcneill val |= DAC_DPC_EN_DA;
698 1.1 jmcneill CODEC_WRITE(sc, AC_DAC_DPC(sc), val);
699 1.1 jmcneill
700 1.1 jmcneill /* Initialize codec */
701 1.1 jmcneill if (sc->sc_cfg->init(sc) != 0) {
702 1.1 jmcneill aprint_error_dev(self, "couldn't initialize codec\n");
703 1.1 jmcneill return;
704 1.1 jmcneill }
705 1.1 jmcneill
706 1.1 jmcneill sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
707 1.1 jmcneill sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
708 1.1 jmcneill sc->sc_format.validbits = 16;
709 1.1 jmcneill sc->sc_format.precision = 16;
710 1.1 jmcneill sc->sc_format.channels = 2;
711 1.1 jmcneill sc->sc_format.channel_mask = AUFMT_STEREO;
712 1.1 jmcneill sc->sc_format.frequency_type = 0;
713 1.1 jmcneill sc->sc_format.frequency[0] = sc->sc_format.frequency[1] = 48000;
714 1.1 jmcneill
715 1.1 jmcneill error = auconv_create_encodings(&sc->sc_format, 1, &sc->sc_encodings);
716 1.1 jmcneill if (error) {
717 1.1 jmcneill aprint_error_dev(self, "couldn't create encodings\n");
718 1.1 jmcneill return;
719 1.1 jmcneill }
720 1.1 jmcneill
721 1.1 jmcneill audio_attach_mi(&sunxi_codec_hw_if, sc, self);
722 1.1 jmcneill }
723 1.1 jmcneill
724 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_codec, sizeof(struct sunxi_codec_softc),
725 1.1 jmcneill sunxi_codec_match, sunxi_codec_attach, NULL, NULL);
726 1.1 jmcneill
727 1.1 jmcneill #ifdef DDB
728 1.1 jmcneill void sunxicodec_dump(void);
729 1.1 jmcneill
730 1.1 jmcneill void
731 1.1 jmcneill sunxicodec_dump(void)
732 1.1 jmcneill {
733 1.1 jmcneill struct sunxi_codec_softc *sc;
734 1.1 jmcneill device_t dev;
735 1.1 jmcneill
736 1.1 jmcneill dev = device_find_by_driver_unit("sunxicodec", 0);
737 1.1 jmcneill if (dev == NULL)
738 1.1 jmcneill return;
739 1.1 jmcneill sc = device_private(dev);
740 1.1 jmcneill
741 1.1 jmcneill device_printf(dev, "AC_DAC_DPC: %08x\n", CODEC_READ(sc, AC_DAC_DPC(sc)));
742 1.1 jmcneill device_printf(dev, "AC_DAC_FIFOC: %08x\n", CODEC_READ(sc, AC_DAC_FIFOC(sc)));
743 1.1 jmcneill device_printf(dev, "AC_DAC_FIFOS: %08x\n", CODEC_READ(sc, AC_DAC_FIFOS(sc)));
744 1.1 jmcneill device_printf(dev, "AC_ADC_FIFOC: %08x\n", CODEC_READ(sc, AC_ADC_FIFOC(sc)));
745 1.1 jmcneill device_printf(dev, "AC_ADC_FIFOS: %08x\n", CODEC_READ(sc, AC_ADC_FIFOS(sc)));
746 1.1 jmcneill device_printf(dev, "AC_DAC_CNT: %08x\n", CODEC_READ(sc, AC_DAC_CNT(sc)));
747 1.1 jmcneill device_printf(dev, "AC_ADC_CNT: %08x\n", CODEC_READ(sc, AC_ADC_CNT(sc)));
748 1.1 jmcneill }
749 1.1 jmcneill #endif
750