xref: /netbsd-src/sys/dev/fdt/fixedregulator.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /* $NetBSD: fixedregulator.c,v 1.4 2016/06/15 13:13:40 jmcneill 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.4 2016/06/15 13:13:40 jmcneill 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 
48 struct fdtbus_regulator_controller_func fixedregulator_funcs = {
49 	.acquire = fixedregulator_acquire,
50 	.release = fixedregulator_release,
51 	.enable = fixedregulator_enable
52 };
53 
54 struct fixedregulator_softc {
55 	device_t	sc_dev;
56 	int		sc_phandle;
57 
58 	struct fdtbus_gpio_pin *sc_pin;
59 	bool		sc_always_on;
60 	bool		sc_boot_on;
61 	bool		sc_enable_val;
62 	uint32_t	sc_delay;
63 };
64 
65 CFATTACH_DECL_NEW(fregulator, sizeof(struct fixedregulator_softc),
66     fixedregulator_match, fixedregulator_attach, NULL, NULL);
67 
68 static int
69 fixedregulator_match(device_t parent, cfdata_t cf, void *aux)
70 {
71 	const char * const compatible[] = { "regulator-fixed", NULL };
72 	const struct fdt_attach_args *faa = aux;
73 
74 	return of_match_compatible(faa->faa_phandle, compatible);
75 }
76 
77 static void
78 fixedregulator_attach(device_t parent, device_t self, void *aux)
79 {
80 	struct fixedregulator_softc * const sc = device_private(self);
81 	const struct fdt_attach_args *faa = aux;
82 	const int phandle = faa->faa_phandle;
83 	char *name;
84 	int len, gpioflags;
85 
86 	sc->sc_dev = self;
87 	sc->sc_phandle = phandle;
88 
89 	aprint_naive("\n");
90 
91 	len = OF_getproplen(phandle, "regulator-name");
92 	if (len > 0) {
93 		name = kmem_zalloc(len, KM_SLEEP);
94 		if (OF_getprop(phandle, "regulator-name", name, len) == len) {
95 			aprint_normal(": %s\n", name);
96 		} else {
97 			aprint_normal("\n");
98 		}
99 		kmem_free(name, len);
100 	} else {
101 		aprint_normal("\n");
102 	}
103 
104 	gpioflags = GPIO_PIN_OUTPUT;
105 	if (of_getprop_bool(phandle, "gpio-open-drain"))
106 		gpioflags |= GPIO_PIN_OPENDRAIN;
107 
108 	sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on");
109 	sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on");
110 	sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high");
111 	if (of_getprop_uint32(phandle, "startup-delay-us", &sc->sc_delay) != 0)
112 		sc->sc_delay = 0;
113 
114 	sc->sc_pin = fdtbus_gpio_acquire(phandle, "gpio", gpioflags);
115 	if (sc->sc_pin == NULL)
116 		sc->sc_always_on = true;
117 
118 	fdtbus_register_regulator_controller(self, phandle,
119 	    &fixedregulator_funcs);
120 
121 	/*
122 	 * If the regulator is flagged as always on or enabled at boot,
123 	 * ensure that it is enabled
124 	 */
125 	if (sc->sc_always_on || sc->sc_boot_on)
126 		fixedregulator_enable(self, true);
127 }
128 
129 static int
130 fixedregulator_acquire(device_t dev)
131 {
132 	return 0;
133 }
134 
135 static void
136 fixedregulator_release(device_t dev)
137 {
138 }
139 
140 static int
141 fixedregulator_enable(device_t dev, bool enable)
142 {
143 	struct fixedregulator_softc * const sc = device_private(dev);
144 
145 	if (enable) {
146 		if (sc->sc_pin != NULL)
147 			fdtbus_gpio_write_raw(sc->sc_pin, sc->sc_enable_val);
148 		if (sc->sc_delay > 0)
149 			delay(sc->sc_delay);
150 	} else {
151 		if (sc->sc_always_on)
152 			return EIO;
153 		fdtbus_gpio_write_raw(sc->sc_pin, !sc->sc_enable_val);
154 	}
155 	return 0;
156 }
157