blob: 5c0c975e5ac2f5a0bb177abca8e39b4d06195130 [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053026#include <linux/of.h>
27#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010028#include <linux/regulator/consumer.h>
29#include <linux/regulator/driver.h>
30#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040031#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010032
Mark Brown02fa3ec2010-11-10 14:38:30 +000033#define CREATE_TRACE_POINTS
34#include <trace/events/regulator.h>
35
Mark Brown34abbd62010-02-12 10:18:08 +000036#include "dummy.h"
37
Mark Brown7d51a0d2011-06-09 16:06:37 +010038#define rdev_crit(rdev, fmt, ...) \
39 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080040#define rdev_err(rdev, fmt, ...) \
41 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42#define rdev_warn(rdev, fmt, ...) \
43 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_info(rdev, fmt, ...) \
45 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_dbg(rdev, fmt, ...) \
47 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48
Liam Girdwood414c70c2008-04-30 15:59:04 +010049static DEFINE_MUTEX(regulator_list_mutex);
50static LIST_HEAD(regulator_list);
51static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000052static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070053static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010054
Mark Brown1130e5b2010-12-21 23:49:31 +000055static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000056
Mark Brown8dc53902008-12-31 12:52:40 +000057/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010058 * struct regulator_map
59 *
60 * Used to provide symbolic supply names to devices.
61 */
62struct regulator_map {
63 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010064 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010065 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010066 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010067};
68
Liam Girdwood414c70c2008-04-30 15:59:04 +010069/*
70 * struct regulator
71 *
72 * One for each consumer device.
73 */
74struct regulator {
75 struct device *dev;
76 struct list_head list;
77 int uA_load;
78 int min_uV;
79 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010080 char *supply_name;
81 struct device_attribute dev_attr;
82 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010083 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010084};
85
86static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010087static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010088static int _regulator_get_voltage(struct regulator_dev *rdev);
89static int _regulator_get_current_limit(struct regulator_dev *rdev);
90static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
91static void _notifier_call_chain(struct regulator_dev *rdev,
92 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000093static int _regulator_do_set_voltage(struct regulator_dev *rdev,
94 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010095static struct regulator *create_regulator(struct regulator_dev *rdev,
96 struct device *dev,
97 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +010098
Mark Brown1083c392009-10-22 16:31:32 +010099static const char *rdev_get_name(struct regulator_dev *rdev)
100{
101 if (rdev->constraints && rdev->constraints->name)
102 return rdev->constraints->name;
103 else if (rdev->desc->name)
104 return rdev->desc->name;
105 else
106 return "";
107}
108
Liam Girdwood414c70c2008-04-30 15:59:04 +0100109/* gets the regulator for a given consumer device */
110static struct regulator *get_device_regulator(struct device *dev)
111{
112 struct regulator *regulator = NULL;
113 struct regulator_dev *rdev;
114
115 mutex_lock(&regulator_list_mutex);
116 list_for_each_entry(rdev, &regulator_list, list) {
117 mutex_lock(&rdev->mutex);
118 list_for_each_entry(regulator, &rdev->consumer_list, list) {
119 if (regulator->dev == dev) {
120 mutex_unlock(&rdev->mutex);
121 mutex_unlock(&regulator_list_mutex);
122 return regulator;
123 }
124 }
125 mutex_unlock(&rdev->mutex);
126 }
127 mutex_unlock(&regulator_list_mutex);
128 return NULL;
129}
130
Rajendra Nayak69511a42011-11-18 16:47:20 +0530131/**
132 * of_get_regulator - get a regulator device node based on supply name
133 * @dev: Device pointer for the consumer (of regulator) device
134 * @supply: regulator supply name
135 *
136 * Extract the regulator device node corresponding to the supply name.
137 * retruns the device node corresponding to the regulator if found, else
138 * returns NULL.
139 */
140static struct device_node *of_get_regulator(struct device *dev, const char *supply)
141{
142 struct device_node *regnode = NULL;
143 char prop_name[32]; /* 32 is max size of property name */
144
145 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
146
147 snprintf(prop_name, 32, "%s-supply", supply);
148 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
149
150 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530151 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530152 prop_name, dev->of_node->full_name);
153 return NULL;
154 }
155 return regnode;
156}
157
Liam Girdwood414c70c2008-04-30 15:59:04 +0100158/* Platform voltage constraint check */
159static int regulator_check_voltage(struct regulator_dev *rdev,
160 int *min_uV, int *max_uV)
161{
162 BUG_ON(*min_uV > *max_uV);
163
164 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800165 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100166 return -ENODEV;
167 }
168 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800169 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100170 return -EPERM;
171 }
172
173 if (*max_uV > rdev->constraints->max_uV)
174 *max_uV = rdev->constraints->max_uV;
175 if (*min_uV < rdev->constraints->min_uV)
176 *min_uV = rdev->constraints->min_uV;
177
Mark Brown89f425e2011-07-12 11:20:37 +0900178 if (*min_uV > *max_uV) {
179 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100180 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100181 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900182 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100183
184 return 0;
185}
186
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100187/* Make sure we select a voltage that suits the needs of all
188 * regulator consumers
189 */
190static int regulator_check_consumers(struct regulator_dev *rdev,
191 int *min_uV, int *max_uV)
192{
193 struct regulator *regulator;
194
195 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700196 /*
197 * Assume consumers that didn't say anything are OK
198 * with anything in the constraint range.
199 */
200 if (!regulator->min_uV && !regulator->max_uV)
201 continue;
202
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100203 if (*max_uV > regulator->max_uV)
204 *max_uV = regulator->max_uV;
205 if (*min_uV < regulator->min_uV)
206 *min_uV = regulator->min_uV;
207 }
208
209 if (*min_uV > *max_uV)
210 return -EINVAL;
211
212 return 0;
213}
214
Liam Girdwood414c70c2008-04-30 15:59:04 +0100215/* current constraint check */
216static int regulator_check_current_limit(struct regulator_dev *rdev,
217 int *min_uA, int *max_uA)
218{
219 BUG_ON(*min_uA > *max_uA);
220
221 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800222 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100223 return -ENODEV;
224 }
225 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800226 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100227 return -EPERM;
228 }
229
230 if (*max_uA > rdev->constraints->max_uA)
231 *max_uA = rdev->constraints->max_uA;
232 if (*min_uA < rdev->constraints->min_uA)
233 *min_uA = rdev->constraints->min_uA;
234
Mark Brown89f425e2011-07-12 11:20:37 +0900235 if (*min_uA > *max_uA) {
236 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100237 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900239 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100240
241 return 0;
242}
243
244/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900245static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100246{
Mark Brown2c608232011-03-30 06:29:12 +0900247 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800248 case REGULATOR_MODE_FAST:
249 case REGULATOR_MODE_NORMAL:
250 case REGULATOR_MODE_IDLE:
251 case REGULATOR_MODE_STANDBY:
252 break;
253 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900254 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800255 return -EINVAL;
256 }
257
Liam Girdwood414c70c2008-04-30 15:59:04 +0100258 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800259 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100260 return -ENODEV;
261 }
262 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800263 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100264 return -EPERM;
265 }
Mark Brown2c608232011-03-30 06:29:12 +0900266
267 /* The modes are bitmasks, the most power hungry modes having
268 * the lowest values. If the requested mode isn't supported
269 * try higher modes. */
270 while (*mode) {
271 if (rdev->constraints->valid_modes_mask & *mode)
272 return 0;
273 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100274 }
Mark Brown2c608232011-03-30 06:29:12 +0900275
276 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100277}
278
279/* dynamic regulator mode switching constraint check */
280static int regulator_check_drms(struct regulator_dev *rdev)
281{
282 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800283 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284 return -ENODEV;
285 }
286 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800287 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100288 return -EPERM;
289 }
290 return 0;
291}
292
293static ssize_t device_requested_uA_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
295{
296 struct regulator *regulator;
297
298 regulator = get_device_regulator(dev);
299 if (regulator == NULL)
300 return 0;
301
302 return sprintf(buf, "%d\n", regulator->uA_load);
303}
304
305static ssize_t regulator_uV_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100308 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100309 ssize_t ret;
310
311 mutex_lock(&rdev->mutex);
312 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
313 mutex_unlock(&rdev->mutex);
314
315 return ret;
316}
David Brownell7ad68e22008-11-11 17:39:02 -0800317static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100318
319static ssize_t regulator_uA_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
321{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100322 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100323
324 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
325}
David Brownell7ad68e22008-11-11 17:39:02 -0800326static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100327
Mark Brownbc558a62008-10-10 15:33:20 +0100328static ssize_t regulator_name_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
330{
331 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100332
Mark Brown1083c392009-10-22 16:31:32 +0100333 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100334}
335
David Brownell4fca9542008-11-11 17:38:53 -0800336static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100337{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100338 switch (mode) {
339 case REGULATOR_MODE_FAST:
340 return sprintf(buf, "fast\n");
341 case REGULATOR_MODE_NORMAL:
342 return sprintf(buf, "normal\n");
343 case REGULATOR_MODE_IDLE:
344 return sprintf(buf, "idle\n");
345 case REGULATOR_MODE_STANDBY:
346 return sprintf(buf, "standby\n");
347 }
348 return sprintf(buf, "unknown\n");
349}
350
David Brownell4fca9542008-11-11 17:38:53 -0800351static ssize_t regulator_opmode_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100353{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100354 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100355
David Brownell4fca9542008-11-11 17:38:53 -0800356 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
357}
David Brownell7ad68e22008-11-11 17:39:02 -0800358static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800359
360static ssize_t regulator_print_state(char *buf, int state)
361{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100362 if (state > 0)
363 return sprintf(buf, "enabled\n");
364 else if (state == 0)
365 return sprintf(buf, "disabled\n");
366 else
367 return sprintf(buf, "unknown\n");
368}
369
David Brownell4fca9542008-11-11 17:38:53 -0800370static ssize_t regulator_state_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
372{
373 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100374 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800375
Mark Brown93325462009-08-03 18:49:56 +0100376 mutex_lock(&rdev->mutex);
377 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
378 mutex_unlock(&rdev->mutex);
379
380 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800381}
David Brownell7ad68e22008-11-11 17:39:02 -0800382static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800383
David Brownell853116a2009-01-14 23:03:17 -0800384static ssize_t regulator_status_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
386{
387 struct regulator_dev *rdev = dev_get_drvdata(dev);
388 int status;
389 char *label;
390
391 status = rdev->desc->ops->get_status(rdev);
392 if (status < 0)
393 return status;
394
395 switch (status) {
396 case REGULATOR_STATUS_OFF:
397 label = "off";
398 break;
399 case REGULATOR_STATUS_ON:
400 label = "on";
401 break;
402 case REGULATOR_STATUS_ERROR:
403 label = "error";
404 break;
405 case REGULATOR_STATUS_FAST:
406 label = "fast";
407 break;
408 case REGULATOR_STATUS_NORMAL:
409 label = "normal";
410 break;
411 case REGULATOR_STATUS_IDLE:
412 label = "idle";
413 break;
414 case REGULATOR_STATUS_STANDBY:
415 label = "standby";
416 break;
417 default:
418 return -ERANGE;
419 }
420
421 return sprintf(buf, "%s\n", label);
422}
423static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
424
Liam Girdwood414c70c2008-04-30 15:59:04 +0100425static ssize_t regulator_min_uA_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100428 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100429
430 if (!rdev->constraints)
431 return sprintf(buf, "constraint not defined\n");
432
433 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
434}
David Brownell7ad68e22008-11-11 17:39:02 -0800435static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100436
437static ssize_t regulator_max_uA_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
439{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100440 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100441
442 if (!rdev->constraints)
443 return sprintf(buf, "constraint not defined\n");
444
445 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
446}
David Brownell7ad68e22008-11-11 17:39:02 -0800447static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448
449static ssize_t regulator_min_uV_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100452 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453
454 if (!rdev->constraints)
455 return sprintf(buf, "constraint not defined\n");
456
457 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
458}
David Brownell7ad68e22008-11-11 17:39:02 -0800459static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460
461static ssize_t regulator_max_uV_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100464 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465
466 if (!rdev->constraints)
467 return sprintf(buf, "constraint not defined\n");
468
469 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
470}
David Brownell7ad68e22008-11-11 17:39:02 -0800471static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472
473static ssize_t regulator_total_uA_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
475{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100476 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477 struct regulator *regulator;
478 int uA = 0;
479
480 mutex_lock(&rdev->mutex);
481 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100482 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483 mutex_unlock(&rdev->mutex);
484 return sprintf(buf, "%d\n", uA);
485}
David Brownell7ad68e22008-11-11 17:39:02 -0800486static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
488static ssize_t regulator_num_users_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
490{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100491 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100492 return sprintf(buf, "%d\n", rdev->use_count);
493}
494
495static ssize_t regulator_type_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
497{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100498 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499
500 switch (rdev->desc->type) {
501 case REGULATOR_VOLTAGE:
502 return sprintf(buf, "voltage\n");
503 case REGULATOR_CURRENT:
504 return sprintf(buf, "current\n");
505 }
506 return sprintf(buf, "unknown\n");
507}
508
509static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100512 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
515}
David Brownell7ad68e22008-11-11 17:39:02 -0800516static DEVICE_ATTR(suspend_mem_microvolts, 0444,
517 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
519static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
521{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100522 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
525}
David Brownell7ad68e22008-11-11 17:39:02 -0800526static DEVICE_ATTR(suspend_disk_microvolts, 0444,
527 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528
529static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
531{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100532 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100533
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
535}
David Brownell7ad68e22008-11-11 17:39:02 -0800536static DEVICE_ATTR(suspend_standby_microvolts, 0444,
537 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100538
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
540 struct device_attribute *attr, char *buf)
541{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100542 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543
David Brownell4fca9542008-11-11 17:38:53 -0800544 return regulator_print_opmode(buf,
545 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546}
David Brownell7ad68e22008-11-11 17:39:02 -0800547static DEVICE_ATTR(suspend_mem_mode, 0444,
548 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100549
550static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
551 struct device_attribute *attr, char *buf)
552{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100553 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100554
David Brownell4fca9542008-11-11 17:38:53 -0800555 return regulator_print_opmode(buf,
556 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557}
David Brownell7ad68e22008-11-11 17:39:02 -0800558static DEVICE_ATTR(suspend_disk_mode, 0444,
559 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560
561static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
562 struct device_attribute *attr, char *buf)
563{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100564 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100565
David Brownell4fca9542008-11-11 17:38:53 -0800566 return regulator_print_opmode(buf,
567 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568}
David Brownell7ad68e22008-11-11 17:39:02 -0800569static DEVICE_ATTR(suspend_standby_mode, 0444,
570 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571
572static ssize_t regulator_suspend_mem_state_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
574{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100575 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100576
David Brownell4fca9542008-11-11 17:38:53 -0800577 return regulator_print_state(buf,
578 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100579}
David Brownell7ad68e22008-11-11 17:39:02 -0800580static DEVICE_ATTR(suspend_mem_state, 0444,
581 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582
583static ssize_t regulator_suspend_disk_state_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100586 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100587
David Brownell4fca9542008-11-11 17:38:53 -0800588 return regulator_print_state(buf,
589 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590}
David Brownell7ad68e22008-11-11 17:39:02 -0800591static DEVICE_ATTR(suspend_disk_state, 0444,
592 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593
594static ssize_t regulator_suspend_standby_state_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
596{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100597 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100598
David Brownell4fca9542008-11-11 17:38:53 -0800599 return regulator_print_state(buf,
600 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100601}
David Brownell7ad68e22008-11-11 17:39:02 -0800602static DEVICE_ATTR(suspend_standby_state, 0444,
603 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100604
David Brownell7ad68e22008-11-11 17:39:02 -0800605
606/*
607 * These are the only attributes are present for all regulators.
608 * Other attributes are a function of regulator functionality.
609 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100610static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100611 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
613 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100614 __ATTR_NULL,
615};
616
617static void regulator_dev_release(struct device *dev)
618{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100619 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620 kfree(rdev);
621}
622
623static struct class regulator_class = {
624 .name = "regulator",
625 .dev_release = regulator_dev_release,
626 .dev_attrs = regulator_dev_attrs,
627};
628
629/* Calculate the new optimum regulator operating mode based on the new total
630 * consumer load. All locks held by caller */
631static void drms_uA_update(struct regulator_dev *rdev)
632{
633 struct regulator *sibling;
634 int current_uA = 0, output_uV, input_uV, err;
635 unsigned int mode;
636
637 err = regulator_check_drms(rdev);
638 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000639 (!rdev->desc->ops->get_voltage &&
640 !rdev->desc->ops->get_voltage_sel) ||
641 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300642 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100643
644 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000645 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100646 if (output_uV <= 0)
647 return;
648
649 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000650 input_uV = 0;
651 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800652 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000653 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100654 input_uV = rdev->constraints->input_uV;
655 if (input_uV <= 0)
656 return;
657
658 /* calc total requested load */
659 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100660 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100661
662 /* now get the optimum mode for our new total regulator load */
663 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
664 output_uV, current_uA);
665
666 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900667 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100668 if (err == 0)
669 rdev->desc->ops->set_mode(rdev, mode);
670}
671
672static int suspend_set_state(struct regulator_dev *rdev,
673 struct regulator_state *rstate)
674{
675 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100676
677 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800678 * only warn if the driver implements set_suspend_voltage or
679 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100680 */
681 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800682 if (rdev->desc->ops->set_suspend_voltage ||
683 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800684 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100685 return 0;
686 }
687
688 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800689 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100690 return -EINVAL;
691 }
692
Axel Lin8ac0e952012-04-14 09:14:34 +0800693 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100694 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800695 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100696 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800697 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
698 ret = 0;
699
Liam Girdwood414c70c2008-04-30 15:59:04 +0100700 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800701 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100702 return ret;
703 }
704
705 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
706 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
707 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800708 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100709 return ret;
710 }
711 }
712
713 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
714 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
715 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800716 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100717 return ret;
718 }
719 }
720 return ret;
721}
722
723/* locks held by caller */
724static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
725{
726 if (!rdev->constraints)
727 return -EINVAL;
728
729 switch (state) {
730 case PM_SUSPEND_STANDBY:
731 return suspend_set_state(rdev,
732 &rdev->constraints->state_standby);
733 case PM_SUSPEND_MEM:
734 return suspend_set_state(rdev,
735 &rdev->constraints->state_mem);
736 case PM_SUSPEND_MAX:
737 return suspend_set_state(rdev,
738 &rdev->constraints->state_disk);
739 default:
740 return -EINVAL;
741 }
742}
743
744static void print_constraints(struct regulator_dev *rdev)
745{
746 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000747 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100748 int count = 0;
749 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100750
Mark Brown8f031b42009-10-22 16:31:31 +0100751 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100752 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100753 count += sprintf(buf + count, "%d mV ",
754 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100755 else
Mark Brown8f031b42009-10-22 16:31:31 +0100756 count += sprintf(buf + count, "%d <--> %d mV ",
757 constraints->min_uV / 1000,
758 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100759 }
Mark Brown8f031b42009-10-22 16:31:31 +0100760
761 if (!constraints->min_uV ||
762 constraints->min_uV != constraints->max_uV) {
763 ret = _regulator_get_voltage(rdev);
764 if (ret > 0)
765 count += sprintf(buf + count, "at %d mV ", ret / 1000);
766 }
767
Mark Brownbf5892a2011-05-08 22:13:37 +0100768 if (constraints->uV_offset)
769 count += sprintf(buf, "%dmV offset ",
770 constraints->uV_offset / 1000);
771
Mark Brown8f031b42009-10-22 16:31:31 +0100772 if (constraints->min_uA && constraints->max_uA) {
773 if (constraints->min_uA == constraints->max_uA)
774 count += sprintf(buf + count, "%d mA ",
775 constraints->min_uA / 1000);
776 else
777 count += sprintf(buf + count, "%d <--> %d mA ",
778 constraints->min_uA / 1000,
779 constraints->max_uA / 1000);
780 }
781
782 if (!constraints->min_uA ||
783 constraints->min_uA != constraints->max_uA) {
784 ret = _regulator_get_current_limit(rdev);
785 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400786 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100787 }
788
Liam Girdwood414c70c2008-04-30 15:59:04 +0100789 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
790 count += sprintf(buf + count, "fast ");
791 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
792 count += sprintf(buf + count, "normal ");
793 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
794 count += sprintf(buf + count, "idle ");
795 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
796 count += sprintf(buf + count, "standby");
797
Mark Brown13ce29f2010-12-17 16:04:12 +0000798 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000799
800 if ((constraints->min_uV != constraints->max_uV) &&
801 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
802 rdev_warn(rdev,
803 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100804}
805
Mark Browne79055d2009-10-19 15:53:50 +0100806static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100807 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100808{
809 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100810 int ret;
811
812 /* do we need to apply the constraint voltage */
813 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000814 rdev->constraints->min_uV == rdev->constraints->max_uV) {
815 ret = _regulator_do_set_voltage(rdev,
816 rdev->constraints->min_uV,
817 rdev->constraints->max_uV);
818 if (ret < 0) {
819 rdev_err(rdev, "failed to apply %duV constraint\n",
820 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000821 return ret;
822 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100823 }
Mark Browne79055d2009-10-19 15:53:50 +0100824
825 /* constrain machine-level voltage specs to fit
826 * the actual range supported by this regulator.
827 */
828 if (ops->list_voltage && rdev->desc->n_voltages) {
829 int count = rdev->desc->n_voltages;
830 int i;
831 int min_uV = INT_MAX;
832 int max_uV = INT_MIN;
833 int cmin = constraints->min_uV;
834 int cmax = constraints->max_uV;
835
836 /* it's safe to autoconfigure fixed-voltage supplies
837 and the constraints are used by list_voltage. */
838 if (count == 1 && !cmin) {
839 cmin = 1;
840 cmax = INT_MAX;
841 constraints->min_uV = cmin;
842 constraints->max_uV = cmax;
843 }
844
845 /* voltage constraints are optional */
846 if ((cmin == 0) && (cmax == 0))
847 return 0;
848
849 /* else require explicit machine-level constraints */
850 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800851 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100852 return -EINVAL;
853 }
854
855 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
856 for (i = 0; i < count; i++) {
857 int value;
858
859 value = ops->list_voltage(rdev, i);
860 if (value <= 0)
861 continue;
862
863 /* maybe adjust [min_uV..max_uV] */
864 if (value >= cmin && value < min_uV)
865 min_uV = value;
866 if (value <= cmax && value > max_uV)
867 max_uV = value;
868 }
869
870 /* final: [min_uV..max_uV] valid iff constraints valid */
871 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800872 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100873 return -EINVAL;
874 }
875
876 /* use regulator's subset of machine constraints */
877 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800878 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
879 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100880 constraints->min_uV = min_uV;
881 }
882 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800883 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
884 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100885 constraints->max_uV = max_uV;
886 }
887 }
888
889 return 0;
890}
891
Liam Girdwooda5766f12008-10-10 13:22:20 +0100892/**
893 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000894 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000895 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100896 *
897 * Allows platform initialisation code to define and constrain
898 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
899 * Constraints *must* be set by platform code in order for some
900 * regulator operations to proceed i.e. set_voltage, set_current_limit,
901 * set_mode.
902 */
903static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000904 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100905{
906 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100907 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100908
Mark Brown9a8f5e02011-11-29 18:11:19 +0000909 if (constraints)
910 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
911 GFP_KERNEL);
912 else
913 rdev->constraints = kzalloc(sizeof(*constraints),
914 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000915 if (!rdev->constraints)
916 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100917
Mark Brownf8c12fe2010-11-29 15:55:17 +0000918 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100919 if (ret != 0)
920 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800921
Liam Girdwooda5766f12008-10-10 13:22:20 +0100922 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000923 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000924 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100925 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800926 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100927 goto out;
928 }
929 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100930
Mark Brown9a8f5e02011-11-29 18:11:19 +0000931 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000932 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800933 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000934 ret = -EINVAL;
935 goto out;
936 }
937
Mark Brownf8c12fe2010-11-29 15:55:17 +0000938 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000939 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800940 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000941 goto out;
942 }
943 }
944
Mark Browncacf90f2009-03-02 16:32:46 +0000945 /* If the constraints say the regulator should be on at this point
946 * and we have control then make sure it is enabled.
947 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000948 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
949 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100950 ret = ops->enable(rdev);
951 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800952 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100953 goto out;
954 }
955 }
956
Liam Girdwooda5766f12008-10-10 13:22:20 +0100957 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800958 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100959out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800960 kfree(rdev->constraints);
961 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100962 return ret;
963}
964
965/**
966 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000967 * @rdev: regulator name
968 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100969 *
970 * Called by platform initialisation code to set the supply regulator for this
971 * regulator. This ensures that a regulators supply will also be enabled by the
972 * core if it's child is enabled.
973 */
974static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100975 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100976{
977 int err;
978
Mark Brown3801b862011-06-09 16:22:22 +0100979 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
980
981 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +0800982 if (rdev->supply == NULL) {
983 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +0100984 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100985 }
Mark Brown3801b862011-06-09 16:22:22 +0100986
987 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100988}
989
990/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800991 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000992 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +0100993 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000994 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100995 *
996 * Allows platform initialisation code to map physical regulator
997 * sources to symbolic names for supplies for use by devices. Devices
998 * should use these symbolic names to request regulators, avoiding the
999 * need to provide board-specific regulator names as platform data.
1000 */
1001static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001002 const char *consumer_dev_name,
1003 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001004{
1005 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001006 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001007
1008 if (supply == NULL)
1009 return -EINVAL;
1010
Mark Brown9ed20992009-07-21 16:00:26 +01001011 if (consumer_dev_name != NULL)
1012 has_dev = 1;
1013 else
1014 has_dev = 0;
1015
David Brownell6001e132008-12-31 12:54:19 +00001016 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001017 if (node->dev_name && consumer_dev_name) {
1018 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1019 continue;
1020 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001021 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001022 }
1023
David Brownell6001e132008-12-31 12:54:19 +00001024 if (strcmp(node->supply, supply) != 0)
1025 continue;
1026
Mark Brown737f3602012-02-02 00:10:51 +00001027 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1028 consumer_dev_name,
1029 dev_name(&node->regulator->dev),
1030 node->regulator->desc->name,
1031 supply,
1032 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001033 return -EBUSY;
1034 }
1035
Mark Brown9ed20992009-07-21 16:00:26 +01001036 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001037 if (node == NULL)
1038 return -ENOMEM;
1039
1040 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001041 node->supply = supply;
1042
Mark Brown9ed20992009-07-21 16:00:26 +01001043 if (has_dev) {
1044 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1045 if (node->dev_name == NULL) {
1046 kfree(node);
1047 return -ENOMEM;
1048 }
Mark Brown40f92442009-06-17 17:56:39 +01001049 }
1050
Liam Girdwooda5766f12008-10-10 13:22:20 +01001051 list_add(&node->list, &regulator_map_list);
1052 return 0;
1053}
1054
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001055static void unset_regulator_supplies(struct regulator_dev *rdev)
1056{
1057 struct regulator_map *node, *n;
1058
1059 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1060 if (rdev == node->regulator) {
1061 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001062 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001063 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001064 }
1065 }
1066}
1067
Mark Brownf5726ae2011-06-09 16:22:20 +01001068#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001069
1070static struct regulator *create_regulator(struct regulator_dev *rdev,
1071 struct device *dev,
1072 const char *supply_name)
1073{
1074 struct regulator *regulator;
1075 char buf[REG_STR_SIZE];
1076 int err, size;
1077
1078 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1079 if (regulator == NULL)
1080 return NULL;
1081
1082 mutex_lock(&rdev->mutex);
1083 regulator->rdev = rdev;
1084 list_add(&regulator->list, &rdev->consumer_list);
1085
1086 if (dev) {
1087 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001088 size = scnprintf(buf, REG_STR_SIZE,
1089 "microamps_requested_%s-%s",
1090 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001091 if (size >= REG_STR_SIZE)
1092 goto overflow_err;
1093
1094 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001095 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001096 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1097 if (regulator->dev_attr.attr.name == NULL)
1098 goto attr_name_err;
1099
Liam Girdwood414c70c2008-04-30 15:59:04 +01001100 regulator->dev_attr.attr.mode = 0444;
1101 regulator->dev_attr.show = device_requested_uA_show;
1102 err = device_create_file(dev, &regulator->dev_attr);
1103 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001104 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001105 goto attr_name_err;
1106 }
1107
1108 /* also add a link to the device sysfs entry */
1109 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1110 dev->kobj.name, supply_name);
1111 if (size >= REG_STR_SIZE)
1112 goto attr_err;
1113
1114 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1115 if (regulator->supply_name == NULL)
1116 goto attr_err;
1117
1118 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1119 buf);
1120 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001121 rdev_warn(rdev, "could not add device link %s err %d\n",
1122 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001123 goto link_name_err;
1124 }
Mark Brown5de70512011-06-19 13:33:16 +01001125 } else {
1126 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1127 if (regulator->supply_name == NULL)
1128 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001129 }
Mark Brown5de70512011-06-19 13:33:16 +01001130
Mark Brown5de70512011-06-19 13:33:16 +01001131 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1132 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001133 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001134 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001135 } else {
1136 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1137 &regulator->uA_load);
1138 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1139 &regulator->min_uV);
1140 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1141 &regulator->max_uV);
1142 }
Mark Brown5de70512011-06-19 13:33:16 +01001143
Liam Girdwood414c70c2008-04-30 15:59:04 +01001144 mutex_unlock(&rdev->mutex);
1145 return regulator;
1146link_name_err:
1147 kfree(regulator->supply_name);
1148attr_err:
1149 device_remove_file(regulator->dev, &regulator->dev_attr);
1150attr_name_err:
1151 kfree(regulator->dev_attr.attr.name);
1152overflow_err:
1153 list_del(&regulator->list);
1154 kfree(regulator);
1155 mutex_unlock(&rdev->mutex);
1156 return NULL;
1157}
1158
Mark Brown31aae2b2009-12-21 12:21:52 +00001159static int _regulator_get_enable_time(struct regulator_dev *rdev)
1160{
1161 if (!rdev->desc->ops->enable_time)
1162 return 0;
1163 return rdev->desc->ops->enable_time(rdev);
1164}
1165
Rajendra Nayak69511a42011-11-18 16:47:20 +05301166static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001167 const char *supply,
1168 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301169{
1170 struct regulator_dev *r;
1171 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001172 struct regulator_map *map;
1173 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301174
1175 /* first do a dt based lookup */
1176 if (dev && dev->of_node) {
1177 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001178 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301179 list_for_each_entry(r, &regulator_list, list)
1180 if (r->dev.parent &&
1181 node == r->dev.of_node)
1182 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001183 } else {
1184 /*
1185 * If we couldn't even get the node then it's
1186 * not just that the device didn't register
1187 * yet, there's no node and we'll never
1188 * succeed.
1189 */
1190 *ret = -ENODEV;
1191 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301192 }
1193
1194 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001195 if (dev)
1196 devname = dev_name(dev);
1197
Rajendra Nayak69511a42011-11-18 16:47:20 +05301198 list_for_each_entry(r, &regulator_list, list)
1199 if (strcmp(rdev_get_name(r), supply) == 0)
1200 return r;
1201
Mark Brown576ca4362012-03-30 12:24:37 +01001202 list_for_each_entry(map, &regulator_map_list, list) {
1203 /* If the mapping has a device set up it must match */
1204 if (map->dev_name &&
1205 (!devname || strcmp(map->dev_name, devname)))
1206 continue;
1207
1208 if (strcmp(map->supply, supply) == 0)
1209 return map->regulator;
1210 }
1211
1212
Rajendra Nayak69511a42011-11-18 16:47:20 +05301213 return NULL;
1214}
1215
Mark Brown5ffbd132009-07-21 16:00:23 +01001216/* Internal regulator request function */
1217static struct regulator *_regulator_get(struct device *dev, const char *id,
1218 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001219{
1220 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001221 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001222 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001223 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001224
1225 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001226 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001227 return regulator;
1228 }
1229
Mark Brown40f92442009-06-17 17:56:39 +01001230 if (dev)
1231 devname = dev_name(dev);
1232
Liam Girdwood414c70c2008-04-30 15:59:04 +01001233 mutex_lock(&regulator_list_mutex);
1234
Mark Brown6d191a52012-03-29 14:19:02 +01001235 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301236 if (rdev)
1237 goto found;
1238
Mark Brown688fe992010-10-05 19:18:32 -07001239 if (board_wants_dummy_regulator) {
1240 rdev = dummy_regulator_rdev;
1241 goto found;
1242 }
1243
Mark Brown34abbd62010-02-12 10:18:08 +00001244#ifdef CONFIG_REGULATOR_DUMMY
1245 if (!devname)
1246 devname = "deviceless";
1247
1248 /* If the board didn't flag that it was fully constrained then
1249 * substitute in a dummy regulator so consumers can continue.
1250 */
1251 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001252 pr_warn("%s supply %s not found, using dummy regulator\n",
1253 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001254 rdev = dummy_regulator_rdev;
1255 goto found;
1256 }
1257#endif
1258
Liam Girdwood414c70c2008-04-30 15:59:04 +01001259 mutex_unlock(&regulator_list_mutex);
1260 return regulator;
1261
1262found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001263 if (rdev->exclusive) {
1264 regulator = ERR_PTR(-EPERM);
1265 goto out;
1266 }
1267
1268 if (exclusive && rdev->open_count) {
1269 regulator = ERR_PTR(-EBUSY);
1270 goto out;
1271 }
1272
Liam Girdwooda5766f12008-10-10 13:22:20 +01001273 if (!try_module_get(rdev->owner))
1274 goto out;
1275
Liam Girdwood414c70c2008-04-30 15:59:04 +01001276 regulator = create_regulator(rdev, dev, id);
1277 if (regulator == NULL) {
1278 regulator = ERR_PTR(-ENOMEM);
1279 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001280 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001281 }
1282
Mark Brown5ffbd132009-07-21 16:00:23 +01001283 rdev->open_count++;
1284 if (exclusive) {
1285 rdev->exclusive = 1;
1286
1287 ret = _regulator_is_enabled(rdev);
1288 if (ret > 0)
1289 rdev->use_count = 1;
1290 else
1291 rdev->use_count = 0;
1292 }
1293
Liam Girdwooda5766f12008-10-10 13:22:20 +01001294out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001295 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001296
Liam Girdwood414c70c2008-04-30 15:59:04 +01001297 return regulator;
1298}
Mark Brown5ffbd132009-07-21 16:00:23 +01001299
1300/**
1301 * regulator_get - lookup and obtain a reference to a regulator.
1302 * @dev: device for regulator "consumer"
1303 * @id: Supply name or regulator ID.
1304 *
1305 * Returns a struct regulator corresponding to the regulator producer,
1306 * or IS_ERR() condition containing errno.
1307 *
1308 * Use of supply names configured via regulator_set_device_supply() is
1309 * strongly encouraged. It is recommended that the supply name used
1310 * should match the name used for the supply and/or the relevant
1311 * device pins in the datasheet.
1312 */
1313struct regulator *regulator_get(struct device *dev, const char *id)
1314{
1315 return _regulator_get(dev, id, 0);
1316}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001317EXPORT_SYMBOL_GPL(regulator_get);
1318
Stephen Boyd070b9072012-01-16 19:39:58 -08001319static void devm_regulator_release(struct device *dev, void *res)
1320{
1321 regulator_put(*(struct regulator **)res);
1322}
1323
1324/**
1325 * devm_regulator_get - Resource managed regulator_get()
1326 * @dev: device for regulator "consumer"
1327 * @id: Supply name or regulator ID.
1328 *
1329 * Managed regulator_get(). Regulators returned from this function are
1330 * automatically regulator_put() on driver detach. See regulator_get() for more
1331 * information.
1332 */
1333struct regulator *devm_regulator_get(struct device *dev, const char *id)
1334{
1335 struct regulator **ptr, *regulator;
1336
1337 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1338 if (!ptr)
1339 return ERR_PTR(-ENOMEM);
1340
1341 regulator = regulator_get(dev, id);
1342 if (!IS_ERR(regulator)) {
1343 *ptr = regulator;
1344 devres_add(dev, ptr);
1345 } else {
1346 devres_free(ptr);
1347 }
1348
1349 return regulator;
1350}
1351EXPORT_SYMBOL_GPL(devm_regulator_get);
1352
Liam Girdwood414c70c2008-04-30 15:59:04 +01001353/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001354 * regulator_get_exclusive - obtain exclusive access to a regulator.
1355 * @dev: device for regulator "consumer"
1356 * @id: Supply name or regulator ID.
1357 *
1358 * Returns a struct regulator corresponding to the regulator producer,
1359 * or IS_ERR() condition containing errno. Other consumers will be
1360 * unable to obtain this reference is held and the use count for the
1361 * regulator will be initialised to reflect the current state of the
1362 * regulator.
1363 *
1364 * This is intended for use by consumers which cannot tolerate shared
1365 * use of the regulator such as those which need to force the
1366 * regulator off for correct operation of the hardware they are
1367 * controlling.
1368 *
1369 * Use of supply names configured via regulator_set_device_supply() is
1370 * strongly encouraged. It is recommended that the supply name used
1371 * should match the name used for the supply and/or the relevant
1372 * device pins in the datasheet.
1373 */
1374struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1375{
1376 return _regulator_get(dev, id, 1);
1377}
1378EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1379
1380/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001381 * regulator_put - "free" the regulator source
1382 * @regulator: regulator source
1383 *
1384 * Note: drivers must ensure that all regulator_enable calls made on this
1385 * regulator source are balanced by regulator_disable calls prior to calling
1386 * this function.
1387 */
1388void regulator_put(struct regulator *regulator)
1389{
1390 struct regulator_dev *rdev;
1391
1392 if (regulator == NULL || IS_ERR(regulator))
1393 return;
1394
Liam Girdwood414c70c2008-04-30 15:59:04 +01001395 mutex_lock(&regulator_list_mutex);
1396 rdev = regulator->rdev;
1397
Mark Brown5de70512011-06-19 13:33:16 +01001398 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001399
Liam Girdwood414c70c2008-04-30 15:59:04 +01001400 /* remove any sysfs entries */
1401 if (regulator->dev) {
1402 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001403 device_remove_file(regulator->dev, &regulator->dev_attr);
1404 kfree(regulator->dev_attr.attr.name);
1405 }
Mark Brown5de70512011-06-19 13:33:16 +01001406 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001407 list_del(&regulator->list);
1408 kfree(regulator);
1409
Mark Brown5ffbd132009-07-21 16:00:23 +01001410 rdev->open_count--;
1411 rdev->exclusive = 0;
1412
Liam Girdwood414c70c2008-04-30 15:59:04 +01001413 module_put(rdev->owner);
1414 mutex_unlock(&regulator_list_mutex);
1415}
1416EXPORT_SYMBOL_GPL(regulator_put);
1417
Mark Brownd5ad34f2012-01-20 20:09:18 +00001418static int devm_regulator_match(struct device *dev, void *res, void *data)
1419{
1420 struct regulator **r = res;
1421 if (!r || !*r) {
1422 WARN_ON(!r || !*r);
1423 return 0;
1424 }
1425 return *r == data;
1426}
1427
1428/**
1429 * devm_regulator_put - Resource managed regulator_put()
1430 * @regulator: regulator to free
1431 *
1432 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1433 * this function will not need to be called and the resource management
1434 * code will ensure that the resource is freed.
1435 */
1436void devm_regulator_put(struct regulator *regulator)
1437{
1438 int rc;
1439
1440 rc = devres_destroy(regulator->dev, devm_regulator_release,
1441 devm_regulator_match, regulator);
1442 WARN_ON(rc);
1443}
1444EXPORT_SYMBOL_GPL(devm_regulator_put);
1445
Mark Brown9a2372f2009-08-03 18:49:57 +01001446static int _regulator_can_change_status(struct regulator_dev *rdev)
1447{
1448 if (!rdev->constraints)
1449 return 0;
1450
1451 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1452 return 1;
1453 else
1454 return 0;
1455}
1456
Liam Girdwood414c70c2008-04-30 15:59:04 +01001457/* locks held by regulator_enable() */
1458static int _regulator_enable(struct regulator_dev *rdev)
1459{
Mark Brown31aae2b2009-12-21 12:21:52 +00001460 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001461
Liam Girdwood414c70c2008-04-30 15:59:04 +01001462 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001463 if (rdev->constraints &&
1464 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1465 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001466
Mark Brown9a2372f2009-08-03 18:49:57 +01001467 if (rdev->use_count == 0) {
1468 /* The regulator may on if it's not switchable or left on */
1469 ret = _regulator_is_enabled(rdev);
1470 if (ret == -EINVAL || ret == 0) {
1471 if (!_regulator_can_change_status(rdev))
1472 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001473
Mark Brown31aae2b2009-12-21 12:21:52 +00001474 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001475 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001476
1477 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001478 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001479 ret = _regulator_get_enable_time(rdev);
1480 if (ret >= 0) {
1481 delay = ret;
1482 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001483 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001484 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001485 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001486 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001487
Mark Brown02fa3ec2010-11-10 14:38:30 +00001488 trace_regulator_enable(rdev_get_name(rdev));
1489
Mark Brown31aae2b2009-12-21 12:21:52 +00001490 /* Allow the regulator to ramp; it would be useful
1491 * to extend this for bulk operations so that the
1492 * regulators can ramp together. */
1493 ret = rdev->desc->ops->enable(rdev);
1494 if (ret < 0)
1495 return ret;
1496
Mark Brown02fa3ec2010-11-10 14:38:30 +00001497 trace_regulator_enable_delay(rdev_get_name(rdev));
1498
Axel Line36c1df2010-11-05 21:51:32 +08001499 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001500 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001501 udelay(delay % 1000);
1502 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001503 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001504 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001505
Mark Brown02fa3ec2010-11-10 14:38:30 +00001506 trace_regulator_enable_complete(rdev_get_name(rdev));
1507
Linus Walleija7433cf2009-08-26 12:54:04 +02001508 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001509 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001510 return ret;
1511 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001512 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001513 }
1514
Mark Brown9a2372f2009-08-03 18:49:57 +01001515 rdev->use_count++;
1516
1517 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001518}
1519
1520/**
1521 * regulator_enable - enable regulator output
1522 * @regulator: regulator source
1523 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001524 * Request that the regulator be enabled with the regulator output at
1525 * the predefined voltage or current value. Calls to regulator_enable()
1526 * must be balanced with calls to regulator_disable().
1527 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001528 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001529 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001530 */
1531int regulator_enable(struct regulator *regulator)
1532{
David Brownell412aec62008-11-16 11:44:46 -08001533 struct regulator_dev *rdev = regulator->rdev;
1534 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001535
Mark Brown3801b862011-06-09 16:22:22 +01001536 if (rdev->supply) {
1537 ret = regulator_enable(rdev->supply);
1538 if (ret != 0)
1539 return ret;
1540 }
1541
David Brownell412aec62008-11-16 11:44:46 -08001542 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001543 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001544 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001545
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001546 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001547 regulator_disable(rdev->supply);
1548
Liam Girdwood414c70c2008-04-30 15:59:04 +01001549 return ret;
1550}
1551EXPORT_SYMBOL_GPL(regulator_enable);
1552
1553/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001554static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001555{
1556 int ret = 0;
1557
David Brownellcd94b502009-03-11 16:43:34 -08001558 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001559 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001560 return -EIO;
1561
Liam Girdwood414c70c2008-04-30 15:59:04 +01001562 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001563 if (rdev->use_count == 1 &&
1564 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001565
1566 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001567 if (_regulator_can_change_status(rdev) &&
1568 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001569 trace_regulator_disable(rdev_get_name(rdev));
1570
Liam Girdwood414c70c2008-04-30 15:59:04 +01001571 ret = rdev->desc->ops->disable(rdev);
1572 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001573 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001574 return ret;
1575 }
Mark Brown84b68262009-12-01 21:12:27 +00001576
Mark Brown02fa3ec2010-11-10 14:38:30 +00001577 trace_regulator_disable_complete(rdev_get_name(rdev));
1578
Mark Brown84b68262009-12-01 21:12:27 +00001579 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1580 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001581 }
1582
Liam Girdwood414c70c2008-04-30 15:59:04 +01001583 rdev->use_count = 0;
1584 } else if (rdev->use_count > 1) {
1585
1586 if (rdev->constraints &&
1587 (rdev->constraints->valid_ops_mask &
1588 REGULATOR_CHANGE_DRMS))
1589 drms_uA_update(rdev);
1590
1591 rdev->use_count--;
1592 }
Mark Brown3801b862011-06-09 16:22:22 +01001593
Liam Girdwood414c70c2008-04-30 15:59:04 +01001594 return ret;
1595}
1596
1597/**
1598 * regulator_disable - disable regulator output
1599 * @regulator: regulator source
1600 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001601 * Disable the regulator output voltage or current. Calls to
1602 * regulator_enable() must be balanced with calls to
1603 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001604 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001605 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001606 * devices have it enabled, the regulator device supports disabling and
1607 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001608 */
1609int regulator_disable(struct regulator *regulator)
1610{
David Brownell412aec62008-11-16 11:44:46 -08001611 struct regulator_dev *rdev = regulator->rdev;
1612 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001613
David Brownell412aec62008-11-16 11:44:46 -08001614 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001615 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001616 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001617
Mark Brown3801b862011-06-09 16:22:22 +01001618 if (ret == 0 && rdev->supply)
1619 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001620
Liam Girdwood414c70c2008-04-30 15:59:04 +01001621 return ret;
1622}
1623EXPORT_SYMBOL_GPL(regulator_disable);
1624
1625/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001626static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001627{
1628 int ret = 0;
1629
1630 /* force disable */
1631 if (rdev->desc->ops->disable) {
1632 /* ah well, who wants to live forever... */
1633 ret = rdev->desc->ops->disable(rdev);
1634 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001635 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001636 return ret;
1637 }
1638 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001639 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1640 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001641 }
1642
Liam Girdwood414c70c2008-04-30 15:59:04 +01001643 return ret;
1644}
1645
1646/**
1647 * regulator_force_disable - force disable regulator output
1648 * @regulator: regulator source
1649 *
1650 * Forcibly disable the regulator output voltage or current.
1651 * NOTE: this *will* disable the regulator output even if other consumer
1652 * devices have it enabled. This should be used for situations when device
1653 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1654 */
1655int regulator_force_disable(struct regulator *regulator)
1656{
Mark Brown82d15832011-05-09 11:41:02 +02001657 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001658 int ret;
1659
Mark Brown82d15832011-05-09 11:41:02 +02001660 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001661 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001662 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001663 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001664
Mark Brown3801b862011-06-09 16:22:22 +01001665 if (rdev->supply)
1666 while (rdev->open_count--)
1667 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001668
Liam Girdwood414c70c2008-04-30 15:59:04 +01001669 return ret;
1670}
1671EXPORT_SYMBOL_GPL(regulator_force_disable);
1672
Mark Brownda07ecd2011-09-11 09:53:50 +01001673static void regulator_disable_work(struct work_struct *work)
1674{
1675 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1676 disable_work.work);
1677 int count, i, ret;
1678
1679 mutex_lock(&rdev->mutex);
1680
1681 BUG_ON(!rdev->deferred_disables);
1682
1683 count = rdev->deferred_disables;
1684 rdev->deferred_disables = 0;
1685
1686 for (i = 0; i < count; i++) {
1687 ret = _regulator_disable(rdev);
1688 if (ret != 0)
1689 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1690 }
1691
1692 mutex_unlock(&rdev->mutex);
1693
1694 if (rdev->supply) {
1695 for (i = 0; i < count; i++) {
1696 ret = regulator_disable(rdev->supply);
1697 if (ret != 0) {
1698 rdev_err(rdev,
1699 "Supply disable failed: %d\n", ret);
1700 }
1701 }
1702 }
1703}
1704
1705/**
1706 * regulator_disable_deferred - disable regulator output with delay
1707 * @regulator: regulator source
1708 * @ms: miliseconds until the regulator is disabled
1709 *
1710 * Execute regulator_disable() on the regulator after a delay. This
1711 * is intended for use with devices that require some time to quiesce.
1712 *
1713 * NOTE: this will only disable the regulator output if no other consumer
1714 * devices have it enabled, the regulator device supports disabling and
1715 * machine constraints permit this operation.
1716 */
1717int regulator_disable_deferred(struct regulator *regulator, int ms)
1718{
1719 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001720 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001721
1722 mutex_lock(&rdev->mutex);
1723 rdev->deferred_disables++;
1724 mutex_unlock(&rdev->mutex);
1725
Mark Brownaa598022011-10-03 22:42:43 +01001726 ret = schedule_delayed_work(&rdev->disable_work,
1727 msecs_to_jiffies(ms));
1728 if (ret < 0)
1729 return ret;
1730 else
1731 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001732}
1733EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1734
Liam Girdwood414c70c2008-04-30 15:59:04 +01001735static int _regulator_is_enabled(struct regulator_dev *rdev)
1736{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001737 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001738 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001739 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001740
Mark Brown93325462009-08-03 18:49:56 +01001741 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001742}
1743
1744/**
1745 * regulator_is_enabled - is the regulator output enabled
1746 * @regulator: regulator source
1747 *
David Brownell412aec62008-11-16 11:44:46 -08001748 * Returns positive if the regulator driver backing the source/client
1749 * has requested that the device be enabled, zero if it hasn't, else a
1750 * negative errno code.
1751 *
1752 * Note that the device backing this regulator handle can have multiple
1753 * users, so it might be enabled even if regulator_enable() was never
1754 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001755 */
1756int regulator_is_enabled(struct regulator *regulator)
1757{
Mark Brown93325462009-08-03 18:49:56 +01001758 int ret;
1759
1760 mutex_lock(&regulator->rdev->mutex);
1761 ret = _regulator_is_enabled(regulator->rdev);
1762 mutex_unlock(&regulator->rdev->mutex);
1763
1764 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001765}
1766EXPORT_SYMBOL_GPL(regulator_is_enabled);
1767
1768/**
David Brownell4367cfd2009-02-26 11:48:36 -08001769 * regulator_count_voltages - count regulator_list_voltage() selectors
1770 * @regulator: regulator source
1771 *
1772 * Returns number of selectors, or negative errno. Selectors are
1773 * numbered starting at zero, and typically correspond to bitfields
1774 * in hardware registers.
1775 */
1776int regulator_count_voltages(struct regulator *regulator)
1777{
1778 struct regulator_dev *rdev = regulator->rdev;
1779
1780 return rdev->desc->n_voltages ? : -EINVAL;
1781}
1782EXPORT_SYMBOL_GPL(regulator_count_voltages);
1783
1784/**
1785 * regulator_list_voltage - enumerate supported voltages
1786 * @regulator: regulator source
1787 * @selector: identify voltage to list
1788 * Context: can sleep
1789 *
1790 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001791 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001792 * negative errno.
1793 */
1794int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1795{
1796 struct regulator_dev *rdev = regulator->rdev;
1797 struct regulator_ops *ops = rdev->desc->ops;
1798 int ret;
1799
1800 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1801 return -EINVAL;
1802
1803 mutex_lock(&rdev->mutex);
1804 ret = ops->list_voltage(rdev, selector);
1805 mutex_unlock(&rdev->mutex);
1806
1807 if (ret > 0) {
1808 if (ret < rdev->constraints->min_uV)
1809 ret = 0;
1810 else if (ret > rdev->constraints->max_uV)
1811 ret = 0;
1812 }
1813
1814 return ret;
1815}
1816EXPORT_SYMBOL_GPL(regulator_list_voltage);
1817
1818/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001819 * regulator_is_supported_voltage - check if a voltage range can be supported
1820 *
1821 * @regulator: Regulator to check.
1822 * @min_uV: Minimum required voltage in uV.
1823 * @max_uV: Maximum required voltage in uV.
1824 *
1825 * Returns a boolean or a negative error code.
1826 */
1827int regulator_is_supported_voltage(struct regulator *regulator,
1828 int min_uV, int max_uV)
1829{
1830 int i, voltages, ret;
1831
1832 ret = regulator_count_voltages(regulator);
1833 if (ret < 0)
1834 return ret;
1835 voltages = ret;
1836
1837 for (i = 0; i < voltages; i++) {
1838 ret = regulator_list_voltage(regulator, i);
1839
1840 if (ret >= min_uV && ret <= max_uV)
1841 return 1;
1842 }
1843
1844 return 0;
1845}
Mark Browna398eaa2011-12-28 12:48:45 +00001846EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01001847
Mark Brown75790252010-12-12 14:25:50 +00001848static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1849 int min_uV, int max_uV)
1850{
1851 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001852 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001853 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08001854 int old_selector = -1;
1855 int best_val = INT_MAX;
Mark Brown75790252010-12-12 14:25:50 +00001856
1857 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1858
Mark Brownbf5892a2011-05-08 22:13:37 +01001859 min_uV += rdev->constraints->uV_offset;
1860 max_uV += rdev->constraints->uV_offset;
1861
Axel Lineba41a52012-04-04 10:32:10 +08001862 /*
1863 * If we can't obtain the old selector there is not enough
1864 * info to call set_voltage_time_sel().
1865 */
1866 if (rdev->desc->ops->set_voltage_time_sel &&
1867 rdev->desc->ops->get_voltage_sel) {
1868 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
1869 if (old_selector < 0)
1870 return old_selector;
1871 }
1872
Mark Brown75790252010-12-12 14:25:50 +00001873 if (rdev->desc->ops->set_voltage) {
1874 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1875 &selector);
1876
1877 if (rdev->desc->ops->list_voltage)
Axel Lineba41a52012-04-04 10:32:10 +08001878 best_val = rdev->desc->ops->list_voltage(rdev,
Mark Brown75790252010-12-12 14:25:50 +00001879 selector);
1880 else
Axel Lineba41a52012-04-04 10:32:10 +08001881 best_val = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001882 } else if (rdev->desc->ops->set_voltage_sel) {
Mark Browne8eef822010-12-12 14:36:17 +00001883 int i;
1884
1885 selector = 0;
1886
1887 /* Find the smallest voltage that falls within the specified
1888 * range.
1889 */
1890 for (i = 0; i < rdev->desc->n_voltages; i++) {
1891 ret = rdev->desc->ops->list_voltage(rdev, i);
1892 if (ret < 0)
1893 continue;
1894
1895 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1896 best_val = ret;
1897 selector = i;
1898 }
1899 }
1900
Axel Lineba41a52012-04-04 10:32:10 +08001901 if (best_val != INT_MAX)
Mark Browne8eef822010-12-12 14:36:17 +00001902 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
Axel Lineba41a52012-04-04 10:32:10 +08001903 else
Mark Browne8eef822010-12-12 14:36:17 +00001904 ret = -EINVAL;
Mark Brown75790252010-12-12 14:25:50 +00001905 } else {
1906 ret = -EINVAL;
1907 }
1908
Axel Lineba41a52012-04-04 10:32:10 +08001909 /* Call set_voltage_time_sel if successfully obtained old_selector */
1910 if (ret == 0 && old_selector >= 0 &&
1911 rdev->desc->ops->set_voltage_time_sel) {
1912
1913 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1914 old_selector, selector);
1915 if (delay < 0) {
1916 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
1917 delay);
1918 delay = 0;
1919 }
1920 }
1921
Linus Walleij77af1b2642011-03-17 13:24:36 +01001922 /* Insert any necessary delays */
1923 if (delay >= 1000) {
1924 mdelay(delay / 1000);
1925 udelay(delay % 1000);
1926 } else if (delay) {
1927 udelay(delay);
1928 }
1929
Mark Brownded06a52010-12-16 13:59:10 +00001930 if (ret == 0)
1931 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1932 NULL);
1933
Axel Lineba41a52012-04-04 10:32:10 +08001934 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00001935
1936 return ret;
1937}
1938
Mark Browna7a1ad92009-07-21 16:00:24 +01001939/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001940 * regulator_set_voltage - set regulator output voltage
1941 * @regulator: regulator source
1942 * @min_uV: Minimum required voltage in uV
1943 * @max_uV: Maximum acceptable voltage in uV
1944 *
1945 * Sets a voltage regulator to the desired output voltage. This can be set
1946 * during any regulator state. IOW, regulator can be disabled or enabled.
1947 *
1948 * If the regulator is enabled then the voltage will change to the new value
1949 * immediately otherwise if the regulator is disabled the regulator will
1950 * output at the new voltage when enabled.
1951 *
1952 * NOTE: If the regulator is shared between several devices then the lowest
1953 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001954 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001955 * calling this function otherwise this call will fail.
1956 */
1957int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1958{
1959 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001960 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001961
1962 mutex_lock(&rdev->mutex);
1963
Mark Brown95a3c232010-12-16 15:49:37 +00001964 /* If we're setting the same range as last time the change
1965 * should be a noop (some cpufreq implementations use the same
1966 * voltage for multiple frequencies, for example).
1967 */
1968 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1969 goto out;
1970
Liam Girdwood414c70c2008-04-30 15:59:04 +01001971 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001972 if (!rdev->desc->ops->set_voltage &&
1973 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001974 ret = -EINVAL;
1975 goto out;
1976 }
1977
1978 /* constraints check */
1979 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1980 if (ret < 0)
1981 goto out;
1982 regulator->min_uV = min_uV;
1983 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001984
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001985 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1986 if (ret < 0)
1987 goto out;
1988
Mark Brown75790252010-12-12 14:25:50 +00001989 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001990
Liam Girdwood414c70c2008-04-30 15:59:04 +01001991out:
1992 mutex_unlock(&rdev->mutex);
1993 return ret;
1994}
1995EXPORT_SYMBOL_GPL(regulator_set_voltage);
1996
Mark Brown606a2562010-12-16 15:49:36 +00001997/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001998 * regulator_set_voltage_time - get raise/fall time
1999 * @regulator: regulator source
2000 * @old_uV: starting voltage in microvolts
2001 * @new_uV: target voltage in microvolts
2002 *
2003 * Provided with the starting and ending voltage, this function attempts to
2004 * calculate the time in microseconds required to rise or fall to this new
2005 * voltage.
2006 */
2007int regulator_set_voltage_time(struct regulator *regulator,
2008 int old_uV, int new_uV)
2009{
2010 struct regulator_dev *rdev = regulator->rdev;
2011 struct regulator_ops *ops = rdev->desc->ops;
2012 int old_sel = -1;
2013 int new_sel = -1;
2014 int voltage;
2015 int i;
2016
2017 /* Currently requires operations to do this */
2018 if (!ops->list_voltage || !ops->set_voltage_time_sel
2019 || !rdev->desc->n_voltages)
2020 return -EINVAL;
2021
2022 for (i = 0; i < rdev->desc->n_voltages; i++) {
2023 /* We only look for exact voltage matches here */
2024 voltage = regulator_list_voltage(regulator, i);
2025 if (voltage < 0)
2026 return -EINVAL;
2027 if (voltage == 0)
2028 continue;
2029 if (voltage == old_uV)
2030 old_sel = i;
2031 if (voltage == new_uV)
2032 new_sel = i;
2033 }
2034
2035 if (old_sel < 0 || new_sel < 0)
2036 return -EINVAL;
2037
2038 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2039}
2040EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2041
2042/**
Mark Brown606a2562010-12-16 15:49:36 +00002043 * regulator_sync_voltage - re-apply last regulator output voltage
2044 * @regulator: regulator source
2045 *
2046 * Re-apply the last configured voltage. This is intended to be used
2047 * where some external control source the consumer is cooperating with
2048 * has caused the configured voltage to change.
2049 */
2050int regulator_sync_voltage(struct regulator *regulator)
2051{
2052 struct regulator_dev *rdev = regulator->rdev;
2053 int ret, min_uV, max_uV;
2054
2055 mutex_lock(&rdev->mutex);
2056
2057 if (!rdev->desc->ops->set_voltage &&
2058 !rdev->desc->ops->set_voltage_sel) {
2059 ret = -EINVAL;
2060 goto out;
2061 }
2062
2063 /* This is only going to work if we've had a voltage configured. */
2064 if (!regulator->min_uV && !regulator->max_uV) {
2065 ret = -EINVAL;
2066 goto out;
2067 }
2068
2069 min_uV = regulator->min_uV;
2070 max_uV = regulator->max_uV;
2071
2072 /* This should be a paranoia check... */
2073 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2074 if (ret < 0)
2075 goto out;
2076
2077 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2078 if (ret < 0)
2079 goto out;
2080
2081 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2082
2083out:
2084 mutex_unlock(&rdev->mutex);
2085 return ret;
2086}
2087EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2088
Liam Girdwood414c70c2008-04-30 15:59:04 +01002089static int _regulator_get_voltage(struct regulator_dev *rdev)
2090{
Mark Brownbf5892a2011-05-08 22:13:37 +01002091 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002092
2093 if (rdev->desc->ops->get_voltage_sel) {
2094 sel = rdev->desc->ops->get_voltage_sel(rdev);
2095 if (sel < 0)
2096 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002097 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002098 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002099 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002100 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002101 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002102 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002103
Axel Lincb220d12011-05-23 20:08:10 +08002104 if (ret < 0)
2105 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002106 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002107}
2108
2109/**
2110 * regulator_get_voltage - get regulator output voltage
2111 * @regulator: regulator source
2112 *
2113 * This returns the current regulator voltage in uV.
2114 *
2115 * NOTE: If the regulator is disabled it will return the voltage value. This
2116 * function should not be used to determine regulator state.
2117 */
2118int regulator_get_voltage(struct regulator *regulator)
2119{
2120 int ret;
2121
2122 mutex_lock(&regulator->rdev->mutex);
2123
2124 ret = _regulator_get_voltage(regulator->rdev);
2125
2126 mutex_unlock(&regulator->rdev->mutex);
2127
2128 return ret;
2129}
2130EXPORT_SYMBOL_GPL(regulator_get_voltage);
2131
2132/**
2133 * regulator_set_current_limit - set regulator output current limit
2134 * @regulator: regulator source
2135 * @min_uA: Minimuum supported current in uA
2136 * @max_uA: Maximum supported current in uA
2137 *
2138 * Sets current sink to the desired output current. This can be set during
2139 * any regulator state. IOW, regulator can be disabled or enabled.
2140 *
2141 * If the regulator is enabled then the current will change to the new value
2142 * immediately otherwise if the regulator is disabled the regulator will
2143 * output at the new current when enabled.
2144 *
2145 * NOTE: Regulator system constraints must be set for this regulator before
2146 * calling this function otherwise this call will fail.
2147 */
2148int regulator_set_current_limit(struct regulator *regulator,
2149 int min_uA, int max_uA)
2150{
2151 struct regulator_dev *rdev = regulator->rdev;
2152 int ret;
2153
2154 mutex_lock(&rdev->mutex);
2155
2156 /* sanity check */
2157 if (!rdev->desc->ops->set_current_limit) {
2158 ret = -EINVAL;
2159 goto out;
2160 }
2161
2162 /* constraints check */
2163 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2164 if (ret < 0)
2165 goto out;
2166
2167 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2168out:
2169 mutex_unlock(&rdev->mutex);
2170 return ret;
2171}
2172EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2173
2174static int _regulator_get_current_limit(struct regulator_dev *rdev)
2175{
2176 int ret;
2177
2178 mutex_lock(&rdev->mutex);
2179
2180 /* sanity check */
2181 if (!rdev->desc->ops->get_current_limit) {
2182 ret = -EINVAL;
2183 goto out;
2184 }
2185
2186 ret = rdev->desc->ops->get_current_limit(rdev);
2187out:
2188 mutex_unlock(&rdev->mutex);
2189 return ret;
2190}
2191
2192/**
2193 * regulator_get_current_limit - get regulator output current
2194 * @regulator: regulator source
2195 *
2196 * This returns the current supplied by the specified current sink in uA.
2197 *
2198 * NOTE: If the regulator is disabled it will return the current value. This
2199 * function should not be used to determine regulator state.
2200 */
2201int regulator_get_current_limit(struct regulator *regulator)
2202{
2203 return _regulator_get_current_limit(regulator->rdev);
2204}
2205EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2206
2207/**
2208 * regulator_set_mode - set regulator operating mode
2209 * @regulator: regulator source
2210 * @mode: operating mode - one of the REGULATOR_MODE constants
2211 *
2212 * Set regulator operating mode to increase regulator efficiency or improve
2213 * regulation performance.
2214 *
2215 * NOTE: Regulator system constraints must be set for this regulator before
2216 * calling this function otherwise this call will fail.
2217 */
2218int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2219{
2220 struct regulator_dev *rdev = regulator->rdev;
2221 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302222 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002223
2224 mutex_lock(&rdev->mutex);
2225
2226 /* sanity check */
2227 if (!rdev->desc->ops->set_mode) {
2228 ret = -EINVAL;
2229 goto out;
2230 }
2231
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302232 /* return if the same mode is requested */
2233 if (rdev->desc->ops->get_mode) {
2234 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2235 if (regulator_curr_mode == mode) {
2236 ret = 0;
2237 goto out;
2238 }
2239 }
2240
Liam Girdwood414c70c2008-04-30 15:59:04 +01002241 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002242 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002243 if (ret < 0)
2244 goto out;
2245
2246 ret = rdev->desc->ops->set_mode(rdev, mode);
2247out:
2248 mutex_unlock(&rdev->mutex);
2249 return ret;
2250}
2251EXPORT_SYMBOL_GPL(regulator_set_mode);
2252
2253static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2254{
2255 int ret;
2256
2257 mutex_lock(&rdev->mutex);
2258
2259 /* sanity check */
2260 if (!rdev->desc->ops->get_mode) {
2261 ret = -EINVAL;
2262 goto out;
2263 }
2264
2265 ret = rdev->desc->ops->get_mode(rdev);
2266out:
2267 mutex_unlock(&rdev->mutex);
2268 return ret;
2269}
2270
2271/**
2272 * regulator_get_mode - get regulator operating mode
2273 * @regulator: regulator source
2274 *
2275 * Get the current regulator operating mode.
2276 */
2277unsigned int regulator_get_mode(struct regulator *regulator)
2278{
2279 return _regulator_get_mode(regulator->rdev);
2280}
2281EXPORT_SYMBOL_GPL(regulator_get_mode);
2282
2283/**
2284 * regulator_set_optimum_mode - set regulator optimum operating mode
2285 * @regulator: regulator source
2286 * @uA_load: load current
2287 *
2288 * Notifies the regulator core of a new device load. This is then used by
2289 * DRMS (if enabled by constraints) to set the most efficient regulator
2290 * operating mode for the new regulator loading.
2291 *
2292 * Consumer devices notify their supply regulator of the maximum power
2293 * they will require (can be taken from device datasheet in the power
2294 * consumption tables) when they change operational status and hence power
2295 * state. Examples of operational state changes that can affect power
2296 * consumption are :-
2297 *
2298 * o Device is opened / closed.
2299 * o Device I/O is about to begin or has just finished.
2300 * o Device is idling in between work.
2301 *
2302 * This information is also exported via sysfs to userspace.
2303 *
2304 * DRMS will sum the total requested load on the regulator and change
2305 * to the most efficient operating mode if platform constraints allow.
2306 *
2307 * Returns the new regulator mode or error.
2308 */
2309int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2310{
2311 struct regulator_dev *rdev = regulator->rdev;
2312 struct regulator *consumer;
2313 int ret, output_uV, input_uV, total_uA_load = 0;
2314 unsigned int mode;
2315
2316 mutex_lock(&rdev->mutex);
2317
Mark Browna4b41482011-05-14 11:19:45 -07002318 /*
2319 * first check to see if we can set modes at all, otherwise just
2320 * tell the consumer everything is OK.
2321 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002322 regulator->uA_load = uA_load;
2323 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002324 if (ret < 0) {
2325 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002326 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002327 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002328
Liam Girdwood414c70c2008-04-30 15:59:04 +01002329 if (!rdev->desc->ops->get_optimum_mode)
2330 goto out;
2331
Mark Browna4b41482011-05-14 11:19:45 -07002332 /*
2333 * we can actually do this so any errors are indicators of
2334 * potential real failure.
2335 */
2336 ret = -EINVAL;
2337
Liam Girdwood414c70c2008-04-30 15:59:04 +01002338 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002339 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002340 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002341 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002342 goto out;
2343 }
2344
2345 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002346 input_uV = 0;
2347 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002348 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002349 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002350 input_uV = rdev->constraints->input_uV;
2351 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002352 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002353 goto out;
2354 }
2355
2356 /* calc total requested load for this regulator */
2357 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002358 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002359
2360 mode = rdev->desc->ops->get_optimum_mode(rdev,
2361 input_uV, output_uV,
2362 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002363 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002364 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002365 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2366 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002367 goto out;
2368 }
2369
2370 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002371 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002372 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002373 goto out;
2374 }
2375 ret = mode;
2376out:
2377 mutex_unlock(&rdev->mutex);
2378 return ret;
2379}
2380EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2381
2382/**
2383 * regulator_register_notifier - register regulator event notifier
2384 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002385 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002386 *
2387 * Register notifier block to receive regulator events.
2388 */
2389int regulator_register_notifier(struct regulator *regulator,
2390 struct notifier_block *nb)
2391{
2392 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2393 nb);
2394}
2395EXPORT_SYMBOL_GPL(regulator_register_notifier);
2396
2397/**
2398 * regulator_unregister_notifier - unregister regulator event notifier
2399 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002400 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002401 *
2402 * Unregister regulator event notifier block.
2403 */
2404int regulator_unregister_notifier(struct regulator *regulator,
2405 struct notifier_block *nb)
2406{
2407 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2408 nb);
2409}
2410EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2411
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002412/* notify regulator consumers and downstream regulator consumers.
2413 * Note mutex must be held by caller.
2414 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002415static void _notifier_call_chain(struct regulator_dev *rdev,
2416 unsigned long event, void *data)
2417{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002418 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002419 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002420}
2421
2422/**
2423 * regulator_bulk_get - get multiple regulator consumers
2424 *
2425 * @dev: Device to supply
2426 * @num_consumers: Number of consumers to register
2427 * @consumers: Configuration of consumers; clients are stored here.
2428 *
2429 * @return 0 on success, an errno on failure.
2430 *
2431 * This helper function allows drivers to get several regulator
2432 * consumers in one operation. If any of the regulators cannot be
2433 * acquired then any regulators that were allocated will be freed
2434 * before returning to the caller.
2435 */
2436int regulator_bulk_get(struct device *dev, int num_consumers,
2437 struct regulator_bulk_data *consumers)
2438{
2439 int i;
2440 int ret;
2441
2442 for (i = 0; i < num_consumers; i++)
2443 consumers[i].consumer = NULL;
2444
2445 for (i = 0; i < num_consumers; i++) {
2446 consumers[i].consumer = regulator_get(dev,
2447 consumers[i].supply);
2448 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002449 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002450 dev_err(dev, "Failed to get supply '%s': %d\n",
2451 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002452 consumers[i].consumer = NULL;
2453 goto err;
2454 }
2455 }
2456
2457 return 0;
2458
2459err:
Axel Linb29c7692012-02-20 10:32:16 +08002460 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002461 regulator_put(consumers[i].consumer);
2462
2463 return ret;
2464}
2465EXPORT_SYMBOL_GPL(regulator_bulk_get);
2466
Mark Browne6e74032012-01-20 20:10:08 +00002467/**
2468 * devm_regulator_bulk_get - managed get multiple regulator consumers
2469 *
2470 * @dev: Device to supply
2471 * @num_consumers: Number of consumers to register
2472 * @consumers: Configuration of consumers; clients are stored here.
2473 *
2474 * @return 0 on success, an errno on failure.
2475 *
2476 * This helper function allows drivers to get several regulator
2477 * consumers in one operation with management, the regulators will
2478 * automatically be freed when the device is unbound. If any of the
2479 * regulators cannot be acquired then any regulators that were
2480 * allocated will be freed before returning to the caller.
2481 */
2482int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2483 struct regulator_bulk_data *consumers)
2484{
2485 int i;
2486 int ret;
2487
2488 for (i = 0; i < num_consumers; i++)
2489 consumers[i].consumer = NULL;
2490
2491 for (i = 0; i < num_consumers; i++) {
2492 consumers[i].consumer = devm_regulator_get(dev,
2493 consumers[i].supply);
2494 if (IS_ERR(consumers[i].consumer)) {
2495 ret = PTR_ERR(consumers[i].consumer);
2496 dev_err(dev, "Failed to get supply '%s': %d\n",
2497 consumers[i].supply, ret);
2498 consumers[i].consumer = NULL;
2499 goto err;
2500 }
2501 }
2502
2503 return 0;
2504
2505err:
2506 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2507 devm_regulator_put(consumers[i].consumer);
2508
2509 return ret;
2510}
2511EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2512
Mark Brownf21e0e82011-05-24 08:12:40 +08002513static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2514{
2515 struct regulator_bulk_data *bulk = data;
2516
2517 bulk->ret = regulator_enable(bulk->consumer);
2518}
2519
Liam Girdwood414c70c2008-04-30 15:59:04 +01002520/**
2521 * regulator_bulk_enable - enable multiple regulator consumers
2522 *
2523 * @num_consumers: Number of consumers
2524 * @consumers: Consumer data; clients are stored here.
2525 * @return 0 on success, an errno on failure
2526 *
2527 * This convenience API allows consumers to enable multiple regulator
2528 * clients in a single API call. If any consumers cannot be enabled
2529 * then any others that were enabled will be disabled again prior to
2530 * return.
2531 */
2532int regulator_bulk_enable(int num_consumers,
2533 struct regulator_bulk_data *consumers)
2534{
Mark Brownf21e0e82011-05-24 08:12:40 +08002535 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002536 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002537 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002538
Mark Brownf21e0e82011-05-24 08:12:40 +08002539 for (i = 0; i < num_consumers; i++)
2540 async_schedule_domain(regulator_bulk_enable_async,
2541 &consumers[i], &async_domain);
2542
2543 async_synchronize_full_domain(&async_domain);
2544
2545 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002546 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002547 if (consumers[i].ret != 0) {
2548 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002549 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002550 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002551 }
2552
2553 return 0;
2554
2555err:
Axel Linb29c7692012-02-20 10:32:16 +08002556 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2557 while (--i >= 0)
2558 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002559
2560 return ret;
2561}
2562EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2563
2564/**
2565 * regulator_bulk_disable - disable multiple regulator consumers
2566 *
2567 * @num_consumers: Number of consumers
2568 * @consumers: Consumer data; clients are stored here.
2569 * @return 0 on success, an errno on failure
2570 *
2571 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002572 * clients in a single API call. If any consumers cannot be disabled
2573 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002574 * return.
2575 */
2576int regulator_bulk_disable(int num_consumers,
2577 struct regulator_bulk_data *consumers)
2578{
2579 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01002580 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002581
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002582 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002583 ret = regulator_disable(consumers[i].consumer);
2584 if (ret != 0)
2585 goto err;
2586 }
2587
2588 return 0;
2589
2590err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002591 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01002592 for (++i; i < num_consumers; ++i) {
2593 r = regulator_enable(consumers[i].consumer);
2594 if (r != 0)
2595 pr_err("Failed to reename %s: %d\n",
2596 consumers[i].supply, r);
2597 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002598
2599 return ret;
2600}
2601EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2602
2603/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002604 * regulator_bulk_force_disable - force disable multiple regulator consumers
2605 *
2606 * @num_consumers: Number of consumers
2607 * @consumers: Consumer data; clients are stored here.
2608 * @return 0 on success, an errno on failure
2609 *
2610 * This convenience API allows consumers to forcibly disable multiple regulator
2611 * clients in a single API call.
2612 * NOTE: This should be used for situations when device damage will
2613 * likely occur if the regulators are not disabled (e.g. over temp).
2614 * Although regulator_force_disable function call for some consumers can
2615 * return error numbers, the function is called for all consumers.
2616 */
2617int regulator_bulk_force_disable(int num_consumers,
2618 struct regulator_bulk_data *consumers)
2619{
2620 int i;
2621 int ret;
2622
2623 for (i = 0; i < num_consumers; i++)
2624 consumers[i].ret =
2625 regulator_force_disable(consumers[i].consumer);
2626
2627 for (i = 0; i < num_consumers; i++) {
2628 if (consumers[i].ret != 0) {
2629 ret = consumers[i].ret;
2630 goto out;
2631 }
2632 }
2633
2634 return 0;
2635out:
2636 return ret;
2637}
2638EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2639
2640/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002641 * regulator_bulk_free - free multiple regulator consumers
2642 *
2643 * @num_consumers: Number of consumers
2644 * @consumers: Consumer data; clients are stored here.
2645 *
2646 * This convenience API allows consumers to free multiple regulator
2647 * clients in a single API call.
2648 */
2649void regulator_bulk_free(int num_consumers,
2650 struct regulator_bulk_data *consumers)
2651{
2652 int i;
2653
2654 for (i = 0; i < num_consumers; i++) {
2655 regulator_put(consumers[i].consumer);
2656 consumers[i].consumer = NULL;
2657 }
2658}
2659EXPORT_SYMBOL_GPL(regulator_bulk_free);
2660
2661/**
2662 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002663 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002664 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002665 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002666 *
2667 * Called by regulator drivers to notify clients a regulator event has
2668 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002669 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002670 */
2671int regulator_notifier_call_chain(struct regulator_dev *rdev,
2672 unsigned long event, void *data)
2673{
2674 _notifier_call_chain(rdev, event, data);
2675 return NOTIFY_DONE;
2676
2677}
2678EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2679
Mark Brownbe721972009-08-04 20:09:52 +02002680/**
2681 * regulator_mode_to_status - convert a regulator mode into a status
2682 *
2683 * @mode: Mode to convert
2684 *
2685 * Convert a regulator mode into a status.
2686 */
2687int regulator_mode_to_status(unsigned int mode)
2688{
2689 switch (mode) {
2690 case REGULATOR_MODE_FAST:
2691 return REGULATOR_STATUS_FAST;
2692 case REGULATOR_MODE_NORMAL:
2693 return REGULATOR_STATUS_NORMAL;
2694 case REGULATOR_MODE_IDLE:
2695 return REGULATOR_STATUS_IDLE;
2696 case REGULATOR_STATUS_STANDBY:
2697 return REGULATOR_STATUS_STANDBY;
2698 default:
2699 return 0;
2700 }
2701}
2702EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2703
David Brownell7ad68e22008-11-11 17:39:02 -08002704/*
2705 * To avoid cluttering sysfs (and memory) with useless state, only
2706 * create attributes that can be meaningfully displayed.
2707 */
2708static int add_regulator_attributes(struct regulator_dev *rdev)
2709{
2710 struct device *dev = &rdev->dev;
2711 struct regulator_ops *ops = rdev->desc->ops;
2712 int status = 0;
2713
2714 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002715 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2716 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002717 status = device_create_file(dev, &dev_attr_microvolts);
2718 if (status < 0)
2719 return status;
2720 }
2721 if (ops->get_current_limit) {
2722 status = device_create_file(dev, &dev_attr_microamps);
2723 if (status < 0)
2724 return status;
2725 }
2726 if (ops->get_mode) {
2727 status = device_create_file(dev, &dev_attr_opmode);
2728 if (status < 0)
2729 return status;
2730 }
2731 if (ops->is_enabled) {
2732 status = device_create_file(dev, &dev_attr_state);
2733 if (status < 0)
2734 return status;
2735 }
David Brownell853116a2009-01-14 23:03:17 -08002736 if (ops->get_status) {
2737 status = device_create_file(dev, &dev_attr_status);
2738 if (status < 0)
2739 return status;
2740 }
David Brownell7ad68e22008-11-11 17:39:02 -08002741
2742 /* some attributes are type-specific */
2743 if (rdev->desc->type == REGULATOR_CURRENT) {
2744 status = device_create_file(dev, &dev_attr_requested_microamps);
2745 if (status < 0)
2746 return status;
2747 }
2748
2749 /* all the other attributes exist to support constraints;
2750 * don't show them if there are no constraints, or if the
2751 * relevant supporting methods are missing.
2752 */
2753 if (!rdev->constraints)
2754 return status;
2755
2756 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002757 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002758 status = device_create_file(dev, &dev_attr_min_microvolts);
2759 if (status < 0)
2760 return status;
2761 status = device_create_file(dev, &dev_attr_max_microvolts);
2762 if (status < 0)
2763 return status;
2764 }
2765 if (ops->set_current_limit) {
2766 status = device_create_file(dev, &dev_attr_min_microamps);
2767 if (status < 0)
2768 return status;
2769 status = device_create_file(dev, &dev_attr_max_microamps);
2770 if (status < 0)
2771 return status;
2772 }
2773
David Brownell7ad68e22008-11-11 17:39:02 -08002774 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2775 if (status < 0)
2776 return status;
2777 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2778 if (status < 0)
2779 return status;
2780 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2781 if (status < 0)
2782 return status;
2783
2784 if (ops->set_suspend_voltage) {
2785 status = device_create_file(dev,
2786 &dev_attr_suspend_standby_microvolts);
2787 if (status < 0)
2788 return status;
2789 status = device_create_file(dev,
2790 &dev_attr_suspend_mem_microvolts);
2791 if (status < 0)
2792 return status;
2793 status = device_create_file(dev,
2794 &dev_attr_suspend_disk_microvolts);
2795 if (status < 0)
2796 return status;
2797 }
2798
2799 if (ops->set_suspend_mode) {
2800 status = device_create_file(dev,
2801 &dev_attr_suspend_standby_mode);
2802 if (status < 0)
2803 return status;
2804 status = device_create_file(dev,
2805 &dev_attr_suspend_mem_mode);
2806 if (status < 0)
2807 return status;
2808 status = device_create_file(dev,
2809 &dev_attr_suspend_disk_mode);
2810 if (status < 0)
2811 return status;
2812 }
2813
2814 return status;
2815}
2816
Mark Brown1130e5b2010-12-21 23:49:31 +00002817static void rdev_init_debugfs(struct regulator_dev *rdev)
2818{
Mark Brown1130e5b2010-12-21 23:49:31 +00002819 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08002820 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00002821 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00002822 return;
2823 }
2824
2825 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2826 &rdev->use_count);
2827 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2828 &rdev->open_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00002829}
2830
Liam Girdwood414c70c2008-04-30 15:59:04 +01002831/**
2832 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002833 * @regulator_desc: regulator to register
2834 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002835 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002836 * @driver_data: private regulator data
Mark Brown4a7cbb52012-01-24 11:17:26 +00002837 * @of_node: OpenFirmware node to parse for device tree bindings (may be
2838 * NULL).
Liam Girdwood414c70c2008-04-30 15:59:04 +01002839 *
2840 * Called by regulator drivers to register a regulator.
2841 * Returns 0 on success.
2842 */
2843struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002844 struct device *dev, const struct regulator_init_data *init_data,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302845 void *driver_data, struct device_node *of_node)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002846{
Mark Brown9a8f5e02011-11-29 18:11:19 +00002847 const struct regulation_constraints *constraints = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002848 static atomic_t regulator_no = ATOMIC_INIT(0);
2849 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002850 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05302851 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002852
2853 if (regulator_desc == NULL)
2854 return ERR_PTR(-EINVAL);
2855
2856 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2857 return ERR_PTR(-EINVAL);
2858
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002859 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2860 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002861 return ERR_PTR(-EINVAL);
2862
Mark Brown476c2d82010-12-10 17:28:07 +00002863 /* Only one of each should be implemented */
2864 WARN_ON(regulator_desc->ops->get_voltage &&
2865 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002866 WARN_ON(regulator_desc->ops->set_voltage &&
2867 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002868
2869 /* If we're using selectors we must implement list_voltage. */
2870 if (regulator_desc->ops->get_voltage_sel &&
2871 !regulator_desc->ops->list_voltage) {
2872 return ERR_PTR(-EINVAL);
2873 }
Mark Browne8eef822010-12-12 14:36:17 +00002874 if (regulator_desc->ops->set_voltage_sel &&
2875 !regulator_desc->ops->list_voltage) {
2876 return ERR_PTR(-EINVAL);
2877 }
Mark Brown476c2d82010-12-10 17:28:07 +00002878
Liam Girdwood414c70c2008-04-30 15:59:04 +01002879 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2880 if (rdev == NULL)
2881 return ERR_PTR(-ENOMEM);
2882
2883 mutex_lock(&regulator_list_mutex);
2884
2885 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002886 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002887 rdev->owner = regulator_desc->owner;
2888 rdev->desc = regulator_desc;
2889 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002890 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002891 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01002892 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002893
Liam Girdwooda5766f12008-10-10 13:22:20 +01002894 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002895 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01002896 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002897 if (ret < 0)
2898 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002899 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002900
Liam Girdwooda5766f12008-10-10 13:22:20 +01002901 /* register with sysfs */
2902 rdev->dev.class = &regulator_class;
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05302903 rdev->dev.of_node = of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002904 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002905 dev_set_name(&rdev->dev, "regulator.%d",
2906 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002907 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002908 if (ret != 0) {
2909 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002910 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002911 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002912
2913 dev_set_drvdata(&rdev->dev, rdev);
2914
Mike Rapoport74f544c2008-11-25 14:53:53 +02002915 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002916 if (init_data)
2917 constraints = &init_data->constraints;
2918
2919 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02002920 if (ret < 0)
2921 goto scrub;
2922
David Brownell7ad68e22008-11-11 17:39:02 -08002923 /* add attributes supported by this regulator */
2924 ret = add_regulator_attributes(rdev);
2925 if (ret < 0)
2926 goto scrub;
2927
Mark Brown9a8f5e02011-11-29 18:11:19 +00002928 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05302929 supply = init_data->supply_regulator;
2930 else if (regulator_desc->supply_name)
2931 supply = regulator_desc->supply_name;
2932
2933 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002934 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01002935
Mark Brown6d191a52012-03-29 14:19:02 +01002936 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01002937
Rajendra Nayak69511a42011-11-18 16:47:20 +05302938 if (!r) {
2939 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00002940 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01002941 goto scrub;
2942 }
2943
2944 ret = set_supply(rdev, r);
2945 if (ret < 0)
2946 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05302947
2948 /* Enable supply if rail is enabled */
2949 if (rdev->desc->ops->is_enabled &&
2950 rdev->desc->ops->is_enabled(rdev)) {
2951 ret = regulator_enable(rdev->supply);
2952 if (ret < 0)
2953 goto scrub;
2954 }
Mark Brown0178f3e2010-04-26 15:18:14 +01002955 }
2956
Liam Girdwooda5766f12008-10-10 13:22:20 +01002957 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002958 if (init_data) {
2959 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2960 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00002961 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00002962 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00002963 if (ret < 0) {
2964 dev_err(dev, "Failed to set supply %s\n",
2965 init_data->consumer_supplies[i].supply);
2966 goto unset_supplies;
2967 }
Mark Brown23c2f042011-02-24 17:39:09 +00002968 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002969 }
2970
2971 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002972
2973 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002974out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002975 mutex_unlock(&regulator_list_mutex);
2976 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002977
Jani Nikulad4033b52010-04-29 10:55:11 +03002978unset_supplies:
2979 unset_regulator_supplies(rdev);
2980
David Brownell4fca9542008-11-11 17:38:53 -08002981scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08002982 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08002983 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002984 /* device core frees rdev */
2985 rdev = ERR_PTR(ret);
2986 goto out;
2987
David Brownell4fca9542008-11-11 17:38:53 -08002988clean:
2989 kfree(rdev);
2990 rdev = ERR_PTR(ret);
2991 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002992}
2993EXPORT_SYMBOL_GPL(regulator_register);
2994
2995/**
2996 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002997 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002998 *
2999 * Called by regulator drivers to unregister a regulator.
3000 */
3001void regulator_unregister(struct regulator_dev *rdev)
3002{
3003 if (rdev == NULL)
3004 return;
3005
3006 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003007 debugfs_remove_recursive(rdev->debugfs);
Mark Brownda07ecd2011-09-11 09:53:50 +01003008 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003009 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003010 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003011 list_del(&rdev->list);
3012 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01003013 regulator_put(rdev->supply);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003014 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003015 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003016 mutex_unlock(&regulator_list_mutex);
3017}
3018EXPORT_SYMBOL_GPL(regulator_unregister);
3019
3020/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003021 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003022 * @state: system suspend state
3023 *
3024 * Configure each regulator with it's suspend operating parameters for state.
3025 * This will usually be called by machine suspend code prior to supending.
3026 */
3027int regulator_suspend_prepare(suspend_state_t state)
3028{
3029 struct regulator_dev *rdev;
3030 int ret = 0;
3031
3032 /* ON is handled by regulator active state */
3033 if (state == PM_SUSPEND_ON)
3034 return -EINVAL;
3035
3036 mutex_lock(&regulator_list_mutex);
3037 list_for_each_entry(rdev, &regulator_list, list) {
3038
3039 mutex_lock(&rdev->mutex);
3040 ret = suspend_prepare(rdev, state);
3041 mutex_unlock(&rdev->mutex);
3042
3043 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003044 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003045 goto out;
3046 }
3047 }
3048out:
3049 mutex_unlock(&regulator_list_mutex);
3050 return ret;
3051}
3052EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3053
3054/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003055 * regulator_suspend_finish - resume regulators from system wide suspend
3056 *
3057 * Turn on regulators that might be turned off by regulator_suspend_prepare
3058 * and that should be turned on according to the regulators properties.
3059 */
3060int regulator_suspend_finish(void)
3061{
3062 struct regulator_dev *rdev;
3063 int ret = 0, error;
3064
3065 mutex_lock(&regulator_list_mutex);
3066 list_for_each_entry(rdev, &regulator_list, list) {
3067 struct regulator_ops *ops = rdev->desc->ops;
3068
3069 mutex_lock(&rdev->mutex);
3070 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3071 ops->enable) {
3072 error = ops->enable(rdev);
3073 if (error)
3074 ret = error;
3075 } else {
3076 if (!has_full_constraints)
3077 goto unlock;
3078 if (!ops->disable)
3079 goto unlock;
3080 if (ops->is_enabled && !ops->is_enabled(rdev))
3081 goto unlock;
3082
3083 error = ops->disable(rdev);
3084 if (error)
3085 ret = error;
3086 }
3087unlock:
3088 mutex_unlock(&rdev->mutex);
3089 }
3090 mutex_unlock(&regulator_list_mutex);
3091 return ret;
3092}
3093EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3094
3095/**
Mark Brownca725562009-03-16 19:36:34 +00003096 * regulator_has_full_constraints - the system has fully specified constraints
3097 *
3098 * Calling this function will cause the regulator API to disable all
3099 * regulators which have a zero use count and don't have an always_on
3100 * constraint in a late_initcall.
3101 *
3102 * The intention is that this will become the default behaviour in a
3103 * future kernel release so users are encouraged to use this facility
3104 * now.
3105 */
3106void regulator_has_full_constraints(void)
3107{
3108 has_full_constraints = 1;
3109}
3110EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3111
3112/**
Mark Brown688fe992010-10-05 19:18:32 -07003113 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3114 *
3115 * Calling this function will cause the regulator API to provide a
3116 * dummy regulator to consumers if no physical regulator is found,
3117 * allowing most consumers to proceed as though a regulator were
3118 * configured. This allows systems such as those with software
3119 * controllable regulators for the CPU core only to be brought up more
3120 * readily.
3121 */
3122void regulator_use_dummy_regulator(void)
3123{
3124 board_wants_dummy_regulator = true;
3125}
3126EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3127
3128/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003129 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003130 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003131 *
3132 * Get rdev regulator driver private data. This call can be used in the
3133 * regulator driver context.
3134 */
3135void *rdev_get_drvdata(struct regulator_dev *rdev)
3136{
3137 return rdev->reg_data;
3138}
3139EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3140
3141/**
3142 * regulator_get_drvdata - get regulator driver data
3143 * @regulator: regulator
3144 *
3145 * Get regulator driver private data. This call can be used in the consumer
3146 * driver context when non API regulator specific functions need to be called.
3147 */
3148void *regulator_get_drvdata(struct regulator *regulator)
3149{
3150 return regulator->rdev->reg_data;
3151}
3152EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3153
3154/**
3155 * regulator_set_drvdata - set regulator driver data
3156 * @regulator: regulator
3157 * @data: data
3158 */
3159void regulator_set_drvdata(struct regulator *regulator, void *data)
3160{
3161 regulator->rdev->reg_data = data;
3162}
3163EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3164
3165/**
3166 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003167 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003168 */
3169int rdev_get_id(struct regulator_dev *rdev)
3170{
3171 return rdev->desc->id;
3172}
3173EXPORT_SYMBOL_GPL(rdev_get_id);
3174
Liam Girdwooda5766f12008-10-10 13:22:20 +01003175struct device *rdev_get_dev(struct regulator_dev *rdev)
3176{
3177 return &rdev->dev;
3178}
3179EXPORT_SYMBOL_GPL(rdev_get_dev);
3180
3181void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3182{
3183 return reg_init_data->driver_data;
3184}
3185EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3186
Mark Brownba55a972011-08-23 17:39:10 +01003187#ifdef CONFIG_DEBUG_FS
3188static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3189 size_t count, loff_t *ppos)
3190{
3191 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3192 ssize_t len, ret = 0;
3193 struct regulator_map *map;
3194
3195 if (!buf)
3196 return -ENOMEM;
3197
3198 list_for_each_entry(map, &regulator_map_list, list) {
3199 len = snprintf(buf + ret, PAGE_SIZE - ret,
3200 "%s -> %s.%s\n",
3201 rdev_get_name(map->regulator), map->dev_name,
3202 map->supply);
3203 if (len >= 0)
3204 ret += len;
3205 if (ret > PAGE_SIZE) {
3206 ret = PAGE_SIZE;
3207 break;
3208 }
3209 }
3210
3211 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3212
3213 kfree(buf);
3214
3215 return ret;
3216}
Stephen Boyd24751432012-02-20 22:50:42 -08003217#endif
Mark Brownba55a972011-08-23 17:39:10 +01003218
3219static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003220#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003221 .read = supply_map_read_file,
3222 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003223#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003224};
Mark Brownba55a972011-08-23 17:39:10 +01003225
Liam Girdwood414c70c2008-04-30 15:59:04 +01003226static int __init regulator_init(void)
3227{
Mark Brown34abbd62010-02-12 10:18:08 +00003228 int ret;
3229
Mark Brown34abbd62010-02-12 10:18:08 +00003230 ret = class_register(&regulator_class);
3231
Mark Brown1130e5b2010-12-21 23:49:31 +00003232 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003233 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003234 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003235
Mark Brownf4d562c2012-02-20 21:01:04 +00003236 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3237 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003238
Mark Brown34abbd62010-02-12 10:18:08 +00003239 regulator_dummy_init();
3240
3241 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003242}
3243
3244/* init early to allow our consumers to complete system booting */
3245core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003246
3247static int __init regulator_init_complete(void)
3248{
3249 struct regulator_dev *rdev;
3250 struct regulator_ops *ops;
3251 struct regulation_constraints *c;
3252 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003253
3254 mutex_lock(&regulator_list_mutex);
3255
3256 /* If we have a full configuration then disable any regulators
3257 * which are not in use or always_on. This will become the
3258 * default behaviour in the future.
3259 */
3260 list_for_each_entry(rdev, &regulator_list, list) {
3261 ops = rdev->desc->ops;
3262 c = rdev->constraints;
3263
Mark Brownf25e0b42009-08-03 18:49:55 +01003264 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003265 continue;
3266
3267 mutex_lock(&rdev->mutex);
3268
3269 if (rdev->use_count)
3270 goto unlock;
3271
3272 /* If we can't read the status assume it's on. */
3273 if (ops->is_enabled)
3274 enabled = ops->is_enabled(rdev);
3275 else
3276 enabled = 1;
3277
3278 if (!enabled)
3279 goto unlock;
3280
3281 if (has_full_constraints) {
3282 /* We log since this may kill the system if it
3283 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003284 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003285 ret = ops->disable(rdev);
3286 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003287 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003288 }
3289 } else {
3290 /* The intention is that in future we will
3291 * assume that full constraints are provided
3292 * so warn even if we aren't going to do
3293 * anything here.
3294 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003295 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003296 }
3297
3298unlock:
3299 mutex_unlock(&rdev->mutex);
3300 }
3301
3302 mutex_unlock(&regulator_list_mutex);
3303
3304 return 0;
3305}
3306late_initcall(regulator_init_complete);