Merge remote-tracking branch 'regulator/topic/supply' into regulator-next
[deliverable/linux.git] / drivers / regulator / core.c
1 /*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
6 *
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/gpio/consumer.h>
28 #include <linux/of.h>
29 #include <linux/regmap.h>
30 #include <linux/regulator/of_regulator.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/module.h>
35
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/regulator.h>
38
39 #include "dummy.h"
40 #include "internal.h"
41
42 #define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50 #define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52
53 static DEFINE_MUTEX(regulator_list_mutex);
54 static LIST_HEAD(regulator_map_list);
55 static LIST_HEAD(regulator_ena_gpio_list);
56 static LIST_HEAD(regulator_supply_alias_list);
57 static bool has_full_constraints;
58
59 static struct dentry *debugfs_root;
60
61 static struct class regulator_class;
62
63 /*
64 * struct regulator_map
65 *
66 * Used to provide symbolic supply names to devices.
67 */
68 struct regulator_map {
69 struct list_head list;
70 const char *dev_name; /* The dev_name() for the consumer */
71 const char *supply;
72 struct regulator_dev *regulator;
73 };
74
75 /*
76 * struct regulator_enable_gpio
77 *
78 * Management for shared enable GPIO pin
79 */
80 struct regulator_enable_gpio {
81 struct list_head list;
82 struct gpio_desc *gpiod;
83 u32 enable_count; /* a number of enabled shared GPIO */
84 u32 request_count; /* a number of requested shared GPIO */
85 unsigned int ena_gpio_invert:1;
86 };
87
88 /*
89 * struct regulator_supply_alias
90 *
91 * Used to map lookups for a supply onto an alternative device.
92 */
93 struct regulator_supply_alias {
94 struct list_head list;
95 struct device *src_dev;
96 const char *src_supply;
97 struct device *alias_dev;
98 const char *alias_supply;
99 };
100
101 static int _regulator_is_enabled(struct regulator_dev *rdev);
102 static int _regulator_disable(struct regulator_dev *rdev);
103 static int _regulator_get_voltage(struct regulator_dev *rdev);
104 static int _regulator_get_current_limit(struct regulator_dev *rdev);
105 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
106 static int _notifier_call_chain(struct regulator_dev *rdev,
107 unsigned long event, void *data);
108 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
109 int min_uV, int max_uV);
110 static struct regulator *create_regulator(struct regulator_dev *rdev,
111 struct device *dev,
112 const char *supply_name);
113 static void _regulator_put(struct regulator *regulator);
114
115 static struct regulator_dev *dev_to_rdev(struct device *dev)
116 {
117 return container_of(dev, struct regulator_dev, dev);
118 }
119
120 static const char *rdev_get_name(struct regulator_dev *rdev)
121 {
122 if (rdev->constraints && rdev->constraints->name)
123 return rdev->constraints->name;
124 else if (rdev->desc->name)
125 return rdev->desc->name;
126 else
127 return "";
128 }
129
130 static bool have_full_constraints(void)
131 {
132 return has_full_constraints || of_have_populated_dt();
133 }
134
135 static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
136 {
137 if (rdev && rdev->supply)
138 return rdev->supply->rdev;
139
140 return NULL;
141 }
142
143 /**
144 * regulator_lock_supply - lock a regulator and its supplies
145 * @rdev: regulator source
146 */
147 static void regulator_lock_supply(struct regulator_dev *rdev)
148 {
149 int i;
150
151 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
152 mutex_lock_nested(&rdev->mutex, i);
153 }
154
155 /**
156 * regulator_unlock_supply - unlock a regulator and its supplies
157 * @rdev: regulator source
158 */
159 static void regulator_unlock_supply(struct regulator_dev *rdev)
160 {
161 struct regulator *supply;
162
163 while (1) {
164 mutex_unlock(&rdev->mutex);
165 supply = rdev->supply;
166
167 if (!rdev->supply)
168 return;
169
170 rdev = supply->rdev;
171 }
172 }
173
174 /**
175 * of_get_regulator - get a regulator device node based on supply name
176 * @dev: Device pointer for the consumer (of regulator) device
177 * @supply: regulator supply name
178 *
179 * Extract the regulator device node corresponding to the supply name.
180 * returns the device node corresponding to the regulator if found, else
181 * returns NULL.
182 */
183 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
184 {
185 struct device_node *regnode = NULL;
186 char prop_name[32]; /* 32 is max size of property name */
187
188 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
189
190 snprintf(prop_name, 32, "%s-supply", supply);
191 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
192
193 if (!regnode) {
194 dev_dbg(dev, "Looking up %s property in node %s failed",
195 prop_name, dev->of_node->full_name);
196 return NULL;
197 }
198 return regnode;
199 }
200
201 static int _regulator_can_change_status(struct regulator_dev *rdev)
202 {
203 if (!rdev->constraints)
204 return 0;
205
206 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
207 return 1;
208 else
209 return 0;
210 }
211
212 /* Platform voltage constraint check */
213 static int regulator_check_voltage(struct regulator_dev *rdev,
214 int *min_uV, int *max_uV)
215 {
216 BUG_ON(*min_uV > *max_uV);
217
218 if (!rdev->constraints) {
219 rdev_err(rdev, "no constraints\n");
220 return -ENODEV;
221 }
222 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
223 rdev_err(rdev, "voltage operation not allowed\n");
224 return -EPERM;
225 }
226
227 if (*max_uV > rdev->constraints->max_uV)
228 *max_uV = rdev->constraints->max_uV;
229 if (*min_uV < rdev->constraints->min_uV)
230 *min_uV = rdev->constraints->min_uV;
231
232 if (*min_uV > *max_uV) {
233 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
234 *min_uV, *max_uV);
235 return -EINVAL;
236 }
237
238 return 0;
239 }
240
241 /* Make sure we select a voltage that suits the needs of all
242 * regulator consumers
243 */
244 static int regulator_check_consumers(struct regulator_dev *rdev,
245 int *min_uV, int *max_uV)
246 {
247 struct regulator *regulator;
248
249 list_for_each_entry(regulator, &rdev->consumer_list, list) {
250 /*
251 * Assume consumers that didn't say anything are OK
252 * with anything in the constraint range.
253 */
254 if (!regulator->min_uV && !regulator->max_uV)
255 continue;
256
257 if (*max_uV > regulator->max_uV)
258 *max_uV = regulator->max_uV;
259 if (*min_uV < regulator->min_uV)
260 *min_uV = regulator->min_uV;
261 }
262
263 if (*min_uV > *max_uV) {
264 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
265 *min_uV, *max_uV);
266 return -EINVAL;
267 }
268
269 return 0;
270 }
271
272 /* current constraint check */
273 static int regulator_check_current_limit(struct regulator_dev *rdev,
274 int *min_uA, int *max_uA)
275 {
276 BUG_ON(*min_uA > *max_uA);
277
278 if (!rdev->constraints) {
279 rdev_err(rdev, "no constraints\n");
280 return -ENODEV;
281 }
282 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
283 rdev_err(rdev, "current operation not allowed\n");
284 return -EPERM;
285 }
286
287 if (*max_uA > rdev->constraints->max_uA)
288 *max_uA = rdev->constraints->max_uA;
289 if (*min_uA < rdev->constraints->min_uA)
290 *min_uA = rdev->constraints->min_uA;
291
292 if (*min_uA > *max_uA) {
293 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
294 *min_uA, *max_uA);
295 return -EINVAL;
296 }
297
298 return 0;
299 }
300
301 /* operating mode constraint check */
302 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
303 {
304 switch (*mode) {
305 case REGULATOR_MODE_FAST:
306 case REGULATOR_MODE_NORMAL:
307 case REGULATOR_MODE_IDLE:
308 case REGULATOR_MODE_STANDBY:
309 break;
310 default:
311 rdev_err(rdev, "invalid mode %x specified\n", *mode);
312 return -EINVAL;
313 }
314
315 if (!rdev->constraints) {
316 rdev_err(rdev, "no constraints\n");
317 return -ENODEV;
318 }
319 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
320 rdev_err(rdev, "mode operation not allowed\n");
321 return -EPERM;
322 }
323
324 /* The modes are bitmasks, the most power hungry modes having
325 * the lowest values. If the requested mode isn't supported
326 * try higher modes. */
327 while (*mode) {
328 if (rdev->constraints->valid_modes_mask & *mode)
329 return 0;
330 *mode /= 2;
331 }
332
333 return -EINVAL;
334 }
335
336 /* dynamic regulator mode switching constraint check */
337 static int regulator_check_drms(struct regulator_dev *rdev)
338 {
339 if (!rdev->constraints) {
340 rdev_err(rdev, "no constraints\n");
341 return -ENODEV;
342 }
343 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
344 rdev_dbg(rdev, "drms operation not allowed\n");
345 return -EPERM;
346 }
347 return 0;
348 }
349
350 static ssize_t regulator_uV_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
352 {
353 struct regulator_dev *rdev = dev_get_drvdata(dev);
354 ssize_t ret;
355
356 mutex_lock(&rdev->mutex);
357 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
358 mutex_unlock(&rdev->mutex);
359
360 return ret;
361 }
362 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
363
364 static ssize_t regulator_uA_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
366 {
367 struct regulator_dev *rdev = dev_get_drvdata(dev);
368
369 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
370 }
371 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
372
373 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
374 char *buf)
375 {
376 struct regulator_dev *rdev = dev_get_drvdata(dev);
377
378 return sprintf(buf, "%s\n", rdev_get_name(rdev));
379 }
380 static DEVICE_ATTR_RO(name);
381
382 static ssize_t regulator_print_opmode(char *buf, int mode)
383 {
384 switch (mode) {
385 case REGULATOR_MODE_FAST:
386 return sprintf(buf, "fast\n");
387 case REGULATOR_MODE_NORMAL:
388 return sprintf(buf, "normal\n");
389 case REGULATOR_MODE_IDLE:
390 return sprintf(buf, "idle\n");
391 case REGULATOR_MODE_STANDBY:
392 return sprintf(buf, "standby\n");
393 }
394 return sprintf(buf, "unknown\n");
395 }
396
397 static ssize_t regulator_opmode_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
399 {
400 struct regulator_dev *rdev = dev_get_drvdata(dev);
401
402 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
403 }
404 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
405
406 static ssize_t regulator_print_state(char *buf, int state)
407 {
408 if (state > 0)
409 return sprintf(buf, "enabled\n");
410 else if (state == 0)
411 return sprintf(buf, "disabled\n");
412 else
413 return sprintf(buf, "unknown\n");
414 }
415
416 static ssize_t regulator_state_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
418 {
419 struct regulator_dev *rdev = dev_get_drvdata(dev);
420 ssize_t ret;
421
422 mutex_lock(&rdev->mutex);
423 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
424 mutex_unlock(&rdev->mutex);
425
426 return ret;
427 }
428 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
429
430 static ssize_t regulator_status_show(struct device *dev,
431 struct device_attribute *attr, char *buf)
432 {
433 struct regulator_dev *rdev = dev_get_drvdata(dev);
434 int status;
435 char *label;
436
437 status = rdev->desc->ops->get_status(rdev);
438 if (status < 0)
439 return status;
440
441 switch (status) {
442 case REGULATOR_STATUS_OFF:
443 label = "off";
444 break;
445 case REGULATOR_STATUS_ON:
446 label = "on";
447 break;
448 case REGULATOR_STATUS_ERROR:
449 label = "error";
450 break;
451 case REGULATOR_STATUS_FAST:
452 label = "fast";
453 break;
454 case REGULATOR_STATUS_NORMAL:
455 label = "normal";
456 break;
457 case REGULATOR_STATUS_IDLE:
458 label = "idle";
459 break;
460 case REGULATOR_STATUS_STANDBY:
461 label = "standby";
462 break;
463 case REGULATOR_STATUS_BYPASS:
464 label = "bypass";
465 break;
466 case REGULATOR_STATUS_UNDEFINED:
467 label = "undefined";
468 break;
469 default:
470 return -ERANGE;
471 }
472
473 return sprintf(buf, "%s\n", label);
474 }
475 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
476
477 static ssize_t regulator_min_uA_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
479 {
480 struct regulator_dev *rdev = dev_get_drvdata(dev);
481
482 if (!rdev->constraints)
483 return sprintf(buf, "constraint not defined\n");
484
485 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
486 }
487 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
488
489 static ssize_t regulator_max_uA_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
491 {
492 struct regulator_dev *rdev = dev_get_drvdata(dev);
493
494 if (!rdev->constraints)
495 return sprintf(buf, "constraint not defined\n");
496
497 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
498 }
499 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
500
501 static ssize_t regulator_min_uV_show(struct device *dev,
502 struct device_attribute *attr, char *buf)
503 {
504 struct regulator_dev *rdev = dev_get_drvdata(dev);
505
506 if (!rdev->constraints)
507 return sprintf(buf, "constraint not defined\n");
508
509 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
510 }
511 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
512
513 static ssize_t regulator_max_uV_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
515 {
516 struct regulator_dev *rdev = dev_get_drvdata(dev);
517
518 if (!rdev->constraints)
519 return sprintf(buf, "constraint not defined\n");
520
521 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
522 }
523 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
524
525 static ssize_t regulator_total_uA_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
527 {
528 struct regulator_dev *rdev = dev_get_drvdata(dev);
529 struct regulator *regulator;
530 int uA = 0;
531
532 mutex_lock(&rdev->mutex);
533 list_for_each_entry(regulator, &rdev->consumer_list, list)
534 uA += regulator->uA_load;
535 mutex_unlock(&rdev->mutex);
536 return sprintf(buf, "%d\n", uA);
537 }
538 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
539
540 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
541 char *buf)
542 {
543 struct regulator_dev *rdev = dev_get_drvdata(dev);
544 return sprintf(buf, "%d\n", rdev->use_count);
545 }
546 static DEVICE_ATTR_RO(num_users);
547
548 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
549 char *buf)
550 {
551 struct regulator_dev *rdev = dev_get_drvdata(dev);
552
553 switch (rdev->desc->type) {
554 case REGULATOR_VOLTAGE:
555 return sprintf(buf, "voltage\n");
556 case REGULATOR_CURRENT:
557 return sprintf(buf, "current\n");
558 }
559 return sprintf(buf, "unknown\n");
560 }
561 static DEVICE_ATTR_RO(type);
562
563 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565 {
566 struct regulator_dev *rdev = dev_get_drvdata(dev);
567
568 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
569 }
570 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
571 regulator_suspend_mem_uV_show, NULL);
572
573 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
575 {
576 struct regulator_dev *rdev = dev_get_drvdata(dev);
577
578 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
579 }
580 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
581 regulator_suspend_disk_uV_show, NULL);
582
583 static ssize_t regulator_suspend_standby_uV_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", rdev->constraints->state_standby.uV);
589 }
590 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
591 regulator_suspend_standby_uV_show, NULL);
592
593 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
595 {
596 struct regulator_dev *rdev = dev_get_drvdata(dev);
597
598 return regulator_print_opmode(buf,
599 rdev->constraints->state_mem.mode);
600 }
601 static DEVICE_ATTR(suspend_mem_mode, 0444,
602 regulator_suspend_mem_mode_show, NULL);
603
604 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
605 struct device_attribute *attr, char *buf)
606 {
607 struct regulator_dev *rdev = dev_get_drvdata(dev);
608
609 return regulator_print_opmode(buf,
610 rdev->constraints->state_disk.mode);
611 }
612 static DEVICE_ATTR(suspend_disk_mode, 0444,
613 regulator_suspend_disk_mode_show, NULL);
614
615 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
616 struct device_attribute *attr, char *buf)
617 {
618 struct regulator_dev *rdev = dev_get_drvdata(dev);
619
620 return regulator_print_opmode(buf,
621 rdev->constraints->state_standby.mode);
622 }
623 static DEVICE_ATTR(suspend_standby_mode, 0444,
624 regulator_suspend_standby_mode_show, NULL);
625
626 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
627 struct device_attribute *attr, char *buf)
628 {
629 struct regulator_dev *rdev = dev_get_drvdata(dev);
630
631 return regulator_print_state(buf,
632 rdev->constraints->state_mem.enabled);
633 }
634 static DEVICE_ATTR(suspend_mem_state, 0444,
635 regulator_suspend_mem_state_show, NULL);
636
637 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
638 struct device_attribute *attr, char *buf)
639 {
640 struct regulator_dev *rdev = dev_get_drvdata(dev);
641
642 return regulator_print_state(buf,
643 rdev->constraints->state_disk.enabled);
644 }
645 static DEVICE_ATTR(suspend_disk_state, 0444,
646 regulator_suspend_disk_state_show, NULL);
647
648 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
649 struct device_attribute *attr, char *buf)
650 {
651 struct regulator_dev *rdev = dev_get_drvdata(dev);
652
653 return regulator_print_state(buf,
654 rdev->constraints->state_standby.enabled);
655 }
656 static DEVICE_ATTR(suspend_standby_state, 0444,
657 regulator_suspend_standby_state_show, NULL);
658
659 static ssize_t regulator_bypass_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
661 {
662 struct regulator_dev *rdev = dev_get_drvdata(dev);
663 const char *report;
664 bool bypass;
665 int ret;
666
667 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
668
669 if (ret != 0)
670 report = "unknown";
671 else if (bypass)
672 report = "enabled";
673 else
674 report = "disabled";
675
676 return sprintf(buf, "%s\n", report);
677 }
678 static DEVICE_ATTR(bypass, 0444,
679 regulator_bypass_show, NULL);
680
681 /* Calculate the new optimum regulator operating mode based on the new total
682 * consumer load. All locks held by caller */
683 static int drms_uA_update(struct regulator_dev *rdev)
684 {
685 struct regulator *sibling;
686 int current_uA = 0, output_uV, input_uV, err;
687 unsigned int mode;
688
689 lockdep_assert_held_once(&rdev->mutex);
690
691 /*
692 * first check to see if we can set modes at all, otherwise just
693 * tell the consumer everything is OK.
694 */
695 err = regulator_check_drms(rdev);
696 if (err < 0)
697 return 0;
698
699 if (!rdev->desc->ops->get_optimum_mode &&
700 !rdev->desc->ops->set_load)
701 return 0;
702
703 if (!rdev->desc->ops->set_mode &&
704 !rdev->desc->ops->set_load)
705 return -EINVAL;
706
707 /* get output voltage */
708 output_uV = _regulator_get_voltage(rdev);
709 if (output_uV <= 0) {
710 rdev_err(rdev, "invalid output voltage found\n");
711 return -EINVAL;
712 }
713
714 /* get input voltage */
715 input_uV = 0;
716 if (rdev->supply)
717 input_uV = regulator_get_voltage(rdev->supply);
718 if (input_uV <= 0)
719 input_uV = rdev->constraints->input_uV;
720 if (input_uV <= 0) {
721 rdev_err(rdev, "invalid input voltage found\n");
722 return -EINVAL;
723 }
724
725 /* calc total requested load */
726 list_for_each_entry(sibling, &rdev->consumer_list, list)
727 current_uA += sibling->uA_load;
728
729 current_uA += rdev->constraints->system_load;
730
731 if (rdev->desc->ops->set_load) {
732 /* set the optimum mode for our new total regulator load */
733 err = rdev->desc->ops->set_load(rdev, current_uA);
734 if (err < 0)
735 rdev_err(rdev, "failed to set load %d\n", current_uA);
736 } else {
737 /* now get the optimum mode for our new total regulator load */
738 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
739 output_uV, current_uA);
740
741 /* check the new mode is allowed */
742 err = regulator_mode_constrain(rdev, &mode);
743 if (err < 0) {
744 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
745 current_uA, input_uV, output_uV);
746 return err;
747 }
748
749 err = rdev->desc->ops->set_mode(rdev, mode);
750 if (err < 0)
751 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
752 }
753
754 return err;
755 }
756
757 static int suspend_set_state(struct regulator_dev *rdev,
758 struct regulator_state *rstate)
759 {
760 int ret = 0;
761
762 /* If we have no suspend mode configration don't set anything;
763 * only warn if the driver implements set_suspend_voltage or
764 * set_suspend_mode callback.
765 */
766 if (!rstate->enabled && !rstate->disabled) {
767 if (rdev->desc->ops->set_suspend_voltage ||
768 rdev->desc->ops->set_suspend_mode)
769 rdev_warn(rdev, "No configuration\n");
770 return 0;
771 }
772
773 if (rstate->enabled && rstate->disabled) {
774 rdev_err(rdev, "invalid configuration\n");
775 return -EINVAL;
776 }
777
778 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
779 ret = rdev->desc->ops->set_suspend_enable(rdev);
780 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
781 ret = rdev->desc->ops->set_suspend_disable(rdev);
782 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
783 ret = 0;
784
785 if (ret < 0) {
786 rdev_err(rdev, "failed to enabled/disable\n");
787 return ret;
788 }
789
790 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
791 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
792 if (ret < 0) {
793 rdev_err(rdev, "failed to set voltage\n");
794 return ret;
795 }
796 }
797
798 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
799 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
800 if (ret < 0) {
801 rdev_err(rdev, "failed to set mode\n");
802 return ret;
803 }
804 }
805 return ret;
806 }
807
808 /* locks held by caller */
809 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
810 {
811 if (!rdev->constraints)
812 return -EINVAL;
813
814 switch (state) {
815 case PM_SUSPEND_STANDBY:
816 return suspend_set_state(rdev,
817 &rdev->constraints->state_standby);
818 case PM_SUSPEND_MEM:
819 return suspend_set_state(rdev,
820 &rdev->constraints->state_mem);
821 case PM_SUSPEND_MAX:
822 return suspend_set_state(rdev,
823 &rdev->constraints->state_disk);
824 default:
825 return -EINVAL;
826 }
827 }
828
829 static void print_constraints(struct regulator_dev *rdev)
830 {
831 struct regulation_constraints *constraints = rdev->constraints;
832 char buf[160] = "";
833 size_t len = sizeof(buf) - 1;
834 int count = 0;
835 int ret;
836
837 if (constraints->min_uV && constraints->max_uV) {
838 if (constraints->min_uV == constraints->max_uV)
839 count += scnprintf(buf + count, len - count, "%d mV ",
840 constraints->min_uV / 1000);
841 else
842 count += scnprintf(buf + count, len - count,
843 "%d <--> %d mV ",
844 constraints->min_uV / 1000,
845 constraints->max_uV / 1000);
846 }
847
848 if (!constraints->min_uV ||
849 constraints->min_uV != constraints->max_uV) {
850 ret = _regulator_get_voltage(rdev);
851 if (ret > 0)
852 count += scnprintf(buf + count, len - count,
853 "at %d mV ", ret / 1000);
854 }
855
856 if (constraints->uV_offset)
857 count += scnprintf(buf + count, len - count, "%dmV offset ",
858 constraints->uV_offset / 1000);
859
860 if (constraints->min_uA && constraints->max_uA) {
861 if (constraints->min_uA == constraints->max_uA)
862 count += scnprintf(buf + count, len - count, "%d mA ",
863 constraints->min_uA / 1000);
864 else
865 count += scnprintf(buf + count, len - count,
866 "%d <--> %d mA ",
867 constraints->min_uA / 1000,
868 constraints->max_uA / 1000);
869 }
870
871 if (!constraints->min_uA ||
872 constraints->min_uA != constraints->max_uA) {
873 ret = _regulator_get_current_limit(rdev);
874 if (ret > 0)
875 count += scnprintf(buf + count, len - count,
876 "at %d mA ", ret / 1000);
877 }
878
879 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
880 count += scnprintf(buf + count, len - count, "fast ");
881 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
882 count += scnprintf(buf + count, len - count, "normal ");
883 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
884 count += scnprintf(buf + count, len - count, "idle ");
885 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
886 count += scnprintf(buf + count, len - count, "standby");
887
888 if (!count)
889 scnprintf(buf, len, "no parameters");
890
891 rdev_dbg(rdev, "%s\n", buf);
892
893 if ((constraints->min_uV != constraints->max_uV) &&
894 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
895 rdev_warn(rdev,
896 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
897 }
898
899 static int machine_constraints_voltage(struct regulator_dev *rdev,
900 struct regulation_constraints *constraints)
901 {
902 const struct regulator_ops *ops = rdev->desc->ops;
903 int ret;
904
905 /* do we need to apply the constraint voltage */
906 if (rdev->constraints->apply_uV &&
907 rdev->constraints->min_uV && rdev->constraints->max_uV) {
908 int target_min, target_max;
909 int current_uV = _regulator_get_voltage(rdev);
910 if (current_uV < 0) {
911 rdev_err(rdev,
912 "failed to get the current voltage(%d)\n",
913 current_uV);
914 return current_uV;
915 }
916
917 /*
918 * If we're below the minimum voltage move up to the
919 * minimum voltage, if we're above the maximum voltage
920 * then move down to the maximum.
921 */
922 target_min = current_uV;
923 target_max = current_uV;
924
925 if (current_uV < rdev->constraints->min_uV) {
926 target_min = rdev->constraints->min_uV;
927 target_max = rdev->constraints->min_uV;
928 }
929
930 if (current_uV > rdev->constraints->max_uV) {
931 target_min = rdev->constraints->max_uV;
932 target_max = rdev->constraints->max_uV;
933 }
934
935 if (target_min != current_uV || target_max != current_uV) {
936 ret = _regulator_do_set_voltage(
937 rdev, target_min, target_max);
938 if (ret < 0) {
939 rdev_err(rdev,
940 "failed to apply %d-%duV constraint(%d)\n",
941 target_min, target_max, ret);
942 return ret;
943 }
944 }
945 }
946
947 /* constrain machine-level voltage specs to fit
948 * the actual range supported by this regulator.
949 */
950 if (ops->list_voltage && rdev->desc->n_voltages) {
951 int count = rdev->desc->n_voltages;
952 int i;
953 int min_uV = INT_MAX;
954 int max_uV = INT_MIN;
955 int cmin = constraints->min_uV;
956 int cmax = constraints->max_uV;
957
958 /* it's safe to autoconfigure fixed-voltage supplies
959 and the constraints are used by list_voltage. */
960 if (count == 1 && !cmin) {
961 cmin = 1;
962 cmax = INT_MAX;
963 constraints->min_uV = cmin;
964 constraints->max_uV = cmax;
965 }
966
967 /* voltage constraints are optional */
968 if ((cmin == 0) && (cmax == 0))
969 return 0;
970
971 /* else require explicit machine-level constraints */
972 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
973 rdev_err(rdev, "invalid voltage constraints\n");
974 return -EINVAL;
975 }
976
977 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
978 for (i = 0; i < count; i++) {
979 int value;
980
981 value = ops->list_voltage(rdev, i);
982 if (value <= 0)
983 continue;
984
985 /* maybe adjust [min_uV..max_uV] */
986 if (value >= cmin && value < min_uV)
987 min_uV = value;
988 if (value <= cmax && value > max_uV)
989 max_uV = value;
990 }
991
992 /* final: [min_uV..max_uV] valid iff constraints valid */
993 if (max_uV < min_uV) {
994 rdev_err(rdev,
995 "unsupportable voltage constraints %u-%uuV\n",
996 min_uV, max_uV);
997 return -EINVAL;
998 }
999
1000 /* use regulator's subset of machine constraints */
1001 if (constraints->min_uV < min_uV) {
1002 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1003 constraints->min_uV, min_uV);
1004 constraints->min_uV = min_uV;
1005 }
1006 if (constraints->max_uV > max_uV) {
1007 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1008 constraints->max_uV, max_uV);
1009 constraints->max_uV = max_uV;
1010 }
1011 }
1012
1013 return 0;
1014 }
1015
1016 static int machine_constraints_current(struct regulator_dev *rdev,
1017 struct regulation_constraints *constraints)
1018 {
1019 const struct regulator_ops *ops = rdev->desc->ops;
1020 int ret;
1021
1022 if (!constraints->min_uA && !constraints->max_uA)
1023 return 0;
1024
1025 if (constraints->min_uA > constraints->max_uA) {
1026 rdev_err(rdev, "Invalid current constraints\n");
1027 return -EINVAL;
1028 }
1029
1030 if (!ops->set_current_limit || !ops->get_current_limit) {
1031 rdev_warn(rdev, "Operation of current configuration missing\n");
1032 return 0;
1033 }
1034
1035 /* Set regulator current in constraints range */
1036 ret = ops->set_current_limit(rdev, constraints->min_uA,
1037 constraints->max_uA);
1038 if (ret < 0) {
1039 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1040 return ret;
1041 }
1042
1043 return 0;
1044 }
1045
1046 static int _regulator_do_enable(struct regulator_dev *rdev);
1047
1048 /**
1049 * set_machine_constraints - sets regulator constraints
1050 * @rdev: regulator source
1051 * @constraints: constraints to apply
1052 *
1053 * Allows platform initialisation code to define and constrain
1054 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1055 * Constraints *must* be set by platform code in order for some
1056 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1057 * set_mode.
1058 */
1059 static int set_machine_constraints(struct regulator_dev *rdev,
1060 const struct regulation_constraints *constraints)
1061 {
1062 int ret = 0;
1063 const struct regulator_ops *ops = rdev->desc->ops;
1064
1065 if (constraints)
1066 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1067 GFP_KERNEL);
1068 else
1069 rdev->constraints = kzalloc(sizeof(*constraints),
1070 GFP_KERNEL);
1071 if (!rdev->constraints)
1072 return -ENOMEM;
1073
1074 ret = machine_constraints_voltage(rdev, rdev->constraints);
1075 if (ret != 0)
1076 return ret;
1077
1078 ret = machine_constraints_current(rdev, rdev->constraints);
1079 if (ret != 0)
1080 return ret;
1081
1082 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1083 ret = ops->set_input_current_limit(rdev,
1084 rdev->constraints->ilim_uA);
1085 if (ret < 0) {
1086 rdev_err(rdev, "failed to set input limit\n");
1087 return ret;
1088 }
1089 }
1090
1091 /* do we need to setup our suspend state */
1092 if (rdev->constraints->initial_state) {
1093 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
1094 if (ret < 0) {
1095 rdev_err(rdev, "failed to set suspend state\n");
1096 return ret;
1097 }
1098 }
1099
1100 if (rdev->constraints->initial_mode) {
1101 if (!ops->set_mode) {
1102 rdev_err(rdev, "no set_mode operation\n");
1103 return -EINVAL;
1104 }
1105
1106 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1107 if (ret < 0) {
1108 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1109 return ret;
1110 }
1111 }
1112
1113 /* If the constraints say the regulator should be on at this point
1114 * and we have control then make sure it is enabled.
1115 */
1116 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1117 ret = _regulator_do_enable(rdev);
1118 if (ret < 0 && ret != -EINVAL) {
1119 rdev_err(rdev, "failed to enable\n");
1120 return ret;
1121 }
1122 }
1123
1124 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1125 && ops->set_ramp_delay) {
1126 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1127 if (ret < 0) {
1128 rdev_err(rdev, "failed to set ramp_delay\n");
1129 return ret;
1130 }
1131 }
1132
1133 if (rdev->constraints->pull_down && ops->set_pull_down) {
1134 ret = ops->set_pull_down(rdev);
1135 if (ret < 0) {
1136 rdev_err(rdev, "failed to set pull down\n");
1137 return ret;
1138 }
1139 }
1140
1141 if (rdev->constraints->soft_start && ops->set_soft_start) {
1142 ret = ops->set_soft_start(rdev);
1143 if (ret < 0) {
1144 rdev_err(rdev, "failed to set soft start\n");
1145 return ret;
1146 }
1147 }
1148
1149 if (rdev->constraints->over_current_protection
1150 && ops->set_over_current_protection) {
1151 ret = ops->set_over_current_protection(rdev);
1152 if (ret < 0) {
1153 rdev_err(rdev, "failed to set over current protection\n");
1154 return ret;
1155 }
1156 }
1157
1158 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1159 bool ad_state = (rdev->constraints->active_discharge ==
1160 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1161
1162 ret = ops->set_active_discharge(rdev, ad_state);
1163 if (ret < 0) {
1164 rdev_err(rdev, "failed to set active discharge\n");
1165 return ret;
1166 }
1167 }
1168
1169 print_constraints(rdev);
1170 return 0;
1171 }
1172
1173 /**
1174 * set_supply - set regulator supply regulator
1175 * @rdev: regulator name
1176 * @supply_rdev: supply regulator name
1177 *
1178 * Called by platform initialisation code to set the supply regulator for this
1179 * regulator. This ensures that a regulators supply will also be enabled by the
1180 * core if it's child is enabled.
1181 */
1182 static int set_supply(struct regulator_dev *rdev,
1183 struct regulator_dev *supply_rdev)
1184 {
1185 int err;
1186
1187 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1188
1189 if (!try_module_get(supply_rdev->owner))
1190 return -ENODEV;
1191
1192 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1193 if (rdev->supply == NULL) {
1194 err = -ENOMEM;
1195 return err;
1196 }
1197 supply_rdev->open_count++;
1198
1199 return 0;
1200 }
1201
1202 /**
1203 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1204 * @rdev: regulator source
1205 * @consumer_dev_name: dev_name() string for device supply applies to
1206 * @supply: symbolic name for supply
1207 *
1208 * Allows platform initialisation code to map physical regulator
1209 * sources to symbolic names for supplies for use by devices. Devices
1210 * should use these symbolic names to request regulators, avoiding the
1211 * need to provide board-specific regulator names as platform data.
1212 */
1213 static int set_consumer_device_supply(struct regulator_dev *rdev,
1214 const char *consumer_dev_name,
1215 const char *supply)
1216 {
1217 struct regulator_map *node;
1218 int has_dev;
1219
1220 if (supply == NULL)
1221 return -EINVAL;
1222
1223 if (consumer_dev_name != NULL)
1224 has_dev = 1;
1225 else
1226 has_dev = 0;
1227
1228 list_for_each_entry(node, &regulator_map_list, list) {
1229 if (node->dev_name && consumer_dev_name) {
1230 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1231 continue;
1232 } else if (node->dev_name || consumer_dev_name) {
1233 continue;
1234 }
1235
1236 if (strcmp(node->supply, supply) != 0)
1237 continue;
1238
1239 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1240 consumer_dev_name,
1241 dev_name(&node->regulator->dev),
1242 node->regulator->desc->name,
1243 supply,
1244 dev_name(&rdev->dev), rdev_get_name(rdev));
1245 return -EBUSY;
1246 }
1247
1248 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1249 if (node == NULL)
1250 return -ENOMEM;
1251
1252 node->regulator = rdev;
1253 node->supply = supply;
1254
1255 if (has_dev) {
1256 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1257 if (node->dev_name == NULL) {
1258 kfree(node);
1259 return -ENOMEM;
1260 }
1261 }
1262
1263 list_add(&node->list, &regulator_map_list);
1264 return 0;
1265 }
1266
1267 static void unset_regulator_supplies(struct regulator_dev *rdev)
1268 {
1269 struct regulator_map *node, *n;
1270
1271 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1272 if (rdev == node->regulator) {
1273 list_del(&node->list);
1274 kfree(node->dev_name);
1275 kfree(node);
1276 }
1277 }
1278 }
1279
1280 #define REG_STR_SIZE 64
1281
1282 static struct regulator *create_regulator(struct regulator_dev *rdev,
1283 struct device *dev,
1284 const char *supply_name)
1285 {
1286 struct regulator *regulator;
1287 char buf[REG_STR_SIZE];
1288 int err, size;
1289
1290 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1291 if (regulator == NULL)
1292 return NULL;
1293
1294 mutex_lock(&rdev->mutex);
1295 regulator->rdev = rdev;
1296 list_add(&regulator->list, &rdev->consumer_list);
1297
1298 if (dev) {
1299 regulator->dev = dev;
1300
1301 /* Add a link to the device sysfs entry */
1302 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1303 dev->kobj.name, supply_name);
1304 if (size >= REG_STR_SIZE)
1305 goto overflow_err;
1306
1307 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1308 if (regulator->supply_name == NULL)
1309 goto overflow_err;
1310
1311 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1312 buf);
1313 if (err) {
1314 rdev_dbg(rdev, "could not add device link %s err %d\n",
1315 dev->kobj.name, err);
1316 /* non-fatal */
1317 }
1318 } else {
1319 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1320 if (regulator->supply_name == NULL)
1321 goto overflow_err;
1322 }
1323
1324 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1325 rdev->debugfs);
1326 if (!regulator->debugfs) {
1327 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1328 } else {
1329 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1330 &regulator->uA_load);
1331 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1332 &regulator->min_uV);
1333 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1334 &regulator->max_uV);
1335 }
1336
1337 /*
1338 * Check now if the regulator is an always on regulator - if
1339 * it is then we don't need to do nearly so much work for
1340 * enable/disable calls.
1341 */
1342 if (!_regulator_can_change_status(rdev) &&
1343 _regulator_is_enabled(rdev))
1344 regulator->always_on = true;
1345
1346 mutex_unlock(&rdev->mutex);
1347 return regulator;
1348 overflow_err:
1349 list_del(&regulator->list);
1350 kfree(regulator);
1351 mutex_unlock(&rdev->mutex);
1352 return NULL;
1353 }
1354
1355 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1356 {
1357 if (rdev->constraints && rdev->constraints->enable_time)
1358 return rdev->constraints->enable_time;
1359 if (!rdev->desc->ops->enable_time)
1360 return rdev->desc->enable_time;
1361 return rdev->desc->ops->enable_time(rdev);
1362 }
1363
1364 static struct regulator_supply_alias *regulator_find_supply_alias(
1365 struct device *dev, const char *supply)
1366 {
1367 struct regulator_supply_alias *map;
1368
1369 list_for_each_entry(map, &regulator_supply_alias_list, list)
1370 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1371 return map;
1372
1373 return NULL;
1374 }
1375
1376 static void regulator_supply_alias(struct device **dev, const char **supply)
1377 {
1378 struct regulator_supply_alias *map;
1379
1380 map = regulator_find_supply_alias(*dev, *supply);
1381 if (map) {
1382 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1383 *supply, map->alias_supply,
1384 dev_name(map->alias_dev));
1385 *dev = map->alias_dev;
1386 *supply = map->alias_supply;
1387 }
1388 }
1389
1390 static int of_node_match(struct device *dev, const void *data)
1391 {
1392 return dev->of_node == data;
1393 }
1394
1395 static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
1396 {
1397 struct device *dev;
1398
1399 dev = class_find_device(&regulator_class, NULL, np, of_node_match);
1400
1401 return dev ? dev_to_rdev(dev) : NULL;
1402 }
1403
1404 static int regulator_match(struct device *dev, const void *data)
1405 {
1406 struct regulator_dev *r = dev_to_rdev(dev);
1407
1408 return strcmp(rdev_get_name(r), data) == 0;
1409 }
1410
1411 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1412 {
1413 struct device *dev;
1414
1415 dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1416
1417 return dev ? dev_to_rdev(dev) : NULL;
1418 }
1419
1420 /**
1421 * regulator_dev_lookup - lookup a regulator device.
1422 * @dev: device for regulator "consumer".
1423 * @supply: Supply name or regulator ID.
1424 * @ret: 0 on success, -ENODEV if lookup fails permanently, -EPROBE_DEFER if
1425 * lookup could succeed in the future.
1426 *
1427 * If successful, returns a struct regulator_dev that corresponds to the name
1428 * @supply and with the embedded struct device refcount incremented by one,
1429 * or NULL on failure. The refcount must be dropped by calling put_device().
1430 */
1431 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1432 const char *supply,
1433 int *ret)
1434 {
1435 struct regulator_dev *r;
1436 struct device_node *node;
1437 struct regulator_map *map;
1438 const char *devname = NULL;
1439
1440 regulator_supply_alias(&dev, &supply);
1441
1442 /* first do a dt based lookup */
1443 if (dev && dev->of_node) {
1444 node = of_get_regulator(dev, supply);
1445 if (node) {
1446 r = of_find_regulator_by_node(node);
1447 if (r)
1448 return r;
1449 *ret = -EPROBE_DEFER;
1450 return NULL;
1451 } else {
1452 /*
1453 * If we couldn't even get the node then it's
1454 * not just that the device didn't register
1455 * yet, there's no node and we'll never
1456 * succeed.
1457 */
1458 *ret = -ENODEV;
1459 }
1460 }
1461
1462 /* if not found, try doing it non-dt way */
1463 if (dev)
1464 devname = dev_name(dev);
1465
1466 r = regulator_lookup_by_name(supply);
1467 if (r)
1468 return r;
1469
1470 mutex_lock(&regulator_list_mutex);
1471 list_for_each_entry(map, &regulator_map_list, list) {
1472 /* If the mapping has a device set up it must match */
1473 if (map->dev_name &&
1474 (!devname || strcmp(map->dev_name, devname)))
1475 continue;
1476
1477 if (strcmp(map->supply, supply) == 0 &&
1478 get_device(&map->regulator->dev)) {
1479 mutex_unlock(&regulator_list_mutex);
1480 return map->regulator;
1481 }
1482 }
1483 mutex_unlock(&regulator_list_mutex);
1484
1485 return NULL;
1486 }
1487
1488 static int regulator_resolve_supply(struct regulator_dev *rdev)
1489 {
1490 struct regulator_dev *r;
1491 struct device *dev = rdev->dev.parent;
1492 int ret;
1493
1494 /* No supply to resovle? */
1495 if (!rdev->supply_name)
1496 return 0;
1497
1498 /* Supply already resolved? */
1499 if (rdev->supply)
1500 return 0;
1501
1502 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1503 if (!r) {
1504 if (ret == -ENODEV) {
1505 /*
1506 * No supply was specified for this regulator and
1507 * there will never be one.
1508 */
1509 return 0;
1510 }
1511
1512 /* Did the lookup explicitly defer for us? */
1513 if (ret == -EPROBE_DEFER)
1514 return ret;
1515
1516 if (have_full_constraints()) {
1517 r = dummy_regulator_rdev;
1518 get_device(&r->dev);
1519 } else {
1520 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1521 rdev->supply_name, rdev->desc->name);
1522 return -EPROBE_DEFER;
1523 }
1524 }
1525
1526 /* Recursively resolve the supply of the supply */
1527 ret = regulator_resolve_supply(r);
1528 if (ret < 0) {
1529 put_device(&r->dev);
1530 return ret;
1531 }
1532
1533 ret = set_supply(rdev, r);
1534 if (ret < 0) {
1535 put_device(&r->dev);
1536 return ret;
1537 }
1538
1539 /* Cascade always-on state to supply */
1540 if (_regulator_is_enabled(rdev)) {
1541 ret = regulator_enable(rdev->supply);
1542 if (ret < 0) {
1543 _regulator_put(rdev->supply);
1544 rdev->supply = NULL;
1545 return ret;
1546 }
1547 }
1548
1549 return 0;
1550 }
1551
1552 /* Internal regulator request function */
1553 static struct regulator *_regulator_get(struct device *dev, const char *id,
1554 bool exclusive, bool allow_dummy)
1555 {
1556 struct regulator_dev *rdev;
1557 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1558 const char *devname = NULL;
1559 int ret;
1560
1561 if (id == NULL) {
1562 pr_err("get() with no identifier\n");
1563 return ERR_PTR(-EINVAL);
1564 }
1565
1566 if (dev)
1567 devname = dev_name(dev);
1568
1569 if (have_full_constraints())
1570 ret = -ENODEV;
1571 else
1572 ret = -EPROBE_DEFER;
1573
1574 rdev = regulator_dev_lookup(dev, id, &ret);
1575 if (rdev)
1576 goto found;
1577
1578 regulator = ERR_PTR(ret);
1579
1580 /*
1581 * If we have return value from dev_lookup fail, we do not expect to
1582 * succeed, so, quit with appropriate error value
1583 */
1584 if (ret && ret != -ENODEV)
1585 return regulator;
1586
1587 if (!devname)
1588 devname = "deviceless";
1589
1590 /*
1591 * Assume that a regulator is physically present and enabled
1592 * even if it isn't hooked up and just provide a dummy.
1593 */
1594 if (have_full_constraints() && allow_dummy) {
1595 pr_warn("%s supply %s not found, using dummy regulator\n",
1596 devname, id);
1597
1598 rdev = dummy_regulator_rdev;
1599 get_device(&rdev->dev);
1600 goto found;
1601 /* Don't log an error when called from regulator_get_optional() */
1602 } else if (!have_full_constraints() || exclusive) {
1603 dev_warn(dev, "dummy supplies not allowed\n");
1604 }
1605
1606 return regulator;
1607
1608 found:
1609 if (rdev->exclusive) {
1610 regulator = ERR_PTR(-EPERM);
1611 put_device(&rdev->dev);
1612 return regulator;
1613 }
1614
1615 if (exclusive && rdev->open_count) {
1616 regulator = ERR_PTR(-EBUSY);
1617 put_device(&rdev->dev);
1618 return regulator;
1619 }
1620
1621 ret = regulator_resolve_supply(rdev);
1622 if (ret < 0) {
1623 regulator = ERR_PTR(ret);
1624 put_device(&rdev->dev);
1625 return regulator;
1626 }
1627
1628 if (!try_module_get(rdev->owner)) {
1629 put_device(&rdev->dev);
1630 return regulator;
1631 }
1632
1633 regulator = create_regulator(rdev, dev, id);
1634 if (regulator == NULL) {
1635 regulator = ERR_PTR(-ENOMEM);
1636 put_device(&rdev->dev);
1637 module_put(rdev->owner);
1638 return regulator;
1639 }
1640
1641 rdev->open_count++;
1642 if (exclusive) {
1643 rdev->exclusive = 1;
1644
1645 ret = _regulator_is_enabled(rdev);
1646 if (ret > 0)
1647 rdev->use_count = 1;
1648 else
1649 rdev->use_count = 0;
1650 }
1651
1652 return regulator;
1653 }
1654
1655 /**
1656 * regulator_get - lookup and obtain a reference to a regulator.
1657 * @dev: device for regulator "consumer"
1658 * @id: Supply name or regulator ID.
1659 *
1660 * Returns a struct regulator corresponding to the regulator producer,
1661 * or IS_ERR() condition containing errno.
1662 *
1663 * Use of supply names configured via regulator_set_device_supply() is
1664 * strongly encouraged. It is recommended that the supply name used
1665 * should match the name used for the supply and/or the relevant
1666 * device pins in the datasheet.
1667 */
1668 struct regulator *regulator_get(struct device *dev, const char *id)
1669 {
1670 return _regulator_get(dev, id, false, true);
1671 }
1672 EXPORT_SYMBOL_GPL(regulator_get);
1673
1674 /**
1675 * regulator_get_exclusive - obtain exclusive access to a regulator.
1676 * @dev: device for regulator "consumer"
1677 * @id: Supply name or regulator ID.
1678 *
1679 * Returns a struct regulator corresponding to the regulator producer,
1680 * or IS_ERR() condition containing errno. Other consumers will be
1681 * unable to obtain this regulator while this reference is held and the
1682 * use count for the regulator will be initialised to reflect the current
1683 * state of the regulator.
1684 *
1685 * This is intended for use by consumers which cannot tolerate shared
1686 * use of the regulator such as those which need to force the
1687 * regulator off for correct operation of the hardware they are
1688 * controlling.
1689 *
1690 * Use of supply names configured via regulator_set_device_supply() is
1691 * strongly encouraged. It is recommended that the supply name used
1692 * should match the name used for the supply and/or the relevant
1693 * device pins in the datasheet.
1694 */
1695 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1696 {
1697 return _regulator_get(dev, id, true, false);
1698 }
1699 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1700
1701 /**
1702 * regulator_get_optional - obtain optional access to a regulator.
1703 * @dev: device for regulator "consumer"
1704 * @id: Supply name or regulator ID.
1705 *
1706 * Returns a struct regulator corresponding to the regulator producer,
1707 * or IS_ERR() condition containing errno.
1708 *
1709 * This is intended for use by consumers for devices which can have
1710 * some supplies unconnected in normal use, such as some MMC devices.
1711 * It can allow the regulator core to provide stub supplies for other
1712 * supplies requested using normal regulator_get() calls without
1713 * disrupting the operation of drivers that can handle absent
1714 * supplies.
1715 *
1716 * Use of supply names configured via regulator_set_device_supply() is
1717 * strongly encouraged. It is recommended that the supply name used
1718 * should match the name used for the supply and/or the relevant
1719 * device pins in the datasheet.
1720 */
1721 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1722 {
1723 return _regulator_get(dev, id, false, false);
1724 }
1725 EXPORT_SYMBOL_GPL(regulator_get_optional);
1726
1727 /* regulator_list_mutex lock held by regulator_put() */
1728 static void _regulator_put(struct regulator *regulator)
1729 {
1730 struct regulator_dev *rdev;
1731
1732 if (IS_ERR_OR_NULL(regulator))
1733 return;
1734
1735 lockdep_assert_held_once(&regulator_list_mutex);
1736
1737 rdev = regulator->rdev;
1738
1739 debugfs_remove_recursive(regulator->debugfs);
1740
1741 /* remove any sysfs entries */
1742 if (regulator->dev)
1743 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1744 mutex_lock(&rdev->mutex);
1745 list_del(&regulator->list);
1746
1747 rdev->open_count--;
1748 rdev->exclusive = 0;
1749 put_device(&rdev->dev);
1750 mutex_unlock(&rdev->mutex);
1751
1752 kfree(regulator->supply_name);
1753 kfree(regulator);
1754
1755 module_put(rdev->owner);
1756 }
1757
1758 /**
1759 * regulator_put - "free" the regulator source
1760 * @regulator: regulator source
1761 *
1762 * Note: drivers must ensure that all regulator_enable calls made on this
1763 * regulator source are balanced by regulator_disable calls prior to calling
1764 * this function.
1765 */
1766 void regulator_put(struct regulator *regulator)
1767 {
1768 mutex_lock(&regulator_list_mutex);
1769 _regulator_put(regulator);
1770 mutex_unlock(&regulator_list_mutex);
1771 }
1772 EXPORT_SYMBOL_GPL(regulator_put);
1773
1774 /**
1775 * regulator_register_supply_alias - Provide device alias for supply lookup
1776 *
1777 * @dev: device that will be given as the regulator "consumer"
1778 * @id: Supply name or regulator ID
1779 * @alias_dev: device that should be used to lookup the supply
1780 * @alias_id: Supply name or regulator ID that should be used to lookup the
1781 * supply
1782 *
1783 * All lookups for id on dev will instead be conducted for alias_id on
1784 * alias_dev.
1785 */
1786 int regulator_register_supply_alias(struct device *dev, const char *id,
1787 struct device *alias_dev,
1788 const char *alias_id)
1789 {
1790 struct regulator_supply_alias *map;
1791
1792 map = regulator_find_supply_alias(dev, id);
1793 if (map)
1794 return -EEXIST;
1795
1796 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1797 if (!map)
1798 return -ENOMEM;
1799
1800 map->src_dev = dev;
1801 map->src_supply = id;
1802 map->alias_dev = alias_dev;
1803 map->alias_supply = alias_id;
1804
1805 list_add(&map->list, &regulator_supply_alias_list);
1806
1807 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1808 id, dev_name(dev), alias_id, dev_name(alias_dev));
1809
1810 return 0;
1811 }
1812 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1813
1814 /**
1815 * regulator_unregister_supply_alias - Remove device alias
1816 *
1817 * @dev: device that will be given as the regulator "consumer"
1818 * @id: Supply name or regulator ID
1819 *
1820 * Remove a lookup alias if one exists for id on dev.
1821 */
1822 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1823 {
1824 struct regulator_supply_alias *map;
1825
1826 map = regulator_find_supply_alias(dev, id);
1827 if (map) {
1828 list_del(&map->list);
1829 kfree(map);
1830 }
1831 }
1832 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1833
1834 /**
1835 * regulator_bulk_register_supply_alias - register multiple aliases
1836 *
1837 * @dev: device that will be given as the regulator "consumer"
1838 * @id: List of supply names or regulator IDs
1839 * @alias_dev: device that should be used to lookup the supply
1840 * @alias_id: List of supply names or regulator IDs that should be used to
1841 * lookup the supply
1842 * @num_id: Number of aliases to register
1843 *
1844 * @return 0 on success, an errno on failure.
1845 *
1846 * This helper function allows drivers to register several supply
1847 * aliases in one operation. If any of the aliases cannot be
1848 * registered any aliases that were registered will be removed
1849 * before returning to the caller.
1850 */
1851 int regulator_bulk_register_supply_alias(struct device *dev,
1852 const char *const *id,
1853 struct device *alias_dev,
1854 const char *const *alias_id,
1855 int num_id)
1856 {
1857 int i;
1858 int ret;
1859
1860 for (i = 0; i < num_id; ++i) {
1861 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1862 alias_id[i]);
1863 if (ret < 0)
1864 goto err;
1865 }
1866
1867 return 0;
1868
1869 err:
1870 dev_err(dev,
1871 "Failed to create supply alias %s,%s -> %s,%s\n",
1872 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1873
1874 while (--i >= 0)
1875 regulator_unregister_supply_alias(dev, id[i]);
1876
1877 return ret;
1878 }
1879 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1880
1881 /**
1882 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1883 *
1884 * @dev: device that will be given as the regulator "consumer"
1885 * @id: List of supply names or regulator IDs
1886 * @num_id: Number of aliases to unregister
1887 *
1888 * This helper function allows drivers to unregister several supply
1889 * aliases in one operation.
1890 */
1891 void regulator_bulk_unregister_supply_alias(struct device *dev,
1892 const char *const *id,
1893 int num_id)
1894 {
1895 int i;
1896
1897 for (i = 0; i < num_id; ++i)
1898 regulator_unregister_supply_alias(dev, id[i]);
1899 }
1900 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1901
1902
1903 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1904 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1905 const struct regulator_config *config)
1906 {
1907 struct regulator_enable_gpio *pin;
1908 struct gpio_desc *gpiod;
1909 int ret;
1910
1911 gpiod = gpio_to_desc(config->ena_gpio);
1912
1913 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1914 if (pin->gpiod == gpiod) {
1915 rdev_dbg(rdev, "GPIO %d is already used\n",
1916 config->ena_gpio);
1917 goto update_ena_gpio_to_rdev;
1918 }
1919 }
1920
1921 ret = gpio_request_one(config->ena_gpio,
1922 GPIOF_DIR_OUT | config->ena_gpio_flags,
1923 rdev_get_name(rdev));
1924 if (ret)
1925 return ret;
1926
1927 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1928 if (pin == NULL) {
1929 gpio_free(config->ena_gpio);
1930 return -ENOMEM;
1931 }
1932
1933 pin->gpiod = gpiod;
1934 pin->ena_gpio_invert = config->ena_gpio_invert;
1935 list_add(&pin->list, &regulator_ena_gpio_list);
1936
1937 update_ena_gpio_to_rdev:
1938 pin->request_count++;
1939 rdev->ena_pin = pin;
1940 return 0;
1941 }
1942
1943 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1944 {
1945 struct regulator_enable_gpio *pin, *n;
1946
1947 if (!rdev->ena_pin)
1948 return;
1949
1950 /* Free the GPIO only in case of no use */
1951 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1952 if (pin->gpiod == rdev->ena_pin->gpiod) {
1953 if (pin->request_count <= 1) {
1954 pin->request_count = 0;
1955 gpiod_put(pin->gpiod);
1956 list_del(&pin->list);
1957 kfree(pin);
1958 rdev->ena_pin = NULL;
1959 return;
1960 } else {
1961 pin->request_count--;
1962 }
1963 }
1964 }
1965 }
1966
1967 /**
1968 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1969 * @rdev: regulator_dev structure
1970 * @enable: enable GPIO at initial use?
1971 *
1972 * GPIO is enabled in case of initial use. (enable_count is 0)
1973 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1974 */
1975 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1976 {
1977 struct regulator_enable_gpio *pin = rdev->ena_pin;
1978
1979 if (!pin)
1980 return -EINVAL;
1981
1982 if (enable) {
1983 /* Enable GPIO at initial use */
1984 if (pin->enable_count == 0)
1985 gpiod_set_value_cansleep(pin->gpiod,
1986 !pin->ena_gpio_invert);
1987
1988 pin->enable_count++;
1989 } else {
1990 if (pin->enable_count > 1) {
1991 pin->enable_count--;
1992 return 0;
1993 }
1994
1995 /* Disable GPIO if not used */
1996 if (pin->enable_count <= 1) {
1997 gpiod_set_value_cansleep(pin->gpiod,
1998 pin->ena_gpio_invert);
1999 pin->enable_count = 0;
2000 }
2001 }
2002
2003 return 0;
2004 }
2005
2006 /**
2007 * _regulator_enable_delay - a delay helper function
2008 * @delay: time to delay in microseconds
2009 *
2010 * Delay for the requested amount of time as per the guidelines in:
2011 *
2012 * Documentation/timers/timers-howto.txt
2013 *
2014 * The assumption here is that regulators will never be enabled in
2015 * atomic context and therefore sleeping functions can be used.
2016 */
2017 static void _regulator_enable_delay(unsigned int delay)
2018 {
2019 unsigned int ms = delay / 1000;
2020 unsigned int us = delay % 1000;
2021
2022 if (ms > 0) {
2023 /*
2024 * For small enough values, handle super-millisecond
2025 * delays in the usleep_range() call below.
2026 */
2027 if (ms < 20)
2028 us += ms * 1000;
2029 else
2030 msleep(ms);
2031 }
2032
2033 /*
2034 * Give the scheduler some room to coalesce with any other
2035 * wakeup sources. For delays shorter than 10 us, don't even
2036 * bother setting up high-resolution timers and just busy-
2037 * loop.
2038 */
2039 if (us >= 10)
2040 usleep_range(us, us + 100);
2041 else
2042 udelay(us);
2043 }
2044
2045 static int _regulator_do_enable(struct regulator_dev *rdev)
2046 {
2047 int ret, delay;
2048
2049 /* Query before enabling in case configuration dependent. */
2050 ret = _regulator_get_enable_time(rdev);
2051 if (ret >= 0) {
2052 delay = ret;
2053 } else {
2054 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2055 delay = 0;
2056 }
2057
2058 trace_regulator_enable(rdev_get_name(rdev));
2059
2060 if (rdev->desc->off_on_delay) {
2061 /* if needed, keep a distance of off_on_delay from last time
2062 * this regulator was disabled.
2063 */
2064 unsigned long start_jiffy = jiffies;
2065 unsigned long intended, max_delay, remaining;
2066
2067 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2068 intended = rdev->last_off_jiffy + max_delay;
2069
2070 if (time_before(start_jiffy, intended)) {
2071 /* calc remaining jiffies to deal with one-time
2072 * timer wrapping.
2073 * in case of multiple timer wrapping, either it can be
2074 * detected by out-of-range remaining, or it cannot be
2075 * detected and we gets a panelty of
2076 * _regulator_enable_delay().
2077 */
2078 remaining = intended - start_jiffy;
2079 if (remaining <= max_delay)
2080 _regulator_enable_delay(
2081 jiffies_to_usecs(remaining));
2082 }
2083 }
2084
2085 if (rdev->ena_pin) {
2086 if (!rdev->ena_gpio_state) {
2087 ret = regulator_ena_gpio_ctrl(rdev, true);
2088 if (ret < 0)
2089 return ret;
2090 rdev->ena_gpio_state = 1;
2091 }
2092 } else if (rdev->desc->ops->enable) {
2093 ret = rdev->desc->ops->enable(rdev);
2094 if (ret < 0)
2095 return ret;
2096 } else {
2097 return -EINVAL;
2098 }
2099
2100 /* Allow the regulator to ramp; it would be useful to extend
2101 * this for bulk operations so that the regulators can ramp
2102 * together. */
2103 trace_regulator_enable_delay(rdev_get_name(rdev));
2104
2105 _regulator_enable_delay(delay);
2106
2107 trace_regulator_enable_complete(rdev_get_name(rdev));
2108
2109 return 0;
2110 }
2111
2112 /* locks held by regulator_enable() */
2113 static int _regulator_enable(struct regulator_dev *rdev)
2114 {
2115 int ret;
2116
2117 lockdep_assert_held_once(&rdev->mutex);
2118
2119 /* check voltage and requested load before enabling */
2120 if (rdev->constraints &&
2121 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
2122 drms_uA_update(rdev);
2123
2124 if (rdev->use_count == 0) {
2125 /* The regulator may on if it's not switchable or left on */
2126 ret = _regulator_is_enabled(rdev);
2127 if (ret == -EINVAL || ret == 0) {
2128 if (!_regulator_can_change_status(rdev))
2129 return -EPERM;
2130
2131 ret = _regulator_do_enable(rdev);
2132 if (ret < 0)
2133 return ret;
2134
2135 } else if (ret < 0) {
2136 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2137 return ret;
2138 }
2139 /* Fallthrough on positive return values - already enabled */
2140 }
2141
2142 rdev->use_count++;
2143
2144 return 0;
2145 }
2146
2147 /**
2148 * regulator_enable - enable regulator output
2149 * @regulator: regulator source
2150 *
2151 * Request that the regulator be enabled with the regulator output at
2152 * the predefined voltage or current value. Calls to regulator_enable()
2153 * must be balanced with calls to regulator_disable().
2154 *
2155 * NOTE: the output value can be set by other drivers, boot loader or may be
2156 * hardwired in the regulator.
2157 */
2158 int regulator_enable(struct regulator *regulator)
2159 {
2160 struct regulator_dev *rdev = regulator->rdev;
2161 int ret = 0;
2162
2163 if (regulator->always_on)
2164 return 0;
2165
2166 if (rdev->supply) {
2167 ret = regulator_enable(rdev->supply);
2168 if (ret != 0)
2169 return ret;
2170 }
2171
2172 mutex_lock(&rdev->mutex);
2173 ret = _regulator_enable(rdev);
2174 mutex_unlock(&rdev->mutex);
2175
2176 if (ret != 0 && rdev->supply)
2177 regulator_disable(rdev->supply);
2178
2179 return ret;
2180 }
2181 EXPORT_SYMBOL_GPL(regulator_enable);
2182
2183 static int _regulator_do_disable(struct regulator_dev *rdev)
2184 {
2185 int ret;
2186
2187 trace_regulator_disable(rdev_get_name(rdev));
2188
2189 if (rdev->ena_pin) {
2190 if (rdev->ena_gpio_state) {
2191 ret = regulator_ena_gpio_ctrl(rdev, false);
2192 if (ret < 0)
2193 return ret;
2194 rdev->ena_gpio_state = 0;
2195 }
2196
2197 } else if (rdev->desc->ops->disable) {
2198 ret = rdev->desc->ops->disable(rdev);
2199 if (ret != 0)
2200 return ret;
2201 }
2202
2203 /* cares about last_off_jiffy only if off_on_delay is required by
2204 * device.
2205 */
2206 if (rdev->desc->off_on_delay)
2207 rdev->last_off_jiffy = jiffies;
2208
2209 trace_regulator_disable_complete(rdev_get_name(rdev));
2210
2211 return 0;
2212 }
2213
2214 /* locks held by regulator_disable() */
2215 static int _regulator_disable(struct regulator_dev *rdev)
2216 {
2217 int ret = 0;
2218
2219 lockdep_assert_held_once(&rdev->mutex);
2220
2221 if (WARN(rdev->use_count <= 0,
2222 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2223 return -EIO;
2224
2225 /* are we the last user and permitted to disable ? */
2226 if (rdev->use_count == 1 &&
2227 (rdev->constraints && !rdev->constraints->always_on)) {
2228
2229 /* we are last user */
2230 if (_regulator_can_change_status(rdev)) {
2231 ret = _notifier_call_chain(rdev,
2232 REGULATOR_EVENT_PRE_DISABLE,
2233 NULL);
2234 if (ret & NOTIFY_STOP_MASK)
2235 return -EINVAL;
2236
2237 ret = _regulator_do_disable(rdev);
2238 if (ret < 0) {
2239 rdev_err(rdev, "failed to disable\n");
2240 _notifier_call_chain(rdev,
2241 REGULATOR_EVENT_ABORT_DISABLE,
2242 NULL);
2243 return ret;
2244 }
2245 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2246 NULL);
2247 }
2248
2249 rdev->use_count = 0;
2250 } else if (rdev->use_count > 1) {
2251
2252 if (rdev->constraints &&
2253 (rdev->constraints->valid_ops_mask &
2254 REGULATOR_CHANGE_DRMS))
2255 drms_uA_update(rdev);
2256
2257 rdev->use_count--;
2258 }
2259
2260 return ret;
2261 }
2262
2263 /**
2264 * regulator_disable - disable regulator output
2265 * @regulator: regulator source
2266 *
2267 * Disable the regulator output voltage or current. Calls to
2268 * regulator_enable() must be balanced with calls to
2269 * regulator_disable().
2270 *
2271 * NOTE: this will only disable the regulator output if no other consumer
2272 * devices have it enabled, the regulator device supports disabling and
2273 * machine constraints permit this operation.
2274 */
2275 int regulator_disable(struct regulator *regulator)
2276 {
2277 struct regulator_dev *rdev = regulator->rdev;
2278 int ret = 0;
2279
2280 if (regulator->always_on)
2281 return 0;
2282
2283 mutex_lock(&rdev->mutex);
2284 ret = _regulator_disable(rdev);
2285 mutex_unlock(&rdev->mutex);
2286
2287 if (ret == 0 && rdev->supply)
2288 regulator_disable(rdev->supply);
2289
2290 return ret;
2291 }
2292 EXPORT_SYMBOL_GPL(regulator_disable);
2293
2294 /* locks held by regulator_force_disable() */
2295 static int _regulator_force_disable(struct regulator_dev *rdev)
2296 {
2297 int ret = 0;
2298
2299 lockdep_assert_held_once(&rdev->mutex);
2300
2301 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2302 REGULATOR_EVENT_PRE_DISABLE, NULL);
2303 if (ret & NOTIFY_STOP_MASK)
2304 return -EINVAL;
2305
2306 ret = _regulator_do_disable(rdev);
2307 if (ret < 0) {
2308 rdev_err(rdev, "failed to force disable\n");
2309 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2310 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2311 return ret;
2312 }
2313
2314 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2315 REGULATOR_EVENT_DISABLE, NULL);
2316
2317 return 0;
2318 }
2319
2320 /**
2321 * regulator_force_disable - force disable regulator output
2322 * @regulator: regulator source
2323 *
2324 * Forcibly disable the regulator output voltage or current.
2325 * NOTE: this *will* disable the regulator output even if other consumer
2326 * devices have it enabled. This should be used for situations when device
2327 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2328 */
2329 int regulator_force_disable(struct regulator *regulator)
2330 {
2331 struct regulator_dev *rdev = regulator->rdev;
2332 int ret;
2333
2334 mutex_lock(&rdev->mutex);
2335 regulator->uA_load = 0;
2336 ret = _regulator_force_disable(regulator->rdev);
2337 mutex_unlock(&rdev->mutex);
2338
2339 if (rdev->supply)
2340 while (rdev->open_count--)
2341 regulator_disable(rdev->supply);
2342
2343 return ret;
2344 }
2345 EXPORT_SYMBOL_GPL(regulator_force_disable);
2346
2347 static void regulator_disable_work(struct work_struct *work)
2348 {
2349 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2350 disable_work.work);
2351 int count, i, ret;
2352
2353 mutex_lock(&rdev->mutex);
2354
2355 BUG_ON(!rdev->deferred_disables);
2356
2357 count = rdev->deferred_disables;
2358 rdev->deferred_disables = 0;
2359
2360 for (i = 0; i < count; i++) {
2361 ret = _regulator_disable(rdev);
2362 if (ret != 0)
2363 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2364 }
2365
2366 mutex_unlock(&rdev->mutex);
2367
2368 if (rdev->supply) {
2369 for (i = 0; i < count; i++) {
2370 ret = regulator_disable(rdev->supply);
2371 if (ret != 0) {
2372 rdev_err(rdev,
2373 "Supply disable failed: %d\n", ret);
2374 }
2375 }
2376 }
2377 }
2378
2379 /**
2380 * regulator_disable_deferred - disable regulator output with delay
2381 * @regulator: regulator source
2382 * @ms: miliseconds until the regulator is disabled
2383 *
2384 * Execute regulator_disable() on the regulator after a delay. This
2385 * is intended for use with devices that require some time to quiesce.
2386 *
2387 * NOTE: this will only disable the regulator output if no other consumer
2388 * devices have it enabled, the regulator device supports disabling and
2389 * machine constraints permit this operation.
2390 */
2391 int regulator_disable_deferred(struct regulator *regulator, int ms)
2392 {
2393 struct regulator_dev *rdev = regulator->rdev;
2394
2395 if (regulator->always_on)
2396 return 0;
2397
2398 if (!ms)
2399 return regulator_disable(regulator);
2400
2401 mutex_lock(&rdev->mutex);
2402 rdev->deferred_disables++;
2403 mutex_unlock(&rdev->mutex);
2404
2405 queue_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2406 msecs_to_jiffies(ms));
2407 return 0;
2408 }
2409 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2410
2411 static int _regulator_is_enabled(struct regulator_dev *rdev)
2412 {
2413 /* A GPIO control always takes precedence */
2414 if (rdev->ena_pin)
2415 return rdev->ena_gpio_state;
2416
2417 /* If we don't know then assume that the regulator is always on */
2418 if (!rdev->desc->ops->is_enabled)
2419 return 1;
2420
2421 return rdev->desc->ops->is_enabled(rdev);
2422 }
2423
2424 static int _regulator_list_voltage(struct regulator *regulator,
2425 unsigned selector, int lock)
2426 {
2427 struct regulator_dev *rdev = regulator->rdev;
2428 const struct regulator_ops *ops = rdev->desc->ops;
2429 int ret;
2430
2431 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2432 return rdev->desc->fixed_uV;
2433
2434 if (ops->list_voltage) {
2435 if (selector >= rdev->desc->n_voltages)
2436 return -EINVAL;
2437 if (lock)
2438 mutex_lock(&rdev->mutex);
2439 ret = ops->list_voltage(rdev, selector);
2440 if (lock)
2441 mutex_unlock(&rdev->mutex);
2442 } else if (rdev->supply) {
2443 ret = _regulator_list_voltage(rdev->supply, selector, lock);
2444 } else {
2445 return -EINVAL;
2446 }
2447
2448 if (ret > 0) {
2449 if (ret < rdev->constraints->min_uV)
2450 ret = 0;
2451 else if (ret > rdev->constraints->max_uV)
2452 ret = 0;
2453 }
2454
2455 return ret;
2456 }
2457
2458 /**
2459 * regulator_is_enabled - is the regulator output enabled
2460 * @regulator: regulator source
2461 *
2462 * Returns positive if the regulator driver backing the source/client
2463 * has requested that the device be enabled, zero if it hasn't, else a
2464 * negative errno code.
2465 *
2466 * Note that the device backing this regulator handle can have multiple
2467 * users, so it might be enabled even if regulator_enable() was never
2468 * called for this particular source.
2469 */
2470 int regulator_is_enabled(struct regulator *regulator)
2471 {
2472 int ret;
2473
2474 if (regulator->always_on)
2475 return 1;
2476
2477 mutex_lock(&regulator->rdev->mutex);
2478 ret = _regulator_is_enabled(regulator->rdev);
2479 mutex_unlock(&regulator->rdev->mutex);
2480
2481 return ret;
2482 }
2483 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2484
2485 /**
2486 * regulator_can_change_voltage - check if regulator can change voltage
2487 * @regulator: regulator source
2488 *
2489 * Returns positive if the regulator driver backing the source/client
2490 * can change its voltage, false otherwise. Useful for detecting fixed
2491 * or dummy regulators and disabling voltage change logic in the client
2492 * driver.
2493 */
2494 int regulator_can_change_voltage(struct regulator *regulator)
2495 {
2496 struct regulator_dev *rdev = regulator->rdev;
2497
2498 if (rdev->constraints &&
2499 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2500 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2501 return 1;
2502
2503 if (rdev->desc->continuous_voltage_range &&
2504 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2505 rdev->constraints->min_uV != rdev->constraints->max_uV)
2506 return 1;
2507 }
2508
2509 return 0;
2510 }
2511 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2512
2513 /**
2514 * regulator_count_voltages - count regulator_list_voltage() selectors
2515 * @regulator: regulator source
2516 *
2517 * Returns number of selectors, or negative errno. Selectors are
2518 * numbered starting at zero, and typically correspond to bitfields
2519 * in hardware registers.
2520 */
2521 int regulator_count_voltages(struct regulator *regulator)
2522 {
2523 struct regulator_dev *rdev = regulator->rdev;
2524
2525 if (rdev->desc->n_voltages)
2526 return rdev->desc->n_voltages;
2527
2528 if (!rdev->supply)
2529 return -EINVAL;
2530
2531 return regulator_count_voltages(rdev->supply);
2532 }
2533 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2534
2535 /**
2536 * regulator_list_voltage - enumerate supported voltages
2537 * @regulator: regulator source
2538 * @selector: identify voltage to list
2539 * Context: can sleep
2540 *
2541 * Returns a voltage that can be passed to @regulator_set_voltage(),
2542 * zero if this selector code can't be used on this system, or a
2543 * negative errno.
2544 */
2545 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2546 {
2547 return _regulator_list_voltage(regulator, selector, 1);
2548 }
2549 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2550
2551 /**
2552 * regulator_get_regmap - get the regulator's register map
2553 * @regulator: regulator source
2554 *
2555 * Returns the register map for the given regulator, or an ERR_PTR value
2556 * if the regulator doesn't use regmap.
2557 */
2558 struct regmap *regulator_get_regmap(struct regulator *regulator)
2559 {
2560 struct regmap *map = regulator->rdev->regmap;
2561
2562 return map ? map : ERR_PTR(-EOPNOTSUPP);
2563 }
2564
2565 /**
2566 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2567 * @regulator: regulator source
2568 * @vsel_reg: voltage selector register, output parameter
2569 * @vsel_mask: mask for voltage selector bitfield, output parameter
2570 *
2571 * Returns the hardware register offset and bitmask used for setting the
2572 * regulator voltage. This might be useful when configuring voltage-scaling
2573 * hardware or firmware that can make I2C requests behind the kernel's back,
2574 * for example.
2575 *
2576 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2577 * and 0 is returned, otherwise a negative errno is returned.
2578 */
2579 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2580 unsigned *vsel_reg,
2581 unsigned *vsel_mask)
2582 {
2583 struct regulator_dev *rdev = regulator->rdev;
2584 const struct regulator_ops *ops = rdev->desc->ops;
2585
2586 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2587 return -EOPNOTSUPP;
2588
2589 *vsel_reg = rdev->desc->vsel_reg;
2590 *vsel_mask = rdev->desc->vsel_mask;
2591
2592 return 0;
2593 }
2594 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2595
2596 /**
2597 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2598 * @regulator: regulator source
2599 * @selector: identify voltage to list
2600 *
2601 * Converts the selector to a hardware-specific voltage selector that can be
2602 * directly written to the regulator registers. The address of the voltage
2603 * register can be determined by calling @regulator_get_hardware_vsel_register.
2604 *
2605 * On error a negative errno is returned.
2606 */
2607 int regulator_list_hardware_vsel(struct regulator *regulator,
2608 unsigned selector)
2609 {
2610 struct regulator_dev *rdev = regulator->rdev;
2611 const struct regulator_ops *ops = rdev->desc->ops;
2612
2613 if (selector >= rdev->desc->n_voltages)
2614 return -EINVAL;
2615 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2616 return -EOPNOTSUPP;
2617
2618 return selector;
2619 }
2620 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2621
2622 /**
2623 * regulator_get_linear_step - return the voltage step size between VSEL values
2624 * @regulator: regulator source
2625 *
2626 * Returns the voltage step size between VSEL values for linear
2627 * regulators, or return 0 if the regulator isn't a linear regulator.
2628 */
2629 unsigned int regulator_get_linear_step(struct regulator *regulator)
2630 {
2631 struct regulator_dev *rdev = regulator->rdev;
2632
2633 return rdev->desc->uV_step;
2634 }
2635 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2636
2637 /**
2638 * regulator_is_supported_voltage - check if a voltage range can be supported
2639 *
2640 * @regulator: Regulator to check.
2641 * @min_uV: Minimum required voltage in uV.
2642 * @max_uV: Maximum required voltage in uV.
2643 *
2644 * Returns a boolean or a negative error code.
2645 */
2646 int regulator_is_supported_voltage(struct regulator *regulator,
2647 int min_uV, int max_uV)
2648 {
2649 struct regulator_dev *rdev = regulator->rdev;
2650 int i, voltages, ret;
2651
2652 /* If we can't change voltage check the current voltage */
2653 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2654 ret = regulator_get_voltage(regulator);
2655 if (ret >= 0)
2656 return min_uV <= ret && ret <= max_uV;
2657 else
2658 return ret;
2659 }
2660
2661 /* Any voltage within constrains range is fine? */
2662 if (rdev->desc->continuous_voltage_range)
2663 return min_uV >= rdev->constraints->min_uV &&
2664 max_uV <= rdev->constraints->max_uV;
2665
2666 ret = regulator_count_voltages(regulator);
2667 if (ret < 0)
2668 return ret;
2669 voltages = ret;
2670
2671 for (i = 0; i < voltages; i++) {
2672 ret = regulator_list_voltage(regulator, i);
2673
2674 if (ret >= min_uV && ret <= max_uV)
2675 return 1;
2676 }
2677
2678 return 0;
2679 }
2680 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2681
2682 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2683 int max_uV)
2684 {
2685 const struct regulator_desc *desc = rdev->desc;
2686
2687 if (desc->ops->map_voltage)
2688 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2689
2690 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2691 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2692
2693 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2694 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2695
2696 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2697 }
2698
2699 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2700 int min_uV, int max_uV,
2701 unsigned *selector)
2702 {
2703 struct pre_voltage_change_data data;
2704 int ret;
2705
2706 data.old_uV = _regulator_get_voltage(rdev);
2707 data.min_uV = min_uV;
2708 data.max_uV = max_uV;
2709 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2710 &data);
2711 if (ret & NOTIFY_STOP_MASK)
2712 return -EINVAL;
2713
2714 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2715 if (ret >= 0)
2716 return ret;
2717
2718 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2719 (void *)data.old_uV);
2720
2721 return ret;
2722 }
2723
2724 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2725 int uV, unsigned selector)
2726 {
2727 struct pre_voltage_change_data data;
2728 int ret;
2729
2730 data.old_uV = _regulator_get_voltage(rdev);
2731 data.min_uV = uV;
2732 data.max_uV = uV;
2733 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2734 &data);
2735 if (ret & NOTIFY_STOP_MASK)
2736 return -EINVAL;
2737
2738 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2739 if (ret >= 0)
2740 return ret;
2741
2742 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2743 (void *)data.old_uV);
2744
2745 return ret;
2746 }
2747
2748 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2749 int min_uV, int max_uV)
2750 {
2751 int ret;
2752 int delay = 0;
2753 int best_val = 0;
2754 unsigned int selector;
2755 int old_selector = -1;
2756
2757 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2758
2759 min_uV += rdev->constraints->uV_offset;
2760 max_uV += rdev->constraints->uV_offset;
2761
2762 /*
2763 * If we can't obtain the old selector there is not enough
2764 * info to call set_voltage_time_sel().
2765 */
2766 if (_regulator_is_enabled(rdev) &&
2767 rdev->desc->ops->set_voltage_time_sel &&
2768 rdev->desc->ops->get_voltage_sel) {
2769 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2770 if (old_selector < 0)
2771 return old_selector;
2772 }
2773
2774 if (rdev->desc->ops->set_voltage) {
2775 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2776 &selector);
2777
2778 if (ret >= 0) {
2779 if (rdev->desc->ops->list_voltage)
2780 best_val = rdev->desc->ops->list_voltage(rdev,
2781 selector);
2782 else
2783 best_val = _regulator_get_voltage(rdev);
2784 }
2785
2786 } else if (rdev->desc->ops->set_voltage_sel) {
2787 ret = regulator_map_voltage(rdev, min_uV, max_uV);
2788 if (ret >= 0) {
2789 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2790 if (min_uV <= best_val && max_uV >= best_val) {
2791 selector = ret;
2792 if (old_selector == selector)
2793 ret = 0;
2794 else
2795 ret = _regulator_call_set_voltage_sel(
2796 rdev, best_val, selector);
2797 } else {
2798 ret = -EINVAL;
2799 }
2800 }
2801 } else {
2802 ret = -EINVAL;
2803 }
2804
2805 /* Call set_voltage_time_sel if successfully obtained old_selector */
2806 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2807 && old_selector != selector) {
2808
2809 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2810 old_selector, selector);
2811 if (delay < 0) {
2812 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2813 delay);
2814 delay = 0;
2815 }
2816
2817 /* Insert any necessary delays */
2818 if (delay >= 1000) {
2819 mdelay(delay / 1000);
2820 udelay(delay % 1000);
2821 } else if (delay) {
2822 udelay(delay);
2823 }
2824 }
2825
2826 if (ret == 0 && best_val >= 0) {
2827 unsigned long data = best_val;
2828
2829 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2830 (void *)data);
2831 }
2832
2833 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2834
2835 return ret;
2836 }
2837
2838 static int regulator_set_voltage_unlocked(struct regulator *regulator,
2839 int min_uV, int max_uV)
2840 {
2841 struct regulator_dev *rdev = regulator->rdev;
2842 int ret = 0;
2843 int old_min_uV, old_max_uV;
2844 int current_uV;
2845 int best_supply_uV = 0;
2846 int supply_change_uV = 0;
2847
2848 /* If we're setting the same range as last time the change
2849 * should be a noop (some cpufreq implementations use the same
2850 * voltage for multiple frequencies, for example).
2851 */
2852 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2853 goto out;
2854
2855 /* If we're trying to set a range that overlaps the current voltage,
2856 * return successfully even though the regulator does not support
2857 * changing the voltage.
2858 */
2859 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2860 current_uV = _regulator_get_voltage(rdev);
2861 if (min_uV <= current_uV && current_uV <= max_uV) {
2862 regulator->min_uV = min_uV;
2863 regulator->max_uV = max_uV;
2864 goto out;
2865 }
2866 }
2867
2868 /* sanity check */
2869 if (!rdev->desc->ops->set_voltage &&
2870 !rdev->desc->ops->set_voltage_sel) {
2871 ret = -EINVAL;
2872 goto out;
2873 }
2874
2875 /* constraints check */
2876 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2877 if (ret < 0)
2878 goto out;
2879
2880 /* restore original values in case of error */
2881 old_min_uV = regulator->min_uV;
2882 old_max_uV = regulator->max_uV;
2883 regulator->min_uV = min_uV;
2884 regulator->max_uV = max_uV;
2885
2886 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2887 if (ret < 0)
2888 goto out2;
2889
2890 if (rdev->supply && (rdev->desc->min_dropout_uV ||
2891 !rdev->desc->ops->get_voltage)) {
2892 int current_supply_uV;
2893 int selector;
2894
2895 selector = regulator_map_voltage(rdev, min_uV, max_uV);
2896 if (selector < 0) {
2897 ret = selector;
2898 goto out2;
2899 }
2900
2901 best_supply_uV = _regulator_list_voltage(regulator, selector, 0);
2902 if (best_supply_uV < 0) {
2903 ret = best_supply_uV;
2904 goto out2;
2905 }
2906
2907 best_supply_uV += rdev->desc->min_dropout_uV;
2908
2909 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
2910 if (current_supply_uV < 0) {
2911 ret = current_supply_uV;
2912 goto out2;
2913 }
2914
2915 supply_change_uV = best_supply_uV - current_supply_uV;
2916 }
2917
2918 if (supply_change_uV > 0) {
2919 ret = regulator_set_voltage_unlocked(rdev->supply,
2920 best_supply_uV, INT_MAX);
2921 if (ret) {
2922 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
2923 ret);
2924 goto out2;
2925 }
2926 }
2927
2928 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2929 if (ret < 0)
2930 goto out2;
2931
2932 if (supply_change_uV < 0) {
2933 ret = regulator_set_voltage_unlocked(rdev->supply,
2934 best_supply_uV, INT_MAX);
2935 if (ret)
2936 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
2937 ret);
2938 /* No need to fail here */
2939 ret = 0;
2940 }
2941
2942 out:
2943 return ret;
2944 out2:
2945 regulator->min_uV = old_min_uV;
2946 regulator->max_uV = old_max_uV;
2947
2948 return ret;
2949 }
2950
2951 /**
2952 * regulator_set_voltage - set regulator output voltage
2953 * @regulator: regulator source
2954 * @min_uV: Minimum required voltage in uV
2955 * @max_uV: Maximum acceptable voltage in uV
2956 *
2957 * Sets a voltage regulator to the desired output voltage. This can be set
2958 * during any regulator state. IOW, regulator can be disabled or enabled.
2959 *
2960 * If the regulator is enabled then the voltage will change to the new value
2961 * immediately otherwise if the regulator is disabled the regulator will
2962 * output at the new voltage when enabled.
2963 *
2964 * NOTE: If the regulator is shared between several devices then the lowest
2965 * request voltage that meets the system constraints will be used.
2966 * Regulator system constraints must be set for this regulator before
2967 * calling this function otherwise this call will fail.
2968 */
2969 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2970 {
2971 int ret = 0;
2972
2973 regulator_lock_supply(regulator->rdev);
2974
2975 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV);
2976
2977 regulator_unlock_supply(regulator->rdev);
2978
2979 return ret;
2980 }
2981 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2982
2983 /**
2984 * regulator_set_voltage_time - get raise/fall time
2985 * @regulator: regulator source
2986 * @old_uV: starting voltage in microvolts
2987 * @new_uV: target voltage in microvolts
2988 *
2989 * Provided with the starting and ending voltage, this function attempts to
2990 * calculate the time in microseconds required to rise or fall to this new
2991 * voltage.
2992 */
2993 int regulator_set_voltage_time(struct regulator *regulator,
2994 int old_uV, int new_uV)
2995 {
2996 struct regulator_dev *rdev = regulator->rdev;
2997 const struct regulator_ops *ops = rdev->desc->ops;
2998 int old_sel = -1;
2999 int new_sel = -1;
3000 int voltage;
3001 int i;
3002
3003 /* Currently requires operations to do this */
3004 if (!ops->list_voltage || !ops->set_voltage_time_sel
3005 || !rdev->desc->n_voltages)
3006 return -EINVAL;
3007
3008 for (i = 0; i < rdev->desc->n_voltages; i++) {
3009 /* We only look for exact voltage matches here */
3010 voltage = regulator_list_voltage(regulator, i);
3011 if (voltage < 0)
3012 return -EINVAL;
3013 if (voltage == 0)
3014 continue;
3015 if (voltage == old_uV)
3016 old_sel = i;
3017 if (voltage == new_uV)
3018 new_sel = i;
3019 }
3020
3021 if (old_sel < 0 || new_sel < 0)
3022 return -EINVAL;
3023
3024 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3025 }
3026 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3027
3028 /**
3029 * regulator_set_voltage_time_sel - get raise/fall time
3030 * @rdev: regulator source device
3031 * @old_selector: selector for starting voltage
3032 * @new_selector: selector for target voltage
3033 *
3034 * Provided with the starting and target voltage selectors, this function
3035 * returns time in microseconds required to rise or fall to this new voltage
3036 *
3037 * Drivers providing ramp_delay in regulation_constraints can use this as their
3038 * set_voltage_time_sel() operation.
3039 */
3040 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3041 unsigned int old_selector,
3042 unsigned int new_selector)
3043 {
3044 unsigned int ramp_delay = 0;
3045 int old_volt, new_volt;
3046
3047 if (rdev->constraints->ramp_delay)
3048 ramp_delay = rdev->constraints->ramp_delay;
3049 else if (rdev->desc->ramp_delay)
3050 ramp_delay = rdev->desc->ramp_delay;
3051
3052 if (ramp_delay == 0) {
3053 rdev_warn(rdev, "ramp_delay not set\n");
3054 return 0;
3055 }
3056
3057 /* sanity check */
3058 if (!rdev->desc->ops->list_voltage)
3059 return -EINVAL;
3060
3061 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3062 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3063
3064 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
3065 }
3066 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3067
3068 /**
3069 * regulator_sync_voltage - re-apply last regulator output voltage
3070 * @regulator: regulator source
3071 *
3072 * Re-apply the last configured voltage. This is intended to be used
3073 * where some external control source the consumer is cooperating with
3074 * has caused the configured voltage to change.
3075 */
3076 int regulator_sync_voltage(struct regulator *regulator)
3077 {
3078 struct regulator_dev *rdev = regulator->rdev;
3079 int ret, min_uV, max_uV;
3080
3081 mutex_lock(&rdev->mutex);
3082
3083 if (!rdev->desc->ops->set_voltage &&
3084 !rdev->desc->ops->set_voltage_sel) {
3085 ret = -EINVAL;
3086 goto out;
3087 }
3088
3089 /* This is only going to work if we've had a voltage configured. */
3090 if (!regulator->min_uV && !regulator->max_uV) {
3091 ret = -EINVAL;
3092 goto out;
3093 }
3094
3095 min_uV = regulator->min_uV;
3096 max_uV = regulator->max_uV;
3097
3098 /* This should be a paranoia check... */
3099 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3100 if (ret < 0)
3101 goto out;
3102
3103 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
3104 if (ret < 0)
3105 goto out;
3106
3107 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3108
3109 out:
3110 mutex_unlock(&rdev->mutex);
3111 return ret;
3112 }
3113 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3114
3115 static int _regulator_get_voltage(struct regulator_dev *rdev)
3116 {
3117 int sel, ret;
3118 bool bypassed;
3119
3120 if (rdev->desc->ops->get_bypass) {
3121 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3122 if (ret < 0)
3123 return ret;
3124 if (bypassed) {
3125 /* if bypassed the regulator must have a supply */
3126 if (!rdev->supply) {
3127 rdev_err(rdev,
3128 "bypassed regulator has no supply!\n");
3129 return -EPROBE_DEFER;
3130 }
3131
3132 return _regulator_get_voltage(rdev->supply->rdev);
3133 }
3134 }
3135
3136 if (rdev->desc->ops->get_voltage_sel) {
3137 sel = rdev->desc->ops->get_voltage_sel(rdev);
3138 if (sel < 0)
3139 return sel;
3140 ret = rdev->desc->ops->list_voltage(rdev, sel);
3141 } else if (rdev->desc->ops->get_voltage) {
3142 ret = rdev->desc->ops->get_voltage(rdev);
3143 } else if (rdev->desc->ops->list_voltage) {
3144 ret = rdev->desc->ops->list_voltage(rdev, 0);
3145 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3146 ret = rdev->desc->fixed_uV;
3147 } else if (rdev->supply) {
3148 ret = _regulator_get_voltage(rdev->supply->rdev);
3149 } else {
3150 return -EINVAL;
3151 }
3152
3153 if (ret < 0)
3154 return ret;
3155 return ret - rdev->constraints->uV_offset;
3156 }
3157
3158 /**
3159 * regulator_get_voltage - get regulator output voltage
3160 * @regulator: regulator source
3161 *
3162 * This returns the current regulator voltage in uV.
3163 *
3164 * NOTE: If the regulator is disabled it will return the voltage value. This
3165 * function should not be used to determine regulator state.
3166 */
3167 int regulator_get_voltage(struct regulator *regulator)
3168 {
3169 int ret;
3170
3171 regulator_lock_supply(regulator->rdev);
3172
3173 ret = _regulator_get_voltage(regulator->rdev);
3174
3175 regulator_unlock_supply(regulator->rdev);
3176
3177 return ret;
3178 }
3179 EXPORT_SYMBOL_GPL(regulator_get_voltage);
3180
3181 /**
3182 * regulator_set_current_limit - set regulator output current limit
3183 * @regulator: regulator source
3184 * @min_uA: Minimum supported current in uA
3185 * @max_uA: Maximum supported current in uA
3186 *
3187 * Sets current sink to the desired output current. This can be set during
3188 * any regulator state. IOW, regulator can be disabled or enabled.
3189 *
3190 * If the regulator is enabled then the current will change to the new value
3191 * immediately otherwise if the regulator is disabled the regulator will
3192 * output at the new current when enabled.
3193 *
3194 * NOTE: Regulator system constraints must be set for this regulator before
3195 * calling this function otherwise this call will fail.
3196 */
3197 int regulator_set_current_limit(struct regulator *regulator,
3198 int min_uA, int max_uA)
3199 {
3200 struct regulator_dev *rdev = regulator->rdev;
3201 int ret;
3202
3203 mutex_lock(&rdev->mutex);
3204
3205 /* sanity check */
3206 if (!rdev->desc->ops->set_current_limit) {
3207 ret = -EINVAL;
3208 goto out;
3209 }
3210
3211 /* constraints check */
3212 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3213 if (ret < 0)
3214 goto out;
3215
3216 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3217 out:
3218 mutex_unlock(&rdev->mutex);
3219 return ret;
3220 }
3221 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3222
3223 static int _regulator_get_current_limit(struct regulator_dev *rdev)
3224 {
3225 int ret;
3226
3227 mutex_lock(&rdev->mutex);
3228
3229 /* sanity check */
3230 if (!rdev->desc->ops->get_current_limit) {
3231 ret = -EINVAL;
3232 goto out;
3233 }
3234
3235 ret = rdev->desc->ops->get_current_limit(rdev);
3236 out:
3237 mutex_unlock(&rdev->mutex);
3238 return ret;
3239 }
3240
3241 /**
3242 * regulator_get_current_limit - get regulator output current
3243 * @regulator: regulator source
3244 *
3245 * This returns the current supplied by the specified current sink in uA.
3246 *
3247 * NOTE: If the regulator is disabled it will return the current value. This
3248 * function should not be used to determine regulator state.
3249 */
3250 int regulator_get_current_limit(struct regulator *regulator)
3251 {
3252 return _regulator_get_current_limit(regulator->rdev);
3253 }
3254 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3255
3256 /**
3257 * regulator_set_mode - set regulator operating mode
3258 * @regulator: regulator source
3259 * @mode: operating mode - one of the REGULATOR_MODE constants
3260 *
3261 * Set regulator operating mode to increase regulator efficiency or improve
3262 * regulation performance.
3263 *
3264 * NOTE: Regulator system constraints must be set for this regulator before
3265 * calling this function otherwise this call will fail.
3266 */
3267 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3268 {
3269 struct regulator_dev *rdev = regulator->rdev;
3270 int ret;
3271 int regulator_curr_mode;
3272
3273 mutex_lock(&rdev->mutex);
3274
3275 /* sanity check */
3276 if (!rdev->desc->ops->set_mode) {
3277 ret = -EINVAL;
3278 goto out;
3279 }
3280
3281 /* return if the same mode is requested */
3282 if (rdev->desc->ops->get_mode) {
3283 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3284 if (regulator_curr_mode == mode) {
3285 ret = 0;
3286 goto out;
3287 }
3288 }
3289
3290 /* constraints check */
3291 ret = regulator_mode_constrain(rdev, &mode);
3292 if (ret < 0)
3293 goto out;
3294
3295 ret = rdev->desc->ops->set_mode(rdev, mode);
3296 out:
3297 mutex_unlock(&rdev->mutex);
3298 return ret;
3299 }
3300 EXPORT_SYMBOL_GPL(regulator_set_mode);
3301
3302 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3303 {
3304 int ret;
3305
3306 mutex_lock(&rdev->mutex);
3307
3308 /* sanity check */
3309 if (!rdev->desc->ops->get_mode) {
3310 ret = -EINVAL;
3311 goto out;
3312 }
3313
3314 ret = rdev->desc->ops->get_mode(rdev);
3315 out:
3316 mutex_unlock(&rdev->mutex);
3317 return ret;
3318 }
3319
3320 /**
3321 * regulator_get_mode - get regulator operating mode
3322 * @regulator: regulator source
3323 *
3324 * Get the current regulator operating mode.
3325 */
3326 unsigned int regulator_get_mode(struct regulator *regulator)
3327 {
3328 return _regulator_get_mode(regulator->rdev);
3329 }
3330 EXPORT_SYMBOL_GPL(regulator_get_mode);
3331
3332 /**
3333 * regulator_set_load - set regulator load
3334 * @regulator: regulator source
3335 * @uA_load: load current
3336 *
3337 * Notifies the regulator core of a new device load. This is then used by
3338 * DRMS (if enabled by constraints) to set the most efficient regulator
3339 * operating mode for the new regulator loading.
3340 *
3341 * Consumer devices notify their supply regulator of the maximum power
3342 * they will require (can be taken from device datasheet in the power
3343 * consumption tables) when they change operational status and hence power
3344 * state. Examples of operational state changes that can affect power
3345 * consumption are :-
3346 *
3347 * o Device is opened / closed.
3348 * o Device I/O is about to begin or has just finished.
3349 * o Device is idling in between work.
3350 *
3351 * This information is also exported via sysfs to userspace.
3352 *
3353 * DRMS will sum the total requested load on the regulator and change
3354 * to the most efficient operating mode if platform constraints allow.
3355 *
3356 * On error a negative errno is returned.
3357 */
3358 int regulator_set_load(struct regulator *regulator, int uA_load)
3359 {
3360 struct regulator_dev *rdev = regulator->rdev;
3361 int ret;
3362
3363 mutex_lock(&rdev->mutex);
3364 regulator->uA_load = uA_load;
3365 ret = drms_uA_update(rdev);
3366 mutex_unlock(&rdev->mutex);
3367
3368 return ret;
3369 }
3370 EXPORT_SYMBOL_GPL(regulator_set_load);
3371
3372 /**
3373 * regulator_allow_bypass - allow the regulator to go into bypass mode
3374 *
3375 * @regulator: Regulator to configure
3376 * @enable: enable or disable bypass mode
3377 *
3378 * Allow the regulator to go into bypass mode if all other consumers
3379 * for the regulator also enable bypass mode and the machine
3380 * constraints allow this. Bypass mode means that the regulator is
3381 * simply passing the input directly to the output with no regulation.
3382 */
3383 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3384 {
3385 struct regulator_dev *rdev = regulator->rdev;
3386 int ret = 0;
3387
3388 if (!rdev->desc->ops->set_bypass)
3389 return 0;
3390
3391 if (rdev->constraints &&
3392 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3393 return 0;
3394
3395 mutex_lock(&rdev->mutex);
3396
3397 if (enable && !regulator->bypass) {
3398 rdev->bypass_count++;
3399
3400 if (rdev->bypass_count == rdev->open_count) {
3401 ret = rdev->desc->ops->set_bypass(rdev, enable);
3402 if (ret != 0)
3403 rdev->bypass_count--;
3404 }
3405
3406 } else if (!enable && regulator->bypass) {
3407 rdev->bypass_count--;
3408
3409 if (rdev->bypass_count != rdev->open_count) {
3410 ret = rdev->desc->ops->set_bypass(rdev, enable);
3411 if (ret != 0)
3412 rdev->bypass_count++;
3413 }
3414 }
3415
3416 if (ret == 0)
3417 regulator->bypass = enable;
3418
3419 mutex_unlock(&rdev->mutex);
3420
3421 return ret;
3422 }
3423 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3424
3425 /**
3426 * regulator_register_notifier - register regulator event notifier
3427 * @regulator: regulator source
3428 * @nb: notifier block
3429 *
3430 * Register notifier block to receive regulator events.
3431 */
3432 int regulator_register_notifier(struct regulator *regulator,
3433 struct notifier_block *nb)
3434 {
3435 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3436 nb);
3437 }
3438 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3439
3440 /**
3441 * regulator_unregister_notifier - unregister regulator event notifier
3442 * @regulator: regulator source
3443 * @nb: notifier block
3444 *
3445 * Unregister regulator event notifier block.
3446 */
3447 int regulator_unregister_notifier(struct regulator *regulator,
3448 struct notifier_block *nb)
3449 {
3450 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3451 nb);
3452 }
3453 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3454
3455 /* notify regulator consumers and downstream regulator consumers.
3456 * Note mutex must be held by caller.
3457 */
3458 static int _notifier_call_chain(struct regulator_dev *rdev,
3459 unsigned long event, void *data)
3460 {
3461 /* call rdev chain first */
3462 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3463 }
3464
3465 /**
3466 * regulator_bulk_get - get multiple regulator consumers
3467 *
3468 * @dev: Device to supply
3469 * @num_consumers: Number of consumers to register
3470 * @consumers: Configuration of consumers; clients are stored here.
3471 *
3472 * @return 0 on success, an errno on failure.
3473 *
3474 * This helper function allows drivers to get several regulator
3475 * consumers in one operation. If any of the regulators cannot be
3476 * acquired then any regulators that were allocated will be freed
3477 * before returning to the caller.
3478 */
3479 int regulator_bulk_get(struct device *dev, int num_consumers,
3480 struct regulator_bulk_data *consumers)
3481 {
3482 int i;
3483 int ret;
3484
3485 for (i = 0; i < num_consumers; i++)
3486 consumers[i].consumer = NULL;
3487
3488 for (i = 0; i < num_consumers; i++) {
3489 consumers[i].consumer = _regulator_get(dev,
3490 consumers[i].supply,
3491 false,
3492 !consumers[i].optional);
3493 if (IS_ERR(consumers[i].consumer)) {
3494 ret = PTR_ERR(consumers[i].consumer);
3495 dev_err(dev, "Failed to get supply '%s': %d\n",
3496 consumers[i].supply, ret);
3497 consumers[i].consumer = NULL;
3498 goto err;
3499 }
3500 }
3501
3502 return 0;
3503
3504 err:
3505 while (--i >= 0)
3506 regulator_put(consumers[i].consumer);
3507
3508 return ret;
3509 }
3510 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3511
3512 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3513 {
3514 struct regulator_bulk_data *bulk = data;
3515
3516 bulk->ret = regulator_enable(bulk->consumer);
3517 }
3518
3519 /**
3520 * regulator_bulk_enable - enable multiple regulator consumers
3521 *
3522 * @num_consumers: Number of consumers
3523 * @consumers: Consumer data; clients are stored here.
3524 * @return 0 on success, an errno on failure
3525 *
3526 * This convenience API allows consumers to enable multiple regulator
3527 * clients in a single API call. If any consumers cannot be enabled
3528 * then any others that were enabled will be disabled again prior to
3529 * return.
3530 */
3531 int regulator_bulk_enable(int num_consumers,
3532 struct regulator_bulk_data *consumers)
3533 {
3534 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3535 int i;
3536 int ret = 0;
3537
3538 for (i = 0; i < num_consumers; i++) {
3539 if (consumers[i].consumer->always_on)
3540 consumers[i].ret = 0;
3541 else
3542 async_schedule_domain(regulator_bulk_enable_async,
3543 &consumers[i], &async_domain);
3544 }
3545
3546 async_synchronize_full_domain(&async_domain);
3547
3548 /* If any consumer failed we need to unwind any that succeeded */
3549 for (i = 0; i < num_consumers; i++) {
3550 if (consumers[i].ret != 0) {
3551 ret = consumers[i].ret;
3552 goto err;
3553 }
3554 }
3555
3556 return 0;
3557
3558 err:
3559 for (i = 0; i < num_consumers; i++) {
3560 if (consumers[i].ret < 0)
3561 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3562 consumers[i].ret);
3563 else
3564 regulator_disable(consumers[i].consumer);
3565 }
3566
3567 return ret;
3568 }
3569 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3570
3571 /**
3572 * regulator_bulk_disable - disable multiple regulator consumers
3573 *
3574 * @num_consumers: Number of consumers
3575 * @consumers: Consumer data; clients are stored here.
3576 * @return 0 on success, an errno on failure
3577 *
3578 * This convenience API allows consumers to disable multiple regulator
3579 * clients in a single API call. If any consumers cannot be disabled
3580 * then any others that were disabled will be enabled again prior to
3581 * return.
3582 */
3583 int regulator_bulk_disable(int num_consumers,
3584 struct regulator_bulk_data *consumers)
3585 {
3586 int i;
3587 int ret, r;
3588
3589 for (i = num_consumers - 1; i >= 0; --i) {
3590 ret = regulator_disable(consumers[i].consumer);
3591 if (ret != 0)
3592 goto err;
3593 }
3594
3595 return 0;
3596
3597 err:
3598 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3599 for (++i; i < num_consumers; ++i) {
3600 r = regulator_enable(consumers[i].consumer);
3601 if (r != 0)
3602 pr_err("Failed to reename %s: %d\n",
3603 consumers[i].supply, r);
3604 }
3605
3606 return ret;
3607 }
3608 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3609
3610 /**
3611 * regulator_bulk_force_disable - force disable multiple regulator consumers
3612 *
3613 * @num_consumers: Number of consumers
3614 * @consumers: Consumer data; clients are stored here.
3615 * @return 0 on success, an errno on failure
3616 *
3617 * This convenience API allows consumers to forcibly disable multiple regulator
3618 * clients in a single API call.
3619 * NOTE: This should be used for situations when device damage will
3620 * likely occur if the regulators are not disabled (e.g. over temp).
3621 * Although regulator_force_disable function call for some consumers can
3622 * return error numbers, the function is called for all consumers.
3623 */
3624 int regulator_bulk_force_disable(int num_consumers,
3625 struct regulator_bulk_data *consumers)
3626 {
3627 int i;
3628 int ret;
3629
3630 for (i = 0; i < num_consumers; i++)
3631 consumers[i].ret =
3632 regulator_force_disable(consumers[i].consumer);
3633
3634 for (i = 0; i < num_consumers; i++) {
3635 if (consumers[i].ret != 0) {
3636 ret = consumers[i].ret;
3637 goto out;
3638 }
3639 }
3640
3641 return 0;
3642 out:
3643 return ret;
3644 }
3645 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3646
3647 /**
3648 * regulator_bulk_free - free multiple regulator consumers
3649 *
3650 * @num_consumers: Number of consumers
3651 * @consumers: Consumer data; clients are stored here.
3652 *
3653 * This convenience API allows consumers to free multiple regulator
3654 * clients in a single API call.
3655 */
3656 void regulator_bulk_free(int num_consumers,
3657 struct regulator_bulk_data *consumers)
3658 {
3659 int i;
3660
3661 for (i = 0; i < num_consumers; i++) {
3662 regulator_put(consumers[i].consumer);
3663 consumers[i].consumer = NULL;
3664 }
3665 }
3666 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3667
3668 /**
3669 * regulator_notifier_call_chain - call regulator event notifier
3670 * @rdev: regulator source
3671 * @event: notifier block
3672 * @data: callback-specific data.
3673 *
3674 * Called by regulator drivers to notify clients a regulator event has
3675 * occurred. We also notify regulator clients downstream.
3676 * Note lock must be held by caller.
3677 */
3678 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3679 unsigned long event, void *data)
3680 {
3681 lockdep_assert_held_once(&rdev->mutex);
3682
3683 _notifier_call_chain(rdev, event, data);
3684 return NOTIFY_DONE;
3685
3686 }
3687 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3688
3689 /**
3690 * regulator_mode_to_status - convert a regulator mode into a status
3691 *
3692 * @mode: Mode to convert
3693 *
3694 * Convert a regulator mode into a status.
3695 */
3696 int regulator_mode_to_status(unsigned int mode)
3697 {
3698 switch (mode) {
3699 case REGULATOR_MODE_FAST:
3700 return REGULATOR_STATUS_FAST;
3701 case REGULATOR_MODE_NORMAL:
3702 return REGULATOR_STATUS_NORMAL;
3703 case REGULATOR_MODE_IDLE:
3704 return REGULATOR_STATUS_IDLE;
3705 case REGULATOR_MODE_STANDBY:
3706 return REGULATOR_STATUS_STANDBY;
3707 default:
3708 return REGULATOR_STATUS_UNDEFINED;
3709 }
3710 }
3711 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3712
3713 static struct attribute *regulator_dev_attrs[] = {
3714 &dev_attr_name.attr,
3715 &dev_attr_num_users.attr,
3716 &dev_attr_type.attr,
3717 &dev_attr_microvolts.attr,
3718 &dev_attr_microamps.attr,
3719 &dev_attr_opmode.attr,
3720 &dev_attr_state.attr,
3721 &dev_attr_status.attr,
3722 &dev_attr_bypass.attr,
3723 &dev_attr_requested_microamps.attr,
3724 &dev_attr_min_microvolts.attr,
3725 &dev_attr_max_microvolts.attr,
3726 &dev_attr_min_microamps.attr,
3727 &dev_attr_max_microamps.attr,
3728 &dev_attr_suspend_standby_state.attr,
3729 &dev_attr_suspend_mem_state.attr,
3730 &dev_attr_suspend_disk_state.attr,
3731 &dev_attr_suspend_standby_microvolts.attr,
3732 &dev_attr_suspend_mem_microvolts.attr,
3733 &dev_attr_suspend_disk_microvolts.attr,
3734 &dev_attr_suspend_standby_mode.attr,
3735 &dev_attr_suspend_mem_mode.attr,
3736 &dev_attr_suspend_disk_mode.attr,
3737 NULL
3738 };
3739
3740 /*
3741 * To avoid cluttering sysfs (and memory) with useless state, only
3742 * create attributes that can be meaningfully displayed.
3743 */
3744 static umode_t regulator_attr_is_visible(struct kobject *kobj,
3745 struct attribute *attr, int idx)
3746 {
3747 struct device *dev = kobj_to_dev(kobj);
3748 struct regulator_dev *rdev = dev_to_rdev(dev);
3749 const struct regulator_ops *ops = rdev->desc->ops;
3750 umode_t mode = attr->mode;
3751
3752 /* these three are always present */
3753 if (attr == &dev_attr_name.attr ||
3754 attr == &dev_attr_num_users.attr ||
3755 attr == &dev_attr_type.attr)
3756 return mode;
3757
3758 /* some attributes need specific methods to be displayed */
3759 if (attr == &dev_attr_microvolts.attr) {
3760 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3761 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3762 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3763 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3764 return mode;
3765 return 0;
3766 }
3767
3768 if (attr == &dev_attr_microamps.attr)
3769 return ops->get_current_limit ? mode : 0;
3770
3771 if (attr == &dev_attr_opmode.attr)
3772 return ops->get_mode ? mode : 0;
3773
3774 if (attr == &dev_attr_state.attr)
3775 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3776
3777 if (attr == &dev_attr_status.attr)
3778 return ops->get_status ? mode : 0;
3779
3780 if (attr == &dev_attr_bypass.attr)
3781 return ops->get_bypass ? mode : 0;
3782
3783 /* some attributes are type-specific */
3784 if (attr == &dev_attr_requested_microamps.attr)
3785 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
3786
3787 /* constraints need specific supporting methods */
3788 if (attr == &dev_attr_min_microvolts.attr ||
3789 attr == &dev_attr_max_microvolts.attr)
3790 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
3791
3792 if (attr == &dev_attr_min_microamps.attr ||
3793 attr == &dev_attr_max_microamps.attr)
3794 return ops->set_current_limit ? mode : 0;
3795
3796 if (attr == &dev_attr_suspend_standby_state.attr ||
3797 attr == &dev_attr_suspend_mem_state.attr ||
3798 attr == &dev_attr_suspend_disk_state.attr)
3799 return mode;
3800
3801 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3802 attr == &dev_attr_suspend_mem_microvolts.attr ||
3803 attr == &dev_attr_suspend_disk_microvolts.attr)
3804 return ops->set_suspend_voltage ? mode : 0;
3805
3806 if (attr == &dev_attr_suspend_standby_mode.attr ||
3807 attr == &dev_attr_suspend_mem_mode.attr ||
3808 attr == &dev_attr_suspend_disk_mode.attr)
3809 return ops->set_suspend_mode ? mode : 0;
3810
3811 return mode;
3812 }
3813
3814 static const struct attribute_group regulator_dev_group = {
3815 .attrs = regulator_dev_attrs,
3816 .is_visible = regulator_attr_is_visible,
3817 };
3818
3819 static const struct attribute_group *regulator_dev_groups[] = {
3820 &regulator_dev_group,
3821 NULL
3822 };
3823
3824 static void regulator_dev_release(struct device *dev)
3825 {
3826 struct regulator_dev *rdev = dev_get_drvdata(dev);
3827
3828 kfree(rdev->constraints);
3829 of_node_put(rdev->dev.of_node);
3830 kfree(rdev);
3831 }
3832
3833 static struct class regulator_class = {
3834 .name = "regulator",
3835 .dev_release = regulator_dev_release,
3836 .dev_groups = regulator_dev_groups,
3837 };
3838
3839 static void rdev_init_debugfs(struct regulator_dev *rdev)
3840 {
3841 struct device *parent = rdev->dev.parent;
3842 const char *rname = rdev_get_name(rdev);
3843 char name[NAME_MAX];
3844
3845 /* Avoid duplicate debugfs directory names */
3846 if (parent && rname == rdev->desc->name) {
3847 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3848 rname);
3849 rname = name;
3850 }
3851
3852 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
3853 if (!rdev->debugfs) {
3854 rdev_warn(rdev, "Failed to create debugfs directory\n");
3855 return;
3856 }
3857
3858 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3859 &rdev->use_count);
3860 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3861 &rdev->open_count);
3862 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3863 &rdev->bypass_count);
3864 }
3865
3866 static int regulator_register_resolve_supply(struct device *dev, void *data)
3867 {
3868 struct regulator_dev *rdev = dev_to_rdev(dev);
3869
3870 if (regulator_resolve_supply(rdev))
3871 rdev_dbg(rdev, "unable to resolve supply\n");
3872
3873 return 0;
3874 }
3875
3876 /**
3877 * regulator_register - register regulator
3878 * @regulator_desc: regulator to register
3879 * @cfg: runtime configuration for regulator
3880 *
3881 * Called by regulator drivers to register a regulator.
3882 * Returns a valid pointer to struct regulator_dev on success
3883 * or an ERR_PTR() on error.
3884 */
3885 struct regulator_dev *
3886 regulator_register(const struct regulator_desc *regulator_desc,
3887 const struct regulator_config *cfg)
3888 {
3889 const struct regulation_constraints *constraints = NULL;
3890 const struct regulator_init_data *init_data;
3891 struct regulator_config *config = NULL;
3892 static atomic_t regulator_no = ATOMIC_INIT(-1);
3893 struct regulator_dev *rdev;
3894 struct device *dev;
3895 int ret, i;
3896
3897 if (regulator_desc == NULL || cfg == NULL)
3898 return ERR_PTR(-EINVAL);
3899
3900 dev = cfg->dev;
3901 WARN_ON(!dev);
3902
3903 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3904 return ERR_PTR(-EINVAL);
3905
3906 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3907 regulator_desc->type != REGULATOR_CURRENT)
3908 return ERR_PTR(-EINVAL);
3909
3910 /* Only one of each should be implemented */
3911 WARN_ON(regulator_desc->ops->get_voltage &&
3912 regulator_desc->ops->get_voltage_sel);
3913 WARN_ON(regulator_desc->ops->set_voltage &&
3914 regulator_desc->ops->set_voltage_sel);
3915
3916 /* If we're using selectors we must implement list_voltage. */
3917 if (regulator_desc->ops->get_voltage_sel &&
3918 !regulator_desc->ops->list_voltage) {
3919 return ERR_PTR(-EINVAL);
3920 }
3921 if (regulator_desc->ops->set_voltage_sel &&
3922 !regulator_desc->ops->list_voltage) {
3923 return ERR_PTR(-EINVAL);
3924 }
3925
3926 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3927 if (rdev == NULL)
3928 return ERR_PTR(-ENOMEM);
3929
3930 /*
3931 * Duplicate the config so the driver could override it after
3932 * parsing init data.
3933 */
3934 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3935 if (config == NULL) {
3936 kfree(rdev);
3937 return ERR_PTR(-ENOMEM);
3938 }
3939
3940 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
3941 &rdev->dev.of_node);
3942 if (!init_data) {
3943 init_data = config->init_data;
3944 rdev->dev.of_node = of_node_get(config->of_node);
3945 }
3946
3947 mutex_init(&rdev->mutex);
3948 rdev->reg_data = config->driver_data;
3949 rdev->owner = regulator_desc->owner;
3950 rdev->desc = regulator_desc;
3951 if (config->regmap)
3952 rdev->regmap = config->regmap;
3953 else if (dev_get_regmap(dev, NULL))
3954 rdev->regmap = dev_get_regmap(dev, NULL);
3955 else if (dev->parent)
3956 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3957 INIT_LIST_HEAD(&rdev->consumer_list);
3958 INIT_LIST_HEAD(&rdev->list);
3959 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3960 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3961
3962 /* preform any regulator specific init */
3963 if (init_data && init_data->regulator_init) {
3964 ret = init_data->regulator_init(rdev->reg_data);
3965 if (ret < 0)
3966 goto clean;
3967 }
3968
3969 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3970 gpio_is_valid(config->ena_gpio)) {
3971 mutex_lock(&regulator_list_mutex);
3972 ret = regulator_ena_gpio_request(rdev, config);
3973 mutex_unlock(&regulator_list_mutex);
3974 if (ret != 0) {
3975 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3976 config->ena_gpio, ret);
3977 goto clean;
3978 }
3979 }
3980
3981 /* register with sysfs */
3982 rdev->dev.class = &regulator_class;
3983 rdev->dev.parent = dev;
3984 dev_set_name(&rdev->dev, "regulator.%lu",
3985 (unsigned long) atomic_inc_return(&regulator_no));
3986
3987 /* set regulator constraints */
3988 if (init_data)
3989 constraints = &init_data->constraints;
3990
3991 if (init_data && init_data->supply_regulator)
3992 rdev->supply_name = init_data->supply_regulator;
3993 else if (regulator_desc->supply_name)
3994 rdev->supply_name = regulator_desc->supply_name;
3995
3996 /*
3997 * Attempt to resolve the regulator supply, if specified,
3998 * but don't return an error if we fail because we will try
3999 * to resolve it again later as more regulators are added.
4000 */
4001 if (regulator_resolve_supply(rdev))
4002 rdev_dbg(rdev, "unable to resolve supply\n");
4003
4004 ret = set_machine_constraints(rdev, constraints);
4005 if (ret < 0)
4006 goto wash;
4007
4008 /* add consumers devices */
4009 if (init_data) {
4010 mutex_lock(&regulator_list_mutex);
4011 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4012 ret = set_consumer_device_supply(rdev,
4013 init_data->consumer_supplies[i].dev_name,
4014 init_data->consumer_supplies[i].supply);
4015 if (ret < 0) {
4016 mutex_unlock(&regulator_list_mutex);
4017 dev_err(dev, "Failed to set supply %s\n",
4018 init_data->consumer_supplies[i].supply);
4019 goto unset_supplies;
4020 }
4021 }
4022 mutex_unlock(&regulator_list_mutex);
4023 }
4024
4025 ret = device_register(&rdev->dev);
4026 if (ret != 0) {
4027 put_device(&rdev->dev);
4028 goto unset_supplies;
4029 }
4030
4031 dev_set_drvdata(&rdev->dev, rdev);
4032 rdev_init_debugfs(rdev);
4033
4034 /* try to resolve regulators supply since a new one was registered */
4035 class_for_each_device(&regulator_class, NULL, NULL,
4036 regulator_register_resolve_supply);
4037 kfree(config);
4038 return rdev;
4039
4040 unset_supplies:
4041 mutex_lock(&regulator_list_mutex);
4042 unset_regulator_supplies(rdev);
4043 mutex_unlock(&regulator_list_mutex);
4044 wash:
4045 kfree(rdev->constraints);
4046 mutex_lock(&regulator_list_mutex);
4047 regulator_ena_gpio_free(rdev);
4048 mutex_unlock(&regulator_list_mutex);
4049 clean:
4050 kfree(rdev);
4051 kfree(config);
4052 return ERR_PTR(ret);
4053 }
4054 EXPORT_SYMBOL_GPL(regulator_register);
4055
4056 /**
4057 * regulator_unregister - unregister regulator
4058 * @rdev: regulator to unregister
4059 *
4060 * Called by regulator drivers to unregister a regulator.
4061 */
4062 void regulator_unregister(struct regulator_dev *rdev)
4063 {
4064 if (rdev == NULL)
4065 return;
4066
4067 if (rdev->supply) {
4068 while (rdev->use_count--)
4069 regulator_disable(rdev->supply);
4070 regulator_put(rdev->supply);
4071 }
4072 mutex_lock(&regulator_list_mutex);
4073 debugfs_remove_recursive(rdev->debugfs);
4074 flush_work(&rdev->disable_work.work);
4075 WARN_ON(rdev->open_count);
4076 unset_regulator_supplies(rdev);
4077 list_del(&rdev->list);
4078 regulator_ena_gpio_free(rdev);
4079 mutex_unlock(&regulator_list_mutex);
4080 device_unregister(&rdev->dev);
4081 }
4082 EXPORT_SYMBOL_GPL(regulator_unregister);
4083
4084 static int _regulator_suspend_prepare(struct device *dev, void *data)
4085 {
4086 struct regulator_dev *rdev = dev_to_rdev(dev);
4087 const suspend_state_t *state = data;
4088 int ret;
4089
4090 mutex_lock(&rdev->mutex);
4091 ret = suspend_prepare(rdev, *state);
4092 mutex_unlock(&rdev->mutex);
4093
4094 return ret;
4095 }
4096
4097 /**
4098 * regulator_suspend_prepare - prepare regulators for system wide suspend
4099 * @state: system suspend state
4100 *
4101 * Configure each regulator with it's suspend operating parameters for state.
4102 * This will usually be called by machine suspend code prior to supending.
4103 */
4104 int regulator_suspend_prepare(suspend_state_t state)
4105 {
4106 /* ON is handled by regulator active state */
4107 if (state == PM_SUSPEND_ON)
4108 return -EINVAL;
4109
4110 return class_for_each_device(&regulator_class, NULL, &state,
4111 _regulator_suspend_prepare);
4112 }
4113 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
4114
4115 static int _regulator_suspend_finish(struct device *dev, void *data)
4116 {
4117 struct regulator_dev *rdev = dev_to_rdev(dev);
4118 int ret;
4119
4120 mutex_lock(&rdev->mutex);
4121 if (rdev->use_count > 0 || rdev->constraints->always_on) {
4122 if (!_regulator_is_enabled(rdev)) {
4123 ret = _regulator_do_enable(rdev);
4124 if (ret)
4125 dev_err(dev,
4126 "Failed to resume regulator %d\n",
4127 ret);
4128 }
4129 } else {
4130 if (!have_full_constraints())
4131 goto unlock;
4132 if (!_regulator_is_enabled(rdev))
4133 goto unlock;
4134
4135 ret = _regulator_do_disable(rdev);
4136 if (ret)
4137 dev_err(dev, "Failed to suspend regulator %d\n", ret);
4138 }
4139 unlock:
4140 mutex_unlock(&rdev->mutex);
4141
4142 /* Keep processing regulators in spite of any errors */
4143 return 0;
4144 }
4145
4146 /**
4147 * regulator_suspend_finish - resume regulators from system wide suspend
4148 *
4149 * Turn on regulators that might be turned off by regulator_suspend_prepare
4150 * and that should be turned on according to the regulators properties.
4151 */
4152 int regulator_suspend_finish(void)
4153 {
4154 return class_for_each_device(&regulator_class, NULL, NULL,
4155 _regulator_suspend_finish);
4156 }
4157 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
4158
4159 /**
4160 * regulator_has_full_constraints - the system has fully specified constraints
4161 *
4162 * Calling this function will cause the regulator API to disable all
4163 * regulators which have a zero use count and don't have an always_on
4164 * constraint in a late_initcall.
4165 *
4166 * The intention is that this will become the default behaviour in a
4167 * future kernel release so users are encouraged to use this facility
4168 * now.
4169 */
4170 void regulator_has_full_constraints(void)
4171 {
4172 has_full_constraints = 1;
4173 }
4174 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4175
4176 /**
4177 * rdev_get_drvdata - get rdev regulator driver data
4178 * @rdev: regulator
4179 *
4180 * Get rdev regulator driver private data. This call can be used in the
4181 * regulator driver context.
4182 */
4183 void *rdev_get_drvdata(struct regulator_dev *rdev)
4184 {
4185 return rdev->reg_data;
4186 }
4187 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4188
4189 /**
4190 * regulator_get_drvdata - get regulator driver data
4191 * @regulator: regulator
4192 *
4193 * Get regulator driver private data. This call can be used in the consumer
4194 * driver context when non API regulator specific functions need to be called.
4195 */
4196 void *regulator_get_drvdata(struct regulator *regulator)
4197 {
4198 return regulator->rdev->reg_data;
4199 }
4200 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4201
4202 /**
4203 * regulator_set_drvdata - set regulator driver data
4204 * @regulator: regulator
4205 * @data: data
4206 */
4207 void regulator_set_drvdata(struct regulator *regulator, void *data)
4208 {
4209 regulator->rdev->reg_data = data;
4210 }
4211 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4212
4213 /**
4214 * regulator_get_id - get regulator ID
4215 * @rdev: regulator
4216 */
4217 int rdev_get_id(struct regulator_dev *rdev)
4218 {
4219 return rdev->desc->id;
4220 }
4221 EXPORT_SYMBOL_GPL(rdev_get_id);
4222
4223 struct device *rdev_get_dev(struct regulator_dev *rdev)
4224 {
4225 return &rdev->dev;
4226 }
4227 EXPORT_SYMBOL_GPL(rdev_get_dev);
4228
4229 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4230 {
4231 return reg_init_data->driver_data;
4232 }
4233 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4234
4235 #ifdef CONFIG_DEBUG_FS
4236 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4237 size_t count, loff_t *ppos)
4238 {
4239 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4240 ssize_t len, ret = 0;
4241 struct regulator_map *map;
4242
4243 if (!buf)
4244 return -ENOMEM;
4245
4246 list_for_each_entry(map, &regulator_map_list, list) {
4247 len = snprintf(buf + ret, PAGE_SIZE - ret,
4248 "%s -> %s.%s\n",
4249 rdev_get_name(map->regulator), map->dev_name,
4250 map->supply);
4251 if (len >= 0)
4252 ret += len;
4253 if (ret > PAGE_SIZE) {
4254 ret = PAGE_SIZE;
4255 break;
4256 }
4257 }
4258
4259 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4260
4261 kfree(buf);
4262
4263 return ret;
4264 }
4265 #endif
4266
4267 static const struct file_operations supply_map_fops = {
4268 #ifdef CONFIG_DEBUG_FS
4269 .read = supply_map_read_file,
4270 .llseek = default_llseek,
4271 #endif
4272 };
4273
4274 #ifdef CONFIG_DEBUG_FS
4275 struct summary_data {
4276 struct seq_file *s;
4277 struct regulator_dev *parent;
4278 int level;
4279 };
4280
4281 static void regulator_summary_show_subtree(struct seq_file *s,
4282 struct regulator_dev *rdev,
4283 int level);
4284
4285 static int regulator_summary_show_children(struct device *dev, void *data)
4286 {
4287 struct regulator_dev *rdev = dev_to_rdev(dev);
4288 struct summary_data *summary_data = data;
4289
4290 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4291 regulator_summary_show_subtree(summary_data->s, rdev,
4292 summary_data->level + 1);
4293
4294 return 0;
4295 }
4296
4297 static void regulator_summary_show_subtree(struct seq_file *s,
4298 struct regulator_dev *rdev,
4299 int level)
4300 {
4301 struct regulation_constraints *c;
4302 struct regulator *consumer;
4303 struct summary_data summary_data;
4304
4305 if (!rdev)
4306 return;
4307
4308 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4309 level * 3 + 1, "",
4310 30 - level * 3, rdev_get_name(rdev),
4311 rdev->use_count, rdev->open_count, rdev->bypass_count);
4312
4313 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4314 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
4315
4316 c = rdev->constraints;
4317 if (c) {
4318 switch (rdev->desc->type) {
4319 case REGULATOR_VOLTAGE:
4320 seq_printf(s, "%5dmV %5dmV ",
4321 c->min_uV / 1000, c->max_uV / 1000);
4322 break;
4323 case REGULATOR_CURRENT:
4324 seq_printf(s, "%5dmA %5dmA ",
4325 c->min_uA / 1000, c->max_uA / 1000);
4326 break;
4327 }
4328 }
4329
4330 seq_puts(s, "\n");
4331
4332 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4333 if (consumer->dev->class == &regulator_class)
4334 continue;
4335
4336 seq_printf(s, "%*s%-*s ",
4337 (level + 1) * 3 + 1, "",
4338 30 - (level + 1) * 3, dev_name(consumer->dev));
4339
4340 switch (rdev->desc->type) {
4341 case REGULATOR_VOLTAGE:
4342 seq_printf(s, "%37dmV %5dmV",
4343 consumer->min_uV / 1000,
4344 consumer->max_uV / 1000);
4345 break;
4346 case REGULATOR_CURRENT:
4347 break;
4348 }
4349
4350 seq_puts(s, "\n");
4351 }
4352
4353 summary_data.s = s;
4354 summary_data.level = level;
4355 summary_data.parent = rdev;
4356
4357 class_for_each_device(&regulator_class, NULL, &summary_data,
4358 regulator_summary_show_children);
4359 }
4360
4361 static int regulator_summary_show_roots(struct device *dev, void *data)
4362 {
4363 struct regulator_dev *rdev = dev_to_rdev(dev);
4364 struct seq_file *s = data;
4365
4366 if (!rdev->supply)
4367 regulator_summary_show_subtree(s, rdev, 0);
4368
4369 return 0;
4370 }
4371
4372 static int regulator_summary_show(struct seq_file *s, void *data)
4373 {
4374 seq_puts(s, " regulator use open bypass voltage current min max\n");
4375 seq_puts(s, "-------------------------------------------------------------------------------\n");
4376
4377 class_for_each_device(&regulator_class, NULL, s,
4378 regulator_summary_show_roots);
4379
4380 return 0;
4381 }
4382
4383 static int regulator_summary_open(struct inode *inode, struct file *file)
4384 {
4385 return single_open(file, regulator_summary_show, inode->i_private);
4386 }
4387 #endif
4388
4389 static const struct file_operations regulator_summary_fops = {
4390 #ifdef CONFIG_DEBUG_FS
4391 .open = regulator_summary_open,
4392 .read = seq_read,
4393 .llseek = seq_lseek,
4394 .release = single_release,
4395 #endif
4396 };
4397
4398 static int __init regulator_init(void)
4399 {
4400 int ret;
4401
4402 ret = class_register(&regulator_class);
4403
4404 debugfs_root = debugfs_create_dir("regulator", NULL);
4405 if (!debugfs_root)
4406 pr_warn("regulator: Failed to create debugfs directory\n");
4407
4408 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4409 &supply_map_fops);
4410
4411 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4412 NULL, &regulator_summary_fops);
4413
4414 regulator_dummy_init();
4415
4416 return ret;
4417 }
4418
4419 /* init early to allow our consumers to complete system booting */
4420 core_initcall(regulator_init);
4421
4422 static int __init regulator_late_cleanup(struct device *dev, void *data)
4423 {
4424 struct regulator_dev *rdev = dev_to_rdev(dev);
4425 const struct regulator_ops *ops = rdev->desc->ops;
4426 struct regulation_constraints *c = rdev->constraints;
4427 int enabled, ret;
4428
4429 if (c && c->always_on)
4430 return 0;
4431
4432 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4433 return 0;
4434
4435 mutex_lock(&rdev->mutex);
4436
4437 if (rdev->use_count)
4438 goto unlock;
4439
4440 /* If we can't read the status assume it's on. */
4441 if (ops->is_enabled)
4442 enabled = ops->is_enabled(rdev);
4443 else
4444 enabled = 1;
4445
4446 if (!enabled)
4447 goto unlock;
4448
4449 if (have_full_constraints()) {
4450 /* We log since this may kill the system if it goes
4451 * wrong. */
4452 rdev_info(rdev, "disabling\n");
4453 ret = _regulator_do_disable(rdev);
4454 if (ret != 0)
4455 rdev_err(rdev, "couldn't disable: %d\n", ret);
4456 } else {
4457 /* The intention is that in future we will
4458 * assume that full constraints are provided
4459 * so warn even if we aren't going to do
4460 * anything here.
4461 */
4462 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4463 }
4464
4465 unlock:
4466 mutex_unlock(&rdev->mutex);
4467
4468 return 0;
4469 }
4470
4471 static int __init regulator_init_complete(void)
4472 {
4473 /*
4474 * Since DT doesn't provide an idiomatic mechanism for
4475 * enabling full constraints and since it's much more natural
4476 * with DT to provide them just assume that a DT enabled
4477 * system has full constraints.
4478 */
4479 if (of_have_populated_dt())
4480 has_full_constraints = true;
4481
4482 /* If we have a full configuration then disable any regulators
4483 * we have permission to change the status for and which are
4484 * not in use or always_on. This is effectively the default
4485 * for DT and ACPI as they have full constraints.
4486 */
4487 class_for_each_device(&regulator_class, NULL, NULL,
4488 regulator_late_cleanup);
4489
4490 return 0;
4491 }
4492 late_initcall_sync(regulator_init_complete);
This page took 0.190693 seconds and 6 git commands to generate.