blob: b85e4a9dafcf5d23f186cc3849f2fc4e4c198c24 [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);
31
Mark Brown8dc53902008-12-31 12:52:40 +000032/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010033 * struct regulator_map
34 *
35 * Used to provide symbolic supply names to devices.
36 */
37struct regulator_map {
38 struct list_head list;
39 struct device *dev;
40 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010041 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010042};
43
Liam Girdwood414c70c2008-04-30 15:59:04 +010044/*
45 * struct regulator
46 *
47 * One for each consumer device.
48 */
49struct regulator {
50 struct device *dev;
51 struct list_head list;
52 int uA_load;
53 int min_uV;
54 int max_uV;
David Brownell412aec62008-11-16 11:44:46 -080055 int enabled; /* count of client enables */
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
695 rdev->constraints = constraints;
696
697 /* do we need to apply the constraint voltage */
698 if (rdev->constraints->apply_uV &&
699 rdev->constraints->min_uV == rdev->constraints->max_uV &&
Mark Browne5fda262008-09-09 16:21:20 +0100700 ops->set_voltage) {
701 ret = ops->set_voltage(rdev,
Liam Girdwooda5766f12008-10-10 13:22:20 +0100702 rdev->constraints->min_uV, rdev->constraints->max_uV);
703 if (ret < 0) {
Mark Browne06f5b42008-09-09 16:21:19 +0100704 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
705 __func__,
706 rdev->constraints->min_uV, name);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100707 rdev->constraints = NULL;
708 goto out;
709 }
710 }
711
712 /* are we enabled at boot time by firmware / bootloader */
713 if (rdev->constraints->boot_on)
714 rdev->use_count = 1;
715
716 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100717 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100718 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100719 if (ret < 0) {
720 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
721 __func__, name);
722 rdev->constraints = NULL;
723 goto out;
724 }
725 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100726
Mark Browne5fda262008-09-09 16:21:20 +0100727 /* if always_on is set then turn the regulator on if it's not
728 * already on. */
729 if (constraints->always_on && ops->enable &&
730 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
731 (!ops->is_enabled && !constraints->boot_on))) {
732 ret = ops->enable(rdev);
733 if (ret < 0) {
734 printk(KERN_ERR "%s: failed to enable %s\n",
735 __func__, name);
736 rdev->constraints = NULL;
737 goto out;
738 }
739 }
740
Liam Girdwooda5766f12008-10-10 13:22:20 +0100741 print_constraints(rdev);
742out:
743 return ret;
744}
745
746/**
747 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000748 * @rdev: regulator name
749 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100750 *
751 * Called by platform initialisation code to set the supply regulator for this
752 * regulator. This ensures that a regulators supply will also be enabled by the
753 * core if it's child is enabled.
754 */
755static int set_supply(struct regulator_dev *rdev,
756 struct regulator_dev *supply_rdev)
757{
758 int err;
759
760 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
761 "supply");
762 if (err) {
763 printk(KERN_ERR
764 "%s: could not add device link %s err %d\n",
765 __func__, supply_rdev->dev.kobj.name, err);
766 goto out;
767 }
768 rdev->supply = supply_rdev;
769 list_add(&rdev->slist, &supply_rdev->supply_list);
770out:
771 return err;
772}
773
774/**
775 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000776 * @rdev: regulator source
777 * @consumer_dev: device the supply applies to
778 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100779 *
780 * Allows platform initialisation code to map physical regulator
781 * sources to symbolic names for supplies for use by devices. Devices
782 * should use these symbolic names to request regulators, avoiding the
783 * need to provide board-specific regulator names as platform data.
784 */
785static int set_consumer_device_supply(struct regulator_dev *rdev,
786 struct device *consumer_dev, const char *supply)
787{
788 struct regulator_map *node;
789
790 if (supply == NULL)
791 return -EINVAL;
792
David Brownell6001e132008-12-31 12:54:19 +0000793 list_for_each_entry(node, &regulator_map_list, list) {
794 if (consumer_dev != node->dev)
795 continue;
796 if (strcmp(node->supply, supply) != 0)
797 continue;
798
799 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
800 dev_name(&node->regulator->dev),
801 node->regulator->desc->name,
802 supply,
803 dev_name(&rdev->dev), rdev->desc->name);
804 return -EBUSY;
805 }
806
Liam Girdwooda5766f12008-10-10 13:22:20 +0100807 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
808 if (node == NULL)
809 return -ENOMEM;
810
811 node->regulator = rdev;
812 node->dev = consumer_dev;
813 node->supply = supply;
814
815 list_add(&node->list, &regulator_map_list);
816 return 0;
817}
818
819static void unset_consumer_device_supply(struct regulator_dev *rdev,
820 struct device *consumer_dev)
821{
822 struct regulator_map *node, *n;
823
824 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
825 if (rdev == node->regulator &&
826 consumer_dev == node->dev) {
827 list_del(&node->list);
828 kfree(node);
829 return;
830 }
831 }
832}
833
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200834static void unset_regulator_supplies(struct regulator_dev *rdev)
835{
836 struct regulator_map *node, *n;
837
838 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
839 if (rdev == node->regulator) {
840 list_del(&node->list);
841 kfree(node);
842 return;
843 }
844 }
845}
846
Liam Girdwood414c70c2008-04-30 15:59:04 +0100847#define REG_STR_SIZE 32
848
849static struct regulator *create_regulator(struct regulator_dev *rdev,
850 struct device *dev,
851 const char *supply_name)
852{
853 struct regulator *regulator;
854 char buf[REG_STR_SIZE];
855 int err, size;
856
857 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
858 if (regulator == NULL)
859 return NULL;
860
861 mutex_lock(&rdev->mutex);
862 regulator->rdev = rdev;
863 list_add(&regulator->list, &rdev->consumer_list);
864
865 if (dev) {
866 /* create a 'requested_microamps_name' sysfs entry */
867 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
868 supply_name);
869 if (size >= REG_STR_SIZE)
870 goto overflow_err;
871
872 regulator->dev = dev;
873 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
874 if (regulator->dev_attr.attr.name == NULL)
875 goto attr_name_err;
876
877 regulator->dev_attr.attr.owner = THIS_MODULE;
878 regulator->dev_attr.attr.mode = 0444;
879 regulator->dev_attr.show = device_requested_uA_show;
880 err = device_create_file(dev, &regulator->dev_attr);
881 if (err < 0) {
882 printk(KERN_WARNING "%s: could not add regulator_dev"
883 " load sysfs\n", __func__);
884 goto attr_name_err;
885 }
886
887 /* also add a link to the device sysfs entry */
888 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
889 dev->kobj.name, supply_name);
890 if (size >= REG_STR_SIZE)
891 goto attr_err;
892
893 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
894 if (regulator->supply_name == NULL)
895 goto attr_err;
896
897 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
898 buf);
899 if (err) {
900 printk(KERN_WARNING
901 "%s: could not add device link %s err %d\n",
902 __func__, dev->kobj.name, err);
903 device_remove_file(dev, &regulator->dev_attr);
904 goto link_name_err;
905 }
906 }
907 mutex_unlock(&rdev->mutex);
908 return regulator;
909link_name_err:
910 kfree(regulator->supply_name);
911attr_err:
912 device_remove_file(regulator->dev, &regulator->dev_attr);
913attr_name_err:
914 kfree(regulator->dev_attr.attr.name);
915overflow_err:
916 list_del(&regulator->list);
917 kfree(regulator);
918 mutex_unlock(&rdev->mutex);
919 return NULL;
920}
921
922/**
923 * regulator_get - lookup and obtain a reference to a regulator.
924 * @dev: device for regulator "consumer"
925 * @id: Supply name or regulator ID.
926 *
927 * Returns a struct regulator corresponding to the regulator producer,
928 * or IS_ERR() condition containing errno. Use of supply names
929 * configured via regulator_set_device_supply() is strongly
930 * encouraged.
931 */
932struct regulator *regulator_get(struct device *dev, const char *id)
933{
934 struct regulator_dev *rdev;
935 struct regulator_map *map;
936 struct regulator *regulator = ERR_PTR(-ENODEV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100937
938 if (id == NULL) {
939 printk(KERN_ERR "regulator: get() with no identifier\n");
940 return regulator;
941 }
942
943 mutex_lock(&regulator_list_mutex);
944
945 list_for_each_entry(map, &regulator_map_list, list) {
946 if (dev == map->dev &&
947 strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100948 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100949 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100950 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100951 }
952 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
953 id);
954 mutex_unlock(&regulator_list_mutex);
955 return regulator;
956
957found:
Liam Girdwooda5766f12008-10-10 13:22:20 +0100958 if (!try_module_get(rdev->owner))
959 goto out;
960
Liam Girdwood414c70c2008-04-30 15:59:04 +0100961 regulator = create_regulator(rdev, dev, id);
962 if (regulator == NULL) {
963 regulator = ERR_PTR(-ENOMEM);
964 module_put(rdev->owner);
965 }
966
Liam Girdwooda5766f12008-10-10 13:22:20 +0100967out:
Liam Girdwood414c70c2008-04-30 15:59:04 +0100968 mutex_unlock(&regulator_list_mutex);
969 return regulator;
970}
971EXPORT_SYMBOL_GPL(regulator_get);
972
973/**
974 * regulator_put - "free" the regulator source
975 * @regulator: regulator source
976 *
977 * Note: drivers must ensure that all regulator_enable calls made on this
978 * regulator source are balanced by regulator_disable calls prior to calling
979 * this function.
980 */
981void regulator_put(struct regulator *regulator)
982{
983 struct regulator_dev *rdev;
984
985 if (regulator == NULL || IS_ERR(regulator))
986 return;
987
Liam Girdwood414c70c2008-04-30 15:59:04 +0100988 mutex_lock(&regulator_list_mutex);
989 rdev = regulator->rdev;
990
David Brownell412aec62008-11-16 11:44:46 -0800991 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
992 regulator->supply_name))
993 _regulator_disable(rdev);
994
Liam Girdwood414c70c2008-04-30 15:59:04 +0100995 /* remove any sysfs entries */
996 if (regulator->dev) {
997 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
998 kfree(regulator->supply_name);
999 device_remove_file(regulator->dev, &regulator->dev_attr);
1000 kfree(regulator->dev_attr.attr.name);
1001 }
1002 list_del(&regulator->list);
1003 kfree(regulator);
1004
1005 module_put(rdev->owner);
1006 mutex_unlock(&regulator_list_mutex);
1007}
1008EXPORT_SYMBOL_GPL(regulator_put);
1009
1010/* locks held by regulator_enable() */
1011static int _regulator_enable(struct regulator_dev *rdev)
1012{
1013 int ret = -EINVAL;
1014
1015 if (!rdev->constraints) {
1016 printk(KERN_ERR "%s: %s has no constraints\n",
1017 __func__, rdev->desc->name);
1018 return ret;
1019 }
1020
1021 /* do we need to enable the supply regulator first */
1022 if (rdev->supply) {
1023 ret = _regulator_enable(rdev->supply);
1024 if (ret < 0) {
1025 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1026 __func__, rdev->desc->name, ret);
1027 return ret;
1028 }
1029 }
1030
1031 /* check voltage and requested load before enabling */
1032 if (rdev->desc->ops->enable) {
1033
1034 if (rdev->constraints &&
1035 (rdev->constraints->valid_ops_mask &
1036 REGULATOR_CHANGE_DRMS))
1037 drms_uA_update(rdev);
1038
1039 ret = rdev->desc->ops->enable(rdev);
1040 if (ret < 0) {
1041 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1042 __func__, rdev->desc->name, ret);
1043 return ret;
1044 }
1045 rdev->use_count++;
1046 return ret;
1047 }
1048
1049 return ret;
1050}
1051
1052/**
1053 * regulator_enable - enable regulator output
1054 * @regulator: regulator source
1055 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001056 * Request that the regulator be enabled with the regulator output at
1057 * the predefined voltage or current value. Calls to regulator_enable()
1058 * must be balanced with calls to regulator_disable().
1059 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001060 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001061 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001062 */
1063int regulator_enable(struct regulator *regulator)
1064{
David Brownell412aec62008-11-16 11:44:46 -08001065 struct regulator_dev *rdev = regulator->rdev;
1066 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001067
David Brownell412aec62008-11-16 11:44:46 -08001068 mutex_lock(&rdev->mutex);
1069 if (regulator->enabled == 0)
1070 ret = _regulator_enable(rdev);
1071 else if (regulator->enabled < 0)
1072 ret = -EIO;
1073 if (ret == 0)
1074 regulator->enabled++;
1075 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001076 return ret;
1077}
1078EXPORT_SYMBOL_GPL(regulator_enable);
1079
1080/* locks held by regulator_disable() */
1081static int _regulator_disable(struct regulator_dev *rdev)
1082{
1083 int ret = 0;
1084
1085 /* are we the last user and permitted to disable ? */
1086 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1087
1088 /* we are last user */
1089 if (rdev->desc->ops->disable) {
1090 ret = rdev->desc->ops->disable(rdev);
1091 if (ret < 0) {
1092 printk(KERN_ERR "%s: failed to disable %s\n",
1093 __func__, rdev->desc->name);
1094 return ret;
1095 }
1096 }
1097
1098 /* decrease our supplies ref count and disable if required */
1099 if (rdev->supply)
1100 _regulator_disable(rdev->supply);
1101
1102 rdev->use_count = 0;
1103 } else if (rdev->use_count > 1) {
1104
1105 if (rdev->constraints &&
1106 (rdev->constraints->valid_ops_mask &
1107 REGULATOR_CHANGE_DRMS))
1108 drms_uA_update(rdev);
1109
1110 rdev->use_count--;
1111 }
1112 return ret;
1113}
1114
1115/**
1116 * regulator_disable - disable regulator output
1117 * @regulator: regulator source
1118 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001119 * Disable the regulator output voltage or current. Calls to
1120 * regulator_enable() must be balanced with calls to
1121 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001122 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001123 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001124 * devices have it enabled, the regulator device supports disabling and
1125 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001126 */
1127int regulator_disable(struct regulator *regulator)
1128{
David Brownell412aec62008-11-16 11:44:46 -08001129 struct regulator_dev *rdev = regulator->rdev;
1130 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001131
David Brownell412aec62008-11-16 11:44:46 -08001132 mutex_lock(&rdev->mutex);
1133 if (regulator->enabled == 1) {
1134 ret = _regulator_disable(rdev);
1135 if (ret == 0)
1136 regulator->uA_load = 0;
1137 } else if (WARN(regulator->enabled <= 0,
1138 "unbalanced disables for supply %s\n",
1139 regulator->supply_name))
1140 ret = -EIO;
1141 if (ret == 0)
1142 regulator->enabled--;
1143 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001144 return ret;
1145}
1146EXPORT_SYMBOL_GPL(regulator_disable);
1147
1148/* locks held by regulator_force_disable() */
1149static int _regulator_force_disable(struct regulator_dev *rdev)
1150{
1151 int ret = 0;
1152
1153 /* force disable */
1154 if (rdev->desc->ops->disable) {
1155 /* ah well, who wants to live forever... */
1156 ret = rdev->desc->ops->disable(rdev);
1157 if (ret < 0) {
1158 printk(KERN_ERR "%s: failed to force disable %s\n",
1159 __func__, rdev->desc->name);
1160 return ret;
1161 }
1162 /* notify other consumers that power has been forced off */
1163 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1164 NULL);
1165 }
1166
1167 /* decrease our supplies ref count and disable if required */
1168 if (rdev->supply)
1169 _regulator_disable(rdev->supply);
1170
1171 rdev->use_count = 0;
1172 return ret;
1173}
1174
1175/**
1176 * regulator_force_disable - force disable regulator output
1177 * @regulator: regulator source
1178 *
1179 * Forcibly disable the regulator output voltage or current.
1180 * NOTE: this *will* disable the regulator output even if other consumer
1181 * devices have it enabled. This should be used for situations when device
1182 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1183 */
1184int regulator_force_disable(struct regulator *regulator)
1185{
1186 int ret;
1187
1188 mutex_lock(&regulator->rdev->mutex);
1189 regulator->enabled = 0;
1190 regulator->uA_load = 0;
1191 ret = _regulator_force_disable(regulator->rdev);
1192 mutex_unlock(&regulator->rdev->mutex);
1193 return ret;
1194}
1195EXPORT_SYMBOL_GPL(regulator_force_disable);
1196
1197static int _regulator_is_enabled(struct regulator_dev *rdev)
1198{
1199 int ret;
1200
1201 mutex_lock(&rdev->mutex);
1202
1203 /* sanity check */
1204 if (!rdev->desc->ops->is_enabled) {
1205 ret = -EINVAL;
1206 goto out;
1207 }
1208
1209 ret = rdev->desc->ops->is_enabled(rdev);
1210out:
1211 mutex_unlock(&rdev->mutex);
1212 return ret;
1213}
1214
1215/**
1216 * regulator_is_enabled - is the regulator output enabled
1217 * @regulator: regulator source
1218 *
David Brownell412aec62008-11-16 11:44:46 -08001219 * Returns positive if the regulator driver backing the source/client
1220 * has requested that the device be enabled, zero if it hasn't, else a
1221 * negative errno code.
1222 *
1223 * Note that the device backing this regulator handle can have multiple
1224 * users, so it might be enabled even if regulator_enable() was never
1225 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001226 */
1227int regulator_is_enabled(struct regulator *regulator)
1228{
1229 return _regulator_is_enabled(regulator->rdev);
1230}
1231EXPORT_SYMBOL_GPL(regulator_is_enabled);
1232
1233/**
1234 * regulator_set_voltage - set regulator output voltage
1235 * @regulator: regulator source
1236 * @min_uV: Minimum required voltage in uV
1237 * @max_uV: Maximum acceptable voltage in uV
1238 *
1239 * Sets a voltage regulator to the desired output voltage. This can be set
1240 * during any regulator state. IOW, regulator can be disabled or enabled.
1241 *
1242 * If the regulator is enabled then the voltage will change to the new value
1243 * immediately otherwise if the regulator is disabled the regulator will
1244 * output at the new voltage when enabled.
1245 *
1246 * NOTE: If the regulator is shared between several devices then the lowest
1247 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001248 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001249 * calling this function otherwise this call will fail.
1250 */
1251int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1252{
1253 struct regulator_dev *rdev = regulator->rdev;
1254 int ret;
1255
1256 mutex_lock(&rdev->mutex);
1257
1258 /* sanity check */
1259 if (!rdev->desc->ops->set_voltage) {
1260 ret = -EINVAL;
1261 goto out;
1262 }
1263
1264 /* constraints check */
1265 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1266 if (ret < 0)
1267 goto out;
1268 regulator->min_uV = min_uV;
1269 regulator->max_uV = max_uV;
1270 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1271
1272out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001273 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001274 mutex_unlock(&rdev->mutex);
1275 return ret;
1276}
1277EXPORT_SYMBOL_GPL(regulator_set_voltage);
1278
1279static int _regulator_get_voltage(struct regulator_dev *rdev)
1280{
1281 /* sanity check */
1282 if (rdev->desc->ops->get_voltage)
1283 return rdev->desc->ops->get_voltage(rdev);
1284 else
1285 return -EINVAL;
1286}
1287
1288/**
1289 * regulator_get_voltage - get regulator output voltage
1290 * @regulator: regulator source
1291 *
1292 * This returns the current regulator voltage in uV.
1293 *
1294 * NOTE: If the regulator is disabled it will return the voltage value. This
1295 * function should not be used to determine regulator state.
1296 */
1297int regulator_get_voltage(struct regulator *regulator)
1298{
1299 int ret;
1300
1301 mutex_lock(&regulator->rdev->mutex);
1302
1303 ret = _regulator_get_voltage(regulator->rdev);
1304
1305 mutex_unlock(&regulator->rdev->mutex);
1306
1307 return ret;
1308}
1309EXPORT_SYMBOL_GPL(regulator_get_voltage);
1310
1311/**
1312 * regulator_set_current_limit - set regulator output current limit
1313 * @regulator: regulator source
1314 * @min_uA: Minimuum supported current in uA
1315 * @max_uA: Maximum supported current in uA
1316 *
1317 * Sets current sink to the desired output current. This can be set during
1318 * any regulator state. IOW, regulator can be disabled or enabled.
1319 *
1320 * If the regulator is enabled then the current will change to the new value
1321 * immediately otherwise if the regulator is disabled the regulator will
1322 * output at the new current when enabled.
1323 *
1324 * NOTE: Regulator system constraints must be set for this regulator before
1325 * calling this function otherwise this call will fail.
1326 */
1327int regulator_set_current_limit(struct regulator *regulator,
1328 int min_uA, int max_uA)
1329{
1330 struct regulator_dev *rdev = regulator->rdev;
1331 int ret;
1332
1333 mutex_lock(&rdev->mutex);
1334
1335 /* sanity check */
1336 if (!rdev->desc->ops->set_current_limit) {
1337 ret = -EINVAL;
1338 goto out;
1339 }
1340
1341 /* constraints check */
1342 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1343 if (ret < 0)
1344 goto out;
1345
1346 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1347out:
1348 mutex_unlock(&rdev->mutex);
1349 return ret;
1350}
1351EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1352
1353static int _regulator_get_current_limit(struct regulator_dev *rdev)
1354{
1355 int ret;
1356
1357 mutex_lock(&rdev->mutex);
1358
1359 /* sanity check */
1360 if (!rdev->desc->ops->get_current_limit) {
1361 ret = -EINVAL;
1362 goto out;
1363 }
1364
1365 ret = rdev->desc->ops->get_current_limit(rdev);
1366out:
1367 mutex_unlock(&rdev->mutex);
1368 return ret;
1369}
1370
1371/**
1372 * regulator_get_current_limit - get regulator output current
1373 * @regulator: regulator source
1374 *
1375 * This returns the current supplied by the specified current sink in uA.
1376 *
1377 * NOTE: If the regulator is disabled it will return the current value. This
1378 * function should not be used to determine regulator state.
1379 */
1380int regulator_get_current_limit(struct regulator *regulator)
1381{
1382 return _regulator_get_current_limit(regulator->rdev);
1383}
1384EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1385
1386/**
1387 * regulator_set_mode - set regulator operating mode
1388 * @regulator: regulator source
1389 * @mode: operating mode - one of the REGULATOR_MODE constants
1390 *
1391 * Set regulator operating mode to increase regulator efficiency or improve
1392 * regulation performance.
1393 *
1394 * NOTE: Regulator system constraints must be set for this regulator before
1395 * calling this function otherwise this call will fail.
1396 */
1397int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1398{
1399 struct regulator_dev *rdev = regulator->rdev;
1400 int ret;
1401
1402 mutex_lock(&rdev->mutex);
1403
1404 /* sanity check */
1405 if (!rdev->desc->ops->set_mode) {
1406 ret = -EINVAL;
1407 goto out;
1408 }
1409
1410 /* constraints check */
1411 ret = regulator_check_mode(rdev, mode);
1412 if (ret < 0)
1413 goto out;
1414
1415 ret = rdev->desc->ops->set_mode(rdev, mode);
1416out:
1417 mutex_unlock(&rdev->mutex);
1418 return ret;
1419}
1420EXPORT_SYMBOL_GPL(regulator_set_mode);
1421
1422static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1423{
1424 int ret;
1425
1426 mutex_lock(&rdev->mutex);
1427
1428 /* sanity check */
1429 if (!rdev->desc->ops->get_mode) {
1430 ret = -EINVAL;
1431 goto out;
1432 }
1433
1434 ret = rdev->desc->ops->get_mode(rdev);
1435out:
1436 mutex_unlock(&rdev->mutex);
1437 return ret;
1438}
1439
1440/**
1441 * regulator_get_mode - get regulator operating mode
1442 * @regulator: regulator source
1443 *
1444 * Get the current regulator operating mode.
1445 */
1446unsigned int regulator_get_mode(struct regulator *regulator)
1447{
1448 return _regulator_get_mode(regulator->rdev);
1449}
1450EXPORT_SYMBOL_GPL(regulator_get_mode);
1451
1452/**
1453 * regulator_set_optimum_mode - set regulator optimum operating mode
1454 * @regulator: regulator source
1455 * @uA_load: load current
1456 *
1457 * Notifies the regulator core of a new device load. This is then used by
1458 * DRMS (if enabled by constraints) to set the most efficient regulator
1459 * operating mode for the new regulator loading.
1460 *
1461 * Consumer devices notify their supply regulator of the maximum power
1462 * they will require (can be taken from device datasheet in the power
1463 * consumption tables) when they change operational status and hence power
1464 * state. Examples of operational state changes that can affect power
1465 * consumption are :-
1466 *
1467 * o Device is opened / closed.
1468 * o Device I/O is about to begin or has just finished.
1469 * o Device is idling in between work.
1470 *
1471 * This information is also exported via sysfs to userspace.
1472 *
1473 * DRMS will sum the total requested load on the regulator and change
1474 * to the most efficient operating mode if platform constraints allow.
1475 *
1476 * Returns the new regulator mode or error.
1477 */
1478int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1479{
1480 struct regulator_dev *rdev = regulator->rdev;
1481 struct regulator *consumer;
1482 int ret, output_uV, input_uV, total_uA_load = 0;
1483 unsigned int mode;
1484
1485 mutex_lock(&rdev->mutex);
1486
1487 regulator->uA_load = uA_load;
1488 ret = regulator_check_drms(rdev);
1489 if (ret < 0)
1490 goto out;
1491 ret = -EINVAL;
1492
1493 /* sanity check */
1494 if (!rdev->desc->ops->get_optimum_mode)
1495 goto out;
1496
1497 /* get output voltage */
1498 output_uV = rdev->desc->ops->get_voltage(rdev);
1499 if (output_uV <= 0) {
1500 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1501 __func__, rdev->desc->name);
1502 goto out;
1503 }
1504
1505 /* get input voltage */
1506 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1507 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1508 else
1509 input_uV = rdev->constraints->input_uV;
1510 if (input_uV <= 0) {
1511 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1512 __func__, rdev->desc->name);
1513 goto out;
1514 }
1515
1516 /* calc total requested load for this regulator */
1517 list_for_each_entry(consumer, &rdev->consumer_list, list)
1518 total_uA_load += consumer->uA_load;
1519
1520 mode = rdev->desc->ops->get_optimum_mode(rdev,
1521 input_uV, output_uV,
1522 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001523 ret = regulator_check_mode(rdev, mode);
1524 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001525 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1526 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1527 total_uA_load, input_uV, output_uV);
1528 goto out;
1529 }
1530
1531 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001532 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001533 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1534 __func__, mode, rdev->desc->name);
1535 goto out;
1536 }
1537 ret = mode;
1538out:
1539 mutex_unlock(&rdev->mutex);
1540 return ret;
1541}
1542EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1543
1544/**
1545 * regulator_register_notifier - register regulator event notifier
1546 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001547 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001548 *
1549 * Register notifier block to receive regulator events.
1550 */
1551int regulator_register_notifier(struct regulator *regulator,
1552 struct notifier_block *nb)
1553{
1554 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1555 nb);
1556}
1557EXPORT_SYMBOL_GPL(regulator_register_notifier);
1558
1559/**
1560 * regulator_unregister_notifier - unregister regulator event notifier
1561 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001562 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001563 *
1564 * Unregister regulator event notifier block.
1565 */
1566int regulator_unregister_notifier(struct regulator *regulator,
1567 struct notifier_block *nb)
1568{
1569 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1570 nb);
1571}
1572EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1573
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001574/* notify regulator consumers and downstream regulator consumers.
1575 * Note mutex must be held by caller.
1576 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001577static void _notifier_call_chain(struct regulator_dev *rdev,
1578 unsigned long event, void *data)
1579{
1580 struct regulator_dev *_rdev;
1581
1582 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001583 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001584
1585 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001586 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1587 mutex_lock(&_rdev->mutex);
1588 _notifier_call_chain(_rdev, event, data);
1589 mutex_unlock(&_rdev->mutex);
1590 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001591}
1592
1593/**
1594 * regulator_bulk_get - get multiple regulator consumers
1595 *
1596 * @dev: Device to supply
1597 * @num_consumers: Number of consumers to register
1598 * @consumers: Configuration of consumers; clients are stored here.
1599 *
1600 * @return 0 on success, an errno on failure.
1601 *
1602 * This helper function allows drivers to get several regulator
1603 * consumers in one operation. If any of the regulators cannot be
1604 * acquired then any regulators that were allocated will be freed
1605 * before returning to the caller.
1606 */
1607int regulator_bulk_get(struct device *dev, int num_consumers,
1608 struct regulator_bulk_data *consumers)
1609{
1610 int i;
1611 int ret;
1612
1613 for (i = 0; i < num_consumers; i++)
1614 consumers[i].consumer = NULL;
1615
1616 for (i = 0; i < num_consumers; i++) {
1617 consumers[i].consumer = regulator_get(dev,
1618 consumers[i].supply);
1619 if (IS_ERR(consumers[i].consumer)) {
1620 dev_err(dev, "Failed to get supply '%s'\n",
1621 consumers[i].supply);
1622 ret = PTR_ERR(consumers[i].consumer);
1623 consumers[i].consumer = NULL;
1624 goto err;
1625 }
1626 }
1627
1628 return 0;
1629
1630err:
1631 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1632 regulator_put(consumers[i].consumer);
1633
1634 return ret;
1635}
1636EXPORT_SYMBOL_GPL(regulator_bulk_get);
1637
1638/**
1639 * regulator_bulk_enable - enable multiple regulator consumers
1640 *
1641 * @num_consumers: Number of consumers
1642 * @consumers: Consumer data; clients are stored here.
1643 * @return 0 on success, an errno on failure
1644 *
1645 * This convenience API allows consumers to enable multiple regulator
1646 * clients in a single API call. If any consumers cannot be enabled
1647 * then any others that were enabled will be disabled again prior to
1648 * return.
1649 */
1650int regulator_bulk_enable(int num_consumers,
1651 struct regulator_bulk_data *consumers)
1652{
1653 int i;
1654 int ret;
1655
1656 for (i = 0; i < num_consumers; i++) {
1657 ret = regulator_enable(consumers[i].consumer);
1658 if (ret != 0)
1659 goto err;
1660 }
1661
1662 return 0;
1663
1664err:
1665 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1666 for (i = 0; i < num_consumers; i++)
1667 regulator_disable(consumers[i].consumer);
1668
1669 return ret;
1670}
1671EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1672
1673/**
1674 * regulator_bulk_disable - disable multiple regulator consumers
1675 *
1676 * @num_consumers: Number of consumers
1677 * @consumers: Consumer data; clients are stored here.
1678 * @return 0 on success, an errno on failure
1679 *
1680 * This convenience API allows consumers to disable multiple regulator
1681 * clients in a single API call. If any consumers cannot be enabled
1682 * then any others that were disabled will be disabled again prior to
1683 * return.
1684 */
1685int regulator_bulk_disable(int num_consumers,
1686 struct regulator_bulk_data *consumers)
1687{
1688 int i;
1689 int ret;
1690
1691 for (i = 0; i < num_consumers; i++) {
1692 ret = regulator_disable(consumers[i].consumer);
1693 if (ret != 0)
1694 goto err;
1695 }
1696
1697 return 0;
1698
1699err:
1700 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1701 for (i = 0; i < num_consumers; i++)
1702 regulator_enable(consumers[i].consumer);
1703
1704 return ret;
1705}
1706EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1707
1708/**
1709 * regulator_bulk_free - free multiple regulator consumers
1710 *
1711 * @num_consumers: Number of consumers
1712 * @consumers: Consumer data; clients are stored here.
1713 *
1714 * This convenience API allows consumers to free multiple regulator
1715 * clients in a single API call.
1716 */
1717void regulator_bulk_free(int num_consumers,
1718 struct regulator_bulk_data *consumers)
1719{
1720 int i;
1721
1722 for (i = 0; i < num_consumers; i++) {
1723 regulator_put(consumers[i].consumer);
1724 consumers[i].consumer = NULL;
1725 }
1726}
1727EXPORT_SYMBOL_GPL(regulator_bulk_free);
1728
1729/**
1730 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00001731 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01001732 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00001733 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001734 *
1735 * Called by regulator drivers to notify clients a regulator event has
1736 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001737 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001738 */
1739int regulator_notifier_call_chain(struct regulator_dev *rdev,
1740 unsigned long event, void *data)
1741{
1742 _notifier_call_chain(rdev, event, data);
1743 return NOTIFY_DONE;
1744
1745}
1746EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1747
David Brownell7ad68e22008-11-11 17:39:02 -08001748/*
1749 * To avoid cluttering sysfs (and memory) with useless state, only
1750 * create attributes that can be meaningfully displayed.
1751 */
1752static int add_regulator_attributes(struct regulator_dev *rdev)
1753{
1754 struct device *dev = &rdev->dev;
1755 struct regulator_ops *ops = rdev->desc->ops;
1756 int status = 0;
1757
1758 /* some attributes need specific methods to be displayed */
1759 if (ops->get_voltage) {
1760 status = device_create_file(dev, &dev_attr_microvolts);
1761 if (status < 0)
1762 return status;
1763 }
1764 if (ops->get_current_limit) {
1765 status = device_create_file(dev, &dev_attr_microamps);
1766 if (status < 0)
1767 return status;
1768 }
1769 if (ops->get_mode) {
1770 status = device_create_file(dev, &dev_attr_opmode);
1771 if (status < 0)
1772 return status;
1773 }
1774 if (ops->is_enabled) {
1775 status = device_create_file(dev, &dev_attr_state);
1776 if (status < 0)
1777 return status;
1778 }
David Brownell853116a2009-01-14 23:03:17 -08001779 if (ops->get_status) {
1780 status = device_create_file(dev, &dev_attr_status);
1781 if (status < 0)
1782 return status;
1783 }
David Brownell7ad68e22008-11-11 17:39:02 -08001784
1785 /* some attributes are type-specific */
1786 if (rdev->desc->type == REGULATOR_CURRENT) {
1787 status = device_create_file(dev, &dev_attr_requested_microamps);
1788 if (status < 0)
1789 return status;
1790 }
1791
1792 /* all the other attributes exist to support constraints;
1793 * don't show them if there are no constraints, or if the
1794 * relevant supporting methods are missing.
1795 */
1796 if (!rdev->constraints)
1797 return status;
1798
1799 /* constraints need specific supporting methods */
1800 if (ops->set_voltage) {
1801 status = device_create_file(dev, &dev_attr_min_microvolts);
1802 if (status < 0)
1803 return status;
1804 status = device_create_file(dev, &dev_attr_max_microvolts);
1805 if (status < 0)
1806 return status;
1807 }
1808 if (ops->set_current_limit) {
1809 status = device_create_file(dev, &dev_attr_min_microamps);
1810 if (status < 0)
1811 return status;
1812 status = device_create_file(dev, &dev_attr_max_microamps);
1813 if (status < 0)
1814 return status;
1815 }
1816
1817 /* suspend mode constraints need multiple supporting methods */
1818 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1819 return status;
1820
1821 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1822 if (status < 0)
1823 return status;
1824 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1825 if (status < 0)
1826 return status;
1827 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1828 if (status < 0)
1829 return status;
1830
1831 if (ops->set_suspend_voltage) {
1832 status = device_create_file(dev,
1833 &dev_attr_suspend_standby_microvolts);
1834 if (status < 0)
1835 return status;
1836 status = device_create_file(dev,
1837 &dev_attr_suspend_mem_microvolts);
1838 if (status < 0)
1839 return status;
1840 status = device_create_file(dev,
1841 &dev_attr_suspend_disk_microvolts);
1842 if (status < 0)
1843 return status;
1844 }
1845
1846 if (ops->set_suspend_mode) {
1847 status = device_create_file(dev,
1848 &dev_attr_suspend_standby_mode);
1849 if (status < 0)
1850 return status;
1851 status = device_create_file(dev,
1852 &dev_attr_suspend_mem_mode);
1853 if (status < 0)
1854 return status;
1855 status = device_create_file(dev,
1856 &dev_attr_suspend_disk_mode);
1857 if (status < 0)
1858 return status;
1859 }
1860
1861 return status;
1862}
1863
Liam Girdwood414c70c2008-04-30 15:59:04 +01001864/**
1865 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001866 * @regulator_desc: regulator to register
1867 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00001868 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00001869 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01001870 *
1871 * Called by regulator drivers to register a regulator.
1872 * Returns 0 on success.
1873 */
1874struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00001875 struct device *dev, struct regulator_init_data *init_data,
1876 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001877{
1878 static atomic_t regulator_no = ATOMIC_INIT(0);
1879 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001880 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001881
1882 if (regulator_desc == NULL)
1883 return ERR_PTR(-EINVAL);
1884
1885 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1886 return ERR_PTR(-EINVAL);
1887
1888 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1889 !regulator_desc->type == REGULATOR_CURRENT)
1890 return ERR_PTR(-EINVAL);
1891
Mark Brown46fabe1e2008-09-09 16:21:18 +01001892 if (!init_data)
1893 return ERR_PTR(-EINVAL);
1894
Liam Girdwood414c70c2008-04-30 15:59:04 +01001895 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1896 if (rdev == NULL)
1897 return ERR_PTR(-ENOMEM);
1898
1899 mutex_lock(&regulator_list_mutex);
1900
1901 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001902 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001903 rdev->owner = regulator_desc->owner;
1904 rdev->desc = regulator_desc;
1905 INIT_LIST_HEAD(&rdev->consumer_list);
1906 INIT_LIST_HEAD(&rdev->supply_list);
1907 INIT_LIST_HEAD(&rdev->list);
1908 INIT_LIST_HEAD(&rdev->slist);
1909 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1910
Liam Girdwooda5766f12008-10-10 13:22:20 +01001911 /* preform any regulator specific init */
1912 if (init_data->regulator_init) {
1913 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08001914 if (ret < 0)
1915 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001916 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001917
Liam Girdwooda5766f12008-10-10 13:22:20 +01001918 /* register with sysfs */
1919 rdev->dev.class = &regulator_class;
1920 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01001921 dev_set_name(&rdev->dev, "regulator.%d",
1922 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001923 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08001924 if (ret != 0)
1925 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001926
1927 dev_set_drvdata(&rdev->dev, rdev);
1928
Mike Rapoport74f544c2008-11-25 14:53:53 +02001929 /* set regulator constraints */
1930 ret = set_machine_constraints(rdev, &init_data->constraints);
1931 if (ret < 0)
1932 goto scrub;
1933
David Brownell7ad68e22008-11-11 17:39:02 -08001934 /* add attributes supported by this regulator */
1935 ret = add_regulator_attributes(rdev);
1936 if (ret < 0)
1937 goto scrub;
1938
Liam Girdwooda5766f12008-10-10 13:22:20 +01001939 /* set supply regulator if it exists */
1940 if (init_data->supply_regulator_dev) {
1941 ret = set_supply(rdev,
1942 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08001943 if (ret < 0)
1944 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001945 }
1946
1947 /* add consumers devices */
1948 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1949 ret = set_consumer_device_supply(rdev,
1950 init_data->consumer_supplies[i].dev,
1951 init_data->consumer_supplies[i].supply);
1952 if (ret < 0) {
1953 for (--i; i >= 0; i--)
1954 unset_consumer_device_supply(rdev,
1955 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08001956 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001957 }
1958 }
1959
1960 list_add(&rdev->list, &regulator_list);
1961out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001962 mutex_unlock(&regulator_list_mutex);
1963 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08001964
1965scrub:
1966 device_unregister(&rdev->dev);
1967clean:
1968 kfree(rdev);
1969 rdev = ERR_PTR(ret);
1970 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001971}
1972EXPORT_SYMBOL_GPL(regulator_register);
1973
1974/**
1975 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001976 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01001977 *
1978 * Called by regulator drivers to unregister a regulator.
1979 */
1980void regulator_unregister(struct regulator_dev *rdev)
1981{
1982 if (rdev == NULL)
1983 return;
1984
1985 mutex_lock(&regulator_list_mutex);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001986 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001987 list_del(&rdev->list);
1988 if (rdev->supply)
1989 sysfs_remove_link(&rdev->dev.kobj, "supply");
1990 device_unregister(&rdev->dev);
1991 mutex_unlock(&regulator_list_mutex);
1992}
1993EXPORT_SYMBOL_GPL(regulator_unregister);
1994
1995/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00001996 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01001997 * @state: system suspend state
1998 *
1999 * Configure each regulator with it's suspend operating parameters for state.
2000 * This will usually be called by machine suspend code prior to supending.
2001 */
2002int regulator_suspend_prepare(suspend_state_t state)
2003{
2004 struct regulator_dev *rdev;
2005 int ret = 0;
2006
2007 /* ON is handled by regulator active state */
2008 if (state == PM_SUSPEND_ON)
2009 return -EINVAL;
2010
2011 mutex_lock(&regulator_list_mutex);
2012 list_for_each_entry(rdev, &regulator_list, list) {
2013
2014 mutex_lock(&rdev->mutex);
2015 ret = suspend_prepare(rdev, state);
2016 mutex_unlock(&rdev->mutex);
2017
2018 if (ret < 0) {
2019 printk(KERN_ERR "%s: failed to prepare %s\n",
2020 __func__, rdev->desc->name);
2021 goto out;
2022 }
2023 }
2024out:
2025 mutex_unlock(&regulator_list_mutex);
2026 return ret;
2027}
2028EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2029
2030/**
2031 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002032 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002033 *
2034 * Get rdev regulator driver private data. This call can be used in the
2035 * regulator driver context.
2036 */
2037void *rdev_get_drvdata(struct regulator_dev *rdev)
2038{
2039 return rdev->reg_data;
2040}
2041EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2042
2043/**
2044 * regulator_get_drvdata - get regulator driver data
2045 * @regulator: regulator
2046 *
2047 * Get regulator driver private data. This call can be used in the consumer
2048 * driver context when non API regulator specific functions need to be called.
2049 */
2050void *regulator_get_drvdata(struct regulator *regulator)
2051{
2052 return regulator->rdev->reg_data;
2053}
2054EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2055
2056/**
2057 * regulator_set_drvdata - set regulator driver data
2058 * @regulator: regulator
2059 * @data: data
2060 */
2061void regulator_set_drvdata(struct regulator *regulator, void *data)
2062{
2063 regulator->rdev->reg_data = data;
2064}
2065EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2066
2067/**
2068 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002069 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002070 */
2071int rdev_get_id(struct regulator_dev *rdev)
2072{
2073 return rdev->desc->id;
2074}
2075EXPORT_SYMBOL_GPL(rdev_get_id);
2076
Liam Girdwooda5766f12008-10-10 13:22:20 +01002077struct device *rdev_get_dev(struct regulator_dev *rdev)
2078{
2079 return &rdev->dev;
2080}
2081EXPORT_SYMBOL_GPL(rdev_get_dev);
2082
2083void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2084{
2085 return reg_init_data->driver_data;
2086}
2087EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2088
Liam Girdwood414c70c2008-04-30 15:59:04 +01002089static int __init regulator_init(void)
2090{
2091 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2092 return class_register(&regulator_class);
2093}
2094
2095/* init early to allow our consumers to complete system booting */
2096core_initcall(regulator_init);