| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | // |
| 3 | // core.c -- Voltage/Current Regulator framework. |
| 4 | // |
| 5 | // Copyright 2007, 2008 Wolfson Microelectronics PLC. |
| 6 | // Copyright 2008 SlimLogic Ltd. |
| 7 | // |
| 8 | // Author: Liam Girdwood <lrg@slimlogic.co.uk> |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/debugfs.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/async.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/suspend.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/gpio/consumer.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/reboot.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/regulator/of_regulator.h> |
| 25 | #include <linux/regulator/consumer.h> |
| 26 | #include <linux/regulator/coupler.h> |
| 27 | #include <linux/regulator/driver.h> |
| 28 | #include <linux/regulator/machine.h> |
| 29 | #include <linux/module.h> |
| 30 | |
| 31 | #define CREATE_TRACE_POINTS |
| 32 | #include <trace/events/regulator.h> |
| 33 | |
| 34 | #include "dummy.h" |
| 35 | #include "internal.h" |
| 36 | #include "regnl.h" |
| 37 | |
| 38 | static DEFINE_WW_CLASS(regulator_ww_class); |
| 39 | static DEFINE_MUTEX(regulator_nesting_mutex); |
| 40 | static DEFINE_MUTEX(regulator_list_mutex); |
| 41 | static LIST_HEAD(regulator_map_list); |
| 42 | static LIST_HEAD(regulator_ena_gpio_list); |
| 43 | static LIST_HEAD(regulator_supply_alias_list); |
| 44 | static LIST_HEAD(regulator_coupler_list); |
| 45 | static bool has_full_constraints; |
| 46 | |
| 47 | static struct dentry *debugfs_root; |
| 48 | |
| 49 | /* |
| 50 | * struct regulator_map |
| 51 | * |
| 52 | * Used to provide symbolic supply names to devices. |
| 53 | */ |
| 54 | struct regulator_map { |
| 55 | struct list_head list; |
| 56 | const char *dev_name; /* The dev_name() for the consumer */ |
| 57 | const char *supply; |
| 58 | struct regulator_dev *regulator; |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * struct regulator_enable_gpio |
| 63 | * |
| 64 | * Management for shared enable GPIO pin |
| 65 | */ |
| 66 | struct regulator_enable_gpio { |
| 67 | struct list_head list; |
| 68 | struct gpio_desc *gpiod; |
| 69 | u32 enable_count; /* a number of enabled shared GPIO */ |
| 70 | u32 request_count; /* a number of requested shared GPIO */ |
| 71 | }; |
| 72 | |
| 73 | /* |
| 74 | * struct regulator_supply_alias |
| 75 | * |
| 76 | * Used to map lookups for a supply onto an alternative device. |
| 77 | */ |
| 78 | struct regulator_supply_alias { |
| 79 | struct list_head list; |
| 80 | struct device *src_dev; |
| 81 | const char *src_supply; |
| 82 | struct device *alias_dev; |
| 83 | const char *alias_supply; |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * Work item used to forward regulator events. |
| 88 | * |
| 89 | * @work: workqueue entry |
| 90 | * @rdev: regulator device to notify (consumer receiving the forwarded event) |
| 91 | * @event: event code to be forwarded |
| 92 | */ |
| 93 | struct regulator_event_work { |
| 94 | struct work_struct work; |
| 95 | struct regulator_dev *rdev; |
| 96 | unsigned long event; |
| 97 | }; |
| 98 | |
| 99 | static int _regulator_is_enabled(struct regulator_dev *rdev); |
| 100 | static int _regulator_disable(struct regulator *regulator); |
| 101 | static int _regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags); |
| 102 | static int _regulator_get_current_limit(struct regulator_dev *rdev); |
| 103 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev); |
| 104 | static int _notifier_call_chain(struct regulator_dev *rdev, |
| 105 | unsigned long event, void *data); |
| 106 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
| 107 | int min_uV, int max_uV); |
| 108 | static int regulator_balance_voltage(struct regulator_dev *rdev, |
| 109 | suspend_state_t state); |
| 110 | static struct regulator *create_regulator(struct regulator_dev *rdev, |
| 111 | struct device *dev, |
| 112 | const char *supply_name); |
| 113 | static void destroy_regulator(struct regulator *regulator); |
| 114 | static void _regulator_put(struct regulator *regulator); |
| 115 | |
| 116 | const char *rdev_get_name(struct regulator_dev *rdev) |
| 117 | { |
| 118 | if (rdev->constraints && rdev->constraints->name) |
| 119 | return rdev->constraints->name; |
| 120 | else if (rdev->desc->name) |
| 121 | return rdev->desc->name; |
| 122 | else |
| 123 | return "" ; |
| 124 | } |
| 125 | EXPORT_SYMBOL_GPL(rdev_get_name); |
| 126 | |
| 127 | static bool have_full_constraints(void) |
| 128 | { |
| 129 | return has_full_constraints || of_have_populated_dt(); |
| 130 | } |
| 131 | |
| 132 | static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops) |
| 133 | { |
| 134 | if (!rdev->constraints) { |
| 135 | rdev_err(rdev, "no constraints\n" ); |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | if (rdev->constraints->valid_ops_mask & ops) |
| 140 | return true; |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * regulator_lock_nested - lock a single regulator |
| 147 | * @rdev: regulator source |
| 148 | * @ww_ctx: w/w mutex acquire context |
| 149 | * |
| 150 | * This function can be called many times by one task on |
| 151 | * a single regulator and its mutex will be locked only |
| 152 | * once. If a task, which is calling this function is other |
| 153 | * than the one, which initially locked the mutex, it will |
| 154 | * wait on mutex. |
| 155 | * |
| 156 | * Return: 0 on success or a negative error number on failure. |
| 157 | */ |
| 158 | static inline int regulator_lock_nested(struct regulator_dev *rdev, |
| 159 | struct ww_acquire_ctx *ww_ctx) |
| 160 | { |
| 161 | bool lock = false; |
| 162 | int ret = 0; |
| 163 | |
| 164 | mutex_lock(®ulator_nesting_mutex); |
| 165 | |
| 166 | if (!ww_mutex_trylock(lock: &rdev->mutex, ctx: ww_ctx)) { |
| 167 | if (rdev->mutex_owner == current) |
| 168 | rdev->ref_cnt++; |
| 169 | else |
| 170 | lock = true; |
| 171 | |
| 172 | if (lock) { |
| 173 | mutex_unlock(lock: ®ulator_nesting_mutex); |
| 174 | ret = ww_mutex_lock(lock: &rdev->mutex, ctx: ww_ctx); |
| 175 | mutex_lock(®ulator_nesting_mutex); |
| 176 | } |
| 177 | } else { |
| 178 | lock = true; |
| 179 | } |
| 180 | |
| 181 | if (lock && ret != -EDEADLK) { |
| 182 | rdev->ref_cnt++; |
| 183 | rdev->mutex_owner = current; |
| 184 | } |
| 185 | |
| 186 | mutex_unlock(lock: ®ulator_nesting_mutex); |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * regulator_lock - lock a single regulator |
| 193 | * @rdev: regulator source |
| 194 | * |
| 195 | * This function can be called many times by one task on |
| 196 | * a single regulator and its mutex will be locked only |
| 197 | * once. If a task, which is calling this function is other |
| 198 | * than the one, which initially locked the mutex, it will |
| 199 | * wait on mutex. |
| 200 | */ |
| 201 | static void regulator_lock(struct regulator_dev *rdev) |
| 202 | { |
| 203 | regulator_lock_nested(rdev, NULL); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * regulator_unlock - unlock a single regulator |
| 208 | * @rdev: regulator_source |
| 209 | * |
| 210 | * This function unlocks the mutex when the |
| 211 | * reference counter reaches 0. |
| 212 | */ |
| 213 | static void regulator_unlock(struct regulator_dev *rdev) |
| 214 | { |
| 215 | mutex_lock(®ulator_nesting_mutex); |
| 216 | |
| 217 | if (--rdev->ref_cnt == 0) { |
| 218 | rdev->mutex_owner = NULL; |
| 219 | ww_mutex_unlock(lock: &rdev->mutex); |
| 220 | } |
| 221 | |
| 222 | WARN_ON_ONCE(rdev->ref_cnt < 0); |
| 223 | |
| 224 | mutex_unlock(lock: ®ulator_nesting_mutex); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * regulator_lock_two - lock two regulators |
| 229 | * @rdev1: first regulator |
| 230 | * @rdev2: second regulator |
| 231 | * @ww_ctx: w/w mutex acquire context |
| 232 | * |
| 233 | * Locks both rdevs using the regulator_ww_class. |
| 234 | */ |
| 235 | static void regulator_lock_two(struct regulator_dev *rdev1, |
| 236 | struct regulator_dev *rdev2, |
| 237 | struct ww_acquire_ctx *ww_ctx) |
| 238 | { |
| 239 | struct regulator_dev *held, *contended; |
| 240 | int ret; |
| 241 | |
| 242 | ww_acquire_init(ctx: ww_ctx, ww_class: ®ulator_ww_class); |
| 243 | |
| 244 | /* Try to just grab both of them */ |
| 245 | ret = regulator_lock_nested(rdev: rdev1, ww_ctx); |
| 246 | WARN_ON(ret); |
| 247 | ret = regulator_lock_nested(rdev: rdev2, ww_ctx); |
| 248 | if (ret != -EDEADLOCK) { |
| 249 | WARN_ON(ret); |
| 250 | goto exit; |
| 251 | } |
| 252 | |
| 253 | held = rdev1; |
| 254 | contended = rdev2; |
| 255 | while (true) { |
| 256 | regulator_unlock(rdev: held); |
| 257 | |
| 258 | ww_mutex_lock_slow(lock: &contended->mutex, ctx: ww_ctx); |
| 259 | contended->ref_cnt++; |
| 260 | contended->mutex_owner = current; |
| 261 | swap(held, contended); |
| 262 | ret = regulator_lock_nested(rdev: contended, ww_ctx); |
| 263 | |
| 264 | if (ret != -EDEADLOCK) { |
| 265 | WARN_ON(ret); |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | exit: |
| 271 | ww_acquire_done(ctx: ww_ctx); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * regulator_unlock_two - unlock two regulators |
| 276 | * @rdev1: first regulator |
| 277 | * @rdev2: second regulator |
| 278 | * @ww_ctx: w/w mutex acquire context |
| 279 | * |
| 280 | * The inverse of regulator_lock_two(). |
| 281 | */ |
| 282 | |
| 283 | static void regulator_unlock_two(struct regulator_dev *rdev1, |
| 284 | struct regulator_dev *rdev2, |
| 285 | struct ww_acquire_ctx *ww_ctx) |
| 286 | { |
| 287 | regulator_unlock(rdev: rdev2); |
| 288 | regulator_unlock(rdev: rdev1); |
| 289 | ww_acquire_fini(ctx: ww_ctx); |
| 290 | } |
| 291 | |
| 292 | static bool regulator_supply_is_couple(struct regulator_dev *rdev) |
| 293 | { |
| 294 | struct regulator_dev *c_rdev; |
| 295 | int i; |
| 296 | |
| 297 | for (i = 1; i < rdev->coupling_desc.n_coupled; i++) { |
| 298 | c_rdev = rdev->coupling_desc.coupled_rdevs[i]; |
| 299 | |
| 300 | if (rdev->supply->rdev == c_rdev) |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | static void regulator_unlock_recursive(struct regulator_dev *rdev, |
| 308 | unsigned int n_coupled) |
| 309 | { |
| 310 | struct regulator_dev *c_rdev, *supply_rdev; |
| 311 | int i, supply_n_coupled; |
| 312 | |
| 313 | for (i = n_coupled; i > 0; i--) { |
| 314 | c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1]; |
| 315 | |
| 316 | if (!c_rdev) |
| 317 | continue; |
| 318 | |
| 319 | if (c_rdev->supply && !regulator_supply_is_couple(rdev: c_rdev)) { |
| 320 | supply_rdev = c_rdev->supply->rdev; |
| 321 | supply_n_coupled = supply_rdev->coupling_desc.n_coupled; |
| 322 | |
| 323 | regulator_unlock_recursive(rdev: supply_rdev, |
| 324 | n_coupled: supply_n_coupled); |
| 325 | } |
| 326 | |
| 327 | regulator_unlock(rdev: c_rdev); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static int regulator_lock_recursive(struct regulator_dev *rdev, |
| 332 | struct regulator_dev **new_contended_rdev, |
| 333 | struct regulator_dev **old_contended_rdev, |
| 334 | struct ww_acquire_ctx *ww_ctx) |
| 335 | { |
| 336 | struct regulator_dev *c_rdev; |
| 337 | int i, err; |
| 338 | |
| 339 | for (i = 0; i < rdev->coupling_desc.n_coupled; i++) { |
| 340 | c_rdev = rdev->coupling_desc.coupled_rdevs[i]; |
| 341 | |
| 342 | if (!c_rdev) |
| 343 | continue; |
| 344 | |
| 345 | if (c_rdev != *old_contended_rdev) { |
| 346 | err = regulator_lock_nested(rdev: c_rdev, ww_ctx); |
| 347 | if (err) { |
| 348 | if (err == -EDEADLK) { |
| 349 | *new_contended_rdev = c_rdev; |
| 350 | goto err_unlock; |
| 351 | } |
| 352 | |
| 353 | /* shouldn't happen */ |
| 354 | WARN_ON_ONCE(err != -EALREADY); |
| 355 | } |
| 356 | } else { |
| 357 | *old_contended_rdev = NULL; |
| 358 | } |
| 359 | |
| 360 | if (c_rdev->supply && !regulator_supply_is_couple(rdev: c_rdev)) { |
| 361 | err = regulator_lock_recursive(rdev: c_rdev->supply->rdev, |
| 362 | new_contended_rdev, |
| 363 | old_contended_rdev, |
| 364 | ww_ctx); |
| 365 | if (err) { |
| 366 | regulator_unlock(rdev: c_rdev); |
| 367 | goto err_unlock; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return 0; |
| 373 | |
| 374 | err_unlock: |
| 375 | regulator_unlock_recursive(rdev, n_coupled: i); |
| 376 | |
| 377 | return err; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * regulator_unlock_dependent - unlock regulator's suppliers and coupled |
| 382 | * regulators |
| 383 | * @rdev: regulator source |
| 384 | * @ww_ctx: w/w mutex acquire context |
| 385 | * |
| 386 | * Unlock all regulators related with rdev by coupling or supplying. |
| 387 | */ |
| 388 | static void regulator_unlock_dependent(struct regulator_dev *rdev, |
| 389 | struct ww_acquire_ctx *ww_ctx) |
| 390 | { |
| 391 | regulator_unlock_recursive(rdev, n_coupled: rdev->coupling_desc.n_coupled); |
| 392 | ww_acquire_fini(ctx: ww_ctx); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * regulator_lock_dependent - lock regulator's suppliers and coupled regulators |
| 397 | * @rdev: regulator source |
| 398 | * @ww_ctx: w/w mutex acquire context |
| 399 | * |
| 400 | * This function as a wrapper on regulator_lock_recursive(), which locks |
| 401 | * all regulators related with rdev by coupling or supplying. |
| 402 | */ |
| 403 | static void regulator_lock_dependent(struct regulator_dev *rdev, |
| 404 | struct ww_acquire_ctx *ww_ctx) |
| 405 | { |
| 406 | struct regulator_dev *new_contended_rdev = NULL; |
| 407 | struct regulator_dev *old_contended_rdev = NULL; |
| 408 | int err; |
| 409 | |
| 410 | mutex_lock(®ulator_list_mutex); |
| 411 | |
| 412 | ww_acquire_init(ctx: ww_ctx, ww_class: ®ulator_ww_class); |
| 413 | |
| 414 | do { |
| 415 | if (new_contended_rdev) { |
| 416 | ww_mutex_lock_slow(lock: &new_contended_rdev->mutex, ctx: ww_ctx); |
| 417 | old_contended_rdev = new_contended_rdev; |
| 418 | old_contended_rdev->ref_cnt++; |
| 419 | old_contended_rdev->mutex_owner = current; |
| 420 | } |
| 421 | |
| 422 | err = regulator_lock_recursive(rdev, |
| 423 | new_contended_rdev: &new_contended_rdev, |
| 424 | old_contended_rdev: &old_contended_rdev, |
| 425 | ww_ctx); |
| 426 | |
| 427 | if (old_contended_rdev) |
| 428 | regulator_unlock(rdev: old_contended_rdev); |
| 429 | |
| 430 | } while (err == -EDEADLK); |
| 431 | |
| 432 | ww_acquire_done(ctx: ww_ctx); |
| 433 | |
| 434 | mutex_unlock(lock: ®ulator_list_mutex); |
| 435 | } |
| 436 | |
| 437 | /* Platform voltage constraint check */ |
| 438 | int regulator_check_voltage(struct regulator_dev *rdev, |
| 439 | int *min_uV, int *max_uV) |
| 440 | { |
| 441 | BUG_ON(*min_uV > *max_uV); |
| 442 | |
| 443 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
| 444 | rdev_err(rdev, "voltage operation not allowed\n" ); |
| 445 | return -EPERM; |
| 446 | } |
| 447 | |
| 448 | if (*max_uV > rdev->constraints->max_uV) |
| 449 | *max_uV = rdev->constraints->max_uV; |
| 450 | if (*min_uV < rdev->constraints->min_uV) |
| 451 | *min_uV = rdev->constraints->min_uV; |
| 452 | |
| 453 | if (*min_uV > *max_uV) { |
| 454 | rdev_err(rdev, "unsupportable voltage range: %d-%duV\n" , |
| 455 | *min_uV, *max_uV); |
| 456 | return -EINVAL; |
| 457 | } |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | /* return 0 if the state is valid */ |
| 463 | static int regulator_check_states(suspend_state_t state) |
| 464 | { |
| 465 | return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE); |
| 466 | } |
| 467 | |
| 468 | /* Make sure we select a voltage that suits the needs of all |
| 469 | * regulator consumers |
| 470 | */ |
| 471 | int regulator_check_consumers(struct regulator_dev *rdev, |
| 472 | int *min_uV, int *max_uV, |
| 473 | suspend_state_t state) |
| 474 | { |
| 475 | struct regulator *regulator; |
| 476 | struct regulator_voltage *voltage; |
| 477 | |
| 478 | list_for_each_entry(regulator, &rdev->consumer_list, list) { |
| 479 | voltage = ®ulator->voltage[state]; |
| 480 | /* |
| 481 | * Assume consumers that didn't say anything are OK |
| 482 | * with anything in the constraint range. |
| 483 | */ |
| 484 | if (!voltage->min_uV && !voltage->max_uV) |
| 485 | continue; |
| 486 | |
| 487 | if (*max_uV > voltage->max_uV) |
| 488 | *max_uV = voltage->max_uV; |
| 489 | if (*min_uV < voltage->min_uV) |
| 490 | *min_uV = voltage->min_uV; |
| 491 | } |
| 492 | |
| 493 | if (*min_uV > *max_uV) { |
| 494 | rdev_err(rdev, "Restricting voltage, %u-%uuV\n" , |
| 495 | *min_uV, *max_uV); |
| 496 | return -EINVAL; |
| 497 | } |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | /* current constraint check */ |
| 503 | static int regulator_check_current_limit(struct regulator_dev *rdev, |
| 504 | int *min_uA, int *max_uA) |
| 505 | { |
| 506 | BUG_ON(*min_uA > *max_uA); |
| 507 | |
| 508 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) { |
| 509 | rdev_err(rdev, "current operation not allowed\n" ); |
| 510 | return -EPERM; |
| 511 | } |
| 512 | |
| 513 | if (*max_uA > rdev->constraints->max_uA && |
| 514 | rdev->constraints->max_uA) |
| 515 | *max_uA = rdev->constraints->max_uA; |
| 516 | if (*min_uA < rdev->constraints->min_uA) |
| 517 | *min_uA = rdev->constraints->min_uA; |
| 518 | |
| 519 | if (*min_uA > *max_uA) { |
| 520 | rdev_err(rdev, "unsupportable current range: %d-%duA\n" , |
| 521 | *min_uA, *max_uA); |
| 522 | return -EINVAL; |
| 523 | } |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /* operating mode constraint check */ |
| 529 | static int regulator_mode_constrain(struct regulator_dev *rdev, |
| 530 | unsigned int *mode) |
| 531 | { |
| 532 | switch (*mode) { |
| 533 | case REGULATOR_MODE_FAST: |
| 534 | case REGULATOR_MODE_NORMAL: |
| 535 | case REGULATOR_MODE_IDLE: |
| 536 | case REGULATOR_MODE_STANDBY: |
| 537 | break; |
| 538 | default: |
| 539 | rdev_err(rdev, "invalid mode %x specified\n" , *mode); |
| 540 | return -EINVAL; |
| 541 | } |
| 542 | |
| 543 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) { |
| 544 | rdev_err(rdev, "mode operation not allowed\n" ); |
| 545 | return -EPERM; |
| 546 | } |
| 547 | |
| 548 | /* The modes are bitmasks, the most power hungry modes having |
| 549 | * the lowest values. If the requested mode isn't supported |
| 550 | * try higher modes. |
| 551 | */ |
| 552 | while (*mode) { |
| 553 | if (rdev->constraints->valid_modes_mask & *mode) |
| 554 | return 0; |
| 555 | *mode /= 2; |
| 556 | } |
| 557 | |
| 558 | return -EINVAL; |
| 559 | } |
| 560 | |
| 561 | static inline struct regulator_state * |
| 562 | regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state) |
| 563 | { |
| 564 | if (rdev->constraints == NULL) |
| 565 | return NULL; |
| 566 | |
| 567 | switch (state) { |
| 568 | case PM_SUSPEND_STANDBY: |
| 569 | return &rdev->constraints->state_standby; |
| 570 | case PM_SUSPEND_MEM: |
| 571 | return &rdev->constraints->state_mem; |
| 572 | case PM_SUSPEND_MAX: |
| 573 | return &rdev->constraints->state_disk; |
| 574 | default: |
| 575 | return NULL; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | static const struct regulator_state * |
| 580 | regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state) |
| 581 | { |
| 582 | const struct regulator_state *rstate; |
| 583 | |
| 584 | rstate = regulator_get_suspend_state(rdev, state); |
| 585 | if (rstate == NULL) |
| 586 | return NULL; |
| 587 | |
| 588 | /* If we have no suspend mode configuration don't set anything; |
| 589 | * only warn if the driver implements set_suspend_voltage or |
| 590 | * set_suspend_mode callback. |
| 591 | */ |
| 592 | if (rstate->enabled != ENABLE_IN_SUSPEND && |
| 593 | rstate->enabled != DISABLE_IN_SUSPEND) { |
| 594 | if (rdev->desc->ops->set_suspend_voltage || |
| 595 | rdev->desc->ops->set_suspend_mode) |
| 596 | rdev_warn(rdev, "No configuration\n" ); |
| 597 | return NULL; |
| 598 | } |
| 599 | |
| 600 | return rstate; |
| 601 | } |
| 602 | |
| 603 | static ssize_t microvolts_show(struct device *dev, |
| 604 | struct device_attribute *attr, char *buf) |
| 605 | { |
| 606 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 607 | int uV; |
| 608 | |
| 609 | regulator_lock(rdev); |
| 610 | uV = regulator_get_voltage_rdev(rdev); |
| 611 | regulator_unlock(rdev); |
| 612 | |
| 613 | if (uV < 0) |
| 614 | return uV; |
| 615 | return sprintf(buf, fmt: "%d\n" , uV); |
| 616 | } |
| 617 | static DEVICE_ATTR_RO(microvolts); |
| 618 | |
| 619 | static ssize_t microamps_show(struct device *dev, |
| 620 | struct device_attribute *attr, char *buf) |
| 621 | { |
| 622 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 623 | |
| 624 | return sprintf(buf, fmt: "%d\n" , _regulator_get_current_limit(rdev)); |
| 625 | } |
| 626 | static DEVICE_ATTR_RO(microamps); |
| 627 | |
| 628 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
| 629 | char *buf) |
| 630 | { |
| 631 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 632 | |
| 633 | return sprintf(buf, fmt: "%s\n" , rdev_get_name(rdev)); |
| 634 | } |
| 635 | static DEVICE_ATTR_RO(name); |
| 636 | |
| 637 | static const char *regulator_opmode_to_str(int mode) |
| 638 | { |
| 639 | switch (mode) { |
| 640 | case REGULATOR_MODE_FAST: |
| 641 | return "fast" ; |
| 642 | case REGULATOR_MODE_NORMAL: |
| 643 | return "normal" ; |
| 644 | case REGULATOR_MODE_IDLE: |
| 645 | return "idle" ; |
| 646 | case REGULATOR_MODE_STANDBY: |
| 647 | return "standby" ; |
| 648 | } |
| 649 | return "unknown" ; |
| 650 | } |
| 651 | |
| 652 | static ssize_t regulator_print_opmode(char *buf, int mode) |
| 653 | { |
| 654 | return sprintf(buf, fmt: "%s\n" , regulator_opmode_to_str(mode)); |
| 655 | } |
| 656 | |
| 657 | static ssize_t opmode_show(struct device *dev, |
| 658 | struct device_attribute *attr, char *buf) |
| 659 | { |
| 660 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 661 | |
| 662 | return regulator_print_opmode(buf, mode: _regulator_get_mode(rdev)); |
| 663 | } |
| 664 | static DEVICE_ATTR_RO(opmode); |
| 665 | |
| 666 | static ssize_t regulator_print_state(char *buf, int state) |
| 667 | { |
| 668 | if (state > 0) |
| 669 | return sprintf(buf, fmt: "enabled\n" ); |
| 670 | else if (state == 0) |
| 671 | return sprintf(buf, fmt: "disabled\n" ); |
| 672 | else |
| 673 | return sprintf(buf, fmt: "unknown\n" ); |
| 674 | } |
| 675 | |
| 676 | static ssize_t state_show(struct device *dev, |
| 677 | struct device_attribute *attr, char *buf) |
| 678 | { |
| 679 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 680 | ssize_t ret; |
| 681 | |
| 682 | regulator_lock(rdev); |
| 683 | ret = regulator_print_state(buf, state: _regulator_is_enabled(rdev)); |
| 684 | regulator_unlock(rdev); |
| 685 | |
| 686 | return ret; |
| 687 | } |
| 688 | static DEVICE_ATTR_RO(state); |
| 689 | |
| 690 | static ssize_t status_show(struct device *dev, |
| 691 | struct device_attribute *attr, char *buf) |
| 692 | { |
| 693 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 694 | int status; |
| 695 | char *label; |
| 696 | |
| 697 | status = rdev->desc->ops->get_status(rdev); |
| 698 | if (status < 0) |
| 699 | return status; |
| 700 | |
| 701 | switch (status) { |
| 702 | case REGULATOR_STATUS_OFF: |
| 703 | label = "off" ; |
| 704 | break; |
| 705 | case REGULATOR_STATUS_ON: |
| 706 | label = "on" ; |
| 707 | break; |
| 708 | case REGULATOR_STATUS_ERROR: |
| 709 | label = "error" ; |
| 710 | break; |
| 711 | case REGULATOR_STATUS_FAST: |
| 712 | label = "fast" ; |
| 713 | break; |
| 714 | case REGULATOR_STATUS_NORMAL: |
| 715 | label = "normal" ; |
| 716 | break; |
| 717 | case REGULATOR_STATUS_IDLE: |
| 718 | label = "idle" ; |
| 719 | break; |
| 720 | case REGULATOR_STATUS_STANDBY: |
| 721 | label = "standby" ; |
| 722 | break; |
| 723 | case REGULATOR_STATUS_BYPASS: |
| 724 | label = "bypass" ; |
| 725 | break; |
| 726 | case REGULATOR_STATUS_UNDEFINED: |
| 727 | label = "undefined" ; |
| 728 | break; |
| 729 | default: |
| 730 | return -ERANGE; |
| 731 | } |
| 732 | |
| 733 | return sprintf(buf, fmt: "%s\n" , label); |
| 734 | } |
| 735 | static DEVICE_ATTR_RO(status); |
| 736 | |
| 737 | static ssize_t min_microamps_show(struct device *dev, |
| 738 | struct device_attribute *attr, char *buf) |
| 739 | { |
| 740 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 741 | |
| 742 | if (!rdev->constraints) |
| 743 | return sprintf(buf, fmt: "constraint not defined\n" ); |
| 744 | |
| 745 | return sprintf(buf, fmt: "%d\n" , rdev->constraints->min_uA); |
| 746 | } |
| 747 | static DEVICE_ATTR_RO(min_microamps); |
| 748 | |
| 749 | static ssize_t max_microamps_show(struct device *dev, |
| 750 | struct device_attribute *attr, char *buf) |
| 751 | { |
| 752 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 753 | |
| 754 | if (!rdev->constraints) |
| 755 | return sprintf(buf, fmt: "constraint not defined\n" ); |
| 756 | |
| 757 | return sprintf(buf, fmt: "%d\n" , rdev->constraints->max_uA); |
| 758 | } |
| 759 | static DEVICE_ATTR_RO(max_microamps); |
| 760 | |
| 761 | static ssize_t min_microvolts_show(struct device *dev, |
| 762 | struct device_attribute *attr, char *buf) |
| 763 | { |
| 764 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 765 | |
| 766 | if (!rdev->constraints) |
| 767 | return sprintf(buf, fmt: "constraint not defined\n" ); |
| 768 | |
| 769 | return sprintf(buf, fmt: "%d\n" , rdev->constraints->min_uV); |
| 770 | } |
| 771 | static DEVICE_ATTR_RO(min_microvolts); |
| 772 | |
| 773 | static ssize_t max_microvolts_show(struct device *dev, |
| 774 | struct device_attribute *attr, char *buf) |
| 775 | { |
| 776 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 777 | |
| 778 | if (!rdev->constraints) |
| 779 | return sprintf(buf, fmt: "constraint not defined\n" ); |
| 780 | |
| 781 | return sprintf(buf, fmt: "%d\n" , rdev->constraints->max_uV); |
| 782 | } |
| 783 | static DEVICE_ATTR_RO(max_microvolts); |
| 784 | |
| 785 | static ssize_t requested_microamps_show(struct device *dev, |
| 786 | struct device_attribute *attr, char *buf) |
| 787 | { |
| 788 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 789 | struct regulator *regulator; |
| 790 | int uA = 0; |
| 791 | |
| 792 | regulator_lock(rdev); |
| 793 | list_for_each_entry(regulator, &rdev->consumer_list, list) { |
| 794 | if (regulator->enable_count) |
| 795 | uA += regulator->uA_load; |
| 796 | } |
| 797 | regulator_unlock(rdev); |
| 798 | return sprintf(buf, fmt: "%d\n" , uA); |
| 799 | } |
| 800 | static DEVICE_ATTR_RO(requested_microamps); |
| 801 | |
| 802 | static ssize_t num_users_show(struct device *dev, struct device_attribute *attr, |
| 803 | char *buf) |
| 804 | { |
| 805 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 806 | return sprintf(buf, fmt: "%d\n" , rdev->use_count); |
| 807 | } |
| 808 | static DEVICE_ATTR_RO(num_users); |
| 809 | |
| 810 | static ssize_t type_show(struct device *dev, struct device_attribute *attr, |
| 811 | char *buf) |
| 812 | { |
| 813 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 814 | |
| 815 | switch (rdev->desc->type) { |
| 816 | case REGULATOR_VOLTAGE: |
| 817 | return sprintf(buf, fmt: "voltage\n" ); |
| 818 | case REGULATOR_CURRENT: |
| 819 | return sprintf(buf, fmt: "current\n" ); |
| 820 | } |
| 821 | return sprintf(buf, fmt: "unknown\n" ); |
| 822 | } |
| 823 | static DEVICE_ATTR_RO(type); |
| 824 | |
| 825 | static ssize_t suspend_mem_microvolts_show(struct device *dev, |
| 826 | struct device_attribute *attr, char *buf) |
| 827 | { |
| 828 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 829 | |
| 830 | return sprintf(buf, fmt: "%d\n" , rdev->constraints->state_mem.uV); |
| 831 | } |
| 832 | static DEVICE_ATTR_RO(suspend_mem_microvolts); |
| 833 | |
| 834 | static ssize_t suspend_disk_microvolts_show(struct device *dev, |
| 835 | struct device_attribute *attr, char *buf) |
| 836 | { |
| 837 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 838 | |
| 839 | return sprintf(buf, fmt: "%d\n" , rdev->constraints->state_disk.uV); |
| 840 | } |
| 841 | static DEVICE_ATTR_RO(suspend_disk_microvolts); |
| 842 | |
| 843 | static ssize_t suspend_standby_microvolts_show(struct device *dev, |
| 844 | struct device_attribute *attr, char *buf) |
| 845 | { |
| 846 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 847 | |
| 848 | return sprintf(buf, fmt: "%d\n" , rdev->constraints->state_standby.uV); |
| 849 | } |
| 850 | static DEVICE_ATTR_RO(suspend_standby_microvolts); |
| 851 | |
| 852 | static ssize_t suspend_mem_mode_show(struct device *dev, |
| 853 | struct device_attribute *attr, char *buf) |
| 854 | { |
| 855 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 856 | |
| 857 | return regulator_print_opmode(buf, |
| 858 | mode: rdev->constraints->state_mem.mode); |
| 859 | } |
| 860 | static DEVICE_ATTR_RO(suspend_mem_mode); |
| 861 | |
| 862 | static ssize_t suspend_disk_mode_show(struct device *dev, |
| 863 | struct device_attribute *attr, char *buf) |
| 864 | { |
| 865 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 866 | |
| 867 | return regulator_print_opmode(buf, |
| 868 | mode: rdev->constraints->state_disk.mode); |
| 869 | } |
| 870 | static DEVICE_ATTR_RO(suspend_disk_mode); |
| 871 | |
| 872 | static ssize_t suspend_standby_mode_show(struct device *dev, |
| 873 | struct device_attribute *attr, char *buf) |
| 874 | { |
| 875 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 876 | |
| 877 | return regulator_print_opmode(buf, |
| 878 | mode: rdev->constraints->state_standby.mode); |
| 879 | } |
| 880 | static DEVICE_ATTR_RO(suspend_standby_mode); |
| 881 | |
| 882 | static ssize_t suspend_mem_state_show(struct device *dev, |
| 883 | struct device_attribute *attr, char *buf) |
| 884 | { |
| 885 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 886 | |
| 887 | return regulator_print_state(buf, |
| 888 | state: rdev->constraints->state_mem.enabled); |
| 889 | } |
| 890 | static DEVICE_ATTR_RO(suspend_mem_state); |
| 891 | |
| 892 | static ssize_t suspend_disk_state_show(struct device *dev, |
| 893 | struct device_attribute *attr, char *buf) |
| 894 | { |
| 895 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 896 | |
| 897 | return regulator_print_state(buf, |
| 898 | state: rdev->constraints->state_disk.enabled); |
| 899 | } |
| 900 | static DEVICE_ATTR_RO(suspend_disk_state); |
| 901 | |
| 902 | static ssize_t suspend_standby_state_show(struct device *dev, |
| 903 | struct device_attribute *attr, char *buf) |
| 904 | { |
| 905 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 906 | |
| 907 | return regulator_print_state(buf, |
| 908 | state: rdev->constraints->state_standby.enabled); |
| 909 | } |
| 910 | static DEVICE_ATTR_RO(suspend_standby_state); |
| 911 | |
| 912 | static ssize_t bypass_show(struct device *dev, |
| 913 | struct device_attribute *attr, char *buf) |
| 914 | { |
| 915 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 916 | const char *report; |
| 917 | bool bypass; |
| 918 | int ret; |
| 919 | |
| 920 | ret = rdev->desc->ops->get_bypass(rdev, &bypass); |
| 921 | |
| 922 | if (ret != 0) |
| 923 | report = "unknown" ; |
| 924 | else if (bypass) |
| 925 | report = "enabled" ; |
| 926 | else |
| 927 | report = "disabled" ; |
| 928 | |
| 929 | return sprintf(buf, fmt: "%s\n" , report); |
| 930 | } |
| 931 | static DEVICE_ATTR_RO(bypass); |
| 932 | |
| 933 | static ssize_t power_budget_milliwatt_show(struct device *dev, |
| 934 | struct device_attribute *attr, |
| 935 | char *buf) |
| 936 | { |
| 937 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 938 | |
| 939 | return sprintf(buf, fmt: "%d\n" , rdev->constraints->pw_budget_mW); |
| 940 | } |
| 941 | static DEVICE_ATTR_RO(power_budget_milliwatt); |
| 942 | |
| 943 | static ssize_t power_requested_milliwatt_show(struct device *dev, |
| 944 | struct device_attribute *attr, |
| 945 | char *buf) |
| 946 | { |
| 947 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 948 | |
| 949 | return sprintf(buf, fmt: "%d\n" , rdev->pw_requested_mW); |
| 950 | } |
| 951 | static DEVICE_ATTR_RO(power_requested_milliwatt); |
| 952 | |
| 953 | #define REGULATOR_ERROR_ATTR(name, bit) \ |
| 954 | static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \ |
| 955 | char *buf) \ |
| 956 | { \ |
| 957 | int ret; \ |
| 958 | unsigned int flags; \ |
| 959 | struct regulator_dev *rdev = dev_get_drvdata(dev); \ |
| 960 | ret = _regulator_get_error_flags(rdev, &flags); \ |
| 961 | if (ret) \ |
| 962 | return ret; \ |
| 963 | return sysfs_emit(buf, "%d\n", !!(flags & (bit))); \ |
| 964 | } \ |
| 965 | static DEVICE_ATTR_RO(name) |
| 966 | |
| 967 | REGULATOR_ERROR_ATTR(under_voltage, REGULATOR_ERROR_UNDER_VOLTAGE); |
| 968 | REGULATOR_ERROR_ATTR(over_current, REGULATOR_ERROR_OVER_CURRENT); |
| 969 | REGULATOR_ERROR_ATTR(regulation_out, REGULATOR_ERROR_REGULATION_OUT); |
| 970 | REGULATOR_ERROR_ATTR(fail, REGULATOR_ERROR_FAIL); |
| 971 | REGULATOR_ERROR_ATTR(over_temp, REGULATOR_ERROR_OVER_TEMP); |
| 972 | REGULATOR_ERROR_ATTR(under_voltage_warn, REGULATOR_ERROR_UNDER_VOLTAGE_WARN); |
| 973 | REGULATOR_ERROR_ATTR(over_current_warn, REGULATOR_ERROR_OVER_CURRENT_WARN); |
| 974 | REGULATOR_ERROR_ATTR(over_voltage_warn, REGULATOR_ERROR_OVER_VOLTAGE_WARN); |
| 975 | REGULATOR_ERROR_ATTR(over_temp_warn, REGULATOR_ERROR_OVER_TEMP_WARN); |
| 976 | |
| 977 | /* Calculate the new optimum regulator operating mode based on the new total |
| 978 | * consumer load. All locks held by caller |
| 979 | */ |
| 980 | static int drms_uA_update(struct regulator_dev *rdev) |
| 981 | { |
| 982 | struct regulator *sibling; |
| 983 | int current_uA = 0, output_uV, input_uV, err; |
| 984 | unsigned int mode; |
| 985 | |
| 986 | /* |
| 987 | * first check to see if we can set modes at all, otherwise just |
| 988 | * tell the consumer everything is OK. |
| 989 | */ |
| 990 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) { |
| 991 | rdev_dbg(rdev, "DRMS operation not allowed\n" ); |
| 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | if (!rdev->desc->ops->get_optimum_mode && |
| 996 | !rdev->desc->ops->set_load) |
| 997 | return 0; |
| 998 | |
| 999 | if (!rdev->desc->ops->set_mode && |
| 1000 | !rdev->desc->ops->set_load) |
| 1001 | return -EINVAL; |
| 1002 | |
| 1003 | /* calc total requested load */ |
| 1004 | list_for_each_entry(sibling, &rdev->consumer_list, list) { |
| 1005 | if (sibling->enable_count) |
| 1006 | current_uA += sibling->uA_load; |
| 1007 | } |
| 1008 | |
| 1009 | current_uA += rdev->constraints->system_load; |
| 1010 | |
| 1011 | if (rdev->desc->ops->set_load) { |
| 1012 | /* set the optimum mode for our new total regulator load */ |
| 1013 | err = rdev->desc->ops->set_load(rdev, current_uA); |
| 1014 | if (err < 0) |
| 1015 | rdev_err(rdev, "failed to set load %d: %pe\n" , |
| 1016 | current_uA, ERR_PTR(err)); |
| 1017 | } else { |
| 1018 | /* |
| 1019 | * Unfortunately in some cases the constraints->valid_ops has |
| 1020 | * REGULATOR_CHANGE_DRMS but there are no valid modes listed. |
| 1021 | * That's not really legit but we won't consider it a fatal |
| 1022 | * error here. We'll treat it as if REGULATOR_CHANGE_DRMS |
| 1023 | * wasn't set. |
| 1024 | */ |
| 1025 | if (!rdev->constraints->valid_modes_mask) { |
| 1026 | rdev_dbg(rdev, "Can change modes; but no valid mode\n" ); |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | /* get output voltage */ |
| 1031 | output_uV = regulator_get_voltage_rdev(rdev); |
| 1032 | |
| 1033 | /* |
| 1034 | * Don't return an error; if regulator driver cares about |
| 1035 | * output_uV then it's up to the driver to validate. |
| 1036 | */ |
| 1037 | if (output_uV <= 0) |
| 1038 | rdev_dbg(rdev, "invalid output voltage found\n" ); |
| 1039 | |
| 1040 | /* get input voltage */ |
| 1041 | input_uV = 0; |
| 1042 | if (rdev->supply) |
| 1043 | input_uV = regulator_get_voltage_rdev(rdev: rdev->supply->rdev); |
| 1044 | if (input_uV <= 0) |
| 1045 | input_uV = rdev->constraints->input_uV; |
| 1046 | |
| 1047 | /* |
| 1048 | * Don't return an error; if regulator driver cares about |
| 1049 | * input_uV then it's up to the driver to validate. |
| 1050 | */ |
| 1051 | if (input_uV <= 0) |
| 1052 | rdev_dbg(rdev, "invalid input voltage found\n" ); |
| 1053 | |
| 1054 | /* now get the optimum mode for our new total regulator load */ |
| 1055 | mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, |
| 1056 | output_uV, current_uA); |
| 1057 | |
| 1058 | /* check the new mode is allowed */ |
| 1059 | err = regulator_mode_constrain(rdev, mode: &mode); |
| 1060 | if (err < 0) { |
| 1061 | rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n" , |
| 1062 | current_uA, input_uV, output_uV, ERR_PTR(err)); |
| 1063 | return err; |
| 1064 | } |
| 1065 | |
| 1066 | err = rdev->desc->ops->set_mode(rdev, mode); |
| 1067 | if (err < 0) |
| 1068 | rdev_err(rdev, "failed to set optimum mode %x: %pe\n" , |
| 1069 | mode, ERR_PTR(err)); |
| 1070 | } |
| 1071 | |
| 1072 | return err; |
| 1073 | } |
| 1074 | |
| 1075 | static int __suspend_set_state(struct regulator_dev *rdev, |
| 1076 | const struct regulator_state *rstate) |
| 1077 | { |
| 1078 | int ret = 0; |
| 1079 | |
| 1080 | if (rstate->enabled == ENABLE_IN_SUSPEND && |
| 1081 | rdev->desc->ops->set_suspend_enable) |
| 1082 | ret = rdev->desc->ops->set_suspend_enable(rdev); |
| 1083 | else if (rstate->enabled == DISABLE_IN_SUSPEND && |
| 1084 | rdev->desc->ops->set_suspend_disable) |
| 1085 | ret = rdev->desc->ops->set_suspend_disable(rdev); |
| 1086 | else /* OK if set_suspend_enable or set_suspend_disable is NULL */ |
| 1087 | ret = 0; |
| 1088 | |
| 1089 | if (ret < 0) { |
| 1090 | rdev_err(rdev, "failed to enabled/disable: %pe\n" , ERR_PTR(ret)); |
| 1091 | return ret; |
| 1092 | } |
| 1093 | |
| 1094 | if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { |
| 1095 | ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); |
| 1096 | if (ret < 0) { |
| 1097 | rdev_err(rdev, "failed to set voltage: %pe\n" , ERR_PTR(ret)); |
| 1098 | return ret; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { |
| 1103 | ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); |
| 1104 | if (ret < 0) { |
| 1105 | rdev_err(rdev, "failed to set mode: %pe\n" , ERR_PTR(ret)); |
| 1106 | return ret; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | return ret; |
| 1111 | } |
| 1112 | |
| 1113 | static int suspend_set_initial_state(struct regulator_dev *rdev) |
| 1114 | { |
| 1115 | const struct regulator_state *rstate; |
| 1116 | |
| 1117 | rstate = regulator_get_suspend_state_check(rdev, |
| 1118 | state: rdev->constraints->initial_state); |
| 1119 | if (!rstate) |
| 1120 | return 0; |
| 1121 | |
| 1122 | return __suspend_set_state(rdev, rstate); |
| 1123 | } |
| 1124 | |
| 1125 | #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) |
| 1126 | static void print_constraints_debug(struct regulator_dev *rdev) |
| 1127 | { |
| 1128 | struct regulation_constraints *constraints = rdev->constraints; |
| 1129 | char buf[160] = "" ; |
| 1130 | size_t len = sizeof(buf) - 1; |
| 1131 | int count = 0; |
| 1132 | int ret; |
| 1133 | |
| 1134 | if (constraints->min_uV && constraints->max_uV) { |
| 1135 | if (constraints->min_uV == constraints->max_uV) |
| 1136 | count += scnprintf(buf: buf + count, size: len - count, fmt: "%d mV " , |
| 1137 | constraints->min_uV / 1000); |
| 1138 | else |
| 1139 | count += scnprintf(buf: buf + count, size: len - count, |
| 1140 | fmt: "%d <--> %d mV " , |
| 1141 | constraints->min_uV / 1000, |
| 1142 | constraints->max_uV / 1000); |
| 1143 | } |
| 1144 | |
| 1145 | if (!constraints->min_uV || |
| 1146 | constraints->min_uV != constraints->max_uV) { |
| 1147 | ret = regulator_get_voltage_rdev(rdev); |
| 1148 | if (ret > 0) |
| 1149 | count += scnprintf(buf: buf + count, size: len - count, |
| 1150 | fmt: "at %d mV " , ret / 1000); |
| 1151 | } |
| 1152 | |
| 1153 | if (constraints->uV_offset) |
| 1154 | count += scnprintf(buf: buf + count, size: len - count, fmt: "%dmV offset " , |
| 1155 | constraints->uV_offset / 1000); |
| 1156 | |
| 1157 | if (constraints->min_uA && constraints->max_uA) { |
| 1158 | if (constraints->min_uA == constraints->max_uA) |
| 1159 | count += scnprintf(buf: buf + count, size: len - count, fmt: "%d mA " , |
| 1160 | constraints->min_uA / 1000); |
| 1161 | else |
| 1162 | count += scnprintf(buf: buf + count, size: len - count, |
| 1163 | fmt: "%d <--> %d mA " , |
| 1164 | constraints->min_uA / 1000, |
| 1165 | constraints->max_uA / 1000); |
| 1166 | } |
| 1167 | |
| 1168 | if (!constraints->min_uA || |
| 1169 | constraints->min_uA != constraints->max_uA) { |
| 1170 | ret = _regulator_get_current_limit(rdev); |
| 1171 | if (ret > 0) |
| 1172 | count += scnprintf(buf: buf + count, size: len - count, |
| 1173 | fmt: "at %d mA " , ret / 1000); |
| 1174 | } |
| 1175 | |
| 1176 | if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) |
| 1177 | count += scnprintf(buf: buf + count, size: len - count, fmt: "fast " ); |
| 1178 | if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) |
| 1179 | count += scnprintf(buf: buf + count, size: len - count, fmt: "normal " ); |
| 1180 | if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) |
| 1181 | count += scnprintf(buf: buf + count, size: len - count, fmt: "idle " ); |
| 1182 | if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) |
| 1183 | count += scnprintf(buf: buf + count, size: len - count, fmt: "standby " ); |
| 1184 | |
| 1185 | if (constraints->pw_budget_mW) |
| 1186 | count += scnprintf(buf: buf + count, size: len - count, fmt: "%d mW budget" , |
| 1187 | constraints->pw_budget_mW); |
| 1188 | |
| 1189 | if (!count) |
| 1190 | count = scnprintf(buf, size: len, fmt: "no parameters" ); |
| 1191 | else |
| 1192 | --count; |
| 1193 | |
| 1194 | count += scnprintf(buf: buf + count, size: len - count, fmt: ", %s" , |
| 1195 | _regulator_is_enabled(rdev) ? "enabled" : "disabled" ); |
| 1196 | |
| 1197 | rdev_dbg(rdev, "%s\n" , buf); |
| 1198 | } |
| 1199 | #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */ |
| 1200 | static inline void print_constraints_debug(struct regulator_dev *rdev) {} |
| 1201 | #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */ |
| 1202 | |
| 1203 | static void print_constraints(struct regulator_dev *rdev) |
| 1204 | { |
| 1205 | struct regulation_constraints *constraints = rdev->constraints; |
| 1206 | |
| 1207 | print_constraints_debug(rdev); |
| 1208 | |
| 1209 | if ((constraints->min_uV != constraints->max_uV) && |
| 1210 | !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) |
| 1211 | rdev_warn(rdev, |
| 1212 | "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n" ); |
| 1213 | } |
| 1214 | |
| 1215 | static int machine_constraints_voltage(struct regulator_dev *rdev, |
| 1216 | struct regulation_constraints *constraints) |
| 1217 | { |
| 1218 | const struct regulator_ops *ops = rdev->desc->ops; |
| 1219 | int ret; |
| 1220 | |
| 1221 | /* do we need to apply the constraint voltage */ |
| 1222 | if (rdev->constraints->apply_uV && |
| 1223 | rdev->constraints->min_uV && rdev->constraints->max_uV) { |
| 1224 | int target_min, target_max; |
| 1225 | int current_uV = regulator_get_voltage_rdev(rdev); |
| 1226 | |
| 1227 | if (current_uV == -ENOTRECOVERABLE) { |
| 1228 | /* This regulator can't be read and must be initialized */ |
| 1229 | rdev_info(rdev, "Setting %d-%duV\n" , |
| 1230 | rdev->constraints->min_uV, |
| 1231 | rdev->constraints->max_uV); |
| 1232 | _regulator_do_set_voltage(rdev, |
| 1233 | min_uV: rdev->constraints->min_uV, |
| 1234 | max_uV: rdev->constraints->max_uV); |
| 1235 | current_uV = regulator_get_voltage_rdev(rdev); |
| 1236 | } |
| 1237 | |
| 1238 | if (current_uV < 0) { |
| 1239 | if (current_uV != -EPROBE_DEFER) |
| 1240 | rdev_err(rdev, |
| 1241 | "failed to get the current voltage: %pe\n" , |
| 1242 | ERR_PTR(current_uV)); |
| 1243 | return current_uV; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * If we're below the minimum voltage move up to the |
| 1248 | * minimum voltage, if we're above the maximum voltage |
| 1249 | * then move down to the maximum. |
| 1250 | */ |
| 1251 | target_min = current_uV; |
| 1252 | target_max = current_uV; |
| 1253 | |
| 1254 | if (current_uV < rdev->constraints->min_uV) { |
| 1255 | target_min = rdev->constraints->min_uV; |
| 1256 | target_max = rdev->constraints->min_uV; |
| 1257 | } |
| 1258 | |
| 1259 | if (current_uV > rdev->constraints->max_uV) { |
| 1260 | target_min = rdev->constraints->max_uV; |
| 1261 | target_max = rdev->constraints->max_uV; |
| 1262 | } |
| 1263 | |
| 1264 | if (target_min != current_uV || target_max != current_uV) { |
| 1265 | rdev_info(rdev, "Bringing %duV into %d-%duV\n" , |
| 1266 | current_uV, target_min, target_max); |
| 1267 | ret = _regulator_do_set_voltage( |
| 1268 | rdev, min_uV: target_min, max_uV: target_max); |
| 1269 | if (ret < 0) { |
| 1270 | rdev_err(rdev, |
| 1271 | "failed to apply %d-%duV constraint: %pe\n" , |
| 1272 | target_min, target_max, ERR_PTR(ret)); |
| 1273 | return ret; |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /* constrain machine-level voltage specs to fit |
| 1279 | * the actual range supported by this regulator. |
| 1280 | */ |
| 1281 | if (ops->list_voltage && rdev->desc->n_voltages) { |
| 1282 | int count = rdev->desc->n_voltages; |
| 1283 | int i; |
| 1284 | int min_uV = INT_MAX; |
| 1285 | int max_uV = INT_MIN; |
| 1286 | int cmin = constraints->min_uV; |
| 1287 | int cmax = constraints->max_uV; |
| 1288 | |
| 1289 | /* it's safe to autoconfigure fixed-voltage supplies |
| 1290 | * and the constraints are used by list_voltage. |
| 1291 | */ |
| 1292 | if (count == 1 && !cmin) { |
| 1293 | cmin = 1; |
| 1294 | cmax = INT_MAX; |
| 1295 | constraints->min_uV = cmin; |
| 1296 | constraints->max_uV = cmax; |
| 1297 | } |
| 1298 | |
| 1299 | /* voltage constraints are optional */ |
| 1300 | if ((cmin == 0) && (cmax == 0)) |
| 1301 | return 0; |
| 1302 | |
| 1303 | /* else require explicit machine-level constraints */ |
| 1304 | if (cmin <= 0 || cmax <= 0 || cmax < cmin) { |
| 1305 | rdev_err(rdev, "invalid voltage constraints\n" ); |
| 1306 | return -EINVAL; |
| 1307 | } |
| 1308 | |
| 1309 | /* no need to loop voltages if range is continuous */ |
| 1310 | if (rdev->desc->continuous_voltage_range) |
| 1311 | return 0; |
| 1312 | |
| 1313 | /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ |
| 1314 | for (i = 0; i < count; i++) { |
| 1315 | int value; |
| 1316 | |
| 1317 | value = ops->list_voltage(rdev, i); |
| 1318 | if (value <= 0) |
| 1319 | continue; |
| 1320 | |
| 1321 | /* maybe adjust [min_uV..max_uV] */ |
| 1322 | if (value >= cmin && value < min_uV) |
| 1323 | min_uV = value; |
| 1324 | if (value <= cmax && value > max_uV) |
| 1325 | max_uV = value; |
| 1326 | } |
| 1327 | |
| 1328 | /* final: [min_uV..max_uV] valid iff constraints valid */ |
| 1329 | if (max_uV < min_uV) { |
| 1330 | rdev_err(rdev, |
| 1331 | "unsupportable voltage constraints %u-%uuV\n" , |
| 1332 | min_uV, max_uV); |
| 1333 | return -EINVAL; |
| 1334 | } |
| 1335 | |
| 1336 | /* use regulator's subset of machine constraints */ |
| 1337 | if (constraints->min_uV < min_uV) { |
| 1338 | rdev_dbg(rdev, "override min_uV, %d -> %d\n" , |
| 1339 | constraints->min_uV, min_uV); |
| 1340 | constraints->min_uV = min_uV; |
| 1341 | } |
| 1342 | if (constraints->max_uV > max_uV) { |
| 1343 | rdev_dbg(rdev, "override max_uV, %d -> %d\n" , |
| 1344 | constraints->max_uV, max_uV); |
| 1345 | constraints->max_uV = max_uV; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
| 1352 | static int machine_constraints_current(struct regulator_dev *rdev, |
| 1353 | struct regulation_constraints *constraints) |
| 1354 | { |
| 1355 | const struct regulator_ops *ops = rdev->desc->ops; |
| 1356 | int ret; |
| 1357 | |
| 1358 | if (!constraints->min_uA && !constraints->max_uA) |
| 1359 | return 0; |
| 1360 | |
| 1361 | if (constraints->min_uA > constraints->max_uA) { |
| 1362 | rdev_err(rdev, "Invalid current constraints\n" ); |
| 1363 | return -EINVAL; |
| 1364 | } |
| 1365 | |
| 1366 | if (!ops->set_current_limit || !ops->get_current_limit) { |
| 1367 | rdev_warn(rdev, "Operation of current configuration missing\n" ); |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | /* Set regulator current in constraints range */ |
| 1372 | ret = ops->set_current_limit(rdev, constraints->min_uA, |
| 1373 | constraints->max_uA); |
| 1374 | if (ret < 0) { |
| 1375 | rdev_err(rdev, "Failed to set current constraint, %d\n" , ret); |
| 1376 | return ret; |
| 1377 | } |
| 1378 | |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | static int _regulator_do_enable(struct regulator_dev *rdev); |
| 1383 | |
| 1384 | static int notif_set_limit(struct regulator_dev *rdev, |
| 1385 | int (*set)(struct regulator_dev *, int, int, bool), |
| 1386 | int limit, int severity) |
| 1387 | { |
| 1388 | bool enable; |
| 1389 | |
| 1390 | if (limit == REGULATOR_NOTIF_LIMIT_DISABLE) { |
| 1391 | enable = false; |
| 1392 | limit = 0; |
| 1393 | } else { |
| 1394 | enable = true; |
| 1395 | } |
| 1396 | |
| 1397 | if (limit == REGULATOR_NOTIF_LIMIT_ENABLE) |
| 1398 | limit = 0; |
| 1399 | |
| 1400 | return set(rdev, limit, severity, enable); |
| 1401 | } |
| 1402 | |
| 1403 | static int handle_notify_limits(struct regulator_dev *rdev, |
| 1404 | int (*set)(struct regulator_dev *, int, int, bool), |
| 1405 | struct notification_limit *limits) |
| 1406 | { |
| 1407 | int ret = 0; |
| 1408 | |
| 1409 | if (!set) |
| 1410 | return -EOPNOTSUPP; |
| 1411 | |
| 1412 | if (limits->prot) |
| 1413 | ret = notif_set_limit(rdev, set, limit: limits->prot, |
| 1414 | severity: REGULATOR_SEVERITY_PROT); |
| 1415 | if (ret) |
| 1416 | return ret; |
| 1417 | |
| 1418 | if (limits->err) |
| 1419 | ret = notif_set_limit(rdev, set, limit: limits->err, |
| 1420 | severity: REGULATOR_SEVERITY_ERR); |
| 1421 | if (ret) |
| 1422 | return ret; |
| 1423 | |
| 1424 | if (limits->warn) |
| 1425 | ret = notif_set_limit(rdev, set, limit: limits->warn, |
| 1426 | severity: REGULATOR_SEVERITY_WARN); |
| 1427 | |
| 1428 | return ret; |
| 1429 | } |
| 1430 | /** |
| 1431 | * set_machine_constraints - sets regulator constraints |
| 1432 | * @rdev: regulator source |
| 1433 | * |
| 1434 | * Allows platform initialisation code to define and constrain |
| 1435 | * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: |
| 1436 | * Constraints *must* be set by platform code in order for some |
| 1437 | * regulator operations to proceed i.e. set_voltage, set_current_limit, |
| 1438 | * set_mode. |
| 1439 | * |
| 1440 | * Return: 0 on success or a negative error number on failure. |
| 1441 | */ |
| 1442 | static int set_machine_constraints(struct regulator_dev *rdev) |
| 1443 | { |
| 1444 | int ret = 0; |
| 1445 | const struct regulator_ops *ops = rdev->desc->ops; |
| 1446 | |
| 1447 | ret = machine_constraints_voltage(rdev, constraints: rdev->constraints); |
| 1448 | if (ret != 0) |
| 1449 | return ret; |
| 1450 | |
| 1451 | ret = machine_constraints_current(rdev, constraints: rdev->constraints); |
| 1452 | if (ret != 0) |
| 1453 | return ret; |
| 1454 | |
| 1455 | if (rdev->constraints->ilim_uA && ops->set_input_current_limit) { |
| 1456 | ret = ops->set_input_current_limit(rdev, |
| 1457 | rdev->constraints->ilim_uA); |
| 1458 | if (ret < 0) { |
| 1459 | rdev_err(rdev, "failed to set input limit: %pe\n" , ERR_PTR(ret)); |
| 1460 | return ret; |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | /* do we need to setup our suspend state */ |
| 1465 | if (rdev->constraints->initial_state) { |
| 1466 | ret = suspend_set_initial_state(rdev); |
| 1467 | if (ret < 0) { |
| 1468 | rdev_err(rdev, "failed to set suspend state: %pe\n" , ERR_PTR(ret)); |
| 1469 | return ret; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | if (rdev->constraints->initial_mode) { |
| 1474 | if (!ops->set_mode) { |
| 1475 | rdev_err(rdev, "no set_mode operation\n" ); |
| 1476 | return -EINVAL; |
| 1477 | } |
| 1478 | |
| 1479 | ret = ops->set_mode(rdev, rdev->constraints->initial_mode); |
| 1480 | if (ret < 0) { |
| 1481 | rdev_err(rdev, "failed to set initial mode: %pe\n" , ERR_PTR(ret)); |
| 1482 | return ret; |
| 1483 | } |
| 1484 | } else if (rdev->constraints->system_load) { |
| 1485 | /* |
| 1486 | * We'll only apply the initial system load if an |
| 1487 | * initial mode wasn't specified. |
| 1488 | */ |
| 1489 | drms_uA_update(rdev); |
| 1490 | } |
| 1491 | |
| 1492 | if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) |
| 1493 | && ops->set_ramp_delay) { |
| 1494 | ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); |
| 1495 | if (ret < 0) { |
| 1496 | rdev_err(rdev, "failed to set ramp_delay: %pe\n" , ERR_PTR(ret)); |
| 1497 | return ret; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | if (rdev->constraints->pull_down && ops->set_pull_down) { |
| 1502 | ret = ops->set_pull_down(rdev); |
| 1503 | if (ret < 0) { |
| 1504 | rdev_err(rdev, "failed to set pull down: %pe\n" , ERR_PTR(ret)); |
| 1505 | return ret; |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | if (rdev->constraints->soft_start && ops->set_soft_start) { |
| 1510 | ret = ops->set_soft_start(rdev); |
| 1511 | if (ret < 0) { |
| 1512 | rdev_err(rdev, "failed to set soft start: %pe\n" , ERR_PTR(ret)); |
| 1513 | return ret; |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * Existing logic does not warn if over_current_protection is given as |
| 1519 | * a constraint but driver does not support that. I think we should |
| 1520 | * warn about this type of issues as it is possible someone changes |
| 1521 | * PMIC on board to another type - and the another PMIC's driver does |
| 1522 | * not support setting protection. Board composer may happily believe |
| 1523 | * the DT limits are respected - especially if the new PMIC HW also |
| 1524 | * supports protection but the driver does not. I won't change the logic |
| 1525 | * without hearing more experienced opinion on this though. |
| 1526 | * |
| 1527 | * If warning is seen as a good idea then we can merge handling the |
| 1528 | * over-curret protection and detection and get rid of this special |
| 1529 | * handling. |
| 1530 | */ |
| 1531 | if (rdev->constraints->over_current_protection |
| 1532 | && ops->set_over_current_protection) { |
| 1533 | int lim = rdev->constraints->over_curr_limits.prot; |
| 1534 | |
| 1535 | ret = ops->set_over_current_protection(rdev, lim, |
| 1536 | REGULATOR_SEVERITY_PROT, |
| 1537 | true); |
| 1538 | if (ret < 0) { |
| 1539 | rdev_err(rdev, "failed to set over current protection: %pe\n" , |
| 1540 | ERR_PTR(ret)); |
| 1541 | return ret; |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | if (rdev->constraints->over_current_detection) |
| 1546 | ret = handle_notify_limits(rdev, |
| 1547 | set: ops->set_over_current_protection, |
| 1548 | limits: &rdev->constraints->over_curr_limits); |
| 1549 | if (ret) { |
| 1550 | if (ret != -EOPNOTSUPP) { |
| 1551 | rdev_err(rdev, "failed to set over current limits: %pe\n" , |
| 1552 | ERR_PTR(ret)); |
| 1553 | return ret; |
| 1554 | } |
| 1555 | rdev_warn(rdev, |
| 1556 | "IC does not support requested over-current limits\n" ); |
| 1557 | } |
| 1558 | |
| 1559 | if (rdev->constraints->over_voltage_detection) |
| 1560 | ret = handle_notify_limits(rdev, |
| 1561 | set: ops->set_over_voltage_protection, |
| 1562 | limits: &rdev->constraints->over_voltage_limits); |
| 1563 | if (ret) { |
| 1564 | if (ret != -EOPNOTSUPP) { |
| 1565 | rdev_err(rdev, "failed to set over voltage limits %pe\n" , |
| 1566 | ERR_PTR(ret)); |
| 1567 | return ret; |
| 1568 | } |
| 1569 | rdev_warn(rdev, |
| 1570 | "IC does not support requested over voltage limits\n" ); |
| 1571 | } |
| 1572 | |
| 1573 | if (rdev->constraints->under_voltage_detection) |
| 1574 | ret = handle_notify_limits(rdev, |
| 1575 | set: ops->set_under_voltage_protection, |
| 1576 | limits: &rdev->constraints->under_voltage_limits); |
| 1577 | if (ret) { |
| 1578 | if (ret != -EOPNOTSUPP) { |
| 1579 | rdev_err(rdev, "failed to set under voltage limits %pe\n" , |
| 1580 | ERR_PTR(ret)); |
| 1581 | return ret; |
| 1582 | } |
| 1583 | rdev_warn(rdev, |
| 1584 | "IC does not support requested under voltage limits\n" ); |
| 1585 | } |
| 1586 | |
| 1587 | if (rdev->constraints->over_temp_detection) |
| 1588 | ret = handle_notify_limits(rdev, |
| 1589 | set: ops->set_thermal_protection, |
| 1590 | limits: &rdev->constraints->temp_limits); |
| 1591 | if (ret) { |
| 1592 | if (ret != -EOPNOTSUPP) { |
| 1593 | rdev_err(rdev, "failed to set temperature limits %pe\n" , |
| 1594 | ERR_PTR(ret)); |
| 1595 | return ret; |
| 1596 | } |
| 1597 | rdev_warn(rdev, |
| 1598 | "IC does not support requested temperature limits\n" ); |
| 1599 | } |
| 1600 | |
| 1601 | if (rdev->constraints->active_discharge && ops->set_active_discharge) { |
| 1602 | bool ad_state = rdev->constraints->active_discharge == |
| 1603 | REGULATOR_ACTIVE_DISCHARGE_ENABLE; |
| 1604 | |
| 1605 | ret = ops->set_active_discharge(rdev, ad_state); |
| 1606 | if (ret < 0) { |
| 1607 | rdev_err(rdev, "failed to set active discharge: %pe\n" , ERR_PTR(ret)); |
| 1608 | return ret; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | /* |
| 1613 | * If there is no mechanism for controlling the regulator then |
| 1614 | * flag it as always_on so we don't end up duplicating checks |
| 1615 | * for this so much. Note that we could control the state of |
| 1616 | * a supply to control the output on a regulator that has no |
| 1617 | * direct control. |
| 1618 | */ |
| 1619 | if (!rdev->ena_pin && !ops->enable) { |
| 1620 | if (rdev->supply_name && !rdev->supply) |
| 1621 | return -EPROBE_DEFER; |
| 1622 | |
| 1623 | if (rdev->supply) |
| 1624 | rdev->constraints->always_on = |
| 1625 | rdev->supply->rdev->constraints->always_on; |
| 1626 | else |
| 1627 | rdev->constraints->always_on = true; |
| 1628 | } |
| 1629 | |
| 1630 | /* If the constraints say the regulator should be on at this point |
| 1631 | * and we have control then make sure it is enabled. |
| 1632 | */ |
| 1633 | if (rdev->constraints->always_on || rdev->constraints->boot_on) { |
| 1634 | bool supply_enabled = false; |
| 1635 | |
| 1636 | /* If we want to enable this regulator, make sure that we know |
| 1637 | * the supplying regulator. |
| 1638 | */ |
| 1639 | if (rdev->supply_name && !rdev->supply) |
| 1640 | return -EPROBE_DEFER; |
| 1641 | |
| 1642 | /* If supplying regulator has already been enabled, |
| 1643 | * it's not intended to have use_count increment |
| 1644 | * when rdev is only boot-on. |
| 1645 | */ |
| 1646 | if (rdev->supply && |
| 1647 | (rdev->constraints->always_on || |
| 1648 | !regulator_is_enabled(regulator: rdev->supply))) { |
| 1649 | ret = regulator_enable(regulator: rdev->supply); |
| 1650 | if (ret < 0) { |
| 1651 | _regulator_put(regulator: rdev->supply); |
| 1652 | rdev->supply = NULL; |
| 1653 | return ret; |
| 1654 | } |
| 1655 | supply_enabled = true; |
| 1656 | } |
| 1657 | |
| 1658 | ret = _regulator_do_enable(rdev); |
| 1659 | if (ret < 0 && ret != -EINVAL) { |
| 1660 | rdev_err(rdev, "failed to enable: %pe\n" , ERR_PTR(ret)); |
| 1661 | if (supply_enabled) |
| 1662 | regulator_disable(regulator: rdev->supply); |
| 1663 | return ret; |
| 1664 | } |
| 1665 | |
| 1666 | if (rdev->constraints->always_on) |
| 1667 | rdev->use_count++; |
| 1668 | } else if (rdev->desc->off_on_delay) { |
| 1669 | rdev->last_off = ktime_get(); |
| 1670 | } |
| 1671 | |
| 1672 | if (!rdev->constraints->pw_budget_mW) |
| 1673 | rdev->constraints->pw_budget_mW = INT_MAX; |
| 1674 | |
| 1675 | print_constraints(rdev); |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | /** |
| 1680 | * regulator_event_work_fn - process a deferred regulator event |
| 1681 | * @work: work_struct queued by the notifier |
| 1682 | * |
| 1683 | * Calls the regulator's notifier chain in process context while holding |
| 1684 | * the rdev lock, then releases the device reference. |
| 1685 | */ |
| 1686 | static void regulator_event_work_fn(struct work_struct *work) |
| 1687 | { |
| 1688 | struct regulator_event_work *rew = |
| 1689 | container_of(work, struct regulator_event_work, work); |
| 1690 | struct regulator_dev *rdev = rew->rdev; |
| 1691 | int ret; |
| 1692 | |
| 1693 | regulator_lock(rdev); |
| 1694 | ret = regulator_notifier_call_chain(rdev, event: rew->event, NULL); |
| 1695 | regulator_unlock(rdev); |
| 1696 | if (ret == NOTIFY_BAD) |
| 1697 | dev_err(rdev_get_dev(rdev), "failed to forward regulator event\n" ); |
| 1698 | |
| 1699 | put_device(dev: rdev_get_dev(rdev)); |
| 1700 | kfree(objp: rew); |
| 1701 | } |
| 1702 | |
| 1703 | /** |
| 1704 | * regulator_event_forward_notifier - notifier callback for supply events |
| 1705 | * @nb: notifier block embedded in the regulator |
| 1706 | * @event: regulator event code |
| 1707 | * @data: unused |
| 1708 | * |
| 1709 | * Packages the event into a work item and schedules it in process context. |
| 1710 | * Takes a reference on @rdev->dev to pin the regulator until the work |
| 1711 | * completes (see put_device() in the worker). |
| 1712 | * |
| 1713 | * Return: NOTIFY_OK on success, NOTIFY_DONE for events that are not forwarded. |
| 1714 | */ |
| 1715 | static int regulator_event_forward_notifier(struct notifier_block *nb, |
| 1716 | unsigned long event, |
| 1717 | void __always_unused *data) |
| 1718 | { |
| 1719 | struct regulator_dev *rdev = container_of(nb, struct regulator_dev, |
| 1720 | supply_fwd_nb); |
| 1721 | struct regulator_event_work *rew; |
| 1722 | |
| 1723 | switch (event) { |
| 1724 | case REGULATOR_EVENT_UNDER_VOLTAGE: |
| 1725 | break; |
| 1726 | default: |
| 1727 | /* Only forward allowed events downstream. */ |
| 1728 | return NOTIFY_DONE; |
| 1729 | } |
| 1730 | |
| 1731 | rew = kmalloc(sizeof(*rew), GFP_ATOMIC); |
| 1732 | if (!rew) |
| 1733 | return NOTIFY_DONE; |
| 1734 | |
| 1735 | get_device(dev: rdev_get_dev(rdev)); |
| 1736 | rew->rdev = rdev; |
| 1737 | rew->event = event; |
| 1738 | INIT_WORK(&rew->work, regulator_event_work_fn); |
| 1739 | |
| 1740 | queue_work(wq: system_highpri_wq, work: &rew->work); |
| 1741 | |
| 1742 | return NOTIFY_OK; |
| 1743 | } |
| 1744 | |
| 1745 | /** |
| 1746 | * register_regulator_event_forwarding - enable supply event forwarding |
| 1747 | * @rdev: regulator device |
| 1748 | * |
| 1749 | * Registers a notifier on the regulator's supply so that supply events |
| 1750 | * are forwarded to the consumer regulator via the deferred work handler. |
| 1751 | * |
| 1752 | * Return: 0 on success, -EALREADY if already enabled, or a negative error code. |
| 1753 | */ |
| 1754 | static int register_regulator_event_forwarding(struct regulator_dev *rdev) |
| 1755 | { |
| 1756 | int ret; |
| 1757 | |
| 1758 | if (!rdev->supply) |
| 1759 | return 0; /* top-level regulator: nothing to forward */ |
| 1760 | |
| 1761 | if (rdev->supply_fwd_nb.notifier_call) |
| 1762 | return -EALREADY; |
| 1763 | |
| 1764 | rdev->supply_fwd_nb.notifier_call = regulator_event_forward_notifier; |
| 1765 | |
| 1766 | ret = regulator_register_notifier(regulator: rdev->supply, nb: &rdev->supply_fwd_nb); |
| 1767 | if (ret) { |
| 1768 | dev_err(&rdev->dev, "failed to register supply notifier: %pe\n" , |
| 1769 | ERR_PTR(ret)); |
| 1770 | rdev->supply_fwd_nb.notifier_call = NULL; |
| 1771 | return ret; |
| 1772 | } |
| 1773 | |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
| 1777 | /** |
| 1778 | * set_supply - set regulator supply regulator |
| 1779 | * @rdev: regulator (locked) |
| 1780 | * @supply_rdev: supply regulator (locked)) |
| 1781 | * |
| 1782 | * Called by platform initialisation code to set the supply regulator for this |
| 1783 | * regulator. This ensures that a regulators supply will also be enabled by the |
| 1784 | * core if it's child is enabled. |
| 1785 | * |
| 1786 | * Return: 0 on success or a negative error number on failure. |
| 1787 | */ |
| 1788 | static int set_supply(struct regulator_dev *rdev, |
| 1789 | struct regulator_dev *supply_rdev) |
| 1790 | { |
| 1791 | int err; |
| 1792 | |
| 1793 | rdev_dbg(rdev, "supplied by %s\n" , rdev_get_name(supply_rdev)); |
| 1794 | |
| 1795 | if (!try_module_get(module: supply_rdev->owner)) |
| 1796 | return -ENODEV; |
| 1797 | |
| 1798 | rdev->supply = create_regulator(rdev: supply_rdev, dev: &rdev->dev, supply_name: "SUPPLY" ); |
| 1799 | if (rdev->supply == NULL) { |
| 1800 | module_put(module: supply_rdev->owner); |
| 1801 | err = -ENOMEM; |
| 1802 | return err; |
| 1803 | } |
| 1804 | supply_rdev->open_count++; |
| 1805 | |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
| 1809 | /** |
| 1810 | * set_consumer_device_supply - Bind a regulator to a symbolic supply |
| 1811 | * @rdev: regulator source |
| 1812 | * @consumer_dev_name: dev_name() string for device supply applies to |
| 1813 | * @supply: symbolic name for supply |
| 1814 | * |
| 1815 | * Allows platform initialisation code to map physical regulator |
| 1816 | * sources to symbolic names for supplies for use by devices. Devices |
| 1817 | * should use these symbolic names to request regulators, avoiding the |
| 1818 | * need to provide board-specific regulator names as platform data. |
| 1819 | * |
| 1820 | * Return: 0 on success or a negative error number on failure. |
| 1821 | */ |
| 1822 | static int set_consumer_device_supply(struct regulator_dev *rdev, |
| 1823 | const char *consumer_dev_name, |
| 1824 | const char *supply) |
| 1825 | { |
| 1826 | struct regulator_map *node, *new_node; |
| 1827 | int has_dev; |
| 1828 | |
| 1829 | if (supply == NULL) |
| 1830 | return -EINVAL; |
| 1831 | |
| 1832 | if (consumer_dev_name != NULL) |
| 1833 | has_dev = 1; |
| 1834 | else |
| 1835 | has_dev = 0; |
| 1836 | |
| 1837 | new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); |
| 1838 | if (new_node == NULL) |
| 1839 | return -ENOMEM; |
| 1840 | |
| 1841 | new_node->regulator = rdev; |
| 1842 | new_node->supply = supply; |
| 1843 | |
| 1844 | if (has_dev) { |
| 1845 | new_node->dev_name = kstrdup(s: consumer_dev_name, GFP_KERNEL); |
| 1846 | if (new_node->dev_name == NULL) { |
| 1847 | kfree(objp: new_node); |
| 1848 | return -ENOMEM; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | mutex_lock(®ulator_list_mutex); |
| 1853 | list_for_each_entry(node, ®ulator_map_list, list) { |
| 1854 | if (node->dev_name && consumer_dev_name) { |
| 1855 | if (strcmp(node->dev_name, consumer_dev_name) != 0) |
| 1856 | continue; |
| 1857 | } else if (node->dev_name || consumer_dev_name) { |
| 1858 | continue; |
| 1859 | } |
| 1860 | |
| 1861 | if (strcmp(node->supply, supply) != 0) |
| 1862 | continue; |
| 1863 | |
| 1864 | pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n" , |
| 1865 | consumer_dev_name, |
| 1866 | dev_name(&node->regulator->dev), |
| 1867 | node->regulator->desc->name, |
| 1868 | supply, |
| 1869 | dev_name(&rdev->dev), rdev_get_name(rdev)); |
| 1870 | goto fail; |
| 1871 | } |
| 1872 | |
| 1873 | list_add(new: &new_node->list, head: ®ulator_map_list); |
| 1874 | mutex_unlock(lock: ®ulator_list_mutex); |
| 1875 | |
| 1876 | return 0; |
| 1877 | |
| 1878 | fail: |
| 1879 | mutex_unlock(lock: ®ulator_list_mutex); |
| 1880 | kfree(objp: new_node->dev_name); |
| 1881 | kfree(objp: new_node); |
| 1882 | return -EBUSY; |
| 1883 | } |
| 1884 | |
| 1885 | static void unset_regulator_supplies(struct regulator_dev *rdev) |
| 1886 | { |
| 1887 | struct regulator_map *node, *n; |
| 1888 | |
| 1889 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { |
| 1890 | if (rdev == node->regulator) { |
| 1891 | list_del(entry: &node->list); |
| 1892 | kfree(objp: node->dev_name); |
| 1893 | kfree(objp: node); |
| 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | #ifdef CONFIG_DEBUG_FS |
| 1899 | static ssize_t constraint_flags_read_file(struct file *file, |
| 1900 | char __user *user_buf, |
| 1901 | size_t count, loff_t *ppos) |
| 1902 | { |
| 1903 | const struct regulator *regulator = file->private_data; |
| 1904 | const struct regulation_constraints *c = regulator->rdev->constraints; |
| 1905 | char *buf; |
| 1906 | ssize_t ret; |
| 1907 | |
| 1908 | if (!c) |
| 1909 | return 0; |
| 1910 | |
| 1911 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 1912 | if (!buf) |
| 1913 | return -ENOMEM; |
| 1914 | |
| 1915 | ret = snprintf(buf, PAGE_SIZE, |
| 1916 | fmt: "always_on: %u\n" |
| 1917 | "boot_on: %u\n" |
| 1918 | "apply_uV: %u\n" |
| 1919 | "ramp_disable: %u\n" |
| 1920 | "soft_start: %u\n" |
| 1921 | "pull_down: %u\n" |
| 1922 | "over_current_protection: %u\n" , |
| 1923 | c->always_on, |
| 1924 | c->boot_on, |
| 1925 | c->apply_uV, |
| 1926 | c->ramp_disable, |
| 1927 | c->soft_start, |
| 1928 | c->pull_down, |
| 1929 | c->over_current_protection); |
| 1930 | |
| 1931 | ret = simple_read_from_buffer(to: user_buf, count, ppos, from: buf, available: ret); |
| 1932 | kfree(objp: buf); |
| 1933 | |
| 1934 | return ret; |
| 1935 | } |
| 1936 | |
| 1937 | #endif |
| 1938 | |
| 1939 | static const struct file_operations constraint_flags_fops = { |
| 1940 | #ifdef CONFIG_DEBUG_FS |
| 1941 | .open = simple_open, |
| 1942 | .read = constraint_flags_read_file, |
| 1943 | .llseek = default_llseek, |
| 1944 | #endif |
| 1945 | }; |
| 1946 | |
| 1947 | #define REG_STR_SIZE 64 |
| 1948 | |
| 1949 | static void link_and_create_debugfs(struct regulator *regulator, struct regulator_dev *rdev, |
| 1950 | struct device *dev) |
| 1951 | { |
| 1952 | int err = 0; |
| 1953 | |
| 1954 | if (dev) { |
| 1955 | regulator->dev = dev; |
| 1956 | |
| 1957 | /* Add a link to the device sysfs entry */ |
| 1958 | err = sysfs_create_link_nowarn(kobj: &rdev->dev.kobj, target: &dev->kobj, |
| 1959 | name: regulator->supply_name); |
| 1960 | if (err) { |
| 1961 | rdev_dbg(rdev, "could not add device link %s: %pe\n" , |
| 1962 | dev->kobj.name, ERR_PTR(err)); |
| 1963 | /* non-fatal */ |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | if (err != -EEXIST) { |
| 1968 | regulator->debugfs = debugfs_create_dir(name: regulator->supply_name, parent: rdev->debugfs); |
| 1969 | if (IS_ERR(ptr: regulator->debugfs)) { |
| 1970 | rdev_dbg(rdev, "Failed to create debugfs directory\n" ); |
| 1971 | regulator->debugfs = NULL; |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | if (regulator->debugfs) { |
| 1976 | debugfs_create_u32(name: "uA_load" , mode: 0444, parent: regulator->debugfs, |
| 1977 | value: ®ulator->uA_load); |
| 1978 | debugfs_create_u32(name: "min_uV" , mode: 0444, parent: regulator->debugfs, |
| 1979 | value: ®ulator->voltage[PM_SUSPEND_ON].min_uV); |
| 1980 | debugfs_create_u32(name: "max_uV" , mode: 0444, parent: regulator->debugfs, |
| 1981 | value: ®ulator->voltage[PM_SUSPEND_ON].max_uV); |
| 1982 | debugfs_create_file("constraint_flags" , 0444, regulator->debugfs, |
| 1983 | regulator, &constraint_flags_fops); |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | static struct regulator *create_regulator(struct regulator_dev *rdev, |
| 1988 | struct device *dev, |
| 1989 | const char *supply_name) |
| 1990 | { |
| 1991 | struct regulator *regulator; |
| 1992 | |
| 1993 | lockdep_assert_held_once(&rdev->mutex.base); |
| 1994 | |
| 1995 | if (dev) { |
| 1996 | char buf[REG_STR_SIZE]; |
| 1997 | int size; |
| 1998 | |
| 1999 | size = snprintf(buf, REG_STR_SIZE, fmt: "%s-%s" , |
| 2000 | dev->kobj.name, supply_name); |
| 2001 | if (size >= REG_STR_SIZE) |
| 2002 | return NULL; |
| 2003 | |
| 2004 | supply_name = kstrdup(s: buf, GFP_KERNEL); |
| 2005 | if (supply_name == NULL) |
| 2006 | return NULL; |
| 2007 | } else { |
| 2008 | supply_name = kstrdup_const(s: supply_name, GFP_KERNEL); |
| 2009 | if (supply_name == NULL) |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | |
| 2013 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); |
| 2014 | if (regulator == NULL) { |
| 2015 | kfree_const(x: supply_name); |
| 2016 | return NULL; |
| 2017 | } |
| 2018 | |
| 2019 | regulator->rdev = rdev; |
| 2020 | regulator->supply_name = supply_name; |
| 2021 | |
| 2022 | list_add(new: ®ulator->list, head: &rdev->consumer_list); |
| 2023 | |
| 2024 | /* |
| 2025 | * Check now if the regulator is an always on regulator - if |
| 2026 | * it is then we don't need to do nearly so much work for |
| 2027 | * enable/disable calls. |
| 2028 | */ |
| 2029 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) && |
| 2030 | _regulator_is_enabled(rdev)) |
| 2031 | regulator->always_on = true; |
| 2032 | |
| 2033 | return regulator; |
| 2034 | } |
| 2035 | |
| 2036 | static int _regulator_get_enable_time(struct regulator_dev *rdev) |
| 2037 | { |
| 2038 | if (rdev->constraints && rdev->constraints->enable_time) |
| 2039 | return rdev->constraints->enable_time; |
| 2040 | if (rdev->desc->ops->enable_time) |
| 2041 | return rdev->desc->ops->enable_time(rdev); |
| 2042 | return rdev->desc->enable_time; |
| 2043 | } |
| 2044 | |
| 2045 | static struct regulator_supply_alias *regulator_find_supply_alias( |
| 2046 | struct device *dev, const char *supply) |
| 2047 | { |
| 2048 | struct regulator_supply_alias *map; |
| 2049 | |
| 2050 | list_for_each_entry(map, ®ulator_supply_alias_list, list) |
| 2051 | if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0) |
| 2052 | return map; |
| 2053 | |
| 2054 | return NULL; |
| 2055 | } |
| 2056 | |
| 2057 | static void regulator_supply_alias(struct device **dev, const char **supply) |
| 2058 | { |
| 2059 | struct regulator_supply_alias *map; |
| 2060 | |
| 2061 | mutex_lock(®ulator_list_mutex); |
| 2062 | map = regulator_find_supply_alias(dev: *dev, supply: *supply); |
| 2063 | if (map) { |
| 2064 | dev_dbg(*dev, "Mapping supply %s to %s,%s\n" , |
| 2065 | *supply, map->alias_supply, |
| 2066 | dev_name(map->alias_dev)); |
| 2067 | *dev = map->alias_dev; |
| 2068 | *supply = map->alias_supply; |
| 2069 | } |
| 2070 | mutex_unlock(lock: ®ulator_list_mutex); |
| 2071 | } |
| 2072 | |
| 2073 | static int regulator_match(struct device *dev, const void *data) |
| 2074 | { |
| 2075 | struct regulator_dev *r = dev_to_rdev(dev); |
| 2076 | |
| 2077 | return strcmp(rdev_get_name(r), data) == 0; |
| 2078 | } |
| 2079 | |
| 2080 | static struct regulator_dev *regulator_lookup_by_name(const char *name) |
| 2081 | { |
| 2082 | struct device *dev; |
| 2083 | |
| 2084 | dev = class_find_device(class: ®ulator_class, NULL, data: name, match: regulator_match); |
| 2085 | |
| 2086 | return dev ? dev_to_rdev(dev) : NULL; |
| 2087 | } |
| 2088 | |
| 2089 | static struct regulator_dev *regulator_dt_lookup(struct device *dev, |
| 2090 | const char *supply) |
| 2091 | { |
| 2092 | struct regulator_dev *r = NULL; |
| 2093 | |
| 2094 | if (dev_of_node(dev)) { |
| 2095 | r = of_regulator_dev_lookup(dev, np: dev_of_node(dev), supply); |
| 2096 | if (PTR_ERR(ptr: r) == -ENODEV) |
| 2097 | r = NULL; |
| 2098 | } |
| 2099 | |
| 2100 | return r; |
| 2101 | } |
| 2102 | |
| 2103 | /** |
| 2104 | * regulator_dev_lookup - lookup a regulator device. |
| 2105 | * @dev: device for regulator "consumer". |
| 2106 | * @supply: Supply name or regulator ID. |
| 2107 | * |
| 2108 | * Return: pointer to &struct regulator_dev or ERR_PTR() encoded negative error number. |
| 2109 | * |
| 2110 | * If successful, returns a struct regulator_dev that corresponds to the name |
| 2111 | * @supply and with the embedded struct device refcount incremented by one. |
| 2112 | * The refcount must be dropped by calling put_device(). |
| 2113 | * On failure one of the following ERR_PTR() encoded values is returned: |
| 2114 | * -%ENODEV if lookup fails permanently, -%EPROBE_DEFER if lookup could succeed |
| 2115 | * in the future. |
| 2116 | */ |
| 2117 | static struct regulator_dev *regulator_dev_lookup(struct device *dev, |
| 2118 | const char *supply) |
| 2119 | { |
| 2120 | struct regulator_dev *r = NULL; |
| 2121 | struct regulator_map *map; |
| 2122 | const char *devname = NULL; |
| 2123 | |
| 2124 | regulator_supply_alias(dev: &dev, supply: &supply); |
| 2125 | |
| 2126 | /* first do a dt based lookup */ |
| 2127 | r = regulator_dt_lookup(dev, supply); |
| 2128 | if (r) |
| 2129 | return r; |
| 2130 | |
| 2131 | /* if not found, try doing it non-dt way */ |
| 2132 | if (dev) |
| 2133 | devname = dev_name(dev); |
| 2134 | |
| 2135 | mutex_lock(®ulator_list_mutex); |
| 2136 | list_for_each_entry(map, ®ulator_map_list, list) { |
| 2137 | /* If the mapping has a device set up it must match */ |
| 2138 | if (map->dev_name && |
| 2139 | (!devname || strcmp(map->dev_name, devname))) |
| 2140 | continue; |
| 2141 | |
| 2142 | if (strcmp(map->supply, supply) == 0 && |
| 2143 | get_device(dev: &map->regulator->dev)) { |
| 2144 | r = map->regulator; |
| 2145 | break; |
| 2146 | } |
| 2147 | } |
| 2148 | mutex_unlock(lock: ®ulator_list_mutex); |
| 2149 | |
| 2150 | if (r) |
| 2151 | return r; |
| 2152 | |
| 2153 | r = regulator_lookup_by_name(name: supply); |
| 2154 | if (r) |
| 2155 | return r; |
| 2156 | |
| 2157 | return ERR_PTR(error: -ENODEV); |
| 2158 | } |
| 2159 | |
| 2160 | static int regulator_resolve_supply(struct regulator_dev *rdev) |
| 2161 | { |
| 2162 | struct regulator_dev *r; |
| 2163 | struct device *dev = rdev->dev.parent; |
| 2164 | struct ww_acquire_ctx ww_ctx; |
| 2165 | int ret = 0; |
| 2166 | |
| 2167 | /* No supply to resolve? */ |
| 2168 | if (!rdev->supply_name) |
| 2169 | return 0; |
| 2170 | |
| 2171 | /* Supply already resolved? (fast-path without locking contention) */ |
| 2172 | if (rdev->supply) |
| 2173 | return 0; |
| 2174 | |
| 2175 | /* first do a dt based lookup on the node described in the virtual |
| 2176 | * device. |
| 2177 | */ |
| 2178 | r = regulator_dt_lookup(dev: &rdev->dev, supply: rdev->supply_name); |
| 2179 | |
| 2180 | /* If regulator not found use usual search path in the parent |
| 2181 | * device. |
| 2182 | */ |
| 2183 | if (!r) |
| 2184 | r = regulator_dev_lookup(dev, supply: rdev->supply_name); |
| 2185 | |
| 2186 | if (IS_ERR(ptr: r)) { |
| 2187 | ret = PTR_ERR(ptr: r); |
| 2188 | |
| 2189 | /* Did the lookup explicitly defer for us? */ |
| 2190 | if (ret == -EPROBE_DEFER) |
| 2191 | goto out; |
| 2192 | |
| 2193 | if (have_full_constraints()) { |
| 2194 | r = dummy_regulator_rdev; |
| 2195 | if (!r) { |
| 2196 | ret = -EPROBE_DEFER; |
| 2197 | goto out; |
| 2198 | } |
| 2199 | get_device(dev: &r->dev); |
| 2200 | } else { |
| 2201 | dev_err(dev, "Failed to resolve %s-supply for %s\n" , |
| 2202 | rdev->supply_name, rdev->desc->name); |
| 2203 | ret = -EPROBE_DEFER; |
| 2204 | goto out; |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | if (r == rdev) { |
| 2209 | dev_err(dev, "Supply for %s (%s) resolved to itself\n" , |
| 2210 | rdev->desc->name, rdev->supply_name); |
| 2211 | if (!have_full_constraints()) { |
| 2212 | ret = -EINVAL; |
| 2213 | goto out; |
| 2214 | } |
| 2215 | r = dummy_regulator_rdev; |
| 2216 | if (!r) { |
| 2217 | ret = -EPROBE_DEFER; |
| 2218 | goto out; |
| 2219 | } |
| 2220 | get_device(dev: &r->dev); |
| 2221 | } |
| 2222 | |
| 2223 | /* |
| 2224 | * If the supply's parent device is not the same as the |
| 2225 | * regulator's parent device, then ensure the parent device |
| 2226 | * is bound before we resolve the supply, in case the parent |
| 2227 | * device get probe deferred and unregisters the supply. |
| 2228 | */ |
| 2229 | if (r->dev.parent && r->dev.parent != rdev->dev.parent) { |
| 2230 | if (!device_is_bound(dev: r->dev.parent)) { |
| 2231 | put_device(dev: &r->dev); |
| 2232 | ret = -EPROBE_DEFER; |
| 2233 | goto out; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | /* Recursively resolve the supply of the supply */ |
| 2238 | ret = regulator_resolve_supply(rdev: r); |
| 2239 | if (ret < 0) { |
| 2240 | put_device(dev: &r->dev); |
| 2241 | goto out; |
| 2242 | } |
| 2243 | |
| 2244 | /* |
| 2245 | * Recheck rdev->supply with rdev->mutex lock held to avoid a race |
| 2246 | * between rdev->supply null check and setting rdev->supply in |
| 2247 | * set_supply() from concurrent tasks. |
| 2248 | */ |
| 2249 | regulator_lock_two(rdev1: rdev, rdev2: r, ww_ctx: &ww_ctx); |
| 2250 | |
| 2251 | /* Supply just resolved by a concurrent task? */ |
| 2252 | if (rdev->supply) { |
| 2253 | regulator_unlock_two(rdev1: rdev, rdev2: r, ww_ctx: &ww_ctx); |
| 2254 | put_device(dev: &r->dev); |
| 2255 | goto out; |
| 2256 | } |
| 2257 | |
| 2258 | ret = set_supply(rdev, supply_rdev: r); |
| 2259 | if (ret < 0) { |
| 2260 | regulator_unlock_two(rdev1: rdev, rdev2: r, ww_ctx: &ww_ctx); |
| 2261 | put_device(dev: &r->dev); |
| 2262 | goto out; |
| 2263 | } |
| 2264 | |
| 2265 | /* |
| 2266 | * Automatically register for event forwarding from the new supply. |
| 2267 | * This creates the downstream propagation link for events like |
| 2268 | * under-voltage. |
| 2269 | */ |
| 2270 | ret = register_regulator_event_forwarding(rdev); |
| 2271 | if (ret < 0) |
| 2272 | rdev_warn(rdev, "Failed to register event forwarding: %pe\n" , |
| 2273 | ERR_PTR(ret)); |
| 2274 | |
| 2275 | regulator_unlock_two(rdev1: rdev, rdev2: r, ww_ctx: &ww_ctx); |
| 2276 | |
| 2277 | /* rdev->supply was created in set_supply() */ |
| 2278 | link_and_create_debugfs(regulator: rdev->supply, rdev: r, dev: &rdev->dev); |
| 2279 | |
| 2280 | /* |
| 2281 | * In set_machine_constraints() we may have turned this regulator on |
| 2282 | * but we couldn't propagate to the supply if it hadn't been resolved |
| 2283 | * yet. Do it now. |
| 2284 | */ |
| 2285 | if (rdev->use_count) { |
| 2286 | ret = regulator_enable(regulator: rdev->supply); |
| 2287 | if (ret < 0) { |
| 2288 | _regulator_put(regulator: rdev->supply); |
| 2289 | rdev->supply = NULL; |
| 2290 | goto out; |
| 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | out: |
| 2295 | return ret; |
| 2296 | } |
| 2297 | |
| 2298 | /* common pre-checks for regulator requests */ |
| 2299 | int _regulator_get_common_check(struct device *dev, const char *id, |
| 2300 | enum regulator_get_type get_type) |
| 2301 | { |
| 2302 | if (get_type >= MAX_GET_TYPE) { |
| 2303 | dev_err(dev, "invalid type %d in %s\n" , get_type, __func__); |
| 2304 | return -EINVAL; |
| 2305 | } |
| 2306 | |
| 2307 | if (id == NULL) { |
| 2308 | dev_err(dev, "regulator request with no identifier\n" ); |
| 2309 | return -EINVAL; |
| 2310 | } |
| 2311 | |
| 2312 | return 0; |
| 2313 | } |
| 2314 | |
| 2315 | /** |
| 2316 | * _regulator_get_common - Common code for regulator requests |
| 2317 | * @rdev: regulator device pointer as returned by *regulator_dev_lookup() |
| 2318 | * Its reference count is expected to have been incremented. |
| 2319 | * @dev: device used for dev_printk messages |
| 2320 | * @id: Supply name or regulator ID |
| 2321 | * @get_type: enum regulator_get_type value corresponding to type of request |
| 2322 | * |
| 2323 | * Returns: pointer to struct regulator corresponding to @rdev, or ERR_PTR() |
| 2324 | * encoded error. |
| 2325 | * |
| 2326 | * This function should be chained with *regulator_dev_lookup() functions. |
| 2327 | */ |
| 2328 | struct regulator *_regulator_get_common(struct regulator_dev *rdev, struct device *dev, |
| 2329 | const char *id, enum regulator_get_type get_type) |
| 2330 | { |
| 2331 | struct regulator *regulator; |
| 2332 | struct device_link *link; |
| 2333 | int ret; |
| 2334 | |
| 2335 | if (IS_ERR(ptr: rdev)) { |
| 2336 | ret = PTR_ERR(ptr: rdev); |
| 2337 | |
| 2338 | /* |
| 2339 | * If regulator_dev_lookup() fails with error other |
| 2340 | * than -ENODEV our job here is done, we simply return it. |
| 2341 | */ |
| 2342 | if (ret != -ENODEV) |
| 2343 | return ERR_PTR(error: ret); |
| 2344 | |
| 2345 | if (!have_full_constraints()) { |
| 2346 | dev_warn(dev, |
| 2347 | "incomplete constraints, dummy supplies not allowed (id=%s)\n" , id); |
| 2348 | return ERR_PTR(error: -ENODEV); |
| 2349 | } |
| 2350 | |
| 2351 | switch (get_type) { |
| 2352 | case NORMAL_GET: |
| 2353 | /* |
| 2354 | * Assume that a regulator is physically present and |
| 2355 | * enabled, even if it isn't hooked up, and just |
| 2356 | * provide a dummy. |
| 2357 | */ |
| 2358 | rdev = dummy_regulator_rdev; |
| 2359 | if (!rdev) |
| 2360 | return ERR_PTR(error: -EPROBE_DEFER); |
| 2361 | dev_warn(dev, "supply %s not found, using dummy regulator\n" , id); |
| 2362 | get_device(dev: &rdev->dev); |
| 2363 | break; |
| 2364 | |
| 2365 | case EXCLUSIVE_GET: |
| 2366 | dev_warn(dev, |
| 2367 | "dummy supplies not allowed for exclusive requests (id=%s)\n" , id); |
| 2368 | fallthrough; |
| 2369 | |
| 2370 | default: |
| 2371 | return ERR_PTR(error: -ENODEV); |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | if (rdev->exclusive) { |
| 2376 | regulator = ERR_PTR(error: -EPERM); |
| 2377 | put_device(dev: &rdev->dev); |
| 2378 | return regulator; |
| 2379 | } |
| 2380 | |
| 2381 | if (get_type == EXCLUSIVE_GET && rdev->open_count) { |
| 2382 | regulator = ERR_PTR(error: -EBUSY); |
| 2383 | put_device(dev: &rdev->dev); |
| 2384 | return regulator; |
| 2385 | } |
| 2386 | |
| 2387 | mutex_lock(®ulator_list_mutex); |
| 2388 | ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled); |
| 2389 | mutex_unlock(lock: ®ulator_list_mutex); |
| 2390 | |
| 2391 | if (ret != 0) { |
| 2392 | regulator = ERR_PTR(error: -EPROBE_DEFER); |
| 2393 | put_device(dev: &rdev->dev); |
| 2394 | return regulator; |
| 2395 | } |
| 2396 | |
| 2397 | ret = regulator_resolve_supply(rdev); |
| 2398 | if (ret < 0) { |
| 2399 | regulator = ERR_PTR(error: ret); |
| 2400 | put_device(dev: &rdev->dev); |
| 2401 | return regulator; |
| 2402 | } |
| 2403 | |
| 2404 | if (!try_module_get(module: rdev->owner)) { |
| 2405 | regulator = ERR_PTR(error: -EPROBE_DEFER); |
| 2406 | put_device(dev: &rdev->dev); |
| 2407 | return regulator; |
| 2408 | } |
| 2409 | |
| 2410 | regulator_lock(rdev); |
| 2411 | regulator = create_regulator(rdev, dev, supply_name: id); |
| 2412 | regulator_unlock(rdev); |
| 2413 | if (regulator == NULL) { |
| 2414 | regulator = ERR_PTR(error: -ENOMEM); |
| 2415 | module_put(module: rdev->owner); |
| 2416 | put_device(dev: &rdev->dev); |
| 2417 | return regulator; |
| 2418 | } |
| 2419 | |
| 2420 | link_and_create_debugfs(regulator, rdev, dev); |
| 2421 | |
| 2422 | rdev->open_count++; |
| 2423 | if (get_type == EXCLUSIVE_GET) { |
| 2424 | rdev->exclusive = 1; |
| 2425 | |
| 2426 | ret = _regulator_is_enabled(rdev); |
| 2427 | if (ret > 0) { |
| 2428 | rdev->use_count = 1; |
| 2429 | regulator->enable_count = 1; |
| 2430 | |
| 2431 | /* Propagate the regulator state to its supply */ |
| 2432 | if (rdev->supply) { |
| 2433 | ret = regulator_enable(regulator: rdev->supply); |
| 2434 | if (ret < 0) { |
| 2435 | destroy_regulator(regulator); |
| 2436 | module_put(module: rdev->owner); |
| 2437 | put_device(dev: &rdev->dev); |
| 2438 | return ERR_PTR(error: ret); |
| 2439 | } |
| 2440 | } |
| 2441 | } else { |
| 2442 | rdev->use_count = 0; |
| 2443 | regulator->enable_count = 0; |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | link = device_link_add(consumer: dev, supplier: &rdev->dev, DL_FLAG_STATELESS); |
| 2448 | if (!IS_ERR_OR_NULL(ptr: link)) |
| 2449 | regulator->device_link = true; |
| 2450 | |
| 2451 | return regulator; |
| 2452 | } |
| 2453 | |
| 2454 | /* Internal regulator request function */ |
| 2455 | struct regulator *_regulator_get(struct device *dev, const char *id, |
| 2456 | enum regulator_get_type get_type) |
| 2457 | { |
| 2458 | struct regulator_dev *rdev; |
| 2459 | int ret; |
| 2460 | |
| 2461 | ret = _regulator_get_common_check(dev, id, get_type); |
| 2462 | if (ret) |
| 2463 | return ERR_PTR(error: ret); |
| 2464 | |
| 2465 | rdev = regulator_dev_lookup(dev, supply: id); |
| 2466 | return _regulator_get_common(rdev, dev, id, get_type); |
| 2467 | } |
| 2468 | |
| 2469 | /** |
| 2470 | * regulator_get - lookup and obtain a reference to a regulator. |
| 2471 | * @dev: device for regulator "consumer" |
| 2472 | * @id: Supply name or regulator ID. |
| 2473 | * |
| 2474 | * Use of supply names configured via set_consumer_device_supply() is |
| 2475 | * strongly encouraged. It is recommended that the supply name used |
| 2476 | * should match the name used for the supply and/or the relevant |
| 2477 | * device pins in the datasheet. |
| 2478 | * |
| 2479 | * Return: Pointer to a &struct regulator corresponding to the regulator |
| 2480 | * producer, or an ERR_PTR() encoded negative error number. |
| 2481 | */ |
| 2482 | struct regulator *regulator_get(struct device *dev, const char *id) |
| 2483 | { |
| 2484 | return _regulator_get(dev, id, get_type: NORMAL_GET); |
| 2485 | } |
| 2486 | EXPORT_SYMBOL_GPL(regulator_get); |
| 2487 | |
| 2488 | /** |
| 2489 | * regulator_get_exclusive - obtain exclusive access to a regulator. |
| 2490 | * @dev: device for regulator "consumer" |
| 2491 | * @id: Supply name or regulator ID. |
| 2492 | * |
| 2493 | * Other consumers will be unable to obtain this regulator while this |
| 2494 | * reference is held and the use count for the regulator will be |
| 2495 | * initialised to reflect the current state of the regulator. |
| 2496 | * |
| 2497 | * This is intended for use by consumers which cannot tolerate shared |
| 2498 | * use of the regulator such as those which need to force the |
| 2499 | * regulator off for correct operation of the hardware they are |
| 2500 | * controlling. |
| 2501 | * |
| 2502 | * Use of supply names configured via set_consumer_device_supply() is |
| 2503 | * strongly encouraged. It is recommended that the supply name used |
| 2504 | * should match the name used for the supply and/or the relevant |
| 2505 | * device pins in the datasheet. |
| 2506 | * |
| 2507 | * Return: Pointer to a &struct regulator corresponding to the regulator |
| 2508 | * producer, or an ERR_PTR() encoded negative error number. |
| 2509 | */ |
| 2510 | struct regulator *regulator_get_exclusive(struct device *dev, const char *id) |
| 2511 | { |
| 2512 | return _regulator_get(dev, id, get_type: EXCLUSIVE_GET); |
| 2513 | } |
| 2514 | EXPORT_SYMBOL_GPL(regulator_get_exclusive); |
| 2515 | |
| 2516 | /** |
| 2517 | * regulator_get_optional - obtain optional access to a regulator. |
| 2518 | * @dev: device for regulator "consumer" |
| 2519 | * @id: Supply name or regulator ID. |
| 2520 | * |
| 2521 | * This is intended for use by consumers for devices which can have |
| 2522 | * some supplies unconnected in normal use, such as some MMC devices. |
| 2523 | * It can allow the regulator core to provide stub supplies for other |
| 2524 | * supplies requested using normal regulator_get() calls without |
| 2525 | * disrupting the operation of drivers that can handle absent |
| 2526 | * supplies. |
| 2527 | * |
| 2528 | * Use of supply names configured via set_consumer_device_supply() is |
| 2529 | * strongly encouraged. It is recommended that the supply name used |
| 2530 | * should match the name used for the supply and/or the relevant |
| 2531 | * device pins in the datasheet. |
| 2532 | * |
| 2533 | * Return: Pointer to a &struct regulator corresponding to the regulator |
| 2534 | * producer, or an ERR_PTR() encoded negative error number. |
| 2535 | */ |
| 2536 | struct regulator *regulator_get_optional(struct device *dev, const char *id) |
| 2537 | { |
| 2538 | return _regulator_get(dev, id, get_type: OPTIONAL_GET); |
| 2539 | } |
| 2540 | EXPORT_SYMBOL_GPL(regulator_get_optional); |
| 2541 | |
| 2542 | static void destroy_regulator(struct regulator *regulator) |
| 2543 | { |
| 2544 | struct regulator_dev *rdev = regulator->rdev; |
| 2545 | |
| 2546 | debugfs_remove_recursive(dentry: regulator->debugfs); |
| 2547 | |
| 2548 | if (regulator->dev) { |
| 2549 | if (regulator->device_link) |
| 2550 | device_link_remove(consumer: regulator->dev, supplier: &rdev->dev); |
| 2551 | |
| 2552 | /* remove any sysfs entries */ |
| 2553 | sysfs_remove_link(kobj: &rdev->dev.kobj, name: regulator->supply_name); |
| 2554 | } |
| 2555 | |
| 2556 | regulator_lock(rdev); |
| 2557 | list_del(entry: ®ulator->list); |
| 2558 | |
| 2559 | rdev->open_count--; |
| 2560 | rdev->exclusive = 0; |
| 2561 | regulator_unlock(rdev); |
| 2562 | |
| 2563 | kfree_const(x: regulator->supply_name); |
| 2564 | kfree(objp: regulator); |
| 2565 | } |
| 2566 | |
| 2567 | /* regulator_list_mutex lock held by regulator_put() */ |
| 2568 | static void _regulator_put(struct regulator *regulator) |
| 2569 | { |
| 2570 | struct regulator_dev *rdev; |
| 2571 | |
| 2572 | if (IS_ERR_OR_NULL(ptr: regulator)) |
| 2573 | return; |
| 2574 | |
| 2575 | lockdep_assert_held_once(®ulator_list_mutex); |
| 2576 | |
| 2577 | /* Docs say you must disable before calling regulator_put() */ |
| 2578 | WARN_ON(regulator->enable_count); |
| 2579 | |
| 2580 | rdev = regulator->rdev; |
| 2581 | |
| 2582 | destroy_regulator(regulator); |
| 2583 | |
| 2584 | module_put(module: rdev->owner); |
| 2585 | put_device(dev: &rdev->dev); |
| 2586 | } |
| 2587 | |
| 2588 | /** |
| 2589 | * regulator_put - "free" the regulator source |
| 2590 | * @regulator: regulator source |
| 2591 | * |
| 2592 | * Note: drivers must ensure that all regulator_enable calls made on this |
| 2593 | * regulator source are balanced by regulator_disable calls prior to calling |
| 2594 | * this function. |
| 2595 | */ |
| 2596 | void regulator_put(struct regulator *regulator) |
| 2597 | { |
| 2598 | mutex_lock(®ulator_list_mutex); |
| 2599 | _regulator_put(regulator); |
| 2600 | mutex_unlock(lock: ®ulator_list_mutex); |
| 2601 | } |
| 2602 | EXPORT_SYMBOL_GPL(regulator_put); |
| 2603 | |
| 2604 | /** |
| 2605 | * regulator_register_supply_alias - Provide device alias for supply lookup |
| 2606 | * |
| 2607 | * @dev: device that will be given as the regulator "consumer" |
| 2608 | * @id: Supply name or regulator ID |
| 2609 | * @alias_dev: device that should be used to lookup the supply |
| 2610 | * @alias_id: Supply name or regulator ID that should be used to lookup the |
| 2611 | * supply |
| 2612 | * |
| 2613 | * All lookups for id on dev will instead be conducted for alias_id on |
| 2614 | * alias_dev. |
| 2615 | * |
| 2616 | * Return: 0 on success or a negative error number on failure. |
| 2617 | */ |
| 2618 | int regulator_register_supply_alias(struct device *dev, const char *id, |
| 2619 | struct device *alias_dev, |
| 2620 | const char *alias_id) |
| 2621 | { |
| 2622 | struct regulator_supply_alias *map; |
| 2623 | struct regulator_supply_alias *new_map; |
| 2624 | |
| 2625 | new_map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL); |
| 2626 | if (!new_map) |
| 2627 | return -ENOMEM; |
| 2628 | |
| 2629 | mutex_lock(®ulator_list_mutex); |
| 2630 | map = regulator_find_supply_alias(dev, supply: id); |
| 2631 | if (map) { |
| 2632 | mutex_unlock(lock: ®ulator_list_mutex); |
| 2633 | kfree(objp: new_map); |
| 2634 | return -EEXIST; |
| 2635 | } |
| 2636 | |
| 2637 | new_map->src_dev = dev; |
| 2638 | new_map->src_supply = id; |
| 2639 | new_map->alias_dev = alias_dev; |
| 2640 | new_map->alias_supply = alias_id; |
| 2641 | list_add(new: &new_map->list, head: ®ulator_supply_alias_list); |
| 2642 | mutex_unlock(lock: ®ulator_list_mutex); |
| 2643 | pr_info("Adding alias for supply %s,%s -> %s,%s\n" , |
| 2644 | id, dev_name(dev), alias_id, dev_name(alias_dev)); |
| 2645 | |
| 2646 | return 0; |
| 2647 | } |
| 2648 | EXPORT_SYMBOL_GPL(regulator_register_supply_alias); |
| 2649 | |
| 2650 | /** |
| 2651 | * regulator_unregister_supply_alias - Remove device alias |
| 2652 | * |
| 2653 | * @dev: device that will be given as the regulator "consumer" |
| 2654 | * @id: Supply name or regulator ID |
| 2655 | * |
| 2656 | * Remove a lookup alias if one exists for id on dev. |
| 2657 | */ |
| 2658 | void regulator_unregister_supply_alias(struct device *dev, const char *id) |
| 2659 | { |
| 2660 | struct regulator_supply_alias *map; |
| 2661 | |
| 2662 | mutex_lock(®ulator_list_mutex); |
| 2663 | map = regulator_find_supply_alias(dev, supply: id); |
| 2664 | if (map) { |
| 2665 | list_del(entry: &map->list); |
| 2666 | kfree(objp: map); |
| 2667 | } |
| 2668 | mutex_unlock(lock: ®ulator_list_mutex); |
| 2669 | } |
| 2670 | EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias); |
| 2671 | |
| 2672 | /** |
| 2673 | * regulator_bulk_register_supply_alias - register multiple aliases |
| 2674 | * |
| 2675 | * @dev: device that will be given as the regulator "consumer" |
| 2676 | * @id: List of supply names or regulator IDs |
| 2677 | * @alias_dev: device that should be used to lookup the supply |
| 2678 | * @alias_id: List of supply names or regulator IDs that should be used to |
| 2679 | * lookup the supply |
| 2680 | * @num_id: Number of aliases to register |
| 2681 | * |
| 2682 | * This helper function allows drivers to register several supply |
| 2683 | * aliases in one operation. If any of the aliases cannot be |
| 2684 | * registered any aliases that were registered will be removed |
| 2685 | * before returning to the caller. |
| 2686 | * |
| 2687 | * Return: 0 on success or a negative error number on failure. |
| 2688 | */ |
| 2689 | int regulator_bulk_register_supply_alias(struct device *dev, |
| 2690 | const char *const *id, |
| 2691 | struct device *alias_dev, |
| 2692 | const char *const *alias_id, |
| 2693 | int num_id) |
| 2694 | { |
| 2695 | int i; |
| 2696 | int ret; |
| 2697 | |
| 2698 | for (i = 0; i < num_id; ++i) { |
| 2699 | ret = regulator_register_supply_alias(dev, id[i], alias_dev, |
| 2700 | alias_id[i]); |
| 2701 | if (ret < 0) |
| 2702 | goto err; |
| 2703 | } |
| 2704 | |
| 2705 | return 0; |
| 2706 | |
| 2707 | err: |
| 2708 | dev_err(dev, |
| 2709 | "Failed to create supply alias %s,%s -> %s,%s\n" , |
| 2710 | id[i], dev_name(dev), alias_id[i], dev_name(alias_dev)); |
| 2711 | |
| 2712 | while (--i >= 0) |
| 2713 | regulator_unregister_supply_alias(dev, id[i]); |
| 2714 | |
| 2715 | return ret; |
| 2716 | } |
| 2717 | EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias); |
| 2718 | |
| 2719 | /** |
| 2720 | * regulator_bulk_unregister_supply_alias - unregister multiple aliases |
| 2721 | * |
| 2722 | * @dev: device that will be given as the regulator "consumer" |
| 2723 | * @id: List of supply names or regulator IDs |
| 2724 | * @num_id: Number of aliases to unregister |
| 2725 | * |
| 2726 | * This helper function allows drivers to unregister several supply |
| 2727 | * aliases in one operation. |
| 2728 | */ |
| 2729 | void regulator_bulk_unregister_supply_alias(struct device *dev, |
| 2730 | const char *const *id, |
| 2731 | int num_id) |
| 2732 | { |
| 2733 | int i; |
| 2734 | |
| 2735 | for (i = 0; i < num_id; ++i) |
| 2736 | regulator_unregister_supply_alias(dev, id[i]); |
| 2737 | } |
| 2738 | EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias); |
| 2739 | |
| 2740 | |
| 2741 | /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */ |
| 2742 | static int regulator_ena_gpio_request(struct regulator_dev *rdev, |
| 2743 | const struct regulator_config *config) |
| 2744 | { |
| 2745 | struct regulator_enable_gpio *pin, *new_pin; |
| 2746 | struct gpio_desc *gpiod; |
| 2747 | |
| 2748 | gpiod = config->ena_gpiod; |
| 2749 | new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL); |
| 2750 | |
| 2751 | mutex_lock(®ulator_list_mutex); |
| 2752 | |
| 2753 | if (gpiod_is_shared(desc: gpiod)) |
| 2754 | /* |
| 2755 | * The sharing of this GPIO pin is managed internally by |
| 2756 | * GPIOLIB. We don't need to keep track of its enable count. |
| 2757 | */ |
| 2758 | goto skip_compare; |
| 2759 | |
| 2760 | list_for_each_entry(pin, ®ulator_ena_gpio_list, list) { |
| 2761 | if (gpiod_is_equal(desc: pin->gpiod, other: gpiod)) { |
| 2762 | rdev_dbg(rdev, "GPIO is already used\n" ); |
| 2763 | goto update_ena_gpio_to_rdev; |
| 2764 | } |
| 2765 | } |
| 2766 | |
| 2767 | if (new_pin == NULL) { |
| 2768 | mutex_unlock(lock: ®ulator_list_mutex); |
| 2769 | return -ENOMEM; |
| 2770 | } |
| 2771 | |
| 2772 | skip_compare: |
| 2773 | pin = new_pin; |
| 2774 | new_pin = NULL; |
| 2775 | |
| 2776 | pin->gpiod = gpiod; |
| 2777 | list_add(new: &pin->list, head: ®ulator_ena_gpio_list); |
| 2778 | |
| 2779 | update_ena_gpio_to_rdev: |
| 2780 | pin->request_count++; |
| 2781 | rdev->ena_pin = pin; |
| 2782 | |
| 2783 | mutex_unlock(lock: ®ulator_list_mutex); |
| 2784 | kfree(objp: new_pin); |
| 2785 | |
| 2786 | return 0; |
| 2787 | } |
| 2788 | |
| 2789 | static void regulator_ena_gpio_free(struct regulator_dev *rdev) |
| 2790 | { |
| 2791 | struct regulator_enable_gpio *pin, *n; |
| 2792 | |
| 2793 | if (!rdev->ena_pin) |
| 2794 | return; |
| 2795 | |
| 2796 | /* Free the GPIO only in case of no use */ |
| 2797 | list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) { |
| 2798 | if (pin != rdev->ena_pin) |
| 2799 | continue; |
| 2800 | |
| 2801 | if (--pin->request_count) |
| 2802 | break; |
| 2803 | |
| 2804 | gpiod_put(desc: pin->gpiod); |
| 2805 | list_del(entry: &pin->list); |
| 2806 | kfree(objp: pin); |
| 2807 | break; |
| 2808 | } |
| 2809 | |
| 2810 | rdev->ena_pin = NULL; |
| 2811 | } |
| 2812 | |
| 2813 | /** |
| 2814 | * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control |
| 2815 | * @rdev: regulator_dev structure |
| 2816 | * @enable: enable GPIO at initial use? |
| 2817 | * |
| 2818 | * GPIO is enabled in case of initial use. (enable_count is 0) |
| 2819 | * GPIO is disabled when it is not shared any more. (enable_count <= 1) |
| 2820 | * |
| 2821 | * Return: 0 on success or a negative error number on failure. |
| 2822 | */ |
| 2823 | static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable) |
| 2824 | { |
| 2825 | struct regulator_enable_gpio *pin = rdev->ena_pin; |
| 2826 | int ret; |
| 2827 | |
| 2828 | if (!pin) |
| 2829 | return -EINVAL; |
| 2830 | |
| 2831 | if (enable) { |
| 2832 | /* Enable GPIO at initial use */ |
| 2833 | if (pin->enable_count == 0) { |
| 2834 | ret = gpiod_set_value_cansleep(desc: pin->gpiod, value: 1); |
| 2835 | if (ret) |
| 2836 | return ret; |
| 2837 | } |
| 2838 | |
| 2839 | pin->enable_count++; |
| 2840 | } else { |
| 2841 | if (pin->enable_count > 1) { |
| 2842 | pin->enable_count--; |
| 2843 | return 0; |
| 2844 | } |
| 2845 | |
| 2846 | /* Disable GPIO if not used */ |
| 2847 | if (pin->enable_count <= 1) { |
| 2848 | ret = gpiod_set_value_cansleep(desc: pin->gpiod, value: 0); |
| 2849 | if (ret) |
| 2850 | return ret; |
| 2851 | |
| 2852 | pin->enable_count = 0; |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | return 0; |
| 2857 | } |
| 2858 | |
| 2859 | /** |
| 2860 | * _regulator_check_status_enabled - check if regulator status can be |
| 2861 | * interpreted as "regulator is enabled" |
| 2862 | * @rdev: the regulator device to check |
| 2863 | * |
| 2864 | * Return: |
| 2865 | * * 1 - if status shows regulator is in enabled state |
| 2866 | * * 0 - if not enabled state |
| 2867 | * * Error Value - as received from ops->get_status() |
| 2868 | */ |
| 2869 | static inline int _regulator_check_status_enabled(struct regulator_dev *rdev) |
| 2870 | { |
| 2871 | int ret = rdev->desc->ops->get_status(rdev); |
| 2872 | |
| 2873 | if (ret < 0) { |
| 2874 | rdev_info(rdev, "get_status returned error: %d\n" , ret); |
| 2875 | return ret; |
| 2876 | } |
| 2877 | |
| 2878 | switch (ret) { |
| 2879 | case REGULATOR_STATUS_OFF: |
| 2880 | case REGULATOR_STATUS_ERROR: |
| 2881 | case REGULATOR_STATUS_UNDEFINED: |
| 2882 | return 0; |
| 2883 | default: |
| 2884 | return 1; |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | static int _regulator_do_enable(struct regulator_dev *rdev) |
| 2889 | { |
| 2890 | int ret, delay; |
| 2891 | |
| 2892 | /* Query before enabling in case configuration dependent. */ |
| 2893 | ret = _regulator_get_enable_time(rdev); |
| 2894 | if (ret >= 0) { |
| 2895 | delay = ret; |
| 2896 | } else { |
| 2897 | rdev_warn(rdev, "enable_time() failed: %pe\n" , ERR_PTR(ret)); |
| 2898 | delay = 0; |
| 2899 | } |
| 2900 | |
| 2901 | trace_regulator_enable(name: rdev_get_name(rdev)); |
| 2902 | |
| 2903 | if (rdev->desc->off_on_delay) { |
| 2904 | /* if needed, keep a distance of off_on_delay from last time |
| 2905 | * this regulator was disabled. |
| 2906 | */ |
| 2907 | ktime_t end = ktime_add_us(kt: rdev->last_off, usec: rdev->desc->off_on_delay); |
| 2908 | s64 remaining = ktime_us_delta(later: end, earlier: ktime_get_boottime()); |
| 2909 | |
| 2910 | if (remaining > 0) |
| 2911 | fsleep(usecs: remaining); |
| 2912 | } |
| 2913 | |
| 2914 | if (rdev->ena_pin) { |
| 2915 | if (!rdev->ena_gpio_state) { |
| 2916 | ret = regulator_ena_gpio_ctrl(rdev, enable: true); |
| 2917 | if (ret < 0) |
| 2918 | return ret; |
| 2919 | rdev->ena_gpio_state = 1; |
| 2920 | } |
| 2921 | } else if (rdev->desc->ops->enable) { |
| 2922 | ret = rdev->desc->ops->enable(rdev); |
| 2923 | if (ret < 0) |
| 2924 | return ret; |
| 2925 | } else { |
| 2926 | return -EINVAL; |
| 2927 | } |
| 2928 | |
| 2929 | /* Allow the regulator to ramp; it would be useful to extend |
| 2930 | * this for bulk operations so that the regulators can ramp |
| 2931 | * together. |
| 2932 | */ |
| 2933 | trace_regulator_enable_delay(name: rdev_get_name(rdev)); |
| 2934 | |
| 2935 | /* If poll_enabled_time is set, poll upto the delay calculated |
| 2936 | * above, delaying poll_enabled_time uS to check if the regulator |
| 2937 | * actually got enabled. |
| 2938 | * If the regulator isn't enabled after our delay helper has expired, |
| 2939 | * return -ETIMEDOUT. |
| 2940 | */ |
| 2941 | if (rdev->desc->poll_enabled_time) { |
| 2942 | int time_remaining = delay; |
| 2943 | |
| 2944 | while (time_remaining > 0) { |
| 2945 | fsleep(usecs: rdev->desc->poll_enabled_time); |
| 2946 | |
| 2947 | if (rdev->desc->ops->get_status) { |
| 2948 | ret = _regulator_check_status_enabled(rdev); |
| 2949 | if (ret < 0) |
| 2950 | return ret; |
| 2951 | else if (ret) |
| 2952 | break; |
| 2953 | } else if (rdev->desc->ops->is_enabled(rdev)) |
| 2954 | break; |
| 2955 | |
| 2956 | time_remaining -= rdev->desc->poll_enabled_time; |
| 2957 | } |
| 2958 | |
| 2959 | if (time_remaining <= 0) { |
| 2960 | rdev_err(rdev, "Enabled check timed out\n" ); |
| 2961 | return -ETIMEDOUT; |
| 2962 | } |
| 2963 | } else { |
| 2964 | fsleep(usecs: delay); |
| 2965 | } |
| 2966 | |
| 2967 | trace_regulator_enable_complete(name: rdev_get_name(rdev)); |
| 2968 | |
| 2969 | return 0; |
| 2970 | } |
| 2971 | |
| 2972 | /** |
| 2973 | * _regulator_handle_consumer_enable - handle that a consumer enabled |
| 2974 | * @regulator: regulator source |
| 2975 | * |
| 2976 | * Some things on a regulator consumer (like the contribution towards total |
| 2977 | * load on the regulator) only have an effect when the consumer wants the |
| 2978 | * regulator enabled. Explained in example with two consumers of the same |
| 2979 | * regulator: |
| 2980 | * consumer A: set_load(100); => total load = 0 |
| 2981 | * consumer A: regulator_enable(); => total load = 100 |
| 2982 | * consumer B: set_load(1000); => total load = 100 |
| 2983 | * consumer B: regulator_enable(); => total load = 1100 |
| 2984 | * consumer A: regulator_disable(); => total_load = 1000 |
| 2985 | * |
| 2986 | * This function (together with _regulator_handle_consumer_disable) is |
| 2987 | * responsible for keeping track of the refcount for a given regulator consumer |
| 2988 | * and applying / unapplying these things. |
| 2989 | * |
| 2990 | * Return: 0 on success or negative error number on failure. |
| 2991 | */ |
| 2992 | static int _regulator_handle_consumer_enable(struct regulator *regulator) |
| 2993 | { |
| 2994 | int ret; |
| 2995 | struct regulator_dev *rdev = regulator->rdev; |
| 2996 | |
| 2997 | lockdep_assert_held_once(&rdev->mutex.base); |
| 2998 | |
| 2999 | regulator->enable_count++; |
| 3000 | if (regulator->uA_load && regulator->enable_count == 1) { |
| 3001 | ret = drms_uA_update(rdev); |
| 3002 | if (ret) |
| 3003 | regulator->enable_count--; |
| 3004 | return ret; |
| 3005 | } |
| 3006 | |
| 3007 | return 0; |
| 3008 | } |
| 3009 | |
| 3010 | /** |
| 3011 | * _regulator_handle_consumer_disable - handle that a consumer disabled |
| 3012 | * @regulator: regulator source |
| 3013 | * |
| 3014 | * The opposite of _regulator_handle_consumer_enable(). |
| 3015 | * |
| 3016 | * Return: 0 on success or a negative error number on failure. |
| 3017 | */ |
| 3018 | static int _regulator_handle_consumer_disable(struct regulator *regulator) |
| 3019 | { |
| 3020 | struct regulator_dev *rdev = regulator->rdev; |
| 3021 | |
| 3022 | lockdep_assert_held_once(&rdev->mutex.base); |
| 3023 | |
| 3024 | if (!regulator->enable_count) { |
| 3025 | rdev_err(rdev, "Underflow of regulator enable count\n" ); |
| 3026 | return -EINVAL; |
| 3027 | } |
| 3028 | |
| 3029 | regulator->enable_count--; |
| 3030 | if (regulator->uA_load && regulator->enable_count == 0) |
| 3031 | return drms_uA_update(rdev); |
| 3032 | |
| 3033 | return 0; |
| 3034 | } |
| 3035 | |
| 3036 | /* locks held by regulator_enable() */ |
| 3037 | static int _regulator_enable(struct regulator *regulator) |
| 3038 | { |
| 3039 | struct regulator_dev *rdev = regulator->rdev; |
| 3040 | int ret; |
| 3041 | |
| 3042 | lockdep_assert_held_once(&rdev->mutex.base); |
| 3043 | |
| 3044 | if (rdev->use_count == 0 && rdev->supply) { |
| 3045 | ret = _regulator_enable(regulator: rdev->supply); |
| 3046 | if (ret < 0) |
| 3047 | return ret; |
| 3048 | } |
| 3049 | |
| 3050 | /* balance only if there are regulators coupled */ |
| 3051 | if (rdev->coupling_desc.n_coupled > 1) { |
| 3052 | ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
| 3053 | if (ret < 0) |
| 3054 | goto err_disable_supply; |
| 3055 | } |
| 3056 | |
| 3057 | ret = _regulator_handle_consumer_enable(regulator); |
| 3058 | if (ret < 0) |
| 3059 | goto err_disable_supply; |
| 3060 | |
| 3061 | if (rdev->use_count == 0) { |
| 3062 | /* |
| 3063 | * The regulator may already be enabled if it's not switchable |
| 3064 | * or was left on |
| 3065 | */ |
| 3066 | ret = _regulator_is_enabled(rdev); |
| 3067 | if (ret == -EINVAL || ret == 0) { |
| 3068 | if (!regulator_ops_is_valid(rdev, |
| 3069 | REGULATOR_CHANGE_STATUS)) { |
| 3070 | ret = -EPERM; |
| 3071 | goto err_consumer_disable; |
| 3072 | } |
| 3073 | |
| 3074 | ret = _regulator_do_enable(rdev); |
| 3075 | if (ret < 0) |
| 3076 | goto err_consumer_disable; |
| 3077 | |
| 3078 | _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE, |
| 3079 | NULL); |
| 3080 | } else if (ret < 0) { |
| 3081 | rdev_err(rdev, "is_enabled() failed: %pe\n" , ERR_PTR(ret)); |
| 3082 | goto err_consumer_disable; |
| 3083 | } |
| 3084 | /* Fallthrough on positive return values - already enabled */ |
| 3085 | } |
| 3086 | |
| 3087 | if (regulator->enable_count == 1) |
| 3088 | rdev->use_count++; |
| 3089 | |
| 3090 | return 0; |
| 3091 | |
| 3092 | err_consumer_disable: |
| 3093 | _regulator_handle_consumer_disable(regulator); |
| 3094 | |
| 3095 | err_disable_supply: |
| 3096 | if (rdev->use_count == 0 && rdev->supply) |
| 3097 | _regulator_disable(regulator: rdev->supply); |
| 3098 | |
| 3099 | return ret; |
| 3100 | } |
| 3101 | |
| 3102 | /** |
| 3103 | * regulator_enable - enable regulator output |
| 3104 | * @regulator: regulator source |
| 3105 | * |
| 3106 | * Request that the regulator be enabled with the regulator output at |
| 3107 | * the predefined voltage or current value. Calls to regulator_enable() |
| 3108 | * must be balanced with calls to regulator_disable(). |
| 3109 | * |
| 3110 | * NOTE: the output value can be set by other drivers, boot loader or may be |
| 3111 | * hardwired in the regulator. |
| 3112 | * |
| 3113 | * Return: 0 on success or a negative error number on failure. |
| 3114 | */ |
| 3115 | int regulator_enable(struct regulator *regulator) |
| 3116 | { |
| 3117 | struct regulator_dev *rdev = regulator->rdev; |
| 3118 | struct ww_acquire_ctx ww_ctx; |
| 3119 | int ret; |
| 3120 | |
| 3121 | regulator_lock_dependent(rdev, ww_ctx: &ww_ctx); |
| 3122 | ret = _regulator_enable(regulator); |
| 3123 | regulator_unlock_dependent(rdev, ww_ctx: &ww_ctx); |
| 3124 | |
| 3125 | return ret; |
| 3126 | } |
| 3127 | EXPORT_SYMBOL_GPL(regulator_enable); |
| 3128 | |
| 3129 | static int _regulator_do_disable(struct regulator_dev *rdev) |
| 3130 | { |
| 3131 | int ret; |
| 3132 | |
| 3133 | trace_regulator_disable(name: rdev_get_name(rdev)); |
| 3134 | |
| 3135 | if (rdev->ena_pin) { |
| 3136 | if (rdev->ena_gpio_state) { |
| 3137 | ret = regulator_ena_gpio_ctrl(rdev, enable: false); |
| 3138 | if (ret < 0) |
| 3139 | return ret; |
| 3140 | rdev->ena_gpio_state = 0; |
| 3141 | } |
| 3142 | |
| 3143 | } else if (rdev->desc->ops->disable) { |
| 3144 | ret = rdev->desc->ops->disable(rdev); |
| 3145 | if (ret != 0) |
| 3146 | return ret; |
| 3147 | } |
| 3148 | |
| 3149 | if (rdev->desc->off_on_delay) |
| 3150 | rdev->last_off = ktime_get_boottime(); |
| 3151 | |
| 3152 | trace_regulator_disable_complete(name: rdev_get_name(rdev)); |
| 3153 | |
| 3154 | return 0; |
| 3155 | } |
| 3156 | |
| 3157 | /* locks held by regulator_disable() */ |
| 3158 | static int _regulator_disable(struct regulator *regulator) |
| 3159 | { |
| 3160 | struct regulator_dev *rdev = regulator->rdev; |
| 3161 | int ret = 0; |
| 3162 | |
| 3163 | lockdep_assert_held_once(&rdev->mutex.base); |
| 3164 | |
| 3165 | if (WARN(regulator->enable_count == 0, |
| 3166 | "unbalanced disables for %s\n" , rdev_get_name(rdev))) |
| 3167 | return -EIO; |
| 3168 | |
| 3169 | if (regulator->enable_count == 1) { |
| 3170 | /* disabling last enable_count from this regulator */ |
| 3171 | /* are we the last user and permitted to disable ? */ |
| 3172 | if (rdev->use_count == 1 && |
| 3173 | (rdev->constraints && !rdev->constraints->always_on)) { |
| 3174 | |
| 3175 | /* we are last user */ |
| 3176 | if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) { |
| 3177 | ret = _notifier_call_chain(rdev, |
| 3178 | REGULATOR_EVENT_PRE_DISABLE, |
| 3179 | NULL); |
| 3180 | if (ret & NOTIFY_STOP_MASK) |
| 3181 | return -EINVAL; |
| 3182 | |
| 3183 | ret = _regulator_do_disable(rdev); |
| 3184 | if (ret < 0) { |
| 3185 | rdev_err(rdev, "failed to disable: %pe\n" , ERR_PTR(ret)); |
| 3186 | _notifier_call_chain(rdev, |
| 3187 | REGULATOR_EVENT_ABORT_DISABLE, |
| 3188 | NULL); |
| 3189 | return ret; |
| 3190 | } |
| 3191 | _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, |
| 3192 | NULL); |
| 3193 | } |
| 3194 | |
| 3195 | rdev->use_count = 0; |
| 3196 | } else if (rdev->use_count > 1) { |
| 3197 | rdev->use_count--; |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | if (ret == 0) |
| 3202 | ret = _regulator_handle_consumer_disable(regulator); |
| 3203 | |
| 3204 | if (ret == 0 && rdev->coupling_desc.n_coupled > 1) |
| 3205 | ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
| 3206 | |
| 3207 | if (ret == 0 && rdev->use_count == 0 && rdev->supply) |
| 3208 | ret = _regulator_disable(regulator: rdev->supply); |
| 3209 | |
| 3210 | return ret; |
| 3211 | } |
| 3212 | |
| 3213 | /** |
| 3214 | * regulator_disable - disable regulator output |
| 3215 | * @regulator: regulator source |
| 3216 | * |
| 3217 | * Disable the regulator output voltage or current. Calls to |
| 3218 | * regulator_enable() must be balanced with calls to |
| 3219 | * regulator_disable(). |
| 3220 | * |
| 3221 | * NOTE: this will only disable the regulator output if no other consumer |
| 3222 | * devices have it enabled, the regulator device supports disabling and |
| 3223 | * machine constraints permit this operation. |
| 3224 | * |
| 3225 | * Return: 0 on success or a negative error number on failure. |
| 3226 | */ |
| 3227 | int regulator_disable(struct regulator *regulator) |
| 3228 | { |
| 3229 | struct regulator_dev *rdev = regulator->rdev; |
| 3230 | struct ww_acquire_ctx ww_ctx; |
| 3231 | int ret; |
| 3232 | |
| 3233 | regulator_lock_dependent(rdev, ww_ctx: &ww_ctx); |
| 3234 | ret = _regulator_disable(regulator); |
| 3235 | regulator_unlock_dependent(rdev, ww_ctx: &ww_ctx); |
| 3236 | |
| 3237 | return ret; |
| 3238 | } |
| 3239 | EXPORT_SYMBOL_GPL(regulator_disable); |
| 3240 | |
| 3241 | /* locks held by regulator_force_disable() */ |
| 3242 | static int _regulator_force_disable(struct regulator_dev *rdev) |
| 3243 | { |
| 3244 | int ret = 0; |
| 3245 | |
| 3246 | lockdep_assert_held_once(&rdev->mutex.base); |
| 3247 | |
| 3248 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
| 3249 | REGULATOR_EVENT_PRE_DISABLE, NULL); |
| 3250 | if (ret & NOTIFY_STOP_MASK) |
| 3251 | return -EINVAL; |
| 3252 | |
| 3253 | ret = _regulator_do_disable(rdev); |
| 3254 | if (ret < 0) { |
| 3255 | rdev_err(rdev, "failed to force disable: %pe\n" , ERR_PTR(ret)); |
| 3256 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
| 3257 | REGULATOR_EVENT_ABORT_DISABLE, NULL); |
| 3258 | return ret; |
| 3259 | } |
| 3260 | |
| 3261 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
| 3262 | REGULATOR_EVENT_DISABLE, NULL); |
| 3263 | |
| 3264 | return 0; |
| 3265 | } |
| 3266 | |
| 3267 | /** |
| 3268 | * regulator_force_disable - force disable regulator output |
| 3269 | * @regulator: regulator source |
| 3270 | * |
| 3271 | * Forcibly disable the regulator output voltage or current. |
| 3272 | * NOTE: this *will* disable the regulator output even if other consumer |
| 3273 | * devices have it enabled. This should be used for situations when device |
| 3274 | * damage will likely occur if the regulator is not disabled (e.g. over temp). |
| 3275 | * |
| 3276 | * Return: 0 on success or a negative error number on failure. |
| 3277 | */ |
| 3278 | int regulator_force_disable(struct regulator *regulator) |
| 3279 | { |
| 3280 | struct regulator_dev *rdev = regulator->rdev; |
| 3281 | struct ww_acquire_ctx ww_ctx; |
| 3282 | int ret; |
| 3283 | |
| 3284 | regulator_lock_dependent(rdev, ww_ctx: &ww_ctx); |
| 3285 | |
| 3286 | ret = _regulator_force_disable(rdev: regulator->rdev); |
| 3287 | |
| 3288 | if (rdev->coupling_desc.n_coupled > 1) |
| 3289 | regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
| 3290 | |
| 3291 | if (regulator->uA_load) { |
| 3292 | regulator->uA_load = 0; |
| 3293 | ret = drms_uA_update(rdev); |
| 3294 | } |
| 3295 | |
| 3296 | if (rdev->use_count != 0 && rdev->supply) |
| 3297 | _regulator_disable(regulator: rdev->supply); |
| 3298 | |
| 3299 | regulator_unlock_dependent(rdev, ww_ctx: &ww_ctx); |
| 3300 | |
| 3301 | return ret; |
| 3302 | } |
| 3303 | EXPORT_SYMBOL_GPL(regulator_force_disable); |
| 3304 | |
| 3305 | static void regulator_disable_work(struct work_struct *work) |
| 3306 | { |
| 3307 | struct regulator_dev *rdev = container_of(work, struct regulator_dev, |
| 3308 | disable_work.work); |
| 3309 | struct ww_acquire_ctx ww_ctx; |
| 3310 | int count, i, ret; |
| 3311 | struct regulator *regulator; |
| 3312 | int total_count = 0; |
| 3313 | |
| 3314 | regulator_lock_dependent(rdev, ww_ctx: &ww_ctx); |
| 3315 | |
| 3316 | /* |
| 3317 | * Workqueue functions queue the new work instance while the previous |
| 3318 | * work instance is being processed. Cancel the queued work instance |
| 3319 | * as the work instance under processing does the job of the queued |
| 3320 | * work instance. |
| 3321 | */ |
| 3322 | cancel_delayed_work(dwork: &rdev->disable_work); |
| 3323 | |
| 3324 | list_for_each_entry(regulator, &rdev->consumer_list, list) { |
| 3325 | count = regulator->deferred_disables; |
| 3326 | |
| 3327 | if (!count) |
| 3328 | continue; |
| 3329 | |
| 3330 | total_count += count; |
| 3331 | regulator->deferred_disables = 0; |
| 3332 | |
| 3333 | for (i = 0; i < count; i++) { |
| 3334 | ret = _regulator_disable(regulator); |
| 3335 | if (ret != 0) |
| 3336 | rdev_err(rdev, "Deferred disable failed: %pe\n" , |
| 3337 | ERR_PTR(ret)); |
| 3338 | } |
| 3339 | } |
| 3340 | WARN_ON(!total_count); |
| 3341 | |
| 3342 | if (rdev->coupling_desc.n_coupled > 1) |
| 3343 | regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
| 3344 | |
| 3345 | regulator_unlock_dependent(rdev, ww_ctx: &ww_ctx); |
| 3346 | } |
| 3347 | |
| 3348 | /** |
| 3349 | * regulator_disable_deferred - disable regulator output with delay |
| 3350 | * @regulator: regulator source |
| 3351 | * @ms: milliseconds until the regulator is disabled |
| 3352 | * |
| 3353 | * Execute regulator_disable() on the regulator after a delay. This |
| 3354 | * is intended for use with devices that require some time to quiesce. |
| 3355 | * |
| 3356 | * NOTE: this will only disable the regulator output if no other consumer |
| 3357 | * devices have it enabled, the regulator device supports disabling and |
| 3358 | * machine constraints permit this operation. |
| 3359 | * |
| 3360 | * Return: 0 on success or a negative error number on failure. |
| 3361 | */ |
| 3362 | int regulator_disable_deferred(struct regulator *regulator, int ms) |
| 3363 | { |
| 3364 | struct regulator_dev *rdev = regulator->rdev; |
| 3365 | |
| 3366 | if (!ms) |
| 3367 | return regulator_disable(regulator); |
| 3368 | |
| 3369 | regulator_lock(rdev); |
| 3370 | regulator->deferred_disables++; |
| 3371 | mod_delayed_work(wq: system_power_efficient_wq, dwork: &rdev->disable_work, |
| 3372 | delay: msecs_to_jiffies(m: ms)); |
| 3373 | regulator_unlock(rdev); |
| 3374 | |
| 3375 | return 0; |
| 3376 | } |
| 3377 | EXPORT_SYMBOL_GPL(regulator_disable_deferred); |
| 3378 | |
| 3379 | static int _regulator_is_enabled(struct regulator_dev *rdev) |
| 3380 | { |
| 3381 | /* A GPIO control always takes precedence */ |
| 3382 | if (rdev->ena_pin) |
| 3383 | return rdev->ena_gpio_state; |
| 3384 | |
| 3385 | /* If we don't know then assume that the regulator is always on */ |
| 3386 | if (!rdev->desc->ops->is_enabled) |
| 3387 | return 1; |
| 3388 | |
| 3389 | return rdev->desc->ops->is_enabled(rdev); |
| 3390 | } |
| 3391 | |
| 3392 | static int _regulator_list_voltage(struct regulator_dev *rdev, |
| 3393 | unsigned selector, int lock) |
| 3394 | { |
| 3395 | const struct regulator_ops *ops = rdev->desc->ops; |
| 3396 | int ret; |
| 3397 | |
| 3398 | if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector) |
| 3399 | return rdev->desc->fixed_uV; |
| 3400 | |
| 3401 | if (ops->list_voltage) { |
| 3402 | if (selector >= rdev->desc->n_voltages) |
| 3403 | return -EINVAL; |
| 3404 | if (selector < rdev->desc->linear_min_sel) |
| 3405 | return 0; |
| 3406 | if (lock) |
| 3407 | regulator_lock(rdev); |
| 3408 | ret = ops->list_voltage(rdev, selector); |
| 3409 | if (lock) |
| 3410 | regulator_unlock(rdev); |
| 3411 | } else if (rdev->is_switch && rdev->supply) { |
| 3412 | ret = _regulator_list_voltage(rdev: rdev->supply->rdev, |
| 3413 | selector, lock); |
| 3414 | } else { |
| 3415 | return -EINVAL; |
| 3416 | } |
| 3417 | |
| 3418 | if (ret > 0) { |
| 3419 | if (ret < rdev->constraints->min_uV) |
| 3420 | ret = 0; |
| 3421 | else if (ret > rdev->constraints->max_uV) |
| 3422 | ret = 0; |
| 3423 | } |
| 3424 | |
| 3425 | return ret; |
| 3426 | } |
| 3427 | |
| 3428 | /** |
| 3429 | * regulator_is_enabled - is the regulator output enabled |
| 3430 | * @regulator: regulator source |
| 3431 | * |
| 3432 | * Note that the device backing this regulator handle can have multiple |
| 3433 | * users, so it might be enabled even if regulator_enable() was never |
| 3434 | * called for this particular source. |
| 3435 | * |
| 3436 | * Return: Positive if the regulator driver backing the source/client |
| 3437 | * has requested that the device be enabled, zero if it hasn't, |
| 3438 | * else a negative error number. |
| 3439 | */ |
| 3440 | int regulator_is_enabled(struct regulator *regulator) |
| 3441 | { |
| 3442 | int ret; |
| 3443 | |
| 3444 | if (regulator->always_on) |
| 3445 | return 1; |
| 3446 | |
| 3447 | regulator_lock(rdev: regulator->rdev); |
| 3448 | ret = _regulator_is_enabled(rdev: regulator->rdev); |
| 3449 | regulator_unlock(rdev: regulator->rdev); |
| 3450 | |
| 3451 | return ret; |
| 3452 | } |
| 3453 | EXPORT_SYMBOL_GPL(regulator_is_enabled); |
| 3454 | |
| 3455 | /** |
| 3456 | * regulator_count_voltages - count regulator_list_voltage() selectors |
| 3457 | * @regulator: regulator source |
| 3458 | * |
| 3459 | * Return: Number of selectors for @regulator, or negative error number. |
| 3460 | * |
| 3461 | * Selectors are numbered starting at zero, and typically correspond to |
| 3462 | * bitfields in hardware registers. |
| 3463 | */ |
| 3464 | int regulator_count_voltages(struct regulator *regulator) |
| 3465 | { |
| 3466 | struct regulator_dev *rdev = regulator->rdev; |
| 3467 | |
| 3468 | if (rdev->desc->n_voltages) |
| 3469 | return rdev->desc->n_voltages; |
| 3470 | |
| 3471 | if (!rdev->is_switch || !rdev->supply) |
| 3472 | return -EINVAL; |
| 3473 | |
| 3474 | return regulator_count_voltages(regulator: rdev->supply); |
| 3475 | } |
| 3476 | EXPORT_SYMBOL_GPL(regulator_count_voltages); |
| 3477 | |
| 3478 | /** |
| 3479 | * regulator_list_voltage - enumerate supported voltages |
| 3480 | * @regulator: regulator source |
| 3481 | * @selector: identify voltage to list |
| 3482 | * Context: can sleep |
| 3483 | * |
| 3484 | * Return: Voltage for @selector that can be passed to regulator_set_voltage(), |
| 3485 | * 0 if @selector can't be used on this system, or a negative error |
| 3486 | * number on failure. |
| 3487 | */ |
| 3488 | int regulator_list_voltage(struct regulator *regulator, unsigned selector) |
| 3489 | { |
| 3490 | return _regulator_list_voltage(rdev: regulator->rdev, selector, lock: 1); |
| 3491 | } |
| 3492 | EXPORT_SYMBOL_GPL(regulator_list_voltage); |
| 3493 | |
| 3494 | /** |
| 3495 | * regulator_get_regmap - get the regulator's register map |
| 3496 | * @regulator: regulator source |
| 3497 | * |
| 3498 | * Return: Pointer to the &struct regmap for @regulator, or ERR_PTR() |
| 3499 | * encoded -%EOPNOTSUPP if @regulator doesn't use regmap. |
| 3500 | */ |
| 3501 | struct regmap *regulator_get_regmap(struct regulator *regulator) |
| 3502 | { |
| 3503 | struct regmap *map = regulator->rdev->regmap; |
| 3504 | |
| 3505 | return map ? map : ERR_PTR(error: -EOPNOTSUPP); |
| 3506 | } |
| 3507 | EXPORT_SYMBOL_GPL(regulator_get_regmap); |
| 3508 | |
| 3509 | /** |
| 3510 | * regulator_get_hardware_vsel_register - get the HW voltage selector register |
| 3511 | * @regulator: regulator source |
| 3512 | * @vsel_reg: voltage selector register, output parameter |
| 3513 | * @vsel_mask: mask for voltage selector bitfield, output parameter |
| 3514 | * |
| 3515 | * Returns the hardware register offset and bitmask used for setting the |
| 3516 | * regulator voltage. This might be useful when configuring voltage-scaling |
| 3517 | * hardware or firmware that can make I2C requests behind the kernel's back, |
| 3518 | * for example. |
| 3519 | * |
| 3520 | * Return: 0 on success, or -%EOPNOTSUPP if the regulator does not support |
| 3521 | * voltage selectors. |
| 3522 | * |
| 3523 | * On success, the output parameters @vsel_reg and @vsel_mask are filled in |
| 3524 | * and 0 is returned, otherwise a negative error number is returned. |
| 3525 | */ |
| 3526 | int regulator_get_hardware_vsel_register(struct regulator *regulator, |
| 3527 | unsigned *vsel_reg, |
| 3528 | unsigned *vsel_mask) |
| 3529 | { |
| 3530 | struct regulator_dev *rdev = regulator->rdev; |
| 3531 | const struct regulator_ops *ops = rdev->desc->ops; |
| 3532 | |
| 3533 | if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) |
| 3534 | return -EOPNOTSUPP; |
| 3535 | |
| 3536 | *vsel_reg = rdev->desc->vsel_reg; |
| 3537 | *vsel_mask = rdev->desc->vsel_mask; |
| 3538 | |
| 3539 | return 0; |
| 3540 | } |
| 3541 | EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register); |
| 3542 | |
| 3543 | /** |
| 3544 | * regulator_list_hardware_vsel - get the HW-specific register value for a selector |
| 3545 | * @regulator: regulator source |
| 3546 | * @selector: identify voltage to list |
| 3547 | * |
| 3548 | * Converts the selector to a hardware-specific voltage selector that can be |
| 3549 | * directly written to the regulator registers. The address of the voltage |
| 3550 | * register can be determined by calling @regulator_get_hardware_vsel_register. |
| 3551 | * |
| 3552 | * Return: 0 on success, -%EINVAL if the selector is outside the supported |
| 3553 | * range, or -%EOPNOTSUPP if the regulator does not support voltage |
| 3554 | * selectors. |
| 3555 | */ |
| 3556 | int regulator_list_hardware_vsel(struct regulator *regulator, |
| 3557 | unsigned selector) |
| 3558 | { |
| 3559 | struct regulator_dev *rdev = regulator->rdev; |
| 3560 | const struct regulator_ops *ops = rdev->desc->ops; |
| 3561 | |
| 3562 | if (selector >= rdev->desc->n_voltages) |
| 3563 | return -EINVAL; |
| 3564 | if (selector < rdev->desc->linear_min_sel) |
| 3565 | return 0; |
| 3566 | if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) |
| 3567 | return -EOPNOTSUPP; |
| 3568 | |
| 3569 | return selector; |
| 3570 | } |
| 3571 | EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel); |
| 3572 | |
| 3573 | /** |
| 3574 | * regulator_hardware_enable - access the HW for enable/disable regulator |
| 3575 | * @regulator: regulator source |
| 3576 | * @enable: true for enable, false for disable |
| 3577 | * |
| 3578 | * Request that the regulator be enabled/disabled with the regulator output at |
| 3579 | * the predefined voltage or current value. |
| 3580 | * |
| 3581 | * Return: 0 on success or a negative error number on failure. |
| 3582 | */ |
| 3583 | int regulator_hardware_enable(struct regulator *regulator, bool enable) |
| 3584 | { |
| 3585 | struct regulator_dev *rdev = regulator->rdev; |
| 3586 | const struct regulator_ops *ops = rdev->desc->ops; |
| 3587 | int ret = -EOPNOTSUPP; |
| 3588 | |
| 3589 | if (!rdev->exclusive || !ops || !ops->enable || !ops->disable) |
| 3590 | return ret; |
| 3591 | |
| 3592 | if (enable) |
| 3593 | ret = ops->enable(rdev); |
| 3594 | else |
| 3595 | ret = ops->disable(rdev); |
| 3596 | |
| 3597 | return ret; |
| 3598 | } |
| 3599 | EXPORT_SYMBOL_GPL(regulator_hardware_enable); |
| 3600 | |
| 3601 | /** |
| 3602 | * regulator_get_linear_step - return the voltage step size between VSEL values |
| 3603 | * @regulator: regulator source |
| 3604 | * |
| 3605 | * Return: The voltage step size between VSEL values for linear regulators, |
| 3606 | * or 0 if the regulator isn't a linear regulator. |
| 3607 | */ |
| 3608 | unsigned int regulator_get_linear_step(struct regulator *regulator) |
| 3609 | { |
| 3610 | struct regulator_dev *rdev = regulator->rdev; |
| 3611 | |
| 3612 | return rdev->desc->uV_step; |
| 3613 | } |
| 3614 | EXPORT_SYMBOL_GPL(regulator_get_linear_step); |
| 3615 | |
| 3616 | /** |
| 3617 | * regulator_is_supported_voltage - check if a voltage range can be supported |
| 3618 | * |
| 3619 | * @regulator: Regulator to check. |
| 3620 | * @min_uV: Minimum required voltage in uV. |
| 3621 | * @max_uV: Maximum required voltage in uV. |
| 3622 | * |
| 3623 | * Return: 1 if the voltage range is supported, 0 if not, or a negative error |
| 3624 | * number if @regulator's voltage can't be changed and voltage readback |
| 3625 | * failed. |
| 3626 | */ |
| 3627 | int regulator_is_supported_voltage(struct regulator *regulator, |
| 3628 | int min_uV, int max_uV) |
| 3629 | { |
| 3630 | struct regulator_dev *rdev = regulator->rdev; |
| 3631 | int i, voltages, ret; |
| 3632 | |
| 3633 | /* If we can't change voltage check the current voltage */ |
| 3634 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
| 3635 | ret = regulator_get_voltage(regulator); |
| 3636 | if (ret >= 0) |
| 3637 | return min_uV <= ret && ret <= max_uV; |
| 3638 | else |
| 3639 | return ret; |
| 3640 | } |
| 3641 | |
| 3642 | /* Any voltage within constrains range is fine? */ |
| 3643 | if (rdev->desc->continuous_voltage_range) |
| 3644 | return min_uV >= rdev->constraints->min_uV && |
| 3645 | max_uV <= rdev->constraints->max_uV; |
| 3646 | |
| 3647 | ret = regulator_count_voltages(regulator); |
| 3648 | if (ret < 0) |
| 3649 | return 0; |
| 3650 | voltages = ret; |
| 3651 | |
| 3652 | for (i = 0; i < voltages; i++) { |
| 3653 | ret = regulator_list_voltage(regulator, i); |
| 3654 | |
| 3655 | if (ret >= min_uV && ret <= max_uV) |
| 3656 | return 1; |
| 3657 | } |
| 3658 | |
| 3659 | return 0; |
| 3660 | } |
| 3661 | EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); |
| 3662 | |
| 3663 | static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV, |
| 3664 | int max_uV) |
| 3665 | { |
| 3666 | const struct regulator_desc *desc = rdev->desc; |
| 3667 | |
| 3668 | if (desc->ops->map_voltage) |
| 3669 | return desc->ops->map_voltage(rdev, min_uV, max_uV); |
| 3670 | |
| 3671 | if (desc->ops->list_voltage == regulator_list_voltage_linear) |
| 3672 | return regulator_map_voltage_linear(rdev, min_uV, max_uV); |
| 3673 | |
| 3674 | if (desc->ops->list_voltage == regulator_list_voltage_linear_range) |
| 3675 | return regulator_map_voltage_linear_range(rdev, min_uV, max_uV); |
| 3676 | |
| 3677 | if (desc->ops->list_voltage == |
| 3678 | regulator_list_voltage_pickable_linear_range) |
| 3679 | return regulator_map_voltage_pickable_linear_range(rdev, |
| 3680 | min_uV, max_uV); |
| 3681 | |
| 3682 | return regulator_map_voltage_iterate(rdev, min_uV, max_uV); |
| 3683 | } |
| 3684 | |
| 3685 | static int _regulator_call_set_voltage(struct regulator_dev *rdev, |
| 3686 | int min_uV, int max_uV, |
| 3687 | unsigned *selector) |
| 3688 | { |
| 3689 | struct pre_voltage_change_data data; |
| 3690 | int ret; |
| 3691 | |
| 3692 | data.old_uV = regulator_get_voltage_rdev(rdev); |
| 3693 | data.min_uV = min_uV; |
| 3694 | data.max_uV = max_uV; |
| 3695 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, |
| 3696 | data: &data); |
| 3697 | if (ret & NOTIFY_STOP_MASK) |
| 3698 | return -EINVAL; |
| 3699 | |
| 3700 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector); |
| 3701 | if (ret >= 0) |
| 3702 | return ret; |
| 3703 | |
| 3704 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, |
| 3705 | data: (void *)data.old_uV); |
| 3706 | |
| 3707 | return ret; |
| 3708 | } |
| 3709 | |
| 3710 | static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev, |
| 3711 | int uV, unsigned selector) |
| 3712 | { |
| 3713 | struct pre_voltage_change_data data; |
| 3714 | int ret; |
| 3715 | |
| 3716 | data.old_uV = regulator_get_voltage_rdev(rdev); |
| 3717 | data.min_uV = uV; |
| 3718 | data.max_uV = uV; |
| 3719 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, |
| 3720 | data: &data); |
| 3721 | if (ret & NOTIFY_STOP_MASK) |
| 3722 | return -EINVAL; |
| 3723 | |
| 3724 | ret = rdev->desc->ops->set_voltage_sel(rdev, selector); |
| 3725 | if (ret >= 0) |
| 3726 | return ret; |
| 3727 | |
| 3728 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, |
| 3729 | data: (void *)data.old_uV); |
| 3730 | |
| 3731 | return ret; |
| 3732 | } |
| 3733 | |
| 3734 | static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev, |
| 3735 | int uV, int new_selector) |
| 3736 | { |
| 3737 | const struct regulator_ops *ops = rdev->desc->ops; |
| 3738 | int diff, old_sel, curr_sel, ret; |
| 3739 | |
| 3740 | /* Stepping is only needed if the regulator is enabled. */ |
| 3741 | if (!_regulator_is_enabled(rdev)) |
| 3742 | goto final_set; |
| 3743 | |
| 3744 | if (!ops->get_voltage_sel) |
| 3745 | return -EINVAL; |
| 3746 | |
| 3747 | old_sel = ops->get_voltage_sel(rdev); |
| 3748 | if (old_sel < 0) |
| 3749 | return old_sel; |
| 3750 | |
| 3751 | diff = new_selector - old_sel; |
| 3752 | if (diff == 0) |
| 3753 | return 0; /* No change needed. */ |
| 3754 | |
| 3755 | if (diff > 0) { |
| 3756 | /* Stepping up. */ |
| 3757 | for (curr_sel = old_sel + rdev->desc->vsel_step; |
| 3758 | curr_sel < new_selector; |
| 3759 | curr_sel += rdev->desc->vsel_step) { |
| 3760 | /* |
| 3761 | * Call the callback directly instead of using |
| 3762 | * _regulator_call_set_voltage_sel() as we don't |
| 3763 | * want to notify anyone yet. Same in the branch |
| 3764 | * below. |
| 3765 | */ |
| 3766 | ret = ops->set_voltage_sel(rdev, curr_sel); |
| 3767 | if (ret) |
| 3768 | goto try_revert; |
| 3769 | } |
| 3770 | } else { |
| 3771 | /* Stepping down. */ |
| 3772 | for (curr_sel = old_sel - rdev->desc->vsel_step; |
| 3773 | curr_sel > new_selector; |
| 3774 | curr_sel -= rdev->desc->vsel_step) { |
| 3775 | ret = ops->set_voltage_sel(rdev, curr_sel); |
| 3776 | if (ret) |
| 3777 | goto try_revert; |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | final_set: |
| 3782 | /* The final selector will trigger the notifiers. */ |
| 3783 | return _regulator_call_set_voltage_sel(rdev, uV, selector: new_selector); |
| 3784 | |
| 3785 | try_revert: |
| 3786 | /* |
| 3787 | * At least try to return to the previous voltage if setting a new |
| 3788 | * one failed. |
| 3789 | */ |
| 3790 | (void)ops->set_voltage_sel(rdev, old_sel); |
| 3791 | return ret; |
| 3792 | } |
| 3793 | |
| 3794 | static int _regulator_set_voltage_time(struct regulator_dev *rdev, |
| 3795 | int old_uV, int new_uV) |
| 3796 | { |
| 3797 | unsigned int ramp_delay = 0; |
| 3798 | |
| 3799 | if (rdev->constraints->ramp_delay) |
| 3800 | ramp_delay = rdev->constraints->ramp_delay; |
| 3801 | else if (rdev->desc->ramp_delay) |
| 3802 | ramp_delay = rdev->desc->ramp_delay; |
| 3803 | else if (rdev->constraints->settling_time) |
| 3804 | return rdev->constraints->settling_time; |
| 3805 | else if (rdev->constraints->settling_time_up && |
| 3806 | (new_uV > old_uV)) |
| 3807 | return rdev->constraints->settling_time_up; |
| 3808 | else if (rdev->constraints->settling_time_down && |
| 3809 | (new_uV < old_uV)) |
| 3810 | return rdev->constraints->settling_time_down; |
| 3811 | |
| 3812 | if (ramp_delay == 0) |
| 3813 | return 0; |
| 3814 | |
| 3815 | return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay); |
| 3816 | } |
| 3817 | |
| 3818 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
| 3819 | int min_uV, int max_uV) |
| 3820 | { |
| 3821 | int ret; |
| 3822 | int delay = 0; |
| 3823 | int best_val = 0; |
| 3824 | unsigned int selector; |
| 3825 | int old_selector = -1; |
| 3826 | const struct regulator_ops *ops = rdev->desc->ops; |
| 3827 | int old_uV = regulator_get_voltage_rdev(rdev); |
| 3828 | |
| 3829 | trace_regulator_set_voltage(name: rdev_get_name(rdev), min: min_uV, max: max_uV); |
| 3830 | |
| 3831 | min_uV += rdev->constraints->uV_offset; |
| 3832 | max_uV += rdev->constraints->uV_offset; |
| 3833 | |
| 3834 | /* |
| 3835 | * If we can't obtain the old selector there is not enough |
| 3836 | * info to call set_voltage_time_sel(). |
| 3837 | */ |
| 3838 | if (_regulator_is_enabled(rdev) && |
| 3839 | ops->set_voltage_time_sel && ops->get_voltage_sel) { |
| 3840 | old_selector = ops->get_voltage_sel(rdev); |
| 3841 | if (old_selector < 0) |
| 3842 | return old_selector; |
| 3843 | } |
| 3844 | |
| 3845 | if (ops->set_voltage) { |
| 3846 | ret = _regulator_call_set_voltage(rdev, min_uV, max_uV, |
| 3847 | selector: &selector); |
| 3848 | |
| 3849 | if (ret >= 0) { |
| 3850 | if (ops->list_voltage) |
| 3851 | best_val = ops->list_voltage(rdev, |
| 3852 | selector); |
| 3853 | else |
| 3854 | best_val = regulator_get_voltage_rdev(rdev); |
| 3855 | } |
| 3856 | |
| 3857 | } else if (ops->set_voltage_sel) { |
| 3858 | ret = regulator_map_voltage(rdev, min_uV, max_uV); |
| 3859 | if (ret >= 0) { |
| 3860 | best_val = ops->list_voltage(rdev, ret); |
| 3861 | if (min_uV <= best_val && max_uV >= best_val) { |
| 3862 | selector = ret; |
| 3863 | if (old_selector == selector) |
| 3864 | ret = 0; |
| 3865 | else if (rdev->desc->vsel_step) |
| 3866 | ret = _regulator_set_voltage_sel_step( |
| 3867 | rdev, uV: best_val, new_selector: selector); |
| 3868 | else |
| 3869 | ret = _regulator_call_set_voltage_sel( |
| 3870 | rdev, uV: best_val, selector); |
| 3871 | } else { |
| 3872 | ret = -EINVAL; |
| 3873 | } |
| 3874 | } |
| 3875 | } else { |
| 3876 | ret = -EINVAL; |
| 3877 | } |
| 3878 | |
| 3879 | if (ret) |
| 3880 | goto out; |
| 3881 | |
| 3882 | if (ops->set_voltage_time_sel) { |
| 3883 | /* |
| 3884 | * Call set_voltage_time_sel if successfully obtained |
| 3885 | * old_selector |
| 3886 | */ |
| 3887 | if (old_selector >= 0 && old_selector != selector) |
| 3888 | delay = ops->set_voltage_time_sel(rdev, old_selector, |
| 3889 | selector); |
| 3890 | } else { |
| 3891 | if (old_uV != best_val) { |
| 3892 | if (ops->set_voltage_time) |
| 3893 | delay = ops->set_voltage_time(rdev, old_uV, |
| 3894 | best_val); |
| 3895 | else |
| 3896 | delay = _regulator_set_voltage_time(rdev, |
| 3897 | old_uV, |
| 3898 | new_uV: best_val); |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | if (delay < 0) { |
| 3903 | rdev_warn(rdev, "failed to get delay: %pe\n" , ERR_PTR(delay)); |
| 3904 | delay = 0; |
| 3905 | } |
| 3906 | |
| 3907 | /* Insert any necessary delays */ |
| 3908 | fsleep(usecs: delay); |
| 3909 | |
| 3910 | if (best_val >= 0) { |
| 3911 | unsigned long data = best_val; |
| 3912 | |
| 3913 | _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, |
| 3914 | data: (void *)data); |
| 3915 | } |
| 3916 | |
| 3917 | out: |
| 3918 | trace_regulator_set_voltage_complete(name: rdev_get_name(rdev), value: best_val); |
| 3919 | |
| 3920 | return ret; |
| 3921 | } |
| 3922 | |
| 3923 | static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev, |
| 3924 | int min_uV, int max_uV, suspend_state_t state) |
| 3925 | { |
| 3926 | struct regulator_state *rstate; |
| 3927 | int uV, sel; |
| 3928 | |
| 3929 | rstate = regulator_get_suspend_state(rdev, state); |
| 3930 | if (rstate == NULL) |
| 3931 | return -EINVAL; |
| 3932 | |
| 3933 | if (min_uV < rstate->min_uV) |
| 3934 | min_uV = rstate->min_uV; |
| 3935 | if (max_uV > rstate->max_uV) |
| 3936 | max_uV = rstate->max_uV; |
| 3937 | |
| 3938 | sel = regulator_map_voltage(rdev, min_uV, max_uV); |
| 3939 | if (sel < 0) |
| 3940 | return sel; |
| 3941 | |
| 3942 | uV = rdev->desc->ops->list_voltage(rdev, sel); |
| 3943 | if (uV >= min_uV && uV <= max_uV) |
| 3944 | rstate->uV = uV; |
| 3945 | |
| 3946 | return 0; |
| 3947 | } |
| 3948 | |
| 3949 | static int regulator_get_voltage_delta(struct regulator_dev *rdev, int uV) |
| 3950 | { |
| 3951 | int current_uV = regulator_get_voltage_rdev(rdev); |
| 3952 | |
| 3953 | if (current_uV < 0) |
| 3954 | return current_uV; |
| 3955 | |
| 3956 | return abs(current_uV - uV); |
| 3957 | } |
| 3958 | |
| 3959 | static int regulator_set_voltage_unlocked(struct regulator *regulator, |
| 3960 | int min_uV, int max_uV, |
| 3961 | suspend_state_t state) |
| 3962 | { |
| 3963 | struct regulator_dev *rdev = regulator->rdev; |
| 3964 | struct regulator_voltage *voltage = ®ulator->voltage[state]; |
| 3965 | int ret = 0; |
| 3966 | int current_uV, delta, new_delta; |
| 3967 | int old_min_uV, old_max_uV; |
| 3968 | |
| 3969 | /* If we're setting the same range as last time the change |
| 3970 | * should be a noop (some cpufreq implementations use the same |
| 3971 | * voltage for multiple frequencies, for example). |
| 3972 | */ |
| 3973 | if (voltage->min_uV == min_uV && voltage->max_uV == max_uV) |
| 3974 | goto out; |
| 3975 | |
| 3976 | /* If we're trying to set a range that overlaps the current voltage, |
| 3977 | * return successfully even though the regulator does not support |
| 3978 | * changing the voltage. |
| 3979 | */ |
| 3980 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
| 3981 | current_uV = regulator_get_voltage_rdev(rdev); |
| 3982 | if (min_uV <= current_uV && current_uV <= max_uV) { |
| 3983 | voltage->min_uV = min_uV; |
| 3984 | voltage->max_uV = max_uV; |
| 3985 | goto out; |
| 3986 | } |
| 3987 | } |
| 3988 | |
| 3989 | /* sanity check */ |
| 3990 | if (!rdev->desc->ops->set_voltage && |
| 3991 | !rdev->desc->ops->set_voltage_sel) { |
| 3992 | ret = -EINVAL; |
| 3993 | goto out; |
| 3994 | } |
| 3995 | |
| 3996 | /* constraints check */ |
| 3997 | ret = regulator_check_voltage(rdev, min_uV: &min_uV, max_uV: &max_uV); |
| 3998 | if (ret < 0) |
| 3999 | goto out; |
| 4000 | |
| 4001 | /* restore original values in case of error */ |
| 4002 | old_min_uV = voltage->min_uV; |
| 4003 | old_max_uV = voltage->max_uV; |
| 4004 | voltage->min_uV = min_uV; |
| 4005 | voltage->max_uV = max_uV; |
| 4006 | |
| 4007 | /* for not coupled regulators this will just set the voltage */ |
| 4008 | ret = regulator_balance_voltage(rdev, state); |
| 4009 | if (ret < 0) { |
| 4010 | voltage->min_uV = old_min_uV; |
| 4011 | voltage->max_uV = old_max_uV; |
| 4012 | } |
| 4013 | |
| 4014 | if (rdev->constraints->max_uV_step > 0) { |
| 4015 | /* For regulators with a maximum voltage step, reaching the desired |
| 4016 | * voltage might take a few retries. |
| 4017 | */ |
| 4018 | ret = regulator_get_voltage_delta(rdev, uV: min_uV); |
| 4019 | if (ret < 0) |
| 4020 | goto out; |
| 4021 | |
| 4022 | delta = ret; |
| 4023 | |
| 4024 | while (delta > 0) { |
| 4025 | ret = regulator_balance_voltage(rdev, state); |
| 4026 | if (ret < 0) |
| 4027 | goto out; |
| 4028 | |
| 4029 | ret = regulator_get_voltage_delta(rdev, uV: min_uV); |
| 4030 | if (ret < 0) |
| 4031 | goto out; |
| 4032 | |
| 4033 | new_delta = ret; |
| 4034 | |
| 4035 | /* check that voltage is converging quickly enough */ |
| 4036 | if (delta - new_delta < rdev->constraints->max_uV_step) { |
| 4037 | ret = -EWOULDBLOCK; |
| 4038 | goto out; |
| 4039 | } |
| 4040 | |
| 4041 | delta = new_delta; |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | out: |
| 4046 | return ret; |
| 4047 | } |
| 4048 | |
| 4049 | int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV, |
| 4050 | int max_uV, suspend_state_t state) |
| 4051 | { |
| 4052 | int best_supply_uV = 0; |
| 4053 | int supply_change_uV = 0; |
| 4054 | int ret; |
| 4055 | |
| 4056 | if (rdev->supply && |
| 4057 | regulator_ops_is_valid(rdev: rdev->supply->rdev, |
| 4058 | REGULATOR_CHANGE_VOLTAGE) && |
| 4059 | (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage || |
| 4060 | rdev->desc->ops->get_voltage_sel))) { |
| 4061 | int current_supply_uV; |
| 4062 | int selector; |
| 4063 | |
| 4064 | selector = regulator_map_voltage(rdev, min_uV, max_uV); |
| 4065 | if (selector < 0) { |
| 4066 | ret = selector; |
| 4067 | goto out; |
| 4068 | } |
| 4069 | |
| 4070 | best_supply_uV = _regulator_list_voltage(rdev, selector, lock: 0); |
| 4071 | if (best_supply_uV < 0) { |
| 4072 | ret = best_supply_uV; |
| 4073 | goto out; |
| 4074 | } |
| 4075 | |
| 4076 | best_supply_uV += rdev->desc->min_dropout_uV; |
| 4077 | |
| 4078 | current_supply_uV = regulator_get_voltage_rdev(rdev: rdev->supply->rdev); |
| 4079 | if (current_supply_uV < 0) { |
| 4080 | ret = current_supply_uV; |
| 4081 | goto out; |
| 4082 | } |
| 4083 | |
| 4084 | supply_change_uV = best_supply_uV - current_supply_uV; |
| 4085 | } |
| 4086 | |
| 4087 | if (supply_change_uV > 0) { |
| 4088 | ret = regulator_set_voltage_unlocked(regulator: rdev->supply, |
| 4089 | min_uV: best_supply_uV, INT_MAX, state); |
| 4090 | if (ret) { |
| 4091 | dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n" , |
| 4092 | ERR_PTR(ret)); |
| 4093 | goto out; |
| 4094 | } |
| 4095 | } |
| 4096 | |
| 4097 | if (state == PM_SUSPEND_ON) |
| 4098 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); |
| 4099 | else |
| 4100 | ret = _regulator_do_set_suspend_voltage(rdev, min_uV, |
| 4101 | max_uV, state); |
| 4102 | if (ret < 0) |
| 4103 | goto out; |
| 4104 | |
| 4105 | if (supply_change_uV < 0) { |
| 4106 | ret = regulator_set_voltage_unlocked(regulator: rdev->supply, |
| 4107 | min_uV: best_supply_uV, INT_MAX, state); |
| 4108 | if (ret) |
| 4109 | dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n" , |
| 4110 | ERR_PTR(ret)); |
| 4111 | /* No need to fail here */ |
| 4112 | ret = 0; |
| 4113 | } |
| 4114 | |
| 4115 | out: |
| 4116 | return ret; |
| 4117 | } |
| 4118 | EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev); |
| 4119 | |
| 4120 | static int regulator_limit_voltage_step(struct regulator_dev *rdev, |
| 4121 | int *current_uV, int *min_uV) |
| 4122 | { |
| 4123 | struct regulation_constraints *constraints = rdev->constraints; |
| 4124 | |
| 4125 | /* Limit voltage change only if necessary */ |
| 4126 | if (!constraints->max_uV_step || !_regulator_is_enabled(rdev)) |
| 4127 | return 1; |
| 4128 | |
| 4129 | if (*current_uV < 0) { |
| 4130 | *current_uV = regulator_get_voltage_rdev(rdev); |
| 4131 | |
| 4132 | if (*current_uV < 0) |
| 4133 | return *current_uV; |
| 4134 | } |
| 4135 | |
| 4136 | if (abs(*current_uV - *min_uV) <= constraints->max_uV_step) |
| 4137 | return 1; |
| 4138 | |
| 4139 | /* Clamp target voltage within the given step */ |
| 4140 | if (*current_uV < *min_uV) |
| 4141 | *min_uV = min(*current_uV + constraints->max_uV_step, |
| 4142 | *min_uV); |
| 4143 | else |
| 4144 | *min_uV = max(*current_uV - constraints->max_uV_step, |
| 4145 | *min_uV); |
| 4146 | |
| 4147 | return 0; |
| 4148 | } |
| 4149 | |
| 4150 | static int regulator_get_optimal_voltage(struct regulator_dev *rdev, |
| 4151 | int *current_uV, |
| 4152 | int *min_uV, int *max_uV, |
| 4153 | suspend_state_t state, |
| 4154 | int n_coupled) |
| 4155 | { |
| 4156 | struct coupling_desc *c_desc = &rdev->coupling_desc; |
| 4157 | struct regulator_dev **c_rdevs = c_desc->coupled_rdevs; |
| 4158 | struct regulation_constraints *constraints = rdev->constraints; |
| 4159 | int desired_min_uV = 0, desired_max_uV = INT_MAX; |
| 4160 | int max_current_uV = 0, min_current_uV = INT_MAX; |
| 4161 | int highest_min_uV = 0, target_uV, possible_uV; |
| 4162 | int i, ret, max_spread; |
| 4163 | bool done; |
| 4164 | |
| 4165 | *current_uV = -1; |
| 4166 | |
| 4167 | /* |
| 4168 | * If there are no coupled regulators, simply set the voltage |
| 4169 | * demanded by consumers. |
| 4170 | */ |
| 4171 | if (n_coupled == 1) { |
| 4172 | /* |
| 4173 | * If consumers don't provide any demands, set voltage |
| 4174 | * to min_uV |
| 4175 | */ |
| 4176 | desired_min_uV = constraints->min_uV; |
| 4177 | desired_max_uV = constraints->max_uV; |
| 4178 | |
| 4179 | ret = regulator_check_consumers(rdev, |
| 4180 | min_uV: &desired_min_uV, |
| 4181 | max_uV: &desired_max_uV, state); |
| 4182 | if (ret < 0) |
| 4183 | return ret; |
| 4184 | |
| 4185 | done = true; |
| 4186 | |
| 4187 | goto finish; |
| 4188 | } |
| 4189 | |
| 4190 | /* Find highest min desired voltage */ |
| 4191 | for (i = 0; i < n_coupled; i++) { |
| 4192 | int tmp_min = 0; |
| 4193 | int tmp_max = INT_MAX; |
| 4194 | |
| 4195 | lockdep_assert_held_once(&c_rdevs[i]->mutex.base); |
| 4196 | |
| 4197 | ret = regulator_check_consumers(rdev: c_rdevs[i], |
| 4198 | min_uV: &tmp_min, |
| 4199 | max_uV: &tmp_max, state); |
| 4200 | if (ret < 0) |
| 4201 | return ret; |
| 4202 | |
| 4203 | ret = regulator_check_voltage(rdev: c_rdevs[i], min_uV: &tmp_min, max_uV: &tmp_max); |
| 4204 | if (ret < 0) |
| 4205 | return ret; |
| 4206 | |
| 4207 | highest_min_uV = max(highest_min_uV, tmp_min); |
| 4208 | |
| 4209 | if (i == 0) { |
| 4210 | desired_min_uV = tmp_min; |
| 4211 | desired_max_uV = tmp_max; |
| 4212 | } |
| 4213 | } |
| 4214 | |
| 4215 | max_spread = constraints->max_spread[0]; |
| 4216 | |
| 4217 | /* |
| 4218 | * Let target_uV be equal to the desired one if possible. |
| 4219 | * If not, set it to minimum voltage, allowed by other coupled |
| 4220 | * regulators. |
| 4221 | */ |
| 4222 | target_uV = max(desired_min_uV, highest_min_uV - max_spread); |
| 4223 | |
| 4224 | /* |
| 4225 | * Find min and max voltages, which currently aren't violating |
| 4226 | * max_spread. |
| 4227 | */ |
| 4228 | for (i = 1; i < n_coupled; i++) { |
| 4229 | int tmp_act; |
| 4230 | |
| 4231 | if (!_regulator_is_enabled(rdev: c_rdevs[i])) |
| 4232 | continue; |
| 4233 | |
| 4234 | tmp_act = regulator_get_voltage_rdev(rdev: c_rdevs[i]); |
| 4235 | if (tmp_act < 0) |
| 4236 | return tmp_act; |
| 4237 | |
| 4238 | min_current_uV = min(tmp_act, min_current_uV); |
| 4239 | max_current_uV = max(tmp_act, max_current_uV); |
| 4240 | } |
| 4241 | |
| 4242 | /* There aren't any other regulators enabled */ |
| 4243 | if (max_current_uV == 0) { |
| 4244 | possible_uV = target_uV; |
| 4245 | } else { |
| 4246 | /* |
| 4247 | * Correct target voltage, so as it currently isn't |
| 4248 | * violating max_spread |
| 4249 | */ |
| 4250 | possible_uV = max(target_uV, max_current_uV - max_spread); |
| 4251 | possible_uV = min(possible_uV, min_current_uV + max_spread); |
| 4252 | } |
| 4253 | |
| 4254 | if (possible_uV > desired_max_uV) |
| 4255 | return -EINVAL; |
| 4256 | |
| 4257 | done = (possible_uV == target_uV); |
| 4258 | desired_min_uV = possible_uV; |
| 4259 | |
| 4260 | finish: |
| 4261 | /* Apply max_uV_step constraint if necessary */ |
| 4262 | if (state == PM_SUSPEND_ON) { |
| 4263 | ret = regulator_limit_voltage_step(rdev, current_uV, |
| 4264 | min_uV: &desired_min_uV); |
| 4265 | if (ret < 0) |
| 4266 | return ret; |
| 4267 | |
| 4268 | if (ret == 0) |
| 4269 | done = false; |
| 4270 | } |
| 4271 | |
| 4272 | /* Set current_uV if wasn't done earlier in the code and if necessary */ |
| 4273 | if (n_coupled > 1 && *current_uV == -1) { |
| 4274 | |
| 4275 | if (_regulator_is_enabled(rdev)) { |
| 4276 | ret = regulator_get_voltage_rdev(rdev); |
| 4277 | if (ret < 0) |
| 4278 | return ret; |
| 4279 | |
| 4280 | *current_uV = ret; |
| 4281 | } else { |
| 4282 | *current_uV = desired_min_uV; |
| 4283 | } |
| 4284 | } |
| 4285 | |
| 4286 | *min_uV = desired_min_uV; |
| 4287 | *max_uV = desired_max_uV; |
| 4288 | |
| 4289 | return done; |
| 4290 | } |
| 4291 | |
| 4292 | int regulator_do_balance_voltage(struct regulator_dev *rdev, |
| 4293 | suspend_state_t state, bool skip_coupled) |
| 4294 | { |
| 4295 | struct regulator_dev **c_rdevs; |
| 4296 | struct regulator_dev *best_rdev; |
| 4297 | struct coupling_desc *c_desc = &rdev->coupling_desc; |
| 4298 | int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev; |
| 4299 | unsigned int delta, best_delta; |
| 4300 | unsigned long c_rdev_done = 0; |
| 4301 | bool best_c_rdev_done; |
| 4302 | |
| 4303 | c_rdevs = c_desc->coupled_rdevs; |
| 4304 | n_coupled = skip_coupled ? 1 : c_desc->n_coupled; |
| 4305 | |
| 4306 | /* |
| 4307 | * Find the best possible voltage change on each loop. Leave the loop |
| 4308 | * if there isn't any possible change. |
| 4309 | */ |
| 4310 | do { |
| 4311 | best_c_rdev_done = false; |
| 4312 | best_delta = 0; |
| 4313 | best_min_uV = 0; |
| 4314 | best_max_uV = 0; |
| 4315 | best_c_rdev = 0; |
| 4316 | best_rdev = NULL; |
| 4317 | |
| 4318 | /* |
| 4319 | * Find highest difference between optimal voltage |
| 4320 | * and current voltage. |
| 4321 | */ |
| 4322 | for (i = 0; i < n_coupled; i++) { |
| 4323 | /* |
| 4324 | * optimal_uV is the best voltage that can be set for |
| 4325 | * i-th regulator at the moment without violating |
| 4326 | * max_spread constraint in order to balance |
| 4327 | * the coupled voltages. |
| 4328 | */ |
| 4329 | int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0; |
| 4330 | |
| 4331 | if (test_bit(i, &c_rdev_done)) |
| 4332 | continue; |
| 4333 | |
| 4334 | ret = regulator_get_optimal_voltage(rdev: c_rdevs[i], |
| 4335 | current_uV: ¤t_uV, |
| 4336 | min_uV: &optimal_uV, |
| 4337 | max_uV: &optimal_max_uV, |
| 4338 | state, n_coupled); |
| 4339 | if (ret < 0) |
| 4340 | goto out; |
| 4341 | |
| 4342 | delta = abs(optimal_uV - current_uV); |
| 4343 | |
| 4344 | if (delta && best_delta <= delta) { |
| 4345 | best_c_rdev_done = ret; |
| 4346 | best_delta = delta; |
| 4347 | best_rdev = c_rdevs[i]; |
| 4348 | best_min_uV = optimal_uV; |
| 4349 | best_max_uV = optimal_max_uV; |
| 4350 | best_c_rdev = i; |
| 4351 | } |
| 4352 | } |
| 4353 | |
| 4354 | /* Nothing to change, return successfully */ |
| 4355 | if (!best_rdev) { |
| 4356 | ret = 0; |
| 4357 | goto out; |
| 4358 | } |
| 4359 | |
| 4360 | ret = regulator_set_voltage_rdev(best_rdev, best_min_uV, |
| 4361 | best_max_uV, state); |
| 4362 | |
| 4363 | if (ret < 0) |
| 4364 | goto out; |
| 4365 | |
| 4366 | if (best_c_rdev_done) |
| 4367 | set_bit(nr: best_c_rdev, addr: &c_rdev_done); |
| 4368 | |
| 4369 | } while (n_coupled > 1); |
| 4370 | |
| 4371 | out: |
| 4372 | return ret; |
| 4373 | } |
| 4374 | |
| 4375 | static int regulator_balance_voltage(struct regulator_dev *rdev, |
| 4376 | suspend_state_t state) |
| 4377 | { |
| 4378 | struct coupling_desc *c_desc = &rdev->coupling_desc; |
| 4379 | struct regulator_coupler *coupler = c_desc->coupler; |
| 4380 | bool skip_coupled = false; |
| 4381 | |
| 4382 | /* |
| 4383 | * If system is in a state other than PM_SUSPEND_ON, don't check |
| 4384 | * other coupled regulators. |
| 4385 | */ |
| 4386 | if (state != PM_SUSPEND_ON) |
| 4387 | skip_coupled = true; |
| 4388 | |
| 4389 | if (c_desc->n_resolved < c_desc->n_coupled) { |
| 4390 | rdev_err(rdev, "Not all coupled regulators registered\n" ); |
| 4391 | return -EPERM; |
| 4392 | } |
| 4393 | |
| 4394 | /* Invoke custom balancer for customized couplers */ |
| 4395 | if (coupler && coupler->balance_voltage) |
| 4396 | return coupler->balance_voltage(coupler, rdev, state); |
| 4397 | |
| 4398 | return regulator_do_balance_voltage(rdev, state, skip_coupled); |
| 4399 | } |
| 4400 | |
| 4401 | /** |
| 4402 | * regulator_set_voltage - set regulator output voltage |
| 4403 | * @regulator: regulator source |
| 4404 | * @min_uV: Minimum required voltage in uV |
| 4405 | * @max_uV: Maximum acceptable voltage in uV |
| 4406 | * |
| 4407 | * Sets a voltage regulator to the desired output voltage. This can be set |
| 4408 | * during any regulator state. IOW, regulator can be disabled or enabled. |
| 4409 | * |
| 4410 | * If the regulator is enabled then the voltage will change to the new value |
| 4411 | * immediately otherwise if the regulator is disabled the regulator will |
| 4412 | * output at the new voltage when enabled. |
| 4413 | * |
| 4414 | * NOTE: If the regulator is shared between several devices then the lowest |
| 4415 | * request voltage that meets the system constraints will be used. |
| 4416 | * Regulator system constraints must be set for this regulator before |
| 4417 | * calling this function otherwise this call will fail. |
| 4418 | * |
| 4419 | * Return: 0 on success or a negative error number on failure. |
| 4420 | */ |
| 4421 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) |
| 4422 | { |
| 4423 | struct ww_acquire_ctx ww_ctx; |
| 4424 | int ret; |
| 4425 | |
| 4426 | regulator_lock_dependent(rdev: regulator->rdev, ww_ctx: &ww_ctx); |
| 4427 | |
| 4428 | ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV, |
| 4429 | PM_SUSPEND_ON); |
| 4430 | |
| 4431 | regulator_unlock_dependent(rdev: regulator->rdev, ww_ctx: &ww_ctx); |
| 4432 | |
| 4433 | return ret; |
| 4434 | } |
| 4435 | EXPORT_SYMBOL_GPL(regulator_set_voltage); |
| 4436 | |
| 4437 | static inline int regulator_suspend_toggle(struct regulator_dev *rdev, |
| 4438 | suspend_state_t state, bool en) |
| 4439 | { |
| 4440 | struct regulator_state *rstate; |
| 4441 | |
| 4442 | rstate = regulator_get_suspend_state(rdev, state); |
| 4443 | if (rstate == NULL) |
| 4444 | return -EINVAL; |
| 4445 | |
| 4446 | if (!rstate->changeable) |
| 4447 | return -EPERM; |
| 4448 | |
| 4449 | rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND; |
| 4450 | |
| 4451 | return 0; |
| 4452 | } |
| 4453 | |
| 4454 | int regulator_suspend_enable(struct regulator_dev *rdev, |
| 4455 | suspend_state_t state) |
| 4456 | { |
| 4457 | return regulator_suspend_toggle(rdev, state, en: true); |
| 4458 | } |
| 4459 | EXPORT_SYMBOL_GPL(regulator_suspend_enable); |
| 4460 | |
| 4461 | int regulator_suspend_disable(struct regulator_dev *rdev, |
| 4462 | suspend_state_t state) |
| 4463 | { |
| 4464 | struct regulator *regulator; |
| 4465 | struct regulator_voltage *voltage; |
| 4466 | |
| 4467 | /* |
| 4468 | * if any consumer wants this regulator device keeping on in |
| 4469 | * suspend states, don't set it as disabled. |
| 4470 | */ |
| 4471 | list_for_each_entry(regulator, &rdev->consumer_list, list) { |
| 4472 | voltage = ®ulator->voltage[state]; |
| 4473 | if (voltage->min_uV || voltage->max_uV) |
| 4474 | return 0; |
| 4475 | } |
| 4476 | |
| 4477 | return regulator_suspend_toggle(rdev, state, en: false); |
| 4478 | } |
| 4479 | EXPORT_SYMBOL_GPL(regulator_suspend_disable); |
| 4480 | |
| 4481 | static int _regulator_set_suspend_voltage(struct regulator *regulator, |
| 4482 | int min_uV, int max_uV, |
| 4483 | suspend_state_t state) |
| 4484 | { |
| 4485 | struct regulator_dev *rdev = regulator->rdev; |
| 4486 | struct regulator_state *rstate; |
| 4487 | |
| 4488 | rstate = regulator_get_suspend_state(rdev, state); |
| 4489 | if (rstate == NULL) |
| 4490 | return -EINVAL; |
| 4491 | |
| 4492 | if (rstate->min_uV == rstate->max_uV) { |
| 4493 | rdev_err(rdev, "The suspend voltage can't be changed!\n" ); |
| 4494 | return -EPERM; |
| 4495 | } |
| 4496 | |
| 4497 | return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state); |
| 4498 | } |
| 4499 | |
| 4500 | int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV, |
| 4501 | int max_uV, suspend_state_t state) |
| 4502 | { |
| 4503 | struct ww_acquire_ctx ww_ctx; |
| 4504 | int ret; |
| 4505 | |
| 4506 | /* PM_SUSPEND_ON is handled by regulator_set_voltage() */ |
| 4507 | if (regulator_check_states(state) || state == PM_SUSPEND_ON) |
| 4508 | return -EINVAL; |
| 4509 | |
| 4510 | regulator_lock_dependent(rdev: regulator->rdev, ww_ctx: &ww_ctx); |
| 4511 | |
| 4512 | ret = _regulator_set_suspend_voltage(regulator, min_uV, |
| 4513 | max_uV, state); |
| 4514 | |
| 4515 | regulator_unlock_dependent(rdev: regulator->rdev, ww_ctx: &ww_ctx); |
| 4516 | |
| 4517 | return ret; |
| 4518 | } |
| 4519 | EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage); |
| 4520 | |
| 4521 | /** |
| 4522 | * regulator_set_voltage_time - get raise/fall time |
| 4523 | * @regulator: regulator source |
| 4524 | * @old_uV: starting voltage in microvolts |
| 4525 | * @new_uV: target voltage in microvolts |
| 4526 | * |
| 4527 | * Provided with the starting and ending voltage, this function attempts to |
| 4528 | * calculate the time in microseconds required to rise or fall to this new |
| 4529 | * voltage. |
| 4530 | * |
| 4531 | * Return: ramp time in microseconds, or a negative error number if calculation failed. |
| 4532 | */ |
| 4533 | int regulator_set_voltage_time(struct regulator *regulator, |
| 4534 | int old_uV, int new_uV) |
| 4535 | { |
| 4536 | struct regulator_dev *rdev = regulator->rdev; |
| 4537 | const struct regulator_ops *ops = rdev->desc->ops; |
| 4538 | int old_sel = -1; |
| 4539 | int new_sel = -1; |
| 4540 | int voltage; |
| 4541 | int i; |
| 4542 | |
| 4543 | if (ops->set_voltage_time) |
| 4544 | return ops->set_voltage_time(rdev, old_uV, new_uV); |
| 4545 | else if (!ops->set_voltage_time_sel) |
| 4546 | return _regulator_set_voltage_time(rdev, old_uV, new_uV); |
| 4547 | |
| 4548 | /* Currently requires operations to do this */ |
| 4549 | if (!ops->list_voltage || !rdev->desc->n_voltages) |
| 4550 | return -EINVAL; |
| 4551 | |
| 4552 | for (i = 0; i < rdev->desc->n_voltages; i++) { |
| 4553 | /* We only look for exact voltage matches here */ |
| 4554 | if (i < rdev->desc->linear_min_sel) |
| 4555 | continue; |
| 4556 | |
| 4557 | if (old_sel >= 0 && new_sel >= 0) |
| 4558 | break; |
| 4559 | |
| 4560 | voltage = regulator_list_voltage(regulator, i); |
| 4561 | if (voltage < 0) |
| 4562 | return -EINVAL; |
| 4563 | if (voltage == 0) |
| 4564 | continue; |
| 4565 | if (voltage == old_uV) |
| 4566 | old_sel = i; |
| 4567 | if (voltage == new_uV) |
| 4568 | new_sel = i; |
| 4569 | } |
| 4570 | |
| 4571 | if (old_sel < 0 || new_sel < 0) |
| 4572 | return -EINVAL; |
| 4573 | |
| 4574 | return ops->set_voltage_time_sel(rdev, old_sel, new_sel); |
| 4575 | } |
| 4576 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time); |
| 4577 | |
| 4578 | /** |
| 4579 | * regulator_set_voltage_time_sel - get raise/fall time |
| 4580 | * @rdev: regulator source device |
| 4581 | * @old_selector: selector for starting voltage |
| 4582 | * @new_selector: selector for target voltage |
| 4583 | * |
| 4584 | * Provided with the starting and target voltage selectors, this function |
| 4585 | * returns time in microseconds required to rise or fall to this new voltage |
| 4586 | * |
| 4587 | * Drivers providing ramp_delay in regulation_constraints can use this as their |
| 4588 | * set_voltage_time_sel() operation. |
| 4589 | * |
| 4590 | * Return: ramp time in microseconds, or a negative error number if calculation failed. |
| 4591 | */ |
| 4592 | int regulator_set_voltage_time_sel(struct regulator_dev *rdev, |
| 4593 | unsigned int old_selector, |
| 4594 | unsigned int new_selector) |
| 4595 | { |
| 4596 | int old_volt, new_volt; |
| 4597 | |
| 4598 | /* sanity check */ |
| 4599 | if (!rdev->desc->ops->list_voltage) |
| 4600 | return -EINVAL; |
| 4601 | |
| 4602 | old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); |
| 4603 | new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); |
| 4604 | |
| 4605 | if (rdev->desc->ops->set_voltage_time) |
| 4606 | return rdev->desc->ops->set_voltage_time(rdev, old_volt, |
| 4607 | new_volt); |
| 4608 | else |
| 4609 | return _regulator_set_voltage_time(rdev, old_uV: old_volt, new_uV: new_volt); |
| 4610 | } |
| 4611 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel); |
| 4612 | |
| 4613 | int regulator_sync_voltage_rdev(struct regulator_dev *rdev) |
| 4614 | { |
| 4615 | int ret; |
| 4616 | |
| 4617 | regulator_lock(rdev); |
| 4618 | |
| 4619 | if (!rdev->desc->ops->set_voltage && |
| 4620 | !rdev->desc->ops->set_voltage_sel) { |
| 4621 | ret = -EINVAL; |
| 4622 | goto out; |
| 4623 | } |
| 4624 | |
| 4625 | /* balance only, if regulator is coupled */ |
| 4626 | if (rdev->coupling_desc.n_coupled > 1) |
| 4627 | ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
| 4628 | else |
| 4629 | ret = -EOPNOTSUPP; |
| 4630 | |
| 4631 | out: |
| 4632 | regulator_unlock(rdev); |
| 4633 | return ret; |
| 4634 | } |
| 4635 | |
| 4636 | /** |
| 4637 | * regulator_sync_voltage - re-apply last regulator output voltage |
| 4638 | * @regulator: regulator source |
| 4639 | * |
| 4640 | * Re-apply the last configured voltage. This is intended to be used |
| 4641 | * where some external control source the consumer is cooperating with |
| 4642 | * has caused the configured voltage to change. |
| 4643 | * |
| 4644 | * Return: 0 on success or a negative error number on failure. |
| 4645 | */ |
| 4646 | int regulator_sync_voltage(struct regulator *regulator) |
| 4647 | { |
| 4648 | struct regulator_dev *rdev = regulator->rdev; |
| 4649 | struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON]; |
| 4650 | int ret, min_uV, max_uV; |
| 4651 | |
| 4652 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) |
| 4653 | return 0; |
| 4654 | |
| 4655 | regulator_lock(rdev); |
| 4656 | |
| 4657 | if (!rdev->desc->ops->set_voltage && |
| 4658 | !rdev->desc->ops->set_voltage_sel) { |
| 4659 | ret = -EINVAL; |
| 4660 | goto out; |
| 4661 | } |
| 4662 | |
| 4663 | /* This is only going to work if we've had a voltage configured. */ |
| 4664 | if (!voltage->min_uV && !voltage->max_uV) { |
| 4665 | ret = -EINVAL; |
| 4666 | goto out; |
| 4667 | } |
| 4668 | |
| 4669 | min_uV = voltage->min_uV; |
| 4670 | max_uV = voltage->max_uV; |
| 4671 | |
| 4672 | /* This should be a paranoia check... */ |
| 4673 | ret = regulator_check_voltage(rdev, min_uV: &min_uV, max_uV: &max_uV); |
| 4674 | if (ret < 0) |
| 4675 | goto out; |
| 4676 | |
| 4677 | ret = regulator_check_consumers(rdev, min_uV: &min_uV, max_uV: &max_uV, state: 0); |
| 4678 | if (ret < 0) |
| 4679 | goto out; |
| 4680 | |
| 4681 | /* balance only, if regulator is coupled */ |
| 4682 | if (rdev->coupling_desc.n_coupled > 1) |
| 4683 | ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); |
| 4684 | else |
| 4685 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); |
| 4686 | |
| 4687 | out: |
| 4688 | regulator_unlock(rdev); |
| 4689 | return ret; |
| 4690 | } |
| 4691 | EXPORT_SYMBOL_GPL(regulator_sync_voltage); |
| 4692 | |
| 4693 | int regulator_get_voltage_rdev(struct regulator_dev *rdev) |
| 4694 | { |
| 4695 | int sel, ret; |
| 4696 | bool bypassed; |
| 4697 | |
| 4698 | if (rdev->desc->ops->get_bypass) { |
| 4699 | ret = rdev->desc->ops->get_bypass(rdev, &bypassed); |
| 4700 | if (ret < 0) |
| 4701 | return ret; |
| 4702 | if (bypassed) { |
| 4703 | /* if bypassed the regulator must have a supply */ |
| 4704 | if (!rdev->supply) { |
| 4705 | rdev_err(rdev, |
| 4706 | "bypassed regulator has no supply!\n" ); |
| 4707 | return -EPROBE_DEFER; |
| 4708 | } |
| 4709 | |
| 4710 | return regulator_get_voltage_rdev(rdev: rdev->supply->rdev); |
| 4711 | } |
| 4712 | } |
| 4713 | |
| 4714 | if (rdev->desc->ops->get_voltage_sel) { |
| 4715 | sel = rdev->desc->ops->get_voltage_sel(rdev); |
| 4716 | if (sel < 0) |
| 4717 | return sel; |
| 4718 | ret = rdev->desc->ops->list_voltage(rdev, sel); |
| 4719 | } else if (rdev->desc->ops->get_voltage) { |
| 4720 | ret = rdev->desc->ops->get_voltage(rdev); |
| 4721 | } else if (rdev->desc->ops->list_voltage) { |
| 4722 | ret = rdev->desc->ops->list_voltage(rdev, 0); |
| 4723 | } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { |
| 4724 | ret = rdev->desc->fixed_uV; |
| 4725 | } else if (rdev->supply) { |
| 4726 | ret = regulator_get_voltage_rdev(rdev: rdev->supply->rdev); |
| 4727 | } else if (rdev->supply_name) { |
| 4728 | return -EPROBE_DEFER; |
| 4729 | } else { |
| 4730 | return -EINVAL; |
| 4731 | } |
| 4732 | |
| 4733 | if (ret < 0) |
| 4734 | return ret; |
| 4735 | return ret - rdev->constraints->uV_offset; |
| 4736 | } |
| 4737 | EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev); |
| 4738 | |
| 4739 | /** |
| 4740 | * regulator_get_voltage - get regulator output voltage |
| 4741 | * @regulator: regulator source |
| 4742 | * |
| 4743 | * Return: Current regulator voltage in uV, or a negative error number on failure. |
| 4744 | * |
| 4745 | * NOTE: If the regulator is disabled it will return the voltage value. This |
| 4746 | * function should not be used to determine regulator state. |
| 4747 | */ |
| 4748 | int regulator_get_voltage(struct regulator *regulator) |
| 4749 | { |
| 4750 | struct ww_acquire_ctx ww_ctx; |
| 4751 | int ret; |
| 4752 | |
| 4753 | regulator_lock_dependent(rdev: regulator->rdev, ww_ctx: &ww_ctx); |
| 4754 | ret = regulator_get_voltage_rdev(regulator->rdev); |
| 4755 | regulator_unlock_dependent(rdev: regulator->rdev, ww_ctx: &ww_ctx); |
| 4756 | |
| 4757 | return ret; |
| 4758 | } |
| 4759 | EXPORT_SYMBOL_GPL(regulator_get_voltage); |
| 4760 | |
| 4761 | /** |
| 4762 | * regulator_set_current_limit - set regulator output current limit |
| 4763 | * @regulator: regulator source |
| 4764 | * @min_uA: Minimum supported current in uA |
| 4765 | * @max_uA: Maximum supported current in uA |
| 4766 | * |
| 4767 | * Sets current sink to the desired output current. This can be set during |
| 4768 | * any regulator state. IOW, regulator can be disabled or enabled. |
| 4769 | * |
| 4770 | * If the regulator is enabled then the current will change to the new value |
| 4771 | * immediately otherwise if the regulator is disabled the regulator will |
| 4772 | * output at the new current when enabled. |
| 4773 | * |
| 4774 | * NOTE: Regulator system constraints must be set for this regulator before |
| 4775 | * calling this function otherwise this call will fail. |
| 4776 | * |
| 4777 | * Return: 0 on success or a negative error number on failure. |
| 4778 | */ |
| 4779 | int regulator_set_current_limit(struct regulator *regulator, |
| 4780 | int min_uA, int max_uA) |
| 4781 | { |
| 4782 | struct regulator_dev *rdev = regulator->rdev; |
| 4783 | int ret; |
| 4784 | |
| 4785 | regulator_lock(rdev); |
| 4786 | |
| 4787 | /* sanity check */ |
| 4788 | if (!rdev->desc->ops->set_current_limit) { |
| 4789 | ret = -EINVAL; |
| 4790 | goto out; |
| 4791 | } |
| 4792 | |
| 4793 | /* constraints check */ |
| 4794 | ret = regulator_check_current_limit(rdev, min_uA: &min_uA, max_uA: &max_uA); |
| 4795 | if (ret < 0) |
| 4796 | goto out; |
| 4797 | |
| 4798 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); |
| 4799 | out: |
| 4800 | regulator_unlock(rdev); |
| 4801 | return ret; |
| 4802 | } |
| 4803 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); |
| 4804 | |
| 4805 | static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev) |
| 4806 | { |
| 4807 | /* sanity check */ |
| 4808 | if (!rdev->desc->ops->get_current_limit) |
| 4809 | return -EINVAL; |
| 4810 | |
| 4811 | return rdev->desc->ops->get_current_limit(rdev); |
| 4812 | } |
| 4813 | |
| 4814 | static int _regulator_get_current_limit(struct regulator_dev *rdev) |
| 4815 | { |
| 4816 | int ret; |
| 4817 | |
| 4818 | regulator_lock(rdev); |
| 4819 | ret = _regulator_get_current_limit_unlocked(rdev); |
| 4820 | regulator_unlock(rdev); |
| 4821 | |
| 4822 | return ret; |
| 4823 | } |
| 4824 | |
| 4825 | /** |
| 4826 | * regulator_get_current_limit - get regulator output current |
| 4827 | * @regulator: regulator source |
| 4828 | * |
| 4829 | * Return: Current supplied by the specified current sink in uA, |
| 4830 | * or a negative error number on failure. |
| 4831 | * |
| 4832 | * NOTE: If the regulator is disabled it will return the current value. This |
| 4833 | * function should not be used to determine regulator state. |
| 4834 | */ |
| 4835 | int regulator_get_current_limit(struct regulator *regulator) |
| 4836 | { |
| 4837 | return _regulator_get_current_limit(rdev: regulator->rdev); |
| 4838 | } |
| 4839 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); |
| 4840 | |
| 4841 | /** |
| 4842 | * regulator_get_unclaimed_power_budget - get regulator unclaimed power budget |
| 4843 | * @regulator: regulator source |
| 4844 | * |
| 4845 | * Return: Unclaimed power budget of the regulator in mW. |
| 4846 | */ |
| 4847 | int regulator_get_unclaimed_power_budget(struct regulator *regulator) |
| 4848 | { |
| 4849 | return regulator->rdev->constraints->pw_budget_mW - |
| 4850 | regulator->rdev->pw_requested_mW; |
| 4851 | } |
| 4852 | EXPORT_SYMBOL_GPL(regulator_get_unclaimed_power_budget); |
| 4853 | |
| 4854 | /** |
| 4855 | * regulator_request_power_budget - request power budget on a regulator |
| 4856 | * @regulator: regulator source |
| 4857 | * @pw_req: Power requested |
| 4858 | * |
| 4859 | * Return: 0 on success or a negative error number on failure. |
| 4860 | */ |
| 4861 | int regulator_request_power_budget(struct regulator *regulator, |
| 4862 | unsigned int pw_req) |
| 4863 | { |
| 4864 | struct regulator_dev *rdev = regulator->rdev; |
| 4865 | int ret = 0, pw_tot_req; |
| 4866 | |
| 4867 | regulator_lock(rdev); |
| 4868 | if (rdev->supply) { |
| 4869 | ret = regulator_request_power_budget(regulator: rdev->supply, pw_req); |
| 4870 | if (ret < 0) |
| 4871 | goto out; |
| 4872 | } |
| 4873 | |
| 4874 | pw_tot_req = rdev->pw_requested_mW + pw_req; |
| 4875 | if (pw_tot_req > rdev->constraints->pw_budget_mW) { |
| 4876 | rdev_warn(rdev, "power requested %d mW out of budget %d mW" , |
| 4877 | pw_req, |
| 4878 | rdev->constraints->pw_budget_mW - rdev->pw_requested_mW); |
| 4879 | regulator_notifier_call_chain(rdev, |
| 4880 | REGULATOR_EVENT_OVER_CURRENT_WARN, |
| 4881 | NULL); |
| 4882 | ret = -ERANGE; |
| 4883 | goto out; |
| 4884 | } |
| 4885 | |
| 4886 | rdev->pw_requested_mW = pw_tot_req; |
| 4887 | out: |
| 4888 | regulator_unlock(rdev); |
| 4889 | return ret; |
| 4890 | } |
| 4891 | EXPORT_SYMBOL_GPL(regulator_request_power_budget); |
| 4892 | |
| 4893 | /** |
| 4894 | * regulator_free_power_budget - free power budget on a regulator |
| 4895 | * @regulator: regulator source |
| 4896 | * @pw: Power to be released. |
| 4897 | * |
| 4898 | * Return: Power budget of the regulator in mW. |
| 4899 | */ |
| 4900 | void regulator_free_power_budget(struct regulator *regulator, |
| 4901 | unsigned int pw) |
| 4902 | { |
| 4903 | struct regulator_dev *rdev = regulator->rdev; |
| 4904 | int pw_tot_req; |
| 4905 | |
| 4906 | regulator_lock(rdev); |
| 4907 | if (rdev->supply) |
| 4908 | regulator_free_power_budget(regulator: rdev->supply, pw); |
| 4909 | |
| 4910 | pw_tot_req = rdev->pw_requested_mW - pw; |
| 4911 | if (pw_tot_req >= 0) |
| 4912 | rdev->pw_requested_mW = pw_tot_req; |
| 4913 | else |
| 4914 | rdev_warn(rdev, |
| 4915 | "too much power freed %d mW (already requested %d mW)" , |
| 4916 | pw, rdev->pw_requested_mW); |
| 4917 | |
| 4918 | regulator_unlock(rdev); |
| 4919 | } |
| 4920 | EXPORT_SYMBOL_GPL(regulator_free_power_budget); |
| 4921 | |
| 4922 | /** |
| 4923 | * regulator_set_mode - set regulator operating mode |
| 4924 | * @regulator: regulator source |
| 4925 | * @mode: operating mode - one of the REGULATOR_MODE constants |
| 4926 | * |
| 4927 | * Set regulator operating mode to increase regulator efficiency or improve |
| 4928 | * regulation performance. |
| 4929 | * |
| 4930 | * NOTE: Regulator system constraints must be set for this regulator before |
| 4931 | * calling this function otherwise this call will fail. |
| 4932 | * |
| 4933 | * Return: 0 on success or a negative error number on failure. |
| 4934 | */ |
| 4935 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) |
| 4936 | { |
| 4937 | struct regulator_dev *rdev = regulator->rdev; |
| 4938 | int ret; |
| 4939 | int regulator_curr_mode; |
| 4940 | |
| 4941 | regulator_lock(rdev); |
| 4942 | |
| 4943 | /* sanity check */ |
| 4944 | if (!rdev->desc->ops->set_mode) { |
| 4945 | ret = -EINVAL; |
| 4946 | goto out; |
| 4947 | } |
| 4948 | |
| 4949 | /* return if the same mode is requested */ |
| 4950 | if (rdev->desc->ops->get_mode) { |
| 4951 | regulator_curr_mode = rdev->desc->ops->get_mode(rdev); |
| 4952 | if (regulator_curr_mode == mode) { |
| 4953 | ret = 0; |
| 4954 | goto out; |
| 4955 | } |
| 4956 | } |
| 4957 | |
| 4958 | /* constraints check */ |
| 4959 | ret = regulator_mode_constrain(rdev, mode: &mode); |
| 4960 | if (ret < 0) |
| 4961 | goto out; |
| 4962 | |
| 4963 | ret = rdev->desc->ops->set_mode(rdev, mode); |
| 4964 | out: |
| 4965 | regulator_unlock(rdev); |
| 4966 | return ret; |
| 4967 | } |
| 4968 | EXPORT_SYMBOL_GPL(regulator_set_mode); |
| 4969 | |
| 4970 | static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev) |
| 4971 | { |
| 4972 | /* sanity check */ |
| 4973 | if (!rdev->desc->ops->get_mode) |
| 4974 | return -EINVAL; |
| 4975 | |
| 4976 | return rdev->desc->ops->get_mode(rdev); |
| 4977 | } |
| 4978 | |
| 4979 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) |
| 4980 | { |
| 4981 | int ret; |
| 4982 | |
| 4983 | regulator_lock(rdev); |
| 4984 | ret = _regulator_get_mode_unlocked(rdev); |
| 4985 | regulator_unlock(rdev); |
| 4986 | |
| 4987 | return ret; |
| 4988 | } |
| 4989 | |
| 4990 | /** |
| 4991 | * regulator_get_mode - get regulator operating mode |
| 4992 | * @regulator: regulator source |
| 4993 | * |
| 4994 | * Get the current regulator operating mode. |
| 4995 | * |
| 4996 | * Return: Current operating mode as %REGULATOR_MODE_* values, |
| 4997 | * or a negative error number on failure. |
| 4998 | */ |
| 4999 | unsigned int regulator_get_mode(struct regulator *regulator) |
| 5000 | { |
| 5001 | return _regulator_get_mode(rdev: regulator->rdev); |
| 5002 | } |
| 5003 | EXPORT_SYMBOL_GPL(regulator_get_mode); |
| 5004 | |
| 5005 | static int rdev_get_cached_err_flags(struct regulator_dev *rdev) |
| 5006 | { |
| 5007 | int ret = 0; |
| 5008 | |
| 5009 | if (rdev->use_cached_err) { |
| 5010 | spin_lock(lock: &rdev->err_lock); |
| 5011 | ret = rdev->cached_err; |
| 5012 | spin_unlock(lock: &rdev->err_lock); |
| 5013 | } |
| 5014 | return ret; |
| 5015 | } |
| 5016 | |
| 5017 | static int _regulator_get_error_flags(struct regulator_dev *rdev, |
| 5018 | unsigned int *flags) |
| 5019 | { |
| 5020 | int cached_flags, ret = 0; |
| 5021 | |
| 5022 | regulator_lock(rdev); |
| 5023 | |
| 5024 | cached_flags = rdev_get_cached_err_flags(rdev); |
| 5025 | |
| 5026 | if (rdev->desc->ops->get_error_flags) |
| 5027 | ret = rdev->desc->ops->get_error_flags(rdev, flags); |
| 5028 | else if (!rdev->use_cached_err) |
| 5029 | ret = -EINVAL; |
| 5030 | |
| 5031 | *flags |= cached_flags; |
| 5032 | |
| 5033 | regulator_unlock(rdev); |
| 5034 | |
| 5035 | return ret; |
| 5036 | } |
| 5037 | |
| 5038 | /** |
| 5039 | * regulator_get_error_flags - get regulator error information |
| 5040 | * @regulator: regulator source |
| 5041 | * @flags: pointer to store error flags |
| 5042 | * |
| 5043 | * Get the current regulator error information. |
| 5044 | * |
| 5045 | * Return: 0 on success or a negative error number on failure. |
| 5046 | */ |
| 5047 | int regulator_get_error_flags(struct regulator *regulator, |
| 5048 | unsigned int *flags) |
| 5049 | { |
| 5050 | return _regulator_get_error_flags(rdev: regulator->rdev, flags); |
| 5051 | } |
| 5052 | EXPORT_SYMBOL_GPL(regulator_get_error_flags); |
| 5053 | |
| 5054 | /** |
| 5055 | * regulator_set_load - set regulator load |
| 5056 | * @regulator: regulator source |
| 5057 | * @uA_load: load current |
| 5058 | * |
| 5059 | * Notifies the regulator core of a new device load. This is then used by |
| 5060 | * DRMS (if enabled by constraints) to set the most efficient regulator |
| 5061 | * operating mode for the new regulator loading. |
| 5062 | * |
| 5063 | * Consumer devices notify their supply regulator of the maximum power |
| 5064 | * they will require (can be taken from device datasheet in the power |
| 5065 | * consumption tables) when they change operational status and hence power |
| 5066 | * state. Examples of operational state changes that can affect power |
| 5067 | * consumption are :- |
| 5068 | * |
| 5069 | * o Device is opened / closed. |
| 5070 | * o Device I/O is about to begin or has just finished. |
| 5071 | * o Device is idling in between work. |
| 5072 | * |
| 5073 | * This information is also exported via sysfs to userspace. |
| 5074 | * |
| 5075 | * DRMS will sum the total requested load on the regulator and change |
| 5076 | * to the most efficient operating mode if platform constraints allow. |
| 5077 | * |
| 5078 | * NOTE: when a regulator consumer requests to have a regulator |
| 5079 | * disabled then any load that consumer requested no longer counts |
| 5080 | * toward the total requested load. If the regulator is re-enabled |
| 5081 | * then the previously requested load will start counting again. |
| 5082 | * |
| 5083 | * If a regulator is an always-on regulator then an individual consumer's |
| 5084 | * load will still be removed if that consumer is fully disabled. |
| 5085 | * |
| 5086 | * Return: 0 on success or a negative error number on failure. |
| 5087 | */ |
| 5088 | int regulator_set_load(struct regulator *regulator, int uA_load) |
| 5089 | { |
| 5090 | struct regulator_dev *rdev = regulator->rdev; |
| 5091 | int old_uA_load; |
| 5092 | int ret = 0; |
| 5093 | |
| 5094 | regulator_lock(rdev); |
| 5095 | old_uA_load = regulator->uA_load; |
| 5096 | regulator->uA_load = uA_load; |
| 5097 | if (regulator->enable_count && old_uA_load != uA_load) { |
| 5098 | ret = drms_uA_update(rdev); |
| 5099 | if (ret < 0) |
| 5100 | regulator->uA_load = old_uA_load; |
| 5101 | } |
| 5102 | regulator_unlock(rdev); |
| 5103 | |
| 5104 | return ret; |
| 5105 | } |
| 5106 | EXPORT_SYMBOL_GPL(regulator_set_load); |
| 5107 | |
| 5108 | /** |
| 5109 | * regulator_allow_bypass - allow the regulator to go into bypass mode |
| 5110 | * |
| 5111 | * @regulator: Regulator to configure |
| 5112 | * @enable: enable or disable bypass mode |
| 5113 | * |
| 5114 | * Allow the regulator to go into bypass mode if all other consumers |
| 5115 | * for the regulator also enable bypass mode and the machine |
| 5116 | * constraints allow this. Bypass mode means that the regulator is |
| 5117 | * simply passing the input directly to the output with no regulation. |
| 5118 | * |
| 5119 | * Return: 0 on success or if changing bypass is not possible, or |
| 5120 | * a negative error number on failure. |
| 5121 | */ |
| 5122 | int regulator_allow_bypass(struct regulator *regulator, bool enable) |
| 5123 | { |
| 5124 | struct regulator_dev *rdev = regulator->rdev; |
| 5125 | const char *name = rdev_get_name(rdev); |
| 5126 | int ret = 0; |
| 5127 | |
| 5128 | if (!rdev->desc->ops->set_bypass) |
| 5129 | return 0; |
| 5130 | |
| 5131 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS)) |
| 5132 | return 0; |
| 5133 | |
| 5134 | regulator_lock(rdev); |
| 5135 | |
| 5136 | if (enable && !regulator->bypass) { |
| 5137 | rdev->bypass_count++; |
| 5138 | |
| 5139 | if (rdev->bypass_count == rdev->open_count) { |
| 5140 | trace_regulator_bypass_enable(name); |
| 5141 | |
| 5142 | ret = rdev->desc->ops->set_bypass(rdev, enable); |
| 5143 | if (ret != 0) |
| 5144 | rdev->bypass_count--; |
| 5145 | else |
| 5146 | trace_regulator_bypass_enable_complete(name); |
| 5147 | } |
| 5148 | |
| 5149 | } else if (!enable && regulator->bypass) { |
| 5150 | rdev->bypass_count--; |
| 5151 | |
| 5152 | if (rdev->bypass_count != rdev->open_count) { |
| 5153 | trace_regulator_bypass_disable(name); |
| 5154 | |
| 5155 | ret = rdev->desc->ops->set_bypass(rdev, enable); |
| 5156 | if (ret != 0) |
| 5157 | rdev->bypass_count++; |
| 5158 | else |
| 5159 | trace_regulator_bypass_disable_complete(name); |
| 5160 | } |
| 5161 | } |
| 5162 | |
| 5163 | if (ret == 0) |
| 5164 | regulator->bypass = enable; |
| 5165 | |
| 5166 | regulator_unlock(rdev); |
| 5167 | |
| 5168 | return ret; |
| 5169 | } |
| 5170 | EXPORT_SYMBOL_GPL(regulator_allow_bypass); |
| 5171 | |
| 5172 | /** |
| 5173 | * regulator_register_notifier - register regulator event notifier |
| 5174 | * @regulator: regulator source |
| 5175 | * @nb: notifier block |
| 5176 | * |
| 5177 | * Register notifier block to receive regulator events. |
| 5178 | * |
| 5179 | * Return: 0 on success or a negative error number on failure. |
| 5180 | */ |
| 5181 | int regulator_register_notifier(struct regulator *regulator, |
| 5182 | struct notifier_block *nb) |
| 5183 | { |
| 5184 | return blocking_notifier_chain_register(nh: ®ulator->rdev->notifier, |
| 5185 | nb); |
| 5186 | } |
| 5187 | EXPORT_SYMBOL_GPL(regulator_register_notifier); |
| 5188 | |
| 5189 | /** |
| 5190 | * regulator_unregister_notifier - unregister regulator event notifier |
| 5191 | * @regulator: regulator source |
| 5192 | * @nb: notifier block |
| 5193 | * |
| 5194 | * Unregister regulator event notifier block. |
| 5195 | * |
| 5196 | * Return: 0 on success or a negative error number on failure. |
| 5197 | */ |
| 5198 | int regulator_unregister_notifier(struct regulator *regulator, |
| 5199 | struct notifier_block *nb) |
| 5200 | { |
| 5201 | return blocking_notifier_chain_unregister(nh: ®ulator->rdev->notifier, |
| 5202 | nb); |
| 5203 | } |
| 5204 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); |
| 5205 | |
| 5206 | /* notify regulator consumers and downstream regulator consumers. |
| 5207 | * Note mutex must be held by caller. |
| 5208 | */ |
| 5209 | static int _notifier_call_chain(struct regulator_dev *rdev, |
| 5210 | unsigned long event, void *data) |
| 5211 | { |
| 5212 | /* call rdev chain first */ |
| 5213 | int ret = blocking_notifier_call_chain(nh: &rdev->notifier, val: event, v: data); |
| 5214 | |
| 5215 | if (IS_REACHABLE(CONFIG_REGULATOR_NETLINK_EVENTS)) { |
| 5216 | struct device *parent = rdev->dev.parent; |
| 5217 | const char *rname = rdev_get_name(rdev); |
| 5218 | char name[32]; |
| 5219 | |
| 5220 | /* Avoid duplicate debugfs directory names */ |
| 5221 | if (parent && rname == rdev->desc->name) { |
| 5222 | snprintf(buf: name, size: sizeof(name), fmt: "%s-%s" , dev_name(dev: parent), |
| 5223 | rname); |
| 5224 | rname = name; |
| 5225 | } |
| 5226 | reg_generate_netlink_event(reg_name: rname, event); |
| 5227 | } |
| 5228 | |
| 5229 | return ret; |
| 5230 | } |
| 5231 | |
| 5232 | int _regulator_bulk_get(struct device *dev, int num_consumers, |
| 5233 | struct regulator_bulk_data *consumers, enum regulator_get_type get_type) |
| 5234 | { |
| 5235 | int i; |
| 5236 | int ret; |
| 5237 | |
| 5238 | for (i = 0; i < num_consumers; i++) |
| 5239 | consumers[i].consumer = NULL; |
| 5240 | |
| 5241 | for (i = 0; i < num_consumers; i++) { |
| 5242 | consumers[i].consumer = _regulator_get(dev, |
| 5243 | id: consumers[i].supply, get_type); |
| 5244 | if (IS_ERR(ptr: consumers[i].consumer)) { |
| 5245 | ret = dev_err_probe(dev, err: PTR_ERR(ptr: consumers[i].consumer), |
| 5246 | fmt: "Failed to get supply '%s'\n" , |
| 5247 | consumers[i].supply); |
| 5248 | consumers[i].consumer = NULL; |
| 5249 | goto err; |
| 5250 | } |
| 5251 | |
| 5252 | if (consumers[i].init_load_uA > 0) { |
| 5253 | ret = regulator_set_load(consumers[i].consumer, |
| 5254 | consumers[i].init_load_uA); |
| 5255 | if (ret) { |
| 5256 | i++; |
| 5257 | goto err; |
| 5258 | } |
| 5259 | } |
| 5260 | } |
| 5261 | |
| 5262 | return 0; |
| 5263 | |
| 5264 | err: |
| 5265 | while (--i >= 0) |
| 5266 | regulator_put(consumers[i].consumer); |
| 5267 | |
| 5268 | return ret; |
| 5269 | } |
| 5270 | |
| 5271 | /** |
| 5272 | * regulator_bulk_get - get multiple regulator consumers |
| 5273 | * |
| 5274 | * @dev: Device to supply |
| 5275 | * @num_consumers: Number of consumers to register |
| 5276 | * @consumers: Configuration of consumers; clients are stored here. |
| 5277 | * |
| 5278 | * This helper function allows drivers to get several regulator |
| 5279 | * consumers in one operation. If any of the regulators cannot be |
| 5280 | * acquired then any regulators that were allocated will be freed |
| 5281 | * before returning to the caller. |
| 5282 | * |
| 5283 | * Return: 0 on success or a negative error number on failure. |
| 5284 | */ |
| 5285 | int regulator_bulk_get(struct device *dev, int num_consumers, |
| 5286 | struct regulator_bulk_data *consumers) |
| 5287 | { |
| 5288 | return _regulator_bulk_get(dev, num_consumers, consumers, get_type: NORMAL_GET); |
| 5289 | } |
| 5290 | EXPORT_SYMBOL_GPL(regulator_bulk_get); |
| 5291 | |
| 5292 | static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) |
| 5293 | { |
| 5294 | struct regulator_bulk_data *bulk = data; |
| 5295 | |
| 5296 | bulk->ret = regulator_enable(bulk->consumer); |
| 5297 | } |
| 5298 | |
| 5299 | /** |
| 5300 | * regulator_bulk_enable - enable multiple regulator consumers |
| 5301 | * |
| 5302 | * @num_consumers: Number of consumers |
| 5303 | * @consumers: Consumer data; clients are stored here. |
| 5304 | * |
| 5305 | * This convenience API allows consumers to enable multiple regulator |
| 5306 | * clients in a single API call. If any consumers cannot be enabled |
| 5307 | * then any others that were enabled will be disabled again prior to |
| 5308 | * return. |
| 5309 | * |
| 5310 | * Return: 0 on success or a negative error number on failure. |
| 5311 | */ |
| 5312 | int regulator_bulk_enable(int num_consumers, |
| 5313 | struct regulator_bulk_data *consumers) |
| 5314 | { |
| 5315 | ASYNC_DOMAIN_EXCLUSIVE(async_domain); |
| 5316 | int i; |
| 5317 | int ret = 0; |
| 5318 | |
| 5319 | for (i = 0; i < num_consumers; i++) { |
| 5320 | async_schedule_domain(func: regulator_bulk_enable_async, |
| 5321 | data: &consumers[i], domain: &async_domain); |
| 5322 | } |
| 5323 | |
| 5324 | async_synchronize_full_domain(domain: &async_domain); |
| 5325 | |
| 5326 | /* If any consumer failed we need to unwind any that succeeded */ |
| 5327 | for (i = 0; i < num_consumers; i++) { |
| 5328 | if (consumers[i].ret != 0) { |
| 5329 | ret = consumers[i].ret; |
| 5330 | goto err; |
| 5331 | } |
| 5332 | } |
| 5333 | |
| 5334 | return 0; |
| 5335 | |
| 5336 | err: |
| 5337 | for (i = 0; i < num_consumers; i++) { |
| 5338 | if (consumers[i].ret < 0) |
| 5339 | pr_err("Failed to enable %s: %pe\n" , consumers[i].supply, |
| 5340 | ERR_PTR(consumers[i].ret)); |
| 5341 | else |
| 5342 | regulator_disable(consumers[i].consumer); |
| 5343 | } |
| 5344 | |
| 5345 | return ret; |
| 5346 | } |
| 5347 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); |
| 5348 | |
| 5349 | /** |
| 5350 | * regulator_bulk_disable - disable multiple regulator consumers |
| 5351 | * |
| 5352 | * @num_consumers: Number of consumers |
| 5353 | * @consumers: Consumer data; clients are stored here. |
| 5354 | * |
| 5355 | * This convenience API allows consumers to disable multiple regulator |
| 5356 | * clients in a single API call. If any consumers cannot be disabled |
| 5357 | * then any others that were disabled will be enabled again prior to |
| 5358 | * return. |
| 5359 | * |
| 5360 | * Return: 0 on success or a negative error number on failure. |
| 5361 | */ |
| 5362 | int regulator_bulk_disable(int num_consumers, |
| 5363 | struct regulator_bulk_data *consumers) |
| 5364 | { |
| 5365 | int i; |
| 5366 | int ret, r; |
| 5367 | |
| 5368 | for (i = num_consumers - 1; i >= 0; --i) { |
| 5369 | ret = regulator_disable(consumers[i].consumer); |
| 5370 | if (ret != 0) |
| 5371 | goto err; |
| 5372 | } |
| 5373 | |
| 5374 | return 0; |
| 5375 | |
| 5376 | err: |
| 5377 | pr_err("Failed to disable %s: %pe\n" , consumers[i].supply, ERR_PTR(ret)); |
| 5378 | for (++i; i < num_consumers; ++i) { |
| 5379 | r = regulator_enable(consumers[i].consumer); |
| 5380 | if (r != 0) |
| 5381 | pr_err("Failed to re-enable %s: %pe\n" , |
| 5382 | consumers[i].supply, ERR_PTR(r)); |
| 5383 | } |
| 5384 | |
| 5385 | return ret; |
| 5386 | } |
| 5387 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); |
| 5388 | |
| 5389 | /** |
| 5390 | * regulator_bulk_force_disable - force disable multiple regulator consumers |
| 5391 | * |
| 5392 | * @num_consumers: Number of consumers |
| 5393 | * @consumers: Consumer data; clients are stored here. |
| 5394 | * |
| 5395 | * This convenience API allows consumers to forcibly disable multiple regulator |
| 5396 | * clients in a single API call. |
| 5397 | * NOTE: This should be used for situations when device damage will |
| 5398 | * likely occur if the regulators are not disabled (e.g. over temp). |
| 5399 | * Although regulator_force_disable function call for some consumers can |
| 5400 | * return error numbers, the function is called for all consumers. |
| 5401 | * |
| 5402 | * Return: 0 on success or a negative error number on failure. |
| 5403 | */ |
| 5404 | int regulator_bulk_force_disable(int num_consumers, |
| 5405 | struct regulator_bulk_data *consumers) |
| 5406 | { |
| 5407 | int i; |
| 5408 | int ret = 0; |
| 5409 | |
| 5410 | for (i = 0; i < num_consumers; i++) { |
| 5411 | consumers[i].ret = |
| 5412 | regulator_force_disable(consumers[i].consumer); |
| 5413 | |
| 5414 | /* Store first error for reporting */ |
| 5415 | if (consumers[i].ret && !ret) |
| 5416 | ret = consumers[i].ret; |
| 5417 | } |
| 5418 | |
| 5419 | return ret; |
| 5420 | } |
| 5421 | EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); |
| 5422 | |
| 5423 | /** |
| 5424 | * regulator_bulk_free - free multiple regulator consumers |
| 5425 | * |
| 5426 | * @num_consumers: Number of consumers |
| 5427 | * @consumers: Consumer data; clients are stored here. |
| 5428 | * |
| 5429 | * This convenience API allows consumers to free multiple regulator |
| 5430 | * clients in a single API call. |
| 5431 | */ |
| 5432 | void regulator_bulk_free(int num_consumers, |
| 5433 | struct regulator_bulk_data *consumers) |
| 5434 | { |
| 5435 | int i; |
| 5436 | |
| 5437 | for (i = 0; i < num_consumers; i++) { |
| 5438 | regulator_put(consumers[i].consumer); |
| 5439 | consumers[i].consumer = NULL; |
| 5440 | } |
| 5441 | } |
| 5442 | EXPORT_SYMBOL_GPL(regulator_bulk_free); |
| 5443 | |
| 5444 | /** |
| 5445 | * regulator_handle_critical - Handle events for system-critical regulators. |
| 5446 | * @rdev: The regulator device. |
| 5447 | * @event: The event being handled. |
| 5448 | * |
| 5449 | * This function handles critical events such as under-voltage, over-current, |
| 5450 | * and unknown errors for regulators deemed system-critical. On detecting such |
| 5451 | * events, it triggers a hardware protection shutdown with a defined timeout. |
| 5452 | */ |
| 5453 | static void regulator_handle_critical(struct regulator_dev *rdev, |
| 5454 | unsigned long event) |
| 5455 | { |
| 5456 | const char *reason = NULL; |
| 5457 | |
| 5458 | if (!rdev->constraints->system_critical) |
| 5459 | return; |
| 5460 | |
| 5461 | switch (event) { |
| 5462 | case REGULATOR_EVENT_UNDER_VOLTAGE: |
| 5463 | reason = "System critical regulator: voltage drop detected" ; |
| 5464 | break; |
| 5465 | case REGULATOR_EVENT_OVER_CURRENT: |
| 5466 | reason = "System critical regulator: over-current detected" ; |
| 5467 | break; |
| 5468 | case REGULATOR_EVENT_FAIL: |
| 5469 | reason = "System critical regulator: unknown error" ; |
| 5470 | } |
| 5471 | |
| 5472 | if (!reason) |
| 5473 | return; |
| 5474 | |
| 5475 | hw_protection_trigger(reason, |
| 5476 | ms_until_forced: rdev->constraints->uv_less_critical_window_ms); |
| 5477 | } |
| 5478 | |
| 5479 | /** |
| 5480 | * regulator_notifier_call_chain - call regulator event notifier |
| 5481 | * @rdev: regulator source |
| 5482 | * @event: notifier block |
| 5483 | * @data: callback-specific data. |
| 5484 | * |
| 5485 | * Called by regulator drivers to notify clients a regulator event has |
| 5486 | * occurred. |
| 5487 | * |
| 5488 | * Return: %NOTIFY_DONE. |
| 5489 | */ |
| 5490 | int regulator_notifier_call_chain(struct regulator_dev *rdev, |
| 5491 | unsigned long event, void *data) |
| 5492 | { |
| 5493 | regulator_handle_critical(rdev, event); |
| 5494 | |
| 5495 | _notifier_call_chain(rdev, event, data); |
| 5496 | return NOTIFY_DONE; |
| 5497 | |
| 5498 | } |
| 5499 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); |
| 5500 | |
| 5501 | /** |
| 5502 | * regulator_mode_to_status - convert a regulator mode into a status |
| 5503 | * |
| 5504 | * @mode: Mode to convert |
| 5505 | * |
| 5506 | * Convert a regulator mode into a status. |
| 5507 | * |
| 5508 | * Return: %REGULATOR_STATUS_* value corresponding to given mode. |
| 5509 | */ |
| 5510 | int regulator_mode_to_status(unsigned int mode) |
| 5511 | { |
| 5512 | switch (mode) { |
| 5513 | case REGULATOR_MODE_FAST: |
| 5514 | return REGULATOR_STATUS_FAST; |
| 5515 | case REGULATOR_MODE_NORMAL: |
| 5516 | return REGULATOR_STATUS_NORMAL; |
| 5517 | case REGULATOR_MODE_IDLE: |
| 5518 | return REGULATOR_STATUS_IDLE; |
| 5519 | case REGULATOR_MODE_STANDBY: |
| 5520 | return REGULATOR_STATUS_STANDBY; |
| 5521 | default: |
| 5522 | return REGULATOR_STATUS_UNDEFINED; |
| 5523 | } |
| 5524 | } |
| 5525 | EXPORT_SYMBOL_GPL(regulator_mode_to_status); |
| 5526 | |
| 5527 | static struct attribute *regulator_dev_attrs[] = { |
| 5528 | &dev_attr_name.attr, |
| 5529 | &dev_attr_num_users.attr, |
| 5530 | &dev_attr_type.attr, |
| 5531 | &dev_attr_microvolts.attr, |
| 5532 | &dev_attr_microamps.attr, |
| 5533 | &dev_attr_opmode.attr, |
| 5534 | &dev_attr_state.attr, |
| 5535 | &dev_attr_status.attr, |
| 5536 | &dev_attr_bypass.attr, |
| 5537 | &dev_attr_requested_microamps.attr, |
| 5538 | &dev_attr_min_microvolts.attr, |
| 5539 | &dev_attr_max_microvolts.attr, |
| 5540 | &dev_attr_min_microamps.attr, |
| 5541 | &dev_attr_max_microamps.attr, |
| 5542 | &dev_attr_under_voltage.attr, |
| 5543 | &dev_attr_over_current.attr, |
| 5544 | &dev_attr_regulation_out.attr, |
| 5545 | &dev_attr_fail.attr, |
| 5546 | &dev_attr_over_temp.attr, |
| 5547 | &dev_attr_under_voltage_warn.attr, |
| 5548 | &dev_attr_over_current_warn.attr, |
| 5549 | &dev_attr_over_voltage_warn.attr, |
| 5550 | &dev_attr_over_temp_warn.attr, |
| 5551 | &dev_attr_suspend_standby_state.attr, |
| 5552 | &dev_attr_suspend_mem_state.attr, |
| 5553 | &dev_attr_suspend_disk_state.attr, |
| 5554 | &dev_attr_suspend_standby_microvolts.attr, |
| 5555 | &dev_attr_suspend_mem_microvolts.attr, |
| 5556 | &dev_attr_suspend_disk_microvolts.attr, |
| 5557 | &dev_attr_suspend_standby_mode.attr, |
| 5558 | &dev_attr_suspend_mem_mode.attr, |
| 5559 | &dev_attr_suspend_disk_mode.attr, |
| 5560 | &dev_attr_power_budget_milliwatt.attr, |
| 5561 | &dev_attr_power_requested_milliwatt.attr, |
| 5562 | NULL |
| 5563 | }; |
| 5564 | |
| 5565 | /* |
| 5566 | * To avoid cluttering sysfs (and memory) with useless state, only |
| 5567 | * create attributes that can be meaningfully displayed. |
| 5568 | */ |
| 5569 | static umode_t regulator_attr_is_visible(struct kobject *kobj, |
| 5570 | struct attribute *attr, int idx) |
| 5571 | { |
| 5572 | struct device *dev = kobj_to_dev(kobj); |
| 5573 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 5574 | const struct regulator_ops *ops = rdev->desc->ops; |
| 5575 | umode_t mode = attr->mode; |
| 5576 | |
| 5577 | /* these three are always present */ |
| 5578 | if (attr == &dev_attr_name.attr || |
| 5579 | attr == &dev_attr_num_users.attr || |
| 5580 | attr == &dev_attr_type.attr) |
| 5581 | return mode; |
| 5582 | |
| 5583 | /* some attributes need specific methods to be displayed */ |
| 5584 | if (attr == &dev_attr_microvolts.attr) { |
| 5585 | if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || |
| 5586 | (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || |
| 5587 | (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) || |
| 5588 | (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1)) |
| 5589 | return mode; |
| 5590 | return 0; |
| 5591 | } |
| 5592 | |
| 5593 | if (attr == &dev_attr_microamps.attr) |
| 5594 | return ops->get_current_limit ? mode : 0; |
| 5595 | |
| 5596 | if (attr == &dev_attr_opmode.attr) |
| 5597 | return ops->get_mode ? mode : 0; |
| 5598 | |
| 5599 | if (attr == &dev_attr_state.attr) |
| 5600 | return (rdev->ena_pin || ops->is_enabled) ? mode : 0; |
| 5601 | |
| 5602 | if (attr == &dev_attr_status.attr) |
| 5603 | return ops->get_status ? mode : 0; |
| 5604 | |
| 5605 | if (attr == &dev_attr_bypass.attr) |
| 5606 | return ops->get_bypass ? mode : 0; |
| 5607 | |
| 5608 | if (attr == &dev_attr_under_voltage.attr || |
| 5609 | attr == &dev_attr_over_current.attr || |
| 5610 | attr == &dev_attr_regulation_out.attr || |
| 5611 | attr == &dev_attr_fail.attr || |
| 5612 | attr == &dev_attr_over_temp.attr || |
| 5613 | attr == &dev_attr_under_voltage_warn.attr || |
| 5614 | attr == &dev_attr_over_current_warn.attr || |
| 5615 | attr == &dev_attr_over_voltage_warn.attr || |
| 5616 | attr == &dev_attr_over_temp_warn.attr) |
| 5617 | return ops->get_error_flags ? mode : 0; |
| 5618 | |
| 5619 | /* constraints need specific supporting methods */ |
| 5620 | if (attr == &dev_attr_min_microvolts.attr || |
| 5621 | attr == &dev_attr_max_microvolts.attr) |
| 5622 | return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0; |
| 5623 | |
| 5624 | if (attr == &dev_attr_min_microamps.attr || |
| 5625 | attr == &dev_attr_max_microamps.attr) |
| 5626 | return ops->set_current_limit ? mode : 0; |
| 5627 | |
| 5628 | if (attr == &dev_attr_suspend_standby_state.attr || |
| 5629 | attr == &dev_attr_suspend_mem_state.attr || |
| 5630 | attr == &dev_attr_suspend_disk_state.attr) |
| 5631 | return mode; |
| 5632 | |
| 5633 | if (attr == &dev_attr_suspend_standby_microvolts.attr || |
| 5634 | attr == &dev_attr_suspend_mem_microvolts.attr || |
| 5635 | attr == &dev_attr_suspend_disk_microvolts.attr) |
| 5636 | return ops->set_suspend_voltage ? mode : 0; |
| 5637 | |
| 5638 | if (attr == &dev_attr_suspend_standby_mode.attr || |
| 5639 | attr == &dev_attr_suspend_mem_mode.attr || |
| 5640 | attr == &dev_attr_suspend_disk_mode.attr) |
| 5641 | return ops->set_suspend_mode ? mode : 0; |
| 5642 | |
| 5643 | if (attr == &dev_attr_power_budget_milliwatt.attr || |
| 5644 | attr == &dev_attr_power_requested_milliwatt.attr) |
| 5645 | return rdev->constraints->pw_budget_mW != INT_MAX ? mode : 0; |
| 5646 | |
| 5647 | return mode; |
| 5648 | } |
| 5649 | |
| 5650 | static const struct attribute_group regulator_dev_group = { |
| 5651 | .attrs = regulator_dev_attrs, |
| 5652 | .is_visible = regulator_attr_is_visible, |
| 5653 | }; |
| 5654 | |
| 5655 | static const struct attribute_group *regulator_dev_groups[] = { |
| 5656 | ®ulator_dev_group, |
| 5657 | NULL |
| 5658 | }; |
| 5659 | |
| 5660 | static void regulator_dev_release(struct device *dev) |
| 5661 | { |
| 5662 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 5663 | |
| 5664 | debugfs_remove_recursive(dentry: rdev->debugfs); |
| 5665 | kfree(objp: rdev->constraints); |
| 5666 | of_node_put(node: rdev->dev.of_node); |
| 5667 | kfree(objp: rdev); |
| 5668 | } |
| 5669 | |
| 5670 | static void rdev_init_debugfs(struct regulator_dev *rdev) |
| 5671 | { |
| 5672 | struct device *parent = rdev->dev.parent; |
| 5673 | const char *rname = rdev_get_name(rdev); |
| 5674 | char name[NAME_MAX]; |
| 5675 | |
| 5676 | /* Avoid duplicate debugfs directory names */ |
| 5677 | if (parent && rname == rdev->desc->name) { |
| 5678 | snprintf(buf: name, size: sizeof(name), fmt: "%s-%s" , dev_name(dev: parent), |
| 5679 | rname); |
| 5680 | rname = name; |
| 5681 | } |
| 5682 | |
| 5683 | rdev->debugfs = debugfs_create_dir(name: rname, parent: debugfs_root); |
| 5684 | if (IS_ERR(ptr: rdev->debugfs)) |
| 5685 | rdev_dbg(rdev, "Failed to create debugfs directory\n" ); |
| 5686 | |
| 5687 | debugfs_create_u32(name: "use_count" , mode: 0444, parent: rdev->debugfs, |
| 5688 | value: &rdev->use_count); |
| 5689 | debugfs_create_u32(name: "open_count" , mode: 0444, parent: rdev->debugfs, |
| 5690 | value: &rdev->open_count); |
| 5691 | debugfs_create_u32(name: "bypass_count" , mode: 0444, parent: rdev->debugfs, |
| 5692 | value: &rdev->bypass_count); |
| 5693 | } |
| 5694 | |
| 5695 | static int regulator_register_resolve_supply(struct device *dev, void *data) |
| 5696 | { |
| 5697 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 5698 | |
| 5699 | if (regulator_resolve_supply(rdev)) |
| 5700 | rdev_dbg(rdev, "unable to resolve supply\n" ); |
| 5701 | |
| 5702 | return 0; |
| 5703 | } |
| 5704 | |
| 5705 | int regulator_coupler_register(struct regulator_coupler *coupler) |
| 5706 | { |
| 5707 | mutex_lock(®ulator_list_mutex); |
| 5708 | list_add_tail(new: &coupler->list, head: ®ulator_coupler_list); |
| 5709 | mutex_unlock(lock: ®ulator_list_mutex); |
| 5710 | |
| 5711 | return 0; |
| 5712 | } |
| 5713 | |
| 5714 | static struct regulator_coupler * |
| 5715 | regulator_find_coupler(struct regulator_dev *rdev) |
| 5716 | { |
| 5717 | struct regulator_coupler *coupler; |
| 5718 | int err; |
| 5719 | |
| 5720 | /* |
| 5721 | * Note that regulators are appended to the list and the generic |
| 5722 | * coupler is registered first, hence it will be attached at last |
| 5723 | * if nobody cared. |
| 5724 | */ |
| 5725 | list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) { |
| 5726 | err = coupler->attach_regulator(coupler, rdev); |
| 5727 | if (!err) { |
| 5728 | if (!coupler->balance_voltage && |
| 5729 | rdev->coupling_desc.n_coupled > 2) |
| 5730 | goto err_unsupported; |
| 5731 | |
| 5732 | return coupler; |
| 5733 | } |
| 5734 | |
| 5735 | if (err < 0) |
| 5736 | return ERR_PTR(error: err); |
| 5737 | |
| 5738 | if (err == 1) |
| 5739 | continue; |
| 5740 | |
| 5741 | break; |
| 5742 | } |
| 5743 | |
| 5744 | return ERR_PTR(error: -EINVAL); |
| 5745 | |
| 5746 | err_unsupported: |
| 5747 | if (coupler->detach_regulator) |
| 5748 | coupler->detach_regulator(coupler, rdev); |
| 5749 | |
| 5750 | rdev_err(rdev, |
| 5751 | "Voltage balancing for multiple regulator couples is unimplemented\n" ); |
| 5752 | |
| 5753 | return ERR_PTR(error: -EPERM); |
| 5754 | } |
| 5755 | |
| 5756 | static void regulator_resolve_coupling(struct regulator_dev *rdev) |
| 5757 | { |
| 5758 | struct regulator_coupler *coupler = rdev->coupling_desc.coupler; |
| 5759 | struct coupling_desc *c_desc = &rdev->coupling_desc; |
| 5760 | int n_coupled = c_desc->n_coupled; |
| 5761 | struct regulator_dev *c_rdev; |
| 5762 | int i; |
| 5763 | |
| 5764 | for (i = 1; i < n_coupled; i++) { |
| 5765 | /* already resolved */ |
| 5766 | if (c_desc->coupled_rdevs[i]) |
| 5767 | continue; |
| 5768 | |
| 5769 | c_rdev = of_parse_coupled_regulator(rdev, index: i - 1); |
| 5770 | |
| 5771 | if (!c_rdev) |
| 5772 | continue; |
| 5773 | |
| 5774 | if (c_rdev->coupling_desc.coupler != coupler) { |
| 5775 | rdev_err(rdev, "coupler mismatch with %s\n" , |
| 5776 | rdev_get_name(c_rdev)); |
| 5777 | return; |
| 5778 | } |
| 5779 | |
| 5780 | c_desc->coupled_rdevs[i] = c_rdev; |
| 5781 | c_desc->n_resolved++; |
| 5782 | |
| 5783 | regulator_resolve_coupling(rdev: c_rdev); |
| 5784 | } |
| 5785 | } |
| 5786 | |
| 5787 | static void regulator_remove_coupling(struct regulator_dev *rdev) |
| 5788 | { |
| 5789 | struct regulator_coupler *coupler = rdev->coupling_desc.coupler; |
| 5790 | struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc; |
| 5791 | struct regulator_dev *__c_rdev, *c_rdev; |
| 5792 | unsigned int __n_coupled, n_coupled; |
| 5793 | int i, k; |
| 5794 | int err; |
| 5795 | |
| 5796 | n_coupled = c_desc->n_coupled; |
| 5797 | |
| 5798 | for (i = 1; i < n_coupled; i++) { |
| 5799 | c_rdev = c_desc->coupled_rdevs[i]; |
| 5800 | |
| 5801 | if (!c_rdev) |
| 5802 | continue; |
| 5803 | |
| 5804 | regulator_lock(rdev: c_rdev); |
| 5805 | |
| 5806 | __c_desc = &c_rdev->coupling_desc; |
| 5807 | __n_coupled = __c_desc->n_coupled; |
| 5808 | |
| 5809 | for (k = 1; k < __n_coupled; k++) { |
| 5810 | __c_rdev = __c_desc->coupled_rdevs[k]; |
| 5811 | |
| 5812 | if (__c_rdev == rdev) { |
| 5813 | __c_desc->coupled_rdevs[k] = NULL; |
| 5814 | __c_desc->n_resolved--; |
| 5815 | break; |
| 5816 | } |
| 5817 | } |
| 5818 | |
| 5819 | regulator_unlock(rdev: c_rdev); |
| 5820 | |
| 5821 | c_desc->coupled_rdevs[i] = NULL; |
| 5822 | c_desc->n_resolved--; |
| 5823 | } |
| 5824 | |
| 5825 | if (coupler && coupler->detach_regulator) { |
| 5826 | err = coupler->detach_regulator(coupler, rdev); |
| 5827 | if (err) |
| 5828 | rdev_err(rdev, "failed to detach from coupler: %pe\n" , |
| 5829 | ERR_PTR(err)); |
| 5830 | } |
| 5831 | |
| 5832 | rdev->coupling_desc.n_coupled = 0; |
| 5833 | kfree(objp: rdev->coupling_desc.coupled_rdevs); |
| 5834 | rdev->coupling_desc.coupled_rdevs = NULL; |
| 5835 | } |
| 5836 | |
| 5837 | static int regulator_init_coupling(struct regulator_dev *rdev) |
| 5838 | { |
| 5839 | struct regulator_dev **coupled; |
| 5840 | int err, n_phandles; |
| 5841 | |
| 5842 | if (!IS_ENABLED(CONFIG_OF)) |
| 5843 | n_phandles = 0; |
| 5844 | else |
| 5845 | n_phandles = of_get_n_coupled(rdev); |
| 5846 | |
| 5847 | coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL); |
| 5848 | if (!coupled) |
| 5849 | return -ENOMEM; |
| 5850 | |
| 5851 | rdev->coupling_desc.coupled_rdevs = coupled; |
| 5852 | |
| 5853 | /* |
| 5854 | * Every regulator should always have coupling descriptor filled with |
| 5855 | * at least pointer to itself. |
| 5856 | */ |
| 5857 | rdev->coupling_desc.coupled_rdevs[0] = rdev; |
| 5858 | rdev->coupling_desc.n_coupled = n_phandles + 1; |
| 5859 | rdev->coupling_desc.n_resolved++; |
| 5860 | |
| 5861 | /* regulator isn't coupled */ |
| 5862 | if (n_phandles == 0) |
| 5863 | return 0; |
| 5864 | |
| 5865 | if (!of_check_coupling_data(rdev)) |
| 5866 | return -EPERM; |
| 5867 | |
| 5868 | mutex_lock(®ulator_list_mutex); |
| 5869 | rdev->coupling_desc.coupler = regulator_find_coupler(rdev); |
| 5870 | mutex_unlock(lock: ®ulator_list_mutex); |
| 5871 | |
| 5872 | if (IS_ERR(ptr: rdev->coupling_desc.coupler)) { |
| 5873 | err = PTR_ERR(ptr: rdev->coupling_desc.coupler); |
| 5874 | rdev_err(rdev, "failed to get coupler: %pe\n" , ERR_PTR(err)); |
| 5875 | return err; |
| 5876 | } |
| 5877 | |
| 5878 | return 0; |
| 5879 | } |
| 5880 | |
| 5881 | static int generic_coupler_attach(struct regulator_coupler *coupler, |
| 5882 | struct regulator_dev *rdev) |
| 5883 | { |
| 5884 | if (rdev->coupling_desc.n_coupled > 2) { |
| 5885 | rdev_err(rdev, |
| 5886 | "Voltage balancing for multiple regulator couples is unimplemented\n" ); |
| 5887 | return -EPERM; |
| 5888 | } |
| 5889 | |
| 5890 | if (!rdev->constraints->always_on) { |
| 5891 | rdev_err(rdev, |
| 5892 | "Coupling of a non always-on regulator is unimplemented\n" ); |
| 5893 | return -ENOTSUPP; |
| 5894 | } |
| 5895 | |
| 5896 | return 0; |
| 5897 | } |
| 5898 | |
| 5899 | static struct regulator_coupler generic_regulator_coupler = { |
| 5900 | .attach_regulator = generic_coupler_attach, |
| 5901 | }; |
| 5902 | |
| 5903 | /** |
| 5904 | * regulator_register - register regulator |
| 5905 | * @dev: the device that drive the regulator |
| 5906 | * @regulator_desc: regulator to register |
| 5907 | * @cfg: runtime configuration for regulator |
| 5908 | * |
| 5909 | * Called by regulator drivers to register a regulator. |
| 5910 | * |
| 5911 | * Return: Pointer to a valid &struct regulator_dev on success or |
| 5912 | * an ERR_PTR() encoded negative error number on failure. |
| 5913 | */ |
| 5914 | struct regulator_dev * |
| 5915 | regulator_register(struct device *dev, |
| 5916 | const struct regulator_desc *regulator_desc, |
| 5917 | const struct regulator_config *cfg) |
| 5918 | { |
| 5919 | const struct regulator_init_data *init_data; |
| 5920 | struct regulator_config *config = NULL; |
| 5921 | static atomic_t regulator_no = ATOMIC_INIT(-1); |
| 5922 | struct regulator_dev *rdev; |
| 5923 | bool dangling_cfg_gpiod = false; |
| 5924 | bool dangling_of_gpiod = false; |
| 5925 | int ret, i; |
| 5926 | bool resolved_early = false; |
| 5927 | |
| 5928 | if (cfg == NULL) |
| 5929 | return ERR_PTR(error: -EINVAL); |
| 5930 | if (cfg->ena_gpiod) |
| 5931 | dangling_cfg_gpiod = true; |
| 5932 | if (regulator_desc == NULL) { |
| 5933 | ret = -EINVAL; |
| 5934 | goto rinse; |
| 5935 | } |
| 5936 | |
| 5937 | WARN_ON(!dev || !cfg->dev); |
| 5938 | |
| 5939 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) { |
| 5940 | ret = -EINVAL; |
| 5941 | goto rinse; |
| 5942 | } |
| 5943 | |
| 5944 | if (regulator_desc->type != REGULATOR_VOLTAGE && |
| 5945 | regulator_desc->type != REGULATOR_CURRENT) { |
| 5946 | ret = -EINVAL; |
| 5947 | goto rinse; |
| 5948 | } |
| 5949 | |
| 5950 | /* Only one of each should be implemented */ |
| 5951 | WARN_ON(regulator_desc->ops->get_voltage && |
| 5952 | regulator_desc->ops->get_voltage_sel); |
| 5953 | WARN_ON(regulator_desc->ops->set_voltage && |
| 5954 | regulator_desc->ops->set_voltage_sel); |
| 5955 | |
| 5956 | /* If we're using selectors we must implement list_voltage. */ |
| 5957 | if (regulator_desc->ops->get_voltage_sel && |
| 5958 | !regulator_desc->ops->list_voltage) { |
| 5959 | ret = -EINVAL; |
| 5960 | goto rinse; |
| 5961 | } |
| 5962 | if (regulator_desc->ops->set_voltage_sel && |
| 5963 | !regulator_desc->ops->list_voltage) { |
| 5964 | ret = -EINVAL; |
| 5965 | goto rinse; |
| 5966 | } |
| 5967 | |
| 5968 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
| 5969 | if (rdev == NULL) { |
| 5970 | ret = -ENOMEM; |
| 5971 | goto rinse; |
| 5972 | } |
| 5973 | device_initialize(dev: &rdev->dev); |
| 5974 | dev_set_drvdata(dev: &rdev->dev, data: rdev); |
| 5975 | rdev->dev.class = ®ulator_class; |
| 5976 | spin_lock_init(&rdev->err_lock); |
| 5977 | |
| 5978 | /* |
| 5979 | * Duplicate the config so the driver could override it after |
| 5980 | * parsing init data. |
| 5981 | */ |
| 5982 | config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL); |
| 5983 | if (config == NULL) { |
| 5984 | ret = -ENOMEM; |
| 5985 | goto clean; |
| 5986 | } |
| 5987 | |
| 5988 | /* |
| 5989 | * DT may override the config->init_data provided if the platform |
| 5990 | * needs to do so. If so, config->init_data is completely ignored. |
| 5991 | */ |
| 5992 | init_data = regulator_of_get_init_data(dev, desc: regulator_desc, config, |
| 5993 | node: &rdev->dev.of_node); |
| 5994 | |
| 5995 | /* |
| 5996 | * Sometimes not all resources are probed already so we need to take |
| 5997 | * that into account. This happens most the time if the ena_gpiod comes |
| 5998 | * from a gpio extender or something else. |
| 5999 | */ |
| 6000 | if (PTR_ERR(ptr: init_data) == -EPROBE_DEFER) { |
| 6001 | ret = -EPROBE_DEFER; |
| 6002 | goto clean; |
| 6003 | } |
| 6004 | |
| 6005 | /* |
| 6006 | * We need to keep track of any GPIO descriptor coming from the |
| 6007 | * device tree until we have handled it over to the core. If the |
| 6008 | * config that was passed in to this function DOES NOT contain |
| 6009 | * a descriptor, and the config after this call DOES contain |
| 6010 | * a descriptor, we definitely got one from parsing the device |
| 6011 | * tree. |
| 6012 | */ |
| 6013 | if (!cfg->ena_gpiod && config->ena_gpiod) |
| 6014 | dangling_of_gpiod = true; |
| 6015 | if (!init_data) { |
| 6016 | init_data = config->init_data; |
| 6017 | rdev->dev.of_node = of_node_get(node: config->of_node); |
| 6018 | } |
| 6019 | |
| 6020 | ww_mutex_init(lock: &rdev->mutex, ww_class: ®ulator_ww_class); |
| 6021 | rdev->reg_data = config->driver_data; |
| 6022 | rdev->owner = regulator_desc->owner; |
| 6023 | rdev->desc = regulator_desc; |
| 6024 | if (config->regmap) |
| 6025 | rdev->regmap = config->regmap; |
| 6026 | else if (dev_get_regmap(dev, NULL)) |
| 6027 | rdev->regmap = dev_get_regmap(dev, NULL); |
| 6028 | else if (dev->parent) |
| 6029 | rdev->regmap = dev_get_regmap(dev: dev->parent, NULL); |
| 6030 | INIT_LIST_HEAD(list: &rdev->consumer_list); |
| 6031 | INIT_LIST_HEAD(list: &rdev->list); |
| 6032 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); |
| 6033 | INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); |
| 6034 | |
| 6035 | if (init_data && init_data->supply_regulator) |
| 6036 | rdev->supply_name = init_data->supply_regulator; |
| 6037 | else if (regulator_desc->supply_name) |
| 6038 | rdev->supply_name = regulator_desc->supply_name; |
| 6039 | |
| 6040 | /* register with sysfs */ |
| 6041 | rdev->dev.parent = config->dev; |
| 6042 | dev_set_name(dev: &rdev->dev, name: "regulator.%lu" , |
| 6043 | (unsigned long) atomic_inc_return(v: ®ulator_no)); |
| 6044 | |
| 6045 | /* set regulator constraints */ |
| 6046 | if (init_data) |
| 6047 | rdev->constraints = kmemdup(&init_data->constraints, |
| 6048 | sizeof(*rdev->constraints), |
| 6049 | GFP_KERNEL); |
| 6050 | else |
| 6051 | rdev->constraints = kzalloc(sizeof(*rdev->constraints), |
| 6052 | GFP_KERNEL); |
| 6053 | if (!rdev->constraints) { |
| 6054 | ret = -ENOMEM; |
| 6055 | goto wash; |
| 6056 | } |
| 6057 | |
| 6058 | if (regulator_desc->init_cb) { |
| 6059 | ret = regulator_desc->init_cb(rdev, config); |
| 6060 | if (ret < 0) |
| 6061 | goto wash; |
| 6062 | } |
| 6063 | |
| 6064 | if ((rdev->supply_name && !rdev->supply) && |
| 6065 | (rdev->constraints->always_on || |
| 6066 | rdev->constraints->boot_on)) { |
| 6067 | ret = regulator_resolve_supply(rdev); |
| 6068 | if (ret) |
| 6069 | rdev_dbg(rdev, "unable to resolve supply early: %pe\n" , |
| 6070 | ERR_PTR(ret)); |
| 6071 | |
| 6072 | resolved_early = true; |
| 6073 | } |
| 6074 | |
| 6075 | if (config->ena_gpiod) { |
| 6076 | ret = regulator_ena_gpio_request(rdev, config); |
| 6077 | if (ret != 0) { |
| 6078 | rdev_err(rdev, "Failed to request enable GPIO: %pe\n" , |
| 6079 | ERR_PTR(ret)); |
| 6080 | goto wash; |
| 6081 | } |
| 6082 | /* The regulator core took over the GPIO descriptor */ |
| 6083 | dangling_cfg_gpiod = false; |
| 6084 | dangling_of_gpiod = false; |
| 6085 | } |
| 6086 | |
| 6087 | ret = set_machine_constraints(rdev); |
| 6088 | if (ret == -EPROBE_DEFER && !resolved_early) { |
| 6089 | /* Regulator might be in bypass mode and so needs its supply |
| 6090 | * to set the constraints |
| 6091 | */ |
| 6092 | /* FIXME: this currently triggers a chicken-and-egg problem |
| 6093 | * when creating -SUPPLY symlink in sysfs to a regulator |
| 6094 | * that is just being created |
| 6095 | */ |
| 6096 | rdev_dbg(rdev, "will resolve supply early: %s\n" , |
| 6097 | rdev->supply_name); |
| 6098 | ret = regulator_resolve_supply(rdev); |
| 6099 | if (!ret) |
| 6100 | ret = set_machine_constraints(rdev); |
| 6101 | else |
| 6102 | rdev_dbg(rdev, "unable to resolve supply early: %pe\n" , |
| 6103 | ERR_PTR(ret)); |
| 6104 | } |
| 6105 | if (ret < 0) |
| 6106 | goto wash; |
| 6107 | |
| 6108 | ret = regulator_init_coupling(rdev); |
| 6109 | if (ret < 0) |
| 6110 | goto wash; |
| 6111 | |
| 6112 | /* add consumers devices */ |
| 6113 | if (init_data) { |
| 6114 | for (i = 0; i < init_data->num_consumer_supplies; i++) { |
| 6115 | ret = set_consumer_device_supply(rdev, |
| 6116 | consumer_dev_name: init_data->consumer_supplies[i].dev_name, |
| 6117 | supply: init_data->consumer_supplies[i].supply); |
| 6118 | if (ret < 0) { |
| 6119 | dev_err(dev, "Failed to set supply %s\n" , |
| 6120 | init_data->consumer_supplies[i].supply); |
| 6121 | goto unset_supplies; |
| 6122 | } |
| 6123 | } |
| 6124 | } |
| 6125 | |
| 6126 | if (!rdev->desc->ops->get_voltage && |
| 6127 | !rdev->desc->ops->list_voltage && |
| 6128 | !rdev->desc->fixed_uV) |
| 6129 | rdev->is_switch = true; |
| 6130 | |
| 6131 | ret = device_add(dev: &rdev->dev); |
| 6132 | if (ret != 0) |
| 6133 | goto unset_supplies; |
| 6134 | |
| 6135 | rdev_init_debugfs(rdev); |
| 6136 | |
| 6137 | /* try to resolve regulators coupling since a new one was registered */ |
| 6138 | mutex_lock(®ulator_list_mutex); |
| 6139 | regulator_resolve_coupling(rdev); |
| 6140 | mutex_unlock(lock: ®ulator_list_mutex); |
| 6141 | |
| 6142 | /* try to resolve regulators supply since a new one was registered */ |
| 6143 | class_for_each_device(class: ®ulator_class, NULL, NULL, |
| 6144 | fn: regulator_register_resolve_supply); |
| 6145 | kfree(objp: config); |
| 6146 | return rdev; |
| 6147 | |
| 6148 | unset_supplies: |
| 6149 | mutex_lock(®ulator_list_mutex); |
| 6150 | unset_regulator_supplies(rdev); |
| 6151 | regulator_remove_coupling(rdev); |
| 6152 | mutex_unlock(lock: ®ulator_list_mutex); |
| 6153 | wash: |
| 6154 | regulator_put(rdev->supply); |
| 6155 | kfree(objp: rdev->coupling_desc.coupled_rdevs); |
| 6156 | mutex_lock(®ulator_list_mutex); |
| 6157 | regulator_ena_gpio_free(rdev); |
| 6158 | mutex_unlock(lock: ®ulator_list_mutex); |
| 6159 | clean: |
| 6160 | if (dangling_of_gpiod) |
| 6161 | gpiod_put(desc: config->ena_gpiod); |
| 6162 | kfree(objp: config); |
| 6163 | put_device(dev: &rdev->dev); |
| 6164 | rinse: |
| 6165 | if (dangling_cfg_gpiod) |
| 6166 | gpiod_put(desc: cfg->ena_gpiod); |
| 6167 | return ERR_PTR(error: ret); |
| 6168 | } |
| 6169 | EXPORT_SYMBOL_GPL(regulator_register); |
| 6170 | |
| 6171 | /** |
| 6172 | * regulator_unregister - unregister regulator |
| 6173 | * @rdev: regulator to unregister |
| 6174 | * |
| 6175 | * Called by regulator drivers to unregister a regulator. |
| 6176 | */ |
| 6177 | void regulator_unregister(struct regulator_dev *rdev) |
| 6178 | { |
| 6179 | if (rdev == NULL) |
| 6180 | return; |
| 6181 | |
| 6182 | if (rdev->supply) { |
| 6183 | regulator_unregister_notifier(rdev->supply, |
| 6184 | &rdev->supply_fwd_nb); |
| 6185 | |
| 6186 | while (rdev->use_count--) |
| 6187 | regulator_disable(rdev->supply); |
| 6188 | regulator_put(rdev->supply); |
| 6189 | } |
| 6190 | |
| 6191 | flush_work(work: &rdev->disable_work.work); |
| 6192 | |
| 6193 | mutex_lock(®ulator_list_mutex); |
| 6194 | |
| 6195 | WARN_ON(rdev->open_count); |
| 6196 | regulator_remove_coupling(rdev); |
| 6197 | unset_regulator_supplies(rdev); |
| 6198 | list_del(entry: &rdev->list); |
| 6199 | regulator_ena_gpio_free(rdev); |
| 6200 | device_unregister(dev: &rdev->dev); |
| 6201 | |
| 6202 | mutex_unlock(lock: ®ulator_list_mutex); |
| 6203 | } |
| 6204 | EXPORT_SYMBOL_GPL(regulator_unregister); |
| 6205 | |
| 6206 | #ifdef CONFIG_SUSPEND |
| 6207 | /** |
| 6208 | * regulator_suspend - prepare regulators for system wide suspend |
| 6209 | * @dev: ``&struct device`` pointer that is passed to _regulator_suspend() |
| 6210 | * |
| 6211 | * Configure each regulator with it's suspend operating parameters for state. |
| 6212 | * |
| 6213 | * Return: 0 on success or a negative error number on failure. |
| 6214 | */ |
| 6215 | static int regulator_suspend(struct device *dev) |
| 6216 | { |
| 6217 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 6218 | suspend_state_t state = pm_suspend_target_state; |
| 6219 | int ret; |
| 6220 | const struct regulator_state *rstate; |
| 6221 | |
| 6222 | rstate = regulator_get_suspend_state_check(rdev, state); |
| 6223 | if (!rstate) |
| 6224 | return 0; |
| 6225 | |
| 6226 | regulator_lock(rdev); |
| 6227 | ret = __suspend_set_state(rdev, rstate); |
| 6228 | regulator_unlock(rdev); |
| 6229 | |
| 6230 | return ret; |
| 6231 | } |
| 6232 | |
| 6233 | static int regulator_resume(struct device *dev) |
| 6234 | { |
| 6235 | suspend_state_t state = pm_suspend_target_state; |
| 6236 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 6237 | struct regulator_state *rstate; |
| 6238 | int ret = 0; |
| 6239 | |
| 6240 | rstate = regulator_get_suspend_state(rdev, state); |
| 6241 | if (rstate == NULL) |
| 6242 | return 0; |
| 6243 | |
| 6244 | /* Avoid grabbing the lock if we don't need to */ |
| 6245 | if (!rdev->desc->ops->resume) |
| 6246 | return 0; |
| 6247 | |
| 6248 | regulator_lock(rdev); |
| 6249 | |
| 6250 | if (rstate->enabled == ENABLE_IN_SUSPEND || |
| 6251 | rstate->enabled == DISABLE_IN_SUSPEND) |
| 6252 | ret = rdev->desc->ops->resume(rdev); |
| 6253 | |
| 6254 | regulator_unlock(rdev); |
| 6255 | |
| 6256 | return ret; |
| 6257 | } |
| 6258 | #else /* !CONFIG_SUSPEND */ |
| 6259 | |
| 6260 | #define regulator_suspend NULL |
| 6261 | #define regulator_resume NULL |
| 6262 | |
| 6263 | #endif /* !CONFIG_SUSPEND */ |
| 6264 | |
| 6265 | #ifdef CONFIG_PM |
| 6266 | static const struct dev_pm_ops __maybe_unused regulator_pm_ops = { |
| 6267 | .suspend = regulator_suspend, |
| 6268 | .resume = regulator_resume, |
| 6269 | }; |
| 6270 | #endif |
| 6271 | |
| 6272 | const struct class regulator_class = { |
| 6273 | .name = "regulator" , |
| 6274 | .dev_release = regulator_dev_release, |
| 6275 | .dev_groups = regulator_dev_groups, |
| 6276 | #ifdef CONFIG_PM |
| 6277 | .pm = ®ulator_pm_ops, |
| 6278 | #endif |
| 6279 | }; |
| 6280 | /** |
| 6281 | * regulator_has_full_constraints - the system has fully specified constraints |
| 6282 | * |
| 6283 | * Calling this function will cause the regulator API to disable all |
| 6284 | * regulators which have a zero use count and don't have an always_on |
| 6285 | * constraint in a late_initcall. |
| 6286 | * |
| 6287 | * The intention is that this will become the default behaviour in a |
| 6288 | * future kernel release so users are encouraged to use this facility |
| 6289 | * now. |
| 6290 | */ |
| 6291 | void regulator_has_full_constraints(void) |
| 6292 | { |
| 6293 | has_full_constraints = 1; |
| 6294 | } |
| 6295 | EXPORT_SYMBOL_GPL(regulator_has_full_constraints); |
| 6296 | |
| 6297 | /** |
| 6298 | * rdev_get_drvdata - get rdev regulator driver data |
| 6299 | * @rdev: regulator |
| 6300 | * |
| 6301 | * Get rdev regulator driver private data. This call can be used in the |
| 6302 | * regulator driver context. |
| 6303 | * |
| 6304 | * Return: Pointer to regulator driver private data. |
| 6305 | */ |
| 6306 | void *rdev_get_drvdata(struct regulator_dev *rdev) |
| 6307 | { |
| 6308 | return rdev->reg_data; |
| 6309 | } |
| 6310 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); |
| 6311 | |
| 6312 | /** |
| 6313 | * regulator_get_drvdata - get regulator driver data |
| 6314 | * @regulator: regulator |
| 6315 | * |
| 6316 | * Get regulator driver private data. This call can be used in the consumer |
| 6317 | * driver context when non API regulator specific functions need to be called. |
| 6318 | * |
| 6319 | * Return: Pointer to regulator driver private data. |
| 6320 | */ |
| 6321 | void *regulator_get_drvdata(struct regulator *regulator) |
| 6322 | { |
| 6323 | return regulator->rdev->reg_data; |
| 6324 | } |
| 6325 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); |
| 6326 | |
| 6327 | /** |
| 6328 | * regulator_set_drvdata - set regulator driver data |
| 6329 | * @regulator: regulator |
| 6330 | * @data: data |
| 6331 | */ |
| 6332 | void regulator_set_drvdata(struct regulator *regulator, void *data) |
| 6333 | { |
| 6334 | regulator->rdev->reg_data = data; |
| 6335 | } |
| 6336 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); |
| 6337 | |
| 6338 | /** |
| 6339 | * rdev_get_id - get regulator ID |
| 6340 | * @rdev: regulator |
| 6341 | * |
| 6342 | * Return: Regulator ID for @rdev. |
| 6343 | */ |
| 6344 | int rdev_get_id(struct regulator_dev *rdev) |
| 6345 | { |
| 6346 | return rdev->desc->id; |
| 6347 | } |
| 6348 | EXPORT_SYMBOL_GPL(rdev_get_id); |
| 6349 | |
| 6350 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
| 6351 | { |
| 6352 | return &rdev->dev; |
| 6353 | } |
| 6354 | EXPORT_SYMBOL_GPL(rdev_get_dev); |
| 6355 | |
| 6356 | struct regmap *rdev_get_regmap(struct regulator_dev *rdev) |
| 6357 | { |
| 6358 | return rdev->regmap; |
| 6359 | } |
| 6360 | EXPORT_SYMBOL_GPL(rdev_get_regmap); |
| 6361 | |
| 6362 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) |
| 6363 | { |
| 6364 | return reg_init_data->driver_data; |
| 6365 | } |
| 6366 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); |
| 6367 | |
| 6368 | #ifdef CONFIG_DEBUG_FS |
| 6369 | static int supply_map_show(struct seq_file *sf, void *data) |
| 6370 | { |
| 6371 | struct regulator_map *map; |
| 6372 | |
| 6373 | list_for_each_entry(map, ®ulator_map_list, list) { |
| 6374 | seq_printf(m: sf, fmt: "%s -> %s.%s\n" , |
| 6375 | rdev_get_name(map->regulator), map->dev_name, |
| 6376 | map->supply); |
| 6377 | } |
| 6378 | |
| 6379 | return 0; |
| 6380 | } |
| 6381 | DEFINE_SHOW_ATTRIBUTE(supply_map); |
| 6382 | |
| 6383 | struct summary_data { |
| 6384 | struct seq_file *s; |
| 6385 | struct regulator_dev *parent; |
| 6386 | int level; |
| 6387 | }; |
| 6388 | |
| 6389 | static void regulator_summary_show_subtree(struct seq_file *s, |
| 6390 | struct regulator_dev *rdev, |
| 6391 | int level); |
| 6392 | |
| 6393 | static int regulator_summary_show_children(struct device *dev, void *data) |
| 6394 | { |
| 6395 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 6396 | struct summary_data *summary_data = data; |
| 6397 | |
| 6398 | if (rdev->supply && rdev->supply->rdev == summary_data->parent) |
| 6399 | regulator_summary_show_subtree(s: summary_data->s, rdev, |
| 6400 | level: summary_data->level + 1); |
| 6401 | |
| 6402 | return 0; |
| 6403 | } |
| 6404 | |
| 6405 | static void regulator_summary_show_subtree(struct seq_file *s, |
| 6406 | struct regulator_dev *rdev, |
| 6407 | int level) |
| 6408 | { |
| 6409 | struct regulation_constraints *c; |
| 6410 | struct regulator *consumer; |
| 6411 | struct summary_data summary_data; |
| 6412 | unsigned int opmode; |
| 6413 | |
| 6414 | if (!rdev) |
| 6415 | return; |
| 6416 | |
| 6417 | opmode = _regulator_get_mode_unlocked(rdev); |
| 6418 | seq_printf(m: s, fmt: "%*s%-*s %3d %4d %6d %7s " , |
| 6419 | level * 3 + 1, "" , |
| 6420 | 30 - level * 3, rdev_get_name(rdev), |
| 6421 | rdev->use_count, rdev->open_count, rdev->bypass_count, |
| 6422 | regulator_opmode_to_str(mode: opmode)); |
| 6423 | |
| 6424 | seq_printf(m: s, fmt: "%5dmV " , regulator_get_voltage_rdev(rdev) / 1000); |
| 6425 | seq_printf(m: s, fmt: "%5dmA " , |
| 6426 | _regulator_get_current_limit_unlocked(rdev) / 1000); |
| 6427 | |
| 6428 | c = rdev->constraints; |
| 6429 | if (c) { |
| 6430 | switch (rdev->desc->type) { |
| 6431 | case REGULATOR_VOLTAGE: |
| 6432 | seq_printf(m: s, fmt: "%5dmV %5dmV " , |
| 6433 | c->min_uV / 1000, c->max_uV / 1000); |
| 6434 | break; |
| 6435 | case REGULATOR_CURRENT: |
| 6436 | seq_printf(m: s, fmt: "%5dmA %5dmA " , |
| 6437 | c->min_uA / 1000, c->max_uA / 1000); |
| 6438 | break; |
| 6439 | } |
| 6440 | } |
| 6441 | |
| 6442 | seq_puts(m: s, s: "\n" ); |
| 6443 | |
| 6444 | list_for_each_entry(consumer, &rdev->consumer_list, list) { |
| 6445 | if (consumer->dev && consumer->dev->class == ®ulator_class) |
| 6446 | continue; |
| 6447 | |
| 6448 | seq_printf(m: s, fmt: "%*s%-*s " , |
| 6449 | (level + 1) * 3 + 1, "" , |
| 6450 | 30 - (level + 1) * 3, |
| 6451 | consumer->supply_name ? consumer->supply_name : |
| 6452 | consumer->dev ? dev_name(dev: consumer->dev) : "deviceless" ); |
| 6453 | |
| 6454 | switch (rdev->desc->type) { |
| 6455 | case REGULATOR_VOLTAGE: |
| 6456 | seq_printf(m: s, fmt: "%3d %33dmA%c%5dmV %5dmV" , |
| 6457 | consumer->enable_count, |
| 6458 | consumer->uA_load / 1000, |
| 6459 | consumer->uA_load && !consumer->enable_count ? |
| 6460 | '*' : ' ', |
| 6461 | consumer->voltage[PM_SUSPEND_ON].min_uV / 1000, |
| 6462 | consumer->voltage[PM_SUSPEND_ON].max_uV / 1000); |
| 6463 | break; |
| 6464 | case REGULATOR_CURRENT: |
| 6465 | break; |
| 6466 | } |
| 6467 | |
| 6468 | seq_puts(m: s, s: "\n" ); |
| 6469 | } |
| 6470 | |
| 6471 | summary_data.s = s; |
| 6472 | summary_data.level = level; |
| 6473 | summary_data.parent = rdev; |
| 6474 | |
| 6475 | class_for_each_device(class: ®ulator_class, NULL, data: &summary_data, |
| 6476 | fn: regulator_summary_show_children); |
| 6477 | } |
| 6478 | |
| 6479 | struct summary_lock_data { |
| 6480 | struct ww_acquire_ctx *ww_ctx; |
| 6481 | struct regulator_dev **new_contended_rdev; |
| 6482 | struct regulator_dev **old_contended_rdev; |
| 6483 | }; |
| 6484 | |
| 6485 | static int regulator_summary_lock_one(struct device *dev, void *data) |
| 6486 | { |
| 6487 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 6488 | struct summary_lock_data *lock_data = data; |
| 6489 | int ret = 0; |
| 6490 | |
| 6491 | if (rdev != *lock_data->old_contended_rdev) { |
| 6492 | ret = regulator_lock_nested(rdev, ww_ctx: lock_data->ww_ctx); |
| 6493 | |
| 6494 | if (ret == -EDEADLK) |
| 6495 | *lock_data->new_contended_rdev = rdev; |
| 6496 | else |
| 6497 | WARN_ON_ONCE(ret); |
| 6498 | } else { |
| 6499 | *lock_data->old_contended_rdev = NULL; |
| 6500 | } |
| 6501 | |
| 6502 | return ret; |
| 6503 | } |
| 6504 | |
| 6505 | static int regulator_summary_unlock_one(struct device *dev, void *data) |
| 6506 | { |
| 6507 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 6508 | struct summary_lock_data *lock_data = data; |
| 6509 | |
| 6510 | if (lock_data) { |
| 6511 | if (rdev == *lock_data->new_contended_rdev) |
| 6512 | return -EDEADLK; |
| 6513 | } |
| 6514 | |
| 6515 | regulator_unlock(rdev); |
| 6516 | |
| 6517 | return 0; |
| 6518 | } |
| 6519 | |
| 6520 | static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx, |
| 6521 | struct regulator_dev **new_contended_rdev, |
| 6522 | struct regulator_dev **old_contended_rdev) |
| 6523 | { |
| 6524 | struct summary_lock_data lock_data; |
| 6525 | int ret; |
| 6526 | |
| 6527 | lock_data.ww_ctx = ww_ctx; |
| 6528 | lock_data.new_contended_rdev = new_contended_rdev; |
| 6529 | lock_data.old_contended_rdev = old_contended_rdev; |
| 6530 | |
| 6531 | ret = class_for_each_device(class: ®ulator_class, NULL, data: &lock_data, |
| 6532 | fn: regulator_summary_lock_one); |
| 6533 | if (ret) |
| 6534 | class_for_each_device(class: ®ulator_class, NULL, data: &lock_data, |
| 6535 | fn: regulator_summary_unlock_one); |
| 6536 | |
| 6537 | return ret; |
| 6538 | } |
| 6539 | |
| 6540 | static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx) |
| 6541 | { |
| 6542 | struct regulator_dev *new_contended_rdev = NULL; |
| 6543 | struct regulator_dev *old_contended_rdev = NULL; |
| 6544 | int err; |
| 6545 | |
| 6546 | mutex_lock(®ulator_list_mutex); |
| 6547 | |
| 6548 | ww_acquire_init(ctx: ww_ctx, ww_class: ®ulator_ww_class); |
| 6549 | |
| 6550 | do { |
| 6551 | if (new_contended_rdev) { |
| 6552 | ww_mutex_lock_slow(lock: &new_contended_rdev->mutex, ctx: ww_ctx); |
| 6553 | old_contended_rdev = new_contended_rdev; |
| 6554 | old_contended_rdev->ref_cnt++; |
| 6555 | old_contended_rdev->mutex_owner = current; |
| 6556 | } |
| 6557 | |
| 6558 | err = regulator_summary_lock_all(ww_ctx, |
| 6559 | new_contended_rdev: &new_contended_rdev, |
| 6560 | old_contended_rdev: &old_contended_rdev); |
| 6561 | |
| 6562 | if (old_contended_rdev) |
| 6563 | regulator_unlock(rdev: old_contended_rdev); |
| 6564 | |
| 6565 | } while (err == -EDEADLK); |
| 6566 | |
| 6567 | ww_acquire_done(ctx: ww_ctx); |
| 6568 | } |
| 6569 | |
| 6570 | static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx) |
| 6571 | { |
| 6572 | class_for_each_device(class: ®ulator_class, NULL, NULL, |
| 6573 | fn: regulator_summary_unlock_one); |
| 6574 | ww_acquire_fini(ctx: ww_ctx); |
| 6575 | |
| 6576 | mutex_unlock(lock: ®ulator_list_mutex); |
| 6577 | } |
| 6578 | |
| 6579 | static int regulator_summary_show_roots(struct device *dev, void *data) |
| 6580 | { |
| 6581 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 6582 | struct seq_file *s = data; |
| 6583 | |
| 6584 | if (!rdev->supply) |
| 6585 | regulator_summary_show_subtree(s, rdev, level: 0); |
| 6586 | |
| 6587 | return 0; |
| 6588 | } |
| 6589 | |
| 6590 | static int regulator_summary_show(struct seq_file *s, void *data) |
| 6591 | { |
| 6592 | struct ww_acquire_ctx ww_ctx; |
| 6593 | |
| 6594 | seq_puts(m: s, s: " regulator use open bypass opmode voltage current min max\n" ); |
| 6595 | seq_puts(m: s, s: "---------------------------------------------------------------------------------------\n" ); |
| 6596 | |
| 6597 | regulator_summary_lock(ww_ctx: &ww_ctx); |
| 6598 | |
| 6599 | class_for_each_device(class: ®ulator_class, NULL, data: s, |
| 6600 | fn: regulator_summary_show_roots); |
| 6601 | |
| 6602 | regulator_summary_unlock(ww_ctx: &ww_ctx); |
| 6603 | |
| 6604 | return 0; |
| 6605 | } |
| 6606 | DEFINE_SHOW_ATTRIBUTE(regulator_summary); |
| 6607 | #endif /* CONFIG_DEBUG_FS */ |
| 6608 | |
| 6609 | static int __init regulator_init(void) |
| 6610 | { |
| 6611 | int ret; |
| 6612 | |
| 6613 | ret = class_register(class: ®ulator_class); |
| 6614 | |
| 6615 | debugfs_root = debugfs_create_dir(name: "regulator" , NULL); |
| 6616 | if (IS_ERR(ptr: debugfs_root)) |
| 6617 | pr_debug("regulator: Failed to create debugfs directory\n" ); |
| 6618 | |
| 6619 | #ifdef CONFIG_DEBUG_FS |
| 6620 | debugfs_create_file("supply_map" , 0444, debugfs_root, NULL, |
| 6621 | &supply_map_fops); |
| 6622 | |
| 6623 | debugfs_create_file("regulator_summary" , 0444, debugfs_root, |
| 6624 | NULL, ®ulator_summary_fops); |
| 6625 | #endif |
| 6626 | regulator_dummy_init(); |
| 6627 | |
| 6628 | regulator_coupler_register(coupler: &generic_regulator_coupler); |
| 6629 | |
| 6630 | return ret; |
| 6631 | } |
| 6632 | |
| 6633 | /* init early to allow our consumers to complete system booting */ |
| 6634 | core_initcall(regulator_init); |
| 6635 | |
| 6636 | static int regulator_late_cleanup(struct device *dev, void *data) |
| 6637 | { |
| 6638 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 6639 | struct regulation_constraints *c = rdev->constraints; |
| 6640 | int ret; |
| 6641 | |
| 6642 | if (c && c->always_on) |
| 6643 | return 0; |
| 6644 | |
| 6645 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) |
| 6646 | return 0; |
| 6647 | |
| 6648 | regulator_lock(rdev); |
| 6649 | |
| 6650 | if (rdev->use_count) |
| 6651 | goto unlock; |
| 6652 | |
| 6653 | /* If reading the status failed, assume that it's off. */ |
| 6654 | if (_regulator_is_enabled(rdev) <= 0) |
| 6655 | goto unlock; |
| 6656 | |
| 6657 | if (have_full_constraints()) { |
| 6658 | /* We log since this may kill the system if it goes |
| 6659 | * wrong. |
| 6660 | */ |
| 6661 | rdev_info(rdev, "disabling\n" ); |
| 6662 | ret = _regulator_do_disable(rdev); |
| 6663 | if (ret != 0) |
| 6664 | rdev_err(rdev, "couldn't disable: %pe\n" , ERR_PTR(ret)); |
| 6665 | } else { |
| 6666 | /* The intention is that in future we will |
| 6667 | * assume that full constraints are provided |
| 6668 | * so warn even if we aren't going to do |
| 6669 | * anything here. |
| 6670 | */ |
| 6671 | rdev_warn(rdev, "incomplete constraints, leaving on\n" ); |
| 6672 | } |
| 6673 | |
| 6674 | unlock: |
| 6675 | regulator_unlock(rdev); |
| 6676 | |
| 6677 | return 0; |
| 6678 | } |
| 6679 | |
| 6680 | static bool regulator_ignore_unused; |
| 6681 | static int __init regulator_ignore_unused_setup(char *__unused) |
| 6682 | { |
| 6683 | regulator_ignore_unused = true; |
| 6684 | return 1; |
| 6685 | } |
| 6686 | __setup("regulator_ignore_unused" , regulator_ignore_unused_setup); |
| 6687 | |
| 6688 | static void regulator_init_complete_work_function(struct work_struct *work) |
| 6689 | { |
| 6690 | /* |
| 6691 | * Regulators may had failed to resolve their input supplies |
| 6692 | * when were registered, either because the input supply was |
| 6693 | * not registered yet or because its parent device was not |
| 6694 | * bound yet. So attempt to resolve the input supplies for |
| 6695 | * pending regulators before trying to disable unused ones. |
| 6696 | */ |
| 6697 | class_for_each_device(class: ®ulator_class, NULL, NULL, |
| 6698 | fn: regulator_register_resolve_supply); |
| 6699 | |
| 6700 | /* |
| 6701 | * For debugging purposes, it may be useful to prevent unused |
| 6702 | * regulators from being disabled. |
| 6703 | */ |
| 6704 | if (regulator_ignore_unused) { |
| 6705 | pr_warn("regulator: Not disabling unused regulators\n" ); |
| 6706 | return; |
| 6707 | } |
| 6708 | |
| 6709 | /* If we have a full configuration then disable any regulators |
| 6710 | * we have permission to change the status for and which are |
| 6711 | * not in use or always_on. This is effectively the default |
| 6712 | * for DT and ACPI as they have full constraints. |
| 6713 | */ |
| 6714 | class_for_each_device(class: ®ulator_class, NULL, NULL, |
| 6715 | fn: regulator_late_cleanup); |
| 6716 | } |
| 6717 | |
| 6718 | static DECLARE_DELAYED_WORK(regulator_init_complete_work, |
| 6719 | regulator_init_complete_work_function); |
| 6720 | |
| 6721 | static int __init regulator_init_complete(void) |
| 6722 | { |
| 6723 | /* |
| 6724 | * Since DT doesn't provide an idiomatic mechanism for |
| 6725 | * enabling full constraints and since it's much more natural |
| 6726 | * with DT to provide them just assume that a DT enabled |
| 6727 | * system has full constraints. |
| 6728 | */ |
| 6729 | if (of_have_populated_dt()) |
| 6730 | has_full_constraints = true; |
| 6731 | |
| 6732 | /* |
| 6733 | * We punt completion for an arbitrary amount of time since |
| 6734 | * systems like distros will load many drivers from userspace |
| 6735 | * so consumers might not always be ready yet, this is |
| 6736 | * particularly an issue with laptops where this might bounce |
| 6737 | * the display off then on. Ideally we'd get a notification |
| 6738 | * from userspace when this happens but we don't so just wait |
| 6739 | * a bit and hope we waited long enough. It'd be better if |
| 6740 | * we'd only do this on systems that need it, and a kernel |
| 6741 | * command line option might be useful. |
| 6742 | */ |
| 6743 | schedule_delayed_work(dwork: ®ulator_init_complete_work, |
| 6744 | delay: msecs_to_jiffies(m: 30000)); |
| 6745 | |
| 6746 | return 0; |
| 6747 | } |
| 6748 | late_initcall_sync(regulator_init_complete); |
| 6749 | |