Przemyslaw Marczak | af41e8d | 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); |
| 44 | |
| 45 | if (!ops || !ops->set_value) |
| 46 | return -ENOSYS; |
| 47 | |
| 48 | return ops->set_value(dev, uV); |
| 49 | } |
| 50 | |
| 51 | int regulator_get_current(struct udevice *dev) |
| 52 | { |
| 53 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 54 | |
| 55 | if (!ops || !ops->get_current) |
| 56 | return -ENOSYS; |
| 57 | |
| 58 | return ops->get_current(dev); |
| 59 | } |
| 60 | |
| 61 | int regulator_set_current(struct udevice *dev, int uA) |
| 62 | { |
| 63 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 64 | |
| 65 | if (!ops || !ops->set_current) |
| 66 | return -ENOSYS; |
| 67 | |
| 68 | return ops->set_current(dev, uA); |
| 69 | } |
| 70 | |
| 71 | bool regulator_get_enable(struct udevice *dev) |
| 72 | { |
| 73 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 74 | |
| 75 | if (!ops || !ops->get_enable) |
| 76 | return -ENOSYS; |
| 77 | |
| 78 | return ops->get_enable(dev); |
| 79 | } |
| 80 | |
| 81 | int regulator_set_enable(struct udevice *dev, bool enable) |
| 82 | { |
| 83 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 84 | |
| 85 | if (!ops || !ops->set_enable) |
| 86 | return -ENOSYS; |
| 87 | |
| 88 | return ops->set_enable(dev, enable); |
| 89 | } |
| 90 | |
| 91 | int regulator_get_mode(struct udevice *dev) |
| 92 | { |
| 93 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 94 | |
| 95 | if (!ops || !ops->get_mode) |
| 96 | return -ENOSYS; |
| 97 | |
| 98 | return ops->get_mode(dev); |
| 99 | } |
| 100 | |
| 101 | int regulator_set_mode(struct udevice *dev, int mode) |
| 102 | { |
| 103 | const struct dm_regulator_ops *ops = dev_get_driver_ops(dev); |
| 104 | |
| 105 | if (!ops || !ops->set_mode) |
| 106 | return -ENOSYS; |
| 107 | |
| 108 | return ops->set_mode(dev, mode); |
| 109 | } |
| 110 | |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 111 | int regulator_get_by_platname(const char *plat_name, struct udevice **devp) |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 112 | { |
| 113 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 114 | struct udevice *dev; |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 115 | int ret; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 116 | |
| 117 | *devp = NULL; |
| 118 | |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 119 | for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev; |
| 120 | ret = uclass_find_next_device(&dev)) { |
| 121 | if (ret) |
| 122 | continue; |
| 123 | |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 124 | uc_pdata = dev_get_uclass_platdata(dev); |
| 125 | if (!uc_pdata || strcmp(plat_name, uc_pdata->name)) |
| 126 | continue; |
| 127 | |
| 128 | return uclass_get_device_tail(dev, 0, devp); |
| 129 | } |
| 130 | |
| 131 | debug("%s: can't find: %s\n", __func__, plat_name); |
| 132 | |
| 133 | return -ENODEV; |
| 134 | } |
| 135 | |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 136 | int regulator_get_by_devname(const char *devname, struct udevice **devp) |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 137 | { |
| 138 | return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp); |
| 139 | } |
| 140 | |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 141 | int regulator_autoset(struct udevice *dev) |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 142 | { |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 143 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 144 | int ret = 0; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 145 | |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 146 | uc_pdata = dev_get_uclass_platdata(dev); |
| 147 | if (!uc_pdata->always_on && !uc_pdata->boot_on) |
| 148 | return -EMEDIUMTYPE; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 149 | |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 150 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV) |
| 151 | ret = regulator_set_value(dev, uc_pdata->min_uV); |
| 152 | if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA)) |
| 153 | ret = regulator_set_current(dev, uc_pdata->min_uA); |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 154 | |
| 155 | if (!ret) |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 156 | ret = regulator_set_enable(dev, true); |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 161 | static void regulator_show(struct udevice *dev, int ret) |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 162 | { |
| 163 | struct dm_regulator_uclass_platdata *uc_pdata; |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 164 | |
| 165 | uc_pdata = dev_get_uclass_platdata(dev); |
| 166 | |
| 167 | printf("%s@%s: ", dev->name, uc_pdata->name); |
| 168 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV) |
| 169 | printf("set %d uV", uc_pdata->min_uV); |
| 170 | if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA) |
| 171 | printf("; set %d uA", uc_pdata->min_uA); |
| 172 | printf("; enabling"); |
| 173 | if (ret) |
| 174 | printf(" (ret: %d)\n", ret); |
| 175 | printf("\n"); |
| 176 | } |
| 177 | |
| 178 | int regulator_autoset_by_name(const char *platname, struct udevice **devp) |
| 179 | { |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 180 | struct udevice *dev; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 181 | int ret; |
| 182 | |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 183 | ret = regulator_get_by_platname(platname, &dev); |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 184 | if (devp) |
| 185 | *devp = dev; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 186 | if (ret) { |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 187 | debug("Can get the regulator: %s!", platname); |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 188 | return ret; |
| 189 | } |
| 190 | |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 191 | return regulator_autoset(dev); |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 194 | int regulator_list_autoset(const char *list_platname[], |
| 195 | struct udevice *list_devp[], |
| 196 | bool verbose) |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 197 | { |
| 198 | struct udevice *dev; |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 199 | int error = 0, i = 0, ret; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 200 | |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 201 | while (list_platname[i]) { |
Simon Glass | 3b55d30 | 2015-06-23 15:38:58 -0600 | [diff] [blame] | 202 | ret = regulator_autoset_by_name(list_platname[i], &dev); |
| 203 | if (ret != -EMEDIUMTYPE && verbose) |
| 204 | regulator_show(dev, ret); |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 205 | if (ret & !error) |
| 206 | error = ret; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 207 | |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 208 | if (list_devp) |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 209 | list_devp[i] = dev; |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 210 | |
| 211 | i++; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 214 | return error; |
| 215 | } |
| 216 | |
| 217 | static bool regulator_name_is_unique(struct udevice *check_dev, |
| 218 | const char *check_name) |
| 219 | { |
| 220 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 221 | struct udevice *dev; |
| 222 | int check_len = strlen(check_name); |
| 223 | int ret; |
| 224 | int len; |
| 225 | |
| 226 | for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev; |
| 227 | ret = uclass_find_next_device(&dev)) { |
| 228 | if (ret || dev == check_dev) |
| 229 | continue; |
| 230 | |
| 231 | uc_pdata = dev_get_uclass_platdata(dev); |
| 232 | len = strlen(uc_pdata->name); |
| 233 | if (len != check_len) |
| 234 | continue; |
| 235 | |
| 236 | if (!strcmp(uc_pdata->name, check_name)) |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | return true; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static int regulator_post_bind(struct udevice *dev) |
| 244 | { |
| 245 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 246 | int offset = dev->of_offset; |
| 247 | const void *blob = gd->fdt_blob; |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 248 | const char *property = "regulator-name"; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 249 | |
| 250 | uc_pdata = dev_get_uclass_platdata(dev); |
| 251 | if (!uc_pdata) |
| 252 | return -ENXIO; |
| 253 | |
| 254 | /* Regulator's mandatory constraint */ |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 255 | uc_pdata->name = fdt_getprop(blob, offset, property, NULL); |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 256 | if (!uc_pdata->name) { |
| 257 | debug("%s: dev: %s has no property 'regulator-name'\n", |
| 258 | __func__, dev->name); |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 259 | return -EINVAL; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 262 | if (regulator_name_is_unique(dev, uc_pdata->name)) |
| 263 | return 0; |
| 264 | |
Simon Glass | 59c26a9 | 2015-06-23 15:39:00 -0600 | [diff] [blame] | 265 | debug("\"%s\" of dev: \"%s\", has nonunique value: \"%s\"", |
Przemyslaw Marczak | 3b88075 | 2015-05-13 13:38:27 +0200 | [diff] [blame] | 266 | property, dev->name, uc_pdata->name); |
| 267 | |
| 268 | return -EINVAL; |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static int regulator_pre_probe(struct udevice *dev) |
| 272 | { |
| 273 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 274 | int offset = dev->of_offset; |
| 275 | |
| 276 | uc_pdata = dev_get_uclass_platdata(dev); |
| 277 | if (!uc_pdata) |
| 278 | return -ENXIO; |
| 279 | |
| 280 | /* Regulator's optional constraints */ |
| 281 | uc_pdata->min_uV = fdtdec_get_int(gd->fdt_blob, offset, |
| 282 | "regulator-min-microvolt", -ENODATA); |
| 283 | uc_pdata->max_uV = fdtdec_get_int(gd->fdt_blob, offset, |
| 284 | "regulator-max-microvolt", -ENODATA); |
| 285 | uc_pdata->min_uA = fdtdec_get_int(gd->fdt_blob, offset, |
| 286 | "regulator-min-microamp", -ENODATA); |
| 287 | uc_pdata->max_uA = fdtdec_get_int(gd->fdt_blob, offset, |
| 288 | "regulator-max-microamp", -ENODATA); |
| 289 | uc_pdata->always_on = fdtdec_get_bool(gd->fdt_blob, offset, |
| 290 | "regulator-always-on"); |
| 291 | uc_pdata->boot_on = fdtdec_get_bool(gd->fdt_blob, offset, |
| 292 | "regulator-boot-on"); |
| 293 | |
Simon Glass | 7837cea | 2015-06-23 15:38:57 -0600 | [diff] [blame] | 294 | /* Those values are optional (-ENODATA if unset) */ |
| 295 | if ((uc_pdata->min_uV != -ENODATA) && |
| 296 | (uc_pdata->max_uV != -ENODATA) && |
| 297 | (uc_pdata->min_uV == uc_pdata->max_uV)) |
| 298 | uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV; |
| 299 | |
| 300 | /* Those values are optional (-ENODATA if unset) */ |
| 301 | if ((uc_pdata->min_uA != -ENODATA) && |
| 302 | (uc_pdata->max_uA != -ENODATA) && |
| 303 | (uc_pdata->min_uA == uc_pdata->max_uA)) |
| 304 | uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA; |
| 305 | |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
Simon Glass | 083fc83 | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 309 | int regulators_enable_boot_on(bool verbose) |
| 310 | { |
| 311 | struct udevice *dev; |
| 312 | struct uclass *uc; |
| 313 | int ret; |
| 314 | |
| 315 | ret = uclass_get(UCLASS_REGULATOR, &uc); |
| 316 | if (ret) |
| 317 | return ret; |
| 318 | for (uclass_first_device(UCLASS_REGULATOR, &dev); |
| 319 | dev && !ret; |
| 320 | uclass_next_device(&dev)) { |
| 321 | ret = regulator_autoset(dev); |
Simon Glass | d08504d | 2015-07-02 18:16:06 -0600 | [diff] [blame] | 322 | if (ret == -EMEDIUMTYPE) { |
| 323 | ret = 0; |
Simon Glass | 083fc83 | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 324 | continue; |
Simon Glass | d08504d | 2015-07-02 18:16:06 -0600 | [diff] [blame] | 325 | } |
Simon Glass | 083fc83 | 2015-06-23 15:38:59 -0600 | [diff] [blame] | 326 | if (verbose) |
| 327 | regulator_show(dev, ret); |
| 328 | } |
| 329 | |
| 330 | return ret; |
| 331 | } |
| 332 | |
Przemyslaw Marczak | af41e8d | 2015-04-20 20:07:42 +0200 | [diff] [blame] | 333 | UCLASS_DRIVER(regulator) = { |
| 334 | .id = UCLASS_REGULATOR, |
| 335 | .name = "regulator", |
| 336 | .post_bind = regulator_post_bind, |
| 337 | .pre_probe = regulator_pre_probe, |
| 338 | .per_device_platdata_auto_alloc_size = |
| 339 | sizeof(struct dm_regulator_uclass_platdata), |
| 340 | }; |