blob: 98c3a74e994943f1041818896ae23961781ed041 [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
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);
Mark Brownca725562009-03-16 19:36:34 +000031static int has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010032
Mark Brown8dc53902008-12-31 12:52:40 +000033/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010034 * struct regulator_map
35 *
36 * Used to provide symbolic supply names to devices.
37 */
38struct regulator_map {
39 struct list_head list;
40 struct device *dev;
41 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010042 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010043};
44
Liam Girdwood414c70c2008-04-30 15:59:04 +010045/*
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;
Liam Girdwood414c70c2008-04-30 15:59:04 +010056 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{
David Brownelle5735202008-11-16 11:46:56 -0800150 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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100160 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{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100209 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100210 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}
David Brownell7ad68e22008-11-11 17:39:02 -0800218static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100219
220static ssize_t regulator_uA_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
222{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100223 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100224
225 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
226}
David Brownell7ad68e22008-11-11 17:39:02 -0800227static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100228
Mark Brownbc558a62008-10-10 15:33:20 +0100229static ssize_t regulator_name_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
231{
232 struct regulator_dev *rdev = dev_get_drvdata(dev);
233 const char *name;
234
235 if (rdev->constraints->name)
236 name = rdev->constraints->name;
237 else if (rdev->desc->name)
238 name = rdev->desc->name;
239 else
240 name = "";
241
242 return sprintf(buf, "%s\n", name);
243}
244
David Brownell4fca9542008-11-11 17:38:53 -0800245static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100246{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100247 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
David Brownell4fca9542008-11-11 17:38:53 -0800260static ssize_t regulator_opmode_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100262{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100263 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100264
David Brownell4fca9542008-11-11 17:38:53 -0800265 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
266}
David Brownell7ad68e22008-11-11 17:39:02 -0800267static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800268
269static ssize_t regulator_print_state(char *buf, int state)
270{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100271 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
David Brownell4fca9542008-11-11 17:38:53 -0800279static ssize_t regulator_state_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
281{
282 struct regulator_dev *rdev = dev_get_drvdata(dev);
283
284 return regulator_print_state(buf, _regulator_is_enabled(rdev));
285}
David Brownell7ad68e22008-11-11 17:39:02 -0800286static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800287
David Brownell853116a2009-01-14 23:03:17 -0800288static ssize_t regulator_status_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
290{
291 struct regulator_dev *rdev = dev_get_drvdata(dev);
292 int status;
293 char *label;
294
295 status = rdev->desc->ops->get_status(rdev);
296 if (status < 0)
297 return status;
298
299 switch (status) {
300 case REGULATOR_STATUS_OFF:
301 label = "off";
302 break;
303 case REGULATOR_STATUS_ON:
304 label = "on";
305 break;
306 case REGULATOR_STATUS_ERROR:
307 label = "error";
308 break;
309 case REGULATOR_STATUS_FAST:
310 label = "fast";
311 break;
312 case REGULATOR_STATUS_NORMAL:
313 label = "normal";
314 break;
315 case REGULATOR_STATUS_IDLE:
316 label = "idle";
317 break;
318 case REGULATOR_STATUS_STANDBY:
319 label = "standby";
320 break;
321 default:
322 return -ERANGE;
323 }
324
325 return sprintf(buf, "%s\n", label);
326}
327static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
328
Liam Girdwood414c70c2008-04-30 15:59:04 +0100329static ssize_t regulator_min_uA_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
331{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100332 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100333
334 if (!rdev->constraints)
335 return sprintf(buf, "constraint not defined\n");
336
337 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
338}
David Brownell7ad68e22008-11-11 17:39:02 -0800339static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100340
341static ssize_t regulator_max_uA_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100344 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345
346 if (!rdev->constraints)
347 return sprintf(buf, "constraint not defined\n");
348
349 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
350}
David Brownell7ad68e22008-11-11 17:39:02 -0800351static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100352
353static ssize_t regulator_min_uV_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
355{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100356 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100357
358 if (!rdev->constraints)
359 return sprintf(buf, "constraint not defined\n");
360
361 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
362}
David Brownell7ad68e22008-11-11 17:39:02 -0800363static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100364
365static ssize_t regulator_max_uV_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
367{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100368 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100369
370 if (!rdev->constraints)
371 return sprintf(buf, "constraint not defined\n");
372
373 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
374}
David Brownell7ad68e22008-11-11 17:39:02 -0800375static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100376
377static ssize_t regulator_total_uA_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
379{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100380 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100381 struct regulator *regulator;
382 int uA = 0;
383
384 mutex_lock(&rdev->mutex);
385 list_for_each_entry(regulator, &rdev->consumer_list, list)
386 uA += regulator->uA_load;
387 mutex_unlock(&rdev->mutex);
388 return sprintf(buf, "%d\n", uA);
389}
David Brownell7ad68e22008-11-11 17:39:02 -0800390static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100391
392static ssize_t regulator_num_users_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
394{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100395 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100396 return sprintf(buf, "%d\n", rdev->use_count);
397}
398
399static ssize_t regulator_type_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
401{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100402 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100403
404 switch (rdev->desc->type) {
405 case REGULATOR_VOLTAGE:
406 return sprintf(buf, "voltage\n");
407 case REGULATOR_CURRENT:
408 return sprintf(buf, "current\n");
409 }
410 return sprintf(buf, "unknown\n");
411}
412
413static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100416 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100417
Liam Girdwood414c70c2008-04-30 15:59:04 +0100418 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
419}
David Brownell7ad68e22008-11-11 17:39:02 -0800420static DEVICE_ATTR(suspend_mem_microvolts, 0444,
421 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100422
423static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
425{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100426 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100427
Liam Girdwood414c70c2008-04-30 15:59:04 +0100428 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
429}
David Brownell7ad68e22008-11-11 17:39:02 -0800430static DEVICE_ATTR(suspend_disk_microvolts, 0444,
431 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100432
433static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
434 struct device_attribute *attr, char *buf)
435{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100436 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100437
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
439}
David Brownell7ad68e22008-11-11 17:39:02 -0800440static DEVICE_ATTR(suspend_standby_microvolts, 0444,
441 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100446 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447
David Brownell4fca9542008-11-11 17:38:53 -0800448 return regulator_print_opmode(buf,
449 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450}
David Brownell7ad68e22008-11-11 17:39:02 -0800451static DEVICE_ATTR(suspend_mem_mode, 0444,
452 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453
454static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
456{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100457 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458
David Brownell4fca9542008-11-11 17:38:53 -0800459 return regulator_print_opmode(buf,
460 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461}
David Brownell7ad68e22008-11-11 17:39:02 -0800462static DEVICE_ATTR(suspend_disk_mode, 0444,
463 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100464
465static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
467{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100468 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469
David Brownell4fca9542008-11-11 17:38:53 -0800470 return regulator_print_opmode(buf,
471 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472}
David Brownell7ad68e22008-11-11 17:39:02 -0800473static DEVICE_ATTR(suspend_standby_mode, 0444,
474 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100475
476static ssize_t regulator_suspend_mem_state_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
478{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100479 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480
David Brownell4fca9542008-11-11 17:38:53 -0800481 return regulator_print_state(buf,
482 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483}
David Brownell7ad68e22008-11-11 17:39:02 -0800484static DEVICE_ATTR(suspend_mem_state, 0444,
485 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100486
487static ssize_t regulator_suspend_disk_state_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
489{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100490 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491
David Brownell4fca9542008-11-11 17:38:53 -0800492 return regulator_print_state(buf,
493 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100494}
David Brownell7ad68e22008-11-11 17:39:02 -0800495static DEVICE_ATTR(suspend_disk_state, 0444,
496 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
498static ssize_t regulator_suspend_standby_state_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100501 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
David Brownell4fca9542008-11-11 17:38:53 -0800503 return regulator_print_state(buf,
504 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505}
David Brownell7ad68e22008-11-11 17:39:02 -0800506static DEVICE_ATTR(suspend_standby_state, 0444,
507 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100508
David Brownell7ad68e22008-11-11 17:39:02 -0800509
510/*
511 * These are the only attributes are present for all regulators.
512 * Other attributes are a function of regulator functionality.
513 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100515 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100516 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
517 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518 __ATTR_NULL,
519};
520
521static void regulator_dev_release(struct device *dev)
522{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100523 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524 kfree(rdev);
525}
526
527static struct class regulator_class = {
528 .name = "regulator",
529 .dev_release = regulator_dev_release,
530 .dev_attrs = regulator_dev_attrs,
531};
532
533/* Calculate the new optimum regulator operating mode based on the new total
534 * consumer load. All locks held by caller */
535static void drms_uA_update(struct regulator_dev *rdev)
536{
537 struct regulator *sibling;
538 int current_uA = 0, output_uV, input_uV, err;
539 unsigned int mode;
540
541 err = regulator_check_drms(rdev);
542 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300543 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
544 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545
546 /* get output voltage */
547 output_uV = rdev->desc->ops->get_voltage(rdev);
548 if (output_uV <= 0)
549 return;
550
551 /* get input voltage */
552 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
553 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
554 else
555 input_uV = rdev->constraints->input_uV;
556 if (input_uV <= 0)
557 return;
558
559 /* calc total requested load */
560 list_for_each_entry(sibling, &rdev->consumer_list, list)
561 current_uA += sibling->uA_load;
562
563 /* now get the optimum mode for our new total regulator load */
564 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
565 output_uV, current_uA);
566
567 /* check the new mode is allowed */
568 err = regulator_check_mode(rdev, mode);
569 if (err == 0)
570 rdev->desc->ops->set_mode(rdev, mode);
571}
572
573static int suspend_set_state(struct regulator_dev *rdev,
574 struct regulator_state *rstate)
575{
576 int ret = 0;
577
578 /* enable & disable are mandatory for suspend control */
579 if (!rdev->desc->ops->set_suspend_enable ||
Liam Girdwooda5766f12008-10-10 13:22:20 +0100580 !rdev->desc->ops->set_suspend_disable) {
581 printk(KERN_ERR "%s: no way to set suspend state\n",
582 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100584 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585
586 if (rstate->enabled)
587 ret = rdev->desc->ops->set_suspend_enable(rdev);
588 else
589 ret = rdev->desc->ops->set_suspend_disable(rdev);
590 if (ret < 0) {
591 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
592 return ret;
593 }
594
595 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
596 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
597 if (ret < 0) {
598 printk(KERN_ERR "%s: failed to set voltage\n",
599 __func__);
600 return ret;
601 }
602 }
603
604 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
605 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
606 if (ret < 0) {
607 printk(KERN_ERR "%s: failed to set mode\n", __func__);
608 return ret;
609 }
610 }
611 return ret;
612}
613
614/* locks held by caller */
615static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
616{
617 if (!rdev->constraints)
618 return -EINVAL;
619
620 switch (state) {
621 case PM_SUSPEND_STANDBY:
622 return suspend_set_state(rdev,
623 &rdev->constraints->state_standby);
624 case PM_SUSPEND_MEM:
625 return suspend_set_state(rdev,
626 &rdev->constraints->state_mem);
627 case PM_SUSPEND_MAX:
628 return suspend_set_state(rdev,
629 &rdev->constraints->state_disk);
630 default:
631 return -EINVAL;
632 }
633}
634
635static void print_constraints(struct regulator_dev *rdev)
636{
637 struct regulation_constraints *constraints = rdev->constraints;
638 char buf[80];
639 int count;
640
641 if (rdev->desc->type == REGULATOR_VOLTAGE) {
642 if (constraints->min_uV == constraints->max_uV)
643 count = sprintf(buf, "%d mV ",
644 constraints->min_uV / 1000);
645 else
646 count = sprintf(buf, "%d <--> %d mV ",
647 constraints->min_uV / 1000,
648 constraints->max_uV / 1000);
649 } else {
650 if (constraints->min_uA == constraints->max_uA)
651 count = sprintf(buf, "%d mA ",
652 constraints->min_uA / 1000);
653 else
654 count = sprintf(buf, "%d <--> %d mA ",
655 constraints->min_uA / 1000,
656 constraints->max_uA / 1000);
657 }
658 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
659 count += sprintf(buf + count, "fast ");
660 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
661 count += sprintf(buf + count, "normal ");
662 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
663 count += sprintf(buf + count, "idle ");
664 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
665 count += sprintf(buf + count, "standby");
666
667 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
668}
669
Liam Girdwooda5766f12008-10-10 13:22:20 +0100670/**
671 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000672 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000673 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100674 *
675 * Allows platform initialisation code to define and constrain
676 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
677 * Constraints *must* be set by platform code in order for some
678 * regulator operations to proceed i.e. set_voltage, set_current_limit,
679 * set_mode.
680 */
681static int set_machine_constraints(struct regulator_dev *rdev,
682 struct regulation_constraints *constraints)
683{
684 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100685 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100686 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100687
688 if (constraints->name)
689 name = constraints->name;
690 else if (rdev->desc->name)
691 name = rdev->desc->name;
692 else
693 name = "regulator";
Liam Girdwooda5766f12008-10-10 13:22:20 +0100694
David Brownell4367cfd2009-02-26 11:48:36 -0800695 /* constrain machine-level voltage specs to fit
696 * the actual range supported by this regulator.
697 */
698 if (ops->list_voltage && rdev->desc->n_voltages) {
699 int count = rdev->desc->n_voltages;
700 int i;
701 int min_uV = INT_MAX;
702 int max_uV = INT_MIN;
703 int cmin = constraints->min_uV;
704 int cmax = constraints->max_uV;
705
Mark Brown3e590912009-04-28 11:09:38 +0100706 /* it's safe to autoconfigure fixed-voltage supplies
707 and the constraints are used by list_voltage. */
David Brownell4367cfd2009-02-26 11:48:36 -0800708 if (count == 1 && !cmin) {
Mark Brown3e590912009-04-28 11:09:38 +0100709 cmin = 1;
David Brownell4367cfd2009-02-26 11:48:36 -0800710 cmax = INT_MAX;
Mark Brown3e590912009-04-28 11:09:38 +0100711 constraints->min_uV = cmin;
712 constraints->max_uV = cmax;
David Brownell4367cfd2009-02-26 11:48:36 -0800713 }
714
Mark Brown3e2b9ab2009-03-10 16:28:36 +0000715 /* voltage constraints are optional */
716 if ((cmin == 0) && (cmax == 0))
717 goto out;
718
David Brownell4367cfd2009-02-26 11:48:36 -0800719 /* else require explicit machine-level constraints */
Mark Brown3e2b9ab2009-03-10 16:28:36 +0000720 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
David Brownell4367cfd2009-02-26 11:48:36 -0800721 pr_err("%s: %s '%s' voltage constraints\n",
722 __func__, "invalid", name);
723 ret = -EINVAL;
724 goto out;
725 }
726
727 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
728 for (i = 0; i < count; i++) {
729 int value;
730
731 value = ops->list_voltage(rdev, i);
732 if (value <= 0)
733 continue;
734
735 /* maybe adjust [min_uV..max_uV] */
736 if (value >= cmin && value < min_uV)
737 min_uV = value;
738 if (value <= cmax && value > max_uV)
739 max_uV = value;
740 }
741
742 /* final: [min_uV..max_uV] valid iff constraints valid */
743 if (max_uV < min_uV) {
744 pr_err("%s: %s '%s' voltage constraints\n",
745 __func__, "unsupportable", name);
746 ret = -EINVAL;
747 goto out;
748 }
749
750 /* use regulator's subset of machine constraints */
751 if (constraints->min_uV < min_uV) {
752 pr_debug("%s: override '%s' %s, %d -> %d\n",
753 __func__, name, "min_uV",
754 constraints->min_uV, min_uV);
755 constraints->min_uV = min_uV;
756 }
757 if (constraints->max_uV > max_uV) {
758 pr_debug("%s: override '%s' %s, %d -> %d\n",
759 __func__, name, "max_uV",
760 constraints->max_uV, max_uV);
761 constraints->max_uV = max_uV;
762 }
763 }
764
Liam Girdwooda5766f12008-10-10 13:22:20 +0100765 rdev->constraints = constraints;
766
767 /* do we need to apply the constraint voltage */
768 if (rdev->constraints->apply_uV &&
769 rdev->constraints->min_uV == rdev->constraints->max_uV &&
Mark Browne5fda262008-09-09 16:21:20 +0100770 ops->set_voltage) {
771 ret = ops->set_voltage(rdev,
Liam Girdwooda5766f12008-10-10 13:22:20 +0100772 rdev->constraints->min_uV, rdev->constraints->max_uV);
773 if (ret < 0) {
Mark Browne06f5b42008-09-09 16:21:19 +0100774 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
775 __func__,
776 rdev->constraints->min_uV, name);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100777 rdev->constraints = NULL;
778 goto out;
779 }
780 }
781
Liam Girdwooda5766f12008-10-10 13:22:20 +0100782 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100783 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100784 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100785 if (ret < 0) {
786 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
787 __func__, name);
788 rdev->constraints = NULL;
789 goto out;
790 }
791 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100792
Mark Browna3084662009-02-26 19:24:19 +0000793 if (constraints->initial_mode) {
794 if (!ops->set_mode) {
795 printk(KERN_ERR "%s: no set_mode operation for %s\n",
796 __func__, name);
797 ret = -EINVAL;
798 goto out;
799 }
800
801 ret = ops->set_mode(rdev, constraints->initial_mode);
802 if (ret < 0) {
803 printk(KERN_ERR
804 "%s: failed to set initial mode for %s: %d\n",
805 __func__, name, ret);
806 goto out;
807 }
808 }
809
Mark Browncacf90f2009-03-02 16:32:46 +0000810 /* If the constraints say the regulator should be on at this point
811 * and we have control then make sure it is enabled.
812 */
813 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100814 ret = ops->enable(rdev);
815 if (ret < 0) {
816 printk(KERN_ERR "%s: failed to enable %s\n",
817 __func__, name);
818 rdev->constraints = NULL;
819 goto out;
820 }
821 }
822
Liam Girdwooda5766f12008-10-10 13:22:20 +0100823 print_constraints(rdev);
824out:
825 return ret;
826}
827
828/**
829 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000830 * @rdev: regulator name
831 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100832 *
833 * Called by platform initialisation code to set the supply regulator for this
834 * regulator. This ensures that a regulators supply will also be enabled by the
835 * core if it's child is enabled.
836 */
837static int set_supply(struct regulator_dev *rdev,
838 struct regulator_dev *supply_rdev)
839{
840 int err;
841
842 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
843 "supply");
844 if (err) {
845 printk(KERN_ERR
846 "%s: could not add device link %s err %d\n",
847 __func__, supply_rdev->dev.kobj.name, err);
848 goto out;
849 }
850 rdev->supply = supply_rdev;
851 list_add(&rdev->slist, &supply_rdev->supply_list);
852out:
853 return err;
854}
855
856/**
857 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000858 * @rdev: regulator source
859 * @consumer_dev: device the supply applies to
860 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100861 *
862 * Allows platform initialisation code to map physical regulator
863 * sources to symbolic names for supplies for use by devices. Devices
864 * should use these symbolic names to request regulators, avoiding the
865 * need to provide board-specific regulator names as platform data.
866 */
867static int set_consumer_device_supply(struct regulator_dev *rdev,
868 struct device *consumer_dev, const char *supply)
869{
870 struct regulator_map *node;
871
872 if (supply == NULL)
873 return -EINVAL;
874
David Brownell6001e132008-12-31 12:54:19 +0000875 list_for_each_entry(node, &regulator_map_list, list) {
876 if (consumer_dev != node->dev)
877 continue;
878 if (strcmp(node->supply, supply) != 0)
879 continue;
880
881 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
882 dev_name(&node->regulator->dev),
883 node->regulator->desc->name,
884 supply,
885 dev_name(&rdev->dev), rdev->desc->name);
886 return -EBUSY;
887 }
888
Liam Girdwooda5766f12008-10-10 13:22:20 +0100889 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
890 if (node == NULL)
891 return -ENOMEM;
892
893 node->regulator = rdev;
894 node->dev = consumer_dev;
895 node->supply = supply;
896
897 list_add(&node->list, &regulator_map_list);
898 return 0;
899}
900
901static void unset_consumer_device_supply(struct regulator_dev *rdev,
902 struct device *consumer_dev)
903{
904 struct regulator_map *node, *n;
905
906 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
907 if (rdev == node->regulator &&
908 consumer_dev == node->dev) {
909 list_del(&node->list);
910 kfree(node);
911 return;
912 }
913 }
914}
915
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200916static void unset_regulator_supplies(struct regulator_dev *rdev)
917{
918 struct regulator_map *node, *n;
919
920 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
921 if (rdev == node->regulator) {
922 list_del(&node->list);
923 kfree(node);
924 return;
925 }
926 }
927}
928
Liam Girdwood414c70c2008-04-30 15:59:04 +0100929#define REG_STR_SIZE 32
930
931static struct regulator *create_regulator(struct regulator_dev *rdev,
932 struct device *dev,
933 const char *supply_name)
934{
935 struct regulator *regulator;
936 char buf[REG_STR_SIZE];
937 int err, size;
938
939 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
940 if (regulator == NULL)
941 return NULL;
942
943 mutex_lock(&rdev->mutex);
944 regulator->rdev = rdev;
945 list_add(&regulator->list, &rdev->consumer_list);
946
947 if (dev) {
948 /* create a 'requested_microamps_name' sysfs entry */
949 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
950 supply_name);
951 if (size >= REG_STR_SIZE)
952 goto overflow_err;
953
954 regulator->dev = dev;
955 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
956 if (regulator->dev_attr.attr.name == NULL)
957 goto attr_name_err;
958
959 regulator->dev_attr.attr.owner = THIS_MODULE;
960 regulator->dev_attr.attr.mode = 0444;
961 regulator->dev_attr.show = device_requested_uA_show;
962 err = device_create_file(dev, &regulator->dev_attr);
963 if (err < 0) {
964 printk(KERN_WARNING "%s: could not add regulator_dev"
965 " load sysfs\n", __func__);
966 goto attr_name_err;
967 }
968
969 /* also add a link to the device sysfs entry */
970 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
971 dev->kobj.name, supply_name);
972 if (size >= REG_STR_SIZE)
973 goto attr_err;
974
975 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
976 if (regulator->supply_name == NULL)
977 goto attr_err;
978
979 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
980 buf);
981 if (err) {
982 printk(KERN_WARNING
983 "%s: could not add device link %s err %d\n",
984 __func__, dev->kobj.name, err);
985 device_remove_file(dev, &regulator->dev_attr);
986 goto link_name_err;
987 }
988 }
989 mutex_unlock(&rdev->mutex);
990 return regulator;
991link_name_err:
992 kfree(regulator->supply_name);
993attr_err:
994 device_remove_file(regulator->dev, &regulator->dev_attr);
995attr_name_err:
996 kfree(regulator->dev_attr.attr.name);
997overflow_err:
998 list_del(&regulator->list);
999 kfree(regulator);
1000 mutex_unlock(&rdev->mutex);
1001 return NULL;
1002}
1003
1004/**
1005 * regulator_get - lookup and obtain a reference to a regulator.
1006 * @dev: device for regulator "consumer"
1007 * @id: Supply name or regulator ID.
1008 *
1009 * Returns a struct regulator corresponding to the regulator producer,
Mark Brownfe203ddf2009-02-11 15:32:06 +00001010 * or IS_ERR() condition containing errno.
1011 *
1012 * Use of supply names configured via regulator_set_device_supply() is
1013 * strongly encouraged. It is recommended that the supply name used
1014 * should match the name used for the supply and/or the relevant
1015 * device pins in the datasheet.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001016 */
1017struct regulator *regulator_get(struct device *dev, const char *id)
1018{
1019 struct regulator_dev *rdev;
1020 struct regulator_map *map;
1021 struct regulator *regulator = ERR_PTR(-ENODEV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001022
1023 if (id == NULL) {
1024 printk(KERN_ERR "regulator: get() with no identifier\n");
1025 return regulator;
1026 }
1027
1028 mutex_lock(&regulator_list_mutex);
1029
1030 list_for_each_entry(map, &regulator_map_list, list) {
1031 if (dev == map->dev &&
1032 strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001033 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001034 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001035 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001036 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001037 mutex_unlock(&regulator_list_mutex);
1038 return regulator;
1039
1040found:
Liam Girdwooda5766f12008-10-10 13:22:20 +01001041 if (!try_module_get(rdev->owner))
1042 goto out;
1043
Liam Girdwood414c70c2008-04-30 15:59:04 +01001044 regulator = create_regulator(rdev, dev, id);
1045 if (regulator == NULL) {
1046 regulator = ERR_PTR(-ENOMEM);
1047 module_put(rdev->owner);
1048 }
1049
Liam Girdwooda5766f12008-10-10 13:22:20 +01001050out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001051 mutex_unlock(&regulator_list_mutex);
1052 return regulator;
1053}
1054EXPORT_SYMBOL_GPL(regulator_get);
1055
1056/**
1057 * regulator_put - "free" the regulator source
1058 * @regulator: regulator source
1059 *
1060 * Note: drivers must ensure that all regulator_enable calls made on this
1061 * regulator source are balanced by regulator_disable calls prior to calling
1062 * this function.
1063 */
1064void regulator_put(struct regulator *regulator)
1065{
1066 struct regulator_dev *rdev;
1067
1068 if (regulator == NULL || IS_ERR(regulator))
1069 return;
1070
Liam Girdwood414c70c2008-04-30 15:59:04 +01001071 mutex_lock(&regulator_list_mutex);
1072 rdev = regulator->rdev;
1073
1074 /* remove any sysfs entries */
1075 if (regulator->dev) {
1076 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1077 kfree(regulator->supply_name);
1078 device_remove_file(regulator->dev, &regulator->dev_attr);
1079 kfree(regulator->dev_attr.attr.name);
1080 }
1081 list_del(&regulator->list);
1082 kfree(regulator);
1083
1084 module_put(rdev->owner);
1085 mutex_unlock(&regulator_list_mutex);
1086}
1087EXPORT_SYMBOL_GPL(regulator_put);
1088
1089/* locks held by regulator_enable() */
1090static int _regulator_enable(struct regulator_dev *rdev)
1091{
1092 int ret = -EINVAL;
1093
1094 if (!rdev->constraints) {
1095 printk(KERN_ERR "%s: %s has no constraints\n",
1096 __func__, rdev->desc->name);
1097 return ret;
1098 }
1099
1100 /* do we need to enable the supply regulator first */
1101 if (rdev->supply) {
1102 ret = _regulator_enable(rdev->supply);
1103 if (ret < 0) {
1104 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1105 __func__, rdev->desc->name, ret);
1106 return ret;
1107 }
1108 }
1109
1110 /* check voltage and requested load before enabling */
1111 if (rdev->desc->ops->enable) {
1112
1113 if (rdev->constraints &&
1114 (rdev->constraints->valid_ops_mask &
1115 REGULATOR_CHANGE_DRMS))
1116 drms_uA_update(rdev);
1117
1118 ret = rdev->desc->ops->enable(rdev);
1119 if (ret < 0) {
1120 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1121 __func__, rdev->desc->name, ret);
1122 return ret;
1123 }
1124 rdev->use_count++;
1125 return ret;
1126 }
1127
1128 return ret;
1129}
1130
1131/**
1132 * regulator_enable - enable regulator output
1133 * @regulator: regulator source
1134 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001135 * Request that the regulator be enabled with the regulator output at
1136 * the predefined voltage or current value. Calls to regulator_enable()
1137 * must be balanced with calls to regulator_disable().
1138 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001139 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001140 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001141 */
1142int regulator_enable(struct regulator *regulator)
1143{
David Brownell412aec62008-11-16 11:44:46 -08001144 struct regulator_dev *rdev = regulator->rdev;
1145 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001146
David Brownell412aec62008-11-16 11:44:46 -08001147 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001148 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001149 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001150 return ret;
1151}
1152EXPORT_SYMBOL_GPL(regulator_enable);
1153
1154/* locks held by regulator_disable() */
1155static int _regulator_disable(struct regulator_dev *rdev)
1156{
1157 int ret = 0;
1158
David Brownellcd94b502009-03-11 16:43:34 -08001159 if (WARN(rdev->use_count <= 0,
1160 "unbalanced disables for %s\n",
1161 rdev->desc->name))
1162 return -EIO;
1163
Liam Girdwood414c70c2008-04-30 15:59:04 +01001164 /* are we the last user and permitted to disable ? */
1165 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1166
1167 /* we are last user */
1168 if (rdev->desc->ops->disable) {
1169 ret = rdev->desc->ops->disable(rdev);
1170 if (ret < 0) {
1171 printk(KERN_ERR "%s: failed to disable %s\n",
1172 __func__, rdev->desc->name);
1173 return ret;
1174 }
1175 }
1176
1177 /* decrease our supplies ref count and disable if required */
1178 if (rdev->supply)
1179 _regulator_disable(rdev->supply);
1180
1181 rdev->use_count = 0;
1182 } else if (rdev->use_count > 1) {
1183
1184 if (rdev->constraints &&
1185 (rdev->constraints->valid_ops_mask &
1186 REGULATOR_CHANGE_DRMS))
1187 drms_uA_update(rdev);
1188
1189 rdev->use_count--;
1190 }
1191 return ret;
1192}
1193
1194/**
1195 * regulator_disable - disable regulator output
1196 * @regulator: regulator source
1197 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001198 * Disable the regulator output voltage or current. Calls to
1199 * regulator_enable() must be balanced with calls to
1200 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001201 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001202 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001203 * devices have it enabled, the regulator device supports disabling and
1204 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001205 */
1206int regulator_disable(struct regulator *regulator)
1207{
David Brownell412aec62008-11-16 11:44:46 -08001208 struct regulator_dev *rdev = regulator->rdev;
1209 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001210
David Brownell412aec62008-11-16 11:44:46 -08001211 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001212 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001213 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001214 return ret;
1215}
1216EXPORT_SYMBOL_GPL(regulator_disable);
1217
1218/* locks held by regulator_force_disable() */
1219static int _regulator_force_disable(struct regulator_dev *rdev)
1220{
1221 int ret = 0;
1222
1223 /* force disable */
1224 if (rdev->desc->ops->disable) {
1225 /* ah well, who wants to live forever... */
1226 ret = rdev->desc->ops->disable(rdev);
1227 if (ret < 0) {
1228 printk(KERN_ERR "%s: failed to force disable %s\n",
1229 __func__, rdev->desc->name);
1230 return ret;
1231 }
1232 /* notify other consumers that power has been forced off */
1233 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1234 NULL);
1235 }
1236
1237 /* decrease our supplies ref count and disable if required */
1238 if (rdev->supply)
1239 _regulator_disable(rdev->supply);
1240
1241 rdev->use_count = 0;
1242 return ret;
1243}
1244
1245/**
1246 * regulator_force_disable - force disable regulator output
1247 * @regulator: regulator source
1248 *
1249 * Forcibly disable the regulator output voltage or current.
1250 * NOTE: this *will* disable the regulator output even if other consumer
1251 * devices have it enabled. This should be used for situations when device
1252 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1253 */
1254int regulator_force_disable(struct regulator *regulator)
1255{
1256 int ret;
1257
1258 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001259 regulator->uA_load = 0;
1260 ret = _regulator_force_disable(regulator->rdev);
1261 mutex_unlock(&regulator->rdev->mutex);
1262 return ret;
1263}
1264EXPORT_SYMBOL_GPL(regulator_force_disable);
1265
1266static int _regulator_is_enabled(struct regulator_dev *rdev)
1267{
1268 int ret;
1269
1270 mutex_lock(&rdev->mutex);
1271
1272 /* sanity check */
1273 if (!rdev->desc->ops->is_enabled) {
1274 ret = -EINVAL;
1275 goto out;
1276 }
1277
1278 ret = rdev->desc->ops->is_enabled(rdev);
1279out:
1280 mutex_unlock(&rdev->mutex);
1281 return ret;
1282}
1283
1284/**
1285 * regulator_is_enabled - is the regulator output enabled
1286 * @regulator: regulator source
1287 *
David Brownell412aec62008-11-16 11:44:46 -08001288 * Returns positive if the regulator driver backing the source/client
1289 * has requested that the device be enabled, zero if it hasn't, else a
1290 * negative errno code.
1291 *
1292 * Note that the device backing this regulator handle can have multiple
1293 * users, so it might be enabled even if regulator_enable() was never
1294 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001295 */
1296int regulator_is_enabled(struct regulator *regulator)
1297{
1298 return _regulator_is_enabled(regulator->rdev);
1299}
1300EXPORT_SYMBOL_GPL(regulator_is_enabled);
1301
1302/**
David Brownell4367cfd2009-02-26 11:48:36 -08001303 * regulator_count_voltages - count regulator_list_voltage() selectors
1304 * @regulator: regulator source
1305 *
1306 * Returns number of selectors, or negative errno. Selectors are
1307 * numbered starting at zero, and typically correspond to bitfields
1308 * in hardware registers.
1309 */
1310int regulator_count_voltages(struct regulator *regulator)
1311{
1312 struct regulator_dev *rdev = regulator->rdev;
1313
1314 return rdev->desc->n_voltages ? : -EINVAL;
1315}
1316EXPORT_SYMBOL_GPL(regulator_count_voltages);
1317
1318/**
1319 * regulator_list_voltage - enumerate supported voltages
1320 * @regulator: regulator source
1321 * @selector: identify voltage to list
1322 * Context: can sleep
1323 *
1324 * Returns a voltage that can be passed to @regulator_set_voltage(),
1325 * zero if this selector code can't be used on this sytem, or a
1326 * negative errno.
1327 */
1328int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1329{
1330 struct regulator_dev *rdev = regulator->rdev;
1331 struct regulator_ops *ops = rdev->desc->ops;
1332 int ret;
1333
1334 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1335 return -EINVAL;
1336
1337 mutex_lock(&rdev->mutex);
1338 ret = ops->list_voltage(rdev, selector);
1339 mutex_unlock(&rdev->mutex);
1340
1341 if (ret > 0) {
1342 if (ret < rdev->constraints->min_uV)
1343 ret = 0;
1344 else if (ret > rdev->constraints->max_uV)
1345 ret = 0;
1346 }
1347
1348 return ret;
1349}
1350EXPORT_SYMBOL_GPL(regulator_list_voltage);
1351
1352/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001353 * regulator_set_voltage - set regulator output voltage
1354 * @regulator: regulator source
1355 * @min_uV: Minimum required voltage in uV
1356 * @max_uV: Maximum acceptable voltage in uV
1357 *
1358 * Sets a voltage regulator to the desired output voltage. This can be set
1359 * during any regulator state. IOW, regulator can be disabled or enabled.
1360 *
1361 * If the regulator is enabled then the voltage will change to the new value
1362 * immediately otherwise if the regulator is disabled the regulator will
1363 * output at the new voltage when enabled.
1364 *
1365 * NOTE: If the regulator is shared between several devices then the lowest
1366 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001367 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001368 * calling this function otherwise this call will fail.
1369 */
1370int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1371{
1372 struct regulator_dev *rdev = regulator->rdev;
1373 int ret;
1374
1375 mutex_lock(&rdev->mutex);
1376
1377 /* sanity check */
1378 if (!rdev->desc->ops->set_voltage) {
1379 ret = -EINVAL;
1380 goto out;
1381 }
1382
1383 /* constraints check */
1384 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1385 if (ret < 0)
1386 goto out;
1387 regulator->min_uV = min_uV;
1388 regulator->max_uV = max_uV;
1389 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1390
1391out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001392 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001393 mutex_unlock(&rdev->mutex);
1394 return ret;
1395}
1396EXPORT_SYMBOL_GPL(regulator_set_voltage);
1397
1398static int _regulator_get_voltage(struct regulator_dev *rdev)
1399{
1400 /* sanity check */
1401 if (rdev->desc->ops->get_voltage)
1402 return rdev->desc->ops->get_voltage(rdev);
1403 else
1404 return -EINVAL;
1405}
1406
1407/**
1408 * regulator_get_voltage - get regulator output voltage
1409 * @regulator: regulator source
1410 *
1411 * This returns the current regulator voltage in uV.
1412 *
1413 * NOTE: If the regulator is disabled it will return the voltage value. This
1414 * function should not be used to determine regulator state.
1415 */
1416int regulator_get_voltage(struct regulator *regulator)
1417{
1418 int ret;
1419
1420 mutex_lock(&regulator->rdev->mutex);
1421
1422 ret = _regulator_get_voltage(regulator->rdev);
1423
1424 mutex_unlock(&regulator->rdev->mutex);
1425
1426 return ret;
1427}
1428EXPORT_SYMBOL_GPL(regulator_get_voltage);
1429
1430/**
1431 * regulator_set_current_limit - set regulator output current limit
1432 * @regulator: regulator source
1433 * @min_uA: Minimuum supported current in uA
1434 * @max_uA: Maximum supported current in uA
1435 *
1436 * Sets current sink to the desired output current. This can be set during
1437 * any regulator state. IOW, regulator can be disabled or enabled.
1438 *
1439 * If the regulator is enabled then the current will change to the new value
1440 * immediately otherwise if the regulator is disabled the regulator will
1441 * output at the new current when enabled.
1442 *
1443 * NOTE: Regulator system constraints must be set for this regulator before
1444 * calling this function otherwise this call will fail.
1445 */
1446int regulator_set_current_limit(struct regulator *regulator,
1447 int min_uA, int max_uA)
1448{
1449 struct regulator_dev *rdev = regulator->rdev;
1450 int ret;
1451
1452 mutex_lock(&rdev->mutex);
1453
1454 /* sanity check */
1455 if (!rdev->desc->ops->set_current_limit) {
1456 ret = -EINVAL;
1457 goto out;
1458 }
1459
1460 /* constraints check */
1461 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1462 if (ret < 0)
1463 goto out;
1464
1465 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1466out:
1467 mutex_unlock(&rdev->mutex);
1468 return ret;
1469}
1470EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1471
1472static int _regulator_get_current_limit(struct regulator_dev *rdev)
1473{
1474 int ret;
1475
1476 mutex_lock(&rdev->mutex);
1477
1478 /* sanity check */
1479 if (!rdev->desc->ops->get_current_limit) {
1480 ret = -EINVAL;
1481 goto out;
1482 }
1483
1484 ret = rdev->desc->ops->get_current_limit(rdev);
1485out:
1486 mutex_unlock(&rdev->mutex);
1487 return ret;
1488}
1489
1490/**
1491 * regulator_get_current_limit - get regulator output current
1492 * @regulator: regulator source
1493 *
1494 * This returns the current supplied by the specified current sink in uA.
1495 *
1496 * NOTE: If the regulator is disabled it will return the current value. This
1497 * function should not be used to determine regulator state.
1498 */
1499int regulator_get_current_limit(struct regulator *regulator)
1500{
1501 return _regulator_get_current_limit(regulator->rdev);
1502}
1503EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1504
1505/**
1506 * regulator_set_mode - set regulator operating mode
1507 * @regulator: regulator source
1508 * @mode: operating mode - one of the REGULATOR_MODE constants
1509 *
1510 * Set regulator operating mode to increase regulator efficiency or improve
1511 * regulation performance.
1512 *
1513 * NOTE: Regulator system constraints must be set for this regulator before
1514 * calling this function otherwise this call will fail.
1515 */
1516int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1517{
1518 struct regulator_dev *rdev = regulator->rdev;
1519 int ret;
1520
1521 mutex_lock(&rdev->mutex);
1522
1523 /* sanity check */
1524 if (!rdev->desc->ops->set_mode) {
1525 ret = -EINVAL;
1526 goto out;
1527 }
1528
1529 /* constraints check */
1530 ret = regulator_check_mode(rdev, mode);
1531 if (ret < 0)
1532 goto out;
1533
1534 ret = rdev->desc->ops->set_mode(rdev, mode);
1535out:
1536 mutex_unlock(&rdev->mutex);
1537 return ret;
1538}
1539EXPORT_SYMBOL_GPL(regulator_set_mode);
1540
1541static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1542{
1543 int ret;
1544
1545 mutex_lock(&rdev->mutex);
1546
1547 /* sanity check */
1548 if (!rdev->desc->ops->get_mode) {
1549 ret = -EINVAL;
1550 goto out;
1551 }
1552
1553 ret = rdev->desc->ops->get_mode(rdev);
1554out:
1555 mutex_unlock(&rdev->mutex);
1556 return ret;
1557}
1558
1559/**
1560 * regulator_get_mode - get regulator operating mode
1561 * @regulator: regulator source
1562 *
1563 * Get the current regulator operating mode.
1564 */
1565unsigned int regulator_get_mode(struct regulator *regulator)
1566{
1567 return _regulator_get_mode(regulator->rdev);
1568}
1569EXPORT_SYMBOL_GPL(regulator_get_mode);
1570
1571/**
1572 * regulator_set_optimum_mode - set regulator optimum operating mode
1573 * @regulator: regulator source
1574 * @uA_load: load current
1575 *
1576 * Notifies the regulator core of a new device load. This is then used by
1577 * DRMS (if enabled by constraints) to set the most efficient regulator
1578 * operating mode for the new regulator loading.
1579 *
1580 * Consumer devices notify their supply regulator of the maximum power
1581 * they will require (can be taken from device datasheet in the power
1582 * consumption tables) when they change operational status and hence power
1583 * state. Examples of operational state changes that can affect power
1584 * consumption are :-
1585 *
1586 * o Device is opened / closed.
1587 * o Device I/O is about to begin or has just finished.
1588 * o Device is idling in between work.
1589 *
1590 * This information is also exported via sysfs to userspace.
1591 *
1592 * DRMS will sum the total requested load on the regulator and change
1593 * to the most efficient operating mode if platform constraints allow.
1594 *
1595 * Returns the new regulator mode or error.
1596 */
1597int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1598{
1599 struct regulator_dev *rdev = regulator->rdev;
1600 struct regulator *consumer;
1601 int ret, output_uV, input_uV, total_uA_load = 0;
1602 unsigned int mode;
1603
1604 mutex_lock(&rdev->mutex);
1605
1606 regulator->uA_load = uA_load;
1607 ret = regulator_check_drms(rdev);
1608 if (ret < 0)
1609 goto out;
1610 ret = -EINVAL;
1611
1612 /* sanity check */
1613 if (!rdev->desc->ops->get_optimum_mode)
1614 goto out;
1615
1616 /* get output voltage */
1617 output_uV = rdev->desc->ops->get_voltage(rdev);
1618 if (output_uV <= 0) {
1619 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1620 __func__, rdev->desc->name);
1621 goto out;
1622 }
1623
1624 /* get input voltage */
1625 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1626 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1627 else
1628 input_uV = rdev->constraints->input_uV;
1629 if (input_uV <= 0) {
1630 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1631 __func__, rdev->desc->name);
1632 goto out;
1633 }
1634
1635 /* calc total requested load for this regulator */
1636 list_for_each_entry(consumer, &rdev->consumer_list, list)
1637 total_uA_load += consumer->uA_load;
1638
1639 mode = rdev->desc->ops->get_optimum_mode(rdev,
1640 input_uV, output_uV,
1641 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001642 ret = regulator_check_mode(rdev, mode);
1643 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001644 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1645 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1646 total_uA_load, input_uV, output_uV);
1647 goto out;
1648 }
1649
1650 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001651 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001652 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1653 __func__, mode, rdev->desc->name);
1654 goto out;
1655 }
1656 ret = mode;
1657out:
1658 mutex_unlock(&rdev->mutex);
1659 return ret;
1660}
1661EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1662
1663/**
1664 * regulator_register_notifier - register regulator event notifier
1665 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001666 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001667 *
1668 * Register notifier block to receive regulator events.
1669 */
1670int regulator_register_notifier(struct regulator *regulator,
1671 struct notifier_block *nb)
1672{
1673 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1674 nb);
1675}
1676EXPORT_SYMBOL_GPL(regulator_register_notifier);
1677
1678/**
1679 * regulator_unregister_notifier - unregister regulator event notifier
1680 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001681 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001682 *
1683 * Unregister regulator event notifier block.
1684 */
1685int regulator_unregister_notifier(struct regulator *regulator,
1686 struct notifier_block *nb)
1687{
1688 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1689 nb);
1690}
1691EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1692
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001693/* notify regulator consumers and downstream regulator consumers.
1694 * Note mutex must be held by caller.
1695 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001696static void _notifier_call_chain(struct regulator_dev *rdev,
1697 unsigned long event, void *data)
1698{
1699 struct regulator_dev *_rdev;
1700
1701 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001702 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001703
1704 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001705 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1706 mutex_lock(&_rdev->mutex);
1707 _notifier_call_chain(_rdev, event, data);
1708 mutex_unlock(&_rdev->mutex);
1709 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001710}
1711
1712/**
1713 * regulator_bulk_get - get multiple regulator consumers
1714 *
1715 * @dev: Device to supply
1716 * @num_consumers: Number of consumers to register
1717 * @consumers: Configuration of consumers; clients are stored here.
1718 *
1719 * @return 0 on success, an errno on failure.
1720 *
1721 * This helper function allows drivers to get several regulator
1722 * consumers in one operation. If any of the regulators cannot be
1723 * acquired then any regulators that were allocated will be freed
1724 * before returning to the caller.
1725 */
1726int regulator_bulk_get(struct device *dev, int num_consumers,
1727 struct regulator_bulk_data *consumers)
1728{
1729 int i;
1730 int ret;
1731
1732 for (i = 0; i < num_consumers; i++)
1733 consumers[i].consumer = NULL;
1734
1735 for (i = 0; i < num_consumers; i++) {
1736 consumers[i].consumer = regulator_get(dev,
1737 consumers[i].supply);
1738 if (IS_ERR(consumers[i].consumer)) {
1739 dev_err(dev, "Failed to get supply '%s'\n",
1740 consumers[i].supply);
1741 ret = PTR_ERR(consumers[i].consumer);
1742 consumers[i].consumer = NULL;
1743 goto err;
1744 }
1745 }
1746
1747 return 0;
1748
1749err:
1750 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1751 regulator_put(consumers[i].consumer);
1752
1753 return ret;
1754}
1755EXPORT_SYMBOL_GPL(regulator_bulk_get);
1756
1757/**
1758 * regulator_bulk_enable - enable multiple regulator consumers
1759 *
1760 * @num_consumers: Number of consumers
1761 * @consumers: Consumer data; clients are stored here.
1762 * @return 0 on success, an errno on failure
1763 *
1764 * This convenience API allows consumers to enable multiple regulator
1765 * clients in a single API call. If any consumers cannot be enabled
1766 * then any others that were enabled will be disabled again prior to
1767 * return.
1768 */
1769int regulator_bulk_enable(int num_consumers,
1770 struct regulator_bulk_data *consumers)
1771{
1772 int i;
1773 int ret;
1774
1775 for (i = 0; i < num_consumers; i++) {
1776 ret = regulator_enable(consumers[i].consumer);
1777 if (ret != 0)
1778 goto err;
1779 }
1780
1781 return 0;
1782
1783err:
1784 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1785 for (i = 0; i < num_consumers; i++)
1786 regulator_disable(consumers[i].consumer);
1787
1788 return ret;
1789}
1790EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1791
1792/**
1793 * regulator_bulk_disable - disable multiple regulator consumers
1794 *
1795 * @num_consumers: Number of consumers
1796 * @consumers: Consumer data; clients are stored here.
1797 * @return 0 on success, an errno on failure
1798 *
1799 * This convenience API allows consumers to disable multiple regulator
1800 * clients in a single API call. If any consumers cannot be enabled
1801 * then any others that were disabled will be disabled again prior to
1802 * return.
1803 */
1804int regulator_bulk_disable(int num_consumers,
1805 struct regulator_bulk_data *consumers)
1806{
1807 int i;
1808 int ret;
1809
1810 for (i = 0; i < num_consumers; i++) {
1811 ret = regulator_disable(consumers[i].consumer);
1812 if (ret != 0)
1813 goto err;
1814 }
1815
1816 return 0;
1817
1818err:
1819 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1820 for (i = 0; i < num_consumers; i++)
1821 regulator_enable(consumers[i].consumer);
1822
1823 return ret;
1824}
1825EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1826
1827/**
1828 * regulator_bulk_free - free multiple regulator consumers
1829 *
1830 * @num_consumers: Number of consumers
1831 * @consumers: Consumer data; clients are stored here.
1832 *
1833 * This convenience API allows consumers to free multiple regulator
1834 * clients in a single API call.
1835 */
1836void regulator_bulk_free(int num_consumers,
1837 struct regulator_bulk_data *consumers)
1838{
1839 int i;
1840
1841 for (i = 0; i < num_consumers; i++) {
1842 regulator_put(consumers[i].consumer);
1843 consumers[i].consumer = NULL;
1844 }
1845}
1846EXPORT_SYMBOL_GPL(regulator_bulk_free);
1847
1848/**
1849 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00001850 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01001851 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00001852 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001853 *
1854 * Called by regulator drivers to notify clients a regulator event has
1855 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001856 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001857 */
1858int regulator_notifier_call_chain(struct regulator_dev *rdev,
1859 unsigned long event, void *data)
1860{
1861 _notifier_call_chain(rdev, event, data);
1862 return NOTIFY_DONE;
1863
1864}
1865EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1866
David Brownell7ad68e22008-11-11 17:39:02 -08001867/*
1868 * To avoid cluttering sysfs (and memory) with useless state, only
1869 * create attributes that can be meaningfully displayed.
1870 */
1871static int add_regulator_attributes(struct regulator_dev *rdev)
1872{
1873 struct device *dev = &rdev->dev;
1874 struct regulator_ops *ops = rdev->desc->ops;
1875 int status = 0;
1876
1877 /* some attributes need specific methods to be displayed */
1878 if (ops->get_voltage) {
1879 status = device_create_file(dev, &dev_attr_microvolts);
1880 if (status < 0)
1881 return status;
1882 }
1883 if (ops->get_current_limit) {
1884 status = device_create_file(dev, &dev_attr_microamps);
1885 if (status < 0)
1886 return status;
1887 }
1888 if (ops->get_mode) {
1889 status = device_create_file(dev, &dev_attr_opmode);
1890 if (status < 0)
1891 return status;
1892 }
1893 if (ops->is_enabled) {
1894 status = device_create_file(dev, &dev_attr_state);
1895 if (status < 0)
1896 return status;
1897 }
David Brownell853116a2009-01-14 23:03:17 -08001898 if (ops->get_status) {
1899 status = device_create_file(dev, &dev_attr_status);
1900 if (status < 0)
1901 return status;
1902 }
David Brownell7ad68e22008-11-11 17:39:02 -08001903
1904 /* some attributes are type-specific */
1905 if (rdev->desc->type == REGULATOR_CURRENT) {
1906 status = device_create_file(dev, &dev_attr_requested_microamps);
1907 if (status < 0)
1908 return status;
1909 }
1910
1911 /* all the other attributes exist to support constraints;
1912 * don't show them if there are no constraints, or if the
1913 * relevant supporting methods are missing.
1914 */
1915 if (!rdev->constraints)
1916 return status;
1917
1918 /* constraints need specific supporting methods */
1919 if (ops->set_voltage) {
1920 status = device_create_file(dev, &dev_attr_min_microvolts);
1921 if (status < 0)
1922 return status;
1923 status = device_create_file(dev, &dev_attr_max_microvolts);
1924 if (status < 0)
1925 return status;
1926 }
1927 if (ops->set_current_limit) {
1928 status = device_create_file(dev, &dev_attr_min_microamps);
1929 if (status < 0)
1930 return status;
1931 status = device_create_file(dev, &dev_attr_max_microamps);
1932 if (status < 0)
1933 return status;
1934 }
1935
1936 /* suspend mode constraints need multiple supporting methods */
1937 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1938 return status;
1939
1940 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1941 if (status < 0)
1942 return status;
1943 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1944 if (status < 0)
1945 return status;
1946 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1947 if (status < 0)
1948 return status;
1949
1950 if (ops->set_suspend_voltage) {
1951 status = device_create_file(dev,
1952 &dev_attr_suspend_standby_microvolts);
1953 if (status < 0)
1954 return status;
1955 status = device_create_file(dev,
1956 &dev_attr_suspend_mem_microvolts);
1957 if (status < 0)
1958 return status;
1959 status = device_create_file(dev,
1960 &dev_attr_suspend_disk_microvolts);
1961 if (status < 0)
1962 return status;
1963 }
1964
1965 if (ops->set_suspend_mode) {
1966 status = device_create_file(dev,
1967 &dev_attr_suspend_standby_mode);
1968 if (status < 0)
1969 return status;
1970 status = device_create_file(dev,
1971 &dev_attr_suspend_mem_mode);
1972 if (status < 0)
1973 return status;
1974 status = device_create_file(dev,
1975 &dev_attr_suspend_disk_mode);
1976 if (status < 0)
1977 return status;
1978 }
1979
1980 return status;
1981}
1982
Liam Girdwood414c70c2008-04-30 15:59:04 +01001983/**
1984 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001985 * @regulator_desc: regulator to register
1986 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00001987 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00001988 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01001989 *
1990 * Called by regulator drivers to register a regulator.
1991 * Returns 0 on success.
1992 */
1993struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00001994 struct device *dev, struct regulator_init_data *init_data,
1995 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001996{
1997 static atomic_t regulator_no = ATOMIC_INIT(0);
1998 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001999 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002000
2001 if (regulator_desc == NULL)
2002 return ERR_PTR(-EINVAL);
2003
2004 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2005 return ERR_PTR(-EINVAL);
2006
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002007 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2008 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002009 return ERR_PTR(-EINVAL);
2010
Mark Brown46fabe1e2008-09-09 16:21:18 +01002011 if (!init_data)
2012 return ERR_PTR(-EINVAL);
2013
Liam Girdwood414c70c2008-04-30 15:59:04 +01002014 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2015 if (rdev == NULL)
2016 return ERR_PTR(-ENOMEM);
2017
2018 mutex_lock(&regulator_list_mutex);
2019
2020 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002021 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002022 rdev->owner = regulator_desc->owner;
2023 rdev->desc = regulator_desc;
2024 INIT_LIST_HEAD(&rdev->consumer_list);
2025 INIT_LIST_HEAD(&rdev->supply_list);
2026 INIT_LIST_HEAD(&rdev->list);
2027 INIT_LIST_HEAD(&rdev->slist);
2028 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2029
Liam Girdwooda5766f12008-10-10 13:22:20 +01002030 /* preform any regulator specific init */
2031 if (init_data->regulator_init) {
2032 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002033 if (ret < 0)
2034 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002035 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002036
Liam Girdwooda5766f12008-10-10 13:22:20 +01002037 /* register with sysfs */
2038 rdev->dev.class = &regulator_class;
2039 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002040 dev_set_name(&rdev->dev, "regulator.%d",
2041 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002042 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002043 if (ret != 0)
2044 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002045
2046 dev_set_drvdata(&rdev->dev, rdev);
2047
Mike Rapoport74f544c2008-11-25 14:53:53 +02002048 /* set regulator constraints */
2049 ret = set_machine_constraints(rdev, &init_data->constraints);
2050 if (ret < 0)
2051 goto scrub;
2052
David Brownell7ad68e22008-11-11 17:39:02 -08002053 /* add attributes supported by this regulator */
2054 ret = add_regulator_attributes(rdev);
2055 if (ret < 0)
2056 goto scrub;
2057
Liam Girdwooda5766f12008-10-10 13:22:20 +01002058 /* set supply regulator if it exists */
2059 if (init_data->supply_regulator_dev) {
2060 ret = set_supply(rdev,
2061 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002062 if (ret < 0)
2063 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002064 }
2065
2066 /* add consumers devices */
2067 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2068 ret = set_consumer_device_supply(rdev,
2069 init_data->consumer_supplies[i].dev,
2070 init_data->consumer_supplies[i].supply);
2071 if (ret < 0) {
2072 for (--i; i >= 0; i--)
2073 unset_consumer_device_supply(rdev,
2074 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08002075 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002076 }
2077 }
2078
2079 list_add(&rdev->list, &regulator_list);
2080out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002081 mutex_unlock(&regulator_list_mutex);
2082 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002083
2084scrub:
2085 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002086 /* device core frees rdev */
2087 rdev = ERR_PTR(ret);
2088 goto out;
2089
David Brownell4fca9542008-11-11 17:38:53 -08002090clean:
2091 kfree(rdev);
2092 rdev = ERR_PTR(ret);
2093 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002094}
2095EXPORT_SYMBOL_GPL(regulator_register);
2096
2097/**
2098 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002099 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002100 *
2101 * Called by regulator drivers to unregister a regulator.
2102 */
2103void regulator_unregister(struct regulator_dev *rdev)
2104{
2105 if (rdev == NULL)
2106 return;
2107
2108 mutex_lock(&regulator_list_mutex);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002109 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002110 list_del(&rdev->list);
2111 if (rdev->supply)
2112 sysfs_remove_link(&rdev->dev.kobj, "supply");
2113 device_unregister(&rdev->dev);
2114 mutex_unlock(&regulator_list_mutex);
2115}
2116EXPORT_SYMBOL_GPL(regulator_unregister);
2117
2118/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002119 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002120 * @state: system suspend state
2121 *
2122 * Configure each regulator with it's suspend operating parameters for state.
2123 * This will usually be called by machine suspend code prior to supending.
2124 */
2125int regulator_suspend_prepare(suspend_state_t state)
2126{
2127 struct regulator_dev *rdev;
2128 int ret = 0;
2129
2130 /* ON is handled by regulator active state */
2131 if (state == PM_SUSPEND_ON)
2132 return -EINVAL;
2133
2134 mutex_lock(&regulator_list_mutex);
2135 list_for_each_entry(rdev, &regulator_list, list) {
2136
2137 mutex_lock(&rdev->mutex);
2138 ret = suspend_prepare(rdev, state);
2139 mutex_unlock(&rdev->mutex);
2140
2141 if (ret < 0) {
2142 printk(KERN_ERR "%s: failed to prepare %s\n",
2143 __func__, rdev->desc->name);
2144 goto out;
2145 }
2146 }
2147out:
2148 mutex_unlock(&regulator_list_mutex);
2149 return ret;
2150}
2151EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2152
2153/**
Mark Brownca725562009-03-16 19:36:34 +00002154 * regulator_has_full_constraints - the system has fully specified constraints
2155 *
2156 * Calling this function will cause the regulator API to disable all
2157 * regulators which have a zero use count and don't have an always_on
2158 * constraint in a late_initcall.
2159 *
2160 * The intention is that this will become the default behaviour in a
2161 * future kernel release so users are encouraged to use this facility
2162 * now.
2163 */
2164void regulator_has_full_constraints(void)
2165{
2166 has_full_constraints = 1;
2167}
2168EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2169
2170/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002171 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002172 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002173 *
2174 * Get rdev regulator driver private data. This call can be used in the
2175 * regulator driver context.
2176 */
2177void *rdev_get_drvdata(struct regulator_dev *rdev)
2178{
2179 return rdev->reg_data;
2180}
2181EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2182
2183/**
2184 * regulator_get_drvdata - get regulator driver data
2185 * @regulator: regulator
2186 *
2187 * Get regulator driver private data. This call can be used in the consumer
2188 * driver context when non API regulator specific functions need to be called.
2189 */
2190void *regulator_get_drvdata(struct regulator *regulator)
2191{
2192 return regulator->rdev->reg_data;
2193}
2194EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2195
2196/**
2197 * regulator_set_drvdata - set regulator driver data
2198 * @regulator: regulator
2199 * @data: data
2200 */
2201void regulator_set_drvdata(struct regulator *regulator, void *data)
2202{
2203 regulator->rdev->reg_data = data;
2204}
2205EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2206
2207/**
2208 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002209 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002210 */
2211int rdev_get_id(struct regulator_dev *rdev)
2212{
2213 return rdev->desc->id;
2214}
2215EXPORT_SYMBOL_GPL(rdev_get_id);
2216
Liam Girdwooda5766f12008-10-10 13:22:20 +01002217struct device *rdev_get_dev(struct regulator_dev *rdev)
2218{
2219 return &rdev->dev;
2220}
2221EXPORT_SYMBOL_GPL(rdev_get_dev);
2222
2223void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2224{
2225 return reg_init_data->driver_data;
2226}
2227EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2228
Liam Girdwood414c70c2008-04-30 15:59:04 +01002229static int __init regulator_init(void)
2230{
2231 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2232 return class_register(&regulator_class);
2233}
2234
2235/* init early to allow our consumers to complete system booting */
2236core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002237
2238static int __init regulator_init_complete(void)
2239{
2240 struct regulator_dev *rdev;
2241 struct regulator_ops *ops;
2242 struct regulation_constraints *c;
2243 int enabled, ret;
2244 const char *name;
2245
2246 mutex_lock(&regulator_list_mutex);
2247
2248 /* If we have a full configuration then disable any regulators
2249 * which are not in use or always_on. This will become the
2250 * default behaviour in the future.
2251 */
2252 list_for_each_entry(rdev, &regulator_list, list) {
2253 ops = rdev->desc->ops;
2254 c = rdev->constraints;
2255
2256 if (c->name)
2257 name = c->name;
2258 else if (rdev->desc->name)
2259 name = rdev->desc->name;
2260 else
2261 name = "regulator";
2262
2263 if (!ops->disable || c->always_on)
2264 continue;
2265
2266 mutex_lock(&rdev->mutex);
2267
2268 if (rdev->use_count)
2269 goto unlock;
2270
2271 /* If we can't read the status assume it's on. */
2272 if (ops->is_enabled)
2273 enabled = ops->is_enabled(rdev);
2274 else
2275 enabled = 1;
2276
2277 if (!enabled)
2278 goto unlock;
2279
2280 if (has_full_constraints) {
2281 /* We log since this may kill the system if it
2282 * goes wrong. */
2283 printk(KERN_INFO "%s: disabling %s\n",
2284 __func__, name);
2285 ret = ops->disable(rdev);
2286 if (ret != 0) {
2287 printk(KERN_ERR
2288 "%s: couldn't disable %s: %d\n",
2289 __func__, name, ret);
2290 }
2291 } else {
2292 /* The intention is that in future we will
2293 * assume that full constraints are provided
2294 * so warn even if we aren't going to do
2295 * anything here.
2296 */
2297 printk(KERN_WARNING
2298 "%s: incomplete constraints, leaving %s on\n",
2299 __func__, name);
2300 }
2301
2302unlock:
2303 mutex_unlock(&rdev->mutex);
2304 }
2305
2306 mutex_unlock(&regulator_list_mutex);
2307
2308 return 0;
2309}
2310late_initcall(regulator_init_complete);