gpioregulator.c revision 1.1 1 1.1 jmcneill /* $NetBSD: gpioregulator.c,v 1.1 2017/08/13 18:27:31 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: gpioregulator.c,v 1.1 2017/08/13 18:27:31 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/kmem.h>
36 1.1 jmcneill #include <sys/bus.h>
37 1.1 jmcneill #include <sys/gpio.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/fdt/fdtvar.h>
40 1.1 jmcneill
41 1.1 jmcneill static int gpioregulator_match(device_t, cfdata_t, void *);
42 1.1 jmcneill static void gpioregulator_attach(device_t, device_t, void *);
43 1.1 jmcneill
44 1.1 jmcneill static int gpioregulator_acquire(device_t);
45 1.1 jmcneill static void gpioregulator_release(device_t);
46 1.1 jmcneill static int gpioregulator_enable(device_t, bool);
47 1.1 jmcneill static int gpioregulator_set_voltage(device_t, u_int, u_int);
48 1.1 jmcneill static int gpioregulator_get_voltage(device_t, u_int *);
49 1.1 jmcneill
50 1.1 jmcneill static const struct fdtbus_regulator_controller_func gpioregulator_funcs = {
51 1.1 jmcneill .acquire = gpioregulator_acquire,
52 1.1 jmcneill .release = gpioregulator_release,
53 1.1 jmcneill .enable = gpioregulator_enable,
54 1.1 jmcneill .set_voltage = gpioregulator_set_voltage,
55 1.1 jmcneill .get_voltage = gpioregulator_get_voltage,
56 1.1 jmcneill };
57 1.1 jmcneill
58 1.1 jmcneill struct gpioregulator_state {
59 1.1 jmcneill u_int st_val;
60 1.1 jmcneill u_int st_mask;
61 1.1 jmcneill };
62 1.1 jmcneill
63 1.1 jmcneill struct gpioregulator_softc {
64 1.1 jmcneill device_t sc_dev;
65 1.1 jmcneill int sc_phandle;
66 1.1 jmcneill
67 1.1 jmcneill struct fdtbus_gpio_pin *sc_pin_enable;
68 1.1 jmcneill
69 1.1 jmcneill struct fdtbus_gpio_pin **sc_pins;
70 1.1 jmcneill u_int sc_npins;
71 1.1 jmcneill
72 1.1 jmcneill struct gpioregulator_state *sc_states;
73 1.1 jmcneill u_int sc_nstates;
74 1.1 jmcneill
75 1.1 jmcneill bool sc_always_on;
76 1.1 jmcneill bool sc_boot_on;
77 1.1 jmcneill bool sc_enable_val;
78 1.1 jmcneill uint32_t sc_delay;
79 1.1 jmcneill
80 1.1 jmcneill int sc_gpioflags;
81 1.1 jmcneill };
82 1.1 jmcneill
83 1.1 jmcneill CFATTACH_DECL_NEW(gregulator, sizeof(struct gpioregulator_softc),
84 1.1 jmcneill gpioregulator_match, gpioregulator_attach, NULL, NULL);
85 1.1 jmcneill
86 1.1 jmcneill static int
87 1.1 jmcneill gpioregulator_match(device_t parent, cfdata_t cf, void *aux)
88 1.1 jmcneill {
89 1.1 jmcneill const char * const compatible[] = { "regulator-gpio", NULL };
90 1.1 jmcneill const struct fdt_attach_args *faa = aux;
91 1.1 jmcneill
92 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
93 1.1 jmcneill }
94 1.1 jmcneill
95 1.1 jmcneill static void
96 1.1 jmcneill gpioregulator_attach(device_t parent, device_t self, void *aux)
97 1.1 jmcneill {
98 1.1 jmcneill struct gpioregulator_softc * const sc = device_private(self);
99 1.1 jmcneill const struct fdt_attach_args *faa = aux;
100 1.1 jmcneill const int phandle = faa->faa_phandle;
101 1.1 jmcneill const uint32_t *pstates;
102 1.1 jmcneill uint32_t mask;
103 1.1 jmcneill char *name;
104 1.1 jmcneill int len, n;
105 1.1 jmcneill
106 1.1 jmcneill sc->sc_dev = self;
107 1.1 jmcneill sc->sc_phandle = phandle;
108 1.1 jmcneill
109 1.1 jmcneill aprint_naive("\n");
110 1.1 jmcneill
111 1.1 jmcneill len = OF_getproplen(phandle, "regulator-name");
112 1.1 jmcneill if (len > 0) {
113 1.1 jmcneill name = kmem_zalloc(len, KM_SLEEP);
114 1.1 jmcneill if (OF_getprop(phandle, "regulator-name", name, len) == len) {
115 1.1 jmcneill aprint_normal(": %s\n", name);
116 1.1 jmcneill } else {
117 1.1 jmcneill aprint_normal("\n");
118 1.1 jmcneill }
119 1.1 jmcneill kmem_free(name, len);
120 1.1 jmcneill } else {
121 1.1 jmcneill aprint_normal("\n");
122 1.1 jmcneill }
123 1.1 jmcneill
124 1.1 jmcneill pstates = fdtbus_get_prop(phandle, "states", &len);
125 1.1 jmcneill if (pstates == NULL || len < 8 || len % 8 != 0) {
126 1.1 jmcneill aprint_error_dev(self, "invalid 'states' property\n");
127 1.1 jmcneill return;
128 1.1 jmcneill }
129 1.1 jmcneill
130 1.1 jmcneill mask = 0;
131 1.1 jmcneill sc->sc_nstates = len / (sizeof(uint32_t) * 2);
132 1.1 jmcneill sc->sc_states = kmem_zalloc(
133 1.1 jmcneill sc->sc_nstates * sizeof(struct gpioregulator_state), KM_SLEEP);
134 1.1 jmcneill for (n = 0; n < sc->sc_nstates; n++) {
135 1.1 jmcneill sc->sc_states[n].st_val = be32toh(pstates[n * 2 + 0]);
136 1.1 jmcneill sc->sc_states[n].st_mask = be32toh(pstates[n * 2 + 1]);
137 1.1 jmcneill mask |= sc->sc_states[n].st_mask;
138 1.1 jmcneill }
139 1.1 jmcneill
140 1.1 jmcneill sc->sc_gpioflags = GPIO_PIN_OUTPUT;
141 1.1 jmcneill if (of_getprop_bool(phandle, "gpio-open-drain"))
142 1.1 jmcneill sc->sc_gpioflags |= GPIO_PIN_OPENDRAIN;
143 1.1 jmcneill
144 1.1 jmcneill sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on");
145 1.1 jmcneill sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on");
146 1.1 jmcneill sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high");
147 1.1 jmcneill if (of_getprop_uint32(phandle, "startup-delay-us", &sc->sc_delay) != 0)
148 1.1 jmcneill sc->sc_delay = 0;
149 1.1 jmcneill
150 1.1 jmcneill /* "enable-gpio" property (optional) */
151 1.1 jmcneill sc->sc_pin_enable = fdtbus_gpio_acquire(phandle, "enable-gpio",
152 1.1 jmcneill sc->sc_gpioflags);
153 1.1 jmcneill
154 1.1 jmcneill /* "gpios" property */
155 1.1 jmcneill sc->sc_npins = 32 - __builtin_clz(mask);
156 1.1 jmcneill sc->sc_pins = kmem_zalloc(sc->sc_npins * sizeof(sc->sc_pins), KM_SLEEP);
157 1.1 jmcneill for (n = 0; n < sc->sc_npins; n++) {
158 1.1 jmcneill sc->sc_pins[n] = fdtbus_gpio_acquire_index(phandle, "gpios",
159 1.1 jmcneill n, sc->sc_gpioflags);
160 1.1 jmcneill if (sc->sc_pins[n] == NULL) {
161 1.1 jmcneill aprint_error_dev(self, "cannot get pin %d\n", n);
162 1.1 jmcneill return;
163 1.1 jmcneill }
164 1.1 jmcneill }
165 1.1 jmcneill
166 1.1 jmcneill fdtbus_register_regulator_controller(self, phandle,
167 1.1 jmcneill &gpioregulator_funcs);
168 1.1 jmcneill
169 1.1 jmcneill /*
170 1.1 jmcneill * If the regulator is flagged as always on or enabled at boot,
171 1.1 jmcneill * ensure that it is enabled
172 1.1 jmcneill */
173 1.1 jmcneill if (sc->sc_always_on || sc->sc_boot_on)
174 1.1 jmcneill gpioregulator_enable(self, true);
175 1.1 jmcneill }
176 1.1 jmcneill
177 1.1 jmcneill static int
178 1.1 jmcneill gpioregulator_acquire(device_t dev)
179 1.1 jmcneill {
180 1.1 jmcneill return 0;
181 1.1 jmcneill }
182 1.1 jmcneill
183 1.1 jmcneill static void
184 1.1 jmcneill gpioregulator_release(device_t dev)
185 1.1 jmcneill {
186 1.1 jmcneill }
187 1.1 jmcneill
188 1.1 jmcneill static int
189 1.1 jmcneill gpioregulator_enable(device_t dev, bool enable)
190 1.1 jmcneill {
191 1.1 jmcneill struct gpioregulator_softc * const sc = device_private(dev);
192 1.1 jmcneill
193 1.1 jmcneill if (enable) {
194 1.1 jmcneill if (sc->sc_pin_enable != NULL)
195 1.1 jmcneill fdtbus_gpio_write_raw(sc->sc_pin_enable, sc->sc_enable_val);
196 1.1 jmcneill if (sc->sc_delay > 0)
197 1.1 jmcneill delay(sc->sc_delay);
198 1.1 jmcneill } else {
199 1.1 jmcneill if (sc->sc_always_on)
200 1.1 jmcneill return EIO;
201 1.1 jmcneill fdtbus_gpio_write_raw(sc->sc_pin_enable, !sc->sc_enable_val);
202 1.1 jmcneill }
203 1.1 jmcneill return 0;
204 1.1 jmcneill }
205 1.1 jmcneill
206 1.1 jmcneill static int
207 1.1 jmcneill gpioregulator_set_voltage(device_t dev, u_int min_uvolt, u_int max_uvolt)
208 1.1 jmcneill {
209 1.1 jmcneill struct gpioregulator_softc * const sc = device_private(dev);
210 1.1 jmcneill const struct gpioregulator_state *state = NULL;
211 1.1 jmcneill int n;
212 1.1 jmcneill
213 1.1 jmcneill for (n = 0; n < sc->sc_nstates; n++)
214 1.1 jmcneill if (sc->sc_states[n].st_val >= min_uvolt &&
215 1.1 jmcneill sc->sc_states[n].st_val <= max_uvolt) {
216 1.1 jmcneill state = &sc->sc_states[n];
217 1.1 jmcneill break;
218 1.1 jmcneill }
219 1.1 jmcneill if (state == NULL)
220 1.1 jmcneill return EINVAL;
221 1.1 jmcneill
222 1.1 jmcneill for (n = 0; n < sc->sc_npins; n++)
223 1.1 jmcneill fdtbus_gpio_write(sc->sc_pins[n], (state->st_mask >> n) & 1);
224 1.1 jmcneill
225 1.1 jmcneill if (sc->sc_delay > 0)
226 1.1 jmcneill delay(sc->sc_delay);
227 1.1 jmcneill
228 1.1 jmcneill return 0;
229 1.1 jmcneill }
230 1.1 jmcneill
231 1.1 jmcneill static int
232 1.1 jmcneill gpioregulator_get_voltage(device_t dev, u_int *puvolt)
233 1.1 jmcneill {
234 1.1 jmcneill struct gpioregulator_softc * const sc = device_private(dev);
235 1.1 jmcneill uint32_t mask = 0;
236 1.1 jmcneill int n, val;
237 1.1 jmcneill
238 1.1 jmcneill for (n = 0; n < sc->sc_npins; n++) {
239 1.1 jmcneill val = fdtbus_gpio_read(sc->sc_pins[n]);
240 1.1 jmcneill mask |= (val << n);
241 1.1 jmcneill }
242 1.1 jmcneill
243 1.1 jmcneill for (n = 0; n < sc->sc_nstates; n++)
244 1.1 jmcneill if (sc->sc_states[n].st_mask == mask) {
245 1.1 jmcneill *puvolt = sc->sc_states[n].st_val;
246 1.1 jmcneill return 0;
247 1.1 jmcneill }
248 1.1 jmcneill
249 1.1 jmcneill return EIO;
250 1.1 jmcneill }
251