1 /* $NetBSD: gpioregulator.c,v 1.4 2021/01/27 03:10:21 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared 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: gpioregulator.c,v 1.4 2021/01/27 03:10:21 thorpej 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 gpioregulator_match(device_t, cfdata_t, void *);
42 static void gpioregulator_attach(device_t, device_t, void *);
43
44 static int gpioregulator_acquire(device_t);
45 static void gpioregulator_release(device_t);
46 static int gpioregulator_enable(device_t, bool);
47 static int gpioregulator_set_voltage(device_t, u_int, u_int);
48 static int gpioregulator_get_voltage(device_t, u_int *);
49
50 static const struct fdtbus_regulator_controller_func gpioregulator_funcs = {
51 .acquire = gpioregulator_acquire,
52 .release = gpioregulator_release,
53 .enable = gpioregulator_enable,
54 .set_voltage = gpioregulator_set_voltage,
55 .get_voltage = gpioregulator_get_voltage,
56 };
57
58 struct gpioregulator_state {
59 u_int st_val;
60 u_int st_mask;
61 };
62
63 struct gpioregulator_softc {
64 device_t sc_dev;
65 int sc_phandle;
66
67 struct fdtbus_gpio_pin *sc_pin_enable;
68
69 struct fdtbus_gpio_pin **sc_pins;
70 u_int sc_npins;
71
72 struct gpioregulator_state *sc_states;
73 u_int sc_nstates;
74
75 bool sc_always_on;
76 bool sc_boot_on;
77 bool sc_enable_val;
78 uint32_t sc_delay;
79
80 int sc_gpioflags;
81 };
82
83 CFATTACH_DECL_NEW(gregulator, sizeof(struct gpioregulator_softc),
84 gpioregulator_match, gpioregulator_attach, NULL, NULL);
85
86 static const struct device_compatible_entry compat_data[] = {
87 { .compat = "regulator-gpio" },
88 DEVICE_COMPAT_EOL
89 };
90
91 static int
gpioregulator_match(device_t parent,cfdata_t cf,void * aux)92 gpioregulator_match(device_t parent, cfdata_t cf, void *aux)
93 {
94 const struct fdt_attach_args *faa = aux;
95
96 return of_compatible_match(faa->faa_phandle, compat_data);
97 }
98
99 static void
gpioregulator_attach(device_t parent,device_t self,void * aux)100 gpioregulator_attach(device_t parent, device_t self, void *aux)
101 {
102 struct gpioregulator_softc * const sc = device_private(self);
103 const struct fdt_attach_args *faa = aux;
104 const int phandle = faa->faa_phandle;
105 const uint32_t *pstates;
106 uint32_t mask;
107 u_int gpios_states;
108 char *name;
109 int len, n;
110
111 sc->sc_dev = self;
112 sc->sc_phandle = phandle;
113
114 aprint_naive("\n");
115
116 len = OF_getproplen(phandle, "regulator-name");
117 if (len > 0) {
118 name = kmem_zalloc(len, KM_SLEEP);
119 if (OF_getprop(phandle, "regulator-name", name, len) == len) {
120 aprint_normal(": %s\n", name);
121 } else {
122 aprint_normal("\n");
123 }
124 kmem_free(name, len);
125 } else {
126 aprint_normal("\n");
127 }
128
129 pstates = fdtbus_get_prop(phandle, "states", &len);
130 if (pstates == NULL || len < 8 || len % 8 != 0) {
131 aprint_error_dev(self, "invalid 'states' property\n");
132 return;
133 }
134
135 mask = 0;
136 sc->sc_nstates = len / (sizeof(uint32_t) * 2);
137 sc->sc_states = kmem_zalloc(
138 sc->sc_nstates * sizeof(struct gpioregulator_state), KM_SLEEP);
139 for (n = 0; n < sc->sc_nstates; n++) {
140 sc->sc_states[n].st_val = be32toh(pstates[n * 2 + 0]);
141 sc->sc_states[n].st_mask = be32toh(pstates[n * 2 + 1]);
142 mask |= sc->sc_states[n].st_mask;
143 }
144
145 sc->sc_gpioflags = GPIO_PIN_OUTPUT;
146 if (of_getprop_bool(phandle, "gpio-open-drain"))
147 sc->sc_gpioflags |= GPIO_PIN_OPENDRAIN;
148
149 sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on");
150 sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on");
151 sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high");
152 if (of_getprop_uint32(phandle, "startup-delay-us", &sc->sc_delay) != 0)
153 sc->sc_delay = 0;
154
155 /* "enable-gpio" property (optional) */
156 sc->sc_pin_enable = fdtbus_gpio_acquire(phandle, "enable-gpio",
157 sc->sc_gpioflags);
158
159 /* "gpios" property */
160 sc->sc_npins = 32 - __builtin_clz(mask);
161 sc->sc_pins = kmem_zalloc(sc->sc_npins * sizeof(sc->sc_pins), KM_SLEEP);
162 for (n = 0; n < sc->sc_npins; n++) {
163 sc->sc_pins[n] = fdtbus_gpio_acquire_index(phandle, "gpios",
164 n, sc->sc_gpioflags);
165 if (sc->sc_pins[n] == NULL) {
166 aprint_error_dev(self, "cannot get pin %d\n", n);
167 return;
168 }
169 }
170
171 /* "gpios-states" property */
172 if (of_getprop_uint32(phandle, "gpios-states", &gpios_states) != 0)
173 gpios_states = 0;
174
175 /* Set initial state */
176 for (n = 0; n < sc->sc_npins; n++)
177 fdtbus_gpio_write(sc->sc_pins[n], (gpios_states >> n) & 1);
178
179 fdtbus_register_regulator_controller(self, phandle,
180 &gpioregulator_funcs);
181
182 /*
183 * If the regulator is flagged as always on or enabled at boot,
184 * ensure that it is enabled
185 */
186 if (sc->sc_always_on || sc->sc_boot_on)
187 gpioregulator_enable(self, true);
188 }
189
190 static int
gpioregulator_acquire(device_t dev)191 gpioregulator_acquire(device_t dev)
192 {
193 return 0;
194 }
195
196 static void
gpioregulator_release(device_t dev)197 gpioregulator_release(device_t dev)
198 {
199 }
200
201 static int
gpioregulator_enable(device_t dev,bool enable)202 gpioregulator_enable(device_t dev, bool enable)
203 {
204 struct gpioregulator_softc * const sc = device_private(dev);
205
206 if (enable) {
207 if (sc->sc_pin_enable != NULL)
208 fdtbus_gpio_write_raw(sc->sc_pin_enable, sc->sc_enable_val);
209 if (sc->sc_delay > 0)
210 delay(sc->sc_delay);
211 } else {
212 if (sc->sc_always_on)
213 return EIO;
214 fdtbus_gpio_write_raw(sc->sc_pin_enable, !sc->sc_enable_val);
215 }
216 return 0;
217 }
218
219 static int
gpioregulator_set_voltage(device_t dev,u_int min_uvolt,u_int max_uvolt)220 gpioregulator_set_voltage(device_t dev, u_int min_uvolt, u_int max_uvolt)
221 {
222 struct gpioregulator_softc * const sc = device_private(dev);
223 const struct gpioregulator_state *state = NULL;
224 int n;
225
226 for (n = 0; n < sc->sc_nstates; n++)
227 if (sc->sc_states[n].st_val >= min_uvolt &&
228 sc->sc_states[n].st_val <= max_uvolt) {
229 state = &sc->sc_states[n];
230 break;
231 }
232 if (state == NULL)
233 return EINVAL;
234
235 for (n = 0; n < sc->sc_npins; n++)
236 fdtbus_gpio_write(sc->sc_pins[n], (state->st_mask >> n) & 1);
237
238 if (sc->sc_delay > 0)
239 delay(sc->sc_delay);
240
241 return 0;
242 }
243
244 static int
gpioregulator_get_voltage(device_t dev,u_int * puvolt)245 gpioregulator_get_voltage(device_t dev, u_int *puvolt)
246 {
247 struct gpioregulator_softc * const sc = device_private(dev);
248 uint32_t mask = 0;
249 int n, val;
250
251 for (n = 0; n < sc->sc_npins; n++) {
252 val = fdtbus_gpio_read(sc->sc_pins[n]);
253 mask |= (val << n);
254 }
255
256 for (n = 0; n < sc->sc_nstates; n++)
257 if (sc->sc_states[n].st_mask == mask) {
258 *puvolt = sc->sc_states[n].st_val;
259 return 0;
260 }
261
262 return EIO;
263 }
264