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