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