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