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