blob: 214640db084b466c931957d11f8eabd29dee9525 [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 Brown9a8f5e02011-11-29 18:11:19 +0000915 if (constraints)
916 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
917 GFP_KERNEL);
918 else
919 rdev->constraints = kzalloc(sizeof(*constraints),
920 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000921 if (!rdev->constraints)
922 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100923
Mark Brownf8c12fe2010-11-29 15:55:17 +0000924 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100925 if (ret != 0)
926 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800927
Liam Girdwooda5766f12008-10-10 13:22:20 +0100928 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000929 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000930 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100931 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800932 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100933 goto out;
934 }
935 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100936
Mark Brown9a8f5e02011-11-29 18:11:19 +0000937 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000938 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800939 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000940 ret = -EINVAL;
941 goto out;
942 }
943
Mark Brownf8c12fe2010-11-29 15:55:17 +0000944 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000945 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800946 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000947 goto out;
948 }
949 }
950
Mark Browncacf90f2009-03-02 16:32:46 +0000951 /* If the constraints say the regulator should be on at this point
952 * and we have control then make sure it is enabled.
953 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000954 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
955 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100956 ret = ops->enable(rdev);
957 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800958 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100959 goto out;
960 }
961 }
962
Liam Girdwooda5766f12008-10-10 13:22:20 +0100963 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800964 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100965out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800966 kfree(rdev->constraints);
967 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100968 return ret;
969}
970
971/**
972 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000973 * @rdev: regulator name
974 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100975 *
976 * Called by platform initialisation code to set the supply regulator for this
977 * regulator. This ensures that a regulators supply will also be enabled by the
978 * core if it's child is enabled.
979 */
980static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100981 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100982{
983 int err;
984
Mark Brown3801b862011-06-09 16:22:22 +0100985 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
986
987 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +0800988 if (rdev->supply == NULL) {
989 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +0100990 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100991 }
Mark Brown3801b862011-06-09 16:22:22 +0100992
993 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100994}
995
996/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800997 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000998 * @rdev: regulator source
999 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +01001000 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001001 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001002 *
1003 * Allows platform initialisation code to map physical regulator
1004 * sources to symbolic names for supplies for use by devices. Devices
1005 * should use these symbolic names to request regulators, avoiding the
1006 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +01001007 *
1008 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +01001009 */
1010static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +01001011 struct device *consumer_dev, const char *consumer_dev_name,
1012 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001013{
1014 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001015 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001016
Mark Brown40f92442009-06-17 17:56:39 +01001017 if (consumer_dev && consumer_dev_name)
1018 return -EINVAL;
1019
1020 if (!consumer_dev_name && consumer_dev)
1021 consumer_dev_name = dev_name(consumer_dev);
1022
Liam Girdwooda5766f12008-10-10 13:22:20 +01001023 if (supply == NULL)
1024 return -EINVAL;
1025
Mark Brown9ed20992009-07-21 16:00:26 +01001026 if (consumer_dev_name != NULL)
1027 has_dev = 1;
1028 else
1029 has_dev = 0;
1030
David Brownell6001e132008-12-31 12:54:19 +00001031 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001032 if (node->dev_name && consumer_dev_name) {
1033 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1034 continue;
1035 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001036 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001037 }
1038
David Brownell6001e132008-12-31 12:54:19 +00001039 if (strcmp(node->supply, supply) != 0)
1040 continue;
1041
1042 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001043 dev_name(&node->regulator->dev),
1044 node->regulator->desc->name,
1045 supply,
1046 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001047 return -EBUSY;
1048 }
1049
Mark Brown9ed20992009-07-21 16:00:26 +01001050 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001051 if (node == NULL)
1052 return -ENOMEM;
1053
1054 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001055 node->supply = supply;
1056
Mark Brown9ed20992009-07-21 16:00:26 +01001057 if (has_dev) {
1058 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1059 if (node->dev_name == NULL) {
1060 kfree(node);
1061 return -ENOMEM;
1062 }
Mark Brown40f92442009-06-17 17:56:39 +01001063 }
1064
Liam Girdwooda5766f12008-10-10 13:22:20 +01001065 list_add(&node->list, &regulator_map_list);
1066 return 0;
1067}
1068
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001069static void unset_regulator_supplies(struct regulator_dev *rdev)
1070{
1071 struct regulator_map *node, *n;
1072
1073 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1074 if (rdev == node->regulator) {
1075 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001076 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001077 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001078 }
1079 }
1080}
1081
Mark Brownf5726ae2011-06-09 16:22:20 +01001082#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001083
1084static struct regulator *create_regulator(struct regulator_dev *rdev,
1085 struct device *dev,
1086 const char *supply_name)
1087{
1088 struct regulator *regulator;
1089 char buf[REG_STR_SIZE];
1090 int err, size;
1091
1092 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1093 if (regulator == NULL)
1094 return NULL;
1095
1096 mutex_lock(&rdev->mutex);
1097 regulator->rdev = rdev;
1098 list_add(&regulator->list, &rdev->consumer_list);
1099
1100 if (dev) {
1101 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001102 size = scnprintf(buf, REG_STR_SIZE,
1103 "microamps_requested_%s-%s",
1104 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001105 if (size >= REG_STR_SIZE)
1106 goto overflow_err;
1107
1108 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001109 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001110 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1111 if (regulator->dev_attr.attr.name == NULL)
1112 goto attr_name_err;
1113
Liam Girdwood414c70c2008-04-30 15:59:04 +01001114 regulator->dev_attr.attr.mode = 0444;
1115 regulator->dev_attr.show = device_requested_uA_show;
1116 err = device_create_file(dev, &regulator->dev_attr);
1117 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001118 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001119 goto attr_name_err;
1120 }
1121
1122 /* also add a link to the device sysfs entry */
1123 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1124 dev->kobj.name, supply_name);
1125 if (size >= REG_STR_SIZE)
1126 goto attr_err;
1127
1128 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1129 if (regulator->supply_name == NULL)
1130 goto attr_err;
1131
1132 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1133 buf);
1134 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001135 rdev_warn(rdev, "could not add device link %s err %d\n",
1136 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001137 goto link_name_err;
1138 }
Mark Brown5de70512011-06-19 13:33:16 +01001139 } else {
1140 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1141 if (regulator->supply_name == NULL)
1142 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001143 }
Mark Brown5de70512011-06-19 13:33:16 +01001144
1145#ifdef CONFIG_DEBUG_FS
1146 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1147 rdev->debugfs);
1148 if (IS_ERR_OR_NULL(regulator->debugfs)) {
1149 rdev_warn(rdev, "Failed to create debugfs directory\n");
1150 regulator->debugfs = NULL;
1151 } else {
1152 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1153 &regulator->uA_load);
1154 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1155 &regulator->min_uV);
1156 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1157 &regulator->max_uV);
1158 }
1159#endif
1160
Liam Girdwood414c70c2008-04-30 15:59:04 +01001161 mutex_unlock(&rdev->mutex);
1162 return regulator;
1163link_name_err:
1164 kfree(regulator->supply_name);
1165attr_err:
1166 device_remove_file(regulator->dev, &regulator->dev_attr);
1167attr_name_err:
1168 kfree(regulator->dev_attr.attr.name);
1169overflow_err:
1170 list_del(&regulator->list);
1171 kfree(regulator);
1172 mutex_unlock(&rdev->mutex);
1173 return NULL;
1174}
1175
Mark Brown31aae2b2009-12-21 12:21:52 +00001176static int _regulator_get_enable_time(struct regulator_dev *rdev)
1177{
1178 if (!rdev->desc->ops->enable_time)
1179 return 0;
1180 return rdev->desc->ops->enable_time(rdev);
1181}
1182
Rajendra Nayak69511a42011-11-18 16:47:20 +05301183static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1184 const char *supply)
1185{
1186 struct regulator_dev *r;
1187 struct device_node *node;
1188
1189 /* first do a dt based lookup */
1190 if (dev && dev->of_node) {
1191 node = of_get_regulator(dev, supply);
1192 if (node)
1193 list_for_each_entry(r, &regulator_list, list)
1194 if (r->dev.parent &&
1195 node == r->dev.of_node)
1196 return r;
1197 }
1198
1199 /* if not found, try doing it non-dt way */
1200 list_for_each_entry(r, &regulator_list, list)
1201 if (strcmp(rdev_get_name(r), supply) == 0)
1202 return r;
1203
1204 return NULL;
1205}
1206
Mark Brown5ffbd132009-07-21 16:00:23 +01001207/* Internal regulator request function */
1208static struct regulator *_regulator_get(struct device *dev, const char *id,
1209 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001210{
1211 struct regulator_dev *rdev;
1212 struct regulator_map *map;
1213 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001214 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001215 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001216
1217 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001218 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001219 return regulator;
1220 }
1221
Mark Brown40f92442009-06-17 17:56:39 +01001222 if (dev)
1223 devname = dev_name(dev);
1224
Liam Girdwood414c70c2008-04-30 15:59:04 +01001225 mutex_lock(&regulator_list_mutex);
1226
Rajendra Nayak69511a42011-11-18 16:47:20 +05301227 rdev = regulator_dev_lookup(dev, id);
1228 if (rdev)
1229 goto found;
1230
Liam Girdwood414c70c2008-04-30 15:59:04 +01001231 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001232 /* If the mapping has a device set up it must match */
1233 if (map->dev_name &&
1234 (!devname || strcmp(map->dev_name, devname)))
1235 continue;
1236
1237 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001238 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001239 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001240 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001241 }
Mark Brown34abbd62010-02-12 10:18:08 +00001242
Mark Brown688fe992010-10-05 19:18:32 -07001243 if (board_wants_dummy_regulator) {
1244 rdev = dummy_regulator_rdev;
1245 goto found;
1246 }
1247
Mark Brown34abbd62010-02-12 10:18:08 +00001248#ifdef CONFIG_REGULATOR_DUMMY
1249 if (!devname)
1250 devname = "deviceless";
1251
1252 /* If the board didn't flag that it was fully constrained then
1253 * substitute in a dummy regulator so consumers can continue.
1254 */
1255 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001256 pr_warn("%s supply %s not found, using dummy regulator\n",
1257 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001258 rdev = dummy_regulator_rdev;
1259 goto found;
1260 }
1261#endif
1262
Liam Girdwood414c70c2008-04-30 15:59:04 +01001263 mutex_unlock(&regulator_list_mutex);
1264 return regulator;
1265
1266found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001267 if (rdev->exclusive) {
1268 regulator = ERR_PTR(-EPERM);
1269 goto out;
1270 }
1271
1272 if (exclusive && rdev->open_count) {
1273 regulator = ERR_PTR(-EBUSY);
1274 goto out;
1275 }
1276
Liam Girdwooda5766f12008-10-10 13:22:20 +01001277 if (!try_module_get(rdev->owner))
1278 goto out;
1279
Liam Girdwood414c70c2008-04-30 15:59:04 +01001280 regulator = create_regulator(rdev, dev, id);
1281 if (regulator == NULL) {
1282 regulator = ERR_PTR(-ENOMEM);
1283 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001284 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001285 }
1286
Mark Brown5ffbd132009-07-21 16:00:23 +01001287 rdev->open_count++;
1288 if (exclusive) {
1289 rdev->exclusive = 1;
1290
1291 ret = _regulator_is_enabled(rdev);
1292 if (ret > 0)
1293 rdev->use_count = 1;
1294 else
1295 rdev->use_count = 0;
1296 }
1297
Liam Girdwooda5766f12008-10-10 13:22:20 +01001298out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001299 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001300
Liam Girdwood414c70c2008-04-30 15:59:04 +01001301 return regulator;
1302}
Mark Brown5ffbd132009-07-21 16:00:23 +01001303
1304/**
1305 * regulator_get - lookup and obtain a reference to a regulator.
1306 * @dev: device for regulator "consumer"
1307 * @id: Supply name or regulator ID.
1308 *
1309 * Returns a struct regulator corresponding to the regulator producer,
1310 * or IS_ERR() condition containing errno.
1311 *
1312 * Use of supply names configured via regulator_set_device_supply() is
1313 * strongly encouraged. It is recommended that the supply name used
1314 * should match the name used for the supply and/or the relevant
1315 * device pins in the datasheet.
1316 */
1317struct regulator *regulator_get(struct device *dev, const char *id)
1318{
1319 return _regulator_get(dev, id, 0);
1320}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001321EXPORT_SYMBOL_GPL(regulator_get);
1322
Stephen Boyd070b9072012-01-16 19:39:58 -08001323static void devm_regulator_release(struct device *dev, void *res)
1324{
1325 regulator_put(*(struct regulator **)res);
1326}
1327
1328/**
1329 * devm_regulator_get - Resource managed regulator_get()
1330 * @dev: device for regulator "consumer"
1331 * @id: Supply name or regulator ID.
1332 *
1333 * Managed regulator_get(). Regulators returned from this function are
1334 * automatically regulator_put() on driver detach. See regulator_get() for more
1335 * information.
1336 */
1337struct regulator *devm_regulator_get(struct device *dev, const char *id)
1338{
1339 struct regulator **ptr, *regulator;
1340
1341 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1342 if (!ptr)
1343 return ERR_PTR(-ENOMEM);
1344
1345 regulator = regulator_get(dev, id);
1346 if (!IS_ERR(regulator)) {
1347 *ptr = regulator;
1348 devres_add(dev, ptr);
1349 } else {
1350 devres_free(ptr);
1351 }
1352
1353 return regulator;
1354}
1355EXPORT_SYMBOL_GPL(devm_regulator_get);
1356
Liam Girdwood414c70c2008-04-30 15:59:04 +01001357/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001358 * regulator_get_exclusive - obtain exclusive access to a regulator.
1359 * @dev: device for regulator "consumer"
1360 * @id: Supply name or regulator ID.
1361 *
1362 * Returns a struct regulator corresponding to the regulator producer,
1363 * or IS_ERR() condition containing errno. Other consumers will be
1364 * unable to obtain this reference is held and the use count for the
1365 * regulator will be initialised to reflect the current state of the
1366 * regulator.
1367 *
1368 * This is intended for use by consumers which cannot tolerate shared
1369 * use of the regulator such as those which need to force the
1370 * regulator off for correct operation of the hardware they are
1371 * controlling.
1372 *
1373 * Use of supply names configured via regulator_set_device_supply() is
1374 * strongly encouraged. It is recommended that the supply name used
1375 * should match the name used for the supply and/or the relevant
1376 * device pins in the datasheet.
1377 */
1378struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1379{
1380 return _regulator_get(dev, id, 1);
1381}
1382EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1383
1384/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001385 * regulator_put - "free" the regulator source
1386 * @regulator: regulator source
1387 *
1388 * Note: drivers must ensure that all regulator_enable calls made on this
1389 * regulator source are balanced by regulator_disable calls prior to calling
1390 * this function.
1391 */
1392void regulator_put(struct regulator *regulator)
1393{
1394 struct regulator_dev *rdev;
1395
1396 if (regulator == NULL || IS_ERR(regulator))
1397 return;
1398
Liam Girdwood414c70c2008-04-30 15:59:04 +01001399 mutex_lock(&regulator_list_mutex);
1400 rdev = regulator->rdev;
1401
Mark Brown5de70512011-06-19 13:33:16 +01001402#ifdef CONFIG_DEBUG_FS
1403 debugfs_remove_recursive(regulator->debugfs);
1404#endif
1405
Liam Girdwood414c70c2008-04-30 15:59:04 +01001406 /* remove any sysfs entries */
1407 if (regulator->dev) {
1408 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001409 device_remove_file(regulator->dev, &regulator->dev_attr);
1410 kfree(regulator->dev_attr.attr.name);
1411 }
Mark Brown5de70512011-06-19 13:33:16 +01001412 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001413 list_del(&regulator->list);
1414 kfree(regulator);
1415
Mark Brown5ffbd132009-07-21 16:00:23 +01001416 rdev->open_count--;
1417 rdev->exclusive = 0;
1418
Liam Girdwood414c70c2008-04-30 15:59:04 +01001419 module_put(rdev->owner);
1420 mutex_unlock(&regulator_list_mutex);
1421}
1422EXPORT_SYMBOL_GPL(regulator_put);
1423
Mark Brown9a2372f2009-08-03 18:49:57 +01001424static int _regulator_can_change_status(struct regulator_dev *rdev)
1425{
1426 if (!rdev->constraints)
1427 return 0;
1428
1429 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1430 return 1;
1431 else
1432 return 0;
1433}
1434
Liam Girdwood414c70c2008-04-30 15:59:04 +01001435/* locks held by regulator_enable() */
1436static int _regulator_enable(struct regulator_dev *rdev)
1437{
Mark Brown31aae2b2009-12-21 12:21:52 +00001438 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001439
Liam Girdwood414c70c2008-04-30 15:59:04 +01001440 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001441 if (rdev->constraints &&
1442 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1443 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001444
Mark Brown9a2372f2009-08-03 18:49:57 +01001445 if (rdev->use_count == 0) {
1446 /* The regulator may on if it's not switchable or left on */
1447 ret = _regulator_is_enabled(rdev);
1448 if (ret == -EINVAL || ret == 0) {
1449 if (!_regulator_can_change_status(rdev))
1450 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001451
Mark Brown31aae2b2009-12-21 12:21:52 +00001452 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001453 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001454
1455 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001456 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001457 ret = _regulator_get_enable_time(rdev);
1458 if (ret >= 0) {
1459 delay = ret;
1460 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001461 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001462 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001463 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001464 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001465
Mark Brown02fa3ec2010-11-10 14:38:30 +00001466 trace_regulator_enable(rdev_get_name(rdev));
1467
Mark Brown31aae2b2009-12-21 12:21:52 +00001468 /* Allow the regulator to ramp; it would be useful
1469 * to extend this for bulk operations so that the
1470 * regulators can ramp together. */
1471 ret = rdev->desc->ops->enable(rdev);
1472 if (ret < 0)
1473 return ret;
1474
Mark Brown02fa3ec2010-11-10 14:38:30 +00001475 trace_regulator_enable_delay(rdev_get_name(rdev));
1476
Axel Line36c1df2010-11-05 21:51:32 +08001477 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001478 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001479 udelay(delay % 1000);
1480 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001481 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001482 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001483
Mark Brown02fa3ec2010-11-10 14:38:30 +00001484 trace_regulator_enable_complete(rdev_get_name(rdev));
1485
Linus Walleija7433cf2009-08-26 12:54:04 +02001486 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001487 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001488 return ret;
1489 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001490 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001491 }
1492
Mark Brown9a2372f2009-08-03 18:49:57 +01001493 rdev->use_count++;
1494
1495 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001496}
1497
1498/**
1499 * regulator_enable - enable regulator output
1500 * @regulator: regulator source
1501 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001502 * Request that the regulator be enabled with the regulator output at
1503 * the predefined voltage or current value. Calls to regulator_enable()
1504 * must be balanced with calls to regulator_disable().
1505 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001506 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001507 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001508 */
1509int regulator_enable(struct regulator *regulator)
1510{
David Brownell412aec62008-11-16 11:44:46 -08001511 struct regulator_dev *rdev = regulator->rdev;
1512 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001513
Mark Brown3801b862011-06-09 16:22:22 +01001514 if (rdev->supply) {
1515 ret = regulator_enable(rdev->supply);
1516 if (ret != 0)
1517 return ret;
1518 }
1519
David Brownell412aec62008-11-16 11:44:46 -08001520 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001521 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001522 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001523
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001524 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001525 regulator_disable(rdev->supply);
1526
Liam Girdwood414c70c2008-04-30 15:59:04 +01001527 return ret;
1528}
1529EXPORT_SYMBOL_GPL(regulator_enable);
1530
1531/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001532static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001533{
1534 int ret = 0;
1535
David Brownellcd94b502009-03-11 16:43:34 -08001536 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001537 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001538 return -EIO;
1539
Liam Girdwood414c70c2008-04-30 15:59:04 +01001540 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001541 if (rdev->use_count == 1 &&
1542 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001543
1544 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001545 if (_regulator_can_change_status(rdev) &&
1546 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001547 trace_regulator_disable(rdev_get_name(rdev));
1548
Liam Girdwood414c70c2008-04-30 15:59:04 +01001549 ret = rdev->desc->ops->disable(rdev);
1550 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001551 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001552 return ret;
1553 }
Mark Brown84b68262009-12-01 21:12:27 +00001554
Mark Brown02fa3ec2010-11-10 14:38:30 +00001555 trace_regulator_disable_complete(rdev_get_name(rdev));
1556
Mark Brown84b68262009-12-01 21:12:27 +00001557 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1558 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001559 }
1560
Liam Girdwood414c70c2008-04-30 15:59:04 +01001561 rdev->use_count = 0;
1562 } else if (rdev->use_count > 1) {
1563
1564 if (rdev->constraints &&
1565 (rdev->constraints->valid_ops_mask &
1566 REGULATOR_CHANGE_DRMS))
1567 drms_uA_update(rdev);
1568
1569 rdev->use_count--;
1570 }
Mark Brown3801b862011-06-09 16:22:22 +01001571
Liam Girdwood414c70c2008-04-30 15:59:04 +01001572 return ret;
1573}
1574
1575/**
1576 * regulator_disable - disable regulator output
1577 * @regulator: regulator source
1578 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001579 * Disable the regulator output voltage or current. Calls to
1580 * regulator_enable() must be balanced with calls to
1581 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001582 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001583 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001584 * devices have it enabled, the regulator device supports disabling and
1585 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001586 */
1587int regulator_disable(struct regulator *regulator)
1588{
David Brownell412aec62008-11-16 11:44:46 -08001589 struct regulator_dev *rdev = regulator->rdev;
1590 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001591
David Brownell412aec62008-11-16 11:44:46 -08001592 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001593 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001594 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001595
Mark Brown3801b862011-06-09 16:22:22 +01001596 if (ret == 0 && rdev->supply)
1597 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001598
Liam Girdwood414c70c2008-04-30 15:59:04 +01001599 return ret;
1600}
1601EXPORT_SYMBOL_GPL(regulator_disable);
1602
1603/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001604static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001605{
1606 int ret = 0;
1607
1608 /* force disable */
1609 if (rdev->desc->ops->disable) {
1610 /* ah well, who wants to live forever... */
1611 ret = rdev->desc->ops->disable(rdev);
1612 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001613 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001614 return ret;
1615 }
1616 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001617 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1618 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001619 }
1620
Liam Girdwood414c70c2008-04-30 15:59:04 +01001621 return ret;
1622}
1623
1624/**
1625 * regulator_force_disable - force disable regulator output
1626 * @regulator: regulator source
1627 *
1628 * Forcibly disable the regulator output voltage or current.
1629 * NOTE: this *will* disable the regulator output even if other consumer
1630 * devices have it enabled. This should be used for situations when device
1631 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1632 */
1633int regulator_force_disable(struct regulator *regulator)
1634{
Mark Brown82d15832011-05-09 11:41:02 +02001635 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001636 int ret;
1637
Mark Brown82d15832011-05-09 11:41:02 +02001638 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001639 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001640 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001641 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001642
Mark Brown3801b862011-06-09 16:22:22 +01001643 if (rdev->supply)
1644 while (rdev->open_count--)
1645 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001646
Liam Girdwood414c70c2008-04-30 15:59:04 +01001647 return ret;
1648}
1649EXPORT_SYMBOL_GPL(regulator_force_disable);
1650
Mark Brownda07ecd2011-09-11 09:53:50 +01001651static void regulator_disable_work(struct work_struct *work)
1652{
1653 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1654 disable_work.work);
1655 int count, i, ret;
1656
1657 mutex_lock(&rdev->mutex);
1658
1659 BUG_ON(!rdev->deferred_disables);
1660
1661 count = rdev->deferred_disables;
1662 rdev->deferred_disables = 0;
1663
1664 for (i = 0; i < count; i++) {
1665 ret = _regulator_disable(rdev);
1666 if (ret != 0)
1667 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1668 }
1669
1670 mutex_unlock(&rdev->mutex);
1671
1672 if (rdev->supply) {
1673 for (i = 0; i < count; i++) {
1674 ret = regulator_disable(rdev->supply);
1675 if (ret != 0) {
1676 rdev_err(rdev,
1677 "Supply disable failed: %d\n", ret);
1678 }
1679 }
1680 }
1681}
1682
1683/**
1684 * regulator_disable_deferred - disable regulator output with delay
1685 * @regulator: regulator source
1686 * @ms: miliseconds until the regulator is disabled
1687 *
1688 * Execute regulator_disable() on the regulator after a delay. This
1689 * is intended for use with devices that require some time to quiesce.
1690 *
1691 * NOTE: this will only disable the regulator output if no other consumer
1692 * devices have it enabled, the regulator device supports disabling and
1693 * machine constraints permit this operation.
1694 */
1695int regulator_disable_deferred(struct regulator *regulator, int ms)
1696{
1697 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001698 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001699
1700 mutex_lock(&rdev->mutex);
1701 rdev->deferred_disables++;
1702 mutex_unlock(&rdev->mutex);
1703
Mark Brownaa598022011-10-03 22:42:43 +01001704 ret = schedule_delayed_work(&rdev->disable_work,
1705 msecs_to_jiffies(ms));
1706 if (ret < 0)
1707 return ret;
1708 else
1709 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001710}
1711EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1712
Liam Girdwood414c70c2008-04-30 15:59:04 +01001713static int _regulator_is_enabled(struct regulator_dev *rdev)
1714{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001715 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001716 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001717 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001718
Mark Brown93325462009-08-03 18:49:56 +01001719 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001720}
1721
1722/**
1723 * regulator_is_enabled - is the regulator output enabled
1724 * @regulator: regulator source
1725 *
David Brownell412aec62008-11-16 11:44:46 -08001726 * Returns positive if the regulator driver backing the source/client
1727 * has requested that the device be enabled, zero if it hasn't, else a
1728 * negative errno code.
1729 *
1730 * Note that the device backing this regulator handle can have multiple
1731 * users, so it might be enabled even if regulator_enable() was never
1732 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001733 */
1734int regulator_is_enabled(struct regulator *regulator)
1735{
Mark Brown93325462009-08-03 18:49:56 +01001736 int ret;
1737
1738 mutex_lock(&regulator->rdev->mutex);
1739 ret = _regulator_is_enabled(regulator->rdev);
1740 mutex_unlock(&regulator->rdev->mutex);
1741
1742 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001743}
1744EXPORT_SYMBOL_GPL(regulator_is_enabled);
1745
1746/**
David Brownell4367cfd2009-02-26 11:48:36 -08001747 * regulator_count_voltages - count regulator_list_voltage() selectors
1748 * @regulator: regulator source
1749 *
1750 * Returns number of selectors, or negative errno. Selectors are
1751 * numbered starting at zero, and typically correspond to bitfields
1752 * in hardware registers.
1753 */
1754int regulator_count_voltages(struct regulator *regulator)
1755{
1756 struct regulator_dev *rdev = regulator->rdev;
1757
1758 return rdev->desc->n_voltages ? : -EINVAL;
1759}
1760EXPORT_SYMBOL_GPL(regulator_count_voltages);
1761
1762/**
1763 * regulator_list_voltage - enumerate supported voltages
1764 * @regulator: regulator source
1765 * @selector: identify voltage to list
1766 * Context: can sleep
1767 *
1768 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001769 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001770 * negative errno.
1771 */
1772int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1773{
1774 struct regulator_dev *rdev = regulator->rdev;
1775 struct regulator_ops *ops = rdev->desc->ops;
1776 int ret;
1777
1778 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1779 return -EINVAL;
1780
1781 mutex_lock(&rdev->mutex);
1782 ret = ops->list_voltage(rdev, selector);
1783 mutex_unlock(&rdev->mutex);
1784
1785 if (ret > 0) {
1786 if (ret < rdev->constraints->min_uV)
1787 ret = 0;
1788 else if (ret > rdev->constraints->max_uV)
1789 ret = 0;
1790 }
1791
1792 return ret;
1793}
1794EXPORT_SYMBOL_GPL(regulator_list_voltage);
1795
1796/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001797 * regulator_is_supported_voltage - check if a voltage range can be supported
1798 *
1799 * @regulator: Regulator to check.
1800 * @min_uV: Minimum required voltage in uV.
1801 * @max_uV: Maximum required voltage in uV.
1802 *
1803 * Returns a boolean or a negative error code.
1804 */
1805int regulator_is_supported_voltage(struct regulator *regulator,
1806 int min_uV, int max_uV)
1807{
1808 int i, voltages, ret;
1809
1810 ret = regulator_count_voltages(regulator);
1811 if (ret < 0)
1812 return ret;
1813 voltages = ret;
1814
1815 for (i = 0; i < voltages; i++) {
1816 ret = regulator_list_voltage(regulator, i);
1817
1818 if (ret >= min_uV && ret <= max_uV)
1819 return 1;
1820 }
1821
1822 return 0;
1823}
Mark Browna398eaa2011-12-28 12:48:45 +00001824EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01001825
Mark Brown75790252010-12-12 14:25:50 +00001826static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1827 int min_uV, int max_uV)
1828{
1829 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001830 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001831 unsigned int selector;
1832
1833 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1834
Mark Brownbf5892a2011-05-08 22:13:37 +01001835 min_uV += rdev->constraints->uV_offset;
1836 max_uV += rdev->constraints->uV_offset;
1837
Mark Brown75790252010-12-12 14:25:50 +00001838 if (rdev->desc->ops->set_voltage) {
1839 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1840 &selector);
1841
1842 if (rdev->desc->ops->list_voltage)
1843 selector = rdev->desc->ops->list_voltage(rdev,
1844 selector);
1845 else
1846 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001847 } else if (rdev->desc->ops->set_voltage_sel) {
1848 int best_val = INT_MAX;
1849 int i;
1850
1851 selector = 0;
1852
1853 /* Find the smallest voltage that falls within the specified
1854 * range.
1855 */
1856 for (i = 0; i < rdev->desc->n_voltages; i++) {
1857 ret = rdev->desc->ops->list_voltage(rdev, i);
1858 if (ret < 0)
1859 continue;
1860
1861 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1862 best_val = ret;
1863 selector = i;
1864 }
1865 }
1866
Linus Walleij77af1b2642011-03-17 13:24:36 +01001867 /*
1868 * If we can't obtain the old selector there is not enough
1869 * info to call set_voltage_time_sel().
1870 */
1871 if (rdev->desc->ops->set_voltage_time_sel &&
1872 rdev->desc->ops->get_voltage_sel) {
1873 unsigned int old_selector = 0;
1874
1875 ret = rdev->desc->ops->get_voltage_sel(rdev);
1876 if (ret < 0)
1877 return ret;
1878 old_selector = ret;
1879 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1880 old_selector, selector);
1881 }
1882
Mark Browne8eef822010-12-12 14:36:17 +00001883 if (best_val != INT_MAX) {
1884 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1885 selector = best_val;
1886 } else {
1887 ret = -EINVAL;
1888 }
Mark Brown75790252010-12-12 14:25:50 +00001889 } else {
1890 ret = -EINVAL;
1891 }
1892
Linus Walleij77af1b2642011-03-17 13:24:36 +01001893 /* Insert any necessary delays */
1894 if (delay >= 1000) {
1895 mdelay(delay / 1000);
1896 udelay(delay % 1000);
1897 } else if (delay) {
1898 udelay(delay);
1899 }
1900
Mark Brownded06a52010-12-16 13:59:10 +00001901 if (ret == 0)
1902 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1903 NULL);
1904
Mark Brown75790252010-12-12 14:25:50 +00001905 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1906
1907 return ret;
1908}
1909
Mark Browna7a1ad92009-07-21 16:00:24 +01001910/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001911 * regulator_set_voltage - set regulator output voltage
1912 * @regulator: regulator source
1913 * @min_uV: Minimum required voltage in uV
1914 * @max_uV: Maximum acceptable voltage in uV
1915 *
1916 * Sets a voltage regulator to the desired output voltage. This can be set
1917 * during any regulator state. IOW, regulator can be disabled or enabled.
1918 *
1919 * If the regulator is enabled then the voltage will change to the new value
1920 * immediately otherwise if the regulator is disabled the regulator will
1921 * output at the new voltage when enabled.
1922 *
1923 * NOTE: If the regulator is shared between several devices then the lowest
1924 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001925 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001926 * calling this function otherwise this call will fail.
1927 */
1928int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1929{
1930 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001931 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001932
1933 mutex_lock(&rdev->mutex);
1934
Mark Brown95a3c232010-12-16 15:49:37 +00001935 /* If we're setting the same range as last time the change
1936 * should be a noop (some cpufreq implementations use the same
1937 * voltage for multiple frequencies, for example).
1938 */
1939 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1940 goto out;
1941
Liam Girdwood414c70c2008-04-30 15:59:04 +01001942 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001943 if (!rdev->desc->ops->set_voltage &&
1944 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001945 ret = -EINVAL;
1946 goto out;
1947 }
1948
1949 /* constraints check */
1950 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1951 if (ret < 0)
1952 goto out;
1953 regulator->min_uV = min_uV;
1954 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001955
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001956 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1957 if (ret < 0)
1958 goto out;
1959
Mark Brown75790252010-12-12 14:25:50 +00001960 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001961
Liam Girdwood414c70c2008-04-30 15:59:04 +01001962out:
1963 mutex_unlock(&rdev->mutex);
1964 return ret;
1965}
1966EXPORT_SYMBOL_GPL(regulator_set_voltage);
1967
Mark Brown606a2562010-12-16 15:49:36 +00001968/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001969 * regulator_set_voltage_time - get raise/fall time
1970 * @regulator: regulator source
1971 * @old_uV: starting voltage in microvolts
1972 * @new_uV: target voltage in microvolts
1973 *
1974 * Provided with the starting and ending voltage, this function attempts to
1975 * calculate the time in microseconds required to rise or fall to this new
1976 * voltage.
1977 */
1978int regulator_set_voltage_time(struct regulator *regulator,
1979 int old_uV, int new_uV)
1980{
1981 struct regulator_dev *rdev = regulator->rdev;
1982 struct regulator_ops *ops = rdev->desc->ops;
1983 int old_sel = -1;
1984 int new_sel = -1;
1985 int voltage;
1986 int i;
1987
1988 /* Currently requires operations to do this */
1989 if (!ops->list_voltage || !ops->set_voltage_time_sel
1990 || !rdev->desc->n_voltages)
1991 return -EINVAL;
1992
1993 for (i = 0; i < rdev->desc->n_voltages; i++) {
1994 /* We only look for exact voltage matches here */
1995 voltage = regulator_list_voltage(regulator, i);
1996 if (voltage < 0)
1997 return -EINVAL;
1998 if (voltage == 0)
1999 continue;
2000 if (voltage == old_uV)
2001 old_sel = i;
2002 if (voltage == new_uV)
2003 new_sel = i;
2004 }
2005
2006 if (old_sel < 0 || new_sel < 0)
2007 return -EINVAL;
2008
2009 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2010}
2011EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2012
2013/**
Mark Brown606a2562010-12-16 15:49:36 +00002014 * regulator_sync_voltage - re-apply last regulator output voltage
2015 * @regulator: regulator source
2016 *
2017 * Re-apply the last configured voltage. This is intended to be used
2018 * where some external control source the consumer is cooperating with
2019 * has caused the configured voltage to change.
2020 */
2021int regulator_sync_voltage(struct regulator *regulator)
2022{
2023 struct regulator_dev *rdev = regulator->rdev;
2024 int ret, min_uV, max_uV;
2025
2026 mutex_lock(&rdev->mutex);
2027
2028 if (!rdev->desc->ops->set_voltage &&
2029 !rdev->desc->ops->set_voltage_sel) {
2030 ret = -EINVAL;
2031 goto out;
2032 }
2033
2034 /* This is only going to work if we've had a voltage configured. */
2035 if (!regulator->min_uV && !regulator->max_uV) {
2036 ret = -EINVAL;
2037 goto out;
2038 }
2039
2040 min_uV = regulator->min_uV;
2041 max_uV = regulator->max_uV;
2042
2043 /* This should be a paranoia check... */
2044 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2045 if (ret < 0)
2046 goto out;
2047
2048 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2049 if (ret < 0)
2050 goto out;
2051
2052 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2053
2054out:
2055 mutex_unlock(&rdev->mutex);
2056 return ret;
2057}
2058EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2059
Liam Girdwood414c70c2008-04-30 15:59:04 +01002060static int _regulator_get_voltage(struct regulator_dev *rdev)
2061{
Mark Brownbf5892a2011-05-08 22:13:37 +01002062 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002063
2064 if (rdev->desc->ops->get_voltage_sel) {
2065 sel = rdev->desc->ops->get_voltage_sel(rdev);
2066 if (sel < 0)
2067 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002068 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002069 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002070 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002071 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002072 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002073 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002074
Axel Lincb220d12011-05-23 20:08:10 +08002075 if (ret < 0)
2076 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002077 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002078}
2079
2080/**
2081 * regulator_get_voltage - get regulator output voltage
2082 * @regulator: regulator source
2083 *
2084 * This returns the current regulator voltage in uV.
2085 *
2086 * NOTE: If the regulator is disabled it will return the voltage value. This
2087 * function should not be used to determine regulator state.
2088 */
2089int regulator_get_voltage(struct regulator *regulator)
2090{
2091 int ret;
2092
2093 mutex_lock(&regulator->rdev->mutex);
2094
2095 ret = _regulator_get_voltage(regulator->rdev);
2096
2097 mutex_unlock(&regulator->rdev->mutex);
2098
2099 return ret;
2100}
2101EXPORT_SYMBOL_GPL(regulator_get_voltage);
2102
2103/**
2104 * regulator_set_current_limit - set regulator output current limit
2105 * @regulator: regulator source
2106 * @min_uA: Minimuum supported current in uA
2107 * @max_uA: Maximum supported current in uA
2108 *
2109 * Sets current sink to the desired output current. This can be set during
2110 * any regulator state. IOW, regulator can be disabled or enabled.
2111 *
2112 * If the regulator is enabled then the current will change to the new value
2113 * immediately otherwise if the regulator is disabled the regulator will
2114 * output at the new current when enabled.
2115 *
2116 * NOTE: Regulator system constraints must be set for this regulator before
2117 * calling this function otherwise this call will fail.
2118 */
2119int regulator_set_current_limit(struct regulator *regulator,
2120 int min_uA, int max_uA)
2121{
2122 struct regulator_dev *rdev = regulator->rdev;
2123 int ret;
2124
2125 mutex_lock(&rdev->mutex);
2126
2127 /* sanity check */
2128 if (!rdev->desc->ops->set_current_limit) {
2129 ret = -EINVAL;
2130 goto out;
2131 }
2132
2133 /* constraints check */
2134 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2135 if (ret < 0)
2136 goto out;
2137
2138 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2139out:
2140 mutex_unlock(&rdev->mutex);
2141 return ret;
2142}
2143EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2144
2145static int _regulator_get_current_limit(struct regulator_dev *rdev)
2146{
2147 int ret;
2148
2149 mutex_lock(&rdev->mutex);
2150
2151 /* sanity check */
2152 if (!rdev->desc->ops->get_current_limit) {
2153 ret = -EINVAL;
2154 goto out;
2155 }
2156
2157 ret = rdev->desc->ops->get_current_limit(rdev);
2158out:
2159 mutex_unlock(&rdev->mutex);
2160 return ret;
2161}
2162
2163/**
2164 * regulator_get_current_limit - get regulator output current
2165 * @regulator: regulator source
2166 *
2167 * This returns the current supplied by the specified current sink in uA.
2168 *
2169 * NOTE: If the regulator is disabled it will return the current value. This
2170 * function should not be used to determine regulator state.
2171 */
2172int regulator_get_current_limit(struct regulator *regulator)
2173{
2174 return _regulator_get_current_limit(regulator->rdev);
2175}
2176EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2177
2178/**
2179 * regulator_set_mode - set regulator operating mode
2180 * @regulator: regulator source
2181 * @mode: operating mode - one of the REGULATOR_MODE constants
2182 *
2183 * Set regulator operating mode to increase regulator efficiency or improve
2184 * regulation performance.
2185 *
2186 * NOTE: Regulator system constraints must be set for this regulator before
2187 * calling this function otherwise this call will fail.
2188 */
2189int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2190{
2191 struct regulator_dev *rdev = regulator->rdev;
2192 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302193 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002194
2195 mutex_lock(&rdev->mutex);
2196
2197 /* sanity check */
2198 if (!rdev->desc->ops->set_mode) {
2199 ret = -EINVAL;
2200 goto out;
2201 }
2202
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302203 /* return if the same mode is requested */
2204 if (rdev->desc->ops->get_mode) {
2205 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2206 if (regulator_curr_mode == mode) {
2207 ret = 0;
2208 goto out;
2209 }
2210 }
2211
Liam Girdwood414c70c2008-04-30 15:59:04 +01002212 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002213 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002214 if (ret < 0)
2215 goto out;
2216
2217 ret = rdev->desc->ops->set_mode(rdev, mode);
2218out:
2219 mutex_unlock(&rdev->mutex);
2220 return ret;
2221}
2222EXPORT_SYMBOL_GPL(regulator_set_mode);
2223
2224static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2225{
2226 int ret;
2227
2228 mutex_lock(&rdev->mutex);
2229
2230 /* sanity check */
2231 if (!rdev->desc->ops->get_mode) {
2232 ret = -EINVAL;
2233 goto out;
2234 }
2235
2236 ret = rdev->desc->ops->get_mode(rdev);
2237out:
2238 mutex_unlock(&rdev->mutex);
2239 return ret;
2240}
2241
2242/**
2243 * regulator_get_mode - get regulator operating mode
2244 * @regulator: regulator source
2245 *
2246 * Get the current regulator operating mode.
2247 */
2248unsigned int regulator_get_mode(struct regulator *regulator)
2249{
2250 return _regulator_get_mode(regulator->rdev);
2251}
2252EXPORT_SYMBOL_GPL(regulator_get_mode);
2253
2254/**
2255 * regulator_set_optimum_mode - set regulator optimum operating mode
2256 * @regulator: regulator source
2257 * @uA_load: load current
2258 *
2259 * Notifies the regulator core of a new device load. This is then used by
2260 * DRMS (if enabled by constraints) to set the most efficient regulator
2261 * operating mode for the new regulator loading.
2262 *
2263 * Consumer devices notify their supply regulator of the maximum power
2264 * they will require (can be taken from device datasheet in the power
2265 * consumption tables) when they change operational status and hence power
2266 * state. Examples of operational state changes that can affect power
2267 * consumption are :-
2268 *
2269 * o Device is opened / closed.
2270 * o Device I/O is about to begin or has just finished.
2271 * o Device is idling in between work.
2272 *
2273 * This information is also exported via sysfs to userspace.
2274 *
2275 * DRMS will sum the total requested load on the regulator and change
2276 * to the most efficient operating mode if platform constraints allow.
2277 *
2278 * Returns the new regulator mode or error.
2279 */
2280int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2281{
2282 struct regulator_dev *rdev = regulator->rdev;
2283 struct regulator *consumer;
2284 int ret, output_uV, input_uV, total_uA_load = 0;
2285 unsigned int mode;
2286
2287 mutex_lock(&rdev->mutex);
2288
Mark Browna4b41482011-05-14 11:19:45 -07002289 /*
2290 * first check to see if we can set modes at all, otherwise just
2291 * tell the consumer everything is OK.
2292 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002293 regulator->uA_load = uA_load;
2294 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002295 if (ret < 0) {
2296 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002297 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002298 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002299
Liam Girdwood414c70c2008-04-30 15:59:04 +01002300 if (!rdev->desc->ops->get_optimum_mode)
2301 goto out;
2302
Mark Browna4b41482011-05-14 11:19:45 -07002303 /*
2304 * we can actually do this so any errors are indicators of
2305 * potential real failure.
2306 */
2307 ret = -EINVAL;
2308
Liam Girdwood414c70c2008-04-30 15:59:04 +01002309 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002310 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002311 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002312 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002313 goto out;
2314 }
2315
2316 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002317 input_uV = 0;
2318 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002319 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002320 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002321 input_uV = rdev->constraints->input_uV;
2322 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002323 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002324 goto out;
2325 }
2326
2327 /* calc total requested load for this regulator */
2328 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002329 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002330
2331 mode = rdev->desc->ops->get_optimum_mode(rdev,
2332 input_uV, output_uV,
2333 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002334 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002335 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002336 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2337 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002338 goto out;
2339 }
2340
2341 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002342 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002343 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002344 goto out;
2345 }
2346 ret = mode;
2347out:
2348 mutex_unlock(&rdev->mutex);
2349 return ret;
2350}
2351EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2352
2353/**
2354 * regulator_register_notifier - register regulator event notifier
2355 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002356 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002357 *
2358 * Register notifier block to receive regulator events.
2359 */
2360int regulator_register_notifier(struct regulator *regulator,
2361 struct notifier_block *nb)
2362{
2363 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2364 nb);
2365}
2366EXPORT_SYMBOL_GPL(regulator_register_notifier);
2367
2368/**
2369 * regulator_unregister_notifier - unregister regulator event notifier
2370 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002371 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002372 *
2373 * Unregister regulator event notifier block.
2374 */
2375int regulator_unregister_notifier(struct regulator *regulator,
2376 struct notifier_block *nb)
2377{
2378 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2379 nb);
2380}
2381EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2382
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002383/* notify regulator consumers and downstream regulator consumers.
2384 * Note mutex must be held by caller.
2385 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002386static void _notifier_call_chain(struct regulator_dev *rdev,
2387 unsigned long event, void *data)
2388{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002389 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002390 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002391}
2392
2393/**
2394 * regulator_bulk_get - get multiple regulator consumers
2395 *
2396 * @dev: Device to supply
2397 * @num_consumers: Number of consumers to register
2398 * @consumers: Configuration of consumers; clients are stored here.
2399 *
2400 * @return 0 on success, an errno on failure.
2401 *
2402 * This helper function allows drivers to get several regulator
2403 * consumers in one operation. If any of the regulators cannot be
2404 * acquired then any regulators that were allocated will be freed
2405 * before returning to the caller.
2406 */
2407int regulator_bulk_get(struct device *dev, int num_consumers,
2408 struct regulator_bulk_data *consumers)
2409{
2410 int i;
2411 int ret;
2412
2413 for (i = 0; i < num_consumers; i++)
2414 consumers[i].consumer = NULL;
2415
2416 for (i = 0; i < num_consumers; i++) {
2417 consumers[i].consumer = regulator_get(dev,
2418 consumers[i].supply);
2419 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002420 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002421 dev_err(dev, "Failed to get supply '%s': %d\n",
2422 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002423 consumers[i].consumer = NULL;
2424 goto err;
2425 }
2426 }
2427
2428 return 0;
2429
2430err:
2431 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2432 regulator_put(consumers[i].consumer);
2433
2434 return ret;
2435}
2436EXPORT_SYMBOL_GPL(regulator_bulk_get);
2437
Mark Brownf21e0e82011-05-24 08:12:40 +08002438static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2439{
2440 struct regulator_bulk_data *bulk = data;
2441
2442 bulk->ret = regulator_enable(bulk->consumer);
2443}
2444
Liam Girdwood414c70c2008-04-30 15:59:04 +01002445/**
2446 * regulator_bulk_enable - enable multiple regulator consumers
2447 *
2448 * @num_consumers: Number of consumers
2449 * @consumers: Consumer data; clients are stored here.
2450 * @return 0 on success, an errno on failure
2451 *
2452 * This convenience API allows consumers to enable multiple regulator
2453 * clients in a single API call. If any consumers cannot be enabled
2454 * then any others that were enabled will be disabled again prior to
2455 * return.
2456 */
2457int regulator_bulk_enable(int num_consumers,
2458 struct regulator_bulk_data *consumers)
2459{
Mark Brownf21e0e82011-05-24 08:12:40 +08002460 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002461 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002462 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002463
Mark Brownf21e0e82011-05-24 08:12:40 +08002464 for (i = 0; i < num_consumers; i++)
2465 async_schedule_domain(regulator_bulk_enable_async,
2466 &consumers[i], &async_domain);
2467
2468 async_synchronize_full_domain(&async_domain);
2469
2470 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002471 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002472 if (consumers[i].ret != 0) {
2473 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002474 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002475 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002476 }
2477
2478 return 0;
2479
2480err:
Mark Brownf21e0e82011-05-24 08:12:40 +08002481 for (i = 0; i < num_consumers; i++)
2482 if (consumers[i].ret == 0)
2483 regulator_disable(consumers[i].consumer);
2484 else
2485 pr_err("Failed to enable %s: %d\n",
2486 consumers[i].supply, consumers[i].ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002487
2488 return ret;
2489}
2490EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2491
2492/**
2493 * regulator_bulk_disable - disable multiple regulator consumers
2494 *
2495 * @num_consumers: Number of consumers
2496 * @consumers: Consumer data; clients are stored here.
2497 * @return 0 on success, an errno on failure
2498 *
2499 * This convenience API allows consumers to disable multiple regulator
2500 * clients in a single API call. If any consumers cannot be enabled
2501 * then any others that were disabled will be disabled again prior to
2502 * return.
2503 */
2504int regulator_bulk_disable(int num_consumers,
2505 struct regulator_bulk_data *consumers)
2506{
2507 int i;
2508 int ret;
2509
2510 for (i = 0; i < num_consumers; i++) {
2511 ret = regulator_disable(consumers[i].consumer);
2512 if (ret != 0)
2513 goto err;
2514 }
2515
2516 return 0;
2517
2518err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002519 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002520 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002521 regulator_enable(consumers[i].consumer);
2522
2523 return ret;
2524}
2525EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2526
2527/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002528 * regulator_bulk_force_disable - force disable multiple regulator consumers
2529 *
2530 * @num_consumers: Number of consumers
2531 * @consumers: Consumer data; clients are stored here.
2532 * @return 0 on success, an errno on failure
2533 *
2534 * This convenience API allows consumers to forcibly disable multiple regulator
2535 * clients in a single API call.
2536 * NOTE: This should be used for situations when device damage will
2537 * likely occur if the regulators are not disabled (e.g. over temp).
2538 * Although regulator_force_disable function call for some consumers can
2539 * return error numbers, the function is called for all consumers.
2540 */
2541int regulator_bulk_force_disable(int num_consumers,
2542 struct regulator_bulk_data *consumers)
2543{
2544 int i;
2545 int ret;
2546
2547 for (i = 0; i < num_consumers; i++)
2548 consumers[i].ret =
2549 regulator_force_disable(consumers[i].consumer);
2550
2551 for (i = 0; i < num_consumers; i++) {
2552 if (consumers[i].ret != 0) {
2553 ret = consumers[i].ret;
2554 goto out;
2555 }
2556 }
2557
2558 return 0;
2559out:
2560 return ret;
2561}
2562EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2563
2564/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002565 * regulator_bulk_free - free multiple regulator consumers
2566 *
2567 * @num_consumers: Number of consumers
2568 * @consumers: Consumer data; clients are stored here.
2569 *
2570 * This convenience API allows consumers to free multiple regulator
2571 * clients in a single API call.
2572 */
2573void regulator_bulk_free(int num_consumers,
2574 struct regulator_bulk_data *consumers)
2575{
2576 int i;
2577
2578 for (i = 0; i < num_consumers; i++) {
2579 regulator_put(consumers[i].consumer);
2580 consumers[i].consumer = NULL;
2581 }
2582}
2583EXPORT_SYMBOL_GPL(regulator_bulk_free);
2584
2585/**
2586 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002587 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002588 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002589 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002590 *
2591 * Called by regulator drivers to notify clients a regulator event has
2592 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002593 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002594 */
2595int regulator_notifier_call_chain(struct regulator_dev *rdev,
2596 unsigned long event, void *data)
2597{
2598 _notifier_call_chain(rdev, event, data);
2599 return NOTIFY_DONE;
2600
2601}
2602EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2603
Mark Brownbe721972009-08-04 20:09:52 +02002604/**
2605 * regulator_mode_to_status - convert a regulator mode into a status
2606 *
2607 * @mode: Mode to convert
2608 *
2609 * Convert a regulator mode into a status.
2610 */
2611int regulator_mode_to_status(unsigned int mode)
2612{
2613 switch (mode) {
2614 case REGULATOR_MODE_FAST:
2615 return REGULATOR_STATUS_FAST;
2616 case REGULATOR_MODE_NORMAL:
2617 return REGULATOR_STATUS_NORMAL;
2618 case REGULATOR_MODE_IDLE:
2619 return REGULATOR_STATUS_IDLE;
2620 case REGULATOR_STATUS_STANDBY:
2621 return REGULATOR_STATUS_STANDBY;
2622 default:
2623 return 0;
2624 }
2625}
2626EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2627
David Brownell7ad68e22008-11-11 17:39:02 -08002628/*
2629 * To avoid cluttering sysfs (and memory) with useless state, only
2630 * create attributes that can be meaningfully displayed.
2631 */
2632static int add_regulator_attributes(struct regulator_dev *rdev)
2633{
2634 struct device *dev = &rdev->dev;
2635 struct regulator_ops *ops = rdev->desc->ops;
2636 int status = 0;
2637
2638 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002639 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2640 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002641 status = device_create_file(dev, &dev_attr_microvolts);
2642 if (status < 0)
2643 return status;
2644 }
2645 if (ops->get_current_limit) {
2646 status = device_create_file(dev, &dev_attr_microamps);
2647 if (status < 0)
2648 return status;
2649 }
2650 if (ops->get_mode) {
2651 status = device_create_file(dev, &dev_attr_opmode);
2652 if (status < 0)
2653 return status;
2654 }
2655 if (ops->is_enabled) {
2656 status = device_create_file(dev, &dev_attr_state);
2657 if (status < 0)
2658 return status;
2659 }
David Brownell853116a2009-01-14 23:03:17 -08002660 if (ops->get_status) {
2661 status = device_create_file(dev, &dev_attr_status);
2662 if (status < 0)
2663 return status;
2664 }
David Brownell7ad68e22008-11-11 17:39:02 -08002665
2666 /* some attributes are type-specific */
2667 if (rdev->desc->type == REGULATOR_CURRENT) {
2668 status = device_create_file(dev, &dev_attr_requested_microamps);
2669 if (status < 0)
2670 return status;
2671 }
2672
2673 /* all the other attributes exist to support constraints;
2674 * don't show them if there are no constraints, or if the
2675 * relevant supporting methods are missing.
2676 */
2677 if (!rdev->constraints)
2678 return status;
2679
2680 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002681 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002682 status = device_create_file(dev, &dev_attr_min_microvolts);
2683 if (status < 0)
2684 return status;
2685 status = device_create_file(dev, &dev_attr_max_microvolts);
2686 if (status < 0)
2687 return status;
2688 }
2689 if (ops->set_current_limit) {
2690 status = device_create_file(dev, &dev_attr_min_microamps);
2691 if (status < 0)
2692 return status;
2693 status = device_create_file(dev, &dev_attr_max_microamps);
2694 if (status < 0)
2695 return status;
2696 }
2697
2698 /* suspend mode constraints need multiple supporting methods */
2699 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2700 return status;
2701
2702 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2703 if (status < 0)
2704 return status;
2705 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2706 if (status < 0)
2707 return status;
2708 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2709 if (status < 0)
2710 return status;
2711
2712 if (ops->set_suspend_voltage) {
2713 status = device_create_file(dev,
2714 &dev_attr_suspend_standby_microvolts);
2715 if (status < 0)
2716 return status;
2717 status = device_create_file(dev,
2718 &dev_attr_suspend_mem_microvolts);
2719 if (status < 0)
2720 return status;
2721 status = device_create_file(dev,
2722 &dev_attr_suspend_disk_microvolts);
2723 if (status < 0)
2724 return status;
2725 }
2726
2727 if (ops->set_suspend_mode) {
2728 status = device_create_file(dev,
2729 &dev_attr_suspend_standby_mode);
2730 if (status < 0)
2731 return status;
2732 status = device_create_file(dev,
2733 &dev_attr_suspend_mem_mode);
2734 if (status < 0)
2735 return status;
2736 status = device_create_file(dev,
2737 &dev_attr_suspend_disk_mode);
2738 if (status < 0)
2739 return status;
2740 }
2741
2742 return status;
2743}
2744
Mark Brown1130e5b2010-12-21 23:49:31 +00002745static void rdev_init_debugfs(struct regulator_dev *rdev)
2746{
2747#ifdef CONFIG_DEBUG_FS
2748 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2749 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2750 rdev_warn(rdev, "Failed to create debugfs directory\n");
2751 rdev->debugfs = NULL;
2752 return;
2753 }
2754
2755 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2756 &rdev->use_count);
2757 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2758 &rdev->open_count);
2759#endif
2760}
2761
Liam Girdwood414c70c2008-04-30 15:59:04 +01002762/**
2763 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002764 * @regulator_desc: regulator to register
2765 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002766 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002767 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002768 *
2769 * Called by regulator drivers to register a regulator.
2770 * Returns 0 on success.
2771 */
2772struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002773 struct device *dev, const struct regulator_init_data *init_data,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302774 void *driver_data, struct device_node *of_node)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002775{
Mark Brown9a8f5e02011-11-29 18:11:19 +00002776 const struct regulation_constraints *constraints = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002777 static atomic_t regulator_no = ATOMIC_INIT(0);
2778 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002779 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05302780 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002781
2782 if (regulator_desc == NULL)
2783 return ERR_PTR(-EINVAL);
2784
2785 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2786 return ERR_PTR(-EINVAL);
2787
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002788 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2789 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002790 return ERR_PTR(-EINVAL);
2791
Mark Brown476c2d82010-12-10 17:28:07 +00002792 /* Only one of each should be implemented */
2793 WARN_ON(regulator_desc->ops->get_voltage &&
2794 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002795 WARN_ON(regulator_desc->ops->set_voltage &&
2796 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002797
2798 /* If we're using selectors we must implement list_voltage. */
2799 if (regulator_desc->ops->get_voltage_sel &&
2800 !regulator_desc->ops->list_voltage) {
2801 return ERR_PTR(-EINVAL);
2802 }
Mark Browne8eef822010-12-12 14:36:17 +00002803 if (regulator_desc->ops->set_voltage_sel &&
2804 !regulator_desc->ops->list_voltage) {
2805 return ERR_PTR(-EINVAL);
2806 }
Mark Brown476c2d82010-12-10 17:28:07 +00002807
Liam Girdwood414c70c2008-04-30 15:59:04 +01002808 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2809 if (rdev == NULL)
2810 return ERR_PTR(-ENOMEM);
2811
2812 mutex_lock(&regulator_list_mutex);
2813
2814 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002815 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002816 rdev->owner = regulator_desc->owner;
2817 rdev->desc = regulator_desc;
2818 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002819 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002820 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01002821 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002822
Liam Girdwooda5766f12008-10-10 13:22:20 +01002823 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002824 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01002825 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002826 if (ret < 0)
2827 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002828 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002829
Liam Girdwooda5766f12008-10-10 13:22:20 +01002830 /* register with sysfs */
2831 rdev->dev.class = &regulator_class;
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302832 rdev->dev.of_node = of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002833 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002834 dev_set_name(&rdev->dev, "regulator.%d",
2835 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002836 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002837 if (ret != 0) {
2838 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002839 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002840 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002841
2842 dev_set_drvdata(&rdev->dev, rdev);
2843
Mike Rapoport74f544c2008-11-25 14:53:53 +02002844 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002845 if (init_data)
2846 constraints = &init_data->constraints;
2847
2848 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02002849 if (ret < 0)
2850 goto scrub;
2851
David Brownell7ad68e22008-11-11 17:39:02 -08002852 /* add attributes supported by this regulator */
2853 ret = add_regulator_attributes(rdev);
2854 if (ret < 0)
2855 goto scrub;
2856
Mark Brown9a8f5e02011-11-29 18:11:19 +00002857 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05302858 supply = init_data->supply_regulator;
2859 else if (regulator_desc->supply_name)
2860 supply = regulator_desc->supply_name;
2861
2862 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002863 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01002864
Rajendra Nayak69511a42011-11-18 16:47:20 +05302865 r = regulator_dev_lookup(dev, supply);
Mark Brown0178f3e2010-04-26 15:18:14 +01002866
Rajendra Nayak69511a42011-11-18 16:47:20 +05302867 if (!r) {
2868 dev_err(dev, "Failed to find supply %s\n", supply);
Axel Lin7727da22010-11-05 15:27:17 +08002869 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002870 goto scrub;
2871 }
2872
2873 ret = set_supply(rdev, r);
2874 if (ret < 0)
2875 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05302876
2877 /* Enable supply if rail is enabled */
2878 if (rdev->desc->ops->is_enabled &&
2879 rdev->desc->ops->is_enabled(rdev)) {
2880 ret = regulator_enable(rdev->supply);
2881 if (ret < 0)
2882 goto scrub;
2883 }
Mark Brown0178f3e2010-04-26 15:18:14 +01002884 }
2885
Liam Girdwooda5766f12008-10-10 13:22:20 +01002886 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002887 if (init_data) {
2888 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2889 ret = set_consumer_device_supply(rdev,
2890 init_data->consumer_supplies[i].dev,
2891 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00002892 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00002893 if (ret < 0) {
2894 dev_err(dev, "Failed to set supply %s\n",
2895 init_data->consumer_supplies[i].supply);
2896 goto unset_supplies;
2897 }
Mark Brown23c2f042011-02-24 17:39:09 +00002898 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002899 }
2900
2901 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002902
2903 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002904out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002905 mutex_unlock(&regulator_list_mutex);
2906 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002907
Jani Nikulad4033b52010-04-29 10:55:11 +03002908unset_supplies:
2909 unset_regulator_supplies(rdev);
2910
David Brownell4fca9542008-11-11 17:38:53 -08002911scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08002912 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08002913 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002914 /* device core frees rdev */
2915 rdev = ERR_PTR(ret);
2916 goto out;
2917
David Brownell4fca9542008-11-11 17:38:53 -08002918clean:
2919 kfree(rdev);
2920 rdev = ERR_PTR(ret);
2921 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002922}
2923EXPORT_SYMBOL_GPL(regulator_register);
2924
2925/**
2926 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002927 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002928 *
2929 * Called by regulator drivers to unregister a regulator.
2930 */
2931void regulator_unregister(struct regulator_dev *rdev)
2932{
2933 if (rdev == NULL)
2934 return;
2935
2936 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00002937#ifdef CONFIG_DEBUG_FS
2938 debugfs_remove_recursive(rdev->debugfs);
2939#endif
Mark Brownda07ecd2011-09-11 09:53:50 +01002940 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01002941 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002942 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002943 list_del(&rdev->list);
2944 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002945 regulator_put(rdev->supply);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002946 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01002947 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002948 mutex_unlock(&regulator_list_mutex);
2949}
2950EXPORT_SYMBOL_GPL(regulator_unregister);
2951
2952/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002953 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002954 * @state: system suspend state
2955 *
2956 * Configure each regulator with it's suspend operating parameters for state.
2957 * This will usually be called by machine suspend code prior to supending.
2958 */
2959int regulator_suspend_prepare(suspend_state_t state)
2960{
2961 struct regulator_dev *rdev;
2962 int ret = 0;
2963
2964 /* ON is handled by regulator active state */
2965 if (state == PM_SUSPEND_ON)
2966 return -EINVAL;
2967
2968 mutex_lock(&regulator_list_mutex);
2969 list_for_each_entry(rdev, &regulator_list, list) {
2970
2971 mutex_lock(&rdev->mutex);
2972 ret = suspend_prepare(rdev, state);
2973 mutex_unlock(&rdev->mutex);
2974
2975 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002976 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002977 goto out;
2978 }
2979 }
2980out:
2981 mutex_unlock(&regulator_list_mutex);
2982 return ret;
2983}
2984EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2985
2986/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09002987 * regulator_suspend_finish - resume regulators from system wide suspend
2988 *
2989 * Turn on regulators that might be turned off by regulator_suspend_prepare
2990 * and that should be turned on according to the regulators properties.
2991 */
2992int regulator_suspend_finish(void)
2993{
2994 struct regulator_dev *rdev;
2995 int ret = 0, error;
2996
2997 mutex_lock(&regulator_list_mutex);
2998 list_for_each_entry(rdev, &regulator_list, list) {
2999 struct regulator_ops *ops = rdev->desc->ops;
3000
3001 mutex_lock(&rdev->mutex);
3002 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3003 ops->enable) {
3004 error = ops->enable(rdev);
3005 if (error)
3006 ret = error;
3007 } else {
3008 if (!has_full_constraints)
3009 goto unlock;
3010 if (!ops->disable)
3011 goto unlock;
3012 if (ops->is_enabled && !ops->is_enabled(rdev))
3013 goto unlock;
3014
3015 error = ops->disable(rdev);
3016 if (error)
3017 ret = error;
3018 }
3019unlock:
3020 mutex_unlock(&rdev->mutex);
3021 }
3022 mutex_unlock(&regulator_list_mutex);
3023 return ret;
3024}
3025EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3026
3027/**
Mark Brownca725562009-03-16 19:36:34 +00003028 * regulator_has_full_constraints - the system has fully specified constraints
3029 *
3030 * Calling this function will cause the regulator API to disable all
3031 * regulators which have a zero use count and don't have an always_on
3032 * constraint in a late_initcall.
3033 *
3034 * The intention is that this will become the default behaviour in a
3035 * future kernel release so users are encouraged to use this facility
3036 * now.
3037 */
3038void regulator_has_full_constraints(void)
3039{
3040 has_full_constraints = 1;
3041}
3042EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3043
3044/**
Mark Brown688fe992010-10-05 19:18:32 -07003045 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3046 *
3047 * Calling this function will cause the regulator API to provide a
3048 * dummy regulator to consumers if no physical regulator is found,
3049 * allowing most consumers to proceed as though a regulator were
3050 * configured. This allows systems such as those with software
3051 * controllable regulators for the CPU core only to be brought up more
3052 * readily.
3053 */
3054void regulator_use_dummy_regulator(void)
3055{
3056 board_wants_dummy_regulator = true;
3057}
3058EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3059
3060/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003061 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003062 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003063 *
3064 * Get rdev regulator driver private data. This call can be used in the
3065 * regulator driver context.
3066 */
3067void *rdev_get_drvdata(struct regulator_dev *rdev)
3068{
3069 return rdev->reg_data;
3070}
3071EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3072
3073/**
3074 * regulator_get_drvdata - get regulator driver data
3075 * @regulator: regulator
3076 *
3077 * Get regulator driver private data. This call can be used in the consumer
3078 * driver context when non API regulator specific functions need to be called.
3079 */
3080void *regulator_get_drvdata(struct regulator *regulator)
3081{
3082 return regulator->rdev->reg_data;
3083}
3084EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3085
3086/**
3087 * regulator_set_drvdata - set regulator driver data
3088 * @regulator: regulator
3089 * @data: data
3090 */
3091void regulator_set_drvdata(struct regulator *regulator, void *data)
3092{
3093 regulator->rdev->reg_data = data;
3094}
3095EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3096
3097/**
3098 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003099 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003100 */
3101int rdev_get_id(struct regulator_dev *rdev)
3102{
3103 return rdev->desc->id;
3104}
3105EXPORT_SYMBOL_GPL(rdev_get_id);
3106
Liam Girdwooda5766f12008-10-10 13:22:20 +01003107struct device *rdev_get_dev(struct regulator_dev *rdev)
3108{
3109 return &rdev->dev;
3110}
3111EXPORT_SYMBOL_GPL(rdev_get_dev);
3112
3113void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3114{
3115 return reg_init_data->driver_data;
3116}
3117EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3118
Mark Brownba55a972011-08-23 17:39:10 +01003119#ifdef CONFIG_DEBUG_FS
3120static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3121 size_t count, loff_t *ppos)
3122{
3123 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3124 ssize_t len, ret = 0;
3125 struct regulator_map *map;
3126
3127 if (!buf)
3128 return -ENOMEM;
3129
3130 list_for_each_entry(map, &regulator_map_list, list) {
3131 len = snprintf(buf + ret, PAGE_SIZE - ret,
3132 "%s -> %s.%s\n",
3133 rdev_get_name(map->regulator), map->dev_name,
3134 map->supply);
3135 if (len >= 0)
3136 ret += len;
3137 if (ret > PAGE_SIZE) {
3138 ret = PAGE_SIZE;
3139 break;
3140 }
3141 }
3142
3143 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3144
3145 kfree(buf);
3146
3147 return ret;
3148}
3149
3150static const struct file_operations supply_map_fops = {
3151 .read = supply_map_read_file,
3152 .llseek = default_llseek,
3153};
3154#endif
3155
Liam Girdwood414c70c2008-04-30 15:59:04 +01003156static int __init regulator_init(void)
3157{
Mark Brown34abbd62010-02-12 10:18:08 +00003158 int ret;
3159
Mark Brown34abbd62010-02-12 10:18:08 +00003160 ret = class_register(&regulator_class);
3161
Mark Brown1130e5b2010-12-21 23:49:31 +00003162#ifdef CONFIG_DEBUG_FS
3163 debugfs_root = debugfs_create_dir("regulator", NULL);
3164 if (IS_ERR(debugfs_root) || !debugfs_root) {
3165 pr_warn("regulator: Failed to create debugfs directory\n");
3166 debugfs_root = NULL;
3167 }
Mark Brownba55a972011-08-23 17:39:10 +01003168
3169 if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root,
3170 NULL, &supply_map_fops)))
3171 pr_warn("regulator: Failed to create supplies debugfs\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003172#endif
3173
Mark Brown34abbd62010-02-12 10:18:08 +00003174 regulator_dummy_init();
3175
3176 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003177}
3178
3179/* init early to allow our consumers to complete system booting */
3180core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003181
3182static int __init regulator_init_complete(void)
3183{
3184 struct regulator_dev *rdev;
3185 struct regulator_ops *ops;
3186 struct regulation_constraints *c;
3187 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003188
3189 mutex_lock(&regulator_list_mutex);
3190
3191 /* If we have a full configuration then disable any regulators
3192 * which are not in use or always_on. This will become the
3193 * default behaviour in the future.
3194 */
3195 list_for_each_entry(rdev, &regulator_list, list) {
3196 ops = rdev->desc->ops;
3197 c = rdev->constraints;
3198
Mark Brownf25e0b42009-08-03 18:49:55 +01003199 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003200 continue;
3201
3202 mutex_lock(&rdev->mutex);
3203
3204 if (rdev->use_count)
3205 goto unlock;
3206
3207 /* If we can't read the status assume it's on. */
3208 if (ops->is_enabled)
3209 enabled = ops->is_enabled(rdev);
3210 else
3211 enabled = 1;
3212
3213 if (!enabled)
3214 goto unlock;
3215
3216 if (has_full_constraints) {
3217 /* We log since this may kill the system if it
3218 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003219 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003220 ret = ops->disable(rdev);
3221 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003222 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003223 }
3224 } else {
3225 /* The intention is that in future we will
3226 * assume that full constraints are provided
3227 * so warn even if we aren't going to do
3228 * anything here.
3229 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003230 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003231 }
3232
3233unlock:
3234 mutex_unlock(&rdev->mutex);
3235 }
3236
3237 mutex_unlock(&regulator_list_mutex);
3238
3239 return 0;
3240}
3241late_initcall(regulator_init_complete);