sunxi_i2s.c revision 1.3 1 1.3 jmcneill /* $NetBSD: sunxi_i2s.c,v 1.3 2018/11/17 20:36:23 jmcneill 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.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_i2s.c,v 1.3 2018/11/17 20:36:23 jmcneill 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.1 jmcneill #include <dev/audio_if.h>
41 1.1 jmcneill #include <dev/auconv.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 struct audio_encoding_set *sc_encodings;
163 1.1 jmcneill
164 1.1 jmcneill struct sunxi_i2s_chan sc_pchan;
165 1.1 jmcneill struct sunxi_i2s_chan sc_rchan;
166 1.1 jmcneill
167 1.1 jmcneill struct audio_dai_device sc_dai;
168 1.1 jmcneill };
169 1.1 jmcneill
170 1.1 jmcneill #define I2S_READ(sc, reg) \
171 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
172 1.1 jmcneill #define I2S_WRITE(sc, reg, val) \
173 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
174 1.1 jmcneill
175 1.1 jmcneill static int
176 1.1 jmcneill sunxi_i2s_allocdma(struct sunxi_i2s_softc *sc, size_t size,
177 1.1 jmcneill size_t align, struct sunxi_i2s_dma *dma)
178 1.1 jmcneill {
179 1.1 jmcneill int error;
180 1.1 jmcneill
181 1.1 jmcneill dma->dma_size = size;
182 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, dma->dma_size, align, 0,
183 1.1 jmcneill dma->dma_segs, 1, &dma->dma_nsegs, BUS_DMA_WAITOK);
184 1.1 jmcneill if (error)
185 1.1 jmcneill return error;
186 1.1 jmcneill
187 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs,
188 1.1 jmcneill dma->dma_size, &dma->dma_addr, BUS_DMA_WAITOK | BUS_DMA_COHERENT);
189 1.1 jmcneill if (error)
190 1.1 jmcneill goto free;
191 1.1 jmcneill
192 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, dma->dma_size, dma->dma_nsegs,
193 1.1 jmcneill dma->dma_size, 0, BUS_DMA_WAITOK, &dma->dma_map);
194 1.1 jmcneill if (error)
195 1.1 jmcneill goto unmap;
196 1.1 jmcneill
197 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_addr,
198 1.1 jmcneill dma->dma_size, NULL, BUS_DMA_WAITOK);
199 1.1 jmcneill if (error)
200 1.1 jmcneill goto destroy;
201 1.1 jmcneill
202 1.1 jmcneill return 0;
203 1.1 jmcneill
204 1.1 jmcneill destroy:
205 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
206 1.1 jmcneill unmap:
207 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
208 1.1 jmcneill free:
209 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
210 1.1 jmcneill
211 1.1 jmcneill return error;
212 1.1 jmcneill }
213 1.1 jmcneill
214 1.1 jmcneill static void
215 1.1 jmcneill sunxi_i2s_freedma(struct sunxi_i2s_softc *sc, struct sunxi_i2s_dma *dma)
216 1.1 jmcneill {
217 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
218 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
219 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
220 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
221 1.1 jmcneill }
222 1.1 jmcneill
223 1.1 jmcneill static int
224 1.1 jmcneill sunxi_i2s_transfer(struct sunxi_i2s_chan *ch)
225 1.1 jmcneill {
226 1.1 jmcneill bus_dma_segment_t seg;
227 1.1 jmcneill
228 1.1 jmcneill seg.ds_addr = ch->ch_cur_phys;
229 1.1 jmcneill seg.ds_len = ch->ch_blksize;
230 1.1 jmcneill ch->ch_req.dreq_segs = &seg;
231 1.1 jmcneill ch->ch_req.dreq_nsegs = 1;
232 1.1 jmcneill
233 1.1 jmcneill return fdtbus_dma_transfer(ch->ch_dma, &ch->ch_req);
234 1.1 jmcneill }
235 1.1 jmcneill
236 1.1 jmcneill static int
237 1.1 jmcneill sunxi_i2s_open(void *priv, int flags)
238 1.1 jmcneill {
239 1.1 jmcneill return 0;
240 1.1 jmcneill }
241 1.1 jmcneill
242 1.1 jmcneill static void
243 1.1 jmcneill sunxi_i2s_close(void *priv)
244 1.1 jmcneill {
245 1.1 jmcneill }
246 1.1 jmcneill
247 1.1 jmcneill static int
248 1.1 jmcneill sunxi_i2s_query_encoding(void *priv, struct audio_encoding *ae)
249 1.1 jmcneill {
250 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
251 1.1 jmcneill
252 1.1 jmcneill return auconv_query_encoding(sc->sc_encodings, ae);
253 1.1 jmcneill }
254 1.1 jmcneill
255 1.1 jmcneill static int
256 1.1 jmcneill sunxi_i2s_set_params(void *priv, int setmode, int usemode,
257 1.1 jmcneill audio_params_t *play, audio_params_t *rec,
258 1.1 jmcneill stream_filter_list_t *pfil, stream_filter_list_t *rfil)
259 1.1 jmcneill {
260 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
261 1.1 jmcneill int index;
262 1.1 jmcneill
263 1.1 jmcneill if (play && (setmode & AUMODE_PLAY)) {
264 1.1 jmcneill index = auconv_set_converter(&sc->sc_format, 1,
265 1.1 jmcneill AUMODE_PLAY, play, true, pfil);
266 1.1 jmcneill if (index < 0)
267 1.1 jmcneill return EINVAL;
268 1.1 jmcneill sc->sc_pchan.ch_params = pfil->req_size > 0 ?
269 1.1 jmcneill pfil->filters[0].param : *play;
270 1.3 jmcneill pfil->prepend(pfil, linear16_16_to_linear32,
271 1.3 jmcneill &sc->sc_pchan.ch_params);
272 1.1 jmcneill }
273 1.1 jmcneill if (rec && (setmode & AUMODE_RECORD)) {
274 1.1 jmcneill index = auconv_set_converter(&sc->sc_format, 1,
275 1.1 jmcneill AUMODE_RECORD, rec, true, rfil);
276 1.1 jmcneill if (index < 0)
277 1.1 jmcneill return EINVAL;
278 1.1 jmcneill sc->sc_rchan.ch_params = rfil->req_size > 0 ?
279 1.1 jmcneill rfil->filters[0].param : *rec;
280 1.3 jmcneill rfil->prepend(rfil, linear32_32_to_linear16,
281 1.3 jmcneill &sc->sc_rchan.ch_params);
282 1.1 jmcneill }
283 1.1 jmcneill
284 1.1 jmcneill return 0;
285 1.1 jmcneill }
286 1.1 jmcneill
287 1.1 jmcneill static void *
288 1.1 jmcneill sunxi_i2s_allocm(void *priv, int dir, size_t size)
289 1.1 jmcneill {
290 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
291 1.1 jmcneill struct sunxi_i2s_dma *dma;
292 1.1 jmcneill int error;
293 1.1 jmcneill
294 1.1 jmcneill dma = kmem_alloc(sizeof(*dma), KM_SLEEP);
295 1.1 jmcneill
296 1.1 jmcneill error = sunxi_i2s_allocdma(sc, size, 16, dma);
297 1.1 jmcneill if (error) {
298 1.1 jmcneill kmem_free(dma, sizeof(*dma));
299 1.1 jmcneill device_printf(sc->sc_dev, "couldn't allocate DMA memory (%d)\n",
300 1.1 jmcneill error);
301 1.1 jmcneill return NULL;
302 1.1 jmcneill }
303 1.1 jmcneill
304 1.1 jmcneill LIST_INSERT_HEAD(&sc->sc_dmalist, dma, dma_list);
305 1.1 jmcneill
306 1.1 jmcneill return dma->dma_addr;
307 1.1 jmcneill }
308 1.1 jmcneill
309 1.1 jmcneill static void
310 1.1 jmcneill sunxi_i2s_freem(void *priv, void *addr, size_t size)
311 1.1 jmcneill {
312 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
313 1.1 jmcneill struct sunxi_i2s_dma *dma;
314 1.1 jmcneill
315 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
316 1.1 jmcneill if (dma->dma_addr == addr) {
317 1.1 jmcneill sunxi_i2s_freedma(sc, dma);
318 1.1 jmcneill LIST_REMOVE(dma, dma_list);
319 1.1 jmcneill kmem_free(dma, sizeof(*dma));
320 1.1 jmcneill break;
321 1.1 jmcneill }
322 1.1 jmcneill }
323 1.1 jmcneill
324 1.1 jmcneill static paddr_t
325 1.1 jmcneill sunxi_i2s_mappage(void *priv, void *addr, off_t off, int prot)
326 1.1 jmcneill {
327 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
328 1.1 jmcneill struct sunxi_i2s_dma *dma;
329 1.1 jmcneill
330 1.1 jmcneill if (off < 0)
331 1.1 jmcneill return -1;
332 1.1 jmcneill
333 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
334 1.1 jmcneill if (dma->dma_addr == addr) {
335 1.1 jmcneill return bus_dmamem_mmap(sc->sc_dmat, dma->dma_segs,
336 1.1 jmcneill dma->dma_nsegs, off, prot, BUS_DMA_WAITOK);
337 1.1 jmcneill }
338 1.1 jmcneill
339 1.1 jmcneill return -1;
340 1.1 jmcneill }
341 1.1 jmcneill
342 1.1 jmcneill static int
343 1.1 jmcneill sunxi_i2s_get_props(void *priv)
344 1.1 jmcneill {
345 1.1 jmcneill return AUDIO_PROP_PLAYBACK|AUDIO_PROP_CAPTURE|
346 1.2 jmcneill AUDIO_PROP_MMAP|AUDIO_PROP_FULLDUPLEX|AUDIO_PROP_INDEPENDENT;
347 1.1 jmcneill }
348 1.1 jmcneill
349 1.1 jmcneill static int
350 1.1 jmcneill sunxi_i2s_round_blocksize(void *priv, int bs, int mode,
351 1.1 jmcneill const audio_params_t *params)
352 1.1 jmcneill {
353 1.1 jmcneill bs &= ~3;
354 1.1 jmcneill if (bs == 0)
355 1.1 jmcneill bs = 4;
356 1.1 jmcneill return bs;
357 1.1 jmcneill }
358 1.1 jmcneill
359 1.1 jmcneill static size_t
360 1.1 jmcneill sunxi_i2s_round_buffersize(void *priv, int dir, size_t bufsize)
361 1.1 jmcneill {
362 1.1 jmcneill return bufsize;
363 1.1 jmcneill }
364 1.1 jmcneill
365 1.1 jmcneill static int
366 1.1 jmcneill sunxi_i2s_trigger_output(void *priv, void *start, void *end, int blksize,
367 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
368 1.1 jmcneill {
369 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
370 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_pchan;
371 1.1 jmcneill struct sunxi_i2s_dma *dma;
372 1.1 jmcneill bus_addr_t pstart;
373 1.1 jmcneill bus_size_t psize;
374 1.1 jmcneill uint32_t val;
375 1.1 jmcneill int error;
376 1.1 jmcneill
377 1.1 jmcneill pstart = 0;
378 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start;
379 1.1 jmcneill
380 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
381 1.1 jmcneill if (dma->dma_addr == start) {
382 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr;
383 1.1 jmcneill break;
384 1.1 jmcneill }
385 1.1 jmcneill if (pstart == 0) {
386 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start);
387 1.1 jmcneill return EINVAL;
388 1.1 jmcneill }
389 1.1 jmcneill
390 1.1 jmcneill ch->ch_intr = intr;
391 1.1 jmcneill ch->ch_intrarg = intrarg;
392 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart;
393 1.1 jmcneill ch->ch_end_phys = pstart + psize;
394 1.1 jmcneill ch->ch_blksize = blksize;
395 1.1 jmcneill
396 1.1 jmcneill /* Flush FIFO */
397 1.1 jmcneill val = I2S_READ(sc, DA_FCTL);
398 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FTX);
399 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FTX);
400 1.1 jmcneill
401 1.1 jmcneill /* Reset TX sample counter */
402 1.1 jmcneill I2S_WRITE(sc, DA_TXCNT, 0);
403 1.1 jmcneill
404 1.1 jmcneill /* Enable transmitter block */
405 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
406 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val | DA_CTL_TXEN);
407 1.1 jmcneill
408 1.1 jmcneill /* Enable TX DRQ */
409 1.1 jmcneill val = I2S_READ(sc, DA_INT);
410 1.1 jmcneill I2S_WRITE(sc, DA_INT, val | DA_INT_TX_DRQ);
411 1.1 jmcneill
412 1.1 jmcneill /* Start DMA transfer */
413 1.1 jmcneill error = sunxi_i2s_transfer(ch);
414 1.1 jmcneill if (error != 0) {
415 1.1 jmcneill aprint_error_dev(sc->sc_dev,
416 1.1 jmcneill "failed to start DMA transfer: %d\n", error);
417 1.1 jmcneill return error;
418 1.1 jmcneill }
419 1.1 jmcneill
420 1.1 jmcneill return 0;
421 1.1 jmcneill }
422 1.1 jmcneill
423 1.1 jmcneill static int
424 1.1 jmcneill sunxi_i2s_trigger_input(void *priv, void *start, void *end, int blksize,
425 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
426 1.1 jmcneill {
427 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
428 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_rchan;
429 1.1 jmcneill struct sunxi_i2s_dma *dma;
430 1.1 jmcneill bus_addr_t pstart;
431 1.1 jmcneill bus_size_t psize;
432 1.1 jmcneill uint32_t val;
433 1.1 jmcneill int error;
434 1.1 jmcneill
435 1.1 jmcneill pstart = 0;
436 1.1 jmcneill psize = (uintptr_t)end - (uintptr_t)start;
437 1.1 jmcneill
438 1.1 jmcneill LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
439 1.1 jmcneill if (dma->dma_addr == start) {
440 1.1 jmcneill pstart = dma->dma_map->dm_segs[0].ds_addr;
441 1.1 jmcneill break;
442 1.1 jmcneill }
443 1.1 jmcneill if (pstart == 0) {
444 1.1 jmcneill device_printf(sc->sc_dev, "bad addr %p\n", start);
445 1.1 jmcneill return EINVAL;
446 1.1 jmcneill }
447 1.1 jmcneill
448 1.1 jmcneill ch->ch_intr = intr;
449 1.1 jmcneill ch->ch_intrarg = intrarg;
450 1.1 jmcneill ch->ch_start_phys = ch->ch_cur_phys = pstart;
451 1.1 jmcneill ch->ch_end_phys = pstart + psize;
452 1.1 jmcneill ch->ch_blksize = blksize;
453 1.1 jmcneill
454 1.1 jmcneill /* Flush FIFO */
455 1.1 jmcneill val = I2S_READ(sc, DA_FCTL);
456 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FRX);
457 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FRX);
458 1.1 jmcneill
459 1.1 jmcneill /* Reset RX sample counter */
460 1.1 jmcneill I2S_WRITE(sc, DA_RXCNT, 0);
461 1.1 jmcneill
462 1.1 jmcneill /* Enable receiver block */
463 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
464 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val | DA_CTL_RXEN);
465 1.1 jmcneill
466 1.1 jmcneill /* Enable RX DRQ */
467 1.1 jmcneill val = I2S_READ(sc, DA_INT);
468 1.1 jmcneill I2S_WRITE(sc, DA_INT, val | DA_INT_RX_DRQ);
469 1.1 jmcneill
470 1.1 jmcneill /* Start DMA transfer */
471 1.1 jmcneill error = sunxi_i2s_transfer(ch);
472 1.1 jmcneill if (error != 0) {
473 1.1 jmcneill aprint_error_dev(sc->sc_dev,
474 1.1 jmcneill "failed to start DMA transfer: %d\n", error);
475 1.1 jmcneill return error;
476 1.1 jmcneill }
477 1.1 jmcneill
478 1.1 jmcneill return 0;
479 1.1 jmcneill }
480 1.1 jmcneill
481 1.1 jmcneill static int
482 1.1 jmcneill sunxi_i2s_halt_output(void *priv)
483 1.1 jmcneill {
484 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
485 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_pchan;
486 1.1 jmcneill uint32_t val;
487 1.1 jmcneill
488 1.1 jmcneill /* Disable DMA channel */
489 1.1 jmcneill fdtbus_dma_halt(ch->ch_dma);
490 1.1 jmcneill
491 1.1 jmcneill /* Disable transmitter block */
492 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
493 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_TXEN);
494 1.1 jmcneill
495 1.1 jmcneill /* Disable TX DRQ */
496 1.1 jmcneill val = I2S_READ(sc, DA_INT);
497 1.1 jmcneill I2S_WRITE(sc, DA_INT, val & ~DA_INT_TX_DRQ);
498 1.1 jmcneill
499 1.1 jmcneill ch->ch_intr = NULL;
500 1.1 jmcneill ch->ch_intrarg = NULL;
501 1.1 jmcneill
502 1.1 jmcneill return 0;
503 1.1 jmcneill }
504 1.1 jmcneill
505 1.1 jmcneill static int
506 1.1 jmcneill sunxi_i2s_halt_input(void *priv)
507 1.1 jmcneill {
508 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
509 1.1 jmcneill struct sunxi_i2s_chan *ch = &sc->sc_rchan;
510 1.1 jmcneill uint32_t val;
511 1.1 jmcneill
512 1.1 jmcneill /* Disable DMA channel */
513 1.1 jmcneill fdtbus_dma_halt(ch->ch_dma);
514 1.1 jmcneill
515 1.1 jmcneill /* Disable receiver block */
516 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
517 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_RXEN);
518 1.1 jmcneill
519 1.1 jmcneill /* Disable RX DRQ */
520 1.1 jmcneill val = I2S_READ(sc, DA_INT);
521 1.1 jmcneill I2S_WRITE(sc, DA_INT, val & ~DA_INT_RX_DRQ);
522 1.1 jmcneill
523 1.1 jmcneill return 0;
524 1.1 jmcneill }
525 1.1 jmcneill
526 1.1 jmcneill static void
527 1.1 jmcneill sunxi_i2s_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
528 1.1 jmcneill {
529 1.1 jmcneill struct sunxi_i2s_softc * const sc = priv;
530 1.1 jmcneill
531 1.1 jmcneill *intr = &sc->sc_intr_lock;
532 1.1 jmcneill *thread = &sc->sc_lock;
533 1.1 jmcneill }
534 1.1 jmcneill
535 1.1 jmcneill static const struct audio_hw_if sunxi_i2s_hw_if = {
536 1.1 jmcneill .open = sunxi_i2s_open,
537 1.1 jmcneill .close = sunxi_i2s_close,
538 1.1 jmcneill .drain = NULL,
539 1.1 jmcneill .query_encoding = sunxi_i2s_query_encoding,
540 1.1 jmcneill .set_params = sunxi_i2s_set_params,
541 1.1 jmcneill .allocm = sunxi_i2s_allocm,
542 1.1 jmcneill .freem = sunxi_i2s_freem,
543 1.1 jmcneill .mappage = sunxi_i2s_mappage,
544 1.1 jmcneill .get_props = sunxi_i2s_get_props,
545 1.1 jmcneill .round_blocksize = sunxi_i2s_round_blocksize,
546 1.1 jmcneill .round_buffersize = sunxi_i2s_round_buffersize,
547 1.1 jmcneill .trigger_output = sunxi_i2s_trigger_output,
548 1.1 jmcneill .trigger_input = sunxi_i2s_trigger_input,
549 1.1 jmcneill .halt_output = sunxi_i2s_halt_output,
550 1.1 jmcneill .halt_input = sunxi_i2s_halt_input,
551 1.1 jmcneill .get_locks = sunxi_i2s_get_locks,
552 1.1 jmcneill };
553 1.1 jmcneill
554 1.1 jmcneill static void
555 1.1 jmcneill sunxi_i2s_dmaintr(void *priv)
556 1.1 jmcneill {
557 1.1 jmcneill struct sunxi_i2s_chan * const ch = priv;
558 1.1 jmcneill struct sunxi_i2s_softc * const sc = ch->ch_sc;
559 1.1 jmcneill
560 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
561 1.1 jmcneill ch->ch_cur_phys += ch->ch_blksize;
562 1.1 jmcneill if (ch->ch_cur_phys >= ch->ch_end_phys)
563 1.1 jmcneill ch->ch_cur_phys = ch->ch_start_phys;
564 1.1 jmcneill
565 1.1 jmcneill if (ch->ch_intr) {
566 1.1 jmcneill ch->ch_intr(ch->ch_intrarg);
567 1.1 jmcneill sunxi_i2s_transfer(ch);
568 1.1 jmcneill }
569 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
570 1.1 jmcneill }
571 1.1 jmcneill
572 1.1 jmcneill static int
573 1.1 jmcneill sunxi_i2s_chan_init(struct sunxi_i2s_softc *sc,
574 1.1 jmcneill struct sunxi_i2s_chan *ch, u_int mode, const char *dmaname)
575 1.1 jmcneill {
576 1.1 jmcneill ch->ch_sc = sc;
577 1.1 jmcneill ch->ch_mode = mode;
578 1.1 jmcneill ch->ch_dma = fdtbus_dma_get(sc->sc_phandle, dmaname, sunxi_i2s_dmaintr, ch);
579 1.1 jmcneill if (ch->ch_dma == NULL) {
580 1.1 jmcneill aprint_error(": couldn't get dma channel \"%s\"\n", dmaname);
581 1.1 jmcneill return ENXIO;
582 1.1 jmcneill }
583 1.1 jmcneill
584 1.1 jmcneill if (mode == AUMODE_PLAY) {
585 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_WRITE;
586 1.1 jmcneill ch->ch_req.dreq_dev_phys =
587 1.1 jmcneill sc->sc_baseaddr + DA_TXFIFO;
588 1.1 jmcneill } else {
589 1.1 jmcneill ch->ch_req.dreq_dir = FDT_DMA_READ;
590 1.1 jmcneill ch->ch_req.dreq_dev_phys =
591 1.1 jmcneill sc->sc_baseaddr + DA_RXFIFO;
592 1.1 jmcneill }
593 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_bus_width = 32;
594 1.1 jmcneill ch->ch_req.dreq_mem_opt.opt_burst_len = 8;
595 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_bus_width = 32;
596 1.1 jmcneill ch->ch_req.dreq_dev_opt.opt_burst_len = 8;
597 1.1 jmcneill
598 1.1 jmcneill return 0;
599 1.1 jmcneill }
600 1.1 jmcneill
601 1.1 jmcneill static int
602 1.1 jmcneill sunxi_i2s_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir)
603 1.1 jmcneill {
604 1.1 jmcneill struct sunxi_i2s_softc * const sc = audio_dai_private(dai);
605 1.1 jmcneill uint32_t val;
606 1.1 jmcneill
607 1.1 jmcneill /* XXX */
608 1.1 jmcneill
609 1.1 jmcneill val = DA_CLKD_MCLKO_EN;
610 1.3 jmcneill val |= __SHIFTIN(DA_CLKD_BCLKDIV_8, DA_CLKD_BCLKDIV);
611 1.1 jmcneill val |= __SHIFTIN(DA_CLKD_MCLKDIV_1, DA_CLKD_MCLKDIV);
612 1.1 jmcneill
613 1.1 jmcneill I2S_WRITE(sc, DA_CLKD, val);
614 1.1 jmcneill
615 1.1 jmcneill return 0;
616 1.1 jmcneill }
617 1.1 jmcneill
618 1.1 jmcneill static int
619 1.1 jmcneill sunxi_i2s_dai_set_format(audio_dai_tag_t dai, u_int format)
620 1.1 jmcneill {
621 1.1 jmcneill struct sunxi_i2s_softc * const sc = audio_dai_private(dai);
622 1.1 jmcneill uint32_t ctl, fat0;
623 1.1 jmcneill
624 1.1 jmcneill const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK);
625 1.1 jmcneill const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK);
626 1.1 jmcneill const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK);
627 1.1 jmcneill
628 1.1 jmcneill ctl = I2S_READ(sc, DA_CTL);
629 1.1 jmcneill fat0 = I2S_READ(sc, DA_FAT0);
630 1.1 jmcneill
631 1.1 jmcneill fat0 &= ~DA_FAT0_FMT;
632 1.1 jmcneill switch (fmt) {
633 1.1 jmcneill case AUDIO_DAI_FORMAT_I2S:
634 1.1 jmcneill fat0 |= __SHIFTIN(DA_FMT_I2S, DA_FAT0_FMT);
635 1.1 jmcneill break;
636 1.1 jmcneill case AUDIO_DAI_FORMAT_RJ:
637 1.1 jmcneill fat0 |= __SHIFTIN(DA_FMT_RJ, DA_FAT0_FMT);
638 1.1 jmcneill break;
639 1.1 jmcneill case AUDIO_DAI_FORMAT_LJ:
640 1.1 jmcneill fat0 |= __SHIFTIN(DA_FMT_LJ, DA_FAT0_FMT);
641 1.1 jmcneill break;
642 1.1 jmcneill default:
643 1.1 jmcneill return EINVAL;
644 1.1 jmcneill }
645 1.1 jmcneill
646 1.1 jmcneill fat0 &= ~(DA_FAT0_LRCP|DA_FAT0_BCP);
647 1.1 jmcneill if (AUDIO_DAI_POLARITY_B(pol))
648 1.1 jmcneill fat0 |= __SHIFTIN(DA_BCP_INVERTED, DA_FAT0_BCP);
649 1.1 jmcneill if (AUDIO_DAI_POLARITY_F(pol))
650 1.1 jmcneill fat0 |= __SHIFTIN(DA_LRCP_INVERTED, DA_FAT0_LRCP);
651 1.1 jmcneill
652 1.1 jmcneill switch (clk) {
653 1.1 jmcneill case AUDIO_DAI_CLOCK_CBM_CFM:
654 1.1 jmcneill ctl |= DA_CTL_MS; /* codec is master */
655 1.1 jmcneill break;
656 1.1 jmcneill case AUDIO_DAI_CLOCK_CBS_CFS:
657 1.1 jmcneill ctl &= ~DA_CTL_MS; /* codec is slave */
658 1.1 jmcneill break;
659 1.1 jmcneill default:
660 1.1 jmcneill return EINVAL;
661 1.1 jmcneill }
662 1.1 jmcneill
663 1.1 jmcneill ctl &= ~DA_CTL_PCM;
664 1.1 jmcneill
665 1.1 jmcneill I2S_WRITE(sc, DA_CTL, ctl);
666 1.1 jmcneill I2S_WRITE(sc, DA_FAT0, fat0);
667 1.1 jmcneill
668 1.1 jmcneill return 0;
669 1.1 jmcneill }
670 1.1 jmcneill
671 1.1 jmcneill static audio_dai_tag_t
672 1.1 jmcneill sunxi_i2s_dai_get_tag(device_t dev, const void *data, size_t len)
673 1.1 jmcneill {
674 1.1 jmcneill struct sunxi_i2s_softc * const sc = device_private(dev);
675 1.1 jmcneill
676 1.1 jmcneill if (len != 4)
677 1.1 jmcneill return NULL;
678 1.1 jmcneill
679 1.1 jmcneill return &sc->sc_dai;
680 1.1 jmcneill }
681 1.1 jmcneill
682 1.1 jmcneill static struct fdtbus_dai_controller_func sunxi_i2s_dai_funcs = {
683 1.1 jmcneill .get_tag = sunxi_i2s_dai_get_tag
684 1.1 jmcneill };
685 1.1 jmcneill
686 1.1 jmcneill static int
687 1.1 jmcneill sunxi_i2s_clock_init(int phandle)
688 1.1 jmcneill {
689 1.1 jmcneill struct fdtbus_reset *rst;
690 1.1 jmcneill struct clk *clk;
691 1.1 jmcneill int error;
692 1.1 jmcneill
693 1.1 jmcneill /* Set module clock to 24.576MHz, suitable for 48 kHz sampling rates */
694 1.1 jmcneill clk = fdtbus_clock_get(phandle, "mod");
695 1.1 jmcneill if (clk == NULL) {
696 1.1 jmcneill aprint_error(": couldn't find mod clock\n");
697 1.1 jmcneill return ENXIO;
698 1.1 jmcneill }
699 1.1 jmcneill error = clk_set_rate(clk, SUNXI_I2S_CLK_RATE);
700 1.1 jmcneill if (error != 0) {
701 1.1 jmcneill aprint_error(": couldn't set mod clock rate: %d\n", error);
702 1.1 jmcneill return error;
703 1.1 jmcneill }
704 1.1 jmcneill error = clk_enable(clk);
705 1.1 jmcneill if (error != 0) {
706 1.1 jmcneill aprint_error(": couldn't enable mod clock: %d\n", error);
707 1.1 jmcneill return error;
708 1.1 jmcneill }
709 1.1 jmcneill
710 1.1 jmcneill /* Enable APB clock */
711 1.1 jmcneill clk = fdtbus_clock_get(phandle, "apb");
712 1.1 jmcneill if (clk == NULL) {
713 1.1 jmcneill aprint_error(": couldn't find apb clock\n");
714 1.1 jmcneill return ENXIO;
715 1.1 jmcneill }
716 1.1 jmcneill error = clk_enable(clk);
717 1.1 jmcneill if (error != 0) {
718 1.1 jmcneill aprint_error(": couldn't enable apb clock: %d\n", error);
719 1.1 jmcneill return error;
720 1.1 jmcneill }
721 1.1 jmcneill
722 1.1 jmcneill /* De-assert reset */
723 1.1 jmcneill rst = fdtbus_reset_get(phandle, "rst");
724 1.1 jmcneill if (rst == NULL) {
725 1.1 jmcneill aprint_error(": couldn't find reset\n");
726 1.1 jmcneill return ENXIO;
727 1.1 jmcneill }
728 1.1 jmcneill error = fdtbus_reset_deassert(rst);
729 1.1 jmcneill if (error != 0) {
730 1.1 jmcneill aprint_error(": couldn't de-assert reset: %d\n", error);
731 1.1 jmcneill return error;
732 1.1 jmcneill }
733 1.1 jmcneill
734 1.1 jmcneill return 0;
735 1.1 jmcneill }
736 1.1 jmcneill
737 1.1 jmcneill static int
738 1.1 jmcneill sunxi_i2s_match(device_t parent, cfdata_t cf, void *aux)
739 1.1 jmcneill {
740 1.1 jmcneill struct fdt_attach_args * const faa = aux;
741 1.1 jmcneill
742 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
743 1.1 jmcneill }
744 1.1 jmcneill
745 1.1 jmcneill static void
746 1.1 jmcneill sunxi_i2s_attach(device_t parent, device_t self, void *aux)
747 1.1 jmcneill {
748 1.1 jmcneill struct sunxi_i2s_softc * const sc = device_private(self);
749 1.1 jmcneill struct fdt_attach_args * const faa = aux;
750 1.1 jmcneill const int phandle = faa->faa_phandle;
751 1.1 jmcneill bus_addr_t addr;
752 1.1 jmcneill bus_size_t size;
753 1.1 jmcneill uint32_t val;
754 1.1 jmcneill int error;
755 1.1 jmcneill
756 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
757 1.1 jmcneill aprint_error(": couldn't get registers\n");
758 1.1 jmcneill return;
759 1.1 jmcneill }
760 1.1 jmcneill
761 1.1 jmcneill if (sunxi_i2s_clock_init(phandle) != 0)
762 1.1 jmcneill return;
763 1.1 jmcneill
764 1.1 jmcneill sc->sc_dev = self;
765 1.1 jmcneill sc->sc_phandle = phandle;
766 1.1 jmcneill sc->sc_baseaddr = addr;
767 1.1 jmcneill sc->sc_bst = faa->faa_bst;
768 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
769 1.1 jmcneill aprint_error(": couldn't map registers\n");
770 1.1 jmcneill return;
771 1.1 jmcneill }
772 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
773 1.1 jmcneill LIST_INIT(&sc->sc_dmalist);
774 1.1 jmcneill sc->sc_cfg = (void *)of_search_compatible(phandle, compat_data)->data;
775 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
776 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
777 1.1 jmcneill
778 1.1 jmcneill if (sunxi_i2s_chan_init(sc, &sc->sc_pchan, AUMODE_PLAY, "tx") != 0 ||
779 1.1 jmcneill sunxi_i2s_chan_init(sc, &sc->sc_rchan, AUMODE_RECORD, "rx") != 0) {
780 1.1 jmcneill aprint_error(": couldn't setup channels\n");
781 1.1 jmcneill return;
782 1.1 jmcneill }
783 1.1 jmcneill
784 1.1 jmcneill aprint_naive("\n");
785 1.1 jmcneill aprint_normal(": %s\n", sc->sc_cfg->name);
786 1.1 jmcneill
787 1.1 jmcneill /* Reset */
788 1.1 jmcneill val = I2S_READ(sc, DA_CTL);
789 1.1 jmcneill val &= ~(DA_CTL_TXEN|DA_CTL_RXEN|DA_CTL_GEN);
790 1.1 jmcneill I2S_WRITE(sc, DA_CTL, val);
791 1.1 jmcneill
792 1.1 jmcneill val = I2S_READ(sc, DA_FCTL);
793 1.1 jmcneill val &= ~(DA_FCTL_FTX|DA_FCTL_FRX);
794 1.1 jmcneill I2S_WRITE(sc, DA_FCTL, val);
795 1.1 jmcneill
796 1.1 jmcneill I2S_WRITE(sc, DA_TXCNT, 0);
797 1.1 jmcneill I2S_WRITE(sc, DA_RXCNT, 0);
798 1.1 jmcneill
799 1.1 jmcneill /* Enable */
800 1.1 jmcneill I2S_WRITE(sc, DA_CTL, DA_CTL_GEN | DA_CTL_SDO_EN);
801 1.1 jmcneill
802 1.1 jmcneill /* Setup channels */
803 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->txchmap, 0x76543210);
804 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->txchsel, __SHIFTIN(1, DA_CHSEL_SEL) |
805 1.1 jmcneill __SHIFTIN(3, DA_CHSEL_EN));
806 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->rxchmap, 0x76543210);
807 1.1 jmcneill I2S_WRITE(sc, sc->sc_cfg->rxchsel, __SHIFTIN(1, DA_CHSEL_SEL) |
808 1.1 jmcneill __SHIFTIN(3, DA_CHSEL_EN));
809 1.1 jmcneill
810 1.1 jmcneill sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
811 1.1 jmcneill sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
812 1.1 jmcneill sc->sc_format.validbits = 16;
813 1.1 jmcneill sc->sc_format.precision = 16;
814 1.1 jmcneill sc->sc_format.channels = 2;
815 1.1 jmcneill sc->sc_format.channel_mask = AUFMT_STEREO;
816 1.1 jmcneill sc->sc_format.frequency_type = 0;
817 1.1 jmcneill sc->sc_format.frequency[0] = sc->sc_format.frequency[1] = 48000;
818 1.1 jmcneill
819 1.1 jmcneill error = auconv_create_encodings(&sc->sc_format, 1, &sc->sc_encodings);
820 1.1 jmcneill if (error) {
821 1.1 jmcneill aprint_error_dev(self, "couldn't create encodings\n");
822 1.1 jmcneill return;
823 1.1 jmcneill }
824 1.1 jmcneill
825 1.1 jmcneill sc->sc_dai.dai_set_sysclk = sunxi_i2s_dai_set_sysclk;
826 1.1 jmcneill sc->sc_dai.dai_set_format = sunxi_i2s_dai_set_format;
827 1.1 jmcneill sc->sc_dai.dai_hw_if = &sunxi_i2s_hw_if;
828 1.1 jmcneill sc->sc_dai.dai_dev = self;
829 1.1 jmcneill sc->sc_dai.dai_priv = sc;
830 1.1 jmcneill fdtbus_register_dai_controller(self, phandle, &sunxi_i2s_dai_funcs);
831 1.1 jmcneill }
832 1.1 jmcneill
833 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_i2s, sizeof(struct sunxi_i2s_softc),
834 1.1 jmcneill sunxi_i2s_match, sunxi_i2s_attach, NULL, NULL);
835