1 /*-
2  * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
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: stable/12/sys/dev/extres/regulator/regulator_fixed.c 362393 2020-06-19 17:49:49Z manu $");
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 static int regnode_fixed_get_voltage(struct regnode *regnode, int *uvolt);
77 
78 static regnode_method_t regnode_fixed_methods[] = {
79 	/* Regulator interface */
80 	REGNODEMETHOD(regnode_init,		regnode_fixed_init),
81 	REGNODEMETHOD(regnode_enable,		regnode_fixed_enable),
82 	REGNODEMETHOD(regnode_status,		regnode_fixed_status),
83 	REGNODEMETHOD(regnode_stop,		regnode_fixed_stop),
84 	REGNODEMETHOD(regnode_get_voltage,	regnode_fixed_get_voltage),
85 	REGNODEMETHOD(regnode_check_voltage,	regnode_method_check_voltage),
86 	REGNODEMETHOD_END
87 };
88 DEFINE_CLASS_1(regnode_fixed, regnode_fixed_class, regnode_fixed_methods,
89    sizeof(struct regnode_fixed_sc), regnode_class);
90 
91 /*
92  * GPIO list functions.
93  * Two or more regulators can share single GPIO pins, so we must track all
94  * GPIOs in gpio_list.
95  * The GPIO pin is registerd and reseved for first consumer, all others share
96  * gpio_entry with it.
97  */
98 static struct gpio_entry *
regnode_get_gpio_entry(struct gpiobus_pin * gpio_pin)99 regnode_get_gpio_entry(struct gpiobus_pin *gpio_pin)
100 {
101 	struct gpio_entry *entry, *tmp;
102 	device_t busdev;
103 	int rv;
104 
105 	busdev = GPIO_GET_BUS(gpio_pin->dev);
106 	if (busdev == NULL)
107 		return (NULL);
108 	entry = malloc(sizeof(struct gpio_entry), M_FIXEDREGULATOR,
109 	    M_WAITOK | M_ZERO);
110 
111 	mtx_lock(&gpio_list_mtx);
112 
113 	TAILQ_FOREACH(tmp, &gpio_list, link) {
114 		if (tmp->gpio_pin.dev == gpio_pin->dev &&
115 		    tmp->gpio_pin.pin == gpio_pin->pin) {
116 			tmp->use_cnt++;
117 			mtx_unlock(&gpio_list_mtx);
118 			free(entry, M_FIXEDREGULATOR);
119 			return (tmp);
120 		}
121 	}
122 
123 	/* Reserve pin. */
124 	/* XXX Can we call gpiobus_acquire_pin() with gpio_list_mtx held? */
125 	rv = gpiobus_acquire_pin(busdev, gpio_pin->pin);
126 	if (rv != 0) {
127 		mtx_unlock(&gpio_list_mtx);
128 		free(entry, M_FIXEDREGULATOR);
129 		return (NULL);
130 	}
131 	/* Everything is OK, build new entry and insert it to list. */
132 	entry->gpio_pin = *gpio_pin;
133 	entry->use_cnt = 1;
134 	TAILQ_INSERT_TAIL(&gpio_list, entry, link);
135 
136 	mtx_unlock(&gpio_list_mtx);
137 	return (entry);
138 }
139 
140 
141 /*
142  * Regulator class implementation.
143  */
144 static int
regnode_fixed_init(struct regnode * regnode)145 regnode_fixed_init(struct regnode *regnode)
146 {
147 	device_t dev;
148 	struct regnode_fixed_sc *sc;
149 	struct gpiobus_pin *pin;
150 	uint32_t flags;
151 	int rv;
152 
153 	sc = regnode_get_softc(regnode);
154 	dev = regnode_get_device(regnode);
155 	sc->param = regnode_get_stdparam(regnode);
156 	if (sc->gpio_entry == NULL)
157 		return (0);
158 	pin = &sc->gpio_entry->gpio_pin;
159 
160 	flags = GPIO_PIN_OUTPUT;
161 	if (sc->gpio_open_drain)
162 		flags |= GPIO_PIN_OPENDRAIN;
163 	if (sc->param->boot_on || sc->param->always_on) {
164 		rv = GPIO_PIN_SET(pin->dev, pin->pin, sc->param->enable_active_high);
165 		if (rv != 0) {
166 			device_printf(dev, "Cannot set GPIO pin: %d\n",
167 			    pin->pin);
168 			return (rv);
169 		}
170 	}
171 
172 	rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
173 	if (rv != 0) {
174 		device_printf(dev, "Cannot configure GPIO pin: %d\n", pin->pin);
175 		return (rv);
176 	}
177 
178 	return (0);
179 }
180 
181 /*
182  * Enable/disable regulator.
183  * Take shared GPIO pins in account
184  */
185 static int
regnode_fixed_enable(struct regnode * regnode,bool enable,int * udelay)186 regnode_fixed_enable(struct regnode *regnode, bool enable, int *udelay)
187 {
188 	device_t dev;
189 	struct regnode_fixed_sc *sc;
190 	struct gpiobus_pin *pin;
191 	int rv;
192 
193 	sc = regnode_get_softc(regnode);
194 	dev = regnode_get_device(regnode);
195 
196 	*udelay = 0;
197 	if (sc->gpio_entry == NULL)
198 		return (0);
199 	pin = &sc->gpio_entry->gpio_pin;
200 	if (enable) {
201 		sc->gpio_entry->enable_cnt++;
202 		if (sc->gpio_entry->enable_cnt > 1)
203 			return (0);
204 	} else {
205 		KASSERT(sc->gpio_entry->enable_cnt > 0,
206 		    ("Invalid enable count"));
207 		sc->gpio_entry->enable_cnt--;
208 		if (sc->gpio_entry->enable_cnt >= 1)
209 			return (0);
210 	}
211 	if (sc->gpio_entry->always_on && !enable)
212 		return (0);
213 	if (!sc->param->enable_active_high)
214 		enable = !enable;
215 	rv = GPIO_PIN_SET(pin->dev, pin->pin, enable);
216 	if (rv != 0) {
217 		device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin);
218 		return (rv);
219 	}
220 	*udelay = sc->param->enable_delay;
221 	return (0);
222 }
223 
224 /*
225  * Stop (physicaly shutdown) regulator.
226  * Take shared GPIO pins in account
227  */
228 static int
regnode_fixed_stop(struct regnode * regnode,int * udelay)229 regnode_fixed_stop(struct regnode *regnode, int *udelay)
230 {
231 	device_t dev;
232 	struct regnode_fixed_sc *sc;
233 	struct gpiobus_pin *pin;
234 	int rv;
235 
236 	sc = regnode_get_softc(regnode);
237 	dev = regnode_get_device(regnode);
238 
239 	*udelay = 0;
240 	if (sc->gpio_entry == NULL)
241 		return (0);
242 	if (sc->gpio_entry->always_on)
243 		return (0);
244 	pin = &sc->gpio_entry->gpio_pin;
245 	if (sc->gpio_entry->enable_cnt > 0) {
246 		/* Other regulator(s) are enabled. */
247 		/* XXXX Any diagnostic message? Or error? */
248 		return (0);
249 	}
250 	rv = GPIO_PIN_SET(pin->dev, pin->pin,
251 	    sc->param->enable_active_high ? false: true);
252 	if (rv != 0) {
253 		device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin);
254 		return (rv);
255 	}
256 	*udelay = sc->param->enable_delay;
257 	return (0);
258 }
259 
260 static int
regnode_fixed_status(struct regnode * regnode,int * status)261 regnode_fixed_status(struct regnode *regnode, int *status)
262 {
263 	struct regnode_fixed_sc *sc;
264 	struct gpiobus_pin *pin;
265 	uint32_t val;
266 	int rv;
267 
268 	sc = regnode_get_softc(regnode);
269 
270 	*status = 0;
271 	if (sc->gpio_entry == NULL) {
272 		*status = REGULATOR_STATUS_ENABLED;
273 		return (0);
274 	}
275 	pin = &sc->gpio_entry->gpio_pin;
276 
277 	rv = GPIO_PIN_GET(pin->dev, pin->pin, &val);
278 	if (rv == 0) {
279 		if (!sc->param->enable_active_high ^ (val != 0))
280 			*status = REGULATOR_STATUS_ENABLED;
281 	}
282 	return (rv);
283 }
284 
285 static int
regnode_fixed_get_voltage(struct regnode * regnode,int * uvolt)286 regnode_fixed_get_voltage(struct regnode *regnode, int *uvolt)
287 {
288 	struct regnode_fixed_sc *sc;
289 
290 	sc = regnode_get_softc(regnode);
291 	*uvolt = sc->param->min_uvolt;
292 	return (0);
293 }
294 
295 int
regnode_fixed_register(device_t dev,struct regnode_fixed_init_def * init_def)296 regnode_fixed_register(device_t dev, struct regnode_fixed_init_def *init_def)
297 {
298 	struct regnode *regnode;
299 	struct regnode_fixed_sc *sc;
300 
301 	regnode = regnode_create(dev, &regnode_fixed_class,
302 	    &init_def->reg_init_def);
303 	if (regnode == NULL) {
304 		device_printf(dev, "Cannot create regulator.\n");
305 		return(ENXIO);
306 	}
307 	sc = regnode_get_softc(regnode);
308 	sc->gpio_open_drain = init_def->gpio_open_drain;
309 	if (init_def->gpio_pin != NULL) {
310 		sc->gpio_entry = regnode_get_gpio_entry(init_def->gpio_pin);
311 		if (sc->gpio_entry == NULL)
312 			return(ENXIO);
313 	}
314 	regnode = regnode_register(regnode);
315 	if (regnode == NULL) {
316 		device_printf(dev, "Cannot register regulator.\n");
317 		return(ENXIO);
318 	}
319 
320 	if (sc->gpio_entry != NULL)
321 		sc->gpio_entry->always_on |= sc->param->always_on;
322 
323 	return (0);
324 }
325 
326 /*
327  * OFW Driver implementation.
328  */
329 #ifdef FDT
330 
331 struct  regfix_softc
332 {
333 	device_t			dev;
334 	bool				attach_done;
335 	struct regnode_fixed_init_def	init_def;
336 	phandle_t			gpio_prodxref;
337 	pcell_t				*gpio_cells;
338 	int				gpio_ncells;
339 	struct gpiobus_pin		gpio_pin;
340 };
341 
342 static struct ofw_compat_data compat_data[] = {
343 	{"regulator-fixed",		1},
344 	{NULL,				0},
345 };
346 
347 static int
regfix_get_gpio(struct regfix_softc * sc)348 regfix_get_gpio(struct regfix_softc * sc)
349 {
350 	device_t busdev;
351 	phandle_t node;
352 
353 	int rv;
354 
355 	if (sc->gpio_prodxref == 0)
356 		return (0);
357 
358 	node = ofw_bus_get_node(sc->dev);
359 
360 	/* Test if controller exist. */
361 	sc->gpio_pin.dev = OF_device_from_xref(sc->gpio_prodxref);
362 	if (sc->gpio_pin.dev == NULL)
363 		return (ENODEV);
364 
365 	/* Test if GPIO bus already exist. */
366 	busdev = GPIO_GET_BUS(sc->gpio_pin.dev);
367 	if (busdev == NULL)
368 		return (ENODEV);
369 
370 	rv = gpio_map_gpios(sc->gpio_pin.dev, node,
371 	    OF_node_from_xref(sc->gpio_prodxref), sc->gpio_ncells,
372 	    sc->gpio_cells, &(sc->gpio_pin.pin), &(sc->gpio_pin.flags));
373 	if (rv != 0) {
374 		device_printf(sc->dev, "Cannot map the gpio property.\n");
375 		return (ENXIO);
376 	}
377 	sc->init_def.gpio_pin = &sc->gpio_pin;
378 	return (0);
379 }
380 
381 static int
regfix_parse_fdt(struct regfix_softc * sc)382 regfix_parse_fdt(struct regfix_softc * sc)
383 {
384 	phandle_t node;
385 	int rv;
386 	struct regnode_init_def *init_def;
387 
388 	node = ofw_bus_get_node(sc->dev);
389 	init_def = &sc->init_def.reg_init_def;
390 
391 	rv = regulator_parse_ofw_stdparam(sc->dev, node, init_def);
392 	if (rv != 0) {
393 		device_printf(sc->dev, "Cannot parse standard parameters.\n");
394 		return(rv);
395 	}
396 
397 	if (init_def->std_param.min_uvolt != init_def->std_param.max_uvolt) {
398 		device_printf(sc->dev, "min_uvolt != max_uvolt\n");
399 		return (ENXIO);
400 	}
401 	/* Fixed regulator uses 'startup-delay-us' property for enable_delay */
402 	rv = OF_getencprop(node, "startup-delay-us",
403 	   &init_def->std_param.enable_delay,
404 	   sizeof(init_def->std_param.enable_delay));
405 	if (rv <= 0)
406 		init_def->std_param.enable_delay = 0;
407 	/* GPIO pin */
408 	if (OF_hasprop(node, "gpio-open-drain"))
409 		sc->init_def.gpio_open_drain = true;
410 
411 	if (!OF_hasprop(node, "gpio"))
412 		return (0);
413 	rv = ofw_bus_parse_xref_list_alloc(node, "gpio", "#gpio-cells", 0,
414 	    &sc->gpio_prodxref, &sc->gpio_ncells, &sc->gpio_cells);
415 	if (rv != 0) {
416 		sc->gpio_prodxref = 0;
417 		device_printf(sc->dev, "Malformed gpio property\n");
418 		return (ENXIO);
419 	}
420 	return (0);
421 }
422 
423 static void
regfix_new_pass(device_t dev)424 regfix_new_pass(device_t dev)
425 {
426 	struct regfix_softc * sc;
427 	int rv;
428 
429 	sc = device_get_softc(dev);
430 	bus_generic_new_pass(dev);
431 
432 	if (sc->attach_done)
433 		return;
434 
435 	/* Try to get and configure GPIO. */
436 	rv = regfix_get_gpio(sc);
437 	if (rv != 0)
438 		return;
439 
440 	/* Register regulator. */
441 	regnode_fixed_register(sc->dev, &sc->init_def);
442 	sc->attach_done = true;
443 }
444 
445 static int
regfix_probe(device_t dev)446 regfix_probe(device_t dev)
447 {
448 
449 	if (!ofw_bus_status_okay(dev))
450 		return (ENXIO);
451 
452 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
453 		return (ENXIO);
454 
455 	device_set_desc(dev, "Fixed Regulator");
456 	return (BUS_PROBE_DEFAULT);
457 }
458 
459 static int
regfix_detach(device_t dev)460 regfix_detach(device_t dev)
461 {
462 
463 	/* This device is always present. */
464 	return (EBUSY);
465 }
466 
467 static int
regfix_attach(device_t dev)468 regfix_attach(device_t dev)
469 {
470 	struct regfix_softc * sc;
471 	int rv;
472 
473 	sc = device_get_softc(dev);
474 	sc->dev = dev;
475 
476 	/* Parse FDT data. */
477 	rv = regfix_parse_fdt(sc);
478 	if (rv != 0)
479 		return(ENXIO);
480 
481 	/* Fill reset of init. */
482 	sc->init_def.reg_init_def.id = 1;
483 	sc->init_def.reg_init_def.flags = REGULATOR_FLAGS_STATIC;
484 
485 	/* Try to get and configure GPIO. */
486 	rv = regfix_get_gpio(sc);
487 	if (rv != 0)
488 		return (bus_generic_attach(dev));
489 
490 	/* Register regulator. */
491 	regnode_fixed_register(sc->dev, &sc->init_def);
492 	sc->attach_done = true;
493 
494 	return (bus_generic_attach(dev));
495 }
496 
497 static device_method_t regfix_methods[] = {
498 	/* Device interface */
499 	DEVMETHOD(device_probe,		regfix_probe),
500 	DEVMETHOD(device_attach,	regfix_attach),
501 	DEVMETHOD(device_detach,	regfix_detach),
502 	/* Bus interface */
503 	DEVMETHOD(bus_new_pass,		regfix_new_pass),
504 	/* Regdev interface */
505 	DEVMETHOD(regdev_map,		regdev_default_ofw_map),
506 
507 	DEVMETHOD_END
508 };
509 
510 static devclass_t regfix_devclass;
511 DEFINE_CLASS_0(regfix, regfix_driver, regfix_methods,
512     sizeof(struct regfix_softc));
513 EARLY_DRIVER_MODULE(regfix, simplebus, regfix_driver,
514    regfix_devclass, 0, 0, BUS_PASS_BUS);
515 
516 #endif /* FDT */
517