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 (!regulator->debugfs) {
1757 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1758 } else {
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,
1766 regulator->debugfs, regulator,
1767 &constraint_flags_fops);
1768 }
1769
1770 /*
1771 * Check now if the regulator is an always on regulator - if
1772 * it is then we don't need to do nearly so much work for
1773 * enable/disable calls.
1774 */
1775 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1776 _regulator_is_enabled(rdev))
1777 regulator->always_on = true;
1778
1779 return regulator;
1780 }
1781
_regulator_get_enable_time(struct regulator_dev * rdev)1782 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1783 {
1784 if (rdev->constraints && rdev->constraints->enable_time)
1785 return rdev->constraints->enable_time;
1786 if (rdev->desc->ops->enable_time)
1787 return rdev->desc->ops->enable_time(rdev);
1788 return rdev->desc->enable_time;
1789 }
1790
regulator_find_supply_alias(struct device * dev,const char * supply)1791 static struct regulator_supply_alias *regulator_find_supply_alias(
1792 struct device *dev, const char *supply)
1793 {
1794 struct regulator_supply_alias *map;
1795
1796 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1797 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1798 return map;
1799
1800 return NULL;
1801 }
1802
regulator_supply_alias(struct device ** dev,const char ** supply)1803 static void regulator_supply_alias(struct device **dev, const char **supply)
1804 {
1805 struct regulator_supply_alias *map;
1806
1807 map = regulator_find_supply_alias(*dev, *supply);
1808 if (map) {
1809 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1810 *supply, map->alias_supply,
1811 dev_name(map->alias_dev));
1812 *dev = map->alias_dev;
1813 *supply = map->alias_supply;
1814 }
1815 }
1816
regulator_match(struct device * dev,const void * data)1817 static int regulator_match(struct device *dev, const void *data)
1818 {
1819 struct regulator_dev *r = dev_to_rdev(dev);
1820
1821 return strcmp(rdev_get_name(r), data) == 0;
1822 }
1823
regulator_lookup_by_name(const char * name)1824 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1825 {
1826 struct device *dev;
1827
1828 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1829
1830 return dev ? dev_to_rdev(dev) : NULL;
1831 }
1832
1833 /**
1834 * regulator_dev_lookup - lookup a regulator device.
1835 * @dev: device for regulator "consumer".
1836 * @supply: Supply name or regulator ID.
1837 *
1838 * If successful, returns a struct regulator_dev that corresponds to the name
1839 * @supply and with the embedded struct device refcount incremented by one.
1840 * The refcount must be dropped by calling put_device().
1841 * On failure one of the following ERR-PTR-encoded values is returned:
1842 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1843 * in the future.
1844 */
regulator_dev_lookup(struct device * dev,const char * supply)1845 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1846 const char *supply)
1847 {
1848 struct regulator_dev *r = NULL;
1849 struct device_node *node;
1850 struct regulator_map *map;
1851 const char *devname = NULL;
1852
1853 regulator_supply_alias(&dev, &supply);
1854
1855 /* first do a dt based lookup */
1856 if (dev && dev->of_node) {
1857 node = of_get_regulator(dev, supply);
1858 if (node) {
1859 r = of_find_regulator_by_node(node);
1860 of_node_put(node);
1861 if (r)
1862 return r;
1863
1864 /*
1865 * We have a node, but there is no device.
1866 * assume it has not registered yet.
1867 */
1868 return ERR_PTR(-EPROBE_DEFER);
1869 }
1870 }
1871
1872 /* if not found, try doing it non-dt way */
1873 if (dev)
1874 devname = dev_name(dev);
1875
1876 mutex_lock(®ulator_list_mutex);
1877 list_for_each_entry(map, ®ulator_map_list, list) {
1878 /* If the mapping has a device set up it must match */
1879 if (map->dev_name &&
1880 (!devname || strcmp(map->dev_name, devname)))
1881 continue;
1882
1883 if (strcmp(map->supply, supply) == 0 &&
1884 get_device(&map->regulator->dev)) {
1885 r = map->regulator;
1886 break;
1887 }
1888 }
1889 mutex_unlock(®ulator_list_mutex);
1890
1891 if (r)
1892 return r;
1893
1894 r = regulator_lookup_by_name(supply);
1895 if (r)
1896 return r;
1897
1898 return ERR_PTR(-ENODEV);
1899 }
1900
regulator_resolve_supply(struct regulator_dev * rdev)1901 static int regulator_resolve_supply(struct regulator_dev *rdev)
1902 {
1903 struct regulator_dev *r;
1904 struct device *dev = rdev->dev.parent;
1905 struct ww_acquire_ctx ww_ctx;
1906 int ret = 0;
1907
1908 /* No supply to resolve? */
1909 if (!rdev->supply_name)
1910 return 0;
1911
1912 /* Supply already resolved? (fast-path without locking contention) */
1913 if (rdev->supply)
1914 return 0;
1915
1916 r = regulator_dev_lookup(dev, rdev->supply_name);
1917 if (IS_ERR(r)) {
1918 ret = PTR_ERR(r);
1919
1920 /* Did the lookup explicitly defer for us? */
1921 if (ret == -EPROBE_DEFER)
1922 goto out;
1923
1924 if (have_full_constraints()) {
1925 r = dummy_regulator_rdev;
1926 get_device(&r->dev);
1927 } else {
1928 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1929 rdev->supply_name, rdev->desc->name);
1930 ret = -EPROBE_DEFER;
1931 goto out;
1932 }
1933 }
1934
1935 if (r == rdev) {
1936 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
1937 rdev->desc->name, rdev->supply_name);
1938 if (!have_full_constraints()) {
1939 ret = -EINVAL;
1940 goto out;
1941 }
1942 r = dummy_regulator_rdev;
1943 get_device(&r->dev);
1944 }
1945
1946 /*
1947 * If the supply's parent device is not the same as the
1948 * regulator's parent device, then ensure the parent device
1949 * is bound before we resolve the supply, in case the parent
1950 * device get probe deferred and unregisters the supply.
1951 */
1952 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1953 if (!device_is_bound(r->dev.parent)) {
1954 put_device(&r->dev);
1955 ret = -EPROBE_DEFER;
1956 goto out;
1957 }
1958 }
1959
1960 /* Recursively resolve the supply of the supply */
1961 ret = regulator_resolve_supply(r);
1962 if (ret < 0) {
1963 put_device(&r->dev);
1964 goto out;
1965 }
1966
1967 /*
1968 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
1969 * between rdev->supply null check and setting rdev->supply in
1970 * set_supply() from concurrent tasks.
1971 */
1972 regulator_lock_two(rdev, r, &ww_ctx);
1973
1974 /* Supply just resolved by a concurrent task? */
1975 if (rdev->supply) {
1976 regulator_unlock_two(rdev, r, &ww_ctx);
1977 put_device(&r->dev);
1978 goto out;
1979 }
1980
1981 ret = set_supply(rdev, r);
1982 if (ret < 0) {
1983 regulator_unlock_two(rdev, r, &ww_ctx);
1984 put_device(&r->dev);
1985 goto out;
1986 }
1987
1988 regulator_unlock_two(rdev, r, &ww_ctx);
1989
1990 /*
1991 * In set_machine_constraints() we may have turned this regulator on
1992 * but we couldn't propagate to the supply if it hadn't been resolved
1993 * yet. Do it now.
1994 */
1995 if (rdev->use_count) {
1996 ret = regulator_enable(rdev->supply);
1997 if (ret < 0) {
1998 _regulator_put(rdev->supply);
1999 rdev->supply = NULL;
2000 goto out;
2001 }
2002 }
2003
2004 out:
2005 return ret;
2006 }
2007
2008 /* Internal regulator request function */
_regulator_get(struct device * dev,const char * id,enum regulator_get_type get_type)2009 struct regulator *_regulator_get(struct device *dev, const char *id,
2010 enum regulator_get_type get_type)
2011 {
2012 struct regulator_dev *rdev;
2013 struct regulator *regulator;
2014 struct device_link *link;
2015 int ret;
2016
2017 if (get_type >= MAX_GET_TYPE) {
2018 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
2019 return ERR_PTR(-EINVAL);
2020 }
2021
2022 if (id == NULL) {
2023 pr_err("get() with no identifier\n");
2024 return ERR_PTR(-EINVAL);
2025 }
2026
2027 rdev = regulator_dev_lookup(dev, id);
2028 if (IS_ERR(rdev)) {
2029 ret = PTR_ERR(rdev);
2030
2031 /*
2032 * If regulator_dev_lookup() fails with error other
2033 * than -ENODEV our job here is done, we simply return it.
2034 */
2035 if (ret != -ENODEV)
2036 return ERR_PTR(ret);
2037
2038 if (!have_full_constraints()) {
2039 dev_warn(dev,
2040 "incomplete constraints, dummy supplies not allowed\n");
2041 return ERR_PTR(-ENODEV);
2042 }
2043
2044 switch (get_type) {
2045 case NORMAL_GET:
2046 /*
2047 * Assume that a regulator is physically present and
2048 * enabled, even if it isn't hooked up, and just
2049 * provide a dummy.
2050 */
2051 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
2052 rdev = dummy_regulator_rdev;
2053 get_device(&rdev->dev);
2054 break;
2055
2056 case EXCLUSIVE_GET:
2057 dev_warn(dev,
2058 "dummy supplies not allowed for exclusive requests\n");
2059 fallthrough;
2060
2061 default:
2062 return ERR_PTR(-ENODEV);
2063 }
2064 }
2065
2066 if (rdev->exclusive) {
2067 regulator = ERR_PTR(-EPERM);
2068 put_device(&rdev->dev);
2069 return regulator;
2070 }
2071
2072 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
2073 regulator = ERR_PTR(-EBUSY);
2074 put_device(&rdev->dev);
2075 return regulator;
2076 }
2077
2078 mutex_lock(®ulator_list_mutex);
2079 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
2080 mutex_unlock(®ulator_list_mutex);
2081
2082 if (ret != 0) {
2083 regulator = ERR_PTR(-EPROBE_DEFER);
2084 put_device(&rdev->dev);
2085 return regulator;
2086 }
2087
2088 ret = regulator_resolve_supply(rdev);
2089 if (ret < 0) {
2090 regulator = ERR_PTR(ret);
2091 put_device(&rdev->dev);
2092 return regulator;
2093 }
2094
2095 if (!try_module_get(rdev->owner)) {
2096 regulator = ERR_PTR(-EPROBE_DEFER);
2097 put_device(&rdev->dev);
2098 return regulator;
2099 }
2100
2101 regulator_lock(rdev);
2102 regulator = create_regulator(rdev, dev, id);
2103 regulator_unlock(rdev);
2104 if (regulator == NULL) {
2105 regulator = ERR_PTR(-ENOMEM);
2106 module_put(rdev->owner);
2107 put_device(&rdev->dev);
2108 return regulator;
2109 }
2110
2111 rdev->open_count++;
2112 if (get_type == EXCLUSIVE_GET) {
2113 rdev->exclusive = 1;
2114
2115 ret = _regulator_is_enabled(rdev);
2116 if (ret > 0) {
2117 rdev->use_count = 1;
2118 regulator->enable_count = 1;
2119 } else {
2120 rdev->use_count = 0;
2121 regulator->enable_count = 0;
2122 }
2123 }
2124
2125 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2126 if (!IS_ERR_OR_NULL(link))
2127 regulator->device_link = true;
2128
2129 return regulator;
2130 }
2131
2132 /**
2133 * regulator_get - lookup and obtain a reference to a regulator.
2134 * @dev: device for regulator "consumer"
2135 * @id: Supply name or regulator ID.
2136 *
2137 * Returns a struct regulator corresponding to the regulator producer,
2138 * or IS_ERR() condition containing errno.
2139 *
2140 * Use of supply names configured via regulator_set_device_supply() is
2141 * strongly encouraged. It is recommended that the supply name used
2142 * should match the name used for the supply and/or the relevant
2143 * device pins in the datasheet.
2144 */
regulator_get(struct device * dev,const char * id)2145 struct regulator *regulator_get(struct device *dev, const char *id)
2146 {
2147 return _regulator_get(dev, id, NORMAL_GET);
2148 }
2149 EXPORT_SYMBOL_GPL(regulator_get);
2150
2151 /**
2152 * regulator_get_exclusive - obtain exclusive access to a regulator.
2153 * @dev: device for regulator "consumer"
2154 * @id: Supply name or regulator ID.
2155 *
2156 * Returns a struct regulator corresponding to the regulator producer,
2157 * or IS_ERR() condition containing errno. Other consumers will be
2158 * unable to obtain this regulator while this reference is held and the
2159 * use count for the regulator will be initialised to reflect the current
2160 * state of the regulator.
2161 *
2162 * This is intended for use by consumers which cannot tolerate shared
2163 * use of the regulator such as those which need to force the
2164 * regulator off for correct operation of the hardware they are
2165 * controlling.
2166 *
2167 * Use of supply names configured via regulator_set_device_supply() is
2168 * strongly encouraged. It is recommended that the supply name used
2169 * should match the name used for the supply and/or the relevant
2170 * device pins in the datasheet.
2171 */
regulator_get_exclusive(struct device * dev,const char * id)2172 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2173 {
2174 return _regulator_get(dev, id, EXCLUSIVE_GET);
2175 }
2176 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2177
2178 /**
2179 * regulator_get_optional - obtain optional access to a regulator.
2180 * @dev: device for regulator "consumer"
2181 * @id: Supply name or regulator ID.
2182 *
2183 * Returns a struct regulator corresponding to the regulator producer,
2184 * or IS_ERR() condition containing errno.
2185 *
2186 * This is intended for use by consumers for devices which can have
2187 * some supplies unconnected in normal use, such as some MMC devices.
2188 * It can allow the regulator core to provide stub supplies for other
2189 * supplies requested using normal regulator_get() calls without
2190 * disrupting the operation of drivers that can handle absent
2191 * supplies.
2192 *
2193 * Use of supply names configured via regulator_set_device_supply() is
2194 * strongly encouraged. It is recommended that the supply name used
2195 * should match the name used for the supply and/or the relevant
2196 * device pins in the datasheet.
2197 */
regulator_get_optional(struct device * dev,const char * id)2198 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2199 {
2200 return _regulator_get(dev, id, OPTIONAL_GET);
2201 }
2202 EXPORT_SYMBOL_GPL(regulator_get_optional);
2203
destroy_regulator(struct regulator * regulator)2204 static void destroy_regulator(struct regulator *regulator)
2205 {
2206 struct regulator_dev *rdev = regulator->rdev;
2207
2208 debugfs_remove_recursive(regulator->debugfs);
2209
2210 if (regulator->dev) {
2211 if (regulator->device_link)
2212 device_link_remove(regulator->dev, &rdev->dev);
2213
2214 /* remove any sysfs entries */
2215 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2216 }
2217
2218 regulator_lock(rdev);
2219 list_del(®ulator->list);
2220
2221 rdev->open_count--;
2222 rdev->exclusive = 0;
2223 regulator_unlock(rdev);
2224
2225 kfree_const(regulator->supply_name);
2226 kfree(regulator);
2227 }
2228
2229 /* regulator_list_mutex lock held by regulator_put() */
_regulator_put(struct regulator * regulator)2230 static void _regulator_put(struct regulator *regulator)
2231 {
2232 struct regulator_dev *rdev;
2233
2234 if (IS_ERR_OR_NULL(regulator))
2235 return;
2236
2237 lockdep_assert_held_once(®ulator_list_mutex);
2238
2239 /* Docs say you must disable before calling regulator_put() */
2240 WARN_ON(regulator->enable_count);
2241
2242 rdev = regulator->rdev;
2243
2244 destroy_regulator(regulator);
2245
2246 module_put(rdev->owner);
2247 put_device(&rdev->dev);
2248 }
2249
2250 /**
2251 * regulator_put - "free" the regulator source
2252 * @regulator: regulator source
2253 *
2254 * Note: drivers must ensure that all regulator_enable calls made on this
2255 * regulator source are balanced by regulator_disable calls prior to calling
2256 * this function.
2257 */
regulator_put(struct regulator * regulator)2258 void regulator_put(struct regulator *regulator)
2259 {
2260 mutex_lock(®ulator_list_mutex);
2261 _regulator_put(regulator);
2262 mutex_unlock(®ulator_list_mutex);
2263 }
2264 EXPORT_SYMBOL_GPL(regulator_put);
2265
2266 /**
2267 * regulator_register_supply_alias - Provide device alias for supply lookup
2268 *
2269 * @dev: device that will be given as the regulator "consumer"
2270 * @id: Supply name or regulator ID
2271 * @alias_dev: device that should be used to lookup the supply
2272 * @alias_id: Supply name or regulator ID that should be used to lookup the
2273 * supply
2274 *
2275 * All lookups for id on dev will instead be conducted for alias_id on
2276 * alias_dev.
2277 */
regulator_register_supply_alias(struct device * dev,const char * id,struct device * alias_dev,const char * alias_id)2278 int regulator_register_supply_alias(struct device *dev, const char *id,
2279 struct device *alias_dev,
2280 const char *alias_id)
2281 {
2282 struct regulator_supply_alias *map;
2283
2284 map = regulator_find_supply_alias(dev, id);
2285 if (map)
2286 return -EEXIST;
2287
2288 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2289 if (!map)
2290 return -ENOMEM;
2291
2292 map->src_dev = dev;
2293 map->src_supply = id;
2294 map->alias_dev = alias_dev;
2295 map->alias_supply = alias_id;
2296
2297 list_add(&map->list, ®ulator_supply_alias_list);
2298
2299 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2300 id, dev_name(dev), alias_id, dev_name(alias_dev));
2301
2302 return 0;
2303 }
2304 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2305
2306 /**
2307 * regulator_unregister_supply_alias - Remove device alias
2308 *
2309 * @dev: device that will be given as the regulator "consumer"
2310 * @id: Supply name or regulator ID
2311 *
2312 * Remove a lookup alias if one exists for id on dev.
2313 */
regulator_unregister_supply_alias(struct device * dev,const char * id)2314 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2315 {
2316 struct regulator_supply_alias *map;
2317
2318 map = regulator_find_supply_alias(dev, id);
2319 if (map) {
2320 list_del(&map->list);
2321 kfree(map);
2322 }
2323 }
2324 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2325
2326 /**
2327 * regulator_bulk_register_supply_alias - register multiple aliases
2328 *
2329 * @dev: device that will be given as the regulator "consumer"
2330 * @id: List of supply names or regulator IDs
2331 * @alias_dev: device that should be used to lookup the supply
2332 * @alias_id: List of supply names or regulator IDs that should be used to
2333 * lookup the supply
2334 * @num_id: Number of aliases to register
2335 *
2336 * @return 0 on success, an errno on failure.
2337 *
2338 * This helper function allows drivers to register several supply
2339 * aliases in one operation. If any of the aliases cannot be
2340 * registered any aliases that were registered will be removed
2341 * before returning to the caller.
2342 */
regulator_bulk_register_supply_alias(struct device * dev,const char * const * id,struct device * alias_dev,const char * const * alias_id,int num_id)2343 int regulator_bulk_register_supply_alias(struct device *dev,
2344 const char *const *id,
2345 struct device *alias_dev,
2346 const char *const *alias_id,
2347 int num_id)
2348 {
2349 int i;
2350 int ret;
2351
2352 for (i = 0; i < num_id; ++i) {
2353 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2354 alias_id[i]);
2355 if (ret < 0)
2356 goto err;
2357 }
2358
2359 return 0;
2360
2361 err:
2362 dev_err(dev,
2363 "Failed to create supply alias %s,%s -> %s,%s\n",
2364 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2365
2366 while (--i >= 0)
2367 regulator_unregister_supply_alias(dev, id[i]);
2368
2369 return ret;
2370 }
2371 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2372
2373 /**
2374 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2375 *
2376 * @dev: device that will be given as the regulator "consumer"
2377 * @id: List of supply names or regulator IDs
2378 * @num_id: Number of aliases to unregister
2379 *
2380 * This helper function allows drivers to unregister several supply
2381 * aliases in one operation.
2382 */
regulator_bulk_unregister_supply_alias(struct device * dev,const char * const * id,int num_id)2383 void regulator_bulk_unregister_supply_alias(struct device *dev,
2384 const char *const *id,
2385 int num_id)
2386 {
2387 int i;
2388
2389 for (i = 0; i < num_id; ++i)
2390 regulator_unregister_supply_alias(dev, id[i]);
2391 }
2392 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2393
2394
2395 /* 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)2396 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2397 const struct regulator_config *config)
2398 {
2399 struct regulator_enable_gpio *pin, *new_pin;
2400 struct gpio_desc *gpiod;
2401
2402 gpiod = config->ena_gpiod;
2403 new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2404
2405 mutex_lock(®ulator_list_mutex);
2406
2407 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2408 if (pin->gpiod == gpiod) {
2409 rdev_dbg(rdev, "GPIO is already used\n");
2410 goto update_ena_gpio_to_rdev;
2411 }
2412 }
2413
2414 if (new_pin == NULL) {
2415 mutex_unlock(®ulator_list_mutex);
2416 return -ENOMEM;
2417 }
2418
2419 pin = new_pin;
2420 new_pin = NULL;
2421
2422 pin->gpiod = gpiod;
2423 list_add(&pin->list, ®ulator_ena_gpio_list);
2424
2425 update_ena_gpio_to_rdev:
2426 pin->request_count++;
2427 rdev->ena_pin = pin;
2428
2429 mutex_unlock(®ulator_list_mutex);
2430 kfree(new_pin);
2431
2432 return 0;
2433 }
2434
regulator_ena_gpio_free(struct regulator_dev * rdev)2435 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2436 {
2437 struct regulator_enable_gpio *pin, *n;
2438
2439 if (!rdev->ena_pin)
2440 return;
2441
2442 /* Free the GPIO only in case of no use */
2443 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2444 if (pin != rdev->ena_pin)
2445 continue;
2446
2447 if (--pin->request_count)
2448 break;
2449
2450 gpiod_put(pin->gpiod);
2451 list_del(&pin->list);
2452 kfree(pin);
2453 break;
2454 }
2455
2456 rdev->ena_pin = NULL;
2457 }
2458
2459 /**
2460 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2461 * @rdev: regulator_dev structure
2462 * @enable: enable GPIO at initial use?
2463 *
2464 * GPIO is enabled in case of initial use. (enable_count is 0)
2465 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2466 */
regulator_ena_gpio_ctrl(struct regulator_dev * rdev,bool enable)2467 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2468 {
2469 struct regulator_enable_gpio *pin = rdev->ena_pin;
2470
2471 if (!pin)
2472 return -EINVAL;
2473
2474 if (enable) {
2475 /* Enable GPIO at initial use */
2476 if (pin->enable_count == 0)
2477 gpiod_set_value_cansleep(pin->gpiod, 1);
2478
2479 pin->enable_count++;
2480 } else {
2481 if (pin->enable_count > 1) {
2482 pin->enable_count--;
2483 return 0;
2484 }
2485
2486 /* Disable GPIO if not used */
2487 if (pin->enable_count <= 1) {
2488 gpiod_set_value_cansleep(pin->gpiod, 0);
2489 pin->enable_count = 0;
2490 }
2491 }
2492
2493 return 0;
2494 }
2495
2496 /**
2497 * _regulator_enable_delay - a delay helper function
2498 * @delay: time to delay in microseconds
2499 *
2500 * Delay for the requested amount of time as per the guidelines in:
2501 *
2502 * Documentation/timers/timers-howto.rst
2503 *
2504 * The assumption here is that regulators will never be enabled in
2505 * atomic context and therefore sleeping functions can be used.
2506 */
_regulator_enable_delay(unsigned int delay)2507 static void _regulator_enable_delay(unsigned int delay)
2508 {
2509 unsigned int ms = delay / 1000;
2510 unsigned int us = delay % 1000;
2511
2512 if (ms > 0) {
2513 /*
2514 * For small enough values, handle super-millisecond
2515 * delays in the usleep_range() call below.
2516 */
2517 if (ms < 20)
2518 us += ms * 1000;
2519 else
2520 msleep(ms);
2521 }
2522
2523 /*
2524 * Give the scheduler some room to coalesce with any other
2525 * wakeup sources. For delays shorter than 10 us, don't even
2526 * bother setting up high-resolution timers and just busy-
2527 * loop.
2528 */
2529 if (us >= 10)
2530 usleep_range(us, us + 100);
2531 else
2532 udelay(us);
2533 }
2534
2535 /**
2536 * _regulator_check_status_enabled
2537 *
2538 * A helper function to check if the regulator status can be interpreted
2539 * as 'regulator is enabled'.
2540 * @rdev: the regulator device to check
2541 *
2542 * Return:
2543 * * 1 - if status shows regulator is in enabled state
2544 * * 0 - if not enabled state
2545 * * Error Value - as received from ops->get_status()
2546 */
_regulator_check_status_enabled(struct regulator_dev * rdev)2547 static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2548 {
2549 int ret = rdev->desc->ops->get_status(rdev);
2550
2551 if (ret < 0) {
2552 rdev_info(rdev, "get_status returned error: %d\n", ret);
2553 return ret;
2554 }
2555
2556 switch (ret) {
2557 case REGULATOR_STATUS_OFF:
2558 case REGULATOR_STATUS_ERROR:
2559 case REGULATOR_STATUS_UNDEFINED:
2560 return 0;
2561 default:
2562 return 1;
2563 }
2564 }
2565
_regulator_do_enable(struct regulator_dev * rdev)2566 static int _regulator_do_enable(struct regulator_dev *rdev)
2567 {
2568 int ret, delay;
2569
2570 /* Query before enabling in case configuration dependent. */
2571 ret = _regulator_get_enable_time(rdev);
2572 if (ret >= 0) {
2573 delay = ret;
2574 } else {
2575 rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
2576 delay = 0;
2577 }
2578
2579 trace_regulator_enable(rdev_get_name(rdev));
2580
2581 if (rdev->desc->off_on_delay) {
2582 /* if needed, keep a distance of off_on_delay from last time
2583 * this regulator was disabled.
2584 */
2585 unsigned long start_jiffy = jiffies;
2586 unsigned long intended, max_delay, remaining;
2587
2588 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2589 intended = rdev->last_off_jiffy + max_delay;
2590
2591 if (time_before(start_jiffy, intended)) {
2592 /* calc remaining jiffies to deal with one-time
2593 * timer wrapping.
2594 * in case of multiple timer wrapping, either it can be
2595 * detected by out-of-range remaining, or it cannot be
2596 * detected and we get a penalty of
2597 * _regulator_enable_delay().
2598 */
2599 remaining = intended - start_jiffy;
2600 if (remaining <= max_delay)
2601 _regulator_enable_delay(
2602 jiffies_to_usecs(remaining));
2603 }
2604 }
2605
2606 if (rdev->ena_pin) {
2607 if (!rdev->ena_gpio_state) {
2608 ret = regulator_ena_gpio_ctrl(rdev, true);
2609 if (ret < 0)
2610 return ret;
2611 rdev->ena_gpio_state = 1;
2612 }
2613 } else if (rdev->desc->ops->enable) {
2614 ret = rdev->desc->ops->enable(rdev);
2615 if (ret < 0)
2616 return ret;
2617 } else {
2618 return -EINVAL;
2619 }
2620
2621 /* Allow the regulator to ramp; it would be useful to extend
2622 * this for bulk operations so that the regulators can ramp
2623 * together. */
2624 trace_regulator_enable_delay(rdev_get_name(rdev));
2625
2626 /* If poll_enabled_time is set, poll upto the delay calculated
2627 * above, delaying poll_enabled_time uS to check if the regulator
2628 * actually got enabled.
2629 * If the regulator isn't enabled after enable_delay has
2630 * expired, return -ETIMEDOUT.
2631 */
2632 if (rdev->desc->poll_enabled_time) {
2633 int time_remaining = delay;
2634
2635 while (time_remaining > 0) {
2636 _regulator_enable_delay(rdev->desc->poll_enabled_time);
2637
2638 if (rdev->desc->ops->get_status) {
2639 ret = _regulator_check_status_enabled(rdev);
2640 if (ret < 0)
2641 return ret;
2642 else if (ret)
2643 break;
2644 } else if (rdev->desc->ops->is_enabled(rdev))
2645 break;
2646
2647 time_remaining -= rdev->desc->poll_enabled_time;
2648 }
2649
2650 if (time_remaining <= 0) {
2651 rdev_err(rdev, "Enabled check timed out\n");
2652 return -ETIMEDOUT;
2653 }
2654 } else {
2655 _regulator_enable_delay(delay);
2656 }
2657
2658 trace_regulator_enable_complete(rdev_get_name(rdev));
2659
2660 return 0;
2661 }
2662
2663 /**
2664 * _regulator_handle_consumer_enable - handle that a consumer enabled
2665 * @regulator: regulator source
2666 *
2667 * Some things on a regulator consumer (like the contribution towards total
2668 * load on the regulator) only have an effect when the consumer wants the
2669 * regulator enabled. Explained in example with two consumers of the same
2670 * regulator:
2671 * consumer A: set_load(100); => total load = 0
2672 * consumer A: regulator_enable(); => total load = 100
2673 * consumer B: set_load(1000); => total load = 100
2674 * consumer B: regulator_enable(); => total load = 1100
2675 * consumer A: regulator_disable(); => total_load = 1000
2676 *
2677 * This function (together with _regulator_handle_consumer_disable) is
2678 * responsible for keeping track of the refcount for a given regulator consumer
2679 * and applying / unapplying these things.
2680 *
2681 * Returns 0 upon no error; -error upon error.
2682 */
_regulator_handle_consumer_enable(struct regulator * regulator)2683 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2684 {
2685 int ret;
2686 struct regulator_dev *rdev = regulator->rdev;
2687
2688 lockdep_assert_held_once(&rdev->mutex.base);
2689
2690 regulator->enable_count++;
2691 if (regulator->uA_load && regulator->enable_count == 1) {
2692 ret = drms_uA_update(rdev);
2693 if (ret)
2694 regulator->enable_count--;
2695 return ret;
2696 }
2697
2698 return 0;
2699 }
2700
2701 /**
2702 * _regulator_handle_consumer_disable - handle that a consumer disabled
2703 * @regulator: regulator source
2704 *
2705 * The opposite of _regulator_handle_consumer_enable().
2706 *
2707 * Returns 0 upon no error; -error upon error.
2708 */
_regulator_handle_consumer_disable(struct regulator * regulator)2709 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2710 {
2711 struct regulator_dev *rdev = regulator->rdev;
2712
2713 lockdep_assert_held_once(&rdev->mutex.base);
2714
2715 if (!regulator->enable_count) {
2716 rdev_err(rdev, "Underflow of regulator enable count\n");
2717 return -EINVAL;
2718 }
2719
2720 regulator->enable_count--;
2721 if (regulator->uA_load && regulator->enable_count == 0)
2722 return drms_uA_update(rdev);
2723
2724 return 0;
2725 }
2726
2727 /* locks held by regulator_enable() */
_regulator_enable(struct regulator * regulator)2728 static int _regulator_enable(struct regulator *regulator)
2729 {
2730 struct regulator_dev *rdev = regulator->rdev;
2731 int ret;
2732
2733 lockdep_assert_held_once(&rdev->mutex.base);
2734
2735 if (rdev->use_count == 0 && rdev->supply) {
2736 ret = _regulator_enable(rdev->supply);
2737 if (ret < 0)
2738 return ret;
2739 }
2740
2741 /* balance only if there are regulators coupled */
2742 if (rdev->coupling_desc.n_coupled > 1) {
2743 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2744 if (ret < 0)
2745 goto err_disable_supply;
2746 }
2747
2748 ret = _regulator_handle_consumer_enable(regulator);
2749 if (ret < 0)
2750 goto err_disable_supply;
2751
2752 if (rdev->use_count == 0) {
2753 /* The regulator may on if it's not switchable or left on */
2754 ret = _regulator_is_enabled(rdev);
2755 if (ret == -EINVAL || ret == 0) {
2756 if (!regulator_ops_is_valid(rdev,
2757 REGULATOR_CHANGE_STATUS)) {
2758 ret = -EPERM;
2759 goto err_consumer_disable;
2760 }
2761
2762 ret = _regulator_do_enable(rdev);
2763 if (ret < 0)
2764 goto err_consumer_disable;
2765
2766 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2767 NULL);
2768 } else if (ret < 0) {
2769 rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
2770 goto err_consumer_disable;
2771 }
2772 /* Fallthrough on positive return values - already enabled */
2773 }
2774
2775 rdev->use_count++;
2776
2777 return 0;
2778
2779 err_consumer_disable:
2780 _regulator_handle_consumer_disable(regulator);
2781
2782 err_disable_supply:
2783 if (rdev->use_count == 0 && rdev->supply)
2784 _regulator_disable(rdev->supply);
2785
2786 return ret;
2787 }
2788
2789 /**
2790 * regulator_enable - enable regulator output
2791 * @regulator: regulator source
2792 *
2793 * Request that the regulator be enabled with the regulator output at
2794 * the predefined voltage or current value. Calls to regulator_enable()
2795 * must be balanced with calls to regulator_disable().
2796 *
2797 * NOTE: the output value can be set by other drivers, boot loader or may be
2798 * hardwired in the regulator.
2799 */
regulator_enable(struct regulator * regulator)2800 int regulator_enable(struct regulator *regulator)
2801 {
2802 struct regulator_dev *rdev = regulator->rdev;
2803 struct ww_acquire_ctx ww_ctx;
2804 int ret;
2805
2806 regulator_lock_dependent(rdev, &ww_ctx);
2807 ret = _regulator_enable(regulator);
2808 regulator_unlock_dependent(rdev, &ww_ctx);
2809
2810 return ret;
2811 }
2812 EXPORT_SYMBOL_GPL(regulator_enable);
2813
_regulator_do_disable(struct regulator_dev * rdev)2814 static int _regulator_do_disable(struct regulator_dev *rdev)
2815 {
2816 int ret;
2817
2818 trace_regulator_disable(rdev_get_name(rdev));
2819
2820 if (rdev->ena_pin) {
2821 if (rdev->ena_gpio_state) {
2822 ret = regulator_ena_gpio_ctrl(rdev, false);
2823 if (ret < 0)
2824 return ret;
2825 rdev->ena_gpio_state = 0;
2826 }
2827
2828 } else if (rdev->desc->ops->disable) {
2829 ret = rdev->desc->ops->disable(rdev);
2830 if (ret != 0)
2831 return ret;
2832 }
2833
2834 /* cares about last_off_jiffy only if off_on_delay is required by
2835 * device.
2836 */
2837 if (rdev->desc->off_on_delay)
2838 rdev->last_off_jiffy = jiffies;
2839
2840 trace_regulator_disable_complete(rdev_get_name(rdev));
2841
2842 return 0;
2843 }
2844
2845 /* locks held by regulator_disable() */
_regulator_disable(struct regulator * regulator)2846 static int _regulator_disable(struct regulator *regulator)
2847 {
2848 struct regulator_dev *rdev = regulator->rdev;
2849 int ret = 0;
2850
2851 lockdep_assert_held_once(&rdev->mutex.base);
2852
2853 if (WARN(rdev->use_count <= 0,
2854 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2855 return -EIO;
2856
2857 /* are we the last user and permitted to disable ? */
2858 if (rdev->use_count == 1 &&
2859 (rdev->constraints && !rdev->constraints->always_on)) {
2860
2861 /* we are last user */
2862 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2863 ret = _notifier_call_chain(rdev,
2864 REGULATOR_EVENT_PRE_DISABLE,
2865 NULL);
2866 if (ret & NOTIFY_STOP_MASK)
2867 return -EINVAL;
2868
2869 ret = _regulator_do_disable(rdev);
2870 if (ret < 0) {
2871 rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
2872 _notifier_call_chain(rdev,
2873 REGULATOR_EVENT_ABORT_DISABLE,
2874 NULL);
2875 return ret;
2876 }
2877 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2878 NULL);
2879 }
2880
2881 rdev->use_count = 0;
2882 } else if (rdev->use_count > 1) {
2883 rdev->use_count--;
2884 }
2885
2886 if (ret == 0)
2887 ret = _regulator_handle_consumer_disable(regulator);
2888
2889 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2890 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2891
2892 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2893 ret = _regulator_disable(rdev->supply);
2894
2895 return ret;
2896 }
2897
2898 /**
2899 * regulator_disable - disable regulator output
2900 * @regulator: regulator source
2901 *
2902 * Disable the regulator output voltage or current. Calls to
2903 * regulator_enable() must be balanced with calls to
2904 * regulator_disable().
2905 *
2906 * NOTE: this will only disable the regulator output if no other consumer
2907 * devices have it enabled, the regulator device supports disabling and
2908 * machine constraints permit this operation.
2909 */
regulator_disable(struct regulator * regulator)2910 int regulator_disable(struct regulator *regulator)
2911 {
2912 struct regulator_dev *rdev = regulator->rdev;
2913 struct ww_acquire_ctx ww_ctx;
2914 int ret;
2915
2916 regulator_lock_dependent(rdev, &ww_ctx);
2917 ret = _regulator_disable(regulator);
2918 regulator_unlock_dependent(rdev, &ww_ctx);
2919
2920 return ret;
2921 }
2922 EXPORT_SYMBOL_GPL(regulator_disable);
2923
2924 /* locks held by regulator_force_disable() */
_regulator_force_disable(struct regulator_dev * rdev)2925 static int _regulator_force_disable(struct regulator_dev *rdev)
2926 {
2927 int ret = 0;
2928
2929 lockdep_assert_held_once(&rdev->mutex.base);
2930
2931 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2932 REGULATOR_EVENT_PRE_DISABLE, NULL);
2933 if (ret & NOTIFY_STOP_MASK)
2934 return -EINVAL;
2935
2936 ret = _regulator_do_disable(rdev);
2937 if (ret < 0) {
2938 rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
2939 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2940 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2941 return ret;
2942 }
2943
2944 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2945 REGULATOR_EVENT_DISABLE, NULL);
2946
2947 return 0;
2948 }
2949
2950 /**
2951 * regulator_force_disable - force disable regulator output
2952 * @regulator: regulator source
2953 *
2954 * Forcibly disable the regulator output voltage or current.
2955 * NOTE: this *will* disable the regulator output even if other consumer
2956 * devices have it enabled. This should be used for situations when device
2957 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2958 */
regulator_force_disable(struct regulator * regulator)2959 int regulator_force_disable(struct regulator *regulator)
2960 {
2961 struct regulator_dev *rdev = regulator->rdev;
2962 struct ww_acquire_ctx ww_ctx;
2963 int ret;
2964
2965 regulator_lock_dependent(rdev, &ww_ctx);
2966
2967 ret = _regulator_force_disable(regulator->rdev);
2968
2969 if (rdev->coupling_desc.n_coupled > 1)
2970 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2971
2972 if (regulator->uA_load) {
2973 regulator->uA_load = 0;
2974 ret = drms_uA_update(rdev);
2975 }
2976
2977 if (rdev->use_count != 0 && rdev->supply)
2978 _regulator_disable(rdev->supply);
2979
2980 regulator_unlock_dependent(rdev, &ww_ctx);
2981
2982 return ret;
2983 }
2984 EXPORT_SYMBOL_GPL(regulator_force_disable);
2985
regulator_disable_work(struct work_struct * work)2986 static void regulator_disable_work(struct work_struct *work)
2987 {
2988 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2989 disable_work.work);
2990 struct ww_acquire_ctx ww_ctx;
2991 int count, i, ret;
2992 struct regulator *regulator;
2993 int total_count = 0;
2994
2995 regulator_lock_dependent(rdev, &ww_ctx);
2996
2997 /*
2998 * Workqueue functions queue the new work instance while the previous
2999 * work instance is being processed. Cancel the queued work instance
3000 * as the work instance under processing does the job of the queued
3001 * work instance.
3002 */
3003 cancel_delayed_work(&rdev->disable_work);
3004
3005 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3006 count = regulator->deferred_disables;
3007
3008 if (!count)
3009 continue;
3010
3011 total_count += count;
3012 regulator->deferred_disables = 0;
3013
3014 for (i = 0; i < count; i++) {
3015 ret = _regulator_disable(regulator);
3016 if (ret != 0)
3017 rdev_err(rdev, "Deferred disable failed: %pe\n",
3018 ERR_PTR(ret));
3019 }
3020 }
3021 WARN_ON(!total_count);
3022
3023 if (rdev->coupling_desc.n_coupled > 1)
3024 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3025
3026 regulator_unlock_dependent(rdev, &ww_ctx);
3027 }
3028
3029 /**
3030 * regulator_disable_deferred - disable regulator output with delay
3031 * @regulator: regulator source
3032 * @ms: milliseconds until the regulator is disabled
3033 *
3034 * Execute regulator_disable() on the regulator after a delay. This
3035 * is intended for use with devices that require some time to quiesce.
3036 *
3037 * NOTE: this will only disable the regulator output if no other consumer
3038 * devices have it enabled, the regulator device supports disabling and
3039 * machine constraints permit this operation.
3040 */
regulator_disable_deferred(struct regulator * regulator,int ms)3041 int regulator_disable_deferred(struct regulator *regulator, int ms)
3042 {
3043 struct regulator_dev *rdev = regulator->rdev;
3044
3045 if (!ms)
3046 return regulator_disable(regulator);
3047
3048 regulator_lock(rdev);
3049 regulator->deferred_disables++;
3050 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
3051 msecs_to_jiffies(ms));
3052 regulator_unlock(rdev);
3053
3054 return 0;
3055 }
3056 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
3057
_regulator_is_enabled(struct regulator_dev * rdev)3058 static int _regulator_is_enabled(struct regulator_dev *rdev)
3059 {
3060 /* A GPIO control always takes precedence */
3061 if (rdev->ena_pin)
3062 return rdev->ena_gpio_state;
3063
3064 /* If we don't know then assume that the regulator is always on */
3065 if (!rdev->desc->ops->is_enabled)
3066 return 1;
3067
3068 return rdev->desc->ops->is_enabled(rdev);
3069 }
3070
_regulator_list_voltage(struct regulator_dev * rdev,unsigned selector,int lock)3071 static int _regulator_list_voltage(struct regulator_dev *rdev,
3072 unsigned selector, int lock)
3073 {
3074 const struct regulator_ops *ops = rdev->desc->ops;
3075 int ret;
3076
3077 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
3078 return rdev->desc->fixed_uV;
3079
3080 if (ops->list_voltage) {
3081 if (selector >= rdev->desc->n_voltages)
3082 return -EINVAL;
3083 if (lock)
3084 regulator_lock(rdev);
3085 ret = ops->list_voltage(rdev, selector);
3086 if (lock)
3087 regulator_unlock(rdev);
3088 } else if (rdev->is_switch && rdev->supply) {
3089 ret = _regulator_list_voltage(rdev->supply->rdev,
3090 selector, lock);
3091 } else {
3092 return -EINVAL;
3093 }
3094
3095 if (ret > 0) {
3096 if (ret < rdev->constraints->min_uV)
3097 ret = 0;
3098 else if (ret > rdev->constraints->max_uV)
3099 ret = 0;
3100 }
3101
3102 return ret;
3103 }
3104
3105 /**
3106 * regulator_is_enabled - is the regulator output enabled
3107 * @regulator: regulator source
3108 *
3109 * Returns positive if the regulator driver backing the source/client
3110 * has requested that the device be enabled, zero if it hasn't, else a
3111 * negative errno code.
3112 *
3113 * Note that the device backing this regulator handle can have multiple
3114 * users, so it might be enabled even if regulator_enable() was never
3115 * called for this particular source.
3116 */
regulator_is_enabled(struct regulator * regulator)3117 int regulator_is_enabled(struct regulator *regulator)
3118 {
3119 int ret;
3120
3121 if (regulator->always_on)
3122 return 1;
3123
3124 regulator_lock(regulator->rdev);
3125 ret = _regulator_is_enabled(regulator->rdev);
3126 regulator_unlock(regulator->rdev);
3127
3128 return ret;
3129 }
3130 EXPORT_SYMBOL_GPL(regulator_is_enabled);
3131
3132 /**
3133 * regulator_count_voltages - count regulator_list_voltage() selectors
3134 * @regulator: regulator source
3135 *
3136 * Returns number of selectors, or negative errno. Selectors are
3137 * numbered starting at zero, and typically correspond to bitfields
3138 * in hardware registers.
3139 */
regulator_count_voltages(struct regulator * regulator)3140 int regulator_count_voltages(struct regulator *regulator)
3141 {
3142 struct regulator_dev *rdev = regulator->rdev;
3143
3144 if (rdev->desc->n_voltages)
3145 return rdev->desc->n_voltages;
3146
3147 if (!rdev->is_switch || !rdev->supply)
3148 return -EINVAL;
3149
3150 return regulator_count_voltages(rdev->supply);
3151 }
3152 EXPORT_SYMBOL_GPL(regulator_count_voltages);
3153
3154 /**
3155 * regulator_list_voltage - enumerate supported voltages
3156 * @regulator: regulator source
3157 * @selector: identify voltage to list
3158 * Context: can sleep
3159 *
3160 * Returns a voltage that can be passed to @regulator_set_voltage(),
3161 * zero if this selector code can't be used on this system, or a
3162 * negative errno.
3163 */
regulator_list_voltage(struct regulator * regulator,unsigned selector)3164 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
3165 {
3166 return _regulator_list_voltage(regulator->rdev, selector, 1);
3167 }
3168 EXPORT_SYMBOL_GPL(regulator_list_voltage);
3169
3170 /**
3171 * regulator_get_regmap - get the regulator's register map
3172 * @regulator: regulator source
3173 *
3174 * Returns the register map for the given regulator, or an ERR_PTR value
3175 * if the regulator doesn't use regmap.
3176 */
regulator_get_regmap(struct regulator * regulator)3177 struct regmap *regulator_get_regmap(struct regulator *regulator)
3178 {
3179 struct regmap *map = regulator->rdev->regmap;
3180
3181 return map ? map : ERR_PTR(-EOPNOTSUPP);
3182 }
3183
3184 /**
3185 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3186 * @regulator: regulator source
3187 * @vsel_reg: voltage selector register, output parameter
3188 * @vsel_mask: mask for voltage selector bitfield, output parameter
3189 *
3190 * Returns the hardware register offset and bitmask used for setting the
3191 * regulator voltage. This might be useful when configuring voltage-scaling
3192 * hardware or firmware that can make I2C requests behind the kernel's back,
3193 * for example.
3194 *
3195 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3196 * and 0 is returned, otherwise a negative errno is returned.
3197 */
regulator_get_hardware_vsel_register(struct regulator * regulator,unsigned * vsel_reg,unsigned * vsel_mask)3198 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3199 unsigned *vsel_reg,
3200 unsigned *vsel_mask)
3201 {
3202 struct regulator_dev *rdev = regulator->rdev;
3203 const struct regulator_ops *ops = rdev->desc->ops;
3204
3205 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3206 return -EOPNOTSUPP;
3207
3208 *vsel_reg = rdev->desc->vsel_reg;
3209 *vsel_mask = rdev->desc->vsel_mask;
3210
3211 return 0;
3212 }
3213 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3214
3215 /**
3216 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3217 * @regulator: regulator source
3218 * @selector: identify voltage to list
3219 *
3220 * Converts the selector to a hardware-specific voltage selector that can be
3221 * directly written to the regulator registers. The address of the voltage
3222 * register can be determined by calling @regulator_get_hardware_vsel_register.
3223 *
3224 * On error a negative errno is returned.
3225 */
regulator_list_hardware_vsel(struct regulator * regulator,unsigned selector)3226 int regulator_list_hardware_vsel(struct regulator *regulator,
3227 unsigned selector)
3228 {
3229 struct regulator_dev *rdev = regulator->rdev;
3230 const struct regulator_ops *ops = rdev->desc->ops;
3231
3232 if (selector >= rdev->desc->n_voltages)
3233 return -EINVAL;
3234 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3235 return -EOPNOTSUPP;
3236
3237 return selector;
3238 }
3239 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3240
3241 /**
3242 * regulator_get_linear_step - return the voltage step size between VSEL values
3243 * @regulator: regulator source
3244 *
3245 * Returns the voltage step size between VSEL values for linear
3246 * regulators, or return 0 if the regulator isn't a linear regulator.
3247 */
regulator_get_linear_step(struct regulator * regulator)3248 unsigned int regulator_get_linear_step(struct regulator *regulator)
3249 {
3250 struct regulator_dev *rdev = regulator->rdev;
3251
3252 return rdev->desc->uV_step;
3253 }
3254 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3255
3256 /**
3257 * regulator_is_supported_voltage - check if a voltage range can be supported
3258 *
3259 * @regulator: Regulator to check.
3260 * @min_uV: Minimum required voltage in uV.
3261 * @max_uV: Maximum required voltage in uV.
3262 *
3263 * Returns a boolean.
3264 */
regulator_is_supported_voltage(struct regulator * regulator,int min_uV,int max_uV)3265 int regulator_is_supported_voltage(struct regulator *regulator,
3266 int min_uV, int max_uV)
3267 {
3268 struct regulator_dev *rdev = regulator->rdev;
3269 int i, voltages, ret;
3270
3271 /* If we can't change voltage check the current voltage */
3272 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3273 ret = regulator_get_voltage(regulator);
3274 if (ret >= 0)
3275 return min_uV <= ret && ret <= max_uV;
3276 else
3277 return ret;
3278 }
3279
3280 /* Any voltage within constrains range is fine? */
3281 if (rdev->desc->continuous_voltage_range)
3282 return min_uV >= rdev->constraints->min_uV &&
3283 max_uV <= rdev->constraints->max_uV;
3284
3285 ret = regulator_count_voltages(regulator);
3286 if (ret < 0)
3287 return 0;
3288 voltages = ret;
3289
3290 for (i = 0; i < voltages; i++) {
3291 ret = regulator_list_voltage(regulator, i);
3292
3293 if (ret >= min_uV && ret <= max_uV)
3294 return 1;
3295 }
3296
3297 return 0;
3298 }
3299 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3300
regulator_map_voltage(struct regulator_dev * rdev,int min_uV,int max_uV)3301 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3302 int max_uV)
3303 {
3304 const struct regulator_desc *desc = rdev->desc;
3305
3306 if (desc->ops->map_voltage)
3307 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3308
3309 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3310 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3311
3312 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3313 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3314
3315 if (desc->ops->list_voltage ==
3316 regulator_list_voltage_pickable_linear_range)
3317 return regulator_map_voltage_pickable_linear_range(rdev,
3318 min_uV, max_uV);
3319
3320 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3321 }
3322
_regulator_call_set_voltage(struct regulator_dev * rdev,int min_uV,int max_uV,unsigned * selector)3323 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3324 int min_uV, int max_uV,
3325 unsigned *selector)
3326 {
3327 struct pre_voltage_change_data data;
3328 int ret;
3329
3330 data.old_uV = regulator_get_voltage_rdev(rdev);
3331 data.min_uV = min_uV;
3332 data.max_uV = max_uV;
3333 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3334 &data);
3335 if (ret & NOTIFY_STOP_MASK)
3336 return -EINVAL;
3337
3338 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3339 if (ret >= 0)
3340 return ret;
3341
3342 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3343 (void *)data.old_uV);
3344
3345 return ret;
3346 }
3347
_regulator_call_set_voltage_sel(struct regulator_dev * rdev,int uV,unsigned selector)3348 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3349 int uV, unsigned selector)
3350 {
3351 struct pre_voltage_change_data data;
3352 int ret;
3353
3354 data.old_uV = regulator_get_voltage_rdev(rdev);
3355 data.min_uV = uV;
3356 data.max_uV = uV;
3357 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3358 &data);
3359 if (ret & NOTIFY_STOP_MASK)
3360 return -EINVAL;
3361
3362 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3363 if (ret >= 0)
3364 return ret;
3365
3366 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3367 (void *)data.old_uV);
3368
3369 return ret;
3370 }
3371
_regulator_set_voltage_sel_step(struct regulator_dev * rdev,int uV,int new_selector)3372 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3373 int uV, int new_selector)
3374 {
3375 const struct regulator_ops *ops = rdev->desc->ops;
3376 int diff, old_sel, curr_sel, ret;
3377
3378 /* Stepping is only needed if the regulator is enabled. */
3379 if (!_regulator_is_enabled(rdev))
3380 goto final_set;
3381
3382 if (!ops->get_voltage_sel)
3383 return -EINVAL;
3384
3385 old_sel = ops->get_voltage_sel(rdev);
3386 if (old_sel < 0)
3387 return old_sel;
3388
3389 diff = new_selector - old_sel;
3390 if (diff == 0)
3391 return 0; /* No change needed. */
3392
3393 if (diff > 0) {
3394 /* Stepping up. */
3395 for (curr_sel = old_sel + rdev->desc->vsel_step;
3396 curr_sel < new_selector;
3397 curr_sel += rdev->desc->vsel_step) {
3398 /*
3399 * Call the callback directly instead of using
3400 * _regulator_call_set_voltage_sel() as we don't
3401 * want to notify anyone yet. Same in the branch
3402 * below.
3403 */
3404 ret = ops->set_voltage_sel(rdev, curr_sel);
3405 if (ret)
3406 goto try_revert;
3407 }
3408 } else {
3409 /* Stepping down. */
3410 for (curr_sel = old_sel - rdev->desc->vsel_step;
3411 curr_sel > new_selector;
3412 curr_sel -= rdev->desc->vsel_step) {
3413 ret = ops->set_voltage_sel(rdev, curr_sel);
3414 if (ret)
3415 goto try_revert;
3416 }
3417 }
3418
3419 final_set:
3420 /* The final selector will trigger the notifiers. */
3421 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3422
3423 try_revert:
3424 /*
3425 * At least try to return to the previous voltage if setting a new
3426 * one failed.
3427 */
3428 (void)ops->set_voltage_sel(rdev, old_sel);
3429 return ret;
3430 }
3431
_regulator_set_voltage_time(struct regulator_dev * rdev,int old_uV,int new_uV)3432 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3433 int old_uV, int new_uV)
3434 {
3435 unsigned int ramp_delay = 0;
3436
3437 if (rdev->constraints->ramp_delay)
3438 ramp_delay = rdev->constraints->ramp_delay;
3439 else if (rdev->desc->ramp_delay)
3440 ramp_delay = rdev->desc->ramp_delay;
3441 else if (rdev->constraints->settling_time)
3442 return rdev->constraints->settling_time;
3443 else if (rdev->constraints->settling_time_up &&
3444 (new_uV > old_uV))
3445 return rdev->constraints->settling_time_up;
3446 else if (rdev->constraints->settling_time_down &&
3447 (new_uV < old_uV))
3448 return rdev->constraints->settling_time_down;
3449
3450 if (ramp_delay == 0) {
3451 rdev_dbg(rdev, "ramp_delay not set\n");
3452 return 0;
3453 }
3454
3455 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3456 }
3457
_regulator_do_set_voltage(struct regulator_dev * rdev,int min_uV,int max_uV)3458 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3459 int min_uV, int max_uV)
3460 {
3461 int ret;
3462 int delay = 0;
3463 int best_val = 0;
3464 unsigned int selector;
3465 int old_selector = -1;
3466 const struct regulator_ops *ops = rdev->desc->ops;
3467 int old_uV = regulator_get_voltage_rdev(rdev);
3468
3469 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3470
3471 min_uV += rdev->constraints->uV_offset;
3472 max_uV += rdev->constraints->uV_offset;
3473
3474 /*
3475 * If we can't obtain the old selector there is not enough
3476 * info to call set_voltage_time_sel().
3477 */
3478 if (_regulator_is_enabled(rdev) &&
3479 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3480 old_selector = ops->get_voltage_sel(rdev);
3481 if (old_selector < 0)
3482 return old_selector;
3483 }
3484
3485 if (ops->set_voltage) {
3486 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3487 &selector);
3488
3489 if (ret >= 0) {
3490 if (ops->list_voltage)
3491 best_val = ops->list_voltage(rdev,
3492 selector);
3493 else
3494 best_val = regulator_get_voltage_rdev(rdev);
3495 }
3496
3497 } else if (ops->set_voltage_sel) {
3498 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3499 if (ret >= 0) {
3500 best_val = ops->list_voltage(rdev, ret);
3501 if (min_uV <= best_val && max_uV >= best_val) {
3502 selector = ret;
3503 if (old_selector == selector)
3504 ret = 0;
3505 else if (rdev->desc->vsel_step)
3506 ret = _regulator_set_voltage_sel_step(
3507 rdev, best_val, selector);
3508 else
3509 ret = _regulator_call_set_voltage_sel(
3510 rdev, best_val, selector);
3511 } else {
3512 ret = -EINVAL;
3513 }
3514 }
3515 } else {
3516 ret = -EINVAL;
3517 }
3518
3519 if (ret)
3520 goto out;
3521
3522 if (ops->set_voltage_time_sel) {
3523 /*
3524 * Call set_voltage_time_sel if successfully obtained
3525 * old_selector
3526 */
3527 if (old_selector >= 0 && old_selector != selector)
3528 delay = ops->set_voltage_time_sel(rdev, old_selector,
3529 selector);
3530 } else {
3531 if (old_uV != best_val) {
3532 if (ops->set_voltage_time)
3533 delay = ops->set_voltage_time(rdev, old_uV,
3534 best_val);
3535 else
3536 delay = _regulator_set_voltage_time(rdev,
3537 old_uV,
3538 best_val);
3539 }
3540 }
3541
3542 if (delay < 0) {
3543 rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
3544 delay = 0;
3545 }
3546
3547 /* Insert any necessary delays */
3548 if (delay >= 1000) {
3549 mdelay(delay / 1000);
3550 udelay(delay % 1000);
3551 } else if (delay) {
3552 udelay(delay);
3553 }
3554
3555 if (best_val >= 0) {
3556 unsigned long data = best_val;
3557
3558 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3559 (void *)data);
3560 }
3561
3562 out:
3563 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3564
3565 return ret;
3566 }
3567
_regulator_do_set_suspend_voltage(struct regulator_dev * rdev,int min_uV,int max_uV,suspend_state_t state)3568 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3569 int min_uV, int max_uV, suspend_state_t state)
3570 {
3571 struct regulator_state *rstate;
3572 int uV, sel;
3573
3574 rstate = regulator_get_suspend_state(rdev, state);
3575 if (rstate == NULL)
3576 return -EINVAL;
3577
3578 if (min_uV < rstate->min_uV)
3579 min_uV = rstate->min_uV;
3580 if (max_uV > rstate->max_uV)
3581 max_uV = rstate->max_uV;
3582
3583 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3584 if (sel < 0)
3585 return sel;
3586
3587 uV = rdev->desc->ops->list_voltage(rdev, sel);
3588 if (uV >= min_uV && uV <= max_uV)
3589 rstate->uV = uV;
3590
3591 return 0;
3592 }
3593
regulator_set_voltage_unlocked(struct regulator * regulator,int min_uV,int max_uV,suspend_state_t state)3594 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3595 int min_uV, int max_uV,
3596 suspend_state_t state)
3597 {
3598 struct regulator_dev *rdev = regulator->rdev;
3599 struct regulator_voltage *voltage = ®ulator->voltage[state];
3600 int ret = 0;
3601 int old_min_uV, old_max_uV;
3602 int current_uV;
3603
3604 /* If we're setting the same range as last time the change
3605 * should be a noop (some cpufreq implementations use the same
3606 * voltage for multiple frequencies, for example).
3607 */
3608 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3609 goto out;
3610
3611 /* If we're trying to set a range that overlaps the current voltage,
3612 * return successfully even though the regulator does not support
3613 * changing the voltage.
3614 */
3615 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3616 current_uV = regulator_get_voltage_rdev(rdev);
3617 if (min_uV <= current_uV && current_uV <= max_uV) {
3618 voltage->min_uV = min_uV;
3619 voltage->max_uV = max_uV;
3620 goto out;
3621 }
3622 }
3623
3624 /* sanity check */
3625 if (!rdev->desc->ops->set_voltage &&
3626 !rdev->desc->ops->set_voltage_sel) {
3627 ret = -EINVAL;
3628 goto out;
3629 }
3630
3631 /* constraints check */
3632 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3633 if (ret < 0)
3634 goto out;
3635
3636 /* restore original values in case of error */
3637 old_min_uV = voltage->min_uV;
3638 old_max_uV = voltage->max_uV;
3639 voltage->min_uV = min_uV;
3640 voltage->max_uV = max_uV;
3641
3642 /* for not coupled regulators this will just set the voltage */
3643 ret = regulator_balance_voltage(rdev, state);
3644 if (ret < 0) {
3645 voltage->min_uV = old_min_uV;
3646 voltage->max_uV = old_max_uV;
3647 }
3648
3649 out:
3650 return ret;
3651 }
3652
regulator_set_voltage_rdev(struct regulator_dev * rdev,int min_uV,int max_uV,suspend_state_t state)3653 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3654 int max_uV, suspend_state_t state)
3655 {
3656 int best_supply_uV = 0;
3657 int supply_change_uV = 0;
3658 int ret;
3659
3660 if (rdev->supply &&
3661 regulator_ops_is_valid(rdev->supply->rdev,
3662 REGULATOR_CHANGE_VOLTAGE) &&
3663 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3664 rdev->desc->ops->get_voltage_sel))) {
3665 int current_supply_uV;
3666 int selector;
3667
3668 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3669 if (selector < 0) {
3670 ret = selector;
3671 goto out;
3672 }
3673
3674 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3675 if (best_supply_uV < 0) {
3676 ret = best_supply_uV;
3677 goto out;
3678 }
3679
3680 best_supply_uV += rdev->desc->min_dropout_uV;
3681
3682 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3683 if (current_supply_uV < 0) {
3684 ret = current_supply_uV;
3685 goto out;
3686 }
3687
3688 supply_change_uV = best_supply_uV - current_supply_uV;
3689 }
3690
3691 if (supply_change_uV > 0) {
3692 ret = regulator_set_voltage_unlocked(rdev->supply,
3693 best_supply_uV, INT_MAX, state);
3694 if (ret) {
3695 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
3696 ERR_PTR(ret));
3697 goto out;
3698 }
3699 }
3700
3701 if (state == PM_SUSPEND_ON)
3702 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3703 else
3704 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3705 max_uV, state);
3706 if (ret < 0)
3707 goto out;
3708
3709 if (supply_change_uV < 0) {
3710 ret = regulator_set_voltage_unlocked(rdev->supply,
3711 best_supply_uV, INT_MAX, state);
3712 if (ret)
3713 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
3714 ERR_PTR(ret));
3715 /* No need to fail here */
3716 ret = 0;
3717 }
3718
3719 out:
3720 return ret;
3721 }
3722 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3723
regulator_limit_voltage_step(struct regulator_dev * rdev,int * current_uV,int * min_uV)3724 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3725 int *current_uV, int *min_uV)
3726 {
3727 struct regulation_constraints *constraints = rdev->constraints;
3728
3729 /* Limit voltage change only if necessary */
3730 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3731 return 1;
3732
3733 if (*current_uV < 0) {
3734 *current_uV = regulator_get_voltage_rdev(rdev);
3735
3736 if (*current_uV < 0)
3737 return *current_uV;
3738 }
3739
3740 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3741 return 1;
3742
3743 /* Clamp target voltage within the given step */
3744 if (*current_uV < *min_uV)
3745 *min_uV = min(*current_uV + constraints->max_uV_step,
3746 *min_uV);
3747 else
3748 *min_uV = max(*current_uV - constraints->max_uV_step,
3749 *min_uV);
3750
3751 return 0;
3752 }
3753
regulator_get_optimal_voltage(struct regulator_dev * rdev,int * current_uV,int * min_uV,int * max_uV,suspend_state_t state,int n_coupled)3754 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3755 int *current_uV,
3756 int *min_uV, int *max_uV,
3757 suspend_state_t state,
3758 int n_coupled)
3759 {
3760 struct coupling_desc *c_desc = &rdev->coupling_desc;
3761 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3762 struct regulation_constraints *constraints = rdev->constraints;
3763 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3764 int max_current_uV = 0, min_current_uV = INT_MAX;
3765 int highest_min_uV = 0, target_uV, possible_uV;
3766 int i, ret, max_spread;
3767 bool done;
3768
3769 *current_uV = -1;
3770
3771 /*
3772 * If there are no coupled regulators, simply set the voltage
3773 * demanded by consumers.
3774 */
3775 if (n_coupled == 1) {
3776 /*
3777 * If consumers don't provide any demands, set voltage
3778 * to min_uV
3779 */
3780 desired_min_uV = constraints->min_uV;
3781 desired_max_uV = constraints->max_uV;
3782
3783 ret = regulator_check_consumers(rdev,
3784 &desired_min_uV,
3785 &desired_max_uV, state);
3786 if (ret < 0)
3787 return ret;
3788
3789 possible_uV = desired_min_uV;
3790 done = true;
3791
3792 goto finish;
3793 }
3794
3795 /* Find highest min desired voltage */
3796 for (i = 0; i < n_coupled; i++) {
3797 int tmp_min = 0;
3798 int tmp_max = INT_MAX;
3799
3800 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3801
3802 ret = regulator_check_consumers(c_rdevs[i],
3803 &tmp_min,
3804 &tmp_max, state);
3805 if (ret < 0)
3806 return ret;
3807
3808 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3809 if (ret < 0)
3810 return ret;
3811
3812 highest_min_uV = max(highest_min_uV, tmp_min);
3813
3814 if (i == 0) {
3815 desired_min_uV = tmp_min;
3816 desired_max_uV = tmp_max;
3817 }
3818 }
3819
3820 max_spread = constraints->max_spread[0];
3821
3822 /*
3823 * Let target_uV be equal to the desired one if possible.
3824 * If not, set it to minimum voltage, allowed by other coupled
3825 * regulators.
3826 */
3827 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3828
3829 /*
3830 * Find min and max voltages, which currently aren't violating
3831 * max_spread.
3832 */
3833 for (i = 1; i < n_coupled; i++) {
3834 int tmp_act;
3835
3836 if (!_regulator_is_enabled(c_rdevs[i]))
3837 continue;
3838
3839 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3840 if (tmp_act < 0)
3841 return tmp_act;
3842
3843 min_current_uV = min(tmp_act, min_current_uV);
3844 max_current_uV = max(tmp_act, max_current_uV);
3845 }
3846
3847 /* There aren't any other regulators enabled */
3848 if (max_current_uV == 0) {
3849 possible_uV = target_uV;
3850 } else {
3851 /*
3852 * Correct target voltage, so as it currently isn't
3853 * violating max_spread
3854 */
3855 possible_uV = max(target_uV, max_current_uV - max_spread);
3856 possible_uV = min(possible_uV, min_current_uV + max_spread);
3857 }
3858
3859 if (possible_uV > desired_max_uV)
3860 return -EINVAL;
3861
3862 done = (possible_uV == target_uV);
3863 desired_min_uV = possible_uV;
3864
3865 finish:
3866 /* Apply max_uV_step constraint if necessary */
3867 if (state == PM_SUSPEND_ON) {
3868 ret = regulator_limit_voltage_step(rdev, current_uV,
3869 &desired_min_uV);
3870 if (ret < 0)
3871 return ret;
3872
3873 if (ret == 0)
3874 done = false;
3875 }
3876
3877 /* Set current_uV if wasn't done earlier in the code and if necessary */
3878 if (n_coupled > 1 && *current_uV == -1) {
3879
3880 if (_regulator_is_enabled(rdev)) {
3881 ret = regulator_get_voltage_rdev(rdev);
3882 if (ret < 0)
3883 return ret;
3884
3885 *current_uV = ret;
3886 } else {
3887 *current_uV = desired_min_uV;
3888 }
3889 }
3890
3891 *min_uV = desired_min_uV;
3892 *max_uV = desired_max_uV;
3893
3894 return done;
3895 }
3896
regulator_do_balance_voltage(struct regulator_dev * rdev,suspend_state_t state,bool skip_coupled)3897 int regulator_do_balance_voltage(struct regulator_dev *rdev,
3898 suspend_state_t state, bool skip_coupled)
3899 {
3900 struct regulator_dev **c_rdevs;
3901 struct regulator_dev *best_rdev;
3902 struct coupling_desc *c_desc = &rdev->coupling_desc;
3903 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3904 unsigned int delta, best_delta;
3905 unsigned long c_rdev_done = 0;
3906 bool best_c_rdev_done;
3907
3908 c_rdevs = c_desc->coupled_rdevs;
3909 n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
3910
3911 /*
3912 * Find the best possible voltage change on each loop. Leave the loop
3913 * if there isn't any possible change.
3914 */
3915 do {
3916 best_c_rdev_done = false;
3917 best_delta = 0;
3918 best_min_uV = 0;
3919 best_max_uV = 0;
3920 best_c_rdev = 0;
3921 best_rdev = NULL;
3922
3923 /*
3924 * Find highest difference between optimal voltage
3925 * and current voltage.
3926 */
3927 for (i = 0; i < n_coupled; i++) {
3928 /*
3929 * optimal_uV is the best voltage that can be set for
3930 * i-th regulator at the moment without violating
3931 * max_spread constraint in order to balance
3932 * the coupled voltages.
3933 */
3934 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3935
3936 if (test_bit(i, &c_rdev_done))
3937 continue;
3938
3939 ret = regulator_get_optimal_voltage(c_rdevs[i],
3940 ¤t_uV,
3941 &optimal_uV,
3942 &optimal_max_uV,
3943 state, n_coupled);
3944 if (ret < 0)
3945 goto out;
3946
3947 delta = abs(optimal_uV - current_uV);
3948
3949 if (delta && best_delta <= delta) {
3950 best_c_rdev_done = ret;
3951 best_delta = delta;
3952 best_rdev = c_rdevs[i];
3953 best_min_uV = optimal_uV;
3954 best_max_uV = optimal_max_uV;
3955 best_c_rdev = i;
3956 }
3957 }
3958
3959 /* Nothing to change, return successfully */
3960 if (!best_rdev) {
3961 ret = 0;
3962 goto out;
3963 }
3964
3965 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3966 best_max_uV, state);
3967
3968 if (ret < 0)
3969 goto out;
3970
3971 if (best_c_rdev_done)
3972 set_bit(best_c_rdev, &c_rdev_done);
3973
3974 } while (n_coupled > 1);
3975
3976 out:
3977 return ret;
3978 }
3979
regulator_balance_voltage(struct regulator_dev * rdev,suspend_state_t state)3980 static int regulator_balance_voltage(struct regulator_dev *rdev,
3981 suspend_state_t state)
3982 {
3983 struct coupling_desc *c_desc = &rdev->coupling_desc;
3984 struct regulator_coupler *coupler = c_desc->coupler;
3985 bool skip_coupled = false;
3986
3987 /*
3988 * If system is in a state other than PM_SUSPEND_ON, don't check
3989 * other coupled regulators.
3990 */
3991 if (state != PM_SUSPEND_ON)
3992 skip_coupled = true;
3993
3994 if (c_desc->n_resolved < c_desc->n_coupled) {
3995 rdev_err(rdev, "Not all coupled regulators registered\n");
3996 return -EPERM;
3997 }
3998
3999 /* Invoke custom balancer for customized couplers */
4000 if (coupler && coupler->balance_voltage)
4001 return coupler->balance_voltage(coupler, rdev, state);
4002
4003 return regulator_do_balance_voltage(rdev, state, skip_coupled);
4004 }
4005
4006 /**
4007 * regulator_set_voltage - set regulator output voltage
4008 * @regulator: regulator source
4009 * @min_uV: Minimum required voltage in uV
4010 * @max_uV: Maximum acceptable voltage in uV
4011 *
4012 * Sets a voltage regulator to the desired output voltage. This can be set
4013 * during any regulator state. IOW, regulator can be disabled or enabled.
4014 *
4015 * If the regulator is enabled then the voltage will change to the new value
4016 * immediately otherwise if the regulator is disabled the regulator will
4017 * output at the new voltage when enabled.
4018 *
4019 * NOTE: If the regulator is shared between several devices then the lowest
4020 * request voltage that meets the system constraints will be used.
4021 * Regulator system constraints must be set for this regulator before
4022 * calling this function otherwise this call will fail.
4023 */
regulator_set_voltage(struct regulator * regulator,int min_uV,int max_uV)4024 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
4025 {
4026 struct ww_acquire_ctx ww_ctx;
4027 int ret;
4028
4029 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4030
4031 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
4032 PM_SUSPEND_ON);
4033
4034 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4035
4036 return ret;
4037 }
4038 EXPORT_SYMBOL_GPL(regulator_set_voltage);
4039
regulator_suspend_toggle(struct regulator_dev * rdev,suspend_state_t state,bool en)4040 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
4041 suspend_state_t state, bool en)
4042 {
4043 struct regulator_state *rstate;
4044
4045 rstate = regulator_get_suspend_state(rdev, state);
4046 if (rstate == NULL)
4047 return -EINVAL;
4048
4049 if (!rstate->changeable)
4050 return -EPERM;
4051
4052 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
4053
4054 return 0;
4055 }
4056
regulator_suspend_enable(struct regulator_dev * rdev,suspend_state_t state)4057 int regulator_suspend_enable(struct regulator_dev *rdev,
4058 suspend_state_t state)
4059 {
4060 return regulator_suspend_toggle(rdev, state, true);
4061 }
4062 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
4063
regulator_suspend_disable(struct regulator_dev * rdev,suspend_state_t state)4064 int regulator_suspend_disable(struct regulator_dev *rdev,
4065 suspend_state_t state)
4066 {
4067 struct regulator *regulator;
4068 struct regulator_voltage *voltage;
4069
4070 /*
4071 * if any consumer wants this regulator device keeping on in
4072 * suspend states, don't set it as disabled.
4073 */
4074 list_for_each_entry(regulator, &rdev->consumer_list, list) {
4075 voltage = ®ulator->voltage[state];
4076 if (voltage->min_uV || voltage->max_uV)
4077 return 0;
4078 }
4079
4080 return regulator_suspend_toggle(rdev, state, false);
4081 }
4082 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
4083
_regulator_set_suspend_voltage(struct regulator * regulator,int min_uV,int max_uV,suspend_state_t state)4084 static int _regulator_set_suspend_voltage(struct regulator *regulator,
4085 int min_uV, int max_uV,
4086 suspend_state_t state)
4087 {
4088 struct regulator_dev *rdev = regulator->rdev;
4089 struct regulator_state *rstate;
4090
4091 rstate = regulator_get_suspend_state(rdev, state);
4092 if (rstate == NULL)
4093 return -EINVAL;
4094
4095 if (rstate->min_uV == rstate->max_uV) {
4096 rdev_err(rdev, "The suspend voltage can't be changed!\n");
4097 return -EPERM;
4098 }
4099
4100 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
4101 }
4102
regulator_set_suspend_voltage(struct regulator * regulator,int min_uV,int max_uV,suspend_state_t state)4103 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
4104 int max_uV, suspend_state_t state)
4105 {
4106 struct ww_acquire_ctx ww_ctx;
4107 int ret;
4108
4109 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
4110 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
4111 return -EINVAL;
4112
4113 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4114
4115 ret = _regulator_set_suspend_voltage(regulator, min_uV,
4116 max_uV, state);
4117
4118 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4119
4120 return ret;
4121 }
4122 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
4123
4124 /**
4125 * regulator_set_voltage_time - get raise/fall time
4126 * @regulator: regulator source
4127 * @old_uV: starting voltage in microvolts
4128 * @new_uV: target voltage in microvolts
4129 *
4130 * Provided with the starting and ending voltage, this function attempts to
4131 * calculate the time in microseconds required to rise or fall to this new
4132 * voltage.
4133 */
regulator_set_voltage_time(struct regulator * regulator,int old_uV,int new_uV)4134 int regulator_set_voltage_time(struct regulator *regulator,
4135 int old_uV, int new_uV)
4136 {
4137 struct regulator_dev *rdev = regulator->rdev;
4138 const struct regulator_ops *ops = rdev->desc->ops;
4139 int old_sel = -1;
4140 int new_sel = -1;
4141 int voltage;
4142 int i;
4143
4144 if (ops->set_voltage_time)
4145 return ops->set_voltage_time(rdev, old_uV, new_uV);
4146 else if (!ops->set_voltage_time_sel)
4147 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
4148
4149 /* Currently requires operations to do this */
4150 if (!ops->list_voltage || !rdev->desc->n_voltages)
4151 return -EINVAL;
4152
4153 for (i = 0; i < rdev->desc->n_voltages; i++) {
4154 /* We only look for exact voltage matches here */
4155
4156 if (old_sel >= 0 && new_sel >= 0)
4157 break;
4158
4159 voltage = regulator_list_voltage(regulator, i);
4160 if (voltage < 0)
4161 return -EINVAL;
4162 if (voltage == 0)
4163 continue;
4164 if (voltage == old_uV)
4165 old_sel = i;
4166 if (voltage == new_uV)
4167 new_sel = i;
4168 }
4169
4170 if (old_sel < 0 || new_sel < 0)
4171 return -EINVAL;
4172
4173 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
4174 }
4175 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
4176
4177 /**
4178 * regulator_set_voltage_time_sel - get raise/fall time
4179 * @rdev: regulator source device
4180 * @old_selector: selector for starting voltage
4181 * @new_selector: selector for target voltage
4182 *
4183 * Provided with the starting and target voltage selectors, this function
4184 * returns time in microseconds required to rise or fall to this new voltage
4185 *
4186 * Drivers providing ramp_delay in regulation_constraints can use this as their
4187 * set_voltage_time_sel() operation.
4188 */
regulator_set_voltage_time_sel(struct regulator_dev * rdev,unsigned int old_selector,unsigned int new_selector)4189 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
4190 unsigned int old_selector,
4191 unsigned int new_selector)
4192 {
4193 int old_volt, new_volt;
4194
4195 /* sanity check */
4196 if (!rdev->desc->ops->list_voltage)
4197 return -EINVAL;
4198
4199 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
4200 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4201
4202 if (rdev->desc->ops->set_voltage_time)
4203 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4204 new_volt);
4205 else
4206 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4207 }
4208 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4209
4210 /**
4211 * regulator_sync_voltage - re-apply last regulator output voltage
4212 * @regulator: regulator source
4213 *
4214 * Re-apply the last configured voltage. This is intended to be used
4215 * where some external control source the consumer is cooperating with
4216 * has caused the configured voltage to change.
4217 */
regulator_sync_voltage(struct regulator * regulator)4218 int regulator_sync_voltage(struct regulator *regulator)
4219 {
4220 struct regulator_dev *rdev = regulator->rdev;
4221 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
4222 int ret, min_uV, max_uV;
4223
4224 regulator_lock(rdev);
4225
4226 if (!rdev->desc->ops->set_voltage &&
4227 !rdev->desc->ops->set_voltage_sel) {
4228 ret = -EINVAL;
4229 goto out;
4230 }
4231
4232 /* This is only going to work if we've had a voltage configured. */
4233 if (!voltage->min_uV && !voltage->max_uV) {
4234 ret = -EINVAL;
4235 goto out;
4236 }
4237
4238 min_uV = voltage->min_uV;
4239 max_uV = voltage->max_uV;
4240
4241 /* This should be a paranoia check... */
4242 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4243 if (ret < 0)
4244 goto out;
4245
4246 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4247 if (ret < 0)
4248 goto out;
4249
4250 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4251
4252 out:
4253 regulator_unlock(rdev);
4254 return ret;
4255 }
4256 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4257
regulator_get_voltage_rdev(struct regulator_dev * rdev)4258 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4259 {
4260 int sel, ret;
4261 bool bypassed;
4262
4263 if (rdev->desc->ops->get_bypass) {
4264 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4265 if (ret < 0)
4266 return ret;
4267 if (bypassed) {
4268 /* if bypassed the regulator must have a supply */
4269 if (!rdev->supply) {
4270 rdev_err(rdev,
4271 "bypassed regulator has no supply!\n");
4272 return -EPROBE_DEFER;
4273 }
4274
4275 return regulator_get_voltage_rdev(rdev->supply->rdev);
4276 }
4277 }
4278
4279 if (rdev->desc->ops->get_voltage_sel) {
4280 sel = rdev->desc->ops->get_voltage_sel(rdev);
4281 if (sel < 0)
4282 return sel;
4283 ret = rdev->desc->ops->list_voltage(rdev, sel);
4284 } else if (rdev->desc->ops->get_voltage) {
4285 ret = rdev->desc->ops->get_voltage(rdev);
4286 } else if (rdev->desc->ops->list_voltage) {
4287 ret = rdev->desc->ops->list_voltage(rdev, 0);
4288 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4289 ret = rdev->desc->fixed_uV;
4290 } else if (rdev->supply) {
4291 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4292 } else if (rdev->supply_name) {
4293 return -EPROBE_DEFER;
4294 } else {
4295 return -EINVAL;
4296 }
4297
4298 if (ret < 0)
4299 return ret;
4300 return ret - rdev->constraints->uV_offset;
4301 }
4302 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4303
4304 /**
4305 * regulator_get_voltage - get regulator output voltage
4306 * @regulator: regulator source
4307 *
4308 * This returns the current regulator voltage in uV.
4309 *
4310 * NOTE: If the regulator is disabled it will return the voltage value. This
4311 * function should not be used to determine regulator state.
4312 */
regulator_get_voltage(struct regulator * regulator)4313 int regulator_get_voltage(struct regulator *regulator)
4314 {
4315 struct ww_acquire_ctx ww_ctx;
4316 int ret;
4317
4318 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4319 ret = regulator_get_voltage_rdev(regulator->rdev);
4320 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4321
4322 return ret;
4323 }
4324 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4325
4326 /**
4327 * regulator_set_current_limit - set regulator output current limit
4328 * @regulator: regulator source
4329 * @min_uA: Minimum supported current in uA
4330 * @max_uA: Maximum supported current in uA
4331 *
4332 * Sets current sink to the desired output current. This can be set during
4333 * any regulator state. IOW, regulator can be disabled or enabled.
4334 *
4335 * If the regulator is enabled then the current will change to the new value
4336 * immediately otherwise if the regulator is disabled the regulator will
4337 * output at the new current when enabled.
4338 *
4339 * NOTE: Regulator system constraints must be set for this regulator before
4340 * calling this function otherwise this call will fail.
4341 */
regulator_set_current_limit(struct regulator * regulator,int min_uA,int max_uA)4342 int regulator_set_current_limit(struct regulator *regulator,
4343 int min_uA, int max_uA)
4344 {
4345 struct regulator_dev *rdev = regulator->rdev;
4346 int ret;
4347
4348 regulator_lock(rdev);
4349
4350 /* sanity check */
4351 if (!rdev->desc->ops->set_current_limit) {
4352 ret = -EINVAL;
4353 goto out;
4354 }
4355
4356 /* constraints check */
4357 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4358 if (ret < 0)
4359 goto out;
4360
4361 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4362 out:
4363 regulator_unlock(rdev);
4364 return ret;
4365 }
4366 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4367
_regulator_get_current_limit_unlocked(struct regulator_dev * rdev)4368 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4369 {
4370 /* sanity check */
4371 if (!rdev->desc->ops->get_current_limit)
4372 return -EINVAL;
4373
4374 return rdev->desc->ops->get_current_limit(rdev);
4375 }
4376
_regulator_get_current_limit(struct regulator_dev * rdev)4377 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4378 {
4379 int ret;
4380
4381 regulator_lock(rdev);
4382 ret = _regulator_get_current_limit_unlocked(rdev);
4383 regulator_unlock(rdev);
4384
4385 return ret;
4386 }
4387
4388 /**
4389 * regulator_get_current_limit - get regulator output current
4390 * @regulator: regulator source
4391 *
4392 * This returns the current supplied by the specified current sink in uA.
4393 *
4394 * NOTE: If the regulator is disabled it will return the current value. This
4395 * function should not be used to determine regulator state.
4396 */
regulator_get_current_limit(struct regulator * regulator)4397 int regulator_get_current_limit(struct regulator *regulator)
4398 {
4399 return _regulator_get_current_limit(regulator->rdev);
4400 }
4401 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4402
4403 /**
4404 * regulator_set_mode - set regulator operating mode
4405 * @regulator: regulator source
4406 * @mode: operating mode - one of the REGULATOR_MODE constants
4407 *
4408 * Set regulator operating mode to increase regulator efficiency or improve
4409 * regulation performance.
4410 *
4411 * NOTE: Regulator system constraints must be set for this regulator before
4412 * calling this function otherwise this call will fail.
4413 */
regulator_set_mode(struct regulator * regulator,unsigned int mode)4414 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4415 {
4416 struct regulator_dev *rdev = regulator->rdev;
4417 int ret;
4418 int regulator_curr_mode;
4419
4420 regulator_lock(rdev);
4421
4422 /* sanity check */
4423 if (!rdev->desc->ops->set_mode) {
4424 ret = -EINVAL;
4425 goto out;
4426 }
4427
4428 /* return if the same mode is requested */
4429 if (rdev->desc->ops->get_mode) {
4430 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4431 if (regulator_curr_mode == mode) {
4432 ret = 0;
4433 goto out;
4434 }
4435 }
4436
4437 /* constraints check */
4438 ret = regulator_mode_constrain(rdev, &mode);
4439 if (ret < 0)
4440 goto out;
4441
4442 ret = rdev->desc->ops->set_mode(rdev, mode);
4443 out:
4444 regulator_unlock(rdev);
4445 return ret;
4446 }
4447 EXPORT_SYMBOL_GPL(regulator_set_mode);
4448
_regulator_get_mode_unlocked(struct regulator_dev * rdev)4449 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4450 {
4451 /* sanity check */
4452 if (!rdev->desc->ops->get_mode)
4453 return -EINVAL;
4454
4455 return rdev->desc->ops->get_mode(rdev);
4456 }
4457
_regulator_get_mode(struct regulator_dev * rdev)4458 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4459 {
4460 int ret;
4461
4462 regulator_lock(rdev);
4463 ret = _regulator_get_mode_unlocked(rdev);
4464 regulator_unlock(rdev);
4465
4466 return ret;
4467 }
4468
4469 /**
4470 * regulator_get_mode - get regulator operating mode
4471 * @regulator: regulator source
4472 *
4473 * Get the current regulator operating mode.
4474 */
regulator_get_mode(struct regulator * regulator)4475 unsigned int regulator_get_mode(struct regulator *regulator)
4476 {
4477 return _regulator_get_mode(regulator->rdev);
4478 }
4479 EXPORT_SYMBOL_GPL(regulator_get_mode);
4480
_regulator_get_error_flags(struct regulator_dev * rdev,unsigned int * flags)4481 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4482 unsigned int *flags)
4483 {
4484 int ret;
4485
4486 regulator_lock(rdev);
4487
4488 /* sanity check */
4489 if (!rdev->desc->ops->get_error_flags) {
4490 ret = -EINVAL;
4491 goto out;
4492 }
4493
4494 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4495 out:
4496 regulator_unlock(rdev);
4497 return ret;
4498 }
4499
4500 /**
4501 * regulator_get_error_flags - get regulator error information
4502 * @regulator: regulator source
4503 * @flags: pointer to store error flags
4504 *
4505 * Get the current regulator error information.
4506 */
regulator_get_error_flags(struct regulator * regulator,unsigned int * flags)4507 int regulator_get_error_flags(struct regulator *regulator,
4508 unsigned int *flags)
4509 {
4510 return _regulator_get_error_flags(regulator->rdev, flags);
4511 }
4512 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4513
4514 /**
4515 * regulator_set_load - set regulator load
4516 * @regulator: regulator source
4517 * @uA_load: load current
4518 *
4519 * Notifies the regulator core of a new device load. This is then used by
4520 * DRMS (if enabled by constraints) to set the most efficient regulator
4521 * operating mode for the new regulator loading.
4522 *
4523 * Consumer devices notify their supply regulator of the maximum power
4524 * they will require (can be taken from device datasheet in the power
4525 * consumption tables) when they change operational status and hence power
4526 * state. Examples of operational state changes that can affect power
4527 * consumption are :-
4528 *
4529 * o Device is opened / closed.
4530 * o Device I/O is about to begin or has just finished.
4531 * o Device is idling in between work.
4532 *
4533 * This information is also exported via sysfs to userspace.
4534 *
4535 * DRMS will sum the total requested load on the regulator and change
4536 * to the most efficient operating mode if platform constraints allow.
4537 *
4538 * NOTE: when a regulator consumer requests to have a regulator
4539 * disabled then any load that consumer requested no longer counts
4540 * toward the total requested load. If the regulator is re-enabled
4541 * then the previously requested load will start counting again.
4542 *
4543 * If a regulator is an always-on regulator then an individual consumer's
4544 * load will still be removed if that consumer is fully disabled.
4545 *
4546 * On error a negative errno is returned.
4547 */
regulator_set_load(struct regulator * regulator,int uA_load)4548 int regulator_set_load(struct regulator *regulator, int uA_load)
4549 {
4550 struct regulator_dev *rdev = regulator->rdev;
4551 int old_uA_load;
4552 int ret = 0;
4553
4554 regulator_lock(rdev);
4555 old_uA_load = regulator->uA_load;
4556 regulator->uA_load = uA_load;
4557 if (regulator->enable_count && old_uA_load != uA_load) {
4558 ret = drms_uA_update(rdev);
4559 if (ret < 0)
4560 regulator->uA_load = old_uA_load;
4561 }
4562 regulator_unlock(rdev);
4563
4564 return ret;
4565 }
4566 EXPORT_SYMBOL_GPL(regulator_set_load);
4567
4568 /**
4569 * regulator_allow_bypass - allow the regulator to go into bypass mode
4570 *
4571 * @regulator: Regulator to configure
4572 * @enable: enable or disable bypass mode
4573 *
4574 * Allow the regulator to go into bypass mode if all other consumers
4575 * for the regulator also enable bypass mode and the machine
4576 * constraints allow this. Bypass mode means that the regulator is
4577 * simply passing the input directly to the output with no regulation.
4578 */
regulator_allow_bypass(struct regulator * regulator,bool enable)4579 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4580 {
4581 struct regulator_dev *rdev = regulator->rdev;
4582 const char *name = rdev_get_name(rdev);
4583 int ret = 0;
4584
4585 if (!rdev->desc->ops->set_bypass)
4586 return 0;
4587
4588 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4589 return 0;
4590
4591 regulator_lock(rdev);
4592
4593 if (enable && !regulator->bypass) {
4594 rdev->bypass_count++;
4595
4596 if (rdev->bypass_count == rdev->open_count) {
4597 trace_regulator_bypass_enable(name);
4598
4599 ret = rdev->desc->ops->set_bypass(rdev, enable);
4600 if (ret != 0)
4601 rdev->bypass_count--;
4602 else
4603 trace_regulator_bypass_enable_complete(name);
4604 }
4605
4606 } else if (!enable && regulator->bypass) {
4607 rdev->bypass_count--;
4608
4609 if (rdev->bypass_count != rdev->open_count) {
4610 trace_regulator_bypass_disable(name);
4611
4612 ret = rdev->desc->ops->set_bypass(rdev, enable);
4613 if (ret != 0)
4614 rdev->bypass_count++;
4615 else
4616 trace_regulator_bypass_disable_complete(name);
4617 }
4618 }
4619
4620 if (ret == 0)
4621 regulator->bypass = enable;
4622
4623 regulator_unlock(rdev);
4624
4625 return ret;
4626 }
4627 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4628
4629 /**
4630 * regulator_register_notifier - register regulator event notifier
4631 * @regulator: regulator source
4632 * @nb: notifier block
4633 *
4634 * Register notifier block to receive regulator events.
4635 */
regulator_register_notifier(struct regulator * regulator,struct notifier_block * nb)4636 int regulator_register_notifier(struct regulator *regulator,
4637 struct notifier_block *nb)
4638 {
4639 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4640 nb);
4641 }
4642 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4643
4644 /**
4645 * regulator_unregister_notifier - unregister regulator event notifier
4646 * @regulator: regulator source
4647 * @nb: notifier block
4648 *
4649 * Unregister regulator event notifier block.
4650 */
regulator_unregister_notifier(struct regulator * regulator,struct notifier_block * nb)4651 int regulator_unregister_notifier(struct regulator *regulator,
4652 struct notifier_block *nb)
4653 {
4654 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4655 nb);
4656 }
4657 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4658
4659 /* notify regulator consumers and downstream regulator consumers.
4660 * Note mutex must be held by caller.
4661 */
_notifier_call_chain(struct regulator_dev * rdev,unsigned long event,void * data)4662 static int _notifier_call_chain(struct regulator_dev *rdev,
4663 unsigned long event, void *data)
4664 {
4665 /* call rdev chain first */
4666 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4667 }
4668
4669 /**
4670 * regulator_bulk_get - get multiple regulator consumers
4671 *
4672 * @dev: Device to supply
4673 * @num_consumers: Number of consumers to register
4674 * @consumers: Configuration of consumers; clients are stored here.
4675 *
4676 * @return 0 on success, an errno on failure.
4677 *
4678 * This helper function allows drivers to get several regulator
4679 * consumers in one operation. If any of the regulators cannot be
4680 * acquired then any regulators that were allocated will be freed
4681 * before returning to the caller.
4682 */
regulator_bulk_get(struct device * dev,int num_consumers,struct regulator_bulk_data * consumers)4683 int regulator_bulk_get(struct device *dev, int num_consumers,
4684 struct regulator_bulk_data *consumers)
4685 {
4686 int i;
4687 int ret;
4688
4689 for (i = 0; i < num_consumers; i++)
4690 consumers[i].consumer = NULL;
4691
4692 for (i = 0; i < num_consumers; i++) {
4693 consumers[i].consumer = regulator_get(dev,
4694 consumers[i].supply);
4695 if (IS_ERR(consumers[i].consumer)) {
4696 ret = PTR_ERR(consumers[i].consumer);
4697 consumers[i].consumer = NULL;
4698 goto err;
4699 }
4700 }
4701
4702 return 0;
4703
4704 err:
4705 if (ret != -EPROBE_DEFER)
4706 dev_err(dev, "Failed to get supply '%s': %pe\n",
4707 consumers[i].supply, ERR_PTR(ret));
4708 else
4709 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4710 consumers[i].supply);
4711
4712 while (--i >= 0)
4713 regulator_put(consumers[i].consumer);
4714
4715 return ret;
4716 }
4717 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4718
regulator_bulk_enable_async(void * data,async_cookie_t cookie)4719 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4720 {
4721 struct regulator_bulk_data *bulk = data;
4722
4723 bulk->ret = regulator_enable(bulk->consumer);
4724 }
4725
4726 /**
4727 * regulator_bulk_enable - enable multiple regulator consumers
4728 *
4729 * @num_consumers: Number of consumers
4730 * @consumers: Consumer data; clients are stored here.
4731 * @return 0 on success, an errno on failure
4732 *
4733 * This convenience API allows consumers to enable multiple regulator
4734 * clients in a single API call. If any consumers cannot be enabled
4735 * then any others that were enabled will be disabled again prior to
4736 * return.
4737 */
regulator_bulk_enable(int num_consumers,struct regulator_bulk_data * consumers)4738 int regulator_bulk_enable(int num_consumers,
4739 struct regulator_bulk_data *consumers)
4740 {
4741 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4742 int i;
4743 int ret = 0;
4744
4745 for (i = 0; i < num_consumers; i++) {
4746 async_schedule_domain(regulator_bulk_enable_async,
4747 &consumers[i], &async_domain);
4748 }
4749
4750 async_synchronize_full_domain(&async_domain);
4751
4752 /* If any consumer failed we need to unwind any that succeeded */
4753 for (i = 0; i < num_consumers; i++) {
4754 if (consumers[i].ret != 0) {
4755 ret = consumers[i].ret;
4756 goto err;
4757 }
4758 }
4759
4760 return 0;
4761
4762 err:
4763 for (i = 0; i < num_consumers; i++) {
4764 if (consumers[i].ret < 0)
4765 pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
4766 ERR_PTR(consumers[i].ret));
4767 else
4768 regulator_disable(consumers[i].consumer);
4769 }
4770
4771 return ret;
4772 }
4773 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4774
4775 /**
4776 * regulator_bulk_disable - disable multiple regulator consumers
4777 *
4778 * @num_consumers: Number of consumers
4779 * @consumers: Consumer data; clients are stored here.
4780 * @return 0 on success, an errno on failure
4781 *
4782 * This convenience API allows consumers to disable multiple regulator
4783 * clients in a single API call. If any consumers cannot be disabled
4784 * then any others that were disabled will be enabled again prior to
4785 * return.
4786 */
regulator_bulk_disable(int num_consumers,struct regulator_bulk_data * consumers)4787 int regulator_bulk_disable(int num_consumers,
4788 struct regulator_bulk_data *consumers)
4789 {
4790 int i;
4791 int ret, r;
4792
4793 for (i = num_consumers - 1; i >= 0; --i) {
4794 ret = regulator_disable(consumers[i].consumer);
4795 if (ret != 0)
4796 goto err;
4797 }
4798
4799 return 0;
4800
4801 err:
4802 pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
4803 for (++i; i < num_consumers; ++i) {
4804 r = regulator_enable(consumers[i].consumer);
4805 if (r != 0)
4806 pr_err("Failed to re-enable %s: %pe\n",
4807 consumers[i].supply, ERR_PTR(r));
4808 }
4809
4810 return ret;
4811 }
4812 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4813
4814 /**
4815 * regulator_bulk_force_disable - force disable multiple regulator consumers
4816 *
4817 * @num_consumers: Number of consumers
4818 * @consumers: Consumer data; clients are stored here.
4819 * @return 0 on success, an errno on failure
4820 *
4821 * This convenience API allows consumers to forcibly disable multiple regulator
4822 * clients in a single API call.
4823 * NOTE: This should be used for situations when device damage will
4824 * likely occur if the regulators are not disabled (e.g. over temp).
4825 * Although regulator_force_disable function call for some consumers can
4826 * return error numbers, the function is called for all consumers.
4827 */
regulator_bulk_force_disable(int num_consumers,struct regulator_bulk_data * consumers)4828 int regulator_bulk_force_disable(int num_consumers,
4829 struct regulator_bulk_data *consumers)
4830 {
4831 int i;
4832 int ret = 0;
4833
4834 for (i = 0; i < num_consumers; i++) {
4835 consumers[i].ret =
4836 regulator_force_disable(consumers[i].consumer);
4837
4838 /* Store first error for reporting */
4839 if (consumers[i].ret && !ret)
4840 ret = consumers[i].ret;
4841 }
4842
4843 return ret;
4844 }
4845 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4846
4847 /**
4848 * regulator_bulk_free - free multiple regulator consumers
4849 *
4850 * @num_consumers: Number of consumers
4851 * @consumers: Consumer data; clients are stored here.
4852 *
4853 * This convenience API allows consumers to free multiple regulator
4854 * clients in a single API call.
4855 */
regulator_bulk_free(int num_consumers,struct regulator_bulk_data * consumers)4856 void regulator_bulk_free(int num_consumers,
4857 struct regulator_bulk_data *consumers)
4858 {
4859 int i;
4860
4861 for (i = 0; i < num_consumers; i++) {
4862 regulator_put(consumers[i].consumer);
4863 consumers[i].consumer = NULL;
4864 }
4865 }
4866 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4867
4868 /**
4869 * regulator_notifier_call_chain - call regulator event notifier
4870 * @rdev: regulator source
4871 * @event: notifier block
4872 * @data: callback-specific data.
4873 *
4874 * Called by regulator drivers to notify clients a regulator event has
4875 * occurred.
4876 */
regulator_notifier_call_chain(struct regulator_dev * rdev,unsigned long event,void * data)4877 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4878 unsigned long event, void *data)
4879 {
4880 _notifier_call_chain(rdev, event, data);
4881 return NOTIFY_DONE;
4882
4883 }
4884 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4885
4886 /**
4887 * regulator_mode_to_status - convert a regulator mode into a status
4888 *
4889 * @mode: Mode to convert
4890 *
4891 * Convert a regulator mode into a status.
4892 */
regulator_mode_to_status(unsigned int mode)4893 int regulator_mode_to_status(unsigned int mode)
4894 {
4895 switch (mode) {
4896 case REGULATOR_MODE_FAST:
4897 return REGULATOR_STATUS_FAST;
4898 case REGULATOR_MODE_NORMAL:
4899 return REGULATOR_STATUS_NORMAL;
4900 case REGULATOR_MODE_IDLE:
4901 return REGULATOR_STATUS_IDLE;
4902 case REGULATOR_MODE_STANDBY:
4903 return REGULATOR_STATUS_STANDBY;
4904 default:
4905 return REGULATOR_STATUS_UNDEFINED;
4906 }
4907 }
4908 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4909
4910 static struct attribute *regulator_dev_attrs[] = {
4911 &dev_attr_name.attr,
4912 &dev_attr_num_users.attr,
4913 &dev_attr_type.attr,
4914 &dev_attr_microvolts.attr,
4915 &dev_attr_microamps.attr,
4916 &dev_attr_opmode.attr,
4917 &dev_attr_state.attr,
4918 &dev_attr_status.attr,
4919 &dev_attr_bypass.attr,
4920 &dev_attr_requested_microamps.attr,
4921 &dev_attr_min_microvolts.attr,
4922 &dev_attr_max_microvolts.attr,
4923 &dev_attr_min_microamps.attr,
4924 &dev_attr_max_microamps.attr,
4925 &dev_attr_suspend_standby_state.attr,
4926 &dev_attr_suspend_mem_state.attr,
4927 &dev_attr_suspend_disk_state.attr,
4928 &dev_attr_suspend_standby_microvolts.attr,
4929 &dev_attr_suspend_mem_microvolts.attr,
4930 &dev_attr_suspend_disk_microvolts.attr,
4931 &dev_attr_suspend_standby_mode.attr,
4932 &dev_attr_suspend_mem_mode.attr,
4933 &dev_attr_suspend_disk_mode.attr,
4934 NULL
4935 };
4936
4937 /*
4938 * To avoid cluttering sysfs (and memory) with useless state, only
4939 * create attributes that can be meaningfully displayed.
4940 */
regulator_attr_is_visible(struct kobject * kobj,struct attribute * attr,int idx)4941 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4942 struct attribute *attr, int idx)
4943 {
4944 struct device *dev = kobj_to_dev(kobj);
4945 struct regulator_dev *rdev = dev_to_rdev(dev);
4946 const struct regulator_ops *ops = rdev->desc->ops;
4947 umode_t mode = attr->mode;
4948
4949 /* these three are always present */
4950 if (attr == &dev_attr_name.attr ||
4951 attr == &dev_attr_num_users.attr ||
4952 attr == &dev_attr_type.attr)
4953 return mode;
4954
4955 /* some attributes need specific methods to be displayed */
4956 if (attr == &dev_attr_microvolts.attr) {
4957 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4958 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4959 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4960 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4961 return mode;
4962 return 0;
4963 }
4964
4965 if (attr == &dev_attr_microamps.attr)
4966 return ops->get_current_limit ? mode : 0;
4967
4968 if (attr == &dev_attr_opmode.attr)
4969 return ops->get_mode ? mode : 0;
4970
4971 if (attr == &dev_attr_state.attr)
4972 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4973
4974 if (attr == &dev_attr_status.attr)
4975 return ops->get_status ? mode : 0;
4976
4977 if (attr == &dev_attr_bypass.attr)
4978 return ops->get_bypass ? mode : 0;
4979
4980 /* constraints need specific supporting methods */
4981 if (attr == &dev_attr_min_microvolts.attr ||
4982 attr == &dev_attr_max_microvolts.attr)
4983 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4984
4985 if (attr == &dev_attr_min_microamps.attr ||
4986 attr == &dev_attr_max_microamps.attr)
4987 return ops->set_current_limit ? mode : 0;
4988
4989 if (attr == &dev_attr_suspend_standby_state.attr ||
4990 attr == &dev_attr_suspend_mem_state.attr ||
4991 attr == &dev_attr_suspend_disk_state.attr)
4992 return mode;
4993
4994 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4995 attr == &dev_attr_suspend_mem_microvolts.attr ||
4996 attr == &dev_attr_suspend_disk_microvolts.attr)
4997 return ops->set_suspend_voltage ? mode : 0;
4998
4999 if (attr == &dev_attr_suspend_standby_mode.attr ||
5000 attr == &dev_attr_suspend_mem_mode.attr ||
5001 attr == &dev_attr_suspend_disk_mode.attr)
5002 return ops->set_suspend_mode ? mode : 0;
5003
5004 return mode;
5005 }
5006
5007 static const struct attribute_group regulator_dev_group = {
5008 .attrs = regulator_dev_attrs,
5009 .is_visible = regulator_attr_is_visible,
5010 };
5011
5012 static const struct attribute_group *regulator_dev_groups[] = {
5013 ®ulator_dev_group,
5014 NULL
5015 };
5016
regulator_dev_release(struct device * dev)5017 static void regulator_dev_release(struct device *dev)
5018 {
5019 struct regulator_dev *rdev = dev_get_drvdata(dev);
5020
5021 debugfs_remove_recursive(rdev->debugfs);
5022 kfree(rdev->constraints);
5023 of_node_put(rdev->dev.of_node);
5024 kfree(rdev);
5025 }
5026
rdev_init_debugfs(struct regulator_dev * rdev)5027 static void rdev_init_debugfs(struct regulator_dev *rdev)
5028 {
5029 struct device *parent = rdev->dev.parent;
5030 const char *rname = rdev_get_name(rdev);
5031 char name[NAME_MAX];
5032
5033 /* Avoid duplicate debugfs directory names */
5034 if (parent && rname == rdev->desc->name) {
5035 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
5036 rname);
5037 rname = name;
5038 }
5039
5040 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
5041 if (!rdev->debugfs) {
5042 rdev_warn(rdev, "Failed to create debugfs directory\n");
5043 return;
5044 }
5045
5046 debugfs_create_u32("use_count", 0444, rdev->debugfs,
5047 &rdev->use_count);
5048 debugfs_create_u32("open_count", 0444, rdev->debugfs,
5049 &rdev->open_count);
5050 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
5051 &rdev->bypass_count);
5052 }
5053
regulator_register_resolve_supply(struct device * dev,void * data)5054 static int regulator_register_resolve_supply(struct device *dev, void *data)
5055 {
5056 struct regulator_dev *rdev = dev_to_rdev(dev);
5057
5058 if (regulator_resolve_supply(rdev))
5059 rdev_dbg(rdev, "unable to resolve supply\n");
5060
5061 return 0;
5062 }
5063
regulator_coupler_register(struct regulator_coupler * coupler)5064 int regulator_coupler_register(struct regulator_coupler *coupler)
5065 {
5066 mutex_lock(®ulator_list_mutex);
5067 list_add_tail(&coupler->list, ®ulator_coupler_list);
5068 mutex_unlock(®ulator_list_mutex);
5069
5070 return 0;
5071 }
5072
5073 static struct regulator_coupler *
regulator_find_coupler(struct regulator_dev * rdev)5074 regulator_find_coupler(struct regulator_dev *rdev)
5075 {
5076 struct regulator_coupler *coupler;
5077 int err;
5078
5079 /*
5080 * Note that regulators are appended to the list and the generic
5081 * coupler is registered first, hence it will be attached at last
5082 * if nobody cared.
5083 */
5084 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
5085 err = coupler->attach_regulator(coupler, rdev);
5086 if (!err) {
5087 if (!coupler->balance_voltage &&
5088 rdev->coupling_desc.n_coupled > 2)
5089 goto err_unsupported;
5090
5091 return coupler;
5092 }
5093
5094 if (err < 0)
5095 return ERR_PTR(err);
5096
5097 if (err == 1)
5098 continue;
5099
5100 break;
5101 }
5102
5103 return ERR_PTR(-EINVAL);
5104
5105 err_unsupported:
5106 if (coupler->detach_regulator)
5107 coupler->detach_regulator(coupler, rdev);
5108
5109 rdev_err(rdev,
5110 "Voltage balancing for multiple regulator couples is unimplemented\n");
5111
5112 return ERR_PTR(-EPERM);
5113 }
5114
regulator_resolve_coupling(struct regulator_dev * rdev)5115 static void regulator_resolve_coupling(struct regulator_dev *rdev)
5116 {
5117 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5118 struct coupling_desc *c_desc = &rdev->coupling_desc;
5119 int n_coupled = c_desc->n_coupled;
5120 struct regulator_dev *c_rdev;
5121 int i;
5122
5123 for (i = 1; i < n_coupled; i++) {
5124 /* already resolved */
5125 if (c_desc->coupled_rdevs[i])
5126 continue;
5127
5128 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
5129
5130 if (!c_rdev)
5131 continue;
5132
5133 if (c_rdev->coupling_desc.coupler != coupler) {
5134 rdev_err(rdev, "coupler mismatch with %s\n",
5135 rdev_get_name(c_rdev));
5136 return;
5137 }
5138
5139 c_desc->coupled_rdevs[i] = c_rdev;
5140 c_desc->n_resolved++;
5141
5142 regulator_resolve_coupling(c_rdev);
5143 }
5144 }
5145
regulator_remove_coupling(struct regulator_dev * rdev)5146 static void regulator_remove_coupling(struct regulator_dev *rdev)
5147 {
5148 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5149 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
5150 struct regulator_dev *__c_rdev, *c_rdev;
5151 unsigned int __n_coupled, n_coupled;
5152 int i, k;
5153 int err;
5154
5155 n_coupled = c_desc->n_coupled;
5156
5157 for (i = 1; i < n_coupled; i++) {
5158 c_rdev = c_desc->coupled_rdevs[i];
5159
5160 if (!c_rdev)
5161 continue;
5162
5163 regulator_lock(c_rdev);
5164
5165 __c_desc = &c_rdev->coupling_desc;
5166 __n_coupled = __c_desc->n_coupled;
5167
5168 for (k = 1; k < __n_coupled; k++) {
5169 __c_rdev = __c_desc->coupled_rdevs[k];
5170
5171 if (__c_rdev == rdev) {
5172 __c_desc->coupled_rdevs[k] = NULL;
5173 __c_desc->n_resolved--;
5174 break;
5175 }
5176 }
5177
5178 regulator_unlock(c_rdev);
5179
5180 c_desc->coupled_rdevs[i] = NULL;
5181 c_desc->n_resolved--;
5182 }
5183
5184 if (coupler && coupler->detach_regulator) {
5185 err = coupler->detach_regulator(coupler, rdev);
5186 if (err)
5187 rdev_err(rdev, "failed to detach from coupler: %pe\n",
5188 ERR_PTR(err));
5189 }
5190
5191 kfree(rdev->coupling_desc.coupled_rdevs);
5192 rdev->coupling_desc.coupled_rdevs = NULL;
5193 }
5194
regulator_init_coupling(struct regulator_dev * rdev)5195 static int regulator_init_coupling(struct regulator_dev *rdev)
5196 {
5197 struct regulator_dev **coupled;
5198 int err, n_phandles;
5199
5200 if (!IS_ENABLED(CONFIG_OF))
5201 n_phandles = 0;
5202 else
5203 n_phandles = of_get_n_coupled(rdev);
5204
5205 coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
5206 if (!coupled)
5207 return -ENOMEM;
5208
5209 rdev->coupling_desc.coupled_rdevs = coupled;
5210
5211 /*
5212 * Every regulator should always have coupling descriptor filled with
5213 * at least pointer to itself.
5214 */
5215 rdev->coupling_desc.coupled_rdevs[0] = rdev;
5216 rdev->coupling_desc.n_coupled = n_phandles + 1;
5217 rdev->coupling_desc.n_resolved++;
5218
5219 /* regulator isn't coupled */
5220 if (n_phandles == 0)
5221 return 0;
5222
5223 if (!of_check_coupling_data(rdev))
5224 return -EPERM;
5225
5226 mutex_lock(®ulator_list_mutex);
5227 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5228 mutex_unlock(®ulator_list_mutex);
5229
5230 if (IS_ERR(rdev->coupling_desc.coupler)) {
5231 err = PTR_ERR(rdev->coupling_desc.coupler);
5232 rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
5233 return err;
5234 }
5235
5236 return 0;
5237 }
5238
generic_coupler_attach(struct regulator_coupler * coupler,struct regulator_dev * rdev)5239 static int generic_coupler_attach(struct regulator_coupler *coupler,
5240 struct regulator_dev *rdev)
5241 {
5242 if (rdev->coupling_desc.n_coupled > 2) {
5243 rdev_err(rdev,
5244 "Voltage balancing for multiple regulator couples is unimplemented\n");
5245 return -EPERM;
5246 }
5247
5248 if (!rdev->constraints->always_on) {
5249 rdev_err(rdev,
5250 "Coupling of a non always-on regulator is unimplemented\n");
5251 return -ENOTSUPP;
5252 }
5253
5254 return 0;
5255 }
5256
5257 static struct regulator_coupler generic_regulator_coupler = {
5258 .attach_regulator = generic_coupler_attach,
5259 };
5260
5261 /**
5262 * regulator_register - register regulator
5263 * @regulator_desc: regulator to register
5264 * @cfg: runtime configuration for regulator
5265 *
5266 * Called by regulator drivers to register a regulator.
5267 * Returns a valid pointer to struct regulator_dev on success
5268 * or an ERR_PTR() on error.
5269 */
5270 struct regulator_dev *
regulator_register(const struct regulator_desc * regulator_desc,const struct regulator_config * cfg)5271 regulator_register(const struct regulator_desc *regulator_desc,
5272 const struct regulator_config *cfg)
5273 {
5274 const struct regulator_init_data *init_data;
5275 struct regulator_config *config = NULL;
5276 static atomic_t regulator_no = ATOMIC_INIT(-1);
5277 struct regulator_dev *rdev;
5278 bool dangling_cfg_gpiod = false;
5279 bool dangling_of_gpiod = false;
5280 struct device *dev;
5281 int ret, i;
5282
5283 if (cfg == NULL)
5284 return ERR_PTR(-EINVAL);
5285 if (cfg->ena_gpiod)
5286 dangling_cfg_gpiod = true;
5287 if (regulator_desc == NULL) {
5288 ret = -EINVAL;
5289 goto rinse;
5290 }
5291
5292 dev = cfg->dev;
5293 WARN_ON(!dev);
5294
5295 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5296 ret = -EINVAL;
5297 goto rinse;
5298 }
5299
5300 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5301 regulator_desc->type != REGULATOR_CURRENT) {
5302 ret = -EINVAL;
5303 goto rinse;
5304 }
5305
5306 /* Only one of each should be implemented */
5307 WARN_ON(regulator_desc->ops->get_voltage &&
5308 regulator_desc->ops->get_voltage_sel);
5309 WARN_ON(regulator_desc->ops->set_voltage &&
5310 regulator_desc->ops->set_voltage_sel);
5311
5312 /* If we're using selectors we must implement list_voltage. */
5313 if (regulator_desc->ops->get_voltage_sel &&
5314 !regulator_desc->ops->list_voltage) {
5315 ret = -EINVAL;
5316 goto rinse;
5317 }
5318 if (regulator_desc->ops->set_voltage_sel &&
5319 !regulator_desc->ops->list_voltage) {
5320 ret = -EINVAL;
5321 goto rinse;
5322 }
5323
5324 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5325 if (rdev == NULL) {
5326 ret = -ENOMEM;
5327 goto rinse;
5328 }
5329 device_initialize(&rdev->dev);
5330
5331 /*
5332 * Duplicate the config so the driver could override it after
5333 * parsing init data.
5334 */
5335 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5336 if (config == NULL) {
5337 ret = -ENOMEM;
5338 goto clean;
5339 }
5340
5341 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5342 &rdev->dev.of_node);
5343
5344 /*
5345 * Sometimes not all resources are probed already so we need to take
5346 * that into account. This happens most the time if the ena_gpiod comes
5347 * from a gpio extender or something else.
5348 */
5349 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5350 ret = -EPROBE_DEFER;
5351 goto clean;
5352 }
5353
5354 /*
5355 * We need to keep track of any GPIO descriptor coming from the
5356 * device tree until we have handled it over to the core. If the
5357 * config that was passed in to this function DOES NOT contain
5358 * a descriptor, and the config after this call DOES contain
5359 * a descriptor, we definitely got one from parsing the device
5360 * tree.
5361 */
5362 if (!cfg->ena_gpiod && config->ena_gpiod)
5363 dangling_of_gpiod = true;
5364 if (!init_data) {
5365 init_data = config->init_data;
5366 rdev->dev.of_node = of_node_get(config->of_node);
5367 }
5368
5369 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5370 rdev->reg_data = config->driver_data;
5371 rdev->owner = regulator_desc->owner;
5372 rdev->desc = regulator_desc;
5373 if (config->regmap)
5374 rdev->regmap = config->regmap;
5375 else if (dev_get_regmap(dev, NULL))
5376 rdev->regmap = dev_get_regmap(dev, NULL);
5377 else if (dev->parent)
5378 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5379 INIT_LIST_HEAD(&rdev->consumer_list);
5380 INIT_LIST_HEAD(&rdev->list);
5381 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5382 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5383
5384 /* preform any regulator specific init */
5385 if (init_data && init_data->regulator_init) {
5386 ret = init_data->regulator_init(rdev->reg_data);
5387 if (ret < 0)
5388 goto clean;
5389 }
5390
5391 if (config->ena_gpiod) {
5392 ret = regulator_ena_gpio_request(rdev, config);
5393 if (ret != 0) {
5394 rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
5395 ERR_PTR(ret));
5396 goto clean;
5397 }
5398 /* The regulator core took over the GPIO descriptor */
5399 dangling_cfg_gpiod = false;
5400 dangling_of_gpiod = false;
5401 }
5402
5403 /* register with sysfs */
5404 rdev->dev.class = ®ulator_class;
5405 rdev->dev.parent = dev;
5406 dev_set_name(&rdev->dev, "regulator.%lu",
5407 (unsigned long) atomic_inc_return(®ulator_no));
5408 dev_set_drvdata(&rdev->dev, rdev);
5409
5410 /* set regulator constraints */
5411 if (init_data)
5412 rdev->constraints = kmemdup(&init_data->constraints,
5413 sizeof(*rdev->constraints),
5414 GFP_KERNEL);
5415 else
5416 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5417 GFP_KERNEL);
5418 if (!rdev->constraints) {
5419 ret = -ENOMEM;
5420 goto wash;
5421 }
5422
5423 if (init_data && init_data->supply_regulator)
5424 rdev->supply_name = init_data->supply_regulator;
5425 else if (regulator_desc->supply_name)
5426 rdev->supply_name = regulator_desc->supply_name;
5427
5428 ret = set_machine_constraints(rdev);
5429 if (ret == -EPROBE_DEFER) {
5430 /* Regulator might be in bypass mode and so needs its supply
5431 * to set the constraints */
5432 /* FIXME: this currently triggers a chicken-and-egg problem
5433 * when creating -SUPPLY symlink in sysfs to a regulator
5434 * that is just being created */
5435 ret = regulator_resolve_supply(rdev);
5436 if (!ret)
5437 ret = set_machine_constraints(rdev);
5438 else
5439 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5440 ERR_PTR(ret));
5441 }
5442 if (ret < 0)
5443 goto wash;
5444
5445 ret = regulator_init_coupling(rdev);
5446 if (ret < 0)
5447 goto wash;
5448
5449 /* add consumers devices */
5450 if (init_data) {
5451 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5452 ret = set_consumer_device_supply(rdev,
5453 init_data->consumer_supplies[i].dev_name,
5454 init_data->consumer_supplies[i].supply);
5455 if (ret < 0) {
5456 dev_err(dev, "Failed to set supply %s\n",
5457 init_data->consumer_supplies[i].supply);
5458 goto unset_supplies;
5459 }
5460 }
5461 }
5462
5463 if (!rdev->desc->ops->get_voltage &&
5464 !rdev->desc->ops->list_voltage &&
5465 !rdev->desc->fixed_uV)
5466 rdev->is_switch = true;
5467
5468 ret = device_add(&rdev->dev);
5469 if (ret != 0)
5470 goto unset_supplies;
5471
5472 rdev_init_debugfs(rdev);
5473
5474 /* try to resolve regulators coupling since a new one was registered */
5475 mutex_lock(®ulator_list_mutex);
5476 regulator_resolve_coupling(rdev);
5477 mutex_unlock(®ulator_list_mutex);
5478
5479 /* try to resolve regulators supply since a new one was registered */
5480 class_for_each_device(®ulator_class, NULL, NULL,
5481 regulator_register_resolve_supply);
5482 kfree(config);
5483 return rdev;
5484
5485 unset_supplies:
5486 mutex_lock(®ulator_list_mutex);
5487 unset_regulator_supplies(rdev);
5488 regulator_remove_coupling(rdev);
5489 mutex_unlock(®ulator_list_mutex);
5490 wash:
5491 regulator_put(rdev->supply);
5492 kfree(rdev->coupling_desc.coupled_rdevs);
5493 mutex_lock(®ulator_list_mutex);
5494 regulator_ena_gpio_free(rdev);
5495 mutex_unlock(®ulator_list_mutex);
5496 put_device(&rdev->dev);
5497 rdev = NULL;
5498 clean:
5499 if (dangling_of_gpiod)
5500 gpiod_put(config->ena_gpiod);
5501 if (rdev && rdev->dev.of_node)
5502 of_node_put(rdev->dev.of_node);
5503 kfree(rdev);
5504 kfree(config);
5505 rinse:
5506 if (dangling_cfg_gpiod)
5507 gpiod_put(cfg->ena_gpiod);
5508 return ERR_PTR(ret);
5509 }
5510 EXPORT_SYMBOL_GPL(regulator_register);
5511
5512 /**
5513 * regulator_unregister - unregister regulator
5514 * @rdev: regulator to unregister
5515 *
5516 * Called by regulator drivers to unregister a regulator.
5517 */
regulator_unregister(struct regulator_dev * rdev)5518 void regulator_unregister(struct regulator_dev *rdev)
5519 {
5520 if (rdev == NULL)
5521 return;
5522
5523 if (rdev->supply) {
5524 while (rdev->use_count--)
5525 regulator_disable(rdev->supply);
5526 regulator_put(rdev->supply);
5527 }
5528
5529 flush_work(&rdev->disable_work.work);
5530
5531 mutex_lock(®ulator_list_mutex);
5532
5533 WARN_ON(rdev->open_count);
5534 regulator_remove_coupling(rdev);
5535 unset_regulator_supplies(rdev);
5536 list_del(&rdev->list);
5537 regulator_ena_gpio_free(rdev);
5538 device_unregister(&rdev->dev);
5539
5540 mutex_unlock(®ulator_list_mutex);
5541 }
5542 EXPORT_SYMBOL_GPL(regulator_unregister);
5543
5544 #ifdef CONFIG_SUSPEND
5545 /**
5546 * regulator_suspend - prepare regulators for system wide suspend
5547 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5548 *
5549 * Configure each regulator with it's suspend operating parameters for state.
5550 */
regulator_suspend(struct device * dev)5551 static int regulator_suspend(struct device *dev)
5552 {
5553 struct regulator_dev *rdev = dev_to_rdev(dev);
5554 suspend_state_t state = pm_suspend_target_state;
5555 int ret;
5556 const struct regulator_state *rstate;
5557
5558 rstate = regulator_get_suspend_state_check(rdev, state);
5559 if (!rstate)
5560 return 0;
5561
5562 regulator_lock(rdev);
5563 ret = __suspend_set_state(rdev, rstate);
5564 regulator_unlock(rdev);
5565
5566 return ret;
5567 }
5568
regulator_resume(struct device * dev)5569 static int regulator_resume(struct device *dev)
5570 {
5571 suspend_state_t state = pm_suspend_target_state;
5572 struct regulator_dev *rdev = dev_to_rdev(dev);
5573 struct regulator_state *rstate;
5574 int ret = 0;
5575
5576 rstate = regulator_get_suspend_state(rdev, state);
5577 if (rstate == NULL)
5578 return 0;
5579
5580 /* Avoid grabbing the lock if we don't need to */
5581 if (!rdev->desc->ops->resume)
5582 return 0;
5583
5584 regulator_lock(rdev);
5585
5586 if (rstate->enabled == ENABLE_IN_SUSPEND ||
5587 rstate->enabled == DISABLE_IN_SUSPEND)
5588 ret = rdev->desc->ops->resume(rdev);
5589
5590 regulator_unlock(rdev);
5591
5592 return ret;
5593 }
5594 #else /* !CONFIG_SUSPEND */
5595
5596 #define regulator_suspend NULL
5597 #define regulator_resume NULL
5598
5599 #endif /* !CONFIG_SUSPEND */
5600
5601 #ifdef CONFIG_PM
5602 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5603 .suspend = regulator_suspend,
5604 .resume = regulator_resume,
5605 };
5606 #endif
5607
5608 struct class regulator_class = {
5609 .name = "regulator",
5610 .dev_release = regulator_dev_release,
5611 .dev_groups = regulator_dev_groups,
5612 #ifdef CONFIG_PM
5613 .pm = ®ulator_pm_ops,
5614 #endif
5615 };
5616 /**
5617 * regulator_has_full_constraints - the system has fully specified constraints
5618 *
5619 * Calling this function will cause the regulator API to disable all
5620 * regulators which have a zero use count and don't have an always_on
5621 * constraint in a late_initcall.
5622 *
5623 * The intention is that this will become the default behaviour in a
5624 * future kernel release so users are encouraged to use this facility
5625 * now.
5626 */
regulator_has_full_constraints(void)5627 void regulator_has_full_constraints(void)
5628 {
5629 has_full_constraints = 1;
5630 }
5631 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5632
5633 /**
5634 * rdev_get_drvdata - get rdev regulator driver data
5635 * @rdev: regulator
5636 *
5637 * Get rdev regulator driver private data. This call can be used in the
5638 * regulator driver context.
5639 */
rdev_get_drvdata(struct regulator_dev * rdev)5640 void *rdev_get_drvdata(struct regulator_dev *rdev)
5641 {
5642 return rdev->reg_data;
5643 }
5644 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5645
5646 /**
5647 * regulator_get_drvdata - get regulator driver data
5648 * @regulator: regulator
5649 *
5650 * Get regulator driver private data. This call can be used in the consumer
5651 * driver context when non API regulator specific functions need to be called.
5652 */
regulator_get_drvdata(struct regulator * regulator)5653 void *regulator_get_drvdata(struct regulator *regulator)
5654 {
5655 return regulator->rdev->reg_data;
5656 }
5657 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5658
5659 /**
5660 * regulator_set_drvdata - set regulator driver data
5661 * @regulator: regulator
5662 * @data: data
5663 */
regulator_set_drvdata(struct regulator * regulator,void * data)5664 void regulator_set_drvdata(struct regulator *regulator, void *data)
5665 {
5666 regulator->rdev->reg_data = data;
5667 }
5668 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5669
5670 /**
5671 * regulator_get_id - get regulator ID
5672 * @rdev: regulator
5673 */
rdev_get_id(struct regulator_dev * rdev)5674 int rdev_get_id(struct regulator_dev *rdev)
5675 {
5676 return rdev->desc->id;
5677 }
5678 EXPORT_SYMBOL_GPL(rdev_get_id);
5679
rdev_get_dev(struct regulator_dev * rdev)5680 struct device *rdev_get_dev(struct regulator_dev *rdev)
5681 {
5682 return &rdev->dev;
5683 }
5684 EXPORT_SYMBOL_GPL(rdev_get_dev);
5685
rdev_get_regmap(struct regulator_dev * rdev)5686 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5687 {
5688 return rdev->regmap;
5689 }
5690 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5691
regulator_get_init_drvdata(struct regulator_init_data * reg_init_data)5692 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5693 {
5694 return reg_init_data->driver_data;
5695 }
5696 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5697
5698 #ifdef CONFIG_DEBUG_FS
supply_map_show(struct seq_file * sf,void * data)5699 static int supply_map_show(struct seq_file *sf, void *data)
5700 {
5701 struct regulator_map *map;
5702
5703 list_for_each_entry(map, ®ulator_map_list, list) {
5704 seq_printf(sf, "%s -> %s.%s\n",
5705 rdev_get_name(map->regulator), map->dev_name,
5706 map->supply);
5707 }
5708
5709 return 0;
5710 }
5711 DEFINE_SHOW_ATTRIBUTE(supply_map);
5712
5713 struct summary_data {
5714 struct seq_file *s;
5715 struct regulator_dev *parent;
5716 int level;
5717 };
5718
5719 static void regulator_summary_show_subtree(struct seq_file *s,
5720 struct regulator_dev *rdev,
5721 int level);
5722
regulator_summary_show_children(struct device * dev,void * data)5723 static int regulator_summary_show_children(struct device *dev, void *data)
5724 {
5725 struct regulator_dev *rdev = dev_to_rdev(dev);
5726 struct summary_data *summary_data = data;
5727
5728 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5729 regulator_summary_show_subtree(summary_data->s, rdev,
5730 summary_data->level + 1);
5731
5732 return 0;
5733 }
5734
regulator_summary_show_subtree(struct seq_file * s,struct regulator_dev * rdev,int level)5735 static void regulator_summary_show_subtree(struct seq_file *s,
5736 struct regulator_dev *rdev,
5737 int level)
5738 {
5739 struct regulation_constraints *c;
5740 struct regulator *consumer;
5741 struct summary_data summary_data;
5742 unsigned int opmode;
5743
5744 if (!rdev)
5745 return;
5746
5747 opmode = _regulator_get_mode_unlocked(rdev);
5748 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5749 level * 3 + 1, "",
5750 30 - level * 3, rdev_get_name(rdev),
5751 rdev->use_count, rdev->open_count, rdev->bypass_count,
5752 regulator_opmode_to_str(opmode));
5753
5754 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5755 seq_printf(s, "%5dmA ",
5756 _regulator_get_current_limit_unlocked(rdev) / 1000);
5757
5758 c = rdev->constraints;
5759 if (c) {
5760 switch (rdev->desc->type) {
5761 case REGULATOR_VOLTAGE:
5762 seq_printf(s, "%5dmV %5dmV ",
5763 c->min_uV / 1000, c->max_uV / 1000);
5764 break;
5765 case REGULATOR_CURRENT:
5766 seq_printf(s, "%5dmA %5dmA ",
5767 c->min_uA / 1000, c->max_uA / 1000);
5768 break;
5769 }
5770 }
5771
5772 seq_puts(s, "\n");
5773
5774 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5775 if (consumer->dev && consumer->dev->class == ®ulator_class)
5776 continue;
5777
5778 seq_printf(s, "%*s%-*s ",
5779 (level + 1) * 3 + 1, "",
5780 30 - (level + 1) * 3,
5781 consumer->supply_name ? consumer->supply_name :
5782 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5783
5784 switch (rdev->desc->type) {
5785 case REGULATOR_VOLTAGE:
5786 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5787 consumer->enable_count,
5788 consumer->uA_load / 1000,
5789 consumer->uA_load && !consumer->enable_count ?
5790 '*' : ' ',
5791 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5792 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5793 break;
5794 case REGULATOR_CURRENT:
5795 break;
5796 }
5797
5798 seq_puts(s, "\n");
5799 }
5800
5801 summary_data.s = s;
5802 summary_data.level = level;
5803 summary_data.parent = rdev;
5804
5805 class_for_each_device(®ulator_class, NULL, &summary_data,
5806 regulator_summary_show_children);
5807 }
5808
5809 struct summary_lock_data {
5810 struct ww_acquire_ctx *ww_ctx;
5811 struct regulator_dev **new_contended_rdev;
5812 struct regulator_dev **old_contended_rdev;
5813 };
5814
regulator_summary_lock_one(struct device * dev,void * data)5815 static int regulator_summary_lock_one(struct device *dev, void *data)
5816 {
5817 struct regulator_dev *rdev = dev_to_rdev(dev);
5818 struct summary_lock_data *lock_data = data;
5819 int ret = 0;
5820
5821 if (rdev != *lock_data->old_contended_rdev) {
5822 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5823
5824 if (ret == -EDEADLK)
5825 *lock_data->new_contended_rdev = rdev;
5826 else
5827 WARN_ON_ONCE(ret);
5828 } else {
5829 *lock_data->old_contended_rdev = NULL;
5830 }
5831
5832 return ret;
5833 }
5834
regulator_summary_unlock_one(struct device * dev,void * data)5835 static int regulator_summary_unlock_one(struct device *dev, void *data)
5836 {
5837 struct regulator_dev *rdev = dev_to_rdev(dev);
5838 struct summary_lock_data *lock_data = data;
5839
5840 if (lock_data) {
5841 if (rdev == *lock_data->new_contended_rdev)
5842 return -EDEADLK;
5843 }
5844
5845 regulator_unlock(rdev);
5846
5847 return 0;
5848 }
5849
regulator_summary_lock_all(struct ww_acquire_ctx * ww_ctx,struct regulator_dev ** new_contended_rdev,struct regulator_dev ** old_contended_rdev)5850 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5851 struct regulator_dev **new_contended_rdev,
5852 struct regulator_dev **old_contended_rdev)
5853 {
5854 struct summary_lock_data lock_data;
5855 int ret;
5856
5857 lock_data.ww_ctx = ww_ctx;
5858 lock_data.new_contended_rdev = new_contended_rdev;
5859 lock_data.old_contended_rdev = old_contended_rdev;
5860
5861 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5862 regulator_summary_lock_one);
5863 if (ret)
5864 class_for_each_device(®ulator_class, NULL, &lock_data,
5865 regulator_summary_unlock_one);
5866
5867 return ret;
5868 }
5869
regulator_summary_lock(struct ww_acquire_ctx * ww_ctx)5870 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5871 {
5872 struct regulator_dev *new_contended_rdev = NULL;
5873 struct regulator_dev *old_contended_rdev = NULL;
5874 int err;
5875
5876 mutex_lock(®ulator_list_mutex);
5877
5878 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5879
5880 do {
5881 if (new_contended_rdev) {
5882 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5883 old_contended_rdev = new_contended_rdev;
5884 old_contended_rdev->ref_cnt++;
5885 old_contended_rdev->mutex_owner = current;
5886 }
5887
5888 err = regulator_summary_lock_all(ww_ctx,
5889 &new_contended_rdev,
5890 &old_contended_rdev);
5891
5892 if (old_contended_rdev)
5893 regulator_unlock(old_contended_rdev);
5894
5895 } while (err == -EDEADLK);
5896
5897 ww_acquire_done(ww_ctx);
5898 }
5899
regulator_summary_unlock(struct ww_acquire_ctx * ww_ctx)5900 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5901 {
5902 class_for_each_device(®ulator_class, NULL, NULL,
5903 regulator_summary_unlock_one);
5904 ww_acquire_fini(ww_ctx);
5905
5906 mutex_unlock(®ulator_list_mutex);
5907 }
5908
regulator_summary_show_roots(struct device * dev,void * data)5909 static int regulator_summary_show_roots(struct device *dev, void *data)
5910 {
5911 struct regulator_dev *rdev = dev_to_rdev(dev);
5912 struct seq_file *s = data;
5913
5914 if (!rdev->supply)
5915 regulator_summary_show_subtree(s, rdev, 0);
5916
5917 return 0;
5918 }
5919
regulator_summary_show(struct seq_file * s,void * data)5920 static int regulator_summary_show(struct seq_file *s, void *data)
5921 {
5922 struct ww_acquire_ctx ww_ctx;
5923
5924 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5925 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5926
5927 regulator_summary_lock(&ww_ctx);
5928
5929 class_for_each_device(®ulator_class, NULL, s,
5930 regulator_summary_show_roots);
5931
5932 regulator_summary_unlock(&ww_ctx);
5933
5934 return 0;
5935 }
5936 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5937 #endif /* CONFIG_DEBUG_FS */
5938
regulator_init(void)5939 static int __init regulator_init(void)
5940 {
5941 int ret;
5942
5943 ret = class_register(®ulator_class);
5944
5945 debugfs_root = debugfs_create_dir("regulator", NULL);
5946 if (!debugfs_root)
5947 pr_warn("regulator: Failed to create debugfs directory\n");
5948
5949 #ifdef CONFIG_DEBUG_FS
5950 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5951 &supply_map_fops);
5952
5953 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5954 NULL, ®ulator_summary_fops);
5955 #endif
5956 regulator_dummy_init();
5957
5958 regulator_coupler_register(&generic_regulator_coupler);
5959
5960 return ret;
5961 }
5962
5963 /* init early to allow our consumers to complete system booting */
5964 core_initcall(regulator_init);
5965
regulator_late_cleanup(struct device * dev,void * data)5966 static int regulator_late_cleanup(struct device *dev, void *data)
5967 {
5968 struct regulator_dev *rdev = dev_to_rdev(dev);
5969 struct regulation_constraints *c = rdev->constraints;
5970 int ret;
5971
5972 if (c && c->always_on)
5973 return 0;
5974
5975 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5976 return 0;
5977
5978 regulator_lock(rdev);
5979
5980 if (rdev->use_count)
5981 goto unlock;
5982
5983 /* If reading the status failed, assume that it's off. */
5984 if (_regulator_is_enabled(rdev) <= 0)
5985 goto unlock;
5986
5987 if (have_full_constraints()) {
5988 /* We log since this may kill the system if it goes
5989 * wrong. */
5990 rdev_info(rdev, "disabling\n");
5991 ret = _regulator_do_disable(rdev);
5992 if (ret != 0)
5993 rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
5994 } else {
5995 /* The intention is that in future we will
5996 * assume that full constraints are provided
5997 * so warn even if we aren't going to do
5998 * anything here.
5999 */
6000 rdev_warn(rdev, "incomplete constraints, leaving on\n");
6001 }
6002
6003 unlock:
6004 regulator_unlock(rdev);
6005
6006 return 0;
6007 }
6008
regulator_init_complete_work_function(struct work_struct * work)6009 static void regulator_init_complete_work_function(struct work_struct *work)
6010 {
6011 /*
6012 * Regulators may had failed to resolve their input supplies
6013 * when were registered, either because the input supply was
6014 * not registered yet or because its parent device was not
6015 * bound yet. So attempt to resolve the input supplies for
6016 * pending regulators before trying to disable unused ones.
6017 */
6018 class_for_each_device(®ulator_class, NULL, NULL,
6019 regulator_register_resolve_supply);
6020
6021 /* If we have a full configuration then disable any regulators
6022 * we have permission to change the status for and which are
6023 * not in use or always_on. This is effectively the default
6024 * for DT and ACPI as they have full constraints.
6025 */
6026 class_for_each_device(®ulator_class, NULL, NULL,
6027 regulator_late_cleanup);
6028 }
6029
6030 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
6031 regulator_init_complete_work_function);
6032
regulator_init_complete(void)6033 static int __init regulator_init_complete(void)
6034 {
6035 /*
6036 * Since DT doesn't provide an idiomatic mechanism for
6037 * enabling full constraints and since it's much more natural
6038 * with DT to provide them just assume that a DT enabled
6039 * system has full constraints.
6040 */
6041 if (of_have_populated_dt())
6042 has_full_constraints = true;
6043
6044 /*
6045 * We punt completion for an arbitrary amount of time since
6046 * systems like distros will load many drivers from userspace
6047 * so consumers might not always be ready yet, this is
6048 * particularly an issue with laptops where this might bounce
6049 * the display off then on. Ideally we'd get a notification
6050 * from userspace when this happens but we don't so just wait
6051 * a bit and hope we waited long enough. It'd be better if
6052 * we'd only do this on systems that need it, and a kernel
6053 * command line option might be useful.
6054 */
6055 schedule_delayed_work(®ulator_init_complete_work,
6056 msecs_to_jiffies(30000));
6057
6058 return 0;
6059 }
6060 late_initcall_sync(regulator_init_complete);
6061