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