]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/gpio/gpioregulator.c
zfs: merge openzfs/zfs@804414aad
[FreeBSD/FreeBSD.git] / sys / dev / gpio / gpioregulator.c
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 /*
27  * GPIO controlled regulators
28  */
29
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/rman.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/gpio.h>
38
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41
42 #include <dev/gpio/gpiobusvar.h>
43
44 #include <dev/extres/regulator/regulator.h>
45
46 #include "regdev_if.h"
47
48 struct gpioregulator_state {
49         int                     val;
50         uint32_t                mask;
51 };
52
53 struct gpioregulator_init_def {
54         struct regnode_init_def         reg_init_def;
55         struct gpiobus_pin              *enable_pin;
56         int                             enable_pin_valid;
57         int                             startup_delay_us;
58         int                             nstates;
59         struct gpioregulator_state      *states;
60         int                             npins;
61         struct gpiobus_pin              **pins;
62 };
63
64 struct gpioregulator_reg_sc {
65         struct regnode                  *regnode;
66         device_t                        base_dev;
67         struct regnode_std_param        *param;
68         struct gpioregulator_init_def   *def;
69 };
70
71 struct gpioregulator_softc {
72         device_t                        dev;
73         struct gpioregulator_reg_sc     *reg_sc;
74         struct gpioregulator_init_def   init_def;
75 };
76
77 static int
78 gpioregulator_regnode_init(struct regnode *regnode)
79 {
80         struct gpioregulator_reg_sc *sc;
81         int error, n;
82
83         sc = regnode_get_softc(regnode);
84
85         if (sc->def->enable_pin_valid == 1) {
86                 error = gpio_pin_setflags(sc->def->enable_pin, GPIO_PIN_OUTPUT);
87                 if (error != 0)
88                         return (error);
89         }
90
91         for (n = 0; n < sc->def->npins; n++) {
92                 error = gpio_pin_setflags(sc->def->pins[n], GPIO_PIN_OUTPUT);
93                 if (error != 0)
94                         return (error);
95         }
96
97         return (0);
98 }
99
100 static int
101 gpioregulator_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
102 {
103         struct gpioregulator_reg_sc *sc;
104         bool active;
105         int error;
106
107         sc = regnode_get_softc(regnode);
108
109         if (sc->def->enable_pin_valid == 1) {
110                 active = enable;
111                 if (!sc->param->enable_active_high)
112                         active = !active;
113                 error = gpio_pin_set_active(sc->def->enable_pin, active);
114                 if (error != 0)
115                         return (error);
116         }
117
118         *udelay = sc->def->startup_delay_us;
119
120         return (0);
121 }
122
123 static int
124 gpioregulator_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
125     int max_uvolt, int *udelay)
126 {
127         struct gpioregulator_reg_sc *sc;
128         const struct gpioregulator_state *state;
129         int error, n;
130
131         sc = regnode_get_softc(regnode);
132         state = NULL;
133
134         for (n = 0; n < sc->def->nstates; n++) {
135                 if (sc->def->states[n].val >= min_uvolt &&
136                     sc->def->states[n].val <= max_uvolt) {
137                         state = &sc->def->states[n];
138                         break;
139                 }
140         }
141         if (state == NULL)
142                 return (EINVAL);
143
144         for (n = 0; n < sc->def->npins; n++) {
145                 error = gpio_pin_set_active(sc->def->pins[n],
146                     (state->mask >> n) & 1);
147                 if (error != 0)
148                         return (error);
149         }
150
151         *udelay = sc->def->startup_delay_us;
152
153         return (0);
154 }
155
156 static int
157 gpioregulator_regnode_get_voltage(struct regnode *regnode, int *uvolt)
158 {
159         struct gpioregulator_reg_sc *sc;
160         uint32_t mask;
161         int error, n;
162         bool active;
163
164         sc = regnode_get_softc(regnode);
165         mask = 0;
166
167         for (n = 0; n < sc->def->npins; n++) {
168                 error = gpio_pin_is_active(sc->def->pins[n], &active);
169                 if (error != 0)
170                         return (error);
171                 mask |= (active << n);
172         }
173
174         for (n = 0; n < sc->def->nstates; n++) {
175                 if (sc->def->states[n].mask == mask) {
176                         *uvolt = sc->def->states[n].val;
177                         return (0);
178                 }
179         }
180
181         return (EIO);
182 }
183
184 static regnode_method_t gpioregulator_regnode_methods[] = {
185         /* Regulator interface */
186         REGNODEMETHOD(regnode_init,     gpioregulator_regnode_init),
187         REGNODEMETHOD(regnode_enable,   gpioregulator_regnode_enable),
188         REGNODEMETHOD(regnode_set_voltage, gpioregulator_regnode_set_voltage),
189         REGNODEMETHOD(regnode_get_voltage, gpioregulator_regnode_get_voltage),
190         REGNODEMETHOD_END
191 };
192 DEFINE_CLASS_1(gpioregulator_regnode, gpioregulator_regnode_class,
193     gpioregulator_regnode_methods, sizeof(struct gpioregulator_reg_sc),
194     regnode_class);
195
196 static int
197 gpioregulator_parse_fdt(struct gpioregulator_softc *sc)
198 {
199         uint32_t *pstates, mask;
200         phandle_t node;
201         ssize_t len;
202         int error, n;
203
204         node = ofw_bus_get_node(sc->dev);
205         pstates = NULL;
206         mask = 0;
207
208         error = regulator_parse_ofw_stdparam(sc->dev, node,
209             &sc->init_def.reg_init_def);
210         if (error != 0)
211                 return (error);
212
213         /* "states" property (required) */
214         len = OF_getencprop_alloc_multi(node, "states", sizeof(*pstates),
215             (void **)&pstates);
216         if (len < 2) {
217                 device_printf(sc->dev, "invalid 'states' property\n");
218                 error = EINVAL;
219                 goto done;
220         }
221         sc->init_def.nstates = len / 2;
222         sc->init_def.states = malloc(sc->init_def.nstates *
223             sizeof(*sc->init_def.states), M_DEVBUF, M_WAITOK);
224         for (n = 0; n < sc->init_def.nstates; n++) {
225                 sc->init_def.states[n].val = pstates[n * 2 + 0];
226                 sc->init_def.states[n].mask = pstates[n * 2 + 1];
227                 mask |= sc->init_def.states[n].mask;
228         }
229
230         /* "startup-delay-us" property (optional) */
231         len = OF_getencprop(node, "startup-delay-us",
232             &sc->init_def.startup_delay_us,
233             sizeof(sc->init_def.startup_delay_us));
234         if (len <= 0)
235                 sc->init_def.startup_delay_us = 0;
236
237         /* "enable-gpio" property (optional) */
238         error = gpio_pin_get_by_ofw_property(sc->dev, node, "enable-gpio",
239             &sc->init_def.enable_pin);
240         if (error == 0)
241                 sc->init_def.enable_pin_valid = 1;
242
243         /* "gpios" property */
244         sc->init_def.npins = 32 - __builtin_clz(mask);
245         sc->init_def.pins = malloc(sc->init_def.npins *
246             sizeof(sc->init_def.pins), M_DEVBUF, M_WAITOK | M_ZERO);
247         for (n = 0; n < sc->init_def.npins; n++) {
248                 error = gpio_pin_get_by_ofw_idx(sc->dev, node, n,
249                     &sc->init_def.pins[n]);
250                 if (error != 0) {
251                         device_printf(sc->dev, "cannot get pin %d\n", n);
252                         goto done;
253                 }
254         }
255
256 done:
257         if (error != 0) {
258                 for (n = 0; n < sc->init_def.npins; n++) {
259                         if (sc->init_def.pins[n] != NULL)
260                                 gpio_pin_release(sc->init_def.pins[n]);
261                 }
262
263                 free(sc->init_def.states, M_DEVBUF);
264                 free(sc->init_def.pins, M_DEVBUF);
265
266         }
267         OF_prop_free(pstates);
268
269         return (error);
270 }
271
272 static int
273 gpioregulator_probe(device_t dev)
274 {
275
276         if (!ofw_bus_is_compatible(dev, "regulator-gpio"))
277                 return (ENXIO);
278
279         device_set_desc(dev, "GPIO controlled regulator");
280         return (BUS_PROBE_GENERIC);
281 }
282
283 static int
284 gpioregulator_attach(device_t dev)
285 {
286         struct gpioregulator_softc *sc;
287         struct regnode *regnode;
288         phandle_t node;
289         int error;
290
291         sc = device_get_softc(dev);
292         sc->dev = dev;
293         node = ofw_bus_get_node(dev);
294
295         error = gpioregulator_parse_fdt(sc);
296         if (error != 0) {
297                 device_printf(dev, "cannot parse parameters\n");
298                 return (ENXIO);
299         }
300         sc->init_def.reg_init_def.id = 1;
301         sc->init_def.reg_init_def.ofw_node = node;
302
303         regnode = regnode_create(dev, &gpioregulator_regnode_class,
304             &sc->init_def.reg_init_def);
305         if (regnode == NULL) {
306                 device_printf(dev, "cannot create regulator\n");
307                 return (ENXIO);
308         }
309
310         sc->reg_sc = regnode_get_softc(regnode);
311         sc->reg_sc->regnode = regnode;
312         sc->reg_sc->base_dev = dev;
313         sc->reg_sc->param = regnode_get_stdparam(regnode);
314         sc->reg_sc->def = &sc->init_def;
315
316         regnode_register(regnode);
317
318         return (0);
319 }
320
321
322 static device_method_t gpioregulator_methods[] = {
323         /* Device interface */
324         DEVMETHOD(device_probe,         gpioregulator_probe),
325         DEVMETHOD(device_attach,        gpioregulator_attach),
326
327         /* Regdev interface */
328         DEVMETHOD(regdev_map,           regdev_default_ofw_map),
329
330         DEVMETHOD_END
331 };
332
333 static driver_t gpioregulator_driver = {
334         "gpioregulator",
335         gpioregulator_methods,
336         sizeof(struct gpioregulator_softc),
337 };
338
339 EARLY_DRIVER_MODULE(gpioregulator, simplebus, gpioregulator_driver, 0, 0,
340     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
341 MODULE_VERSION(gpioregulator, 1);