xref: /netbsd-src/sys/arch/arm/sunxi/sunxi_i2s.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: sunxi_i2s.c,v 1.12 2021/01/27 03:10:20 thorpej Exp $ */
244ca330bSjmcneill 
344ca330bSjmcneill /*-
444ca330bSjmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
544ca330bSjmcneill  * All rights reserved.
644ca330bSjmcneill  *
744ca330bSjmcneill  * Redistribution and use in source and binary forms, with or without
844ca330bSjmcneill  * modification, are permitted provided that the following conditions
944ca330bSjmcneill  * are met:
1044ca330bSjmcneill  * 1. Redistributions of source code must retain the above copyright
1144ca330bSjmcneill  *    notice, this list of conditions and the following disclaimer.
1244ca330bSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
1344ca330bSjmcneill  *    notice, this list of conditions and the following disclaimer in the
1444ca330bSjmcneill  *    documentation and/or other materials provided with the distribution.
1544ca330bSjmcneill  *
1644ca330bSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1744ca330bSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1844ca330bSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1944ca330bSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2044ca330bSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2144ca330bSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2244ca330bSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2344ca330bSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2444ca330bSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2544ca330bSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2644ca330bSjmcneill  * SUCH DAMAGE.
2744ca330bSjmcneill  */
2844ca330bSjmcneill 
2944ca330bSjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_i2s.c,v 1.12 2021/01/27 03:10:20 thorpej Exp $");
3144ca330bSjmcneill 
3244ca330bSjmcneill #include <sys/param.h>
3344ca330bSjmcneill #include <sys/bus.h>
3444ca330bSjmcneill #include <sys/cpu.h>
3544ca330bSjmcneill #include <sys/device.h>
3644ca330bSjmcneill #include <sys/kmem.h>
3744ca330bSjmcneill #include <sys/gpio.h>
3844ca330bSjmcneill 
3944ca330bSjmcneill #include <sys/audioio.h>
40e622eac4Sisaki #include <dev/audio/audio_if.h>
41e622eac4Sisaki #include <dev/audio/linear.h>
4244ca330bSjmcneill 
4344ca330bSjmcneill #include <dev/fdt/fdtvar.h>
4444ca330bSjmcneill 
4544ca330bSjmcneill #define	SUNXI_I2S_CLK_RATE		24576000
4650c8e8bfSjmcneill #define	SUNXI_I2S_SAMPLE_RATE		48000
4744ca330bSjmcneill 
4844ca330bSjmcneill #define	DA_CTL		0x00
4950c8e8bfSjmcneill #define	 DA_CTL_BCLK_OUT __BIT(18)	/* sun8i */
5050c8e8bfSjmcneill #define	 DA_CLK_LRCK_OUT __BIT(17)	/* sun8i */
5144ca330bSjmcneill #define	 DA_CTL_SDO_EN	__BIT(8)
5250c8e8bfSjmcneill #define	 DA_CTL_MS	__BIT(5)	/* sun4i */
5350c8e8bfSjmcneill #define	 DA_CTL_PCM	__BIT(4)	/* sun4i */
5450c8e8bfSjmcneill #define	 DA_CTL_MODE_SEL __BITS(5,4)	/* sun8i */
5550c8e8bfSjmcneill #define	  DA_CTL_MODE_SEL_PCM	0
5650c8e8bfSjmcneill #define	  DA_CTL_MODE_SEL_LJ	1
5750c8e8bfSjmcneill #define	  DA_CTL_MODE_SEL_RJ	2
5844ca330bSjmcneill #define	 DA_CTL_TXEN	__BIT(2)
5944ca330bSjmcneill #define	 DA_CTL_RXEN	__BIT(1)
6044ca330bSjmcneill #define	 DA_CTL_GEN	__BIT(0)
6144ca330bSjmcneill #define	DA_FAT0		0x04
6250c8e8bfSjmcneill #define	 DA_FAT0_LRCK_PERIOD __BITS(17,8) /* sun8i */
6344ca330bSjmcneill #define	 DA_FAT0_LRCP	__BIT(7)
6444ca330bSjmcneill #define	  DA_LRCP_NORMAL	0
6544ca330bSjmcneill #define	  DA_LRCP_INVERTED	1
6644ca330bSjmcneill #define	 DA_FAT0_BCP	__BIT(6)
6744ca330bSjmcneill #define	  DA_BCP_NORMAL		0
6844ca330bSjmcneill #define	  DA_BCP_INVERTED	1
6944ca330bSjmcneill #define	 DA_FAT0_SR	__BITS(5,4)
7044ca330bSjmcneill #define	 DA_FAT0_WSS	__BITS(3,2)
7144ca330bSjmcneill #define	 DA_FAT0_FMT	__BITS(1,0)
7244ca330bSjmcneill #define	  DA_FMT_I2S	0
7344ca330bSjmcneill #define	  DA_FMT_LJ	1
7444ca330bSjmcneill #define	  DA_FMT_RJ	2
7544ca330bSjmcneill #define	DA_FAT1		0x08
7644ca330bSjmcneill #define	DA_ISTA		0x0c
7744ca330bSjmcneill #define	DA_RXFIFO	0x10
7844ca330bSjmcneill #define	DA_FCTL		0x14
7944ca330bSjmcneill #define	 DA_FCTL_HUB_EN	__BIT(31)
8044ca330bSjmcneill #define	 DA_FCTL_FTX	__BIT(25)
8144ca330bSjmcneill #define	 DA_FCTL_FRX	__BIT(24)
82716c8fbaSjmcneill #define	 DA_FCTL_TXIM	__BIT(2)
83716c8fbaSjmcneill #define	 DA_FCTL_RXIM	__BITS(1,0)
8444ca330bSjmcneill #define	DA_FSTA		0x18
8544ca330bSjmcneill #define	DA_INT		0x1c
8644ca330bSjmcneill #define	 DA_INT_TX_DRQ	__BIT(7)
8744ca330bSjmcneill #define	 DA_INT_RX_DRQ	__BIT(3)
8844ca330bSjmcneill #define	DA_TXFIFO	0x20
8944ca330bSjmcneill #define	DA_CLKD		0x24
9050c8e8bfSjmcneill #define	 DA_CLKD_MCLKO_EN_SUN8I __BIT(8)
9150c8e8bfSjmcneill #define	 DA_CLKD_MCLKO_EN_SUN4I __BIT(7)
9250c8e8bfSjmcneill #define	 DA_CLKD_BCLKDIV_SUN8I __BITS(7,4)
9350c8e8bfSjmcneill #define	 DA_CLKD_BCLKDIV_SUN4I __BITS(6,4)
94716c8fbaSjmcneill #define	  DA_CLKD_BCLKDIV_8	3
9544ca330bSjmcneill #define	  DA_CLKD_BCLKDIV_16	5
9644ca330bSjmcneill #define	 DA_CLKD_MCLKDIV __BITS(3,0)
9744ca330bSjmcneill #define	  DA_CLKD_MCLKDIV_1	0
9844ca330bSjmcneill #define	DA_TXCNT	0x28
9944ca330bSjmcneill #define	DA_RXCNT	0x2c
10050c8e8bfSjmcneill #define	DA_CHCFG	0x30		/* sun8i */
10150c8e8bfSjmcneill #define	 DA_CHCFG_TX_SLOT_HIZ	__BIT(9)
10250c8e8bfSjmcneill #define	 DA_CHCFG_TXN_STATE	__BIT(8)
10350c8e8bfSjmcneill #define	 DA_CHCFG_RX_SLOT_NUM	__BITS(6,4)
10450c8e8bfSjmcneill #define	 DA_CHCFG_TX_SLOT_NUM	__BITS(2,0)
10544ca330bSjmcneill 
10650c8e8bfSjmcneill #define	DA_CHSEL_OFFSET	__BITS(13,12)	/* sun8i */
10744ca330bSjmcneill #define	DA_CHSEL_EN	__BITS(11,4)
10844ca330bSjmcneill #define	DA_CHSEL_SEL	__BITS(2,0)
10944ca330bSjmcneill 
11050c8e8bfSjmcneill enum sunxi_i2s_type {
11150c8e8bfSjmcneill 	SUNXI_I2S_SUN4I,
11250c8e8bfSjmcneill 	SUNXI_I2S_SUN8I,
11350c8e8bfSjmcneill };
11450c8e8bfSjmcneill 
11544ca330bSjmcneill struct sunxi_i2s_config {
11644ca330bSjmcneill 	const char	*name;
11750c8e8bfSjmcneill 	enum sunxi_i2s_type type;
11844ca330bSjmcneill 	bus_size_t	txchsel;
11944ca330bSjmcneill 	bus_size_t	txchmap;
12044ca330bSjmcneill 	bus_size_t	rxchsel;
12144ca330bSjmcneill 	bus_size_t	rxchmap;
12244ca330bSjmcneill };
12344ca330bSjmcneill 
12444ca330bSjmcneill static const struct sunxi_i2s_config sun50i_a64_codec_config = {
12544ca330bSjmcneill 	.name = "Audio Codec (digital part)",
12650c8e8bfSjmcneill 	.type = SUNXI_I2S_SUN4I,
12744ca330bSjmcneill 	.txchsel = 0x30,
12844ca330bSjmcneill 	.txchmap = 0x34,
12944ca330bSjmcneill 	.rxchsel = 0x38,
13044ca330bSjmcneill 	.rxchmap = 0x3c,
13144ca330bSjmcneill };
13244ca330bSjmcneill 
13350c8e8bfSjmcneill static const struct sunxi_i2s_config sun8i_h3_config = {
13450c8e8bfSjmcneill 	.name = "I2S/PCM controller",
13550c8e8bfSjmcneill 	.type = SUNXI_I2S_SUN8I,
13650c8e8bfSjmcneill 	.txchsel = 0x34,
13750c8e8bfSjmcneill 	.txchmap = 0x44,
13850c8e8bfSjmcneill 	.rxchsel = 0x54,
13950c8e8bfSjmcneill 	.rxchmap = 0x58,
14050c8e8bfSjmcneill };
14150c8e8bfSjmcneill 
142646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
143646c0f59Sthorpej 	{ .compat = "allwinner,sun50i-a64-codec-i2s",
144646c0f59Sthorpej 	  .data = &sun50i_a64_codec_config },
145646c0f59Sthorpej 	{ .compat = "allwinner,sun8i-h3-i2s",
146646c0f59Sthorpej 	  .data = &sun8i_h3_config },
14744ca330bSjmcneill 
148ec189949Sthorpej 	DEVICE_COMPAT_EOL
14944ca330bSjmcneill };
15044ca330bSjmcneill 
15144ca330bSjmcneill struct sunxi_i2s_softc;
15244ca330bSjmcneill 
15344ca330bSjmcneill struct sunxi_i2s_chan {
15444ca330bSjmcneill 	struct sunxi_i2s_softc	*ch_sc;
15544ca330bSjmcneill 	u_int			ch_mode;
15644ca330bSjmcneill 
15744ca330bSjmcneill 	struct fdtbus_dma	*ch_dma;
15844ca330bSjmcneill 	struct fdtbus_dma_req	ch_req;
15944ca330bSjmcneill 
16044ca330bSjmcneill 	audio_params_t		ch_params;
16144ca330bSjmcneill 
16244ca330bSjmcneill 	bus_addr_t		ch_start_phys;
16344ca330bSjmcneill 	bus_addr_t		ch_end_phys;
16444ca330bSjmcneill 	bus_addr_t		ch_cur_phys;
16544ca330bSjmcneill 	int			ch_blksize;
16644ca330bSjmcneill 
16744ca330bSjmcneill 	void			(*ch_intr)(void *);
16844ca330bSjmcneill 	void			*ch_intrarg;
16944ca330bSjmcneill };
17044ca330bSjmcneill 
17144ca330bSjmcneill struct sunxi_i2s_dma {
17244ca330bSjmcneill 	LIST_ENTRY(sunxi_i2s_dma) dma_list;
17344ca330bSjmcneill 	bus_dmamap_t		dma_map;
17444ca330bSjmcneill 	void			*dma_addr;
17544ca330bSjmcneill 	size_t			dma_size;
17644ca330bSjmcneill 	bus_dma_segment_t	dma_segs[1];
17744ca330bSjmcneill 	int			dma_nsegs;
17844ca330bSjmcneill };
17944ca330bSjmcneill 
18044ca330bSjmcneill struct sunxi_i2s_softc {
18144ca330bSjmcneill 	device_t		sc_dev;
18244ca330bSjmcneill 	bus_space_tag_t		sc_bst;
18344ca330bSjmcneill 	bus_space_handle_t	sc_bsh;
18444ca330bSjmcneill 	bus_dma_tag_t		sc_dmat;
18544ca330bSjmcneill 	int			sc_phandle;
18644ca330bSjmcneill 	bus_addr_t		sc_baseaddr;
18750c8e8bfSjmcneill 	struct clk		*sc_clk;
18844ca330bSjmcneill 
189646c0f59Sthorpej 	const struct sunxi_i2s_config *sc_cfg;
19044ca330bSjmcneill 
19144ca330bSjmcneill 	LIST_HEAD(, sunxi_i2s_dma) sc_dmalist;
19244ca330bSjmcneill 
19344ca330bSjmcneill 	kmutex_t		sc_lock;
19444ca330bSjmcneill 	kmutex_t		sc_intr_lock;
19544ca330bSjmcneill 
19644ca330bSjmcneill 	struct audio_format	sc_format;
19744ca330bSjmcneill 
19844ca330bSjmcneill 	struct sunxi_i2s_chan	sc_pchan;
19944ca330bSjmcneill 	struct sunxi_i2s_chan	sc_rchan;
20044ca330bSjmcneill 
20144ca330bSjmcneill 	struct audio_dai_device	sc_dai;
20244ca330bSjmcneill };
20344ca330bSjmcneill 
20450c8e8bfSjmcneill #define	I2S_TYPE(sc)	((sc)->sc_cfg->type)
20550c8e8bfSjmcneill 
20644ca330bSjmcneill #define	I2S_READ(sc, reg)			\
20744ca330bSjmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
20844ca330bSjmcneill #define	I2S_WRITE(sc, reg, val)		\
20944ca330bSjmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
21044ca330bSjmcneill 
21150c8e8bfSjmcneill static const u_int sun4i_i2s_bclk_divmap[] = {
21250c8e8bfSjmcneill 	[0] = 2,
21350c8e8bfSjmcneill 	[1] = 4,
21450c8e8bfSjmcneill 	[2] = 6,
21550c8e8bfSjmcneill 	[3] = 8,
21650c8e8bfSjmcneill 	[4] = 12,
21750c8e8bfSjmcneill 	[5] = 16,
21850c8e8bfSjmcneill };
21950c8e8bfSjmcneill 
22050c8e8bfSjmcneill static const u_int sun4i_i2s_mclk_divmap[] = {
22150c8e8bfSjmcneill 	[0] = 1,
22250c8e8bfSjmcneill 	[1] = 2,
22350c8e8bfSjmcneill 	[2] = 4,
22450c8e8bfSjmcneill 	[3] = 6,
22550c8e8bfSjmcneill 	[4] = 8,
22650c8e8bfSjmcneill 	[5] = 12,
22750c8e8bfSjmcneill 	[6] = 16,
22850c8e8bfSjmcneill 	[7] = 24,
22950c8e8bfSjmcneill };
23050c8e8bfSjmcneill 
23150c8e8bfSjmcneill static const u_int sun8i_i2s_divmap[] = {
23250c8e8bfSjmcneill 	[1] = 1,
23350c8e8bfSjmcneill 	[2] = 2,
23450c8e8bfSjmcneill 	[3] = 4,
23550c8e8bfSjmcneill 	[4] = 6,
23650c8e8bfSjmcneill 	[5] = 8,
23750c8e8bfSjmcneill 	[6] = 12,
23850c8e8bfSjmcneill 	[7] = 16,
23950c8e8bfSjmcneill 	[8] = 24,
24050c8e8bfSjmcneill 	[9] = 32,
24150c8e8bfSjmcneill 	[10] = 48,
24250c8e8bfSjmcneill 	[11] = 64,
24350c8e8bfSjmcneill 	[12] = 96,
24450c8e8bfSjmcneill 	[13] = 128,
24550c8e8bfSjmcneill 	[14] = 176,
24650c8e8bfSjmcneill 	[15] = 192,
24750c8e8bfSjmcneill };
24850c8e8bfSjmcneill 
24950c8e8bfSjmcneill static u_int
sunxi_i2s_div_to_regval(const u_int * divmap,u_int divmaplen,u_int div)25050c8e8bfSjmcneill sunxi_i2s_div_to_regval(const u_int *divmap, u_int divmaplen, u_int div)
25150c8e8bfSjmcneill {
25250c8e8bfSjmcneill 	u_int n;
25350c8e8bfSjmcneill 
25450c8e8bfSjmcneill 	for (n = 0; n < divmaplen; n++)
25550c8e8bfSjmcneill 		if (divmap[n] == div)
25650c8e8bfSjmcneill 			return n;
25750c8e8bfSjmcneill 
25850c8e8bfSjmcneill 	return -1;
25950c8e8bfSjmcneill }
26050c8e8bfSjmcneill 
26144ca330bSjmcneill static int
sunxi_i2s_allocdma(struct sunxi_i2s_softc * sc,size_t size,size_t align,struct sunxi_i2s_dma * dma)26244ca330bSjmcneill sunxi_i2s_allocdma(struct sunxi_i2s_softc *sc, size_t size,
26344ca330bSjmcneill     size_t align, struct sunxi_i2s_dma *dma)
26444ca330bSjmcneill {
26544ca330bSjmcneill 	int error;
26644ca330bSjmcneill 
26744ca330bSjmcneill 	dma->dma_size = size;
26844ca330bSjmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, dma->dma_size, align, 0,
26944ca330bSjmcneill 	    dma->dma_segs, 1, &dma->dma_nsegs, BUS_DMA_WAITOK);
27044ca330bSjmcneill 	if (error)
27144ca330bSjmcneill 		return error;
27244ca330bSjmcneill 
27344ca330bSjmcneill 	error = bus_dmamem_map(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs,
27444ca330bSjmcneill 	    dma->dma_size, &dma->dma_addr, BUS_DMA_WAITOK | BUS_DMA_COHERENT);
27544ca330bSjmcneill 	if (error)
27644ca330bSjmcneill 		goto free;
27744ca330bSjmcneill 
27844ca330bSjmcneill 	error = bus_dmamap_create(sc->sc_dmat, dma->dma_size, dma->dma_nsegs,
27944ca330bSjmcneill 	    dma->dma_size, 0, BUS_DMA_WAITOK, &dma->dma_map);
28044ca330bSjmcneill 	if (error)
28144ca330bSjmcneill 		goto unmap;
28244ca330bSjmcneill 
28344ca330bSjmcneill 	error = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_addr,
28444ca330bSjmcneill 	    dma->dma_size, NULL, BUS_DMA_WAITOK);
28544ca330bSjmcneill 	if (error)
28644ca330bSjmcneill 		goto destroy;
28744ca330bSjmcneill 
28844ca330bSjmcneill 	return 0;
28944ca330bSjmcneill 
29044ca330bSjmcneill destroy:
29144ca330bSjmcneill 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
29244ca330bSjmcneill unmap:
29344ca330bSjmcneill 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
29444ca330bSjmcneill free:
29544ca330bSjmcneill 	bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
29644ca330bSjmcneill 
29744ca330bSjmcneill 	return error;
29844ca330bSjmcneill }
29944ca330bSjmcneill 
30044ca330bSjmcneill static void
sunxi_i2s_freedma(struct sunxi_i2s_softc * sc,struct sunxi_i2s_dma * dma)30144ca330bSjmcneill sunxi_i2s_freedma(struct sunxi_i2s_softc *sc, struct sunxi_i2s_dma *dma)
30244ca330bSjmcneill {
30344ca330bSjmcneill 	bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
30444ca330bSjmcneill 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
30544ca330bSjmcneill 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
30644ca330bSjmcneill 	bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
30744ca330bSjmcneill }
30844ca330bSjmcneill 
30944ca330bSjmcneill static int
sunxi_i2s_transfer(struct sunxi_i2s_chan * ch)31044ca330bSjmcneill sunxi_i2s_transfer(struct sunxi_i2s_chan *ch)
31144ca330bSjmcneill {
31244ca330bSjmcneill 	bus_dma_segment_t seg;
31344ca330bSjmcneill 
31444ca330bSjmcneill 	seg.ds_addr = ch->ch_cur_phys;
31544ca330bSjmcneill 	seg.ds_len = ch->ch_blksize;
31644ca330bSjmcneill 	ch->ch_req.dreq_segs = &seg;
31744ca330bSjmcneill 	ch->ch_req.dreq_nsegs = 1;
31844ca330bSjmcneill 
31944ca330bSjmcneill 	return fdtbus_dma_transfer(ch->ch_dma, &ch->ch_req);
32044ca330bSjmcneill }
32144ca330bSjmcneill 
32244ca330bSjmcneill static int
sunxi_i2s_query_format(void * priv,audio_format_query_t * afp)323e622eac4Sisaki sunxi_i2s_query_format(void *priv, audio_format_query_t *afp)
32444ca330bSjmcneill {
32544ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = priv;
32644ca330bSjmcneill 
327e622eac4Sisaki 	return audio_query_format(&sc->sc_format, 1, afp);
32844ca330bSjmcneill }
32944ca330bSjmcneill 
33044ca330bSjmcneill static int
sunxi_i2s_set_format(void * priv,int setmode,const audio_params_t * play,const audio_params_t * rec,audio_filter_reg_t * pfil,audio_filter_reg_t * rfil)331e622eac4Sisaki sunxi_i2s_set_format(void *priv, int setmode,
332e622eac4Sisaki     const audio_params_t *play, const audio_params_t *rec,
333e622eac4Sisaki     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
33444ca330bSjmcneill {
33544ca330bSjmcneill 
33644ca330bSjmcneill 	return 0;
33744ca330bSjmcneill }
33844ca330bSjmcneill 
33944ca330bSjmcneill static void *
sunxi_i2s_allocm(void * priv,int dir,size_t size)34044ca330bSjmcneill sunxi_i2s_allocm(void *priv, int dir, size_t size)
34144ca330bSjmcneill {
34244ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = priv;
34344ca330bSjmcneill 	struct sunxi_i2s_dma *dma;
34444ca330bSjmcneill 	int error;
34544ca330bSjmcneill 
34644ca330bSjmcneill 	dma = kmem_alloc(sizeof(*dma), KM_SLEEP);
34744ca330bSjmcneill 
34844ca330bSjmcneill 	error = sunxi_i2s_allocdma(sc, size, 16, dma);
34944ca330bSjmcneill 	if (error) {
35044ca330bSjmcneill 		kmem_free(dma, sizeof(*dma));
35144ca330bSjmcneill 		device_printf(sc->sc_dev, "couldn't allocate DMA memory (%d)\n",
35244ca330bSjmcneill 		    error);
35344ca330bSjmcneill 		return NULL;
35444ca330bSjmcneill 	}
35544ca330bSjmcneill 
35644ca330bSjmcneill 	LIST_INSERT_HEAD(&sc->sc_dmalist, dma, dma_list);
35744ca330bSjmcneill 
35844ca330bSjmcneill 	return dma->dma_addr;
35944ca330bSjmcneill }
36044ca330bSjmcneill 
36144ca330bSjmcneill static void
sunxi_i2s_freem(void * priv,void * addr,size_t size)36244ca330bSjmcneill sunxi_i2s_freem(void *priv, void *addr, size_t size)
36344ca330bSjmcneill {
36444ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = priv;
36544ca330bSjmcneill 	struct sunxi_i2s_dma *dma;
36644ca330bSjmcneill 
36744ca330bSjmcneill 	LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
36844ca330bSjmcneill 		if (dma->dma_addr == addr) {
36944ca330bSjmcneill 			sunxi_i2s_freedma(sc, dma);
37044ca330bSjmcneill 			LIST_REMOVE(dma, dma_list);
37144ca330bSjmcneill 			kmem_free(dma, sizeof(*dma));
37244ca330bSjmcneill 			break;
37344ca330bSjmcneill 		}
37444ca330bSjmcneill }
37544ca330bSjmcneill 
37644ca330bSjmcneill static int
sunxi_i2s_get_props(void * priv)37744ca330bSjmcneill sunxi_i2s_get_props(void *priv)
37844ca330bSjmcneill {
37950c8e8bfSjmcneill 	struct sunxi_i2s_softc * const sc = priv;
38050c8e8bfSjmcneill 	int props = 0;
381ede47d01Sisaki 
38250c8e8bfSjmcneill 	if (sc->sc_pchan.ch_dma != NULL)
38350c8e8bfSjmcneill 		props |= AUDIO_PROP_PLAYBACK;
38450c8e8bfSjmcneill 	if (sc->sc_rchan.ch_dma != NULL)
38550c8e8bfSjmcneill 		props |= AUDIO_PROP_CAPTURE;
38650c8e8bfSjmcneill 	if (sc->sc_pchan.ch_dma != NULL && sc->sc_rchan.ch_dma != NULL)
38750c8e8bfSjmcneill 		props |= AUDIO_PROP_FULLDUPLEX;
38850c8e8bfSjmcneill 
38950c8e8bfSjmcneill 	return props;
39044ca330bSjmcneill }
39144ca330bSjmcneill 
39244ca330bSjmcneill static int
sunxi_i2s_trigger_output(void * priv,void * start,void * end,int blksize,void (* intr)(void *),void * intrarg,const audio_params_t * params)39344ca330bSjmcneill sunxi_i2s_trigger_output(void *priv, void *start, void *end, int blksize,
39444ca330bSjmcneill     void (*intr)(void *), void *intrarg, const audio_params_t *params)
39544ca330bSjmcneill {
39644ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = priv;
39744ca330bSjmcneill 	struct sunxi_i2s_chan *ch = &sc->sc_pchan;
39844ca330bSjmcneill 	struct sunxi_i2s_dma *dma;
39944ca330bSjmcneill 	bus_addr_t pstart;
40044ca330bSjmcneill 	bus_size_t psize;
40144ca330bSjmcneill 	uint32_t val;
40244ca330bSjmcneill 	int error;
40344ca330bSjmcneill 
40450c8e8bfSjmcneill 	if (ch->ch_dma == NULL)
40550c8e8bfSjmcneill 		return EIO;
40650c8e8bfSjmcneill 
40744ca330bSjmcneill 	pstart = 0;
40844ca330bSjmcneill 	psize = (uintptr_t)end - (uintptr_t)start;
40944ca330bSjmcneill 
41044ca330bSjmcneill 	LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
41144ca330bSjmcneill 		if (dma->dma_addr == start) {
41244ca330bSjmcneill 			pstart = dma->dma_map->dm_segs[0].ds_addr;
41344ca330bSjmcneill 			break;
41444ca330bSjmcneill 		}
41544ca330bSjmcneill 	if (pstart == 0) {
41644ca330bSjmcneill 		device_printf(sc->sc_dev, "bad addr %p\n", start);
41744ca330bSjmcneill 		return EINVAL;
41844ca330bSjmcneill 	}
41944ca330bSjmcneill 
42044ca330bSjmcneill 	ch->ch_intr = intr;
42144ca330bSjmcneill 	ch->ch_intrarg = intrarg;
42244ca330bSjmcneill 	ch->ch_start_phys = ch->ch_cur_phys = pstart;
42344ca330bSjmcneill 	ch->ch_end_phys = pstart + psize;
42444ca330bSjmcneill 	ch->ch_blksize = blksize;
42544ca330bSjmcneill 
42644ca330bSjmcneill 	/* Flush FIFO */
42744ca330bSjmcneill 	val = I2S_READ(sc, DA_FCTL);
42844ca330bSjmcneill 	I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FTX);
42944ca330bSjmcneill 	I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FTX);
43044ca330bSjmcneill 
43144ca330bSjmcneill 	/* Reset TX sample counter */
43244ca330bSjmcneill 	I2S_WRITE(sc, DA_TXCNT, 0);
43344ca330bSjmcneill 
43444ca330bSjmcneill 	/* Enable transmitter block */
43544ca330bSjmcneill 	val = I2S_READ(sc, DA_CTL);
43644ca330bSjmcneill 	I2S_WRITE(sc, DA_CTL, val | DA_CTL_TXEN);
43744ca330bSjmcneill 
43844ca330bSjmcneill 	/* Enable TX DRQ */
43944ca330bSjmcneill 	val = I2S_READ(sc, DA_INT);
44044ca330bSjmcneill 	I2S_WRITE(sc, DA_INT, val | DA_INT_TX_DRQ);
44144ca330bSjmcneill 
44244ca330bSjmcneill 	/* Start DMA transfer */
44344ca330bSjmcneill 	error = sunxi_i2s_transfer(ch);
44444ca330bSjmcneill 	if (error != 0) {
44544ca330bSjmcneill 		aprint_error_dev(sc->sc_dev,
44644ca330bSjmcneill 		    "failed to start DMA transfer: %d\n", error);
44744ca330bSjmcneill 		return error;
44844ca330bSjmcneill 	}
44944ca330bSjmcneill 
45044ca330bSjmcneill 	return 0;
45144ca330bSjmcneill }
45244ca330bSjmcneill 
45344ca330bSjmcneill static int
sunxi_i2s_trigger_input(void * priv,void * start,void * end,int blksize,void (* intr)(void *),void * intrarg,const audio_params_t * params)45444ca330bSjmcneill sunxi_i2s_trigger_input(void *priv, void *start, void *end, int blksize,
45544ca330bSjmcneill     void (*intr)(void *), void *intrarg, const audio_params_t *params)
45644ca330bSjmcneill {
45744ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = priv;
45844ca330bSjmcneill 	struct sunxi_i2s_chan *ch = &sc->sc_rchan;
45944ca330bSjmcneill 	struct sunxi_i2s_dma *dma;
46044ca330bSjmcneill 	bus_addr_t pstart;
46144ca330bSjmcneill 	bus_size_t psize;
46244ca330bSjmcneill 	uint32_t val;
46344ca330bSjmcneill 	int error;
46444ca330bSjmcneill 
46550c8e8bfSjmcneill 	if (ch->ch_dma == NULL)
46650c8e8bfSjmcneill 		return EIO;
46750c8e8bfSjmcneill 
46844ca330bSjmcneill 	pstart = 0;
46944ca330bSjmcneill 	psize = (uintptr_t)end - (uintptr_t)start;
47044ca330bSjmcneill 
47144ca330bSjmcneill 	LIST_FOREACH(dma, &sc->sc_dmalist, dma_list)
47244ca330bSjmcneill 		if (dma->dma_addr == start) {
47344ca330bSjmcneill 			pstart = dma->dma_map->dm_segs[0].ds_addr;
47444ca330bSjmcneill 			break;
47544ca330bSjmcneill 		}
47644ca330bSjmcneill 	if (pstart == 0) {
47744ca330bSjmcneill 		device_printf(sc->sc_dev, "bad addr %p\n", start);
47844ca330bSjmcneill 		return EINVAL;
47944ca330bSjmcneill 	}
48044ca330bSjmcneill 
48144ca330bSjmcneill 	ch->ch_intr = intr;
48244ca330bSjmcneill 	ch->ch_intrarg = intrarg;
48344ca330bSjmcneill 	ch->ch_start_phys = ch->ch_cur_phys = pstart;
48444ca330bSjmcneill 	ch->ch_end_phys = pstart + psize;
48544ca330bSjmcneill 	ch->ch_blksize = blksize;
48644ca330bSjmcneill 
48744ca330bSjmcneill 	/* Flush FIFO */
48844ca330bSjmcneill 	val = I2S_READ(sc, DA_FCTL);
48944ca330bSjmcneill 	I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FRX);
49044ca330bSjmcneill 	I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FRX);
49144ca330bSjmcneill 
49244ca330bSjmcneill 	/* Reset RX sample counter */
49344ca330bSjmcneill 	I2S_WRITE(sc, DA_RXCNT, 0);
49444ca330bSjmcneill 
49544ca330bSjmcneill 	/* Enable receiver block */
49644ca330bSjmcneill 	val = I2S_READ(sc, DA_CTL);
49744ca330bSjmcneill 	I2S_WRITE(sc, DA_CTL, val | DA_CTL_RXEN);
49844ca330bSjmcneill 
49944ca330bSjmcneill 	/* Enable RX DRQ */
50044ca330bSjmcneill 	val = I2S_READ(sc, DA_INT);
50144ca330bSjmcneill 	I2S_WRITE(sc, DA_INT, val | DA_INT_RX_DRQ);
50244ca330bSjmcneill 
50344ca330bSjmcneill 	/* Start DMA transfer */
50444ca330bSjmcneill 	error = sunxi_i2s_transfer(ch);
50544ca330bSjmcneill 	if (error != 0) {
50644ca330bSjmcneill 		aprint_error_dev(sc->sc_dev,
50744ca330bSjmcneill 		    "failed to start DMA transfer: %d\n", error);
50844ca330bSjmcneill 		return error;
50944ca330bSjmcneill 	}
51044ca330bSjmcneill 
51144ca330bSjmcneill 	return 0;
51244ca330bSjmcneill }
51344ca330bSjmcneill 
51444ca330bSjmcneill static int
sunxi_i2s_halt_output(void * priv)51544ca330bSjmcneill sunxi_i2s_halt_output(void *priv)
51644ca330bSjmcneill {
51744ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = priv;
51844ca330bSjmcneill 	struct sunxi_i2s_chan *ch = &sc->sc_pchan;
51944ca330bSjmcneill 	uint32_t val;
52044ca330bSjmcneill 
52150c8e8bfSjmcneill 	if (ch->ch_dma == NULL)
52250c8e8bfSjmcneill 		return EIO;
52350c8e8bfSjmcneill 
52444ca330bSjmcneill 	/* Disable DMA channel */
52544ca330bSjmcneill 	fdtbus_dma_halt(ch->ch_dma);
52644ca330bSjmcneill 
52744ca330bSjmcneill 	/* Disable transmitter block */
52844ca330bSjmcneill 	val = I2S_READ(sc, DA_CTL);
52944ca330bSjmcneill 	I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_TXEN);
53044ca330bSjmcneill 
53144ca330bSjmcneill 	/* Disable TX DRQ */
53244ca330bSjmcneill 	val = I2S_READ(sc, DA_INT);
53344ca330bSjmcneill 	I2S_WRITE(sc, DA_INT, val & ~DA_INT_TX_DRQ);
53444ca330bSjmcneill 
53544ca330bSjmcneill 	ch->ch_intr = NULL;
53644ca330bSjmcneill 	ch->ch_intrarg = NULL;
53744ca330bSjmcneill 
53844ca330bSjmcneill 	return 0;
53944ca330bSjmcneill }
54044ca330bSjmcneill 
54144ca330bSjmcneill static int
sunxi_i2s_halt_input(void * priv)54244ca330bSjmcneill sunxi_i2s_halt_input(void *priv)
54344ca330bSjmcneill {
54444ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = priv;
54544ca330bSjmcneill 	struct sunxi_i2s_chan *ch = &sc->sc_rchan;
54644ca330bSjmcneill 	uint32_t val;
54744ca330bSjmcneill 
54850c8e8bfSjmcneill 	if (ch->ch_dma == NULL)
54950c8e8bfSjmcneill 		return EIO;
55050c8e8bfSjmcneill 
55144ca330bSjmcneill 	/* Disable DMA channel */
55244ca330bSjmcneill 	fdtbus_dma_halt(ch->ch_dma);
55344ca330bSjmcneill 
55444ca330bSjmcneill 	/* Disable receiver block */
55544ca330bSjmcneill 	val = I2S_READ(sc, DA_CTL);
55644ca330bSjmcneill 	I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_RXEN);
55744ca330bSjmcneill 
55844ca330bSjmcneill 	/* Disable RX DRQ */
55944ca330bSjmcneill 	val = I2S_READ(sc, DA_INT);
56044ca330bSjmcneill 	I2S_WRITE(sc, DA_INT, val & ~DA_INT_RX_DRQ);
56144ca330bSjmcneill 
56244ca330bSjmcneill 	return 0;
56344ca330bSjmcneill }
56444ca330bSjmcneill 
56544ca330bSjmcneill static void
sunxi_i2s_get_locks(void * priv,kmutex_t ** intr,kmutex_t ** thread)56644ca330bSjmcneill sunxi_i2s_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
56744ca330bSjmcneill {
56844ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = priv;
56944ca330bSjmcneill 
57044ca330bSjmcneill 	*intr = &sc->sc_intr_lock;
57144ca330bSjmcneill 	*thread = &sc->sc_lock;
57244ca330bSjmcneill }
57344ca330bSjmcneill 
57444ca330bSjmcneill static const struct audio_hw_if sunxi_i2s_hw_if = {
575e622eac4Sisaki 	.query_format = sunxi_i2s_query_format,
576e622eac4Sisaki 	.set_format = sunxi_i2s_set_format,
57744ca330bSjmcneill 	.allocm = sunxi_i2s_allocm,
57844ca330bSjmcneill 	.freem = sunxi_i2s_freem,
57944ca330bSjmcneill 	.get_props = sunxi_i2s_get_props,
58044ca330bSjmcneill 	.trigger_output = sunxi_i2s_trigger_output,
58144ca330bSjmcneill 	.trigger_input = sunxi_i2s_trigger_input,
58244ca330bSjmcneill 	.halt_output = sunxi_i2s_halt_output,
58344ca330bSjmcneill 	.halt_input = sunxi_i2s_halt_input,
58444ca330bSjmcneill 	.get_locks = sunxi_i2s_get_locks,
58544ca330bSjmcneill };
58644ca330bSjmcneill 
58744ca330bSjmcneill static void
sunxi_i2s_dmaintr(void * priv)58844ca330bSjmcneill sunxi_i2s_dmaintr(void *priv)
58944ca330bSjmcneill {
59044ca330bSjmcneill 	struct sunxi_i2s_chan * const ch = priv;
59144ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = ch->ch_sc;
59244ca330bSjmcneill 
59344ca330bSjmcneill 	mutex_enter(&sc->sc_intr_lock);
59444ca330bSjmcneill 	ch->ch_cur_phys += ch->ch_blksize;
59544ca330bSjmcneill 	if (ch->ch_cur_phys >= ch->ch_end_phys)
59644ca330bSjmcneill 		ch->ch_cur_phys = ch->ch_start_phys;
59744ca330bSjmcneill 
59844ca330bSjmcneill 	if (ch->ch_intr) {
59944ca330bSjmcneill 		ch->ch_intr(ch->ch_intrarg);
60044ca330bSjmcneill 		sunxi_i2s_transfer(ch);
60144ca330bSjmcneill 	}
60244ca330bSjmcneill 	mutex_exit(&sc->sc_intr_lock);
60344ca330bSjmcneill }
60444ca330bSjmcneill 
60544ca330bSjmcneill static int
sunxi_i2s_chan_init(struct sunxi_i2s_softc * sc,struct sunxi_i2s_chan * ch,u_int mode,const char * dmaname)60644ca330bSjmcneill sunxi_i2s_chan_init(struct sunxi_i2s_softc *sc,
60744ca330bSjmcneill     struct sunxi_i2s_chan *ch, u_int mode, const char *dmaname)
60844ca330bSjmcneill {
60944ca330bSjmcneill 	ch->ch_sc = sc;
61044ca330bSjmcneill 	ch->ch_mode = mode;
61144ca330bSjmcneill 	ch->ch_dma = fdtbus_dma_get(sc->sc_phandle, dmaname, sunxi_i2s_dmaintr, ch);
61250c8e8bfSjmcneill 	if (ch->ch_dma == NULL)
61344ca330bSjmcneill 		return ENXIO;
61444ca330bSjmcneill 
61544ca330bSjmcneill 	if (mode == AUMODE_PLAY) {
61644ca330bSjmcneill 		ch->ch_req.dreq_dir = FDT_DMA_WRITE;
61744ca330bSjmcneill 		ch->ch_req.dreq_dev_phys =
61844ca330bSjmcneill 		    sc->sc_baseaddr + DA_TXFIFO;
61944ca330bSjmcneill 	} else {
62044ca330bSjmcneill 		ch->ch_req.dreq_dir = FDT_DMA_READ;
62144ca330bSjmcneill 		ch->ch_req.dreq_dev_phys =
62244ca330bSjmcneill 		    sc->sc_baseaddr + DA_RXFIFO;
62344ca330bSjmcneill 	}
62450c8e8bfSjmcneill 	ch->ch_req.dreq_mem_opt.opt_bus_width = 16;
62544ca330bSjmcneill 	ch->ch_req.dreq_mem_opt.opt_burst_len = 8;
62650c8e8bfSjmcneill 	ch->ch_req.dreq_dev_opt.opt_bus_width = 16;
62744ca330bSjmcneill 	ch->ch_req.dreq_dev_opt.opt_burst_len = 8;
62844ca330bSjmcneill 
62944ca330bSjmcneill 	return 0;
63044ca330bSjmcneill }
63144ca330bSjmcneill 
63244ca330bSjmcneill static int
sunxi_i2s_dai_set_sysclk(audio_dai_tag_t dai,u_int rate,int dir)63344ca330bSjmcneill sunxi_i2s_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir)
63444ca330bSjmcneill {
63544ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = audio_dai_private(dai);
63650c8e8bfSjmcneill 	int bclk_val, mclk_val;
63744ca330bSjmcneill 	uint32_t val;
63850c8e8bfSjmcneill 	int error;
63944ca330bSjmcneill 
64050c8e8bfSjmcneill 	error = clk_set_rate(sc->sc_clk, SUNXI_I2S_CLK_RATE);
64150c8e8bfSjmcneill 	if (error != 0) {
64250c8e8bfSjmcneill 		aprint_error_dev(sc->sc_dev,
64350c8e8bfSjmcneill 		    "couldn't set mod clock rate to %u Hz: %d\n", SUNXI_I2S_CLK_RATE, error);
64450c8e8bfSjmcneill 		return error;
64550c8e8bfSjmcneill 	}
64650c8e8bfSjmcneill 	error = clk_enable(sc->sc_clk);
64750c8e8bfSjmcneill 	if (error != 0) {
64850c8e8bfSjmcneill 		aprint_error_dev(sc->sc_dev,
64950c8e8bfSjmcneill 		    "couldn't enable mod clock: %d\n", error);
65050c8e8bfSjmcneill 		return error;
65150c8e8bfSjmcneill 	}
65244ca330bSjmcneill 
65350c8e8bfSjmcneill 	const u_int bclk_prate = I2S_TYPE(sc) == SUNXI_I2S_SUN4I ? rate : SUNXI_I2S_CLK_RATE;
65444ca330bSjmcneill 
65550c8e8bfSjmcneill 	const u_int bclk_div = bclk_prate / (2 * 32 * SUNXI_I2S_SAMPLE_RATE);
65650c8e8bfSjmcneill 	const u_int mclk_div = SUNXI_I2S_CLK_RATE / rate;
65750c8e8bfSjmcneill 
65850c8e8bfSjmcneill 	if (I2S_TYPE(sc) == SUNXI_I2S_SUN4I) {
65950c8e8bfSjmcneill 		bclk_val = sunxi_i2s_div_to_regval(sun4i_i2s_bclk_divmap,
66050c8e8bfSjmcneill 		    __arraycount(sun4i_i2s_bclk_divmap), bclk_div);
66150c8e8bfSjmcneill 		mclk_val = sunxi_i2s_div_to_regval(sun4i_i2s_mclk_divmap,
66250c8e8bfSjmcneill 		    __arraycount(sun4i_i2s_mclk_divmap), mclk_div);
66350c8e8bfSjmcneill 	} else {
66450c8e8bfSjmcneill 		bclk_val = sunxi_i2s_div_to_regval(sun8i_i2s_divmap,
66550c8e8bfSjmcneill 		    __arraycount(sun8i_i2s_divmap), bclk_div);
66650c8e8bfSjmcneill 		mclk_val = sunxi_i2s_div_to_regval(sun8i_i2s_divmap,
66750c8e8bfSjmcneill 		    __arraycount(sun8i_i2s_divmap), mclk_div);
66850c8e8bfSjmcneill 	}
66950c8e8bfSjmcneill 	if (bclk_val == -1 || mclk_val == -1) {
67050c8e8bfSjmcneill 		aprint_error_dev(sc->sc_dev, "couldn't configure bclk/mclk dividers\n");
67150c8e8bfSjmcneill 		return EIO;
67250c8e8bfSjmcneill 	}
67350c8e8bfSjmcneill 
67450c8e8bfSjmcneill 	val = I2S_READ(sc, DA_CLKD);
67550c8e8bfSjmcneill 	if (I2S_TYPE(sc) == SUNXI_I2S_SUN4I) {
67650c8e8bfSjmcneill 		val |= DA_CLKD_MCLKO_EN_SUN4I;
67750c8e8bfSjmcneill 		val &= ~DA_CLKD_BCLKDIV_SUN4I;
67850c8e8bfSjmcneill 		val |= __SHIFTIN(bclk_val, DA_CLKD_BCLKDIV_SUN4I);
67950c8e8bfSjmcneill 	} else {
68050c8e8bfSjmcneill 		val |= DA_CLKD_MCLKO_EN_SUN8I;
68150c8e8bfSjmcneill 		val &= ~DA_CLKD_BCLKDIV_SUN8I;
68250c8e8bfSjmcneill 		val |= __SHIFTIN(bclk_val, DA_CLKD_BCLKDIV_SUN8I);
68350c8e8bfSjmcneill 	}
68450c8e8bfSjmcneill 	val &= ~DA_CLKD_MCLKDIV;
68550c8e8bfSjmcneill 	val |= __SHIFTIN(mclk_val, DA_CLKD_MCLKDIV);
68644ca330bSjmcneill 	I2S_WRITE(sc, DA_CLKD, val);
68744ca330bSjmcneill 
68844ca330bSjmcneill 	return 0;
68944ca330bSjmcneill }
69044ca330bSjmcneill 
69144ca330bSjmcneill static int
sunxi_i2s_dai_set_format(audio_dai_tag_t dai,u_int format)69244ca330bSjmcneill sunxi_i2s_dai_set_format(audio_dai_tag_t dai, u_int format)
69344ca330bSjmcneill {
69444ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = audio_dai_private(dai);
69550c8e8bfSjmcneill 	uint32_t ctl, fat0, chsel;
69650c8e8bfSjmcneill 	u_int offset;
69744ca330bSjmcneill 
69844ca330bSjmcneill 	const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK);
69944ca330bSjmcneill 	const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK);
70044ca330bSjmcneill 	const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK);
70144ca330bSjmcneill 
70244ca330bSjmcneill 	ctl = I2S_READ(sc, DA_CTL);
70344ca330bSjmcneill 	fat0 = I2S_READ(sc, DA_FAT0);
70444ca330bSjmcneill 
70550c8e8bfSjmcneill 	if (I2S_TYPE(sc) == SUNXI_I2S_SUN4I) {
70644ca330bSjmcneill 		fat0 &= ~DA_FAT0_FMT;
70744ca330bSjmcneill 		switch (fmt) {
70844ca330bSjmcneill 		case AUDIO_DAI_FORMAT_I2S:
70944ca330bSjmcneill 			fat0 |= __SHIFTIN(DA_FMT_I2S, DA_FAT0_FMT);
71044ca330bSjmcneill 			break;
71144ca330bSjmcneill 		case AUDIO_DAI_FORMAT_RJ:
71244ca330bSjmcneill 			fat0 |= __SHIFTIN(DA_FMT_RJ, DA_FAT0_FMT);
71344ca330bSjmcneill 			break;
71444ca330bSjmcneill 		case AUDIO_DAI_FORMAT_LJ:
71544ca330bSjmcneill 			fat0 |= __SHIFTIN(DA_FMT_LJ, DA_FAT0_FMT);
71644ca330bSjmcneill 			break;
71744ca330bSjmcneill 		default:
71844ca330bSjmcneill 			return EINVAL;
71944ca330bSjmcneill 		}
72050c8e8bfSjmcneill 		ctl &= ~DA_CTL_PCM;
72150c8e8bfSjmcneill 	} else {
72250c8e8bfSjmcneill 		ctl &= ~DA_CTL_MODE_SEL;
72350c8e8bfSjmcneill 		switch (fmt) {
72450c8e8bfSjmcneill 		case AUDIO_DAI_FORMAT_I2S:
72550c8e8bfSjmcneill 			ctl |= __SHIFTIN(DA_CTL_MODE_SEL_LJ, DA_CTL_MODE_SEL);
72650c8e8bfSjmcneill 			offset = 1;
72744ca330bSjmcneill 			break;
72850c8e8bfSjmcneill 		case AUDIO_DAI_FORMAT_LJ:
72950c8e8bfSjmcneill 			ctl |= __SHIFTIN(DA_CTL_MODE_SEL_LJ, DA_CTL_MODE_SEL);
73050c8e8bfSjmcneill 			offset = 0;
73150c8e8bfSjmcneill 			break;
73250c8e8bfSjmcneill 		case AUDIO_DAI_FORMAT_RJ:
73350c8e8bfSjmcneill 			ctl |= __SHIFTIN(DA_CTL_MODE_SEL_RJ, DA_CTL_MODE_SEL);
73450c8e8bfSjmcneill 			offset = 0;
73550c8e8bfSjmcneill 			break;
73650c8e8bfSjmcneill 		case AUDIO_DAI_FORMAT_DSPA:
73750c8e8bfSjmcneill 			ctl |= __SHIFTIN(DA_CTL_MODE_SEL_PCM, DA_CTL_MODE_SEL);
73850c8e8bfSjmcneill 			offset = 1;
73950c8e8bfSjmcneill 			break;
74050c8e8bfSjmcneill 		case AUDIO_DAI_FORMAT_DSPB:
74150c8e8bfSjmcneill 			ctl |= __SHIFTIN(DA_CTL_MODE_SEL_PCM, DA_CTL_MODE_SEL);
74250c8e8bfSjmcneill 			offset = 0;
74344ca330bSjmcneill 			break;
74444ca330bSjmcneill 		default:
74544ca330bSjmcneill 			return EINVAL;
74644ca330bSjmcneill 		}
74744ca330bSjmcneill 
74850c8e8bfSjmcneill 		chsel = I2S_READ(sc, sc->sc_cfg->txchsel);
74950c8e8bfSjmcneill 		chsel &= ~DA_CHSEL_OFFSET;
75050c8e8bfSjmcneill 		chsel |= __SHIFTIN(offset, DA_CHSEL_OFFSET);
75150c8e8bfSjmcneill 		I2S_WRITE(sc, sc->sc_cfg->txchsel, chsel);
75250c8e8bfSjmcneill 
75350c8e8bfSjmcneill 		chsel = I2S_READ(sc, sc->sc_cfg->rxchsel);
75450c8e8bfSjmcneill 		chsel &= ~DA_CHSEL_OFFSET;
75550c8e8bfSjmcneill 		chsel |= __SHIFTIN(offset, DA_CHSEL_OFFSET);
75650c8e8bfSjmcneill 		I2S_WRITE(sc, sc->sc_cfg->rxchsel, chsel);
75750c8e8bfSjmcneill 	}
75850c8e8bfSjmcneill 
75950c8e8bfSjmcneill 	fat0 &= ~(DA_FAT0_LRCP|DA_FAT0_BCP);
76050c8e8bfSjmcneill 	if (I2S_TYPE(sc) == SUNXI_I2S_SUN4I) {
76150c8e8bfSjmcneill 		if (AUDIO_DAI_POLARITY_B(pol))
76250c8e8bfSjmcneill 			fat0 |= __SHIFTIN(DA_BCP_INVERTED, DA_FAT0_BCP);
76350c8e8bfSjmcneill 		if (AUDIO_DAI_POLARITY_F(pol))
76450c8e8bfSjmcneill 			fat0 |= __SHIFTIN(DA_LRCP_INVERTED, DA_FAT0_LRCP);
76550c8e8bfSjmcneill 	} else {
76650c8e8bfSjmcneill 		if (AUDIO_DAI_POLARITY_B(pol))
76750c8e8bfSjmcneill 			fat0 |= __SHIFTIN(DA_BCP_INVERTED, DA_FAT0_BCP);
76850c8e8bfSjmcneill 		if (!AUDIO_DAI_POLARITY_F(pol))
76950c8e8bfSjmcneill 			fat0 |= __SHIFTIN(DA_LRCP_INVERTED, DA_FAT0_LRCP);
77050c8e8bfSjmcneill 
77150c8e8bfSjmcneill 		fat0 &= ~DA_FAT0_LRCK_PERIOD;
77250c8e8bfSjmcneill 		fat0 |= __SHIFTIN(32 - 1, DA_FAT0_LRCK_PERIOD);
77350c8e8bfSjmcneill 	}
77450c8e8bfSjmcneill 
77550c8e8bfSjmcneill 	switch (clk) {
77650c8e8bfSjmcneill 	case AUDIO_DAI_CLOCK_CBM_CFM:
77750c8e8bfSjmcneill 		if (I2S_TYPE(sc) == SUNXI_I2S_SUN4I) {
77850c8e8bfSjmcneill 			ctl |= DA_CTL_MS;	/* codec is master */
77950c8e8bfSjmcneill 		} else {
78050c8e8bfSjmcneill 			ctl &= ~DA_CTL_BCLK_OUT;
78150c8e8bfSjmcneill 			ctl &= ~DA_CLK_LRCK_OUT;
78250c8e8bfSjmcneill 		}
78350c8e8bfSjmcneill 		break;
78450c8e8bfSjmcneill 	case AUDIO_DAI_CLOCK_CBS_CFS:
78550c8e8bfSjmcneill 		if (I2S_TYPE(sc) == SUNXI_I2S_SUN4I) {
78650c8e8bfSjmcneill 			ctl &= ~DA_CTL_MS;	/* codec is slave */
78750c8e8bfSjmcneill 		} else {
78850c8e8bfSjmcneill 			ctl |= DA_CTL_BCLK_OUT;
78950c8e8bfSjmcneill 			ctl |= DA_CLK_LRCK_OUT;
79050c8e8bfSjmcneill 		}
79150c8e8bfSjmcneill 		break;
79250c8e8bfSjmcneill 	default:
79350c8e8bfSjmcneill 		return EINVAL;
79450c8e8bfSjmcneill 	}
79544ca330bSjmcneill 
79644ca330bSjmcneill 	I2S_WRITE(sc, DA_CTL, ctl);
79744ca330bSjmcneill 	I2S_WRITE(sc, DA_FAT0, fat0);
79844ca330bSjmcneill 
79944ca330bSjmcneill 	return 0;
80044ca330bSjmcneill }
80144ca330bSjmcneill 
80244ca330bSjmcneill static audio_dai_tag_t
sunxi_i2s_dai_get_tag(device_t dev,const void * data,size_t len)80344ca330bSjmcneill sunxi_i2s_dai_get_tag(device_t dev, const void *data, size_t len)
80444ca330bSjmcneill {
80544ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = device_private(dev);
80644ca330bSjmcneill 
80744ca330bSjmcneill 	if (len != 4)
80844ca330bSjmcneill 		return NULL;
80944ca330bSjmcneill 
81044ca330bSjmcneill 	return &sc->sc_dai;
81144ca330bSjmcneill }
81244ca330bSjmcneill 
81344ca330bSjmcneill static struct fdtbus_dai_controller_func sunxi_i2s_dai_funcs = {
81444ca330bSjmcneill 	.get_tag = sunxi_i2s_dai_get_tag
81544ca330bSjmcneill };
81644ca330bSjmcneill 
81744ca330bSjmcneill static int
sunxi_i2s_clock_init(struct sunxi_i2s_softc * sc)81850c8e8bfSjmcneill sunxi_i2s_clock_init(struct sunxi_i2s_softc *sc)
81944ca330bSjmcneill {
82050c8e8bfSjmcneill 	const int phandle = sc->sc_phandle;
82144ca330bSjmcneill 	struct fdtbus_reset *rst;
82244ca330bSjmcneill 	struct clk *clk;
82344ca330bSjmcneill 	int error;
82444ca330bSjmcneill 
82550c8e8bfSjmcneill 	sc->sc_clk = fdtbus_clock_get(phandle, "mod");
82650c8e8bfSjmcneill 	if (sc->sc_clk == NULL) {
82744ca330bSjmcneill 		aprint_error(": couldn't find mod clock\n");
82844ca330bSjmcneill 		return ENXIO;
82944ca330bSjmcneill 	}
83044ca330bSjmcneill 
83144ca330bSjmcneill 	/* Enable APB clock */
83244ca330bSjmcneill 	clk = fdtbus_clock_get(phandle, "apb");
83344ca330bSjmcneill 	if (clk == NULL) {
83444ca330bSjmcneill 		aprint_error(": couldn't find apb clock\n");
83544ca330bSjmcneill 		return ENXIO;
83644ca330bSjmcneill 	}
83744ca330bSjmcneill 	error = clk_enable(clk);
83844ca330bSjmcneill 	if (error != 0) {
83944ca330bSjmcneill 		aprint_error(": couldn't enable apb clock: %d\n", error);
84044ca330bSjmcneill 		return error;
84144ca330bSjmcneill 	}
84244ca330bSjmcneill 
84344ca330bSjmcneill 	/* De-assert reset */
84450c8e8bfSjmcneill 	rst = fdtbus_reset_get_index(phandle, 0);
84544ca330bSjmcneill 	if (rst == NULL) {
84644ca330bSjmcneill 		aprint_error(": couldn't find reset\n");
84744ca330bSjmcneill 		return ENXIO;
84844ca330bSjmcneill 	}
84944ca330bSjmcneill 	error = fdtbus_reset_deassert(rst);
85044ca330bSjmcneill 	if (error != 0) {
85144ca330bSjmcneill 		aprint_error(": couldn't de-assert reset: %d\n", error);
85244ca330bSjmcneill 		return error;
85344ca330bSjmcneill 	}
85444ca330bSjmcneill 
85544ca330bSjmcneill 	return 0;
85644ca330bSjmcneill }
85744ca330bSjmcneill 
85844ca330bSjmcneill static int
sunxi_i2s_match(device_t parent,cfdata_t cf,void * aux)85944ca330bSjmcneill sunxi_i2s_match(device_t parent, cfdata_t cf, void *aux)
86044ca330bSjmcneill {
86144ca330bSjmcneill 	struct fdt_attach_args * const faa = aux;
86244ca330bSjmcneill 
863*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
86444ca330bSjmcneill }
86544ca330bSjmcneill 
86644ca330bSjmcneill static void
sunxi_i2s_attach(device_t parent,device_t self,void * aux)86744ca330bSjmcneill sunxi_i2s_attach(device_t parent, device_t self, void *aux)
86844ca330bSjmcneill {
86944ca330bSjmcneill 	struct sunxi_i2s_softc * const sc = device_private(self);
87044ca330bSjmcneill 	struct fdt_attach_args * const faa = aux;
87144ca330bSjmcneill 	const int phandle = faa->faa_phandle;
87244ca330bSjmcneill 	bus_addr_t addr;
87344ca330bSjmcneill 	bus_size_t size;
87444ca330bSjmcneill 	uint32_t val;
87544ca330bSjmcneill 
87644ca330bSjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
87744ca330bSjmcneill 		aprint_error(": couldn't get registers\n");
87844ca330bSjmcneill 		return;
87944ca330bSjmcneill 	}
88044ca330bSjmcneill 
88144ca330bSjmcneill 	sc->sc_dev = self;
88244ca330bSjmcneill 	sc->sc_phandle = phandle;
88344ca330bSjmcneill 	sc->sc_baseaddr = addr;
88444ca330bSjmcneill 	sc->sc_bst = faa->faa_bst;
88544ca330bSjmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
88644ca330bSjmcneill 		aprint_error(": couldn't map registers\n");
88744ca330bSjmcneill 		return;
88844ca330bSjmcneill 	}
88944ca330bSjmcneill 	sc->sc_dmat = faa->faa_dmat;
89044ca330bSjmcneill 	LIST_INIT(&sc->sc_dmalist);
891*6e54367aSthorpej 	sc->sc_cfg = of_compatible_lookup(phandle, compat_data)->data;
89244ca330bSjmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
89344ca330bSjmcneill 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
89444ca330bSjmcneill 
89550c8e8bfSjmcneill 	if (sunxi_i2s_clock_init(sc) != 0)
89650c8e8bfSjmcneill 		return;
89750c8e8bfSjmcneill 
89850c8e8bfSjmcneill 	/* At least one of these needs to succeed */
89950c8e8bfSjmcneill 	sunxi_i2s_chan_init(sc, &sc->sc_pchan, AUMODE_PLAY, "tx");
90050c8e8bfSjmcneill 	sunxi_i2s_chan_init(sc, &sc->sc_rchan, AUMODE_RECORD, "rx");
90150c8e8bfSjmcneill 	if (sc->sc_pchan.ch_dma == NULL && sc->sc_rchan.ch_dma == NULL) {
90244ca330bSjmcneill 		aprint_error(": couldn't setup channels\n");
90344ca330bSjmcneill 		return;
90444ca330bSjmcneill 	}
90544ca330bSjmcneill 
90644ca330bSjmcneill 	aprint_naive("\n");
90744ca330bSjmcneill 	aprint_normal(": %s\n", sc->sc_cfg->name);
90844ca330bSjmcneill 
90944ca330bSjmcneill 	/* Reset */
91044ca330bSjmcneill 	val = I2S_READ(sc, DA_CTL);
91144ca330bSjmcneill 	val &= ~(DA_CTL_TXEN|DA_CTL_RXEN|DA_CTL_GEN);
91244ca330bSjmcneill 	I2S_WRITE(sc, DA_CTL, val);
91344ca330bSjmcneill 
91444ca330bSjmcneill 	val = I2S_READ(sc, DA_FCTL);
91544ca330bSjmcneill 	val &= ~(DA_FCTL_FTX|DA_FCTL_FRX);
91644ca330bSjmcneill 	I2S_WRITE(sc, DA_FCTL, val);
91744ca330bSjmcneill 
91844ca330bSjmcneill 	I2S_WRITE(sc, DA_TXCNT, 0);
91944ca330bSjmcneill 	I2S_WRITE(sc, DA_RXCNT, 0);
92044ca330bSjmcneill 
92144ca330bSjmcneill 	/* Enable */
92250c8e8bfSjmcneill 	val = I2S_READ(sc, DA_CTL);
92350c8e8bfSjmcneill 	val |= DA_CTL_GEN;
92450c8e8bfSjmcneill 	I2S_WRITE(sc, DA_CTL, val);
92550c8e8bfSjmcneill 	val |= DA_CTL_SDO_EN;
92650c8e8bfSjmcneill 	I2S_WRITE(sc, DA_CTL, val);
92744ca330bSjmcneill 
92844ca330bSjmcneill 	/* Setup channels */
92944ca330bSjmcneill 	I2S_WRITE(sc, sc->sc_cfg->txchmap, 0x76543210);
93050c8e8bfSjmcneill 	val = I2S_READ(sc, sc->sc_cfg->txchsel);
93150c8e8bfSjmcneill 	val &= ~DA_CHSEL_EN;
93250c8e8bfSjmcneill 	val |= __SHIFTIN(3, DA_CHSEL_EN);
93350c8e8bfSjmcneill 	val &= ~DA_CHSEL_SEL;
93450c8e8bfSjmcneill 	val |= __SHIFTIN(1, DA_CHSEL_SEL);
93550c8e8bfSjmcneill 	I2S_WRITE(sc, sc->sc_cfg->txchsel, val);
93644ca330bSjmcneill 	I2S_WRITE(sc, sc->sc_cfg->rxchmap, 0x76543210);
93750c8e8bfSjmcneill 	val = I2S_READ(sc, sc->sc_cfg->rxchsel);
93850c8e8bfSjmcneill 	val &= ~DA_CHSEL_EN;
93950c8e8bfSjmcneill 	val |= __SHIFTIN(3, DA_CHSEL_EN);
94050c8e8bfSjmcneill 	val &= ~DA_CHSEL_SEL;
94150c8e8bfSjmcneill 	val |= __SHIFTIN(1, DA_CHSEL_SEL);
94250c8e8bfSjmcneill 	I2S_WRITE(sc, sc->sc_cfg->rxchsel, val);
94350c8e8bfSjmcneill 
94450c8e8bfSjmcneill 	if (I2S_TYPE(sc) == SUNXI_I2S_SUN8I) {
94550c8e8bfSjmcneill 		val = I2S_READ(sc, DA_CHCFG);
94650c8e8bfSjmcneill 		val &= ~DA_CHCFG_TX_SLOT_NUM;
94750c8e8bfSjmcneill 		val |= __SHIFTIN(1, DA_CHCFG_TX_SLOT_NUM);
94850c8e8bfSjmcneill 		val &= ~DA_CHCFG_RX_SLOT_NUM;
94950c8e8bfSjmcneill 		val |= __SHIFTIN(1, DA_CHCFG_RX_SLOT_NUM);
95050c8e8bfSjmcneill 		I2S_WRITE(sc, DA_CHCFG, val);
95150c8e8bfSjmcneill 	}
95244ca330bSjmcneill 
95344ca330bSjmcneill 	sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
95444ca330bSjmcneill 	sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
95550c8e8bfSjmcneill 	sc->sc_format.validbits = 16;
95650c8e8bfSjmcneill 	sc->sc_format.precision = 16;
95744ca330bSjmcneill 	sc->sc_format.channels = 2;
95844ca330bSjmcneill 	sc->sc_format.channel_mask = AUFMT_STEREO;
959e622eac4Sisaki 	sc->sc_format.frequency_type = 1;
96050c8e8bfSjmcneill 	sc->sc_format.frequency[0] = SUNXI_I2S_SAMPLE_RATE;
96144ca330bSjmcneill 
96244ca330bSjmcneill 	sc->sc_dai.dai_set_sysclk = sunxi_i2s_dai_set_sysclk;
96344ca330bSjmcneill 	sc->sc_dai.dai_set_format = sunxi_i2s_dai_set_format;
96444ca330bSjmcneill 	sc->sc_dai.dai_hw_if = &sunxi_i2s_hw_if;
96544ca330bSjmcneill 	sc->sc_dai.dai_dev = self;
96644ca330bSjmcneill 	sc->sc_dai.dai_priv = sc;
96744ca330bSjmcneill 	fdtbus_register_dai_controller(self, phandle, &sunxi_i2s_dai_funcs);
96844ca330bSjmcneill }
96944ca330bSjmcneill 
97044ca330bSjmcneill CFATTACH_DECL_NEW(sunxi_i2s, sizeof(struct sunxi_i2s_softc),
97144ca330bSjmcneill     sunxi_i2s_match, sunxi_i2s_attach, NULL, NULL);
972