blob: bca1e598924378118e60d5b1fcf20b97cdc06708 [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>
Mark Brown65b19ce2012-04-15 11:16:05 +010027#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053028#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010029#include <linux/regulator/consumer.h>
30#include <linux/regulator/driver.h>
31#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040032#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010033
Mark Brown02fa3ec2010-11-10 14:38:30 +000034#define CREATE_TRACE_POINTS
35#include <trace/events/regulator.h>
36
Mark Brown34abbd62010-02-12 10:18:08 +000037#include "dummy.h"
38
Mark Brown7d51a0d2011-06-09 16:06:37 +010039#define rdev_crit(rdev, fmt, ...) \
40 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080041#define rdev_err(rdev, fmt, ...) \
42 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43#define rdev_warn(rdev, fmt, ...) \
44 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45#define rdev_info(rdev, fmt, ...) \
46 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47#define rdev_dbg(rdev, fmt, ...) \
48 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49
Liam Girdwood414c70c2008-04-30 15:59:04 +010050static DEFINE_MUTEX(regulator_list_mutex);
51static LIST_HEAD(regulator_list);
52static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000053static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070054static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010055
Mark Brown1130e5b2010-12-21 23:49:31 +000056static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000057
Mark Brown8dc53902008-12-31 12:52:40 +000058/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010059 * struct regulator_map
60 *
61 * Used to provide symbolic supply names to devices.
62 */
63struct regulator_map {
64 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010065 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010066 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010067 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010068};
69
Liam Girdwood414c70c2008-04-30 15:59:04 +010070/*
71 * struct regulator
72 *
73 * One for each consumer device.
74 */
75struct regulator {
76 struct device *dev;
77 struct list_head list;
78 int uA_load;
79 int min_uV;
80 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010081 char *supply_name;
82 struct device_attribute dev_attr;
83 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010084 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010085};
86
87static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010088static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010089static int _regulator_get_voltage(struct regulator_dev *rdev);
90static int _regulator_get_current_limit(struct regulator_dev *rdev);
91static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
92static void _notifier_call_chain(struct regulator_dev *rdev,
93 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000094static int _regulator_do_set_voltage(struct regulator_dev *rdev,
95 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010096static struct regulator *create_regulator(struct regulator_dev *rdev,
97 struct device *dev,
98 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +010099
Mark Brown1083c392009-10-22 16:31:32 +0100100static const char *rdev_get_name(struct regulator_dev *rdev)
101{
102 if (rdev->constraints && rdev->constraints->name)
103 return rdev->constraints->name;
104 else if (rdev->desc->name)
105 return rdev->desc->name;
106 else
107 return "";
108}
109
Liam Girdwood414c70c2008-04-30 15:59:04 +0100110/* gets the regulator for a given consumer device */
111static struct regulator *get_device_regulator(struct device *dev)
112{
113 struct regulator *regulator = NULL;
114 struct regulator_dev *rdev;
115
116 mutex_lock(&regulator_list_mutex);
117 list_for_each_entry(rdev, &regulator_list, list) {
118 mutex_lock(&rdev->mutex);
119 list_for_each_entry(regulator, &rdev->consumer_list, list) {
120 if (regulator->dev == dev) {
121 mutex_unlock(&rdev->mutex);
122 mutex_unlock(&regulator_list_mutex);
123 return regulator;
124 }
125 }
126 mutex_unlock(&rdev->mutex);
127 }
128 mutex_unlock(&regulator_list_mutex);
129 return NULL;
130}
131
Rajendra Nayak69511a42011-11-18 16:47:20 +0530132/**
133 * of_get_regulator - get a regulator device node based on supply name
134 * @dev: Device pointer for the consumer (of regulator) device
135 * @supply: regulator supply name
136 *
137 * Extract the regulator device node corresponding to the supply name.
138 * retruns the device node corresponding to the regulator if found, else
139 * returns NULL.
140 */
141static struct device_node *of_get_regulator(struct device *dev, const char *supply)
142{
143 struct device_node *regnode = NULL;
144 char prop_name[32]; /* 32 is max size of property name */
145
146 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
147
148 snprintf(prop_name, 32, "%s-supply", supply);
149 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
150
151 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530152 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530153 prop_name, dev->of_node->full_name);
154 return NULL;
155 }
156 return regnode;
157}
158
Liam Girdwood414c70c2008-04-30 15:59:04 +0100159/* Platform voltage constraint check */
160static int regulator_check_voltage(struct regulator_dev *rdev,
161 int *min_uV, int *max_uV)
162{
163 BUG_ON(*min_uV > *max_uV);
164
165 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800166 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100167 return -ENODEV;
168 }
169 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800170 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100171 return -EPERM;
172 }
173
174 if (*max_uV > rdev->constraints->max_uV)
175 *max_uV = rdev->constraints->max_uV;
176 if (*min_uV < rdev->constraints->min_uV)
177 *min_uV = rdev->constraints->min_uV;
178
Mark Brown89f425e2011-07-12 11:20:37 +0900179 if (*min_uV > *max_uV) {
180 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100181 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100182 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900183 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100184
185 return 0;
186}
187
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100188/* Make sure we select a voltage that suits the needs of all
189 * regulator consumers
190 */
191static int regulator_check_consumers(struct regulator_dev *rdev,
192 int *min_uV, int *max_uV)
193{
194 struct regulator *regulator;
195
196 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700197 /*
198 * Assume consumers that didn't say anything are OK
199 * with anything in the constraint range.
200 */
201 if (!regulator->min_uV && !regulator->max_uV)
202 continue;
203
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100204 if (*max_uV > regulator->max_uV)
205 *max_uV = regulator->max_uV;
206 if (*min_uV < regulator->min_uV)
207 *min_uV = regulator->min_uV;
208 }
209
210 if (*min_uV > *max_uV)
211 return -EINVAL;
212
213 return 0;
214}
215
Liam Girdwood414c70c2008-04-30 15:59:04 +0100216/* current constraint check */
217static int regulator_check_current_limit(struct regulator_dev *rdev,
218 int *min_uA, int *max_uA)
219{
220 BUG_ON(*min_uA > *max_uA);
221
222 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800223 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100224 return -ENODEV;
225 }
226 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800227 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100228 return -EPERM;
229 }
230
231 if (*max_uA > rdev->constraints->max_uA)
232 *max_uA = rdev->constraints->max_uA;
233 if (*min_uA < rdev->constraints->min_uA)
234 *min_uA = rdev->constraints->min_uA;
235
Mark Brown89f425e2011-07-12 11:20:37 +0900236 if (*min_uA > *max_uA) {
237 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100238 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100239 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900240 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100241
242 return 0;
243}
244
245/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900246static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100247{
Mark Brown2c608232011-03-30 06:29:12 +0900248 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800249 case REGULATOR_MODE_FAST:
250 case REGULATOR_MODE_NORMAL:
251 case REGULATOR_MODE_IDLE:
252 case REGULATOR_MODE_STANDBY:
253 break;
254 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900255 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800256 return -EINVAL;
257 }
258
Liam Girdwood414c70c2008-04-30 15:59:04 +0100259 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800260 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100261 return -ENODEV;
262 }
263 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800264 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100265 return -EPERM;
266 }
Mark Brown2c608232011-03-30 06:29:12 +0900267
268 /* The modes are bitmasks, the most power hungry modes having
269 * the lowest values. If the requested mode isn't supported
270 * try higher modes. */
271 while (*mode) {
272 if (rdev->constraints->valid_modes_mask & *mode)
273 return 0;
274 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100275 }
Mark Brown2c608232011-03-30 06:29:12 +0900276
277 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100278}
279
280/* dynamic regulator mode switching constraint check */
281static int regulator_check_drms(struct regulator_dev *rdev)
282{
283 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800284 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100285 return -ENODEV;
286 }
287 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800288 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289 return -EPERM;
290 }
291 return 0;
292}
293
294static ssize_t device_requested_uA_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
296{
297 struct regulator *regulator;
298
299 regulator = get_device_regulator(dev);
300 if (regulator == NULL)
301 return 0;
302
303 return sprintf(buf, "%d\n", regulator->uA_load);
304}
305
306static ssize_t regulator_uV_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
308{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100309 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100310 ssize_t ret;
311
312 mutex_lock(&rdev->mutex);
313 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
314 mutex_unlock(&rdev->mutex);
315
316 return ret;
317}
David Brownell7ad68e22008-11-11 17:39:02 -0800318static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100319
320static ssize_t regulator_uA_show(struct device *dev,
321 struct device_attribute *attr, char *buf)
322{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100323 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100324
325 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
326}
David Brownell7ad68e22008-11-11 17:39:02 -0800327static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100328
Mark Brownbc558a62008-10-10 15:33:20 +0100329static ssize_t regulator_name_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
331{
332 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100333
Mark Brown1083c392009-10-22 16:31:32 +0100334 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100335}
336
David Brownell4fca9542008-11-11 17:38:53 -0800337static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100338{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100339 switch (mode) {
340 case REGULATOR_MODE_FAST:
341 return sprintf(buf, "fast\n");
342 case REGULATOR_MODE_NORMAL:
343 return sprintf(buf, "normal\n");
344 case REGULATOR_MODE_IDLE:
345 return sprintf(buf, "idle\n");
346 case REGULATOR_MODE_STANDBY:
347 return sprintf(buf, "standby\n");
348 }
349 return sprintf(buf, "unknown\n");
350}
351
David Brownell4fca9542008-11-11 17:38:53 -0800352static ssize_t regulator_opmode_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100354{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100355 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100356
David Brownell4fca9542008-11-11 17:38:53 -0800357 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
358}
David Brownell7ad68e22008-11-11 17:39:02 -0800359static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800360
361static ssize_t regulator_print_state(char *buf, int state)
362{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100363 if (state > 0)
364 return sprintf(buf, "enabled\n");
365 else if (state == 0)
366 return sprintf(buf, "disabled\n");
367 else
368 return sprintf(buf, "unknown\n");
369}
370
David Brownell4fca9542008-11-11 17:38:53 -0800371static ssize_t regulator_state_show(struct device *dev,
372 struct device_attribute *attr, char *buf)
373{
374 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100375 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800376
Mark Brown93325462009-08-03 18:49:56 +0100377 mutex_lock(&rdev->mutex);
378 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
379 mutex_unlock(&rdev->mutex);
380
381 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800382}
David Brownell7ad68e22008-11-11 17:39:02 -0800383static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800384
David Brownell853116a2009-01-14 23:03:17 -0800385static ssize_t regulator_status_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
387{
388 struct regulator_dev *rdev = dev_get_drvdata(dev);
389 int status;
390 char *label;
391
392 status = rdev->desc->ops->get_status(rdev);
393 if (status < 0)
394 return status;
395
396 switch (status) {
397 case REGULATOR_STATUS_OFF:
398 label = "off";
399 break;
400 case REGULATOR_STATUS_ON:
401 label = "on";
402 break;
403 case REGULATOR_STATUS_ERROR:
404 label = "error";
405 break;
406 case REGULATOR_STATUS_FAST:
407 label = "fast";
408 break;
409 case REGULATOR_STATUS_NORMAL:
410 label = "normal";
411 break;
412 case REGULATOR_STATUS_IDLE:
413 label = "idle";
414 break;
415 case REGULATOR_STATUS_STANDBY:
416 label = "standby";
417 break;
418 default:
419 return -ERANGE;
420 }
421
422 return sprintf(buf, "%s\n", label);
423}
424static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
425
Liam Girdwood414c70c2008-04-30 15:59:04 +0100426static ssize_t regulator_min_uA_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
428{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100429 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100430
431 if (!rdev->constraints)
432 return sprintf(buf, "constraint not defined\n");
433
434 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
435}
David Brownell7ad68e22008-11-11 17:39:02 -0800436static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100437
438static ssize_t regulator_max_uA_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
440{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100441 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
443 if (!rdev->constraints)
444 return sprintf(buf, "constraint not defined\n");
445
446 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
447}
David Brownell7ad68e22008-11-11 17:39:02 -0800448static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100449
450static ssize_t regulator_min_uV_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100453 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454
455 if (!rdev->constraints)
456 return sprintf(buf, "constraint not defined\n");
457
458 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
459}
David Brownell7ad68e22008-11-11 17:39:02 -0800460static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461
462static ssize_t regulator_max_uV_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
464{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100465 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466
467 if (!rdev->constraints)
468 return sprintf(buf, "constraint not defined\n");
469
470 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
471}
David Brownell7ad68e22008-11-11 17:39:02 -0800472static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100473
474static ssize_t regulator_total_uA_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
476{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100477 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100478 struct regulator *regulator;
479 int uA = 0;
480
481 mutex_lock(&rdev->mutex);
482 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100483 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100484 mutex_unlock(&rdev->mutex);
485 return sprintf(buf, "%d\n", uA);
486}
David Brownell7ad68e22008-11-11 17:39:02 -0800487static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488
489static ssize_t regulator_num_users_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
491{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100492 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493 return sprintf(buf, "%d\n", rdev->use_count);
494}
495
496static ssize_t regulator_type_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
498{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100499 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500
501 switch (rdev->desc->type) {
502 case REGULATOR_VOLTAGE:
503 return sprintf(buf, "voltage\n");
504 case REGULATOR_CURRENT:
505 return sprintf(buf, "current\n");
506 }
507 return sprintf(buf, "unknown\n");
508}
509
510static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
512{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100513 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
516}
David Brownell7ad68e22008-11-11 17:39:02 -0800517static DEVICE_ATTR(suspend_mem_microvolts, 0444,
518 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100519
520static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
521 struct device_attribute *attr, char *buf)
522{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100523 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524
Liam Girdwood414c70c2008-04-30 15:59:04 +0100525 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
526}
David Brownell7ad68e22008-11-11 17:39:02 -0800527static DEVICE_ATTR(suspend_disk_microvolts, 0444,
528 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529
530static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100533 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
536}
David Brownell7ad68e22008-11-11 17:39:02 -0800537static DEVICE_ATTR(suspend_standby_microvolts, 0444,
538 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100543 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544
David Brownell4fca9542008-11-11 17:38:53 -0800545 return regulator_print_opmode(buf,
546 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547}
David Brownell7ad68e22008-11-11 17:39:02 -0800548static DEVICE_ATTR(suspend_mem_mode, 0444,
549 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100550
551static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100554 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555
David Brownell4fca9542008-11-11 17:38:53 -0800556 return regulator_print_opmode(buf,
557 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558}
David Brownell7ad68e22008-11-11 17:39:02 -0800559static DEVICE_ATTR(suspend_disk_mode, 0444,
560 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561
562static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
563 struct device_attribute *attr, char *buf)
564{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100565 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566
David Brownell4fca9542008-11-11 17:38:53 -0800567 return regulator_print_opmode(buf,
568 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569}
David Brownell7ad68e22008-11-11 17:39:02 -0800570static DEVICE_ATTR(suspend_standby_mode, 0444,
571 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572
573static ssize_t regulator_suspend_mem_state_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
575{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100576 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577
David Brownell4fca9542008-11-11 17:38:53 -0800578 return regulator_print_state(buf,
579 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580}
David Brownell7ad68e22008-11-11 17:39:02 -0800581static DEVICE_ATTR(suspend_mem_state, 0444,
582 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583
584static ssize_t regulator_suspend_disk_state_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
586{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100587 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588
David Brownell4fca9542008-11-11 17:38:53 -0800589 return regulator_print_state(buf,
590 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591}
David Brownell7ad68e22008-11-11 17:39:02 -0800592static DEVICE_ATTR(suspend_disk_state, 0444,
593 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100594
595static ssize_t regulator_suspend_standby_state_show(struct device *dev,
596 struct device_attribute *attr, char *buf)
597{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100598 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100599
David Brownell4fca9542008-11-11 17:38:53 -0800600 return regulator_print_state(buf,
601 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100602}
David Brownell7ad68e22008-11-11 17:39:02 -0800603static DEVICE_ATTR(suspend_standby_state, 0444,
604 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100605
David Brownell7ad68e22008-11-11 17:39:02 -0800606
607/*
608 * These are the only attributes are present for all regulators.
609 * Other attributes are a function of regulator functionality.
610 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100611static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100612 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100613 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
614 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615 __ATTR_NULL,
616};
617
618static void regulator_dev_release(struct device *dev)
619{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100620 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100621 kfree(rdev);
622}
623
624static struct class regulator_class = {
625 .name = "regulator",
626 .dev_release = regulator_dev_release,
627 .dev_attrs = regulator_dev_attrs,
628};
629
630/* Calculate the new optimum regulator operating mode based on the new total
631 * consumer load. All locks held by caller */
632static void drms_uA_update(struct regulator_dev *rdev)
633{
634 struct regulator *sibling;
635 int current_uA = 0, output_uV, input_uV, err;
636 unsigned int mode;
637
638 err = regulator_check_drms(rdev);
639 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000640 (!rdev->desc->ops->get_voltage &&
641 !rdev->desc->ops->get_voltage_sel) ||
642 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300643 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100644
645 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000646 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100647 if (output_uV <= 0)
648 return;
649
650 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000651 input_uV = 0;
652 if (rdev->supply)
653 input_uV = _regulator_get_voltage(rdev);
654 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100655 input_uV = rdev->constraints->input_uV;
656 if (input_uV <= 0)
657 return;
658
659 /* calc total requested load */
660 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100661 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100662
663 /* now get the optimum mode for our new total regulator load */
664 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
665 output_uV, current_uA);
666
667 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900668 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100669 if (err == 0)
670 rdev->desc->ops->set_mode(rdev, mode);
671}
672
673static int suspend_set_state(struct regulator_dev *rdev,
674 struct regulator_state *rstate)
675{
676 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100677 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100678
Mark Brown638f85c2009-10-22 16:31:33 +0100679 can_set_state = rdev->desc->ops->set_suspend_enable &&
680 rdev->desc->ops->set_suspend_disable;
681
682 /* If we have no suspend mode configration don't set anything;
683 * only warn if the driver actually makes the suspend mode
684 * configurable.
685 */
686 if (!rstate->enabled && !rstate->disabled) {
687 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800688 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100689 return 0;
690 }
691
692 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800693 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100694 return -EINVAL;
695 }
696
697 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800698 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100699 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100700 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100701
702 if (rstate->enabled)
703 ret = rdev->desc->ops->set_suspend_enable(rdev);
704 else
705 ret = rdev->desc->ops->set_suspend_disable(rdev);
706 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800707 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100708 return ret;
709 }
710
711 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
712 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
713 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800714 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100715 return ret;
716 }
717 }
718
719 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
720 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
721 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800722 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100723 return ret;
724 }
725 }
726 return ret;
727}
728
729/* locks held by caller */
730static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
731{
732 if (!rdev->constraints)
733 return -EINVAL;
734
735 switch (state) {
736 case PM_SUSPEND_STANDBY:
737 return suspend_set_state(rdev,
738 &rdev->constraints->state_standby);
739 case PM_SUSPEND_MEM:
740 return suspend_set_state(rdev,
741 &rdev->constraints->state_mem);
742 case PM_SUSPEND_MAX:
743 return suspend_set_state(rdev,
744 &rdev->constraints->state_disk);
745 default:
746 return -EINVAL;
747 }
748}
749
750static void print_constraints(struct regulator_dev *rdev)
751{
752 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000753 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100754 int count = 0;
755 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100756
Mark Brown8f031b42009-10-22 16:31:31 +0100757 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100758 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100759 count += sprintf(buf + count, "%d mV ",
760 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100761 else
Mark Brown8f031b42009-10-22 16:31:31 +0100762 count += sprintf(buf + count, "%d <--> %d mV ",
763 constraints->min_uV / 1000,
764 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100765 }
Mark Brown8f031b42009-10-22 16:31:31 +0100766
767 if (!constraints->min_uV ||
768 constraints->min_uV != constraints->max_uV) {
769 ret = _regulator_get_voltage(rdev);
770 if (ret > 0)
771 count += sprintf(buf + count, "at %d mV ", ret / 1000);
772 }
773
Mark Brownbf5892a2011-05-08 22:13:37 +0100774 if (constraints->uV_offset)
775 count += sprintf(buf, "%dmV offset ",
776 constraints->uV_offset / 1000);
777
Mark Brown8f031b42009-10-22 16:31:31 +0100778 if (constraints->min_uA && constraints->max_uA) {
779 if (constraints->min_uA == constraints->max_uA)
780 count += sprintf(buf + count, "%d mA ",
781 constraints->min_uA / 1000);
782 else
783 count += sprintf(buf + count, "%d <--> %d mA ",
784 constraints->min_uA / 1000,
785 constraints->max_uA / 1000);
786 }
787
788 if (!constraints->min_uA ||
789 constraints->min_uA != constraints->max_uA) {
790 ret = _regulator_get_current_limit(rdev);
791 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400792 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100793 }
794
Liam Girdwood414c70c2008-04-30 15:59:04 +0100795 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
796 count += sprintf(buf + count, "fast ");
797 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
798 count += sprintf(buf + count, "normal ");
799 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
800 count += sprintf(buf + count, "idle ");
801 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
802 count += sprintf(buf + count, "standby");
803
Mark Brown13ce29f2010-12-17 16:04:12 +0000804 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000805
806 if ((constraints->min_uV != constraints->max_uV) &&
807 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
808 rdev_warn(rdev,
809 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100810}
811
Mark Browne79055d2009-10-19 15:53:50 +0100812static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100813 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100814{
815 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100816 int ret;
817
818 /* do we need to apply the constraint voltage */
819 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000820 rdev->constraints->min_uV == rdev->constraints->max_uV) {
821 ret = _regulator_do_set_voltage(rdev,
822 rdev->constraints->min_uV,
823 rdev->constraints->max_uV);
824 if (ret < 0) {
825 rdev_err(rdev, "failed to apply %duV constraint\n",
826 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000827 return ret;
828 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100829 }
Mark Browne79055d2009-10-19 15:53:50 +0100830
831 /* constrain machine-level voltage specs to fit
832 * the actual range supported by this regulator.
833 */
834 if (ops->list_voltage && rdev->desc->n_voltages) {
835 int count = rdev->desc->n_voltages;
836 int i;
837 int min_uV = INT_MAX;
838 int max_uV = INT_MIN;
839 int cmin = constraints->min_uV;
840 int cmax = constraints->max_uV;
841
842 /* it's safe to autoconfigure fixed-voltage supplies
843 and the constraints are used by list_voltage. */
844 if (count == 1 && !cmin) {
845 cmin = 1;
846 cmax = INT_MAX;
847 constraints->min_uV = cmin;
848 constraints->max_uV = cmax;
849 }
850
851 /* voltage constraints are optional */
852 if ((cmin == 0) && (cmax == 0))
853 return 0;
854
855 /* else require explicit machine-level constraints */
856 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800857 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100858 return -EINVAL;
859 }
860
861 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
862 for (i = 0; i < count; i++) {
863 int value;
864
865 value = ops->list_voltage(rdev, i);
866 if (value <= 0)
867 continue;
868
869 /* maybe adjust [min_uV..max_uV] */
870 if (value >= cmin && value < min_uV)
871 min_uV = value;
872 if (value <= cmax && value > max_uV)
873 max_uV = value;
874 }
875
876 /* final: [min_uV..max_uV] valid iff constraints valid */
877 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800878 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100879 return -EINVAL;
880 }
881
882 /* use regulator's subset of machine constraints */
883 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800884 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
885 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100886 constraints->min_uV = min_uV;
887 }
888 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800889 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
890 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100891 constraints->max_uV = max_uV;
892 }
893 }
894
895 return 0;
896}
897
Liam Girdwooda5766f12008-10-10 13:22:20 +0100898/**
899 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000900 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000901 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100902 *
903 * Allows platform initialisation code to define and constrain
904 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
905 * Constraints *must* be set by platform code in order for some
906 * regulator operations to proceed i.e. set_voltage, set_current_limit,
907 * set_mode.
908 */
909static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000910 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100911{
912 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100913 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100914
Mark Brown9a8f5e02011-11-29 18:11:19 +0000915 if (constraints)
916 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
917 GFP_KERNEL);
918 else
919 rdev->constraints = kzalloc(sizeof(*constraints),
920 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000921 if (!rdev->constraints)
922 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100923
Mark Brownf8c12fe2010-11-29 15:55:17 +0000924 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100925 if (ret != 0)
926 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800927
Liam Girdwooda5766f12008-10-10 13:22:20 +0100928 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000929 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000930 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100931 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800932 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100933 goto out;
934 }
935 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100936
Mark Brown9a8f5e02011-11-29 18:11:19 +0000937 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000938 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800939 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000940 ret = -EINVAL;
941 goto out;
942 }
943
Mark Brownf8c12fe2010-11-29 15:55:17 +0000944 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000945 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800946 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000947 goto out;
948 }
949 }
950
Mark Browncacf90f2009-03-02 16:32:46 +0000951 /* If the constraints say the regulator should be on at this point
952 * and we have control then make sure it is enabled.
953 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000954 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
955 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100956 ret = ops->enable(rdev);
957 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800958 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100959 goto out;
960 }
961 }
962
Liam Girdwooda5766f12008-10-10 13:22:20 +0100963 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800964 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100965out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800966 kfree(rdev->constraints);
967 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100968 return ret;
969}
970
971/**
972 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000973 * @rdev: regulator name
974 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100975 *
976 * Called by platform initialisation code to set the supply regulator for this
977 * regulator. This ensures that a regulators supply will also be enabled by the
978 * core if it's child is enabled.
979 */
980static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100981 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100982{
983 int err;
984
Mark Brown3801b862011-06-09 16:22:22 +0100985 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
986
987 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +0800988 if (rdev->supply == NULL) {
989 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +0100990 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100991 }
Mark Brown3801b862011-06-09 16:22:22 +0100992
993 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100994}
995
996/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800997 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000998 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +0100999 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001000 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001001 *
1002 * Allows platform initialisation code to map physical regulator
1003 * sources to symbolic names for supplies for use by devices. Devices
1004 * should use these symbolic names to request regulators, avoiding the
1005 * need to provide board-specific regulator names as platform data.
1006 */
1007static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001008 const char *consumer_dev_name,
1009 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001010{
1011 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001012 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001013
1014 if (supply == NULL)
1015 return -EINVAL;
1016
Mark Brown9ed20992009-07-21 16:00:26 +01001017 if (consumer_dev_name != NULL)
1018 has_dev = 1;
1019 else
1020 has_dev = 0;
1021
David Brownell6001e132008-12-31 12:54:19 +00001022 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001023 if (node->dev_name && consumer_dev_name) {
1024 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1025 continue;
1026 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001027 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001028 }
1029
David Brownell6001e132008-12-31 12:54:19 +00001030 if (strcmp(node->supply, supply) != 0)
1031 continue;
1032
Mark Brown737f3602012-02-02 00:10:51 +00001033 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1034 consumer_dev_name,
1035 dev_name(&node->regulator->dev),
1036 node->regulator->desc->name,
1037 supply,
1038 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001039 return -EBUSY;
1040 }
1041
Mark Brown9ed20992009-07-21 16:00:26 +01001042 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001043 if (node == NULL)
1044 return -ENOMEM;
1045
1046 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001047 node->supply = supply;
1048
Mark Brown9ed20992009-07-21 16:00:26 +01001049 if (has_dev) {
1050 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1051 if (node->dev_name == NULL) {
1052 kfree(node);
1053 return -ENOMEM;
1054 }
Mark Brown40f92442009-06-17 17:56:39 +01001055 }
1056
Liam Girdwooda5766f12008-10-10 13:22:20 +01001057 list_add(&node->list, &regulator_map_list);
1058 return 0;
1059}
1060
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001061static void unset_regulator_supplies(struct regulator_dev *rdev)
1062{
1063 struct regulator_map *node, *n;
1064
1065 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1066 if (rdev == node->regulator) {
1067 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001068 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001069 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001070 }
1071 }
1072}
1073
Mark Brownf5726ae2011-06-09 16:22:20 +01001074#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001075
1076static struct regulator *create_regulator(struct regulator_dev *rdev,
1077 struct device *dev,
1078 const char *supply_name)
1079{
1080 struct regulator *regulator;
1081 char buf[REG_STR_SIZE];
1082 int err, size;
1083
1084 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1085 if (regulator == NULL)
1086 return NULL;
1087
1088 mutex_lock(&rdev->mutex);
1089 regulator->rdev = rdev;
1090 list_add(&regulator->list, &rdev->consumer_list);
1091
1092 if (dev) {
1093 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001094 size = scnprintf(buf, REG_STR_SIZE,
1095 "microamps_requested_%s-%s",
1096 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001097 if (size >= REG_STR_SIZE)
1098 goto overflow_err;
1099
1100 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001101 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001102 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1103 if (regulator->dev_attr.attr.name == NULL)
1104 goto attr_name_err;
1105
Liam Girdwood414c70c2008-04-30 15:59:04 +01001106 regulator->dev_attr.attr.mode = 0444;
1107 regulator->dev_attr.show = device_requested_uA_show;
1108 err = device_create_file(dev, &regulator->dev_attr);
1109 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001110 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001111 goto attr_name_err;
1112 }
1113
1114 /* also add a link to the device sysfs entry */
1115 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1116 dev->kobj.name, supply_name);
1117 if (size >= REG_STR_SIZE)
1118 goto attr_err;
1119
1120 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1121 if (regulator->supply_name == NULL)
1122 goto attr_err;
1123
1124 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1125 buf);
1126 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001127 rdev_warn(rdev, "could not add device link %s err %d\n",
1128 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001129 goto link_name_err;
1130 }
Mark Brown5de70512011-06-19 13:33:16 +01001131 } else {
1132 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1133 if (regulator->supply_name == NULL)
1134 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001135 }
Mark Brown5de70512011-06-19 13:33:16 +01001136
Mark Brown5de70512011-06-19 13:33:16 +01001137 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1138 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001139 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001140 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001141 } else {
1142 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1143 &regulator->uA_load);
1144 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1145 &regulator->min_uV);
1146 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1147 &regulator->max_uV);
1148 }
Mark Brown5de70512011-06-19 13:33:16 +01001149
Liam Girdwood414c70c2008-04-30 15:59:04 +01001150 mutex_unlock(&rdev->mutex);
1151 return regulator;
1152link_name_err:
1153 kfree(regulator->supply_name);
1154attr_err:
1155 device_remove_file(regulator->dev, &regulator->dev_attr);
1156attr_name_err:
1157 kfree(regulator->dev_attr.attr.name);
1158overflow_err:
1159 list_del(&regulator->list);
1160 kfree(regulator);
1161 mutex_unlock(&rdev->mutex);
1162 return NULL;
1163}
1164
Mark Brown31aae2b2009-12-21 12:21:52 +00001165static int _regulator_get_enable_time(struct regulator_dev *rdev)
1166{
1167 if (!rdev->desc->ops->enable_time)
1168 return 0;
1169 return rdev->desc->ops->enable_time(rdev);
1170}
1171
Rajendra Nayak69511a42011-11-18 16:47:20 +05301172static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1173 const char *supply)
1174{
1175 struct regulator_dev *r;
1176 struct device_node *node;
1177
1178 /* first do a dt based lookup */
1179 if (dev && dev->of_node) {
1180 node = of_get_regulator(dev, supply);
1181 if (node)
1182 list_for_each_entry(r, &regulator_list, list)
1183 if (r->dev.parent &&
1184 node == r->dev.of_node)
1185 return r;
1186 }
1187
1188 /* if not found, try doing it non-dt way */
1189 list_for_each_entry(r, &regulator_list, list)
1190 if (strcmp(rdev_get_name(r), supply) == 0)
1191 return r;
1192
1193 return NULL;
1194}
1195
Mark Brown5ffbd132009-07-21 16:00:23 +01001196/* Internal regulator request function */
1197static struct regulator *_regulator_get(struct device *dev, const char *id,
1198 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001199{
1200 struct regulator_dev *rdev;
1201 struct regulator_map *map;
Mark Brown04bf3012012-03-11 13:07:56 +00001202 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001203 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001204 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001205
1206 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001207 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001208 return regulator;
1209 }
1210
Mark Brown40f92442009-06-17 17:56:39 +01001211 if (dev)
1212 devname = dev_name(dev);
1213
Liam Girdwood414c70c2008-04-30 15:59:04 +01001214 mutex_lock(&regulator_list_mutex);
1215
Rajendra Nayak69511a42011-11-18 16:47:20 +05301216 rdev = regulator_dev_lookup(dev, id);
1217 if (rdev)
1218 goto found;
1219
Liam Girdwood414c70c2008-04-30 15:59:04 +01001220 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001221 /* If the mapping has a device set up it must match */
1222 if (map->dev_name &&
1223 (!devname || strcmp(map->dev_name, devname)))
1224 continue;
1225
1226 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001227 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001228 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001229 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001230 }
Mark Brown34abbd62010-02-12 10:18:08 +00001231
Mark Brown688fe992010-10-05 19:18:32 -07001232 if (board_wants_dummy_regulator) {
1233 rdev = dummy_regulator_rdev;
1234 goto found;
1235 }
1236
Mark Brown34abbd62010-02-12 10:18:08 +00001237#ifdef CONFIG_REGULATOR_DUMMY
1238 if (!devname)
1239 devname = "deviceless";
1240
1241 /* If the board didn't flag that it was fully constrained then
1242 * substitute in a dummy regulator so consumers can continue.
1243 */
1244 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001245 pr_warn("%s supply %s not found, using dummy regulator\n",
1246 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001247 rdev = dummy_regulator_rdev;
1248 goto found;
1249 }
1250#endif
1251
Liam Girdwood414c70c2008-04-30 15:59:04 +01001252 mutex_unlock(&regulator_list_mutex);
1253 return regulator;
1254
1255found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001256 if (rdev->exclusive) {
1257 regulator = ERR_PTR(-EPERM);
1258 goto out;
1259 }
1260
1261 if (exclusive && rdev->open_count) {
1262 regulator = ERR_PTR(-EBUSY);
1263 goto out;
1264 }
1265
Liam Girdwooda5766f12008-10-10 13:22:20 +01001266 if (!try_module_get(rdev->owner))
1267 goto out;
1268
Liam Girdwood414c70c2008-04-30 15:59:04 +01001269 regulator = create_regulator(rdev, dev, id);
1270 if (regulator == NULL) {
1271 regulator = ERR_PTR(-ENOMEM);
1272 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001273 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001274 }
1275
Mark Brown5ffbd132009-07-21 16:00:23 +01001276 rdev->open_count++;
1277 if (exclusive) {
1278 rdev->exclusive = 1;
1279
1280 ret = _regulator_is_enabled(rdev);
1281 if (ret > 0)
1282 rdev->use_count = 1;
1283 else
1284 rdev->use_count = 0;
1285 }
1286
Liam Girdwooda5766f12008-10-10 13:22:20 +01001287out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001288 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001289
Liam Girdwood414c70c2008-04-30 15:59:04 +01001290 return regulator;
1291}
Mark Brown5ffbd132009-07-21 16:00:23 +01001292
1293/**
1294 * regulator_get - lookup and obtain a reference to a regulator.
1295 * @dev: device for regulator "consumer"
1296 * @id: Supply name or regulator ID.
1297 *
1298 * Returns a struct regulator corresponding to the regulator producer,
1299 * or IS_ERR() condition containing errno.
1300 *
1301 * Use of supply names configured via regulator_set_device_supply() is
1302 * strongly encouraged. It is recommended that the supply name used
1303 * should match the name used for the supply and/or the relevant
1304 * device pins in the datasheet.
1305 */
1306struct regulator *regulator_get(struct device *dev, const char *id)
1307{
1308 return _regulator_get(dev, id, 0);
1309}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001310EXPORT_SYMBOL_GPL(regulator_get);
1311
Stephen Boyd070b9072012-01-16 19:39:58 -08001312static void devm_regulator_release(struct device *dev, void *res)
1313{
1314 regulator_put(*(struct regulator **)res);
1315}
1316
1317/**
1318 * devm_regulator_get - Resource managed regulator_get()
1319 * @dev: device for regulator "consumer"
1320 * @id: Supply name or regulator ID.
1321 *
1322 * Managed regulator_get(). Regulators returned from this function are
1323 * automatically regulator_put() on driver detach. See regulator_get() for more
1324 * information.
1325 */
1326struct regulator *devm_regulator_get(struct device *dev, const char *id)
1327{
1328 struct regulator **ptr, *regulator;
1329
1330 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1331 if (!ptr)
1332 return ERR_PTR(-ENOMEM);
1333
1334 regulator = regulator_get(dev, id);
1335 if (!IS_ERR(regulator)) {
1336 *ptr = regulator;
1337 devres_add(dev, ptr);
1338 } else {
1339 devres_free(ptr);
1340 }
1341
1342 return regulator;
1343}
1344EXPORT_SYMBOL_GPL(devm_regulator_get);
1345
Liam Girdwood414c70c2008-04-30 15:59:04 +01001346/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001347 * regulator_get_exclusive - obtain exclusive access to a regulator.
1348 * @dev: device for regulator "consumer"
1349 * @id: Supply name or regulator ID.
1350 *
1351 * Returns a struct regulator corresponding to the regulator producer,
1352 * or IS_ERR() condition containing errno. Other consumers will be
1353 * unable to obtain this reference is held and the use count for the
1354 * regulator will be initialised to reflect the current state of the
1355 * regulator.
1356 *
1357 * This is intended for use by consumers which cannot tolerate shared
1358 * use of the regulator such as those which need to force the
1359 * regulator off for correct operation of the hardware they are
1360 * controlling.
1361 *
1362 * Use of supply names configured via regulator_set_device_supply() is
1363 * strongly encouraged. It is recommended that the supply name used
1364 * should match the name used for the supply and/or the relevant
1365 * device pins in the datasheet.
1366 */
1367struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1368{
1369 return _regulator_get(dev, id, 1);
1370}
1371EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1372
1373/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001374 * regulator_put - "free" the regulator source
1375 * @regulator: regulator source
1376 *
1377 * Note: drivers must ensure that all regulator_enable calls made on this
1378 * regulator source are balanced by regulator_disable calls prior to calling
1379 * this function.
1380 */
1381void regulator_put(struct regulator *regulator)
1382{
1383 struct regulator_dev *rdev;
1384
1385 if (regulator == NULL || IS_ERR(regulator))
1386 return;
1387
Liam Girdwood414c70c2008-04-30 15:59:04 +01001388 mutex_lock(&regulator_list_mutex);
1389 rdev = regulator->rdev;
1390
Mark Brown5de70512011-06-19 13:33:16 +01001391 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001392
Liam Girdwood414c70c2008-04-30 15:59:04 +01001393 /* remove any sysfs entries */
1394 if (regulator->dev) {
1395 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001396 device_remove_file(regulator->dev, &regulator->dev_attr);
1397 kfree(regulator->dev_attr.attr.name);
1398 }
Mark Brown5de70512011-06-19 13:33:16 +01001399 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001400 list_del(&regulator->list);
1401 kfree(regulator);
1402
Mark Brown5ffbd132009-07-21 16:00:23 +01001403 rdev->open_count--;
1404 rdev->exclusive = 0;
1405
Liam Girdwood414c70c2008-04-30 15:59:04 +01001406 module_put(rdev->owner);
1407 mutex_unlock(&regulator_list_mutex);
1408}
1409EXPORT_SYMBOL_GPL(regulator_put);
1410
Mark Brownd5ad34f2012-01-20 20:09:18 +00001411static int devm_regulator_match(struct device *dev, void *res, void *data)
1412{
1413 struct regulator **r = res;
1414 if (!r || !*r) {
1415 WARN_ON(!r || !*r);
1416 return 0;
1417 }
1418 return *r == data;
1419}
1420
1421/**
1422 * devm_regulator_put - Resource managed regulator_put()
1423 * @regulator: regulator to free
1424 *
1425 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1426 * this function will not need to be called and the resource management
1427 * code will ensure that the resource is freed.
1428 */
1429void devm_regulator_put(struct regulator *regulator)
1430{
1431 int rc;
1432
1433 rc = devres_destroy(regulator->dev, devm_regulator_release,
1434 devm_regulator_match, regulator);
1435 WARN_ON(rc);
1436}
1437EXPORT_SYMBOL_GPL(devm_regulator_put);
1438
Mark Brown9a2372f2009-08-03 18:49:57 +01001439static int _regulator_can_change_status(struct regulator_dev *rdev)
1440{
1441 if (!rdev->constraints)
1442 return 0;
1443
1444 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1445 return 1;
1446 else
1447 return 0;
1448}
1449
Liam Girdwood414c70c2008-04-30 15:59:04 +01001450/* locks held by regulator_enable() */
1451static int _regulator_enable(struct regulator_dev *rdev)
1452{
Mark Brown31aae2b2009-12-21 12:21:52 +00001453 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001454
Liam Girdwood414c70c2008-04-30 15:59:04 +01001455 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001456 if (rdev->constraints &&
1457 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1458 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001459
Mark Brown9a2372f2009-08-03 18:49:57 +01001460 if (rdev->use_count == 0) {
1461 /* The regulator may on if it's not switchable or left on */
1462 ret = _regulator_is_enabled(rdev);
1463 if (ret == -EINVAL || ret == 0) {
1464 if (!_regulator_can_change_status(rdev))
1465 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001466
Mark Brown31aae2b2009-12-21 12:21:52 +00001467 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001468 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001469
1470 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001471 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001472 ret = _regulator_get_enable_time(rdev);
1473 if (ret >= 0) {
1474 delay = ret;
1475 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001476 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001477 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001478 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001479 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001480
Mark Brown02fa3ec2010-11-10 14:38:30 +00001481 trace_regulator_enable(rdev_get_name(rdev));
1482
Mark Brown31aae2b2009-12-21 12:21:52 +00001483 /* Allow the regulator to ramp; it would be useful
1484 * to extend this for bulk operations so that the
1485 * regulators can ramp together. */
1486 ret = rdev->desc->ops->enable(rdev);
1487 if (ret < 0)
1488 return ret;
1489
Mark Brown02fa3ec2010-11-10 14:38:30 +00001490 trace_regulator_enable_delay(rdev_get_name(rdev));
1491
Axel Line36c1df2010-11-05 21:51:32 +08001492 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001493 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001494 udelay(delay % 1000);
1495 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001496 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001497 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001498
Mark Brown02fa3ec2010-11-10 14:38:30 +00001499 trace_regulator_enable_complete(rdev_get_name(rdev));
1500
Linus Walleija7433cf2009-08-26 12:54:04 +02001501 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001502 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001503 return ret;
1504 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001505 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001506 }
1507
Mark Brown9a2372f2009-08-03 18:49:57 +01001508 rdev->use_count++;
1509
1510 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001511}
1512
1513/**
1514 * regulator_enable - enable regulator output
1515 * @regulator: regulator source
1516 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001517 * Request that the regulator be enabled with the regulator output at
1518 * the predefined voltage or current value. Calls to regulator_enable()
1519 * must be balanced with calls to regulator_disable().
1520 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001521 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001522 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001523 */
1524int regulator_enable(struct regulator *regulator)
1525{
David Brownell412aec62008-11-16 11:44:46 -08001526 struct regulator_dev *rdev = regulator->rdev;
1527 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001528
Mark Brown3801b862011-06-09 16:22:22 +01001529 if (rdev->supply) {
1530 ret = regulator_enable(rdev->supply);
1531 if (ret != 0)
1532 return ret;
1533 }
1534
David Brownell412aec62008-11-16 11:44:46 -08001535 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001536 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001537 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001538
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001539 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001540 regulator_disable(rdev->supply);
1541
Liam Girdwood414c70c2008-04-30 15:59:04 +01001542 return ret;
1543}
1544EXPORT_SYMBOL_GPL(regulator_enable);
1545
1546/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001547static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001548{
1549 int ret = 0;
1550
David Brownellcd94b502009-03-11 16:43:34 -08001551 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001552 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001553 return -EIO;
1554
Liam Girdwood414c70c2008-04-30 15:59:04 +01001555 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001556 if (rdev->use_count == 1 &&
1557 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001558
1559 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001560 if (_regulator_can_change_status(rdev) &&
1561 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001562 trace_regulator_disable(rdev_get_name(rdev));
1563
Liam Girdwood414c70c2008-04-30 15:59:04 +01001564 ret = rdev->desc->ops->disable(rdev);
1565 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001566 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001567 return ret;
1568 }
Mark Brown84b68262009-12-01 21:12:27 +00001569
Mark Brown02fa3ec2010-11-10 14:38:30 +00001570 trace_regulator_disable_complete(rdev_get_name(rdev));
1571
Mark Brown84b68262009-12-01 21:12:27 +00001572 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1573 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001574 }
1575
Liam Girdwood414c70c2008-04-30 15:59:04 +01001576 rdev->use_count = 0;
1577 } else if (rdev->use_count > 1) {
1578
1579 if (rdev->constraints &&
1580 (rdev->constraints->valid_ops_mask &
1581 REGULATOR_CHANGE_DRMS))
1582 drms_uA_update(rdev);
1583
1584 rdev->use_count--;
1585 }
Mark Brown3801b862011-06-09 16:22:22 +01001586
Liam Girdwood414c70c2008-04-30 15:59:04 +01001587 return ret;
1588}
1589
1590/**
1591 * regulator_disable - disable regulator output
1592 * @regulator: regulator source
1593 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001594 * Disable the regulator output voltage or current. Calls to
1595 * regulator_enable() must be balanced with calls to
1596 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001597 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001598 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001599 * devices have it enabled, the regulator device supports disabling and
1600 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001601 */
1602int regulator_disable(struct regulator *regulator)
1603{
David Brownell412aec62008-11-16 11:44:46 -08001604 struct regulator_dev *rdev = regulator->rdev;
1605 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001606
David Brownell412aec62008-11-16 11:44:46 -08001607 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001608 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001609 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001610
Mark Brown3801b862011-06-09 16:22:22 +01001611 if (ret == 0 && rdev->supply)
1612 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001613
Liam Girdwood414c70c2008-04-30 15:59:04 +01001614 return ret;
1615}
1616EXPORT_SYMBOL_GPL(regulator_disable);
1617
1618/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001619static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001620{
1621 int ret = 0;
1622
1623 /* force disable */
1624 if (rdev->desc->ops->disable) {
1625 /* ah well, who wants to live forever... */
1626 ret = rdev->desc->ops->disable(rdev);
1627 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001628 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001629 return ret;
1630 }
1631 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001632 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1633 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001634 }
1635
Liam Girdwood414c70c2008-04-30 15:59:04 +01001636 return ret;
1637}
1638
1639/**
1640 * regulator_force_disable - force disable regulator output
1641 * @regulator: regulator source
1642 *
1643 * Forcibly disable the regulator output voltage or current.
1644 * NOTE: this *will* disable the regulator output even if other consumer
1645 * devices have it enabled. This should be used for situations when device
1646 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1647 */
1648int regulator_force_disable(struct regulator *regulator)
1649{
Mark Brown82d15832011-05-09 11:41:02 +02001650 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001651 int ret;
1652
Mark Brown82d15832011-05-09 11:41:02 +02001653 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001654 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001655 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001656 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001657
Mark Brown3801b862011-06-09 16:22:22 +01001658 if (rdev->supply)
1659 while (rdev->open_count--)
1660 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001661
Liam Girdwood414c70c2008-04-30 15:59:04 +01001662 return ret;
1663}
1664EXPORT_SYMBOL_GPL(regulator_force_disable);
1665
Mark Brownda07ecd2011-09-11 09:53:50 +01001666static void regulator_disable_work(struct work_struct *work)
1667{
1668 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1669 disable_work.work);
1670 int count, i, ret;
1671
1672 mutex_lock(&rdev->mutex);
1673
1674 BUG_ON(!rdev->deferred_disables);
1675
1676 count = rdev->deferred_disables;
1677 rdev->deferred_disables = 0;
1678
1679 for (i = 0; i < count; i++) {
1680 ret = _regulator_disable(rdev);
1681 if (ret != 0)
1682 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1683 }
1684
1685 mutex_unlock(&rdev->mutex);
1686
1687 if (rdev->supply) {
1688 for (i = 0; i < count; i++) {
1689 ret = regulator_disable(rdev->supply);
1690 if (ret != 0) {
1691 rdev_err(rdev,
1692 "Supply disable failed: %d\n", ret);
1693 }
1694 }
1695 }
1696}
1697
1698/**
1699 * regulator_disable_deferred - disable regulator output with delay
1700 * @regulator: regulator source
1701 * @ms: miliseconds until the regulator is disabled
1702 *
1703 * Execute regulator_disable() on the regulator after a delay. This
1704 * is intended for use with devices that require some time to quiesce.
1705 *
1706 * NOTE: this will only disable the regulator output if no other consumer
1707 * devices have it enabled, the regulator device supports disabling and
1708 * machine constraints permit this operation.
1709 */
1710int regulator_disable_deferred(struct regulator *regulator, int ms)
1711{
1712 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001713 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001714
1715 mutex_lock(&rdev->mutex);
1716 rdev->deferred_disables++;
1717 mutex_unlock(&rdev->mutex);
1718
Mark Brownaa598022011-10-03 22:42:43 +01001719 ret = schedule_delayed_work(&rdev->disable_work,
1720 msecs_to_jiffies(ms));
1721 if (ret < 0)
1722 return ret;
1723 else
1724 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001725}
1726EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1727
Liam Girdwood414c70c2008-04-30 15:59:04 +01001728static int _regulator_is_enabled(struct regulator_dev *rdev)
1729{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001730 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001731 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001732 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001733
Mark Brown93325462009-08-03 18:49:56 +01001734 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001735}
1736
1737/**
1738 * regulator_is_enabled - is the regulator output enabled
1739 * @regulator: regulator source
1740 *
David Brownell412aec62008-11-16 11:44:46 -08001741 * Returns positive if the regulator driver backing the source/client
1742 * has requested that the device be enabled, zero if it hasn't, else a
1743 * negative errno code.
1744 *
1745 * Note that the device backing this regulator handle can have multiple
1746 * users, so it might be enabled even if regulator_enable() was never
1747 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001748 */
1749int regulator_is_enabled(struct regulator *regulator)
1750{
Mark Brown93325462009-08-03 18:49:56 +01001751 int ret;
1752
1753 mutex_lock(&regulator->rdev->mutex);
1754 ret = _regulator_is_enabled(regulator->rdev);
1755 mutex_unlock(&regulator->rdev->mutex);
1756
1757 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001758}
1759EXPORT_SYMBOL_GPL(regulator_is_enabled);
1760
1761/**
David Brownell4367cfd2009-02-26 11:48:36 -08001762 * regulator_count_voltages - count regulator_list_voltage() selectors
1763 * @regulator: regulator source
1764 *
1765 * Returns number of selectors, or negative errno. Selectors are
1766 * numbered starting at zero, and typically correspond to bitfields
1767 * in hardware registers.
1768 */
1769int regulator_count_voltages(struct regulator *regulator)
1770{
1771 struct regulator_dev *rdev = regulator->rdev;
1772
1773 return rdev->desc->n_voltages ? : -EINVAL;
1774}
1775EXPORT_SYMBOL_GPL(regulator_count_voltages);
1776
1777/**
1778 * regulator_list_voltage - enumerate supported voltages
1779 * @regulator: regulator source
1780 * @selector: identify voltage to list
1781 * Context: can sleep
1782 *
1783 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001784 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001785 * negative errno.
1786 */
1787int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1788{
1789 struct regulator_dev *rdev = regulator->rdev;
1790 struct regulator_ops *ops = rdev->desc->ops;
1791 int ret;
1792
1793 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1794 return -EINVAL;
1795
1796 mutex_lock(&rdev->mutex);
1797 ret = ops->list_voltage(rdev, selector);
1798 mutex_unlock(&rdev->mutex);
1799
1800 if (ret > 0) {
1801 if (ret < rdev->constraints->min_uV)
1802 ret = 0;
1803 else if (ret > rdev->constraints->max_uV)
1804 ret = 0;
1805 }
1806
1807 return ret;
1808}
1809EXPORT_SYMBOL_GPL(regulator_list_voltage);
1810
1811/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001812 * regulator_is_supported_voltage - check if a voltage range can be supported
1813 *
1814 * @regulator: Regulator to check.
1815 * @min_uV: Minimum required voltage in uV.
1816 * @max_uV: Maximum required voltage in uV.
1817 *
1818 * Returns a boolean or a negative error code.
1819 */
1820int regulator_is_supported_voltage(struct regulator *regulator,
1821 int min_uV, int max_uV)
1822{
1823 int i, voltages, ret;
1824
1825 ret = regulator_count_voltages(regulator);
1826 if (ret < 0)
1827 return ret;
1828 voltages = ret;
1829
1830 for (i = 0; i < voltages; i++) {
1831 ret = regulator_list_voltage(regulator, i);
1832
1833 if (ret >= min_uV && ret <= max_uV)
1834 return 1;
1835 }
1836
1837 return 0;
1838}
Mark Browna398eaa2011-12-28 12:48:45 +00001839EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01001840
Mark Brown75790252010-12-12 14:25:50 +00001841static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1842 int min_uV, int max_uV)
1843{
1844 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001845 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001846 unsigned int selector;
1847
1848 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1849
Mark Brownbf5892a2011-05-08 22:13:37 +01001850 min_uV += rdev->constraints->uV_offset;
1851 max_uV += rdev->constraints->uV_offset;
1852
Mark Brown75790252010-12-12 14:25:50 +00001853 if (rdev->desc->ops->set_voltage) {
1854 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1855 &selector);
1856
1857 if (rdev->desc->ops->list_voltage)
1858 selector = rdev->desc->ops->list_voltage(rdev,
1859 selector);
1860 else
1861 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001862 } else if (rdev->desc->ops->set_voltage_sel) {
1863 int best_val = INT_MAX;
1864 int i;
1865
1866 selector = 0;
1867
1868 /* Find the smallest voltage that falls within the specified
1869 * range.
1870 */
1871 for (i = 0; i < rdev->desc->n_voltages; i++) {
1872 ret = rdev->desc->ops->list_voltage(rdev, i);
1873 if (ret < 0)
1874 continue;
1875
1876 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1877 best_val = ret;
1878 selector = i;
1879 }
1880 }
1881
Linus Walleij77af1b2642011-03-17 13:24:36 +01001882 /*
1883 * If we can't obtain the old selector there is not enough
1884 * info to call set_voltage_time_sel().
1885 */
1886 if (rdev->desc->ops->set_voltage_time_sel &&
1887 rdev->desc->ops->get_voltage_sel) {
1888 unsigned int old_selector = 0;
1889
1890 ret = rdev->desc->ops->get_voltage_sel(rdev);
1891 if (ret < 0)
1892 return ret;
1893 old_selector = ret;
Axel Lin07351232012-02-24 23:13:19 +08001894 ret = rdev->desc->ops->set_voltage_time_sel(rdev,
Linus Walleij77af1b2642011-03-17 13:24:36 +01001895 old_selector, selector);
Axel Lin07351232012-02-24 23:13:19 +08001896 if (ret < 0)
1897 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
1898 else
1899 delay = ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001900 }
1901
Mark Browne8eef822010-12-12 14:36:17 +00001902 if (best_val != INT_MAX) {
1903 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1904 selector = best_val;
1905 } else {
1906 ret = -EINVAL;
1907 }
Mark Brown75790252010-12-12 14:25:50 +00001908 } else {
1909 ret = -EINVAL;
1910 }
1911
Linus Walleij77af1b2642011-03-17 13:24:36 +01001912 /* Insert any necessary delays */
1913 if (delay >= 1000) {
1914 mdelay(delay / 1000);
1915 udelay(delay % 1000);
1916 } else if (delay) {
1917 udelay(delay);
1918 }
1919
Mark Brownded06a52010-12-16 13:59:10 +00001920 if (ret == 0)
1921 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1922 NULL);
1923
Mark Brown75790252010-12-12 14:25:50 +00001924 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1925
1926 return ret;
1927}
1928
Mark Browna7a1ad92009-07-21 16:00:24 +01001929/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001930 * regulator_set_voltage - set regulator output voltage
1931 * @regulator: regulator source
1932 * @min_uV: Minimum required voltage in uV
1933 * @max_uV: Maximum acceptable voltage in uV
1934 *
1935 * Sets a voltage regulator to the desired output voltage. This can be set
1936 * during any regulator state. IOW, regulator can be disabled or enabled.
1937 *
1938 * If the regulator is enabled then the voltage will change to the new value
1939 * immediately otherwise if the regulator is disabled the regulator will
1940 * output at the new voltage when enabled.
1941 *
1942 * NOTE: If the regulator is shared between several devices then the lowest
1943 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001944 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001945 * calling this function otherwise this call will fail.
1946 */
1947int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1948{
1949 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001950 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001951
1952 mutex_lock(&rdev->mutex);
1953
Mark Brown95a3c232010-12-16 15:49:37 +00001954 /* If we're setting the same range as last time the change
1955 * should be a noop (some cpufreq implementations use the same
1956 * voltage for multiple frequencies, for example).
1957 */
1958 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1959 goto out;
1960
Liam Girdwood414c70c2008-04-30 15:59:04 +01001961 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001962 if (!rdev->desc->ops->set_voltage &&
1963 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001964 ret = -EINVAL;
1965 goto out;
1966 }
1967
1968 /* constraints check */
1969 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1970 if (ret < 0)
1971 goto out;
1972 regulator->min_uV = min_uV;
1973 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001974
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001975 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1976 if (ret < 0)
1977 goto out;
1978
Mark Brown75790252010-12-12 14:25:50 +00001979 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001980
Liam Girdwood414c70c2008-04-30 15:59:04 +01001981out:
1982 mutex_unlock(&rdev->mutex);
1983 return ret;
1984}
1985EXPORT_SYMBOL_GPL(regulator_set_voltage);
1986
Mark Brown606a2562010-12-16 15:49:36 +00001987/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001988 * regulator_set_voltage_time - get raise/fall time
1989 * @regulator: regulator source
1990 * @old_uV: starting voltage in microvolts
1991 * @new_uV: target voltage in microvolts
1992 *
1993 * Provided with the starting and ending voltage, this function attempts to
1994 * calculate the time in microseconds required to rise or fall to this new
1995 * voltage.
1996 */
1997int regulator_set_voltage_time(struct regulator *regulator,
1998 int old_uV, int new_uV)
1999{
2000 struct regulator_dev *rdev = regulator->rdev;
2001 struct regulator_ops *ops = rdev->desc->ops;
2002 int old_sel = -1;
2003 int new_sel = -1;
2004 int voltage;
2005 int i;
2006
2007 /* Currently requires operations to do this */
2008 if (!ops->list_voltage || !ops->set_voltage_time_sel
2009 || !rdev->desc->n_voltages)
2010 return -EINVAL;
2011
2012 for (i = 0; i < rdev->desc->n_voltages; i++) {
2013 /* We only look for exact voltage matches here */
2014 voltage = regulator_list_voltage(regulator, i);
2015 if (voltage < 0)
2016 return -EINVAL;
2017 if (voltage == 0)
2018 continue;
2019 if (voltage == old_uV)
2020 old_sel = i;
2021 if (voltage == new_uV)
2022 new_sel = i;
2023 }
2024
2025 if (old_sel < 0 || new_sel < 0)
2026 return -EINVAL;
2027
2028 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2029}
2030EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2031
2032/**
Mark Brown606a2562010-12-16 15:49:36 +00002033 * regulator_sync_voltage - re-apply last regulator output voltage
2034 * @regulator: regulator source
2035 *
2036 * Re-apply the last configured voltage. This is intended to be used
2037 * where some external control source the consumer is cooperating with
2038 * has caused the configured voltage to change.
2039 */
2040int regulator_sync_voltage(struct regulator *regulator)
2041{
2042 struct regulator_dev *rdev = regulator->rdev;
2043 int ret, min_uV, max_uV;
2044
2045 mutex_lock(&rdev->mutex);
2046
2047 if (!rdev->desc->ops->set_voltage &&
2048 !rdev->desc->ops->set_voltage_sel) {
2049 ret = -EINVAL;
2050 goto out;
2051 }
2052
2053 /* This is only going to work if we've had a voltage configured. */
2054 if (!regulator->min_uV && !regulator->max_uV) {
2055 ret = -EINVAL;
2056 goto out;
2057 }
2058
2059 min_uV = regulator->min_uV;
2060 max_uV = regulator->max_uV;
2061
2062 /* This should be a paranoia check... */
2063 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2064 if (ret < 0)
2065 goto out;
2066
2067 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2068 if (ret < 0)
2069 goto out;
2070
2071 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2072
2073out:
2074 mutex_unlock(&rdev->mutex);
2075 return ret;
2076}
2077EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2078
Liam Girdwood414c70c2008-04-30 15:59:04 +01002079static int _regulator_get_voltage(struct regulator_dev *rdev)
2080{
Mark Brownbf5892a2011-05-08 22:13:37 +01002081 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002082
2083 if (rdev->desc->ops->get_voltage_sel) {
2084 sel = rdev->desc->ops->get_voltage_sel(rdev);
2085 if (sel < 0)
2086 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002087 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002088 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002089 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002090 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002091 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002092 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002093
Axel Lincb220d12011-05-23 20:08:10 +08002094 if (ret < 0)
2095 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002096 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002097}
2098
2099/**
2100 * regulator_get_voltage - get regulator output voltage
2101 * @regulator: regulator source
2102 *
2103 * This returns the current regulator voltage in uV.
2104 *
2105 * NOTE: If the regulator is disabled it will return the voltage value. This
2106 * function should not be used to determine regulator state.
2107 */
2108int regulator_get_voltage(struct regulator *regulator)
2109{
2110 int ret;
2111
2112 mutex_lock(&regulator->rdev->mutex);
2113
2114 ret = _regulator_get_voltage(regulator->rdev);
2115
2116 mutex_unlock(&regulator->rdev->mutex);
2117
2118 return ret;
2119}
2120EXPORT_SYMBOL_GPL(regulator_get_voltage);
2121
2122/**
2123 * regulator_set_current_limit - set regulator output current limit
2124 * @regulator: regulator source
2125 * @min_uA: Minimuum supported current in uA
2126 * @max_uA: Maximum supported current in uA
2127 *
2128 * Sets current sink to the desired output current. This can be set during
2129 * any regulator state. IOW, regulator can be disabled or enabled.
2130 *
2131 * If the regulator is enabled then the current will change to the new value
2132 * immediately otherwise if the regulator is disabled the regulator will
2133 * output at the new current when enabled.
2134 *
2135 * NOTE: Regulator system constraints must be set for this regulator before
2136 * calling this function otherwise this call will fail.
2137 */
2138int regulator_set_current_limit(struct regulator *regulator,
2139 int min_uA, int max_uA)
2140{
2141 struct regulator_dev *rdev = regulator->rdev;
2142 int ret;
2143
2144 mutex_lock(&rdev->mutex);
2145
2146 /* sanity check */
2147 if (!rdev->desc->ops->set_current_limit) {
2148 ret = -EINVAL;
2149 goto out;
2150 }
2151
2152 /* constraints check */
2153 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2154 if (ret < 0)
2155 goto out;
2156
2157 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2158out:
2159 mutex_unlock(&rdev->mutex);
2160 return ret;
2161}
2162EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2163
2164static int _regulator_get_current_limit(struct regulator_dev *rdev)
2165{
2166 int ret;
2167
2168 mutex_lock(&rdev->mutex);
2169
2170 /* sanity check */
2171 if (!rdev->desc->ops->get_current_limit) {
2172 ret = -EINVAL;
2173 goto out;
2174 }
2175
2176 ret = rdev->desc->ops->get_current_limit(rdev);
2177out:
2178 mutex_unlock(&rdev->mutex);
2179 return ret;
2180}
2181
2182/**
2183 * regulator_get_current_limit - get regulator output current
2184 * @regulator: regulator source
2185 *
2186 * This returns the current supplied by the specified current sink in uA.
2187 *
2188 * NOTE: If the regulator is disabled it will return the current value. This
2189 * function should not be used to determine regulator state.
2190 */
2191int regulator_get_current_limit(struct regulator *regulator)
2192{
2193 return _regulator_get_current_limit(regulator->rdev);
2194}
2195EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2196
2197/**
2198 * regulator_set_mode - set regulator operating mode
2199 * @regulator: regulator source
2200 * @mode: operating mode - one of the REGULATOR_MODE constants
2201 *
2202 * Set regulator operating mode to increase regulator efficiency or improve
2203 * regulation performance.
2204 *
2205 * NOTE: Regulator system constraints must be set for this regulator before
2206 * calling this function otherwise this call will fail.
2207 */
2208int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2209{
2210 struct regulator_dev *rdev = regulator->rdev;
2211 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302212 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002213
2214 mutex_lock(&rdev->mutex);
2215
2216 /* sanity check */
2217 if (!rdev->desc->ops->set_mode) {
2218 ret = -EINVAL;
2219 goto out;
2220 }
2221
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302222 /* return if the same mode is requested */
2223 if (rdev->desc->ops->get_mode) {
2224 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2225 if (regulator_curr_mode == mode) {
2226 ret = 0;
2227 goto out;
2228 }
2229 }
2230
Liam Girdwood414c70c2008-04-30 15:59:04 +01002231 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002232 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002233 if (ret < 0)
2234 goto out;
2235
2236 ret = rdev->desc->ops->set_mode(rdev, mode);
2237out:
2238 mutex_unlock(&rdev->mutex);
2239 return ret;
2240}
2241EXPORT_SYMBOL_GPL(regulator_set_mode);
2242
2243static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2244{
2245 int ret;
2246
2247 mutex_lock(&rdev->mutex);
2248
2249 /* sanity check */
2250 if (!rdev->desc->ops->get_mode) {
2251 ret = -EINVAL;
2252 goto out;
2253 }
2254
2255 ret = rdev->desc->ops->get_mode(rdev);
2256out:
2257 mutex_unlock(&rdev->mutex);
2258 return ret;
2259}
2260
2261/**
2262 * regulator_get_mode - get regulator operating mode
2263 * @regulator: regulator source
2264 *
2265 * Get the current regulator operating mode.
2266 */
2267unsigned int regulator_get_mode(struct regulator *regulator)
2268{
2269 return _regulator_get_mode(regulator->rdev);
2270}
2271EXPORT_SYMBOL_GPL(regulator_get_mode);
2272
2273/**
2274 * regulator_set_optimum_mode - set regulator optimum operating mode
2275 * @regulator: regulator source
2276 * @uA_load: load current
2277 *
2278 * Notifies the regulator core of a new device load. This is then used by
2279 * DRMS (if enabled by constraints) to set the most efficient regulator
2280 * operating mode for the new regulator loading.
2281 *
2282 * Consumer devices notify their supply regulator of the maximum power
2283 * they will require (can be taken from device datasheet in the power
2284 * consumption tables) when they change operational status and hence power
2285 * state. Examples of operational state changes that can affect power
2286 * consumption are :-
2287 *
2288 * o Device is opened / closed.
2289 * o Device I/O is about to begin or has just finished.
2290 * o Device is idling in between work.
2291 *
2292 * This information is also exported via sysfs to userspace.
2293 *
2294 * DRMS will sum the total requested load on the regulator and change
2295 * to the most efficient operating mode if platform constraints allow.
2296 *
2297 * Returns the new regulator mode or error.
2298 */
2299int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2300{
2301 struct regulator_dev *rdev = regulator->rdev;
2302 struct regulator *consumer;
2303 int ret, output_uV, input_uV, total_uA_load = 0;
2304 unsigned int mode;
2305
2306 mutex_lock(&rdev->mutex);
2307
Mark Browna4b41482011-05-14 11:19:45 -07002308 /*
2309 * first check to see if we can set modes at all, otherwise just
2310 * tell the consumer everything is OK.
2311 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002312 regulator->uA_load = uA_load;
2313 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002314 if (ret < 0) {
2315 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002316 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002317 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002318
Liam Girdwood414c70c2008-04-30 15:59:04 +01002319 if (!rdev->desc->ops->get_optimum_mode)
2320 goto out;
2321
Mark Browna4b41482011-05-14 11:19:45 -07002322 /*
2323 * we can actually do this so any errors are indicators of
2324 * potential real failure.
2325 */
2326 ret = -EINVAL;
2327
Liam Girdwood414c70c2008-04-30 15:59:04 +01002328 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002329 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002330 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002331 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002332 goto out;
2333 }
2334
2335 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002336 input_uV = 0;
2337 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002338 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002339 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002340 input_uV = rdev->constraints->input_uV;
2341 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002342 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002343 goto out;
2344 }
2345
2346 /* calc total requested load for this regulator */
2347 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002348 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002349
2350 mode = rdev->desc->ops->get_optimum_mode(rdev,
2351 input_uV, output_uV,
2352 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002353 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002354 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002355 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2356 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002357 goto out;
2358 }
2359
2360 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002361 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002362 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002363 goto out;
2364 }
2365 ret = mode;
2366out:
2367 mutex_unlock(&rdev->mutex);
2368 return ret;
2369}
2370EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2371
2372/**
2373 * regulator_register_notifier - register regulator event notifier
2374 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002375 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002376 *
2377 * Register notifier block to receive regulator events.
2378 */
2379int regulator_register_notifier(struct regulator *regulator,
2380 struct notifier_block *nb)
2381{
2382 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2383 nb);
2384}
2385EXPORT_SYMBOL_GPL(regulator_register_notifier);
2386
2387/**
2388 * regulator_unregister_notifier - unregister regulator event notifier
2389 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002390 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002391 *
2392 * Unregister regulator event notifier block.
2393 */
2394int regulator_unregister_notifier(struct regulator *regulator,
2395 struct notifier_block *nb)
2396{
2397 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2398 nb);
2399}
2400EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2401
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002402/* notify regulator consumers and downstream regulator consumers.
2403 * Note mutex must be held by caller.
2404 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002405static void _notifier_call_chain(struct regulator_dev *rdev,
2406 unsigned long event, void *data)
2407{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002408 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002409 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002410}
2411
2412/**
2413 * regulator_bulk_get - get multiple regulator consumers
2414 *
2415 * @dev: Device to supply
2416 * @num_consumers: Number of consumers to register
2417 * @consumers: Configuration of consumers; clients are stored here.
2418 *
2419 * @return 0 on success, an errno on failure.
2420 *
2421 * This helper function allows drivers to get several regulator
2422 * consumers in one operation. If any of the regulators cannot be
2423 * acquired then any regulators that were allocated will be freed
2424 * before returning to the caller.
2425 */
2426int regulator_bulk_get(struct device *dev, int num_consumers,
2427 struct regulator_bulk_data *consumers)
2428{
2429 int i;
2430 int ret;
2431
2432 for (i = 0; i < num_consumers; i++)
2433 consumers[i].consumer = NULL;
2434
2435 for (i = 0; i < num_consumers; i++) {
2436 consumers[i].consumer = regulator_get(dev,
2437 consumers[i].supply);
2438 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002439 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002440 dev_err(dev, "Failed to get supply '%s': %d\n",
2441 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002442 consumers[i].consumer = NULL;
2443 goto err;
2444 }
2445 }
2446
2447 return 0;
2448
2449err:
Axel Linb29c7692012-02-20 10:32:16 +08002450 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002451 regulator_put(consumers[i].consumer);
2452
2453 return ret;
2454}
2455EXPORT_SYMBOL_GPL(regulator_bulk_get);
2456
Mark Browne6e74032012-01-20 20:10:08 +00002457/**
2458 * devm_regulator_bulk_get - managed get multiple regulator consumers
2459 *
2460 * @dev: Device to supply
2461 * @num_consumers: Number of consumers to register
2462 * @consumers: Configuration of consumers; clients are stored here.
2463 *
2464 * @return 0 on success, an errno on failure.
2465 *
2466 * This helper function allows drivers to get several regulator
2467 * consumers in one operation with management, the regulators will
2468 * automatically be freed when the device is unbound. If any of the
2469 * regulators cannot be acquired then any regulators that were
2470 * allocated will be freed before returning to the caller.
2471 */
2472int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2473 struct regulator_bulk_data *consumers)
2474{
2475 int i;
2476 int ret;
2477
2478 for (i = 0; i < num_consumers; i++)
2479 consumers[i].consumer = NULL;
2480
2481 for (i = 0; i < num_consumers; i++) {
2482 consumers[i].consumer = devm_regulator_get(dev,
2483 consumers[i].supply);
2484 if (IS_ERR(consumers[i].consumer)) {
2485 ret = PTR_ERR(consumers[i].consumer);
2486 dev_err(dev, "Failed to get supply '%s': %d\n",
2487 consumers[i].supply, ret);
2488 consumers[i].consumer = NULL;
2489 goto err;
2490 }
2491 }
2492
2493 return 0;
2494
2495err:
2496 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2497 devm_regulator_put(consumers[i].consumer);
2498
2499 return ret;
2500}
2501EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2502
Mark Brownf21e0e82011-05-24 08:12:40 +08002503static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2504{
2505 struct regulator_bulk_data *bulk = data;
2506
2507 bulk->ret = regulator_enable(bulk->consumer);
2508}
2509
Liam Girdwood414c70c2008-04-30 15:59:04 +01002510/**
2511 * regulator_bulk_enable - enable multiple regulator consumers
2512 *
2513 * @num_consumers: Number of consumers
2514 * @consumers: Consumer data; clients are stored here.
2515 * @return 0 on success, an errno on failure
2516 *
2517 * This convenience API allows consumers to enable multiple regulator
2518 * clients in a single API call. If any consumers cannot be enabled
2519 * then any others that were enabled will be disabled again prior to
2520 * return.
2521 */
2522int regulator_bulk_enable(int num_consumers,
2523 struct regulator_bulk_data *consumers)
2524{
Mark Brownf21e0e82011-05-24 08:12:40 +08002525 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002526 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002527 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002528
Mark Brownf21e0e82011-05-24 08:12:40 +08002529 for (i = 0; i < num_consumers; i++)
2530 async_schedule_domain(regulator_bulk_enable_async,
2531 &consumers[i], &async_domain);
2532
2533 async_synchronize_full_domain(&async_domain);
2534
2535 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002536 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002537 if (consumers[i].ret != 0) {
2538 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002539 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002540 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002541 }
2542
2543 return 0;
2544
2545err:
Axel Linb29c7692012-02-20 10:32:16 +08002546 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2547 while (--i >= 0)
2548 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002549
2550 return ret;
2551}
2552EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2553
2554/**
2555 * regulator_bulk_disable - disable multiple regulator consumers
2556 *
2557 * @num_consumers: Number of consumers
2558 * @consumers: Consumer data; clients are stored here.
2559 * @return 0 on success, an errno on failure
2560 *
2561 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002562 * clients in a single API call. If any consumers cannot be disabled
2563 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002564 * return.
2565 */
2566int regulator_bulk_disable(int num_consumers,
2567 struct regulator_bulk_data *consumers)
2568{
2569 int i;
2570 int ret;
2571
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002572 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002573 ret = regulator_disable(consumers[i].consumer);
2574 if (ret != 0)
2575 goto err;
2576 }
2577
2578 return 0;
2579
2580err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002581 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002582 for (++i; i < num_consumers; ++i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002583 regulator_enable(consumers[i].consumer);
2584
2585 return ret;
2586}
2587EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2588
2589/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002590 * regulator_bulk_force_disable - force disable multiple regulator consumers
2591 *
2592 * @num_consumers: Number of consumers
2593 * @consumers: Consumer data; clients are stored here.
2594 * @return 0 on success, an errno on failure
2595 *
2596 * This convenience API allows consumers to forcibly disable multiple regulator
2597 * clients in a single API call.
2598 * NOTE: This should be used for situations when device damage will
2599 * likely occur if the regulators are not disabled (e.g. over temp).
2600 * Although regulator_force_disable function call for some consumers can
2601 * return error numbers, the function is called for all consumers.
2602 */
2603int regulator_bulk_force_disable(int num_consumers,
2604 struct regulator_bulk_data *consumers)
2605{
2606 int i;
2607 int ret;
2608
2609 for (i = 0; i < num_consumers; i++)
2610 consumers[i].ret =
2611 regulator_force_disable(consumers[i].consumer);
2612
2613 for (i = 0; i < num_consumers; i++) {
2614 if (consumers[i].ret != 0) {
2615 ret = consumers[i].ret;
2616 goto out;
2617 }
2618 }
2619
2620 return 0;
2621out:
2622 return ret;
2623}
2624EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2625
2626/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002627 * regulator_bulk_free - free multiple regulator consumers
2628 *
2629 * @num_consumers: Number of consumers
2630 * @consumers: Consumer data; clients are stored here.
2631 *
2632 * This convenience API allows consumers to free multiple regulator
2633 * clients in a single API call.
2634 */
2635void regulator_bulk_free(int num_consumers,
2636 struct regulator_bulk_data *consumers)
2637{
2638 int i;
2639
2640 for (i = 0; i < num_consumers; i++) {
2641 regulator_put(consumers[i].consumer);
2642 consumers[i].consumer = NULL;
2643 }
2644}
2645EXPORT_SYMBOL_GPL(regulator_bulk_free);
2646
2647/**
2648 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002649 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002650 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002651 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002652 *
2653 * Called by regulator drivers to notify clients a regulator event has
2654 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002655 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002656 */
2657int regulator_notifier_call_chain(struct regulator_dev *rdev,
2658 unsigned long event, void *data)
2659{
2660 _notifier_call_chain(rdev, event, data);
2661 return NOTIFY_DONE;
2662
2663}
2664EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2665
Mark Brownbe721972009-08-04 20:09:52 +02002666/**
2667 * regulator_mode_to_status - convert a regulator mode into a status
2668 *
2669 * @mode: Mode to convert
2670 *
2671 * Convert a regulator mode into a status.
2672 */
2673int regulator_mode_to_status(unsigned int mode)
2674{
2675 switch (mode) {
2676 case REGULATOR_MODE_FAST:
2677 return REGULATOR_STATUS_FAST;
2678 case REGULATOR_MODE_NORMAL:
2679 return REGULATOR_STATUS_NORMAL;
2680 case REGULATOR_MODE_IDLE:
2681 return REGULATOR_STATUS_IDLE;
2682 case REGULATOR_STATUS_STANDBY:
2683 return REGULATOR_STATUS_STANDBY;
2684 default:
2685 return 0;
2686 }
2687}
2688EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2689
David Brownell7ad68e22008-11-11 17:39:02 -08002690/*
2691 * To avoid cluttering sysfs (and memory) with useless state, only
2692 * create attributes that can be meaningfully displayed.
2693 */
2694static int add_regulator_attributes(struct regulator_dev *rdev)
2695{
2696 struct device *dev = &rdev->dev;
2697 struct regulator_ops *ops = rdev->desc->ops;
2698 int status = 0;
2699
2700 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002701 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2702 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002703 status = device_create_file(dev, &dev_attr_microvolts);
2704 if (status < 0)
2705 return status;
2706 }
2707 if (ops->get_current_limit) {
2708 status = device_create_file(dev, &dev_attr_microamps);
2709 if (status < 0)
2710 return status;
2711 }
2712 if (ops->get_mode) {
2713 status = device_create_file(dev, &dev_attr_opmode);
2714 if (status < 0)
2715 return status;
2716 }
2717 if (ops->is_enabled) {
2718 status = device_create_file(dev, &dev_attr_state);
2719 if (status < 0)
2720 return status;
2721 }
David Brownell853116a2009-01-14 23:03:17 -08002722 if (ops->get_status) {
2723 status = device_create_file(dev, &dev_attr_status);
2724 if (status < 0)
2725 return status;
2726 }
David Brownell7ad68e22008-11-11 17:39:02 -08002727
2728 /* some attributes are type-specific */
2729 if (rdev->desc->type == REGULATOR_CURRENT) {
2730 status = device_create_file(dev, &dev_attr_requested_microamps);
2731 if (status < 0)
2732 return status;
2733 }
2734
2735 /* all the other attributes exist to support constraints;
2736 * don't show them if there are no constraints, or if the
2737 * relevant supporting methods are missing.
2738 */
2739 if (!rdev->constraints)
2740 return status;
2741
2742 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002743 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002744 status = device_create_file(dev, &dev_attr_min_microvolts);
2745 if (status < 0)
2746 return status;
2747 status = device_create_file(dev, &dev_attr_max_microvolts);
2748 if (status < 0)
2749 return status;
2750 }
2751 if (ops->set_current_limit) {
2752 status = device_create_file(dev, &dev_attr_min_microamps);
2753 if (status < 0)
2754 return status;
2755 status = device_create_file(dev, &dev_attr_max_microamps);
2756 if (status < 0)
2757 return status;
2758 }
2759
2760 /* suspend mode constraints need multiple supporting methods */
2761 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2762 return status;
2763
2764 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2765 if (status < 0)
2766 return status;
2767 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2768 if (status < 0)
2769 return status;
2770 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2771 if (status < 0)
2772 return status;
2773
2774 if (ops->set_suspend_voltage) {
2775 status = device_create_file(dev,
2776 &dev_attr_suspend_standby_microvolts);
2777 if (status < 0)
2778 return status;
2779 status = device_create_file(dev,
2780 &dev_attr_suspend_mem_microvolts);
2781 if (status < 0)
2782 return status;
2783 status = device_create_file(dev,
2784 &dev_attr_suspend_disk_microvolts);
2785 if (status < 0)
2786 return status;
2787 }
2788
2789 if (ops->set_suspend_mode) {
2790 status = device_create_file(dev,
2791 &dev_attr_suspend_standby_mode);
2792 if (status < 0)
2793 return status;
2794 status = device_create_file(dev,
2795 &dev_attr_suspend_mem_mode);
2796 if (status < 0)
2797 return status;
2798 status = device_create_file(dev,
2799 &dev_attr_suspend_disk_mode);
2800 if (status < 0)
2801 return status;
2802 }
2803
2804 return status;
2805}
2806
Mark Brown1130e5b2010-12-21 23:49:31 +00002807static void rdev_init_debugfs(struct regulator_dev *rdev)
2808{
Mark Brown1130e5b2010-12-21 23:49:31 +00002809 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08002810 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00002811 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00002812 return;
2813 }
2814
2815 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2816 &rdev->use_count);
2817 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2818 &rdev->open_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00002819}
2820
Liam Girdwood414c70c2008-04-30 15:59:04 +01002821/**
2822 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002823 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01002824 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002825 *
2826 * Called by regulator drivers to register a regulator.
2827 * Returns 0 on success.
2828 */
Mark Brown65f26842012-04-03 20:46:53 +01002829struct regulator_dev *
2830regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01002831 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002832{
Mark Brown9a8f5e02011-11-29 18:11:19 +00002833 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01002834 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002835 static atomic_t regulator_no = ATOMIC_INIT(0);
2836 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01002837 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002838 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05302839 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002840
Mark Brownc1727082012-04-04 00:50:22 +01002841 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002842 return ERR_PTR(-EINVAL);
2843
Mark Brown32c8fad2012-04-11 10:19:12 +01002844 dev = config->dev;
2845
Liam Girdwood414c70c2008-04-30 15:59:04 +01002846 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2847 return ERR_PTR(-EINVAL);
2848
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002849 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2850 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002851 return ERR_PTR(-EINVAL);
2852
Mark Brown476c2d82010-12-10 17:28:07 +00002853 /* Only one of each should be implemented */
2854 WARN_ON(regulator_desc->ops->get_voltage &&
2855 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002856 WARN_ON(regulator_desc->ops->set_voltage &&
2857 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002858
2859 /* If we're using selectors we must implement list_voltage. */
2860 if (regulator_desc->ops->get_voltage_sel &&
2861 !regulator_desc->ops->list_voltage) {
2862 return ERR_PTR(-EINVAL);
2863 }
Mark Browne8eef822010-12-12 14:36:17 +00002864 if (regulator_desc->ops->set_voltage_sel &&
2865 !regulator_desc->ops->list_voltage) {
2866 return ERR_PTR(-EINVAL);
2867 }
Mark Brown476c2d82010-12-10 17:28:07 +00002868
Mark Brownc1727082012-04-04 00:50:22 +01002869 init_data = config->init_data;
2870
Liam Girdwood414c70c2008-04-30 15:59:04 +01002871 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2872 if (rdev == NULL)
2873 return ERR_PTR(-ENOMEM);
2874
2875 mutex_lock(&regulator_list_mutex);
2876
2877 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01002878 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002879 rdev->owner = regulator_desc->owner;
2880 rdev->desc = regulator_desc;
Mark Brown65b19ce2012-04-15 11:16:05 +01002881 rdev->regmap = config->regmap;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002882 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002883 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002884 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01002885 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002886
Liam Girdwooda5766f12008-10-10 13:22:20 +01002887 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002888 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01002889 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002890 if (ret < 0)
2891 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002892 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002893
Liam Girdwooda5766f12008-10-10 13:22:20 +01002894 /* register with sysfs */
2895 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01002896 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002897 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002898 dev_set_name(&rdev->dev, "regulator.%d",
2899 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002900 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002901 if (ret != 0) {
2902 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002903 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002904 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002905
2906 dev_set_drvdata(&rdev->dev, rdev);
2907
Mike Rapoport74f544c2008-11-25 14:53:53 +02002908 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002909 if (init_data)
2910 constraints = &init_data->constraints;
2911
2912 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02002913 if (ret < 0)
2914 goto scrub;
2915
David Brownell7ad68e22008-11-11 17:39:02 -08002916 /* add attributes supported by this regulator */
2917 ret = add_regulator_attributes(rdev);
2918 if (ret < 0)
2919 goto scrub;
2920
Mark Brown9a8f5e02011-11-29 18:11:19 +00002921 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05302922 supply = init_data->supply_regulator;
2923 else if (regulator_desc->supply_name)
2924 supply = regulator_desc->supply_name;
2925
2926 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002927 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01002928
Rajendra Nayak69511a42011-11-18 16:47:20 +05302929 r = regulator_dev_lookup(dev, supply);
Mark Brown0178f3e2010-04-26 15:18:14 +01002930
Rajendra Nayak69511a42011-11-18 16:47:20 +05302931 if (!r) {
2932 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00002933 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01002934 goto scrub;
2935 }
2936
2937 ret = set_supply(rdev, r);
2938 if (ret < 0)
2939 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05302940
2941 /* Enable supply if rail is enabled */
2942 if (rdev->desc->ops->is_enabled &&
2943 rdev->desc->ops->is_enabled(rdev)) {
2944 ret = regulator_enable(rdev->supply);
2945 if (ret < 0)
2946 goto scrub;
2947 }
Mark Brown0178f3e2010-04-26 15:18:14 +01002948 }
2949
Liam Girdwooda5766f12008-10-10 13:22:20 +01002950 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002951 if (init_data) {
2952 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2953 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00002954 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00002955 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00002956 if (ret < 0) {
2957 dev_err(dev, "Failed to set supply %s\n",
2958 init_data->consumer_supplies[i].supply);
2959 goto unset_supplies;
2960 }
Mark Brown23c2f042011-02-24 17:39:09 +00002961 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002962 }
2963
2964 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002965
2966 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002967out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002968 mutex_unlock(&regulator_list_mutex);
2969 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002970
Jani Nikulad4033b52010-04-29 10:55:11 +03002971unset_supplies:
2972 unset_regulator_supplies(rdev);
2973
David Brownell4fca9542008-11-11 17:38:53 -08002974scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08002975 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08002976 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002977 /* device core frees rdev */
2978 rdev = ERR_PTR(ret);
2979 goto out;
2980
David Brownell4fca9542008-11-11 17:38:53 -08002981clean:
2982 kfree(rdev);
2983 rdev = ERR_PTR(ret);
2984 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002985}
2986EXPORT_SYMBOL_GPL(regulator_register);
2987
2988/**
2989 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002990 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002991 *
2992 * Called by regulator drivers to unregister a regulator.
2993 */
2994void regulator_unregister(struct regulator_dev *rdev)
2995{
2996 if (rdev == NULL)
2997 return;
2998
Mark Browne032b372012-03-28 21:17:55 +01002999 if (rdev->supply)
3000 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003001 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003002 debugfs_remove_recursive(rdev->debugfs);
Mark Brownda07ecd2011-09-11 09:53:50 +01003003 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003004 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003005 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003006 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003007 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003008 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003009 mutex_unlock(&regulator_list_mutex);
3010}
3011EXPORT_SYMBOL_GPL(regulator_unregister);
3012
3013/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003014 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003015 * @state: system suspend state
3016 *
3017 * Configure each regulator with it's suspend operating parameters for state.
3018 * This will usually be called by machine suspend code prior to supending.
3019 */
3020int regulator_suspend_prepare(suspend_state_t state)
3021{
3022 struct regulator_dev *rdev;
3023 int ret = 0;
3024
3025 /* ON is handled by regulator active state */
3026 if (state == PM_SUSPEND_ON)
3027 return -EINVAL;
3028
3029 mutex_lock(&regulator_list_mutex);
3030 list_for_each_entry(rdev, &regulator_list, list) {
3031
3032 mutex_lock(&rdev->mutex);
3033 ret = suspend_prepare(rdev, state);
3034 mutex_unlock(&rdev->mutex);
3035
3036 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003037 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003038 goto out;
3039 }
3040 }
3041out:
3042 mutex_unlock(&regulator_list_mutex);
3043 return ret;
3044}
3045EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3046
3047/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003048 * regulator_suspend_finish - resume regulators from system wide suspend
3049 *
3050 * Turn on regulators that might be turned off by regulator_suspend_prepare
3051 * and that should be turned on according to the regulators properties.
3052 */
3053int regulator_suspend_finish(void)
3054{
3055 struct regulator_dev *rdev;
3056 int ret = 0, error;
3057
3058 mutex_lock(&regulator_list_mutex);
3059 list_for_each_entry(rdev, &regulator_list, list) {
3060 struct regulator_ops *ops = rdev->desc->ops;
3061
3062 mutex_lock(&rdev->mutex);
3063 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3064 ops->enable) {
3065 error = ops->enable(rdev);
3066 if (error)
3067 ret = error;
3068 } else {
3069 if (!has_full_constraints)
3070 goto unlock;
3071 if (!ops->disable)
3072 goto unlock;
3073 if (ops->is_enabled && !ops->is_enabled(rdev))
3074 goto unlock;
3075
3076 error = ops->disable(rdev);
3077 if (error)
3078 ret = error;
3079 }
3080unlock:
3081 mutex_unlock(&rdev->mutex);
3082 }
3083 mutex_unlock(&regulator_list_mutex);
3084 return ret;
3085}
3086EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3087
3088/**
Mark Brownca725562009-03-16 19:36:34 +00003089 * regulator_has_full_constraints - the system has fully specified constraints
3090 *
3091 * Calling this function will cause the regulator API to disable all
3092 * regulators which have a zero use count and don't have an always_on
3093 * constraint in a late_initcall.
3094 *
3095 * The intention is that this will become the default behaviour in a
3096 * future kernel release so users are encouraged to use this facility
3097 * now.
3098 */
3099void regulator_has_full_constraints(void)
3100{
3101 has_full_constraints = 1;
3102}
3103EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3104
3105/**
Mark Brown688fe992010-10-05 19:18:32 -07003106 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3107 *
3108 * Calling this function will cause the regulator API to provide a
3109 * dummy regulator to consumers if no physical regulator is found,
3110 * allowing most consumers to proceed as though a regulator were
3111 * configured. This allows systems such as those with software
3112 * controllable regulators for the CPU core only to be brought up more
3113 * readily.
3114 */
3115void regulator_use_dummy_regulator(void)
3116{
3117 board_wants_dummy_regulator = true;
3118}
3119EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3120
3121/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003122 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003123 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003124 *
3125 * Get rdev regulator driver private data. This call can be used in the
3126 * regulator driver context.
3127 */
3128void *rdev_get_drvdata(struct regulator_dev *rdev)
3129{
3130 return rdev->reg_data;
3131}
3132EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3133
3134/**
3135 * regulator_get_drvdata - get regulator driver data
3136 * @regulator: regulator
3137 *
3138 * Get regulator driver private data. This call can be used in the consumer
3139 * driver context when non API regulator specific functions need to be called.
3140 */
3141void *regulator_get_drvdata(struct regulator *regulator)
3142{
3143 return regulator->rdev->reg_data;
3144}
3145EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3146
3147/**
3148 * regulator_set_drvdata - set regulator driver data
3149 * @regulator: regulator
3150 * @data: data
3151 */
3152void regulator_set_drvdata(struct regulator *regulator, void *data)
3153{
3154 regulator->rdev->reg_data = data;
3155}
3156EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3157
3158/**
3159 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003160 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003161 */
3162int rdev_get_id(struct regulator_dev *rdev)
3163{
3164 return rdev->desc->id;
3165}
3166EXPORT_SYMBOL_GPL(rdev_get_id);
3167
Liam Girdwooda5766f12008-10-10 13:22:20 +01003168struct device *rdev_get_dev(struct regulator_dev *rdev)
3169{
3170 return &rdev->dev;
3171}
3172EXPORT_SYMBOL_GPL(rdev_get_dev);
3173
3174void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3175{
3176 return reg_init_data->driver_data;
3177}
3178EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3179
Mark Brownba55a972011-08-23 17:39:10 +01003180#ifdef CONFIG_DEBUG_FS
3181static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3182 size_t count, loff_t *ppos)
3183{
3184 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3185 ssize_t len, ret = 0;
3186 struct regulator_map *map;
3187
3188 if (!buf)
3189 return -ENOMEM;
3190
3191 list_for_each_entry(map, &regulator_map_list, list) {
3192 len = snprintf(buf + ret, PAGE_SIZE - ret,
3193 "%s -> %s.%s\n",
3194 rdev_get_name(map->regulator), map->dev_name,
3195 map->supply);
3196 if (len >= 0)
3197 ret += len;
3198 if (ret > PAGE_SIZE) {
3199 ret = PAGE_SIZE;
3200 break;
3201 }
3202 }
3203
3204 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3205
3206 kfree(buf);
3207
3208 return ret;
3209}
Stephen Boyd24751432012-02-20 22:50:42 -08003210#endif
Mark Brownba55a972011-08-23 17:39:10 +01003211
3212static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003213#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003214 .read = supply_map_read_file,
3215 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003216#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003217};
Mark Brownba55a972011-08-23 17:39:10 +01003218
Liam Girdwood414c70c2008-04-30 15:59:04 +01003219static int __init regulator_init(void)
3220{
Mark Brown34abbd62010-02-12 10:18:08 +00003221 int ret;
3222
Mark Brown34abbd62010-02-12 10:18:08 +00003223 ret = class_register(&regulator_class);
3224
Mark Brown1130e5b2010-12-21 23:49:31 +00003225 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003226 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003227 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003228
Mark Brownf4d562c2012-02-20 21:01:04 +00003229 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3230 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003231
Mark Brown34abbd62010-02-12 10:18:08 +00003232 regulator_dummy_init();
3233
3234 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003235}
3236
3237/* init early to allow our consumers to complete system booting */
3238core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003239
3240static int __init regulator_init_complete(void)
3241{
3242 struct regulator_dev *rdev;
3243 struct regulator_ops *ops;
3244 struct regulation_constraints *c;
3245 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003246
3247 mutex_lock(&regulator_list_mutex);
3248
3249 /* If we have a full configuration then disable any regulators
3250 * which are not in use or always_on. This will become the
3251 * default behaviour in the future.
3252 */
3253 list_for_each_entry(rdev, &regulator_list, list) {
3254 ops = rdev->desc->ops;
3255 c = rdev->constraints;
3256
Mark Brownf25e0b42009-08-03 18:49:55 +01003257 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003258 continue;
3259
3260 mutex_lock(&rdev->mutex);
3261
3262 if (rdev->use_count)
3263 goto unlock;
3264
3265 /* If we can't read the status assume it's on. */
3266 if (ops->is_enabled)
3267 enabled = ops->is_enabled(rdev);
3268 else
3269 enabled = 1;
3270
3271 if (!enabled)
3272 goto unlock;
3273
3274 if (has_full_constraints) {
3275 /* We log since this may kill the system if it
3276 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003277 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003278 ret = ops->disable(rdev);
3279 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003280 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003281 }
3282 } else {
3283 /* The intention is that in future we will
3284 * assume that full constraints are provided
3285 * so warn even if we aren't going to do
3286 * anything here.
3287 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003288 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003289 }
3290
3291unlock:
3292 mutex_unlock(&rdev->mutex);
3293 }
3294
3295 mutex_unlock(&regulator_list_mutex);
3296
3297 return 0;
3298}
3299late_initcall(regulator_init_complete);