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