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