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