sunxi_i2s.c revision 1.4 1 1.4 isaki /* $NetBSD: sunxi_i2s.c,v 1.4 2019/05/08 13:40:14 isaki Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 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 <sys/cdefs.h>
30 1.4 isaki __KERNEL_RCSID(0, "$NetBSD: sunxi_i2s.c,v 1.4 2019/05/08 13:40:14 isaki Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/cpu.h>
35 1.1 jmcneill #include <sys/device.h>
36 1.1 jmcneill #include <sys/kmem.h>
37 1.1 jmcneill #include <sys/gpio.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <sys/audioio.h>
40 1.4 isaki #include <dev/audio/audio_if.h>
41 1.4 isaki #include <dev/audio/linear.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <dev/fdt/fdtvar.h>
44 1.1 jmcneill
45 1.1 jmcneill #define SUNXI_I2S_CLK_RATE 24576000
46 1.1 jmcneill
47 1.1 jmcneill #define DA_CTL 0x00
48 1.1 jmcneill #define DA_CTL_SDO_EN __BIT(8)
49 1.1 jmcneill #define DA_CTL_MS __BIT(5)
50 1.1 jmcneill #define DA_CTL_PCM __BIT(4)
51 1.1 jmcneill #define DA_CTL_TXEN __BIT(2)
52 1.1 jmcneill #define DA_CTL_RXEN __BIT(1)
53 1.1 jmcneill #define DA_CTL_GEN __BIT(0)
54 1.1 jmcneill #define DA_FAT0 0x04
55 1.1 jmcneill #define DA_FAT0_LRCP __BIT(7)
56 1.1 jmcneill #define DA_LRCP_NORMAL 0
57 1.1 jmcneill #define DA_LRCP_INVERTED 1
58 1.1 jmcneill #define DA_FAT0_BCP __BIT(6)
59 1.1 jmcneill #define DA_BCP_NORMAL 0
60 1.1 jmcneill #define DA_BCP_INVERTED 1
61 1.1 jmcneill #define DA_FAT0_SR __BITS(5,4)
62 1.1 jmcneill #define DA_FAT0_WSS __BITS(3,2)
63 1.1 jmcneill #define DA_FAT0_FMT __BITS(1,0)
64 1.1 jmcneill #define DA_FMT_I2S 0
65 1.1 jmcneill #define DA_FMT_LJ 1
66 1.1 jmcneill #define DA_FMT_RJ 2
67 1.1 jmcneill #define DA_FAT1 0x08
68 1.1 jmcneill #define DA_ISTA 0x0c
69 1.1 jmcneill #define DA_RXFIFO 0x10
70 1.1 jmcneill #define DA_FCTL 0x14
71 1.1 jmcneill #define DA_FCTL_HUB_EN __BIT(31)
72 1.1 jmcneill #define DA_FCTL_FTX __BIT(25)
73 1.1 jmcneill #define DA_FCTL_FRX __BIT(24)
74 1.3 jmcneill #define DA_FCTL_TXIM __BIT(2)
75 1.3 jmcneill #define DA_FCTL_RXIM __BITS(1,0)
76 1.1 jmcneill #define DA_FSTA 0x18
77 1.1 jmcneill #define DA_INT 0x1c
78 1.1 jmcneill #define DA_INT_TX_DRQ __BIT(7)
79 1.1 jmcneill #define DA_INT_RX_DRQ __BIT(3)
80 1.1 jmcneill #define DA_TXFIFO 0x20
81 1.1 jmcneill #define DA_CLKD 0x24
82 1.1 jmcneill #define DA_CLKD_MCLKO_EN __BIT(7)
83 1.1 jmcneill #define DA_CLKD_BCLKDIV __BITS(6,4)
84 1.3 jmcneill #define DA_CLKD_BCLKDIV_8 3
85 1.1 jmcneill #define DA_CLKD_BCLKDIV_16 5
86 1.1 jmcneill #define DA_CLKD_MCLKDIV __BITS(3,0)
87 1.1 jmcneill #define DA_CLKD_MCLKDIV_1 0
88 1.1 jmcneill #define DA_TXCNT 0x28
89 1.1 jmcneill #define DA_RXCNT 0x2c
90 1.1 jmcneill
91 1.1 jmcneill #define DA_CHSEL_EN __BITS(11,4)
92 1.1 jmcneill #define DA_CHSEL_SEL __BITS(2,0)
93 1.1 jmcneill
94 1.1 jmcneill struct sunxi_i2s_config {
95 1.1 jmcneill const char *name;
96 1.1 jmcneill bus_size_t txchsel;
97 1.1 jmcneill bus_size_t txchmap;
98 1.1 jmcneill bus_size_t rxchsel;
99 1.1 jmcneill bus_size_t rxchmap;
100 1.1 jmcneill };
101 1.1 jmcneill
102 1.1 jmcneill static const struct sunxi_i2s_config sun50i_a64_codec_config = {
103 1.1 jmcneill .name = "Audio Codec (digital part)",
104 1.1 jmcneill .txchsel = 0x30,
105 1.1 jmcneill .txchmap = 0x34,
106 1.1 jmcneill .rxchsel = 0x38,
107 1.1 jmcneill .rxchmap = 0x3c,
108 1.1 jmcneill };
109 1.1 jmcneill
110 1.1 jmcneill static const struct of_compat_data compat_data[] = {
111 1.1 jmcneill { "allwinner,sun50i-a64-acodec-i2s",
112 1.1 jmcneill (uintptr_t)&sun50i_a64_codec_config },
113 1.1 jmcneill
114 1.1 jmcneill { NULL }
115 1.1 jmcneill };
116 1.1 jmcneill
117 1.1 jmcneill struct sunxi_i2s_softc;
118 1.1 jmcneill
119 1.1 jmcneill struct sunxi_i2s_chan {
120 1.1 jmcneill struct sunxi_i2s_softc *ch_sc;
121 1.1 jmcneill u_int ch_mode;
122 1.1 jmcneill
123 1.1 jmcneill struct fdtbus_dma *ch_dma;
124 1.1 jmcneill struct fdtbus_dma_req ch_req;
125 1.1 jmcneill
126 1.1 jmcneill audio_params_t ch_params;
127 1.1 jmcneill
128 1.1 jmcneill bus_addr_t ch_start_phys;
129 1.1 jmcneill bus_addr_t ch_end_phys;
130 1.1 jmcneill bus_addr_t ch_cur_phys;
131 1.1 jmcneill int ch_blksize;
132 1.1 jmcneill
133 1.1 jmcneill void (*ch_intr)(void *);
134 1.1 jmcneill void *ch_intrarg;
135 1.1 jmcneill };
136 1.1 jmcneill
137 1.1 jmcneill struct sunxi_i2s_dma {
138 1.1 jmcneill LIST_ENTRY(sunxi_i2s_dma) dma_list;
139 1.1 jmcneill bus_dmamap_t dma_map;
140 1.1 jmcneill void *dma_addr;
141 1.1 jmcneill size_t dma_size;
142 1.1 jmcneill bus_dma_segment_t dma_segs[1];
143 1.1 jmcneill int dma_nsegs;
144 1.1 jmcneill };
145 1.1 jmcneill
146 1.1 jmcneill struct sunxi_i2s_softc {
147 1.1 jmcneill device_t sc_dev;
148 1.1 jmcneill bus_space_tag_t sc_bst;
149 1.1 jmcneill bus_space_handle_t sc_bsh;
150 1.1 jmcneill bus_dma_tag_t sc_dmat;
151 1.1 jmcneill int sc_phandle;
152 1.1 jmcneill bus_addr_t sc_baseaddr;
153 1.1 jmcneill
154 1.1 jmcneill struct sunxi_i2s_config *sc_cfg;
155 1.1 jmcneill
156 1.1 jmcneill LIST_HEAD(, sunxi_i2s_dma) sc_dmalist;
157 1.1 jmcneill
158 1.1 jmcneill kmutex_t sc_lock;
159 1.1 jmcneill kmutex_t sc_intr_lock;
160 1.1 jmcneill
161 1.1 jmcneill struct audio_format sc_format;
162 1.1 jmcneill
163 1.1 jmcneill struct sunxi_i2s_chan sc_pchan;
164 1.1 jmcneill struct sunxi_i2s_chan sc_rchan;
165 1.1 jmcneill
166 1.1 jmcneill struct audio_dai_device sc_dai;
167 1.1 jmcneill };
168 1.1 jmcneill
169 1.1 jmcneill #define I2S_READ(sc, reg) \
170 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
171 1.1 jmcneill #define I2S_WRITE(sc, reg, val) \
172 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
173 1.1 jmcneill
174 1.1 jmcneill static int
175 1.1 jmcneill sunxi_i2s_allocdma(struct sunxi_i2s_softc *sc, size_t size,
176 1.1 jmcneill size_t align, struct sunxi_i2s_dma *dma)
177 1.1 jmcneill {
178 1.1 jmcneill int error;
179 1.1 jmcneill
180 1.1 jmcneill dma->dma_size = size;
181 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, dma->dma_size, align, 0,
182 1.1 jmcneill dma->dma_segs, 1, &dma->dma_nsegs, BUS_DMA_WAITOK);
183 1.1 jmcneill if (error)
184 1.1 jmcneill return error;
185 1.1 jmcneill
186 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs,
187 1.1 jmcneill dma->dma_size, &dma->dma_addr, BUS_DMA_WAITOK | BUS_DMA_COHERENT);
188 1.1 jmcneill if (error)
189 1.1 jmcneill goto free;
190 1.1 jmcneill
191 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, dma->dma_size, dma->dma_nsegs,
192 1.1 jmcneill dma->dma_size, 0, BUS_DMA_WAITOK, &dma->dma_map);
193 1.1 jmcneill if (error)
194 1.1 jmcneill goto unmap;
195 1.1 jmcneill
196 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_addr,
197 1.1 jmcneill dma->dma_size, NULL, BUS_DMA_WAITOK);
198 1.1 jmcneill if (error)
199 1.1 jmcneill goto destroy;
200 1.1 jmcneill
201 1.1 jmcneill return 0;
202 1.1 jmcneill
203 1.1 jmcneill destroy:
204 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
205 1.1 jmcneill unmap:
206 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
207 1.1 jmcneill free:
208 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
209 1.1 jmcneill
210 1.1 jmcneill return error;
211 1.1 jmcneill }
212 1.1 jmcneill
213 1.1 jmcneill static void
214 1.1 jmcneill sunxi_i2s_freedma(struct sunxi_i2s_softc *sc, struct sunxi_i2s_dma *dma)
215 1.1 jmcneill {
216 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
217 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
218 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
219 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
220 1.1 jmcneill }
221 1.1 jmcneill
222 1.1 jmcneill static int
223 1.1 jmcneill sunxi_i2s_transfer(struct sunxi_i2s_chan *ch)
224 1.1 jmcneill {
225 1.1 jmcneill bus_dma_segment_t seg;
226 1.1 jmcneill
227 1.1 jmcneill seg.ds_addr = ch->ch_cur_phys;
228 1.1 jmcneill seg.ds_len = ch->ch_blksize;
229 1.1 jmcneill ch->ch_req.dreq_segs = &seg;
230 1.1 jmcneill ch->ch_req.dreq_nsegs = 1;
231 1.1 jmcneill
232 1.1 jmcneill return fdtbus_dma_transfer(ch->ch_dma, &ch->ch_req);
233 1.1 jmcneill }
234 1.1 jmcneill
235 1.1 jmcneill static int
236 1.4 isaki sunxi_i2s_query_format(void *priv, audio_format_query_t *afp)
237 1.1 jmcneill {
238 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
239 1.1 jmcneill
240 1.4 isaki return audio_query_format(&sc->sc_format, 1, afp);
241 1.1 jmcneill }
242 1.1 jmcneill
243 1.1 jmcneill static int
244 1.4 isaki sunxi_i2s_set_format(void *priv, int setmode,
245 1.4 isaki const audio_params_t *play, const audio_params_t *rec,
246 1.4 isaki audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
247 1.1 jmcneill {
248 1.1 jmcneill
249 1.4 isaki if ((setmode & AUMODE_PLAY)) {
250 1.4 isaki pfil->codec = audio_internal_to_linear32;
251 1.4 isaki }
252 1.4 isaki if ((setmode & AUMODE_RECORD)) {
253 1.4 isaki rfil->codec = audio_linear32_to_internal;
254 1.1 jmcneill }
255 1.1 jmcneill
256 1.1 jmcneill return 0;
257 1.1 jmcneill }
258 1.1 jmcneill
259 1.1 jmcneill static void *
260 1.1 jmcneill sunxi_i2s_allocm(void *priv, int dir, size_t size)
261 1.1 jmcneill {
262 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
263 1.1 jmcneill struct sunxi_i2s_dma *dma;
264 1.1 jmcneill int error;
265 1.1 jmcneill
266 1.1 jmcneill dma = kmem_alloc(sizeof(*dma), KM_SLEEP);
267 1.1 jmcneill
268 1.1 jmcneill error = sunxi_i2s_allocdma(sc, size, 16, dma);
269 1.1 jmcneill if (error) {
270 1.1 jmcneill kmem_free(dma, sizeof(*dma));
271 1.1 jmcneill device_printf(sc->sc_dev, "couldn't allocate DMA memory (%d)\n",
272 1.1 jmcneill error);
273 1.1 jmcneill return NULL;
274 1.1 jmcneill }
275 1.1 jmcneill
276 1.1 jmcneill LIST_INSERT_HEAD(&sc->sc_dmalist, dma, dma_list);
277 1.1 jmcneill
278 1.1 jmcneill return dma->dma_addr;
279 1.1 jmcneill }
280 1.1 jmcneill
281 1.1 jmcneill static void
282 1.1 jmcneill sunxi_i2s_freem(void *priv, void *addr, size_t size)
283 1.1 jmcneill {
284 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
285 1.1 jmcneill struct sunxi_i2s_dma *dma;
286 1.1 jmcneill
287 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
288 1.1 jmcneill if (dma->dma_addr == addr) {
289 1.1 jmcneill sunxi_i2s_freedma(sc, dma);
290 1.1 jmcneill LIST_REMOVE(dma, dma_list);
291 1.1 jmcneill kmem_free(dma, sizeof(*dma));
292 1.1 jmcneill break;
293 1.1 jmcneill }
294 1.1 jmcneill }
295 1.1 jmcneill
296 1.1 jmcneill static int
297 1.1 jmcneill sunxi_i2s_get_props(void *priv)
298 1.1 jmcneill {
299 1.1 jmcneill return AUDIO_PROP_PLAYBACK|AUDIO_PROP_CAPTURE|
300 1.2 jmcneill AUDIO_PROP_MMAP|AUDIO_PROP_FULLDUPLEX|AUDIO_PROP_INDEPENDENT;
301 1.1 jmcneill }
302 1.1 jmcneill
303 1.1 jmcneill static int
304 1.1 jmcneill sunxi_i2s_round_blocksize(void *priv, int bs, int mode,
305 1.1 jmcneill const audio_params_t *params)
306 1.1 jmcneill {
307 1.1 jmcneill bs &= ~3;
308 1.1 jmcneill if (bs == 0)
309 1.1 jmcneill bs = 4;
310 1.1 jmcneill return bs;
311 1.1 jmcneill }
312 1.1 jmcneill
313 1.1 jmcneill static int
314 1.1 jmcneill sunxi_i2s_trigger_output(void *priv, void *start, void *end, int blksize,
315 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
316 1.1 jmcneill {
317 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
318 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_pchan;
319 1.1 jmcneill struct sunxi_i2s_dma *dma;
320 1.1 jmcneill bus_addr_t pstart;
321 1.1 jmcneill bus_size_t psize;
322 1.1 jmcneill uint32_t val;
323 1.1 jmcneill int error;
324 1.1 jmcneill
325 1.1 jmcneill pstart = 0;
326 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start;
327 1.1 jmcneill
328 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
329 1.1 jmcneill if (dma->dma_addr == start) {
330 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr;
331 1.1 jmcneill break;
332 1.1 jmcneill }
333 1.1 jmcneill if (pstart == 0) {
334 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start);
335 1.1 jmcneill return EINVAL;
336 1.1 jmcneill }
337 1.1 jmcneill
338 1.1 jmcneill ch->ch_intr = intr;
339 1.1 jmcneill ch->ch_intrarg = intrarg;
340 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart;
341 1.1 jmcneill ch->ch_end_phys = pstart + psize;
342 1.1 jmcneill ch->ch_blksize = blksize;
343 1.1 jmcneill
344 1.1 jmcneill /* Flush FIFO */
345 1.1 jmcneill val = I2S_READ(sc, DA_FCTL);
346 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FTX);
347 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FTX);
348 1.1 jmcneill
349 1.1 jmcneill /* Reset TX sample counter */
350 1.1 jmcneill I2S_WRITE(sc, DA_TXCNT, 0);
351 1.1 jmcneill
352 1.1 jmcneill /* Enable transmitter block */
353 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
354 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val | DA_CTL_TXEN);
355 1.1 jmcneill
356 1.1 jmcneill /* Enable TX DRQ */
357 1.1 jmcneill val = I2S_READ(sc, DA_INT);
358 1.1 jmcneill I2S_WRITE(sc, DA_INT, val | DA_INT_TX_DRQ);
359 1.1 jmcneill
360 1.1 jmcneill /* Start DMA transfer */
361 1.1 jmcneill error = sunxi_i2s_transfer(ch);
362 1.1 jmcneill if (error != 0) {
363 1.1 jmcneill aprint_error_dev(sc->sc_dev,
364 1.1 jmcneill "failed to start DMA transfer: %d\n", error);
365 1.1 jmcneill return error;
366 1.1 jmcneill }
367 1.1 jmcneill
368 1.1 jmcneill return 0;
369 1.1 jmcneill }
370 1.1 jmcneill
371 1.1 jmcneill static int
372 1.1 jmcneill sunxi_i2s_trigger_input(void *priv, void *start, void *end, int blksize,
373 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
374 1.1 jmcneill {
375 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
376 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_rchan;
377 1.1 jmcneill struct sunxi_i2s_dma *dma;
378 1.1 jmcneill bus_addr_t pstart;
379 1.1 jmcneill bus_size_t psize;
380 1.1 jmcneill uint32_t val;
381 1.1 jmcneill int error;
382 1.1 jmcneill
383 1.1 jmcneill pstart = 0;
384 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start;
385 1.1 jmcneill
386 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
387 1.1 jmcneill if (dma->dma_addr == start) {
388 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr;
389 1.1 jmcneill break;
390 1.1 jmcneill }
391 1.1 jmcneill if (pstart == 0) {
392 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start);
393 1.1 jmcneill return EINVAL;
394 1.1 jmcneill }
395 1.1 jmcneill
396 1.1 jmcneill ch->ch_intr = intr;
397 1.1 jmcneill ch->ch_intrarg = intrarg;
398 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart;
399 1.1 jmcneill ch->ch_end_phys = pstart + psize;
400 1.1 jmcneill ch->ch_blksize = blksize;
401 1.1 jmcneill
402 1.1 jmcneill /* Flush FIFO */
403 1.1 jmcneill val = I2S_READ(sc, DA_FCTL);
404 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FRX);
405 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FRX);
406 1.1 jmcneill
407 1.1 jmcneill /* Reset RX sample counter */
408 1.1 jmcneill I2S_WRITE(sc, DA_RXCNT, 0);
409 1.1 jmcneill
410 1.1 jmcneill /* Enable receiver block */
411 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
412 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val | DA_CTL_RXEN);
413 1.1 jmcneill
414 1.1 jmcneill /* Enable RX DRQ */
415 1.1 jmcneill val = I2S_READ(sc, DA_INT);
416 1.1 jmcneill I2S_WRITE(sc, DA_INT, val | DA_INT_RX_DRQ);
417 1.1 jmcneill
418 1.1 jmcneill /* Start DMA transfer */
419 1.1 jmcneill error = sunxi_i2s_transfer(ch);
420 1.1 jmcneill if (error != 0) {
421 1.1 jmcneill aprint_error_dev(sc->sc_dev,
422 1.1 jmcneill "failed to start DMA transfer: %d\n", error);
423 1.1 jmcneill return error;
424 1.1 jmcneill }
425 1.1 jmcneill
426 1.1 jmcneill return 0;
427 1.1 jmcneill }
428 1.1 jmcneill
429 1.1 jmcneill static int
430 1.1 jmcneill sunxi_i2s_halt_output(void *priv)
431 1.1 jmcneill {
432 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
433 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_pchan;
434 1.1 jmcneill uint32_t val;
435 1.1 jmcneill
436 1.1 jmcneill /* Disable DMA channel */
437 1.1 jmcneill fdtbus_dma_halt(ch->ch_dma);
438 1.1 jmcneill
439 1.1 jmcneill /* Disable transmitter block */
440 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
441 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_TXEN);
442 1.1 jmcneill
443 1.1 jmcneill /* Disable TX DRQ */
444 1.1 jmcneill val = I2S_READ(sc, DA_INT);
445 1.1 jmcneill I2S_WRITE(sc, DA_INT, val & ~DA_INT_TX_DRQ);
446 1.1 jmcneill
447 1.1 jmcneill ch->ch_intr = NULL;
448 1.1 jmcneill ch->ch_intrarg = NULL;
449 1.1 jmcneill
450 1.1 jmcneill return 0;
451 1.1 jmcneill }
452 1.1 jmcneill
453 1.1 jmcneill static int
454 1.1 jmcneill sunxi_i2s_halt_input(void *priv)
455 1.1 jmcneill {
456 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
457 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_rchan;
458 1.1 jmcneill uint32_t val;
459 1.1 jmcneill
460 1.1 jmcneill /* Disable DMA channel */
461 1.1 jmcneill fdtbus_dma_halt(ch->ch_dma);
462 1.1 jmcneill
463 1.1 jmcneill /* Disable receiver block */
464 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
465 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_RXEN);
466 1.1 jmcneill
467 1.1 jmcneill /* Disable RX DRQ */
468 1.1 jmcneill val = I2S_READ(sc, DA_INT);
469 1.1 jmcneill I2S_WRITE(sc, DA_INT, val & ~DA_INT_RX_DRQ);
470 1.1 jmcneill
471 1.1 jmcneill return 0;
472 1.1 jmcneill }
473 1.1 jmcneill
474 1.1 jmcneill static void
475 1.1 jmcneill sunxi_i2s_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
476 1.1 jmcneill {
477 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
478 1.1 jmcneill
479 1.1 jmcneill *intr = &sc->sc_intr_lock;
480 1.1 jmcneill *thread = &sc->sc_lock;
481 1.1 jmcneill }
482 1.1 jmcneill
483 1.1 jmcneill static const struct audio_hw_if sunxi_i2s_hw_if = {
484 1.4 isaki .query_format = sunxi_i2s_query_format,
485 1.4 isaki .set_format = sunxi_i2s_set_format,
486 1.1 jmcneill .allocm = sunxi_i2s_allocm,
487 1.1 jmcneill .freem = sunxi_i2s_freem,
488 1.1 jmcneill .get_props = sunxi_i2s_get_props,
489 1.1 jmcneill .round_blocksize = sunxi_i2s_round_blocksize,
490 1.1 jmcneill .trigger_output = sunxi_i2s_trigger_output,
491 1.1 jmcneill .trigger_input = sunxi_i2s_trigger_input,
492 1.1 jmcneill .halt_output = sunxi_i2s_halt_output,
493 1.1 jmcneill .halt_input = sunxi_i2s_halt_input,
494 1.1 jmcneill .get_locks = sunxi_i2s_get_locks,
495 1.1 jmcneill };
496 1.1 jmcneill
497 1.1 jmcneill static void
498 1.1 jmcneill sunxi_i2s_dmaintr(void *priv)
499 1.1 jmcneill {
500 1.1 jmcneill struct sunxi_i2s_chan * const ch = priv;
501 1.1 jmcneill struct sunxi_i2s_softc * const sc = ch->ch_sc;
502 1.1 jmcneill
503 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
504 1.1 jmcneill ch->ch_cur_phys += ch->ch_blksize;
505 1.1 jmcneill if (ch->ch_cur_phys >= ch->ch_end_phys)
506 1.1 jmcneill ch->ch_cur_phys = ch->ch_start_phys;
507 1.1 jmcneill
508 1.1 jmcneill if (ch->ch_intr) {
509 1.1 jmcneill ch->ch_intr(ch->ch_intrarg);
510 1.1 jmcneill sunxi_i2s_transfer(ch);
511 1.1 jmcneill }
512 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
513 1.1 jmcneill }
514 1.1 jmcneill
515 1.1 jmcneill static int
516 1.1 jmcneill sunxi_i2s_chan_init(struct sunxi_i2s_softc *sc,
517 1.1 jmcneill struct sunxi_i2s_chan *ch, u_int mode, const char *dmaname)
518 1.1 jmcneill {
519 1.1 jmcneill ch->ch_sc = sc;
520 1.1 jmcneill ch->ch_mode = mode;
521 1.1 jmcneill ch->ch_dma = fdtbus_dma_get(sc->sc_phandle, dmaname, sunxi_i2s_dmaintr, ch);
522 1.1 jmcneill if (ch->ch_dma == NULL) {
523 1.1 jmcneill aprint_error(": couldn't get dma channel \"%s\"\n", dmaname);
524 1.1 jmcneill return ENXIO;
525 1.1 jmcneill }
526 1.1 jmcneill
527 1.1 jmcneill if (mode == AUMODE_PLAY) {
528 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_WRITE;
529 1.1 jmcneill ch->ch_req.dreq_dev_phys =
530 1.1 jmcneill sc->sc_baseaddr + DA_TXFIFO;
531 1.1 jmcneill } else {
532 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_READ;
533 1.1 jmcneill ch->ch_req.dreq_dev_phys =
534 1.1 jmcneill sc->sc_baseaddr + DA_RXFIFO;
535 1.1 jmcneill }
536 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_bus_width = 32;
537 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_burst_len = 8;
538 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_bus_width = 32;
539 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_burst_len = 8;
540 1.1 jmcneill
541 1.1 jmcneill return 0;
542 1.1 jmcneill }
543 1.1 jmcneill
544 1.1 jmcneill static int
545 1.1 jmcneill sunxi_i2s_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir)
546 1.1 jmcneill {
547 1.1 jmcneill struct sunxi_i2s_softc * const sc = audio_dai_private(dai);
548 1.1 jmcneill uint32_t val;
549 1.1 jmcneill
550 1.1 jmcneill /* XXX */
551 1.1 jmcneill
552 1.1 jmcneill val = DA_CLKD_MCLKO_EN;
553 1.3 jmcneill val |= __SHIFTIN(DA_CLKD_BCLKDIV_8, DA_CLKD_BCLKDIV);
554 1.1 jmcneill val |= __SHIFTIN(DA_CLKD_MCLKDIV_1, DA_CLKD_MCLKDIV);
555 1.1 jmcneill
556 1.1 jmcneill I2S_WRITE(sc, DA_CLKD, val);
557 1.1 jmcneill
558 1.1 jmcneill return 0;
559 1.1 jmcneill }
560 1.1 jmcneill
561 1.1 jmcneill static int
562 1.1 jmcneill sunxi_i2s_dai_set_format(audio_dai_tag_t dai, u_int format)
563 1.1 jmcneill {
564 1.1 jmcneill struct sunxi_i2s_softc * const sc = audio_dai_private(dai);
565 1.1 jmcneill uint32_t ctl, fat0;
566 1.1 jmcneill
567 1.1 jmcneill const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK);
568 1.1 jmcneill const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK);
569 1.1 jmcneill const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK);
570 1.1 jmcneill
571 1.1 jmcneill ctl = I2S_READ(sc, DA_CTL);
572 1.1 jmcneill fat0 = I2S_READ(sc, DA_FAT0);
573 1.1 jmcneill
574 1.1 jmcneill fat0 &= ~DA_FAT0_FMT;
575 1.1 jmcneill switch (fmt) {
576 1.1 jmcneill case AUDIO_DAI_FORMAT_I2S:
577 1.1 jmcneill fat0 |= __SHIFTIN(DA_FMT_I2S, DA_FAT0_FMT);
578 1.1 jmcneill break;
579 1.1 jmcneill case AUDIO_DAI_FORMAT_RJ:
580 1.1 jmcneill fat0 |= __SHIFTIN(DA_FMT_RJ, DA_FAT0_FMT);
581 1.1 jmcneill break;
582 1.1 jmcneill case AUDIO_DAI_FORMAT_LJ:
583 1.1 jmcneill fat0 |= __SHIFTIN(DA_FMT_LJ, DA_FAT0_FMT);
584 1.1 jmcneill break;
585 1.1 jmcneill default:
586 1.1 jmcneill return EINVAL;
587 1.1 jmcneill }
588 1.1 jmcneill
589 1.1 jmcneill fat0 &= ~(DA_FAT0_LRCP|DA_FAT0_BCP);
590 1.1 jmcneill if (AUDIO_DAI_POLARITY_B(pol))
591 1.1 jmcneill fat0 |= __SHIFTIN(DA_BCP_INVERTED, DA_FAT0_BCP);
592 1.1 jmcneill if (AUDIO_DAI_POLARITY_F(pol))
593 1.1 jmcneill fat0 |= __SHIFTIN(DA_LRCP_INVERTED, DA_FAT0_LRCP);
594 1.1 jmcneill
595 1.1 jmcneill switch (clk) {
596 1.1 jmcneill case AUDIO_DAI_CLOCK_CBM_CFM:
597 1.1 jmcneill ctl |= DA_CTL_MS; /* codec is master */
598 1.1 jmcneill break;
599 1.1 jmcneill case AUDIO_DAI_CLOCK_CBS_CFS:
600 1.1 jmcneill ctl &= ~DA_CTL_MS; /* codec is slave */
601 1.1 jmcneill break;
602 1.1 jmcneill default:
603 1.1 jmcneill return EINVAL;
604 1.1 jmcneill }
605 1.1 jmcneill
606 1.1 jmcneill ctl &= ~DA_CTL_PCM;
607 1.1 jmcneill
608 1.1 jmcneill I2S_WRITE(sc, DA_CTL, ctl);
609 1.1 jmcneill I2S_WRITE(sc, DA_FAT0, fat0);
610 1.1 jmcneill
611 1.1 jmcneill return 0;
612 1.1 jmcneill }
613 1.1 jmcneill
614 1.1 jmcneill static audio_dai_tag_t
615 1.1 jmcneill sunxi_i2s_dai_get_tag(device_t dev, const void *data, size_t len)
616 1.1 jmcneill {
617 1.1 jmcneill struct sunxi_i2s_softc * const sc = device_private(dev);
618 1.1 jmcneill
619 1.1 jmcneill if (len != 4)
620 1.1 jmcneill return NULL;
621 1.1 jmcneill
622 1.1 jmcneill return &sc->sc_dai;
623 1.1 jmcneill }
624 1.1 jmcneill
625 1.1 jmcneill static struct fdtbus_dai_controller_func sunxi_i2s_dai_funcs = {
626 1.1 jmcneill .get_tag = sunxi_i2s_dai_get_tag
627 1.1 jmcneill };
628 1.1 jmcneill
629 1.1 jmcneill static int
630 1.1 jmcneill sunxi_i2s_clock_init(int phandle)
631 1.1 jmcneill {
632 1.1 jmcneill struct fdtbus_reset *rst;
633 1.1 jmcneill struct clk *clk;
634 1.1 jmcneill int error;
635 1.1 jmcneill
636 1.1 jmcneill /* Set module clock to 24.576MHz, suitable for 48 kHz sampling rates */
637 1.1 jmcneill clk = fdtbus_clock_get(phandle, "mod");
638 1.1 jmcneill if (clk == NULL) {
639 1.1 jmcneill aprint_error(": couldn't find mod clock\n");
640 1.1 jmcneill return ENXIO;
641 1.1 jmcneill }
642 1.1 jmcneill error = clk_set_rate(clk, SUNXI_I2S_CLK_RATE);
643 1.1 jmcneill if (error != 0) {
644 1.1 jmcneill aprint_error(": couldn't set mod clock rate: %d\n", error);
645 1.1 jmcneill return error;
646 1.1 jmcneill }
647 1.1 jmcneill error = clk_enable(clk);
648 1.1 jmcneill if (error != 0) {
649 1.1 jmcneill aprint_error(": couldn't enable mod clock: %d\n", error);
650 1.1 jmcneill return error;
651 1.1 jmcneill }
652 1.1 jmcneill
653 1.1 jmcneill /* Enable APB clock */
654 1.1 jmcneill clk = fdtbus_clock_get(phandle, "apb");
655 1.1 jmcneill if (clk == NULL) {
656 1.1 jmcneill aprint_error(": couldn't find apb clock\n");
657 1.1 jmcneill return ENXIO;
658 1.1 jmcneill }
659 1.1 jmcneill error = clk_enable(clk);
660 1.1 jmcneill if (error != 0) {
661 1.1 jmcneill aprint_error(": couldn't enable apb clock: %d\n", error);
662 1.1 jmcneill return error;
663 1.1 jmcneill }
664 1.1 jmcneill
665 1.1 jmcneill /* De-assert reset */
666 1.1 jmcneill rst = fdtbus_reset_get(phandle, "rst");
667 1.1 jmcneill if (rst == NULL) {
668 1.1 jmcneill aprint_error(": couldn't find reset\n");
669 1.1 jmcneill return ENXIO;
670 1.1 jmcneill }
671 1.1 jmcneill error = fdtbus_reset_deassert(rst);
672 1.1 jmcneill if (error != 0) {
673 1.1 jmcneill aprint_error(": couldn't de-assert reset: %d\n", error);
674 1.1 jmcneill return error;
675 1.1 jmcneill }
676 1.1 jmcneill
677 1.1 jmcneill return 0;
678 1.1 jmcneill }
679 1.1 jmcneill
680 1.1 jmcneill static int
681 1.1 jmcneill sunxi_i2s_match(device_t parent, cfdata_t cf, void *aux)
682 1.1 jmcneill {
683 1.1 jmcneill struct fdt_attach_args * const faa = aux;
684 1.1 jmcneill
685 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
686 1.1 jmcneill }
687 1.1 jmcneill
688 1.1 jmcneill static void
689 1.1 jmcneill sunxi_i2s_attach(device_t parent, device_t self, void *aux)
690 1.1 jmcneill {
691 1.1 jmcneill struct sunxi_i2s_softc * const sc = device_private(self);
692 1.1 jmcneill struct fdt_attach_args * const faa = aux;
693 1.1 jmcneill const int phandle = faa->faa_phandle;
694 1.1 jmcneill bus_addr_t addr;
695 1.1 jmcneill bus_size_t size;
696 1.1 jmcneill uint32_t val;
697 1.1 jmcneill
698 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
699 1.1 jmcneill aprint_error(": couldn't get registers\n");
700 1.1 jmcneill return;
701 1.1 jmcneill }
702 1.1 jmcneill
703 1.1 jmcneill if (sunxi_i2s_clock_init(phandle) != 0)
704 1.1 jmcneill return;
705 1.1 jmcneill
706 1.1 jmcneill sc->sc_dev = self;
707 1.1 jmcneill sc->sc_phandle = phandle;
708 1.1 jmcneill sc->sc_baseaddr = addr;
709 1.1 jmcneill sc->sc_bst = faa->faa_bst;
710 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
711 1.1 jmcneill aprint_error(": couldn't map registers\n");
712 1.1 jmcneill return;
713 1.1 jmcneill }
714 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
715 1.1 jmcneill LIST_INIT(&sc->sc_dmalist);
716 1.1 jmcneill sc->sc_cfg = (void *)of_search_compatible(phandle, compat_data)->data;
717 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
718 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
719 1.1 jmcneill
720 1.1 jmcneill if (sunxi_i2s_chan_init(sc, &sc->sc_pchan, AUMODE_PLAY, "tx") != 0 ||
721 1.1 jmcneill sunxi_i2s_chan_init(sc, &sc->sc_rchan, AUMODE_RECORD, "rx") != 0) {
722 1.1 jmcneill aprint_error(": couldn't setup channels\n");
723 1.1 jmcneill return;
724 1.1 jmcneill }
725 1.1 jmcneill
726 1.1 jmcneill aprint_naive("\n");
727 1.1 jmcneill aprint_normal(": %s\n", sc->sc_cfg->name);
728 1.1 jmcneill
729 1.1 jmcneill /* Reset */
730 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
731 1.1 jmcneill val &= ~(DA_CTL_TXEN|DA_CTL_RXEN|DA_CTL_GEN);
732 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val);
733 1.1 jmcneill
734 1.1 jmcneill val = I2S_READ(sc, DA_FCTL);
735 1.1 jmcneill val &= ~(DA_FCTL_FTX|DA_FCTL_FRX);
736 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val);
737 1.1 jmcneill
738 1.1 jmcneill I2S_WRITE(sc, DA_TXCNT, 0);
739 1.1 jmcneill I2S_WRITE(sc, DA_RXCNT, 0);
740 1.1 jmcneill
741 1.1 jmcneill /* Enable */
742 1.1 jmcneill I2S_WRITE(sc, DA_CTL, DA_CTL_GEN | DA_CTL_SDO_EN);
743 1.1 jmcneill
744 1.1 jmcneill /* Setup channels */
745 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->txchmap, 0x76543210);
746 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->txchsel, __SHIFTIN(1, DA_CHSEL_SEL) |
747 1.1 jmcneill __SHIFTIN(3, DA_CHSEL_EN));
748 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->rxchmap, 0x76543210);
749 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->rxchsel, __SHIFTIN(1, DA_CHSEL_SEL) |
750 1.1 jmcneill __SHIFTIN(3, DA_CHSEL_EN));
751 1.1 jmcneill
752 1.1 jmcneill sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
753 1.1 jmcneill sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
754 1.4 isaki sc->sc_format.validbits = 32;
755 1.4 isaki sc->sc_format.precision = 32;
756 1.1 jmcneill sc->sc_format.channels = 2;
757 1.1 jmcneill sc->sc_format.channel_mask = AUFMT_STEREO;
758 1.4 isaki sc->sc_format.frequency_type = 1;
759 1.4 isaki sc->sc_format.frequency[0] = 48000;
760 1.1 jmcneill
761 1.1 jmcneill sc->sc_dai.dai_set_sysclk = sunxi_i2s_dai_set_sysclk;
762 1.1 jmcneill sc->sc_dai.dai_set_format = sunxi_i2s_dai_set_format;
763 1.1 jmcneill sc->sc_dai.dai_hw_if = &sunxi_i2s_hw_if;
764 1.1 jmcneill sc->sc_dai.dai_dev = self;
765 1.1 jmcneill sc->sc_dai.dai_priv = sc;
766 1.1 jmcneill fdtbus_register_dai_controller(self, phandle, &sunxi_i2s_dai_funcs);
767 1.1 jmcneill }
768 1.1 jmcneill
769 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_i2s, sizeof(struct sunxi_i2s_softc),
770 1.1 jmcneill sunxi_i2s_match, sunxi_i2s_attach, NULL, NULL);
771