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