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