simple_amplifier.c revision 1.1
11.1Sjmcneill/* $NetBSD: simple_amplifier.c,v 1.1 2020/01/02 00:57:09 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2020 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: simple_amplifier.c,v 1.1 2020/01/02 00:57:09 jmcneill Exp $");
311.1Sjmcneill
321.1Sjmcneill#include <sys/param.h>
331.1Sjmcneill#include <sys/bus.h>
341.1Sjmcneill#include <sys/device.h>
351.1Sjmcneill#include <sys/systm.h>
361.1Sjmcneill#include <sys/sysctl.h>
371.1Sjmcneill#include <sys/kmem.h>
381.1Sjmcneill
391.1Sjmcneill#include <dev/audio/audio_dai.h>
401.1Sjmcneill
411.1Sjmcneill#include <dev/fdt/fdtvar.h>
421.1Sjmcneill
431.1Sjmcneillstruct simple_amplifier_softc {
441.1Sjmcneill	device_t		sc_dev;
451.1Sjmcneill	struct audio_dai_device	sc_dai;
461.1Sjmcneill	struct fdtbus_gpio_pin	*sc_pin;
471.1Sjmcneill	struct fdtbus_regulator	*sc_vcc;
481.1Sjmcneill};
491.1Sjmcneill
501.1Sjmcneillstatic int	simple_amplifier_match(device_t, cfdata_t, void *);
511.1Sjmcneillstatic void	simple_amplifier_attach(device_t, device_t, void *);
521.1Sjmcneill
531.1Sjmcneillstatic const char *compatible[] = {
541.1Sjmcneill	"simple-audio-amplifier",
551.1Sjmcneill	NULL
561.1Sjmcneill};
571.1Sjmcneill
581.1SjmcneillCFATTACH_DECL_NEW(simpleamp, sizeof(struct simple_amplifier_softc),
591.1Sjmcneill	simple_amplifier_match, simple_amplifier_attach, NULL, NULL);
601.1Sjmcneill
611.1Sjmcneillstatic int
621.1Sjmcneillsimple_amplifier_open(void *priv, int flags)
631.1Sjmcneill{
641.1Sjmcneill	struct simple_amplifier_softc * const sc = priv;
651.1Sjmcneill	int error;
661.1Sjmcneill
671.1Sjmcneill	if (sc->sc_pin != NULL)
681.1Sjmcneill		fdtbus_gpio_write(sc->sc_pin, 1);
691.1Sjmcneill
701.1Sjmcneill	if (sc->sc_vcc != NULL) {
711.1Sjmcneill		error = fdtbus_regulator_enable(sc->sc_vcc);
721.1Sjmcneill		if (error != 0)
731.1Sjmcneill			aprint_error_dev(sc->sc_dev, "couldn't enable power supply\n");
741.1Sjmcneill	}
751.1Sjmcneill
761.1Sjmcneill	return 0;
771.1Sjmcneill}
781.1Sjmcneill
791.1Sjmcneillstatic void
801.1Sjmcneillsimple_amplifier_close(void *priv)
811.1Sjmcneill{
821.1Sjmcneill	struct simple_amplifier_softc * const sc = priv;
831.1Sjmcneill
841.1Sjmcneill	if (sc->sc_pin != NULL)
851.1Sjmcneill		fdtbus_gpio_write(sc->sc_pin, 0);
861.1Sjmcneill
871.1Sjmcneill	if (sc->sc_vcc != NULL)
881.1Sjmcneill		(void)fdtbus_regulator_disable(sc->sc_vcc);
891.1Sjmcneill}
901.1Sjmcneill
911.1Sjmcneillstatic const struct audio_hw_if simple_amplifier_hw_if = {
921.1Sjmcneill	.open = simple_amplifier_open,
931.1Sjmcneill	.close = simple_amplifier_close,
941.1Sjmcneill};
951.1Sjmcneill
961.1Sjmcneillstatic audio_dai_tag_t
971.1Sjmcneillsimple_amplifier_dai_get_tag(device_t dev, const void *data, size_t len)
981.1Sjmcneill{
991.1Sjmcneill	struct simple_amplifier_softc * const sc = device_private(dev);
1001.1Sjmcneill
1011.1Sjmcneill	if (len != 4)
1021.1Sjmcneill		return NULL;
1031.1Sjmcneill
1041.1Sjmcneill	return &sc->sc_dai;
1051.1Sjmcneill}
1061.1Sjmcneill
1071.1Sjmcneillstatic struct fdtbus_dai_controller_func simple_amplifier_dai_funcs = {
1081.1Sjmcneill	.get_tag = simple_amplifier_dai_get_tag
1091.1Sjmcneill};
1101.1Sjmcneill
1111.1Sjmcneillstatic int
1121.1Sjmcneillsimple_amplifier_match(device_t parent, cfdata_t cf, void *aux)
1131.1Sjmcneill{
1141.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1151.1Sjmcneill
1161.1Sjmcneill	return of_match_compatible(faa->faa_phandle, compatible);
1171.1Sjmcneill}
1181.1Sjmcneill
1191.1Sjmcneillstatic void
1201.1Sjmcneillsimple_amplifier_attach(device_t parent, device_t self, void *aux)
1211.1Sjmcneill{
1221.1Sjmcneill	struct simple_amplifier_softc * const sc = device_private(self);
1231.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1241.1Sjmcneill	const int phandle = faa->faa_phandle;
1251.1Sjmcneill
1261.1Sjmcneill	sc->sc_dev = self;
1271.1Sjmcneill	sc->sc_pin = fdtbus_gpio_acquire(phandle, "enable-gpios", GPIO_PIN_OUTPUT);
1281.1Sjmcneill	sc->sc_vcc = fdtbus_regulator_acquire(phandle, "VCC-supply");
1291.1Sjmcneill
1301.1Sjmcneill	aprint_naive("\n");
1311.1Sjmcneill	aprint_normal(": Simple Amplifier\n");
1321.1Sjmcneill
1331.1Sjmcneill	sc->sc_dai.dai_hw_if = &simple_amplifier_hw_if;
1341.1Sjmcneill	sc->sc_dai.dai_dev = self;
1351.1Sjmcneill	sc->sc_dai.dai_priv = sc;
1361.1Sjmcneill	fdtbus_register_dai_controller(self, phandle, &simple_amplifier_dai_funcs);
1371.1Sjmcneill}
138