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