fdt_regulator.c revision 1.9
1/* $NetBSD: fdt_regulator.c,v 1.9 2021/08/08 15:23:42 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: fdt_regulator.c,v 1.9 2021/08/08 15:23:42 jmcneill Exp $"); 31 32#include <sys/param.h> 33#include <sys/bus.h> 34#include <sys/kmem.h> 35#include <sys/queue.h> 36 37#include <libfdt.h> 38#include <dev/fdt/fdtvar.h> 39 40#define REGULATOR_TO_RC(_reg) \ 41 container_of((_reg), struct fdtbus_regulator_controller, rc_reg) 42 43struct fdtbus_regulator_controller { 44 device_t rc_dev; 45 int rc_phandle; 46 const struct fdtbus_regulator_controller_func *rc_funcs; 47 48 u_int rc_enable_ramp_delay; 49 50 struct fdtbus_regulator rc_reg; /* handle returned by acquire() */ 51 52 LIST_ENTRY(fdtbus_regulator_controller) rc_next; 53}; 54 55static LIST_HEAD(, fdtbus_regulator_controller) fdtbus_regulator_controllers = 56 LIST_HEAD_INITIALIZER(fdtbus_regulator_controllers); 57 58int 59fdtbus_register_regulator_controller(device_t dev, int phandle, 60 const struct fdtbus_regulator_controller_func *funcs) 61{ 62 struct fdtbus_regulator_controller *rc; 63 64 rc = kmem_zalloc(sizeof(*rc), KM_SLEEP); 65 rc->rc_dev = dev; 66 rc->rc_phandle = phandle; 67 rc->rc_funcs = funcs; 68 rc->rc_reg.reg_rc = rc; 69 70 of_getprop_uint32(phandle, "regulator-enable-ramp-delay", &rc->rc_enable_ramp_delay); 71 72 LIST_INSERT_HEAD(&fdtbus_regulator_controllers, rc, rc_next); 73 74 return 0; 75} 76 77static struct fdtbus_regulator_controller * 78fdtbus_get_regulator_controller(int phandle) 79{ 80 struct fdtbus_regulator_controller *rc; 81 82 LIST_FOREACH(rc, &fdtbus_regulator_controllers, rc_next) { 83 if (rc->rc_phandle == phandle) 84 return rc; 85 } 86 87 return NULL; 88} 89 90struct fdtbus_regulator * 91fdtbus_regulator_acquire(int phandle, const char *prop) 92{ 93 struct fdtbus_regulator_controller *rc; 94 int regulator_phandle; 95 int error; 96 97 regulator_phandle = fdtbus_get_phandle(phandle, prop); 98 if (regulator_phandle == -1) { 99 return NULL; 100 } 101 102 rc = fdtbus_get_regulator_controller(regulator_phandle); 103 if (rc == NULL) { 104 return NULL; 105 } 106 107 error = rc->rc_funcs->acquire(rc->rc_dev); 108 if (error) { 109 aprint_error_dev(rc->rc_dev, "failed to acquire regulator: %d\n", error); 110 return NULL; 111 } 112 113 return &rc->rc_reg; 114} 115 116void 117fdtbus_regulator_release(struct fdtbus_regulator *reg) 118{ 119 struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg); 120 121 rc->rc_funcs->release(rc->rc_dev); 122} 123 124int 125fdtbus_regulator_enable(struct fdtbus_regulator *reg) 126{ 127 struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg); 128 int error; 129 130 error = rc->rc_funcs->enable(rc->rc_dev, true); 131 if (error != 0) 132 return error; 133 134 if (rc->rc_enable_ramp_delay != 0) 135 delay(rc->rc_enable_ramp_delay); 136 137 return 0; 138} 139 140int 141fdtbus_regulator_disable(struct fdtbus_regulator *reg) 142{ 143 struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg); 144 145 if (of_hasprop(rc->rc_phandle, "regulator-always-on")) 146 return EIO; 147 148 return rc->rc_funcs->enable(rc->rc_dev, false); 149} 150 151int 152fdtbus_regulator_set_voltage(struct fdtbus_regulator *reg, u_int min_uvol, 153 u_int max_uvol) 154{ 155 struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg); 156 157 if (rc->rc_funcs->set_voltage == NULL) 158 return EINVAL; 159 160 return rc->rc_funcs->set_voltage(rc->rc_dev, min_uvol, max_uvol); 161} 162 163int 164fdtbus_regulator_get_voltage(struct fdtbus_regulator *reg, u_int *puvol) 165{ 166 struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg); 167 168 if (rc->rc_funcs->get_voltage == NULL) 169 return EINVAL; 170 171 return rc->rc_funcs->get_voltage(rc->rc_dev, puvol); 172} 173 174int 175fdtbus_regulator_supports_voltage(struct fdtbus_regulator *reg, u_int min_uvol, 176 u_int max_uvol) 177{ 178 struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg); 179 u_int uvol; 180 181 if (rc->rc_funcs->set_voltage == NULL) 182 return EINVAL; 183 184 if (of_getprop_uint32(rc->rc_phandle, "regulator-min-microvolt", &uvol) == 0) { 185 if (uvol < min_uvol) 186 return ERANGE; 187 } 188 if (of_getprop_uint32(rc->rc_phandle, "regulator-max-microvolt", &uvol) == 0) { 189 if (uvol > max_uvol) 190 return ERANGE; 191 } 192 193 return 0; 194} 195