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