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 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_REGULATOR |
| 8 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 12 | #include <dm/uclass-internal.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 14 | #include <power/pmic.h> |
| 15 | #include <power/regulator.h> |
| 16 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 17 | int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep) |
| 18 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 19 | struct dm_regulator_uclass_plat *uc_pdata; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 20 | |
| 21 | *modep = NULL; |
| 22 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 23 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 24 | if (!uc_pdata) |
| 25 | return -ENXIO; |
| 26 | |
| 27 | *modep = uc_pdata->mode; |
| 28 | return uc_pdata->mode_count; |
| 29 | } |
| 30 | |
| 31 | int regulator_get_value(struct udevice *dev) |
| 32 | { |
| 33 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 34 | |
| 35 | if (!ops || !ops->get_value) |
| 36 | return -ENOSYS; |
| 37 | |
| 38 | return ops->get_value(dev); |
| 39 | } |
| 40 | |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 41 | static void regulator_set_value_ramp_delay(struct udevice *dev, int old_uV, |
| 42 | int new_uV, unsigned int ramp_delay) |
| 43 | { |
| 44 | int delay = DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay); |
| 45 | |
| 46 | debug("regulator %s: delay %u us (%d uV -> %d uV)\n", dev->name, delay, |
| 47 | old_uV, new_uV); |
| 48 | |
| 49 | udelay(delay); |
| 50 | } |
| 51 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 52 | int regulator_set_value(struct udevice *dev, int uV) |
| 53 | { |
| 54 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 55 | struct dm_regulator_uclass_plat *uc_pdata; |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 56 | int ret, old_uV = uV, is_enabled = 0; |
Keerthy | c6e6669 | 2016-10-26 13:42:31 +0530 | [diff] [blame] | 57 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 58 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | c6e6669 | 2016-10-26 13:42:31 +0530 | [diff] [blame] | 59 | if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV) |
| 60 | return -EINVAL; |
| 61 | if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV) |
| 62 | return -EINVAL; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 63 | |
| 64 | if (!ops || !ops->set_value) |
| 65 | return -ENOSYS; |
| 66 | |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 67 | if (uc_pdata->ramp_delay) { |
| 68 | is_enabled = regulator_get_enable(dev); |
| 69 | old_uV = regulator_get_value(dev); |
| 70 | } |
| 71 | |
| 72 | ret = ops->set_value(dev, uV); |
| 73 | |
| 74 | if (!ret) { |
| 75 | if (uc_pdata->ramp_delay && old_uV > 0 && is_enabled) |
| 76 | regulator_set_value_ramp_delay(dev, old_uV, uV, |
| 77 | uc_pdata->ramp_delay); |
| 78 | } |
| 79 | |
| 80 | return ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 83 | int regulator_set_suspend_value(struct udevice *dev, int uV) |
| 84 | { |
| 85 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 86 | struct dm_regulator_uclass_plat *uc_pdata; |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 87 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 88 | uc_pdata = dev_get_uclass_plat(dev); |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 89 | if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV) |
| 90 | return -EINVAL; |
| 91 | if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV) |
| 92 | return -EINVAL; |
| 93 | |
| 94 | if (!ops->set_suspend_value) |
| 95 | return -ENOSYS; |
| 96 | |
| 97 | return ops->set_suspend_value(dev, uV); |
| 98 | } |
| 99 | |
| 100 | int regulator_get_suspend_value(struct udevice *dev) |
| 101 | { |
| 102 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 103 | |
| 104 | if (!ops->get_suspend_value) |
| 105 | return -ENOSYS; |
| 106 | |
| 107 | return ops->get_suspend_value(dev); |
| 108 | } |
| 109 | |
Keerthy | 162c02e | 2016-10-26 13:42:30 +0530 | [diff] [blame] | 110 | /* |
| 111 | * To be called with at most caution as there is no check |
| 112 | * before setting the actual voltage value. |
| 113 | */ |
| 114 | int regulator_set_value_force(struct udevice *dev, int uV) |
| 115 | { |
| 116 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 117 | |
| 118 | if (!ops || !ops->set_value) |
| 119 | return -ENOSYS; |
| 120 | |
| 121 | return ops->set_value(dev, uV); |
| 122 | } |
| 123 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 124 | int regulator_get_current(struct udevice *dev) |
| 125 | { |
| 126 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 127 | |
| 128 | if (!ops || !ops->get_current) |
| 129 | return -ENOSYS; |
| 130 | |
| 131 | return ops->get_current(dev); |
| 132 | } |
| 133 | |
| 134 | int regulator_set_current(struct udevice *dev, int uA) |
| 135 | { |
| 136 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 137 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | ce152be | 2016-10-26 13:42:32 +0530 | [diff] [blame] | 138 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 139 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | ce152be | 2016-10-26 13:42:32 +0530 | [diff] [blame] | 140 | if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA) |
| 141 | return -EINVAL; |
| 142 | if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA) |
| 143 | return -EINVAL; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 144 | |
| 145 | if (!ops || !ops->set_current) |
| 146 | return -ENOSYS; |
| 147 | |
| 148 | return ops->set_current(dev, uA); |
| 149 | } |
| 150 | |
Keerthy | 23be7fb | 2017-06-13 09:53:45 +0530 | [diff] [blame] | 151 | int regulator_get_enable(struct udevice *dev) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 152 | { |
| 153 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 154 | |
| 155 | if (!ops || !ops->get_enable) |
| 156 | return -ENOSYS; |
| 157 | |
| 158 | return ops->get_enable(dev); |
| 159 | } |
| 160 | |
| 161 | int regulator_set_enable(struct udevice *dev, bool enable) |
| 162 | { |
| 163 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 164 | struct dm_regulator_uclass_plat *uc_pdata; |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 165 | int ret, old_enable = 0; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 166 | |
| 167 | if (!ops || !ops->set_enable) |
| 168 | return -ENOSYS; |
| 169 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 170 | uc_pdata = dev_get_uclass_plat(dev); |
Patrick Delaunay | d7585f2 | 2018-11-15 13:45:31 +0100 | [diff] [blame] | 171 | if (!enable && uc_pdata->always_on) |
Lokesh Vutla | f870da2 | 2019-01-11 15:15:50 +0530 | [diff] [blame] | 172 | return -EACCES; |
Patrick Delaunay | d7585f2 | 2018-11-15 13:45:31 +0100 | [diff] [blame] | 173 | |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 174 | if (uc_pdata->ramp_delay) |
| 175 | old_enable = regulator_get_enable(dev); |
| 176 | |
| 177 | ret = ops->set_enable(dev, enable); |
| 178 | if (!ret) { |
| 179 | if (uc_pdata->ramp_delay && !old_enable && enable) { |
| 180 | int uV = regulator_get_value(dev); |
| 181 | |
| 182 | if (uV > 0) { |
| 183 | regulator_set_value_ramp_delay(dev, 0, uV, |
| 184 | uc_pdata->ramp_delay); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Lokesh Vutla | 7106b78 | 2019-01-11 15:15:51 +0530 | [diff] [blame] | 192 | int regulator_set_enable_if_allowed(struct udevice *dev, bool enable) |
| 193 | { |
| 194 | int ret; |
| 195 | |
| 196 | ret = regulator_set_enable(dev, enable); |
| 197 | if (ret == -ENOSYS || ret == -EACCES) |
| 198 | return 0; |
Eugen Hristev | 5a1c0f3 | 2023-04-19 16:45:26 +0300 | [diff] [blame] | 199 | /* if we want to disable but it's in use by someone else */ |
| 200 | if (!enable && ret == -EBUSY) |
| 201 | return 0; |
| 202 | /* if it's already enabled/disabled */ |
| 203 | if (ret == -EALREADY) |
| 204 | return 0; |
Lokesh Vutla | 7106b78 | 2019-01-11 15:15:51 +0530 | [diff] [blame] | 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 209 | int regulator_set_suspend_enable(struct udevice *dev, bool enable) |
| 210 | { |
| 211 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 212 | |
| 213 | if (!ops->set_suspend_enable) |
| 214 | return -ENOSYS; |
| 215 | |
| 216 | return ops->set_suspend_enable(dev, enable); |
| 217 | } |
| 218 | |
| 219 | int regulator_get_suspend_enable(struct udevice *dev) |
| 220 | { |
| 221 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 222 | |
| 223 | if (!ops->get_suspend_enable) |
| 224 | return -ENOSYS; |
| 225 | |
| 226 | return ops->get_suspend_enable(dev); |
| 227 | } |
| 228 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 229 | int regulator_get_mode(struct udevice *dev) |
| 230 | { |
| 231 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 232 | |
| 233 | if (!ops || !ops->get_mode) |
| 234 | return -ENOSYS; |
| 235 | |
| 236 | return ops->get_mode(dev); |
| 237 | } |
| 238 | |
| 239 | int regulator_set_mode(struct udevice *dev, int mode) |
| 240 | { |
| 241 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 242 | |
| 243 | if (!ops || !ops->set_mode) |
| 244 | return -ENOSYS; |
| 245 | |
| 246 | return ops->set_mode(dev, mode); |
| 247 | } |
| 248 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 249 | int regulator_get_by_platname(const char *plat_name, struct udevice **devp) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 250 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 251 | struct dm_regulator_uclass_plat *uc_pdata; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 252 | struct udevice *dev; |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 253 | int ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 254 | |
| 255 | *devp = NULL; |
| 256 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 257 | for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev; |
| 258 | ret = uclass_find_next_device(&dev)) { |
Simon Glass | fc3ebf1 | 2017-05-31 17:57:15 -0600 | [diff] [blame] | 259 | if (ret) { |
| 260 | debug("regulator %s, ret=%d\n", dev->name, ret); |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 261 | continue; |
Simon Glass | fc3ebf1 | 2017-05-31 17:57:15 -0600 | [diff] [blame] | 262 | } |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 263 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 264 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 265 | if (!uc_pdata || strcmp(plat_name, uc_pdata->name)) |
| 266 | continue; |
| 267 | |
| 268 | return uclass_get_device_tail(dev, 0, devp); |
| 269 | } |
| 270 | |
Simon Glass | fc3ebf1 | 2017-05-31 17:57:15 -0600 | [diff] [blame] | 271 | 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] | 272 | |
| 273 | return -ENODEV; |
| 274 | } |
| 275 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 276 | int regulator_get_by_devname(const char *devname, struct udevice **devp) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 277 | { |
| 278 | return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp); |
| 279 | } |
| 280 | |
Przemyslaw Marczak | c900103 | 2015-10-27 13:07:59 +0100 | [diff] [blame] | 281 | int device_get_supply_regulator(struct udevice *dev, const char *supply_name, |
| 282 | struct udevice **devp) |
| 283 | { |
| 284 | return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev, |
| 285 | supply_name, devp); |
| 286 | } |
| 287 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 288 | int regulator_autoset(struct udevice *dev) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 289 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 290 | struct dm_regulator_uclass_plat *uc_pdata; |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 291 | int ret = 0; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 292 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 293 | uc_pdata = dev_get_uclass_plat(dev); |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 294 | |
Jonas Karlman | c2ef881 | 2023-08-21 22:30:24 +0000 | [diff] [blame] | 295 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_DONE) |
| 296 | return -EALREADY; |
| 297 | |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 298 | ret = regulator_set_suspend_enable(dev, uc_pdata->suspend_on); |
John Keeping | 6ec6a07 | 2022-09-08 12:38:35 +0100 | [diff] [blame] | 299 | if (ret == -ENOSYS) |
| 300 | ret = 0; |
| 301 | |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 302 | if (!ret && uc_pdata->suspend_on) { |
| 303 | ret = regulator_set_suspend_value(dev, uc_pdata->suspend_uV); |
John Keeping | 6ec6a07 | 2022-09-08 12:38:35 +0100 | [diff] [blame] | 304 | if (ret == -ENOSYS) |
| 305 | ret = 0; |
| 306 | |
| 307 | if (ret) |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 308 | return ret; |
| 309 | } |
| 310 | |
Jonas Karlman | c2ef881 | 2023-08-21 22:30:24 +0000 | [diff] [blame] | 311 | if (!uc_pdata->always_on && !uc_pdata->boot_on) { |
| 312 | ret = -EMEDIUMTYPE; |
| 313 | goto out; |
| 314 | } |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 315 | |
Jonas Karlman | c2ef881 | 2023-08-21 22:30:24 +0000 | [diff] [blame] | 316 | if (uc_pdata->type == REGULATOR_TYPE_FIXED) { |
| 317 | ret = regulator_set_enable(dev, true); |
| 318 | goto out; |
| 319 | } |
Sven Schwermer | d033cbf | 2019-06-12 08:32:38 +0200 | [diff] [blame] | 320 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 321 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV) |
| 322 | ret = regulator_set_value(dev, uc_pdata->min_uV); |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 323 | if (uc_pdata->init_uV > 0) |
| 324 | ret = regulator_set_value(dev, uc_pdata->init_uV); |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 325 | if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)) |
| 326 | ret = regulator_set_current(dev, uc_pdata->min_uA); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 327 | |
| 328 | if (!ret) |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 329 | ret = regulator_set_enable(dev, true); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 330 | |
Jonas Karlman | c2ef881 | 2023-08-21 22:30:24 +0000 | [diff] [blame] | 331 | out: |
| 332 | uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_DONE; |
| 333 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 334 | return ret; |
| 335 | } |
| 336 | |
Konstantin Porotchkin | 621dab5 | 2017-05-29 15:59:38 +0300 | [diff] [blame] | 337 | int regulator_unset(struct udevice *dev) |
| 338 | { |
| 339 | struct dm_regulator_uclass_plat *uc_pdata; |
| 340 | |
| 341 | uc_pdata = dev_get_uclass_plat(dev); |
| 342 | if (uc_pdata && uc_pdata->force_off) |
| 343 | return regulator_set_enable(dev, false); |
| 344 | |
| 345 | return -EMEDIUMTYPE; |
| 346 | } |
| 347 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 348 | static void regulator_show(struct udevice *dev, int ret) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 349 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 350 | struct dm_regulator_uclass_plat *uc_pdata; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 351 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 352 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 353 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 354 | printf("%s@%s: ", dev->name, uc_pdata->name); |
| 355 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV) |
| 356 | printf("set %d uV", uc_pdata->min_uV); |
| 357 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA) |
| 358 | printf("; set %d uA", uc_pdata->min_uA); |
| 359 | printf("; enabling"); |
| 360 | if (ret) |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 361 | printf(" (ret: %d)", ret); |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 362 | printf("\n"); |
| 363 | } |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 364 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 365 | int regulator_autoset_by_name(const char *platname, struct udevice **devp) |
| 366 | { |
| 367 | struct udevice *dev; |
| 368 | int ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 369 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 370 | ret = regulator_get_by_platname(platname, &dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 371 | if (devp) |
| 372 | *devp = dev; |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 373 | if (ret) { |
Simon Glass | fc3ebf1 | 2017-05-31 17:57:15 -0600 | [diff] [blame] | 374 | debug("Can get the regulator: %s (err=%d)\n", platname, ret); |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 375 | return ret; |
| 376 | } |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 377 | |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 378 | return regulator_autoset(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 381 | int regulator_list_autoset(const char *list_platname[], |
| 382 | struct udevice *list_devp[], |
| 383 | bool verbose) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 384 | { |
| 385 | struct udevice *dev; |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 386 | int error = 0, i = 0, ret; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 387 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 388 | while (list_platname[i]) { |
Simon Glass | 46cb824 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 389 | ret = regulator_autoset_by_name(list_platname[i], &dev); |
| 390 | if (ret != -EMEDIUMTYPE && verbose) |
| 391 | regulator_show(dev, ret); |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 392 | if (ret & !error) |
| 393 | error = ret; |
| 394 | |
| 395 | if (list_devp) |
| 396 | list_devp[i] = dev; |
| 397 | |
| 398 | i++; |
| 399 | } |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 400 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 401 | return error; |
| 402 | } |
| 403 | |
| 404 | static bool regulator_name_is_unique(struct udevice *check_dev, |
| 405 | const char *check_name) |
| 406 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 407 | struct dm_regulator_uclass_plat *uc_pdata; |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 408 | struct udevice *dev; |
| 409 | int check_len = strlen(check_name); |
| 410 | int ret; |
| 411 | int len; |
| 412 | |
| 413 | for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev; |
| 414 | ret = uclass_find_next_device(&dev)) { |
| 415 | if (ret || dev == check_dev) |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 416 | continue; |
| 417 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 418 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 419 | len = strlen(uc_pdata->name); |
| 420 | if (len != check_len) |
| 421 | continue; |
| 422 | |
| 423 | if (!strcmp(uc_pdata->name, check_name)) |
| 424 | return false; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 427 | return true; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static int regulator_post_bind(struct udevice *dev) |
| 431 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 432 | struct dm_regulator_uclass_plat *uc_pdata; |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 433 | const char *property = "regulator-name"; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 434 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 435 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 436 | |
| 437 | /* Regulator's mandatory constraint */ |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 438 | uc_pdata->name = dev_read_string(dev, property); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 439 | if (!uc_pdata->name) { |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 440 | debug("%s: dev '%s' has no property '%s'\n", |
| 441 | __func__, dev->name, property); |
| 442 | uc_pdata->name = dev_read_name(dev); |
Peng Fan | cd672d4 | 2015-08-07 16:43:42 +0800 | [diff] [blame] | 443 | if (!uc_pdata->name) |
| 444 | return -EINVAL; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 447 | if (regulator_name_is_unique(dev, uc_pdata->name)) |
| 448 | return 0; |
| 449 | |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 450 | debug("'%s' of dev: '%s', has nonunique value: '%s\n", |
Przemyslaw Marczak | 75692a3 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 451 | property, dev->name, uc_pdata->name); |
| 452 | |
| 453 | return -EINVAL; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static int regulator_pre_probe(struct udevice *dev) |
| 457 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 458 | struct dm_regulator_uclass_plat *uc_pdata; |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 459 | ofnode node; |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 460 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 461 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 462 | if (!uc_pdata) |
| 463 | return -ENXIO; |
| 464 | |
| 465 | /* Regulator's optional constraints */ |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 466 | uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt", |
| 467 | -ENODATA); |
| 468 | uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt", |
| 469 | -ENODATA); |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 470 | uc_pdata->init_uV = dev_read_u32_default(dev, "regulator-init-microvolt", |
| 471 | -ENODATA); |
Simon Glass | eeb99de | 2017-05-18 20:09:34 -0600 | [diff] [blame] | 472 | uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp", |
| 473 | -ENODATA); |
| 474 | uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp", |
| 475 | -ENODATA); |
| 476 | uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on"); |
| 477 | uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on"); |
Krzysztof Kozlowski | e2fa397 | 2019-03-06 19:37:54 +0100 | [diff] [blame] | 478 | uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay", |
| 479 | 0); |
Konstantin Porotchkin | 621dab5 | 2017-05-29 15:59:38 +0300 | [diff] [blame] | 480 | uc_pdata->force_off = dev_read_bool(dev, "regulator-force-boot-off"); |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 481 | |
Joseph Chen | bb51132 | 2019-09-26 15:43:52 +0800 | [diff] [blame] | 482 | node = dev_read_subnode(dev, "regulator-state-mem"); |
| 483 | if (ofnode_valid(node)) { |
| 484 | uc_pdata->suspend_on = !ofnode_read_bool(node, "regulator-off-in-suspend"); |
| 485 | if (ofnode_read_u32(node, "regulator-suspend-microvolt", &uc_pdata->suspend_uV)) |
| 486 | uc_pdata->suspend_uV = uc_pdata->max_uV; |
| 487 | } else { |
| 488 | uc_pdata->suspend_on = true; |
| 489 | uc_pdata->suspend_uV = uc_pdata->max_uV; |
| 490 | } |
| 491 | |
Simon Glass | 991f9bc | 2015-06-23 15:38:57 -0600 | [diff] [blame] | 492 | /* Those values are optional (-ENODATA if unset) */ |
| 493 | if ((uc_pdata->min_uV != -ENODATA) && |
| 494 | (uc_pdata->max_uV != -ENODATA) && |
| 495 | (uc_pdata->min_uV == uc_pdata->max_uV)) |
| 496 | uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV; |
| 497 | |
| 498 | /* Those values are optional (-ENODATA if unset) */ |
| 499 | if ((uc_pdata->min_uA != -ENODATA) && |
| 500 | (uc_pdata->max_uA != -ENODATA) && |
| 501 | (uc_pdata->min_uA == uc_pdata->max_uA)) |
| 502 | uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA; |
| 503 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 507 | int regulators_enable_boot_on(bool verbose) |
| 508 | { |
| 509 | struct udevice *dev; |
| 510 | struct uclass *uc; |
| 511 | int ret; |
| 512 | |
| 513 | ret = uclass_get(UCLASS_REGULATOR, &uc); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | for (uclass_first_device(UCLASS_REGULATOR, &dev); |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 517 | dev; |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 518 | uclass_next_device(&dev)) { |
| 519 | ret = regulator_autoset(dev); |
Simon Glass | f55c951 | 2015-07-02 18:16:06 -0600 | [diff] [blame] | 520 | if (ret == -EMEDIUMTYPE) { |
| 521 | ret = 0; |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 522 | continue; |
Simon Glass | f55c951 | 2015-07-02 18:16:06 -0600 | [diff] [blame] | 523 | } |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 524 | if (verbose) |
| 525 | regulator_show(dev, ret); |
Simon Glass | d6eddad | 2016-01-21 19:43:59 -0700 | [diff] [blame] | 526 | if (ret == -ENOSYS) |
| 527 | ret = 0; |
Simon Glass | efeeb2c | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | return ret; |
| 531 | } |
| 532 | |
Konstantin Porotchkin | 621dab5 | 2017-05-29 15:59:38 +0300 | [diff] [blame] | 533 | int regulators_enable_boot_off(bool verbose) |
| 534 | { |
| 535 | struct udevice *dev; |
| 536 | struct uclass *uc; |
| 537 | int ret; |
| 538 | |
| 539 | ret = uclass_get(UCLASS_REGULATOR, &uc); |
| 540 | if (ret) |
| 541 | return ret; |
| 542 | for (uclass_first_device(UCLASS_REGULATOR, &dev); |
| 543 | dev; |
| 544 | uclass_next_device(&dev)) { |
| 545 | ret = regulator_unset(dev); |
| 546 | if (ret == -EMEDIUMTYPE) { |
| 547 | ret = 0; |
| 548 | continue; |
| 549 | } |
| 550 | if (verbose) |
| 551 | regulator_show(dev, ret); |
| 552 | if (ret == -ENOSYS) |
| 553 | ret = 0; |
| 554 | } |
| 555 | |
| 556 | return ret; |
| 557 | } |
| 558 | |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 559 | UCLASS_DRIVER(regulator) = { |
| 560 | .id = UCLASS_REGULATOR, |
| 561 | .name = "regulator", |
| 562 | .post_bind = regulator_post_bind, |
| 563 | .pre_probe = regulator_pre_probe, |
Simon Glass | 33b2efb | 2020-12-03 16:55:22 -0700 | [diff] [blame] | 564 | .per_device_plat_auto = sizeof(struct dm_regulator_uclass_plat), |
Przemyslaw Marczak | 08edd00 | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 565 | }; |