xref: /netbsd-src/sys/dev/fdt/fixedregulator.c (revision 3feacbccbd4c8b6fd5f19c8e34474c4c3f8923d7)
1 /* $NetBSD: fixedregulator.c,v 1.3 2015/12/22 22:19:07 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fixedregulator.c,v 1.3 2015/12/22 22:19:07 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/kmem.h>
36 #include <sys/bus.h>
37 #include <sys/gpio.h>
38 
39 #include <dev/fdt/fdtvar.h>
40 
41 static int	fixedregulator_match(device_t, cfdata_t, void *);
42 static void	fixedregulator_attach(device_t, device_t, void *);
43 
44 static int	fixedregulator_acquire(device_t);
45 static void 	fixedregulator_release(device_t);
46 static int	fixedregulator_enable(device_t, bool);
47 
48 struct fdtbus_regulator_controller_func fixedregulator_funcs = {
49 	.acquire = fixedregulator_acquire,
50 	.release = fixedregulator_release,
51 	.enable = fixedregulator_enable
52 };
53 
54 struct fixedregulator_softc {
55 	device_t	sc_dev;
56 	int		sc_phandle;
57 
58 	struct fdtbus_gpio_pin *sc_pin;
59 	bool		sc_always_on;
60 	bool		sc_enable_val;
61 };
62 
63 CFATTACH_DECL_NEW(fregulator, sizeof(struct fixedregulator_softc),
64     fixedregulator_match, fixedregulator_attach, NULL, NULL);
65 
66 static int
67 fixedregulator_match(device_t parent, cfdata_t cf, void *aux)
68 {
69 	const char * const compatible[] = { "regulator-fixed", NULL };
70 	const struct fdt_attach_args *faa = aux;
71 
72 	return of_match_compatible(faa->faa_phandle, compatible);
73 }
74 
75 static void
76 fixedregulator_attach(device_t parent, device_t self, void *aux)
77 {
78 	struct fixedregulator_softc * const sc = device_private(self);
79 	const struct fdt_attach_args *faa = aux;
80 	const int phandle = faa->faa_phandle;
81 	char *name;
82 	int len, gpioflags;
83 
84 	sc->sc_dev = self;
85 	sc->sc_phandle = phandle;
86 
87 	aprint_naive("\n");
88 
89 	len = OF_getproplen(phandle, "regulator-name");
90 	if (len > 0) {
91 		name = kmem_zalloc(len, KM_SLEEP);
92 		if (OF_getprop(phandle, "regulator-name", name, len) == len) {
93 			aprint_normal(": %s\n", name);
94 		} else {
95 			aprint_normal("\n");
96 		}
97 		kmem_free(name, len);
98 	} else {
99 		aprint_normal("\n");
100 	}
101 
102 	gpioflags = GPIO_PIN_OUTPUT;
103 	if (of_getprop_bool(phandle, "gpio-open-drain"))
104 		gpioflags |= GPIO_PIN_OPENDRAIN;
105 
106 	sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on");
107 	sc->sc_pin = fdtbus_gpio_acquire(phandle, "gpio", gpioflags);
108 	if (sc->sc_pin == NULL)
109 		sc->sc_always_on = true;
110 	sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high");
111 
112 	fdtbus_register_regulator_controller(self, phandle,
113 	    &fixedregulator_funcs);
114 }
115 
116 static int
117 fixedregulator_acquire(device_t dev)
118 {
119 	return 0;
120 }
121 
122 static void
123 fixedregulator_release(device_t dev)
124 {
125 }
126 
127 static int
128 fixedregulator_enable(device_t dev, bool enable)
129 {
130 	struct fixedregulator_softc * const sc = device_private(dev);
131 
132 	if (enable) {
133 		if (sc->sc_always_on) {
134 			return 0;
135 		} else {
136 			fdtbus_gpio_write_raw(sc->sc_pin, sc->sc_enable_val);
137 			return 0;
138 		}
139 	} else {
140 		if (sc->sc_always_on) {
141 			return EIO;
142 		} else {
143 			fdtbus_gpio_write_raw(sc->sc_pin, !sc->sc_enable_val);
144 			return 0;
145 		}
146 	}
147 }
148