xref: /freebsd/sys/arm/allwinner/aw_i2s.c (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2020 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
5  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/rman.h>
42 #include <sys/resource.h>
43 #include <machine/bus.h>
44 
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include <dev/extres/clk/clk.h>
49 #include <dev/extres/hwreset/hwreset.h>
50 
51 #include "syscon_if.h"
52 
53 #include "opt_snd.h"
54 #include <dev/sound/pcm/sound.h>
55 #include <dev/sound/fdt/audio_dai.h>
56 #include "audio_dai_if.h"
57 
58 #define	FIFO_LEVEL	0x40
59 
60 #define	DA_CTL		0x00
61 #define		DA_CTL_BCLK_OUT (1 << 18)	/* sun8i */
62 #define		DA_CLK_LRCK_OUT (1 << 17)	/* sun8i */
63 #define		DA_CTL_SDO_EN	(1 << 8)
64 #define		DA_CTL_MS	(1 << 5)	/* sun4i */
65 #define		DA_CTL_PCM	(1 << 4)	/* sun4i */
66 #define		DA_CTL_MODE_SEL_MASK	(3 << 4) /* sun8i */
67 #define		DA_CTL_MODE_SEL_PCM	(0 << 4) /* sun8i */
68 #define		DA_CTL_MODE_SEL_LJ	(1 << 4) /* sun8i */
69 #define		DA_CTL_MODE_SEL_RJ	(2 << 4) /* sun8i */
70 #define		DA_CTL_TXEN	(1 << 2)
71 #define		DA_CTL_RXEN	(1 << 1)
72 #define		DA_CTL_GEN	(1 << 0)
73 #define	DA_FAT0		0x04
74 #define		DA_FAT0_LRCK_PERIOD_MASK	(0x3ff << 8) /* sun8i */
75 #define		DA_FAT0_LRCK_PERIOD(n)		(((n) & 0x3fff) << 8) /* sun8i */
76 #define		DA_FAT0_LRCP_MASK	(1 << 7)
77 #define		DA_LRCP_NORMAL		(0 << 7)
78 #define		DA_LRCP_INVERTED	(1 << 7)
79 #define		DA_FAT0_BCP_MASK	(1 << 6)
80 #define		DA_BCP_NORMAL		(0 << 6)
81 #define		DA_BCP_INVERTED		(1 << 6)
82 #define		DA_FAT0_SR	__BITS(5,4)
83 #define		DA_FAT0_WSS	__BITS(3,2)
84 #define		DA_FAT0_FMT_MASK	(3 << 0)
85 #define		 DA_FMT_I2S	0
86 #define		 DA_FMT_LJ	1
87 #define		 DA_FMT_RJ	2
88 #define	DA_FAT1		0x08
89 #define	DA_ISTA		0x0c
90 #define		DA_ISTA_TXUI_INT	(1 << 6)
91 #define		DA_ISTA_TXEI_INT	(1 << 4)
92 #define		DA_ISTA_RXAI_INT	(1 << 0)
93 #define	DA_RXFIFO	0x10
94 #define	DA_FCTL		0x14
95 #define		DA_FCTL_HUB_EN	(1 << 31)
96 #define		DA_FCTL_FTX	(1 << 25)
97 #define		DA_FCTL_FRX	(1 << 24)
98 #define		DA_FCTL_TXTL_MASK	(0x7f << 12)
99 #define		DA_FCTL_TXTL(v)		(((v) & 0x7f) << 12)
100 #define		DA_FCTL_TXIM	(1 << 2)
101 #define	DA_FSTA		0x18
102 #define		DA_FSTA_TXE_CNT(v)	(((v) >> 16) & 0xff)
103 #define		DA_FSTA_RXA_CNT(v)	((v) & 0x3f)
104 #define	DA_INT		0x1c
105 #define		DA_INT_TX_DRQ	(1 << 7)
106 #define		DA_INT_TXUI_EN	(1 << 6)
107 #define		DA_INT_TXEI_EN	(1 << 4)
108 #define		DA_INT_RX_DRQ	(1 << 3)
109 #define		DA_INT_RXAI_EN	(1 << 0)
110 #define	DA_TXFIFO	0x20
111 #define	DA_CLKD		0x24
112 #define		DA_CLKD_MCLKO_EN_SUN8I (1 << 8)
113 #define		DA_CLKD_MCLKO_EN_SUN4I (1 << 7)
114 #define		DA_CLKD_BCLKDIV_SUN8I(n)	(((n) & 0xf) << 4)
115 #define		DA_CLKD_BCLKDIV_SUN8I_MASK	(0xf << 4)
116 #define		DA_CLKD_BCLKDIV_SUN4I(n)	(((n) & 7) << 4)
117 #define		DA_CLKD_BCLKDIV_SUN4I_MASK	(7 << 4)
118 #define		 DA_CLKD_BCLKDIV_8	3
119 #define		 DA_CLKD_BCLKDIV_16	5
120 #define		DA_CLKD_MCLKDIV(n)	(((n) & 0xff) << 0)
121 #define		DA_CLKD_MCLKDIV_MASK	(0xf << 0)
122 #define		 DA_CLKD_MCLKDIV_1	0
123 #define	DA_TXCNT	0x28
124 #define	DA_RXCNT	0x2c
125 #define	DA_CHCFG	0x30		/* sun8i */
126 #define		DA_CHCFG_TX_SLOT_HIZ	(1 << 9)
127 #define		DA_CHCFG_TXN_STATE	(1 << 8)
128 #define		DA_CHCFG_RX_SLOT_NUM_MASK	(7 << 4)
129 #define		DA_CHCFG_RX_SLOT_NUM(n)		(((n) & 7) << 4)
130 #define		DA_CHCFG_TX_SLOT_NUM_MASK	(7 << 0)
131 #define		DA_CHCFG_TX_SLOT_NUM(n)		(((n) & 7) << 0)
132 
133 #define	DA_CHSEL_OFFSET(n)	(((n) & 3) << 12)	/* sun8i */
134 #define	DA_CHSEL_OFFSET_MASK	(3 << 12)	/* sun8i */
135 #define	DA_CHSEL_EN(n)		(((n) & 0xff) << 4)
136 #define	DA_CHSEL_EN_MASK	(0xff << 4)
137 #define	DA_CHSEL_SEL(n)		(((n) & 7) << 0)
138 #define	DA_CHSEL_SEL_MASK	(7 << 0)
139 
140 #define	AUDIO_BUFFER_SIZE	48000 * 4
141 
142 #define	AW_I2S_SAMPLE_RATE	48000
143 #define	AW_I2S_CLK_RATE		24576000
144 
145 enum sunxi_i2s_type {
146 	SUNXI_I2S_SUN4I,
147 	SUNXI_I2S_SUN8I,
148 };
149 
150 struct sunxi_i2s_config {
151 	const char	*name;
152 	enum sunxi_i2s_type type;
153 	bus_size_t	txchsel;
154 	bus_size_t	txchmap;
155 	bus_size_t	rxchsel;
156 	bus_size_t	rxchmap;
157 };
158 
159 static const struct sunxi_i2s_config sun50i_a64_codec_config = {
160 	.name = "Audio Codec (digital part)",
161 	.type = SUNXI_I2S_SUN4I,
162 	.txchsel = 0x30,
163 	.txchmap = 0x34,
164 	.rxchsel = 0x38,
165 	.rxchmap = 0x3c,
166 };
167 
168 static const struct sunxi_i2s_config sun8i_h3_config = {
169 	.name = "I2S/PCM controller",
170 	.type = SUNXI_I2S_SUN8I,
171 	.txchsel = 0x34,
172 	.txchmap = 0x44,
173 	.rxchsel = 0x54,
174 	.rxchmap = 0x58,
175 };
176 
177 static const u_int sun4i_i2s_bclk_divmap[] = {
178 	[0] = 2,
179 	[1] = 4,
180 	[2] = 6,
181 	[3] = 8,
182 	[4] = 12,
183 	[5] = 16,
184 };
185 
186 static const u_int sun4i_i2s_mclk_divmap[] = {
187 	[0] = 1,
188 	[1] = 2,
189 	[2] = 4,
190 	[3] = 6,
191 	[4] = 8,
192 	[5] = 12,
193 	[6] = 16,
194 	[7] = 24,
195 };
196 
197 static const u_int sun8i_i2s_divmap[] = {
198 	[1] = 1,
199 	[2] = 2,
200 	[3] = 4,
201 	[4] = 6,
202 	[5] = 8,
203 	[6] = 12,
204 	[7] = 16,
205 	[8] = 24,
206 	[9] = 32,
207 	[10] = 48,
208 	[11] = 64,
209 	[12] = 96,
210 	[13] = 128,
211 	[14] = 176,
212 	[15] = 192,
213 };
214 
215 
216 static struct ofw_compat_data compat_data[] = {
217 	{ "allwinner,sun50i-a64-codec-i2s", (uintptr_t)&sun50i_a64_codec_config },
218 	{ "allwinner,sun8i-h3-i2s", (uintptr_t)&sun8i_h3_config },
219 	{ NULL,					0 }
220 };
221 
222 static struct resource_spec aw_i2s_spec[] = {
223 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
224 	{ SYS_RES_IRQ,		0,	RF_ACTIVE | RF_SHAREABLE },
225 	{ -1, 0 }
226 };
227 
228 struct aw_i2s_softc {
229 	device_t	dev;
230 	struct resource	*res[2];
231 	struct mtx	mtx;
232 	clk_t		clk;
233 	struct sunxi_i2s_config	*cfg;
234 	void *		intrhand;
235 	/* pointers to playback/capture buffers */
236 	uint32_t	play_ptr;
237 	uint32_t	rec_ptr;
238 };
239 
240 #define	I2S_LOCK(sc)		mtx_lock(&(sc)->mtx)
241 #define	I2S_UNLOCK(sc)		mtx_unlock(&(sc)->mtx)
242 #define	I2S_READ(sc, reg)	bus_read_4((sc)->res[0], (reg))
243 #define	I2S_WRITE(sc, reg, val)	bus_write_4((sc)->res[0], (reg), (val))
244 #define	I2S_TYPE(sc)		((sc)->cfg->type)
245 
246 static int aw_i2s_probe(device_t dev);
247 static int aw_i2s_attach(device_t dev);
248 static int aw_i2s_detach(device_t dev);
249 
250 static u_int
251 sunxi_i2s_div_to_regval(const u_int *divmap, u_int divmaplen, u_int div)
252 {
253 	u_int n;
254 
255 	for (n = 0; n < divmaplen; n++)
256 		if (divmap[n] == div)
257 			return n;
258 
259 	return -1;
260 }
261 
262 static uint32_t sc_fmt[] = {
263 	SND_FORMAT(AFMT_S16_LE, 2, 0),
264 	0
265 };
266 static struct pcmchan_caps aw_i2s_caps = {AW_I2S_SAMPLE_RATE, AW_I2S_SAMPLE_RATE, sc_fmt, 0};
267 
268 
269 static int
270 aw_i2s_init(struct aw_i2s_softc *sc)
271 {
272 	uint32_t val;
273 	int error;
274 
275 	error = clk_enable(sc->clk);
276 	if (error != 0) {
277 		device_printf(sc->dev, "cannot enable mod clock\n");
278 		return (ENXIO);
279 	}
280 
281 	/* Reset */
282 	val = I2S_READ(sc, DA_CTL);
283 	val &= ~(DA_CTL_TXEN|DA_CTL_RXEN|DA_CTL_GEN);
284 	I2S_WRITE(sc, DA_CTL, val);
285 
286 	val = I2S_READ(sc, DA_FCTL);
287 	val &= ~(DA_FCTL_FTX|DA_FCTL_FRX);
288 	val &= ~(DA_FCTL_TXTL_MASK);
289 	val |= DA_FCTL_TXTL(FIFO_LEVEL);
290 	I2S_WRITE(sc, DA_FCTL, val);
291 
292 	I2S_WRITE(sc, DA_TXCNT, 0);
293 	I2S_WRITE(sc, DA_RXCNT, 0);
294 
295 	/* Enable */
296 	val = I2S_READ(sc, DA_CTL);
297 	val |= DA_CTL_GEN;
298 	I2S_WRITE(sc, DA_CTL, val);
299 	val |= DA_CTL_SDO_EN;
300 	I2S_WRITE(sc, DA_CTL, val);
301 
302 	/* Setup channels */
303 	I2S_WRITE(sc, sc->cfg->txchmap, 0x76543210);
304 	val = I2S_READ(sc, sc->cfg->txchsel);
305 	val &= ~DA_CHSEL_EN_MASK;
306 	val |= DA_CHSEL_EN(3);
307 	val &= ~DA_CHSEL_SEL_MASK;
308 	val |= DA_CHSEL_SEL(1);
309 	I2S_WRITE(sc, sc->cfg->txchsel, val);
310 	I2S_WRITE(sc, sc->cfg->rxchmap, 0x76543210);
311 	val = I2S_READ(sc, sc->cfg->rxchsel);
312 	val &= ~DA_CHSEL_EN_MASK;
313 	val |= DA_CHSEL_EN(3);
314 	val &= ~DA_CHSEL_SEL_MASK;
315 	val |= DA_CHSEL_SEL(1);
316 	I2S_WRITE(sc, sc->cfg->rxchsel, val);
317 
318 	if (I2S_TYPE(sc) == SUNXI_I2S_SUN8I) {
319 		val = I2S_READ(sc, DA_CHCFG);
320 		val &= ~DA_CHCFG_TX_SLOT_NUM_MASK;
321 		val |= DA_CHCFG_TX_SLOT_NUM(1);
322 		val &= ~DA_CHCFG_RX_SLOT_NUM_MASK;
323 		val |= DA_CHCFG_RX_SLOT_NUM(1);
324 		I2S_WRITE(sc, DA_CHCFG, val);
325 	}
326 
327 	return (0);
328 }
329 
330 static int
331 aw_i2s_probe(device_t dev)
332 {
333 	if (!ofw_bus_status_okay(dev))
334 		return (ENXIO);
335 
336 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
337 		return (ENXIO);
338 
339 	device_set_desc(dev, "Allwinner I2S");
340 	return (BUS_PROBE_DEFAULT);
341 }
342 
343 static int
344 aw_i2s_attach(device_t dev)
345 {
346 	struct aw_i2s_softc *sc;
347 	int error;
348 	phandle_t node;
349 	hwreset_t rst;
350 	clk_t clk;
351 
352 	sc = device_get_softc(dev);
353 	sc->dev = dev;
354 
355 	sc->cfg  = (void*)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
356 
357 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
358 
359 	if (bus_alloc_resources(dev, aw_i2s_spec, sc->res) != 0) {
360 		device_printf(dev, "cannot allocate resources for device\n");
361 		error = ENXIO;
362 		goto fail;
363 	}
364 
365 	error = clk_get_by_ofw_name(dev, 0, "mod", &sc->clk);
366 	if (error != 0) {
367 		device_printf(dev, "cannot get i2s_clk clock\n");
368 		goto fail;
369 	}
370 
371 	error = clk_get_by_ofw_name(dev, 0, "apb", &clk);
372 	if (error != 0) {
373 		device_printf(dev, "cannot get APB clock\n");
374 		goto fail;
375 	}
376 
377 	error = clk_enable(clk);
378 	if (error != 0) {
379 		device_printf(dev, "cannot enable APB clock\n");
380 		goto fail;
381 	}
382 
383 	if (hwreset_get_by_ofw_idx(dev, 0, 0, &rst) == 0) {
384 		error = hwreset_deassert(rst);
385 		if (error != 0) {
386 			device_printf(dev, "cannot de-assert reset\n");
387 			goto fail;
388 		}
389 	}
390 
391 	aw_i2s_init(sc);
392 
393 	node = ofw_bus_get_node(dev);
394 	OF_device_register_xref(OF_xref_from_node(node), dev);
395 
396 	return (0);
397 
398 fail:
399 	aw_i2s_detach(dev);
400 	return (error);
401 }
402 
403 static int
404 aw_i2s_detach(device_t dev)
405 {
406 	struct aw_i2s_softc *i2s;
407 
408 	i2s = device_get_softc(dev);
409 
410 	if (i2s->clk)
411 		clk_release(i2s->clk);
412 
413 	if (i2s->intrhand != NULL)
414 		bus_teardown_intr(i2s->dev, i2s->res[1], i2s->intrhand);
415 
416 	bus_release_resources(dev, aw_i2s_spec, i2s->res);
417 	mtx_destroy(&i2s->mtx);
418 
419 	return (0);
420 }
421 
422 static int
423 aw_i2s_dai_init(device_t dev, uint32_t format)
424 {
425 	struct aw_i2s_softc *sc;
426 	int fmt, pol, clk;
427 	uint32_t ctl, fat0, chsel;
428 	u_int offset;
429 
430 	sc = device_get_softc(dev);
431 
432 	fmt = AUDIO_DAI_FORMAT_FORMAT(format);
433 	pol = AUDIO_DAI_FORMAT_POLARITY(format);
434 	clk = AUDIO_DAI_FORMAT_CLOCK(format);
435 
436 	ctl = I2S_READ(sc, DA_CTL);
437 	fat0 = I2S_READ(sc, DA_FAT0);
438 
439 	if (I2S_TYPE(sc) == SUNXI_I2S_SUN4I) {
440 		fat0 &= ~DA_FAT0_FMT_MASK;
441 		switch (fmt) {
442 		case AUDIO_DAI_FORMAT_I2S:
443 			fat0 |= DA_FMT_I2S;
444 			break;
445 		case AUDIO_DAI_FORMAT_RJ:
446 			fat0 |= DA_FMT_RJ;
447 			break;
448 		case AUDIO_DAI_FORMAT_LJ:
449 			fat0 |= DA_FMT_LJ;
450 			break;
451 		default:
452 			return EINVAL;
453 		}
454 		ctl &= ~DA_CTL_PCM;
455 	} else {
456 		ctl &= ~DA_CTL_MODE_SEL_MASK;
457 		switch (fmt) {
458 		case AUDIO_DAI_FORMAT_I2S:
459 			ctl |= DA_CTL_MODE_SEL_LJ;
460 			offset = 1;
461 			break;
462 		case AUDIO_DAI_FORMAT_LJ:
463 			ctl |= DA_CTL_MODE_SEL_LJ;
464 			offset = 0;
465 			break;
466 		case AUDIO_DAI_FORMAT_RJ:
467 			ctl |= DA_CTL_MODE_SEL_RJ;
468 			offset = 0;
469 			break;
470 		case AUDIO_DAI_FORMAT_DSPA:
471 			ctl |= DA_CTL_MODE_SEL_PCM;
472 			offset = 1;
473 			break;
474 		case AUDIO_DAI_FORMAT_DSPB:
475 			ctl |= DA_CTL_MODE_SEL_PCM;
476 			offset = 0;
477 			break;
478 		default:
479 			return EINVAL;
480 		}
481 
482 		chsel = I2S_READ(sc, sc->cfg->txchsel);
483 		chsel &= ~DA_CHSEL_OFFSET_MASK;
484 		chsel |= DA_CHSEL_OFFSET(offset);
485 		I2S_WRITE(sc, sc->cfg->txchsel, chsel);
486 
487 		chsel = I2S_READ(sc, sc->cfg->rxchsel);
488 		chsel &= ~DA_CHSEL_OFFSET_MASK;
489 		chsel |= DA_CHSEL_OFFSET(offset);
490 		I2S_WRITE(sc, sc->cfg->rxchsel, chsel);
491 	}
492 
493 	fat0 &= ~(DA_FAT0_LRCP_MASK|DA_FAT0_BCP_MASK);
494 	if (I2S_TYPE(sc) == SUNXI_I2S_SUN4I) {
495 		if (AUDIO_DAI_POLARITY_INVERTED_BCLK(pol))
496 			fat0 |= DA_BCP_INVERTED;
497 		if (AUDIO_DAI_POLARITY_INVERTED_FRAME(pol))
498 			fat0 |= DA_LRCP_INVERTED;
499 	} else {
500 		if (AUDIO_DAI_POLARITY_INVERTED_BCLK(pol))
501 			fat0 |= DA_BCP_INVERTED;
502 		if (!AUDIO_DAI_POLARITY_INVERTED_FRAME(pol))
503 			fat0 |= DA_LRCP_INVERTED;
504 
505 		fat0 &= ~DA_FAT0_LRCK_PERIOD_MASK;
506 		fat0 |= DA_FAT0_LRCK_PERIOD(32 - 1);
507 	}
508 
509 	I2S_WRITE(sc, DA_CTL, ctl);
510 	I2S_WRITE(sc, DA_FAT0, fat0);
511 
512 	return (0);
513 }
514 
515 
516 static int
517 aw_i2s_dai_intr(device_t dev, struct snd_dbuf *play_buf, struct snd_dbuf *rec_buf)
518 {
519 	struct aw_i2s_softc *sc;
520 	int ret = 0;
521 	uint32_t val, status;
522 
523 	sc = device_get_softc(dev);
524 
525 	I2S_LOCK(sc);
526 
527 	status = I2S_READ(sc, DA_ISTA);
528 	/* Clear interrupts */
529 	// device_printf(sc->dev, "status: %08x\n", status);
530 	I2S_WRITE(sc, DA_ISTA, status);
531 
532 	if (status & DA_ISTA_TXEI_INT) {
533 		uint8_t *samples;
534 		uint32_t count, size, readyptr, written, empty;
535 
536 		val  = I2S_READ(sc, DA_FSTA);
537 		empty = DA_FSTA_TXE_CNT(val);
538 		count = sndbuf_getready(play_buf);
539 		size = sndbuf_getsize(play_buf);
540 		readyptr = sndbuf_getreadyptr(play_buf);
541 
542 		samples = (uint8_t*)sndbuf_getbuf(play_buf);
543 		written = 0;
544 		if (empty > count / 2)
545 			empty = count / 2;
546 		for (; empty > 0; empty--) {
547 			val = (samples[readyptr++ % size] << 16);
548 			val |= (samples[readyptr++ % size] << 24);
549 			written += 2;
550 			I2S_WRITE(sc, DA_TXFIFO, val);
551 		}
552 		sc->play_ptr += written;
553 		sc->play_ptr %= size;
554 		ret |= AUDIO_DAI_PLAY_INTR;
555 	}
556 
557 	if (status & DA_ISTA_RXAI_INT) {
558 		uint8_t *samples;
559 		uint32_t count, size, freeptr, recorded, available;
560 
561 		val  = I2S_READ(sc, DA_FSTA);
562 		available = DA_FSTA_RXA_CNT(val);
563 
564 		count = sndbuf_getfree(rec_buf);
565 		size = sndbuf_getsize(rec_buf);
566 		freeptr = sndbuf_getfreeptr(rec_buf);
567 		samples = (uint8_t*)sndbuf_getbuf(rec_buf);
568 		recorded = 0;
569 		if (available > count / 2)
570 			available = count / 2;
571 
572 		for (; available > 0; available--) {
573 			val = I2S_READ(sc, DA_RXFIFO);
574 			samples[freeptr++ % size] = (val >> 16) & 0xff;
575 			samples[freeptr++ % size] = (val >> 24) & 0xff;
576 			recorded += 2;
577 		}
578 		sc->rec_ptr += recorded;
579 		sc->rec_ptr %= size;
580 		ret |= AUDIO_DAI_REC_INTR;
581 	}
582 
583 	I2S_UNLOCK(sc);
584 
585 	return (ret);
586 }
587 
588 static struct pcmchan_caps *
589 aw_i2s_dai_get_caps(device_t dev)
590 {
591 	return (&aw_i2s_caps);
592 }
593 
594 static int
595 aw_i2s_dai_trigger(device_t dev, int go, int pcm_dir)
596 {
597 	struct aw_i2s_softc 	*sc = device_get_softc(dev);
598 	uint32_t val;
599 
600 	if ((pcm_dir != PCMDIR_PLAY) && (pcm_dir != PCMDIR_REC))
601 		return (EINVAL);
602 
603 	switch (go) {
604 	case PCMTRIG_START:
605 		if (pcm_dir == PCMDIR_PLAY) {
606 			/* Flush FIFO */
607 			val = I2S_READ(sc, DA_FCTL);
608 			I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FTX);
609 			I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FTX);
610 
611 			/* Reset TX sample counter */
612 			I2S_WRITE(sc, DA_TXCNT, 0);
613 
614 			/* Enable TX block */
615 			val = I2S_READ(sc, DA_CTL);
616 			I2S_WRITE(sc, DA_CTL, val | DA_CTL_TXEN);
617 
618 			/* Enable TX underrun interrupt */
619 			val = I2S_READ(sc, DA_INT);
620 			I2S_WRITE(sc, DA_INT, val | DA_INT_TXEI_EN);
621 		}
622 
623 		if (pcm_dir == PCMDIR_REC) {
624 			/* Flush FIFO */
625 			val = I2S_READ(sc, DA_FCTL);
626 			I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FRX);
627 			I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FRX);
628 
629 			/* Reset RX sample counter */
630 			I2S_WRITE(sc, DA_RXCNT, 0);
631 
632 			/* Enable RX block */
633 			val = I2S_READ(sc, DA_CTL);
634 			I2S_WRITE(sc, DA_CTL, val | DA_CTL_RXEN);
635 
636 			/* Enable RX data available interrupt */
637 			val = I2S_READ(sc, DA_INT);
638 			I2S_WRITE(sc, DA_INT, val | DA_INT_RXAI_EN);
639 		}
640 
641 		break;
642 
643 	case PCMTRIG_STOP:
644 	case PCMTRIG_ABORT:
645 		I2S_LOCK(sc);
646 
647 		if (pcm_dir == PCMDIR_PLAY) {
648 			/* Disable TX block */
649 			val = I2S_READ(sc, DA_CTL);
650 			I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_TXEN);
651 
652 			/* Enable TX underrun interrupt */
653 			val = I2S_READ(sc, DA_INT);
654 			I2S_WRITE(sc, DA_INT, val & ~DA_INT_TXEI_EN);
655 
656 			sc->play_ptr = 0;
657 		} else {
658 			/* Disable RX block */
659 			val = I2S_READ(sc, DA_CTL);
660 			I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_RXEN);
661 
662 			/* Disable RX data available interrupt */
663 			val = I2S_READ(sc, DA_INT);
664 			I2S_WRITE(sc, DA_INT, val & ~DA_INT_RXAI_EN);
665 
666 			sc->rec_ptr = 0;
667 		}
668 
669 		I2S_UNLOCK(sc);
670 		break;
671 	}
672 
673 	return (0);
674 }
675 
676 static uint32_t
677 aw_i2s_dai_get_ptr(device_t dev, int pcm_dir)
678 {
679 	struct aw_i2s_softc *sc;
680 	uint32_t ptr;
681 
682 	sc = device_get_softc(dev);
683 
684 	I2S_LOCK(sc);
685 	if (pcm_dir == PCMDIR_PLAY)
686 		ptr = sc->play_ptr;
687 	else
688 		ptr = sc->rec_ptr;
689 	I2S_UNLOCK(sc);
690 
691 	return ptr;
692 }
693 
694 static int
695 aw_i2s_dai_setup_intr(device_t dev, driver_intr_t intr_handler, void *intr_arg)
696 {
697 	struct aw_i2s_softc 	*sc = device_get_softc(dev);
698 
699 	if (bus_setup_intr(dev, sc->res[1],
700 	    INTR_TYPE_MISC | INTR_MPSAFE, NULL, intr_handler, intr_arg,
701 	    &sc->intrhand)) {
702 		device_printf(dev, "cannot setup interrupt handler\n");
703 		return (ENXIO);
704 	}
705 
706 	return (0);
707 }
708 
709 static uint32_t
710 aw_i2s_dai_set_chanformat(device_t dev, uint32_t format)
711 {
712 
713 	return (0);
714 }
715 
716 static int
717 aw_i2s_dai_set_sysclk(device_t dev, unsigned int rate, int dai_dir)
718 {
719 	struct aw_i2s_softc *sc;
720 	int bclk_val, mclk_val;
721 	uint32_t val;
722 	int error;
723 
724 	sc = device_get_softc(dev);
725 
726 	error = clk_set_freq(sc->clk, AW_I2S_CLK_RATE, CLK_SET_ROUND_DOWN);
727 	if (error != 0) {
728 		device_printf(sc->dev,
729 		    "couldn't set mod clock rate to %u Hz: %d\n", AW_I2S_CLK_RATE, error);
730 		return error;
731 	}
732 	error = clk_enable(sc->clk);
733 	if (error != 0) {
734 		device_printf(sc->dev,
735 		    "couldn't enable mod clock: %d\n", error);
736 		return error;
737 	}
738 
739 	const u_int bclk_prate = I2S_TYPE(sc) == SUNXI_I2S_SUN4I ? rate : AW_I2S_CLK_RATE;
740 
741 	const u_int bclk_div = bclk_prate / (2 * 32 * AW_I2S_SAMPLE_RATE);
742 	const u_int mclk_div = AW_I2S_CLK_RATE / rate;
743 
744 	if (I2S_TYPE(sc) == SUNXI_I2S_SUN4I) {
745 		bclk_val = sunxi_i2s_div_to_regval(sun4i_i2s_bclk_divmap,
746 		    nitems(sun4i_i2s_bclk_divmap), bclk_div);
747 		mclk_val = sunxi_i2s_div_to_regval(sun4i_i2s_mclk_divmap,
748 		    nitems(sun4i_i2s_mclk_divmap), mclk_div);
749 	} else {
750 		bclk_val = sunxi_i2s_div_to_regval(sun8i_i2s_divmap,
751 		    nitems(sun8i_i2s_divmap), bclk_div);
752 		mclk_val = sunxi_i2s_div_to_regval(sun8i_i2s_divmap,
753 		    nitems(sun8i_i2s_divmap), mclk_div);
754 	}
755 	if (bclk_val == -1 || mclk_val == -1) {
756 		device_printf(sc->dev, "couldn't configure bclk/mclk dividers\n");
757 		return EIO;
758 	}
759 
760 	val = I2S_READ(sc, DA_CLKD);
761 	if (I2S_TYPE(sc) == SUNXI_I2S_SUN4I) {
762 		val |= DA_CLKD_MCLKO_EN_SUN4I;
763 		val &= ~DA_CLKD_BCLKDIV_SUN4I_MASK;
764 		val |= DA_CLKD_BCLKDIV_SUN4I(bclk_val);
765 	} else {
766 		val |= DA_CLKD_MCLKO_EN_SUN8I;
767 		val &= ~DA_CLKD_BCLKDIV_SUN8I_MASK;
768 		val |= DA_CLKD_BCLKDIV_SUN8I(bclk_val);
769 	}
770 	val &= ~DA_CLKD_MCLKDIV_MASK;
771 	val |= DA_CLKD_MCLKDIV(mclk_val);
772 	I2S_WRITE(sc, DA_CLKD, val);
773 
774 
775 	return (0);
776 }
777 
778 static uint32_t
779 aw_i2s_dai_set_chanspeed(device_t dev, uint32_t speed)
780 {
781 
782 	return (speed);
783 }
784 
785 static device_method_t aw_i2s_methods[] = {
786 	/* Device interface */
787 	DEVMETHOD(device_probe,		aw_i2s_probe),
788 	DEVMETHOD(device_attach,	aw_i2s_attach),
789 	DEVMETHOD(device_detach,	aw_i2s_detach),
790 
791 	DEVMETHOD(audio_dai_init,	aw_i2s_dai_init),
792 	DEVMETHOD(audio_dai_setup_intr,	aw_i2s_dai_setup_intr),
793 	DEVMETHOD(audio_dai_set_sysclk,	aw_i2s_dai_set_sysclk),
794 	DEVMETHOD(audio_dai_set_chanspeed,	aw_i2s_dai_set_chanspeed),
795 	DEVMETHOD(audio_dai_set_chanformat,	aw_i2s_dai_set_chanformat),
796 	DEVMETHOD(audio_dai_intr,	aw_i2s_dai_intr),
797 	DEVMETHOD(audio_dai_get_caps,	aw_i2s_dai_get_caps),
798 	DEVMETHOD(audio_dai_trigger,	aw_i2s_dai_trigger),
799 	DEVMETHOD(audio_dai_get_ptr,	aw_i2s_dai_get_ptr),
800 
801 	DEVMETHOD_END
802 };
803 
804 static driver_t aw_i2s_driver = {
805 	"i2s",
806 	aw_i2s_methods,
807 	sizeof(struct aw_i2s_softc),
808 };
809 
810 static devclass_t aw_i2s_devclass;
811 
812 DRIVER_MODULE(aw_i2s, simplebus, aw_i2s_driver, aw_i2s_devclass, 0, 0);
813 SIMPLEBUS_PNP_INFO(compat_data);
814