blob: 01f7702a805dc462f29d59110c1c76bb9985c480 [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 ||
543 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
544 return;
545
546 /* get output voltage */
547 output_uV = rdev->desc->ops->get_voltage(rdev);
548 if (output_uV <= 0)
549 return;
550
551 /* get input voltage */
552 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
553 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
554 else
555 input_uV = rdev->constraints->input_uV;
556 if (input_uV <= 0)
557 return;
558
559 /* calc total requested load */
560 list_for_each_entry(sibling, &rdev->consumer_list, list)
561 current_uA += sibling->uA_load;
562
563 /* now get the optimum mode for our new total regulator load */
564 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
565 output_uV, current_uA);
566
567 /* check the new mode is allowed */
568 err = regulator_check_mode(rdev, mode);
569 if (err == 0)
570 rdev->desc->ops->set_mode(rdev, mode);
571}
572
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
706 /* it's safe to autoconfigure fixed-voltage supplies */
707 if (count == 1 && !cmin) {
708 cmin = INT_MIN;
709 cmax = INT_MAX;
710 }
711
Mark Brown3e2b9ab2009-03-10 16:28:36 +0000712 /* voltage constraints are optional */
713 if ((cmin == 0) && (cmax == 0))
714 goto out;
715
David Brownell4367cfd2009-02-26 11:48:36 -0800716 /* else require explicit machine-level constraints */
Mark Brown3e2b9ab2009-03-10 16:28:36 +0000717 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
David Brownell4367cfd2009-02-26 11:48:36 -0800718 pr_err("%s: %s '%s' voltage constraints\n",
719 __func__, "invalid", name);
720 ret = -EINVAL;
721 goto out;
722 }
723
724 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
725 for (i = 0; i < count; i++) {
726 int value;
727
728 value = ops->list_voltage(rdev, i);
729 if (value <= 0)
730 continue;
731
732 /* maybe adjust [min_uV..max_uV] */
733 if (value >= cmin && value < min_uV)
734 min_uV = value;
735 if (value <= cmax && value > max_uV)
736 max_uV = value;
737 }
738
739 /* final: [min_uV..max_uV] valid iff constraints valid */
740 if (max_uV < min_uV) {
741 pr_err("%s: %s '%s' voltage constraints\n",
742 __func__, "unsupportable", name);
743 ret = -EINVAL;
744 goto out;
745 }
746
747 /* use regulator's subset of machine constraints */
748 if (constraints->min_uV < min_uV) {
749 pr_debug("%s: override '%s' %s, %d -> %d\n",
750 __func__, name, "min_uV",
751 constraints->min_uV, min_uV);
752 constraints->min_uV = min_uV;
753 }
754 if (constraints->max_uV > max_uV) {
755 pr_debug("%s: override '%s' %s, %d -> %d\n",
756 __func__, name, "max_uV",
757 constraints->max_uV, max_uV);
758 constraints->max_uV = max_uV;
759 }
760 }
761
Liam Girdwooda5766f12008-10-10 13:22:20 +0100762 rdev->constraints = constraints;
763
764 /* do we need to apply the constraint voltage */
765 if (rdev->constraints->apply_uV &&
766 rdev->constraints->min_uV == rdev->constraints->max_uV &&
Mark Browne5fda262008-09-09 16:21:20 +0100767 ops->set_voltage) {
768 ret = ops->set_voltage(rdev,
Liam Girdwooda5766f12008-10-10 13:22:20 +0100769 rdev->constraints->min_uV, rdev->constraints->max_uV);
770 if (ret < 0) {
Mark Browne06f5b42008-09-09 16:21:19 +0100771 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
772 __func__,
773 rdev->constraints->min_uV, name);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100774 rdev->constraints = NULL;
775 goto out;
776 }
777 }
778
Liam Girdwooda5766f12008-10-10 13:22:20 +0100779 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100780 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100781 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100782 if (ret < 0) {
783 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
784 __func__, name);
785 rdev->constraints = NULL;
786 goto out;
787 }
788 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100789
Mark Browna3084662009-02-26 19:24:19 +0000790 if (constraints->initial_mode) {
791 if (!ops->set_mode) {
792 printk(KERN_ERR "%s: no set_mode operation for %s\n",
793 __func__, name);
794 ret = -EINVAL;
795 goto out;
796 }
797
798 ret = ops->set_mode(rdev, constraints->initial_mode);
799 if (ret < 0) {
800 printk(KERN_ERR
801 "%s: failed to set initial mode for %s: %d\n",
802 __func__, name, ret);
803 goto out;
804 }
805 }
806
Mark Browncacf90f2009-03-02 16:32:46 +0000807 /* If the constraints say the regulator should be on at this point
808 * and we have control then make sure it is enabled.
809 */
810 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100811 ret = ops->enable(rdev);
812 if (ret < 0) {
813 printk(KERN_ERR "%s: failed to enable %s\n",
814 __func__, name);
815 rdev->constraints = NULL;
816 goto out;
817 }
818 }
819
Liam Girdwooda5766f12008-10-10 13:22:20 +0100820 print_constraints(rdev);
821out:
822 return ret;
823}
824
825/**
826 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000827 * @rdev: regulator name
828 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100829 *
830 * Called by platform initialisation code to set the supply regulator for this
831 * regulator. This ensures that a regulators supply will also be enabled by the
832 * core if it's child is enabled.
833 */
834static int set_supply(struct regulator_dev *rdev,
835 struct regulator_dev *supply_rdev)
836{
837 int err;
838
839 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
840 "supply");
841 if (err) {
842 printk(KERN_ERR
843 "%s: could not add device link %s err %d\n",
844 __func__, supply_rdev->dev.kobj.name, err);
845 goto out;
846 }
847 rdev->supply = supply_rdev;
848 list_add(&rdev->slist, &supply_rdev->supply_list);
849out:
850 return err;
851}
852
853/**
854 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000855 * @rdev: regulator source
856 * @consumer_dev: device the supply applies to
857 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100858 *
859 * Allows platform initialisation code to map physical regulator
860 * sources to symbolic names for supplies for use by devices. Devices
861 * should use these symbolic names to request regulators, avoiding the
862 * need to provide board-specific regulator names as platform data.
863 */
864static int set_consumer_device_supply(struct regulator_dev *rdev,
865 struct device *consumer_dev, const char *supply)
866{
867 struct regulator_map *node;
868
869 if (supply == NULL)
870 return -EINVAL;
871
David Brownell6001e132008-12-31 12:54:19 +0000872 list_for_each_entry(node, &regulator_map_list, list) {
873 if (consumer_dev != node->dev)
874 continue;
875 if (strcmp(node->supply, supply) != 0)
876 continue;
877
878 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
879 dev_name(&node->regulator->dev),
880 node->regulator->desc->name,
881 supply,
882 dev_name(&rdev->dev), rdev->desc->name);
883 return -EBUSY;
884 }
885
Liam Girdwooda5766f12008-10-10 13:22:20 +0100886 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
887 if (node == NULL)
888 return -ENOMEM;
889
890 node->regulator = rdev;
891 node->dev = consumer_dev;
892 node->supply = supply;
893
894 list_add(&node->list, &regulator_map_list);
895 return 0;
896}
897
898static void unset_consumer_device_supply(struct regulator_dev *rdev,
899 struct device *consumer_dev)
900{
901 struct regulator_map *node, *n;
902
903 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
904 if (rdev == node->regulator &&
905 consumer_dev == node->dev) {
906 list_del(&node->list);
907 kfree(node);
908 return;
909 }
910 }
911}
912
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200913static void unset_regulator_supplies(struct regulator_dev *rdev)
914{
915 struct regulator_map *node, *n;
916
917 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
918 if (rdev == node->regulator) {
919 list_del(&node->list);
920 kfree(node);
921 return;
922 }
923 }
924}
925
Liam Girdwood414c70c2008-04-30 15:59:04 +0100926#define REG_STR_SIZE 32
927
928static struct regulator *create_regulator(struct regulator_dev *rdev,
929 struct device *dev,
930 const char *supply_name)
931{
932 struct regulator *regulator;
933 char buf[REG_STR_SIZE];
934 int err, size;
935
936 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
937 if (regulator == NULL)
938 return NULL;
939
940 mutex_lock(&rdev->mutex);
941 regulator->rdev = rdev;
942 list_add(&regulator->list, &rdev->consumer_list);
943
944 if (dev) {
945 /* create a 'requested_microamps_name' sysfs entry */
946 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
947 supply_name);
948 if (size >= REG_STR_SIZE)
949 goto overflow_err;
950
951 regulator->dev = dev;
952 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
953 if (regulator->dev_attr.attr.name == NULL)
954 goto attr_name_err;
955
956 regulator->dev_attr.attr.owner = THIS_MODULE;
957 regulator->dev_attr.attr.mode = 0444;
958 regulator->dev_attr.show = device_requested_uA_show;
959 err = device_create_file(dev, &regulator->dev_attr);
960 if (err < 0) {
961 printk(KERN_WARNING "%s: could not add regulator_dev"
962 " load sysfs\n", __func__);
963 goto attr_name_err;
964 }
965
966 /* also add a link to the device sysfs entry */
967 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
968 dev->kobj.name, supply_name);
969 if (size >= REG_STR_SIZE)
970 goto attr_err;
971
972 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
973 if (regulator->supply_name == NULL)
974 goto attr_err;
975
976 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
977 buf);
978 if (err) {
979 printk(KERN_WARNING
980 "%s: could not add device link %s err %d\n",
981 __func__, dev->kobj.name, err);
982 device_remove_file(dev, &regulator->dev_attr);
983 goto link_name_err;
984 }
985 }
986 mutex_unlock(&rdev->mutex);
987 return regulator;
988link_name_err:
989 kfree(regulator->supply_name);
990attr_err:
991 device_remove_file(regulator->dev, &regulator->dev_attr);
992attr_name_err:
993 kfree(regulator->dev_attr.attr.name);
994overflow_err:
995 list_del(&regulator->list);
996 kfree(regulator);
997 mutex_unlock(&rdev->mutex);
998 return NULL;
999}
1000
1001/**
1002 * regulator_get - lookup and obtain a reference to a regulator.
1003 * @dev: device for regulator "consumer"
1004 * @id: Supply name or regulator ID.
1005 *
1006 * Returns a struct regulator corresponding to the regulator producer,
Mark Brownfe203ddf2009-02-11 15:32:06 +00001007 * or IS_ERR() condition containing errno.
1008 *
1009 * Use of supply names configured via regulator_set_device_supply() is
1010 * strongly encouraged. It is recommended that the supply name used
1011 * should match the name used for the supply and/or the relevant
1012 * device pins in the datasheet.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001013 */
1014struct regulator *regulator_get(struct device *dev, const char *id)
1015{
1016 struct regulator_dev *rdev;
1017 struct regulator_map *map;
1018 struct regulator *regulator = ERR_PTR(-ENODEV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001019
1020 if (id == NULL) {
1021 printk(KERN_ERR "regulator: get() with no identifier\n");
1022 return regulator;
1023 }
1024
1025 mutex_lock(&regulator_list_mutex);
1026
1027 list_for_each_entry(map, &regulator_map_list, list) {
1028 if (dev == map->dev &&
1029 strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001030 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001031 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001032 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001033 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001034 mutex_unlock(&regulator_list_mutex);
1035 return regulator;
1036
1037found:
Liam Girdwooda5766f12008-10-10 13:22:20 +01001038 if (!try_module_get(rdev->owner))
1039 goto out;
1040
Liam Girdwood414c70c2008-04-30 15:59:04 +01001041 regulator = create_regulator(rdev, dev, id);
1042 if (regulator == NULL) {
1043 regulator = ERR_PTR(-ENOMEM);
1044 module_put(rdev->owner);
1045 }
1046
Liam Girdwooda5766f12008-10-10 13:22:20 +01001047out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001048 mutex_unlock(&regulator_list_mutex);
1049 return regulator;
1050}
1051EXPORT_SYMBOL_GPL(regulator_get);
1052
1053/**
1054 * regulator_put - "free" the regulator source
1055 * @regulator: regulator source
1056 *
1057 * Note: drivers must ensure that all regulator_enable calls made on this
1058 * regulator source are balanced by regulator_disable calls prior to calling
1059 * this function.
1060 */
1061void regulator_put(struct regulator *regulator)
1062{
1063 struct regulator_dev *rdev;
1064
1065 if (regulator == NULL || IS_ERR(regulator))
1066 return;
1067
Liam Girdwood414c70c2008-04-30 15:59:04 +01001068 mutex_lock(&regulator_list_mutex);
1069 rdev = regulator->rdev;
1070
1071 /* remove any sysfs entries */
1072 if (regulator->dev) {
1073 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1074 kfree(regulator->supply_name);
1075 device_remove_file(regulator->dev, &regulator->dev_attr);
1076 kfree(regulator->dev_attr.attr.name);
1077 }
1078 list_del(&regulator->list);
1079 kfree(regulator);
1080
1081 module_put(rdev->owner);
1082 mutex_unlock(&regulator_list_mutex);
1083}
1084EXPORT_SYMBOL_GPL(regulator_put);
1085
1086/* locks held by regulator_enable() */
1087static int _regulator_enable(struct regulator_dev *rdev)
1088{
1089 int ret = -EINVAL;
1090
1091 if (!rdev->constraints) {
1092 printk(KERN_ERR "%s: %s has no constraints\n",
1093 __func__, rdev->desc->name);
1094 return ret;
1095 }
1096
1097 /* do we need to enable the supply regulator first */
1098 if (rdev->supply) {
1099 ret = _regulator_enable(rdev->supply);
1100 if (ret < 0) {
1101 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1102 __func__, rdev->desc->name, ret);
1103 return ret;
1104 }
1105 }
1106
1107 /* check voltage and requested load before enabling */
1108 if (rdev->desc->ops->enable) {
1109
1110 if (rdev->constraints &&
1111 (rdev->constraints->valid_ops_mask &
1112 REGULATOR_CHANGE_DRMS))
1113 drms_uA_update(rdev);
1114
1115 ret = rdev->desc->ops->enable(rdev);
1116 if (ret < 0) {
1117 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1118 __func__, rdev->desc->name, ret);
1119 return ret;
1120 }
1121 rdev->use_count++;
1122 return ret;
1123 }
1124
1125 return ret;
1126}
1127
1128/**
1129 * regulator_enable - enable regulator output
1130 * @regulator: regulator source
1131 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001132 * Request that the regulator be enabled with the regulator output at
1133 * the predefined voltage or current value. Calls to regulator_enable()
1134 * must be balanced with calls to regulator_disable().
1135 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001136 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001137 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001138 */
1139int regulator_enable(struct regulator *regulator)
1140{
David Brownell412aec62008-11-16 11:44:46 -08001141 struct regulator_dev *rdev = regulator->rdev;
1142 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001143
David Brownell412aec62008-11-16 11:44:46 -08001144 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001145 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001146 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001147 return ret;
1148}
1149EXPORT_SYMBOL_GPL(regulator_enable);
1150
1151/* locks held by regulator_disable() */
1152static int _regulator_disable(struct regulator_dev *rdev)
1153{
1154 int ret = 0;
1155
David Brownellcd94b502009-03-11 16:43:34 -08001156 if (WARN(rdev->use_count <= 0,
1157 "unbalanced disables for %s\n",
1158 rdev->desc->name))
1159 return -EIO;
1160
Liam Girdwood414c70c2008-04-30 15:59:04 +01001161 /* are we the last user and permitted to disable ? */
1162 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1163
1164 /* we are last user */
1165 if (rdev->desc->ops->disable) {
1166 ret = rdev->desc->ops->disable(rdev);
1167 if (ret < 0) {
1168 printk(KERN_ERR "%s: failed to disable %s\n",
1169 __func__, rdev->desc->name);
1170 return ret;
1171 }
1172 }
1173
1174 /* decrease our supplies ref count and disable if required */
1175 if (rdev->supply)
1176 _regulator_disable(rdev->supply);
1177
1178 rdev->use_count = 0;
1179 } else if (rdev->use_count > 1) {
1180
1181 if (rdev->constraints &&
1182 (rdev->constraints->valid_ops_mask &
1183 REGULATOR_CHANGE_DRMS))
1184 drms_uA_update(rdev);
1185
1186 rdev->use_count--;
1187 }
1188 return ret;
1189}
1190
1191/**
1192 * regulator_disable - disable regulator output
1193 * @regulator: regulator source
1194 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001195 * Disable the regulator output voltage or current. Calls to
1196 * regulator_enable() must be balanced with calls to
1197 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001198 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001199 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001200 * devices have it enabled, the regulator device supports disabling and
1201 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001202 */
1203int regulator_disable(struct regulator *regulator)
1204{
David Brownell412aec62008-11-16 11:44:46 -08001205 struct regulator_dev *rdev = regulator->rdev;
1206 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001207
David Brownell412aec62008-11-16 11:44:46 -08001208 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001209 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001210 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001211 return ret;
1212}
1213EXPORT_SYMBOL_GPL(regulator_disable);
1214
1215/* locks held by regulator_force_disable() */
1216static int _regulator_force_disable(struct regulator_dev *rdev)
1217{
1218 int ret = 0;
1219
1220 /* force disable */
1221 if (rdev->desc->ops->disable) {
1222 /* ah well, who wants to live forever... */
1223 ret = rdev->desc->ops->disable(rdev);
1224 if (ret < 0) {
1225 printk(KERN_ERR "%s: failed to force disable %s\n",
1226 __func__, rdev->desc->name);
1227 return ret;
1228 }
1229 /* notify other consumers that power has been forced off */
1230 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1231 NULL);
1232 }
1233
1234 /* decrease our supplies ref count and disable if required */
1235 if (rdev->supply)
1236 _regulator_disable(rdev->supply);
1237
1238 rdev->use_count = 0;
1239 return ret;
1240}
1241
1242/**
1243 * regulator_force_disable - force disable regulator output
1244 * @regulator: regulator source
1245 *
1246 * Forcibly disable the regulator output voltage or current.
1247 * NOTE: this *will* disable the regulator output even if other consumer
1248 * devices have it enabled. This should be used for situations when device
1249 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1250 */
1251int regulator_force_disable(struct regulator *regulator)
1252{
1253 int ret;
1254
1255 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001256 regulator->uA_load = 0;
1257 ret = _regulator_force_disable(regulator->rdev);
1258 mutex_unlock(&regulator->rdev->mutex);
1259 return ret;
1260}
1261EXPORT_SYMBOL_GPL(regulator_force_disable);
1262
1263static int _regulator_is_enabled(struct regulator_dev *rdev)
1264{
1265 int ret;
1266
1267 mutex_lock(&rdev->mutex);
1268
1269 /* sanity check */
1270 if (!rdev->desc->ops->is_enabled) {
1271 ret = -EINVAL;
1272 goto out;
1273 }
1274
1275 ret = rdev->desc->ops->is_enabled(rdev);
1276out:
1277 mutex_unlock(&rdev->mutex);
1278 return ret;
1279}
1280
1281/**
1282 * regulator_is_enabled - is the regulator output enabled
1283 * @regulator: regulator source
1284 *
David Brownell412aec62008-11-16 11:44:46 -08001285 * Returns positive if the regulator driver backing the source/client
1286 * has requested that the device be enabled, zero if it hasn't, else a
1287 * negative errno code.
1288 *
1289 * Note that the device backing this regulator handle can have multiple
1290 * users, so it might be enabled even if regulator_enable() was never
1291 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001292 */
1293int regulator_is_enabled(struct regulator *regulator)
1294{
1295 return _regulator_is_enabled(regulator->rdev);
1296}
1297EXPORT_SYMBOL_GPL(regulator_is_enabled);
1298
1299/**
David Brownell4367cfd2009-02-26 11:48:36 -08001300 * regulator_count_voltages - count regulator_list_voltage() selectors
1301 * @regulator: regulator source
1302 *
1303 * Returns number of selectors, or negative errno. Selectors are
1304 * numbered starting at zero, and typically correspond to bitfields
1305 * in hardware registers.
1306 */
1307int regulator_count_voltages(struct regulator *regulator)
1308{
1309 struct regulator_dev *rdev = regulator->rdev;
1310
1311 return rdev->desc->n_voltages ? : -EINVAL;
1312}
1313EXPORT_SYMBOL_GPL(regulator_count_voltages);
1314
1315/**
1316 * regulator_list_voltage - enumerate supported voltages
1317 * @regulator: regulator source
1318 * @selector: identify voltage to list
1319 * Context: can sleep
1320 *
1321 * Returns a voltage that can be passed to @regulator_set_voltage(),
1322 * zero if this selector code can't be used on this sytem, or a
1323 * negative errno.
1324 */
1325int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1326{
1327 struct regulator_dev *rdev = regulator->rdev;
1328 struct regulator_ops *ops = rdev->desc->ops;
1329 int ret;
1330
1331 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1332 return -EINVAL;
1333
1334 mutex_lock(&rdev->mutex);
1335 ret = ops->list_voltage(rdev, selector);
1336 mutex_unlock(&rdev->mutex);
1337
1338 if (ret > 0) {
1339 if (ret < rdev->constraints->min_uV)
1340 ret = 0;
1341 else if (ret > rdev->constraints->max_uV)
1342 ret = 0;
1343 }
1344
1345 return ret;
1346}
1347EXPORT_SYMBOL_GPL(regulator_list_voltage);
1348
1349/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001350 * regulator_set_voltage - set regulator output voltage
1351 * @regulator: regulator source
1352 * @min_uV: Minimum required voltage in uV
1353 * @max_uV: Maximum acceptable voltage in uV
1354 *
1355 * Sets a voltage regulator to the desired output voltage. This can be set
1356 * during any regulator state. IOW, regulator can be disabled or enabled.
1357 *
1358 * If the regulator is enabled then the voltage will change to the new value
1359 * immediately otherwise if the regulator is disabled the regulator will
1360 * output at the new voltage when enabled.
1361 *
1362 * NOTE: If the regulator is shared between several devices then the lowest
1363 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001364 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001365 * calling this function otherwise this call will fail.
1366 */
1367int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1368{
1369 struct regulator_dev *rdev = regulator->rdev;
1370 int ret;
1371
1372 mutex_lock(&rdev->mutex);
1373
1374 /* sanity check */
1375 if (!rdev->desc->ops->set_voltage) {
1376 ret = -EINVAL;
1377 goto out;
1378 }
1379
1380 /* constraints check */
1381 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1382 if (ret < 0)
1383 goto out;
1384 regulator->min_uV = min_uV;
1385 regulator->max_uV = max_uV;
1386 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1387
1388out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001389 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001390 mutex_unlock(&rdev->mutex);
1391 return ret;
1392}
1393EXPORT_SYMBOL_GPL(regulator_set_voltage);
1394
1395static int _regulator_get_voltage(struct regulator_dev *rdev)
1396{
1397 /* sanity check */
1398 if (rdev->desc->ops->get_voltage)
1399 return rdev->desc->ops->get_voltage(rdev);
1400 else
1401 return -EINVAL;
1402}
1403
1404/**
1405 * regulator_get_voltage - get regulator output voltage
1406 * @regulator: regulator source
1407 *
1408 * This returns the current regulator voltage in uV.
1409 *
1410 * NOTE: If the regulator is disabled it will return the voltage value. This
1411 * function should not be used to determine regulator state.
1412 */
1413int regulator_get_voltage(struct regulator *regulator)
1414{
1415 int ret;
1416
1417 mutex_lock(&regulator->rdev->mutex);
1418
1419 ret = _regulator_get_voltage(regulator->rdev);
1420
1421 mutex_unlock(&regulator->rdev->mutex);
1422
1423 return ret;
1424}
1425EXPORT_SYMBOL_GPL(regulator_get_voltage);
1426
1427/**
1428 * regulator_set_current_limit - set regulator output current limit
1429 * @regulator: regulator source
1430 * @min_uA: Minimuum supported current in uA
1431 * @max_uA: Maximum supported current in uA
1432 *
1433 * Sets current sink to the desired output current. This can be set during
1434 * any regulator state. IOW, regulator can be disabled or enabled.
1435 *
1436 * If the regulator is enabled then the current will change to the new value
1437 * immediately otherwise if the regulator is disabled the regulator will
1438 * output at the new current when enabled.
1439 *
1440 * NOTE: Regulator system constraints must be set for this regulator before
1441 * calling this function otherwise this call will fail.
1442 */
1443int regulator_set_current_limit(struct regulator *regulator,
1444 int min_uA, int max_uA)
1445{
1446 struct regulator_dev *rdev = regulator->rdev;
1447 int ret;
1448
1449 mutex_lock(&rdev->mutex);
1450
1451 /* sanity check */
1452 if (!rdev->desc->ops->set_current_limit) {
1453 ret = -EINVAL;
1454 goto out;
1455 }
1456
1457 /* constraints check */
1458 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1459 if (ret < 0)
1460 goto out;
1461
1462 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1463out:
1464 mutex_unlock(&rdev->mutex);
1465 return ret;
1466}
1467EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1468
1469static int _regulator_get_current_limit(struct regulator_dev *rdev)
1470{
1471 int ret;
1472
1473 mutex_lock(&rdev->mutex);
1474
1475 /* sanity check */
1476 if (!rdev->desc->ops->get_current_limit) {
1477 ret = -EINVAL;
1478 goto out;
1479 }
1480
1481 ret = rdev->desc->ops->get_current_limit(rdev);
1482out:
1483 mutex_unlock(&rdev->mutex);
1484 return ret;
1485}
1486
1487/**
1488 * regulator_get_current_limit - get regulator output current
1489 * @regulator: regulator source
1490 *
1491 * This returns the current supplied by the specified current sink in uA.
1492 *
1493 * NOTE: If the regulator is disabled it will return the current value. This
1494 * function should not be used to determine regulator state.
1495 */
1496int regulator_get_current_limit(struct regulator *regulator)
1497{
1498 return _regulator_get_current_limit(regulator->rdev);
1499}
1500EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1501
1502/**
1503 * regulator_set_mode - set regulator operating mode
1504 * @regulator: regulator source
1505 * @mode: operating mode - one of the REGULATOR_MODE constants
1506 *
1507 * Set regulator operating mode to increase regulator efficiency or improve
1508 * regulation performance.
1509 *
1510 * NOTE: Regulator system constraints must be set for this regulator before
1511 * calling this function otherwise this call will fail.
1512 */
1513int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1514{
1515 struct regulator_dev *rdev = regulator->rdev;
1516 int ret;
1517
1518 mutex_lock(&rdev->mutex);
1519
1520 /* sanity check */
1521 if (!rdev->desc->ops->set_mode) {
1522 ret = -EINVAL;
1523 goto out;
1524 }
1525
1526 /* constraints check */
1527 ret = regulator_check_mode(rdev, mode);
1528 if (ret < 0)
1529 goto out;
1530
1531 ret = rdev->desc->ops->set_mode(rdev, mode);
1532out:
1533 mutex_unlock(&rdev->mutex);
1534 return ret;
1535}
1536EXPORT_SYMBOL_GPL(regulator_set_mode);
1537
1538static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1539{
1540 int ret;
1541
1542 mutex_lock(&rdev->mutex);
1543
1544 /* sanity check */
1545 if (!rdev->desc->ops->get_mode) {
1546 ret = -EINVAL;
1547 goto out;
1548 }
1549
1550 ret = rdev->desc->ops->get_mode(rdev);
1551out:
1552 mutex_unlock(&rdev->mutex);
1553 return ret;
1554}
1555
1556/**
1557 * regulator_get_mode - get regulator operating mode
1558 * @regulator: regulator source
1559 *
1560 * Get the current regulator operating mode.
1561 */
1562unsigned int regulator_get_mode(struct regulator *regulator)
1563{
1564 return _regulator_get_mode(regulator->rdev);
1565}
1566EXPORT_SYMBOL_GPL(regulator_get_mode);
1567
1568/**
1569 * regulator_set_optimum_mode - set regulator optimum operating mode
1570 * @regulator: regulator source
1571 * @uA_load: load current
1572 *
1573 * Notifies the regulator core of a new device load. This is then used by
1574 * DRMS (if enabled by constraints) to set the most efficient regulator
1575 * operating mode for the new regulator loading.
1576 *
1577 * Consumer devices notify their supply regulator of the maximum power
1578 * they will require (can be taken from device datasheet in the power
1579 * consumption tables) when they change operational status and hence power
1580 * state. Examples of operational state changes that can affect power
1581 * consumption are :-
1582 *
1583 * o Device is opened / closed.
1584 * o Device I/O is about to begin or has just finished.
1585 * o Device is idling in between work.
1586 *
1587 * This information is also exported via sysfs to userspace.
1588 *
1589 * DRMS will sum the total requested load on the regulator and change
1590 * to the most efficient operating mode if platform constraints allow.
1591 *
1592 * Returns the new regulator mode or error.
1593 */
1594int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1595{
1596 struct regulator_dev *rdev = regulator->rdev;
1597 struct regulator *consumer;
1598 int ret, output_uV, input_uV, total_uA_load = 0;
1599 unsigned int mode;
1600
1601 mutex_lock(&rdev->mutex);
1602
1603 regulator->uA_load = uA_load;
1604 ret = regulator_check_drms(rdev);
1605 if (ret < 0)
1606 goto out;
1607 ret = -EINVAL;
1608
1609 /* sanity check */
1610 if (!rdev->desc->ops->get_optimum_mode)
1611 goto out;
1612
1613 /* get output voltage */
1614 output_uV = rdev->desc->ops->get_voltage(rdev);
1615 if (output_uV <= 0) {
1616 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1617 __func__, rdev->desc->name);
1618 goto out;
1619 }
1620
1621 /* get input voltage */
1622 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1623 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1624 else
1625 input_uV = rdev->constraints->input_uV;
1626 if (input_uV <= 0) {
1627 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1628 __func__, rdev->desc->name);
1629 goto out;
1630 }
1631
1632 /* calc total requested load for this regulator */
1633 list_for_each_entry(consumer, &rdev->consumer_list, list)
1634 total_uA_load += consumer->uA_load;
1635
1636 mode = rdev->desc->ops->get_optimum_mode(rdev,
1637 input_uV, output_uV,
1638 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001639 ret = regulator_check_mode(rdev, mode);
1640 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001641 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1642 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1643 total_uA_load, input_uV, output_uV);
1644 goto out;
1645 }
1646
1647 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001648 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001649 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1650 __func__, mode, rdev->desc->name);
1651 goto out;
1652 }
1653 ret = mode;
1654out:
1655 mutex_unlock(&rdev->mutex);
1656 return ret;
1657}
1658EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1659
1660/**
1661 * regulator_register_notifier - register regulator event notifier
1662 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001663 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001664 *
1665 * Register notifier block to receive regulator events.
1666 */
1667int regulator_register_notifier(struct regulator *regulator,
1668 struct notifier_block *nb)
1669{
1670 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1671 nb);
1672}
1673EXPORT_SYMBOL_GPL(regulator_register_notifier);
1674
1675/**
1676 * regulator_unregister_notifier - unregister regulator event notifier
1677 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001678 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001679 *
1680 * Unregister regulator event notifier block.
1681 */
1682int regulator_unregister_notifier(struct regulator *regulator,
1683 struct notifier_block *nb)
1684{
1685 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1686 nb);
1687}
1688EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1689
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001690/* notify regulator consumers and downstream regulator consumers.
1691 * Note mutex must be held by caller.
1692 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001693static void _notifier_call_chain(struct regulator_dev *rdev,
1694 unsigned long event, void *data)
1695{
1696 struct regulator_dev *_rdev;
1697
1698 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001699 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001700
1701 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001702 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1703 mutex_lock(&_rdev->mutex);
1704 _notifier_call_chain(_rdev, event, data);
1705 mutex_unlock(&_rdev->mutex);
1706 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001707}
1708
1709/**
1710 * regulator_bulk_get - get multiple regulator consumers
1711 *
1712 * @dev: Device to supply
1713 * @num_consumers: Number of consumers to register
1714 * @consumers: Configuration of consumers; clients are stored here.
1715 *
1716 * @return 0 on success, an errno on failure.
1717 *
1718 * This helper function allows drivers to get several regulator
1719 * consumers in one operation. If any of the regulators cannot be
1720 * acquired then any regulators that were allocated will be freed
1721 * before returning to the caller.
1722 */
1723int regulator_bulk_get(struct device *dev, int num_consumers,
1724 struct regulator_bulk_data *consumers)
1725{
1726 int i;
1727 int ret;
1728
1729 for (i = 0; i < num_consumers; i++)
1730 consumers[i].consumer = NULL;
1731
1732 for (i = 0; i < num_consumers; i++) {
1733 consumers[i].consumer = regulator_get(dev,
1734 consumers[i].supply);
1735 if (IS_ERR(consumers[i].consumer)) {
1736 dev_err(dev, "Failed to get supply '%s'\n",
1737 consumers[i].supply);
1738 ret = PTR_ERR(consumers[i].consumer);
1739 consumers[i].consumer = NULL;
1740 goto err;
1741 }
1742 }
1743
1744 return 0;
1745
1746err:
1747 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1748 regulator_put(consumers[i].consumer);
1749
1750 return ret;
1751}
1752EXPORT_SYMBOL_GPL(regulator_bulk_get);
1753
1754/**
1755 * regulator_bulk_enable - enable multiple regulator consumers
1756 *
1757 * @num_consumers: Number of consumers
1758 * @consumers: Consumer data; clients are stored here.
1759 * @return 0 on success, an errno on failure
1760 *
1761 * This convenience API allows consumers to enable multiple regulator
1762 * clients in a single API call. If any consumers cannot be enabled
1763 * then any others that were enabled will be disabled again prior to
1764 * return.
1765 */
1766int regulator_bulk_enable(int num_consumers,
1767 struct regulator_bulk_data *consumers)
1768{
1769 int i;
1770 int ret;
1771
1772 for (i = 0; i < num_consumers; i++) {
1773 ret = regulator_enable(consumers[i].consumer);
1774 if (ret != 0)
1775 goto err;
1776 }
1777
1778 return 0;
1779
1780err:
1781 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1782 for (i = 0; i < num_consumers; i++)
1783 regulator_disable(consumers[i].consumer);
1784
1785 return ret;
1786}
1787EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1788
1789/**
1790 * regulator_bulk_disable - disable multiple regulator consumers
1791 *
1792 * @num_consumers: Number of consumers
1793 * @consumers: Consumer data; clients are stored here.
1794 * @return 0 on success, an errno on failure
1795 *
1796 * This convenience API allows consumers to disable multiple regulator
1797 * clients in a single API call. If any consumers cannot be enabled
1798 * then any others that were disabled will be disabled again prior to
1799 * return.
1800 */
1801int regulator_bulk_disable(int num_consumers,
1802 struct regulator_bulk_data *consumers)
1803{
1804 int i;
1805 int ret;
1806
1807 for (i = 0; i < num_consumers; i++) {
1808 ret = regulator_disable(consumers[i].consumer);
1809 if (ret != 0)
1810 goto err;
1811 }
1812
1813 return 0;
1814
1815err:
1816 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1817 for (i = 0; i < num_consumers; i++)
1818 regulator_enable(consumers[i].consumer);
1819
1820 return ret;
1821}
1822EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1823
1824/**
1825 * regulator_bulk_free - free multiple regulator consumers
1826 *
1827 * @num_consumers: Number of consumers
1828 * @consumers: Consumer data; clients are stored here.
1829 *
1830 * This convenience API allows consumers to free multiple regulator
1831 * clients in a single API call.
1832 */
1833void regulator_bulk_free(int num_consumers,
1834 struct regulator_bulk_data *consumers)
1835{
1836 int i;
1837
1838 for (i = 0; i < num_consumers; i++) {
1839 regulator_put(consumers[i].consumer);
1840 consumers[i].consumer = NULL;
1841 }
1842}
1843EXPORT_SYMBOL_GPL(regulator_bulk_free);
1844
1845/**
1846 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00001847 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01001848 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00001849 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001850 *
1851 * Called by regulator drivers to notify clients a regulator event has
1852 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001853 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001854 */
1855int regulator_notifier_call_chain(struct regulator_dev *rdev,
1856 unsigned long event, void *data)
1857{
1858 _notifier_call_chain(rdev, event, data);
1859 return NOTIFY_DONE;
1860
1861}
1862EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1863
David Brownell7ad68e22008-11-11 17:39:02 -08001864/*
1865 * To avoid cluttering sysfs (and memory) with useless state, only
1866 * create attributes that can be meaningfully displayed.
1867 */
1868static int add_regulator_attributes(struct regulator_dev *rdev)
1869{
1870 struct device *dev = &rdev->dev;
1871 struct regulator_ops *ops = rdev->desc->ops;
1872 int status = 0;
1873
1874 /* some attributes need specific methods to be displayed */
1875 if (ops->get_voltage) {
1876 status = device_create_file(dev, &dev_attr_microvolts);
1877 if (status < 0)
1878 return status;
1879 }
1880 if (ops->get_current_limit) {
1881 status = device_create_file(dev, &dev_attr_microamps);
1882 if (status < 0)
1883 return status;
1884 }
1885 if (ops->get_mode) {
1886 status = device_create_file(dev, &dev_attr_opmode);
1887 if (status < 0)
1888 return status;
1889 }
1890 if (ops->is_enabled) {
1891 status = device_create_file(dev, &dev_attr_state);
1892 if (status < 0)
1893 return status;
1894 }
David Brownell853116a2009-01-14 23:03:17 -08001895 if (ops->get_status) {
1896 status = device_create_file(dev, &dev_attr_status);
1897 if (status < 0)
1898 return status;
1899 }
David Brownell7ad68e22008-11-11 17:39:02 -08001900
1901 /* some attributes are type-specific */
1902 if (rdev->desc->type == REGULATOR_CURRENT) {
1903 status = device_create_file(dev, &dev_attr_requested_microamps);
1904 if (status < 0)
1905 return status;
1906 }
1907
1908 /* all the other attributes exist to support constraints;
1909 * don't show them if there are no constraints, or if the
1910 * relevant supporting methods are missing.
1911 */
1912 if (!rdev->constraints)
1913 return status;
1914
1915 /* constraints need specific supporting methods */
1916 if (ops->set_voltage) {
1917 status = device_create_file(dev, &dev_attr_min_microvolts);
1918 if (status < 0)
1919 return status;
1920 status = device_create_file(dev, &dev_attr_max_microvolts);
1921 if (status < 0)
1922 return status;
1923 }
1924 if (ops->set_current_limit) {
1925 status = device_create_file(dev, &dev_attr_min_microamps);
1926 if (status < 0)
1927 return status;
1928 status = device_create_file(dev, &dev_attr_max_microamps);
1929 if (status < 0)
1930 return status;
1931 }
1932
1933 /* suspend mode constraints need multiple supporting methods */
1934 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1935 return status;
1936
1937 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1938 if (status < 0)
1939 return status;
1940 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1941 if (status < 0)
1942 return status;
1943 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1944 if (status < 0)
1945 return status;
1946
1947 if (ops->set_suspend_voltage) {
1948 status = device_create_file(dev,
1949 &dev_attr_suspend_standby_microvolts);
1950 if (status < 0)
1951 return status;
1952 status = device_create_file(dev,
1953 &dev_attr_suspend_mem_microvolts);
1954 if (status < 0)
1955 return status;
1956 status = device_create_file(dev,
1957 &dev_attr_suspend_disk_microvolts);
1958 if (status < 0)
1959 return status;
1960 }
1961
1962 if (ops->set_suspend_mode) {
1963 status = device_create_file(dev,
1964 &dev_attr_suspend_standby_mode);
1965 if (status < 0)
1966 return status;
1967 status = device_create_file(dev,
1968 &dev_attr_suspend_mem_mode);
1969 if (status < 0)
1970 return status;
1971 status = device_create_file(dev,
1972 &dev_attr_suspend_disk_mode);
1973 if (status < 0)
1974 return status;
1975 }
1976
1977 return status;
1978}
1979
Liam Girdwood414c70c2008-04-30 15:59:04 +01001980/**
1981 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001982 * @regulator_desc: regulator to register
1983 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00001984 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00001985 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01001986 *
1987 * Called by regulator drivers to register a regulator.
1988 * Returns 0 on success.
1989 */
1990struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00001991 struct device *dev, struct regulator_init_data *init_data,
1992 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001993{
1994 static atomic_t regulator_no = ATOMIC_INIT(0);
1995 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001996 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001997
1998 if (regulator_desc == NULL)
1999 return ERR_PTR(-EINVAL);
2000
2001 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2002 return ERR_PTR(-EINVAL);
2003
2004 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
2005 !regulator_desc->type == REGULATOR_CURRENT)
2006 return ERR_PTR(-EINVAL);
2007
Mark Brown46fabe1e2008-09-09 16:21:18 +01002008 if (!init_data)
2009 return ERR_PTR(-EINVAL);
2010
Liam Girdwood414c70c2008-04-30 15:59:04 +01002011 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2012 if (rdev == NULL)
2013 return ERR_PTR(-ENOMEM);
2014
2015 mutex_lock(&regulator_list_mutex);
2016
2017 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002018 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002019 rdev->owner = regulator_desc->owner;
2020 rdev->desc = regulator_desc;
2021 INIT_LIST_HEAD(&rdev->consumer_list);
2022 INIT_LIST_HEAD(&rdev->supply_list);
2023 INIT_LIST_HEAD(&rdev->list);
2024 INIT_LIST_HEAD(&rdev->slist);
2025 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2026
Liam Girdwooda5766f12008-10-10 13:22:20 +01002027 /* preform any regulator specific init */
2028 if (init_data->regulator_init) {
2029 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002030 if (ret < 0)
2031 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002032 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002033
Liam Girdwooda5766f12008-10-10 13:22:20 +01002034 /* register with sysfs */
2035 rdev->dev.class = &regulator_class;
2036 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002037 dev_set_name(&rdev->dev, "regulator.%d",
2038 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002039 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002040 if (ret != 0)
2041 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002042
2043 dev_set_drvdata(&rdev->dev, rdev);
2044
Mike Rapoport74f544c2008-11-25 14:53:53 +02002045 /* set regulator constraints */
2046 ret = set_machine_constraints(rdev, &init_data->constraints);
2047 if (ret < 0)
2048 goto scrub;
2049
David Brownell7ad68e22008-11-11 17:39:02 -08002050 /* add attributes supported by this regulator */
2051 ret = add_regulator_attributes(rdev);
2052 if (ret < 0)
2053 goto scrub;
2054
Liam Girdwooda5766f12008-10-10 13:22:20 +01002055 /* set supply regulator if it exists */
2056 if (init_data->supply_regulator_dev) {
2057 ret = set_supply(rdev,
2058 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002059 if (ret < 0)
2060 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002061 }
2062
2063 /* add consumers devices */
2064 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2065 ret = set_consumer_device_supply(rdev,
2066 init_data->consumer_supplies[i].dev,
2067 init_data->consumer_supplies[i].supply);
2068 if (ret < 0) {
2069 for (--i; i >= 0; i--)
2070 unset_consumer_device_supply(rdev,
2071 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08002072 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002073 }
2074 }
2075
2076 list_add(&rdev->list, &regulator_list);
2077out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002078 mutex_unlock(&regulator_list_mutex);
2079 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002080
2081scrub:
2082 device_unregister(&rdev->dev);
2083clean:
2084 kfree(rdev);
2085 rdev = ERR_PTR(ret);
2086 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002087}
2088EXPORT_SYMBOL_GPL(regulator_register);
2089
2090/**
2091 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002092 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002093 *
2094 * Called by regulator drivers to unregister a regulator.
2095 */
2096void regulator_unregister(struct regulator_dev *rdev)
2097{
2098 if (rdev == NULL)
2099 return;
2100
2101 mutex_lock(&regulator_list_mutex);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002102 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002103 list_del(&rdev->list);
2104 if (rdev->supply)
2105 sysfs_remove_link(&rdev->dev.kobj, "supply");
2106 device_unregister(&rdev->dev);
2107 mutex_unlock(&regulator_list_mutex);
2108}
2109EXPORT_SYMBOL_GPL(regulator_unregister);
2110
2111/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002112 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002113 * @state: system suspend state
2114 *
2115 * Configure each regulator with it's suspend operating parameters for state.
2116 * This will usually be called by machine suspend code prior to supending.
2117 */
2118int regulator_suspend_prepare(suspend_state_t state)
2119{
2120 struct regulator_dev *rdev;
2121 int ret = 0;
2122
2123 /* ON is handled by regulator active state */
2124 if (state == PM_SUSPEND_ON)
2125 return -EINVAL;
2126
2127 mutex_lock(&regulator_list_mutex);
2128 list_for_each_entry(rdev, &regulator_list, list) {
2129
2130 mutex_lock(&rdev->mutex);
2131 ret = suspend_prepare(rdev, state);
2132 mutex_unlock(&rdev->mutex);
2133
2134 if (ret < 0) {
2135 printk(KERN_ERR "%s: failed to prepare %s\n",
2136 __func__, rdev->desc->name);
2137 goto out;
2138 }
2139 }
2140out:
2141 mutex_unlock(&regulator_list_mutex);
2142 return ret;
2143}
2144EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2145
2146/**
Mark Brownca725562009-03-16 19:36:34 +00002147 * regulator_has_full_constraints - the system has fully specified constraints
2148 *
2149 * Calling this function will cause the regulator API to disable all
2150 * regulators which have a zero use count and don't have an always_on
2151 * constraint in a late_initcall.
2152 *
2153 * The intention is that this will become the default behaviour in a
2154 * future kernel release so users are encouraged to use this facility
2155 * now.
2156 */
2157void regulator_has_full_constraints(void)
2158{
2159 has_full_constraints = 1;
2160}
2161EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2162
2163/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002164 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002165 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002166 *
2167 * Get rdev regulator driver private data. This call can be used in the
2168 * regulator driver context.
2169 */
2170void *rdev_get_drvdata(struct regulator_dev *rdev)
2171{
2172 return rdev->reg_data;
2173}
2174EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2175
2176/**
2177 * regulator_get_drvdata - get regulator driver data
2178 * @regulator: regulator
2179 *
2180 * Get regulator driver private data. This call can be used in the consumer
2181 * driver context when non API regulator specific functions need to be called.
2182 */
2183void *regulator_get_drvdata(struct regulator *regulator)
2184{
2185 return regulator->rdev->reg_data;
2186}
2187EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2188
2189/**
2190 * regulator_set_drvdata - set regulator driver data
2191 * @regulator: regulator
2192 * @data: data
2193 */
2194void regulator_set_drvdata(struct regulator *regulator, void *data)
2195{
2196 regulator->rdev->reg_data = data;
2197}
2198EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2199
2200/**
2201 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002202 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002203 */
2204int rdev_get_id(struct regulator_dev *rdev)
2205{
2206 return rdev->desc->id;
2207}
2208EXPORT_SYMBOL_GPL(rdev_get_id);
2209
Liam Girdwooda5766f12008-10-10 13:22:20 +01002210struct device *rdev_get_dev(struct regulator_dev *rdev)
2211{
2212 return &rdev->dev;
2213}
2214EXPORT_SYMBOL_GPL(rdev_get_dev);
2215
2216void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2217{
2218 return reg_init_data->driver_data;
2219}
2220EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2221
Liam Girdwood414c70c2008-04-30 15:59:04 +01002222static int __init regulator_init(void)
2223{
2224 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2225 return class_register(&regulator_class);
2226}
2227
2228/* init early to allow our consumers to complete system booting */
2229core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002230
2231static int __init regulator_init_complete(void)
2232{
2233 struct regulator_dev *rdev;
2234 struct regulator_ops *ops;
2235 struct regulation_constraints *c;
2236 int enabled, ret;
2237 const char *name;
2238
2239 mutex_lock(&regulator_list_mutex);
2240
2241 /* If we have a full configuration then disable any regulators
2242 * which are not in use or always_on. This will become the
2243 * default behaviour in the future.
2244 */
2245 list_for_each_entry(rdev, &regulator_list, list) {
2246 ops = rdev->desc->ops;
2247 c = rdev->constraints;
2248
2249 if (c->name)
2250 name = c->name;
2251 else if (rdev->desc->name)
2252 name = rdev->desc->name;
2253 else
2254 name = "regulator";
2255
2256 if (!ops->disable || c->always_on)
2257 continue;
2258
2259 mutex_lock(&rdev->mutex);
2260
2261 if (rdev->use_count)
2262 goto unlock;
2263
2264 /* If we can't read the status assume it's on. */
2265 if (ops->is_enabled)
2266 enabled = ops->is_enabled(rdev);
2267 else
2268 enabled = 1;
2269
2270 if (!enabled)
2271 goto unlock;
2272
2273 if (has_full_constraints) {
2274 /* We log since this may kill the system if it
2275 * goes wrong. */
2276 printk(KERN_INFO "%s: disabling %s\n",
2277 __func__, name);
2278 ret = ops->disable(rdev);
2279 if (ret != 0) {
2280 printk(KERN_ERR
2281 "%s: couldn't disable %s: %d\n",
2282 __func__, name, ret);
2283 }
2284 } else {
2285 /* The intention is that in future we will
2286 * assume that full constraints are provided
2287 * so warn even if we aren't going to do
2288 * anything here.
2289 */
2290 printk(KERN_WARNING
2291 "%s: incomplete constraints, leaving %s on\n",
2292 __func__, name);
2293 }
2294
2295unlock:
2296 mutex_unlock(&rdev->mutex);
2297 }
2298
2299 mutex_unlock(&regulator_list_mutex);
2300
2301 return 0;
2302}
2303late_initcall(regulator_init_complete);