blob: 5af16c2bb54043d090b93185c3f019a40257fcf2 [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>
Mark Brown31aae2b2009-12-21 12:21:52 +000022#include <linux/delay.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010023#include <linux/regulator/consumer.h>
24#include <linux/regulator/driver.h>
25#include <linux/regulator/machine.h>
26
Mark Brown34abbd62010-02-12 10:18:08 +000027#include "dummy.h"
28
Liam Girdwood414c70c2008-04-30 15:59:04 +010029#define REGULATOR_VERSION "0.5"
30
31static DEFINE_MUTEX(regulator_list_mutex);
32static LIST_HEAD(regulator_list);
33static LIST_HEAD(regulator_map_list);
Mark Brownca725562009-03-16 19:36:34 +000034static int has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010035
Mark Brown8dc53902008-12-31 12:52:40 +000036/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010037 * struct regulator_map
38 *
39 * Used to provide symbolic supply names to devices.
40 */
41struct regulator_map {
42 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010043 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010044 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010045 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010046};
47
Liam Girdwood414c70c2008-04-30 15:59:04 +010048/*
49 * struct regulator
50 *
51 * One for each consumer device.
52 */
53struct regulator {
54 struct device *dev;
55 struct list_head list;
56 int uA_load;
57 int min_uV;
58 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010059 char *supply_name;
60 struct device_attribute dev_attr;
61 struct regulator_dev *rdev;
62};
63
64static int _regulator_is_enabled(struct regulator_dev *rdev);
65static int _regulator_disable(struct regulator_dev *rdev);
66static int _regulator_get_voltage(struct regulator_dev *rdev);
67static int _regulator_get_current_limit(struct regulator_dev *rdev);
68static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
69static void _notifier_call_chain(struct regulator_dev *rdev,
70 unsigned long event, void *data);
71
Mark Brown1083c392009-10-22 16:31:32 +010072static const char *rdev_get_name(struct regulator_dev *rdev)
73{
74 if (rdev->constraints && rdev->constraints->name)
75 return rdev->constraints->name;
76 else if (rdev->desc->name)
77 return rdev->desc->name;
78 else
79 return "";
80}
81
Liam Girdwood414c70c2008-04-30 15:59:04 +010082/* gets the regulator for a given consumer device */
83static struct regulator *get_device_regulator(struct device *dev)
84{
85 struct regulator *regulator = NULL;
86 struct regulator_dev *rdev;
87
88 mutex_lock(&regulator_list_mutex);
89 list_for_each_entry(rdev, &regulator_list, list) {
90 mutex_lock(&rdev->mutex);
91 list_for_each_entry(regulator, &rdev->consumer_list, list) {
92 if (regulator->dev == dev) {
93 mutex_unlock(&rdev->mutex);
94 mutex_unlock(&regulator_list_mutex);
95 return regulator;
96 }
97 }
98 mutex_unlock(&rdev->mutex);
99 }
100 mutex_unlock(&regulator_list_mutex);
101 return NULL;
102}
103
104/* Platform voltage constraint check */
105static int regulator_check_voltage(struct regulator_dev *rdev,
106 int *min_uV, int *max_uV)
107{
108 BUG_ON(*min_uV > *max_uV);
109
110 if (!rdev->constraints) {
111 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100112 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100113 return -ENODEV;
114 }
115 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
116 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100117 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100118 return -EPERM;
119 }
120
121 if (*max_uV > rdev->constraints->max_uV)
122 *max_uV = rdev->constraints->max_uV;
123 if (*min_uV < rdev->constraints->min_uV)
124 *min_uV = rdev->constraints->min_uV;
125
126 if (*min_uV > *max_uV)
127 return -EINVAL;
128
129 return 0;
130}
131
132/* current constraint check */
133static int regulator_check_current_limit(struct regulator_dev *rdev,
134 int *min_uA, int *max_uA)
135{
136 BUG_ON(*min_uA > *max_uA);
137
138 if (!rdev->constraints) {
139 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100140 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100141 return -ENODEV;
142 }
143 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
144 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100145 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100146 return -EPERM;
147 }
148
149 if (*max_uA > rdev->constraints->max_uA)
150 *max_uA = rdev->constraints->max_uA;
151 if (*min_uA < rdev->constraints->min_uA)
152 *min_uA = rdev->constraints->min_uA;
153
154 if (*min_uA > *max_uA)
155 return -EINVAL;
156
157 return 0;
158}
159
160/* operating mode constraint check */
161static int regulator_check_mode(struct regulator_dev *rdev, int mode)
162{
David Brownelle5735202008-11-16 11:46:56 -0800163 switch (mode) {
164 case REGULATOR_MODE_FAST:
165 case REGULATOR_MODE_NORMAL:
166 case REGULATOR_MODE_IDLE:
167 case REGULATOR_MODE_STANDBY:
168 break;
169 default:
170 return -EINVAL;
171 }
172
Liam Girdwood414c70c2008-04-30 15:59:04 +0100173 if (!rdev->constraints) {
174 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100175 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100176 return -ENODEV;
177 }
178 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
179 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100180 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100181 return -EPERM;
182 }
183 if (!(rdev->constraints->valid_modes_mask & mode)) {
184 printk(KERN_ERR "%s: invalid mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100185 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100186 return -EINVAL;
187 }
188 return 0;
189}
190
191/* dynamic regulator mode switching constraint check */
192static int regulator_check_drms(struct regulator_dev *rdev)
193{
194 if (!rdev->constraints) {
195 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100196 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100197 return -ENODEV;
198 }
199 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
200 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100201 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100202 return -EPERM;
203 }
204 return 0;
205}
206
207static ssize_t device_requested_uA_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 struct regulator *regulator;
211
212 regulator = get_device_regulator(dev);
213 if (regulator == NULL)
214 return 0;
215
216 return sprintf(buf, "%d\n", regulator->uA_load);
217}
218
219static ssize_t regulator_uV_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100222 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100223 ssize_t ret;
224
225 mutex_lock(&rdev->mutex);
226 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
227 mutex_unlock(&rdev->mutex);
228
229 return ret;
230}
David Brownell7ad68e22008-11-11 17:39:02 -0800231static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100232
233static ssize_t regulator_uA_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
235{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100236 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100237
238 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
239}
David Brownell7ad68e22008-11-11 17:39:02 -0800240static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100241
Mark Brownbc558a62008-10-10 15:33:20 +0100242static ssize_t regulator_name_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
244{
245 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100246
Mark Brown1083c392009-10-22 16:31:32 +0100247 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100248}
249
David Brownell4fca9542008-11-11 17:38:53 -0800250static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100251{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252 switch (mode) {
253 case REGULATOR_MODE_FAST:
254 return sprintf(buf, "fast\n");
255 case REGULATOR_MODE_NORMAL:
256 return sprintf(buf, "normal\n");
257 case REGULATOR_MODE_IDLE:
258 return sprintf(buf, "idle\n");
259 case REGULATOR_MODE_STANDBY:
260 return sprintf(buf, "standby\n");
261 }
262 return sprintf(buf, "unknown\n");
263}
264
David Brownell4fca9542008-11-11 17:38:53 -0800265static ssize_t regulator_opmode_show(struct device *dev,
266 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100267{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100268 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100269
David Brownell4fca9542008-11-11 17:38:53 -0800270 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
271}
David Brownell7ad68e22008-11-11 17:39:02 -0800272static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800273
274static ssize_t regulator_print_state(char *buf, int state)
275{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100276 if (state > 0)
277 return sprintf(buf, "enabled\n");
278 else if (state == 0)
279 return sprintf(buf, "disabled\n");
280 else
281 return sprintf(buf, "unknown\n");
282}
283
David Brownell4fca9542008-11-11 17:38:53 -0800284static ssize_t regulator_state_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
286{
287 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100288 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800289
Mark Brown93325462009-08-03 18:49:56 +0100290 mutex_lock(&rdev->mutex);
291 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
292 mutex_unlock(&rdev->mutex);
293
294 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800295}
David Brownell7ad68e22008-11-11 17:39:02 -0800296static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800297
David Brownell853116a2009-01-14 23:03:17 -0800298static ssize_t regulator_status_show(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
301 struct regulator_dev *rdev = dev_get_drvdata(dev);
302 int status;
303 char *label;
304
305 status = rdev->desc->ops->get_status(rdev);
306 if (status < 0)
307 return status;
308
309 switch (status) {
310 case REGULATOR_STATUS_OFF:
311 label = "off";
312 break;
313 case REGULATOR_STATUS_ON:
314 label = "on";
315 break;
316 case REGULATOR_STATUS_ERROR:
317 label = "error";
318 break;
319 case REGULATOR_STATUS_FAST:
320 label = "fast";
321 break;
322 case REGULATOR_STATUS_NORMAL:
323 label = "normal";
324 break;
325 case REGULATOR_STATUS_IDLE:
326 label = "idle";
327 break;
328 case REGULATOR_STATUS_STANDBY:
329 label = "standby";
330 break;
331 default:
332 return -ERANGE;
333 }
334
335 return sprintf(buf, "%s\n", label);
336}
337static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
338
Liam Girdwood414c70c2008-04-30 15:59:04 +0100339static ssize_t regulator_min_uA_show(struct device *dev,
340 struct device_attribute *attr, char *buf)
341{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100342 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100343
344 if (!rdev->constraints)
345 return sprintf(buf, "constraint not defined\n");
346
347 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
348}
David Brownell7ad68e22008-11-11 17:39:02 -0800349static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100350
351static ssize_t regulator_max_uA_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100354 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100355
356 if (!rdev->constraints)
357 return sprintf(buf, "constraint not defined\n");
358
359 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
360}
David Brownell7ad68e22008-11-11 17:39:02 -0800361static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100362
363static ssize_t regulator_min_uV_show(struct device *dev,
364 struct device_attribute *attr, char *buf)
365{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100366 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100367
368 if (!rdev->constraints)
369 return sprintf(buf, "constraint not defined\n");
370
371 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
372}
David Brownell7ad68e22008-11-11 17:39:02 -0800373static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100374
375static ssize_t regulator_max_uV_show(struct device *dev,
376 struct device_attribute *attr, char *buf)
377{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100378 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100379
380 if (!rdev->constraints)
381 return sprintf(buf, "constraint not defined\n");
382
383 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
384}
David Brownell7ad68e22008-11-11 17:39:02 -0800385static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100386
387static ssize_t regulator_total_uA_show(struct device *dev,
388 struct device_attribute *attr, char *buf)
389{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100390 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100391 struct regulator *regulator;
392 int uA = 0;
393
394 mutex_lock(&rdev->mutex);
395 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100396 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100397 mutex_unlock(&rdev->mutex);
398 return sprintf(buf, "%d\n", uA);
399}
David Brownell7ad68e22008-11-11 17:39:02 -0800400static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100401
402static ssize_t regulator_num_users_show(struct device *dev,
403 struct device_attribute *attr, char *buf)
404{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100405 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100406 return sprintf(buf, "%d\n", rdev->use_count);
407}
408
409static ssize_t regulator_type_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
411{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100412 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100413
414 switch (rdev->desc->type) {
415 case REGULATOR_VOLTAGE:
416 return sprintf(buf, "voltage\n");
417 case REGULATOR_CURRENT:
418 return sprintf(buf, "current\n");
419 }
420 return sprintf(buf, "unknown\n");
421}
422
423static ssize_t regulator_suspend_mem_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_mem.uV);
429}
David Brownell7ad68e22008-11-11 17:39:02 -0800430static DEVICE_ATTR(suspend_mem_microvolts, 0444,
431 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100432
433static ssize_t regulator_suspend_disk_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_disk.uV);
439}
David Brownell7ad68e22008-11-11 17:39:02 -0800440static DEVICE_ATTR(suspend_disk_microvolts, 0444,
441 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
443static ssize_t regulator_suspend_standby_uV_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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
449}
David Brownell7ad68e22008-11-11 17:39:02 -0800450static DEVICE_ATTR(suspend_standby_microvolts, 0444,
451 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100452
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
455{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100456 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100457
David Brownell4fca9542008-11-11 17:38:53 -0800458 return regulator_print_opmode(buf,
459 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460}
David Brownell7ad68e22008-11-11 17:39:02 -0800461static DEVICE_ATTR(suspend_mem_mode, 0444,
462 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100463
464static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
465 struct device_attribute *attr, char *buf)
466{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100467 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100468
David Brownell4fca9542008-11-11 17:38:53 -0800469 return regulator_print_opmode(buf,
470 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100471}
David Brownell7ad68e22008-11-11 17:39:02 -0800472static DEVICE_ATTR(suspend_disk_mode, 0444,
473 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474
475static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
477{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100478 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100479
David Brownell4fca9542008-11-11 17:38:53 -0800480 return regulator_print_opmode(buf,
481 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482}
David Brownell7ad68e22008-11-11 17:39:02 -0800483static DEVICE_ATTR(suspend_standby_mode, 0444,
484 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485
486static ssize_t regulator_suspend_mem_state_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
488{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100489 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100490
David Brownell4fca9542008-11-11 17:38:53 -0800491 return regulator_print_state(buf,
492 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493}
David Brownell7ad68e22008-11-11 17:39:02 -0800494static DEVICE_ATTR(suspend_mem_state, 0444,
495 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100496
497static ssize_t regulator_suspend_disk_state_show(struct device *dev,
498 struct device_attribute *attr, char *buf)
499{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100500 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100501
David Brownell4fca9542008-11-11 17:38:53 -0800502 return regulator_print_state(buf,
503 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504}
David Brownell7ad68e22008-11-11 17:39:02 -0800505static DEVICE_ATTR(suspend_disk_state, 0444,
506 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507
508static ssize_t regulator_suspend_standby_state_show(struct device *dev,
509 struct device_attribute *attr, char *buf)
510{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100511 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100512
David Brownell4fca9542008-11-11 17:38:53 -0800513 return regulator_print_state(buf,
514 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515}
David Brownell7ad68e22008-11-11 17:39:02 -0800516static DEVICE_ATTR(suspend_standby_state, 0444,
517 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100518
David Brownell7ad68e22008-11-11 17:39:02 -0800519
520/*
521 * These are the only attributes are present for all regulators.
522 * Other attributes are a function of regulator functionality.
523 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100525 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
527 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528 __ATTR_NULL,
529};
530
531static void regulator_dev_release(struct device *dev)
532{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100533 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534 kfree(rdev);
535}
536
537static struct class regulator_class = {
538 .name = "regulator",
539 .dev_release = regulator_dev_release,
540 .dev_attrs = regulator_dev_attrs,
541};
542
543/* Calculate the new optimum regulator operating mode based on the new total
544 * consumer load. All locks held by caller */
545static void drms_uA_update(struct regulator_dev *rdev)
546{
547 struct regulator *sibling;
548 int current_uA = 0, output_uV, input_uV, err;
549 unsigned int mode;
550
551 err = regulator_check_drms(rdev);
552 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300553 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
554 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555
556 /* get output voltage */
557 output_uV = rdev->desc->ops->get_voltage(rdev);
558 if (output_uV <= 0)
559 return;
560
561 /* get input voltage */
562 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
563 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
564 else
565 input_uV = rdev->constraints->input_uV;
566 if (input_uV <= 0)
567 return;
568
569 /* calc total requested load */
570 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100571 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572
573 /* now get the optimum mode for our new total regulator load */
574 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
575 output_uV, current_uA);
576
577 /* check the new mode is allowed */
578 err = regulator_check_mode(rdev, mode);
579 if (err == 0)
580 rdev->desc->ops->set_mode(rdev, mode);
581}
582
583static int suspend_set_state(struct regulator_dev *rdev,
584 struct regulator_state *rstate)
585{
586 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100587 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588
Mark Brown638f85c2009-10-22 16:31:33 +0100589 can_set_state = rdev->desc->ops->set_suspend_enable &&
590 rdev->desc->ops->set_suspend_disable;
591
592 /* If we have no suspend mode configration don't set anything;
593 * only warn if the driver actually makes the suspend mode
594 * configurable.
595 */
596 if (!rstate->enabled && !rstate->disabled) {
597 if (can_set_state)
598 printk(KERN_WARNING "%s: No configuration for %s\n",
599 __func__, rdev_get_name(rdev));
600 return 0;
601 }
602
603 if (rstate->enabled && rstate->disabled) {
604 printk(KERN_ERR "%s: invalid configuration for %s\n",
605 __func__, rdev_get_name(rdev));
606 return -EINVAL;
607 }
608
609 if (!can_set_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100610 printk(KERN_ERR "%s: no way to set suspend state\n",
611 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100613 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100614
615 if (rstate->enabled)
616 ret = rdev->desc->ops->set_suspend_enable(rdev);
617 else
618 ret = rdev->desc->ops->set_suspend_disable(rdev);
619 if (ret < 0) {
620 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
621 return ret;
622 }
623
624 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
625 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
626 if (ret < 0) {
627 printk(KERN_ERR "%s: failed to set voltage\n",
628 __func__);
629 return ret;
630 }
631 }
632
633 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
634 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
635 if (ret < 0) {
636 printk(KERN_ERR "%s: failed to set mode\n", __func__);
637 return ret;
638 }
639 }
640 return ret;
641}
642
643/* locks held by caller */
644static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
645{
646 if (!rdev->constraints)
647 return -EINVAL;
648
649 switch (state) {
650 case PM_SUSPEND_STANDBY:
651 return suspend_set_state(rdev,
652 &rdev->constraints->state_standby);
653 case PM_SUSPEND_MEM:
654 return suspend_set_state(rdev,
655 &rdev->constraints->state_mem);
656 case PM_SUSPEND_MAX:
657 return suspend_set_state(rdev,
658 &rdev->constraints->state_disk);
659 default:
660 return -EINVAL;
661 }
662}
663
664static void print_constraints(struct regulator_dev *rdev)
665{
666 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000667 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100668 int count = 0;
669 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100670
Mark Brown8f031b42009-10-22 16:31:31 +0100671 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100672 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100673 count += sprintf(buf + count, "%d mV ",
674 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100675 else
Mark Brown8f031b42009-10-22 16:31:31 +0100676 count += sprintf(buf + count, "%d <--> %d mV ",
677 constraints->min_uV / 1000,
678 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100679 }
Mark Brown8f031b42009-10-22 16:31:31 +0100680
681 if (!constraints->min_uV ||
682 constraints->min_uV != constraints->max_uV) {
683 ret = _regulator_get_voltage(rdev);
684 if (ret > 0)
685 count += sprintf(buf + count, "at %d mV ", ret / 1000);
686 }
687
688 if (constraints->min_uA && constraints->max_uA) {
689 if (constraints->min_uA == constraints->max_uA)
690 count += sprintf(buf + count, "%d mA ",
691 constraints->min_uA / 1000);
692 else
693 count += sprintf(buf + count, "%d <--> %d mA ",
694 constraints->min_uA / 1000,
695 constraints->max_uA / 1000);
696 }
697
698 if (!constraints->min_uA ||
699 constraints->min_uA != constraints->max_uA) {
700 ret = _regulator_get_current_limit(rdev);
701 if (ret > 0)
702 count += sprintf(buf + count, "at %d uA ", ret / 1000);
703 }
704
Liam Girdwood414c70c2008-04-30 15:59:04 +0100705 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
706 count += sprintf(buf + count, "fast ");
707 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
708 count += sprintf(buf + count, "normal ");
709 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
710 count += sprintf(buf + count, "idle ");
711 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
712 count += sprintf(buf + count, "standby");
713
Mark Brown1083c392009-10-22 16:31:32 +0100714 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100715}
716
Mark Browne79055d2009-10-19 15:53:50 +0100717static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100718 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100719{
720 struct regulator_ops *ops = rdev->desc->ops;
Mark Brown1083c392009-10-22 16:31:32 +0100721 const char *name = rdev_get_name(rdev);
Mark Brownaf5866c2009-10-22 16:31:30 +0100722 int ret;
723
724 /* do we need to apply the constraint voltage */
725 if (rdev->constraints->apply_uV &&
726 rdev->constraints->min_uV == rdev->constraints->max_uV &&
727 ops->set_voltage) {
728 ret = ops->set_voltage(rdev,
729 rdev->constraints->min_uV, rdev->constraints->max_uV);
730 if (ret < 0) {
731 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
732 __func__,
733 rdev->constraints->min_uV, name);
734 rdev->constraints = NULL;
735 return ret;
736 }
737 }
Mark Browne79055d2009-10-19 15:53:50 +0100738
739 /* constrain machine-level voltage specs to fit
740 * the actual range supported by this regulator.
741 */
742 if (ops->list_voltage && rdev->desc->n_voltages) {
743 int count = rdev->desc->n_voltages;
744 int i;
745 int min_uV = INT_MAX;
746 int max_uV = INT_MIN;
747 int cmin = constraints->min_uV;
748 int cmax = constraints->max_uV;
749
750 /* it's safe to autoconfigure fixed-voltage supplies
751 and the constraints are used by list_voltage. */
752 if (count == 1 && !cmin) {
753 cmin = 1;
754 cmax = INT_MAX;
755 constraints->min_uV = cmin;
756 constraints->max_uV = cmax;
757 }
758
759 /* voltage constraints are optional */
760 if ((cmin == 0) && (cmax == 0))
761 return 0;
762
763 /* else require explicit machine-level constraints */
764 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
765 pr_err("%s: %s '%s' voltage constraints\n",
766 __func__, "invalid", name);
767 return -EINVAL;
768 }
769
770 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
771 for (i = 0; i < count; i++) {
772 int value;
773
774 value = ops->list_voltage(rdev, i);
775 if (value <= 0)
776 continue;
777
778 /* maybe adjust [min_uV..max_uV] */
779 if (value >= cmin && value < min_uV)
780 min_uV = value;
781 if (value <= cmax && value > max_uV)
782 max_uV = value;
783 }
784
785 /* final: [min_uV..max_uV] valid iff constraints valid */
786 if (max_uV < min_uV) {
787 pr_err("%s: %s '%s' voltage constraints\n",
788 __func__, "unsupportable", name);
789 return -EINVAL;
790 }
791
792 /* use regulator's subset of machine constraints */
793 if (constraints->min_uV < min_uV) {
794 pr_debug("%s: override '%s' %s, %d -> %d\n",
795 __func__, name, "min_uV",
796 constraints->min_uV, min_uV);
797 constraints->min_uV = min_uV;
798 }
799 if (constraints->max_uV > max_uV) {
800 pr_debug("%s: override '%s' %s, %d -> %d\n",
801 __func__, name, "max_uV",
802 constraints->max_uV, max_uV);
803 constraints->max_uV = max_uV;
804 }
805 }
806
807 return 0;
808}
809
Liam Girdwooda5766f12008-10-10 13:22:20 +0100810/**
811 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000812 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000813 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100814 *
815 * Allows platform initialisation code to define and constrain
816 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
817 * Constraints *must* be set by platform code in order for some
818 * regulator operations to proceed i.e. set_voltage, set_current_limit,
819 * set_mode.
820 */
821static int set_machine_constraints(struct regulator_dev *rdev,
822 struct regulation_constraints *constraints)
823{
824 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100825 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100826 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100827
Mark Brownaf5866c2009-10-22 16:31:30 +0100828 rdev->constraints = constraints;
829
Mark Brown1083c392009-10-22 16:31:32 +0100830 name = rdev_get_name(rdev);
831
832 ret = machine_constraints_voltage(rdev, constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100833 if (ret != 0)
834 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800835
Liam Girdwooda5766f12008-10-10 13:22:20 +0100836 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100837 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100838 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100839 if (ret < 0) {
840 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
841 __func__, name);
842 rdev->constraints = NULL;
843 goto out;
844 }
845 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100846
Mark Browna3084662009-02-26 19:24:19 +0000847 if (constraints->initial_mode) {
848 if (!ops->set_mode) {
849 printk(KERN_ERR "%s: no set_mode operation for %s\n",
850 __func__, name);
851 ret = -EINVAL;
852 goto out;
853 }
854
855 ret = ops->set_mode(rdev, constraints->initial_mode);
856 if (ret < 0) {
857 printk(KERN_ERR
858 "%s: failed to set initial mode for %s: %d\n",
859 __func__, name, ret);
860 goto out;
861 }
862 }
863
Mark Browncacf90f2009-03-02 16:32:46 +0000864 /* If the constraints say the regulator should be on at this point
865 * and we have control then make sure it is enabled.
866 */
867 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100868 ret = ops->enable(rdev);
869 if (ret < 0) {
870 printk(KERN_ERR "%s: failed to enable %s\n",
871 __func__, name);
872 rdev->constraints = NULL;
873 goto out;
874 }
875 }
876
Liam Girdwooda5766f12008-10-10 13:22:20 +0100877 print_constraints(rdev);
878out:
879 return ret;
880}
881
882/**
883 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000884 * @rdev: regulator name
885 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100886 *
887 * Called by platform initialisation code to set the supply regulator for this
888 * regulator. This ensures that a regulators supply will also be enabled by the
889 * core if it's child is enabled.
890 */
891static int set_supply(struct regulator_dev *rdev,
892 struct regulator_dev *supply_rdev)
893{
894 int err;
895
896 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
897 "supply");
898 if (err) {
899 printk(KERN_ERR
900 "%s: could not add device link %s err %d\n",
901 __func__, supply_rdev->dev.kobj.name, err);
902 goto out;
903 }
904 rdev->supply = supply_rdev;
905 list_add(&rdev->slist, &supply_rdev->supply_list);
906out:
907 return err;
908}
909
910/**
911 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000912 * @rdev: regulator source
913 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100914 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000915 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100916 *
917 * Allows platform initialisation code to map physical regulator
918 * sources to symbolic names for supplies for use by devices. Devices
919 * should use these symbolic names to request regulators, avoiding the
920 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100921 *
922 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100923 */
924static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100925 struct device *consumer_dev, const char *consumer_dev_name,
926 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100927{
928 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100929 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100930
Mark Brown40f92442009-06-17 17:56:39 +0100931 if (consumer_dev && consumer_dev_name)
932 return -EINVAL;
933
934 if (!consumer_dev_name && consumer_dev)
935 consumer_dev_name = dev_name(consumer_dev);
936
Liam Girdwooda5766f12008-10-10 13:22:20 +0100937 if (supply == NULL)
938 return -EINVAL;
939
Mark Brown9ed20992009-07-21 16:00:26 +0100940 if (consumer_dev_name != NULL)
941 has_dev = 1;
942 else
943 has_dev = 0;
944
David Brownell6001e132008-12-31 12:54:19 +0000945 list_for_each_entry(node, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100946 if (consumer_dev_name != node->dev_name)
David Brownell6001e132008-12-31 12:54:19 +0000947 continue;
948 if (strcmp(node->supply, supply) != 0)
949 continue;
950
951 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
952 dev_name(&node->regulator->dev),
953 node->regulator->desc->name,
954 supply,
Mark Brown1083c392009-10-22 16:31:32 +0100955 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000956 return -EBUSY;
957 }
958
Mark Brown9ed20992009-07-21 16:00:26 +0100959 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100960 if (node == NULL)
961 return -ENOMEM;
962
963 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100964 node->supply = supply;
965
Mark Brown9ed20992009-07-21 16:00:26 +0100966 if (has_dev) {
967 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
968 if (node->dev_name == NULL) {
969 kfree(node);
970 return -ENOMEM;
971 }
Mark Brown40f92442009-06-17 17:56:39 +0100972 }
973
Liam Girdwooda5766f12008-10-10 13:22:20 +0100974 list_add(&node->list, &regulator_map_list);
975 return 0;
976}
977
978static void unset_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100979 const char *consumer_dev_name, struct device *consumer_dev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100980{
981 struct regulator_map *node, *n;
982
Mark Brown40f92442009-06-17 17:56:39 +0100983 if (consumer_dev && !consumer_dev_name)
984 consumer_dev_name = dev_name(consumer_dev);
985
Liam Girdwooda5766f12008-10-10 13:22:20 +0100986 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100987 if (rdev != node->regulator)
988 continue;
989
990 if (consumer_dev_name && node->dev_name &&
991 strcmp(consumer_dev_name, node->dev_name))
992 continue;
993
994 list_del(&node->list);
995 kfree(node->dev_name);
996 kfree(node);
997 return;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100998 }
999}
1000
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001001static void unset_regulator_supplies(struct regulator_dev *rdev)
1002{
1003 struct regulator_map *node, *n;
1004
1005 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1006 if (rdev == node->regulator) {
1007 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001008 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001009 kfree(node);
1010 return;
1011 }
1012 }
1013}
1014
Liam Girdwood414c70c2008-04-30 15:59:04 +01001015#define REG_STR_SIZE 32
1016
1017static struct regulator *create_regulator(struct regulator_dev *rdev,
1018 struct device *dev,
1019 const char *supply_name)
1020{
1021 struct regulator *regulator;
1022 char buf[REG_STR_SIZE];
1023 int err, size;
1024
1025 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1026 if (regulator == NULL)
1027 return NULL;
1028
1029 mutex_lock(&rdev->mutex);
1030 regulator->rdev = rdev;
1031 list_add(&regulator->list, &rdev->consumer_list);
1032
1033 if (dev) {
1034 /* create a 'requested_microamps_name' sysfs entry */
1035 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1036 supply_name);
1037 if (size >= REG_STR_SIZE)
1038 goto overflow_err;
1039
1040 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001041 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001042 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1043 if (regulator->dev_attr.attr.name == NULL)
1044 goto attr_name_err;
1045
1046 regulator->dev_attr.attr.owner = THIS_MODULE;
1047 regulator->dev_attr.attr.mode = 0444;
1048 regulator->dev_attr.show = device_requested_uA_show;
1049 err = device_create_file(dev, &regulator->dev_attr);
1050 if (err < 0) {
1051 printk(KERN_WARNING "%s: could not add regulator_dev"
1052 " load sysfs\n", __func__);
1053 goto attr_name_err;
1054 }
1055
1056 /* also add a link to the device sysfs entry */
1057 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1058 dev->kobj.name, supply_name);
1059 if (size >= REG_STR_SIZE)
1060 goto attr_err;
1061
1062 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1063 if (regulator->supply_name == NULL)
1064 goto attr_err;
1065
1066 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1067 buf);
1068 if (err) {
1069 printk(KERN_WARNING
1070 "%s: could not add device link %s err %d\n",
1071 __func__, dev->kobj.name, err);
1072 device_remove_file(dev, &regulator->dev_attr);
1073 goto link_name_err;
1074 }
1075 }
1076 mutex_unlock(&rdev->mutex);
1077 return regulator;
1078link_name_err:
1079 kfree(regulator->supply_name);
1080attr_err:
1081 device_remove_file(regulator->dev, &regulator->dev_attr);
1082attr_name_err:
1083 kfree(regulator->dev_attr.attr.name);
1084overflow_err:
1085 list_del(&regulator->list);
1086 kfree(regulator);
1087 mutex_unlock(&rdev->mutex);
1088 return NULL;
1089}
1090
Mark Brown31aae2b2009-12-21 12:21:52 +00001091static int _regulator_get_enable_time(struct regulator_dev *rdev)
1092{
1093 if (!rdev->desc->ops->enable_time)
1094 return 0;
1095 return rdev->desc->ops->enable_time(rdev);
1096}
1097
Mark Brown5ffbd132009-07-21 16:00:23 +01001098/* Internal regulator request function */
1099static struct regulator *_regulator_get(struct device *dev, const char *id,
1100 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001101{
1102 struct regulator_dev *rdev;
1103 struct regulator_map *map;
1104 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001105 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001106 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001107
1108 if (id == NULL) {
1109 printk(KERN_ERR "regulator: get() with no identifier\n");
1110 return regulator;
1111 }
1112
Mark Brown40f92442009-06-17 17:56:39 +01001113 if (dev)
1114 devname = dev_name(dev);
1115
Liam Girdwood414c70c2008-04-30 15:59:04 +01001116 mutex_lock(&regulator_list_mutex);
1117
1118 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001119 /* If the mapping has a device set up it must match */
1120 if (map->dev_name &&
1121 (!devname || strcmp(map->dev_name, devname)))
1122 continue;
1123
1124 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001125 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001126 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001127 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001128 }
Mark Brown34abbd62010-02-12 10:18:08 +00001129
1130#ifdef CONFIG_REGULATOR_DUMMY
1131 if (!devname)
1132 devname = "deviceless";
1133
1134 /* If the board didn't flag that it was fully constrained then
1135 * substitute in a dummy regulator so consumers can continue.
1136 */
1137 if (!has_full_constraints) {
1138 pr_warning("%s supply %s not found, using dummy regulator\n",
1139 devname, id);
1140 rdev = dummy_regulator_rdev;
1141 goto found;
1142 }
1143#endif
1144
Liam Girdwood414c70c2008-04-30 15:59:04 +01001145 mutex_unlock(&regulator_list_mutex);
1146 return regulator;
1147
1148found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001149 if (rdev->exclusive) {
1150 regulator = ERR_PTR(-EPERM);
1151 goto out;
1152 }
1153
1154 if (exclusive && rdev->open_count) {
1155 regulator = ERR_PTR(-EBUSY);
1156 goto out;
1157 }
1158
Liam Girdwooda5766f12008-10-10 13:22:20 +01001159 if (!try_module_get(rdev->owner))
1160 goto out;
1161
Liam Girdwood414c70c2008-04-30 15:59:04 +01001162 regulator = create_regulator(rdev, dev, id);
1163 if (regulator == NULL) {
1164 regulator = ERR_PTR(-ENOMEM);
1165 module_put(rdev->owner);
1166 }
1167
Mark Brown5ffbd132009-07-21 16:00:23 +01001168 rdev->open_count++;
1169 if (exclusive) {
1170 rdev->exclusive = 1;
1171
1172 ret = _regulator_is_enabled(rdev);
1173 if (ret > 0)
1174 rdev->use_count = 1;
1175 else
1176 rdev->use_count = 0;
1177 }
1178
Liam Girdwooda5766f12008-10-10 13:22:20 +01001179out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001180 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001181
Liam Girdwood414c70c2008-04-30 15:59:04 +01001182 return regulator;
1183}
Mark Brown5ffbd132009-07-21 16:00:23 +01001184
1185/**
1186 * regulator_get - lookup and obtain a reference to a regulator.
1187 * @dev: device for regulator "consumer"
1188 * @id: Supply name or regulator ID.
1189 *
1190 * Returns a struct regulator corresponding to the regulator producer,
1191 * or IS_ERR() condition containing errno.
1192 *
1193 * Use of supply names configured via regulator_set_device_supply() is
1194 * strongly encouraged. It is recommended that the supply name used
1195 * should match the name used for the supply and/or the relevant
1196 * device pins in the datasheet.
1197 */
1198struct regulator *regulator_get(struct device *dev, const char *id)
1199{
1200 return _regulator_get(dev, id, 0);
1201}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001202EXPORT_SYMBOL_GPL(regulator_get);
1203
1204/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001205 * regulator_get_exclusive - obtain exclusive access to a regulator.
1206 * @dev: device for regulator "consumer"
1207 * @id: Supply name or regulator ID.
1208 *
1209 * Returns a struct regulator corresponding to the regulator producer,
1210 * or IS_ERR() condition containing errno. Other consumers will be
1211 * unable to obtain this reference is held and the use count for the
1212 * regulator will be initialised to reflect the current state of the
1213 * regulator.
1214 *
1215 * This is intended for use by consumers which cannot tolerate shared
1216 * use of the regulator such as those which need to force the
1217 * regulator off for correct operation of the hardware they are
1218 * controlling.
1219 *
1220 * Use of supply names configured via regulator_set_device_supply() is
1221 * strongly encouraged. It is recommended that the supply name used
1222 * should match the name used for the supply and/or the relevant
1223 * device pins in the datasheet.
1224 */
1225struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1226{
1227 return _regulator_get(dev, id, 1);
1228}
1229EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1230
1231/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001232 * regulator_put - "free" the regulator source
1233 * @regulator: regulator source
1234 *
1235 * Note: drivers must ensure that all regulator_enable calls made on this
1236 * regulator source are balanced by regulator_disable calls prior to calling
1237 * this function.
1238 */
1239void regulator_put(struct regulator *regulator)
1240{
1241 struct regulator_dev *rdev;
1242
1243 if (regulator == NULL || IS_ERR(regulator))
1244 return;
1245
Liam Girdwood414c70c2008-04-30 15:59:04 +01001246 mutex_lock(&regulator_list_mutex);
1247 rdev = regulator->rdev;
1248
1249 /* remove any sysfs entries */
1250 if (regulator->dev) {
1251 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1252 kfree(regulator->supply_name);
1253 device_remove_file(regulator->dev, &regulator->dev_attr);
1254 kfree(regulator->dev_attr.attr.name);
1255 }
1256 list_del(&regulator->list);
1257 kfree(regulator);
1258
Mark Brown5ffbd132009-07-21 16:00:23 +01001259 rdev->open_count--;
1260 rdev->exclusive = 0;
1261
Liam Girdwood414c70c2008-04-30 15:59:04 +01001262 module_put(rdev->owner);
1263 mutex_unlock(&regulator_list_mutex);
1264}
1265EXPORT_SYMBOL_GPL(regulator_put);
1266
Mark Brown9a2372f2009-08-03 18:49:57 +01001267static int _regulator_can_change_status(struct regulator_dev *rdev)
1268{
1269 if (!rdev->constraints)
1270 return 0;
1271
1272 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1273 return 1;
1274 else
1275 return 0;
1276}
1277
Liam Girdwood414c70c2008-04-30 15:59:04 +01001278/* locks held by regulator_enable() */
1279static int _regulator_enable(struct regulator_dev *rdev)
1280{
Mark Brown31aae2b2009-12-21 12:21:52 +00001281 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001282
1283 /* do we need to enable the supply regulator first */
1284 if (rdev->supply) {
1285 ret = _regulator_enable(rdev->supply);
1286 if (ret < 0) {
1287 printk(KERN_ERR "%s: failed to enable %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001288 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001289 return ret;
1290 }
1291 }
1292
1293 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001294 if (rdev->constraints &&
1295 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1296 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001297
Mark Brown9a2372f2009-08-03 18:49:57 +01001298 if (rdev->use_count == 0) {
1299 /* The regulator may on if it's not switchable or left on */
1300 ret = _regulator_is_enabled(rdev);
1301 if (ret == -EINVAL || ret == 0) {
1302 if (!_regulator_can_change_status(rdev))
1303 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001304
Mark Brown31aae2b2009-12-21 12:21:52 +00001305 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001306 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001307
1308 /* Query before enabling in case configuration
1309 * dependant. */
1310 ret = _regulator_get_enable_time(rdev);
1311 if (ret >= 0) {
1312 delay = ret;
1313 } else {
1314 printk(KERN_WARNING
1315 "%s: enable_time() failed for %s: %d\n",
1316 __func__, rdev_get_name(rdev),
1317 ret);
1318 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001319 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001320
1321 /* Allow the regulator to ramp; it would be useful
1322 * to extend this for bulk operations so that the
1323 * regulators can ramp together. */
1324 ret = rdev->desc->ops->enable(rdev);
1325 if (ret < 0)
1326 return ret;
1327
1328 if (delay >= 1000)
1329 mdelay(delay / 1000);
1330 else if (delay)
1331 udelay(delay);
1332
Linus Walleija7433cf2009-08-26 12:54:04 +02001333 } else if (ret < 0) {
Mark Brown9a2372f2009-08-03 18:49:57 +01001334 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001335 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001336 return ret;
1337 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001338 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001339 }
1340
Mark Brown9a2372f2009-08-03 18:49:57 +01001341 rdev->use_count++;
1342
1343 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001344}
1345
1346/**
1347 * regulator_enable - enable regulator output
1348 * @regulator: regulator source
1349 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001350 * Request that the regulator be enabled with the regulator output at
1351 * the predefined voltage or current value. Calls to regulator_enable()
1352 * must be balanced with calls to regulator_disable().
1353 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001354 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001355 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001356 */
1357int regulator_enable(struct regulator *regulator)
1358{
David Brownell412aec62008-11-16 11:44:46 -08001359 struct regulator_dev *rdev = regulator->rdev;
1360 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001361
David Brownell412aec62008-11-16 11:44:46 -08001362 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001363 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001364 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001365 return ret;
1366}
1367EXPORT_SYMBOL_GPL(regulator_enable);
1368
1369/* locks held by regulator_disable() */
1370static int _regulator_disable(struct regulator_dev *rdev)
1371{
1372 int ret = 0;
1373
David Brownellcd94b502009-03-11 16:43:34 -08001374 if (WARN(rdev->use_count <= 0,
1375 "unbalanced disables for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001376 rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001377 return -EIO;
1378
Liam Girdwood414c70c2008-04-30 15:59:04 +01001379 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001380 if (rdev->use_count == 1 &&
1381 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001382
1383 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001384 if (_regulator_can_change_status(rdev) &&
1385 rdev->desc->ops->disable) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001386 ret = rdev->desc->ops->disable(rdev);
1387 if (ret < 0) {
1388 printk(KERN_ERR "%s: failed to disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001389 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001390 return ret;
1391 }
Mark Brown84b68262009-12-01 21:12:27 +00001392
1393 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1394 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001395 }
1396
1397 /* decrease our supplies ref count and disable if required */
1398 if (rdev->supply)
1399 _regulator_disable(rdev->supply);
1400
1401 rdev->use_count = 0;
1402 } else if (rdev->use_count > 1) {
1403
1404 if (rdev->constraints &&
1405 (rdev->constraints->valid_ops_mask &
1406 REGULATOR_CHANGE_DRMS))
1407 drms_uA_update(rdev);
1408
1409 rdev->use_count--;
1410 }
1411 return ret;
1412}
1413
1414/**
1415 * regulator_disable - disable regulator output
1416 * @regulator: regulator source
1417 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001418 * Disable the regulator output voltage or current. Calls to
1419 * regulator_enable() must be balanced with calls to
1420 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001421 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001422 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001423 * devices have it enabled, the regulator device supports disabling and
1424 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001425 */
1426int regulator_disable(struct regulator *regulator)
1427{
David Brownell412aec62008-11-16 11:44:46 -08001428 struct regulator_dev *rdev = regulator->rdev;
1429 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001430
David Brownell412aec62008-11-16 11:44:46 -08001431 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001432 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001433 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001434 return ret;
1435}
1436EXPORT_SYMBOL_GPL(regulator_disable);
1437
1438/* locks held by regulator_force_disable() */
1439static int _regulator_force_disable(struct regulator_dev *rdev)
1440{
1441 int ret = 0;
1442
1443 /* force disable */
1444 if (rdev->desc->ops->disable) {
1445 /* ah well, who wants to live forever... */
1446 ret = rdev->desc->ops->disable(rdev);
1447 if (ret < 0) {
1448 printk(KERN_ERR "%s: failed to force disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001449 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001450 return ret;
1451 }
1452 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001453 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1454 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001455 }
1456
1457 /* decrease our supplies ref count and disable if required */
1458 if (rdev->supply)
1459 _regulator_disable(rdev->supply);
1460
1461 rdev->use_count = 0;
1462 return ret;
1463}
1464
1465/**
1466 * regulator_force_disable - force disable regulator output
1467 * @regulator: regulator source
1468 *
1469 * Forcibly disable the regulator output voltage or current.
1470 * NOTE: this *will* disable the regulator output even if other consumer
1471 * devices have it enabled. This should be used for situations when device
1472 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1473 */
1474int regulator_force_disable(struct regulator *regulator)
1475{
1476 int ret;
1477
1478 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001479 regulator->uA_load = 0;
1480 ret = _regulator_force_disable(regulator->rdev);
1481 mutex_unlock(&regulator->rdev->mutex);
1482 return ret;
1483}
1484EXPORT_SYMBOL_GPL(regulator_force_disable);
1485
1486static int _regulator_is_enabled(struct regulator_dev *rdev)
1487{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001488 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001489 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001490 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001491
Mark Brown93325462009-08-03 18:49:56 +01001492 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001493}
1494
1495/**
1496 * regulator_is_enabled - is the regulator output enabled
1497 * @regulator: regulator source
1498 *
David Brownell412aec62008-11-16 11:44:46 -08001499 * Returns positive if the regulator driver backing the source/client
1500 * has requested that the device be enabled, zero if it hasn't, else a
1501 * negative errno code.
1502 *
1503 * Note that the device backing this regulator handle can have multiple
1504 * users, so it might be enabled even if regulator_enable() was never
1505 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001506 */
1507int regulator_is_enabled(struct regulator *regulator)
1508{
Mark Brown93325462009-08-03 18:49:56 +01001509 int ret;
1510
1511 mutex_lock(&regulator->rdev->mutex);
1512 ret = _regulator_is_enabled(regulator->rdev);
1513 mutex_unlock(&regulator->rdev->mutex);
1514
1515 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516}
1517EXPORT_SYMBOL_GPL(regulator_is_enabled);
1518
1519/**
David Brownell4367cfd2009-02-26 11:48:36 -08001520 * regulator_count_voltages - count regulator_list_voltage() selectors
1521 * @regulator: regulator source
1522 *
1523 * Returns number of selectors, or negative errno. Selectors are
1524 * numbered starting at zero, and typically correspond to bitfields
1525 * in hardware registers.
1526 */
1527int regulator_count_voltages(struct regulator *regulator)
1528{
1529 struct regulator_dev *rdev = regulator->rdev;
1530
1531 return rdev->desc->n_voltages ? : -EINVAL;
1532}
1533EXPORT_SYMBOL_GPL(regulator_count_voltages);
1534
1535/**
1536 * regulator_list_voltage - enumerate supported voltages
1537 * @regulator: regulator source
1538 * @selector: identify voltage to list
1539 * Context: can sleep
1540 *
1541 * Returns a voltage that can be passed to @regulator_set_voltage(),
1542 * zero if this selector code can't be used on this sytem, or a
1543 * negative errno.
1544 */
1545int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1546{
1547 struct regulator_dev *rdev = regulator->rdev;
1548 struct regulator_ops *ops = rdev->desc->ops;
1549 int ret;
1550
1551 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1552 return -EINVAL;
1553
1554 mutex_lock(&rdev->mutex);
1555 ret = ops->list_voltage(rdev, selector);
1556 mutex_unlock(&rdev->mutex);
1557
1558 if (ret > 0) {
1559 if (ret < rdev->constraints->min_uV)
1560 ret = 0;
1561 else if (ret > rdev->constraints->max_uV)
1562 ret = 0;
1563 }
1564
1565 return ret;
1566}
1567EXPORT_SYMBOL_GPL(regulator_list_voltage);
1568
1569/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001570 * regulator_is_supported_voltage - check if a voltage range can be supported
1571 *
1572 * @regulator: Regulator to check.
1573 * @min_uV: Minimum required voltage in uV.
1574 * @max_uV: Maximum required voltage in uV.
1575 *
1576 * Returns a boolean or a negative error code.
1577 */
1578int regulator_is_supported_voltage(struct regulator *regulator,
1579 int min_uV, int max_uV)
1580{
1581 int i, voltages, ret;
1582
1583 ret = regulator_count_voltages(regulator);
1584 if (ret < 0)
1585 return ret;
1586 voltages = ret;
1587
1588 for (i = 0; i < voltages; i++) {
1589 ret = regulator_list_voltage(regulator, i);
1590
1591 if (ret >= min_uV && ret <= max_uV)
1592 return 1;
1593 }
1594
1595 return 0;
1596}
1597
1598/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001599 * regulator_set_voltage - set regulator output voltage
1600 * @regulator: regulator source
1601 * @min_uV: Minimum required voltage in uV
1602 * @max_uV: Maximum acceptable voltage in uV
1603 *
1604 * Sets a voltage regulator to the desired output voltage. This can be set
1605 * during any regulator state. IOW, regulator can be disabled or enabled.
1606 *
1607 * If the regulator is enabled then the voltage will change to the new value
1608 * immediately otherwise if the regulator is disabled the regulator will
1609 * output at the new voltage when enabled.
1610 *
1611 * NOTE: If the regulator is shared between several devices then the lowest
1612 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001613 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001614 * calling this function otherwise this call will fail.
1615 */
1616int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1617{
1618 struct regulator_dev *rdev = regulator->rdev;
1619 int ret;
1620
1621 mutex_lock(&rdev->mutex);
1622
1623 /* sanity check */
1624 if (!rdev->desc->ops->set_voltage) {
1625 ret = -EINVAL;
1626 goto out;
1627 }
1628
1629 /* constraints check */
1630 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1631 if (ret < 0)
1632 goto out;
1633 regulator->min_uV = min_uV;
1634 regulator->max_uV = max_uV;
1635 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1636
1637out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001638 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001639 mutex_unlock(&rdev->mutex);
1640 return ret;
1641}
1642EXPORT_SYMBOL_GPL(regulator_set_voltage);
1643
1644static int _regulator_get_voltage(struct regulator_dev *rdev)
1645{
1646 /* sanity check */
1647 if (rdev->desc->ops->get_voltage)
1648 return rdev->desc->ops->get_voltage(rdev);
1649 else
1650 return -EINVAL;
1651}
1652
1653/**
1654 * regulator_get_voltage - get regulator output voltage
1655 * @regulator: regulator source
1656 *
1657 * This returns the current regulator voltage in uV.
1658 *
1659 * NOTE: If the regulator is disabled it will return the voltage value. This
1660 * function should not be used to determine regulator state.
1661 */
1662int regulator_get_voltage(struct regulator *regulator)
1663{
1664 int ret;
1665
1666 mutex_lock(&regulator->rdev->mutex);
1667
1668 ret = _regulator_get_voltage(regulator->rdev);
1669
1670 mutex_unlock(&regulator->rdev->mutex);
1671
1672 return ret;
1673}
1674EXPORT_SYMBOL_GPL(regulator_get_voltage);
1675
1676/**
1677 * regulator_set_current_limit - set regulator output current limit
1678 * @regulator: regulator source
1679 * @min_uA: Minimuum supported current in uA
1680 * @max_uA: Maximum supported current in uA
1681 *
1682 * Sets current sink to the desired output current. This can be set during
1683 * any regulator state. IOW, regulator can be disabled or enabled.
1684 *
1685 * If the regulator is enabled then the current will change to the new value
1686 * immediately otherwise if the regulator is disabled the regulator will
1687 * output at the new current when enabled.
1688 *
1689 * NOTE: Regulator system constraints must be set for this regulator before
1690 * calling this function otherwise this call will fail.
1691 */
1692int regulator_set_current_limit(struct regulator *regulator,
1693 int min_uA, int max_uA)
1694{
1695 struct regulator_dev *rdev = regulator->rdev;
1696 int ret;
1697
1698 mutex_lock(&rdev->mutex);
1699
1700 /* sanity check */
1701 if (!rdev->desc->ops->set_current_limit) {
1702 ret = -EINVAL;
1703 goto out;
1704 }
1705
1706 /* constraints check */
1707 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1708 if (ret < 0)
1709 goto out;
1710
1711 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1712out:
1713 mutex_unlock(&rdev->mutex);
1714 return ret;
1715}
1716EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1717
1718static int _regulator_get_current_limit(struct regulator_dev *rdev)
1719{
1720 int ret;
1721
1722 mutex_lock(&rdev->mutex);
1723
1724 /* sanity check */
1725 if (!rdev->desc->ops->get_current_limit) {
1726 ret = -EINVAL;
1727 goto out;
1728 }
1729
1730 ret = rdev->desc->ops->get_current_limit(rdev);
1731out:
1732 mutex_unlock(&rdev->mutex);
1733 return ret;
1734}
1735
1736/**
1737 * regulator_get_current_limit - get regulator output current
1738 * @regulator: regulator source
1739 *
1740 * This returns the current supplied by the specified current sink in uA.
1741 *
1742 * NOTE: If the regulator is disabled it will return the current value. This
1743 * function should not be used to determine regulator state.
1744 */
1745int regulator_get_current_limit(struct regulator *regulator)
1746{
1747 return _regulator_get_current_limit(regulator->rdev);
1748}
1749EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1750
1751/**
1752 * regulator_set_mode - set regulator operating mode
1753 * @regulator: regulator source
1754 * @mode: operating mode - one of the REGULATOR_MODE constants
1755 *
1756 * Set regulator operating mode to increase regulator efficiency or improve
1757 * regulation performance.
1758 *
1759 * NOTE: Regulator system constraints must be set for this regulator before
1760 * calling this function otherwise this call will fail.
1761 */
1762int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1763{
1764 struct regulator_dev *rdev = regulator->rdev;
1765 int ret;
1766
1767 mutex_lock(&rdev->mutex);
1768
1769 /* sanity check */
1770 if (!rdev->desc->ops->set_mode) {
1771 ret = -EINVAL;
1772 goto out;
1773 }
1774
1775 /* constraints check */
1776 ret = regulator_check_mode(rdev, mode);
1777 if (ret < 0)
1778 goto out;
1779
1780 ret = rdev->desc->ops->set_mode(rdev, mode);
1781out:
1782 mutex_unlock(&rdev->mutex);
1783 return ret;
1784}
1785EXPORT_SYMBOL_GPL(regulator_set_mode);
1786
1787static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1788{
1789 int ret;
1790
1791 mutex_lock(&rdev->mutex);
1792
1793 /* sanity check */
1794 if (!rdev->desc->ops->get_mode) {
1795 ret = -EINVAL;
1796 goto out;
1797 }
1798
1799 ret = rdev->desc->ops->get_mode(rdev);
1800out:
1801 mutex_unlock(&rdev->mutex);
1802 return ret;
1803}
1804
1805/**
1806 * regulator_get_mode - get regulator operating mode
1807 * @regulator: regulator source
1808 *
1809 * Get the current regulator operating mode.
1810 */
1811unsigned int regulator_get_mode(struct regulator *regulator)
1812{
1813 return _regulator_get_mode(regulator->rdev);
1814}
1815EXPORT_SYMBOL_GPL(regulator_get_mode);
1816
1817/**
1818 * regulator_set_optimum_mode - set regulator optimum operating mode
1819 * @regulator: regulator source
1820 * @uA_load: load current
1821 *
1822 * Notifies the regulator core of a new device load. This is then used by
1823 * DRMS (if enabled by constraints) to set the most efficient regulator
1824 * operating mode for the new regulator loading.
1825 *
1826 * Consumer devices notify their supply regulator of the maximum power
1827 * they will require (can be taken from device datasheet in the power
1828 * consumption tables) when they change operational status and hence power
1829 * state. Examples of operational state changes that can affect power
1830 * consumption are :-
1831 *
1832 * o Device is opened / closed.
1833 * o Device I/O is about to begin or has just finished.
1834 * o Device is idling in between work.
1835 *
1836 * This information is also exported via sysfs to userspace.
1837 *
1838 * DRMS will sum the total requested load on the regulator and change
1839 * to the most efficient operating mode if platform constraints allow.
1840 *
1841 * Returns the new regulator mode or error.
1842 */
1843int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1844{
1845 struct regulator_dev *rdev = regulator->rdev;
1846 struct regulator *consumer;
1847 int ret, output_uV, input_uV, total_uA_load = 0;
1848 unsigned int mode;
1849
1850 mutex_lock(&rdev->mutex);
1851
1852 regulator->uA_load = uA_load;
1853 ret = regulator_check_drms(rdev);
1854 if (ret < 0)
1855 goto out;
1856 ret = -EINVAL;
1857
1858 /* sanity check */
1859 if (!rdev->desc->ops->get_optimum_mode)
1860 goto out;
1861
1862 /* get output voltage */
1863 output_uV = rdev->desc->ops->get_voltage(rdev);
1864 if (output_uV <= 0) {
1865 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001866 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001867 goto out;
1868 }
1869
1870 /* get input voltage */
1871 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1872 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1873 else
1874 input_uV = rdev->constraints->input_uV;
1875 if (input_uV <= 0) {
1876 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001877 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001878 goto out;
1879 }
1880
1881 /* calc total requested load for this regulator */
1882 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001883 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001884
1885 mode = rdev->desc->ops->get_optimum_mode(rdev,
1886 input_uV, output_uV,
1887 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001888 ret = regulator_check_mode(rdev, mode);
1889 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001890 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
Mark Brown1083c392009-10-22 16:31:32 +01001891 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
Liam Girdwood414c70c2008-04-30 15:59:04 +01001892 total_uA_load, input_uV, output_uV);
1893 goto out;
1894 }
1895
1896 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001897 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001898 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001899 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001900 goto out;
1901 }
1902 ret = mode;
1903out:
1904 mutex_unlock(&rdev->mutex);
1905 return ret;
1906}
1907EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1908
1909/**
1910 * regulator_register_notifier - register regulator event notifier
1911 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001912 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001913 *
1914 * Register notifier block to receive regulator events.
1915 */
1916int regulator_register_notifier(struct regulator *regulator,
1917 struct notifier_block *nb)
1918{
1919 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1920 nb);
1921}
1922EXPORT_SYMBOL_GPL(regulator_register_notifier);
1923
1924/**
1925 * regulator_unregister_notifier - unregister regulator event notifier
1926 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001927 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001928 *
1929 * Unregister regulator event notifier block.
1930 */
1931int regulator_unregister_notifier(struct regulator *regulator,
1932 struct notifier_block *nb)
1933{
1934 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1935 nb);
1936}
1937EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1938
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001939/* notify regulator consumers and downstream regulator consumers.
1940 * Note mutex must be held by caller.
1941 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001942static void _notifier_call_chain(struct regulator_dev *rdev,
1943 unsigned long event, void *data)
1944{
1945 struct regulator_dev *_rdev;
1946
1947 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001948 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001949
1950 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001951 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01001952 mutex_lock(&_rdev->mutex);
1953 _notifier_call_chain(_rdev, event, data);
1954 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001955 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001956}
1957
1958/**
1959 * regulator_bulk_get - get multiple regulator consumers
1960 *
1961 * @dev: Device to supply
1962 * @num_consumers: Number of consumers to register
1963 * @consumers: Configuration of consumers; clients are stored here.
1964 *
1965 * @return 0 on success, an errno on failure.
1966 *
1967 * This helper function allows drivers to get several regulator
1968 * consumers in one operation. If any of the regulators cannot be
1969 * acquired then any regulators that were allocated will be freed
1970 * before returning to the caller.
1971 */
1972int regulator_bulk_get(struct device *dev, int num_consumers,
1973 struct regulator_bulk_data *consumers)
1974{
1975 int i;
1976 int ret;
1977
1978 for (i = 0; i < num_consumers; i++)
1979 consumers[i].consumer = NULL;
1980
1981 for (i = 0; i < num_consumers; i++) {
1982 consumers[i].consumer = regulator_get(dev,
1983 consumers[i].supply);
1984 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001985 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01001986 dev_err(dev, "Failed to get supply '%s': %d\n",
1987 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001988 consumers[i].consumer = NULL;
1989 goto err;
1990 }
1991 }
1992
1993 return 0;
1994
1995err:
1996 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1997 regulator_put(consumers[i].consumer);
1998
1999 return ret;
2000}
2001EXPORT_SYMBOL_GPL(regulator_bulk_get);
2002
2003/**
2004 * regulator_bulk_enable - enable multiple regulator consumers
2005 *
2006 * @num_consumers: Number of consumers
2007 * @consumers: Consumer data; clients are stored here.
2008 * @return 0 on success, an errno on failure
2009 *
2010 * This convenience API allows consumers to enable multiple regulator
2011 * clients in a single API call. If any consumers cannot be enabled
2012 * then any others that were enabled will be disabled again prior to
2013 * return.
2014 */
2015int regulator_bulk_enable(int num_consumers,
2016 struct regulator_bulk_data *consumers)
2017{
2018 int i;
2019 int ret;
2020
2021 for (i = 0; i < num_consumers; i++) {
2022 ret = regulator_enable(consumers[i].consumer);
2023 if (ret != 0)
2024 goto err;
2025 }
2026
2027 return 0;
2028
2029err:
Mark Brown5b307622009-10-13 13:06:49 +01002030 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002031 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002032 regulator_disable(consumers[i].consumer);
2033
2034 return ret;
2035}
2036EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2037
2038/**
2039 * regulator_bulk_disable - disable multiple regulator consumers
2040 *
2041 * @num_consumers: Number of consumers
2042 * @consumers: Consumer data; clients are stored here.
2043 * @return 0 on success, an errno on failure
2044 *
2045 * This convenience API allows consumers to disable multiple regulator
2046 * clients in a single API call. If any consumers cannot be enabled
2047 * then any others that were disabled will be disabled again prior to
2048 * return.
2049 */
2050int regulator_bulk_disable(int num_consumers,
2051 struct regulator_bulk_data *consumers)
2052{
2053 int i;
2054 int ret;
2055
2056 for (i = 0; i < num_consumers; i++) {
2057 ret = regulator_disable(consumers[i].consumer);
2058 if (ret != 0)
2059 goto err;
2060 }
2061
2062 return 0;
2063
2064err:
Mark Brown5b307622009-10-13 13:06:49 +01002065 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2066 ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002067 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002068 regulator_enable(consumers[i].consumer);
2069
2070 return ret;
2071}
2072EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2073
2074/**
2075 * regulator_bulk_free - free multiple regulator consumers
2076 *
2077 * @num_consumers: Number of consumers
2078 * @consumers: Consumer data; clients are stored here.
2079 *
2080 * This convenience API allows consumers to free multiple regulator
2081 * clients in a single API call.
2082 */
2083void regulator_bulk_free(int num_consumers,
2084 struct regulator_bulk_data *consumers)
2085{
2086 int i;
2087
2088 for (i = 0; i < num_consumers; i++) {
2089 regulator_put(consumers[i].consumer);
2090 consumers[i].consumer = NULL;
2091 }
2092}
2093EXPORT_SYMBOL_GPL(regulator_bulk_free);
2094
2095/**
2096 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002097 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002098 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002099 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002100 *
2101 * Called by regulator drivers to notify clients a regulator event has
2102 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002103 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002104 */
2105int regulator_notifier_call_chain(struct regulator_dev *rdev,
2106 unsigned long event, void *data)
2107{
2108 _notifier_call_chain(rdev, event, data);
2109 return NOTIFY_DONE;
2110
2111}
2112EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2113
Mark Brownbe721972009-08-04 20:09:52 +02002114/**
2115 * regulator_mode_to_status - convert a regulator mode into a status
2116 *
2117 * @mode: Mode to convert
2118 *
2119 * Convert a regulator mode into a status.
2120 */
2121int regulator_mode_to_status(unsigned int mode)
2122{
2123 switch (mode) {
2124 case REGULATOR_MODE_FAST:
2125 return REGULATOR_STATUS_FAST;
2126 case REGULATOR_MODE_NORMAL:
2127 return REGULATOR_STATUS_NORMAL;
2128 case REGULATOR_MODE_IDLE:
2129 return REGULATOR_STATUS_IDLE;
2130 case REGULATOR_STATUS_STANDBY:
2131 return REGULATOR_STATUS_STANDBY;
2132 default:
2133 return 0;
2134 }
2135}
2136EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2137
David Brownell7ad68e22008-11-11 17:39:02 -08002138/*
2139 * To avoid cluttering sysfs (and memory) with useless state, only
2140 * create attributes that can be meaningfully displayed.
2141 */
2142static int add_regulator_attributes(struct regulator_dev *rdev)
2143{
2144 struct device *dev = &rdev->dev;
2145 struct regulator_ops *ops = rdev->desc->ops;
2146 int status = 0;
2147
2148 /* some attributes need specific methods to be displayed */
2149 if (ops->get_voltage) {
2150 status = device_create_file(dev, &dev_attr_microvolts);
2151 if (status < 0)
2152 return status;
2153 }
2154 if (ops->get_current_limit) {
2155 status = device_create_file(dev, &dev_attr_microamps);
2156 if (status < 0)
2157 return status;
2158 }
2159 if (ops->get_mode) {
2160 status = device_create_file(dev, &dev_attr_opmode);
2161 if (status < 0)
2162 return status;
2163 }
2164 if (ops->is_enabled) {
2165 status = device_create_file(dev, &dev_attr_state);
2166 if (status < 0)
2167 return status;
2168 }
David Brownell853116a2009-01-14 23:03:17 -08002169 if (ops->get_status) {
2170 status = device_create_file(dev, &dev_attr_status);
2171 if (status < 0)
2172 return status;
2173 }
David Brownell7ad68e22008-11-11 17:39:02 -08002174
2175 /* some attributes are type-specific */
2176 if (rdev->desc->type == REGULATOR_CURRENT) {
2177 status = device_create_file(dev, &dev_attr_requested_microamps);
2178 if (status < 0)
2179 return status;
2180 }
2181
2182 /* all the other attributes exist to support constraints;
2183 * don't show them if there are no constraints, or if the
2184 * relevant supporting methods are missing.
2185 */
2186 if (!rdev->constraints)
2187 return status;
2188
2189 /* constraints need specific supporting methods */
2190 if (ops->set_voltage) {
2191 status = device_create_file(dev, &dev_attr_min_microvolts);
2192 if (status < 0)
2193 return status;
2194 status = device_create_file(dev, &dev_attr_max_microvolts);
2195 if (status < 0)
2196 return status;
2197 }
2198 if (ops->set_current_limit) {
2199 status = device_create_file(dev, &dev_attr_min_microamps);
2200 if (status < 0)
2201 return status;
2202 status = device_create_file(dev, &dev_attr_max_microamps);
2203 if (status < 0)
2204 return status;
2205 }
2206
2207 /* suspend mode constraints need multiple supporting methods */
2208 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2209 return status;
2210
2211 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2212 if (status < 0)
2213 return status;
2214 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2215 if (status < 0)
2216 return status;
2217 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2218 if (status < 0)
2219 return status;
2220
2221 if (ops->set_suspend_voltage) {
2222 status = device_create_file(dev,
2223 &dev_attr_suspend_standby_microvolts);
2224 if (status < 0)
2225 return status;
2226 status = device_create_file(dev,
2227 &dev_attr_suspend_mem_microvolts);
2228 if (status < 0)
2229 return status;
2230 status = device_create_file(dev,
2231 &dev_attr_suspend_disk_microvolts);
2232 if (status < 0)
2233 return status;
2234 }
2235
2236 if (ops->set_suspend_mode) {
2237 status = device_create_file(dev,
2238 &dev_attr_suspend_standby_mode);
2239 if (status < 0)
2240 return status;
2241 status = device_create_file(dev,
2242 &dev_attr_suspend_mem_mode);
2243 if (status < 0)
2244 return status;
2245 status = device_create_file(dev,
2246 &dev_attr_suspend_disk_mode);
2247 if (status < 0)
2248 return status;
2249 }
2250
2251 return status;
2252}
2253
Liam Girdwood414c70c2008-04-30 15:59:04 +01002254/**
2255 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002256 * @regulator_desc: regulator to register
2257 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002258 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002259 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002260 *
2261 * Called by regulator drivers to register a regulator.
2262 * Returns 0 on success.
2263 */
2264struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002265 struct device *dev, struct regulator_init_data *init_data,
2266 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002267{
2268 static atomic_t regulator_no = ATOMIC_INIT(0);
2269 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002270 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002271
2272 if (regulator_desc == NULL)
2273 return ERR_PTR(-EINVAL);
2274
2275 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2276 return ERR_PTR(-EINVAL);
2277
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002278 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2279 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002280 return ERR_PTR(-EINVAL);
2281
Mark Brown46fabe1e2008-09-09 16:21:18 +01002282 if (!init_data)
2283 return ERR_PTR(-EINVAL);
2284
Liam Girdwood414c70c2008-04-30 15:59:04 +01002285 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2286 if (rdev == NULL)
2287 return ERR_PTR(-ENOMEM);
2288
2289 mutex_lock(&regulator_list_mutex);
2290
2291 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002292 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002293 rdev->owner = regulator_desc->owner;
2294 rdev->desc = regulator_desc;
2295 INIT_LIST_HEAD(&rdev->consumer_list);
2296 INIT_LIST_HEAD(&rdev->supply_list);
2297 INIT_LIST_HEAD(&rdev->list);
2298 INIT_LIST_HEAD(&rdev->slist);
2299 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2300
Liam Girdwooda5766f12008-10-10 13:22:20 +01002301 /* preform any regulator specific init */
2302 if (init_data->regulator_init) {
2303 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002304 if (ret < 0)
2305 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002306 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002307
Liam Girdwooda5766f12008-10-10 13:22:20 +01002308 /* register with sysfs */
2309 rdev->dev.class = &regulator_class;
2310 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002311 dev_set_name(&rdev->dev, "regulator.%d",
2312 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002313 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002314 if (ret != 0)
2315 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002316
2317 dev_set_drvdata(&rdev->dev, rdev);
2318
Mike Rapoport74f544c2008-11-25 14:53:53 +02002319 /* set regulator constraints */
2320 ret = set_machine_constraints(rdev, &init_data->constraints);
2321 if (ret < 0)
2322 goto scrub;
2323
David Brownell7ad68e22008-11-11 17:39:02 -08002324 /* add attributes supported by this regulator */
2325 ret = add_regulator_attributes(rdev);
2326 if (ret < 0)
2327 goto scrub;
2328
Liam Girdwooda5766f12008-10-10 13:22:20 +01002329 /* set supply regulator if it exists */
2330 if (init_data->supply_regulator_dev) {
2331 ret = set_supply(rdev,
2332 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002333 if (ret < 0)
2334 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002335 }
2336
2337 /* add consumers devices */
2338 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2339 ret = set_consumer_device_supply(rdev,
2340 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002341 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002342 init_data->consumer_supplies[i].supply);
2343 if (ret < 0) {
2344 for (--i; i >= 0; i--)
2345 unset_consumer_device_supply(rdev,
Mark Brown40f92442009-06-17 17:56:39 +01002346 init_data->consumer_supplies[i].dev_name,
2347 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08002348 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002349 }
2350 }
2351
2352 list_add(&rdev->list, &regulator_list);
2353out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002354 mutex_unlock(&regulator_list_mutex);
2355 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002356
2357scrub:
2358 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002359 /* device core frees rdev */
2360 rdev = ERR_PTR(ret);
2361 goto out;
2362
David Brownell4fca9542008-11-11 17:38:53 -08002363clean:
2364 kfree(rdev);
2365 rdev = ERR_PTR(ret);
2366 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002367}
2368EXPORT_SYMBOL_GPL(regulator_register);
2369
2370/**
2371 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002372 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002373 *
2374 * Called by regulator drivers to unregister a regulator.
2375 */
2376void regulator_unregister(struct regulator_dev *rdev)
2377{
2378 if (rdev == NULL)
2379 return;
2380
2381 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002382 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002383 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002384 list_del(&rdev->list);
2385 if (rdev->supply)
2386 sysfs_remove_link(&rdev->dev.kobj, "supply");
2387 device_unregister(&rdev->dev);
2388 mutex_unlock(&regulator_list_mutex);
2389}
2390EXPORT_SYMBOL_GPL(regulator_unregister);
2391
2392/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002393 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002394 * @state: system suspend state
2395 *
2396 * Configure each regulator with it's suspend operating parameters for state.
2397 * This will usually be called by machine suspend code prior to supending.
2398 */
2399int regulator_suspend_prepare(suspend_state_t state)
2400{
2401 struct regulator_dev *rdev;
2402 int ret = 0;
2403
2404 /* ON is handled by regulator active state */
2405 if (state == PM_SUSPEND_ON)
2406 return -EINVAL;
2407
2408 mutex_lock(&regulator_list_mutex);
2409 list_for_each_entry(rdev, &regulator_list, list) {
2410
2411 mutex_lock(&rdev->mutex);
2412 ret = suspend_prepare(rdev, state);
2413 mutex_unlock(&rdev->mutex);
2414
2415 if (ret < 0) {
2416 printk(KERN_ERR "%s: failed to prepare %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01002417 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01002418 goto out;
2419 }
2420 }
2421out:
2422 mutex_unlock(&regulator_list_mutex);
2423 return ret;
2424}
2425EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2426
2427/**
Mark Brownca725562009-03-16 19:36:34 +00002428 * regulator_has_full_constraints - the system has fully specified constraints
2429 *
2430 * Calling this function will cause the regulator API to disable all
2431 * regulators which have a zero use count and don't have an always_on
2432 * constraint in a late_initcall.
2433 *
2434 * The intention is that this will become the default behaviour in a
2435 * future kernel release so users are encouraged to use this facility
2436 * now.
2437 */
2438void regulator_has_full_constraints(void)
2439{
2440 has_full_constraints = 1;
2441}
2442EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2443
2444/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002445 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002446 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002447 *
2448 * Get rdev regulator driver private data. This call can be used in the
2449 * regulator driver context.
2450 */
2451void *rdev_get_drvdata(struct regulator_dev *rdev)
2452{
2453 return rdev->reg_data;
2454}
2455EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2456
2457/**
2458 * regulator_get_drvdata - get regulator driver data
2459 * @regulator: regulator
2460 *
2461 * Get regulator driver private data. This call can be used in the consumer
2462 * driver context when non API regulator specific functions need to be called.
2463 */
2464void *regulator_get_drvdata(struct regulator *regulator)
2465{
2466 return regulator->rdev->reg_data;
2467}
2468EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2469
2470/**
2471 * regulator_set_drvdata - set regulator driver data
2472 * @regulator: regulator
2473 * @data: data
2474 */
2475void regulator_set_drvdata(struct regulator *regulator, void *data)
2476{
2477 regulator->rdev->reg_data = data;
2478}
2479EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2480
2481/**
2482 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002483 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002484 */
2485int rdev_get_id(struct regulator_dev *rdev)
2486{
2487 return rdev->desc->id;
2488}
2489EXPORT_SYMBOL_GPL(rdev_get_id);
2490
Liam Girdwooda5766f12008-10-10 13:22:20 +01002491struct device *rdev_get_dev(struct regulator_dev *rdev)
2492{
2493 return &rdev->dev;
2494}
2495EXPORT_SYMBOL_GPL(rdev_get_dev);
2496
2497void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2498{
2499 return reg_init_data->driver_data;
2500}
2501EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2502
Liam Girdwood414c70c2008-04-30 15:59:04 +01002503static int __init regulator_init(void)
2504{
Mark Brown34abbd62010-02-12 10:18:08 +00002505 int ret;
2506
Liam Girdwood414c70c2008-04-30 15:59:04 +01002507 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
Mark Brown34abbd62010-02-12 10:18:08 +00002508
2509 ret = class_register(&regulator_class);
2510
2511 regulator_dummy_init();
2512
2513 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002514}
2515
2516/* init early to allow our consumers to complete system booting */
2517core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002518
2519static int __init regulator_init_complete(void)
2520{
2521 struct regulator_dev *rdev;
2522 struct regulator_ops *ops;
2523 struct regulation_constraints *c;
2524 int enabled, ret;
2525 const char *name;
2526
2527 mutex_lock(&regulator_list_mutex);
2528
2529 /* If we have a full configuration then disable any regulators
2530 * which are not in use or always_on. This will become the
2531 * default behaviour in the future.
2532 */
2533 list_for_each_entry(rdev, &regulator_list, list) {
2534 ops = rdev->desc->ops;
2535 c = rdev->constraints;
2536
Mark Brown1083c392009-10-22 16:31:32 +01002537 name = rdev_get_name(rdev);
Mark Brownca725562009-03-16 19:36:34 +00002538
Mark Brownf25e0b42009-08-03 18:49:55 +01002539 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002540 continue;
2541
2542 mutex_lock(&rdev->mutex);
2543
2544 if (rdev->use_count)
2545 goto unlock;
2546
2547 /* If we can't read the status assume it's on. */
2548 if (ops->is_enabled)
2549 enabled = ops->is_enabled(rdev);
2550 else
2551 enabled = 1;
2552
2553 if (!enabled)
2554 goto unlock;
2555
2556 if (has_full_constraints) {
2557 /* We log since this may kill the system if it
2558 * goes wrong. */
2559 printk(KERN_INFO "%s: disabling %s\n",
2560 __func__, name);
2561 ret = ops->disable(rdev);
2562 if (ret != 0) {
2563 printk(KERN_ERR
2564 "%s: couldn't disable %s: %d\n",
2565 __func__, name, ret);
2566 }
2567 } else {
2568 /* The intention is that in future we will
2569 * assume that full constraints are provided
2570 * so warn even if we aren't going to do
2571 * anything here.
2572 */
2573 printk(KERN_WARNING
2574 "%s: incomplete constraints, leaving %s on\n",
2575 __func__, name);
2576 }
2577
2578unlock:
2579 mutex_unlock(&rdev->mutex);
2580 }
2581
2582 mutex_unlock(&regulator_list_mutex);
2583
2584 return 0;
2585}
2586late_initcall(regulator_init_complete);