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