]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/regulator/core.c
regulator: Check for constraints in regulator_init_complete()
[mirror_ubuntu-bionic-kernel.git] / drivers / regulator / core.c
1 /*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
6 *
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/mutex.h>
21 #include <linux/suspend.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/machine.h>
25
26 #define REGULATOR_VERSION "0.5"
27
28 static DEFINE_MUTEX(regulator_list_mutex);
29 static LIST_HEAD(regulator_list);
30 static LIST_HEAD(regulator_map_list);
31 static int has_full_constraints;
32
33 /*
34 * struct regulator_map
35 *
36 * Used to provide symbolic supply names to devices.
37 */
38 struct regulator_map {
39 struct list_head list;
40 const char *dev_name; /* The dev_name() for the consumer */
41 const char *supply;
42 struct regulator_dev *regulator;
43 };
44
45 /*
46 * struct regulator
47 *
48 * One for each consumer device.
49 */
50 struct regulator {
51 struct device *dev;
52 struct list_head list;
53 int uA_load;
54 int min_uV;
55 int max_uV;
56 char *supply_name;
57 struct device_attribute dev_attr;
58 struct regulator_dev *rdev;
59 };
60
61 static int _regulator_is_enabled(struct regulator_dev *rdev);
62 static int _regulator_disable(struct regulator_dev *rdev);
63 static int _regulator_get_voltage(struct regulator_dev *rdev);
64 static int _regulator_get_current_limit(struct regulator_dev *rdev);
65 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
66 static void _notifier_call_chain(struct regulator_dev *rdev,
67 unsigned long event, void *data);
68
69 /* gets the regulator for a given consumer device */
70 static struct regulator *get_device_regulator(struct device *dev)
71 {
72 struct regulator *regulator = NULL;
73 struct regulator_dev *rdev;
74
75 mutex_lock(&regulator_list_mutex);
76 list_for_each_entry(rdev, &regulator_list, list) {
77 mutex_lock(&rdev->mutex);
78 list_for_each_entry(regulator, &rdev->consumer_list, list) {
79 if (regulator->dev == dev) {
80 mutex_unlock(&rdev->mutex);
81 mutex_unlock(&regulator_list_mutex);
82 return regulator;
83 }
84 }
85 mutex_unlock(&rdev->mutex);
86 }
87 mutex_unlock(&regulator_list_mutex);
88 return NULL;
89 }
90
91 /* Platform voltage constraint check */
92 static int regulator_check_voltage(struct regulator_dev *rdev,
93 int *min_uV, int *max_uV)
94 {
95 BUG_ON(*min_uV > *max_uV);
96
97 if (!rdev->constraints) {
98 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
99 rdev->desc->name);
100 return -ENODEV;
101 }
102 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
103 printk(KERN_ERR "%s: operation not allowed for %s\n",
104 __func__, rdev->desc->name);
105 return -EPERM;
106 }
107
108 if (*max_uV > rdev->constraints->max_uV)
109 *max_uV = rdev->constraints->max_uV;
110 if (*min_uV < rdev->constraints->min_uV)
111 *min_uV = rdev->constraints->min_uV;
112
113 if (*min_uV > *max_uV)
114 return -EINVAL;
115
116 return 0;
117 }
118
119 /* current constraint check */
120 static int regulator_check_current_limit(struct regulator_dev *rdev,
121 int *min_uA, int *max_uA)
122 {
123 BUG_ON(*min_uA > *max_uA);
124
125 if (!rdev->constraints) {
126 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
127 rdev->desc->name);
128 return -ENODEV;
129 }
130 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
131 printk(KERN_ERR "%s: operation not allowed for %s\n",
132 __func__, rdev->desc->name);
133 return -EPERM;
134 }
135
136 if (*max_uA > rdev->constraints->max_uA)
137 *max_uA = rdev->constraints->max_uA;
138 if (*min_uA < rdev->constraints->min_uA)
139 *min_uA = rdev->constraints->min_uA;
140
141 if (*min_uA > *max_uA)
142 return -EINVAL;
143
144 return 0;
145 }
146
147 /* operating mode constraint check */
148 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
149 {
150 switch (mode) {
151 case REGULATOR_MODE_FAST:
152 case REGULATOR_MODE_NORMAL:
153 case REGULATOR_MODE_IDLE:
154 case REGULATOR_MODE_STANDBY:
155 break;
156 default:
157 return -EINVAL;
158 }
159
160 if (!rdev->constraints) {
161 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
162 rdev->desc->name);
163 return -ENODEV;
164 }
165 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
166 printk(KERN_ERR "%s: operation not allowed for %s\n",
167 __func__, rdev->desc->name);
168 return -EPERM;
169 }
170 if (!(rdev->constraints->valid_modes_mask & mode)) {
171 printk(KERN_ERR "%s: invalid mode %x for %s\n",
172 __func__, mode, rdev->desc->name);
173 return -EINVAL;
174 }
175 return 0;
176 }
177
178 /* dynamic regulator mode switching constraint check */
179 static int regulator_check_drms(struct regulator_dev *rdev)
180 {
181 if (!rdev->constraints) {
182 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
183 rdev->desc->name);
184 return -ENODEV;
185 }
186 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
187 printk(KERN_ERR "%s: operation not allowed for %s\n",
188 __func__, rdev->desc->name);
189 return -EPERM;
190 }
191 return 0;
192 }
193
194 static ssize_t device_requested_uA_show(struct device *dev,
195 struct device_attribute *attr, char *buf)
196 {
197 struct regulator *regulator;
198
199 regulator = get_device_regulator(dev);
200 if (regulator == NULL)
201 return 0;
202
203 return sprintf(buf, "%d\n", regulator->uA_load);
204 }
205
206 static ssize_t regulator_uV_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
208 {
209 struct regulator_dev *rdev = dev_get_drvdata(dev);
210 ssize_t ret;
211
212 mutex_lock(&rdev->mutex);
213 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
214 mutex_unlock(&rdev->mutex);
215
216 return ret;
217 }
218 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
219
220 static ssize_t regulator_uA_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
222 {
223 struct regulator_dev *rdev = dev_get_drvdata(dev);
224
225 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
226 }
227 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
228
229 static ssize_t regulator_name_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
231 {
232 struct regulator_dev *rdev = dev_get_drvdata(dev);
233 const char *name;
234
235 if (rdev->constraints && rdev->constraints->name)
236 name = rdev->constraints->name;
237 else if (rdev->desc->name)
238 name = rdev->desc->name;
239 else
240 name = "";
241
242 return sprintf(buf, "%s\n", name);
243 }
244
245 static ssize_t regulator_print_opmode(char *buf, int mode)
246 {
247 switch (mode) {
248 case REGULATOR_MODE_FAST:
249 return sprintf(buf, "fast\n");
250 case REGULATOR_MODE_NORMAL:
251 return sprintf(buf, "normal\n");
252 case REGULATOR_MODE_IDLE:
253 return sprintf(buf, "idle\n");
254 case REGULATOR_MODE_STANDBY:
255 return sprintf(buf, "standby\n");
256 }
257 return sprintf(buf, "unknown\n");
258 }
259
260 static ssize_t regulator_opmode_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
262 {
263 struct regulator_dev *rdev = dev_get_drvdata(dev);
264
265 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
266 }
267 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
268
269 static ssize_t regulator_print_state(char *buf, int state)
270 {
271 if (state > 0)
272 return sprintf(buf, "enabled\n");
273 else if (state == 0)
274 return sprintf(buf, "disabled\n");
275 else
276 return sprintf(buf, "unknown\n");
277 }
278
279 static ssize_t regulator_state_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
281 {
282 struct regulator_dev *rdev = dev_get_drvdata(dev);
283
284 return regulator_print_state(buf, _regulator_is_enabled(rdev));
285 }
286 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
287
288 static ssize_t regulator_status_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
290 {
291 struct regulator_dev *rdev = dev_get_drvdata(dev);
292 int status;
293 char *label;
294
295 status = rdev->desc->ops->get_status(rdev);
296 if (status < 0)
297 return status;
298
299 switch (status) {
300 case REGULATOR_STATUS_OFF:
301 label = "off";
302 break;
303 case REGULATOR_STATUS_ON:
304 label = "on";
305 break;
306 case REGULATOR_STATUS_ERROR:
307 label = "error";
308 break;
309 case REGULATOR_STATUS_FAST:
310 label = "fast";
311 break;
312 case REGULATOR_STATUS_NORMAL:
313 label = "normal";
314 break;
315 case REGULATOR_STATUS_IDLE:
316 label = "idle";
317 break;
318 case REGULATOR_STATUS_STANDBY:
319 label = "standby";
320 break;
321 default:
322 return -ERANGE;
323 }
324
325 return sprintf(buf, "%s\n", label);
326 }
327 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
328
329 static ssize_t regulator_min_uA_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
331 {
332 struct regulator_dev *rdev = dev_get_drvdata(dev);
333
334 if (!rdev->constraints)
335 return sprintf(buf, "constraint not defined\n");
336
337 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
338 }
339 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
340
341 static ssize_t regulator_max_uA_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
343 {
344 struct regulator_dev *rdev = dev_get_drvdata(dev);
345
346 if (!rdev->constraints)
347 return sprintf(buf, "constraint not defined\n");
348
349 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
350 }
351 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
352
353 static ssize_t regulator_min_uV_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
355 {
356 struct regulator_dev *rdev = dev_get_drvdata(dev);
357
358 if (!rdev->constraints)
359 return sprintf(buf, "constraint not defined\n");
360
361 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
362 }
363 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
364
365 static ssize_t regulator_max_uV_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
367 {
368 struct regulator_dev *rdev = dev_get_drvdata(dev);
369
370 if (!rdev->constraints)
371 return sprintf(buf, "constraint not defined\n");
372
373 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
374 }
375 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
376
377 static ssize_t regulator_total_uA_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
379 {
380 struct regulator_dev *rdev = dev_get_drvdata(dev);
381 struct regulator *regulator;
382 int uA = 0;
383
384 mutex_lock(&rdev->mutex);
385 list_for_each_entry(regulator, &rdev->consumer_list, list)
386 uA += regulator->uA_load;
387 mutex_unlock(&rdev->mutex);
388 return sprintf(buf, "%d\n", uA);
389 }
390 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
391
392 static ssize_t regulator_num_users_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
394 {
395 struct regulator_dev *rdev = dev_get_drvdata(dev);
396 return sprintf(buf, "%d\n", rdev->use_count);
397 }
398
399 static ssize_t regulator_type_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
401 {
402 struct regulator_dev *rdev = dev_get_drvdata(dev);
403
404 switch (rdev->desc->type) {
405 case REGULATOR_VOLTAGE:
406 return sprintf(buf, "voltage\n");
407 case REGULATOR_CURRENT:
408 return sprintf(buf, "current\n");
409 }
410 return sprintf(buf, "unknown\n");
411 }
412
413 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415 {
416 struct regulator_dev *rdev = dev_get_drvdata(dev);
417
418 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
419 }
420 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
421 regulator_suspend_mem_uV_show, NULL);
422
423 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
425 {
426 struct regulator_dev *rdev = dev_get_drvdata(dev);
427
428 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
429 }
430 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
431 regulator_suspend_disk_uV_show, NULL);
432
433 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
434 struct device_attribute *attr, char *buf)
435 {
436 struct regulator_dev *rdev = dev_get_drvdata(dev);
437
438 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
439 }
440 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
441 regulator_suspend_standby_uV_show, NULL);
442
443 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445 {
446 struct regulator_dev *rdev = dev_get_drvdata(dev);
447
448 return regulator_print_opmode(buf,
449 rdev->constraints->state_mem.mode);
450 }
451 static DEVICE_ATTR(suspend_mem_mode, 0444,
452 regulator_suspend_mem_mode_show, NULL);
453
454 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
456 {
457 struct regulator_dev *rdev = dev_get_drvdata(dev);
458
459 return regulator_print_opmode(buf,
460 rdev->constraints->state_disk.mode);
461 }
462 static DEVICE_ATTR(suspend_disk_mode, 0444,
463 regulator_suspend_disk_mode_show, NULL);
464
465 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
467 {
468 struct regulator_dev *rdev = dev_get_drvdata(dev);
469
470 return regulator_print_opmode(buf,
471 rdev->constraints->state_standby.mode);
472 }
473 static DEVICE_ATTR(suspend_standby_mode, 0444,
474 regulator_suspend_standby_mode_show, NULL);
475
476 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
478 {
479 struct regulator_dev *rdev = dev_get_drvdata(dev);
480
481 return regulator_print_state(buf,
482 rdev->constraints->state_mem.enabled);
483 }
484 static DEVICE_ATTR(suspend_mem_state, 0444,
485 regulator_suspend_mem_state_show, NULL);
486
487 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
489 {
490 struct regulator_dev *rdev = dev_get_drvdata(dev);
491
492 return regulator_print_state(buf,
493 rdev->constraints->state_disk.enabled);
494 }
495 static DEVICE_ATTR(suspend_disk_state, 0444,
496 regulator_suspend_disk_state_show, NULL);
497
498 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500 {
501 struct regulator_dev *rdev = dev_get_drvdata(dev);
502
503 return regulator_print_state(buf,
504 rdev->constraints->state_standby.enabled);
505 }
506 static DEVICE_ATTR(suspend_standby_state, 0444,
507 regulator_suspend_standby_state_show, NULL);
508
509
510 /*
511 * These are the only attributes are present for all regulators.
512 * Other attributes are a function of regulator functionality.
513 */
514 static struct device_attribute regulator_dev_attrs[] = {
515 __ATTR(name, 0444, regulator_name_show, NULL),
516 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
517 __ATTR(type, 0444, regulator_type_show, NULL),
518 __ATTR_NULL,
519 };
520
521 static void regulator_dev_release(struct device *dev)
522 {
523 struct regulator_dev *rdev = dev_get_drvdata(dev);
524 kfree(rdev);
525 }
526
527 static struct class regulator_class = {
528 .name = "regulator",
529 .dev_release = regulator_dev_release,
530 .dev_attrs = regulator_dev_attrs,
531 };
532
533 /* Calculate the new optimum regulator operating mode based on the new total
534 * consumer load. All locks held by caller */
535 static void drms_uA_update(struct regulator_dev *rdev)
536 {
537 struct regulator *sibling;
538 int current_uA = 0, output_uV, input_uV, err;
539 unsigned int mode;
540
541 err = regulator_check_drms(rdev);
542 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
543 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
544 return;
545
546 /* get output voltage */
547 output_uV = rdev->desc->ops->get_voltage(rdev);
548 if (output_uV <= 0)
549 return;
550
551 /* get input voltage */
552 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
553 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
554 else
555 input_uV = rdev->constraints->input_uV;
556 if (input_uV <= 0)
557 return;
558
559 /* calc total requested load */
560 list_for_each_entry(sibling, &rdev->consumer_list, list)
561 current_uA += sibling->uA_load;
562
563 /* now get the optimum mode for our new total regulator load */
564 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
565 output_uV, current_uA);
566
567 /* check the new mode is allowed */
568 err = regulator_check_mode(rdev, mode);
569 if (err == 0)
570 rdev->desc->ops->set_mode(rdev, mode);
571 }
572
573 static int suspend_set_state(struct regulator_dev *rdev,
574 struct regulator_state *rstate)
575 {
576 int ret = 0;
577
578 /* enable & disable are mandatory for suspend control */
579 if (!rdev->desc->ops->set_suspend_enable ||
580 !rdev->desc->ops->set_suspend_disable) {
581 printk(KERN_ERR "%s: no way to set suspend state\n",
582 __func__);
583 return -EINVAL;
584 }
585
586 if (rstate->enabled)
587 ret = rdev->desc->ops->set_suspend_enable(rdev);
588 else
589 ret = rdev->desc->ops->set_suspend_disable(rdev);
590 if (ret < 0) {
591 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
592 return ret;
593 }
594
595 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
596 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
597 if (ret < 0) {
598 printk(KERN_ERR "%s: failed to set voltage\n",
599 __func__);
600 return ret;
601 }
602 }
603
604 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
605 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
606 if (ret < 0) {
607 printk(KERN_ERR "%s: failed to set mode\n", __func__);
608 return ret;
609 }
610 }
611 return ret;
612 }
613
614 /* locks held by caller */
615 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
616 {
617 if (!rdev->constraints)
618 return -EINVAL;
619
620 switch (state) {
621 case PM_SUSPEND_STANDBY:
622 return suspend_set_state(rdev,
623 &rdev->constraints->state_standby);
624 case PM_SUSPEND_MEM:
625 return suspend_set_state(rdev,
626 &rdev->constraints->state_mem);
627 case PM_SUSPEND_MAX:
628 return suspend_set_state(rdev,
629 &rdev->constraints->state_disk);
630 default:
631 return -EINVAL;
632 }
633 }
634
635 static void print_constraints(struct regulator_dev *rdev)
636 {
637 struct regulation_constraints *constraints = rdev->constraints;
638 char buf[80];
639 int count;
640
641 if (rdev->desc->type == REGULATOR_VOLTAGE) {
642 if (constraints->min_uV == constraints->max_uV)
643 count = sprintf(buf, "%d mV ",
644 constraints->min_uV / 1000);
645 else
646 count = sprintf(buf, "%d <--> %d mV ",
647 constraints->min_uV / 1000,
648 constraints->max_uV / 1000);
649 } else {
650 if (constraints->min_uA == constraints->max_uA)
651 count = sprintf(buf, "%d mA ",
652 constraints->min_uA / 1000);
653 else
654 count = sprintf(buf, "%d <--> %d mA ",
655 constraints->min_uA / 1000,
656 constraints->max_uA / 1000);
657 }
658 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
659 count += sprintf(buf + count, "fast ");
660 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
661 count += sprintf(buf + count, "normal ");
662 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
663 count += sprintf(buf + count, "idle ");
664 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
665 count += sprintf(buf + count, "standby");
666
667 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
668 }
669
670 /**
671 * set_machine_constraints - sets regulator constraints
672 * @rdev: regulator source
673 * @constraints: constraints to apply
674 *
675 * Allows platform initialisation code to define and constrain
676 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
677 * Constraints *must* be set by platform code in order for some
678 * regulator operations to proceed i.e. set_voltage, set_current_limit,
679 * set_mode.
680 */
681 static int set_machine_constraints(struct regulator_dev *rdev,
682 struct regulation_constraints *constraints)
683 {
684 int ret = 0;
685 const char *name;
686 struct regulator_ops *ops = rdev->desc->ops;
687
688 if (constraints->name)
689 name = constraints->name;
690 else if (rdev->desc->name)
691 name = rdev->desc->name;
692 else
693 name = "regulator";
694
695 /* constrain machine-level voltage specs to fit
696 * the actual range supported by this regulator.
697 */
698 if (ops->list_voltage && rdev->desc->n_voltages) {
699 int count = rdev->desc->n_voltages;
700 int i;
701 int min_uV = INT_MAX;
702 int max_uV = INT_MIN;
703 int cmin = constraints->min_uV;
704 int cmax = constraints->max_uV;
705
706 /* it's safe to autoconfigure fixed-voltage supplies
707 and the constraints are used by list_voltage. */
708 if (count == 1 && !cmin) {
709 cmin = 1;
710 cmax = INT_MAX;
711 constraints->min_uV = cmin;
712 constraints->max_uV = cmax;
713 }
714
715 /* voltage constraints are optional */
716 if ((cmin == 0) && (cmax == 0))
717 goto out;
718
719 /* else require explicit machine-level constraints */
720 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
721 pr_err("%s: %s '%s' voltage constraints\n",
722 __func__, "invalid", name);
723 ret = -EINVAL;
724 goto out;
725 }
726
727 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
728 for (i = 0; i < count; i++) {
729 int value;
730
731 value = ops->list_voltage(rdev, i);
732 if (value <= 0)
733 continue;
734
735 /* maybe adjust [min_uV..max_uV] */
736 if (value >= cmin && value < min_uV)
737 min_uV = value;
738 if (value <= cmax && value > max_uV)
739 max_uV = value;
740 }
741
742 /* final: [min_uV..max_uV] valid iff constraints valid */
743 if (max_uV < min_uV) {
744 pr_err("%s: %s '%s' voltage constraints\n",
745 __func__, "unsupportable", name);
746 ret = -EINVAL;
747 goto out;
748 }
749
750 /* use regulator's subset of machine constraints */
751 if (constraints->min_uV < min_uV) {
752 pr_debug("%s: override '%s' %s, %d -> %d\n",
753 __func__, name, "min_uV",
754 constraints->min_uV, min_uV);
755 constraints->min_uV = min_uV;
756 }
757 if (constraints->max_uV > max_uV) {
758 pr_debug("%s: override '%s' %s, %d -> %d\n",
759 __func__, name, "max_uV",
760 constraints->max_uV, max_uV);
761 constraints->max_uV = max_uV;
762 }
763 }
764
765 rdev->constraints = constraints;
766
767 /* do we need to apply the constraint voltage */
768 if (rdev->constraints->apply_uV &&
769 rdev->constraints->min_uV == rdev->constraints->max_uV &&
770 ops->set_voltage) {
771 ret = ops->set_voltage(rdev,
772 rdev->constraints->min_uV, rdev->constraints->max_uV);
773 if (ret < 0) {
774 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
775 __func__,
776 rdev->constraints->min_uV, name);
777 rdev->constraints = NULL;
778 goto out;
779 }
780 }
781
782 /* do we need to setup our suspend state */
783 if (constraints->initial_state) {
784 ret = suspend_prepare(rdev, constraints->initial_state);
785 if (ret < 0) {
786 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
787 __func__, name);
788 rdev->constraints = NULL;
789 goto out;
790 }
791 }
792
793 if (constraints->initial_mode) {
794 if (!ops->set_mode) {
795 printk(KERN_ERR "%s: no set_mode operation for %s\n",
796 __func__, name);
797 ret = -EINVAL;
798 goto out;
799 }
800
801 ret = ops->set_mode(rdev, constraints->initial_mode);
802 if (ret < 0) {
803 printk(KERN_ERR
804 "%s: failed to set initial mode for %s: %d\n",
805 __func__, name, ret);
806 goto out;
807 }
808 }
809
810 /* If the constraints say the regulator should be on at this point
811 * and we have control then make sure it is enabled.
812 */
813 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
814 ret = ops->enable(rdev);
815 if (ret < 0) {
816 printk(KERN_ERR "%s: failed to enable %s\n",
817 __func__, name);
818 rdev->constraints = NULL;
819 goto out;
820 }
821 }
822
823 print_constraints(rdev);
824 out:
825 return ret;
826 }
827
828 /**
829 * set_supply - set regulator supply regulator
830 * @rdev: regulator name
831 * @supply_rdev: supply regulator name
832 *
833 * Called by platform initialisation code to set the supply regulator for this
834 * regulator. This ensures that a regulators supply will also be enabled by the
835 * core if it's child is enabled.
836 */
837 static int set_supply(struct regulator_dev *rdev,
838 struct regulator_dev *supply_rdev)
839 {
840 int err;
841
842 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
843 "supply");
844 if (err) {
845 printk(KERN_ERR
846 "%s: could not add device link %s err %d\n",
847 __func__, supply_rdev->dev.kobj.name, err);
848 goto out;
849 }
850 rdev->supply = supply_rdev;
851 list_add(&rdev->slist, &supply_rdev->supply_list);
852 out:
853 return err;
854 }
855
856 /**
857 * set_consumer_device_supply: Bind a regulator to a symbolic supply
858 * @rdev: regulator source
859 * @consumer_dev: device the supply applies to
860 * @consumer_dev_name: dev_name() string for device supply applies to
861 * @supply: symbolic name for supply
862 *
863 * Allows platform initialisation code to map physical regulator
864 * sources to symbolic names for supplies for use by devices. Devices
865 * should use these symbolic names to request regulators, avoiding the
866 * need to provide board-specific regulator names as platform data.
867 *
868 * Only one of consumer_dev and consumer_dev_name may be specified.
869 */
870 static int set_consumer_device_supply(struct regulator_dev *rdev,
871 struct device *consumer_dev, const char *consumer_dev_name,
872 const char *supply)
873 {
874 struct regulator_map *node;
875 int has_dev;
876
877 if (consumer_dev && consumer_dev_name)
878 return -EINVAL;
879
880 if (!consumer_dev_name && consumer_dev)
881 consumer_dev_name = dev_name(consumer_dev);
882
883 if (supply == NULL)
884 return -EINVAL;
885
886 if (consumer_dev_name != NULL)
887 has_dev = 1;
888 else
889 has_dev = 0;
890
891 list_for_each_entry(node, &regulator_map_list, list) {
892 if (consumer_dev_name != node->dev_name)
893 continue;
894 if (strcmp(node->supply, supply) != 0)
895 continue;
896
897 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
898 dev_name(&node->regulator->dev),
899 node->regulator->desc->name,
900 supply,
901 dev_name(&rdev->dev), rdev->desc->name);
902 return -EBUSY;
903 }
904
905 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
906 if (node == NULL)
907 return -ENOMEM;
908
909 node->regulator = rdev;
910 node->supply = supply;
911
912 if (has_dev) {
913 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
914 if (node->dev_name == NULL) {
915 kfree(node);
916 return -ENOMEM;
917 }
918 }
919
920 list_add(&node->list, &regulator_map_list);
921 return 0;
922 }
923
924 static void unset_consumer_device_supply(struct regulator_dev *rdev,
925 const char *consumer_dev_name, struct device *consumer_dev)
926 {
927 struct regulator_map *node, *n;
928
929 if (consumer_dev && !consumer_dev_name)
930 consumer_dev_name = dev_name(consumer_dev);
931
932 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
933 if (rdev != node->regulator)
934 continue;
935
936 if (consumer_dev_name && node->dev_name &&
937 strcmp(consumer_dev_name, node->dev_name))
938 continue;
939
940 list_del(&node->list);
941 kfree(node->dev_name);
942 kfree(node);
943 return;
944 }
945 }
946
947 static void unset_regulator_supplies(struct regulator_dev *rdev)
948 {
949 struct regulator_map *node, *n;
950
951 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
952 if (rdev == node->regulator) {
953 list_del(&node->list);
954 kfree(node->dev_name);
955 kfree(node);
956 return;
957 }
958 }
959 }
960
961 #define REG_STR_SIZE 32
962
963 static struct regulator *create_regulator(struct regulator_dev *rdev,
964 struct device *dev,
965 const char *supply_name)
966 {
967 struct regulator *regulator;
968 char buf[REG_STR_SIZE];
969 int err, size;
970
971 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
972 if (regulator == NULL)
973 return NULL;
974
975 mutex_lock(&rdev->mutex);
976 regulator->rdev = rdev;
977 list_add(&regulator->list, &rdev->consumer_list);
978
979 if (dev) {
980 /* create a 'requested_microamps_name' sysfs entry */
981 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
982 supply_name);
983 if (size >= REG_STR_SIZE)
984 goto overflow_err;
985
986 regulator->dev = dev;
987 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
988 if (regulator->dev_attr.attr.name == NULL)
989 goto attr_name_err;
990
991 regulator->dev_attr.attr.owner = THIS_MODULE;
992 regulator->dev_attr.attr.mode = 0444;
993 regulator->dev_attr.show = device_requested_uA_show;
994 err = device_create_file(dev, &regulator->dev_attr);
995 if (err < 0) {
996 printk(KERN_WARNING "%s: could not add regulator_dev"
997 " load sysfs\n", __func__);
998 goto attr_name_err;
999 }
1000
1001 /* also add a link to the device sysfs entry */
1002 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1003 dev->kobj.name, supply_name);
1004 if (size >= REG_STR_SIZE)
1005 goto attr_err;
1006
1007 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1008 if (regulator->supply_name == NULL)
1009 goto attr_err;
1010
1011 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1012 buf);
1013 if (err) {
1014 printk(KERN_WARNING
1015 "%s: could not add device link %s err %d\n",
1016 __func__, dev->kobj.name, err);
1017 device_remove_file(dev, &regulator->dev_attr);
1018 goto link_name_err;
1019 }
1020 }
1021 mutex_unlock(&rdev->mutex);
1022 return regulator;
1023 link_name_err:
1024 kfree(regulator->supply_name);
1025 attr_err:
1026 device_remove_file(regulator->dev, &regulator->dev_attr);
1027 attr_name_err:
1028 kfree(regulator->dev_attr.attr.name);
1029 overflow_err:
1030 list_del(&regulator->list);
1031 kfree(regulator);
1032 mutex_unlock(&rdev->mutex);
1033 return NULL;
1034 }
1035
1036 /* Internal regulator request function */
1037 static struct regulator *_regulator_get(struct device *dev, const char *id,
1038 int exclusive)
1039 {
1040 struct regulator_dev *rdev;
1041 struct regulator_map *map;
1042 struct regulator *regulator = ERR_PTR(-ENODEV);
1043 const char *devname = NULL;
1044 int ret;
1045
1046 if (id == NULL) {
1047 printk(KERN_ERR "regulator: get() with no identifier\n");
1048 return regulator;
1049 }
1050
1051 if (dev)
1052 devname = dev_name(dev);
1053
1054 mutex_lock(&regulator_list_mutex);
1055
1056 list_for_each_entry(map, &regulator_map_list, list) {
1057 /* If the mapping has a device set up it must match */
1058 if (map->dev_name &&
1059 (!devname || strcmp(map->dev_name, devname)))
1060 continue;
1061
1062 if (strcmp(map->supply, id) == 0) {
1063 rdev = map->regulator;
1064 goto found;
1065 }
1066 }
1067 mutex_unlock(&regulator_list_mutex);
1068 return regulator;
1069
1070 found:
1071 if (rdev->exclusive) {
1072 regulator = ERR_PTR(-EPERM);
1073 goto out;
1074 }
1075
1076 if (exclusive && rdev->open_count) {
1077 regulator = ERR_PTR(-EBUSY);
1078 goto out;
1079 }
1080
1081 if (!try_module_get(rdev->owner))
1082 goto out;
1083
1084 regulator = create_regulator(rdev, dev, id);
1085 if (regulator == NULL) {
1086 regulator = ERR_PTR(-ENOMEM);
1087 module_put(rdev->owner);
1088 }
1089
1090 rdev->open_count++;
1091 if (exclusive) {
1092 rdev->exclusive = 1;
1093
1094 ret = _regulator_is_enabled(rdev);
1095 if (ret > 0)
1096 rdev->use_count = 1;
1097 else
1098 rdev->use_count = 0;
1099 }
1100
1101 out:
1102 mutex_unlock(&regulator_list_mutex);
1103
1104 return regulator;
1105 }
1106
1107 /**
1108 * regulator_get - lookup and obtain a reference to a regulator.
1109 * @dev: device for regulator "consumer"
1110 * @id: Supply name or regulator ID.
1111 *
1112 * Returns a struct regulator corresponding to the regulator producer,
1113 * or IS_ERR() condition containing errno.
1114 *
1115 * Use of supply names configured via regulator_set_device_supply() is
1116 * strongly encouraged. It is recommended that the supply name used
1117 * should match the name used for the supply and/or the relevant
1118 * device pins in the datasheet.
1119 */
1120 struct regulator *regulator_get(struct device *dev, const char *id)
1121 {
1122 return _regulator_get(dev, id, 0);
1123 }
1124 EXPORT_SYMBOL_GPL(regulator_get);
1125
1126 /**
1127 * regulator_get_exclusive - obtain exclusive access to a regulator.
1128 * @dev: device for regulator "consumer"
1129 * @id: Supply name or regulator ID.
1130 *
1131 * Returns a struct regulator corresponding to the regulator producer,
1132 * or IS_ERR() condition containing errno. Other consumers will be
1133 * unable to obtain this reference is held and the use count for the
1134 * regulator will be initialised to reflect the current state of the
1135 * regulator.
1136 *
1137 * This is intended for use by consumers which cannot tolerate shared
1138 * use of the regulator such as those which need to force the
1139 * regulator off for correct operation of the hardware they are
1140 * controlling.
1141 *
1142 * Use of supply names configured via regulator_set_device_supply() is
1143 * strongly encouraged. It is recommended that the supply name used
1144 * should match the name used for the supply and/or the relevant
1145 * device pins in the datasheet.
1146 */
1147 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1148 {
1149 return _regulator_get(dev, id, 1);
1150 }
1151 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1152
1153 /**
1154 * regulator_put - "free" the regulator source
1155 * @regulator: regulator source
1156 *
1157 * Note: drivers must ensure that all regulator_enable calls made on this
1158 * regulator source are balanced by regulator_disable calls prior to calling
1159 * this function.
1160 */
1161 void regulator_put(struct regulator *regulator)
1162 {
1163 struct regulator_dev *rdev;
1164
1165 if (regulator == NULL || IS_ERR(regulator))
1166 return;
1167
1168 mutex_lock(&regulator_list_mutex);
1169 rdev = regulator->rdev;
1170
1171 /* remove any sysfs entries */
1172 if (regulator->dev) {
1173 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1174 kfree(regulator->supply_name);
1175 device_remove_file(regulator->dev, &regulator->dev_attr);
1176 kfree(regulator->dev_attr.attr.name);
1177 }
1178 list_del(&regulator->list);
1179 kfree(regulator);
1180
1181 rdev->open_count--;
1182 rdev->exclusive = 0;
1183
1184 module_put(rdev->owner);
1185 mutex_unlock(&regulator_list_mutex);
1186 }
1187 EXPORT_SYMBOL_GPL(regulator_put);
1188
1189 /* locks held by regulator_enable() */
1190 static int _regulator_enable(struct regulator_dev *rdev)
1191 {
1192 int ret = -EINVAL;
1193
1194 if (!rdev->constraints) {
1195 printk(KERN_ERR "%s: %s has no constraints\n",
1196 __func__, rdev->desc->name);
1197 return ret;
1198 }
1199
1200 /* do we need to enable the supply regulator first */
1201 if (rdev->supply) {
1202 ret = _regulator_enable(rdev->supply);
1203 if (ret < 0) {
1204 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1205 __func__, rdev->desc->name, ret);
1206 return ret;
1207 }
1208 }
1209
1210 /* check voltage and requested load before enabling */
1211 if (rdev->desc->ops->enable) {
1212
1213 if (rdev->constraints &&
1214 (rdev->constraints->valid_ops_mask &
1215 REGULATOR_CHANGE_DRMS))
1216 drms_uA_update(rdev);
1217
1218 ret = rdev->desc->ops->enable(rdev);
1219 if (ret < 0) {
1220 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1221 __func__, rdev->desc->name, ret);
1222 return ret;
1223 }
1224 rdev->use_count++;
1225 return ret;
1226 }
1227
1228 return ret;
1229 }
1230
1231 /**
1232 * regulator_enable - enable regulator output
1233 * @regulator: regulator source
1234 *
1235 * Request that the regulator be enabled with the regulator output at
1236 * the predefined voltage or current value. Calls to regulator_enable()
1237 * must be balanced with calls to regulator_disable().
1238 *
1239 * NOTE: the output value can be set by other drivers, boot loader or may be
1240 * hardwired in the regulator.
1241 */
1242 int regulator_enable(struct regulator *regulator)
1243 {
1244 struct regulator_dev *rdev = regulator->rdev;
1245 int ret = 0;
1246
1247 mutex_lock(&rdev->mutex);
1248 ret = _regulator_enable(rdev);
1249 mutex_unlock(&rdev->mutex);
1250 return ret;
1251 }
1252 EXPORT_SYMBOL_GPL(regulator_enable);
1253
1254 /* locks held by regulator_disable() */
1255 static int _regulator_disable(struct regulator_dev *rdev)
1256 {
1257 int ret = 0;
1258
1259 if (WARN(rdev->use_count <= 0,
1260 "unbalanced disables for %s\n",
1261 rdev->desc->name))
1262 return -EIO;
1263
1264 /* are we the last user and permitted to disable ? */
1265 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1266
1267 /* we are last user */
1268 if (rdev->desc->ops->disable) {
1269 ret = rdev->desc->ops->disable(rdev);
1270 if (ret < 0) {
1271 printk(KERN_ERR "%s: failed to disable %s\n",
1272 __func__, rdev->desc->name);
1273 return ret;
1274 }
1275 }
1276
1277 /* decrease our supplies ref count and disable if required */
1278 if (rdev->supply)
1279 _regulator_disable(rdev->supply);
1280
1281 rdev->use_count = 0;
1282 } else if (rdev->use_count > 1) {
1283
1284 if (rdev->constraints &&
1285 (rdev->constraints->valid_ops_mask &
1286 REGULATOR_CHANGE_DRMS))
1287 drms_uA_update(rdev);
1288
1289 rdev->use_count--;
1290 }
1291 return ret;
1292 }
1293
1294 /**
1295 * regulator_disable - disable regulator output
1296 * @regulator: regulator source
1297 *
1298 * Disable the regulator output voltage or current. Calls to
1299 * regulator_enable() must be balanced with calls to
1300 * regulator_disable().
1301 *
1302 * NOTE: this will only disable the regulator output if no other consumer
1303 * devices have it enabled, the regulator device supports disabling and
1304 * machine constraints permit this operation.
1305 */
1306 int regulator_disable(struct regulator *regulator)
1307 {
1308 struct regulator_dev *rdev = regulator->rdev;
1309 int ret = 0;
1310
1311 mutex_lock(&rdev->mutex);
1312 ret = _regulator_disable(rdev);
1313 mutex_unlock(&rdev->mutex);
1314 return ret;
1315 }
1316 EXPORT_SYMBOL_GPL(regulator_disable);
1317
1318 /* locks held by regulator_force_disable() */
1319 static int _regulator_force_disable(struct regulator_dev *rdev)
1320 {
1321 int ret = 0;
1322
1323 /* force disable */
1324 if (rdev->desc->ops->disable) {
1325 /* ah well, who wants to live forever... */
1326 ret = rdev->desc->ops->disable(rdev);
1327 if (ret < 0) {
1328 printk(KERN_ERR "%s: failed to force disable %s\n",
1329 __func__, rdev->desc->name);
1330 return ret;
1331 }
1332 /* notify other consumers that power has been forced off */
1333 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1334 NULL);
1335 }
1336
1337 /* decrease our supplies ref count and disable if required */
1338 if (rdev->supply)
1339 _regulator_disable(rdev->supply);
1340
1341 rdev->use_count = 0;
1342 return ret;
1343 }
1344
1345 /**
1346 * regulator_force_disable - force disable regulator output
1347 * @regulator: regulator source
1348 *
1349 * Forcibly disable the regulator output voltage or current.
1350 * NOTE: this *will* disable the regulator output even if other consumer
1351 * devices have it enabled. This should be used for situations when device
1352 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1353 */
1354 int regulator_force_disable(struct regulator *regulator)
1355 {
1356 int ret;
1357
1358 mutex_lock(&regulator->rdev->mutex);
1359 regulator->uA_load = 0;
1360 ret = _regulator_force_disable(regulator->rdev);
1361 mutex_unlock(&regulator->rdev->mutex);
1362 return ret;
1363 }
1364 EXPORT_SYMBOL_GPL(regulator_force_disable);
1365
1366 static int _regulator_is_enabled(struct regulator_dev *rdev)
1367 {
1368 int ret;
1369
1370 mutex_lock(&rdev->mutex);
1371
1372 /* sanity check */
1373 if (!rdev->desc->ops->is_enabled) {
1374 ret = -EINVAL;
1375 goto out;
1376 }
1377
1378 ret = rdev->desc->ops->is_enabled(rdev);
1379 out:
1380 mutex_unlock(&rdev->mutex);
1381 return ret;
1382 }
1383
1384 /**
1385 * regulator_is_enabled - is the regulator output enabled
1386 * @regulator: regulator source
1387 *
1388 * Returns positive if the regulator driver backing the source/client
1389 * has requested that the device be enabled, zero if it hasn't, else a
1390 * negative errno code.
1391 *
1392 * Note that the device backing this regulator handle can have multiple
1393 * users, so it might be enabled even if regulator_enable() was never
1394 * called for this particular source.
1395 */
1396 int regulator_is_enabled(struct regulator *regulator)
1397 {
1398 return _regulator_is_enabled(regulator->rdev);
1399 }
1400 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1401
1402 /**
1403 * regulator_count_voltages - count regulator_list_voltage() selectors
1404 * @regulator: regulator source
1405 *
1406 * Returns number of selectors, or negative errno. Selectors are
1407 * numbered starting at zero, and typically correspond to bitfields
1408 * in hardware registers.
1409 */
1410 int regulator_count_voltages(struct regulator *regulator)
1411 {
1412 struct regulator_dev *rdev = regulator->rdev;
1413
1414 return rdev->desc->n_voltages ? : -EINVAL;
1415 }
1416 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1417
1418 /**
1419 * regulator_list_voltage - enumerate supported voltages
1420 * @regulator: regulator source
1421 * @selector: identify voltage to list
1422 * Context: can sleep
1423 *
1424 * Returns a voltage that can be passed to @regulator_set_voltage(),
1425 * zero if this selector code can't be used on this sytem, or a
1426 * negative errno.
1427 */
1428 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1429 {
1430 struct regulator_dev *rdev = regulator->rdev;
1431 struct regulator_ops *ops = rdev->desc->ops;
1432 int ret;
1433
1434 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1435 return -EINVAL;
1436
1437 mutex_lock(&rdev->mutex);
1438 ret = ops->list_voltage(rdev, selector);
1439 mutex_unlock(&rdev->mutex);
1440
1441 if (ret > 0) {
1442 if (ret < rdev->constraints->min_uV)
1443 ret = 0;
1444 else if (ret > rdev->constraints->max_uV)
1445 ret = 0;
1446 }
1447
1448 return ret;
1449 }
1450 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1451
1452 /**
1453 * regulator_is_supported_voltage - check if a voltage range can be supported
1454 *
1455 * @regulator: Regulator to check.
1456 * @min_uV: Minimum required voltage in uV.
1457 * @max_uV: Maximum required voltage in uV.
1458 *
1459 * Returns a boolean or a negative error code.
1460 */
1461 int regulator_is_supported_voltage(struct regulator *regulator,
1462 int min_uV, int max_uV)
1463 {
1464 int i, voltages, ret;
1465
1466 ret = regulator_count_voltages(regulator);
1467 if (ret < 0)
1468 return ret;
1469 voltages = ret;
1470
1471 for (i = 0; i < voltages; i++) {
1472 ret = regulator_list_voltage(regulator, i);
1473
1474 if (ret >= min_uV && ret <= max_uV)
1475 return 1;
1476 }
1477
1478 return 0;
1479 }
1480
1481 /**
1482 * regulator_set_voltage - set regulator output voltage
1483 * @regulator: regulator source
1484 * @min_uV: Minimum required voltage in uV
1485 * @max_uV: Maximum acceptable voltage in uV
1486 *
1487 * Sets a voltage regulator to the desired output voltage. This can be set
1488 * during any regulator state. IOW, regulator can be disabled or enabled.
1489 *
1490 * If the regulator is enabled then the voltage will change to the new value
1491 * immediately otherwise if the regulator is disabled the regulator will
1492 * output at the new voltage when enabled.
1493 *
1494 * NOTE: If the regulator is shared between several devices then the lowest
1495 * request voltage that meets the system constraints will be used.
1496 * Regulator system constraints must be set for this regulator before
1497 * calling this function otherwise this call will fail.
1498 */
1499 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1500 {
1501 struct regulator_dev *rdev = regulator->rdev;
1502 int ret;
1503
1504 mutex_lock(&rdev->mutex);
1505
1506 /* sanity check */
1507 if (!rdev->desc->ops->set_voltage) {
1508 ret = -EINVAL;
1509 goto out;
1510 }
1511
1512 /* constraints check */
1513 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1514 if (ret < 0)
1515 goto out;
1516 regulator->min_uV = min_uV;
1517 regulator->max_uV = max_uV;
1518 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1519
1520 out:
1521 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1522 mutex_unlock(&rdev->mutex);
1523 return ret;
1524 }
1525 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1526
1527 static int _regulator_get_voltage(struct regulator_dev *rdev)
1528 {
1529 /* sanity check */
1530 if (rdev->desc->ops->get_voltage)
1531 return rdev->desc->ops->get_voltage(rdev);
1532 else
1533 return -EINVAL;
1534 }
1535
1536 /**
1537 * regulator_get_voltage - get regulator output voltage
1538 * @regulator: regulator source
1539 *
1540 * This returns the current regulator voltage in uV.
1541 *
1542 * NOTE: If the regulator is disabled it will return the voltage value. This
1543 * function should not be used to determine regulator state.
1544 */
1545 int regulator_get_voltage(struct regulator *regulator)
1546 {
1547 int ret;
1548
1549 mutex_lock(&regulator->rdev->mutex);
1550
1551 ret = _regulator_get_voltage(regulator->rdev);
1552
1553 mutex_unlock(&regulator->rdev->mutex);
1554
1555 return ret;
1556 }
1557 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1558
1559 /**
1560 * regulator_set_current_limit - set regulator output current limit
1561 * @regulator: regulator source
1562 * @min_uA: Minimuum supported current in uA
1563 * @max_uA: Maximum supported current in uA
1564 *
1565 * Sets current sink to the desired output current. This can be set during
1566 * any regulator state. IOW, regulator can be disabled or enabled.
1567 *
1568 * If the regulator is enabled then the current will change to the new value
1569 * immediately otherwise if the regulator is disabled the regulator will
1570 * output at the new current when enabled.
1571 *
1572 * NOTE: Regulator system constraints must be set for this regulator before
1573 * calling this function otherwise this call will fail.
1574 */
1575 int regulator_set_current_limit(struct regulator *regulator,
1576 int min_uA, int max_uA)
1577 {
1578 struct regulator_dev *rdev = regulator->rdev;
1579 int ret;
1580
1581 mutex_lock(&rdev->mutex);
1582
1583 /* sanity check */
1584 if (!rdev->desc->ops->set_current_limit) {
1585 ret = -EINVAL;
1586 goto out;
1587 }
1588
1589 /* constraints check */
1590 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1591 if (ret < 0)
1592 goto out;
1593
1594 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1595 out:
1596 mutex_unlock(&rdev->mutex);
1597 return ret;
1598 }
1599 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1600
1601 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1602 {
1603 int ret;
1604
1605 mutex_lock(&rdev->mutex);
1606
1607 /* sanity check */
1608 if (!rdev->desc->ops->get_current_limit) {
1609 ret = -EINVAL;
1610 goto out;
1611 }
1612
1613 ret = rdev->desc->ops->get_current_limit(rdev);
1614 out:
1615 mutex_unlock(&rdev->mutex);
1616 return ret;
1617 }
1618
1619 /**
1620 * regulator_get_current_limit - get regulator output current
1621 * @regulator: regulator source
1622 *
1623 * This returns the current supplied by the specified current sink in uA.
1624 *
1625 * NOTE: If the regulator is disabled it will return the current value. This
1626 * function should not be used to determine regulator state.
1627 */
1628 int regulator_get_current_limit(struct regulator *regulator)
1629 {
1630 return _regulator_get_current_limit(regulator->rdev);
1631 }
1632 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1633
1634 /**
1635 * regulator_set_mode - set regulator operating mode
1636 * @regulator: regulator source
1637 * @mode: operating mode - one of the REGULATOR_MODE constants
1638 *
1639 * Set regulator operating mode to increase regulator efficiency or improve
1640 * regulation performance.
1641 *
1642 * NOTE: Regulator system constraints must be set for this regulator before
1643 * calling this function otherwise this call will fail.
1644 */
1645 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1646 {
1647 struct regulator_dev *rdev = regulator->rdev;
1648 int ret;
1649
1650 mutex_lock(&rdev->mutex);
1651
1652 /* sanity check */
1653 if (!rdev->desc->ops->set_mode) {
1654 ret = -EINVAL;
1655 goto out;
1656 }
1657
1658 /* constraints check */
1659 ret = regulator_check_mode(rdev, mode);
1660 if (ret < 0)
1661 goto out;
1662
1663 ret = rdev->desc->ops->set_mode(rdev, mode);
1664 out:
1665 mutex_unlock(&rdev->mutex);
1666 return ret;
1667 }
1668 EXPORT_SYMBOL_GPL(regulator_set_mode);
1669
1670 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1671 {
1672 int ret;
1673
1674 mutex_lock(&rdev->mutex);
1675
1676 /* sanity check */
1677 if (!rdev->desc->ops->get_mode) {
1678 ret = -EINVAL;
1679 goto out;
1680 }
1681
1682 ret = rdev->desc->ops->get_mode(rdev);
1683 out:
1684 mutex_unlock(&rdev->mutex);
1685 return ret;
1686 }
1687
1688 /**
1689 * regulator_get_mode - get regulator operating mode
1690 * @regulator: regulator source
1691 *
1692 * Get the current regulator operating mode.
1693 */
1694 unsigned int regulator_get_mode(struct regulator *regulator)
1695 {
1696 return _regulator_get_mode(regulator->rdev);
1697 }
1698 EXPORT_SYMBOL_GPL(regulator_get_mode);
1699
1700 /**
1701 * regulator_set_optimum_mode - set regulator optimum operating mode
1702 * @regulator: regulator source
1703 * @uA_load: load current
1704 *
1705 * Notifies the regulator core of a new device load. This is then used by
1706 * DRMS (if enabled by constraints) to set the most efficient regulator
1707 * operating mode for the new regulator loading.
1708 *
1709 * Consumer devices notify their supply regulator of the maximum power
1710 * they will require (can be taken from device datasheet in the power
1711 * consumption tables) when they change operational status and hence power
1712 * state. Examples of operational state changes that can affect power
1713 * consumption are :-
1714 *
1715 * o Device is opened / closed.
1716 * o Device I/O is about to begin or has just finished.
1717 * o Device is idling in between work.
1718 *
1719 * This information is also exported via sysfs to userspace.
1720 *
1721 * DRMS will sum the total requested load on the regulator and change
1722 * to the most efficient operating mode if platform constraints allow.
1723 *
1724 * Returns the new regulator mode or error.
1725 */
1726 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1727 {
1728 struct regulator_dev *rdev = regulator->rdev;
1729 struct regulator *consumer;
1730 int ret, output_uV, input_uV, total_uA_load = 0;
1731 unsigned int mode;
1732
1733 mutex_lock(&rdev->mutex);
1734
1735 regulator->uA_load = uA_load;
1736 ret = regulator_check_drms(rdev);
1737 if (ret < 0)
1738 goto out;
1739 ret = -EINVAL;
1740
1741 /* sanity check */
1742 if (!rdev->desc->ops->get_optimum_mode)
1743 goto out;
1744
1745 /* get output voltage */
1746 output_uV = rdev->desc->ops->get_voltage(rdev);
1747 if (output_uV <= 0) {
1748 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1749 __func__, rdev->desc->name);
1750 goto out;
1751 }
1752
1753 /* get input voltage */
1754 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1755 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1756 else
1757 input_uV = rdev->constraints->input_uV;
1758 if (input_uV <= 0) {
1759 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1760 __func__, rdev->desc->name);
1761 goto out;
1762 }
1763
1764 /* calc total requested load for this regulator */
1765 list_for_each_entry(consumer, &rdev->consumer_list, list)
1766 total_uA_load += consumer->uA_load;
1767
1768 mode = rdev->desc->ops->get_optimum_mode(rdev,
1769 input_uV, output_uV,
1770 total_uA_load);
1771 ret = regulator_check_mode(rdev, mode);
1772 if (ret < 0) {
1773 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1774 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1775 total_uA_load, input_uV, output_uV);
1776 goto out;
1777 }
1778
1779 ret = rdev->desc->ops->set_mode(rdev, mode);
1780 if (ret < 0) {
1781 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1782 __func__, mode, rdev->desc->name);
1783 goto out;
1784 }
1785 ret = mode;
1786 out:
1787 mutex_unlock(&rdev->mutex);
1788 return ret;
1789 }
1790 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1791
1792 /**
1793 * regulator_register_notifier - register regulator event notifier
1794 * @regulator: regulator source
1795 * @nb: notifier block
1796 *
1797 * Register notifier block to receive regulator events.
1798 */
1799 int regulator_register_notifier(struct regulator *regulator,
1800 struct notifier_block *nb)
1801 {
1802 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1803 nb);
1804 }
1805 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1806
1807 /**
1808 * regulator_unregister_notifier - unregister regulator event notifier
1809 * @regulator: regulator source
1810 * @nb: notifier block
1811 *
1812 * Unregister regulator event notifier block.
1813 */
1814 int regulator_unregister_notifier(struct regulator *regulator,
1815 struct notifier_block *nb)
1816 {
1817 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1818 nb);
1819 }
1820 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1821
1822 /* notify regulator consumers and downstream regulator consumers.
1823 * Note mutex must be held by caller.
1824 */
1825 static void _notifier_call_chain(struct regulator_dev *rdev,
1826 unsigned long event, void *data)
1827 {
1828 struct regulator_dev *_rdev;
1829
1830 /* call rdev chain first */
1831 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1832
1833 /* now notify regulator we supply */
1834 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1835 mutex_lock(&_rdev->mutex);
1836 _notifier_call_chain(_rdev, event, data);
1837 mutex_unlock(&_rdev->mutex);
1838 }
1839 }
1840
1841 /**
1842 * regulator_bulk_get - get multiple regulator consumers
1843 *
1844 * @dev: Device to supply
1845 * @num_consumers: Number of consumers to register
1846 * @consumers: Configuration of consumers; clients are stored here.
1847 *
1848 * @return 0 on success, an errno on failure.
1849 *
1850 * This helper function allows drivers to get several regulator
1851 * consumers in one operation. If any of the regulators cannot be
1852 * acquired then any regulators that were allocated will be freed
1853 * before returning to the caller.
1854 */
1855 int regulator_bulk_get(struct device *dev, int num_consumers,
1856 struct regulator_bulk_data *consumers)
1857 {
1858 int i;
1859 int ret;
1860
1861 for (i = 0; i < num_consumers; i++)
1862 consumers[i].consumer = NULL;
1863
1864 for (i = 0; i < num_consumers; i++) {
1865 consumers[i].consumer = regulator_get(dev,
1866 consumers[i].supply);
1867 if (IS_ERR(consumers[i].consumer)) {
1868 dev_err(dev, "Failed to get supply '%s'\n",
1869 consumers[i].supply);
1870 ret = PTR_ERR(consumers[i].consumer);
1871 consumers[i].consumer = NULL;
1872 goto err;
1873 }
1874 }
1875
1876 return 0;
1877
1878 err:
1879 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1880 regulator_put(consumers[i].consumer);
1881
1882 return ret;
1883 }
1884 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1885
1886 /**
1887 * regulator_bulk_enable - enable multiple regulator consumers
1888 *
1889 * @num_consumers: Number of consumers
1890 * @consumers: Consumer data; clients are stored here.
1891 * @return 0 on success, an errno on failure
1892 *
1893 * This convenience API allows consumers to enable multiple regulator
1894 * clients in a single API call. If any consumers cannot be enabled
1895 * then any others that were enabled will be disabled again prior to
1896 * return.
1897 */
1898 int regulator_bulk_enable(int num_consumers,
1899 struct regulator_bulk_data *consumers)
1900 {
1901 int i;
1902 int ret;
1903
1904 for (i = 0; i < num_consumers; i++) {
1905 ret = regulator_enable(consumers[i].consumer);
1906 if (ret != 0)
1907 goto err;
1908 }
1909
1910 return 0;
1911
1912 err:
1913 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1914 for (i = 0; i < num_consumers; i++)
1915 regulator_disable(consumers[i].consumer);
1916
1917 return ret;
1918 }
1919 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1920
1921 /**
1922 * regulator_bulk_disable - disable multiple regulator consumers
1923 *
1924 * @num_consumers: Number of consumers
1925 * @consumers: Consumer data; clients are stored here.
1926 * @return 0 on success, an errno on failure
1927 *
1928 * This convenience API allows consumers to disable multiple regulator
1929 * clients in a single API call. If any consumers cannot be enabled
1930 * then any others that were disabled will be disabled again prior to
1931 * return.
1932 */
1933 int regulator_bulk_disable(int num_consumers,
1934 struct regulator_bulk_data *consumers)
1935 {
1936 int i;
1937 int ret;
1938
1939 for (i = 0; i < num_consumers; i++) {
1940 ret = regulator_disable(consumers[i].consumer);
1941 if (ret != 0)
1942 goto err;
1943 }
1944
1945 return 0;
1946
1947 err:
1948 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1949 for (i = 0; i < num_consumers; i++)
1950 regulator_enable(consumers[i].consumer);
1951
1952 return ret;
1953 }
1954 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1955
1956 /**
1957 * regulator_bulk_free - free multiple regulator consumers
1958 *
1959 * @num_consumers: Number of consumers
1960 * @consumers: Consumer data; clients are stored here.
1961 *
1962 * This convenience API allows consumers to free multiple regulator
1963 * clients in a single API call.
1964 */
1965 void regulator_bulk_free(int num_consumers,
1966 struct regulator_bulk_data *consumers)
1967 {
1968 int i;
1969
1970 for (i = 0; i < num_consumers; i++) {
1971 regulator_put(consumers[i].consumer);
1972 consumers[i].consumer = NULL;
1973 }
1974 }
1975 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1976
1977 /**
1978 * regulator_notifier_call_chain - call regulator event notifier
1979 * @rdev: regulator source
1980 * @event: notifier block
1981 * @data: callback-specific data.
1982 *
1983 * Called by regulator drivers to notify clients a regulator event has
1984 * occurred. We also notify regulator clients downstream.
1985 * Note lock must be held by caller.
1986 */
1987 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1988 unsigned long event, void *data)
1989 {
1990 _notifier_call_chain(rdev, event, data);
1991 return NOTIFY_DONE;
1992
1993 }
1994 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1995
1996 /**
1997 * regulator_mode_to_status - convert a regulator mode into a status
1998 *
1999 * @mode: Mode to convert
2000 *
2001 * Convert a regulator mode into a status.
2002 */
2003 int regulator_mode_to_status(unsigned int mode)
2004 {
2005 switch (mode) {
2006 case REGULATOR_MODE_FAST:
2007 return REGULATOR_STATUS_FAST;
2008 case REGULATOR_MODE_NORMAL:
2009 return REGULATOR_STATUS_NORMAL;
2010 case REGULATOR_MODE_IDLE:
2011 return REGULATOR_STATUS_IDLE;
2012 case REGULATOR_STATUS_STANDBY:
2013 return REGULATOR_STATUS_STANDBY;
2014 default:
2015 return 0;
2016 }
2017 }
2018 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2019
2020 /*
2021 * To avoid cluttering sysfs (and memory) with useless state, only
2022 * create attributes that can be meaningfully displayed.
2023 */
2024 static int add_regulator_attributes(struct regulator_dev *rdev)
2025 {
2026 struct device *dev = &rdev->dev;
2027 struct regulator_ops *ops = rdev->desc->ops;
2028 int status = 0;
2029
2030 /* some attributes need specific methods to be displayed */
2031 if (ops->get_voltage) {
2032 status = device_create_file(dev, &dev_attr_microvolts);
2033 if (status < 0)
2034 return status;
2035 }
2036 if (ops->get_current_limit) {
2037 status = device_create_file(dev, &dev_attr_microamps);
2038 if (status < 0)
2039 return status;
2040 }
2041 if (ops->get_mode) {
2042 status = device_create_file(dev, &dev_attr_opmode);
2043 if (status < 0)
2044 return status;
2045 }
2046 if (ops->is_enabled) {
2047 status = device_create_file(dev, &dev_attr_state);
2048 if (status < 0)
2049 return status;
2050 }
2051 if (ops->get_status) {
2052 status = device_create_file(dev, &dev_attr_status);
2053 if (status < 0)
2054 return status;
2055 }
2056
2057 /* some attributes are type-specific */
2058 if (rdev->desc->type == REGULATOR_CURRENT) {
2059 status = device_create_file(dev, &dev_attr_requested_microamps);
2060 if (status < 0)
2061 return status;
2062 }
2063
2064 /* all the other attributes exist to support constraints;
2065 * don't show them if there are no constraints, or if the
2066 * relevant supporting methods are missing.
2067 */
2068 if (!rdev->constraints)
2069 return status;
2070
2071 /* constraints need specific supporting methods */
2072 if (ops->set_voltage) {
2073 status = device_create_file(dev, &dev_attr_min_microvolts);
2074 if (status < 0)
2075 return status;
2076 status = device_create_file(dev, &dev_attr_max_microvolts);
2077 if (status < 0)
2078 return status;
2079 }
2080 if (ops->set_current_limit) {
2081 status = device_create_file(dev, &dev_attr_min_microamps);
2082 if (status < 0)
2083 return status;
2084 status = device_create_file(dev, &dev_attr_max_microamps);
2085 if (status < 0)
2086 return status;
2087 }
2088
2089 /* suspend mode constraints need multiple supporting methods */
2090 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2091 return status;
2092
2093 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2094 if (status < 0)
2095 return status;
2096 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2097 if (status < 0)
2098 return status;
2099 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2100 if (status < 0)
2101 return status;
2102
2103 if (ops->set_suspend_voltage) {
2104 status = device_create_file(dev,
2105 &dev_attr_suspend_standby_microvolts);
2106 if (status < 0)
2107 return status;
2108 status = device_create_file(dev,
2109 &dev_attr_suspend_mem_microvolts);
2110 if (status < 0)
2111 return status;
2112 status = device_create_file(dev,
2113 &dev_attr_suspend_disk_microvolts);
2114 if (status < 0)
2115 return status;
2116 }
2117
2118 if (ops->set_suspend_mode) {
2119 status = device_create_file(dev,
2120 &dev_attr_suspend_standby_mode);
2121 if (status < 0)
2122 return status;
2123 status = device_create_file(dev,
2124 &dev_attr_suspend_mem_mode);
2125 if (status < 0)
2126 return status;
2127 status = device_create_file(dev,
2128 &dev_attr_suspend_disk_mode);
2129 if (status < 0)
2130 return status;
2131 }
2132
2133 return status;
2134 }
2135
2136 /**
2137 * regulator_register - register regulator
2138 * @regulator_desc: regulator to register
2139 * @dev: struct device for the regulator
2140 * @init_data: platform provided init data, passed through by driver
2141 * @driver_data: private regulator data
2142 *
2143 * Called by regulator drivers to register a regulator.
2144 * Returns 0 on success.
2145 */
2146 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2147 struct device *dev, struct regulator_init_data *init_data,
2148 void *driver_data)
2149 {
2150 static atomic_t regulator_no = ATOMIC_INIT(0);
2151 struct regulator_dev *rdev;
2152 int ret, i;
2153
2154 if (regulator_desc == NULL)
2155 return ERR_PTR(-EINVAL);
2156
2157 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2158 return ERR_PTR(-EINVAL);
2159
2160 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2161 regulator_desc->type != REGULATOR_CURRENT)
2162 return ERR_PTR(-EINVAL);
2163
2164 if (!init_data)
2165 return ERR_PTR(-EINVAL);
2166
2167 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2168 if (rdev == NULL)
2169 return ERR_PTR(-ENOMEM);
2170
2171 mutex_lock(&regulator_list_mutex);
2172
2173 mutex_init(&rdev->mutex);
2174 rdev->reg_data = driver_data;
2175 rdev->owner = regulator_desc->owner;
2176 rdev->desc = regulator_desc;
2177 INIT_LIST_HEAD(&rdev->consumer_list);
2178 INIT_LIST_HEAD(&rdev->supply_list);
2179 INIT_LIST_HEAD(&rdev->list);
2180 INIT_LIST_HEAD(&rdev->slist);
2181 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2182
2183 /* preform any regulator specific init */
2184 if (init_data->regulator_init) {
2185 ret = init_data->regulator_init(rdev->reg_data);
2186 if (ret < 0)
2187 goto clean;
2188 }
2189
2190 /* register with sysfs */
2191 rdev->dev.class = &regulator_class;
2192 rdev->dev.parent = dev;
2193 dev_set_name(&rdev->dev, "regulator.%d",
2194 atomic_inc_return(&regulator_no) - 1);
2195 ret = device_register(&rdev->dev);
2196 if (ret != 0)
2197 goto clean;
2198
2199 dev_set_drvdata(&rdev->dev, rdev);
2200
2201 /* set regulator constraints */
2202 ret = set_machine_constraints(rdev, &init_data->constraints);
2203 if (ret < 0)
2204 goto scrub;
2205
2206 /* add attributes supported by this regulator */
2207 ret = add_regulator_attributes(rdev);
2208 if (ret < 0)
2209 goto scrub;
2210
2211 /* set supply regulator if it exists */
2212 if (init_data->supply_regulator_dev) {
2213 ret = set_supply(rdev,
2214 dev_get_drvdata(init_data->supply_regulator_dev));
2215 if (ret < 0)
2216 goto scrub;
2217 }
2218
2219 /* add consumers devices */
2220 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2221 ret = set_consumer_device_supply(rdev,
2222 init_data->consumer_supplies[i].dev,
2223 init_data->consumer_supplies[i].dev_name,
2224 init_data->consumer_supplies[i].supply);
2225 if (ret < 0) {
2226 for (--i; i >= 0; i--)
2227 unset_consumer_device_supply(rdev,
2228 init_data->consumer_supplies[i].dev_name,
2229 init_data->consumer_supplies[i].dev);
2230 goto scrub;
2231 }
2232 }
2233
2234 list_add(&rdev->list, &regulator_list);
2235 out:
2236 mutex_unlock(&regulator_list_mutex);
2237 return rdev;
2238
2239 scrub:
2240 device_unregister(&rdev->dev);
2241 /* device core frees rdev */
2242 rdev = ERR_PTR(ret);
2243 goto out;
2244
2245 clean:
2246 kfree(rdev);
2247 rdev = ERR_PTR(ret);
2248 goto out;
2249 }
2250 EXPORT_SYMBOL_GPL(regulator_register);
2251
2252 /**
2253 * regulator_unregister - unregister regulator
2254 * @rdev: regulator to unregister
2255 *
2256 * Called by regulator drivers to unregister a regulator.
2257 */
2258 void regulator_unregister(struct regulator_dev *rdev)
2259 {
2260 if (rdev == NULL)
2261 return;
2262
2263 mutex_lock(&regulator_list_mutex);
2264 WARN_ON(rdev->open_count);
2265 unset_regulator_supplies(rdev);
2266 list_del(&rdev->list);
2267 if (rdev->supply)
2268 sysfs_remove_link(&rdev->dev.kobj, "supply");
2269 device_unregister(&rdev->dev);
2270 mutex_unlock(&regulator_list_mutex);
2271 }
2272 EXPORT_SYMBOL_GPL(regulator_unregister);
2273
2274 /**
2275 * regulator_suspend_prepare - prepare regulators for system wide suspend
2276 * @state: system suspend state
2277 *
2278 * Configure each regulator with it's suspend operating parameters for state.
2279 * This will usually be called by machine suspend code prior to supending.
2280 */
2281 int regulator_suspend_prepare(suspend_state_t state)
2282 {
2283 struct regulator_dev *rdev;
2284 int ret = 0;
2285
2286 /* ON is handled by regulator active state */
2287 if (state == PM_SUSPEND_ON)
2288 return -EINVAL;
2289
2290 mutex_lock(&regulator_list_mutex);
2291 list_for_each_entry(rdev, &regulator_list, list) {
2292
2293 mutex_lock(&rdev->mutex);
2294 ret = suspend_prepare(rdev, state);
2295 mutex_unlock(&rdev->mutex);
2296
2297 if (ret < 0) {
2298 printk(KERN_ERR "%s: failed to prepare %s\n",
2299 __func__, rdev->desc->name);
2300 goto out;
2301 }
2302 }
2303 out:
2304 mutex_unlock(&regulator_list_mutex);
2305 return ret;
2306 }
2307 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2308
2309 /**
2310 * regulator_has_full_constraints - the system has fully specified constraints
2311 *
2312 * Calling this function will cause the regulator API to disable all
2313 * regulators which have a zero use count and don't have an always_on
2314 * constraint in a late_initcall.
2315 *
2316 * The intention is that this will become the default behaviour in a
2317 * future kernel release so users are encouraged to use this facility
2318 * now.
2319 */
2320 void regulator_has_full_constraints(void)
2321 {
2322 has_full_constraints = 1;
2323 }
2324 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2325
2326 /**
2327 * rdev_get_drvdata - get rdev regulator driver data
2328 * @rdev: regulator
2329 *
2330 * Get rdev regulator driver private data. This call can be used in the
2331 * regulator driver context.
2332 */
2333 void *rdev_get_drvdata(struct regulator_dev *rdev)
2334 {
2335 return rdev->reg_data;
2336 }
2337 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2338
2339 /**
2340 * regulator_get_drvdata - get regulator driver data
2341 * @regulator: regulator
2342 *
2343 * Get regulator driver private data. This call can be used in the consumer
2344 * driver context when non API regulator specific functions need to be called.
2345 */
2346 void *regulator_get_drvdata(struct regulator *regulator)
2347 {
2348 return regulator->rdev->reg_data;
2349 }
2350 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2351
2352 /**
2353 * regulator_set_drvdata - set regulator driver data
2354 * @regulator: regulator
2355 * @data: data
2356 */
2357 void regulator_set_drvdata(struct regulator *regulator, void *data)
2358 {
2359 regulator->rdev->reg_data = data;
2360 }
2361 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2362
2363 /**
2364 * regulator_get_id - get regulator ID
2365 * @rdev: regulator
2366 */
2367 int rdev_get_id(struct regulator_dev *rdev)
2368 {
2369 return rdev->desc->id;
2370 }
2371 EXPORT_SYMBOL_GPL(rdev_get_id);
2372
2373 struct device *rdev_get_dev(struct regulator_dev *rdev)
2374 {
2375 return &rdev->dev;
2376 }
2377 EXPORT_SYMBOL_GPL(rdev_get_dev);
2378
2379 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2380 {
2381 return reg_init_data->driver_data;
2382 }
2383 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2384
2385 static int __init regulator_init(void)
2386 {
2387 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2388 return class_register(&regulator_class);
2389 }
2390
2391 /* init early to allow our consumers to complete system booting */
2392 core_initcall(regulator_init);
2393
2394 static int __init regulator_init_complete(void)
2395 {
2396 struct regulator_dev *rdev;
2397 struct regulator_ops *ops;
2398 struct regulation_constraints *c;
2399 int enabled, ret;
2400 const char *name;
2401
2402 mutex_lock(&regulator_list_mutex);
2403
2404 /* If we have a full configuration then disable any regulators
2405 * which are not in use or always_on. This will become the
2406 * default behaviour in the future.
2407 */
2408 list_for_each_entry(rdev, &regulator_list, list) {
2409 ops = rdev->desc->ops;
2410 c = rdev->constraints;
2411
2412 if (c && c->name)
2413 name = c->name;
2414 else if (rdev->desc->name)
2415 name = rdev->desc->name;
2416 else
2417 name = "regulator";
2418
2419 if (!ops->disable || (c && c->always_on))
2420 continue;
2421
2422 mutex_lock(&rdev->mutex);
2423
2424 if (rdev->use_count)
2425 goto unlock;
2426
2427 /* If we can't read the status assume it's on. */
2428 if (ops->is_enabled)
2429 enabled = ops->is_enabled(rdev);
2430 else
2431 enabled = 1;
2432
2433 if (!enabled)
2434 goto unlock;
2435
2436 if (has_full_constraints) {
2437 /* We log since this may kill the system if it
2438 * goes wrong. */
2439 printk(KERN_INFO "%s: disabling %s\n",
2440 __func__, name);
2441 ret = ops->disable(rdev);
2442 if (ret != 0) {
2443 printk(KERN_ERR
2444 "%s: couldn't disable %s: %d\n",
2445 __func__, name, ret);
2446 }
2447 } else {
2448 /* The intention is that in future we will
2449 * assume that full constraints are provided
2450 * so warn even if we aren't going to do
2451 * anything here.
2452 */
2453 printk(KERN_WARNING
2454 "%s: incomplete constraints, leaving %s on\n",
2455 __func__, name);
2456 }
2457
2458 unlock:
2459 mutex_unlock(&rdev->mutex);
2460 }
2461
2462 mutex_unlock(&regulator_list_mutex);
2463
2464 return 0;
2465 }
2466 late_initcall(regulator_init_complete);