1 /* $NetBSD: fixedregulator.c,v 1.10 2021/01/27 03:10:21 thorpej 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.10 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 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 static int fixedregulator_set_voltage(device_t, u_int, u_int);
48 static int fixedregulator_get_voltage(device_t, u_int *);
49
50 struct fdtbus_regulator_controller_func fixedregulator_funcs = {
51 .acquire = fixedregulator_acquire,
52 .release = fixedregulator_release,
53 .enable = fixedregulator_enable,
54 .set_voltage = fixedregulator_set_voltage,
55 .get_voltage = fixedregulator_get_voltage,
56 };
57
58 struct fixedregulator_softc {
59 device_t sc_dev;
60 int sc_phandle;
61
62 struct fdtbus_gpio_pin *sc_pin;
63 bool sc_always_on;
64 bool sc_boot_on;
65 bool sc_enable_val;
66 uint32_t sc_delay;
67 uint32_t sc_min_uvol;
68 uint32_t sc_max_uvol;
69
70 int sc_gpioflags;
71 bool sc_deferred;
72 };
73
74 CFATTACH_DECL_NEW(fregulator, sizeof(struct fixedregulator_softc),
75 fixedregulator_match, fixedregulator_attach, NULL, NULL);
76
77 static const struct device_compatible_entry compat_data[] = {
78 { .compat = "regulator-fixed" },
79 DEVICE_COMPAT_EOL
80 };
81
82 static int
fixedregulator_match(device_t parent,cfdata_t cf,void * aux)83 fixedregulator_match(device_t parent, cfdata_t cf, void *aux)
84 {
85 const struct fdt_attach_args *faa = aux;
86
87 return of_compatible_match(faa->faa_phandle, compat_data);
88 }
89
90 static void
fixedregulator_attach(device_t parent,device_t self,void * aux)91 fixedregulator_attach(device_t parent, device_t self, void *aux)
92 {
93 struct fixedregulator_softc * const sc = device_private(self);
94 const struct fdt_attach_args *faa = aux;
95 const int phandle = faa->faa_phandle;
96 char *name;
97 int len;
98
99 sc->sc_dev = self;
100 sc->sc_phandle = phandle;
101
102 aprint_naive("\n");
103
104 len = OF_getproplen(phandle, "regulator-name");
105 if (len > 0) {
106 name = kmem_zalloc(len, KM_SLEEP);
107 if (OF_getprop(phandle, "regulator-name", name, len) == len) {
108 aprint_normal(": %s\n", name);
109 } else {
110 aprint_normal("\n");
111 }
112 kmem_free(name, len);
113 } else {
114 aprint_normal("\n");
115 }
116
117 sc->sc_gpioflags = GPIO_PIN_OUTPUT;
118 if (of_getprop_bool(phandle, "gpio-open-drain"))
119 sc->sc_gpioflags |= GPIO_PIN_OPENDRAIN;
120
121 sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on");
122 sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on");
123 sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high");
124 if (of_getprop_uint32(phandle, "startup-delay-us", &sc->sc_delay) != 0)
125 sc->sc_delay = 0;
126 of_getprop_uint32(phandle, "regulator-min-microvolt", &sc->sc_min_uvol);
127 of_getprop_uint32(phandle, "regulator-max-microvolt", &sc->sc_max_uvol);
128
129 sc->sc_pin = fdtbus_gpio_acquire(phandle, "gpio", sc->sc_gpioflags);
130 if (sc->sc_pin == NULL)
131 sc->sc_always_on = OF_getproplen(phandle, "gpio") <= 0;
132
133 fdtbus_register_regulator_controller(self, phandle,
134 &fixedregulator_funcs);
135
136 /*
137 * If the regulator is flagged as always on or enabled at boot,
138 * ensure that it is enabled
139 */
140 if (sc->sc_always_on || sc->sc_boot_on)
141 fixedregulator_enable(self, true);
142 }
143
144 static int
fixedregulator_acquire(device_t dev)145 fixedregulator_acquire(device_t dev)
146 {
147 struct fixedregulator_softc * const sc = device_private(dev);
148
149 if (sc->sc_pin == NULL && !sc->sc_always_on) {
150 sc->sc_pin = fdtbus_gpio_acquire(sc->sc_phandle, "gpio",
151 sc->sc_gpioflags);
152 if (sc->sc_pin == NULL)
153 return ENXIO;
154 }
155
156 return 0;
157 }
158
159 static void
fixedregulator_release(device_t dev)160 fixedregulator_release(device_t dev)
161 {
162 }
163
164 static int
fixedregulator_enable(device_t dev,bool enable)165 fixedregulator_enable(device_t dev, bool enable)
166 {
167 struct fixedregulator_softc * const sc = device_private(dev);
168
169 if (enable) {
170 if (sc->sc_pin != NULL)
171 fdtbus_gpio_write_raw(sc->sc_pin, sc->sc_enable_val);
172 if (sc->sc_delay > 0)
173 delay(sc->sc_delay);
174 } else {
175 if (sc->sc_always_on)
176 return EIO;
177 fdtbus_gpio_write_raw(sc->sc_pin, !sc->sc_enable_val);
178 }
179 return 0;
180 }
181
182 static int
fixedregulator_set_voltage(device_t dev,u_int min_uvol,u_int max_uvol)183 fixedregulator_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
184 {
185 struct fixedregulator_softc * const sc = device_private(dev);
186
187 if (sc->sc_min_uvol > max_uvol || sc->sc_max_uvol < min_uvol)
188 return EINVAL;
189
190 return 0;
191 }
192
193 static int
fixedregulator_get_voltage(device_t dev,u_int * uvol)194 fixedregulator_get_voltage(device_t dev, u_int *uvol)
195 {
196 struct fixedregulator_softc * const sc = device_private(dev);
197
198 *uvol = sc->sc_min_uvol;
199
200 return 0;
201 }
202