blob: 10fb252323fbb007ba6183f2ea0ae5cefed39e1f [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
Mark Browncd6dffb2012-04-15 12:37:47 +01001728/**
1729 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1730 *
1731 * @rdev: regulator to operate on
1732 *
1733 * Regulators that use regmap for their register I/O can set the
1734 * enable_reg and enable_mask fields in their descriptor and then use
1735 * this as their is_enabled operation, saving some code.
1736 */
1737int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1738{
1739 unsigned int val;
1740 int ret;
1741
1742 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1743 if (ret != 0)
1744 return ret;
1745
1746 return (val & rdev->desc->enable_mask) != 0;
1747}
1748EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1749
1750/**
1751 * regulator_enable_regmap - standard enable() for regmap users
1752 *
1753 * @rdev: regulator to operate on
1754 *
1755 * Regulators that use regmap for their register I/O can set the
1756 * enable_reg and enable_mask fields in their descriptor and then use
1757 * this as their enable() operation, saving some code.
1758 */
1759int regulator_enable_regmap(struct regulator_dev *rdev)
1760{
1761 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1762 rdev->desc->enable_mask,
1763 rdev->desc->enable_mask);
1764}
1765EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1766
1767/**
1768 * regulator_disable_regmap - standard disable() for regmap users
1769 *
1770 * @rdev: regulator to operate on
1771 *
1772 * Regulators that use regmap for their register I/O can set the
1773 * enable_reg and enable_mask fields in their descriptor and then use
1774 * this as their disable() operation, saving some code.
1775 */
1776int regulator_disable_regmap(struct regulator_dev *rdev)
1777{
1778 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1779 rdev->desc->enable_mask, 0);
1780}
1781EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1782
Liam Girdwood414c70c2008-04-30 15:59:04 +01001783static int _regulator_is_enabled(struct regulator_dev *rdev)
1784{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001785 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001786 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001787 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001788
Mark Brown93325462009-08-03 18:49:56 +01001789 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001790}
1791
1792/**
1793 * regulator_is_enabled - is the regulator output enabled
1794 * @regulator: regulator source
1795 *
David Brownell412aec62008-11-16 11:44:46 -08001796 * Returns positive if the regulator driver backing the source/client
1797 * has requested that the device be enabled, zero if it hasn't, else a
1798 * negative errno code.
1799 *
1800 * Note that the device backing this regulator handle can have multiple
1801 * users, so it might be enabled even if regulator_enable() was never
1802 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001803 */
1804int regulator_is_enabled(struct regulator *regulator)
1805{
Mark Brown93325462009-08-03 18:49:56 +01001806 int ret;
1807
1808 mutex_lock(&regulator->rdev->mutex);
1809 ret = _regulator_is_enabled(regulator->rdev);
1810 mutex_unlock(&regulator->rdev->mutex);
1811
1812 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001813}
1814EXPORT_SYMBOL_GPL(regulator_is_enabled);
1815
1816/**
David Brownell4367cfd2009-02-26 11:48:36 -08001817 * regulator_count_voltages - count regulator_list_voltage() selectors
1818 * @regulator: regulator source
1819 *
1820 * Returns number of selectors, or negative errno. Selectors are
1821 * numbered starting at zero, and typically correspond to bitfields
1822 * in hardware registers.
1823 */
1824int regulator_count_voltages(struct regulator *regulator)
1825{
1826 struct regulator_dev *rdev = regulator->rdev;
1827
1828 return rdev->desc->n_voltages ? : -EINVAL;
1829}
1830EXPORT_SYMBOL_GPL(regulator_count_voltages);
1831
1832/**
1833 * regulator_list_voltage - enumerate supported voltages
1834 * @regulator: regulator source
1835 * @selector: identify voltage to list
1836 * Context: can sleep
1837 *
1838 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001839 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001840 * negative errno.
1841 */
1842int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1843{
1844 struct regulator_dev *rdev = regulator->rdev;
1845 struct regulator_ops *ops = rdev->desc->ops;
1846 int ret;
1847
1848 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1849 return -EINVAL;
1850
1851 mutex_lock(&rdev->mutex);
1852 ret = ops->list_voltage(rdev, selector);
1853 mutex_unlock(&rdev->mutex);
1854
1855 if (ret > 0) {
1856 if (ret < rdev->constraints->min_uV)
1857 ret = 0;
1858 else if (ret > rdev->constraints->max_uV)
1859 ret = 0;
1860 }
1861
1862 return ret;
1863}
1864EXPORT_SYMBOL_GPL(regulator_list_voltage);
1865
1866/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001867 * regulator_is_supported_voltage - check if a voltage range can be supported
1868 *
1869 * @regulator: Regulator to check.
1870 * @min_uV: Minimum required voltage in uV.
1871 * @max_uV: Maximum required voltage in uV.
1872 *
1873 * Returns a boolean or a negative error code.
1874 */
1875int regulator_is_supported_voltage(struct regulator *regulator,
1876 int min_uV, int max_uV)
1877{
1878 int i, voltages, ret;
1879
1880 ret = regulator_count_voltages(regulator);
1881 if (ret < 0)
1882 return ret;
1883 voltages = ret;
1884
1885 for (i = 0; i < voltages; i++) {
1886 ret = regulator_list_voltage(regulator, i);
1887
1888 if (ret >= min_uV && ret <= max_uV)
1889 return 1;
1890 }
1891
1892 return 0;
1893}
Mark Browna398eaa2011-12-28 12:48:45 +00001894EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01001895
Mark Brown4ab5b3d2012-04-15 11:23:30 +01001896/**
1897 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
1898 *
1899 * @rdev: regulator to operate on
1900 *
1901 * Regulators that use regmap for their register I/O can set the
1902 * vsel_reg and vsel_mask fields in their descriptor and then use this
1903 * as their get_voltage_vsel operation, saving some code.
1904 */
1905int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
1906{
1907 unsigned int val;
1908 int ret;
1909
1910 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
1911 if (ret != 0)
1912 return ret;
1913
1914 val &= rdev->desc->vsel_mask;
1915 val >>= ffs(rdev->desc->vsel_mask) - 1;
1916
1917 return val;
1918}
1919EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
1920
1921/**
1922 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
1923 *
1924 * @rdev: regulator to operate on
1925 * @sel: Selector to set
1926 *
1927 * Regulators that use regmap for their register I/O can set the
1928 * vsel_reg and vsel_mask fields in their descriptor and then use this
1929 * as their set_voltage_vsel operation, saving some code.
1930 */
1931int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
1932{
1933 sel <<= ffs(rdev->desc->vsel_mask) - 1;
1934
1935 return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
1936 rdev->desc->vsel_mask, sel);
1937}
1938EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
1939
Mark Brown75790252010-12-12 14:25:50 +00001940static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1941 int min_uV, int max_uV)
1942{
1943 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001944 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001945 unsigned int selector;
1946
1947 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1948
Mark Brownbf5892a2011-05-08 22:13:37 +01001949 min_uV += rdev->constraints->uV_offset;
1950 max_uV += rdev->constraints->uV_offset;
1951
Mark Brown75790252010-12-12 14:25:50 +00001952 if (rdev->desc->ops->set_voltage) {
1953 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1954 &selector);
1955
1956 if (rdev->desc->ops->list_voltage)
1957 selector = rdev->desc->ops->list_voltage(rdev,
1958 selector);
1959 else
1960 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001961 } else if (rdev->desc->ops->set_voltage_sel) {
1962 int best_val = INT_MAX;
1963 int i;
1964
1965 selector = 0;
1966
1967 /* Find the smallest voltage that falls within the specified
1968 * range.
1969 */
1970 for (i = 0; i < rdev->desc->n_voltages; i++) {
1971 ret = rdev->desc->ops->list_voltage(rdev, i);
1972 if (ret < 0)
1973 continue;
1974
1975 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1976 best_val = ret;
1977 selector = i;
1978 }
1979 }
1980
Linus Walleij77af1b2642011-03-17 13:24:36 +01001981 /*
1982 * If we can't obtain the old selector there is not enough
1983 * info to call set_voltage_time_sel().
1984 */
1985 if (rdev->desc->ops->set_voltage_time_sel &&
1986 rdev->desc->ops->get_voltage_sel) {
1987 unsigned int old_selector = 0;
1988
1989 ret = rdev->desc->ops->get_voltage_sel(rdev);
1990 if (ret < 0)
1991 return ret;
1992 old_selector = ret;
Axel Lin07351232012-02-24 23:13:19 +08001993 ret = rdev->desc->ops->set_voltage_time_sel(rdev,
Linus Walleij77af1b2642011-03-17 13:24:36 +01001994 old_selector, selector);
Axel Lin07351232012-02-24 23:13:19 +08001995 if (ret < 0)
1996 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
1997 else
1998 delay = ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001999 }
2000
Mark Browne8eef822010-12-12 14:36:17 +00002001 if (best_val != INT_MAX) {
2002 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2003 selector = best_val;
2004 } else {
2005 ret = -EINVAL;
2006 }
Mark Brown75790252010-12-12 14:25:50 +00002007 } else {
2008 ret = -EINVAL;
2009 }
2010
Linus Walleij77af1b2642011-03-17 13:24:36 +01002011 /* Insert any necessary delays */
2012 if (delay >= 1000) {
2013 mdelay(delay / 1000);
2014 udelay(delay % 1000);
2015 } else if (delay) {
2016 udelay(delay);
2017 }
2018
Mark Brownded06a52010-12-16 13:59:10 +00002019 if (ret == 0)
2020 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2021 NULL);
2022
Mark Brown75790252010-12-12 14:25:50 +00002023 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
2024
2025 return ret;
2026}
2027
Mark Browna7a1ad92009-07-21 16:00:24 +01002028/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002029 * regulator_set_voltage - set regulator output voltage
2030 * @regulator: regulator source
2031 * @min_uV: Minimum required voltage in uV
2032 * @max_uV: Maximum acceptable voltage in uV
2033 *
2034 * Sets a voltage regulator to the desired output voltage. This can be set
2035 * during any regulator state. IOW, regulator can be disabled or enabled.
2036 *
2037 * If the regulator is enabled then the voltage will change to the new value
2038 * immediately otherwise if the regulator is disabled the regulator will
2039 * output at the new voltage when enabled.
2040 *
2041 * NOTE: If the regulator is shared between several devices then the lowest
2042 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002043 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002044 * calling this function otherwise this call will fail.
2045 */
2046int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2047{
2048 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002049 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002050
2051 mutex_lock(&rdev->mutex);
2052
Mark Brown95a3c232010-12-16 15:49:37 +00002053 /* If we're setting the same range as last time the change
2054 * should be a noop (some cpufreq implementations use the same
2055 * voltage for multiple frequencies, for example).
2056 */
2057 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2058 goto out;
2059
Liam Girdwood414c70c2008-04-30 15:59:04 +01002060 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002061 if (!rdev->desc->ops->set_voltage &&
2062 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002063 ret = -EINVAL;
2064 goto out;
2065 }
2066
2067 /* constraints check */
2068 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2069 if (ret < 0)
2070 goto out;
2071 regulator->min_uV = min_uV;
2072 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002073
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002074 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2075 if (ret < 0)
2076 goto out;
2077
Mark Brown75790252010-12-12 14:25:50 +00002078 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00002079
Liam Girdwood414c70c2008-04-30 15:59:04 +01002080out:
2081 mutex_unlock(&rdev->mutex);
2082 return ret;
2083}
2084EXPORT_SYMBOL_GPL(regulator_set_voltage);
2085
Mark Brown606a2562010-12-16 15:49:36 +00002086/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002087 * regulator_set_voltage_time - get raise/fall time
2088 * @regulator: regulator source
2089 * @old_uV: starting voltage in microvolts
2090 * @new_uV: target voltage in microvolts
2091 *
2092 * Provided with the starting and ending voltage, this function attempts to
2093 * calculate the time in microseconds required to rise or fall to this new
2094 * voltage.
2095 */
2096int regulator_set_voltage_time(struct regulator *regulator,
2097 int old_uV, int new_uV)
2098{
2099 struct regulator_dev *rdev = regulator->rdev;
2100 struct regulator_ops *ops = rdev->desc->ops;
2101 int old_sel = -1;
2102 int new_sel = -1;
2103 int voltage;
2104 int i;
2105
2106 /* Currently requires operations to do this */
2107 if (!ops->list_voltage || !ops->set_voltage_time_sel
2108 || !rdev->desc->n_voltages)
2109 return -EINVAL;
2110
2111 for (i = 0; i < rdev->desc->n_voltages; i++) {
2112 /* We only look for exact voltage matches here */
2113 voltage = regulator_list_voltage(regulator, i);
2114 if (voltage < 0)
2115 return -EINVAL;
2116 if (voltage == 0)
2117 continue;
2118 if (voltage == old_uV)
2119 old_sel = i;
2120 if (voltage == new_uV)
2121 new_sel = i;
2122 }
2123
2124 if (old_sel < 0 || new_sel < 0)
2125 return -EINVAL;
2126
2127 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2128}
2129EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2130
2131/**
Mark Brown606a2562010-12-16 15:49:36 +00002132 * regulator_sync_voltage - re-apply last regulator output voltage
2133 * @regulator: regulator source
2134 *
2135 * Re-apply the last configured voltage. This is intended to be used
2136 * where some external control source the consumer is cooperating with
2137 * has caused the configured voltage to change.
2138 */
2139int regulator_sync_voltage(struct regulator *regulator)
2140{
2141 struct regulator_dev *rdev = regulator->rdev;
2142 int ret, min_uV, max_uV;
2143
2144 mutex_lock(&rdev->mutex);
2145
2146 if (!rdev->desc->ops->set_voltage &&
2147 !rdev->desc->ops->set_voltage_sel) {
2148 ret = -EINVAL;
2149 goto out;
2150 }
2151
2152 /* This is only going to work if we've had a voltage configured. */
2153 if (!regulator->min_uV && !regulator->max_uV) {
2154 ret = -EINVAL;
2155 goto out;
2156 }
2157
2158 min_uV = regulator->min_uV;
2159 max_uV = regulator->max_uV;
2160
2161 /* This should be a paranoia check... */
2162 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2163 if (ret < 0)
2164 goto out;
2165
2166 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2167 if (ret < 0)
2168 goto out;
2169
2170 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2171
2172out:
2173 mutex_unlock(&rdev->mutex);
2174 return ret;
2175}
2176EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2177
Liam Girdwood414c70c2008-04-30 15:59:04 +01002178static int _regulator_get_voltage(struct regulator_dev *rdev)
2179{
Mark Brownbf5892a2011-05-08 22:13:37 +01002180 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002181
2182 if (rdev->desc->ops->get_voltage_sel) {
2183 sel = rdev->desc->ops->get_voltage_sel(rdev);
2184 if (sel < 0)
2185 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002186 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002187 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002188 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002189 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002190 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002191 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002192
Axel Lincb220d12011-05-23 20:08:10 +08002193 if (ret < 0)
2194 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002195 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002196}
2197
2198/**
2199 * regulator_get_voltage - get regulator output voltage
2200 * @regulator: regulator source
2201 *
2202 * This returns the current regulator voltage in uV.
2203 *
2204 * NOTE: If the regulator is disabled it will return the voltage value. This
2205 * function should not be used to determine regulator state.
2206 */
2207int regulator_get_voltage(struct regulator *regulator)
2208{
2209 int ret;
2210
2211 mutex_lock(&regulator->rdev->mutex);
2212
2213 ret = _regulator_get_voltage(regulator->rdev);
2214
2215 mutex_unlock(&regulator->rdev->mutex);
2216
2217 return ret;
2218}
2219EXPORT_SYMBOL_GPL(regulator_get_voltage);
2220
2221/**
2222 * regulator_set_current_limit - set regulator output current limit
2223 * @regulator: regulator source
2224 * @min_uA: Minimuum supported current in uA
2225 * @max_uA: Maximum supported current in uA
2226 *
2227 * Sets current sink to the desired output current. This can be set during
2228 * any regulator state. IOW, regulator can be disabled or enabled.
2229 *
2230 * If the regulator is enabled then the current will change to the new value
2231 * immediately otherwise if the regulator is disabled the regulator will
2232 * output at the new current when enabled.
2233 *
2234 * NOTE: Regulator system constraints must be set for this regulator before
2235 * calling this function otherwise this call will fail.
2236 */
2237int regulator_set_current_limit(struct regulator *regulator,
2238 int min_uA, int max_uA)
2239{
2240 struct regulator_dev *rdev = regulator->rdev;
2241 int ret;
2242
2243 mutex_lock(&rdev->mutex);
2244
2245 /* sanity check */
2246 if (!rdev->desc->ops->set_current_limit) {
2247 ret = -EINVAL;
2248 goto out;
2249 }
2250
2251 /* constraints check */
2252 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2253 if (ret < 0)
2254 goto out;
2255
2256 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2257out:
2258 mutex_unlock(&rdev->mutex);
2259 return ret;
2260}
2261EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2262
2263static int _regulator_get_current_limit(struct regulator_dev *rdev)
2264{
2265 int ret;
2266
2267 mutex_lock(&rdev->mutex);
2268
2269 /* sanity check */
2270 if (!rdev->desc->ops->get_current_limit) {
2271 ret = -EINVAL;
2272 goto out;
2273 }
2274
2275 ret = rdev->desc->ops->get_current_limit(rdev);
2276out:
2277 mutex_unlock(&rdev->mutex);
2278 return ret;
2279}
2280
2281/**
2282 * regulator_get_current_limit - get regulator output current
2283 * @regulator: regulator source
2284 *
2285 * This returns the current supplied by the specified current sink in uA.
2286 *
2287 * NOTE: If the regulator is disabled it will return the current value. This
2288 * function should not be used to determine regulator state.
2289 */
2290int regulator_get_current_limit(struct regulator *regulator)
2291{
2292 return _regulator_get_current_limit(regulator->rdev);
2293}
2294EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2295
2296/**
2297 * regulator_set_mode - set regulator operating mode
2298 * @regulator: regulator source
2299 * @mode: operating mode - one of the REGULATOR_MODE constants
2300 *
2301 * Set regulator operating mode to increase regulator efficiency or improve
2302 * regulation performance.
2303 *
2304 * NOTE: Regulator system constraints must be set for this regulator before
2305 * calling this function otherwise this call will fail.
2306 */
2307int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2308{
2309 struct regulator_dev *rdev = regulator->rdev;
2310 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302311 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002312
2313 mutex_lock(&rdev->mutex);
2314
2315 /* sanity check */
2316 if (!rdev->desc->ops->set_mode) {
2317 ret = -EINVAL;
2318 goto out;
2319 }
2320
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302321 /* return if the same mode is requested */
2322 if (rdev->desc->ops->get_mode) {
2323 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2324 if (regulator_curr_mode == mode) {
2325 ret = 0;
2326 goto out;
2327 }
2328 }
2329
Liam Girdwood414c70c2008-04-30 15:59:04 +01002330 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002331 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002332 if (ret < 0)
2333 goto out;
2334
2335 ret = rdev->desc->ops->set_mode(rdev, mode);
2336out:
2337 mutex_unlock(&rdev->mutex);
2338 return ret;
2339}
2340EXPORT_SYMBOL_GPL(regulator_set_mode);
2341
2342static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2343{
2344 int ret;
2345
2346 mutex_lock(&rdev->mutex);
2347
2348 /* sanity check */
2349 if (!rdev->desc->ops->get_mode) {
2350 ret = -EINVAL;
2351 goto out;
2352 }
2353
2354 ret = rdev->desc->ops->get_mode(rdev);
2355out:
2356 mutex_unlock(&rdev->mutex);
2357 return ret;
2358}
2359
2360/**
2361 * regulator_get_mode - get regulator operating mode
2362 * @regulator: regulator source
2363 *
2364 * Get the current regulator operating mode.
2365 */
2366unsigned int regulator_get_mode(struct regulator *regulator)
2367{
2368 return _regulator_get_mode(regulator->rdev);
2369}
2370EXPORT_SYMBOL_GPL(regulator_get_mode);
2371
2372/**
2373 * regulator_set_optimum_mode - set regulator optimum operating mode
2374 * @regulator: regulator source
2375 * @uA_load: load current
2376 *
2377 * Notifies the regulator core of a new device load. This is then used by
2378 * DRMS (if enabled by constraints) to set the most efficient regulator
2379 * operating mode for the new regulator loading.
2380 *
2381 * Consumer devices notify their supply regulator of the maximum power
2382 * they will require (can be taken from device datasheet in the power
2383 * consumption tables) when they change operational status and hence power
2384 * state. Examples of operational state changes that can affect power
2385 * consumption are :-
2386 *
2387 * o Device is opened / closed.
2388 * o Device I/O is about to begin or has just finished.
2389 * o Device is idling in between work.
2390 *
2391 * This information is also exported via sysfs to userspace.
2392 *
2393 * DRMS will sum the total requested load on the regulator and change
2394 * to the most efficient operating mode if platform constraints allow.
2395 *
2396 * Returns the new regulator mode or error.
2397 */
2398int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2399{
2400 struct regulator_dev *rdev = regulator->rdev;
2401 struct regulator *consumer;
2402 int ret, output_uV, input_uV, total_uA_load = 0;
2403 unsigned int mode;
2404
2405 mutex_lock(&rdev->mutex);
2406
Mark Browna4b41482011-05-14 11:19:45 -07002407 /*
2408 * first check to see if we can set modes at all, otherwise just
2409 * tell the consumer everything is OK.
2410 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002411 regulator->uA_load = uA_load;
2412 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002413 if (ret < 0) {
2414 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002415 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002416 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002417
Liam Girdwood414c70c2008-04-30 15:59:04 +01002418 if (!rdev->desc->ops->get_optimum_mode)
2419 goto out;
2420
Mark Browna4b41482011-05-14 11:19:45 -07002421 /*
2422 * we can actually do this so any errors are indicators of
2423 * potential real failure.
2424 */
2425 ret = -EINVAL;
2426
Liam Girdwood414c70c2008-04-30 15:59:04 +01002427 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002428 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002429 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002430 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002431 goto out;
2432 }
2433
2434 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002435 input_uV = 0;
2436 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002437 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002438 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002439 input_uV = rdev->constraints->input_uV;
2440 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002441 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002442 goto out;
2443 }
2444
2445 /* calc total requested load for this regulator */
2446 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002447 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002448
2449 mode = rdev->desc->ops->get_optimum_mode(rdev,
2450 input_uV, output_uV,
2451 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002452 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002453 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002454 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2455 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002456 goto out;
2457 }
2458
2459 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002460 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002461 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002462 goto out;
2463 }
2464 ret = mode;
2465out:
2466 mutex_unlock(&rdev->mutex);
2467 return ret;
2468}
2469EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2470
2471/**
2472 * regulator_register_notifier - register regulator event notifier
2473 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002474 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002475 *
2476 * Register notifier block to receive regulator events.
2477 */
2478int regulator_register_notifier(struct regulator *regulator,
2479 struct notifier_block *nb)
2480{
2481 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2482 nb);
2483}
2484EXPORT_SYMBOL_GPL(regulator_register_notifier);
2485
2486/**
2487 * regulator_unregister_notifier - unregister regulator event notifier
2488 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002489 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002490 *
2491 * Unregister regulator event notifier block.
2492 */
2493int regulator_unregister_notifier(struct regulator *regulator,
2494 struct notifier_block *nb)
2495{
2496 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2497 nb);
2498}
2499EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2500
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002501/* notify regulator consumers and downstream regulator consumers.
2502 * Note mutex must be held by caller.
2503 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002504static void _notifier_call_chain(struct regulator_dev *rdev,
2505 unsigned long event, void *data)
2506{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002507 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002508 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002509}
2510
2511/**
2512 * regulator_bulk_get - get multiple regulator consumers
2513 *
2514 * @dev: Device to supply
2515 * @num_consumers: Number of consumers to register
2516 * @consumers: Configuration of consumers; clients are stored here.
2517 *
2518 * @return 0 on success, an errno on failure.
2519 *
2520 * This helper function allows drivers to get several regulator
2521 * consumers in one operation. If any of the regulators cannot be
2522 * acquired then any regulators that were allocated will be freed
2523 * before returning to the caller.
2524 */
2525int regulator_bulk_get(struct device *dev, int num_consumers,
2526 struct regulator_bulk_data *consumers)
2527{
2528 int i;
2529 int ret;
2530
2531 for (i = 0; i < num_consumers; i++)
2532 consumers[i].consumer = NULL;
2533
2534 for (i = 0; i < num_consumers; i++) {
2535 consumers[i].consumer = regulator_get(dev,
2536 consumers[i].supply);
2537 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002538 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002539 dev_err(dev, "Failed to get supply '%s': %d\n",
2540 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002541 consumers[i].consumer = NULL;
2542 goto err;
2543 }
2544 }
2545
2546 return 0;
2547
2548err:
Axel Linb29c7692012-02-20 10:32:16 +08002549 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002550 regulator_put(consumers[i].consumer);
2551
2552 return ret;
2553}
2554EXPORT_SYMBOL_GPL(regulator_bulk_get);
2555
Mark Browne6e74032012-01-20 20:10:08 +00002556/**
2557 * devm_regulator_bulk_get - managed get multiple regulator consumers
2558 *
2559 * @dev: Device to supply
2560 * @num_consumers: Number of consumers to register
2561 * @consumers: Configuration of consumers; clients are stored here.
2562 *
2563 * @return 0 on success, an errno on failure.
2564 *
2565 * This helper function allows drivers to get several regulator
2566 * consumers in one operation with management, the regulators will
2567 * automatically be freed when the device is unbound. If any of the
2568 * regulators cannot be acquired then any regulators that were
2569 * allocated will be freed before returning to the caller.
2570 */
2571int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2572 struct regulator_bulk_data *consumers)
2573{
2574 int i;
2575 int ret;
2576
2577 for (i = 0; i < num_consumers; i++)
2578 consumers[i].consumer = NULL;
2579
2580 for (i = 0; i < num_consumers; i++) {
2581 consumers[i].consumer = devm_regulator_get(dev,
2582 consumers[i].supply);
2583 if (IS_ERR(consumers[i].consumer)) {
2584 ret = PTR_ERR(consumers[i].consumer);
2585 dev_err(dev, "Failed to get supply '%s': %d\n",
2586 consumers[i].supply, ret);
2587 consumers[i].consumer = NULL;
2588 goto err;
2589 }
2590 }
2591
2592 return 0;
2593
2594err:
2595 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2596 devm_regulator_put(consumers[i].consumer);
2597
2598 return ret;
2599}
2600EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2601
Mark Brownf21e0e82011-05-24 08:12:40 +08002602static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2603{
2604 struct regulator_bulk_data *bulk = data;
2605
2606 bulk->ret = regulator_enable(bulk->consumer);
2607}
2608
Liam Girdwood414c70c2008-04-30 15:59:04 +01002609/**
2610 * regulator_bulk_enable - enable multiple regulator consumers
2611 *
2612 * @num_consumers: Number of consumers
2613 * @consumers: Consumer data; clients are stored here.
2614 * @return 0 on success, an errno on failure
2615 *
2616 * This convenience API allows consumers to enable multiple regulator
2617 * clients in a single API call. If any consumers cannot be enabled
2618 * then any others that were enabled will be disabled again prior to
2619 * return.
2620 */
2621int regulator_bulk_enable(int num_consumers,
2622 struct regulator_bulk_data *consumers)
2623{
Mark Brownf21e0e82011-05-24 08:12:40 +08002624 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002625 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002626 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002627
Mark Brownf21e0e82011-05-24 08:12:40 +08002628 for (i = 0; i < num_consumers; i++)
2629 async_schedule_domain(regulator_bulk_enable_async,
2630 &consumers[i], &async_domain);
2631
2632 async_synchronize_full_domain(&async_domain);
2633
2634 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002635 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002636 if (consumers[i].ret != 0) {
2637 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002638 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002639 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002640 }
2641
2642 return 0;
2643
2644err:
Axel Linb29c7692012-02-20 10:32:16 +08002645 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2646 while (--i >= 0)
2647 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002648
2649 return ret;
2650}
2651EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2652
2653/**
2654 * regulator_bulk_disable - disable multiple regulator consumers
2655 *
2656 * @num_consumers: Number of consumers
2657 * @consumers: Consumer data; clients are stored here.
2658 * @return 0 on success, an errno on failure
2659 *
2660 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002661 * clients in a single API call. If any consumers cannot be disabled
2662 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002663 * return.
2664 */
2665int regulator_bulk_disable(int num_consumers,
2666 struct regulator_bulk_data *consumers)
2667{
2668 int i;
2669 int ret;
2670
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002671 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002672 ret = regulator_disable(consumers[i].consumer);
2673 if (ret != 0)
2674 goto err;
2675 }
2676
2677 return 0;
2678
2679err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002680 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002681 for (++i; i < num_consumers; ++i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002682 regulator_enable(consumers[i].consumer);
2683
2684 return ret;
2685}
2686EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2687
2688/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002689 * regulator_bulk_force_disable - force disable multiple regulator consumers
2690 *
2691 * @num_consumers: Number of consumers
2692 * @consumers: Consumer data; clients are stored here.
2693 * @return 0 on success, an errno on failure
2694 *
2695 * This convenience API allows consumers to forcibly disable multiple regulator
2696 * clients in a single API call.
2697 * NOTE: This should be used for situations when device damage will
2698 * likely occur if the regulators are not disabled (e.g. over temp).
2699 * Although regulator_force_disable function call for some consumers can
2700 * return error numbers, the function is called for all consumers.
2701 */
2702int regulator_bulk_force_disable(int num_consumers,
2703 struct regulator_bulk_data *consumers)
2704{
2705 int i;
2706 int ret;
2707
2708 for (i = 0; i < num_consumers; i++)
2709 consumers[i].ret =
2710 regulator_force_disable(consumers[i].consumer);
2711
2712 for (i = 0; i < num_consumers; i++) {
2713 if (consumers[i].ret != 0) {
2714 ret = consumers[i].ret;
2715 goto out;
2716 }
2717 }
2718
2719 return 0;
2720out:
2721 return ret;
2722}
2723EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2724
2725/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002726 * regulator_bulk_free - free multiple regulator consumers
2727 *
2728 * @num_consumers: Number of consumers
2729 * @consumers: Consumer data; clients are stored here.
2730 *
2731 * This convenience API allows consumers to free multiple regulator
2732 * clients in a single API call.
2733 */
2734void regulator_bulk_free(int num_consumers,
2735 struct regulator_bulk_data *consumers)
2736{
2737 int i;
2738
2739 for (i = 0; i < num_consumers; i++) {
2740 regulator_put(consumers[i].consumer);
2741 consumers[i].consumer = NULL;
2742 }
2743}
2744EXPORT_SYMBOL_GPL(regulator_bulk_free);
2745
2746/**
2747 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002748 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002749 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002750 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002751 *
2752 * Called by regulator drivers to notify clients a regulator event has
2753 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002754 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002755 */
2756int regulator_notifier_call_chain(struct regulator_dev *rdev,
2757 unsigned long event, void *data)
2758{
2759 _notifier_call_chain(rdev, event, data);
2760 return NOTIFY_DONE;
2761
2762}
2763EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2764
Mark Brownbe721972009-08-04 20:09:52 +02002765/**
2766 * regulator_mode_to_status - convert a regulator mode into a status
2767 *
2768 * @mode: Mode to convert
2769 *
2770 * Convert a regulator mode into a status.
2771 */
2772int regulator_mode_to_status(unsigned int mode)
2773{
2774 switch (mode) {
2775 case REGULATOR_MODE_FAST:
2776 return REGULATOR_STATUS_FAST;
2777 case REGULATOR_MODE_NORMAL:
2778 return REGULATOR_STATUS_NORMAL;
2779 case REGULATOR_MODE_IDLE:
2780 return REGULATOR_STATUS_IDLE;
2781 case REGULATOR_STATUS_STANDBY:
2782 return REGULATOR_STATUS_STANDBY;
2783 default:
2784 return 0;
2785 }
2786}
2787EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2788
David Brownell7ad68e22008-11-11 17:39:02 -08002789/*
2790 * To avoid cluttering sysfs (and memory) with useless state, only
2791 * create attributes that can be meaningfully displayed.
2792 */
2793static int add_regulator_attributes(struct regulator_dev *rdev)
2794{
2795 struct device *dev = &rdev->dev;
2796 struct regulator_ops *ops = rdev->desc->ops;
2797 int status = 0;
2798
2799 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002800 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2801 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002802 status = device_create_file(dev, &dev_attr_microvolts);
2803 if (status < 0)
2804 return status;
2805 }
2806 if (ops->get_current_limit) {
2807 status = device_create_file(dev, &dev_attr_microamps);
2808 if (status < 0)
2809 return status;
2810 }
2811 if (ops->get_mode) {
2812 status = device_create_file(dev, &dev_attr_opmode);
2813 if (status < 0)
2814 return status;
2815 }
2816 if (ops->is_enabled) {
2817 status = device_create_file(dev, &dev_attr_state);
2818 if (status < 0)
2819 return status;
2820 }
David Brownell853116a2009-01-14 23:03:17 -08002821 if (ops->get_status) {
2822 status = device_create_file(dev, &dev_attr_status);
2823 if (status < 0)
2824 return status;
2825 }
David Brownell7ad68e22008-11-11 17:39:02 -08002826
2827 /* some attributes are type-specific */
2828 if (rdev->desc->type == REGULATOR_CURRENT) {
2829 status = device_create_file(dev, &dev_attr_requested_microamps);
2830 if (status < 0)
2831 return status;
2832 }
2833
2834 /* all the other attributes exist to support constraints;
2835 * don't show them if there are no constraints, or if the
2836 * relevant supporting methods are missing.
2837 */
2838 if (!rdev->constraints)
2839 return status;
2840
2841 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002842 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002843 status = device_create_file(dev, &dev_attr_min_microvolts);
2844 if (status < 0)
2845 return status;
2846 status = device_create_file(dev, &dev_attr_max_microvolts);
2847 if (status < 0)
2848 return status;
2849 }
2850 if (ops->set_current_limit) {
2851 status = device_create_file(dev, &dev_attr_min_microamps);
2852 if (status < 0)
2853 return status;
2854 status = device_create_file(dev, &dev_attr_max_microamps);
2855 if (status < 0)
2856 return status;
2857 }
2858
2859 /* suspend mode constraints need multiple supporting methods */
2860 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2861 return status;
2862
2863 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2864 if (status < 0)
2865 return status;
2866 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2867 if (status < 0)
2868 return status;
2869 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2870 if (status < 0)
2871 return status;
2872
2873 if (ops->set_suspend_voltage) {
2874 status = device_create_file(dev,
2875 &dev_attr_suspend_standby_microvolts);
2876 if (status < 0)
2877 return status;
2878 status = device_create_file(dev,
2879 &dev_attr_suspend_mem_microvolts);
2880 if (status < 0)
2881 return status;
2882 status = device_create_file(dev,
2883 &dev_attr_suspend_disk_microvolts);
2884 if (status < 0)
2885 return status;
2886 }
2887
2888 if (ops->set_suspend_mode) {
2889 status = device_create_file(dev,
2890 &dev_attr_suspend_standby_mode);
2891 if (status < 0)
2892 return status;
2893 status = device_create_file(dev,
2894 &dev_attr_suspend_mem_mode);
2895 if (status < 0)
2896 return status;
2897 status = device_create_file(dev,
2898 &dev_attr_suspend_disk_mode);
2899 if (status < 0)
2900 return status;
2901 }
2902
2903 return status;
2904}
2905
Mark Brown1130e5b2010-12-21 23:49:31 +00002906static void rdev_init_debugfs(struct regulator_dev *rdev)
2907{
Mark Brown1130e5b2010-12-21 23:49:31 +00002908 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08002909 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00002910 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00002911 return;
2912 }
2913
2914 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2915 &rdev->use_count);
2916 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2917 &rdev->open_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00002918}
2919
Liam Girdwood414c70c2008-04-30 15:59:04 +01002920/**
2921 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002922 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01002923 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002924 *
2925 * Called by regulator drivers to register a regulator.
2926 * Returns 0 on success.
2927 */
Mark Brown65f26842012-04-03 20:46:53 +01002928struct regulator_dev *
2929regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01002930 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002931{
Mark Brown9a8f5e02011-11-29 18:11:19 +00002932 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01002933 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002934 static atomic_t regulator_no = ATOMIC_INIT(0);
2935 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01002936 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002937 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05302938 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002939
Mark Brownc1727082012-04-04 00:50:22 +01002940 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002941 return ERR_PTR(-EINVAL);
2942
Mark Brown32c8fad2012-04-11 10:19:12 +01002943 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01002944 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01002945
Liam Girdwood414c70c2008-04-30 15:59:04 +01002946 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2947 return ERR_PTR(-EINVAL);
2948
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002949 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2950 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002951 return ERR_PTR(-EINVAL);
2952
Mark Brown476c2d82010-12-10 17:28:07 +00002953 /* Only one of each should be implemented */
2954 WARN_ON(regulator_desc->ops->get_voltage &&
2955 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002956 WARN_ON(regulator_desc->ops->set_voltage &&
2957 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002958
2959 /* If we're using selectors we must implement list_voltage. */
2960 if (regulator_desc->ops->get_voltage_sel &&
2961 !regulator_desc->ops->list_voltage) {
2962 return ERR_PTR(-EINVAL);
2963 }
Mark Browne8eef822010-12-12 14:36:17 +00002964 if (regulator_desc->ops->set_voltage_sel &&
2965 !regulator_desc->ops->list_voltage) {
2966 return ERR_PTR(-EINVAL);
2967 }
Mark Brown476c2d82010-12-10 17:28:07 +00002968
Mark Brownc1727082012-04-04 00:50:22 +01002969 init_data = config->init_data;
2970
Liam Girdwood414c70c2008-04-30 15:59:04 +01002971 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2972 if (rdev == NULL)
2973 return ERR_PTR(-ENOMEM);
2974
2975 mutex_lock(&regulator_list_mutex);
2976
2977 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01002978 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002979 rdev->owner = regulator_desc->owner;
2980 rdev->desc = regulator_desc;
Mark Brown65b19ce2012-04-15 11:16:05 +01002981 rdev->regmap = config->regmap;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002982 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002983 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002984 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01002985 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002986
Liam Girdwooda5766f12008-10-10 13:22:20 +01002987 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00002988 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01002989 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002990 if (ret < 0)
2991 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002992 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002993
Liam Girdwooda5766f12008-10-10 13:22:20 +01002994 /* register with sysfs */
2995 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01002996 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002997 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002998 dev_set_name(&rdev->dev, "regulator.%d",
2999 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003000 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003001 if (ret != 0) {
3002 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003003 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003004 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003005
3006 dev_set_drvdata(&rdev->dev, rdev);
3007
Mike Rapoport74f544c2008-11-25 14:53:53 +02003008 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003009 if (init_data)
3010 constraints = &init_data->constraints;
3011
3012 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003013 if (ret < 0)
3014 goto scrub;
3015
David Brownell7ad68e22008-11-11 17:39:02 -08003016 /* add attributes supported by this regulator */
3017 ret = add_regulator_attributes(rdev);
3018 if (ret < 0)
3019 goto scrub;
3020
Mark Brown9a8f5e02011-11-29 18:11:19 +00003021 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303022 supply = init_data->supply_regulator;
3023 else if (regulator_desc->supply_name)
3024 supply = regulator_desc->supply_name;
3025
3026 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003027 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003028
Rajendra Nayak69511a42011-11-18 16:47:20 +05303029 r = regulator_dev_lookup(dev, supply);
Mark Brown0178f3e2010-04-26 15:18:14 +01003030
Rajendra Nayak69511a42011-11-18 16:47:20 +05303031 if (!r) {
3032 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003033 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003034 goto scrub;
3035 }
3036
3037 ret = set_supply(rdev, r);
3038 if (ret < 0)
3039 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303040
3041 /* Enable supply if rail is enabled */
3042 if (rdev->desc->ops->is_enabled &&
3043 rdev->desc->ops->is_enabled(rdev)) {
3044 ret = regulator_enable(rdev->supply);
3045 if (ret < 0)
3046 goto scrub;
3047 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003048 }
3049
Liam Girdwooda5766f12008-10-10 13:22:20 +01003050 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003051 if (init_data) {
3052 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3053 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003054 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003055 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003056 if (ret < 0) {
3057 dev_err(dev, "Failed to set supply %s\n",
3058 init_data->consumer_supplies[i].supply);
3059 goto unset_supplies;
3060 }
Mark Brown23c2f042011-02-24 17:39:09 +00003061 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003062 }
3063
3064 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003065
3066 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003067out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003068 mutex_unlock(&regulator_list_mutex);
3069 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003070
Jani Nikulad4033b52010-04-29 10:55:11 +03003071unset_supplies:
3072 unset_regulator_supplies(rdev);
3073
David Brownell4fca9542008-11-11 17:38:53 -08003074scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08003075 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08003076 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003077 /* device core frees rdev */
3078 rdev = ERR_PTR(ret);
3079 goto out;
3080
David Brownell4fca9542008-11-11 17:38:53 -08003081clean:
3082 kfree(rdev);
3083 rdev = ERR_PTR(ret);
3084 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003085}
3086EXPORT_SYMBOL_GPL(regulator_register);
3087
3088/**
3089 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003090 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003091 *
3092 * Called by regulator drivers to unregister a regulator.
3093 */
3094void regulator_unregister(struct regulator_dev *rdev)
3095{
3096 if (rdev == NULL)
3097 return;
3098
Mark Browne032b372012-03-28 21:17:55 +01003099 if (rdev->supply)
3100 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003101 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003102 debugfs_remove_recursive(rdev->debugfs);
Mark Brownda07ecd2011-09-11 09:53:50 +01003103 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003104 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003105 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003106 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003107 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003108 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003109 mutex_unlock(&regulator_list_mutex);
3110}
3111EXPORT_SYMBOL_GPL(regulator_unregister);
3112
3113/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003114 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003115 * @state: system suspend state
3116 *
3117 * Configure each regulator with it's suspend operating parameters for state.
3118 * This will usually be called by machine suspend code prior to supending.
3119 */
3120int regulator_suspend_prepare(suspend_state_t state)
3121{
3122 struct regulator_dev *rdev;
3123 int ret = 0;
3124
3125 /* ON is handled by regulator active state */
3126 if (state == PM_SUSPEND_ON)
3127 return -EINVAL;
3128
3129 mutex_lock(&regulator_list_mutex);
3130 list_for_each_entry(rdev, &regulator_list, list) {
3131
3132 mutex_lock(&rdev->mutex);
3133 ret = suspend_prepare(rdev, state);
3134 mutex_unlock(&rdev->mutex);
3135
3136 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003137 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003138 goto out;
3139 }
3140 }
3141out:
3142 mutex_unlock(&regulator_list_mutex);
3143 return ret;
3144}
3145EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3146
3147/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003148 * regulator_suspend_finish - resume regulators from system wide suspend
3149 *
3150 * Turn on regulators that might be turned off by regulator_suspend_prepare
3151 * and that should be turned on according to the regulators properties.
3152 */
3153int regulator_suspend_finish(void)
3154{
3155 struct regulator_dev *rdev;
3156 int ret = 0, error;
3157
3158 mutex_lock(&regulator_list_mutex);
3159 list_for_each_entry(rdev, &regulator_list, list) {
3160 struct regulator_ops *ops = rdev->desc->ops;
3161
3162 mutex_lock(&rdev->mutex);
3163 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3164 ops->enable) {
3165 error = ops->enable(rdev);
3166 if (error)
3167 ret = error;
3168 } else {
3169 if (!has_full_constraints)
3170 goto unlock;
3171 if (!ops->disable)
3172 goto unlock;
3173 if (ops->is_enabled && !ops->is_enabled(rdev))
3174 goto unlock;
3175
3176 error = ops->disable(rdev);
3177 if (error)
3178 ret = error;
3179 }
3180unlock:
3181 mutex_unlock(&rdev->mutex);
3182 }
3183 mutex_unlock(&regulator_list_mutex);
3184 return ret;
3185}
3186EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3187
3188/**
Mark Brownca725562009-03-16 19:36:34 +00003189 * regulator_has_full_constraints - the system has fully specified constraints
3190 *
3191 * Calling this function will cause the regulator API to disable all
3192 * regulators which have a zero use count and don't have an always_on
3193 * constraint in a late_initcall.
3194 *
3195 * The intention is that this will become the default behaviour in a
3196 * future kernel release so users are encouraged to use this facility
3197 * now.
3198 */
3199void regulator_has_full_constraints(void)
3200{
3201 has_full_constraints = 1;
3202}
3203EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3204
3205/**
Mark Brown688fe992010-10-05 19:18:32 -07003206 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3207 *
3208 * Calling this function will cause the regulator API to provide a
3209 * dummy regulator to consumers if no physical regulator is found,
3210 * allowing most consumers to proceed as though a regulator were
3211 * configured. This allows systems such as those with software
3212 * controllable regulators for the CPU core only to be brought up more
3213 * readily.
3214 */
3215void regulator_use_dummy_regulator(void)
3216{
3217 board_wants_dummy_regulator = true;
3218}
3219EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3220
3221/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003222 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003223 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003224 *
3225 * Get rdev regulator driver private data. This call can be used in the
3226 * regulator driver context.
3227 */
3228void *rdev_get_drvdata(struct regulator_dev *rdev)
3229{
3230 return rdev->reg_data;
3231}
3232EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3233
3234/**
3235 * regulator_get_drvdata - get regulator driver data
3236 * @regulator: regulator
3237 *
3238 * Get regulator driver private data. This call can be used in the consumer
3239 * driver context when non API regulator specific functions need to be called.
3240 */
3241void *regulator_get_drvdata(struct regulator *regulator)
3242{
3243 return regulator->rdev->reg_data;
3244}
3245EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3246
3247/**
3248 * regulator_set_drvdata - set regulator driver data
3249 * @regulator: regulator
3250 * @data: data
3251 */
3252void regulator_set_drvdata(struct regulator *regulator, void *data)
3253{
3254 regulator->rdev->reg_data = data;
3255}
3256EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3257
3258/**
3259 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003260 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003261 */
3262int rdev_get_id(struct regulator_dev *rdev)
3263{
3264 return rdev->desc->id;
3265}
3266EXPORT_SYMBOL_GPL(rdev_get_id);
3267
Liam Girdwooda5766f12008-10-10 13:22:20 +01003268struct device *rdev_get_dev(struct regulator_dev *rdev)
3269{
3270 return &rdev->dev;
3271}
3272EXPORT_SYMBOL_GPL(rdev_get_dev);
3273
3274void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3275{
3276 return reg_init_data->driver_data;
3277}
3278EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3279
Mark Brownba55a972011-08-23 17:39:10 +01003280#ifdef CONFIG_DEBUG_FS
3281static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3282 size_t count, loff_t *ppos)
3283{
3284 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3285 ssize_t len, ret = 0;
3286 struct regulator_map *map;
3287
3288 if (!buf)
3289 return -ENOMEM;
3290
3291 list_for_each_entry(map, &regulator_map_list, list) {
3292 len = snprintf(buf + ret, PAGE_SIZE - ret,
3293 "%s -> %s.%s\n",
3294 rdev_get_name(map->regulator), map->dev_name,
3295 map->supply);
3296 if (len >= 0)
3297 ret += len;
3298 if (ret > PAGE_SIZE) {
3299 ret = PAGE_SIZE;
3300 break;
3301 }
3302 }
3303
3304 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3305
3306 kfree(buf);
3307
3308 return ret;
3309}
Stephen Boyd24751432012-02-20 22:50:42 -08003310#endif
Mark Brownba55a972011-08-23 17:39:10 +01003311
3312static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003313#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003314 .read = supply_map_read_file,
3315 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003316#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003317};
Mark Brownba55a972011-08-23 17:39:10 +01003318
Liam Girdwood414c70c2008-04-30 15:59:04 +01003319static int __init regulator_init(void)
3320{
Mark Brown34abbd62010-02-12 10:18:08 +00003321 int ret;
3322
Mark Brown34abbd62010-02-12 10:18:08 +00003323 ret = class_register(&regulator_class);
3324
Mark Brown1130e5b2010-12-21 23:49:31 +00003325 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003326 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003327 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003328
Mark Brownf4d562c2012-02-20 21:01:04 +00003329 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3330 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003331
Mark Brown34abbd62010-02-12 10:18:08 +00003332 regulator_dummy_init();
3333
3334 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003335}
3336
3337/* init early to allow our consumers to complete system booting */
3338core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003339
3340static int __init regulator_init_complete(void)
3341{
3342 struct regulator_dev *rdev;
3343 struct regulator_ops *ops;
3344 struct regulation_constraints *c;
3345 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003346
3347 mutex_lock(&regulator_list_mutex);
3348
3349 /* If we have a full configuration then disable any regulators
3350 * which are not in use or always_on. This will become the
3351 * default behaviour in the future.
3352 */
3353 list_for_each_entry(rdev, &regulator_list, list) {
3354 ops = rdev->desc->ops;
3355 c = rdev->constraints;
3356
Mark Brownf25e0b42009-08-03 18:49:55 +01003357 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003358 continue;
3359
3360 mutex_lock(&rdev->mutex);
3361
3362 if (rdev->use_count)
3363 goto unlock;
3364
3365 /* If we can't read the status assume it's on. */
3366 if (ops->is_enabled)
3367 enabled = ops->is_enabled(rdev);
3368 else
3369 enabled = 1;
3370
3371 if (!enabled)
3372 goto unlock;
3373
3374 if (has_full_constraints) {
3375 /* We log since this may kill the system if it
3376 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003377 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003378 ret = ops->disable(rdev);
3379 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003380 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003381 }
3382 } else {
3383 /* The intention is that in future we will
3384 * assume that full constraints are provided
3385 * so warn even if we aren't going to do
3386 * anything here.
3387 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003388 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003389 }
3390
3391unlock:
3392 mutex_unlock(&rdev->mutex);
3393 }
3394
3395 mutex_unlock(&regulator_list_mutex);
3396
3397 return 0;
3398}
3399late_initcall(regulator_init_complete);