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 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 241 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV) |
| 242 | ret = regulator_set_value(dev, uc_pdata->min_uV); |
| 243 | if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)) |
| 244 | ret = regulator_set_current(dev, uc_pdata->min_uA); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 245 | |
| 246 | if (!ret) |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 247 | ret = regulator_set_enable(dev, true); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 248 | |
| 249 | return ret; |
| 250 | } |
| 251 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 252 | static void regulator_show(struct udevice *dev, int ret) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 253 | { |
| 254 | struct dm_regulator_uclass_platdata *uc_pdata; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 255 | |
| 256 | uc_pdata = dev_get_uclass_platdata(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 257 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 258 | printf("%s@%s: ", dev->name, uc_pdata->name); |
| 259 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV) |
| 260 | printf("set %d uV", uc_pdata->min_uV); |
| 261 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA) |
| 262 | printf("; set %d uA", uc_pdata->min_uA); |
| 263 | printf("; enabling"); |
| 264 | if (ret) |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 265 | printf(" (ret: %d)", ret); |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 266 | printf("\n"); |
| 267 | } |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 268 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 269 | int regulator_autoset_by_name(const char *platname, struct udevice **devp) |
| 270 | { |
| 271 | struct udevice *dev; |
| 272 | int ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 273 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 274 | ret = regulator_get_by_platname(platname, &dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 275 | if (devp) |
| 276 | *devp = dev; |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 277 | if (ret) { |
Simon Glass | fc3ebf1 | 2017-05-31 17:57:15 -0600 | [diff] [blame] | 278 | debug("Can get the regulator: %s (err=%d)\n", platname, ret); |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 279 | return ret; |
| 280 | } |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 281 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 282 | return regulator_autoset(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 285 | int regulator_list_autoset(const char *list_platname[], |
| 286 | struct udevice *list_devp[], |
| 287 | bool verbose) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 288 | { |
| 289 | struct udevice *dev; |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 290 | int error = 0, i = 0, ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 291 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 292 | while (list_platname[i]) { |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 293 | ret = regulator_autoset_by_name(list_platname[i], &dev); |
| 294 | if (ret != -EMEDIUMTYPE && verbose) |
| 295 | regulator_show(dev, ret); |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 296 | if (ret & !error) |
| 297 | error = ret; |
| 298 | |
| 299 | if (list_devp) |
| 300 | list_devp[i] = dev; |
| 301 | |
| 302 | i++; |
| 303 | } |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 304 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 305 | return error; |
| 306 | } |
| 307 | |
| 308 | static bool regulator_name_is_unique(struct udevice *check_dev, |
| 309 | const char *check_name) |
| 310 | { |
| 311 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 312 | struct udevice *dev; |
| 313 | int check_len = strlen(check_name); |
| 314 | int ret; |
| 315 | int len; |
| 316 | |
| 317 | for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev; |
| 318 | ret = uclass_find_next_device(&dev)) { |
| 319 | if (ret || dev == check_dev) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 320 | continue; |
| 321 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 322 | uc_pdata = dev_get_uclass_platdata(dev); |
| 323 | len = strlen(uc_pdata->name); |
| 324 | if (len != check_len) |
| 325 | continue; |
| 326 | |
| 327 | if (!strcmp(uc_pdata->name, check_name)) |
| 328 | return false; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 331 | return true; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static int regulator_post_bind(struct udevice *dev) |
| 335 | { |
| 336 | struct dm_regulator_uclass_platdata *uc_pdata; |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 337 | const char *property = "regulator-name"; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 338 | |
| 339 | uc_pdata = dev_get_uclass_platdata(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 340 | |
| 341 | /* Regulator's mandatory constraint */ |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 342 | uc_pdata->name = dev_read_string(dev, property); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 343 | if (!uc_pdata->name) { |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 344 | debug("%s: dev '%s' has no property '%s'\n", |
| 345 | __func__, dev->name, property); |
| 346 | uc_pdata->name = dev_read_name(dev); |
Peng Fan | cd672d4 | 2015-08-07 16:43:42 +0800 | [diff] [blame] | 347 | if (!uc_pdata->name) |
| 348 | return -EINVAL; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 351 | if (regulator_name_is_unique(dev, uc_pdata->name)) |
| 352 | return 0; |
| 353 | |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 354 | debug("'%s' of dev: '%s', has nonunique value: '%s\n", |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 355 | property, dev->name, uc_pdata->name); |
| 356 | |
| 357 | return -EINVAL; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static int regulator_pre_probe(struct udevice *dev) |
| 361 | { |
| 362 | struct dm_regulator_uclass_platdata *uc_pdata; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 363 | |
| 364 | uc_pdata = dev_get_uclass_platdata(dev); |
| 365 | if (!uc_pdata) |
| 366 | return -ENXIO; |
| 367 | |
| 368 | /* Regulator's optional constraints */ |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 369 | uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt", |
| 370 | -ENODATA); |
| 371 | uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt", |
| 372 | -ENODATA); |
| 373 | uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp", |
| 374 | -ENODATA); |
| 375 | uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp", |
| 376 | -ENODATA); |
| 377 | uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on"); |
| 378 | uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on"); |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 379 | uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay", |
| 380 | 0); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 381 | |
Simon Glass | 991f9bc | 2015-06-23 15:38:57 -0600 | [diff] [blame] | 382 | /* Those values are optional (-ENODATA if unset) */ |
| 383 | if ((uc_pdata->min_uV != -ENODATA) && |
| 384 | (uc_pdata->max_uV != -ENODATA) && |
| 385 | (uc_pdata->min_uV == uc_pdata->max_uV)) |
| 386 | uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV; |
| 387 | |
| 388 | /* Those values are optional (-ENODATA if unset) */ |
| 389 | if ((uc_pdata->min_uA != -ENODATA) && |
| 390 | (uc_pdata->max_uA != -ENODATA) && |
| 391 | (uc_pdata->min_uA == uc_pdata->max_uA)) |
| 392 | uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA; |
| 393 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 397 | int regulators_enable_boot_on(bool verbose) |
| 398 | { |
| 399 | struct udevice *dev; |
| 400 | struct uclass *uc; |
| 401 | int ret; |
| 402 | |
| 403 | ret = uclass_get(UCLASS_REGULATOR, &uc); |
| 404 | if (ret) |
| 405 | return ret; |
| 406 | for (uclass_first_device(UCLASS_REGULATOR, &dev); |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 407 | dev; |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 408 | uclass_next_device(&dev)) { |
| 409 | ret = regulator_autoset(dev); |
Simon Glass | f55c951 | 2015-07-02 18:16:06 -0600 | [diff] [blame] | 410 | if (ret == -EMEDIUMTYPE) { |
| 411 | ret = 0; |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 412 | continue; |
Simon Glass | f55c951 | 2015-07-02 18:16:06 -0600 | [diff] [blame] | 413 | } |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 414 | if (verbose) |
| 415 | regulator_show(dev, ret); |
Simon Glass | d6eddad | 2016-01-21 19:43:59 -0700 | [diff] [blame] | 416 | if (ret == -ENOSYS) |
| 417 | ret = 0; |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return ret; |
| 421 | } |
| 422 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 423 | UCLASS_DRIVER(regulator) = { |
| 424 | .id = UCLASS_REGULATOR, |
| 425 | .name = "regulator", |
| 426 | .post_bind = regulator_post_bind, |
| 427 | .pre_probe = regulator_pre_probe, |
| 428 | .per_device_platdata_auto_alloc_size = |
| 429 | sizeof(struct dm_regulator_uclass_platdata), |
| 430 | }; |