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