1 /*-
2 * Copyright 2016 Michal Meloun <[email protected]>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_platform.h"
31 #include <sys/param.h>
32 #include <sys/conf.h>
33 #include <sys/gpio.h>
34 #include <sys/kernel.h>
35 #include <sys/kobj.h>
36 #include <sys/systm.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39
40 #ifdef FDT
41 #include <dev/fdt/fdt_common.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 #endif
45 #include <dev/gpio/gpiobusvar.h>
46 #include <dev/extres/regulator/regulator_fixed.h>
47
48 #include "regdev_if.h"
49
50 MALLOC_DEFINE(M_FIXEDREGULATOR, "fixedregulator", "Fixed regulator");
51
52 /* GPIO list for shared pins. */
53 typedef TAILQ_HEAD(gpio_list, gpio_entry) gpio_list_t;
54 struct gpio_entry {
55 TAILQ_ENTRY(gpio_entry) link;
56 struct gpiobus_pin gpio_pin;
57 int use_cnt;
58 int enable_cnt;
59 bool always_on;
60 };
61 static gpio_list_t gpio_list = TAILQ_HEAD_INITIALIZER(gpio_list);
62 static struct mtx gpio_list_mtx;
63 MTX_SYSINIT(gpio_list_lock, &gpio_list_mtx, "Regulator GPIO lock", MTX_DEF);
64
65 struct regnode_fixed_sc {
66 struct regnode_std_param *param;
67 bool gpio_open_drain;
68 struct gpio_entry *gpio_entry;
69 };
70
71 static int regnode_fixed_init(struct regnode *regnode);
72 static int regnode_fixed_enable(struct regnode *regnode, bool enable,
73 int *udelay);
74 static int regnode_fixed_status(struct regnode *regnode, int *status);
75 static int regnode_fixed_stop(struct regnode *regnode, int *udelay);
76
77 static regnode_method_t regnode_fixed_methods[] = {
78 /* Regulator interface */
79 REGNODEMETHOD(regnode_init, regnode_fixed_init),
80 REGNODEMETHOD(regnode_enable, regnode_fixed_enable),
81 REGNODEMETHOD(regnode_status, regnode_fixed_status),
82 REGNODEMETHOD(regnode_stop, regnode_fixed_stop),
83 REGNODEMETHOD_END
84 };
85 DEFINE_CLASS_1(regnode_fixed, regnode_fixed_class, regnode_fixed_methods,
86 sizeof(struct regnode_fixed_sc), regnode_class);
87
88 /*
89 * GPIO list functions.
90 * Two or more regulators can share single GPIO pins, so we must track all
91 * GPIOs in gpio_list.
92 * The GPIO pin is registerd and reseved for first consumer, all others share
93 * gpio_entry with it.
94 */
95 static struct gpio_entry *
regnode_get_gpio_entry(struct gpiobus_pin * gpio_pin)96 regnode_get_gpio_entry(struct gpiobus_pin *gpio_pin)
97 {
98 struct gpio_entry *entry, *tmp;
99 device_t busdev;
100 int rv;
101
102 busdev = GPIO_GET_BUS(gpio_pin->dev);
103 if (busdev == NULL)
104 return (NULL);
105 entry = malloc(sizeof(struct gpio_entry), M_FIXEDREGULATOR,
106 M_WAITOK | M_ZERO);
107
108 mtx_lock(&gpio_list_mtx);
109
110 TAILQ_FOREACH(tmp, &gpio_list, link) {
111 if (tmp->gpio_pin.dev == gpio_pin->dev &&
112 tmp->gpio_pin.pin == gpio_pin->pin) {
113 tmp->use_cnt++;
114 mtx_unlock(&gpio_list_mtx);
115 free(entry, M_FIXEDREGULATOR);
116 return (tmp);
117 }
118 }
119
120 /* Reserve pin. */
121 /* XXX Can we call gpiobus_acquire_pin() with gpio_list_mtx held? */
122 rv = gpiobus_acquire_pin(busdev, gpio_pin->pin);
123 if (rv != 0) {
124 mtx_unlock(&gpio_list_mtx);
125 free(entry, M_FIXEDREGULATOR);
126 return (NULL);
127 }
128 /* Everything is OK, build new entry and insert it to list. */
129 entry->gpio_pin = *gpio_pin;
130 entry->use_cnt = 1;
131 TAILQ_INSERT_TAIL(&gpio_list, entry, link);
132
133 mtx_unlock(&gpio_list_mtx);
134 return (entry);
135 }
136
137
138 /*
139 * Regulator class implementation.
140 */
141 static int
regnode_fixed_init(struct regnode * regnode)142 regnode_fixed_init(struct regnode *regnode)
143 {
144 device_t dev;
145 struct regnode_fixed_sc *sc;
146 struct gpiobus_pin *pin;
147 uint32_t flags;
148 int rv;
149
150 sc = regnode_get_softc(regnode);
151 dev = regnode_get_device(regnode);
152 sc->param = regnode_get_stdparam(regnode);
153 if (sc->gpio_entry == NULL)
154 return (0);
155 pin = &sc->gpio_entry->gpio_pin;
156
157 flags = GPIO_PIN_OUTPUT;
158 if (sc->gpio_open_drain)
159 flags |= GPIO_PIN_OPENDRAIN;
160 if (sc->param->boot_on || sc->param->always_on) {
161 rv = GPIO_PIN_SET(pin->dev, pin->pin, sc->param->enable_active_high);
162 if (rv != 0) {
163 device_printf(dev, "Cannot set GPIO pin: %d\n",
164 pin->pin);
165 return (rv);
166 }
167 }
168
169 rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
170 if (rv != 0) {
171 device_printf(dev, "Cannot configure GPIO pin: %d\n", pin->pin);
172 return (rv);
173 }
174
175 return (0);
176 }
177
178 /*
179 * Enable/disable regulator.
180 * Take shared GPIO pins in account
181 */
182 static int
regnode_fixed_enable(struct regnode * regnode,bool enable,int * udelay)183 regnode_fixed_enable(struct regnode *regnode, bool enable, int *udelay)
184 {
185 device_t dev;
186 struct regnode_fixed_sc *sc;
187 struct gpiobus_pin *pin;
188 int rv;
189
190 sc = regnode_get_softc(regnode);
191 dev = regnode_get_device(regnode);
192
193 *udelay = 0;
194 if (sc->gpio_entry == NULL)
195 return (0);
196 pin = &sc->gpio_entry->gpio_pin;
197 if (enable) {
198 sc->gpio_entry->enable_cnt++;
199 if (sc->gpio_entry->enable_cnt > 1)
200 return (0);
201 } else {
202 KASSERT(sc->gpio_entry->enable_cnt > 0,
203 ("Invalid enable count"));
204 sc->gpio_entry->enable_cnt--;
205 if (sc->gpio_entry->enable_cnt >= 1)
206 return (0);
207 }
208 if (sc->gpio_entry->always_on && !enable)
209 return (0);
210 if (!sc->param->enable_active_high)
211 enable = !enable;
212 rv = GPIO_PIN_SET(pin->dev, pin->pin, enable);
213 if (rv != 0) {
214 device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin);
215 return (rv);
216 }
217 *udelay = sc->param->enable_delay;
218 return (0);
219 }
220
221 /*
222 * Stop (physicaly shutdown) regulator.
223 * Take shared GPIO pins in account
224 */
225 static int
regnode_fixed_stop(struct regnode * regnode,int * udelay)226 regnode_fixed_stop(struct regnode *regnode, int *udelay)
227 {
228 device_t dev;
229 struct regnode_fixed_sc *sc;
230 struct gpiobus_pin *pin;
231 int rv;
232
233 sc = regnode_get_softc(regnode);
234 dev = regnode_get_device(regnode);
235
236 *udelay = 0;
237 if (sc->gpio_entry == NULL)
238 return (0);
239 if (sc->gpio_entry->always_on)
240 return (0);
241 pin = &sc->gpio_entry->gpio_pin;
242 if (sc->gpio_entry->enable_cnt > 0) {
243 /* Other regulator(s) are enabled. */
244 /* XXXX Any diagnostic message? Or error? */
245 return (0);
246 }
247 rv = GPIO_PIN_SET(pin->dev, pin->pin,
248 sc->param->enable_active_high ? false: true);
249 if (rv != 0) {
250 device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin);
251 return (rv);
252 }
253 *udelay = sc->param->enable_delay;
254 return (0);
255 }
256
257 static int
regnode_fixed_status(struct regnode * regnode,int * status)258 regnode_fixed_status(struct regnode *regnode, int *status)
259 {
260 struct regnode_fixed_sc *sc;
261 struct gpiobus_pin *pin;
262 uint32_t val;
263 int rv;
264
265 sc = regnode_get_softc(regnode);
266
267 *status = 0;
268 if (sc->gpio_entry == NULL) {
269 *status = REGULATOR_STATUS_ENABLED;
270 return (0);
271 }
272 pin = &sc->gpio_entry->gpio_pin;
273
274 rv = GPIO_PIN_GET(pin->dev, pin->pin, &val);
275 if (rv == 0) {
276 if (!sc->param->enable_active_high ^ (val != 0))
277 *status = REGULATOR_STATUS_ENABLED;
278 }
279 return (rv);
280 }
281
282 int
regnode_fixed_register(device_t dev,struct regnode_fixed_init_def * init_def)283 regnode_fixed_register(device_t dev, struct regnode_fixed_init_def *init_def)
284 {
285 struct regnode *regnode;
286 struct regnode_fixed_sc *sc;
287
288 regnode = regnode_create(dev, ®node_fixed_class,
289 &init_def->reg_init_def);
290 if (regnode == NULL) {
291 device_printf(dev, "Cannot create regulator.\n");
292 return(ENXIO);
293 }
294 sc = regnode_get_softc(regnode);
295 sc->gpio_open_drain = init_def->gpio_open_drain;
296 if (init_def->gpio_pin != NULL) {
297 sc->gpio_entry = regnode_get_gpio_entry(init_def->gpio_pin);
298 if (sc->gpio_entry == NULL)
299 return(ENXIO);
300 }
301 regnode = regnode_register(regnode);
302 if (regnode == NULL) {
303 device_printf(dev, "Cannot register regulator.\n");
304 return(ENXIO);
305 }
306
307 if (sc->gpio_entry != NULL)
308 sc->gpio_entry->always_on |= sc->param->always_on;
309
310 return (0);
311 }
312
313 /*
314 * OFW Driver implementation.
315 */
316 #ifdef FDT
317
318 struct regfix_softc
319 {
320 device_t dev;
321 bool attach_done;
322 struct regnode_fixed_init_def init_def;
323 phandle_t gpio_prodxref;
324 pcell_t *gpio_cells;
325 int gpio_ncells;
326 struct gpiobus_pin gpio_pin;
327 };
328
329 static struct ofw_compat_data compat_data[] = {
330 {"regulator-fixed", 1},
331 {NULL, 0},
332 };
333
334 static int
regfix_get_gpio(struct regfix_softc * sc)335 regfix_get_gpio(struct regfix_softc * sc)
336 {
337 device_t busdev;
338 phandle_t node;
339
340 int rv;
341
342 if (sc->gpio_prodxref == 0)
343 return (0);
344
345 node = ofw_bus_get_node(sc->dev);
346
347 /* Test if controller exist. */
348 sc->gpio_pin.dev = OF_device_from_xref(sc->gpio_prodxref);
349 if (sc->gpio_pin.dev == NULL)
350 return (ENODEV);
351
352 /* Test if GPIO bus already exist. */
353 busdev = GPIO_GET_BUS(sc->gpio_pin.dev);
354 if (busdev == NULL)
355 return (ENODEV);
356
357 rv = gpio_map_gpios(sc->gpio_pin.dev, node,
358 OF_node_from_xref(sc->gpio_prodxref), sc->gpio_ncells,
359 sc->gpio_cells, &(sc->gpio_pin.pin), &(sc->gpio_pin.flags));
360 if (rv != 0) {
361 device_printf(sc->dev, "Cannot map the gpio property.\n");
362 return (ENXIO);
363 }
364 sc->init_def.gpio_pin = &sc->gpio_pin;
365 return (0);
366 }
367
368 static int
regfix_parse_fdt(struct regfix_softc * sc)369 regfix_parse_fdt(struct regfix_softc * sc)
370 {
371 phandle_t node;
372 int rv;
373 struct regnode_init_def *init_def;
374
375 node = ofw_bus_get_node(sc->dev);
376 init_def = &sc->init_def.reg_init_def;
377
378 rv = regulator_parse_ofw_stdparam(sc->dev, node, init_def);
379 if (rv != 0) {
380 device_printf(sc->dev, "Cannot parse standard parameters.\n");
381 return(rv);
382 }
383
384 /* Fixed regulator uses 'startup-delay-us' property for enable_delay */
385 rv = OF_getencprop(node, "startup-delay-us",
386 &init_def->std_param.enable_delay,
387 sizeof(init_def->std_param.enable_delay));
388 if (rv <= 0)
389 init_def->std_param.enable_delay = 0;
390 /* GPIO pin */
391 if (OF_hasprop(node, "gpio-open-drain"))
392 sc->init_def.gpio_open_drain = true;
393
394 if (!OF_hasprop(node, "gpio"))
395 return (0);
396 rv = ofw_bus_parse_xref_list_alloc(node, "gpio", "#gpio-cells", 0,
397 &sc->gpio_prodxref, &sc->gpio_ncells, &sc->gpio_cells);
398 if (rv != 0) {
399 sc->gpio_prodxref = 0;
400 device_printf(sc->dev, "Malformed gpio property\n");
401 return (ENXIO);
402 }
403 return (0);
404 }
405
406 static void
regfix_new_pass(device_t dev)407 regfix_new_pass(device_t dev)
408 {
409 struct regfix_softc * sc;
410 int rv;
411
412 sc = device_get_softc(dev);
413 bus_generic_new_pass(dev);
414
415 if (sc->attach_done)
416 return;
417
418 /* Try to get and configure GPIO. */
419 rv = regfix_get_gpio(sc);
420 if (rv != 0)
421 return;
422
423 /* Register regulator. */
424 regnode_fixed_register(sc->dev, &sc->init_def);
425 sc->attach_done = true;
426 }
427
428 static int
regfix_probe(device_t dev)429 regfix_probe(device_t dev)
430 {
431
432 if (!ofw_bus_status_okay(dev))
433 return (ENXIO);
434
435 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
436 return (ENXIO);
437
438 device_set_desc(dev, "Fixed Regulator");
439 return (BUS_PROBE_DEFAULT);
440 }
441
442 static int
regfix_detach(device_t dev)443 regfix_detach(device_t dev)
444 {
445
446 /* This device is always present. */
447 return (EBUSY);
448 }
449
450 static int
regfix_attach(device_t dev)451 regfix_attach(device_t dev)
452 {
453 struct regfix_softc * sc;
454 int rv;
455
456 sc = device_get_softc(dev);
457 sc->dev = dev;
458
459 /* Parse FDT data. */
460 rv = regfix_parse_fdt(sc);
461 if (rv != 0)
462 return(ENXIO);
463
464 /* Fill reset of init. */
465 sc->init_def.reg_init_def.id = 1;
466 sc->init_def.reg_init_def.flags = REGULATOR_FLAGS_STATIC;
467
468 /* Try to get and configure GPIO. */
469 rv = regfix_get_gpio(sc);
470 if (rv != 0)
471 return (bus_generic_attach(dev));
472
473 /* Register regulator. */
474 regnode_fixed_register(sc->dev, &sc->init_def);
475 sc->attach_done = true;
476
477 return (bus_generic_attach(dev));
478 }
479
480 static device_method_t regfix_methods[] = {
481 /* Device interface */
482 DEVMETHOD(device_probe, regfix_probe),
483 DEVMETHOD(device_attach, regfix_attach),
484 DEVMETHOD(device_detach, regfix_detach),
485 /* Bus interface */
486 DEVMETHOD(bus_new_pass, regfix_new_pass),
487 /* Regdev interface */
488 DEVMETHOD(regdev_map, regdev_default_ofw_map),
489
490 DEVMETHOD_END
491 };
492
493 static devclass_t regfix_devclass;
494 DEFINE_CLASS_0(regfix, regfix_driver, regfix_methods,
495 sizeof(struct regfix_softc));
496 EARLY_DRIVER_MODULE(regfix, simplebus, regfix_driver,
497 regfix_devclass, 0, 0, BUS_PASS_BUS);
498
499 #endif /* FDT */
500