2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
41 #include <sys/resource.h>
42 #include <machine/bus.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/extres/regulator/regulator.h>
48 #include <dev/gpio/gpiobusvar.h>
51 #include <dev/sound/pcm/sound.h>
52 #include <dev/sound/fdt/audio_dai.h>
53 #include "audio_dai_if.h"
55 static struct ofw_compat_data compat_data[] = {
56 { "simple-audio-amplifier", 1},
60 struct simple_amp_softc {
62 regulator_t supply_vcc;
63 gpio_pin_t gpio_enable;
67 static int simple_amp_probe(device_t dev);
68 static int simple_amp_attach(device_t dev);
69 static int simple_amp_detach(device_t dev);
72 simple_amp_probe(device_t dev)
74 if (!ofw_bus_status_okay(dev))
77 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
80 device_set_desc(dev, "Simple Amplifier");
81 return (BUS_PROBE_DEFAULT);
85 simple_amp_attach(device_t dev)
87 struct simple_amp_softc *sc;
91 sc = device_get_softc(dev);
93 node = ofw_bus_get_node(dev);
95 error = gpio_pin_get_by_ofw_property(dev, node,
96 "enable-gpios", &sc->gpio_enable);
98 sc->gpio_is_valid = false;
100 sc->gpio_is_valid = true;
102 error = regulator_get_by_ofw_property(dev, 0, "VCC-supply",
105 device_printf(dev, "no VCC supply");
107 OF_device_register_xref(OF_xref_from_node(node), dev);
113 simple_amp_detach(device_t dev)
120 simple_amp_dai_init(device_t dev, uint32_t format)
127 simple_amp_dai_trigger(device_t dev, int go, int pcm_dir)
129 struct simple_amp_softc *sc;
132 if ((pcm_dir != PCMDIR_PLAY) && (pcm_dir != PCMDIR_REC))
135 sc = device_get_softc(dev);
139 if (sc->supply_vcc != NULL) {
140 error = regulator_enable(sc->supply_vcc);
142 device_printf(sc->dev,
143 "could not enable 'VCC' regulator\n");
148 if (sc->gpio_is_valid) {
149 error = gpio_pin_set_active(sc->gpio_enable, 1);
151 device_printf(sc->dev,
152 "could not set 'gpio-enable' gpio\n");
161 if (sc->gpio_is_valid) {
162 error = gpio_pin_set_active(sc->gpio_enable, 0);
164 device_printf(sc->dev,
165 "could not clear 'gpio-enable' gpio\n");
170 if (sc->supply_vcc != NULL) {
171 error = regulator_disable(sc->supply_vcc);
173 device_printf(sc->dev,
174 "could not disable 'VCC' regulator\n");
185 static device_method_t simple_amp_methods[] = {
186 /* Device interface */
187 DEVMETHOD(device_probe, simple_amp_probe),
188 DEVMETHOD(device_attach, simple_amp_attach),
189 DEVMETHOD(device_detach, simple_amp_detach),
191 DEVMETHOD(audio_dai_init, simple_amp_dai_init),
192 DEVMETHOD(audio_dai_trigger, simple_amp_dai_trigger),
197 static driver_t simple_amp_driver = {
200 sizeof(struct simple_amp_softc),
203 static devclass_t simple_amp_devclass;
205 DRIVER_MODULE(simple_amp, simplebus, simple_amp_driver, simple_amp_devclass, 0, 0);
206 SIMPLEBUS_PNP_INFO(compat_data);