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