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