blob: 00c787c01f3b6dfb02dc4e6b04d109a5f2125a5c [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;
Mark Brown6492bc12012-04-19 13:19:07 +010078 unsigned int always_on:1;
Liam Girdwood414c70c2008-04-30 15:59:04 +010079 int uA_load;
80 int min_uV;
81 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010082 char *supply_name;
83 struct device_attribute dev_attr;
84 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010085 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010086};
87
88static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010089static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010090static int _regulator_get_voltage(struct regulator_dev *rdev);
91static int _regulator_get_current_limit(struct regulator_dev *rdev);
92static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
93static void _notifier_call_chain(struct regulator_dev *rdev,
94 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000095static int _regulator_do_set_voltage(struct regulator_dev *rdev,
96 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010097static struct regulator *create_regulator(struct regulator_dev *rdev,
98 struct device *dev,
99 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100100
Mark Brown1083c392009-10-22 16:31:32 +0100101static const char *rdev_get_name(struct regulator_dev *rdev)
102{
103 if (rdev->constraints && rdev->constraints->name)
104 return rdev->constraints->name;
105 else if (rdev->desc->name)
106 return rdev->desc->name;
107 else
108 return "";
109}
110
Liam Girdwood414c70c2008-04-30 15:59:04 +0100111/* gets the regulator for a given consumer device */
112static struct regulator *get_device_regulator(struct device *dev)
113{
114 struct regulator *regulator = NULL;
115 struct regulator_dev *rdev;
116
117 mutex_lock(&regulator_list_mutex);
118 list_for_each_entry(rdev, &regulator_list, list) {
119 mutex_lock(&rdev->mutex);
120 list_for_each_entry(regulator, &rdev->consumer_list, list) {
121 if (regulator->dev == dev) {
122 mutex_unlock(&rdev->mutex);
123 mutex_unlock(&regulator_list_mutex);
124 return regulator;
125 }
126 }
127 mutex_unlock(&rdev->mutex);
128 }
129 mutex_unlock(&regulator_list_mutex);
130 return NULL;
131}
132
Rajendra Nayak69511a42011-11-18 16:47:20 +0530133/**
134 * of_get_regulator - get a regulator device node based on supply name
135 * @dev: Device pointer for the consumer (of regulator) device
136 * @supply: regulator supply name
137 *
138 * Extract the regulator device node corresponding to the supply name.
139 * retruns the device node corresponding to the regulator if found, else
140 * returns NULL.
141 */
142static struct device_node *of_get_regulator(struct device *dev, const char *supply)
143{
144 struct device_node *regnode = NULL;
145 char prop_name[32]; /* 32 is max size of property name */
146
147 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
148
149 snprintf(prop_name, 32, "%s-supply", supply);
150 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
151
152 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530153 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530154 prop_name, dev->of_node->full_name);
155 return NULL;
156 }
157 return regnode;
158}
159
Mark Brown6492bc12012-04-19 13:19:07 +0100160static int _regulator_can_change_status(struct regulator_dev *rdev)
161{
162 if (!rdev->constraints)
163 return 0;
164
165 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
166 return 1;
167 else
168 return 0;
169}
170
Liam Girdwood414c70c2008-04-30 15:59:04 +0100171/* Platform voltage constraint check */
172static int regulator_check_voltage(struct regulator_dev *rdev,
173 int *min_uV, int *max_uV)
174{
175 BUG_ON(*min_uV > *max_uV);
176
177 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800178 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100179 return -ENODEV;
180 }
181 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800182 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100183 return -EPERM;
184 }
185
186 if (*max_uV > rdev->constraints->max_uV)
187 *max_uV = rdev->constraints->max_uV;
188 if (*min_uV < rdev->constraints->min_uV)
189 *min_uV = rdev->constraints->min_uV;
190
Mark Brown89f425e2011-07-12 11:20:37 +0900191 if (*min_uV > *max_uV) {
192 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100193 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100194 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900195 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100196
197 return 0;
198}
199
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100200/* Make sure we select a voltage that suits the needs of all
201 * regulator consumers
202 */
203static int regulator_check_consumers(struct regulator_dev *rdev,
204 int *min_uV, int *max_uV)
205{
206 struct regulator *regulator;
207
208 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700209 /*
210 * Assume consumers that didn't say anything are OK
211 * with anything in the constraint range.
212 */
213 if (!regulator->min_uV && !regulator->max_uV)
214 continue;
215
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100216 if (*max_uV > regulator->max_uV)
217 *max_uV = regulator->max_uV;
218 if (*min_uV < regulator->min_uV)
219 *min_uV = regulator->min_uV;
220 }
221
222 if (*min_uV > *max_uV)
223 return -EINVAL;
224
225 return 0;
226}
227
Liam Girdwood414c70c2008-04-30 15:59:04 +0100228/* current constraint check */
229static int regulator_check_current_limit(struct regulator_dev *rdev,
230 int *min_uA, int *max_uA)
231{
232 BUG_ON(*min_uA > *max_uA);
233
234 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800235 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100236 return -ENODEV;
237 }
238 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800239 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100240 return -EPERM;
241 }
242
243 if (*max_uA > rdev->constraints->max_uA)
244 *max_uA = rdev->constraints->max_uA;
245 if (*min_uA < rdev->constraints->min_uA)
246 *min_uA = rdev->constraints->min_uA;
247
Mark Brown89f425e2011-07-12 11:20:37 +0900248 if (*min_uA > *max_uA) {
249 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100250 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100251 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900252 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100253
254 return 0;
255}
256
257/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900258static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100259{
Mark Brown2c608232011-03-30 06:29:12 +0900260 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800261 case REGULATOR_MODE_FAST:
262 case REGULATOR_MODE_NORMAL:
263 case REGULATOR_MODE_IDLE:
264 case REGULATOR_MODE_STANDBY:
265 break;
266 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900267 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800268 return -EINVAL;
269 }
270
Liam Girdwood414c70c2008-04-30 15:59:04 +0100271 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800272 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100273 return -ENODEV;
274 }
275 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800276 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100277 return -EPERM;
278 }
Mark Brown2c608232011-03-30 06:29:12 +0900279
280 /* The modes are bitmasks, the most power hungry modes having
281 * the lowest values. If the requested mode isn't supported
282 * try higher modes. */
283 while (*mode) {
284 if (rdev->constraints->valid_modes_mask & *mode)
285 return 0;
286 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100287 }
Mark Brown2c608232011-03-30 06:29:12 +0900288
289 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100290}
291
292/* dynamic regulator mode switching constraint check */
293static int regulator_check_drms(struct regulator_dev *rdev)
294{
295 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800296 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100297 return -ENODEV;
298 }
299 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800300 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100301 return -EPERM;
302 }
303 return 0;
304}
305
Liam Girdwood414c70c2008-04-30 15:59:04 +0100306static 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)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800653 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000654 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
678 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800679 * only warn if the driver implements set_suspend_voltage or
680 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100681 */
682 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800683 if (rdev->desc->ops->set_suspend_voltage ||
684 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800685 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100686 return 0;
687 }
688
689 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800690 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100691 return -EINVAL;
692 }
693
Axel Lin8ac0e952012-04-14 09:14:34 +0800694 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100695 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800696 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100697 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800698 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
699 ret = 0;
700
Liam Girdwood414c70c2008-04-30 15:59:04 +0100701 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800702 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100703 return ret;
704 }
705
706 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
707 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
708 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800709 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100710 return ret;
711 }
712 }
713
714 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
715 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
716 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800717 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100718 return ret;
719 }
720 }
721 return ret;
722}
723
724/* locks held by caller */
725static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
726{
727 if (!rdev->constraints)
728 return -EINVAL;
729
730 switch (state) {
731 case PM_SUSPEND_STANDBY:
732 return suspend_set_state(rdev,
733 &rdev->constraints->state_standby);
734 case PM_SUSPEND_MEM:
735 return suspend_set_state(rdev,
736 &rdev->constraints->state_mem);
737 case PM_SUSPEND_MAX:
738 return suspend_set_state(rdev,
739 &rdev->constraints->state_disk);
740 default:
741 return -EINVAL;
742 }
743}
744
745static void print_constraints(struct regulator_dev *rdev)
746{
747 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000748 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100749 int count = 0;
750 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100751
Mark Brown8f031b42009-10-22 16:31:31 +0100752 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100753 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100754 count += sprintf(buf + count, "%d mV ",
755 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100756 else
Mark Brown8f031b42009-10-22 16:31:31 +0100757 count += sprintf(buf + count, "%d <--> %d mV ",
758 constraints->min_uV / 1000,
759 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100760 }
Mark Brown8f031b42009-10-22 16:31:31 +0100761
762 if (!constraints->min_uV ||
763 constraints->min_uV != constraints->max_uV) {
764 ret = _regulator_get_voltage(rdev);
765 if (ret > 0)
766 count += sprintf(buf + count, "at %d mV ", ret / 1000);
767 }
768
Mark Brownbf5892a2011-05-08 22:13:37 +0100769 if (constraints->uV_offset)
770 count += sprintf(buf, "%dmV offset ",
771 constraints->uV_offset / 1000);
772
Mark Brown8f031b42009-10-22 16:31:31 +0100773 if (constraints->min_uA && constraints->max_uA) {
774 if (constraints->min_uA == constraints->max_uA)
775 count += sprintf(buf + count, "%d mA ",
776 constraints->min_uA / 1000);
777 else
778 count += sprintf(buf + count, "%d <--> %d mA ",
779 constraints->min_uA / 1000,
780 constraints->max_uA / 1000);
781 }
782
783 if (!constraints->min_uA ||
784 constraints->min_uA != constraints->max_uA) {
785 ret = _regulator_get_current_limit(rdev);
786 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400787 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100788 }
789
Liam Girdwood414c70c2008-04-30 15:59:04 +0100790 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
791 count += sprintf(buf + count, "fast ");
792 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
793 count += sprintf(buf + count, "normal ");
794 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
795 count += sprintf(buf + count, "idle ");
796 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
797 count += sprintf(buf + count, "standby");
798
Mark Brown13ce29f2010-12-17 16:04:12 +0000799 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000800
801 if ((constraints->min_uV != constraints->max_uV) &&
802 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
803 rdev_warn(rdev,
804 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100805}
806
Mark Browne79055d2009-10-19 15:53:50 +0100807static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100808 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100809{
810 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100811 int ret;
812
813 /* do we need to apply the constraint voltage */
814 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000815 rdev->constraints->min_uV == rdev->constraints->max_uV) {
816 ret = _regulator_do_set_voltage(rdev,
817 rdev->constraints->min_uV,
818 rdev->constraints->max_uV);
819 if (ret < 0) {
820 rdev_err(rdev, "failed to apply %duV constraint\n",
821 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000822 return ret;
823 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100824 }
Mark Browne79055d2009-10-19 15:53:50 +0100825
826 /* constrain machine-level voltage specs to fit
827 * the actual range supported by this regulator.
828 */
829 if (ops->list_voltage && rdev->desc->n_voltages) {
830 int count = rdev->desc->n_voltages;
831 int i;
832 int min_uV = INT_MAX;
833 int max_uV = INT_MIN;
834 int cmin = constraints->min_uV;
835 int cmax = constraints->max_uV;
836
837 /* it's safe to autoconfigure fixed-voltage supplies
838 and the constraints are used by list_voltage. */
839 if (count == 1 && !cmin) {
840 cmin = 1;
841 cmax = INT_MAX;
842 constraints->min_uV = cmin;
843 constraints->max_uV = cmax;
844 }
845
846 /* voltage constraints are optional */
847 if ((cmin == 0) && (cmax == 0))
848 return 0;
849
850 /* else require explicit machine-level constraints */
851 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800852 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100853 return -EINVAL;
854 }
855
856 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
857 for (i = 0; i < count; i++) {
858 int value;
859
860 value = ops->list_voltage(rdev, i);
861 if (value <= 0)
862 continue;
863
864 /* maybe adjust [min_uV..max_uV] */
865 if (value >= cmin && value < min_uV)
866 min_uV = value;
867 if (value <= cmax && value > max_uV)
868 max_uV = value;
869 }
870
871 /* final: [min_uV..max_uV] valid iff constraints valid */
872 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800873 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100874 return -EINVAL;
875 }
876
877 /* use regulator's subset of machine constraints */
878 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800879 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
880 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100881 constraints->min_uV = min_uV;
882 }
883 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800884 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
885 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100886 constraints->max_uV = max_uV;
887 }
888 }
889
890 return 0;
891}
892
Liam Girdwooda5766f12008-10-10 13:22:20 +0100893/**
894 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000895 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000896 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100897 *
898 * Allows platform initialisation code to define and constrain
899 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
900 * Constraints *must* be set by platform code in order for some
901 * regulator operations to proceed i.e. set_voltage, set_current_limit,
902 * set_mode.
903 */
904static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000905 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100906{
907 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100908 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100909
Mark Brown9a8f5e02011-11-29 18:11:19 +0000910 if (constraints)
911 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
912 GFP_KERNEL);
913 else
914 rdev->constraints = kzalloc(sizeof(*constraints),
915 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000916 if (!rdev->constraints)
917 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100918
Mark Brownf8c12fe2010-11-29 15:55:17 +0000919 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100920 if (ret != 0)
921 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800922
Liam Girdwooda5766f12008-10-10 13:22:20 +0100923 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000924 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000925 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100926 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800927 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100928 goto out;
929 }
930 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100931
Mark Brown9a8f5e02011-11-29 18:11:19 +0000932 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000933 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800934 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000935 ret = -EINVAL;
936 goto out;
937 }
938
Mark Brownf8c12fe2010-11-29 15:55:17 +0000939 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000940 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800941 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000942 goto out;
943 }
944 }
945
Mark Browncacf90f2009-03-02 16:32:46 +0000946 /* If the constraints say the regulator should be on at this point
947 * and we have control then make sure it is enabled.
948 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000949 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
950 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100951 ret = ops->enable(rdev);
952 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800953 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100954 goto out;
955 }
956 }
957
Liam Girdwooda5766f12008-10-10 13:22:20 +0100958 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800959 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100960out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800961 kfree(rdev->constraints);
962 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100963 return ret;
964}
965
966/**
967 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000968 * @rdev: regulator name
969 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100970 *
971 * Called by platform initialisation code to set the supply regulator for this
972 * regulator. This ensures that a regulators supply will also be enabled by the
973 * core if it's child is enabled.
974 */
975static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100976 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100977{
978 int err;
979
Mark Brown3801b862011-06-09 16:22:22 +0100980 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
981
982 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +0800983 if (rdev->supply == NULL) {
984 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +0100985 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100986 }
Mark Brown3801b862011-06-09 16:22:22 +0100987
988 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100989}
990
991/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800992 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000993 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +0100994 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000995 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100996 *
997 * Allows platform initialisation code to map physical regulator
998 * sources to symbolic names for supplies for use by devices. Devices
999 * should use these symbolic names to request regulators, avoiding the
1000 * need to provide board-specific regulator names as platform data.
1001 */
1002static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001003 const char *consumer_dev_name,
1004 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001005{
1006 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001007 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001008
1009 if (supply == NULL)
1010 return -EINVAL;
1011
Mark Brown9ed20992009-07-21 16:00:26 +01001012 if (consumer_dev_name != NULL)
1013 has_dev = 1;
1014 else
1015 has_dev = 0;
1016
David Brownell6001e132008-12-31 12:54:19 +00001017 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001018 if (node->dev_name && consumer_dev_name) {
1019 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1020 continue;
1021 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001022 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001023 }
1024
David Brownell6001e132008-12-31 12:54:19 +00001025 if (strcmp(node->supply, supply) != 0)
1026 continue;
1027
Mark Brown737f3602012-02-02 00:10:51 +00001028 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1029 consumer_dev_name,
1030 dev_name(&node->regulator->dev),
1031 node->regulator->desc->name,
1032 supply,
1033 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001034 return -EBUSY;
1035 }
1036
Mark Brown9ed20992009-07-21 16:00:26 +01001037 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001038 if (node == NULL)
1039 return -ENOMEM;
1040
1041 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001042 node->supply = supply;
1043
Mark Brown9ed20992009-07-21 16:00:26 +01001044 if (has_dev) {
1045 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1046 if (node->dev_name == NULL) {
1047 kfree(node);
1048 return -ENOMEM;
1049 }
Mark Brown40f92442009-06-17 17:56:39 +01001050 }
1051
Liam Girdwooda5766f12008-10-10 13:22:20 +01001052 list_add(&node->list, &regulator_map_list);
1053 return 0;
1054}
1055
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001056static void unset_regulator_supplies(struct regulator_dev *rdev)
1057{
1058 struct regulator_map *node, *n;
1059
1060 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1061 if (rdev == node->regulator) {
1062 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001063 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001064 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001065 }
1066 }
1067}
1068
Mark Brownf5726ae2011-06-09 16:22:20 +01001069#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001070
1071static struct regulator *create_regulator(struct regulator_dev *rdev,
1072 struct device *dev,
1073 const char *supply_name)
1074{
1075 struct regulator *regulator;
1076 char buf[REG_STR_SIZE];
1077 int err, size;
1078
1079 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1080 if (regulator == NULL)
1081 return NULL;
1082
1083 mutex_lock(&rdev->mutex);
1084 regulator->rdev = rdev;
1085 list_add(&regulator->list, &rdev->consumer_list);
1086
1087 if (dev) {
Mark Brown222cc7b2012-06-22 11:39:16 +01001088 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001089 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1090 dev->kobj.name, supply_name);
1091 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001092 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001093
1094 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1095 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001096 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001097
1098 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1099 buf);
1100 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001101 rdev_warn(rdev, "could not add device link %s err %d\n",
1102 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001103 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001104 }
Mark Brown5de70512011-06-19 13:33:16 +01001105 } else {
1106 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1107 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001108 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001109 }
Mark Brown5de70512011-06-19 13:33:16 +01001110
Mark Brown5de70512011-06-19 13:33:16 +01001111 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1112 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001113 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001114 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001115 } else {
1116 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1117 &regulator->uA_load);
1118 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1119 &regulator->min_uV);
1120 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1121 &regulator->max_uV);
1122 }
Mark Brown5de70512011-06-19 13:33:16 +01001123
Mark Brown6492bc12012-04-19 13:19:07 +01001124 /*
1125 * Check now if the regulator is an always on regulator - if
1126 * it is then we don't need to do nearly so much work for
1127 * enable/disable calls.
1128 */
1129 if (!_regulator_can_change_status(rdev) &&
1130 _regulator_is_enabled(rdev))
1131 regulator->always_on = true;
1132
Liam Girdwood414c70c2008-04-30 15:59:04 +01001133 mutex_unlock(&rdev->mutex);
1134 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001135overflow_err:
1136 list_del(&regulator->list);
1137 kfree(regulator);
1138 mutex_unlock(&rdev->mutex);
1139 return NULL;
1140}
1141
Mark Brown31aae2b2009-12-21 12:21:52 +00001142static int _regulator_get_enable_time(struct regulator_dev *rdev)
1143{
1144 if (!rdev->desc->ops->enable_time)
1145 return 0;
1146 return rdev->desc->ops->enable_time(rdev);
1147}
1148
Rajendra Nayak69511a42011-11-18 16:47:20 +05301149static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001150 const char *supply,
1151 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301152{
1153 struct regulator_dev *r;
1154 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001155 struct regulator_map *map;
1156 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301157
1158 /* first do a dt based lookup */
1159 if (dev && dev->of_node) {
1160 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001161 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301162 list_for_each_entry(r, &regulator_list, list)
1163 if (r->dev.parent &&
1164 node == r->dev.of_node)
1165 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001166 } else {
1167 /*
1168 * If we couldn't even get the node then it's
1169 * not just that the device didn't register
1170 * yet, there's no node and we'll never
1171 * succeed.
1172 */
1173 *ret = -ENODEV;
1174 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301175 }
1176
1177 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001178 if (dev)
1179 devname = dev_name(dev);
1180
Rajendra Nayak69511a42011-11-18 16:47:20 +05301181 list_for_each_entry(r, &regulator_list, list)
1182 if (strcmp(rdev_get_name(r), supply) == 0)
1183 return r;
1184
Mark Brown576ca4362012-03-30 12:24:37 +01001185 list_for_each_entry(map, &regulator_map_list, list) {
1186 /* If the mapping has a device set up it must match */
1187 if (map->dev_name &&
1188 (!devname || strcmp(map->dev_name, devname)))
1189 continue;
1190
1191 if (strcmp(map->supply, supply) == 0)
1192 return map->regulator;
1193 }
1194
1195
Rajendra Nayak69511a42011-11-18 16:47:20 +05301196 return NULL;
1197}
1198
Mark Brown5ffbd132009-07-21 16:00:23 +01001199/* Internal regulator request function */
1200static struct regulator *_regulator_get(struct device *dev, const char *id,
1201 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001202{
1203 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001204 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001205 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001206 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001207
1208 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001209 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001210 return regulator;
1211 }
1212
Mark Brown40f92442009-06-17 17:56:39 +01001213 if (dev)
1214 devname = dev_name(dev);
1215
Liam Girdwood414c70c2008-04-30 15:59:04 +01001216 mutex_lock(&regulator_list_mutex);
1217
Mark Brown6d191a52012-03-29 14:19:02 +01001218 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301219 if (rdev)
1220 goto found;
1221
Mark Brown688fe992010-10-05 19:18:32 -07001222 if (board_wants_dummy_regulator) {
1223 rdev = dummy_regulator_rdev;
1224 goto found;
1225 }
1226
Mark Brown34abbd62010-02-12 10:18:08 +00001227#ifdef CONFIG_REGULATOR_DUMMY
1228 if (!devname)
1229 devname = "deviceless";
1230
1231 /* If the board didn't flag that it was fully constrained then
1232 * substitute in a dummy regulator so consumers can continue.
1233 */
1234 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001235 pr_warn("%s supply %s not found, using dummy regulator\n",
1236 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001237 rdev = dummy_regulator_rdev;
1238 goto found;
1239 }
1240#endif
1241
Liam Girdwood414c70c2008-04-30 15:59:04 +01001242 mutex_unlock(&regulator_list_mutex);
1243 return regulator;
1244
1245found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001246 if (rdev->exclusive) {
1247 regulator = ERR_PTR(-EPERM);
1248 goto out;
1249 }
1250
1251 if (exclusive && rdev->open_count) {
1252 regulator = ERR_PTR(-EBUSY);
1253 goto out;
1254 }
1255
Liam Girdwooda5766f12008-10-10 13:22:20 +01001256 if (!try_module_get(rdev->owner))
1257 goto out;
1258
Liam Girdwood414c70c2008-04-30 15:59:04 +01001259 regulator = create_regulator(rdev, dev, id);
1260 if (regulator == NULL) {
1261 regulator = ERR_PTR(-ENOMEM);
1262 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001263 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001264 }
1265
Mark Brown5ffbd132009-07-21 16:00:23 +01001266 rdev->open_count++;
1267 if (exclusive) {
1268 rdev->exclusive = 1;
1269
1270 ret = _regulator_is_enabled(rdev);
1271 if (ret > 0)
1272 rdev->use_count = 1;
1273 else
1274 rdev->use_count = 0;
1275 }
1276
Liam Girdwooda5766f12008-10-10 13:22:20 +01001277out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001278 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001279
Liam Girdwood414c70c2008-04-30 15:59:04 +01001280 return regulator;
1281}
Mark Brown5ffbd132009-07-21 16:00:23 +01001282
1283/**
1284 * regulator_get - lookup and obtain a reference to a regulator.
1285 * @dev: device for regulator "consumer"
1286 * @id: Supply name or regulator ID.
1287 *
1288 * Returns a struct regulator corresponding to the regulator producer,
1289 * or IS_ERR() condition containing errno.
1290 *
1291 * Use of supply names configured via regulator_set_device_supply() is
1292 * strongly encouraged. It is recommended that the supply name used
1293 * should match the name used for the supply and/or the relevant
1294 * device pins in the datasheet.
1295 */
1296struct regulator *regulator_get(struct device *dev, const char *id)
1297{
1298 return _regulator_get(dev, id, 0);
1299}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001300EXPORT_SYMBOL_GPL(regulator_get);
1301
Stephen Boyd070b9072012-01-16 19:39:58 -08001302static void devm_regulator_release(struct device *dev, void *res)
1303{
1304 regulator_put(*(struct regulator **)res);
1305}
1306
1307/**
1308 * devm_regulator_get - Resource managed regulator_get()
1309 * @dev: device for regulator "consumer"
1310 * @id: Supply name or regulator ID.
1311 *
1312 * Managed regulator_get(). Regulators returned from this function are
1313 * automatically regulator_put() on driver detach. See regulator_get() for more
1314 * information.
1315 */
1316struct regulator *devm_regulator_get(struct device *dev, const char *id)
1317{
1318 struct regulator **ptr, *regulator;
1319
1320 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1321 if (!ptr)
1322 return ERR_PTR(-ENOMEM);
1323
1324 regulator = regulator_get(dev, id);
1325 if (!IS_ERR(regulator)) {
1326 *ptr = regulator;
1327 devres_add(dev, ptr);
1328 } else {
1329 devres_free(ptr);
1330 }
1331
1332 return regulator;
1333}
1334EXPORT_SYMBOL_GPL(devm_regulator_get);
1335
Liam Girdwood414c70c2008-04-30 15:59:04 +01001336/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001337 * regulator_get_exclusive - obtain exclusive access to a regulator.
1338 * @dev: device for regulator "consumer"
1339 * @id: Supply name or regulator ID.
1340 *
1341 * Returns a struct regulator corresponding to the regulator producer,
1342 * or IS_ERR() condition containing errno. Other consumers will be
1343 * unable to obtain this reference is held and the use count for the
1344 * regulator will be initialised to reflect the current state of the
1345 * regulator.
1346 *
1347 * This is intended for use by consumers which cannot tolerate shared
1348 * use of the regulator such as those which need to force the
1349 * regulator off for correct operation of the hardware they are
1350 * controlling.
1351 *
1352 * Use of supply names configured via regulator_set_device_supply() is
1353 * strongly encouraged. It is recommended that the supply name used
1354 * should match the name used for the supply and/or the relevant
1355 * device pins in the datasheet.
1356 */
1357struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1358{
1359 return _regulator_get(dev, id, 1);
1360}
1361EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1362
1363/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001364 * regulator_put - "free" the regulator source
1365 * @regulator: regulator source
1366 *
1367 * Note: drivers must ensure that all regulator_enable calls made on this
1368 * regulator source are balanced by regulator_disable calls prior to calling
1369 * this function.
1370 */
1371void regulator_put(struct regulator *regulator)
1372{
1373 struct regulator_dev *rdev;
1374
1375 if (regulator == NULL || IS_ERR(regulator))
1376 return;
1377
Liam Girdwood414c70c2008-04-30 15:59:04 +01001378 mutex_lock(&regulator_list_mutex);
1379 rdev = regulator->rdev;
1380
Mark Brown5de70512011-06-19 13:33:16 +01001381 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001382
Liam Girdwood414c70c2008-04-30 15:59:04 +01001383 /* remove any sysfs entries */
1384 if (regulator->dev) {
1385 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001386 device_remove_file(regulator->dev, &regulator->dev_attr);
1387 kfree(regulator->dev_attr.attr.name);
1388 }
Mark Brown5de70512011-06-19 13:33:16 +01001389 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001390 list_del(&regulator->list);
1391 kfree(regulator);
1392
Mark Brown5ffbd132009-07-21 16:00:23 +01001393 rdev->open_count--;
1394 rdev->exclusive = 0;
1395
Liam Girdwood414c70c2008-04-30 15:59:04 +01001396 module_put(rdev->owner);
1397 mutex_unlock(&regulator_list_mutex);
1398}
1399EXPORT_SYMBOL_GPL(regulator_put);
1400
Mark Brownd5ad34f2012-01-20 20:09:18 +00001401static int devm_regulator_match(struct device *dev, void *res, void *data)
1402{
1403 struct regulator **r = res;
1404 if (!r || !*r) {
1405 WARN_ON(!r || !*r);
1406 return 0;
1407 }
1408 return *r == data;
1409}
1410
1411/**
1412 * devm_regulator_put - Resource managed regulator_put()
1413 * @regulator: regulator to free
1414 *
1415 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1416 * this function will not need to be called and the resource management
1417 * code will ensure that the resource is freed.
1418 */
1419void devm_regulator_put(struct regulator *regulator)
1420{
1421 int rc;
1422
Mark Brown361ff502012-05-07 14:14:30 +01001423 rc = devres_release(regulator->dev, devm_regulator_release,
Mark Brownd5ad34f2012-01-20 20:09:18 +00001424 devm_regulator_match, regulator);
Mark Brown230a5a1c2012-06-15 18:25:08 +01001425 if (rc != 0)
Mark Brown968c2c12012-05-07 11:34:52 +01001426 WARN_ON(rc);
Mark Brownd5ad34f2012-01-20 20:09:18 +00001427}
1428EXPORT_SYMBOL_GPL(devm_regulator_put);
1429
Liam Girdwood414c70c2008-04-30 15:59:04 +01001430/* locks held by regulator_enable() */
1431static int _regulator_enable(struct regulator_dev *rdev)
1432{
Mark Brown31aae2b2009-12-21 12:21:52 +00001433 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001434
Liam Girdwood414c70c2008-04-30 15:59:04 +01001435 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001436 if (rdev->constraints &&
1437 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1438 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001439
Mark Brown9a2372f2009-08-03 18:49:57 +01001440 if (rdev->use_count == 0) {
1441 /* The regulator may on if it's not switchable or left on */
1442 ret = _regulator_is_enabled(rdev);
1443 if (ret == -EINVAL || ret == 0) {
1444 if (!_regulator_can_change_status(rdev))
1445 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001446
Mark Brown31aae2b2009-12-21 12:21:52 +00001447 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001448 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001449
1450 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001451 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001452 ret = _regulator_get_enable_time(rdev);
1453 if (ret >= 0) {
1454 delay = ret;
1455 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001456 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001457 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001458 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001459 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001460
Mark Brown02fa3ec2010-11-10 14:38:30 +00001461 trace_regulator_enable(rdev_get_name(rdev));
1462
Mark Brown31aae2b2009-12-21 12:21:52 +00001463 /* Allow the regulator to ramp; it would be useful
1464 * to extend this for bulk operations so that the
1465 * regulators can ramp together. */
1466 ret = rdev->desc->ops->enable(rdev);
1467 if (ret < 0)
1468 return ret;
1469
Mark Brown02fa3ec2010-11-10 14:38:30 +00001470 trace_regulator_enable_delay(rdev_get_name(rdev));
1471
Axel Line36c1df2010-11-05 21:51:32 +08001472 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001473 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001474 udelay(delay % 1000);
1475 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001476 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001477 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001478
Mark Brown02fa3ec2010-11-10 14:38:30 +00001479 trace_regulator_enable_complete(rdev_get_name(rdev));
1480
Linus Walleija7433cf2009-08-26 12:54:04 +02001481 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001482 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001483 return ret;
1484 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001485 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001486 }
1487
Mark Brown9a2372f2009-08-03 18:49:57 +01001488 rdev->use_count++;
1489
1490 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001491}
1492
1493/**
1494 * regulator_enable - enable regulator output
1495 * @regulator: regulator source
1496 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001497 * Request that the regulator be enabled with the regulator output at
1498 * the predefined voltage or current value. Calls to regulator_enable()
1499 * must be balanced with calls to regulator_disable().
1500 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001501 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001502 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001503 */
1504int regulator_enable(struct regulator *regulator)
1505{
David Brownell412aec62008-11-16 11:44:46 -08001506 struct regulator_dev *rdev = regulator->rdev;
1507 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001508
Mark Brown6492bc12012-04-19 13:19:07 +01001509 if (regulator->always_on)
1510 return 0;
1511
Mark Brown3801b862011-06-09 16:22:22 +01001512 if (rdev->supply) {
1513 ret = regulator_enable(rdev->supply);
1514 if (ret != 0)
1515 return ret;
1516 }
1517
David Brownell412aec62008-11-16 11:44:46 -08001518 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001519 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001520 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001521
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001522 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001523 regulator_disable(rdev->supply);
1524
Liam Girdwood414c70c2008-04-30 15:59:04 +01001525 return ret;
1526}
1527EXPORT_SYMBOL_GPL(regulator_enable);
1528
1529/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001530static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001531{
1532 int ret = 0;
1533
David Brownellcd94b502009-03-11 16:43:34 -08001534 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001535 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001536 return -EIO;
1537
Liam Girdwood414c70c2008-04-30 15:59:04 +01001538 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001539 if (rdev->use_count == 1 &&
1540 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001541
1542 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001543 if (_regulator_can_change_status(rdev) &&
1544 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001545 trace_regulator_disable(rdev_get_name(rdev));
1546
Liam Girdwood414c70c2008-04-30 15:59:04 +01001547 ret = rdev->desc->ops->disable(rdev);
1548 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001549 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001550 return ret;
1551 }
Mark Brown84b68262009-12-01 21:12:27 +00001552
Mark Brown02fa3ec2010-11-10 14:38:30 +00001553 trace_regulator_disable_complete(rdev_get_name(rdev));
1554
Mark Brown84b68262009-12-01 21:12:27 +00001555 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1556 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001557 }
1558
Liam Girdwood414c70c2008-04-30 15:59:04 +01001559 rdev->use_count = 0;
1560 } else if (rdev->use_count > 1) {
1561
1562 if (rdev->constraints &&
1563 (rdev->constraints->valid_ops_mask &
1564 REGULATOR_CHANGE_DRMS))
1565 drms_uA_update(rdev);
1566
1567 rdev->use_count--;
1568 }
Mark Brown3801b862011-06-09 16:22:22 +01001569
Liam Girdwood414c70c2008-04-30 15:59:04 +01001570 return ret;
1571}
1572
1573/**
1574 * regulator_disable - disable regulator output
1575 * @regulator: regulator source
1576 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001577 * Disable the regulator output voltage or current. Calls to
1578 * regulator_enable() must be balanced with calls to
1579 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001580 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001581 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001582 * devices have it enabled, the regulator device supports disabling and
1583 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001584 */
1585int regulator_disable(struct regulator *regulator)
1586{
David Brownell412aec62008-11-16 11:44:46 -08001587 struct regulator_dev *rdev = regulator->rdev;
1588 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001589
Mark Brown6492bc12012-04-19 13:19:07 +01001590 if (regulator->always_on)
1591 return 0;
1592
David Brownell412aec62008-11-16 11:44:46 -08001593 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001594 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001595 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001596
Mark Brown3801b862011-06-09 16:22:22 +01001597 if (ret == 0 && rdev->supply)
1598 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001599
Liam Girdwood414c70c2008-04-30 15:59:04 +01001600 return ret;
1601}
1602EXPORT_SYMBOL_GPL(regulator_disable);
1603
1604/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001605static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001606{
1607 int ret = 0;
1608
1609 /* force disable */
1610 if (rdev->desc->ops->disable) {
1611 /* ah well, who wants to live forever... */
1612 ret = rdev->desc->ops->disable(rdev);
1613 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001614 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001615 return ret;
1616 }
1617 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001618 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1619 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001620 }
1621
Liam Girdwood414c70c2008-04-30 15:59:04 +01001622 return ret;
1623}
1624
1625/**
1626 * regulator_force_disable - force disable regulator output
1627 * @regulator: regulator source
1628 *
1629 * Forcibly disable the regulator output voltage or current.
1630 * NOTE: this *will* disable the regulator output even if other consumer
1631 * devices have it enabled. This should be used for situations when device
1632 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1633 */
1634int regulator_force_disable(struct regulator *regulator)
1635{
Mark Brown82d15832011-05-09 11:41:02 +02001636 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001637 int ret;
1638
Mark Brown82d15832011-05-09 11:41:02 +02001639 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001640 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001641 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001642 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001643
Mark Brown3801b862011-06-09 16:22:22 +01001644 if (rdev->supply)
1645 while (rdev->open_count--)
1646 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001647
Liam Girdwood414c70c2008-04-30 15:59:04 +01001648 return ret;
1649}
1650EXPORT_SYMBOL_GPL(regulator_force_disable);
1651
Mark Brownda07ecd2011-09-11 09:53:50 +01001652static void regulator_disable_work(struct work_struct *work)
1653{
1654 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1655 disable_work.work);
1656 int count, i, ret;
1657
1658 mutex_lock(&rdev->mutex);
1659
1660 BUG_ON(!rdev->deferred_disables);
1661
1662 count = rdev->deferred_disables;
1663 rdev->deferred_disables = 0;
1664
1665 for (i = 0; i < count; i++) {
1666 ret = _regulator_disable(rdev);
1667 if (ret != 0)
1668 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1669 }
1670
1671 mutex_unlock(&rdev->mutex);
1672
1673 if (rdev->supply) {
1674 for (i = 0; i < count; i++) {
1675 ret = regulator_disable(rdev->supply);
1676 if (ret != 0) {
1677 rdev_err(rdev,
1678 "Supply disable failed: %d\n", ret);
1679 }
1680 }
1681 }
1682}
1683
1684/**
1685 * regulator_disable_deferred - disable regulator output with delay
1686 * @regulator: regulator source
1687 * @ms: miliseconds until the regulator is disabled
1688 *
1689 * Execute regulator_disable() on the regulator after a delay. This
1690 * is intended for use with devices that require some time to quiesce.
1691 *
1692 * NOTE: this will only disable the regulator output if no other consumer
1693 * devices have it enabled, the regulator device supports disabling and
1694 * machine constraints permit this operation.
1695 */
1696int regulator_disable_deferred(struct regulator *regulator, int ms)
1697{
1698 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001699 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001700
Mark Brown6492bc12012-04-19 13:19:07 +01001701 if (regulator->always_on)
1702 return 0;
1703
Mark Brownda07ecd2011-09-11 09:53:50 +01001704 mutex_lock(&rdev->mutex);
1705 rdev->deferred_disables++;
1706 mutex_unlock(&rdev->mutex);
1707
Mark Brownaa598022011-10-03 22:42:43 +01001708 ret = schedule_delayed_work(&rdev->disable_work,
1709 msecs_to_jiffies(ms));
1710 if (ret < 0)
1711 return ret;
1712 else
1713 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001714}
1715EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1716
Mark Browncd6dffb2012-04-15 12:37:47 +01001717/**
1718 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1719 *
1720 * @rdev: regulator to operate on
1721 *
1722 * Regulators that use regmap for their register I/O can set the
1723 * enable_reg and enable_mask fields in their descriptor and then use
1724 * this as their is_enabled operation, saving some code.
1725 */
1726int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1727{
1728 unsigned int val;
1729 int ret;
1730
1731 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1732 if (ret != 0)
1733 return ret;
1734
1735 return (val & rdev->desc->enable_mask) != 0;
1736}
1737EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1738
1739/**
1740 * regulator_enable_regmap - standard enable() for regmap users
1741 *
1742 * @rdev: regulator to operate on
1743 *
1744 * Regulators that use regmap for their register I/O can set the
1745 * enable_reg and enable_mask fields in their descriptor and then use
1746 * this as their enable() operation, saving some code.
1747 */
1748int regulator_enable_regmap(struct regulator_dev *rdev)
1749{
1750 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1751 rdev->desc->enable_mask,
1752 rdev->desc->enable_mask);
1753}
1754EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1755
1756/**
1757 * regulator_disable_regmap - standard disable() for regmap users
1758 *
1759 * @rdev: regulator to operate on
1760 *
1761 * Regulators that use regmap for their register I/O can set the
1762 * enable_reg and enable_mask fields in their descriptor and then use
1763 * this as their disable() operation, saving some code.
1764 */
1765int regulator_disable_regmap(struct regulator_dev *rdev)
1766{
1767 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1768 rdev->desc->enable_mask, 0);
1769}
1770EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1771
Liam Girdwood414c70c2008-04-30 15:59:04 +01001772static int _regulator_is_enabled(struct regulator_dev *rdev)
1773{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001774 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001775 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001776 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001777
Mark Brown93325462009-08-03 18:49:56 +01001778 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001779}
1780
1781/**
1782 * regulator_is_enabled - is the regulator output enabled
1783 * @regulator: regulator source
1784 *
David Brownell412aec62008-11-16 11:44:46 -08001785 * Returns positive if the regulator driver backing the source/client
1786 * has requested that the device be enabled, zero if it hasn't, else a
1787 * negative errno code.
1788 *
1789 * Note that the device backing this regulator handle can have multiple
1790 * users, so it might be enabled even if regulator_enable() was never
1791 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001792 */
1793int regulator_is_enabled(struct regulator *regulator)
1794{
Mark Brown93325462009-08-03 18:49:56 +01001795 int ret;
1796
Mark Brown6492bc12012-04-19 13:19:07 +01001797 if (regulator->always_on)
1798 return 1;
1799
Mark Brown93325462009-08-03 18:49:56 +01001800 mutex_lock(&regulator->rdev->mutex);
1801 ret = _regulator_is_enabled(regulator->rdev);
1802 mutex_unlock(&regulator->rdev->mutex);
1803
1804 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001805}
1806EXPORT_SYMBOL_GPL(regulator_is_enabled);
1807
1808/**
David Brownell4367cfd2009-02-26 11:48:36 -08001809 * regulator_count_voltages - count regulator_list_voltage() selectors
1810 * @regulator: regulator source
1811 *
1812 * Returns number of selectors, or negative errno. Selectors are
1813 * numbered starting at zero, and typically correspond to bitfields
1814 * in hardware registers.
1815 */
1816int regulator_count_voltages(struct regulator *regulator)
1817{
1818 struct regulator_dev *rdev = regulator->rdev;
1819
1820 return rdev->desc->n_voltages ? : -EINVAL;
1821}
1822EXPORT_SYMBOL_GPL(regulator_count_voltages);
1823
1824/**
Mark Brownbca7bbf2012-05-09 21:38:33 +01001825 * regulator_list_voltage_linear - List voltages with simple calculation
1826 *
1827 * @rdev: Regulator device
1828 * @selector: Selector to convert into a voltage
1829 *
1830 * Regulators with a simple linear mapping between voltages and
1831 * selectors can set min_uV and uV_step in the regulator descriptor
1832 * and then use this function as their list_voltage() operation,
1833 */
1834int regulator_list_voltage_linear(struct regulator_dev *rdev,
1835 unsigned int selector)
1836{
1837 if (selector >= rdev->desc->n_voltages)
1838 return -EINVAL;
1839
1840 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
1841}
1842EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
1843
1844/**
Axel Lincffc9592012-05-20 10:30:21 +08001845 * regulator_list_voltage_table - List voltages with table based mapping
1846 *
1847 * @rdev: Regulator device
1848 * @selector: Selector to convert into a voltage
1849 *
1850 * Regulators with table based mapping between voltages and
1851 * selectors can set volt_table in the regulator descriptor
1852 * and then use this function as their list_voltage() operation.
1853 */
1854int regulator_list_voltage_table(struct regulator_dev *rdev,
1855 unsigned int selector)
1856{
1857 if (!rdev->desc->volt_table) {
1858 BUG_ON(!rdev->desc->volt_table);
1859 return -EINVAL;
1860 }
1861
1862 if (selector >= rdev->desc->n_voltages)
1863 return -EINVAL;
1864
1865 return rdev->desc->volt_table[selector];
1866}
1867EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
1868
1869/**
David Brownell4367cfd2009-02-26 11:48:36 -08001870 * regulator_list_voltage - enumerate supported voltages
1871 * @regulator: regulator source
1872 * @selector: identify voltage to list
1873 * Context: can sleep
1874 *
1875 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001876 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001877 * negative errno.
1878 */
1879int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1880{
1881 struct regulator_dev *rdev = regulator->rdev;
1882 struct regulator_ops *ops = rdev->desc->ops;
1883 int ret;
1884
1885 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1886 return -EINVAL;
1887
1888 mutex_lock(&rdev->mutex);
1889 ret = ops->list_voltage(rdev, selector);
1890 mutex_unlock(&rdev->mutex);
1891
1892 if (ret > 0) {
1893 if (ret < rdev->constraints->min_uV)
1894 ret = 0;
1895 else if (ret > rdev->constraints->max_uV)
1896 ret = 0;
1897 }
1898
1899 return ret;
1900}
1901EXPORT_SYMBOL_GPL(regulator_list_voltage);
1902
1903/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001904 * regulator_is_supported_voltage - check if a voltage range can be supported
1905 *
1906 * @regulator: Regulator to check.
1907 * @min_uV: Minimum required voltage in uV.
1908 * @max_uV: Maximum required voltage in uV.
1909 *
1910 * Returns a boolean or a negative error code.
1911 */
1912int regulator_is_supported_voltage(struct regulator *regulator,
1913 int min_uV, int max_uV)
1914{
1915 int i, voltages, ret;
1916
1917 ret = regulator_count_voltages(regulator);
1918 if (ret < 0)
1919 return ret;
1920 voltages = ret;
1921
1922 for (i = 0; i < voltages; i++) {
1923 ret = regulator_list_voltage(regulator, i);
1924
1925 if (ret >= min_uV && ret <= max_uV)
1926 return 1;
1927 }
1928
1929 return 0;
1930}
Mark Browna398eaa2011-12-28 12:48:45 +00001931EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01001932
Mark Brown4ab5b3d2012-04-15 11:23:30 +01001933/**
1934 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
1935 *
1936 * @rdev: regulator to operate on
1937 *
1938 * Regulators that use regmap for their register I/O can set the
1939 * vsel_reg and vsel_mask fields in their descriptor and then use this
1940 * as their get_voltage_vsel operation, saving some code.
1941 */
1942int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
1943{
1944 unsigned int val;
1945 int ret;
1946
1947 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
1948 if (ret != 0)
1949 return ret;
1950
1951 val &= rdev->desc->vsel_mask;
1952 val >>= ffs(rdev->desc->vsel_mask) - 1;
1953
1954 return val;
1955}
1956EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
1957
1958/**
1959 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
1960 *
1961 * @rdev: regulator to operate on
1962 * @sel: Selector to set
1963 *
1964 * Regulators that use regmap for their register I/O can set the
1965 * vsel_reg and vsel_mask fields in their descriptor and then use this
1966 * as their set_voltage_vsel operation, saving some code.
1967 */
1968int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
1969{
1970 sel <<= ffs(rdev->desc->vsel_mask) - 1;
1971
1972 return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
1973 rdev->desc->vsel_mask, sel);
1974}
1975EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
1976
Mark Browne843fc42012-05-09 21:16:06 +01001977/**
1978 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
1979 *
1980 * @rdev: Regulator to operate on
1981 * @min_uV: Lower bound for voltage
1982 * @max_uV: Upper bound for voltage
1983 *
1984 * Drivers implementing set_voltage_sel() and list_voltage() can use
1985 * this as their map_voltage() operation. It will find a suitable
1986 * voltage by calling list_voltage() until it gets something in bounds
1987 * for the requested voltages.
1988 */
1989int regulator_map_voltage_iterate(struct regulator_dev *rdev,
1990 int min_uV, int max_uV)
1991{
1992 int best_val = INT_MAX;
1993 int selector = 0;
1994 int i, ret;
1995
1996 /* Find the smallest voltage that falls within the specified
1997 * range.
1998 */
1999 for (i = 0; i < rdev->desc->n_voltages; i++) {
2000 ret = rdev->desc->ops->list_voltage(rdev, i);
2001 if (ret < 0)
2002 continue;
2003
2004 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2005 best_val = ret;
2006 selector = i;
2007 }
2008 }
2009
2010 if (best_val != INT_MAX)
2011 return selector;
2012 else
2013 return -EINVAL;
2014}
2015EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2016
Mark Brownbca7bbf2012-05-09 21:38:33 +01002017/**
2018 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2019 *
2020 * @rdev: Regulator to operate on
2021 * @min_uV: Lower bound for voltage
2022 * @max_uV: Upper bound for voltage
2023 *
2024 * Drivers providing min_uV and uV_step in their regulator_desc can
2025 * use this as their map_voltage() operation.
2026 */
2027int regulator_map_voltage_linear(struct regulator_dev *rdev,
2028 int min_uV, int max_uV)
2029{
2030 int ret, voltage;
2031
2032 if (!rdev->desc->uV_step) {
2033 BUG_ON(!rdev->desc->uV_step);
2034 return -EINVAL;
2035 }
2036
Axel Linccfcb1c2012-05-14 10:33:37 +08002037 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
Mark Brownbca7bbf2012-05-09 21:38:33 +01002038 if (ret < 0)
2039 return ret;
2040
2041 /* Map back into a voltage to verify we're still in bounds */
2042 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2043 if (voltage < min_uV || voltage > max_uV)
2044 return -EINVAL;
2045
2046 return ret;
2047}
2048EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2049
Mark Brown75790252010-12-12 14:25:50 +00002050static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2051 int min_uV, int max_uV)
2052{
2053 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002054 int delay = 0;
Mark Browne843fc42012-05-09 21:16:06 +01002055 int best_val;
Mark Brown75790252010-12-12 14:25:50 +00002056 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002057 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002058
2059 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2060
Mark Brownbf5892a2011-05-08 22:13:37 +01002061 min_uV += rdev->constraints->uV_offset;
2062 max_uV += rdev->constraints->uV_offset;
2063
Axel Lineba41a52012-04-04 10:32:10 +08002064 /*
2065 * If we can't obtain the old selector there is not enough
2066 * info to call set_voltage_time_sel().
2067 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002068 if (_regulator_is_enabled(rdev) &&
2069 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002070 rdev->desc->ops->get_voltage_sel) {
2071 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2072 if (old_selector < 0)
2073 return old_selector;
2074 }
2075
Mark Brown75790252010-12-12 14:25:50 +00002076 if (rdev->desc->ops->set_voltage) {
2077 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2078 &selector);
Mark Browne8eef822010-12-12 14:36:17 +00002079 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002080 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002081 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2082 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002083 } else {
2084 if (rdev->desc->ops->list_voltage ==
2085 regulator_list_voltage_linear)
2086 ret = regulator_map_voltage_linear(rdev,
2087 min_uV, max_uV);
2088 else
2089 ret = regulator_map_voltage_iterate(rdev,
2090 min_uV, max_uV);
2091 }
Mark Browne8eef822010-12-12 14:36:17 +00002092
Mark Browne843fc42012-05-09 21:16:06 +01002093 if (ret >= 0) {
2094 selector = ret;
2095 ret = rdev->desc->ops->set_voltage_sel(rdev, ret);
Mark Browne8eef822010-12-12 14:36:17 +00002096 }
Mark Brown75790252010-12-12 14:25:50 +00002097 } else {
2098 ret = -EINVAL;
2099 }
2100
Mark Browne843fc42012-05-09 21:16:06 +01002101 if (rdev->desc->ops->list_voltage)
2102 best_val = rdev->desc->ops->list_voltage(rdev, selector);
2103 else
Philip Rakity2f7baf82012-06-15 11:27:36 -07002104 best_val = _regulator_get_voltage(rdev);
Mark Browne843fc42012-05-09 21:16:06 +01002105
Axel Lineba41a52012-04-04 10:32:10 +08002106 /* Call set_voltage_time_sel if successfully obtained old_selector */
Mark Brown5aff3a82012-06-26 11:16:58 +01002107 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
Axel Lineba41a52012-04-04 10:32:10 +08002108 rdev->desc->ops->set_voltage_time_sel) {
2109
2110 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2111 old_selector, selector);
2112 if (delay < 0) {
2113 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2114 delay);
2115 delay = 0;
2116 }
Axel Lineba41a52012-04-04 10:32:10 +08002117
Philip Rakity8b96de32012-06-14 15:07:56 -07002118 /* Insert any necessary delays */
2119 if (delay >= 1000) {
2120 mdelay(delay / 1000);
2121 udelay(delay % 1000);
2122 } else if (delay) {
2123 udelay(delay);
2124 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002125 }
2126
Philip Rakity2f7baf82012-06-15 11:27:36 -07002127 if (ret == 0 && best_val >= 0)
Mark Brownded06a52010-12-16 13:59:10 +00002128 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Philip Rakity2f7baf82012-06-15 11:27:36 -07002129 (void *)best_val);
Mark Brownded06a52010-12-16 13:59:10 +00002130
Axel Lineba41a52012-04-04 10:32:10 +08002131 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002132
2133 return ret;
2134}
2135
Mark Browna7a1ad92009-07-21 16:00:24 +01002136/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002137 * regulator_set_voltage - set regulator output voltage
2138 * @regulator: regulator source
2139 * @min_uV: Minimum required voltage in uV
2140 * @max_uV: Maximum acceptable voltage in uV
2141 *
2142 * Sets a voltage regulator to the desired output voltage. This can be set
2143 * during any regulator state. IOW, regulator can be disabled or enabled.
2144 *
2145 * If the regulator is enabled then the voltage will change to the new value
2146 * immediately otherwise if the regulator is disabled the regulator will
2147 * output at the new voltage when enabled.
2148 *
2149 * NOTE: If the regulator is shared between several devices then the lowest
2150 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002151 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002152 * calling this function otherwise this call will fail.
2153 */
2154int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2155{
2156 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002157 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002158
2159 mutex_lock(&rdev->mutex);
2160
Mark Brown95a3c232010-12-16 15:49:37 +00002161 /* If we're setting the same range as last time the change
2162 * should be a noop (some cpufreq implementations use the same
2163 * voltage for multiple frequencies, for example).
2164 */
2165 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2166 goto out;
2167
Liam Girdwood414c70c2008-04-30 15:59:04 +01002168 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002169 if (!rdev->desc->ops->set_voltage &&
2170 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002171 ret = -EINVAL;
2172 goto out;
2173 }
2174
2175 /* constraints check */
2176 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2177 if (ret < 0)
2178 goto out;
2179 regulator->min_uV = min_uV;
2180 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002181
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002182 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2183 if (ret < 0)
2184 goto out;
2185
Mark Brown75790252010-12-12 14:25:50 +00002186 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00002187
Liam Girdwood414c70c2008-04-30 15:59:04 +01002188out:
2189 mutex_unlock(&rdev->mutex);
2190 return ret;
2191}
2192EXPORT_SYMBOL_GPL(regulator_set_voltage);
2193
Mark Brown606a2562010-12-16 15:49:36 +00002194/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002195 * regulator_set_voltage_time - get raise/fall time
2196 * @regulator: regulator source
2197 * @old_uV: starting voltage in microvolts
2198 * @new_uV: target voltage in microvolts
2199 *
2200 * Provided with the starting and ending voltage, this function attempts to
2201 * calculate the time in microseconds required to rise or fall to this new
2202 * voltage.
2203 */
2204int regulator_set_voltage_time(struct regulator *regulator,
2205 int old_uV, int new_uV)
2206{
2207 struct regulator_dev *rdev = regulator->rdev;
2208 struct regulator_ops *ops = rdev->desc->ops;
2209 int old_sel = -1;
2210 int new_sel = -1;
2211 int voltage;
2212 int i;
2213
2214 /* Currently requires operations to do this */
2215 if (!ops->list_voltage || !ops->set_voltage_time_sel
2216 || !rdev->desc->n_voltages)
2217 return -EINVAL;
2218
2219 for (i = 0; i < rdev->desc->n_voltages; i++) {
2220 /* We only look for exact voltage matches here */
2221 voltage = regulator_list_voltage(regulator, i);
2222 if (voltage < 0)
2223 return -EINVAL;
2224 if (voltage == 0)
2225 continue;
2226 if (voltage == old_uV)
2227 old_sel = i;
2228 if (voltage == new_uV)
2229 new_sel = i;
2230 }
2231
2232 if (old_sel < 0 || new_sel < 0)
2233 return -EINVAL;
2234
2235 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2236}
2237EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2238
2239/**
Mark Brown606a2562010-12-16 15:49:36 +00002240 * regulator_sync_voltage - re-apply last regulator output voltage
2241 * @regulator: regulator source
2242 *
2243 * Re-apply the last configured voltage. This is intended to be used
2244 * where some external control source the consumer is cooperating with
2245 * has caused the configured voltage to change.
2246 */
2247int regulator_sync_voltage(struct regulator *regulator)
2248{
2249 struct regulator_dev *rdev = regulator->rdev;
2250 int ret, min_uV, max_uV;
2251
2252 mutex_lock(&rdev->mutex);
2253
2254 if (!rdev->desc->ops->set_voltage &&
2255 !rdev->desc->ops->set_voltage_sel) {
2256 ret = -EINVAL;
2257 goto out;
2258 }
2259
2260 /* This is only going to work if we've had a voltage configured. */
2261 if (!regulator->min_uV && !regulator->max_uV) {
2262 ret = -EINVAL;
2263 goto out;
2264 }
2265
2266 min_uV = regulator->min_uV;
2267 max_uV = regulator->max_uV;
2268
2269 /* This should be a paranoia check... */
2270 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2271 if (ret < 0)
2272 goto out;
2273
2274 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2275 if (ret < 0)
2276 goto out;
2277
2278 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2279
2280out:
2281 mutex_unlock(&rdev->mutex);
2282 return ret;
2283}
2284EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2285
Liam Girdwood414c70c2008-04-30 15:59:04 +01002286static int _regulator_get_voltage(struct regulator_dev *rdev)
2287{
Mark Brownbf5892a2011-05-08 22:13:37 +01002288 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002289
2290 if (rdev->desc->ops->get_voltage_sel) {
2291 sel = rdev->desc->ops->get_voltage_sel(rdev);
2292 if (sel < 0)
2293 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002294 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002295 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002296 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002297 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002298 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002299 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002300
Axel Lincb220d12011-05-23 20:08:10 +08002301 if (ret < 0)
2302 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002303 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002304}
2305
2306/**
2307 * regulator_get_voltage - get regulator output voltage
2308 * @regulator: regulator source
2309 *
2310 * This returns the current regulator voltage in uV.
2311 *
2312 * NOTE: If the regulator is disabled it will return the voltage value. This
2313 * function should not be used to determine regulator state.
2314 */
2315int regulator_get_voltage(struct regulator *regulator)
2316{
2317 int ret;
2318
2319 mutex_lock(&regulator->rdev->mutex);
2320
2321 ret = _regulator_get_voltage(regulator->rdev);
2322
2323 mutex_unlock(&regulator->rdev->mutex);
2324
2325 return ret;
2326}
2327EXPORT_SYMBOL_GPL(regulator_get_voltage);
2328
2329/**
2330 * regulator_set_current_limit - set regulator output current limit
2331 * @regulator: regulator source
2332 * @min_uA: Minimuum supported current in uA
2333 * @max_uA: Maximum supported current in uA
2334 *
2335 * Sets current sink to the desired output current. This can be set during
2336 * any regulator state. IOW, regulator can be disabled or enabled.
2337 *
2338 * If the regulator is enabled then the current will change to the new value
2339 * immediately otherwise if the regulator is disabled the regulator will
2340 * output at the new current when enabled.
2341 *
2342 * NOTE: Regulator system constraints must be set for this regulator before
2343 * calling this function otherwise this call will fail.
2344 */
2345int regulator_set_current_limit(struct regulator *regulator,
2346 int min_uA, int max_uA)
2347{
2348 struct regulator_dev *rdev = regulator->rdev;
2349 int ret;
2350
2351 mutex_lock(&rdev->mutex);
2352
2353 /* sanity check */
2354 if (!rdev->desc->ops->set_current_limit) {
2355 ret = -EINVAL;
2356 goto out;
2357 }
2358
2359 /* constraints check */
2360 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2361 if (ret < 0)
2362 goto out;
2363
2364 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2365out:
2366 mutex_unlock(&rdev->mutex);
2367 return ret;
2368}
2369EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2370
2371static int _regulator_get_current_limit(struct regulator_dev *rdev)
2372{
2373 int ret;
2374
2375 mutex_lock(&rdev->mutex);
2376
2377 /* sanity check */
2378 if (!rdev->desc->ops->get_current_limit) {
2379 ret = -EINVAL;
2380 goto out;
2381 }
2382
2383 ret = rdev->desc->ops->get_current_limit(rdev);
2384out:
2385 mutex_unlock(&rdev->mutex);
2386 return ret;
2387}
2388
2389/**
2390 * regulator_get_current_limit - get regulator output current
2391 * @regulator: regulator source
2392 *
2393 * This returns the current supplied by the specified current sink in uA.
2394 *
2395 * NOTE: If the regulator is disabled it will return the current value. This
2396 * function should not be used to determine regulator state.
2397 */
2398int regulator_get_current_limit(struct regulator *regulator)
2399{
2400 return _regulator_get_current_limit(regulator->rdev);
2401}
2402EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2403
2404/**
2405 * regulator_set_mode - set regulator operating mode
2406 * @regulator: regulator source
2407 * @mode: operating mode - one of the REGULATOR_MODE constants
2408 *
2409 * Set regulator operating mode to increase regulator efficiency or improve
2410 * regulation performance.
2411 *
2412 * NOTE: Regulator system constraints must be set for this regulator before
2413 * calling this function otherwise this call will fail.
2414 */
2415int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2416{
2417 struct regulator_dev *rdev = regulator->rdev;
2418 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302419 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002420
2421 mutex_lock(&rdev->mutex);
2422
2423 /* sanity check */
2424 if (!rdev->desc->ops->set_mode) {
2425 ret = -EINVAL;
2426 goto out;
2427 }
2428
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302429 /* return if the same mode is requested */
2430 if (rdev->desc->ops->get_mode) {
2431 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2432 if (regulator_curr_mode == mode) {
2433 ret = 0;
2434 goto out;
2435 }
2436 }
2437
Liam Girdwood414c70c2008-04-30 15:59:04 +01002438 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002439 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002440 if (ret < 0)
2441 goto out;
2442
2443 ret = rdev->desc->ops->set_mode(rdev, mode);
2444out:
2445 mutex_unlock(&rdev->mutex);
2446 return ret;
2447}
2448EXPORT_SYMBOL_GPL(regulator_set_mode);
2449
2450static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2451{
2452 int ret;
2453
2454 mutex_lock(&rdev->mutex);
2455
2456 /* sanity check */
2457 if (!rdev->desc->ops->get_mode) {
2458 ret = -EINVAL;
2459 goto out;
2460 }
2461
2462 ret = rdev->desc->ops->get_mode(rdev);
2463out:
2464 mutex_unlock(&rdev->mutex);
2465 return ret;
2466}
2467
2468/**
2469 * regulator_get_mode - get regulator operating mode
2470 * @regulator: regulator source
2471 *
2472 * Get the current regulator operating mode.
2473 */
2474unsigned int regulator_get_mode(struct regulator *regulator)
2475{
2476 return _regulator_get_mode(regulator->rdev);
2477}
2478EXPORT_SYMBOL_GPL(regulator_get_mode);
2479
2480/**
2481 * regulator_set_optimum_mode - set regulator optimum operating mode
2482 * @regulator: regulator source
2483 * @uA_load: load current
2484 *
2485 * Notifies the regulator core of a new device load. This is then used by
2486 * DRMS (if enabled by constraints) to set the most efficient regulator
2487 * operating mode for the new regulator loading.
2488 *
2489 * Consumer devices notify their supply regulator of the maximum power
2490 * they will require (can be taken from device datasheet in the power
2491 * consumption tables) when they change operational status and hence power
2492 * state. Examples of operational state changes that can affect power
2493 * consumption are :-
2494 *
2495 * o Device is opened / closed.
2496 * o Device I/O is about to begin or has just finished.
2497 * o Device is idling in between work.
2498 *
2499 * This information is also exported via sysfs to userspace.
2500 *
2501 * DRMS will sum the total requested load on the regulator and change
2502 * to the most efficient operating mode if platform constraints allow.
2503 *
2504 * Returns the new regulator mode or error.
2505 */
2506int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2507{
2508 struct regulator_dev *rdev = regulator->rdev;
2509 struct regulator *consumer;
2510 int ret, output_uV, input_uV, total_uA_load = 0;
2511 unsigned int mode;
2512
2513 mutex_lock(&rdev->mutex);
2514
Mark Browna4b41482011-05-14 11:19:45 -07002515 /*
2516 * first check to see if we can set modes at all, otherwise just
2517 * tell the consumer everything is OK.
2518 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002519 regulator->uA_load = uA_load;
2520 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002521 if (ret < 0) {
2522 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002523 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002524 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002525
Liam Girdwood414c70c2008-04-30 15:59:04 +01002526 if (!rdev->desc->ops->get_optimum_mode)
2527 goto out;
2528
Mark Browna4b41482011-05-14 11:19:45 -07002529 /*
2530 * we can actually do this so any errors are indicators of
2531 * potential real failure.
2532 */
2533 ret = -EINVAL;
2534
Axel Lin854ccba2012-04-16 18:44:23 +08002535 if (!rdev->desc->ops->set_mode)
2536 goto out;
2537
Liam Girdwood414c70c2008-04-30 15:59:04 +01002538 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002539 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002540 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002541 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002542 goto out;
2543 }
2544
2545 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002546 input_uV = 0;
2547 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002548 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002549 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002550 input_uV = rdev->constraints->input_uV;
2551 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002552 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002553 goto out;
2554 }
2555
2556 /* calc total requested load for this regulator */
2557 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002558 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002559
2560 mode = rdev->desc->ops->get_optimum_mode(rdev,
2561 input_uV, output_uV,
2562 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002563 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002564 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002565 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2566 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002567 goto out;
2568 }
2569
2570 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002571 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002572 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002573 goto out;
2574 }
2575 ret = mode;
2576out:
2577 mutex_unlock(&rdev->mutex);
2578 return ret;
2579}
2580EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2581
2582/**
2583 * regulator_register_notifier - register regulator event notifier
2584 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002585 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002586 *
2587 * Register notifier block to receive regulator events.
2588 */
2589int regulator_register_notifier(struct regulator *regulator,
2590 struct notifier_block *nb)
2591{
2592 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2593 nb);
2594}
2595EXPORT_SYMBOL_GPL(regulator_register_notifier);
2596
2597/**
2598 * regulator_unregister_notifier - unregister regulator event notifier
2599 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002600 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002601 *
2602 * Unregister regulator event notifier block.
2603 */
2604int regulator_unregister_notifier(struct regulator *regulator,
2605 struct notifier_block *nb)
2606{
2607 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2608 nb);
2609}
2610EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2611
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002612/* notify regulator consumers and downstream regulator consumers.
2613 * Note mutex must be held by caller.
2614 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002615static void _notifier_call_chain(struct regulator_dev *rdev,
2616 unsigned long event, void *data)
2617{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002618 /* call rdev chain first */
Mark Brownd8493d22012-06-15 19:09:09 +01002619 blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002620}
2621
2622/**
2623 * regulator_bulk_get - get multiple regulator consumers
2624 *
2625 * @dev: Device to supply
2626 * @num_consumers: Number of consumers to register
2627 * @consumers: Configuration of consumers; clients are stored here.
2628 *
2629 * @return 0 on success, an errno on failure.
2630 *
2631 * This helper function allows drivers to get several regulator
2632 * consumers in one operation. If any of the regulators cannot be
2633 * acquired then any regulators that were allocated will be freed
2634 * before returning to the caller.
2635 */
2636int regulator_bulk_get(struct device *dev, int num_consumers,
2637 struct regulator_bulk_data *consumers)
2638{
2639 int i;
2640 int ret;
2641
2642 for (i = 0; i < num_consumers; i++)
2643 consumers[i].consumer = NULL;
2644
2645 for (i = 0; i < num_consumers; i++) {
2646 consumers[i].consumer = regulator_get(dev,
2647 consumers[i].supply);
2648 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002649 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002650 dev_err(dev, "Failed to get supply '%s': %d\n",
2651 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002652 consumers[i].consumer = NULL;
2653 goto err;
2654 }
2655 }
2656
2657 return 0;
2658
2659err:
Axel Linb29c7692012-02-20 10:32:16 +08002660 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002661 regulator_put(consumers[i].consumer);
2662
2663 return ret;
2664}
2665EXPORT_SYMBOL_GPL(regulator_bulk_get);
2666
Mark Browne6e74032012-01-20 20:10:08 +00002667/**
2668 * devm_regulator_bulk_get - managed get multiple regulator consumers
2669 *
2670 * @dev: Device to supply
2671 * @num_consumers: Number of consumers to register
2672 * @consumers: Configuration of consumers; clients are stored here.
2673 *
2674 * @return 0 on success, an errno on failure.
2675 *
2676 * This helper function allows drivers to get several regulator
2677 * consumers in one operation with management, the regulators will
2678 * automatically be freed when the device is unbound. If any of the
2679 * regulators cannot be acquired then any regulators that were
2680 * allocated will be freed before returning to the caller.
2681 */
2682int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2683 struct regulator_bulk_data *consumers)
2684{
2685 int i;
2686 int ret;
2687
2688 for (i = 0; i < num_consumers; i++)
2689 consumers[i].consumer = NULL;
2690
2691 for (i = 0; i < num_consumers; i++) {
2692 consumers[i].consumer = devm_regulator_get(dev,
2693 consumers[i].supply);
2694 if (IS_ERR(consumers[i].consumer)) {
2695 ret = PTR_ERR(consumers[i].consumer);
2696 dev_err(dev, "Failed to get supply '%s': %d\n",
2697 consumers[i].supply, ret);
2698 consumers[i].consumer = NULL;
2699 goto err;
2700 }
2701 }
2702
2703 return 0;
2704
2705err:
2706 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2707 devm_regulator_put(consumers[i].consumer);
2708
2709 return ret;
2710}
2711EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2712
Mark Brownf21e0e82011-05-24 08:12:40 +08002713static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2714{
2715 struct regulator_bulk_data *bulk = data;
2716
2717 bulk->ret = regulator_enable(bulk->consumer);
2718}
2719
Liam Girdwood414c70c2008-04-30 15:59:04 +01002720/**
2721 * regulator_bulk_enable - enable multiple regulator consumers
2722 *
2723 * @num_consumers: Number of consumers
2724 * @consumers: Consumer data; clients are stored here.
2725 * @return 0 on success, an errno on failure
2726 *
2727 * This convenience API allows consumers to enable multiple regulator
2728 * clients in a single API call. If any consumers cannot be enabled
2729 * then any others that were enabled will be disabled again prior to
2730 * return.
2731 */
2732int regulator_bulk_enable(int num_consumers,
2733 struct regulator_bulk_data *consumers)
2734{
Mark Brownf21e0e82011-05-24 08:12:40 +08002735 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002736 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002737 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002738
Mark Brown6492bc12012-04-19 13:19:07 +01002739 for (i = 0; i < num_consumers; i++) {
2740 if (consumers[i].consumer->always_on)
2741 consumers[i].ret = 0;
2742 else
2743 async_schedule_domain(regulator_bulk_enable_async,
2744 &consumers[i], &async_domain);
2745 }
Mark Brownf21e0e82011-05-24 08:12:40 +08002746
2747 async_synchronize_full_domain(&async_domain);
2748
2749 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002750 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002751 if (consumers[i].ret != 0) {
2752 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002753 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002754 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002755 }
2756
2757 return 0;
2758
2759err:
Axel Linb29c7692012-02-20 10:32:16 +08002760 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2761 while (--i >= 0)
2762 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002763
2764 return ret;
2765}
2766EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2767
2768/**
2769 * regulator_bulk_disable - disable multiple regulator consumers
2770 *
2771 * @num_consumers: Number of consumers
2772 * @consumers: Consumer data; clients are stored here.
2773 * @return 0 on success, an errno on failure
2774 *
2775 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002776 * clients in a single API call. If any consumers cannot be disabled
2777 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002778 * return.
2779 */
2780int regulator_bulk_disable(int num_consumers,
2781 struct regulator_bulk_data *consumers)
2782{
2783 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01002784 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002785
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002786 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002787 ret = regulator_disable(consumers[i].consumer);
2788 if (ret != 0)
2789 goto err;
2790 }
2791
2792 return 0;
2793
2794err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002795 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01002796 for (++i; i < num_consumers; ++i) {
2797 r = regulator_enable(consumers[i].consumer);
2798 if (r != 0)
2799 pr_err("Failed to reename %s: %d\n",
2800 consumers[i].supply, r);
2801 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002802
2803 return ret;
2804}
2805EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2806
2807/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002808 * regulator_bulk_force_disable - force disable multiple regulator consumers
2809 *
2810 * @num_consumers: Number of consumers
2811 * @consumers: Consumer data; clients are stored here.
2812 * @return 0 on success, an errno on failure
2813 *
2814 * This convenience API allows consumers to forcibly disable multiple regulator
2815 * clients in a single API call.
2816 * NOTE: This should be used for situations when device damage will
2817 * likely occur if the regulators are not disabled (e.g. over temp).
2818 * Although regulator_force_disable function call for some consumers can
2819 * return error numbers, the function is called for all consumers.
2820 */
2821int regulator_bulk_force_disable(int num_consumers,
2822 struct regulator_bulk_data *consumers)
2823{
2824 int i;
2825 int ret;
2826
2827 for (i = 0; i < num_consumers; i++)
2828 consumers[i].ret =
2829 regulator_force_disable(consumers[i].consumer);
2830
2831 for (i = 0; i < num_consumers; i++) {
2832 if (consumers[i].ret != 0) {
2833 ret = consumers[i].ret;
2834 goto out;
2835 }
2836 }
2837
2838 return 0;
2839out:
2840 return ret;
2841}
2842EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2843
2844/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002845 * regulator_bulk_free - free multiple regulator consumers
2846 *
2847 * @num_consumers: Number of consumers
2848 * @consumers: Consumer data; clients are stored here.
2849 *
2850 * This convenience API allows consumers to free multiple regulator
2851 * clients in a single API call.
2852 */
2853void regulator_bulk_free(int num_consumers,
2854 struct regulator_bulk_data *consumers)
2855{
2856 int i;
2857
2858 for (i = 0; i < num_consumers; i++) {
2859 regulator_put(consumers[i].consumer);
2860 consumers[i].consumer = NULL;
2861 }
2862}
2863EXPORT_SYMBOL_GPL(regulator_bulk_free);
2864
2865/**
2866 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002867 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002868 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002869 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002870 *
2871 * Called by regulator drivers to notify clients a regulator event has
2872 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002873 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002874 */
2875int regulator_notifier_call_chain(struct regulator_dev *rdev,
2876 unsigned long event, void *data)
2877{
2878 _notifier_call_chain(rdev, event, data);
2879 return NOTIFY_DONE;
2880
2881}
2882EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2883
Mark Brownbe721972009-08-04 20:09:52 +02002884/**
2885 * regulator_mode_to_status - convert a regulator mode into a status
2886 *
2887 * @mode: Mode to convert
2888 *
2889 * Convert a regulator mode into a status.
2890 */
2891int regulator_mode_to_status(unsigned int mode)
2892{
2893 switch (mode) {
2894 case REGULATOR_MODE_FAST:
2895 return REGULATOR_STATUS_FAST;
2896 case REGULATOR_MODE_NORMAL:
2897 return REGULATOR_STATUS_NORMAL;
2898 case REGULATOR_MODE_IDLE:
2899 return REGULATOR_STATUS_IDLE;
2900 case REGULATOR_STATUS_STANDBY:
2901 return REGULATOR_STATUS_STANDBY;
2902 default:
2903 return 0;
2904 }
2905}
2906EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2907
David Brownell7ad68e22008-11-11 17:39:02 -08002908/*
2909 * To avoid cluttering sysfs (and memory) with useless state, only
2910 * create attributes that can be meaningfully displayed.
2911 */
2912static int add_regulator_attributes(struct regulator_dev *rdev)
2913{
2914 struct device *dev = &rdev->dev;
2915 struct regulator_ops *ops = rdev->desc->ops;
2916 int status = 0;
2917
2918 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002919 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2920 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002921 status = device_create_file(dev, &dev_attr_microvolts);
2922 if (status < 0)
2923 return status;
2924 }
2925 if (ops->get_current_limit) {
2926 status = device_create_file(dev, &dev_attr_microamps);
2927 if (status < 0)
2928 return status;
2929 }
2930 if (ops->get_mode) {
2931 status = device_create_file(dev, &dev_attr_opmode);
2932 if (status < 0)
2933 return status;
2934 }
2935 if (ops->is_enabled) {
2936 status = device_create_file(dev, &dev_attr_state);
2937 if (status < 0)
2938 return status;
2939 }
David Brownell853116a2009-01-14 23:03:17 -08002940 if (ops->get_status) {
2941 status = device_create_file(dev, &dev_attr_status);
2942 if (status < 0)
2943 return status;
2944 }
David Brownell7ad68e22008-11-11 17:39:02 -08002945
2946 /* some attributes are type-specific */
2947 if (rdev->desc->type == REGULATOR_CURRENT) {
2948 status = device_create_file(dev, &dev_attr_requested_microamps);
2949 if (status < 0)
2950 return status;
2951 }
2952
2953 /* all the other attributes exist to support constraints;
2954 * don't show them if there are no constraints, or if the
2955 * relevant supporting methods are missing.
2956 */
2957 if (!rdev->constraints)
2958 return status;
2959
2960 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002961 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002962 status = device_create_file(dev, &dev_attr_min_microvolts);
2963 if (status < 0)
2964 return status;
2965 status = device_create_file(dev, &dev_attr_max_microvolts);
2966 if (status < 0)
2967 return status;
2968 }
2969 if (ops->set_current_limit) {
2970 status = device_create_file(dev, &dev_attr_min_microamps);
2971 if (status < 0)
2972 return status;
2973 status = device_create_file(dev, &dev_attr_max_microamps);
2974 if (status < 0)
2975 return status;
2976 }
2977
David Brownell7ad68e22008-11-11 17:39:02 -08002978 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2979 if (status < 0)
2980 return status;
2981 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2982 if (status < 0)
2983 return status;
2984 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2985 if (status < 0)
2986 return status;
2987
2988 if (ops->set_suspend_voltage) {
2989 status = device_create_file(dev,
2990 &dev_attr_suspend_standby_microvolts);
2991 if (status < 0)
2992 return status;
2993 status = device_create_file(dev,
2994 &dev_attr_suspend_mem_microvolts);
2995 if (status < 0)
2996 return status;
2997 status = device_create_file(dev,
2998 &dev_attr_suspend_disk_microvolts);
2999 if (status < 0)
3000 return status;
3001 }
3002
3003 if (ops->set_suspend_mode) {
3004 status = device_create_file(dev,
3005 &dev_attr_suspend_standby_mode);
3006 if (status < 0)
3007 return status;
3008 status = device_create_file(dev,
3009 &dev_attr_suspend_mem_mode);
3010 if (status < 0)
3011 return status;
3012 status = device_create_file(dev,
3013 &dev_attr_suspend_disk_mode);
3014 if (status < 0)
3015 return status;
3016 }
3017
3018 return status;
3019}
3020
Mark Brown1130e5b2010-12-21 23:49:31 +00003021static void rdev_init_debugfs(struct regulator_dev *rdev)
3022{
Mark Brown1130e5b2010-12-21 23:49:31 +00003023 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003024 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003025 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003026 return;
3027 }
3028
3029 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3030 &rdev->use_count);
3031 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3032 &rdev->open_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003033}
3034
Liam Girdwood414c70c2008-04-30 15:59:04 +01003035/**
3036 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003037 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003038 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003039 *
3040 * Called by regulator drivers to register a regulator.
3041 * Returns 0 on success.
3042 */
Mark Brown65f26842012-04-03 20:46:53 +01003043struct regulator_dev *
3044regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003045 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003046{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003047 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003048 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003049 static atomic_t regulator_no = ATOMIC_INIT(0);
3050 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003051 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003052 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303053 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003054
Mark Brownc1727082012-04-04 00:50:22 +01003055 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003056 return ERR_PTR(-EINVAL);
3057
Mark Brown32c8fad2012-04-11 10:19:12 +01003058 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003059 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003060
Liam Girdwood414c70c2008-04-30 15:59:04 +01003061 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3062 return ERR_PTR(-EINVAL);
3063
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003064 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3065 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003066 return ERR_PTR(-EINVAL);
3067
Mark Brown476c2d82010-12-10 17:28:07 +00003068 /* Only one of each should be implemented */
3069 WARN_ON(regulator_desc->ops->get_voltage &&
3070 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003071 WARN_ON(regulator_desc->ops->set_voltage &&
3072 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003073
3074 /* If we're using selectors we must implement list_voltage. */
3075 if (regulator_desc->ops->get_voltage_sel &&
3076 !regulator_desc->ops->list_voltage) {
3077 return ERR_PTR(-EINVAL);
3078 }
Mark Browne8eef822010-12-12 14:36:17 +00003079 if (regulator_desc->ops->set_voltage_sel &&
3080 !regulator_desc->ops->list_voltage) {
3081 return ERR_PTR(-EINVAL);
3082 }
Mark Brown476c2d82010-12-10 17:28:07 +00003083
Mark Brownc1727082012-04-04 00:50:22 +01003084 init_data = config->init_data;
3085
Liam Girdwood414c70c2008-04-30 15:59:04 +01003086 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3087 if (rdev == NULL)
3088 return ERR_PTR(-ENOMEM);
3089
3090 mutex_lock(&regulator_list_mutex);
3091
3092 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003093 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003094 rdev->owner = regulator_desc->owner;
3095 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003096 if (config->regmap)
3097 rdev->regmap = config->regmap;
3098 else
3099 rdev->regmap = dev_get_regmap(dev, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003100 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003101 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003102 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003103 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003104
Liam Girdwooda5766f12008-10-10 13:22:20 +01003105 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003106 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003107 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003108 if (ret < 0)
3109 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003110 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003111
Liam Girdwooda5766f12008-10-10 13:22:20 +01003112 /* register with sysfs */
3113 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003114 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003115 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003116 dev_set_name(&rdev->dev, "regulator.%d",
3117 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003118 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003119 if (ret != 0) {
3120 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003121 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003122 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003123
3124 dev_set_drvdata(&rdev->dev, rdev);
3125
Mike Rapoport74f544c2008-11-25 14:53:53 +02003126 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003127 if (init_data)
3128 constraints = &init_data->constraints;
3129
3130 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003131 if (ret < 0)
3132 goto scrub;
3133
David Brownell7ad68e22008-11-11 17:39:02 -08003134 /* add attributes supported by this regulator */
3135 ret = add_regulator_attributes(rdev);
3136 if (ret < 0)
3137 goto scrub;
3138
Mark Brown9a8f5e02011-11-29 18:11:19 +00003139 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303140 supply = init_data->supply_regulator;
3141 else if (regulator_desc->supply_name)
3142 supply = regulator_desc->supply_name;
3143
3144 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003145 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003146
Mark Brown6d191a52012-03-29 14:19:02 +01003147 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003148
Rajendra Nayak69511a42011-11-18 16:47:20 +05303149 if (!r) {
3150 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003151 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003152 goto scrub;
3153 }
3154
3155 ret = set_supply(rdev, r);
3156 if (ret < 0)
3157 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303158
3159 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003160 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303161 ret = regulator_enable(rdev->supply);
3162 if (ret < 0)
3163 goto scrub;
3164 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003165 }
3166
Liam Girdwooda5766f12008-10-10 13:22:20 +01003167 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003168 if (init_data) {
3169 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3170 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003171 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003172 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003173 if (ret < 0) {
3174 dev_err(dev, "Failed to set supply %s\n",
3175 init_data->consumer_supplies[i].supply);
3176 goto unset_supplies;
3177 }
Mark Brown23c2f042011-02-24 17:39:09 +00003178 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003179 }
3180
3181 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003182
3183 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003184out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003185 mutex_unlock(&regulator_list_mutex);
3186 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003187
Jani Nikulad4033b52010-04-29 10:55:11 +03003188unset_supplies:
3189 unset_regulator_supplies(rdev);
3190
David Brownell4fca9542008-11-11 17:38:53 -08003191scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003192 if (rdev->supply)
3193 regulator_put(rdev->supply);
Axel Lin1a6958e72011-07-15 10:50:43 +08003194 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08003195 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003196 /* device core frees rdev */
3197 rdev = ERR_PTR(ret);
3198 goto out;
3199
David Brownell4fca9542008-11-11 17:38:53 -08003200clean:
3201 kfree(rdev);
3202 rdev = ERR_PTR(ret);
3203 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003204}
3205EXPORT_SYMBOL_GPL(regulator_register);
3206
3207/**
3208 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003209 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003210 *
3211 * Called by regulator drivers to unregister a regulator.
3212 */
3213void regulator_unregister(struct regulator_dev *rdev)
3214{
3215 if (rdev == NULL)
3216 return;
3217
Mark Browne032b372012-03-28 21:17:55 +01003218 if (rdev->supply)
3219 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003220 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003221 debugfs_remove_recursive(rdev->debugfs);
Mark Brownda07ecd2011-09-11 09:53:50 +01003222 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003223 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003224 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003225 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003226 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003227 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003228 mutex_unlock(&regulator_list_mutex);
3229}
3230EXPORT_SYMBOL_GPL(regulator_unregister);
3231
3232/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003233 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003234 * @state: system suspend state
3235 *
3236 * Configure each regulator with it's suspend operating parameters for state.
3237 * This will usually be called by machine suspend code prior to supending.
3238 */
3239int regulator_suspend_prepare(suspend_state_t state)
3240{
3241 struct regulator_dev *rdev;
3242 int ret = 0;
3243
3244 /* ON is handled by regulator active state */
3245 if (state == PM_SUSPEND_ON)
3246 return -EINVAL;
3247
3248 mutex_lock(&regulator_list_mutex);
3249 list_for_each_entry(rdev, &regulator_list, list) {
3250
3251 mutex_lock(&rdev->mutex);
3252 ret = suspend_prepare(rdev, state);
3253 mutex_unlock(&rdev->mutex);
3254
3255 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003256 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003257 goto out;
3258 }
3259 }
3260out:
3261 mutex_unlock(&regulator_list_mutex);
3262 return ret;
3263}
3264EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3265
3266/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003267 * regulator_suspend_finish - resume regulators from system wide suspend
3268 *
3269 * Turn on regulators that might be turned off by regulator_suspend_prepare
3270 * and that should be turned on according to the regulators properties.
3271 */
3272int regulator_suspend_finish(void)
3273{
3274 struct regulator_dev *rdev;
3275 int ret = 0, error;
3276
3277 mutex_lock(&regulator_list_mutex);
3278 list_for_each_entry(rdev, &regulator_list, list) {
3279 struct regulator_ops *ops = rdev->desc->ops;
3280
3281 mutex_lock(&rdev->mutex);
3282 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3283 ops->enable) {
3284 error = ops->enable(rdev);
3285 if (error)
3286 ret = error;
3287 } else {
3288 if (!has_full_constraints)
3289 goto unlock;
3290 if (!ops->disable)
3291 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003292 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003293 goto unlock;
3294
3295 error = ops->disable(rdev);
3296 if (error)
3297 ret = error;
3298 }
3299unlock:
3300 mutex_unlock(&rdev->mutex);
3301 }
3302 mutex_unlock(&regulator_list_mutex);
3303 return ret;
3304}
3305EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3306
3307/**
Mark Brownca725562009-03-16 19:36:34 +00003308 * regulator_has_full_constraints - the system has fully specified constraints
3309 *
3310 * Calling this function will cause the regulator API to disable all
3311 * regulators which have a zero use count and don't have an always_on
3312 * constraint in a late_initcall.
3313 *
3314 * The intention is that this will become the default behaviour in a
3315 * future kernel release so users are encouraged to use this facility
3316 * now.
3317 */
3318void regulator_has_full_constraints(void)
3319{
3320 has_full_constraints = 1;
3321}
3322EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3323
3324/**
Mark Brown688fe992010-10-05 19:18:32 -07003325 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3326 *
3327 * Calling this function will cause the regulator API to provide a
3328 * dummy regulator to consumers if no physical regulator is found,
3329 * allowing most consumers to proceed as though a regulator were
3330 * configured. This allows systems such as those with software
3331 * controllable regulators for the CPU core only to be brought up more
3332 * readily.
3333 */
3334void regulator_use_dummy_regulator(void)
3335{
3336 board_wants_dummy_regulator = true;
3337}
3338EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3339
3340/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003341 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003342 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003343 *
3344 * Get rdev regulator driver private data. This call can be used in the
3345 * regulator driver context.
3346 */
3347void *rdev_get_drvdata(struct regulator_dev *rdev)
3348{
3349 return rdev->reg_data;
3350}
3351EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3352
3353/**
3354 * regulator_get_drvdata - get regulator driver data
3355 * @regulator: regulator
3356 *
3357 * Get regulator driver private data. This call can be used in the consumer
3358 * driver context when non API regulator specific functions need to be called.
3359 */
3360void *regulator_get_drvdata(struct regulator *regulator)
3361{
3362 return regulator->rdev->reg_data;
3363}
3364EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3365
3366/**
3367 * regulator_set_drvdata - set regulator driver data
3368 * @regulator: regulator
3369 * @data: data
3370 */
3371void regulator_set_drvdata(struct regulator *regulator, void *data)
3372{
3373 regulator->rdev->reg_data = data;
3374}
3375EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3376
3377/**
3378 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003379 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003380 */
3381int rdev_get_id(struct regulator_dev *rdev)
3382{
3383 return rdev->desc->id;
3384}
3385EXPORT_SYMBOL_GPL(rdev_get_id);
3386
Liam Girdwooda5766f12008-10-10 13:22:20 +01003387struct device *rdev_get_dev(struct regulator_dev *rdev)
3388{
3389 return &rdev->dev;
3390}
3391EXPORT_SYMBOL_GPL(rdev_get_dev);
3392
3393void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3394{
3395 return reg_init_data->driver_data;
3396}
3397EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3398
Mark Brownba55a972011-08-23 17:39:10 +01003399#ifdef CONFIG_DEBUG_FS
3400static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3401 size_t count, loff_t *ppos)
3402{
3403 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3404 ssize_t len, ret = 0;
3405 struct regulator_map *map;
3406
3407 if (!buf)
3408 return -ENOMEM;
3409
3410 list_for_each_entry(map, &regulator_map_list, list) {
3411 len = snprintf(buf + ret, PAGE_SIZE - ret,
3412 "%s -> %s.%s\n",
3413 rdev_get_name(map->regulator), map->dev_name,
3414 map->supply);
3415 if (len >= 0)
3416 ret += len;
3417 if (ret > PAGE_SIZE) {
3418 ret = PAGE_SIZE;
3419 break;
3420 }
3421 }
3422
3423 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3424
3425 kfree(buf);
3426
3427 return ret;
3428}
Stephen Boyd24751432012-02-20 22:50:42 -08003429#endif
Mark Brownba55a972011-08-23 17:39:10 +01003430
3431static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003432#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003433 .read = supply_map_read_file,
3434 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003435#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003436};
Mark Brownba55a972011-08-23 17:39:10 +01003437
Liam Girdwood414c70c2008-04-30 15:59:04 +01003438static int __init regulator_init(void)
3439{
Mark Brown34abbd62010-02-12 10:18:08 +00003440 int ret;
3441
Mark Brown34abbd62010-02-12 10:18:08 +00003442 ret = class_register(&regulator_class);
3443
Mark Brown1130e5b2010-12-21 23:49:31 +00003444 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003445 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003446 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003447
Mark Brownf4d562c2012-02-20 21:01:04 +00003448 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3449 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003450
Mark Brown34abbd62010-02-12 10:18:08 +00003451 regulator_dummy_init();
3452
3453 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003454}
3455
3456/* init early to allow our consumers to complete system booting */
3457core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003458
3459static int __init regulator_init_complete(void)
3460{
3461 struct regulator_dev *rdev;
3462 struct regulator_ops *ops;
3463 struct regulation_constraints *c;
3464 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003465
3466 mutex_lock(&regulator_list_mutex);
3467
3468 /* If we have a full configuration then disable any regulators
3469 * which are not in use or always_on. This will become the
3470 * default behaviour in the future.
3471 */
3472 list_for_each_entry(rdev, &regulator_list, list) {
3473 ops = rdev->desc->ops;
3474 c = rdev->constraints;
3475
Mark Brownf25e0b42009-08-03 18:49:55 +01003476 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003477 continue;
3478
3479 mutex_lock(&rdev->mutex);
3480
3481 if (rdev->use_count)
3482 goto unlock;
3483
3484 /* If we can't read the status assume it's on. */
3485 if (ops->is_enabled)
3486 enabled = ops->is_enabled(rdev);
3487 else
3488 enabled = 1;
3489
3490 if (!enabled)
3491 goto unlock;
3492
3493 if (has_full_constraints) {
3494 /* We log since this may kill the system if it
3495 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003496 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003497 ret = ops->disable(rdev);
3498 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003499 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003500 }
3501 } else {
3502 /* The intention is that in future we will
3503 * assume that full constraints are provided
3504 * so warn even if we aren't going to do
3505 * anything here.
3506 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003507 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003508 }
3509
3510unlock:
3511 mutex_unlock(&rdev->mutex);
3512 }
3513
3514 mutex_unlock(&regulator_list_mutex);
3515
3516 return 0;
3517}
3518late_initcall(regulator_init_complete);