1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2020 Oleksandr Tymoshenko <gonzo@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/systm.h> 30 #include <sys/bus.h> 31 #include <sys/kernel.h> 32 #include <sys/lock.h> 33 #include <sys/module.h> 34 #include <sys/mutex.h> 35 #include <sys/rman.h> 36 #include <sys/resource.h> 37 #include <machine/bus.h> 38 39 #include <dev/ofw/ofw_bus.h> 40 #include <dev/ofw/ofw_bus_subr.h> 41 42 #include <dev/extres/regulator/regulator.h> 43 #include <dev/gpio/gpiobusvar.h> 44 45 #include "opt_snd.h" 46 #include <dev/sound/pcm/sound.h> 47 #include <dev/sound/fdt/audio_dai.h> 48 #include "audio_dai_if.h" 49 50 static struct ofw_compat_data compat_data[] = { 51 { "simple-audio-amplifier", 1}, 52 { NULL, 0} 53 }; 54 55 struct simple_amp_softc { 56 device_t dev; 57 regulator_t supply_vcc; 58 gpio_pin_t gpio_enable; 59 bool gpio_is_valid; 60 }; 61 62 static int simple_amp_probe(device_t dev); 63 static int simple_amp_attach(device_t dev); 64 static int simple_amp_detach(device_t dev); 65 66 static int 67 simple_amp_probe(device_t dev) 68 { 69 if (!ofw_bus_status_okay(dev)) 70 return (ENXIO); 71 72 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) 73 return (ENXIO); 74 75 device_set_desc(dev, "Simple Amplifier"); 76 return (BUS_PROBE_DEFAULT); 77 } 78 79 static int 80 simple_amp_attach(device_t dev) 81 { 82 struct simple_amp_softc *sc; 83 phandle_t node; 84 int error; 85 86 sc = device_get_softc(dev); 87 sc->dev = dev; 88 node = ofw_bus_get_node(dev); 89 90 error = gpio_pin_get_by_ofw_property(dev, node, 91 "enable-gpios", &sc->gpio_enable); 92 if (error != 0) 93 sc->gpio_is_valid = false; 94 else 95 sc->gpio_is_valid = true; 96 97 error = regulator_get_by_ofw_property(dev, 0, "VCC-supply", 98 &sc->supply_vcc); 99 if (error != 0) 100 device_printf(dev, "no VCC supply"); 101 102 OF_device_register_xref(OF_xref_from_node(node), dev); 103 104 return (0); 105 } 106 107 static int 108 simple_amp_detach(device_t dev) 109 { 110 111 return (0); 112 } 113 114 static int 115 simple_amp_dai_init(device_t dev, uint32_t format) 116 { 117 118 return (0); 119 } 120 121 static int 122 simple_amp_dai_trigger(device_t dev, int go, int pcm_dir) 123 { 124 struct simple_amp_softc *sc; 125 int error; 126 127 if ((pcm_dir != PCMDIR_PLAY) && (pcm_dir != PCMDIR_REC)) 128 return (EINVAL); 129 130 sc = device_get_softc(dev); 131 error = 0; 132 switch (go) { 133 case PCMTRIG_START: 134 if (sc->supply_vcc != NULL) { 135 error = regulator_enable(sc->supply_vcc); 136 if (error != 0) { 137 device_printf(sc->dev, 138 "could not enable 'VCC' regulator\n"); 139 break; 140 } 141 } 142 143 if (sc->gpio_is_valid) { 144 error = gpio_pin_set_active(sc->gpio_enable, 1); 145 if (error != 0) { 146 device_printf(sc->dev, 147 "could not set 'gpio-enable' gpio\n"); 148 break; 149 } 150 } 151 152 break; 153 154 case PCMTRIG_STOP: 155 case PCMTRIG_ABORT: 156 if (sc->gpio_is_valid) { 157 error = gpio_pin_set_active(sc->gpio_enable, 0); 158 if (error != 0) { 159 device_printf(sc->dev, 160 "could not clear 'gpio-enable' gpio\n"); 161 break; 162 } 163 } 164 165 if (sc->supply_vcc != NULL) { 166 error = regulator_disable(sc->supply_vcc); 167 if (error != 0) { 168 device_printf(sc->dev, 169 "could not disable 'VCC' regulator\n"); 170 break; 171 } 172 } 173 174 break; 175 } 176 177 return (error); 178 } 179 180 static device_method_t simple_amp_methods[] = { 181 /* Device interface */ 182 DEVMETHOD(device_probe, simple_amp_probe), 183 DEVMETHOD(device_attach, simple_amp_attach), 184 DEVMETHOD(device_detach, simple_amp_detach), 185 186 DEVMETHOD(audio_dai_init, simple_amp_dai_init), 187 DEVMETHOD(audio_dai_trigger, simple_amp_dai_trigger), 188 189 DEVMETHOD_END 190 }; 191 192 static driver_t simple_amp_driver = { 193 "simpleamp", 194 simple_amp_methods, 195 sizeof(struct simple_amp_softc), 196 }; 197 198 DRIVER_MODULE(simple_amp, simplebus, simple_amp_driver, 0, 0); 199 SIMPLEBUS_PNP_INFO(compat_data); 200