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