Home | History | Annotate | Line # | Download | only in dev
bwai.c revision 1.2
      1 /* $NetBSD: bwai.c,v 1.2 2024/01/23 21:49:20 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2024 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: bwai.c,v 1.2 2024/01/23 21:49:20 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/cpu.h>
     35 #include <sys/device.h>
     36 #include <sys/kmem.h>
     37 #include <sys/mutex.h>
     38 #include <sys/bitops.h>
     39 
     40 #include <dev/audio/audio_dai.h>
     41 
     42 #include <machine/wii.h>
     43 
     44 #include "mainbus.h"
     45 #include "avenc.h"
     46 #include "bwai.h"
     47 
     48 #define	AI_CONTROL		0x00
     49 #define	 AI_CONTROL_RATE	__BIT(6)
     50 #define	 AI_CONTROL_SCRESET	__BIT(5)
     51 #define	 AI_CONTROL_AIINTVLD	__BIT(4)
     52 #define	 AI_CONTROL_AIINT	__BIT(3)
     53 #define	 AI_CONTROL_AIINTMSK	__BIT(2)
     54 #define	 AI_CONTROL_AFR		__BIT(1)
     55 #define	 AI_CONTROL_PSTAT	__BIT(0)
     56 #define AI_AIIT			0x0c
     57 
     58 struct bwai_softc {
     59 	device_t		sc_dev;
     60 	bus_space_tag_t		sc_bst;
     61 	bus_space_handle_t	sc_bsh;
     62 	int			sc_irq;
     63 
     64 	struct audio_dai_device	sc_dai;
     65 
     66 	void			(*sc_intr)(void *);
     67 	void			*sc_intrarg;
     68 
     69 	kmutex_t		*sc_intr_lock;
     70 };
     71 
     72 enum bwai_mixer_ctrl {
     73 	BWAI_OUTPUT_CLASS,
     74 	BWAI_INPUT_CLASS,
     75 
     76 	BWAI_OUTPUT_MASTER_VOLUME,
     77 	BWAI_INPUT_DAC_VOLUME,
     78 
     79 	BWAI_MIXER_CTRL_LAST
     80 };
     81 
     82 #define	RD4(sc, reg)			\
     83 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     84 #define	WR4(sc, reg, val)		\
     85 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     86 
     87 static int
     88 bwai_intr(void *priv)
     89 {
     90 	struct bwai_softc * const sc = priv;
     91 	uint32_t val;
     92 
     93 	val = RD4(sc, AI_CONTROL);
     94 	if ((val & AI_CONTROL_AIINT) != 0) {
     95 		WR4(sc, AI_CONTROL, val | AI_CONTROL_SCRESET);
     96 
     97 		mutex_enter(sc->sc_intr_lock);
     98 		if (sc->sc_intr) {
     99 			sc->sc_intr(sc->sc_intrarg);
    100 		}
    101 		mutex_exit(sc->sc_intr_lock);
    102 	}
    103 
    104 	return 1;
    105 }
    106 
    107 audio_dai_tag_t
    108 bwai_dsp_init(kmutex_t *intr_lock)
    109 {
    110 	struct bwai_softc *sc;
    111 	device_t dev;
    112 
    113 	dev = device_find_by_driver_unit("bwai", 0);
    114 	if (dev == NULL) {
    115 		return NULL;
    116 	}
    117 	sc = device_private(dev);
    118 
    119 	sc->sc_intr_lock = intr_lock;
    120 
    121 	intr_establish(sc->sc_irq, IST_LEVEL, IPL_AUDIO, bwai_intr, sc);
    122 
    123 	return &sc->sc_dai;
    124 }
    125 
    126 static int
    127 bwai_set_port(void *priv, mixer_ctrl_t *mc)
    128 {
    129 	if (mc->dev != BWAI_OUTPUT_MASTER_VOLUME &&
    130 	    mc->dev != BWAI_INPUT_DAC_VOLUME) {
    131 		return ENXIO;
    132 	}
    133 
    134 	avenc_set_volume(mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT],
    135 			 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
    136 
    137 	return 0;
    138 }
    139 
    140 static int
    141 bwai_get_port(void *priv, mixer_ctrl_t *mc)
    142 {
    143 	if (mc->dev != BWAI_OUTPUT_MASTER_VOLUME &&
    144 	    mc->dev != BWAI_INPUT_DAC_VOLUME) {
    145 		return ENXIO;
    146 	}
    147 
    148 	avenc_get_volume(&mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT],
    149 			 &mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
    150 
    151 	return 0;
    152 }
    153 
    154 static int
    155 bwai_query_devinfo(void *priv, mixer_devinfo_t *di)
    156 {
    157 	switch (di->index) {
    158 	case BWAI_OUTPUT_CLASS:
    159 		di->mixer_class = di->index;
    160 		strcpy(di->label.name, AudioCoutputs);
    161 		di->type = AUDIO_MIXER_CLASS;
    162 		di->next = di->prev = AUDIO_MIXER_LAST;
    163 		return 0;
    164 
    165 	case BWAI_INPUT_CLASS:
    166 		di->mixer_class = di->index;
    167 		strcpy(di->label.name, AudioCinputs);
    168 		di->type = AUDIO_MIXER_CLASS;
    169 		di->next = di->prev = AUDIO_MIXER_LAST;
    170 		return 0;
    171 
    172 	case BWAI_OUTPUT_MASTER_VOLUME:
    173 		di->mixer_class = BWAI_OUTPUT_CLASS;
    174 		strcpy(di->label.name, AudioNmaster);
    175 		di->un.v.delta = 1;
    176 		di->type = AUDIO_MIXER_VALUE;
    177 		di->next = di->prev = AUDIO_MIXER_LAST;
    178 		di->un.v.num_channels = 2;
    179 		strcpy(di->un.v.units.name, AudioNvolume);
    180 		return 0;
    181 
    182 	case BWAI_INPUT_DAC_VOLUME:
    183 		di->mixer_class = BWAI_INPUT_CLASS;
    184 		strcpy(di->label.name, AudioNdac);
    185 		di->un.v.delta = 1;
    186 		di->type = AUDIO_MIXER_VALUE;
    187 		di->next = di->prev = AUDIO_MIXER_LAST;
    188 		di->un.v.num_channels = 2;
    189 		strcpy(di->un.v.units.name, AudioNvolume);
    190 		return 0;
    191 	}
    192 
    193 	return ENXIO;
    194 }
    195 
    196 static int
    197 bwai_set_format(void *priv, int setmode,
    198     const audio_params_t *play, const audio_params_t *rec,
    199     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    200 {
    201 	return 0;
    202 }
    203 
    204 static int
    205 bwai_trigger_output(void *priv, void *start, void *end, int blksize,
    206     void (*intr)(void *), void *intrarg, const audio_params_t *params)
    207 {
    208 	struct bwai_softc * const sc = priv;
    209 	uint32_t val;
    210 
    211 	sc->sc_intr = intr;
    212 	sc->sc_intrarg = intrarg;
    213 
    214 	val = RD4(sc, AI_CONTROL);
    215 	if ((val & AI_CONTROL_PSTAT) != 0) {
    216 		WR4(sc, AI_CONTROL, 0);
    217 	}
    218 
    219 	WR4(sc, AI_AIIT, blksize / 4);
    220 
    221 	val = AI_CONTROL_SCRESET |
    222 	      AI_CONTROL_AIINT |
    223 	      AI_CONTROL_AIINTMSK |
    224 	      AI_CONTROL_AFR;
    225 	WR4(sc, AI_CONTROL, val);
    226 
    227 	val |= AI_CONTROL_PSTAT;
    228 	WR4(sc, AI_CONTROL, val);
    229 
    230 	return 0;
    231 }
    232 
    233 static int
    234 bwai_halt_output(void *priv)
    235 {
    236 	struct bwai_softc * const sc = priv;
    237 
    238 	WR4(sc, AI_CONTROL, 0);
    239 
    240 	sc->sc_intr = NULL;
    241 	sc->sc_intrarg = NULL;
    242 
    243 	return 0;
    244 }
    245 
    246 static const struct audio_hw_if bwai_hw_if = {
    247 	.set_port = bwai_set_port,
    248 	.get_port = bwai_get_port,
    249 	.query_devinfo = bwai_query_devinfo,
    250 	.set_format = bwai_set_format,
    251 	.trigger_output = bwai_trigger_output,
    252 	.halt_output = bwai_halt_output,
    253 };
    254 
    255 static int
    256 bwai_match(device_t parent, cfdata_t cf, void *aux)
    257 {
    258 	struct mainbus_attach_args * const maa = aux;
    259 
    260 	return strcmp(maa->maa_name, "bwai") == 0;
    261 }
    262 
    263 static void
    264 bwai_attach(device_t parent, device_t self, void *aux)
    265 {
    266 	struct bwai_softc * const sc = device_private(self);
    267 	struct mainbus_attach_args * const maa = aux;
    268 
    269 	sc->sc_dev = self;
    270 	sc->sc_bst = maa->maa_bst;
    271 	if (bus_space_map(sc->sc_bst, maa->maa_addr, AI_SIZE, 0,
    272 	    &sc->sc_bsh) != 0) {
    273 		aprint_error(": couldn't map registers\n");
    274 		return;
    275 	}
    276 	sc->sc_irq = maa->maa_irq;
    277 
    278 	aprint_naive("\n");
    279 	aprint_normal(": Audio Interface\n");
    280 
    281 	sc->sc_dai.dai_hw_if = &bwai_hw_if;
    282 	sc->sc_dai.dai_dev = self;
    283 	sc->sc_dai.dai_priv = sc;
    284 }
    285 
    286 CFATTACH_DECL_NEW(bwai, sizeof(struct bwai_softc),
    287     bwai_match, bwai_attach, NULL, NULL);
    288