xref: /openbsd-src/sys/dev/fdt/simpleamp.c (revision 471aeecfc619bc9b69519928152daf993376c2a1)
1*471aeecfSnaddy /*	$OpenBSD: simpleamp.c,v 1.4 2022/04/06 18:59:28 naddy Exp $	*/
2e07adabeSpatrick /*
3e07adabeSpatrick  * Copyright (c) 2020 Patrick Wildt <patrick@blueri.se>
4e07adabeSpatrick  *
5e07adabeSpatrick  * Permission to use, copy, modify, and distribute this software for any
6e07adabeSpatrick  * purpose with or without fee is hereby granted, provided that the above
7e07adabeSpatrick  * copyright notice and this permission notice appear in all copies.
8e07adabeSpatrick  *
9e07adabeSpatrick  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10e07adabeSpatrick  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11e07adabeSpatrick  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12e07adabeSpatrick  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13e07adabeSpatrick  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14e07adabeSpatrick  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15e07adabeSpatrick  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16e07adabeSpatrick  */
17e07adabeSpatrick 
18e07adabeSpatrick #include <sys/param.h>
19e07adabeSpatrick #include <sys/systm.h>
20e07adabeSpatrick #include <sys/device.h>
21e07adabeSpatrick #include <sys/malloc.h>
22e07adabeSpatrick 
23e07adabeSpatrick #include <machine/fdt.h>
24e07adabeSpatrick 
25e07adabeSpatrick #include <dev/ofw/openfirm.h>
26e07adabeSpatrick #include <dev/ofw/ofw_gpio.h>
27e07adabeSpatrick #include <dev/ofw/ofw_misc.h>
28e07adabeSpatrick #include <dev/ofw/ofw_regulator.h>
29e07adabeSpatrick 
30e07adabeSpatrick #include <sys/audioio.h>
31e07adabeSpatrick #include <dev/audio_if.h>
32e07adabeSpatrick #include <dev/midi_if.h>
33e07adabeSpatrick 
34e07adabeSpatrick int simpleamp_match(struct device *, void *, void *);
35e07adabeSpatrick void simpleamp_attach(struct device *, struct device *, void *);
36e07adabeSpatrick 
37e07adabeSpatrick int simpleamp_open(void *, int);
38e07adabeSpatrick void simpleamp_close(void *);
39e07adabeSpatrick 
40e07adabeSpatrick struct simpleamp_softc {
41e07adabeSpatrick 	struct device		sc_dev;
42e07adabeSpatrick 	struct dai_device	sc_dai;
43e07adabeSpatrick 
44e07adabeSpatrick 	uint32_t		*sc_gpio;
4536defc4bSjsg 	int			sc_gpiolen;
46e07adabeSpatrick 	uint32_t		sc_vcc;
47e07adabeSpatrick };
48e07adabeSpatrick 
490d6a2fdeSmiod const struct audio_hw_if simpleamp_hw_if = {
50e07adabeSpatrick 	.open = simpleamp_open,
51e07adabeSpatrick 	.close = simpleamp_close,
52e07adabeSpatrick };
53e07adabeSpatrick 
54*471aeecfSnaddy const struct cfattach simpleamp_ca = {
55e07adabeSpatrick 	sizeof(struct simpleamp_softc), simpleamp_match, simpleamp_attach
56e07adabeSpatrick };
57e07adabeSpatrick 
58e07adabeSpatrick struct cfdriver simpleamp_cd = {
59e07adabeSpatrick 	NULL, "simpleamp", DV_DULL
60e07adabeSpatrick };
61e07adabeSpatrick 
62e07adabeSpatrick int
simpleamp_match(struct device * parent,void * match,void * aux)63e07adabeSpatrick simpleamp_match(struct device *parent, void *match, void *aux)
64e07adabeSpatrick {
65e07adabeSpatrick 	struct fdt_attach_args *faa = aux;
66e07adabeSpatrick 
67e07adabeSpatrick 	return OF_is_compatible(faa->fa_node, "simple-audio-amplifier");
68e07adabeSpatrick }
69e07adabeSpatrick 
70e07adabeSpatrick void
simpleamp_attach(struct device * parent,struct device * self,void * aux)71e07adabeSpatrick simpleamp_attach(struct device *parent, struct device *self, void *aux)
72e07adabeSpatrick {
73e07adabeSpatrick 	struct simpleamp_softc *sc = (struct simpleamp_softc *)self;
74e07adabeSpatrick 	struct fdt_attach_args *faa = aux;
75e07adabeSpatrick 
76e07adabeSpatrick 	sc->sc_gpiolen = OF_getproplen(faa->fa_node, "enable-gpios");
77e07adabeSpatrick 	if (sc->sc_gpiolen > 0) {
78e07adabeSpatrick 		sc->sc_gpio = malloc(sc->sc_gpiolen, M_DEVBUF, M_WAITOK);
79e07adabeSpatrick 		OF_getpropintarray(faa->fa_node, "enable-gpios",
80e07adabeSpatrick 		    sc->sc_gpio, sc->sc_gpiolen);
81e07adabeSpatrick 		gpio_controller_config_pin(sc->sc_gpio, GPIO_CONFIG_OUTPUT);
82e07adabeSpatrick 	}
83e07adabeSpatrick 	sc->sc_vcc = OF_getpropint(faa->fa_node, "VCC-supply", 0);
84e07adabeSpatrick 
85e07adabeSpatrick 	printf("\n");
86e07adabeSpatrick 
87e07adabeSpatrick 	sc->sc_dai.dd_node = faa->fa_node;
88e07adabeSpatrick 	sc->sc_dai.dd_cookie = sc;
89e07adabeSpatrick 	sc->sc_dai.dd_hw_if = &simpleamp_hw_if;
90e07adabeSpatrick 	dai_register(&sc->sc_dai);
91e07adabeSpatrick }
92e07adabeSpatrick 
93e07adabeSpatrick int
simpleamp_open(void * cookie,int flags)94e07adabeSpatrick simpleamp_open(void *cookie, int flags)
95e07adabeSpatrick {
96e07adabeSpatrick 	struct simpleamp_softc *sc = cookie;
97e07adabeSpatrick 
98e07adabeSpatrick 	if (sc->sc_gpio)
99e07adabeSpatrick 		gpio_controller_set_pin(sc->sc_gpio, 1);
100e07adabeSpatrick 	if (sc->sc_vcc)
101e07adabeSpatrick 		regulator_enable(sc->sc_vcc);
102e07adabeSpatrick 
103e07adabeSpatrick 	return 0;
104e07adabeSpatrick }
105e07adabeSpatrick 
106e07adabeSpatrick void
simpleamp_close(void * cookie)107e07adabeSpatrick simpleamp_close(void *cookie)
108e07adabeSpatrick {
109e07adabeSpatrick 	struct simpleamp_softc *sc = cookie;
110e07adabeSpatrick 
111e07adabeSpatrick 	if (sc->sc_gpio)
112e07adabeSpatrick 		gpio_controller_set_pin(sc->sc_gpio, 0);
113e07adabeSpatrick 	if (sc->sc_vcc)
114e07adabeSpatrick 		regulator_disable(sc->sc_vcc);
115e07adabeSpatrick }
116