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