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