Home | History | Annotate | Line # | Download | only in ic
pl041.c revision 1.7
      1 /* $NetBSD: pl041.c,v 1.7 2020/02/23 04:02:46 isaki Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: pl041.c,v 1.7 2020/02/23 04:02:46 isaki Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 #include <sys/kmem.h>
     36 #include <sys/bus.h>
     37 #include <sys/audioio.h>
     38 
     39 #include <dev/audio/audio_if.h>
     40 
     41 #include <dev/ic/ac97var.h>
     42 #include <dev/ic/ac97reg.h>
     43 
     44 #include <dev/ic/pl041var.h>
     45 
     46 #define	AACIRXCR		0x00
     47 #define	AACITXCR		0x04
     48 #define	 AACITXCR_TXFEN		__BIT(16)
     49 #define	 AACITXCR_TXCM		__BIT(15)
     50 #define	 AACITXCR_TSIZE		__BITS(14,13)
     51 #define	  AACITXCR_TSIZE_16	__SHIFTIN(0, AACITXCR_TSIZE)
     52 #define	  AACITXCR_TX(n)	__BIT(n)
     53 #define	 AACITXCR_TXEN		__BIT(0)
     54 #define	AACISR			0x08
     55 #define	 AACISR_TXFF		__BIT(5)
     56 #define	 AACISR_TXHE		__BIT(3)
     57 #define	AACIISR			0x0c
     58 #define	 AACIISR_URINTR		__BIT(5)
     59 #define	 AACIISR_TXINTR		__BIT(2)
     60 #define	AACIIE			0x10
     61 #define	 AACIIE_TXUIE		__BIT(5)
     62 #define	 AACIIE_TXIE		__BIT(2)
     63 #define	AACISL1RX		0x50
     64 #define	AACISL1TX		0x54
     65 #define	AACISL2RX		0x58
     66 #define	AACISL2TX		0x5c
     67 #define	AACISLFR		0x68
     68 #define	AACISLISTAT		0x6c
     69 #define	AACISLIEN		0x70
     70 #define	AACIINTCLR		0x74
     71 #define	AACIMAINCR		0x78
     72 #define	AACIRESET		0x7c
     73 #define	AACISYNC		0x80
     74 #define	AACIALLINTS		0x84
     75 #define	AACIMAINFR		0x88
     76 #define	AACIDR			0x90
     77 
     78 #define	AACI_FIFO_DEPTH		512
     79 #define	AACI_BLOCK_ALIGN	4
     80 
     81 #define	AACI_READ(sc, reg)			\
     82 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     83 #define	AACI_WRITE(sc, reg, val)		\
     84 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     85 #define	AACI_WRITE_MULTI(sc, reg, val, cnt)	\
     86 	bus_space_write_multi_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val), (cnt))
     87 
     88 static const struct audio_format aaci_format = {
     89 	.mode = AUMODE_PLAY,
     90 	.encoding = AUDIO_ENCODING_SLINEAR_LE,
     91 	.validbits = 16,
     92 	.precision = 16,
     93 	.channels = 2,
     94 	.channel_mask = AUFMT_STEREO,
     95 	.frequency_type = 1,
     96 	.frequency = { 48000 }
     97 };
     98 
     99 static void
    100 aaci_write_data(struct aaci_softc *sc)
    101 {
    102 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    103 
    104 	if (sc->sc_pint == NULL)
    105 		return;
    106 
    107 	while ((AACI_READ(sc, AACISR) & AACISR_TXHE) != 0) {
    108 		const int len = uimin(AACI_FIFO_DEPTH / 2, uimin(sc->sc_pblkresid,
    109 		    (uintptr_t)sc->sc_pend - (uintptr_t)sc->sc_pcur));
    110 		KASSERT((len & 3) == 0);
    111 		AACI_WRITE_MULTI(sc, AACIDR, sc->sc_pcur, len >> 2);
    112 		sc->sc_pcur += (len >> 2);
    113 		if (sc->sc_pcur == sc->sc_pend)
    114 			sc->sc_pcur = sc->sc_pstart;
    115 		sc->sc_pblkresid -= len;
    116 		if (sc->sc_pblkresid == 0) {
    117 			sc->sc_pblkresid = sc->sc_pblksize;
    118 			sc->sc_pint(sc->sc_pintarg);
    119 		}
    120 	}
    121 }
    122 
    123 static int
    124 aaci_query_format(void *priv, audio_format_query_t *afp)
    125 {
    126 
    127 	return audio_query_format(&aaci_format, 1, afp);
    128 }
    129 
    130 static int
    131 aaci_set_format(void *priv, int setmode,
    132     const audio_params_t *play, const audio_params_t *rec,
    133     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    134 {
    135 
    136 	/* We have only one format so nothing to do here. */
    137 	return 0;
    138 }
    139 
    140 static int
    141 aaci_getdev(void *priv, struct audio_device *audiodev)
    142 {
    143 	snprintf(audiodev->name, sizeof(audiodev->name), "ARM");
    144 	snprintf(audiodev->version, sizeof(audiodev->version),
    145 	    "PrimeCell AACI");
    146 	snprintf(audiodev->config, sizeof(audiodev->config), "aaci");
    147 	return 0;
    148 }
    149 
    150 static int
    151 aaci_set_port(void *priv, mixer_ctrl_t *mc)
    152 {
    153 	struct aaci_softc * const sc = priv;
    154 
    155 	return sc->sc_ac97_codec->vtbl->mixer_set_port(sc->sc_ac97_codec, mc);
    156 }
    157 
    158 static int
    159 aaci_get_port(void *priv, mixer_ctrl_t *mc)
    160 {
    161 	struct aaci_softc * const sc = priv;
    162 
    163 	return sc->sc_ac97_codec->vtbl->mixer_get_port(sc->sc_ac97_codec, mc);
    164 }
    165 
    166 static int
    167 aaci_query_devinfo(void *priv, mixer_devinfo_t *di)
    168 {
    169 	struct aaci_softc * const sc = priv;
    170 
    171 	return sc->sc_ac97_codec->vtbl->query_devinfo(sc->sc_ac97_codec, di);
    172 }
    173 
    174 static int
    175 aaci_get_props(void *priv)
    176 {
    177 	return AUDIO_PROP_PLAYBACK;
    178 }
    179 
    180 static int
    181 aaci_trigger_output(void *priv, void *start, void *end, int blksize,
    182     void (*intr)(void *), void *intrarg, const audio_params_t *params)
    183 {
    184 	struct aaci_softc * const sc = priv;
    185 
    186 	sc->sc_pcur = start;
    187 	sc->sc_pstart = start;
    188 	sc->sc_pend = end;
    189 	sc->sc_pblksize = blksize;
    190 	sc->sc_pblkresid = blksize;
    191 	sc->sc_pint = intr;
    192 	sc->sc_pintarg = intrarg;
    193 
    194 	AACI_WRITE(sc, AACIIE, AACIIE_TXIE | AACIIE_TXUIE);
    195 	AACI_WRITE(sc, AACITXCR, AACITXCR_TXFEN | AACITXCR_TXEN |
    196 	    AACITXCR_TXCM | AACITXCR_TSIZE_16 |
    197 	    AACITXCR_TX(AC97_SLOT_PCM_L) | AACITXCR_TX(AC97_SLOT_PCM_R));
    198 
    199 	return 0;
    200 }
    201 
    202 static int
    203 aaci_halt_output(void *priv)
    204 {
    205 	struct aaci_softc * const sc = priv;
    206 
    207 	sc->sc_pint = NULL;
    208 
    209 	AACI_WRITE(sc, AACITXCR, 0);
    210 	AACI_WRITE(sc, AACIIE, 0);
    211 
    212 	return 0;
    213 }
    214 
    215 static int
    216 aaci_round_blocksize(void *priv, int bs, int mode, const audio_params_t *params)
    217 {
    218 	return roundup(bs, AACI_BLOCK_ALIGN);
    219 }
    220 
    221 static void
    222 aaci_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
    223 {
    224 	struct aaci_softc * const sc = priv;
    225 
    226 	*intr = &sc->sc_intr_lock;
    227 	*thread = &sc->sc_lock;
    228 }
    229 
    230 static const struct audio_hw_if aaci_hw_if = {
    231 	.query_format = aaci_query_format,
    232 	.set_format = aaci_set_format,
    233 	.getdev = aaci_getdev,
    234 	.set_port = aaci_set_port,
    235 	.get_port = aaci_get_port,
    236 	.query_devinfo = aaci_query_devinfo,
    237 	.get_props = aaci_get_props,
    238 	.trigger_output = aaci_trigger_output,
    239 	.halt_output = aaci_halt_output,
    240 	.round_blocksize = aaci_round_blocksize,
    241 	.get_locks = aaci_get_locks,
    242 };
    243 
    244 static int
    245 aaci_ac97_attach(void *priv, struct ac97_codec_if *codec)
    246 {
    247 	struct aaci_softc * const sc = priv;
    248 
    249 	sc->sc_ac97_codec = codec;
    250 
    251 	return 0;
    252 }
    253 
    254 static int
    255 aaci_ac97_read(void *priv, uint8_t reg, uint16_t *val)
    256 {
    257 	struct aaci_softc * const sc = priv;
    258 
    259 	AACI_WRITE(sc, AACISL1TX, (reg << 12) | (1 << 19));
    260 
    261 	if (AACI_READ(sc, AACISL1RX) != (reg << 12))
    262 		return 1;
    263 
    264 	*val = AACI_READ(sc, AACISL2RX) >> 4;
    265 	return 0;
    266 }
    267 
    268 static int
    269 aaci_ac97_write(void *priv, uint8_t reg, uint16_t val)
    270 {
    271 	struct aaci_softc * const sc = priv;
    272 
    273 	AACI_WRITE(sc, AACISL2TX, val << 4);
    274 	AACI_WRITE(sc, AACISL1TX, (reg << 12) | (0 << 19));
    275 
    276 	return 0;
    277 }
    278 
    279 void
    280 aaci_attach(struct aaci_softc *sc)
    281 {
    282 	int error;
    283 
    284 	aprint_naive("\n");
    285 	aprint_normal(": Advanced Audio CODEC\n");
    286 
    287 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    288 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    289 
    290 	sc->sc_ac97_host.arg = sc;
    291 	sc->sc_ac97_host.attach = aaci_ac97_attach;
    292 	sc->sc_ac97_host.read = aaci_ac97_read;
    293 	sc->sc_ac97_host.write = aaci_ac97_write;
    294 	error = ac97_attach(&sc->sc_ac97_host, sc->sc_dev, &sc->sc_lock);
    295 	if (error) {
    296 		aprint_error_dev(sc->sc_dev, "couldn't attach codec (%d)\n",
    297 		    error);
    298 		return;
    299 	}
    300 
    301 	sc->sc_audiodev = audio_attach_mi(&aaci_hw_if, sc, sc->sc_dev);
    302 }
    303 
    304 int
    305 aaci_intr(void *priv)
    306 {
    307 	struct aaci_softc * const sc = priv;
    308 	uint32_t isr;
    309 
    310 	if (sc->sc_audiodev == NULL)
    311 		return 0;
    312 
    313 	isr = AACI_READ(sc, AACIISR);
    314 
    315 	if (isr & AACIISR_URINTR)
    316 		AACI_WRITE(sc, AACIINTCLR, AACIISR_URINTR);
    317 
    318 	if (isr & AACIISR_TXINTR) {
    319 		mutex_enter(&sc->sc_intr_lock);
    320 		if (sc->sc_pint == NULL)
    321 			AACI_WRITE(sc, AACIIE, 0);
    322 		aaci_write_data(sc);
    323 		mutex_exit(&sc->sc_intr_lock);
    324 	}
    325 
    326 	return 1;
    327 }
    328