Home | History | Annotate | Line # | Download | only in rockchip
rk_i2s.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: rk_i2s.c,v 1.1 2019/11/16 13:24:03 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2019 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.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: rk_i2s.c,v 1.1 2019/11/16 13:24:03 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 
     38  1.1  jmcneill #include <sys/audioio.h>
     39  1.1  jmcneill #include <dev/audio/audio_if.h>
     40  1.1  jmcneill #include <dev/audio/linear.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     43  1.1  jmcneill #include <dev/fdt/syscon.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #define	RK_I2S_FIFO_DEPTH	32
     46  1.1  jmcneill #define	RK_I2S_SAMPLE_RATE	48000
     47  1.1  jmcneill 
     48  1.1  jmcneill #define	I2S_TXCR		0x00
     49  1.1  jmcneill #define	 TXCR_RCNT			__BITS(22,17)
     50  1.1  jmcneill #define	 TXCR_TCSR			__BITS(16,15)
     51  1.1  jmcneill #define	 TXCR_HWT			__BIT(14)
     52  1.1  jmcneill #define	 TXCR_SJM			__BIT(12)
     53  1.1  jmcneill #define	 TXCR_FBM			__BIT(11)
     54  1.1  jmcneill #define	 TXCR_IBM			__BITS(10,9)
     55  1.1  jmcneill #define	 TXCR_PBM			__BITS(8,7)
     56  1.1  jmcneill #define	 TXCR_TFS			__BIT(5)
     57  1.1  jmcneill #define	 TXCR_VDW			__BITS(4,0)
     58  1.1  jmcneill #define	I2S_RXCR		0x04
     59  1.1  jmcneill #define	 RXCR_RCSR			__BITS(16,15)
     60  1.1  jmcneill #define	 RXCR_HWT			__BIT(14)
     61  1.1  jmcneill #define	 RXCR_SJM			__BIT(12)
     62  1.1  jmcneill #define	 RXCR_FBM			__BIT(11)
     63  1.1  jmcneill #define	 RXCR_IBM			__BITS(10,9)
     64  1.1  jmcneill #define	 RXCR_PBM			__BITS(8,7)
     65  1.1  jmcneill #define	 RXCR_TFS			__BIT(5)
     66  1.1  jmcneill #define	 RXCR_VDW			__BITS(4,0)
     67  1.1  jmcneill #define	I2S_CKR			0x08
     68  1.1  jmcneill #define	 CKR_TRCM			__BITS(29,28)
     69  1.1  jmcneill #define	 CKR_MSS			__BIT(27)
     70  1.1  jmcneill #define	 CKR_CKP			__BIT(26)
     71  1.1  jmcneill #define	 CKR_RLP			__BIT(25)
     72  1.1  jmcneill #define	 CKR_TLP			__BIT(24)
     73  1.1  jmcneill #define	 CKR_MDIV			__BITS(23,16)
     74  1.1  jmcneill #define	 CKR_RSD			__BITS(15,8)
     75  1.1  jmcneill #define	 CKR_TSD			__BITS(7,0)
     76  1.1  jmcneill #define	I2S_TXFIFOLR		0x0c
     77  1.1  jmcneill #define	 TXFIFOLR_TFL(n)		__BITS((n) * 6 + 5, (n) * 6)
     78  1.1  jmcneill #define	I2S_DMACR		0x10
     79  1.1  jmcneill #define	 DMACR_RDE			__BIT(24)
     80  1.1  jmcneill #define	 DMACR_RDL			__BITS(20,16)
     81  1.1  jmcneill #define	 DMACR_TDE			__BIT(8)
     82  1.1  jmcneill #define	 DMACR_TDL			__BITS(4,0)
     83  1.1  jmcneill #define	I2S_INTCR		0x14
     84  1.1  jmcneill #define	 INTCR_RFT			__BITS(24,20)
     85  1.1  jmcneill #define	 INTCR_RXOIC			__BIT(18)
     86  1.1  jmcneill #define	 INTCR_RXOIE			__BIT(17)
     87  1.1  jmcneill #define	 INTCR_RXFIE			__BIT(16)
     88  1.1  jmcneill #define	 INTCR_TFT			__BITS(8,4)
     89  1.1  jmcneill #define	 INTCR_TXUIC			__BIT(2)
     90  1.1  jmcneill #define	 INTCR_TXUIE			__BIT(1)
     91  1.1  jmcneill #define	 INTCR_TXEIE			__BIT(0)
     92  1.1  jmcneill #define	I2S_INTSR		0x18
     93  1.1  jmcneill #define	 INTSR_RXOI			__BIT(17)
     94  1.1  jmcneill #define	 INTSR_RXFI			__BIT(16)
     95  1.1  jmcneill #define	 INTSR_TXUI			__BIT(1)
     96  1.1  jmcneill #define	 INTSR_TXEI			__BIT(0)
     97  1.1  jmcneill #define	I2S_XFER		0x1c
     98  1.1  jmcneill #define	 XFER_RXS			__BIT(1)
     99  1.1  jmcneill #define	 XFER_TXS			__BIT(0)
    100  1.1  jmcneill #define	I2S_CLR			0x20
    101  1.1  jmcneill #define	 CLR_RXC			__BIT(1)
    102  1.1  jmcneill #define	 CLR_TXC			__BIT(0)
    103  1.1  jmcneill #define	I2S_TXDR		0x24
    104  1.1  jmcneill #define	I2S_RXDR		0x28
    105  1.1  jmcneill #define	I2S_RXFIFOLR		0x2c
    106  1.1  jmcneill #define	 RXFIFOLR_RFL(n)		__BITS((n) * 6 + 5, (n) * 6)
    107  1.1  jmcneill 
    108  1.1  jmcneill struct rk_i2s_config {
    109  1.1  jmcneill 	bus_size_t		oe_reg;
    110  1.1  jmcneill 	u_int			oe_mask;
    111  1.1  jmcneill 	u_int			oe_val;
    112  1.1  jmcneill };
    113  1.1  jmcneill 
    114  1.1  jmcneill static const struct rk_i2s_config rk3399_i2s_config = {
    115  1.1  jmcneill 	.oe_reg = 0x0e220,
    116  1.1  jmcneill 	.oe_mask = __BITS(13,11),
    117  1.1  jmcneill 	.oe_val = 0x7,
    118  1.1  jmcneill };
    119  1.1  jmcneill 
    120  1.1  jmcneill static const struct of_compat_data compat_data[] = {
    121  1.1  jmcneill 	{ "rockchip,rk3399-i2s",	(uintptr_t)&rk3399_i2s_config },
    122  1.1  jmcneill 	{ NULL }
    123  1.1  jmcneill };
    124  1.1  jmcneill 
    125  1.1  jmcneill struct rk_i2s_softc;
    126  1.1  jmcneill 
    127  1.1  jmcneill struct rk_i2s_chan {
    128  1.1  jmcneill 	uint32_t		*ch_start;
    129  1.1  jmcneill 	uint32_t		*ch_end;
    130  1.1  jmcneill 	uint32_t		*ch_cur;
    131  1.1  jmcneill 
    132  1.1  jmcneill 	int			ch_blksize;
    133  1.1  jmcneill 	int			ch_resid;
    134  1.1  jmcneill 
    135  1.1  jmcneill 	void			(*ch_intr)(void *);
    136  1.1  jmcneill 	void			*ch_intrarg;
    137  1.1  jmcneill };
    138  1.1  jmcneill 
    139  1.1  jmcneill struct rk_i2s_softc {
    140  1.1  jmcneill 	device_t		sc_dev;
    141  1.1  jmcneill 	bus_space_tag_t		sc_bst;
    142  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
    143  1.1  jmcneill 	int			sc_phandle;
    144  1.1  jmcneill 	struct clk		*sc_clk;
    145  1.1  jmcneill 	struct syscon		*sc_grf;
    146  1.1  jmcneill 	const struct rk_i2s_config *sc_conf;
    147  1.1  jmcneill 
    148  1.1  jmcneill 	kmutex_t		sc_lock;
    149  1.1  jmcneill 	kmutex_t		sc_intr_lock;
    150  1.1  jmcneill 
    151  1.1  jmcneill 	struct audio_format	sc_format;
    152  1.1  jmcneill 
    153  1.1  jmcneill 	struct rk_i2s_chan	sc_pchan;
    154  1.1  jmcneill 	struct rk_i2s_chan	sc_rchan;
    155  1.1  jmcneill 
    156  1.1  jmcneill 	u_int			sc_active;
    157  1.1  jmcneill 
    158  1.1  jmcneill 	struct audio_dai_device	sc_dai;
    159  1.1  jmcneill };
    160  1.1  jmcneill 
    161  1.1  jmcneill #define	RD4(sc, reg)			\
    162  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    163  1.1  jmcneill #define	WR4(sc, reg, val)		\
    164  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    165  1.1  jmcneill 
    166  1.1  jmcneill static int
    167  1.1  jmcneill rk_i2s_query_format(void *priv, audio_format_query_t *afp)
    168  1.1  jmcneill {
    169  1.1  jmcneill 	struct rk_i2s_softc * const sc = priv;
    170  1.1  jmcneill 
    171  1.1  jmcneill 	return audio_query_format(&sc->sc_format, 1, afp);
    172  1.1  jmcneill }
    173  1.1  jmcneill 
    174  1.1  jmcneill static int
    175  1.1  jmcneill rk_i2s_set_format(void *priv, int setmode,
    176  1.1  jmcneill     const audio_params_t *play, const audio_params_t *rec,
    177  1.1  jmcneill     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    178  1.1  jmcneill {
    179  1.1  jmcneill 	struct rk_i2s_softc * const sc = priv;
    180  1.1  jmcneill 	uint32_t ckr, txcr, rxcr;
    181  1.1  jmcneill 
    182  1.1  jmcneill 	ckr = RD4(sc, I2S_CKR);
    183  1.1  jmcneill 	if ((ckr & CKR_MSS) == 0) {
    184  1.1  jmcneill 		const u_int mclk_rate = clk_get_rate(sc->sc_clk);
    185  1.1  jmcneill 		device_printf(sc->sc_dev, "%s: sysclk rate %u Hz\n", __func__, mclk_rate);
    186  1.1  jmcneill 		const u_int bclk_rate = 2 * 32 * RK_I2S_SAMPLE_RATE;
    187  1.1  jmcneill 		const u_int bclk_div = mclk_rate / bclk_rate;
    188  1.1  jmcneill 		const u_int lrck_div = bclk_rate / RK_I2S_SAMPLE_RATE;
    189  1.1  jmcneill 
    190  1.1  jmcneill 		ckr &= ~CKR_MDIV;
    191  1.1  jmcneill 		ckr |= __SHIFTIN(bclk_div - 1, CKR_MDIV);
    192  1.1  jmcneill 		ckr &= ~CKR_TSD;
    193  1.1  jmcneill 		ckr |= __SHIFTIN(lrck_div - 1, CKR_TSD);
    194  1.1  jmcneill 		ckr &= ~CKR_RSD;
    195  1.1  jmcneill 		ckr |= __SHIFTIN(lrck_div - 1, CKR_RSD);
    196  1.1  jmcneill 	}
    197  1.1  jmcneill 
    198  1.1  jmcneill 	ckr &= ~CKR_TRCM;
    199  1.1  jmcneill 	ckr |= __SHIFTIN(0, CKR_TRCM);
    200  1.1  jmcneill 	WR4(sc, I2S_CKR, ckr);
    201  1.1  jmcneill 
    202  1.1  jmcneill 	if (play && (setmode & AUMODE_PLAY) != 0) {
    203  1.1  jmcneill 		if (play->channels & 1)
    204  1.1  jmcneill 			return EINVAL;
    205  1.1  jmcneill 		txcr = RD4(sc, I2S_TXCR);
    206  1.1  jmcneill 		txcr &= ~TXCR_VDW;
    207  1.1  jmcneill 		txcr |= __SHIFTIN(play->validbits - 1, TXCR_VDW);
    208  1.1  jmcneill 		txcr &= ~TXCR_TCSR;
    209  1.1  jmcneill 		txcr |= __SHIFTIN(play->channels / 2 - 1, TXCR_TCSR);
    210  1.1  jmcneill 		WR4(sc, I2S_TXCR, txcr);
    211  1.1  jmcneill 	}
    212  1.1  jmcneill 
    213  1.1  jmcneill 	if (rec && (setmode & AUMODE_RECORD) != 0) {
    214  1.1  jmcneill 		if (rec->channels & 1)
    215  1.1  jmcneill 			return EINVAL;
    216  1.1  jmcneill 		rxcr = RD4(sc, I2S_RXCR);
    217  1.1  jmcneill 		rxcr &= ~RXCR_VDW;
    218  1.1  jmcneill 		rxcr |= __SHIFTIN(rec->validbits - 1, RXCR_VDW);
    219  1.1  jmcneill 		rxcr &= ~RXCR_RCSR;
    220  1.1  jmcneill 		rxcr |= __SHIFTIN(rec->channels / 2 - 1, RXCR_RCSR);
    221  1.1  jmcneill 		WR4(sc, I2S_RXCR, rxcr);
    222  1.1  jmcneill 	}
    223  1.1  jmcneill 
    224  1.1  jmcneill 	return 0;
    225  1.1  jmcneill }
    226  1.1  jmcneill 
    227  1.1  jmcneill static int
    228  1.1  jmcneill rk_i2s_get_props(void *priv)
    229  1.1  jmcneill {
    230  1.1  jmcneill 
    231  1.1  jmcneill 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
    232  1.1  jmcneill 	    AUDIO_PROP_FULLDUPLEX;
    233  1.1  jmcneill }
    234  1.1  jmcneill 
    235  1.1  jmcneill static int
    236  1.1  jmcneill rk_i2s_round_blocksize(void *priv, int bs, int mode,
    237  1.1  jmcneill     const audio_params_t *params)
    238  1.1  jmcneill {
    239  1.1  jmcneill 	bs &= ~3;
    240  1.1  jmcneill 	if (bs == 0)
    241  1.1  jmcneill 		bs = 4;
    242  1.1  jmcneill 	return bs;
    243  1.1  jmcneill }
    244  1.1  jmcneill 
    245  1.1  jmcneill static void *
    246  1.1  jmcneill rk_i2s_allocm(void *priv, int dir, size_t size)
    247  1.1  jmcneill {
    248  1.1  jmcneill 	return kmem_zalloc(size, KM_SLEEP);
    249  1.1  jmcneill }
    250  1.1  jmcneill 
    251  1.1  jmcneill static void
    252  1.1  jmcneill rk_i2s_freem(void *priv, void *addr, size_t size)
    253  1.1  jmcneill {
    254  1.1  jmcneill 	kmem_free(addr, size);
    255  1.1  jmcneill }
    256  1.1  jmcneill 
    257  1.1  jmcneill static int
    258  1.1  jmcneill rk_i2s_trigger_output(void *priv, void *start, void *end, int blksize,
    259  1.1  jmcneill     void (*intr)(void *), void *intrarg, const audio_params_t *params)
    260  1.1  jmcneill {
    261  1.1  jmcneill 	struct rk_i2s_softc * const sc = priv;
    262  1.1  jmcneill 	struct rk_i2s_chan *ch = &sc->sc_pchan;
    263  1.1  jmcneill 	uint32_t val;
    264  1.1  jmcneill 
    265  1.1  jmcneill 	if (sc->sc_active == 0) {
    266  1.1  jmcneill 		val = RD4(sc, I2S_XFER);
    267  1.1  jmcneill 		val |= (XFER_TXS | XFER_RXS);
    268  1.1  jmcneill 		WR4(sc, I2S_XFER, val);
    269  1.1  jmcneill 	}
    270  1.1  jmcneill 
    271  1.1  jmcneill 	sc->sc_active |= XFER_TXS;
    272  1.1  jmcneill 
    273  1.1  jmcneill 	val = RD4(sc, I2S_INTCR);
    274  1.1  jmcneill 	val |= INTCR_TXEIE;
    275  1.1  jmcneill 	val &= ~INTCR_TFT;
    276  1.1  jmcneill 	val |= __SHIFTIN(RK_I2S_FIFO_DEPTH / 2, INTCR_TFT);
    277  1.1  jmcneill 	WR4(sc, I2S_INTCR, val);
    278  1.1  jmcneill 
    279  1.1  jmcneill 	ch->ch_intr = intr;
    280  1.1  jmcneill 	ch->ch_intrarg = intrarg;
    281  1.1  jmcneill 	ch->ch_start = ch->ch_cur = start;
    282  1.1  jmcneill 	ch->ch_end = end;
    283  1.1  jmcneill 	ch->ch_blksize = blksize;
    284  1.1  jmcneill 	ch->ch_resid = blksize;
    285  1.1  jmcneill 
    286  1.1  jmcneill 	return 0;
    287  1.1  jmcneill }
    288  1.1  jmcneill 
    289  1.1  jmcneill static int
    290  1.1  jmcneill rk_i2s_trigger_input(void *priv, void *start, void *end, int blksize,
    291  1.1  jmcneill     void (*intr)(void *), void *intrarg, const audio_params_t *params)
    292  1.1  jmcneill {
    293  1.1  jmcneill 	return EIO;
    294  1.1  jmcneill }
    295  1.1  jmcneill 
    296  1.1  jmcneill static int
    297  1.1  jmcneill rk_i2s_halt_output(void *priv)
    298  1.1  jmcneill {
    299  1.1  jmcneill 	struct rk_i2s_softc * const sc = priv;
    300  1.1  jmcneill 	struct rk_i2s_chan *ch = &sc->sc_pchan;
    301  1.1  jmcneill 	uint32_t val;
    302  1.1  jmcneill 
    303  1.1  jmcneill 	sc->sc_active &= ~XFER_TXS;
    304  1.1  jmcneill 	if (sc->sc_active == 0) {
    305  1.1  jmcneill 		val = RD4(sc, I2S_XFER);
    306  1.1  jmcneill 		val &= ~(XFER_TXS|XFER_RXS);
    307  1.1  jmcneill 		WR4(sc, I2S_XFER, val);
    308  1.1  jmcneill 	}
    309  1.1  jmcneill 
    310  1.1  jmcneill 	val = RD4(sc, I2S_INTCR);
    311  1.1  jmcneill 	val &= ~INTCR_TXEIE;
    312  1.1  jmcneill 	WR4(sc, I2S_INTCR, val);
    313  1.1  jmcneill 
    314  1.1  jmcneill 	val = RD4(sc, I2S_CLR);
    315  1.1  jmcneill 	val |= CLR_TXC;
    316  1.1  jmcneill 	WR4(sc, I2S_CLR, val);
    317  1.1  jmcneill 
    318  1.1  jmcneill 	while ((RD4(sc, I2S_CLR) & CLR_TXC) != 0)
    319  1.1  jmcneill 		delay(1);
    320  1.1  jmcneill 
    321  1.1  jmcneill 	ch->ch_intr = NULL;
    322  1.1  jmcneill 	ch->ch_intrarg = NULL;
    323  1.1  jmcneill 
    324  1.1  jmcneill 	return 0;
    325  1.1  jmcneill }
    326  1.1  jmcneill 
    327  1.1  jmcneill static int
    328  1.1  jmcneill rk_i2s_halt_input(void *priv)
    329  1.1  jmcneill {
    330  1.1  jmcneill 	struct rk_i2s_softc * const sc = priv;
    331  1.1  jmcneill 	struct rk_i2s_chan *ch = &sc->sc_rchan;
    332  1.1  jmcneill 	uint32_t val;
    333  1.1  jmcneill 
    334  1.1  jmcneill 	sc->sc_active &= ~XFER_RXS;
    335  1.1  jmcneill 	if (sc->sc_active == 0) {
    336  1.1  jmcneill 		val = RD4(sc, I2S_XFER);
    337  1.1  jmcneill 		val &= ~(XFER_TXS|XFER_RXS);
    338  1.1  jmcneill 		WR4(sc, I2S_XFER, val);
    339  1.1  jmcneill 	}
    340  1.1  jmcneill 
    341  1.1  jmcneill 	val = RD4(sc, I2S_INTCR);
    342  1.1  jmcneill 	val &= ~INTCR_RXFIE;
    343  1.1  jmcneill 	WR4(sc, I2S_INTCR, val);
    344  1.1  jmcneill 
    345  1.1  jmcneill 	ch->ch_intr = NULL;
    346  1.1  jmcneill 	ch->ch_intrarg = NULL;
    347  1.1  jmcneill 
    348  1.1  jmcneill 	return 0;
    349  1.1  jmcneill }
    350  1.1  jmcneill 
    351  1.1  jmcneill static void
    352  1.1  jmcneill rk_i2s_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
    353  1.1  jmcneill {
    354  1.1  jmcneill 	struct rk_i2s_softc * const sc = priv;
    355  1.1  jmcneill 
    356  1.1  jmcneill 	*intr = &sc->sc_intr_lock;
    357  1.1  jmcneill 	*thread = &sc->sc_lock;
    358  1.1  jmcneill }
    359  1.1  jmcneill 
    360  1.1  jmcneill static const struct audio_hw_if rk_i2s_hw_if = {
    361  1.1  jmcneill 	.query_format = rk_i2s_query_format,
    362  1.1  jmcneill 	.set_format = rk_i2s_set_format,
    363  1.1  jmcneill 	.get_props = rk_i2s_get_props,
    364  1.1  jmcneill 	.round_blocksize = rk_i2s_round_blocksize,
    365  1.1  jmcneill 	.allocm = rk_i2s_allocm,
    366  1.1  jmcneill 	.freem = rk_i2s_freem,
    367  1.1  jmcneill 	.trigger_output = rk_i2s_trigger_output,
    368  1.1  jmcneill 	.trigger_input = rk_i2s_trigger_input,
    369  1.1  jmcneill 	.halt_output = rk_i2s_halt_output,
    370  1.1  jmcneill 	.halt_input = rk_i2s_halt_input,
    371  1.1  jmcneill 	.get_locks = rk_i2s_get_locks,
    372  1.1  jmcneill };
    373  1.1  jmcneill 
    374  1.1  jmcneill static int
    375  1.1  jmcneill rk_i2s_intr(void *priv)
    376  1.1  jmcneill {
    377  1.1  jmcneill 	struct rk_i2s_softc * const sc = priv;
    378  1.1  jmcneill 	struct rk_i2s_chan * const pch = &sc->sc_pchan;
    379  1.1  jmcneill #if notyet
    380  1.1  jmcneill 	struct rk_i2s_chan * const rch = &sc->sc_rchan;
    381  1.1  jmcneill #endif
    382  1.1  jmcneill 	uint32_t sr, val;
    383  1.1  jmcneill 	int fifolr;
    384  1.1  jmcneill 
    385  1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    386  1.1  jmcneill 
    387  1.1  jmcneill 	sr = RD4(sc, I2S_INTSR);
    388  1.1  jmcneill 
    389  1.1  jmcneill 	if ((sr & INTSR_RXFI) != 0) {
    390  1.1  jmcneill #if notyet
    391  1.1  jmcneill 		val = RD4(sc, I2S_RXFIFOLR);
    392  1.1  jmcneill 		fifolr = __SHIFTOUT(val, RXFIFOLR_RFL(0));
    393  1.1  jmcneill 		while (fifolr > 0) {
    394  1.1  jmcneill 			*rch->ch_data = RD4(sc, I2S_RXDR);
    395  1.1  jmcneill 			rch->ch_data++;
    396  1.1  jmcneill 			rch->ch_resid -= 4;
    397  1.1  jmcneill 			if (rch->ch_resid == 0)
    398  1.1  jmcneill 				rch->ch_intr(rch->ch_intrarg);
    399  1.1  jmcneill 			--fifolr;
    400  1.1  jmcneill 		}
    401  1.1  jmcneill #endif
    402  1.1  jmcneill 	}
    403  1.1  jmcneill 
    404  1.1  jmcneill 	if ((sr & INTSR_TXEI) != 0) {
    405  1.1  jmcneill 		val = RD4(sc, I2S_TXFIFOLR);
    406  1.1  jmcneill 		fifolr = __SHIFTOUT(val, TXFIFOLR_TFL(0));
    407  1.1  jmcneill 		fifolr = uimin(fifolr, RK_I2S_FIFO_DEPTH);
    408  1.1  jmcneill 		while (fifolr < RK_I2S_FIFO_DEPTH - 1) {
    409  1.1  jmcneill 			WR4(sc, I2S_TXDR, *pch->ch_cur);
    410  1.1  jmcneill 			pch->ch_cur++;
    411  1.1  jmcneill 			if (pch->ch_cur == pch->ch_end)
    412  1.1  jmcneill 				pch->ch_cur = pch->ch_start;
    413  1.1  jmcneill 			pch->ch_resid -= 4;
    414  1.1  jmcneill 			if (pch->ch_resid == 0) {
    415  1.1  jmcneill 				pch->ch_intr(pch->ch_intrarg);
    416  1.1  jmcneill 				pch->ch_resid = pch->ch_blksize;
    417  1.1  jmcneill 			}
    418  1.1  jmcneill 			++fifolr;
    419  1.1  jmcneill 		}
    420  1.1  jmcneill 	}
    421  1.1  jmcneill 
    422  1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
    423  1.1  jmcneill 
    424  1.1  jmcneill 	return 0;
    425  1.1  jmcneill }
    426  1.1  jmcneill 
    427  1.1  jmcneill static int
    428  1.1  jmcneill rk_i2s_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir)
    429  1.1  jmcneill {
    430  1.1  jmcneill 	struct rk_i2s_softc * const sc = audio_dai_private(dai);
    431  1.1  jmcneill 	int error;
    432  1.1  jmcneill 
    433  1.1  jmcneill 	device_printf(sc->sc_dev, "set sysclk %u Hz\n", rate);
    434  1.1  jmcneill 	error = clk_set_rate(sc->sc_clk, rate);
    435  1.1  jmcneill 	if (error != 0) {
    436  1.1  jmcneill 		device_printf(sc->sc_dev, "failed to set sysclk to %u Hz: %d\n",
    437  1.1  jmcneill 		    rate, error);
    438  1.1  jmcneill 		return error;
    439  1.1  jmcneill 	}
    440  1.1  jmcneill 
    441  1.1  jmcneill 	return 0;
    442  1.1  jmcneill }
    443  1.1  jmcneill 
    444  1.1  jmcneill static int
    445  1.1  jmcneill rk_i2s_dai_set_format(audio_dai_tag_t dai, u_int format)
    446  1.1  jmcneill {
    447  1.1  jmcneill 	struct rk_i2s_softc * const sc = audio_dai_private(dai);
    448  1.1  jmcneill 	uint32_t txcr, rxcr, ckr;
    449  1.1  jmcneill 
    450  1.1  jmcneill 	const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK);
    451  1.1  jmcneill 	const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK);
    452  1.1  jmcneill 	const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK);
    453  1.1  jmcneill 
    454  1.1  jmcneill 	txcr = RD4(sc, I2S_TXCR);
    455  1.1  jmcneill 	rxcr = RD4(sc, I2S_RXCR);
    456  1.1  jmcneill 	ckr = RD4(sc, I2S_CKR);
    457  1.1  jmcneill 
    458  1.1  jmcneill 	txcr &= ~(TXCR_IBM|TXCR_PBM|TXCR_TFS);
    459  1.1  jmcneill 	rxcr &= ~(RXCR_IBM|RXCR_PBM|RXCR_TFS);
    460  1.1  jmcneill 	switch (fmt) {
    461  1.1  jmcneill 	case AUDIO_DAI_FORMAT_I2S:
    462  1.1  jmcneill 		txcr |= __SHIFTIN(0, TXCR_IBM);
    463  1.1  jmcneill 		rxcr |= __SHIFTIN(0, RXCR_IBM);
    464  1.1  jmcneill 		break;
    465  1.1  jmcneill 	case AUDIO_DAI_FORMAT_LJ:
    466  1.1  jmcneill 		txcr |= __SHIFTIN(1, TXCR_IBM);
    467  1.1  jmcneill 		rxcr |= __SHIFTIN(1, RXCR_IBM);
    468  1.1  jmcneill 		break;
    469  1.1  jmcneill 	case AUDIO_DAI_FORMAT_RJ:
    470  1.1  jmcneill 		txcr |= __SHIFTIN(2, TXCR_IBM);
    471  1.1  jmcneill 		rxcr |= __SHIFTIN(2, RXCR_IBM);
    472  1.1  jmcneill 		break;
    473  1.1  jmcneill 	case AUDIO_DAI_FORMAT_DSPA:
    474  1.1  jmcneill 		txcr |= __SHIFTIN(0, TXCR_PBM);
    475  1.1  jmcneill 		txcr |= TXCR_TFS;
    476  1.1  jmcneill 		rxcr |= __SHIFTIN(0, RXCR_PBM);
    477  1.1  jmcneill 		txcr |= RXCR_TFS;
    478  1.1  jmcneill 		break;
    479  1.1  jmcneill 	case AUDIO_DAI_FORMAT_DSPB:
    480  1.1  jmcneill 		txcr |= __SHIFTIN(1, TXCR_PBM);
    481  1.1  jmcneill 		txcr |= TXCR_TFS;
    482  1.1  jmcneill 		rxcr |= __SHIFTIN(1, RXCR_PBM);
    483  1.1  jmcneill 		txcr |= RXCR_TFS;
    484  1.1  jmcneill 		break;
    485  1.1  jmcneill 	default:
    486  1.1  jmcneill 		return EINVAL;
    487  1.1  jmcneill 	}
    488  1.1  jmcneill 
    489  1.1  jmcneill 	WR4(sc, I2S_TXCR, txcr);
    490  1.1  jmcneill 	WR4(sc, I2S_RXCR, rxcr);
    491  1.1  jmcneill 
    492  1.1  jmcneill 	switch (pol) {
    493  1.1  jmcneill 	case AUDIO_DAI_POLARITY_IB_NF:
    494  1.1  jmcneill 		ckr |= CKR_CKP;
    495  1.1  jmcneill 		break;
    496  1.1  jmcneill 	case AUDIO_DAI_POLARITY_NB_NF:
    497  1.1  jmcneill 		ckr &= ~CKR_CKP;
    498  1.1  jmcneill 		break;
    499  1.1  jmcneill 	default:
    500  1.1  jmcneill 		return EINVAL;
    501  1.1  jmcneill 	}
    502  1.1  jmcneill 
    503  1.1  jmcneill 	switch (clk) {
    504  1.1  jmcneill 	case AUDIO_DAI_CLOCK_CBM_CFM:
    505  1.1  jmcneill 		ckr |= CKR_MSS;		/* sclk input */
    506  1.1  jmcneill 		break;
    507  1.1  jmcneill 	case AUDIO_DAI_CLOCK_CBS_CFS:
    508  1.1  jmcneill 		ckr &= ~CKR_MSS;	/* sclk output */
    509  1.1  jmcneill 		break;
    510  1.1  jmcneill 	default:
    511  1.1  jmcneill 		return EINVAL;
    512  1.1  jmcneill 	}
    513  1.1  jmcneill 
    514  1.1  jmcneill 	WR4(sc, I2S_CKR, ckr);
    515  1.1  jmcneill 
    516  1.1  jmcneill 	return 0;
    517  1.1  jmcneill }
    518  1.1  jmcneill 
    519  1.1  jmcneill static audio_dai_tag_t
    520  1.1  jmcneill rk_i2s_dai_get_tag(device_t dev, const void *data, size_t len)
    521  1.1  jmcneill {
    522  1.1  jmcneill 	struct rk_i2s_softc * const sc = device_private(dev);
    523  1.1  jmcneill 
    524  1.1  jmcneill 	if (len != 4)
    525  1.1  jmcneill 		return NULL;
    526  1.1  jmcneill 
    527  1.1  jmcneill 	return &sc->sc_dai;
    528  1.1  jmcneill }
    529  1.1  jmcneill 
    530  1.1  jmcneill static struct fdtbus_dai_controller_func rk_i2s_dai_funcs = {
    531  1.1  jmcneill 	.get_tag = rk_i2s_dai_get_tag
    532  1.1  jmcneill };
    533  1.1  jmcneill 
    534  1.1  jmcneill static int
    535  1.1  jmcneill rk_i2s_clock_init(struct rk_i2s_softc *sc)
    536  1.1  jmcneill {
    537  1.1  jmcneill 	const int phandle = sc->sc_phandle;
    538  1.1  jmcneill 	int error;
    539  1.1  jmcneill 
    540  1.1  jmcneill 	sc->sc_clk = fdtbus_clock_get(phandle, "i2s_clk");
    541  1.1  jmcneill 	if (sc->sc_clk == NULL) {
    542  1.1  jmcneill 		aprint_error(": couldn't find i2s_clk clock\n");
    543  1.1  jmcneill 		return ENXIO;
    544  1.1  jmcneill 	}
    545  1.1  jmcneill 	error = clk_enable(sc->sc_clk);
    546  1.1  jmcneill 	if (error != 0) {
    547  1.1  jmcneill 		aprint_error(": couldn't enable i2s_clk clock: %d\n", error);
    548  1.1  jmcneill 		return error;
    549  1.1  jmcneill 	}
    550  1.1  jmcneill 
    551  1.1  jmcneill 	/* Enable bus clock */
    552  1.1  jmcneill 	if (fdtbus_clock_enable(phandle, "i2s_hclk", true) != 0) {
    553  1.1  jmcneill 		aprint_error(": couldn't enable i2s_hclk clock: %d\n", error);
    554  1.1  jmcneill 		return error;
    555  1.1  jmcneill 	}
    556  1.1  jmcneill 
    557  1.1  jmcneill 	return 0;
    558  1.1  jmcneill }
    559  1.1  jmcneill 
    560  1.1  jmcneill static int
    561  1.1  jmcneill rk_i2s_match(device_t parent, cfdata_t cf, void *aux)
    562  1.1  jmcneill {
    563  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    564  1.1  jmcneill 
    565  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    566  1.1  jmcneill }
    567  1.1  jmcneill 
    568  1.1  jmcneill static void
    569  1.1  jmcneill rk_i2s_attach(device_t parent, device_t self, void *aux)
    570  1.1  jmcneill {
    571  1.1  jmcneill 	struct rk_i2s_softc * const sc = device_private(self);
    572  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    573  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    574  1.1  jmcneill 	char intrstr[128];
    575  1.1  jmcneill 	bus_addr_t addr;
    576  1.1  jmcneill 	bus_size_t size;
    577  1.1  jmcneill 	uint32_t val;
    578  1.1  jmcneill 
    579  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    580  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    581  1.1  jmcneill 		return;
    582  1.1  jmcneill 	}
    583  1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    584  1.1  jmcneill 		aprint_error(": couldn't decode interrupt\n");
    585  1.1  jmcneill 		return;
    586  1.1  jmcneill 	}
    587  1.1  jmcneill 
    588  1.1  jmcneill 	sc->sc_dev = self;
    589  1.1  jmcneill 	sc->sc_phandle = phandle;
    590  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    591  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    592  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    593  1.1  jmcneill 		return;
    594  1.1  jmcneill 	}
    595  1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    596  1.1  jmcneill 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
    597  1.1  jmcneill 
    598  1.1  jmcneill 	sc->sc_conf = (void *)of_search_compatible(phandle, compat_data)->data;
    599  1.1  jmcneill 	sc->sc_grf = fdtbus_syscon_acquire(phandle, "rockchip,grf");
    600  1.1  jmcneill 	if (sc->sc_grf != NULL && sc->sc_conf->oe_mask != 0) {
    601  1.1  jmcneill 		syscon_lock(sc->sc_grf);
    602  1.1  jmcneill 		val = __SHIFTIN(sc->sc_conf->oe_val, sc->sc_conf->oe_mask);
    603  1.1  jmcneill 		val |= (sc->sc_conf->oe_mask << 16);
    604  1.1  jmcneill 		syscon_write_4(sc->sc_grf, sc->sc_conf->oe_reg, val);
    605  1.1  jmcneill 		syscon_unlock(sc->sc_grf);
    606  1.1  jmcneill 	}
    607  1.1  jmcneill 
    608  1.1  jmcneill 	if (rk_i2s_clock_init(sc) != 0)
    609  1.1  jmcneill 		return;
    610  1.1  jmcneill 
    611  1.1  jmcneill 	aprint_naive("\n");
    612  1.1  jmcneill 	aprint_normal(": I2S/PCM controller\n");
    613  1.1  jmcneill 
    614  1.1  jmcneill 	if (fdtbus_intr_establish(phandle, 0, IPL_AUDIO, FDT_INTR_MPSAFE, rk_i2s_intr, sc) == NULL) {
    615  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n", intrstr);
    616  1.1  jmcneill 		return;
    617  1.1  jmcneill 	}
    618  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    619  1.1  jmcneill 
    620  1.1  jmcneill 	sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
    621  1.1  jmcneill 	sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
    622  1.1  jmcneill 	sc->sc_format.validbits = 16;
    623  1.1  jmcneill 	sc->sc_format.precision = 16;
    624  1.1  jmcneill 	sc->sc_format.channels = 2;
    625  1.1  jmcneill 	sc->sc_format.channel_mask = AUFMT_STEREO;
    626  1.1  jmcneill 	sc->sc_format.frequency_type = 1;
    627  1.1  jmcneill 	sc->sc_format.frequency[0] = RK_I2S_SAMPLE_RATE;
    628  1.1  jmcneill 
    629  1.1  jmcneill 	sc->sc_dai.dai_set_sysclk = rk_i2s_dai_set_sysclk;
    630  1.1  jmcneill 	sc->sc_dai.dai_set_format = rk_i2s_dai_set_format;
    631  1.1  jmcneill 	sc->sc_dai.dai_hw_if = &rk_i2s_hw_if;
    632  1.1  jmcneill 	sc->sc_dai.dai_dev = self;
    633  1.1  jmcneill 	sc->sc_dai.dai_priv = sc;
    634  1.1  jmcneill 	fdtbus_register_dai_controller(self, phandle, &rk_i2s_dai_funcs);
    635  1.1  jmcneill }
    636  1.1  jmcneill 
    637  1.1  jmcneill CFATTACH_DECL_NEW(rk_i2s, sizeof(struct rk_i2s_softc),
    638  1.1  jmcneill     rk_i2s_match, rk_i2s_attach, NULL, NULL);
    639