Thu Jan 2 00:57:10 2020 UTC ()
Add driver for simple-audio-amplifier binding


(jmcneill)
diff -r1.49 -r1.50 src/sys/dev/fdt/files.fdt
diff -r0 -r1.1 src/sys/dev/fdt/simple_amplifier.c

cvs diff -r1.49 -r1.50 src/sys/dev/fdt/files.fdt (expand / switch to unified diff)

--- src/sys/dev/fdt/files.fdt 2019/12/19 00:35:01 1.49
+++ src/sys/dev/fdt/files.fdt 2020/01/02 00:57:10 1.50
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: files.fdt,v 1.49 2019/12/19 00:35:01 jakllsch Exp $ 1# $NetBSD: files.fdt,v 1.50 2020/01/02 00:57:10 jmcneill Exp $
2 2
3include "external/bsd/libfdt/conf/files.libfdt" 3include "external/bsd/libfdt/conf/files.libfdt"
4 4
5defflag opt_fdt.h FDTBASE : libfdt, ofw_subr 5defflag opt_fdt.h FDTBASE : libfdt, ofw_subr
6defflag opt_fdt.h FDT: FDTBASE 6defflag opt_fdt.h FDT: FDTBASE
7 7
8define fdt { [pass = 10] } : clk, pwm 8define fdt { [pass = 10] } : clk, pwm
9 9
10device simplebus { } : fdt 10device simplebus { } : fdt
11attach simplebus at fdt 11attach simplebus at fdt
12file dev/fdt/fdtbus.c fdt 12file dev/fdt/fdtbus.c fdt
13 13
14device fregulator 14device fregulator
@@ -155,13 +155,19 @@ file dev/fdt/dwiic_fdt.c dwiic_fdt @@ -155,13 +155,19 @@ file dev/fdt/dwiic_fdt.c dwiic_fdt
155 155
156# AMD Cryptographic Coprocessor 156# AMD Cryptographic Coprocessor
157attach amdccp at fdt with amdccp_fdt 157attach amdccp at fdt with amdccp_fdt
158file dev/fdt/amdccp_fdt.c amdccp_fdt 158file dev/fdt/amdccp_fdt.c amdccp_fdt
159 159
160# Arasan SDHCI controller 160# Arasan SDHCI controller
161attach sdhc at fdt with arasan_sdhc_fdt 161attach sdhc at fdt with arasan_sdhc_fdt
162file dev/fdt/arasan_sdhc_fdt.c arasan_sdhc_fdt 162file dev/fdt/arasan_sdhc_fdt.c arasan_sdhc_fdt
163 163
164# Generic USB PHY 164# Generic USB PHY
165device usbnopphy 165device usbnopphy
166attach usbnopphy at fdt 166attach usbnopphy at fdt
167file dev/fdt/usbnopphy.c usbnopphy 167file dev/fdt/usbnopphy.c usbnopphy
 168
 169# Simple Amplifier Audio Driver
 170device simpleamp
 171attach simpleamp at fdt
 172file dev/fdt/simple_amplifier.c simpleamp
 173

File Added: src/sys/dev/fdt/simple_amplifier.c
/* $NetBSD: simple_amplifier.c,v 1.1 2020/01/02 00:57:09 jmcneill Exp $ */

/*-
 * Copyright (c) 2020 Jared McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: simple_amplifier.c,v 1.1 2020/01/02 00:57:09 jmcneill Exp $");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/kmem.h>

#include <dev/audio/audio_dai.h>

#include <dev/fdt/fdtvar.h>

struct simple_amplifier_softc {
	device_t		sc_dev;
	struct audio_dai_device	sc_dai;
	struct fdtbus_gpio_pin	*sc_pin;
	struct fdtbus_regulator	*sc_vcc;
};

static int	simple_amplifier_match(device_t, cfdata_t, void *);
static void	simple_amplifier_attach(device_t, device_t, void *);

static const char *compatible[] = {
	"simple-audio-amplifier",
	NULL
};

CFATTACH_DECL_NEW(simpleamp, sizeof(struct simple_amplifier_softc),
	simple_amplifier_match, simple_amplifier_attach, NULL, NULL);

static int
simple_amplifier_open(void *priv, int flags)
{
	struct simple_amplifier_softc * const sc = priv;
	int error;

	if (sc->sc_pin != NULL)
		fdtbus_gpio_write(sc->sc_pin, 1);

	if (sc->sc_vcc != NULL) {
		error = fdtbus_regulator_enable(sc->sc_vcc);
		if (error != 0)
			aprint_error_dev(sc->sc_dev, "couldn't enable power supply\n");
	}

	return 0;
}

static void
simple_amplifier_close(void *priv)
{
	struct simple_amplifier_softc * const sc = priv;

	if (sc->sc_pin != NULL)
		fdtbus_gpio_write(sc->sc_pin, 0);

	if (sc->sc_vcc != NULL)
		(void)fdtbus_regulator_disable(sc->sc_vcc);
}

static const struct audio_hw_if simple_amplifier_hw_if = {
	.open = simple_amplifier_open,
	.close = simple_amplifier_close,
};

static audio_dai_tag_t
simple_amplifier_dai_get_tag(device_t dev, const void *data, size_t len)
{
	struct simple_amplifier_softc * const sc = device_private(dev);

	if (len != 4)
		return NULL;

	return &sc->sc_dai;
}

static struct fdtbus_dai_controller_func simple_amplifier_dai_funcs = {
	.get_tag = simple_amplifier_dai_get_tag
};

static int
simple_amplifier_match(device_t parent, cfdata_t cf, void *aux)
{
	struct fdt_attach_args * const faa = aux;

	return of_match_compatible(faa->faa_phandle, compatible);
}

static void
simple_amplifier_attach(device_t parent, device_t self, void *aux)
{
	struct simple_amplifier_softc * const sc = device_private(self);
	struct fdt_attach_args * const faa = aux;
	const int phandle = faa->faa_phandle;

	sc->sc_dev = self;
	sc->sc_pin = fdtbus_gpio_acquire(phandle, "enable-gpios", GPIO_PIN_OUTPUT);
	sc->sc_vcc = fdtbus_regulator_acquire(phandle, "VCC-supply");

	aprint_naive("\n");
	aprint_normal(": Simple Amplifier\n");

	sc->sc_dai.dai_hw_if = &simple_amplifier_hw_if;
	sc->sc_dai.dai_dev = self;
	sc->sc_dai.dai_priv = sc;
	fdtbus_register_dai_controller(self, phandle, &simple_amplifier_dai_funcs);
}