File Index
Files and directories
Sign in
FILE

core.c

File preview with syntax highlighting.

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