blob: 9867ebc00bed872a54cb4958c48c2ac6fa91d536 [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
Daniel Walker1d7372e2010-11-17 15:30:28 -080016#define pr_fmt(fmt) "%s: " fmt, __func__
Daniel Walkerc5e28ed72010-11-17 15:30:27 -080017
Liam Girdwood414c70c2008-04-30 15:59:04 +010018#include <linux/kernel.h>
19#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000020#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010021#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080023#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010024#include <linux/err.h>
25#include <linux/mutex.h>
26#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000027#include <linux/delay.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053028#include <linux/of.h>
29#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#include <linux/regulator/consumer.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040033#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010034
Mark Brown02fa3ec2010-11-10 14:38:30 +000035#define CREATE_TRACE_POINTS
36#include <trace/events/regulator.h>
37
Mark Brown34abbd62010-02-12 10:18:08 +000038#include "dummy.h"
39
Mark Brown7d51a0d2011-06-09 16:06:37 +010040#define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080042#define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50
Liam Girdwood414c70c2008-04-30 15:59:04 +010051static DEFINE_MUTEX(regulator_list_mutex);
52static LIST_HEAD(regulator_list);
53static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000054static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070055static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010056
Mark Brown1130e5b2010-12-21 23:49:31 +000057#ifdef CONFIG_DEBUG_FS
58static struct dentry *debugfs_root;
59#endif
60
Mark Brown8dc53902008-12-31 12:52:40 +000061/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010062 * struct regulator_map
63 *
64 * Used to provide symbolic supply names to devices.
65 */
66struct regulator_map {
67 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010068 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010069 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010070 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010071};
72
Liam Girdwood414c70c2008-04-30 15:59:04 +010073/*
74 * struct regulator
75 *
76 * One for each consumer device.
77 */
78struct regulator {
79 struct device *dev;
80 struct list_head list;
81 int uA_load;
82 int min_uV;
83 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010084 char *supply_name;
85 struct device_attribute dev_attr;
86 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010087#ifdef CONFIG_DEBUG_FS
88 struct dentry *debugfs;
89#endif
Liam Girdwood414c70c2008-04-30 15:59:04 +010090};
91
92static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010093static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010094static int _regulator_get_voltage(struct regulator_dev *rdev);
95static int _regulator_get_current_limit(struct regulator_dev *rdev);
96static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
97static void _notifier_call_chain(struct regulator_dev *rdev,
98 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000099static int _regulator_do_set_voltage(struct regulator_dev *rdev,
100 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100101static struct regulator *create_regulator(struct regulator_dev *rdev,
102 struct device *dev,
103 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100104
Mark Brown1083c392009-10-22 16:31:32 +0100105static const char *rdev_get_name(struct regulator_dev *rdev)
106{
107 if (rdev->constraints && rdev->constraints->name)
108 return rdev->constraints->name;
109 else if (rdev->desc->name)
110 return rdev->desc->name;
111 else
112 return "";
113}
114
Liam Girdwood414c70c2008-04-30 15:59:04 +0100115/* gets the regulator for a given consumer device */
116static struct regulator *get_device_regulator(struct device *dev)
117{
118 struct regulator *regulator = NULL;
119 struct regulator_dev *rdev;
120
121 mutex_lock(&regulator_list_mutex);
122 list_for_each_entry(rdev, &regulator_list, list) {
123 mutex_lock(&rdev->mutex);
124 list_for_each_entry(regulator, &rdev->consumer_list, list) {
125 if (regulator->dev == dev) {
126 mutex_unlock(&rdev->mutex);
127 mutex_unlock(&regulator_list_mutex);
128 return regulator;
129 }
130 }
131 mutex_unlock(&rdev->mutex);
132 }
133 mutex_unlock(&regulator_list_mutex);
134 return NULL;
135}
136
Rajendra Nayak69511a42011-11-18 16:47:20 +0530137/**
138 * of_get_regulator - get a regulator device node based on supply name
139 * @dev: Device pointer for the consumer (of regulator) device
140 * @supply: regulator supply name
141 *
142 * Extract the regulator device node corresponding to the supply name.
143 * retruns the device node corresponding to the regulator if found, else
144 * returns NULL.
145 */
146static struct device_node *of_get_regulator(struct device *dev, const char *supply)
147{
148 struct device_node *regnode = NULL;
149 char prop_name[32]; /* 32 is max size of property name */
150
151 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
152
153 snprintf(prop_name, 32, "%s-supply", supply);
154 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
155
156 if (!regnode) {
157 dev_warn(dev, "%s property in node %s references invalid phandle",
158 prop_name, dev->of_node->full_name);
159 return NULL;
160 }
161 return regnode;
162}
163
Liam Girdwood414c70c2008-04-30 15:59:04 +0100164/* Platform voltage constraint check */
165static int regulator_check_voltage(struct regulator_dev *rdev,
166 int *min_uV, int *max_uV)
167{
168 BUG_ON(*min_uV > *max_uV);
169
170 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800171 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100172 return -ENODEV;
173 }
174 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800175 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100176 return -EPERM;
177 }
178
179 if (*max_uV > rdev->constraints->max_uV)
180 *max_uV = rdev->constraints->max_uV;
181 if (*min_uV < rdev->constraints->min_uV)
182 *min_uV = rdev->constraints->min_uV;
183
Mark Brown89f425e2011-07-12 11:20:37 +0900184 if (*min_uV > *max_uV) {
185 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100186 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100187 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900188 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100189
190 return 0;
191}
192
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100193/* Make sure we select a voltage that suits the needs of all
194 * regulator consumers
195 */
196static int regulator_check_consumers(struct regulator_dev *rdev,
197 int *min_uV, int *max_uV)
198{
199 struct regulator *regulator;
200
201 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700202 /*
203 * Assume consumers that didn't say anything are OK
204 * with anything in the constraint range.
205 */
206 if (!regulator->min_uV && !regulator->max_uV)
207 continue;
208
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100209 if (*max_uV > regulator->max_uV)
210 *max_uV = regulator->max_uV;
211 if (*min_uV < regulator->min_uV)
212 *min_uV = regulator->min_uV;
213 }
214
215 if (*min_uV > *max_uV)
216 return -EINVAL;
217
218 return 0;
219}
220
Liam Girdwood414c70c2008-04-30 15:59:04 +0100221/* current constraint check */
222static int regulator_check_current_limit(struct regulator_dev *rdev,
223 int *min_uA, int *max_uA)
224{
225 BUG_ON(*min_uA > *max_uA);
226
227 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800228 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100229 return -ENODEV;
230 }
231 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800232 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233 return -EPERM;
234 }
235
236 if (*max_uA > rdev->constraints->max_uA)
237 *max_uA = rdev->constraints->max_uA;
238 if (*min_uA < rdev->constraints->min_uA)
239 *min_uA = rdev->constraints->min_uA;
240
Mark Brown89f425e2011-07-12 11:20:37 +0900241 if (*min_uA > *max_uA) {
242 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100243 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100244 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900245 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100246
247 return 0;
248}
249
250/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900251static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252{
Mark Brown2c608232011-03-30 06:29:12 +0900253 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800254 case REGULATOR_MODE_FAST:
255 case REGULATOR_MODE_NORMAL:
256 case REGULATOR_MODE_IDLE:
257 case REGULATOR_MODE_STANDBY:
258 break;
259 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900260 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800261 return -EINVAL;
262 }
263
Liam Girdwood414c70c2008-04-30 15:59:04 +0100264 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800265 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100266 return -ENODEV;
267 }
268 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800269 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270 return -EPERM;
271 }
Mark Brown2c608232011-03-30 06:29:12 +0900272
273 /* The modes are bitmasks, the most power hungry modes having
274 * the lowest values. If the requested mode isn't supported
275 * try higher modes. */
276 while (*mode) {
277 if (rdev->constraints->valid_modes_mask & *mode)
278 return 0;
279 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 }
Mark Brown2c608232011-03-30 06:29:12 +0900281
282 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100283}
284
285/* dynamic regulator mode switching constraint check */
286static int regulator_check_drms(struct regulator_dev *rdev)
287{
288 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800289 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100290 return -ENODEV;
291 }
292 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800293 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100294 return -EPERM;
295 }
296 return 0;
297}
298
299static ssize_t device_requested_uA_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
302 struct regulator *regulator;
303
304 regulator = get_device_regulator(dev);
305 if (regulator == NULL)
306 return 0;
307
308 return sprintf(buf, "%d\n", regulator->uA_load);
309}
310
311static ssize_t regulator_uV_show(struct device *dev,
312 struct device_attribute *attr, char *buf)
313{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100314 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100315 ssize_t ret;
316
317 mutex_lock(&rdev->mutex);
318 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
319 mutex_unlock(&rdev->mutex);
320
321 return ret;
322}
David Brownell7ad68e22008-11-11 17:39:02 -0800323static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100324
325static ssize_t regulator_uA_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
327{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100328 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100329
330 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
331}
David Brownell7ad68e22008-11-11 17:39:02 -0800332static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100333
Mark Brownbc558a62008-10-10 15:33:20 +0100334static ssize_t regulator_name_show(struct device *dev,
335 struct device_attribute *attr, char *buf)
336{
337 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100338
Mark Brown1083c392009-10-22 16:31:32 +0100339 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100340}
341
David Brownell4fca9542008-11-11 17:38:53 -0800342static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100343{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100344 switch (mode) {
345 case REGULATOR_MODE_FAST:
346 return sprintf(buf, "fast\n");
347 case REGULATOR_MODE_NORMAL:
348 return sprintf(buf, "normal\n");
349 case REGULATOR_MODE_IDLE:
350 return sprintf(buf, "idle\n");
351 case REGULATOR_MODE_STANDBY:
352 return sprintf(buf, "standby\n");
353 }
354 return sprintf(buf, "unknown\n");
355}
356
David Brownell4fca9542008-11-11 17:38:53 -0800357static ssize_t regulator_opmode_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100359{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100360 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100361
David Brownell4fca9542008-11-11 17:38:53 -0800362 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
363}
David Brownell7ad68e22008-11-11 17:39:02 -0800364static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800365
366static ssize_t regulator_print_state(char *buf, int state)
367{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100368 if (state > 0)
369 return sprintf(buf, "enabled\n");
370 else if (state == 0)
371 return sprintf(buf, "disabled\n");
372 else
373 return sprintf(buf, "unknown\n");
374}
375
David Brownell4fca9542008-11-11 17:38:53 -0800376static ssize_t regulator_state_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
378{
379 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100380 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800381
Mark Brown93325462009-08-03 18:49:56 +0100382 mutex_lock(&rdev->mutex);
383 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
384 mutex_unlock(&rdev->mutex);
385
386 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800387}
David Brownell7ad68e22008-11-11 17:39:02 -0800388static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800389
David Brownell853116a2009-01-14 23:03:17 -0800390static ssize_t regulator_status_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
392{
393 struct regulator_dev *rdev = dev_get_drvdata(dev);
394 int status;
395 char *label;
396
397 status = rdev->desc->ops->get_status(rdev);
398 if (status < 0)
399 return status;
400
401 switch (status) {
402 case REGULATOR_STATUS_OFF:
403 label = "off";
404 break;
405 case REGULATOR_STATUS_ON:
406 label = "on";
407 break;
408 case REGULATOR_STATUS_ERROR:
409 label = "error";
410 break;
411 case REGULATOR_STATUS_FAST:
412 label = "fast";
413 break;
414 case REGULATOR_STATUS_NORMAL:
415 label = "normal";
416 break;
417 case REGULATOR_STATUS_IDLE:
418 label = "idle";
419 break;
420 case REGULATOR_STATUS_STANDBY:
421 label = "standby";
422 break;
423 default:
424 return -ERANGE;
425 }
426
427 return sprintf(buf, "%s\n", label);
428}
429static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
430
Liam Girdwood414c70c2008-04-30 15:59:04 +0100431static ssize_t regulator_min_uA_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
433{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100434 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435
436 if (!rdev->constraints)
437 return sprintf(buf, "constraint not defined\n");
438
439 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
440}
David Brownell7ad68e22008-11-11 17:39:02 -0800441static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
443static ssize_t regulator_max_uA_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
448 if (!rdev->constraints)
449 return sprintf(buf, "constraint not defined\n");
450
451 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
452}
David Brownell7ad68e22008-11-11 17:39:02 -0800453static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454
455static ssize_t regulator_min_uV_show(struct device *dev,
456 struct device_attribute *attr, char *buf)
457{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100458 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459
460 if (!rdev->constraints)
461 return sprintf(buf, "constraint not defined\n");
462
463 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
464}
David Brownell7ad68e22008-11-11 17:39:02 -0800465static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466
467static ssize_t regulator_max_uV_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
469{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100470 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100471
472 if (!rdev->constraints)
473 return sprintf(buf, "constraint not defined\n");
474
475 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
476}
David Brownell7ad68e22008-11-11 17:39:02 -0800477static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100478
479static ssize_t regulator_total_uA_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
481{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100482 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483 struct regulator *regulator;
484 int uA = 0;
485
486 mutex_lock(&rdev->mutex);
487 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100488 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489 mutex_unlock(&rdev->mutex);
490 return sprintf(buf, "%d\n", uA);
491}
David Brownell7ad68e22008-11-11 17:39:02 -0800492static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493
494static ssize_t regulator_num_users_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
496{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100497 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498 return sprintf(buf, "%d\n", rdev->use_count);
499}
500
501static ssize_t regulator_type_show(struct device *dev,
502 struct device_attribute *attr, char *buf)
503{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100504 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505
506 switch (rdev->desc->type) {
507 case REGULATOR_VOLTAGE:
508 return sprintf(buf, "voltage\n");
509 case REGULATOR_CURRENT:
510 return sprintf(buf, "current\n");
511 }
512 return sprintf(buf, "unknown\n");
513}
514
515static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
517{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100518 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100519
Liam Girdwood414c70c2008-04-30 15:59:04 +0100520 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
521}
David Brownell7ad68e22008-11-11 17:39:02 -0800522static DEVICE_ATTR(suspend_mem_microvolts, 0444,
523 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524
525static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
527{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100528 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
531}
David Brownell7ad68e22008-11-11 17:39:02 -0800532static DEVICE_ATTR(suspend_disk_microvolts, 0444,
533 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534
535static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
536 struct device_attribute *attr, char *buf)
537{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100538 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
541}
David Brownell7ad68e22008-11-11 17:39:02 -0800542static DEVICE_ATTR(suspend_standby_microvolts, 0444,
543 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
546 struct device_attribute *attr, char *buf)
547{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100548 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100549
David Brownell4fca9542008-11-11 17:38:53 -0800550 return regulator_print_opmode(buf,
551 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552}
David Brownell7ad68e22008-11-11 17:39:02 -0800553static DEVICE_ATTR(suspend_mem_mode, 0444,
554 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555
556static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
557 struct device_attribute *attr, char *buf)
558{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100559 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560
David Brownell4fca9542008-11-11 17:38:53 -0800561 return regulator_print_opmode(buf,
562 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100563}
David Brownell7ad68e22008-11-11 17:39:02 -0800564static DEVICE_ATTR(suspend_disk_mode, 0444,
565 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566
567static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
569{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100570 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571
David Brownell4fca9542008-11-11 17:38:53 -0800572 return regulator_print_opmode(buf,
573 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574}
David Brownell7ad68e22008-11-11 17:39:02 -0800575static DEVICE_ATTR(suspend_standby_mode, 0444,
576 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577
578static ssize_t regulator_suspend_mem_state_show(struct device *dev,
579 struct device_attribute *attr, char *buf)
580{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100581 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582
David Brownell4fca9542008-11-11 17:38:53 -0800583 return regulator_print_state(buf,
584 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585}
David Brownell7ad68e22008-11-11 17:39:02 -0800586static DEVICE_ATTR(suspend_mem_state, 0444,
587 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588
589static ssize_t regulator_suspend_disk_state_show(struct device *dev,
590 struct device_attribute *attr, char *buf)
591{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100592 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593
David Brownell4fca9542008-11-11 17:38:53 -0800594 return regulator_print_state(buf,
595 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100596}
David Brownell7ad68e22008-11-11 17:39:02 -0800597static DEVICE_ATTR(suspend_disk_state, 0444,
598 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100599
600static ssize_t regulator_suspend_standby_state_show(struct device *dev,
601 struct device_attribute *attr, char *buf)
602{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100603 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100604
David Brownell4fca9542008-11-11 17:38:53 -0800605 return regulator_print_state(buf,
606 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100607}
David Brownell7ad68e22008-11-11 17:39:02 -0800608static DEVICE_ATTR(suspend_standby_state, 0444,
609 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100610
David Brownell7ad68e22008-11-11 17:39:02 -0800611
612/*
613 * These are the only attributes are present for all regulators.
614 * Other attributes are a function of regulator functionality.
615 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100616static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100617 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100618 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
619 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620 __ATTR_NULL,
621};
622
623static void regulator_dev_release(struct device *dev)
624{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100625 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100626 kfree(rdev);
627}
628
629static struct class regulator_class = {
630 .name = "regulator",
631 .dev_release = regulator_dev_release,
632 .dev_attrs = regulator_dev_attrs,
633};
634
635/* Calculate the new optimum regulator operating mode based on the new total
636 * consumer load. All locks held by caller */
637static void drms_uA_update(struct regulator_dev *rdev)
638{
639 struct regulator *sibling;
640 int current_uA = 0, output_uV, input_uV, err;
641 unsigned int mode;
642
643 err = regulator_check_drms(rdev);
644 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000645 (!rdev->desc->ops->get_voltage &&
646 !rdev->desc->ops->get_voltage_sel) ||
647 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300648 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100649
650 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000651 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100652 if (output_uV <= 0)
653 return;
654
655 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000656 input_uV = 0;
657 if (rdev->supply)
658 input_uV = _regulator_get_voltage(rdev);
659 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100660 input_uV = rdev->constraints->input_uV;
661 if (input_uV <= 0)
662 return;
663
664 /* calc total requested load */
665 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100666 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100667
668 /* now get the optimum mode for our new total regulator load */
669 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
670 output_uV, current_uA);
671
672 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900673 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100674 if (err == 0)
675 rdev->desc->ops->set_mode(rdev, mode);
676}
677
678static int suspend_set_state(struct regulator_dev *rdev,
679 struct regulator_state *rstate)
680{
681 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100682 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100683
Mark Brown638f85c2009-10-22 16:31:33 +0100684 can_set_state = rdev->desc->ops->set_suspend_enable &&
685 rdev->desc->ops->set_suspend_disable;
686
687 /* If we have no suspend mode configration don't set anything;
688 * only warn if the driver actually makes the suspend mode
689 * configurable.
690 */
691 if (!rstate->enabled && !rstate->disabled) {
692 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800693 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100694 return 0;
695 }
696
697 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800698 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100699 return -EINVAL;
700 }
701
702 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800703 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100704 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100705 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100706
707 if (rstate->enabled)
708 ret = rdev->desc->ops->set_suspend_enable(rdev);
709 else
710 ret = rdev->desc->ops->set_suspend_disable(rdev);
711 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800712 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100713 return ret;
714 }
715
716 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
717 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
718 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800719 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100720 return ret;
721 }
722 }
723
724 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
725 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
726 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800727 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728 return ret;
729 }
730 }
731 return ret;
732}
733
734/* locks held by caller */
735static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
736{
737 if (!rdev->constraints)
738 return -EINVAL;
739
740 switch (state) {
741 case PM_SUSPEND_STANDBY:
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_standby);
744 case PM_SUSPEND_MEM:
745 return suspend_set_state(rdev,
746 &rdev->constraints->state_mem);
747 case PM_SUSPEND_MAX:
748 return suspend_set_state(rdev,
749 &rdev->constraints->state_disk);
750 default:
751 return -EINVAL;
752 }
753}
754
755static void print_constraints(struct regulator_dev *rdev)
756{
757 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000758 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100759 int count = 0;
760 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100761
Mark Brown8f031b42009-10-22 16:31:31 +0100762 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100763 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100764 count += sprintf(buf + count, "%d mV ",
765 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100766 else
Mark Brown8f031b42009-10-22 16:31:31 +0100767 count += sprintf(buf + count, "%d <--> %d mV ",
768 constraints->min_uV / 1000,
769 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100770 }
Mark Brown8f031b42009-10-22 16:31:31 +0100771
772 if (!constraints->min_uV ||
773 constraints->min_uV != constraints->max_uV) {
774 ret = _regulator_get_voltage(rdev);
775 if (ret > 0)
776 count += sprintf(buf + count, "at %d mV ", ret / 1000);
777 }
778
Mark Brownbf5892a2011-05-08 22:13:37 +0100779 if (constraints->uV_offset)
780 count += sprintf(buf, "%dmV offset ",
781 constraints->uV_offset / 1000);
782
Mark Brown8f031b42009-10-22 16:31:31 +0100783 if (constraints->min_uA && constraints->max_uA) {
784 if (constraints->min_uA == constraints->max_uA)
785 count += sprintf(buf + count, "%d mA ",
786 constraints->min_uA / 1000);
787 else
788 count += sprintf(buf + count, "%d <--> %d mA ",
789 constraints->min_uA / 1000,
790 constraints->max_uA / 1000);
791 }
792
793 if (!constraints->min_uA ||
794 constraints->min_uA != constraints->max_uA) {
795 ret = _regulator_get_current_limit(rdev);
796 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400797 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100798 }
799
Liam Girdwood414c70c2008-04-30 15:59:04 +0100800 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
801 count += sprintf(buf + count, "fast ");
802 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
803 count += sprintf(buf + count, "normal ");
804 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
805 count += sprintf(buf + count, "idle ");
806 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
807 count += sprintf(buf + count, "standby");
808
Mark Brown13ce29f2010-12-17 16:04:12 +0000809 rdev_info(rdev, "%s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100810}
811
Mark Browne79055d2009-10-19 15:53:50 +0100812static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100813 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100814{
815 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100816 int ret;
817
818 /* do we need to apply the constraint voltage */
819 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000820 rdev->constraints->min_uV == rdev->constraints->max_uV) {
821 ret = _regulator_do_set_voltage(rdev,
822 rdev->constraints->min_uV,
823 rdev->constraints->max_uV);
824 if (ret < 0) {
825 rdev_err(rdev, "failed to apply %duV constraint\n",
826 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000827 return ret;
828 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100829 }
Mark Browne79055d2009-10-19 15:53:50 +0100830
831 /* constrain machine-level voltage specs to fit
832 * the actual range supported by this regulator.
833 */
834 if (ops->list_voltage && rdev->desc->n_voltages) {
835 int count = rdev->desc->n_voltages;
836 int i;
837 int min_uV = INT_MAX;
838 int max_uV = INT_MIN;
839 int cmin = constraints->min_uV;
840 int cmax = constraints->max_uV;
841
842 /* it's safe to autoconfigure fixed-voltage supplies
843 and the constraints are used by list_voltage. */
844 if (count == 1 && !cmin) {
845 cmin = 1;
846 cmax = INT_MAX;
847 constraints->min_uV = cmin;
848 constraints->max_uV = cmax;
849 }
850
851 /* voltage constraints are optional */
852 if ((cmin == 0) && (cmax == 0))
853 return 0;
854
855 /* else require explicit machine-level constraints */
856 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800857 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100858 return -EINVAL;
859 }
860
861 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
862 for (i = 0; i < count; i++) {
863 int value;
864
865 value = ops->list_voltage(rdev, i);
866 if (value <= 0)
867 continue;
868
869 /* maybe adjust [min_uV..max_uV] */
870 if (value >= cmin && value < min_uV)
871 min_uV = value;
872 if (value <= cmax && value > max_uV)
873 max_uV = value;
874 }
875
876 /* final: [min_uV..max_uV] valid iff constraints valid */
877 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800878 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100879 return -EINVAL;
880 }
881
882 /* use regulator's subset of machine constraints */
883 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800884 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
885 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100886 constraints->min_uV = min_uV;
887 }
888 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800889 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
890 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100891 constraints->max_uV = max_uV;
892 }
893 }
894
895 return 0;
896}
897
Liam Girdwooda5766f12008-10-10 13:22:20 +0100898/**
899 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000900 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000901 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100902 *
903 * Allows platform initialisation code to define and constrain
904 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
905 * Constraints *must* be set by platform code in order for some
906 * regulator operations to proceed i.e. set_voltage, set_current_limit,
907 * set_mode.
908 */
909static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000910 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100911{
912 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100913 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100914
Mark Brownf8c12fe2010-11-29 15:55:17 +0000915 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
916 GFP_KERNEL);
917 if (!rdev->constraints)
918 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100919
Mark Brownf8c12fe2010-11-29 15:55:17 +0000920 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100921 if (ret != 0)
922 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800923
Liam Girdwooda5766f12008-10-10 13:22:20 +0100924 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100925 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000926 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100927 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800928 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100929 goto out;
930 }
931 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100932
Mark Browna3084662009-02-26 19:24:19 +0000933 if (constraints->initial_mode) {
934 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800935 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000936 ret = -EINVAL;
937 goto out;
938 }
939
Mark Brownf8c12fe2010-11-29 15:55:17 +0000940 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000941 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800942 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000943 goto out;
944 }
945 }
946
Mark Browncacf90f2009-03-02 16:32:46 +0000947 /* If the constraints say the regulator should be on at this point
948 * and we have control then make sure it is enabled.
949 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000950 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
951 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100952 ret = ops->enable(rdev);
953 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800954 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100955 goto out;
956 }
957 }
958
Liam Girdwooda5766f12008-10-10 13:22:20 +0100959 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800960 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100961out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800962 kfree(rdev->constraints);
963 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100964 return ret;
965}
966
967/**
968 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000969 * @rdev: regulator name
970 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100971 *
972 * Called by platform initialisation code to set the supply regulator for this
973 * regulator. This ensures that a regulators supply will also be enabled by the
974 * core if it's child is enabled.
975 */
976static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100977 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100978{
979 int err;
980
Mark Brown3801b862011-06-09 16:22:22 +0100981 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
982
983 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
984 if (IS_ERR(rdev->supply)) {
985 err = PTR_ERR(rdev->supply);
986 rdev->supply = NULL;
987 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100988 }
Mark Brown3801b862011-06-09 16:22:22 +0100989
990 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100991}
992
993/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800994 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000995 * @rdev: regulator source
996 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100997 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000998 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100999 *
1000 * Allows platform initialisation code to map physical regulator
1001 * sources to symbolic names for supplies for use by devices. Devices
1002 * should use these symbolic names to request regulators, avoiding the
1003 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +01001004 *
1005 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +01001006 */
1007static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +01001008 struct device *consumer_dev, const char *consumer_dev_name,
1009 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001010{
1011 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001012 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001013
Mark Brown40f92442009-06-17 17:56:39 +01001014 if (consumer_dev && consumer_dev_name)
1015 return -EINVAL;
1016
1017 if (!consumer_dev_name && consumer_dev)
1018 consumer_dev_name = dev_name(consumer_dev);
1019
Liam Girdwooda5766f12008-10-10 13:22:20 +01001020 if (supply == NULL)
1021 return -EINVAL;
1022
Mark Brown9ed20992009-07-21 16:00:26 +01001023 if (consumer_dev_name != NULL)
1024 has_dev = 1;
1025 else
1026 has_dev = 0;
1027
David Brownell6001e132008-12-31 12:54:19 +00001028 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001029 if (node->dev_name && consumer_dev_name) {
1030 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1031 continue;
1032 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001033 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001034 }
1035
David Brownell6001e132008-12-31 12:54:19 +00001036 if (strcmp(node->supply, supply) != 0)
1037 continue;
1038
1039 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001040 dev_name(&node->regulator->dev),
1041 node->regulator->desc->name,
1042 supply,
1043 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001044 return -EBUSY;
1045 }
1046
Mark Brown9ed20992009-07-21 16:00:26 +01001047 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001048 if (node == NULL)
1049 return -ENOMEM;
1050
1051 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001052 node->supply = supply;
1053
Mark Brown9ed20992009-07-21 16:00:26 +01001054 if (has_dev) {
1055 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1056 if (node->dev_name == NULL) {
1057 kfree(node);
1058 return -ENOMEM;
1059 }
Mark Brown40f92442009-06-17 17:56:39 +01001060 }
1061
Liam Girdwooda5766f12008-10-10 13:22:20 +01001062 list_add(&node->list, &regulator_map_list);
1063 return 0;
1064}
1065
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001066static void unset_regulator_supplies(struct regulator_dev *rdev)
1067{
1068 struct regulator_map *node, *n;
1069
1070 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1071 if (rdev == node->regulator) {
1072 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001073 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001074 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001075 }
1076 }
1077}
1078
Mark Brownf5726ae2011-06-09 16:22:20 +01001079#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001080
1081static struct regulator *create_regulator(struct regulator_dev *rdev,
1082 struct device *dev,
1083 const char *supply_name)
1084{
1085 struct regulator *regulator;
1086 char buf[REG_STR_SIZE];
1087 int err, size;
1088
1089 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1090 if (regulator == NULL)
1091 return NULL;
1092
1093 mutex_lock(&rdev->mutex);
1094 regulator->rdev = rdev;
1095 list_add(&regulator->list, &rdev->consumer_list);
1096
1097 if (dev) {
1098 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001099 size = scnprintf(buf, REG_STR_SIZE,
1100 "microamps_requested_%s-%s",
1101 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001102 if (size >= REG_STR_SIZE)
1103 goto overflow_err;
1104
1105 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001106 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001107 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1108 if (regulator->dev_attr.attr.name == NULL)
1109 goto attr_name_err;
1110
Liam Girdwood414c70c2008-04-30 15:59:04 +01001111 regulator->dev_attr.attr.mode = 0444;
1112 regulator->dev_attr.show = device_requested_uA_show;
1113 err = device_create_file(dev, &regulator->dev_attr);
1114 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001115 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001116 goto attr_name_err;
1117 }
1118
1119 /* also add a link to the device sysfs entry */
1120 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1121 dev->kobj.name, supply_name);
1122 if (size >= REG_STR_SIZE)
1123 goto attr_err;
1124
1125 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1126 if (regulator->supply_name == NULL)
1127 goto attr_err;
1128
1129 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1130 buf);
1131 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001132 rdev_warn(rdev, "could not add device link %s err %d\n",
1133 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001134 goto link_name_err;
1135 }
Mark Brown5de70512011-06-19 13:33:16 +01001136 } else {
1137 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1138 if (regulator->supply_name == NULL)
1139 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001140 }
Mark Brown5de70512011-06-19 13:33:16 +01001141
1142#ifdef CONFIG_DEBUG_FS
1143 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1144 rdev->debugfs);
1145 if (IS_ERR_OR_NULL(regulator->debugfs)) {
1146 rdev_warn(rdev, "Failed to create debugfs directory\n");
1147 regulator->debugfs = NULL;
1148 } else {
1149 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1150 &regulator->uA_load);
1151 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1152 &regulator->min_uV);
1153 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1154 &regulator->max_uV);
1155 }
1156#endif
1157
Liam Girdwood414c70c2008-04-30 15:59:04 +01001158 mutex_unlock(&rdev->mutex);
1159 return regulator;
1160link_name_err:
1161 kfree(regulator->supply_name);
1162attr_err:
1163 device_remove_file(regulator->dev, &regulator->dev_attr);
1164attr_name_err:
1165 kfree(regulator->dev_attr.attr.name);
1166overflow_err:
1167 list_del(&regulator->list);
1168 kfree(regulator);
1169 mutex_unlock(&rdev->mutex);
1170 return NULL;
1171}
1172
Mark Brown31aae2b2009-12-21 12:21:52 +00001173static int _regulator_get_enable_time(struct regulator_dev *rdev)
1174{
1175 if (!rdev->desc->ops->enable_time)
1176 return 0;
1177 return rdev->desc->ops->enable_time(rdev);
1178}
1179
Rajendra Nayak69511a42011-11-18 16:47:20 +05301180static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1181 const char *supply)
1182{
1183 struct regulator_dev *r;
1184 struct device_node *node;
1185
1186 /* first do a dt based lookup */
1187 if (dev && dev->of_node) {
1188 node = of_get_regulator(dev, supply);
1189 if (node)
1190 list_for_each_entry(r, &regulator_list, list)
1191 if (r->dev.parent &&
1192 node == r->dev.of_node)
1193 return r;
1194 }
1195
1196 /* if not found, try doing it non-dt way */
1197 list_for_each_entry(r, &regulator_list, list)
1198 if (strcmp(rdev_get_name(r), supply) == 0)
1199 return r;
1200
1201 return NULL;
1202}
1203
Mark Brown5ffbd132009-07-21 16:00:23 +01001204/* Internal regulator request function */
1205static struct regulator *_regulator_get(struct device *dev, const char *id,
1206 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001207{
1208 struct regulator_dev *rdev;
1209 struct regulator_map *map;
1210 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001211 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001212 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001213
1214 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001215 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001216 return regulator;
1217 }
1218
Mark Brown40f92442009-06-17 17:56:39 +01001219 if (dev)
1220 devname = dev_name(dev);
1221
Liam Girdwood414c70c2008-04-30 15:59:04 +01001222 mutex_lock(&regulator_list_mutex);
1223
Rajendra Nayak69511a42011-11-18 16:47:20 +05301224 rdev = regulator_dev_lookup(dev, id);
1225 if (rdev)
1226 goto found;
1227
Liam Girdwood414c70c2008-04-30 15:59:04 +01001228 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001229 /* If the mapping has a device set up it must match */
1230 if (map->dev_name &&
1231 (!devname || strcmp(map->dev_name, devname)))
1232 continue;
1233
1234 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001235 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001236 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001237 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001238 }
Mark Brown34abbd62010-02-12 10:18:08 +00001239
Mark Brown688fe992010-10-05 19:18:32 -07001240 if (board_wants_dummy_regulator) {
1241 rdev = dummy_regulator_rdev;
1242 goto found;
1243 }
1244
Mark Brown34abbd62010-02-12 10:18:08 +00001245#ifdef CONFIG_REGULATOR_DUMMY
1246 if (!devname)
1247 devname = "deviceless";
1248
1249 /* If the board didn't flag that it was fully constrained then
1250 * substitute in a dummy regulator so consumers can continue.
1251 */
1252 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001253 pr_warn("%s supply %s not found, using dummy regulator\n",
1254 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001255 rdev = dummy_regulator_rdev;
1256 goto found;
1257 }
1258#endif
1259
Liam Girdwood414c70c2008-04-30 15:59:04 +01001260 mutex_unlock(&regulator_list_mutex);
1261 return regulator;
1262
1263found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001264 if (rdev->exclusive) {
1265 regulator = ERR_PTR(-EPERM);
1266 goto out;
1267 }
1268
1269 if (exclusive && rdev->open_count) {
1270 regulator = ERR_PTR(-EBUSY);
1271 goto out;
1272 }
1273
Liam Girdwooda5766f12008-10-10 13:22:20 +01001274 if (!try_module_get(rdev->owner))
1275 goto out;
1276
Liam Girdwood414c70c2008-04-30 15:59:04 +01001277 regulator = create_regulator(rdev, dev, id);
1278 if (regulator == NULL) {
1279 regulator = ERR_PTR(-ENOMEM);
1280 module_put(rdev->owner);
1281 }
1282
Mark Brown5ffbd132009-07-21 16:00:23 +01001283 rdev->open_count++;
1284 if (exclusive) {
1285 rdev->exclusive = 1;
1286
1287 ret = _regulator_is_enabled(rdev);
1288 if (ret > 0)
1289 rdev->use_count = 1;
1290 else
1291 rdev->use_count = 0;
1292 }
1293
Liam Girdwooda5766f12008-10-10 13:22:20 +01001294out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001295 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001296
Liam Girdwood414c70c2008-04-30 15:59:04 +01001297 return regulator;
1298}
Mark Brown5ffbd132009-07-21 16:00:23 +01001299
1300/**
1301 * regulator_get - lookup and obtain a reference to a regulator.
1302 * @dev: device for regulator "consumer"
1303 * @id: Supply name or regulator ID.
1304 *
1305 * Returns a struct regulator corresponding to the regulator producer,
1306 * or IS_ERR() condition containing errno.
1307 *
1308 * Use of supply names configured via regulator_set_device_supply() is
1309 * strongly encouraged. It is recommended that the supply name used
1310 * should match the name used for the supply and/or the relevant
1311 * device pins in the datasheet.
1312 */
1313struct regulator *regulator_get(struct device *dev, const char *id)
1314{
1315 return _regulator_get(dev, id, 0);
1316}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001317EXPORT_SYMBOL_GPL(regulator_get);
1318
1319/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001320 * regulator_get_exclusive - obtain exclusive access to a regulator.
1321 * @dev: device for regulator "consumer"
1322 * @id: Supply name or regulator ID.
1323 *
1324 * Returns a struct regulator corresponding to the regulator producer,
1325 * or IS_ERR() condition containing errno. Other consumers will be
1326 * unable to obtain this reference is held and the use count for the
1327 * regulator will be initialised to reflect the current state of the
1328 * regulator.
1329 *
1330 * This is intended for use by consumers which cannot tolerate shared
1331 * use of the regulator such as those which need to force the
1332 * regulator off for correct operation of the hardware they are
1333 * controlling.
1334 *
1335 * Use of supply names configured via regulator_set_device_supply() is
1336 * strongly encouraged. It is recommended that the supply name used
1337 * should match the name used for the supply and/or the relevant
1338 * device pins in the datasheet.
1339 */
1340struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1341{
1342 return _regulator_get(dev, id, 1);
1343}
1344EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1345
1346/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001347 * regulator_put - "free" the regulator source
1348 * @regulator: regulator source
1349 *
1350 * Note: drivers must ensure that all regulator_enable calls made on this
1351 * regulator source are balanced by regulator_disable calls prior to calling
1352 * this function.
1353 */
1354void regulator_put(struct regulator *regulator)
1355{
1356 struct regulator_dev *rdev;
1357
1358 if (regulator == NULL || IS_ERR(regulator))
1359 return;
1360
Liam Girdwood414c70c2008-04-30 15:59:04 +01001361 mutex_lock(&regulator_list_mutex);
1362 rdev = regulator->rdev;
1363
Mark Brown5de70512011-06-19 13:33:16 +01001364#ifdef CONFIG_DEBUG_FS
1365 debugfs_remove_recursive(regulator->debugfs);
1366#endif
1367
Liam Girdwood414c70c2008-04-30 15:59:04 +01001368 /* remove any sysfs entries */
1369 if (regulator->dev) {
1370 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001371 device_remove_file(regulator->dev, &regulator->dev_attr);
1372 kfree(regulator->dev_attr.attr.name);
1373 }
Mark Brown5de70512011-06-19 13:33:16 +01001374 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001375 list_del(&regulator->list);
1376 kfree(regulator);
1377
Mark Brown5ffbd132009-07-21 16:00:23 +01001378 rdev->open_count--;
1379 rdev->exclusive = 0;
1380
Liam Girdwood414c70c2008-04-30 15:59:04 +01001381 module_put(rdev->owner);
1382 mutex_unlock(&regulator_list_mutex);
1383}
1384EXPORT_SYMBOL_GPL(regulator_put);
1385
Mark Brown9a2372f2009-08-03 18:49:57 +01001386static int _regulator_can_change_status(struct regulator_dev *rdev)
1387{
1388 if (!rdev->constraints)
1389 return 0;
1390
1391 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1392 return 1;
1393 else
1394 return 0;
1395}
1396
Liam Girdwood414c70c2008-04-30 15:59:04 +01001397/* locks held by regulator_enable() */
1398static int _regulator_enable(struct regulator_dev *rdev)
1399{
Mark Brown31aae2b2009-12-21 12:21:52 +00001400 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001401
Liam Girdwood414c70c2008-04-30 15:59:04 +01001402 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001403 if (rdev->constraints &&
1404 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1405 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001406
Mark Brown9a2372f2009-08-03 18:49:57 +01001407 if (rdev->use_count == 0) {
1408 /* The regulator may on if it's not switchable or left on */
1409 ret = _regulator_is_enabled(rdev);
1410 if (ret == -EINVAL || ret == 0) {
1411 if (!_regulator_can_change_status(rdev))
1412 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001413
Mark Brown31aae2b2009-12-21 12:21:52 +00001414 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001415 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001416
1417 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001418 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001419 ret = _regulator_get_enable_time(rdev);
1420 if (ret >= 0) {
1421 delay = ret;
1422 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001423 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001424 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001425 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001426 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001427
Mark Brown02fa3ec2010-11-10 14:38:30 +00001428 trace_regulator_enable(rdev_get_name(rdev));
1429
Mark Brown31aae2b2009-12-21 12:21:52 +00001430 /* Allow the regulator to ramp; it would be useful
1431 * to extend this for bulk operations so that the
1432 * regulators can ramp together. */
1433 ret = rdev->desc->ops->enable(rdev);
1434 if (ret < 0)
1435 return ret;
1436
Mark Brown02fa3ec2010-11-10 14:38:30 +00001437 trace_regulator_enable_delay(rdev_get_name(rdev));
1438
Axel Line36c1df2010-11-05 21:51:32 +08001439 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001440 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001441 udelay(delay % 1000);
1442 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001443 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001444 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001445
Mark Brown02fa3ec2010-11-10 14:38:30 +00001446 trace_regulator_enable_complete(rdev_get_name(rdev));
1447
Linus Walleija7433cf2009-08-26 12:54:04 +02001448 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001449 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001450 return ret;
1451 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001452 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001453 }
1454
Mark Brown9a2372f2009-08-03 18:49:57 +01001455 rdev->use_count++;
1456
1457 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001458}
1459
1460/**
1461 * regulator_enable - enable regulator output
1462 * @regulator: regulator source
1463 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001464 * Request that the regulator be enabled with the regulator output at
1465 * the predefined voltage or current value. Calls to regulator_enable()
1466 * must be balanced with calls to regulator_disable().
1467 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001468 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001469 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001470 */
1471int regulator_enable(struct regulator *regulator)
1472{
David Brownell412aec62008-11-16 11:44:46 -08001473 struct regulator_dev *rdev = regulator->rdev;
1474 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001475
Mark Brown3801b862011-06-09 16:22:22 +01001476 if (rdev->supply) {
1477 ret = regulator_enable(rdev->supply);
1478 if (ret != 0)
1479 return ret;
1480 }
1481
David Brownell412aec62008-11-16 11:44:46 -08001482 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001483 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001484 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001485
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001486 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001487 regulator_disable(rdev->supply);
1488
Liam Girdwood414c70c2008-04-30 15:59:04 +01001489 return ret;
1490}
1491EXPORT_SYMBOL_GPL(regulator_enable);
1492
1493/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001494static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001495{
1496 int ret = 0;
1497
David Brownellcd94b502009-03-11 16:43:34 -08001498 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001499 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001500 return -EIO;
1501
Liam Girdwood414c70c2008-04-30 15:59:04 +01001502 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001503 if (rdev->use_count == 1 &&
1504 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001505
1506 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001507 if (_regulator_can_change_status(rdev) &&
1508 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001509 trace_regulator_disable(rdev_get_name(rdev));
1510
Liam Girdwood414c70c2008-04-30 15:59:04 +01001511 ret = rdev->desc->ops->disable(rdev);
1512 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001513 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001514 return ret;
1515 }
Mark Brown84b68262009-12-01 21:12:27 +00001516
Mark Brown02fa3ec2010-11-10 14:38:30 +00001517 trace_regulator_disable_complete(rdev_get_name(rdev));
1518
Mark Brown84b68262009-12-01 21:12:27 +00001519 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1520 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001521 }
1522
Liam Girdwood414c70c2008-04-30 15:59:04 +01001523 rdev->use_count = 0;
1524 } else if (rdev->use_count > 1) {
1525
1526 if (rdev->constraints &&
1527 (rdev->constraints->valid_ops_mask &
1528 REGULATOR_CHANGE_DRMS))
1529 drms_uA_update(rdev);
1530
1531 rdev->use_count--;
1532 }
Mark Brown3801b862011-06-09 16:22:22 +01001533
Liam Girdwood414c70c2008-04-30 15:59:04 +01001534 return ret;
1535}
1536
1537/**
1538 * regulator_disable - disable regulator output
1539 * @regulator: regulator source
1540 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001541 * Disable the regulator output voltage or current. Calls to
1542 * regulator_enable() must be balanced with calls to
1543 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001544 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001545 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001546 * devices have it enabled, the regulator device supports disabling and
1547 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001548 */
1549int regulator_disable(struct regulator *regulator)
1550{
David Brownell412aec62008-11-16 11:44:46 -08001551 struct regulator_dev *rdev = regulator->rdev;
1552 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001553
David Brownell412aec62008-11-16 11:44:46 -08001554 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001555 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001556 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001557
Mark Brown3801b862011-06-09 16:22:22 +01001558 if (ret == 0 && rdev->supply)
1559 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001560
Liam Girdwood414c70c2008-04-30 15:59:04 +01001561 return ret;
1562}
1563EXPORT_SYMBOL_GPL(regulator_disable);
1564
1565/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001566static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001567{
1568 int ret = 0;
1569
1570 /* force disable */
1571 if (rdev->desc->ops->disable) {
1572 /* ah well, who wants to live forever... */
1573 ret = rdev->desc->ops->disable(rdev);
1574 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001575 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001576 return ret;
1577 }
1578 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001579 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1580 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001581 }
1582
Liam Girdwood414c70c2008-04-30 15:59:04 +01001583 return ret;
1584}
1585
1586/**
1587 * regulator_force_disable - force disable regulator output
1588 * @regulator: regulator source
1589 *
1590 * Forcibly disable the regulator output voltage or current.
1591 * NOTE: this *will* disable the regulator output even if other consumer
1592 * devices have it enabled. This should be used for situations when device
1593 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1594 */
1595int regulator_force_disable(struct regulator *regulator)
1596{
Mark Brown82d15832011-05-09 11:41:02 +02001597 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001598 int ret;
1599
Mark Brown82d15832011-05-09 11:41:02 +02001600 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001601 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001602 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001603 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001604
Mark Brown3801b862011-06-09 16:22:22 +01001605 if (rdev->supply)
1606 while (rdev->open_count--)
1607 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001608
Liam Girdwood414c70c2008-04-30 15:59:04 +01001609 return ret;
1610}
1611EXPORT_SYMBOL_GPL(regulator_force_disable);
1612
Mark Brownda07ecd2011-09-11 09:53:50 +01001613static void regulator_disable_work(struct work_struct *work)
1614{
1615 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1616 disable_work.work);
1617 int count, i, ret;
1618
1619 mutex_lock(&rdev->mutex);
1620
1621 BUG_ON(!rdev->deferred_disables);
1622
1623 count = rdev->deferred_disables;
1624 rdev->deferred_disables = 0;
1625
1626 for (i = 0; i < count; i++) {
1627 ret = _regulator_disable(rdev);
1628 if (ret != 0)
1629 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1630 }
1631
1632 mutex_unlock(&rdev->mutex);
1633
1634 if (rdev->supply) {
1635 for (i = 0; i < count; i++) {
1636 ret = regulator_disable(rdev->supply);
1637 if (ret != 0) {
1638 rdev_err(rdev,
1639 "Supply disable failed: %d\n", ret);
1640 }
1641 }
1642 }
1643}
1644
1645/**
1646 * regulator_disable_deferred - disable regulator output with delay
1647 * @regulator: regulator source
1648 * @ms: miliseconds until the regulator is disabled
1649 *
1650 * Execute regulator_disable() on the regulator after a delay. This
1651 * is intended for use with devices that require some time to quiesce.
1652 *
1653 * NOTE: this will only disable the regulator output if no other consumer
1654 * devices have it enabled, the regulator device supports disabling and
1655 * machine constraints permit this operation.
1656 */
1657int regulator_disable_deferred(struct regulator *regulator, int ms)
1658{
1659 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001660 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001661
1662 mutex_lock(&rdev->mutex);
1663 rdev->deferred_disables++;
1664 mutex_unlock(&rdev->mutex);
1665
Mark Brownaa598022011-10-03 22:42:43 +01001666 ret = schedule_delayed_work(&rdev->disable_work,
1667 msecs_to_jiffies(ms));
1668 if (ret < 0)
1669 return ret;
1670 else
1671 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001672}
1673EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1674
Liam Girdwood414c70c2008-04-30 15:59:04 +01001675static int _regulator_is_enabled(struct regulator_dev *rdev)
1676{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001677 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001678 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001679 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001680
Mark Brown93325462009-08-03 18:49:56 +01001681 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001682}
1683
1684/**
1685 * regulator_is_enabled - is the regulator output enabled
1686 * @regulator: regulator source
1687 *
David Brownell412aec62008-11-16 11:44:46 -08001688 * Returns positive if the regulator driver backing the source/client
1689 * has requested that the device be enabled, zero if it hasn't, else a
1690 * negative errno code.
1691 *
1692 * Note that the device backing this regulator handle can have multiple
1693 * users, so it might be enabled even if regulator_enable() was never
1694 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001695 */
1696int regulator_is_enabled(struct regulator *regulator)
1697{
Mark Brown93325462009-08-03 18:49:56 +01001698 int ret;
1699
1700 mutex_lock(&regulator->rdev->mutex);
1701 ret = _regulator_is_enabled(regulator->rdev);
1702 mutex_unlock(&regulator->rdev->mutex);
1703
1704 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001705}
1706EXPORT_SYMBOL_GPL(regulator_is_enabled);
1707
1708/**
David Brownell4367cfd2009-02-26 11:48:36 -08001709 * regulator_count_voltages - count regulator_list_voltage() selectors
1710 * @regulator: regulator source
1711 *
1712 * Returns number of selectors, or negative errno. Selectors are
1713 * numbered starting at zero, and typically correspond to bitfields
1714 * in hardware registers.
1715 */
1716int regulator_count_voltages(struct regulator *regulator)
1717{
1718 struct regulator_dev *rdev = regulator->rdev;
1719
1720 return rdev->desc->n_voltages ? : -EINVAL;
1721}
1722EXPORT_SYMBOL_GPL(regulator_count_voltages);
1723
1724/**
1725 * regulator_list_voltage - enumerate supported voltages
1726 * @regulator: regulator source
1727 * @selector: identify voltage to list
1728 * Context: can sleep
1729 *
1730 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001731 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001732 * negative errno.
1733 */
1734int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1735{
1736 struct regulator_dev *rdev = regulator->rdev;
1737 struct regulator_ops *ops = rdev->desc->ops;
1738 int ret;
1739
1740 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1741 return -EINVAL;
1742
1743 mutex_lock(&rdev->mutex);
1744 ret = ops->list_voltage(rdev, selector);
1745 mutex_unlock(&rdev->mutex);
1746
1747 if (ret > 0) {
1748 if (ret < rdev->constraints->min_uV)
1749 ret = 0;
1750 else if (ret > rdev->constraints->max_uV)
1751 ret = 0;
1752 }
1753
1754 return ret;
1755}
1756EXPORT_SYMBOL_GPL(regulator_list_voltage);
1757
1758/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001759 * regulator_is_supported_voltage - check if a voltage range can be supported
1760 *
1761 * @regulator: Regulator to check.
1762 * @min_uV: Minimum required voltage in uV.
1763 * @max_uV: Maximum required voltage in uV.
1764 *
1765 * Returns a boolean or a negative error code.
1766 */
1767int regulator_is_supported_voltage(struct regulator *regulator,
1768 int min_uV, int max_uV)
1769{
1770 int i, voltages, ret;
1771
1772 ret = regulator_count_voltages(regulator);
1773 if (ret < 0)
1774 return ret;
1775 voltages = ret;
1776
1777 for (i = 0; i < voltages; i++) {
1778 ret = regulator_list_voltage(regulator, i);
1779
1780 if (ret >= min_uV && ret <= max_uV)
1781 return 1;
1782 }
1783
1784 return 0;
1785}
1786
Mark Brown75790252010-12-12 14:25:50 +00001787static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1788 int min_uV, int max_uV)
1789{
1790 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001791 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001792 unsigned int selector;
1793
1794 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1795
Mark Brownbf5892a2011-05-08 22:13:37 +01001796 min_uV += rdev->constraints->uV_offset;
1797 max_uV += rdev->constraints->uV_offset;
1798
Mark Brown75790252010-12-12 14:25:50 +00001799 if (rdev->desc->ops->set_voltage) {
1800 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1801 &selector);
1802
1803 if (rdev->desc->ops->list_voltage)
1804 selector = rdev->desc->ops->list_voltage(rdev,
1805 selector);
1806 else
1807 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001808 } else if (rdev->desc->ops->set_voltage_sel) {
1809 int best_val = INT_MAX;
1810 int i;
1811
1812 selector = 0;
1813
1814 /* Find the smallest voltage that falls within the specified
1815 * range.
1816 */
1817 for (i = 0; i < rdev->desc->n_voltages; i++) {
1818 ret = rdev->desc->ops->list_voltage(rdev, i);
1819 if (ret < 0)
1820 continue;
1821
1822 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1823 best_val = ret;
1824 selector = i;
1825 }
1826 }
1827
Linus Walleij77af1b2642011-03-17 13:24:36 +01001828 /*
1829 * If we can't obtain the old selector there is not enough
1830 * info to call set_voltage_time_sel().
1831 */
1832 if (rdev->desc->ops->set_voltage_time_sel &&
1833 rdev->desc->ops->get_voltage_sel) {
1834 unsigned int old_selector = 0;
1835
1836 ret = rdev->desc->ops->get_voltage_sel(rdev);
1837 if (ret < 0)
1838 return ret;
1839 old_selector = ret;
1840 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1841 old_selector, selector);
1842 }
1843
Mark Browne8eef822010-12-12 14:36:17 +00001844 if (best_val != INT_MAX) {
1845 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1846 selector = best_val;
1847 } else {
1848 ret = -EINVAL;
1849 }
Mark Brown75790252010-12-12 14:25:50 +00001850 } else {
1851 ret = -EINVAL;
1852 }
1853
Linus Walleij77af1b2642011-03-17 13:24:36 +01001854 /* Insert any necessary delays */
1855 if (delay >= 1000) {
1856 mdelay(delay / 1000);
1857 udelay(delay % 1000);
1858 } else if (delay) {
1859 udelay(delay);
1860 }
1861
Mark Brownded06a52010-12-16 13:59:10 +00001862 if (ret == 0)
1863 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1864 NULL);
1865
Mark Brown75790252010-12-12 14:25:50 +00001866 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1867
1868 return ret;
1869}
1870
Mark Browna7a1ad92009-07-21 16:00:24 +01001871/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001872 * regulator_set_voltage - set regulator output voltage
1873 * @regulator: regulator source
1874 * @min_uV: Minimum required voltage in uV
1875 * @max_uV: Maximum acceptable voltage in uV
1876 *
1877 * Sets a voltage regulator to the desired output voltage. This can be set
1878 * during any regulator state. IOW, regulator can be disabled or enabled.
1879 *
1880 * If the regulator is enabled then the voltage will change to the new value
1881 * immediately otherwise if the regulator is disabled the regulator will
1882 * output at the new voltage when enabled.
1883 *
1884 * NOTE: If the regulator is shared between several devices then the lowest
1885 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001886 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001887 * calling this function otherwise this call will fail.
1888 */
1889int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1890{
1891 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001892 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001893
1894 mutex_lock(&rdev->mutex);
1895
Mark Brown95a3c232010-12-16 15:49:37 +00001896 /* If we're setting the same range as last time the change
1897 * should be a noop (some cpufreq implementations use the same
1898 * voltage for multiple frequencies, for example).
1899 */
1900 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1901 goto out;
1902
Liam Girdwood414c70c2008-04-30 15:59:04 +01001903 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001904 if (!rdev->desc->ops->set_voltage &&
1905 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001906 ret = -EINVAL;
1907 goto out;
1908 }
1909
1910 /* constraints check */
1911 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1912 if (ret < 0)
1913 goto out;
1914 regulator->min_uV = min_uV;
1915 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001916
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001917 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1918 if (ret < 0)
1919 goto out;
1920
Mark Brown75790252010-12-12 14:25:50 +00001921 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001922
Liam Girdwood414c70c2008-04-30 15:59:04 +01001923out:
1924 mutex_unlock(&rdev->mutex);
1925 return ret;
1926}
1927EXPORT_SYMBOL_GPL(regulator_set_voltage);
1928
Mark Brown606a2562010-12-16 15:49:36 +00001929/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001930 * regulator_set_voltage_time - get raise/fall time
1931 * @regulator: regulator source
1932 * @old_uV: starting voltage in microvolts
1933 * @new_uV: target voltage in microvolts
1934 *
1935 * Provided with the starting and ending voltage, this function attempts to
1936 * calculate the time in microseconds required to rise or fall to this new
1937 * voltage.
1938 */
1939int regulator_set_voltage_time(struct regulator *regulator,
1940 int old_uV, int new_uV)
1941{
1942 struct regulator_dev *rdev = regulator->rdev;
1943 struct regulator_ops *ops = rdev->desc->ops;
1944 int old_sel = -1;
1945 int new_sel = -1;
1946 int voltage;
1947 int i;
1948
1949 /* Currently requires operations to do this */
1950 if (!ops->list_voltage || !ops->set_voltage_time_sel
1951 || !rdev->desc->n_voltages)
1952 return -EINVAL;
1953
1954 for (i = 0; i < rdev->desc->n_voltages; i++) {
1955 /* We only look for exact voltage matches here */
1956 voltage = regulator_list_voltage(regulator, i);
1957 if (voltage < 0)
1958 return -EINVAL;
1959 if (voltage == 0)
1960 continue;
1961 if (voltage == old_uV)
1962 old_sel = i;
1963 if (voltage == new_uV)
1964 new_sel = i;
1965 }
1966
1967 if (old_sel < 0 || new_sel < 0)
1968 return -EINVAL;
1969
1970 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1971}
1972EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1973
1974/**
Mark Brown606a2562010-12-16 15:49:36 +00001975 * regulator_sync_voltage - re-apply last regulator output voltage
1976 * @regulator: regulator source
1977 *
1978 * Re-apply the last configured voltage. This is intended to be used
1979 * where some external control source the consumer is cooperating with
1980 * has caused the configured voltage to change.
1981 */
1982int regulator_sync_voltage(struct regulator *regulator)
1983{
1984 struct regulator_dev *rdev = regulator->rdev;
1985 int ret, min_uV, max_uV;
1986
1987 mutex_lock(&rdev->mutex);
1988
1989 if (!rdev->desc->ops->set_voltage &&
1990 !rdev->desc->ops->set_voltage_sel) {
1991 ret = -EINVAL;
1992 goto out;
1993 }
1994
1995 /* This is only going to work if we've had a voltage configured. */
1996 if (!regulator->min_uV && !regulator->max_uV) {
1997 ret = -EINVAL;
1998 goto out;
1999 }
2000
2001 min_uV = regulator->min_uV;
2002 max_uV = regulator->max_uV;
2003
2004 /* This should be a paranoia check... */
2005 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2006 if (ret < 0)
2007 goto out;
2008
2009 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2010 if (ret < 0)
2011 goto out;
2012
2013 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2014
2015out:
2016 mutex_unlock(&rdev->mutex);
2017 return ret;
2018}
2019EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2020
Liam Girdwood414c70c2008-04-30 15:59:04 +01002021static int _regulator_get_voltage(struct regulator_dev *rdev)
2022{
Mark Brownbf5892a2011-05-08 22:13:37 +01002023 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002024
2025 if (rdev->desc->ops->get_voltage_sel) {
2026 sel = rdev->desc->ops->get_voltage_sel(rdev);
2027 if (sel < 0)
2028 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002029 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002030 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002031 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002032 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002033 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002034 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002035
Axel Lincb220d12011-05-23 20:08:10 +08002036 if (ret < 0)
2037 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002038 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002039}
2040
2041/**
2042 * regulator_get_voltage - get regulator output voltage
2043 * @regulator: regulator source
2044 *
2045 * This returns the current regulator voltage in uV.
2046 *
2047 * NOTE: If the regulator is disabled it will return the voltage value. This
2048 * function should not be used to determine regulator state.
2049 */
2050int regulator_get_voltage(struct regulator *regulator)
2051{
2052 int ret;
2053
2054 mutex_lock(&regulator->rdev->mutex);
2055
2056 ret = _regulator_get_voltage(regulator->rdev);
2057
2058 mutex_unlock(&regulator->rdev->mutex);
2059
2060 return ret;
2061}
2062EXPORT_SYMBOL_GPL(regulator_get_voltage);
2063
2064/**
2065 * regulator_set_current_limit - set regulator output current limit
2066 * @regulator: regulator source
2067 * @min_uA: Minimuum supported current in uA
2068 * @max_uA: Maximum supported current in uA
2069 *
2070 * Sets current sink to the desired output current. This can be set during
2071 * any regulator state. IOW, regulator can be disabled or enabled.
2072 *
2073 * If the regulator is enabled then the current will change to the new value
2074 * immediately otherwise if the regulator is disabled the regulator will
2075 * output at the new current when enabled.
2076 *
2077 * NOTE: Regulator system constraints must be set for this regulator before
2078 * calling this function otherwise this call will fail.
2079 */
2080int regulator_set_current_limit(struct regulator *regulator,
2081 int min_uA, int max_uA)
2082{
2083 struct regulator_dev *rdev = regulator->rdev;
2084 int ret;
2085
2086 mutex_lock(&rdev->mutex);
2087
2088 /* sanity check */
2089 if (!rdev->desc->ops->set_current_limit) {
2090 ret = -EINVAL;
2091 goto out;
2092 }
2093
2094 /* constraints check */
2095 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2096 if (ret < 0)
2097 goto out;
2098
2099 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2100out:
2101 mutex_unlock(&rdev->mutex);
2102 return ret;
2103}
2104EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2105
2106static int _regulator_get_current_limit(struct regulator_dev *rdev)
2107{
2108 int ret;
2109
2110 mutex_lock(&rdev->mutex);
2111
2112 /* sanity check */
2113 if (!rdev->desc->ops->get_current_limit) {
2114 ret = -EINVAL;
2115 goto out;
2116 }
2117
2118 ret = rdev->desc->ops->get_current_limit(rdev);
2119out:
2120 mutex_unlock(&rdev->mutex);
2121 return ret;
2122}
2123
2124/**
2125 * regulator_get_current_limit - get regulator output current
2126 * @regulator: regulator source
2127 *
2128 * This returns the current supplied by the specified current sink in uA.
2129 *
2130 * NOTE: If the regulator is disabled it will return the current value. This
2131 * function should not be used to determine regulator state.
2132 */
2133int regulator_get_current_limit(struct regulator *regulator)
2134{
2135 return _regulator_get_current_limit(regulator->rdev);
2136}
2137EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2138
2139/**
2140 * regulator_set_mode - set regulator operating mode
2141 * @regulator: regulator source
2142 * @mode: operating mode - one of the REGULATOR_MODE constants
2143 *
2144 * Set regulator operating mode to increase regulator efficiency or improve
2145 * regulation performance.
2146 *
2147 * NOTE: Regulator system constraints must be set for this regulator before
2148 * calling this function otherwise this call will fail.
2149 */
2150int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2151{
2152 struct regulator_dev *rdev = regulator->rdev;
2153 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302154 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002155
2156 mutex_lock(&rdev->mutex);
2157
2158 /* sanity check */
2159 if (!rdev->desc->ops->set_mode) {
2160 ret = -EINVAL;
2161 goto out;
2162 }
2163
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302164 /* return if the same mode is requested */
2165 if (rdev->desc->ops->get_mode) {
2166 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2167 if (regulator_curr_mode == mode) {
2168 ret = 0;
2169 goto out;
2170 }
2171 }
2172
Liam Girdwood414c70c2008-04-30 15:59:04 +01002173 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002174 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002175 if (ret < 0)
2176 goto out;
2177
2178 ret = rdev->desc->ops->set_mode(rdev, mode);
2179out:
2180 mutex_unlock(&rdev->mutex);
2181 return ret;
2182}
2183EXPORT_SYMBOL_GPL(regulator_set_mode);
2184
2185static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2186{
2187 int ret;
2188
2189 mutex_lock(&rdev->mutex);
2190
2191 /* sanity check */
2192 if (!rdev->desc->ops->get_mode) {
2193 ret = -EINVAL;
2194 goto out;
2195 }
2196
2197 ret = rdev->desc->ops->get_mode(rdev);
2198out:
2199 mutex_unlock(&rdev->mutex);
2200 return ret;
2201}
2202
2203/**
2204 * regulator_get_mode - get regulator operating mode
2205 * @regulator: regulator source
2206 *
2207 * Get the current regulator operating mode.
2208 */
2209unsigned int regulator_get_mode(struct regulator *regulator)
2210{
2211 return _regulator_get_mode(regulator->rdev);
2212}
2213EXPORT_SYMBOL_GPL(regulator_get_mode);
2214
2215/**
2216 * regulator_set_optimum_mode - set regulator optimum operating mode
2217 * @regulator: regulator source
2218 * @uA_load: load current
2219 *
2220 * Notifies the regulator core of a new device load. This is then used by
2221 * DRMS (if enabled by constraints) to set the most efficient regulator
2222 * operating mode for the new regulator loading.
2223 *
2224 * Consumer devices notify their supply regulator of the maximum power
2225 * they will require (can be taken from device datasheet in the power
2226 * consumption tables) when they change operational status and hence power
2227 * state. Examples of operational state changes that can affect power
2228 * consumption are :-
2229 *
2230 * o Device is opened / closed.
2231 * o Device I/O is about to begin or has just finished.
2232 * o Device is idling in between work.
2233 *
2234 * This information is also exported via sysfs to userspace.
2235 *
2236 * DRMS will sum the total requested load on the regulator and change
2237 * to the most efficient operating mode if platform constraints allow.
2238 *
2239 * Returns the new regulator mode or error.
2240 */
2241int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2242{
2243 struct regulator_dev *rdev = regulator->rdev;
2244 struct regulator *consumer;
2245 int ret, output_uV, input_uV, total_uA_load = 0;
2246 unsigned int mode;
2247
2248 mutex_lock(&rdev->mutex);
2249
Mark Browna4b41482011-05-14 11:19:45 -07002250 /*
2251 * first check to see if we can set modes at all, otherwise just
2252 * tell the consumer everything is OK.
2253 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002254 regulator->uA_load = uA_load;
2255 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002256 if (ret < 0) {
2257 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002258 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002259 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002260
Liam Girdwood414c70c2008-04-30 15:59:04 +01002261 if (!rdev->desc->ops->get_optimum_mode)
2262 goto out;
2263
Mark Browna4b41482011-05-14 11:19:45 -07002264 /*
2265 * we can actually do this so any errors are indicators of
2266 * potential real failure.
2267 */
2268 ret = -EINVAL;
2269
Liam Girdwood414c70c2008-04-30 15:59:04 +01002270 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002271 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002272 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002273 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002274 goto out;
2275 }
2276
2277 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002278 input_uV = 0;
2279 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002280 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002281 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002282 input_uV = rdev->constraints->input_uV;
2283 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002284 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002285 goto out;
2286 }
2287
2288 /* calc total requested load for this regulator */
2289 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002290 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002291
2292 mode = rdev->desc->ops->get_optimum_mode(rdev,
2293 input_uV, output_uV,
2294 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002295 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002296 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002297 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2298 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002299 goto out;
2300 }
2301
2302 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002303 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002304 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002305 goto out;
2306 }
2307 ret = mode;
2308out:
2309 mutex_unlock(&rdev->mutex);
2310 return ret;
2311}
2312EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2313
2314/**
2315 * regulator_register_notifier - register regulator event notifier
2316 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002317 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002318 *
2319 * Register notifier block to receive regulator events.
2320 */
2321int regulator_register_notifier(struct regulator *regulator,
2322 struct notifier_block *nb)
2323{
2324 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2325 nb);
2326}
2327EXPORT_SYMBOL_GPL(regulator_register_notifier);
2328
2329/**
2330 * regulator_unregister_notifier - unregister regulator event notifier
2331 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002332 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002333 *
2334 * Unregister regulator event notifier block.
2335 */
2336int regulator_unregister_notifier(struct regulator *regulator,
2337 struct notifier_block *nb)
2338{
2339 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2340 nb);
2341}
2342EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2343
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002344/* notify regulator consumers and downstream regulator consumers.
2345 * Note mutex must be held by caller.
2346 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002347static void _notifier_call_chain(struct regulator_dev *rdev,
2348 unsigned long event, void *data)
2349{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002350 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002351 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002352}
2353
2354/**
2355 * regulator_bulk_get - get multiple regulator consumers
2356 *
2357 * @dev: Device to supply
2358 * @num_consumers: Number of consumers to register
2359 * @consumers: Configuration of consumers; clients are stored here.
2360 *
2361 * @return 0 on success, an errno on failure.
2362 *
2363 * This helper function allows drivers to get several regulator
2364 * consumers in one operation. If any of the regulators cannot be
2365 * acquired then any regulators that were allocated will be freed
2366 * before returning to the caller.
2367 */
2368int regulator_bulk_get(struct device *dev, int num_consumers,
2369 struct regulator_bulk_data *consumers)
2370{
2371 int i;
2372 int ret;
2373
2374 for (i = 0; i < num_consumers; i++)
2375 consumers[i].consumer = NULL;
2376
2377 for (i = 0; i < num_consumers; i++) {
2378 consumers[i].consumer = regulator_get(dev,
2379 consumers[i].supply);
2380 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002381 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002382 dev_err(dev, "Failed to get supply '%s': %d\n",
2383 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002384 consumers[i].consumer = NULL;
2385 goto err;
2386 }
2387 }
2388
2389 return 0;
2390
2391err:
2392 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2393 regulator_put(consumers[i].consumer);
2394
2395 return ret;
2396}
2397EXPORT_SYMBOL_GPL(regulator_bulk_get);
2398
Mark Brownf21e0e82011-05-24 08:12:40 +08002399static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2400{
2401 struct regulator_bulk_data *bulk = data;
2402
2403 bulk->ret = regulator_enable(bulk->consumer);
2404}
2405
Liam Girdwood414c70c2008-04-30 15:59:04 +01002406/**
2407 * regulator_bulk_enable - enable multiple regulator consumers
2408 *
2409 * @num_consumers: Number of consumers
2410 * @consumers: Consumer data; clients are stored here.
2411 * @return 0 on success, an errno on failure
2412 *
2413 * This convenience API allows consumers to enable multiple regulator
2414 * clients in a single API call. If any consumers cannot be enabled
2415 * then any others that were enabled will be disabled again prior to
2416 * return.
2417 */
2418int regulator_bulk_enable(int num_consumers,
2419 struct regulator_bulk_data *consumers)
2420{
Mark Brownf21e0e82011-05-24 08:12:40 +08002421 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002422 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002423 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002424
Mark Brownf21e0e82011-05-24 08:12:40 +08002425 for (i = 0; i < num_consumers; i++)
2426 async_schedule_domain(regulator_bulk_enable_async,
2427 &consumers[i], &async_domain);
2428
2429 async_synchronize_full_domain(&async_domain);
2430
2431 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002432 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002433 if (consumers[i].ret != 0) {
2434 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002435 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002436 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002437 }
2438
2439 return 0;
2440
2441err:
Mark Brownf21e0e82011-05-24 08:12:40 +08002442 for (i = 0; i < num_consumers; i++)
2443 if (consumers[i].ret == 0)
2444 regulator_disable(consumers[i].consumer);
2445 else
2446 pr_err("Failed to enable %s: %d\n",
2447 consumers[i].supply, consumers[i].ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002448
2449 return ret;
2450}
2451EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2452
2453/**
2454 * regulator_bulk_disable - disable multiple regulator consumers
2455 *
2456 * @num_consumers: Number of consumers
2457 * @consumers: Consumer data; clients are stored here.
2458 * @return 0 on success, an errno on failure
2459 *
2460 * This convenience API allows consumers to disable multiple regulator
2461 * clients in a single API call. If any consumers cannot be enabled
2462 * then any others that were disabled will be disabled again prior to
2463 * return.
2464 */
2465int regulator_bulk_disable(int num_consumers,
2466 struct regulator_bulk_data *consumers)
2467{
2468 int i;
2469 int ret;
2470
2471 for (i = 0; i < num_consumers; i++) {
2472 ret = regulator_disable(consumers[i].consumer);
2473 if (ret != 0)
2474 goto err;
2475 }
2476
2477 return 0;
2478
2479err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002480 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002481 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002482 regulator_enable(consumers[i].consumer);
2483
2484 return ret;
2485}
2486EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2487
2488/**
2489 * regulator_bulk_free - free multiple regulator consumers
2490 *
2491 * @num_consumers: Number of consumers
2492 * @consumers: Consumer data; clients are stored here.
2493 *
2494 * This convenience API allows consumers to free multiple regulator
2495 * clients in a single API call.
2496 */
2497void regulator_bulk_free(int num_consumers,
2498 struct regulator_bulk_data *consumers)
2499{
2500 int i;
2501
2502 for (i = 0; i < num_consumers; i++) {
2503 regulator_put(consumers[i].consumer);
2504 consumers[i].consumer = NULL;
2505 }
2506}
2507EXPORT_SYMBOL_GPL(regulator_bulk_free);
2508
2509/**
2510 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002511 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002512 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002513 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002514 *
2515 * Called by regulator drivers to notify clients a regulator event has
2516 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002517 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002518 */
2519int regulator_notifier_call_chain(struct regulator_dev *rdev,
2520 unsigned long event, void *data)
2521{
2522 _notifier_call_chain(rdev, event, data);
2523 return NOTIFY_DONE;
2524
2525}
2526EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2527
Mark Brownbe721972009-08-04 20:09:52 +02002528/**
2529 * regulator_mode_to_status - convert a regulator mode into a status
2530 *
2531 * @mode: Mode to convert
2532 *
2533 * Convert a regulator mode into a status.
2534 */
2535int regulator_mode_to_status(unsigned int mode)
2536{
2537 switch (mode) {
2538 case REGULATOR_MODE_FAST:
2539 return REGULATOR_STATUS_FAST;
2540 case REGULATOR_MODE_NORMAL:
2541 return REGULATOR_STATUS_NORMAL;
2542 case REGULATOR_MODE_IDLE:
2543 return REGULATOR_STATUS_IDLE;
2544 case REGULATOR_STATUS_STANDBY:
2545 return REGULATOR_STATUS_STANDBY;
2546 default:
2547 return 0;
2548 }
2549}
2550EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2551
David Brownell7ad68e22008-11-11 17:39:02 -08002552/*
2553 * To avoid cluttering sysfs (and memory) with useless state, only
2554 * create attributes that can be meaningfully displayed.
2555 */
2556static int add_regulator_attributes(struct regulator_dev *rdev)
2557{
2558 struct device *dev = &rdev->dev;
2559 struct regulator_ops *ops = rdev->desc->ops;
2560 int status = 0;
2561
2562 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002563 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002564 status = device_create_file(dev, &dev_attr_microvolts);
2565 if (status < 0)
2566 return status;
2567 }
2568 if (ops->get_current_limit) {
2569 status = device_create_file(dev, &dev_attr_microamps);
2570 if (status < 0)
2571 return status;
2572 }
2573 if (ops->get_mode) {
2574 status = device_create_file(dev, &dev_attr_opmode);
2575 if (status < 0)
2576 return status;
2577 }
2578 if (ops->is_enabled) {
2579 status = device_create_file(dev, &dev_attr_state);
2580 if (status < 0)
2581 return status;
2582 }
David Brownell853116a2009-01-14 23:03:17 -08002583 if (ops->get_status) {
2584 status = device_create_file(dev, &dev_attr_status);
2585 if (status < 0)
2586 return status;
2587 }
David Brownell7ad68e22008-11-11 17:39:02 -08002588
2589 /* some attributes are type-specific */
2590 if (rdev->desc->type == REGULATOR_CURRENT) {
2591 status = device_create_file(dev, &dev_attr_requested_microamps);
2592 if (status < 0)
2593 return status;
2594 }
2595
2596 /* all the other attributes exist to support constraints;
2597 * don't show them if there are no constraints, or if the
2598 * relevant supporting methods are missing.
2599 */
2600 if (!rdev->constraints)
2601 return status;
2602
2603 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002604 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002605 status = device_create_file(dev, &dev_attr_min_microvolts);
2606 if (status < 0)
2607 return status;
2608 status = device_create_file(dev, &dev_attr_max_microvolts);
2609 if (status < 0)
2610 return status;
2611 }
2612 if (ops->set_current_limit) {
2613 status = device_create_file(dev, &dev_attr_min_microamps);
2614 if (status < 0)
2615 return status;
2616 status = device_create_file(dev, &dev_attr_max_microamps);
2617 if (status < 0)
2618 return status;
2619 }
2620
2621 /* suspend mode constraints need multiple supporting methods */
2622 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2623 return status;
2624
2625 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2626 if (status < 0)
2627 return status;
2628 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2629 if (status < 0)
2630 return status;
2631 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2632 if (status < 0)
2633 return status;
2634
2635 if (ops->set_suspend_voltage) {
2636 status = device_create_file(dev,
2637 &dev_attr_suspend_standby_microvolts);
2638 if (status < 0)
2639 return status;
2640 status = device_create_file(dev,
2641 &dev_attr_suspend_mem_microvolts);
2642 if (status < 0)
2643 return status;
2644 status = device_create_file(dev,
2645 &dev_attr_suspend_disk_microvolts);
2646 if (status < 0)
2647 return status;
2648 }
2649
2650 if (ops->set_suspend_mode) {
2651 status = device_create_file(dev,
2652 &dev_attr_suspend_standby_mode);
2653 if (status < 0)
2654 return status;
2655 status = device_create_file(dev,
2656 &dev_attr_suspend_mem_mode);
2657 if (status < 0)
2658 return status;
2659 status = device_create_file(dev,
2660 &dev_attr_suspend_disk_mode);
2661 if (status < 0)
2662 return status;
2663 }
2664
2665 return status;
2666}
2667
Mark Brown1130e5b2010-12-21 23:49:31 +00002668static void rdev_init_debugfs(struct regulator_dev *rdev)
2669{
2670#ifdef CONFIG_DEBUG_FS
2671 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2672 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2673 rdev_warn(rdev, "Failed to create debugfs directory\n");
2674 rdev->debugfs = NULL;
2675 return;
2676 }
2677
2678 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2679 &rdev->use_count);
2680 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2681 &rdev->open_count);
2682#endif
2683}
2684
Liam Girdwood414c70c2008-04-30 15:59:04 +01002685/**
2686 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002687 * @regulator_desc: regulator to register
2688 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002689 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002690 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002691 *
2692 * Called by regulator drivers to register a regulator.
2693 * Returns 0 on success.
2694 */
2695struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002696 struct device *dev, const struct regulator_init_data *init_data,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302697 void *driver_data, struct device_node *of_node)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002698{
2699 static atomic_t regulator_no = ATOMIC_INIT(0);
2700 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002701 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05302702 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002703
2704 if (regulator_desc == NULL)
2705 return ERR_PTR(-EINVAL);
2706
2707 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2708 return ERR_PTR(-EINVAL);
2709
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002710 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2711 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002712 return ERR_PTR(-EINVAL);
2713
Mark Brown46fabe1e2008-09-09 16:21:18 +01002714 if (!init_data)
2715 return ERR_PTR(-EINVAL);
2716
Mark Brown476c2d82010-12-10 17:28:07 +00002717 /* Only one of each should be implemented */
2718 WARN_ON(regulator_desc->ops->get_voltage &&
2719 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002720 WARN_ON(regulator_desc->ops->set_voltage &&
2721 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002722
2723 /* If we're using selectors we must implement list_voltage. */
2724 if (regulator_desc->ops->get_voltage_sel &&
2725 !regulator_desc->ops->list_voltage) {
2726 return ERR_PTR(-EINVAL);
2727 }
Mark Browne8eef822010-12-12 14:36:17 +00002728 if (regulator_desc->ops->set_voltage_sel &&
2729 !regulator_desc->ops->list_voltage) {
2730 return ERR_PTR(-EINVAL);
2731 }
Mark Brown476c2d82010-12-10 17:28:07 +00002732
Liam Girdwood414c70c2008-04-30 15:59:04 +01002733 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2734 if (rdev == NULL)
2735 return ERR_PTR(-ENOMEM);
2736
2737 mutex_lock(&regulator_list_mutex);
2738
2739 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002740 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002741 rdev->owner = regulator_desc->owner;
2742 rdev->desc = regulator_desc;
2743 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002744 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002745 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01002746 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002747
Liam Girdwooda5766f12008-10-10 13:22:20 +01002748 /* preform any regulator specific init */
2749 if (init_data->regulator_init) {
2750 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002751 if (ret < 0)
2752 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002753 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002754
Liam Girdwooda5766f12008-10-10 13:22:20 +01002755 /* register with sysfs */
2756 rdev->dev.class = &regulator_class;
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302757 rdev->dev.of_node = of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002758 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002759 dev_set_name(&rdev->dev, "regulator.%d",
2760 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002761 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002762 if (ret != 0) {
2763 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002764 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002765 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002766
2767 dev_set_drvdata(&rdev->dev, rdev);
2768
Mike Rapoport74f544c2008-11-25 14:53:53 +02002769 /* set regulator constraints */
2770 ret = set_machine_constraints(rdev, &init_data->constraints);
2771 if (ret < 0)
2772 goto scrub;
2773
David Brownell7ad68e22008-11-11 17:39:02 -08002774 /* add attributes supported by this regulator */
2775 ret = add_regulator_attributes(rdev);
2776 if (ret < 0)
2777 goto scrub;
2778
Rajendra Nayak69511a42011-11-18 16:47:20 +05302779 if (init_data->supply_regulator)
2780 supply = init_data->supply_regulator;
2781 else if (regulator_desc->supply_name)
2782 supply = regulator_desc->supply_name;
2783
2784 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002785 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01002786
Rajendra Nayak69511a42011-11-18 16:47:20 +05302787 r = regulator_dev_lookup(dev, supply);
Mark Brown0178f3e2010-04-26 15:18:14 +01002788
Rajendra Nayak69511a42011-11-18 16:47:20 +05302789 if (!r) {
2790 dev_err(dev, "Failed to find supply %s\n", supply);
Axel Lin7727da22010-11-05 15:27:17 +08002791 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002792 goto scrub;
2793 }
2794
2795 ret = set_supply(rdev, r);
2796 if (ret < 0)
2797 goto scrub;
2798 }
2799
Liam Girdwooda5766f12008-10-10 13:22:20 +01002800 /* add consumers devices */
2801 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2802 ret = set_consumer_device_supply(rdev,
2803 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002804 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002805 init_data->consumer_supplies[i].supply);
Mark Brown23c2f042011-02-24 17:39:09 +00002806 if (ret < 0) {
2807 dev_err(dev, "Failed to set supply %s\n",
2808 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002809 goto unset_supplies;
Mark Brown23c2f042011-02-24 17:39:09 +00002810 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002811 }
2812
2813 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002814
2815 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002816out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002817 mutex_unlock(&regulator_list_mutex);
2818 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002819
Jani Nikulad4033b52010-04-29 10:55:11 +03002820unset_supplies:
2821 unset_regulator_supplies(rdev);
2822
David Brownell4fca9542008-11-11 17:38:53 -08002823scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08002824 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08002825 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002826 /* device core frees rdev */
2827 rdev = ERR_PTR(ret);
2828 goto out;
2829
David Brownell4fca9542008-11-11 17:38:53 -08002830clean:
2831 kfree(rdev);
2832 rdev = ERR_PTR(ret);
2833 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002834}
2835EXPORT_SYMBOL_GPL(regulator_register);
2836
2837/**
2838 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002839 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002840 *
2841 * Called by regulator drivers to unregister a regulator.
2842 */
2843void regulator_unregister(struct regulator_dev *rdev)
2844{
2845 if (rdev == NULL)
2846 return;
2847
2848 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00002849#ifdef CONFIG_DEBUG_FS
2850 debugfs_remove_recursive(rdev->debugfs);
2851#endif
Mark Brownda07ecd2011-09-11 09:53:50 +01002852 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01002853 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002854 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002855 list_del(&rdev->list);
2856 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002857 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002858 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002859 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002860 mutex_unlock(&regulator_list_mutex);
2861}
2862EXPORT_SYMBOL_GPL(regulator_unregister);
2863
2864/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002865 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002866 * @state: system suspend state
2867 *
2868 * Configure each regulator with it's suspend operating parameters for state.
2869 * This will usually be called by machine suspend code prior to supending.
2870 */
2871int regulator_suspend_prepare(suspend_state_t state)
2872{
2873 struct regulator_dev *rdev;
2874 int ret = 0;
2875
2876 /* ON is handled by regulator active state */
2877 if (state == PM_SUSPEND_ON)
2878 return -EINVAL;
2879
2880 mutex_lock(&regulator_list_mutex);
2881 list_for_each_entry(rdev, &regulator_list, list) {
2882
2883 mutex_lock(&rdev->mutex);
2884 ret = suspend_prepare(rdev, state);
2885 mutex_unlock(&rdev->mutex);
2886
2887 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002888 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002889 goto out;
2890 }
2891 }
2892out:
2893 mutex_unlock(&regulator_list_mutex);
2894 return ret;
2895}
2896EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2897
2898/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09002899 * regulator_suspend_finish - resume regulators from system wide suspend
2900 *
2901 * Turn on regulators that might be turned off by regulator_suspend_prepare
2902 * and that should be turned on according to the regulators properties.
2903 */
2904int regulator_suspend_finish(void)
2905{
2906 struct regulator_dev *rdev;
2907 int ret = 0, error;
2908
2909 mutex_lock(&regulator_list_mutex);
2910 list_for_each_entry(rdev, &regulator_list, list) {
2911 struct regulator_ops *ops = rdev->desc->ops;
2912
2913 mutex_lock(&rdev->mutex);
2914 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2915 ops->enable) {
2916 error = ops->enable(rdev);
2917 if (error)
2918 ret = error;
2919 } else {
2920 if (!has_full_constraints)
2921 goto unlock;
2922 if (!ops->disable)
2923 goto unlock;
2924 if (ops->is_enabled && !ops->is_enabled(rdev))
2925 goto unlock;
2926
2927 error = ops->disable(rdev);
2928 if (error)
2929 ret = error;
2930 }
2931unlock:
2932 mutex_unlock(&rdev->mutex);
2933 }
2934 mutex_unlock(&regulator_list_mutex);
2935 return ret;
2936}
2937EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2938
2939/**
Mark Brownca725562009-03-16 19:36:34 +00002940 * regulator_has_full_constraints - the system has fully specified constraints
2941 *
2942 * Calling this function will cause the regulator API to disable all
2943 * regulators which have a zero use count and don't have an always_on
2944 * constraint in a late_initcall.
2945 *
2946 * The intention is that this will become the default behaviour in a
2947 * future kernel release so users are encouraged to use this facility
2948 * now.
2949 */
2950void regulator_has_full_constraints(void)
2951{
2952 has_full_constraints = 1;
2953}
2954EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2955
2956/**
Mark Brown688fe992010-10-05 19:18:32 -07002957 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2958 *
2959 * Calling this function will cause the regulator API to provide a
2960 * dummy regulator to consumers if no physical regulator is found,
2961 * allowing most consumers to proceed as though a regulator were
2962 * configured. This allows systems such as those with software
2963 * controllable regulators for the CPU core only to be brought up more
2964 * readily.
2965 */
2966void regulator_use_dummy_regulator(void)
2967{
2968 board_wants_dummy_regulator = true;
2969}
2970EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2971
2972/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002973 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002974 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002975 *
2976 * Get rdev regulator driver private data. This call can be used in the
2977 * regulator driver context.
2978 */
2979void *rdev_get_drvdata(struct regulator_dev *rdev)
2980{
2981 return rdev->reg_data;
2982}
2983EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2984
2985/**
2986 * regulator_get_drvdata - get regulator driver data
2987 * @regulator: regulator
2988 *
2989 * Get regulator driver private data. This call can be used in the consumer
2990 * driver context when non API regulator specific functions need to be called.
2991 */
2992void *regulator_get_drvdata(struct regulator *regulator)
2993{
2994 return regulator->rdev->reg_data;
2995}
2996EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2997
2998/**
2999 * regulator_set_drvdata - set regulator driver data
3000 * @regulator: regulator
3001 * @data: data
3002 */
3003void regulator_set_drvdata(struct regulator *regulator, void *data)
3004{
3005 regulator->rdev->reg_data = data;
3006}
3007EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3008
3009/**
3010 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003011 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003012 */
3013int rdev_get_id(struct regulator_dev *rdev)
3014{
3015 return rdev->desc->id;
3016}
3017EXPORT_SYMBOL_GPL(rdev_get_id);
3018
Liam Girdwooda5766f12008-10-10 13:22:20 +01003019struct device *rdev_get_dev(struct regulator_dev *rdev)
3020{
3021 return &rdev->dev;
3022}
3023EXPORT_SYMBOL_GPL(rdev_get_dev);
3024
3025void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3026{
3027 return reg_init_data->driver_data;
3028}
3029EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3030
Mark Brownba55a972011-08-23 17:39:10 +01003031#ifdef CONFIG_DEBUG_FS
3032static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3033 size_t count, loff_t *ppos)
3034{
3035 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3036 ssize_t len, ret = 0;
3037 struct regulator_map *map;
3038
3039 if (!buf)
3040 return -ENOMEM;
3041
3042 list_for_each_entry(map, &regulator_map_list, list) {
3043 len = snprintf(buf + ret, PAGE_SIZE - ret,
3044 "%s -> %s.%s\n",
3045 rdev_get_name(map->regulator), map->dev_name,
3046 map->supply);
3047 if (len >= 0)
3048 ret += len;
3049 if (ret > PAGE_SIZE) {
3050 ret = PAGE_SIZE;
3051 break;
3052 }
3053 }
3054
3055 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3056
3057 kfree(buf);
3058
3059 return ret;
3060}
3061
3062static const struct file_operations supply_map_fops = {
3063 .read = supply_map_read_file,
3064 .llseek = default_llseek,
3065};
3066#endif
3067
Liam Girdwood414c70c2008-04-30 15:59:04 +01003068static int __init regulator_init(void)
3069{
Mark Brown34abbd62010-02-12 10:18:08 +00003070 int ret;
3071
Mark Brown34abbd62010-02-12 10:18:08 +00003072 ret = class_register(&regulator_class);
3073
Mark Brown1130e5b2010-12-21 23:49:31 +00003074#ifdef CONFIG_DEBUG_FS
3075 debugfs_root = debugfs_create_dir("regulator", NULL);
3076 if (IS_ERR(debugfs_root) || !debugfs_root) {
3077 pr_warn("regulator: Failed to create debugfs directory\n");
3078 debugfs_root = NULL;
3079 }
Mark Brownba55a972011-08-23 17:39:10 +01003080
3081 if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root,
3082 NULL, &supply_map_fops)))
3083 pr_warn("regulator: Failed to create supplies debugfs\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003084#endif
3085
Mark Brown34abbd62010-02-12 10:18:08 +00003086 regulator_dummy_init();
3087
3088 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003089}
3090
3091/* init early to allow our consumers to complete system booting */
3092core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003093
3094static int __init regulator_init_complete(void)
3095{
3096 struct regulator_dev *rdev;
3097 struct regulator_ops *ops;
3098 struct regulation_constraints *c;
3099 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003100
3101 mutex_lock(&regulator_list_mutex);
3102
3103 /* If we have a full configuration then disable any regulators
3104 * which are not in use or always_on. This will become the
3105 * default behaviour in the future.
3106 */
3107 list_for_each_entry(rdev, &regulator_list, list) {
3108 ops = rdev->desc->ops;
3109 c = rdev->constraints;
3110
Mark Brownf25e0b42009-08-03 18:49:55 +01003111 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003112 continue;
3113
3114 mutex_lock(&rdev->mutex);
3115
3116 if (rdev->use_count)
3117 goto unlock;
3118
3119 /* If we can't read the status assume it's on. */
3120 if (ops->is_enabled)
3121 enabled = ops->is_enabled(rdev);
3122 else
3123 enabled = 1;
3124
3125 if (!enabled)
3126 goto unlock;
3127
3128 if (has_full_constraints) {
3129 /* We log since this may kill the system if it
3130 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003131 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003132 ret = ops->disable(rdev);
3133 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003134 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003135 }
3136 } else {
3137 /* The intention is that in future we will
3138 * assume that full constraints are provided
3139 * so warn even if we aren't going to do
3140 * anything here.
3141 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003142 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003143 }
3144
3145unlock:
3146 mutex_unlock(&rdev->mutex);
3147 }
3148
3149 mutex_unlock(&regulator_list_mutex);
3150
3151 return 0;
3152}
3153late_initcall(regulator_init_complete);