sunxi_i2s.c revision 1.6 1 1.6 isaki /* $NetBSD: sunxi_i2s.c,v 1.6 2019/06/08 08:02:37 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.6 isaki __KERNEL_RCSID(0, "$NetBSD: sunxi_i2s.c,v 1.6 2019/06/08 08:02:37 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.5 jmcneill { "allwinner,sun50i-a64-codec-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.6 isaki
300 1.6 isaki return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
301 1.6 isaki AUDIO_PROP_FULLDUPLEX | AUDIO_PROP_INDEPENDENT;
302 1.1 jmcneill }
303 1.1 jmcneill
304 1.1 jmcneill static int
305 1.1 jmcneill sunxi_i2s_round_blocksize(void *priv, int bs, int mode,
306 1.1 jmcneill const audio_params_t *params)
307 1.1 jmcneill {
308 1.1 jmcneill bs &= ~3;
309 1.1 jmcneill if (bs == 0)
310 1.1 jmcneill bs = 4;
311 1.1 jmcneill return bs;
312 1.1 jmcneill }
313 1.1 jmcneill
314 1.1 jmcneill static int
315 1.1 jmcneill sunxi_i2s_trigger_output(void *priv, void *start, void *end, int blksize,
316 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
317 1.1 jmcneill {
318 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
319 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_pchan;
320 1.1 jmcneill struct sunxi_i2s_dma *dma;
321 1.1 jmcneill bus_addr_t pstart;
322 1.1 jmcneill bus_size_t psize;
323 1.1 jmcneill uint32_t val;
324 1.1 jmcneill int error;
325 1.1 jmcneill
326 1.1 jmcneill pstart = 0;
327 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start;
328 1.1 jmcneill
329 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
330 1.1 jmcneill if (dma->dma_addr == start) {
331 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr;
332 1.1 jmcneill break;
333 1.1 jmcneill }
334 1.1 jmcneill if (pstart == 0) {
335 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start);
336 1.1 jmcneill return EINVAL;
337 1.1 jmcneill }
338 1.1 jmcneill
339 1.1 jmcneill ch->ch_intr = intr;
340 1.1 jmcneill ch->ch_intrarg = intrarg;
341 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart;
342 1.1 jmcneill ch->ch_end_phys = pstart + psize;
343 1.1 jmcneill ch->ch_blksize = blksize;
344 1.1 jmcneill
345 1.1 jmcneill /* Flush FIFO */
346 1.1 jmcneill val = I2S_READ(sc, DA_FCTL);
347 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FTX);
348 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FTX);
349 1.1 jmcneill
350 1.1 jmcneill /* Reset TX sample counter */
351 1.1 jmcneill I2S_WRITE(sc, DA_TXCNT, 0);
352 1.1 jmcneill
353 1.1 jmcneill /* Enable transmitter block */
354 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
355 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val | DA_CTL_TXEN);
356 1.1 jmcneill
357 1.1 jmcneill /* Enable TX DRQ */
358 1.1 jmcneill val = I2S_READ(sc, DA_INT);
359 1.1 jmcneill I2S_WRITE(sc, DA_INT, val | DA_INT_TX_DRQ);
360 1.1 jmcneill
361 1.1 jmcneill /* Start DMA transfer */
362 1.1 jmcneill error = sunxi_i2s_transfer(ch);
363 1.1 jmcneill if (error != 0) {
364 1.1 jmcneill aprint_error_dev(sc->sc_dev,
365 1.1 jmcneill "failed to start DMA transfer: %d\n", error);
366 1.1 jmcneill return error;
367 1.1 jmcneill }
368 1.1 jmcneill
369 1.1 jmcneill return 0;
370 1.1 jmcneill }
371 1.1 jmcneill
372 1.1 jmcneill static int
373 1.1 jmcneill sunxi_i2s_trigger_input(void *priv, void *start, void *end, int blksize,
374 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
375 1.1 jmcneill {
376 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
377 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_rchan;
378 1.1 jmcneill struct sunxi_i2s_dma *dma;
379 1.1 jmcneill bus_addr_t pstart;
380 1.1 jmcneill bus_size_t psize;
381 1.1 jmcneill uint32_t val;
382 1.1 jmcneill int error;
383 1.1 jmcneill
384 1.1 jmcneill pstart = 0;
385 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start;
386 1.1 jmcneill
387 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
388 1.1 jmcneill if (dma->dma_addr == start) {
389 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr;
390 1.1 jmcneill break;
391 1.1 jmcneill }
392 1.1 jmcneill if (pstart == 0) {
393 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start);
394 1.1 jmcneill return EINVAL;
395 1.1 jmcneill }
396 1.1 jmcneill
397 1.1 jmcneill ch->ch_intr = intr;
398 1.1 jmcneill ch->ch_intrarg = intrarg;
399 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart;
400 1.1 jmcneill ch->ch_end_phys = pstart + psize;
401 1.1 jmcneill ch->ch_blksize = blksize;
402 1.1 jmcneill
403 1.1 jmcneill /* Flush FIFO */
404 1.1 jmcneill val = I2S_READ(sc, DA_FCTL);
405 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FRX);
406 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FRX);
407 1.1 jmcneill
408 1.1 jmcneill /* Reset RX sample counter */
409 1.1 jmcneill I2S_WRITE(sc, DA_RXCNT, 0);
410 1.1 jmcneill
411 1.1 jmcneill /* Enable receiver block */
412 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
413 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val | DA_CTL_RXEN);
414 1.1 jmcneill
415 1.1 jmcneill /* Enable RX DRQ */
416 1.1 jmcneill val = I2S_READ(sc, DA_INT);
417 1.1 jmcneill I2S_WRITE(sc, DA_INT, val | DA_INT_RX_DRQ);
418 1.1 jmcneill
419 1.1 jmcneill /* Start DMA transfer */
420 1.1 jmcneill error = sunxi_i2s_transfer(ch);
421 1.1 jmcneill if (error != 0) {
422 1.1 jmcneill aprint_error_dev(sc->sc_dev,
423 1.1 jmcneill "failed to start DMA transfer: %d\n", error);
424 1.1 jmcneill return error;
425 1.1 jmcneill }
426 1.1 jmcneill
427 1.1 jmcneill return 0;
428 1.1 jmcneill }
429 1.1 jmcneill
430 1.1 jmcneill static int
431 1.1 jmcneill sunxi_i2s_halt_output(void *priv)
432 1.1 jmcneill {
433 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
434 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_pchan;
435 1.1 jmcneill uint32_t val;
436 1.1 jmcneill
437 1.1 jmcneill /* Disable DMA channel */
438 1.1 jmcneill fdtbus_dma_halt(ch->ch_dma);
439 1.1 jmcneill
440 1.1 jmcneill /* Disable transmitter block */
441 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
442 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_TXEN);
443 1.1 jmcneill
444 1.1 jmcneill /* Disable TX DRQ */
445 1.1 jmcneill val = I2S_READ(sc, DA_INT);
446 1.1 jmcneill I2S_WRITE(sc, DA_INT, val & ~DA_INT_TX_DRQ);
447 1.1 jmcneill
448 1.1 jmcneill ch->ch_intr = NULL;
449 1.1 jmcneill ch->ch_intrarg = NULL;
450 1.1 jmcneill
451 1.1 jmcneill return 0;
452 1.1 jmcneill }
453 1.1 jmcneill
454 1.1 jmcneill static int
455 1.1 jmcneill sunxi_i2s_halt_input(void *priv)
456 1.1 jmcneill {
457 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
458 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_rchan;
459 1.1 jmcneill uint32_t val;
460 1.1 jmcneill
461 1.1 jmcneill /* Disable DMA channel */
462 1.1 jmcneill fdtbus_dma_halt(ch->ch_dma);
463 1.1 jmcneill
464 1.1 jmcneill /* Disable receiver block */
465 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
466 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_RXEN);
467 1.1 jmcneill
468 1.1 jmcneill /* Disable RX DRQ */
469 1.1 jmcneill val = I2S_READ(sc, DA_INT);
470 1.1 jmcneill I2S_WRITE(sc, DA_INT, val & ~DA_INT_RX_DRQ);
471 1.1 jmcneill
472 1.1 jmcneill return 0;
473 1.1 jmcneill }
474 1.1 jmcneill
475 1.1 jmcneill static void
476 1.1 jmcneill sunxi_i2s_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
477 1.1 jmcneill {
478 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
479 1.1 jmcneill
480 1.1 jmcneill *intr = &sc->sc_intr_lock;
481 1.1 jmcneill *thread = &sc->sc_lock;
482 1.1 jmcneill }
483 1.1 jmcneill
484 1.1 jmcneill static const struct audio_hw_if sunxi_i2s_hw_if = {
485 1.4 isaki .query_format = sunxi_i2s_query_format,
486 1.4 isaki .set_format = sunxi_i2s_set_format,
487 1.1 jmcneill .allocm = sunxi_i2s_allocm,
488 1.1 jmcneill .freem = sunxi_i2s_freem,
489 1.1 jmcneill .get_props = sunxi_i2s_get_props,
490 1.1 jmcneill .round_blocksize = sunxi_i2s_round_blocksize,
491 1.1 jmcneill .trigger_output = sunxi_i2s_trigger_output,
492 1.1 jmcneill .trigger_input = sunxi_i2s_trigger_input,
493 1.1 jmcneill .halt_output = sunxi_i2s_halt_output,
494 1.1 jmcneill .halt_input = sunxi_i2s_halt_input,
495 1.1 jmcneill .get_locks = sunxi_i2s_get_locks,
496 1.1 jmcneill };
497 1.1 jmcneill
498 1.1 jmcneill static void
499 1.1 jmcneill sunxi_i2s_dmaintr(void *priv)
500 1.1 jmcneill {
501 1.1 jmcneill struct sunxi_i2s_chan * const ch = priv;
502 1.1 jmcneill struct sunxi_i2s_softc * const sc = ch->ch_sc;
503 1.1 jmcneill
504 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
505 1.1 jmcneill ch->ch_cur_phys += ch->ch_blksize;
506 1.1 jmcneill if (ch->ch_cur_phys >= ch->ch_end_phys)
507 1.1 jmcneill ch->ch_cur_phys = ch->ch_start_phys;
508 1.1 jmcneill
509 1.1 jmcneill if (ch->ch_intr) {
510 1.1 jmcneill ch->ch_intr(ch->ch_intrarg);
511 1.1 jmcneill sunxi_i2s_transfer(ch);
512 1.1 jmcneill }
513 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
514 1.1 jmcneill }
515 1.1 jmcneill
516 1.1 jmcneill static int
517 1.1 jmcneill sunxi_i2s_chan_init(struct sunxi_i2s_softc *sc,
518 1.1 jmcneill struct sunxi_i2s_chan *ch, u_int mode, const char *dmaname)
519 1.1 jmcneill {
520 1.1 jmcneill ch->ch_sc = sc;
521 1.1 jmcneill ch->ch_mode = mode;
522 1.1 jmcneill ch->ch_dma = fdtbus_dma_get(sc->sc_phandle, dmaname, sunxi_i2s_dmaintr, ch);
523 1.1 jmcneill if (ch->ch_dma == NULL) {
524 1.1 jmcneill aprint_error(": couldn't get dma channel \"%s\"\n", dmaname);
525 1.1 jmcneill return ENXIO;
526 1.1 jmcneill }
527 1.1 jmcneill
528 1.1 jmcneill if (mode == AUMODE_PLAY) {
529 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_WRITE;
530 1.1 jmcneill ch->ch_req.dreq_dev_phys =
531 1.1 jmcneill sc->sc_baseaddr + DA_TXFIFO;
532 1.1 jmcneill } else {
533 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_READ;
534 1.1 jmcneill ch->ch_req.dreq_dev_phys =
535 1.1 jmcneill sc->sc_baseaddr + DA_RXFIFO;
536 1.1 jmcneill }
537 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_bus_width = 32;
538 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_burst_len = 8;
539 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_bus_width = 32;
540 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_burst_len = 8;
541 1.1 jmcneill
542 1.1 jmcneill return 0;
543 1.1 jmcneill }
544 1.1 jmcneill
545 1.1 jmcneill static int
546 1.1 jmcneill sunxi_i2s_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir)
547 1.1 jmcneill {
548 1.1 jmcneill struct sunxi_i2s_softc * const sc = audio_dai_private(dai);
549 1.1 jmcneill uint32_t val;
550 1.1 jmcneill
551 1.1 jmcneill /* XXX */
552 1.1 jmcneill
553 1.1 jmcneill val = DA_CLKD_MCLKO_EN;
554 1.3 jmcneill val |= __SHIFTIN(DA_CLKD_BCLKDIV_8, DA_CLKD_BCLKDIV);
555 1.1 jmcneill val |= __SHIFTIN(DA_CLKD_MCLKDIV_1, DA_CLKD_MCLKDIV);
556 1.1 jmcneill
557 1.1 jmcneill I2S_WRITE(sc, DA_CLKD, val);
558 1.1 jmcneill
559 1.1 jmcneill return 0;
560 1.1 jmcneill }
561 1.1 jmcneill
562 1.1 jmcneill static int
563 1.1 jmcneill sunxi_i2s_dai_set_format(audio_dai_tag_t dai, u_int format)
564 1.1 jmcneill {
565 1.1 jmcneill struct sunxi_i2s_softc * const sc = audio_dai_private(dai);
566 1.1 jmcneill uint32_t ctl, fat0;
567 1.1 jmcneill
568 1.1 jmcneill const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK);
569 1.1 jmcneill const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK);
570 1.1 jmcneill const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK);
571 1.1 jmcneill
572 1.1 jmcneill ctl = I2S_READ(sc, DA_CTL);
573 1.1 jmcneill fat0 = I2S_READ(sc, DA_FAT0);
574 1.1 jmcneill
575 1.1 jmcneill fat0 &= ~DA_FAT0_FMT;
576 1.1 jmcneill switch (fmt) {
577 1.1 jmcneill case AUDIO_DAI_FORMAT_I2S:
578 1.1 jmcneill fat0 |= __SHIFTIN(DA_FMT_I2S, DA_FAT0_FMT);
579 1.1 jmcneill break;
580 1.1 jmcneill case AUDIO_DAI_FORMAT_RJ:
581 1.1 jmcneill fat0 |= __SHIFTIN(DA_FMT_RJ, DA_FAT0_FMT);
582 1.1 jmcneill break;
583 1.1 jmcneill case AUDIO_DAI_FORMAT_LJ:
584 1.1 jmcneill fat0 |= __SHIFTIN(DA_FMT_LJ, DA_FAT0_FMT);
585 1.1 jmcneill break;
586 1.1 jmcneill default:
587 1.1 jmcneill return EINVAL;
588 1.1 jmcneill }
589 1.1 jmcneill
590 1.1 jmcneill fat0 &= ~(DA_FAT0_LRCP|DA_FAT0_BCP);
591 1.1 jmcneill if (AUDIO_DAI_POLARITY_B(pol))
592 1.1 jmcneill fat0 |= __SHIFTIN(DA_BCP_INVERTED, DA_FAT0_BCP);
593 1.1 jmcneill if (AUDIO_DAI_POLARITY_F(pol))
594 1.1 jmcneill fat0 |= __SHIFTIN(DA_LRCP_INVERTED, DA_FAT0_LRCP);
595 1.1 jmcneill
596 1.1 jmcneill switch (clk) {
597 1.1 jmcneill case AUDIO_DAI_CLOCK_CBM_CFM:
598 1.1 jmcneill ctl |= DA_CTL_MS; /* codec is master */
599 1.1 jmcneill break;
600 1.1 jmcneill case AUDIO_DAI_CLOCK_CBS_CFS:
601 1.1 jmcneill ctl &= ~DA_CTL_MS; /* codec is slave */
602 1.1 jmcneill break;
603 1.1 jmcneill default:
604 1.1 jmcneill return EINVAL;
605 1.1 jmcneill }
606 1.1 jmcneill
607 1.1 jmcneill ctl &= ~DA_CTL_PCM;
608 1.1 jmcneill
609 1.1 jmcneill I2S_WRITE(sc, DA_CTL, ctl);
610 1.1 jmcneill I2S_WRITE(sc, DA_FAT0, fat0);
611 1.1 jmcneill
612 1.1 jmcneill return 0;
613 1.1 jmcneill }
614 1.1 jmcneill
615 1.1 jmcneill static audio_dai_tag_t
616 1.1 jmcneill sunxi_i2s_dai_get_tag(device_t dev, const void *data, size_t len)
617 1.1 jmcneill {
618 1.1 jmcneill struct sunxi_i2s_softc * const sc = device_private(dev);
619 1.1 jmcneill
620 1.1 jmcneill if (len != 4)
621 1.1 jmcneill return NULL;
622 1.1 jmcneill
623 1.1 jmcneill return &sc->sc_dai;
624 1.1 jmcneill }
625 1.1 jmcneill
626 1.1 jmcneill static struct fdtbus_dai_controller_func sunxi_i2s_dai_funcs = {
627 1.1 jmcneill .get_tag = sunxi_i2s_dai_get_tag
628 1.1 jmcneill };
629 1.1 jmcneill
630 1.1 jmcneill static int
631 1.1 jmcneill sunxi_i2s_clock_init(int phandle)
632 1.1 jmcneill {
633 1.1 jmcneill struct fdtbus_reset *rst;
634 1.1 jmcneill struct clk *clk;
635 1.1 jmcneill int error;
636 1.1 jmcneill
637 1.1 jmcneill /* Set module clock to 24.576MHz, suitable for 48 kHz sampling rates */
638 1.1 jmcneill clk = fdtbus_clock_get(phandle, "mod");
639 1.1 jmcneill if (clk == NULL) {
640 1.1 jmcneill aprint_error(": couldn't find mod clock\n");
641 1.1 jmcneill return ENXIO;
642 1.1 jmcneill }
643 1.1 jmcneill error = clk_set_rate(clk, SUNXI_I2S_CLK_RATE);
644 1.1 jmcneill if (error != 0) {
645 1.1 jmcneill aprint_error(": couldn't set mod clock rate: %d\n", error);
646 1.1 jmcneill return error;
647 1.1 jmcneill }
648 1.1 jmcneill error = clk_enable(clk);
649 1.1 jmcneill if (error != 0) {
650 1.1 jmcneill aprint_error(": couldn't enable mod clock: %d\n", error);
651 1.1 jmcneill return error;
652 1.1 jmcneill }
653 1.1 jmcneill
654 1.1 jmcneill /* Enable APB clock */
655 1.1 jmcneill clk = fdtbus_clock_get(phandle, "apb");
656 1.1 jmcneill if (clk == NULL) {
657 1.1 jmcneill aprint_error(": couldn't find apb clock\n");
658 1.1 jmcneill return ENXIO;
659 1.1 jmcneill }
660 1.1 jmcneill error = clk_enable(clk);
661 1.1 jmcneill if (error != 0) {
662 1.1 jmcneill aprint_error(": couldn't enable apb clock: %d\n", error);
663 1.1 jmcneill return error;
664 1.1 jmcneill }
665 1.1 jmcneill
666 1.1 jmcneill /* De-assert reset */
667 1.1 jmcneill rst = fdtbus_reset_get(phandle, "rst");
668 1.1 jmcneill if (rst == NULL) {
669 1.1 jmcneill aprint_error(": couldn't find reset\n");
670 1.1 jmcneill return ENXIO;
671 1.1 jmcneill }
672 1.1 jmcneill error = fdtbus_reset_deassert(rst);
673 1.1 jmcneill if (error != 0) {
674 1.1 jmcneill aprint_error(": couldn't de-assert reset: %d\n", error);
675 1.1 jmcneill return error;
676 1.1 jmcneill }
677 1.1 jmcneill
678 1.1 jmcneill return 0;
679 1.1 jmcneill }
680 1.1 jmcneill
681 1.1 jmcneill static int
682 1.1 jmcneill sunxi_i2s_match(device_t parent, cfdata_t cf, void *aux)
683 1.1 jmcneill {
684 1.1 jmcneill struct fdt_attach_args * const faa = aux;
685 1.1 jmcneill
686 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
687 1.1 jmcneill }
688 1.1 jmcneill
689 1.1 jmcneill static void
690 1.1 jmcneill sunxi_i2s_attach(device_t parent, device_t self, void *aux)
691 1.1 jmcneill {
692 1.1 jmcneill struct sunxi_i2s_softc * const sc = device_private(self);
693 1.1 jmcneill struct fdt_attach_args * const faa = aux;
694 1.1 jmcneill const int phandle = faa->faa_phandle;
695 1.1 jmcneill bus_addr_t addr;
696 1.1 jmcneill bus_size_t size;
697 1.1 jmcneill uint32_t val;
698 1.1 jmcneill
699 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
700 1.1 jmcneill aprint_error(": couldn't get registers\n");
701 1.1 jmcneill return;
702 1.1 jmcneill }
703 1.1 jmcneill
704 1.1 jmcneill if (sunxi_i2s_clock_init(phandle) != 0)
705 1.1 jmcneill return;
706 1.1 jmcneill
707 1.1 jmcneill sc->sc_dev = self;
708 1.1 jmcneill sc->sc_phandle = phandle;
709 1.1 jmcneill sc->sc_baseaddr = addr;
710 1.1 jmcneill sc->sc_bst = faa->faa_bst;
711 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
712 1.1 jmcneill aprint_error(": couldn't map registers\n");
713 1.1 jmcneill return;
714 1.1 jmcneill }
715 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
716 1.1 jmcneill LIST_INIT(&sc->sc_dmalist);
717 1.1 jmcneill sc->sc_cfg = (void *)of_search_compatible(phandle, compat_data)->data;
718 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
719 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
720 1.1 jmcneill
721 1.1 jmcneill if (sunxi_i2s_chan_init(sc, &sc->sc_pchan, AUMODE_PLAY, "tx") != 0 ||
722 1.1 jmcneill sunxi_i2s_chan_init(sc, &sc->sc_rchan, AUMODE_RECORD, "rx") != 0) {
723 1.1 jmcneill aprint_error(": couldn't setup channels\n");
724 1.1 jmcneill return;
725 1.1 jmcneill }
726 1.1 jmcneill
727 1.1 jmcneill aprint_naive("\n");
728 1.1 jmcneill aprint_normal(": %s\n", sc->sc_cfg->name);
729 1.1 jmcneill
730 1.1 jmcneill /* Reset */
731 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
732 1.1 jmcneill val &= ~(DA_CTL_TXEN|DA_CTL_RXEN|DA_CTL_GEN);
733 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val);
734 1.1 jmcneill
735 1.1 jmcneill val = I2S_READ(sc, DA_FCTL);
736 1.1 jmcneill val &= ~(DA_FCTL_FTX|DA_FCTL_FRX);
737 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val);
738 1.1 jmcneill
739 1.1 jmcneill I2S_WRITE(sc, DA_TXCNT, 0);
740 1.1 jmcneill I2S_WRITE(sc, DA_RXCNT, 0);
741 1.1 jmcneill
742 1.1 jmcneill /* Enable */
743 1.1 jmcneill I2S_WRITE(sc, DA_CTL, DA_CTL_GEN | DA_CTL_SDO_EN);
744 1.1 jmcneill
745 1.1 jmcneill /* Setup channels */
746 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->txchmap, 0x76543210);
747 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->txchsel, __SHIFTIN(1, DA_CHSEL_SEL) |
748 1.1 jmcneill __SHIFTIN(3, DA_CHSEL_EN));
749 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->rxchmap, 0x76543210);
750 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->rxchsel, __SHIFTIN(1, DA_CHSEL_SEL) |
751 1.1 jmcneill __SHIFTIN(3, DA_CHSEL_EN));
752 1.1 jmcneill
753 1.1 jmcneill sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
754 1.1 jmcneill sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
755 1.4 isaki sc->sc_format.validbits = 32;
756 1.4 isaki sc->sc_format.precision = 32;
757 1.1 jmcneill sc->sc_format.channels = 2;
758 1.1 jmcneill sc->sc_format.channel_mask = AUFMT_STEREO;
759 1.4 isaki sc->sc_format.frequency_type = 1;
760 1.4 isaki sc->sc_format.frequency[0] = 48000;
761 1.1 jmcneill
762 1.1 jmcneill sc->sc_dai.dai_set_sysclk = sunxi_i2s_dai_set_sysclk;
763 1.1 jmcneill sc->sc_dai.dai_set_format = sunxi_i2s_dai_set_format;
764 1.1 jmcneill sc->sc_dai.dai_hw_if = &sunxi_i2s_hw_if;
765 1.1 jmcneill sc->sc_dai.dai_dev = self;
766 1.1 jmcneill sc->sc_dai.dai_priv = sc;
767 1.1 jmcneill fdtbus_register_dai_controller(self, phandle, &sunxi_i2s_dai_funcs);
768 1.1 jmcneill }
769 1.1 jmcneill
770 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_i2s, sizeof(struct sunxi_i2s_softc),
771 1.1 jmcneill sunxi_i2s_match, sunxi_i2s_attach, NULL, NULL);
772