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