Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014-2015 Samsung Electronics |
| 4 | * Przemyslaw Marczak <p.marczak@samsung.com> |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 5 | */ |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 6 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 7 | #include <common.h> |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 8 | #include <errno.h> |
| 9 | #include <dm.h> |
| 10 | #include <dm/uclass-internal.h> |
| 11 | #include <power/pmic.h> |
| 12 | #include <power/regulator.h> |
| 13 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 14 | int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep) |
| 15 | { |
| 16 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 17 | |
| 18 | *modep = NULL; |
| 19 | |
| 20 | uc_pdata = dev_get_uclass_platdata(dev); |
| 21 | if (!uc_pdata) |
| 22 | return -ENXIO; |
| 23 | |
| 24 | *modep = uc_pdata->mode; |
| 25 | return uc_pdata->mode_count; |
| 26 | } |
| 27 | |
| 28 | int regulator_get_value(struct udevice *dev) |
| 29 | { |
| 30 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 31 | |
| 32 | if (!ops || !ops->get_value) |
| 33 | return -ENOSYS; |
| 34 | |
| 35 | return ops->get_value(dev); |
| 36 | } |
| 37 | |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 38 | static void regulator_set_value_ramp_delay(struct udevice *dev, int old_uV, |
| 39 | int new_uV, unsigned int ramp_delay) |
| 40 | { |
| 41 | int delay = DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay); |
| 42 | |
| 43 | debug("regulator %s: delay %u us (%d uV -> %d uV)\n", dev->name, delay, |
| 44 | old_uV, new_uV); |
| 45 | |
| 46 | udelay(delay); |
| 47 | } |
| 48 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 49 | int regulator_set_value(struct udevice *dev, int uV) |
| 50 | { |
| 51 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
Keerthy | c6e6669 | 2016-10-26 13:42:31 +0530 | [diff] [blame] | 52 | struct dm_regulator_uclass_platdata *uc_pdata; |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 53 | int ret, old_uV = uV, is_enabled = 0; |
Keerthy | c6e6669 | 2016-10-26 13:42:31 +0530 | [diff] [blame] | 54 | |
| 55 | uc_pdata = dev_get_uclass_platdata(dev); |
| 56 | if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV) |
| 57 | return -EINVAL; |
| 58 | if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV) |
| 59 | return -EINVAL; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 60 | |
| 61 | if (!ops || !ops->set_value) |
| 62 | return -ENOSYS; |
| 63 | |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 64 | if (uc_pdata->ramp_delay) { |
| 65 | is_enabled = regulator_get_enable(dev); |
| 66 | old_uV = regulator_get_value(dev); |
| 67 | } |
| 68 | |
| 69 | ret = ops->set_value(dev, uV); |
| 70 | |
| 71 | if (!ret) { |
| 72 | if (uc_pdata->ramp_delay && old_uV > 0 && is_enabled) |
| 73 | regulator_set_value_ramp_delay(dev, old_uV, uV, |
| 74 | uc_pdata->ramp_delay); |
| 75 | } |
| 76 | |
| 77 | return ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Keerthy | 162c02e | 2016-10-26 13:42:30 +0530 | [diff] [blame] | 80 | /* |
| 81 | * To be called with at most caution as there is no check |
| 82 | * before setting the actual voltage value. |
| 83 | */ |
| 84 | int regulator_set_value_force(struct udevice *dev, int uV) |
| 85 | { |
| 86 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 87 | |
| 88 | if (!ops || !ops->set_value) |
| 89 | return -ENOSYS; |
| 90 | |
| 91 | return ops->set_value(dev, uV); |
| 92 | } |
| 93 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 94 | int regulator_get_current(struct udevice *dev) |
| 95 | { |
| 96 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 97 | |
| 98 | if (!ops || !ops->get_current) |
| 99 | return -ENOSYS; |
| 100 | |
| 101 | return ops->get_current(dev); |
| 102 | } |
| 103 | |
| 104 | int regulator_set_current(struct udevice *dev, int uA) |
| 105 | { |
| 106 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
Keerthy | ce152be | 2016-10-26 13:42:32 +0530 | [diff] [blame] | 107 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 108 | |
| 109 | uc_pdata = dev_get_uclass_platdata(dev); |
| 110 | if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA) |
| 111 | return -EINVAL; |
| 112 | if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA) |
| 113 | return -EINVAL; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 114 | |
| 115 | if (!ops || !ops->set_current) |
| 116 | return -ENOSYS; |
| 117 | |
| 118 | return ops->set_current(dev, uA); |
| 119 | } |
| 120 | |
Keerthy | 23be7fb | 2017-06-13 09:53:45 +0530 | [diff] [blame] | 121 | int regulator_get_enable(struct udevice *dev) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 122 | { |
| 123 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 124 | |
| 125 | if (!ops || !ops->get_enable) |
| 126 | return -ENOSYS; |
| 127 | |
| 128 | return ops->get_enable(dev); |
| 129 | } |
| 130 | |
| 131 | int regulator_set_enable(struct udevice *dev, bool enable) |
| 132 | { |
| 133 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
Patrick Delaunay | d7585f2 | 2018-11-15 13:45:31 +0100 | [diff] [blame] | 134 | struct dm_regulator_uclass_platdata *uc_pdata; |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 135 | int ret, old_enable = 0; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 136 | |
| 137 | if (!ops || !ops->set_enable) |
| 138 | return -ENOSYS; |
| 139 | |
Patrick Delaunay | d7585f2 | 2018-11-15 13:45:31 +0100 | [diff] [blame] | 140 | uc_pdata = dev_get_uclass_platdata(dev); |
| 141 | if (!enable && uc_pdata->always_on) |
Lokesh Vutla | f870da2 | 2019-01-11 15:15:50 +0530 | [diff] [blame] | 142 | return -EACCES; |
Patrick Delaunay | d7585f2 | 2018-11-15 13:45:31 +0100 | [diff] [blame] | 143 | |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 144 | if (uc_pdata->ramp_delay) |
| 145 | old_enable = regulator_get_enable(dev); |
| 146 | |
| 147 | ret = ops->set_enable(dev, enable); |
| 148 | if (!ret) { |
| 149 | if (uc_pdata->ramp_delay && !old_enable && enable) { |
| 150 | int uV = regulator_get_value(dev); |
| 151 | |
| 152 | if (uV > 0) { |
| 153 | regulator_set_value_ramp_delay(dev, 0, uV, |
| 154 | uc_pdata->ramp_delay); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Lokesh Vutla | 7106b78 | 2019-01-11 15:15:51 +0530 | [diff] [blame] | 162 | int regulator_set_enable_if_allowed(struct udevice *dev, bool enable) |
| 163 | { |
| 164 | int ret; |
| 165 | |
| 166 | ret = regulator_set_enable(dev, enable); |
| 167 | if (ret == -ENOSYS || ret == -EACCES) |
| 168 | return 0; |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 173 | int regulator_get_mode(struct udevice *dev) |
| 174 | { |
| 175 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 176 | |
| 177 | if (!ops || !ops->get_mode) |
| 178 | return -ENOSYS; |
| 179 | |
| 180 | return ops->get_mode(dev); |
| 181 | } |
| 182 | |
| 183 | int regulator_set_mode(struct udevice *dev, int mode) |
| 184 | { |
| 185 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 186 | |
| 187 | if (!ops || !ops->set_mode) |
| 188 | return -ENOSYS; |
| 189 | |
| 190 | return ops->set_mode(dev, mode); |
| 191 | } |
| 192 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 193 | int regulator_get_by_platname(const char *plat_name, struct udevice **devp) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 194 | { |
| 195 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 196 | struct udevice *dev; |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 197 | int ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 198 | |
| 199 | *devp = NULL; |
| 200 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 201 | for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev; |
| 202 | ret = uclass_find_next_device(&dev)) { |
Simon Glass | fc3ebf1 | 2017-05-31 17:57:15 -0600 | [diff] [blame] | 203 | if (ret) { |
| 204 | debug("regulator %s, ret=%d\n", dev->name, ret); |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 205 | continue; |
Simon Glass | fc3ebf1 | 2017-05-31 17:57:15 -0600 | [diff] [blame] | 206 | } |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 207 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 208 | uc_pdata = dev_get_uclass_platdata(dev); |
| 209 | if (!uc_pdata || strcmp(plat_name, uc_pdata->name)) |
| 210 | continue; |
| 211 | |
| 212 | return uclass_get_device_tail(dev, 0, devp); |
| 213 | } |
| 214 | |
Simon Glass | fc3ebf1 | 2017-05-31 17:57:15 -0600 | [diff] [blame] | 215 | debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 216 | |
| 217 | return -ENODEV; |
| 218 | } |
| 219 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 220 | int regulator_get_by_devname(const char *devname, struct udevice **devp) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 221 | { |
| 222 | return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp); |
| 223 | } |
| 224 | |
Przemyslaw Marczak | c900103 | 2015-10-27 13:07:59 +0100 | [diff] [blame] | 225 | int device_get_supply_regulator(struct udevice *dev, const char *supply_name, |
| 226 | struct udevice **devp) |
| 227 | { |
| 228 | return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev, |
| 229 | supply_name, devp); |
| 230 | } |
| 231 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 232 | int regulator_autoset(struct udevice *dev) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 233 | { |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 234 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 235 | int ret = 0; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 236 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 237 | uc_pdata = dev_get_uclass_platdata(dev); |
| 238 | if (!uc_pdata->always_on && !uc_pdata->boot_on) |
| 239 | return -EMEDIUMTYPE; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 240 | |
Sven Schwermer | d033cbf | 2019-06-12 08:32:38 +0200 | [diff] [blame] | 241 | if (uc_pdata->type == REGULATOR_TYPE_FIXED) |
| 242 | return regulator_set_enable(dev, true); |
| 243 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 244 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV) |
| 245 | ret = regulator_set_value(dev, uc_pdata->min_uV); |
| 246 | if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)) |
| 247 | ret = regulator_set_current(dev, uc_pdata->min_uA); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 248 | |
| 249 | if (!ret) |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 250 | ret = regulator_set_enable(dev, true); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 255 | static void regulator_show(struct udevice *dev, int ret) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 256 | { |
| 257 | struct dm_regulator_uclass_platdata *uc_pdata; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 258 | |
| 259 | uc_pdata = dev_get_uclass_platdata(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 260 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 261 | printf("%s@%s: ", dev->name, uc_pdata->name); |
| 262 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV) |
| 263 | printf("set %d uV", uc_pdata->min_uV); |
| 264 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA) |
| 265 | printf("; set %d uA", uc_pdata->min_uA); |
| 266 | printf("; enabling"); |
| 267 | if (ret) |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 268 | printf(" (ret: %d)", ret); |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 269 | printf("\n"); |
| 270 | } |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 271 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 272 | int regulator_autoset_by_name(const char *platname, struct udevice **devp) |
| 273 | { |
| 274 | struct udevice *dev; |
| 275 | int ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 276 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 277 | ret = regulator_get_by_platname(platname, &dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 278 | if (devp) |
| 279 | *devp = dev; |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 280 | if (ret) { |
Simon Glass | fc3ebf1 | 2017-05-31 17:57:15 -0600 | [diff] [blame] | 281 | debug("Can get the regulator: %s (err=%d)\n", platname, ret); |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 282 | return ret; |
| 283 | } |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 284 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 285 | return regulator_autoset(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 288 | int regulator_list_autoset(const char *list_platname[], |
| 289 | struct udevice *list_devp[], |
| 290 | bool verbose) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 291 | { |
| 292 | struct udevice *dev; |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 293 | int error = 0, i = 0, ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 294 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 295 | while (list_platname[i]) { |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 296 | ret = regulator_autoset_by_name(list_platname[i], &dev); |
| 297 | if (ret != -EMEDIUMTYPE && verbose) |
| 298 | regulator_show(dev, ret); |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 299 | if (ret & !error) |
| 300 | error = ret; |
| 301 | |
| 302 | if (list_devp) |
| 303 | list_devp[i] = dev; |
| 304 | |
| 305 | i++; |
| 306 | } |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 307 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 308 | return error; |
| 309 | } |
| 310 | |
| 311 | static bool regulator_name_is_unique(struct udevice *check_dev, |
| 312 | const char *check_name) |
| 313 | { |
| 314 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 315 | struct udevice *dev; |
| 316 | int check_len = strlen(check_name); |
| 317 | int ret; |
| 318 | int len; |
| 319 | |
| 320 | for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev; |
| 321 | ret = uclass_find_next_device(&dev)) { |
| 322 | if (ret || dev == check_dev) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 323 | continue; |
| 324 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 325 | uc_pdata = dev_get_uclass_platdata(dev); |
| 326 | len = strlen(uc_pdata->name); |
| 327 | if (len != check_len) |
| 328 | continue; |
| 329 | |
| 330 | if (!strcmp(uc_pdata->name, check_name)) |
| 331 | return false; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 334 | return true; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static int regulator_post_bind(struct udevice *dev) |
| 338 | { |
| 339 | struct dm_regulator_uclass_platdata *uc_pdata; |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 340 | const char *property = "regulator-name"; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 341 | |
| 342 | uc_pdata = dev_get_uclass_platdata(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 343 | |
| 344 | /* Regulator's mandatory constraint */ |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 345 | uc_pdata->name = dev_read_string(dev, property); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 346 | if (!uc_pdata->name) { |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 347 | debug("%s: dev '%s' has no property '%s'\n", |
| 348 | __func__, dev->name, property); |
| 349 | uc_pdata->name = dev_read_name(dev); |
Peng Fan | cd672d4 | 2015-08-07 16:43:42 +0800 | [diff] [blame] | 350 | if (!uc_pdata->name) |
| 351 | return -EINVAL; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 354 | if (regulator_name_is_unique(dev, uc_pdata->name)) |
| 355 | return 0; |
| 356 | |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 357 | debug("'%s' of dev: '%s', has nonunique value: '%s\n", |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 358 | property, dev->name, uc_pdata->name); |
| 359 | |
| 360 | return -EINVAL; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static int regulator_pre_probe(struct udevice *dev) |
| 364 | { |
| 365 | struct dm_regulator_uclass_platdata *uc_pdata; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 366 | |
| 367 | uc_pdata = dev_get_uclass_platdata(dev); |
| 368 | if (!uc_pdata) |
| 369 | return -ENXIO; |
| 370 | |
| 371 | /* Regulator's optional constraints */ |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 372 | uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt", |
| 373 | -ENODATA); |
| 374 | uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt", |
| 375 | -ENODATA); |
| 376 | uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp", |
| 377 | -ENODATA); |
| 378 | uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp", |
| 379 | -ENODATA); |
| 380 | uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on"); |
| 381 | uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on"); |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 382 | uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay", |
| 383 | 0); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 384 | |
Simon Glass | 991f9bc | 2015-06-23 15:38:57 -0600 | [diff] [blame] | 385 | /* Those values are optional (-ENODATA if unset) */ |
| 386 | if ((uc_pdata->min_uV != -ENODATA) && |
| 387 | (uc_pdata->max_uV != -ENODATA) && |
| 388 | (uc_pdata->min_uV == uc_pdata->max_uV)) |
| 389 | uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV; |
| 390 | |
| 391 | /* Those values are optional (-ENODATA if unset) */ |
| 392 | if ((uc_pdata->min_uA != -ENODATA) && |
| 393 | (uc_pdata->max_uA != -ENODATA) && |
| 394 | (uc_pdata->min_uA == uc_pdata->max_uA)) |
| 395 | uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA; |
| 396 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 400 | int regulators_enable_boot_on(bool verbose) |
| 401 | { |
| 402 | struct udevice *dev; |
| 403 | struct uclass *uc; |
| 404 | int ret; |
| 405 | |
| 406 | ret = uclass_get(UCLASS_REGULATOR, &uc); |
| 407 | if (ret) |
| 408 | return ret; |
| 409 | for (uclass_first_device(UCLASS_REGULATOR, &dev); |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 410 | dev; |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 411 | uclass_next_device(&dev)) { |
| 412 | ret = regulator_autoset(dev); |
Simon Glass | f55c951 | 2015-07-02 18:16:06 -0600 | [diff] [blame] | 413 | if (ret == -EMEDIUMTYPE) { |
| 414 | ret = 0; |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 415 | continue; |
Simon Glass | f55c951 | 2015-07-02 18:16:06 -0600 | [diff] [blame] | 416 | } |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 417 | if (verbose) |
| 418 | regulator_show(dev, ret); |
Simon Glass | d6eddad | 2016-01-21 19:43:59 -0700 | [diff] [blame] | 419 | if (ret == -ENOSYS) |
| 420 | ret = 0; |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | return ret; |
| 424 | } |
| 425 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 426 | UCLASS_DRIVER(regulator) = { |
| 427 | .id = UCLASS_REGULATOR, |
| 428 | .name = "regulator", |
| 429 | .post_bind = regulator_post_bind, |
| 430 | .pre_probe = regulator_pre_probe, |
| 431 | .per_device_platdata_auto_alloc_size = |
| 432 | sizeof(struct dm_regulator_uclass_platdata), |
| 433 | }; |