1 /* $NetBSD: fdt_regulator.c,v 1.8 2019/05/27 23:18:33 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.8 2019/05/27 23:18:33 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 struct fdtbus_regulator_controller { 41 device_t rc_dev; 42 int rc_phandle; 43 const struct fdtbus_regulator_controller_func *rc_funcs; 44 45 u_int rc_enable_ramp_delay; 46 47 LIST_ENTRY(fdtbus_regulator_controller) rc_next; 48 }; 49 50 static LIST_HEAD(, fdtbus_regulator_controller) fdtbus_regulator_controllers = 51 LIST_HEAD_INITIALIZER(fdtbus_regulator_controllers); 52 53 int 54 fdtbus_register_regulator_controller(device_t dev, int phandle, 55 const struct fdtbus_regulator_controller_func *funcs) 56 { 57 struct fdtbus_regulator_controller *rc; 58 59 rc = kmem_zalloc(sizeof(*rc), KM_SLEEP); 60 rc->rc_dev = dev; 61 rc->rc_phandle = phandle; 62 rc->rc_funcs = funcs; 63 64 of_getprop_uint32(phandle, "regulator-enable-ramp-delay", &rc->rc_enable_ramp_delay); 65 66 LIST_INSERT_HEAD(&fdtbus_regulator_controllers, rc, rc_next); 67 68 return 0; 69 } 70 71 static struct fdtbus_regulator_controller * 72 fdtbus_get_regulator_controller(int phandle) 73 { 74 struct fdtbus_regulator_controller *rc; 75 76 LIST_FOREACH(rc, &fdtbus_regulator_controllers, rc_next) { 77 if (rc->rc_phandle == phandle) 78 return rc; 79 } 80 81 return NULL; 82 } 83 84 struct fdtbus_regulator * 85 fdtbus_regulator_acquire(int phandle, const char *prop) 86 { 87 struct fdtbus_regulator_controller *rc; 88 struct fdtbus_regulator *reg; 89 int regulator_phandle; 90 int error; 91 92 regulator_phandle = fdtbus_get_phandle(phandle, prop); 93 if (regulator_phandle == -1) { 94 return NULL; 95 } 96 97 rc = fdtbus_get_regulator_controller(regulator_phandle); 98 if (rc == NULL) { 99 return NULL; 100 } 101 102 error = rc->rc_funcs->acquire(rc->rc_dev); 103 if (error) { 104 aprint_error_dev(rc->rc_dev, "failed to acquire regulator: %d\n", error); 105 return NULL; 106 } 107 108 reg = kmem_alloc(sizeof(*reg), KM_SLEEP); 109 reg->reg_rc = rc; 110 111 return reg; 112 } 113 114 void 115 fdtbus_regulator_release(struct fdtbus_regulator *reg) 116 { 117 struct fdtbus_regulator_controller *rc = reg->reg_rc; 118 119 rc->rc_funcs->release(rc->rc_dev); 120 121 kmem_free(reg, sizeof(*reg)); 122 } 123 124 int 125 fdtbus_regulator_enable(struct fdtbus_regulator *reg) 126 { 127 struct fdtbus_regulator_controller *rc = reg->reg_rc; 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 140 int 141 fdtbus_regulator_disable(struct fdtbus_regulator *reg) 142 { 143 struct fdtbus_regulator_controller *rc = reg->reg_rc; 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 151 int 152 fdtbus_regulator_set_voltage(struct fdtbus_regulator *reg, u_int min_uvol, 153 u_int max_uvol) 154 { 155 struct fdtbus_regulator_controller *rc = reg->reg_rc; 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 163 int 164 fdtbus_regulator_get_voltage(struct fdtbus_regulator *reg, u_int *puvol) 165 { 166 struct fdtbus_regulator_controller *rc = reg->reg_rc; 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 174 int 175 fdtbus_regulator_supports_voltage(struct fdtbus_regulator *reg, u_int min_uvol, 176 u_int max_uvol) 177 { 178 struct fdtbus_regulator_controller *rc = reg->reg_rc; 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