blob: 6382f0af353bc257e3ee6c3b545f75c0c1f2904e [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>
Mark Brown65f73502012-06-27 14:14:38 +010026#include <linux/gpio.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053027#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010028#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053029#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#include <linux/regulator/consumer.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040033#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010034
Mark Brown02fa3ec2010-11-10 14:38:30 +000035#define CREATE_TRACE_POINTS
36#include <trace/events/regulator.h>
37
Mark Brown34abbd62010-02-12 10:18:08 +000038#include "dummy.h"
Mark Brown0cdfcc02013-09-11 13:15:40 +010039#include "internal.h"
Mark Brown34abbd62010-02-12 10:18:08 +000040
Mark Brown7d51a0d2011-06-09 16:06:37 +010041#define rdev_crit(rdev, fmt, ...) \
42 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080043#define rdev_err(rdev, fmt, ...) \
44 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45#define rdev_warn(rdev, fmt, ...) \
46 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47#define rdev_info(rdev, fmt, ...) \
48 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49#define rdev_dbg(rdev, fmt, ...) \
50 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
51
Liam Girdwood414c70c2008-04-30 15:59:04 +010052static DEFINE_MUTEX(regulator_list_mutex);
53static LIST_HEAD(regulator_list);
54static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000055static LIST_HEAD(regulator_ena_gpio_list);
Charles Keepaxa06ccd92013-10-15 20:14:20 +010056static LIST_HEAD(regulator_supply_alias_list);
Mark Brown21cf8912010-12-21 23:30:07 +000057static bool has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010058
Mark Brown1130e5b2010-12-21 23:49:31 +000059static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000060
Mark Brown8dc53902008-12-31 12:52:40 +000061/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010062 * struct regulator_map
63 *
64 * Used to provide symbolic supply names to devices.
65 */
66struct regulator_map {
67 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010068 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010069 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010070 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010071};
72
Liam Girdwood414c70c2008-04-30 15:59:04 +010073/*
Kim, Milof19b00d2013-02-18 06:50:39 +000074 * struct regulator_enable_gpio
75 *
76 * Management for shared enable GPIO pin
77 */
78struct regulator_enable_gpio {
79 struct list_head list;
80 int gpio;
81 u32 enable_count; /* a number of enabled shared GPIO */
82 u32 request_count; /* a number of requested shared GPIO */
83 unsigned int ena_gpio_invert:1;
84};
85
Charles Keepaxa06ccd92013-10-15 20:14:20 +010086/*
87 * struct regulator_supply_alias
88 *
89 * Used to map lookups for a supply onto an alternative device.
90 */
91struct regulator_supply_alias {
92 struct list_head list;
93 struct device *src_dev;
94 const char *src_supply;
95 struct device *alias_dev;
96 const char *alias_supply;
97};
98
Liam Girdwood414c70c2008-04-30 15:59:04 +010099static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100100static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100101static int _regulator_get_voltage(struct regulator_dev *rdev);
102static int _regulator_get_current_limit(struct regulator_dev *rdev);
103static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
104static void _notifier_call_chain(struct regulator_dev *rdev,
105 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000106static int _regulator_do_set_voltage(struct regulator_dev *rdev,
107 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100108static struct regulator *create_regulator(struct regulator_dev *rdev,
109 struct device *dev,
110 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100111
Mark Brown1083c392009-10-22 16:31:32 +0100112static const char *rdev_get_name(struct regulator_dev *rdev)
113{
114 if (rdev->constraints && rdev->constraints->name)
115 return rdev->constraints->name;
116 else if (rdev->desc->name)
117 return rdev->desc->name;
118 else
119 return "";
120}
121
Rajendra Nayak69511a42011-11-18 16:47:20 +0530122/**
123 * of_get_regulator - get a regulator device node based on supply name
124 * @dev: Device pointer for the consumer (of regulator) device
125 * @supply: regulator supply name
126 *
127 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100128 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530129 * returns NULL.
130 */
131static struct device_node *of_get_regulator(struct device *dev, const char *supply)
132{
133 struct device_node *regnode = NULL;
134 char prop_name[32]; /* 32 is max size of property name */
135
136 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
137
138 snprintf(prop_name, 32, "%s-supply", supply);
139 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
140
141 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530142 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530143 prop_name, dev->of_node->full_name);
144 return NULL;
145 }
146 return regnode;
147}
148
Mark Brown6492bc12012-04-19 13:19:07 +0100149static int _regulator_can_change_status(struct regulator_dev *rdev)
150{
151 if (!rdev->constraints)
152 return 0;
153
154 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
155 return 1;
156 else
157 return 0;
158}
159
Liam Girdwood414c70c2008-04-30 15:59:04 +0100160/* Platform voltage constraint check */
161static int regulator_check_voltage(struct regulator_dev *rdev,
162 int *min_uV, int *max_uV)
163{
164 BUG_ON(*min_uV > *max_uV);
165
166 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800167 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100168 return -ENODEV;
169 }
170 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800171 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100172 return -EPERM;
173 }
174
175 if (*max_uV > rdev->constraints->max_uV)
176 *max_uV = rdev->constraints->max_uV;
177 if (*min_uV < rdev->constraints->min_uV)
178 *min_uV = rdev->constraints->min_uV;
179
Mark Brown89f425e2011-07-12 11:20:37 +0900180 if (*min_uV > *max_uV) {
181 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100182 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100183 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900184 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100185
186 return 0;
187}
188
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100189/* Make sure we select a voltage that suits the needs of all
190 * regulator consumers
191 */
192static int regulator_check_consumers(struct regulator_dev *rdev,
193 int *min_uV, int *max_uV)
194{
195 struct regulator *regulator;
196
197 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700198 /*
199 * Assume consumers that didn't say anything are OK
200 * with anything in the constraint range.
201 */
202 if (!regulator->min_uV && !regulator->max_uV)
203 continue;
204
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100205 if (*max_uV > regulator->max_uV)
206 *max_uV = regulator->max_uV;
207 if (*min_uV < regulator->min_uV)
208 *min_uV = regulator->min_uV;
209 }
210
Mark Browndd8004a2012-11-28 17:09:27 +0000211 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800212 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
213 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100214 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000215 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100216
217 return 0;
218}
219
Liam Girdwood414c70c2008-04-30 15:59:04 +0100220/* current constraint check */
221static int regulator_check_current_limit(struct regulator_dev *rdev,
222 int *min_uA, int *max_uA)
223{
224 BUG_ON(*min_uA > *max_uA);
225
226 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800227 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100228 return -ENODEV;
229 }
230 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800231 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100232 return -EPERM;
233 }
234
235 if (*max_uA > rdev->constraints->max_uA)
236 *max_uA = rdev->constraints->max_uA;
237 if (*min_uA < rdev->constraints->min_uA)
238 *min_uA = rdev->constraints->min_uA;
239
Mark Brown89f425e2011-07-12 11:20:37 +0900240 if (*min_uA > *max_uA) {
241 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100242 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100243 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900244 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100245
246 return 0;
247}
248
249/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900250static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100251{
Mark Brown2c608232011-03-30 06:29:12 +0900252 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800253 case REGULATOR_MODE_FAST:
254 case REGULATOR_MODE_NORMAL:
255 case REGULATOR_MODE_IDLE:
256 case REGULATOR_MODE_STANDBY:
257 break;
258 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900259 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800260 return -EINVAL;
261 }
262
Liam Girdwood414c70c2008-04-30 15:59:04 +0100263 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800264 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100265 return -ENODEV;
266 }
267 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800268 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100269 return -EPERM;
270 }
Mark Brown2c608232011-03-30 06:29:12 +0900271
272 /* The modes are bitmasks, the most power hungry modes having
273 * the lowest values. If the requested mode isn't supported
274 * try higher modes. */
275 while (*mode) {
276 if (rdev->constraints->valid_modes_mask & *mode)
277 return 0;
278 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100279 }
Mark Brown2c608232011-03-30 06:29:12 +0900280
281 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100282}
283
284/* dynamic regulator mode switching constraint check */
285static int regulator_check_drms(struct regulator_dev *rdev)
286{
287 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800288 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289 return -ENODEV;
290 }
291 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800292 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100293 return -EPERM;
294 }
295 return 0;
296}
297
Liam Girdwood414c70c2008-04-30 15:59:04 +0100298static ssize_t regulator_uV_show(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100301 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100302 ssize_t ret;
303
304 mutex_lock(&rdev->mutex);
305 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
306 mutex_unlock(&rdev->mutex);
307
308 return ret;
309}
David Brownell7ad68e22008-11-11 17:39:02 -0800310static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100311
312static ssize_t regulator_uA_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
314{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100315 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100316
317 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
318}
David Brownell7ad68e22008-11-11 17:39:02 -0800319static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100320
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700321static ssize_t name_show(struct device *dev, struct device_attribute *attr,
322 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100323{
324 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100325
Mark Brown1083c392009-10-22 16:31:32 +0100326 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100327}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700328static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100329
David Brownell4fca9542008-11-11 17:38:53 -0800330static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100331{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100332 switch (mode) {
333 case REGULATOR_MODE_FAST:
334 return sprintf(buf, "fast\n");
335 case REGULATOR_MODE_NORMAL:
336 return sprintf(buf, "normal\n");
337 case REGULATOR_MODE_IDLE:
338 return sprintf(buf, "idle\n");
339 case REGULATOR_MODE_STANDBY:
340 return sprintf(buf, "standby\n");
341 }
342 return sprintf(buf, "unknown\n");
343}
344
David Brownell4fca9542008-11-11 17:38:53 -0800345static ssize_t regulator_opmode_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100347{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100348 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100349
David Brownell4fca9542008-11-11 17:38:53 -0800350 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
351}
David Brownell7ad68e22008-11-11 17:39:02 -0800352static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800353
354static ssize_t regulator_print_state(char *buf, int state)
355{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100356 if (state > 0)
357 return sprintf(buf, "enabled\n");
358 else if (state == 0)
359 return sprintf(buf, "disabled\n");
360 else
361 return sprintf(buf, "unknown\n");
362}
363
David Brownell4fca9542008-11-11 17:38:53 -0800364static ssize_t regulator_state_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
366{
367 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100368 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800369
Mark Brown93325462009-08-03 18:49:56 +0100370 mutex_lock(&rdev->mutex);
371 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
372 mutex_unlock(&rdev->mutex);
373
374 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800375}
David Brownell7ad68e22008-11-11 17:39:02 -0800376static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800377
David Brownell853116a2009-01-14 23:03:17 -0800378static ssize_t regulator_status_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
380{
381 struct regulator_dev *rdev = dev_get_drvdata(dev);
382 int status;
383 char *label;
384
385 status = rdev->desc->ops->get_status(rdev);
386 if (status < 0)
387 return status;
388
389 switch (status) {
390 case REGULATOR_STATUS_OFF:
391 label = "off";
392 break;
393 case REGULATOR_STATUS_ON:
394 label = "on";
395 break;
396 case REGULATOR_STATUS_ERROR:
397 label = "error";
398 break;
399 case REGULATOR_STATUS_FAST:
400 label = "fast";
401 break;
402 case REGULATOR_STATUS_NORMAL:
403 label = "normal";
404 break;
405 case REGULATOR_STATUS_IDLE:
406 label = "idle";
407 break;
408 case REGULATOR_STATUS_STANDBY:
409 label = "standby";
410 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700411 case REGULATOR_STATUS_BYPASS:
412 label = "bypass";
413 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100414 case REGULATOR_STATUS_UNDEFINED:
415 label = "undefined";
416 break;
David Brownell853116a2009-01-14 23:03:17 -0800417 default:
418 return -ERANGE;
419 }
420
421 return sprintf(buf, "%s\n", label);
422}
423static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
424
Liam Girdwood414c70c2008-04-30 15:59:04 +0100425static ssize_t regulator_min_uA_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100428 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100429
430 if (!rdev->constraints)
431 return sprintf(buf, "constraint not defined\n");
432
433 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
434}
David Brownell7ad68e22008-11-11 17:39:02 -0800435static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100436
437static ssize_t regulator_max_uA_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
439{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100440 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100441
442 if (!rdev->constraints)
443 return sprintf(buf, "constraint not defined\n");
444
445 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
446}
David Brownell7ad68e22008-11-11 17:39:02 -0800447static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448
449static ssize_t regulator_min_uV_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100452 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453
454 if (!rdev->constraints)
455 return sprintf(buf, "constraint not defined\n");
456
457 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
458}
David Brownell7ad68e22008-11-11 17:39:02 -0800459static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460
461static ssize_t regulator_max_uV_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100464 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465
466 if (!rdev->constraints)
467 return sprintf(buf, "constraint not defined\n");
468
469 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
470}
David Brownell7ad68e22008-11-11 17:39:02 -0800471static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472
473static ssize_t regulator_total_uA_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
475{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100476 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477 struct regulator *regulator;
478 int uA = 0;
479
480 mutex_lock(&rdev->mutex);
481 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100482 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483 mutex_unlock(&rdev->mutex);
484 return sprintf(buf, "%d\n", uA);
485}
David Brownell7ad68e22008-11-11 17:39:02 -0800486static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700488static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
489 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100490{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100491 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100492 return sprintf(buf, "%d\n", rdev->use_count);
493}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700494static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100495
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700496static ssize_t type_show(struct device *dev, struct device_attribute *attr,
497 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498{
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}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700509static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510
511static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100514 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515
Liam Girdwood414c70c2008-04-30 15:59:04 +0100516 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
517}
David Brownell7ad68e22008-11-11 17:39:02 -0800518static DEVICE_ATTR(suspend_mem_microvolts, 0444,
519 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100520
521static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
522 struct device_attribute *attr, char *buf)
523{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100524 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100525
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
527}
David Brownell7ad68e22008-11-11 17:39:02 -0800528static DEVICE_ATTR(suspend_disk_microvolts, 0444,
529 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530
531static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
532 struct device_attribute *attr, char *buf)
533{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100534 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
537}
David Brownell7ad68e22008-11-11 17:39:02 -0800538static DEVICE_ATTR(suspend_standby_microvolts, 0444,
539 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540
Liam Girdwood414c70c2008-04-30 15:59:04 +0100541static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
543{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100544 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545
David Brownell4fca9542008-11-11 17:38:53 -0800546 return regulator_print_opmode(buf,
547 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100548}
David Brownell7ad68e22008-11-11 17:39:02 -0800549static DEVICE_ATTR(suspend_mem_mode, 0444,
550 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551
552static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
553 struct device_attribute *attr, char *buf)
554{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100555 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100556
David Brownell4fca9542008-11-11 17:38:53 -0800557 return regulator_print_opmode(buf,
558 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100559}
David Brownell7ad68e22008-11-11 17:39:02 -0800560static DEVICE_ATTR(suspend_disk_mode, 0444,
561 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562
563static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100566 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100567
David Brownell4fca9542008-11-11 17:38:53 -0800568 return regulator_print_opmode(buf,
569 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100570}
David Brownell7ad68e22008-11-11 17:39:02 -0800571static DEVICE_ATTR(suspend_standby_mode, 0444,
572 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100573
574static ssize_t regulator_suspend_mem_state_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
576{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100577 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100578
David Brownell4fca9542008-11-11 17:38:53 -0800579 return regulator_print_state(buf,
580 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100581}
David Brownell7ad68e22008-11-11 17:39:02 -0800582static DEVICE_ATTR(suspend_mem_state, 0444,
583 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100584
585static ssize_t regulator_suspend_disk_state_show(struct device *dev,
586 struct device_attribute *attr, char *buf)
587{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100588 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100589
David Brownell4fca9542008-11-11 17:38:53 -0800590 return regulator_print_state(buf,
591 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100592}
David Brownell7ad68e22008-11-11 17:39:02 -0800593static DEVICE_ATTR(suspend_disk_state, 0444,
594 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100595
596static ssize_t regulator_suspend_standby_state_show(struct device *dev,
597 struct device_attribute *attr, char *buf)
598{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100599 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100600
David Brownell4fca9542008-11-11 17:38:53 -0800601 return regulator_print_state(buf,
602 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100603}
David Brownell7ad68e22008-11-11 17:39:02 -0800604static DEVICE_ATTR(suspend_standby_state, 0444,
605 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100606
Mark Brownf59c8f92012-08-31 10:36:37 -0700607static ssize_t regulator_bypass_show(struct device *dev,
608 struct device_attribute *attr, char *buf)
609{
610 struct regulator_dev *rdev = dev_get_drvdata(dev);
611 const char *report;
612 bool bypass;
613 int ret;
614
615 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
616
617 if (ret != 0)
618 report = "unknown";
619 else if (bypass)
620 report = "enabled";
621 else
622 report = "disabled";
623
624 return sprintf(buf, "%s\n", report);
625}
626static DEVICE_ATTR(bypass, 0444,
627 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800628
629/*
630 * These are the only attributes are present for all regulators.
631 * Other attributes are a function of regulator functionality.
632 */
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700633static struct attribute *regulator_dev_attrs[] = {
634 &dev_attr_name.attr,
635 &dev_attr_num_users.attr,
636 &dev_attr_type.attr,
637 NULL,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100638};
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700639ATTRIBUTE_GROUPS(regulator_dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100640
641static void regulator_dev_release(struct device *dev)
642{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100643 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100644 kfree(rdev);
645}
646
647static struct class regulator_class = {
648 .name = "regulator",
649 .dev_release = regulator_dev_release,
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700650 .dev_groups = regulator_dev_groups,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100651};
652
653/* Calculate the new optimum regulator operating mode based on the new total
654 * consumer load. All locks held by caller */
655static void drms_uA_update(struct regulator_dev *rdev)
656{
657 struct regulator *sibling;
658 int current_uA = 0, output_uV, input_uV, err;
659 unsigned int mode;
660
661 err = regulator_check_drms(rdev);
662 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000663 (!rdev->desc->ops->get_voltage &&
664 !rdev->desc->ops->get_voltage_sel) ||
665 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300666 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100667
668 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000669 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100670 if (output_uV <= 0)
671 return;
672
673 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000674 input_uV = 0;
675 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800676 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000677 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100678 input_uV = rdev->constraints->input_uV;
679 if (input_uV <= 0)
680 return;
681
682 /* calc total requested load */
683 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100684 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100685
686 /* now get the optimum mode for our new total regulator load */
687 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
688 output_uV, current_uA);
689
690 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900691 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100692 if (err == 0)
693 rdev->desc->ops->set_mode(rdev, mode);
694}
695
696static int suspend_set_state(struct regulator_dev *rdev,
697 struct regulator_state *rstate)
698{
699 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100700
701 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800702 * only warn if the driver implements set_suspend_voltage or
703 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100704 */
705 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800706 if (rdev->desc->ops->set_suspend_voltage ||
707 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800708 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100709 return 0;
710 }
711
712 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800713 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100714 return -EINVAL;
715 }
716
Axel Lin8ac0e952012-04-14 09:14:34 +0800717 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100718 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800719 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100720 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800721 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
722 ret = 0;
723
Liam Girdwood414c70c2008-04-30 15:59:04 +0100724 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800725 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100726 return ret;
727 }
728
729 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
730 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
731 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800732 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100733 return ret;
734 }
735 }
736
737 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
738 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
739 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800740 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100741 return ret;
742 }
743 }
744 return ret;
745}
746
747/* locks held by caller */
748static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
749{
750 if (!rdev->constraints)
751 return -EINVAL;
752
753 switch (state) {
754 case PM_SUSPEND_STANDBY:
755 return suspend_set_state(rdev,
756 &rdev->constraints->state_standby);
757 case PM_SUSPEND_MEM:
758 return suspend_set_state(rdev,
759 &rdev->constraints->state_mem);
760 case PM_SUSPEND_MAX:
761 return suspend_set_state(rdev,
762 &rdev->constraints->state_disk);
763 default:
764 return -EINVAL;
765 }
766}
767
768static void print_constraints(struct regulator_dev *rdev)
769{
770 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000771 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100772 int count = 0;
773 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100774
Mark Brown8f031b42009-10-22 16:31:31 +0100775 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100776 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100777 count += sprintf(buf + count, "%d mV ",
778 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100779 else
Mark Brown8f031b42009-10-22 16:31:31 +0100780 count += sprintf(buf + count, "%d <--> %d mV ",
781 constraints->min_uV / 1000,
782 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100783 }
Mark Brown8f031b42009-10-22 16:31:31 +0100784
785 if (!constraints->min_uV ||
786 constraints->min_uV != constraints->max_uV) {
787 ret = _regulator_get_voltage(rdev);
788 if (ret > 0)
789 count += sprintf(buf + count, "at %d mV ", ret / 1000);
790 }
791
Mark Brownbf5892a2011-05-08 22:13:37 +0100792 if (constraints->uV_offset)
793 count += sprintf(buf, "%dmV offset ",
794 constraints->uV_offset / 1000);
795
Mark Brown8f031b42009-10-22 16:31:31 +0100796 if (constraints->min_uA && constraints->max_uA) {
797 if (constraints->min_uA == constraints->max_uA)
798 count += sprintf(buf + count, "%d mA ",
799 constraints->min_uA / 1000);
800 else
801 count += sprintf(buf + count, "%d <--> %d mA ",
802 constraints->min_uA / 1000,
803 constraints->max_uA / 1000);
804 }
805
806 if (!constraints->min_uA ||
807 constraints->min_uA != constraints->max_uA) {
808 ret = _regulator_get_current_limit(rdev);
809 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400810 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100811 }
812
Liam Girdwood414c70c2008-04-30 15:59:04 +0100813 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
814 count += sprintf(buf + count, "fast ");
815 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
816 count += sprintf(buf + count, "normal ");
817 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
818 count += sprintf(buf + count, "idle ");
819 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
820 count += sprintf(buf + count, "standby");
821
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200822 if (!count)
823 sprintf(buf, "no parameters");
824
Mark Brown13ce29f2010-12-17 16:04:12 +0000825 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000826
827 if ((constraints->min_uV != constraints->max_uV) &&
828 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
829 rdev_warn(rdev,
830 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100831}
832
Mark Browne79055d2009-10-19 15:53:50 +0100833static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100834 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100835{
836 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100837 int ret;
838
839 /* do we need to apply the constraint voltage */
840 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000841 rdev->constraints->min_uV == rdev->constraints->max_uV) {
842 ret = _regulator_do_set_voltage(rdev,
843 rdev->constraints->min_uV,
844 rdev->constraints->max_uV);
845 if (ret < 0) {
846 rdev_err(rdev, "failed to apply %duV constraint\n",
847 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000848 return ret;
849 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100850 }
Mark Browne79055d2009-10-19 15:53:50 +0100851
852 /* constrain machine-level voltage specs to fit
853 * the actual range supported by this regulator.
854 */
855 if (ops->list_voltage && rdev->desc->n_voltages) {
856 int count = rdev->desc->n_voltages;
857 int i;
858 int min_uV = INT_MAX;
859 int max_uV = INT_MIN;
860 int cmin = constraints->min_uV;
861 int cmax = constraints->max_uV;
862
863 /* it's safe to autoconfigure fixed-voltage supplies
864 and the constraints are used by list_voltage. */
865 if (count == 1 && !cmin) {
866 cmin = 1;
867 cmax = INT_MAX;
868 constraints->min_uV = cmin;
869 constraints->max_uV = cmax;
870 }
871
872 /* voltage constraints are optional */
873 if ((cmin == 0) && (cmax == 0))
874 return 0;
875
876 /* else require explicit machine-level constraints */
877 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800878 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100879 return -EINVAL;
880 }
881
882 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
883 for (i = 0; i < count; i++) {
884 int value;
885
886 value = ops->list_voltage(rdev, i);
887 if (value <= 0)
888 continue;
889
890 /* maybe adjust [min_uV..max_uV] */
891 if (value >= cmin && value < min_uV)
892 min_uV = value;
893 if (value <= cmax && value > max_uV)
894 max_uV = value;
895 }
896
897 /* final: [min_uV..max_uV] valid iff constraints valid */
898 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000899 rdev_err(rdev,
900 "unsupportable voltage constraints %u-%uuV\n",
901 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100902 return -EINVAL;
903 }
904
905 /* use regulator's subset of machine constraints */
906 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800907 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
908 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100909 constraints->min_uV = min_uV;
910 }
911 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800912 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
913 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100914 constraints->max_uV = max_uV;
915 }
916 }
917
918 return 0;
919}
920
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530921static int machine_constraints_current(struct regulator_dev *rdev,
922 struct regulation_constraints *constraints)
923{
924 struct regulator_ops *ops = rdev->desc->ops;
925 int ret;
926
927 if (!constraints->min_uA && !constraints->max_uA)
928 return 0;
929
930 if (constraints->min_uA > constraints->max_uA) {
931 rdev_err(rdev, "Invalid current constraints\n");
932 return -EINVAL;
933 }
934
935 if (!ops->set_current_limit || !ops->get_current_limit) {
936 rdev_warn(rdev, "Operation of current configuration missing\n");
937 return 0;
938 }
939
940 /* Set regulator current in constraints range */
941 ret = ops->set_current_limit(rdev, constraints->min_uA,
942 constraints->max_uA);
943 if (ret < 0) {
944 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
945 return ret;
946 }
947
948 return 0;
949}
950
Liam Girdwooda5766f12008-10-10 13:22:20 +0100951/**
952 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000953 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000954 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100955 *
956 * Allows platform initialisation code to define and constrain
957 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
958 * Constraints *must* be set by platform code in order for some
959 * regulator operations to proceed i.e. set_voltage, set_current_limit,
960 * set_mode.
961 */
962static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000963 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100964{
965 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100966 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100967
Mark Brown9a8f5e02011-11-29 18:11:19 +0000968 if (constraints)
969 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
970 GFP_KERNEL);
971 else
972 rdev->constraints = kzalloc(sizeof(*constraints),
973 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000974 if (!rdev->constraints)
975 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100976
Mark Brownf8c12fe2010-11-29 15:55:17 +0000977 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100978 if (ret != 0)
979 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800980
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530981 ret = machine_constraints_current(rdev, rdev->constraints);
982 if (ret != 0)
983 goto out;
984
Liam Girdwooda5766f12008-10-10 13:22:20 +0100985 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000986 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000987 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100988 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800989 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100990 goto out;
991 }
992 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100993
Mark Brown9a8f5e02011-11-29 18:11:19 +0000994 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000995 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800996 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000997 ret = -EINVAL;
998 goto out;
999 }
1000
Mark Brownf8c12fe2010-11-29 15:55:17 +00001001 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001002 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001003 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +00001004 goto out;
1005 }
1006 }
1007
Mark Browncacf90f2009-03-02 16:32:46 +00001008 /* If the constraints say the regulator should be on at this point
1009 * and we have control then make sure it is enabled.
1010 */
Mark Brownf8c12fe2010-11-29 15:55:17 +00001011 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
1012 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +01001013 ret = ops->enable(rdev);
1014 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001015 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +01001016 goto out;
1017 }
1018 }
1019
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301020 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1021 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301022 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1023 if (ret < 0) {
1024 rdev_err(rdev, "failed to set ramp_delay\n");
1025 goto out;
1026 }
1027 }
1028
Liam Girdwooda5766f12008-10-10 13:22:20 +01001029 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001030 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001031out:
Axel Lin1a6958e72011-07-15 10:50:43 +08001032 kfree(rdev->constraints);
1033 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001034 return ret;
1035}
1036
1037/**
1038 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001039 * @rdev: regulator name
1040 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001041 *
1042 * Called by platform initialisation code to set the supply regulator for this
1043 * regulator. This ensures that a regulators supply will also be enabled by the
1044 * core if it's child is enabled.
1045 */
1046static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001047 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001048{
1049 int err;
1050
Mark Brown3801b862011-06-09 16:22:22 +01001051 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1052
1053 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001054 if (rdev->supply == NULL) {
1055 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001056 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001057 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301058 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001059
1060 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001061}
1062
1063/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001064 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001065 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001066 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001067 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001068 *
1069 * Allows platform initialisation code to map physical regulator
1070 * sources to symbolic names for supplies for use by devices. Devices
1071 * should use these symbolic names to request regulators, avoiding the
1072 * need to provide board-specific regulator names as platform data.
1073 */
1074static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001075 const char *consumer_dev_name,
1076 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001077{
1078 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001079 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001080
1081 if (supply == NULL)
1082 return -EINVAL;
1083
Mark Brown9ed20992009-07-21 16:00:26 +01001084 if (consumer_dev_name != NULL)
1085 has_dev = 1;
1086 else
1087 has_dev = 0;
1088
David Brownell6001e132008-12-31 12:54:19 +00001089 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001090 if (node->dev_name && consumer_dev_name) {
1091 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1092 continue;
1093 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001094 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001095 }
1096
David Brownell6001e132008-12-31 12:54:19 +00001097 if (strcmp(node->supply, supply) != 0)
1098 continue;
1099
Mark Brown737f3602012-02-02 00:10:51 +00001100 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1101 consumer_dev_name,
1102 dev_name(&node->regulator->dev),
1103 node->regulator->desc->name,
1104 supply,
1105 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001106 return -EBUSY;
1107 }
1108
Mark Brown9ed20992009-07-21 16:00:26 +01001109 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001110 if (node == NULL)
1111 return -ENOMEM;
1112
1113 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001114 node->supply = supply;
1115
Mark Brown9ed20992009-07-21 16:00:26 +01001116 if (has_dev) {
1117 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1118 if (node->dev_name == NULL) {
1119 kfree(node);
1120 return -ENOMEM;
1121 }
Mark Brown40f92442009-06-17 17:56:39 +01001122 }
1123
Liam Girdwooda5766f12008-10-10 13:22:20 +01001124 list_add(&node->list, &regulator_map_list);
1125 return 0;
1126}
1127
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001128static void unset_regulator_supplies(struct regulator_dev *rdev)
1129{
1130 struct regulator_map *node, *n;
1131
1132 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1133 if (rdev == node->regulator) {
1134 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001135 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001136 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001137 }
1138 }
1139}
1140
Mark Brownf5726ae2011-06-09 16:22:20 +01001141#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001142
1143static struct regulator *create_regulator(struct regulator_dev *rdev,
1144 struct device *dev,
1145 const char *supply_name)
1146{
1147 struct regulator *regulator;
1148 char buf[REG_STR_SIZE];
1149 int err, size;
1150
1151 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1152 if (regulator == NULL)
1153 return NULL;
1154
1155 mutex_lock(&rdev->mutex);
1156 regulator->rdev = rdev;
1157 list_add(&regulator->list, &rdev->consumer_list);
1158
1159 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001160 regulator->dev = dev;
1161
Mark Brown222cc7b2012-06-22 11:39:16 +01001162 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001163 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1164 dev->kobj.name, supply_name);
1165 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001166 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001167
1168 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1169 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001170 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001171
1172 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1173 buf);
1174 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001175 rdev_warn(rdev, "could not add device link %s err %d\n",
1176 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001177 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001178 }
Mark Brown5de70512011-06-19 13:33:16 +01001179 } else {
1180 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1181 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001182 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001183 }
Mark Brown5de70512011-06-19 13:33:16 +01001184
Mark Brown5de70512011-06-19 13:33:16 +01001185 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1186 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001187 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001188 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001189 } else {
1190 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1191 &regulator->uA_load);
1192 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1193 &regulator->min_uV);
1194 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1195 &regulator->max_uV);
1196 }
Mark Brown5de70512011-06-19 13:33:16 +01001197
Mark Brown6492bc12012-04-19 13:19:07 +01001198 /*
1199 * Check now if the regulator is an always on regulator - if
1200 * it is then we don't need to do nearly so much work for
1201 * enable/disable calls.
1202 */
1203 if (!_regulator_can_change_status(rdev) &&
1204 _regulator_is_enabled(rdev))
1205 regulator->always_on = true;
1206
Liam Girdwood414c70c2008-04-30 15:59:04 +01001207 mutex_unlock(&rdev->mutex);
1208 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001209overflow_err:
1210 list_del(&regulator->list);
1211 kfree(regulator);
1212 mutex_unlock(&rdev->mutex);
1213 return NULL;
1214}
1215
Mark Brown31aae2b2009-12-21 12:21:52 +00001216static int _regulator_get_enable_time(struct regulator_dev *rdev)
1217{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301218 if (rdev->constraints && rdev->constraints->enable_time)
1219 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001220 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001221 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001222 return rdev->desc->ops->enable_time(rdev);
1223}
1224
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001225static struct regulator_supply_alias *regulator_find_supply_alias(
1226 struct device *dev, const char *supply)
1227{
1228 struct regulator_supply_alias *map;
1229
1230 list_for_each_entry(map, &regulator_supply_alias_list, list)
1231 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1232 return map;
1233
1234 return NULL;
1235}
1236
1237static void regulator_supply_alias(struct device **dev, const char **supply)
1238{
1239 struct regulator_supply_alias *map;
1240
1241 map = regulator_find_supply_alias(*dev, *supply);
1242 if (map) {
1243 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1244 *supply, map->alias_supply,
1245 dev_name(map->alias_dev));
1246 *dev = map->alias_dev;
1247 *supply = map->alias_supply;
1248 }
1249}
1250
Rajendra Nayak69511a42011-11-18 16:47:20 +05301251static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001252 const char *supply,
1253 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301254{
1255 struct regulator_dev *r;
1256 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001257 struct regulator_map *map;
1258 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301259
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001260 regulator_supply_alias(&dev, &supply);
1261
Rajendra Nayak69511a42011-11-18 16:47:20 +05301262 /* first do a dt based lookup */
1263 if (dev && dev->of_node) {
1264 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001265 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301266 list_for_each_entry(r, &regulator_list, list)
1267 if (r->dev.parent &&
1268 node == r->dev.of_node)
1269 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001270 } else {
1271 /*
1272 * If we couldn't even get the node then it's
1273 * not just that the device didn't register
1274 * yet, there's no node and we'll never
1275 * succeed.
1276 */
1277 *ret = -ENODEV;
1278 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301279 }
1280
1281 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001282 if (dev)
1283 devname = dev_name(dev);
1284
Rajendra Nayak69511a42011-11-18 16:47:20 +05301285 list_for_each_entry(r, &regulator_list, list)
1286 if (strcmp(rdev_get_name(r), supply) == 0)
1287 return r;
1288
Mark Brown576ca4362012-03-30 12:24:37 +01001289 list_for_each_entry(map, &regulator_map_list, list) {
1290 /* If the mapping has a device set up it must match */
1291 if (map->dev_name &&
1292 (!devname || strcmp(map->dev_name, devname)))
1293 continue;
1294
1295 if (strcmp(map->supply, supply) == 0)
1296 return map->regulator;
1297 }
1298
1299
Rajendra Nayak69511a42011-11-18 16:47:20 +05301300 return NULL;
1301}
1302
Mark Brown5ffbd132009-07-21 16:00:23 +01001303/* Internal regulator request function */
1304static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown4ddfebd2013-09-13 19:50:37 +01001305 bool exclusive, bool allow_dummy)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001306{
1307 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001308 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001309 const char *devname = NULL;
Mark Brown9b92da12013-09-20 12:23:30 +01001310 int ret = -EPROBE_DEFER;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001311
1312 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001313 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001314 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001315 }
1316
Mark Brown40f92442009-06-17 17:56:39 +01001317 if (dev)
1318 devname = dev_name(dev);
1319
Liam Girdwood414c70c2008-04-30 15:59:04 +01001320 mutex_lock(&regulator_list_mutex);
1321
Mark Brown6d191a52012-03-29 14:19:02 +01001322 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301323 if (rdev)
1324 goto found;
1325
Mark Brownef60abb2013-09-23 16:12:52 +01001326 regulator = ERR_PTR(ret);
1327
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001328 /*
1329 * If we have return value from dev_lookup fail, we do not expect to
1330 * succeed, so, quit with appropriate error value
1331 */
Mark Brown4ddfebd2013-09-13 19:50:37 +01001332 if (ret && ret != -ENODEV) {
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001333 goto out;
1334 }
1335
Mark Brown34abbd62010-02-12 10:18:08 +00001336 if (!devname)
1337 devname = "deviceless";
1338
Mark Brown4ddfebd2013-09-13 19:50:37 +01001339 /*
1340 * Assume that a regulator is physically present and enabled
1341 * even if it isn't hooked up and just provide a dummy.
Mark Brown34abbd62010-02-12 10:18:08 +00001342 */
Mark Brown4ddfebd2013-09-13 19:50:37 +01001343 if (has_full_constraints && allow_dummy) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001344 pr_warn("%s supply %s not found, using dummy regulator\n",
1345 devname, id);
Mark Brown4ddfebd2013-09-13 19:50:37 +01001346
Mark Brown34abbd62010-02-12 10:18:08 +00001347 rdev = dummy_regulator_rdev;
1348 goto found;
Mark Brown4ddfebd2013-09-13 19:50:37 +01001349 } else {
1350 dev_err(dev, "dummy supplies not allowed\n");
Mark Brown34abbd62010-02-12 10:18:08 +00001351 }
Mark Brown34abbd62010-02-12 10:18:08 +00001352
Liam Girdwood414c70c2008-04-30 15:59:04 +01001353 mutex_unlock(&regulator_list_mutex);
1354 return regulator;
1355
1356found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001357 if (rdev->exclusive) {
1358 regulator = ERR_PTR(-EPERM);
1359 goto out;
1360 }
1361
1362 if (exclusive && rdev->open_count) {
1363 regulator = ERR_PTR(-EBUSY);
1364 goto out;
1365 }
1366
Liam Girdwooda5766f12008-10-10 13:22:20 +01001367 if (!try_module_get(rdev->owner))
1368 goto out;
1369
Liam Girdwood414c70c2008-04-30 15:59:04 +01001370 regulator = create_regulator(rdev, dev, id);
1371 if (regulator == NULL) {
1372 regulator = ERR_PTR(-ENOMEM);
1373 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001374 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001375 }
1376
Mark Brown5ffbd132009-07-21 16:00:23 +01001377 rdev->open_count++;
1378 if (exclusive) {
1379 rdev->exclusive = 1;
1380
1381 ret = _regulator_is_enabled(rdev);
1382 if (ret > 0)
1383 rdev->use_count = 1;
1384 else
1385 rdev->use_count = 0;
1386 }
1387
Liam Girdwooda5766f12008-10-10 13:22:20 +01001388out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001389 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001390
Liam Girdwood414c70c2008-04-30 15:59:04 +01001391 return regulator;
1392}
Mark Brown5ffbd132009-07-21 16:00:23 +01001393
1394/**
1395 * regulator_get - lookup and obtain a reference to a regulator.
1396 * @dev: device for regulator "consumer"
1397 * @id: Supply name or regulator ID.
1398 *
1399 * Returns a struct regulator corresponding to the regulator producer,
1400 * or IS_ERR() condition containing errno.
1401 *
1402 * Use of supply names configured via regulator_set_device_supply() is
1403 * strongly encouraged. It is recommended that the supply name used
1404 * should match the name used for the supply and/or the relevant
1405 * device pins in the datasheet.
1406 */
1407struct regulator *regulator_get(struct device *dev, const char *id)
1408{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001409 return _regulator_get(dev, id, false, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001410}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001411EXPORT_SYMBOL_GPL(regulator_get);
1412
Stephen Boyd070b9072012-01-16 19:39:58 -08001413/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001414 * regulator_get_exclusive - obtain exclusive access to a regulator.
1415 * @dev: device for regulator "consumer"
1416 * @id: Supply name or regulator ID.
1417 *
1418 * Returns a struct regulator corresponding to the regulator producer,
1419 * or IS_ERR() condition containing errno. Other consumers will be
1420 * unable to obtain this reference is held and the use count for the
1421 * regulator will be initialised to reflect the current state of the
1422 * regulator.
1423 *
1424 * This is intended for use by consumers which cannot tolerate shared
1425 * use of the regulator such as those which need to force the
1426 * regulator off for correct operation of the hardware they are
1427 * controlling.
1428 *
1429 * Use of supply names configured via regulator_set_device_supply() is
1430 * strongly encouraged. It is recommended that the supply name used
1431 * should match the name used for the supply and/or the relevant
1432 * device pins in the datasheet.
1433 */
1434struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1435{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001436 return _regulator_get(dev, id, true, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001437}
1438EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1439
Mark Brownde1dd9f2013-07-29 21:42:42 +01001440/**
1441 * regulator_get_optional - obtain optional access to a regulator.
1442 * @dev: device for regulator "consumer"
1443 * @id: Supply name or regulator ID.
1444 *
1445 * Returns a struct regulator corresponding to the regulator producer,
1446 * or IS_ERR() condition containing errno. Other consumers will be
1447 * unable to obtain this reference is held and the use count for the
1448 * regulator will be initialised to reflect the current state of the
1449 * regulator.
1450 *
1451 * This is intended for use by consumers for devices which can have
1452 * some supplies unconnected in normal use, such as some MMC devices.
1453 * It can allow the regulator core to provide stub supplies for other
1454 * supplies requested using normal regulator_get() calls without
1455 * disrupting the operation of drivers that can handle absent
1456 * supplies.
1457 *
1458 * Use of supply names configured via regulator_set_device_supply() is
1459 * strongly encouraged. It is recommended that the supply name used
1460 * should match the name used for the supply and/or the relevant
1461 * device pins in the datasheet.
1462 */
1463struct regulator *regulator_get_optional(struct device *dev, const char *id)
1464{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001465 return _regulator_get(dev, id, false, false);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001466}
1467EXPORT_SYMBOL_GPL(regulator_get_optional);
1468
Charles Keepax23ff2f02012-11-14 09:39:31 +00001469/* Locks held by regulator_put() */
1470static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001471{
1472 struct regulator_dev *rdev;
1473
1474 if (regulator == NULL || IS_ERR(regulator))
1475 return;
1476
Liam Girdwood414c70c2008-04-30 15:59:04 +01001477 rdev = regulator->rdev;
1478
Mark Brown5de70512011-06-19 13:33:16 +01001479 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001480
Liam Girdwood414c70c2008-04-30 15:59:04 +01001481 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001482 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001483 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Mark Brown5de70512011-06-19 13:33:16 +01001484 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001485 list_del(&regulator->list);
1486 kfree(regulator);
1487
Mark Brown5ffbd132009-07-21 16:00:23 +01001488 rdev->open_count--;
1489 rdev->exclusive = 0;
1490
Liam Girdwood414c70c2008-04-30 15:59:04 +01001491 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001492}
1493
1494/**
1495 * regulator_put - "free" the regulator source
1496 * @regulator: regulator source
1497 *
1498 * Note: drivers must ensure that all regulator_enable calls made on this
1499 * regulator source are balanced by regulator_disable calls prior to calling
1500 * this function.
1501 */
1502void regulator_put(struct regulator *regulator)
1503{
1504 mutex_lock(&regulator_list_mutex);
1505 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001506 mutex_unlock(&regulator_list_mutex);
1507}
1508EXPORT_SYMBOL_GPL(regulator_put);
1509
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001510/**
1511 * regulator_register_supply_alias - Provide device alias for supply lookup
1512 *
1513 * @dev: device that will be given as the regulator "consumer"
1514 * @id: Supply name or regulator ID
1515 * @alias_dev: device that should be used to lookup the supply
1516 * @alias_id: Supply name or regulator ID that should be used to lookup the
1517 * supply
1518 *
1519 * All lookups for id on dev will instead be conducted for alias_id on
1520 * alias_dev.
1521 */
1522int regulator_register_supply_alias(struct device *dev, const char *id,
1523 struct device *alias_dev,
1524 const char *alias_id)
1525{
1526 struct regulator_supply_alias *map;
1527
1528 map = regulator_find_supply_alias(dev, id);
1529 if (map)
1530 return -EEXIST;
1531
1532 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1533 if (!map)
1534 return -ENOMEM;
1535
1536 map->src_dev = dev;
1537 map->src_supply = id;
1538 map->alias_dev = alias_dev;
1539 map->alias_supply = alias_id;
1540
1541 list_add(&map->list, &regulator_supply_alias_list);
1542
1543 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1544 id, dev_name(dev), alias_id, dev_name(alias_dev));
1545
1546 return 0;
1547}
1548EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1549
1550/**
1551 * regulator_unregister_supply_alias - Remove device alias
1552 *
1553 * @dev: device that will be given as the regulator "consumer"
1554 * @id: Supply name or regulator ID
1555 *
1556 * Remove a lookup alias if one exists for id on dev.
1557 */
1558void regulator_unregister_supply_alias(struct device *dev, const char *id)
1559{
1560 struct regulator_supply_alias *map;
1561
1562 map = regulator_find_supply_alias(dev, id);
1563 if (map) {
1564 list_del(&map->list);
1565 kfree(map);
1566 }
1567}
1568EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1569
1570/**
1571 * regulator_bulk_register_supply_alias - register multiple aliases
1572 *
1573 * @dev: device that will be given as the regulator "consumer"
1574 * @id: List of supply names or regulator IDs
1575 * @alias_dev: device that should be used to lookup the supply
1576 * @alias_id: List of supply names or regulator IDs that should be used to
1577 * lookup the supply
1578 * @num_id: Number of aliases to register
1579 *
1580 * @return 0 on success, an errno on failure.
1581 *
1582 * This helper function allows drivers to register several supply
1583 * aliases in one operation. If any of the aliases cannot be
1584 * registered any aliases that were registered will be removed
1585 * before returning to the caller.
1586 */
1587int regulator_bulk_register_supply_alias(struct device *dev, const char **id,
1588 struct device *alias_dev,
1589 const char **alias_id,
1590 int num_id)
1591{
1592 int i;
1593 int ret;
1594
1595 for (i = 0; i < num_id; ++i) {
1596 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1597 alias_id[i]);
1598 if (ret < 0)
1599 goto err;
1600 }
1601
1602 return 0;
1603
1604err:
1605 dev_err(dev,
1606 "Failed to create supply alias %s,%s -> %s,%s\n",
1607 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1608
1609 while (--i >= 0)
1610 regulator_unregister_supply_alias(dev, id[i]);
1611
1612 return ret;
1613}
1614EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1615
1616/**
1617 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1618 *
1619 * @dev: device that will be given as the regulator "consumer"
1620 * @id: List of supply names or regulator IDs
1621 * @num_id: Number of aliases to unregister
1622 *
1623 * This helper function allows drivers to unregister several supply
1624 * aliases in one operation.
1625 */
1626void regulator_bulk_unregister_supply_alias(struct device *dev,
1627 const char **id,
1628 int num_id)
1629{
1630 int i;
1631
1632 for (i = 0; i < num_id; ++i)
1633 regulator_unregister_supply_alias(dev, id[i]);
1634}
1635EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1636
1637
Kim, Milof19b00d2013-02-18 06:50:39 +00001638/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1639static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1640 const struct regulator_config *config)
1641{
1642 struct regulator_enable_gpio *pin;
1643 int ret;
1644
1645 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1646 if (pin->gpio == config->ena_gpio) {
1647 rdev_dbg(rdev, "GPIO %d is already used\n",
1648 config->ena_gpio);
1649 goto update_ena_gpio_to_rdev;
1650 }
1651 }
1652
1653 ret = gpio_request_one(config->ena_gpio,
1654 GPIOF_DIR_OUT | config->ena_gpio_flags,
1655 rdev_get_name(rdev));
1656 if (ret)
1657 return ret;
1658
1659 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1660 if (pin == NULL) {
1661 gpio_free(config->ena_gpio);
1662 return -ENOMEM;
1663 }
1664
1665 pin->gpio = config->ena_gpio;
1666 pin->ena_gpio_invert = config->ena_gpio_invert;
1667 list_add(&pin->list, &regulator_ena_gpio_list);
1668
1669update_ena_gpio_to_rdev:
1670 pin->request_count++;
1671 rdev->ena_pin = pin;
1672 return 0;
1673}
1674
1675static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1676{
1677 struct regulator_enable_gpio *pin, *n;
1678
1679 if (!rdev->ena_pin)
1680 return;
1681
1682 /* Free the GPIO only in case of no use */
1683 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1684 if (pin->gpio == rdev->ena_pin->gpio) {
1685 if (pin->request_count <= 1) {
1686 pin->request_count = 0;
1687 gpio_free(pin->gpio);
1688 list_del(&pin->list);
1689 kfree(pin);
1690 } else {
1691 pin->request_count--;
1692 }
1693 }
1694 }
1695}
1696
Kim, Milo967cfb12013-02-18 06:50:48 +00001697/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001698 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1699 * @rdev: regulator_dev structure
1700 * @enable: enable GPIO at initial use?
1701 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001702 * GPIO is enabled in case of initial use. (enable_count is 0)
1703 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1704 */
1705static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1706{
1707 struct regulator_enable_gpio *pin = rdev->ena_pin;
1708
1709 if (!pin)
1710 return -EINVAL;
1711
1712 if (enable) {
1713 /* Enable GPIO at initial use */
1714 if (pin->enable_count == 0)
1715 gpio_set_value_cansleep(pin->gpio,
1716 !pin->ena_gpio_invert);
1717
1718 pin->enable_count++;
1719 } else {
1720 if (pin->enable_count > 1) {
1721 pin->enable_count--;
1722 return 0;
1723 }
1724
1725 /* Disable GPIO if not used */
1726 if (pin->enable_count <= 1) {
1727 gpio_set_value_cansleep(pin->gpio,
1728 pin->ena_gpio_invert);
1729 pin->enable_count = 0;
1730 }
1731 }
1732
1733 return 0;
1734}
1735
Mark Brown5c5659d2012-06-27 13:21:26 +01001736static int _regulator_do_enable(struct regulator_dev *rdev)
1737{
1738 int ret, delay;
1739
1740 /* Query before enabling in case configuration dependent. */
1741 ret = _regulator_get_enable_time(rdev);
1742 if (ret >= 0) {
1743 delay = ret;
1744 } else {
1745 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1746 delay = 0;
1747 }
1748
1749 trace_regulator_enable(rdev_get_name(rdev));
1750
Kim, Milo967cfb12013-02-18 06:50:48 +00001751 if (rdev->ena_pin) {
1752 ret = regulator_ena_gpio_ctrl(rdev, true);
1753 if (ret < 0)
1754 return ret;
Mark Brown65f73502012-06-27 14:14:38 +01001755 rdev->ena_gpio_state = 1;
1756 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001757 ret = rdev->desc->ops->enable(rdev);
1758 if (ret < 0)
1759 return ret;
1760 } else {
1761 return -EINVAL;
1762 }
1763
1764 /* Allow the regulator to ramp; it would be useful to extend
1765 * this for bulk operations so that the regulators can ramp
1766 * together. */
1767 trace_regulator_enable_delay(rdev_get_name(rdev));
1768
Thierry Reding5df529d2013-09-20 13:51:56 +02001769 /*
1770 * Delay for the requested amount of time as per the guidelines in:
1771 *
1772 * Documentation/timers/timers-howto.txt
1773 *
1774 * The assumption here is that regulators will never be enabled in
1775 * atomic context and therefore sleeping functions can be used.
1776 */
1777 if (delay) {
1778 unsigned int ms = delay / 1000;
1779 unsigned int us = delay % 1000;
1780
1781 if (ms > 0) {
1782 /*
1783 * For small enough values, handle super-millisecond
1784 * delays in the usleep_range() call below.
1785 */
1786 if (ms < 20)
1787 us += ms * 1000;
1788 else
1789 msleep(ms);
1790 }
1791
1792 /*
1793 * Give the scheduler some room to coalesce with any other
1794 * wakeup sources. For delays shorter than 10 us, don't even
1795 * bother setting up high-resolution timers and just busy-
1796 * loop.
1797 */
1798 if (us >= 10)
1799 usleep_range(us, us + 100);
1800 else
1801 udelay(us);
Mark Brown5c5659d2012-06-27 13:21:26 +01001802 }
1803
1804 trace_regulator_enable_complete(rdev_get_name(rdev));
1805
1806 return 0;
1807}
1808
Liam Girdwood414c70c2008-04-30 15:59:04 +01001809/* locks held by regulator_enable() */
1810static int _regulator_enable(struct regulator_dev *rdev)
1811{
Mark Brown5c5659d2012-06-27 13:21:26 +01001812 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001813
Liam Girdwood414c70c2008-04-30 15:59:04 +01001814 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001815 if (rdev->constraints &&
1816 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1817 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001818
Mark Brown9a2372f2009-08-03 18:49:57 +01001819 if (rdev->use_count == 0) {
1820 /* The regulator may on if it's not switchable or left on */
1821 ret = _regulator_is_enabled(rdev);
1822 if (ret == -EINVAL || ret == 0) {
1823 if (!_regulator_can_change_status(rdev))
1824 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001825
Mark Brown5c5659d2012-06-27 13:21:26 +01001826 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001827 if (ret < 0)
1828 return ret;
1829
Linus Walleija7433cf2009-08-26 12:54:04 +02001830 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001831 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001832 return ret;
1833 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001834 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001835 }
1836
Mark Brown9a2372f2009-08-03 18:49:57 +01001837 rdev->use_count++;
1838
1839 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001840}
1841
1842/**
1843 * regulator_enable - enable regulator output
1844 * @regulator: regulator source
1845 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001846 * Request that the regulator be enabled with the regulator output at
1847 * the predefined voltage or current value. Calls to regulator_enable()
1848 * must be balanced with calls to regulator_disable().
1849 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001850 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001851 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001852 */
1853int regulator_enable(struct regulator *regulator)
1854{
David Brownell412aec62008-11-16 11:44:46 -08001855 struct regulator_dev *rdev = regulator->rdev;
1856 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001857
Mark Brown6492bc12012-04-19 13:19:07 +01001858 if (regulator->always_on)
1859 return 0;
1860
Mark Brown3801b862011-06-09 16:22:22 +01001861 if (rdev->supply) {
1862 ret = regulator_enable(rdev->supply);
1863 if (ret != 0)
1864 return ret;
1865 }
1866
David Brownell412aec62008-11-16 11:44:46 -08001867 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001868 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001869 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001870
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001871 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001872 regulator_disable(rdev->supply);
1873
Liam Girdwood414c70c2008-04-30 15:59:04 +01001874 return ret;
1875}
1876EXPORT_SYMBOL_GPL(regulator_enable);
1877
Mark Brown5c5659d2012-06-27 13:21:26 +01001878static int _regulator_do_disable(struct regulator_dev *rdev)
1879{
1880 int ret;
1881
1882 trace_regulator_disable(rdev_get_name(rdev));
1883
Kim, Milo967cfb12013-02-18 06:50:48 +00001884 if (rdev->ena_pin) {
1885 ret = regulator_ena_gpio_ctrl(rdev, false);
1886 if (ret < 0)
1887 return ret;
Mark Brown5c5659d2012-06-27 13:21:26 +01001888 rdev->ena_gpio_state = 0;
1889
1890 } else if (rdev->desc->ops->disable) {
1891 ret = rdev->desc->ops->disable(rdev);
1892 if (ret != 0)
1893 return ret;
1894 }
1895
1896 trace_regulator_disable_complete(rdev_get_name(rdev));
1897
1898 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1899 NULL);
1900 return 0;
1901}
1902
Liam Girdwood414c70c2008-04-30 15:59:04 +01001903/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001904static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001905{
1906 int ret = 0;
1907
David Brownellcd94b502009-03-11 16:43:34 -08001908 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001909 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001910 return -EIO;
1911
Liam Girdwood414c70c2008-04-30 15:59:04 +01001912 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001913 if (rdev->use_count == 1 &&
1914 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001915
1916 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01001917 if (_regulator_can_change_status(rdev)) {
1918 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001919 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001920 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001921 return ret;
1922 }
1923 }
1924
Liam Girdwood414c70c2008-04-30 15:59:04 +01001925 rdev->use_count = 0;
1926 } else if (rdev->use_count > 1) {
1927
1928 if (rdev->constraints &&
1929 (rdev->constraints->valid_ops_mask &
1930 REGULATOR_CHANGE_DRMS))
1931 drms_uA_update(rdev);
1932
1933 rdev->use_count--;
1934 }
Mark Brown3801b862011-06-09 16:22:22 +01001935
Liam Girdwood414c70c2008-04-30 15:59:04 +01001936 return ret;
1937}
1938
1939/**
1940 * regulator_disable - disable regulator output
1941 * @regulator: regulator source
1942 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001943 * Disable the regulator output voltage or current. Calls to
1944 * regulator_enable() must be balanced with calls to
1945 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001946 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001947 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001948 * devices have it enabled, the regulator device supports disabling and
1949 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001950 */
1951int regulator_disable(struct regulator *regulator)
1952{
David Brownell412aec62008-11-16 11:44:46 -08001953 struct regulator_dev *rdev = regulator->rdev;
1954 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001955
Mark Brown6492bc12012-04-19 13:19:07 +01001956 if (regulator->always_on)
1957 return 0;
1958
David Brownell412aec62008-11-16 11:44:46 -08001959 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001960 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001961 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001962
Mark Brown3801b862011-06-09 16:22:22 +01001963 if (ret == 0 && rdev->supply)
1964 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001965
Liam Girdwood414c70c2008-04-30 15:59:04 +01001966 return ret;
1967}
1968EXPORT_SYMBOL_GPL(regulator_disable);
1969
1970/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001971static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001972{
1973 int ret = 0;
1974
1975 /* force disable */
1976 if (rdev->desc->ops->disable) {
1977 /* ah well, who wants to live forever... */
1978 ret = rdev->desc->ops->disable(rdev);
1979 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001980 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001981 return ret;
1982 }
1983 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001984 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1985 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001986 }
1987
Liam Girdwood414c70c2008-04-30 15:59:04 +01001988 return ret;
1989}
1990
1991/**
1992 * regulator_force_disable - force disable regulator output
1993 * @regulator: regulator source
1994 *
1995 * Forcibly disable the regulator output voltage or current.
1996 * NOTE: this *will* disable the regulator output even if other consumer
1997 * devices have it enabled. This should be used for situations when device
1998 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1999 */
2000int regulator_force_disable(struct regulator *regulator)
2001{
Mark Brown82d15832011-05-09 11:41:02 +02002002 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002003 int ret;
2004
Mark Brown82d15832011-05-09 11:41:02 +02002005 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002006 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002007 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002008 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002009
Mark Brown3801b862011-06-09 16:22:22 +01002010 if (rdev->supply)
2011 while (rdev->open_count--)
2012 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002013
Liam Girdwood414c70c2008-04-30 15:59:04 +01002014 return ret;
2015}
2016EXPORT_SYMBOL_GPL(regulator_force_disable);
2017
Mark Brownda07ecd2011-09-11 09:53:50 +01002018static void regulator_disable_work(struct work_struct *work)
2019{
2020 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2021 disable_work.work);
2022 int count, i, ret;
2023
2024 mutex_lock(&rdev->mutex);
2025
2026 BUG_ON(!rdev->deferred_disables);
2027
2028 count = rdev->deferred_disables;
2029 rdev->deferred_disables = 0;
2030
2031 for (i = 0; i < count; i++) {
2032 ret = _regulator_disable(rdev);
2033 if (ret != 0)
2034 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2035 }
2036
2037 mutex_unlock(&rdev->mutex);
2038
2039 if (rdev->supply) {
2040 for (i = 0; i < count; i++) {
2041 ret = regulator_disable(rdev->supply);
2042 if (ret != 0) {
2043 rdev_err(rdev,
2044 "Supply disable failed: %d\n", ret);
2045 }
2046 }
2047 }
2048}
2049
2050/**
2051 * regulator_disable_deferred - disable regulator output with delay
2052 * @regulator: regulator source
2053 * @ms: miliseconds until the regulator is disabled
2054 *
2055 * Execute regulator_disable() on the regulator after a delay. This
2056 * is intended for use with devices that require some time to quiesce.
2057 *
2058 * NOTE: this will only disable the regulator output if no other consumer
2059 * devices have it enabled, the regulator device supports disabling and
2060 * machine constraints permit this operation.
2061 */
2062int regulator_disable_deferred(struct regulator *regulator, int ms)
2063{
2064 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01002065 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01002066
Mark Brown6492bc12012-04-19 13:19:07 +01002067 if (regulator->always_on)
2068 return 0;
2069
Mark Brown2b5a24a2012-09-07 11:00:53 +08002070 if (!ms)
2071 return regulator_disable(regulator);
2072
Mark Brownda07ecd2011-09-11 09:53:50 +01002073 mutex_lock(&rdev->mutex);
2074 rdev->deferred_disables++;
2075 mutex_unlock(&rdev->mutex);
2076
Mark Brown070260f2013-07-18 11:52:04 +01002077 ret = queue_delayed_work(system_power_efficient_wq,
2078 &rdev->disable_work,
2079 msecs_to_jiffies(ms));
Mark Brownaa598022011-10-03 22:42:43 +01002080 if (ret < 0)
2081 return ret;
2082 else
2083 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002084}
2085EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2086
Liam Girdwood414c70c2008-04-30 15:59:04 +01002087static int _regulator_is_enabled(struct regulator_dev *rdev)
2088{
Mark Brown65f73502012-06-27 14:14:38 +01002089 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002090 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002091 return rdev->ena_gpio_state;
2092
Mark Brown9a7f6a42010-02-11 17:22:45 +00002093 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002094 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002095 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002096
Mark Brown93325462009-08-03 18:49:56 +01002097 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002098}
2099
2100/**
2101 * regulator_is_enabled - is the regulator output enabled
2102 * @regulator: regulator source
2103 *
David Brownell412aec62008-11-16 11:44:46 -08002104 * Returns positive if the regulator driver backing the source/client
2105 * has requested that the device be enabled, zero if it hasn't, else a
2106 * negative errno code.
2107 *
2108 * Note that the device backing this regulator handle can have multiple
2109 * users, so it might be enabled even if regulator_enable() was never
2110 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002111 */
2112int regulator_is_enabled(struct regulator *regulator)
2113{
Mark Brown93325462009-08-03 18:49:56 +01002114 int ret;
2115
Mark Brown6492bc12012-04-19 13:19:07 +01002116 if (regulator->always_on)
2117 return 1;
2118
Mark Brown93325462009-08-03 18:49:56 +01002119 mutex_lock(&regulator->rdev->mutex);
2120 ret = _regulator_is_enabled(regulator->rdev);
2121 mutex_unlock(&regulator->rdev->mutex);
2122
2123 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002124}
2125EXPORT_SYMBOL_GPL(regulator_is_enabled);
2126
2127/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002128 * regulator_can_change_voltage - check if regulator can change voltage
2129 * @regulator: regulator source
2130 *
2131 * Returns positive if the regulator driver backing the source/client
2132 * can change its voltage, false otherwise. Usefull for detecting fixed
2133 * or dummy regulators and disabling voltage change logic in the client
2134 * driver.
2135 */
2136int regulator_can_change_voltage(struct regulator *regulator)
2137{
2138 struct regulator_dev *rdev = regulator->rdev;
2139
2140 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002141 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2142 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2143 return 1;
2144
2145 if (rdev->desc->continuous_voltage_range &&
2146 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2147 rdev->constraints->min_uV != rdev->constraints->max_uV)
2148 return 1;
2149 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002150
2151 return 0;
2152}
2153EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2154
2155/**
David Brownell4367cfd2009-02-26 11:48:36 -08002156 * regulator_count_voltages - count regulator_list_voltage() selectors
2157 * @regulator: regulator source
2158 *
2159 * Returns number of selectors, or negative errno. Selectors are
2160 * numbered starting at zero, and typically correspond to bitfields
2161 * in hardware registers.
2162 */
2163int regulator_count_voltages(struct regulator *regulator)
2164{
2165 struct regulator_dev *rdev = regulator->rdev;
2166
2167 return rdev->desc->n_voltages ? : -EINVAL;
2168}
2169EXPORT_SYMBOL_GPL(regulator_count_voltages);
2170
2171/**
2172 * regulator_list_voltage - enumerate supported voltages
2173 * @regulator: regulator source
2174 * @selector: identify voltage to list
2175 * Context: can sleep
2176 *
2177 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002178 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002179 * negative errno.
2180 */
2181int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2182{
2183 struct regulator_dev *rdev = regulator->rdev;
2184 struct regulator_ops *ops = rdev->desc->ops;
2185 int ret;
2186
2187 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2188 return -EINVAL;
2189
2190 mutex_lock(&rdev->mutex);
2191 ret = ops->list_voltage(rdev, selector);
2192 mutex_unlock(&rdev->mutex);
2193
2194 if (ret > 0) {
2195 if (ret < rdev->constraints->min_uV)
2196 ret = 0;
2197 else if (ret > rdev->constraints->max_uV)
2198 ret = 0;
2199 }
2200
2201 return ret;
2202}
2203EXPORT_SYMBOL_GPL(regulator_list_voltage);
2204
2205/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002206 * regulator_get_linear_step - return the voltage step size between VSEL values
2207 * @regulator: regulator source
2208 *
2209 * Returns the voltage step size between VSEL values for linear
2210 * regulators, or return 0 if the regulator isn't a linear regulator.
2211 */
2212unsigned int regulator_get_linear_step(struct regulator *regulator)
2213{
2214 struct regulator_dev *rdev = regulator->rdev;
2215
2216 return rdev->desc->uV_step;
2217}
2218EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2219
2220/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002221 * regulator_is_supported_voltage - check if a voltage range can be supported
2222 *
2223 * @regulator: Regulator to check.
2224 * @min_uV: Minimum required voltage in uV.
2225 * @max_uV: Maximum required voltage in uV.
2226 *
2227 * Returns a boolean or a negative error code.
2228 */
2229int regulator_is_supported_voltage(struct regulator *regulator,
2230 int min_uV, int max_uV)
2231{
Mark Brownc5f39392012-07-02 15:00:18 +01002232 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002233 int i, voltages, ret;
2234
Mark Brownc5f39392012-07-02 15:00:18 +01002235 /* If we can't change voltage check the current voltage */
2236 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2237 ret = regulator_get_voltage(regulator);
2238 if (ret >= 0)
Marek Szyprowskif0f98b12012-11-13 09:48:51 +01002239 return (min_uV <= ret && ret <= max_uV);
Mark Brownc5f39392012-07-02 15:00:18 +01002240 else
2241 return ret;
2242 }
2243
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002244 /* Any voltage within constrains range is fine? */
2245 if (rdev->desc->continuous_voltage_range)
2246 return min_uV >= rdev->constraints->min_uV &&
2247 max_uV <= rdev->constraints->max_uV;
2248
Mark Browna7a1ad92009-07-21 16:00:24 +01002249 ret = regulator_count_voltages(regulator);
2250 if (ret < 0)
2251 return ret;
2252 voltages = ret;
2253
2254 for (i = 0; i < voltages; i++) {
2255 ret = regulator_list_voltage(regulator, i);
2256
2257 if (ret >= min_uV && ret <= max_uV)
2258 return 1;
2259 }
2260
2261 return 0;
2262}
Mark Browna398eaa2011-12-28 12:48:45 +00002263EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002264
Mark Brown75790252010-12-12 14:25:50 +00002265static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2266 int min_uV, int max_uV)
2267{
2268 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002269 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002270 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002271 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002272 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002273
2274 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2275
Mark Brownbf5892a2011-05-08 22:13:37 +01002276 min_uV += rdev->constraints->uV_offset;
2277 max_uV += rdev->constraints->uV_offset;
2278
Axel Lineba41a52012-04-04 10:32:10 +08002279 /*
2280 * If we can't obtain the old selector there is not enough
2281 * info to call set_voltage_time_sel().
2282 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002283 if (_regulator_is_enabled(rdev) &&
2284 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002285 rdev->desc->ops->get_voltage_sel) {
2286 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2287 if (old_selector < 0)
2288 return old_selector;
2289 }
2290
Mark Brown75790252010-12-12 14:25:50 +00002291 if (rdev->desc->ops->set_voltage) {
2292 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2293 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002294
2295 if (ret >= 0) {
2296 if (rdev->desc->ops->list_voltage)
2297 best_val = rdev->desc->ops->list_voltage(rdev,
2298 selector);
2299 else
2300 best_val = _regulator_get_voltage(rdev);
2301 }
2302
Mark Browne8eef822010-12-12 14:36:17 +00002303 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002304 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002305 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2306 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002307 } else {
2308 if (rdev->desc->ops->list_voltage ==
2309 regulator_list_voltage_linear)
2310 ret = regulator_map_voltage_linear(rdev,
2311 min_uV, max_uV);
2312 else
2313 ret = regulator_map_voltage_iterate(rdev,
2314 min_uV, max_uV);
2315 }
Mark Browne8eef822010-12-12 14:36:17 +00002316
Mark Browne843fc42012-05-09 21:16:06 +01002317 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002318 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2319 if (min_uV <= best_val && max_uV >= best_val) {
2320 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002321 if (old_selector == selector)
2322 ret = 0;
2323 else
2324 ret = rdev->desc->ops->set_voltage_sel(
2325 rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002326 } else {
2327 ret = -EINVAL;
2328 }
Mark Browne8eef822010-12-12 14:36:17 +00002329 }
Mark Brown75790252010-12-12 14:25:50 +00002330 } else {
2331 ret = -EINVAL;
2332 }
2333
Axel Lineba41a52012-04-04 10:32:10 +08002334 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302335 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2336 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002337
2338 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2339 old_selector, selector);
2340 if (delay < 0) {
2341 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2342 delay);
2343 delay = 0;
2344 }
Axel Lineba41a52012-04-04 10:32:10 +08002345
Philip Rakity8b96de32012-06-14 15:07:56 -07002346 /* Insert any necessary delays */
2347 if (delay >= 1000) {
2348 mdelay(delay / 1000);
2349 udelay(delay % 1000);
2350 } else if (delay) {
2351 udelay(delay);
2352 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002353 }
2354
Axel Lin2f6c7972012-08-06 23:44:19 +08002355 if (ret == 0 && best_val >= 0) {
2356 unsigned long data = best_val;
2357
Mark Brownded06a52010-12-16 13:59:10 +00002358 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002359 (void *)data);
2360 }
Mark Brownded06a52010-12-16 13:59:10 +00002361
Axel Lineba41a52012-04-04 10:32:10 +08002362 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002363
2364 return ret;
2365}
2366
Mark Browna7a1ad92009-07-21 16:00:24 +01002367/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002368 * regulator_set_voltage - set regulator output voltage
2369 * @regulator: regulator source
2370 * @min_uV: Minimum required voltage in uV
2371 * @max_uV: Maximum acceptable voltage in uV
2372 *
2373 * Sets a voltage regulator to the desired output voltage. This can be set
2374 * during any regulator state. IOW, regulator can be disabled or enabled.
2375 *
2376 * If the regulator is enabled then the voltage will change to the new value
2377 * immediately otherwise if the regulator is disabled the regulator will
2378 * output at the new voltage when enabled.
2379 *
2380 * NOTE: If the regulator is shared between several devices then the lowest
2381 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002382 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002383 * calling this function otherwise this call will fail.
2384 */
2385int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2386{
2387 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002388 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002389 int old_min_uV, old_max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002390
2391 mutex_lock(&rdev->mutex);
2392
Mark Brown95a3c232010-12-16 15:49:37 +00002393 /* If we're setting the same range as last time the change
2394 * should be a noop (some cpufreq implementations use the same
2395 * voltage for multiple frequencies, for example).
2396 */
2397 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2398 goto out;
2399
Liam Girdwood414c70c2008-04-30 15:59:04 +01002400 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002401 if (!rdev->desc->ops->set_voltage &&
2402 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002403 ret = -EINVAL;
2404 goto out;
2405 }
2406
2407 /* constraints check */
2408 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2409 if (ret < 0)
2410 goto out;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002411
2412 /* restore original values in case of error */
2413 old_min_uV = regulator->min_uV;
2414 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002415 regulator->min_uV = min_uV;
2416 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002417
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002418 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2419 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002420 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002421
Mark Brown75790252010-12-12 14:25:50 +00002422 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002423 if (ret < 0)
2424 goto out2;
2425
Liam Girdwood414c70c2008-04-30 15:59:04 +01002426out:
2427 mutex_unlock(&rdev->mutex);
2428 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002429out2:
2430 regulator->min_uV = old_min_uV;
2431 regulator->max_uV = old_max_uV;
2432 mutex_unlock(&rdev->mutex);
2433 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002434}
2435EXPORT_SYMBOL_GPL(regulator_set_voltage);
2436
Mark Brown606a2562010-12-16 15:49:36 +00002437/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002438 * regulator_set_voltage_time - get raise/fall time
2439 * @regulator: regulator source
2440 * @old_uV: starting voltage in microvolts
2441 * @new_uV: target voltage in microvolts
2442 *
2443 * Provided with the starting and ending voltage, this function attempts to
2444 * calculate the time in microseconds required to rise or fall to this new
2445 * voltage.
2446 */
2447int regulator_set_voltage_time(struct regulator *regulator,
2448 int old_uV, int new_uV)
2449{
2450 struct regulator_dev *rdev = regulator->rdev;
2451 struct regulator_ops *ops = rdev->desc->ops;
2452 int old_sel = -1;
2453 int new_sel = -1;
2454 int voltage;
2455 int i;
2456
2457 /* Currently requires operations to do this */
2458 if (!ops->list_voltage || !ops->set_voltage_time_sel
2459 || !rdev->desc->n_voltages)
2460 return -EINVAL;
2461
2462 for (i = 0; i < rdev->desc->n_voltages; i++) {
2463 /* We only look for exact voltage matches here */
2464 voltage = regulator_list_voltage(regulator, i);
2465 if (voltage < 0)
2466 return -EINVAL;
2467 if (voltage == 0)
2468 continue;
2469 if (voltage == old_uV)
2470 old_sel = i;
2471 if (voltage == new_uV)
2472 new_sel = i;
2473 }
2474
2475 if (old_sel < 0 || new_sel < 0)
2476 return -EINVAL;
2477
2478 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2479}
2480EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2481
2482/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002483 * regulator_set_voltage_time_sel - get raise/fall time
2484 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302485 * @old_selector: selector for starting voltage
2486 * @new_selector: selector for target voltage
2487 *
2488 * Provided with the starting and target voltage selectors, this function
2489 * returns time in microseconds required to rise or fall to this new voltage
2490 *
Axel Linf11d08c2012-06-19 09:38:45 +08002491 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002492 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302493 */
2494int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2495 unsigned int old_selector,
2496 unsigned int new_selector)
2497{
Axel Lin398715a2012-06-18 10:11:28 +08002498 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002499 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002500
2501 if (rdev->constraints->ramp_delay)
2502 ramp_delay = rdev->constraints->ramp_delay;
2503 else if (rdev->desc->ramp_delay)
2504 ramp_delay = rdev->desc->ramp_delay;
2505
2506 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302507 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002508 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302509 }
Axel Lin398715a2012-06-18 10:11:28 +08002510
Axel Linf11d08c2012-06-19 09:38:45 +08002511 /* sanity check */
2512 if (!rdev->desc->ops->list_voltage)
2513 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002514
Axel Linf11d08c2012-06-19 09:38:45 +08002515 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2516 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2517
2518 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302519}
Mark Brownb19dbf72012-06-23 11:34:20 +01002520EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302521
2522/**
Mark Brown606a2562010-12-16 15:49:36 +00002523 * regulator_sync_voltage - re-apply last regulator output voltage
2524 * @regulator: regulator source
2525 *
2526 * Re-apply the last configured voltage. This is intended to be used
2527 * where some external control source the consumer is cooperating with
2528 * has caused the configured voltage to change.
2529 */
2530int regulator_sync_voltage(struct regulator *regulator)
2531{
2532 struct regulator_dev *rdev = regulator->rdev;
2533 int ret, min_uV, max_uV;
2534
2535 mutex_lock(&rdev->mutex);
2536
2537 if (!rdev->desc->ops->set_voltage &&
2538 !rdev->desc->ops->set_voltage_sel) {
2539 ret = -EINVAL;
2540 goto out;
2541 }
2542
2543 /* This is only going to work if we've had a voltage configured. */
2544 if (!regulator->min_uV && !regulator->max_uV) {
2545 ret = -EINVAL;
2546 goto out;
2547 }
2548
2549 min_uV = regulator->min_uV;
2550 max_uV = regulator->max_uV;
2551
2552 /* This should be a paranoia check... */
2553 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2554 if (ret < 0)
2555 goto out;
2556
2557 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2558 if (ret < 0)
2559 goto out;
2560
2561 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2562
2563out:
2564 mutex_unlock(&rdev->mutex);
2565 return ret;
2566}
2567EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2568
Liam Girdwood414c70c2008-04-30 15:59:04 +01002569static int _regulator_get_voltage(struct regulator_dev *rdev)
2570{
Mark Brownbf5892a2011-05-08 22:13:37 +01002571 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002572
2573 if (rdev->desc->ops->get_voltage_sel) {
2574 sel = rdev->desc->ops->get_voltage_sel(rdev);
2575 if (sel < 0)
2576 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002577 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002578 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002579 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002580 } else if (rdev->desc->ops->list_voltage) {
2581 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05302582 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2583 ret = rdev->desc->fixed_uV;
Axel Lincb220d12011-05-23 20:08:10 +08002584 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002585 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002586 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002587
Axel Lincb220d12011-05-23 20:08:10 +08002588 if (ret < 0)
2589 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002590 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002591}
2592
2593/**
2594 * regulator_get_voltage - get regulator output voltage
2595 * @regulator: regulator source
2596 *
2597 * This returns the current regulator voltage in uV.
2598 *
2599 * NOTE: If the regulator is disabled it will return the voltage value. This
2600 * function should not be used to determine regulator state.
2601 */
2602int regulator_get_voltage(struct regulator *regulator)
2603{
2604 int ret;
2605
2606 mutex_lock(&regulator->rdev->mutex);
2607
2608 ret = _regulator_get_voltage(regulator->rdev);
2609
2610 mutex_unlock(&regulator->rdev->mutex);
2611
2612 return ret;
2613}
2614EXPORT_SYMBOL_GPL(regulator_get_voltage);
2615
2616/**
2617 * regulator_set_current_limit - set regulator output current limit
2618 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01002619 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01002620 * @max_uA: Maximum supported current in uA
2621 *
2622 * Sets current sink to the desired output current. This can be set during
2623 * any regulator state. IOW, regulator can be disabled or enabled.
2624 *
2625 * If the regulator is enabled then the current will change to the new value
2626 * immediately otherwise if the regulator is disabled the regulator will
2627 * output at the new current when enabled.
2628 *
2629 * NOTE: Regulator system constraints must be set for this regulator before
2630 * calling this function otherwise this call will fail.
2631 */
2632int regulator_set_current_limit(struct regulator *regulator,
2633 int min_uA, int max_uA)
2634{
2635 struct regulator_dev *rdev = regulator->rdev;
2636 int ret;
2637
2638 mutex_lock(&rdev->mutex);
2639
2640 /* sanity check */
2641 if (!rdev->desc->ops->set_current_limit) {
2642 ret = -EINVAL;
2643 goto out;
2644 }
2645
2646 /* constraints check */
2647 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2648 if (ret < 0)
2649 goto out;
2650
2651 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2652out:
2653 mutex_unlock(&rdev->mutex);
2654 return ret;
2655}
2656EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2657
2658static int _regulator_get_current_limit(struct regulator_dev *rdev)
2659{
2660 int ret;
2661
2662 mutex_lock(&rdev->mutex);
2663
2664 /* sanity check */
2665 if (!rdev->desc->ops->get_current_limit) {
2666 ret = -EINVAL;
2667 goto out;
2668 }
2669
2670 ret = rdev->desc->ops->get_current_limit(rdev);
2671out:
2672 mutex_unlock(&rdev->mutex);
2673 return ret;
2674}
2675
2676/**
2677 * regulator_get_current_limit - get regulator output current
2678 * @regulator: regulator source
2679 *
2680 * This returns the current supplied by the specified current sink in uA.
2681 *
2682 * NOTE: If the regulator is disabled it will return the current value. This
2683 * function should not be used to determine regulator state.
2684 */
2685int regulator_get_current_limit(struct regulator *regulator)
2686{
2687 return _regulator_get_current_limit(regulator->rdev);
2688}
2689EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2690
2691/**
2692 * regulator_set_mode - set regulator operating mode
2693 * @regulator: regulator source
2694 * @mode: operating mode - one of the REGULATOR_MODE constants
2695 *
2696 * Set regulator operating mode to increase regulator efficiency or improve
2697 * regulation performance.
2698 *
2699 * NOTE: Regulator system constraints must be set for this regulator before
2700 * calling this function otherwise this call will fail.
2701 */
2702int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2703{
2704 struct regulator_dev *rdev = regulator->rdev;
2705 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302706 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002707
2708 mutex_lock(&rdev->mutex);
2709
2710 /* sanity check */
2711 if (!rdev->desc->ops->set_mode) {
2712 ret = -EINVAL;
2713 goto out;
2714 }
2715
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302716 /* return if the same mode is requested */
2717 if (rdev->desc->ops->get_mode) {
2718 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2719 if (regulator_curr_mode == mode) {
2720 ret = 0;
2721 goto out;
2722 }
2723 }
2724
Liam Girdwood414c70c2008-04-30 15:59:04 +01002725 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002726 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002727 if (ret < 0)
2728 goto out;
2729
2730 ret = rdev->desc->ops->set_mode(rdev, mode);
2731out:
2732 mutex_unlock(&rdev->mutex);
2733 return ret;
2734}
2735EXPORT_SYMBOL_GPL(regulator_set_mode);
2736
2737static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2738{
2739 int ret;
2740
2741 mutex_lock(&rdev->mutex);
2742
2743 /* sanity check */
2744 if (!rdev->desc->ops->get_mode) {
2745 ret = -EINVAL;
2746 goto out;
2747 }
2748
2749 ret = rdev->desc->ops->get_mode(rdev);
2750out:
2751 mutex_unlock(&rdev->mutex);
2752 return ret;
2753}
2754
2755/**
2756 * regulator_get_mode - get regulator operating mode
2757 * @regulator: regulator source
2758 *
2759 * Get the current regulator operating mode.
2760 */
2761unsigned int regulator_get_mode(struct regulator *regulator)
2762{
2763 return _regulator_get_mode(regulator->rdev);
2764}
2765EXPORT_SYMBOL_GPL(regulator_get_mode);
2766
2767/**
2768 * regulator_set_optimum_mode - set regulator optimum operating mode
2769 * @regulator: regulator source
2770 * @uA_load: load current
2771 *
2772 * Notifies the regulator core of a new device load. This is then used by
2773 * DRMS (if enabled by constraints) to set the most efficient regulator
2774 * operating mode for the new regulator loading.
2775 *
2776 * Consumer devices notify their supply regulator of the maximum power
2777 * they will require (can be taken from device datasheet in the power
2778 * consumption tables) when they change operational status and hence power
2779 * state. Examples of operational state changes that can affect power
2780 * consumption are :-
2781 *
2782 * o Device is opened / closed.
2783 * o Device I/O is about to begin or has just finished.
2784 * o Device is idling in between work.
2785 *
2786 * This information is also exported via sysfs to userspace.
2787 *
2788 * DRMS will sum the total requested load on the regulator and change
2789 * to the most efficient operating mode if platform constraints allow.
2790 *
2791 * Returns the new regulator mode or error.
2792 */
2793int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2794{
2795 struct regulator_dev *rdev = regulator->rdev;
2796 struct regulator *consumer;
Stephen Boydd92d95b62012-07-02 19:21:06 -07002797 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002798 unsigned int mode;
2799
Stephen Boydd92d95b62012-07-02 19:21:06 -07002800 if (rdev->supply)
2801 input_uV = regulator_get_voltage(rdev->supply);
2802
Liam Girdwood414c70c2008-04-30 15:59:04 +01002803 mutex_lock(&rdev->mutex);
2804
Mark Browna4b41482011-05-14 11:19:45 -07002805 /*
2806 * first check to see if we can set modes at all, otherwise just
2807 * tell the consumer everything is OK.
2808 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002809 regulator->uA_load = uA_load;
2810 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002811 if (ret < 0) {
2812 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002813 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002814 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002815
Liam Girdwood414c70c2008-04-30 15:59:04 +01002816 if (!rdev->desc->ops->get_optimum_mode)
2817 goto out;
2818
Mark Browna4b41482011-05-14 11:19:45 -07002819 /*
2820 * we can actually do this so any errors are indicators of
2821 * potential real failure.
2822 */
2823 ret = -EINVAL;
2824
Axel Lin854ccba2012-04-16 18:44:23 +08002825 if (!rdev->desc->ops->set_mode)
2826 goto out;
2827
Liam Girdwood414c70c2008-04-30 15:59:04 +01002828 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002829 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002830 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002831 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002832 goto out;
2833 }
2834
Stephen Boydd92d95b62012-07-02 19:21:06 -07002835 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002836 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002837 input_uV = rdev->constraints->input_uV;
2838 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002839 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002840 goto out;
2841 }
2842
2843 /* calc total requested load for this regulator */
2844 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002845 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002846
2847 mode = rdev->desc->ops->get_optimum_mode(rdev,
2848 input_uV, output_uV,
2849 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002850 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002851 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002852 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2853 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002854 goto out;
2855 }
2856
2857 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002858 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002859 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002860 goto out;
2861 }
2862 ret = mode;
2863out:
2864 mutex_unlock(&rdev->mutex);
2865 return ret;
2866}
2867EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2868
2869/**
Mark Brownf59c8f92012-08-31 10:36:37 -07002870 * regulator_allow_bypass - allow the regulator to go into bypass mode
2871 *
2872 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06002873 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07002874 *
2875 * Allow the regulator to go into bypass mode if all other consumers
2876 * for the regulator also enable bypass mode and the machine
2877 * constraints allow this. Bypass mode means that the regulator is
2878 * simply passing the input directly to the output with no regulation.
2879 */
2880int regulator_allow_bypass(struct regulator *regulator, bool enable)
2881{
2882 struct regulator_dev *rdev = regulator->rdev;
2883 int ret = 0;
2884
2885 if (!rdev->desc->ops->set_bypass)
2886 return 0;
2887
2888 if (rdev->constraints &&
2889 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2890 return 0;
2891
2892 mutex_lock(&rdev->mutex);
2893
2894 if (enable && !regulator->bypass) {
2895 rdev->bypass_count++;
2896
2897 if (rdev->bypass_count == rdev->open_count) {
2898 ret = rdev->desc->ops->set_bypass(rdev, enable);
2899 if (ret != 0)
2900 rdev->bypass_count--;
2901 }
2902
2903 } else if (!enable && regulator->bypass) {
2904 rdev->bypass_count--;
2905
2906 if (rdev->bypass_count != rdev->open_count) {
2907 ret = rdev->desc->ops->set_bypass(rdev, enable);
2908 if (ret != 0)
2909 rdev->bypass_count++;
2910 }
2911 }
2912
2913 if (ret == 0)
2914 regulator->bypass = enable;
2915
2916 mutex_unlock(&rdev->mutex);
2917
2918 return ret;
2919}
2920EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2921
2922/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002923 * regulator_register_notifier - register regulator event notifier
2924 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002925 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002926 *
2927 * Register notifier block to receive regulator events.
2928 */
2929int regulator_register_notifier(struct regulator *regulator,
2930 struct notifier_block *nb)
2931{
2932 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2933 nb);
2934}
2935EXPORT_SYMBOL_GPL(regulator_register_notifier);
2936
2937/**
2938 * regulator_unregister_notifier - unregister regulator event notifier
2939 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002940 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002941 *
2942 * Unregister regulator event notifier block.
2943 */
2944int regulator_unregister_notifier(struct regulator *regulator,
2945 struct notifier_block *nb)
2946{
2947 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2948 nb);
2949}
2950EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2951
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002952/* notify regulator consumers and downstream regulator consumers.
2953 * Note mutex must be held by caller.
2954 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002955static void _notifier_call_chain(struct regulator_dev *rdev,
2956 unsigned long event, void *data)
2957{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002958 /* call rdev chain first */
Mark Brownd8493d22012-06-15 19:09:09 +01002959 blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002960}
2961
2962/**
2963 * regulator_bulk_get - get multiple regulator consumers
2964 *
2965 * @dev: Device to supply
2966 * @num_consumers: Number of consumers to register
2967 * @consumers: Configuration of consumers; clients are stored here.
2968 *
2969 * @return 0 on success, an errno on failure.
2970 *
2971 * This helper function allows drivers to get several regulator
2972 * consumers in one operation. If any of the regulators cannot be
2973 * acquired then any regulators that were allocated will be freed
2974 * before returning to the caller.
2975 */
2976int regulator_bulk_get(struct device *dev, int num_consumers,
2977 struct regulator_bulk_data *consumers)
2978{
2979 int i;
2980 int ret;
2981
2982 for (i = 0; i < num_consumers; i++)
2983 consumers[i].consumer = NULL;
2984
2985 for (i = 0; i < num_consumers; i++) {
2986 consumers[i].consumer = regulator_get(dev,
2987 consumers[i].supply);
2988 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002989 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002990 dev_err(dev, "Failed to get supply '%s': %d\n",
2991 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002992 consumers[i].consumer = NULL;
2993 goto err;
2994 }
2995 }
2996
2997 return 0;
2998
2999err:
Axel Linb29c7692012-02-20 10:32:16 +08003000 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003001 regulator_put(consumers[i].consumer);
3002
3003 return ret;
3004}
3005EXPORT_SYMBOL_GPL(regulator_bulk_get);
3006
Mark Brownf21e0e82011-05-24 08:12:40 +08003007static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3008{
3009 struct regulator_bulk_data *bulk = data;
3010
3011 bulk->ret = regulator_enable(bulk->consumer);
3012}
3013
Liam Girdwood414c70c2008-04-30 15:59:04 +01003014/**
3015 * regulator_bulk_enable - enable multiple regulator consumers
3016 *
3017 * @num_consumers: Number of consumers
3018 * @consumers: Consumer data; clients are stored here.
3019 * @return 0 on success, an errno on failure
3020 *
3021 * This convenience API allows consumers to enable multiple regulator
3022 * clients in a single API call. If any consumers cannot be enabled
3023 * then any others that were enabled will be disabled again prior to
3024 * return.
3025 */
3026int regulator_bulk_enable(int num_consumers,
3027 struct regulator_bulk_data *consumers)
3028{
Dan Williams2955b472012-07-09 19:33:25 -07003029 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003030 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003031 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003032
Mark Brown6492bc12012-04-19 13:19:07 +01003033 for (i = 0; i < num_consumers; i++) {
3034 if (consumers[i].consumer->always_on)
3035 consumers[i].ret = 0;
3036 else
3037 async_schedule_domain(regulator_bulk_enable_async,
3038 &consumers[i], &async_domain);
3039 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003040
3041 async_synchronize_full_domain(&async_domain);
3042
3043 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003044 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003045 if (consumers[i].ret != 0) {
3046 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003047 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003048 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003049 }
3050
3051 return 0;
3052
3053err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003054 for (i = 0; i < num_consumers; i++) {
3055 if (consumers[i].ret < 0)
3056 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3057 consumers[i].ret);
3058 else
3059 regulator_disable(consumers[i].consumer);
3060 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003061
3062 return ret;
3063}
3064EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3065
3066/**
3067 * regulator_bulk_disable - disable multiple regulator consumers
3068 *
3069 * @num_consumers: Number of consumers
3070 * @consumers: Consumer data; clients are stored here.
3071 * @return 0 on success, an errno on failure
3072 *
3073 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003074 * clients in a single API call. If any consumers cannot be disabled
3075 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003076 * return.
3077 */
3078int regulator_bulk_disable(int num_consumers,
3079 struct regulator_bulk_data *consumers)
3080{
3081 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003082 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003083
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003084 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003085 ret = regulator_disable(consumers[i].consumer);
3086 if (ret != 0)
3087 goto err;
3088 }
3089
3090 return 0;
3091
3092err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003093 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003094 for (++i; i < num_consumers; ++i) {
3095 r = regulator_enable(consumers[i].consumer);
3096 if (r != 0)
3097 pr_err("Failed to reename %s: %d\n",
3098 consumers[i].supply, r);
3099 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003100
3101 return ret;
3102}
3103EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3104
3105/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003106 * regulator_bulk_force_disable - force disable multiple regulator consumers
3107 *
3108 * @num_consumers: Number of consumers
3109 * @consumers: Consumer data; clients are stored here.
3110 * @return 0 on success, an errno on failure
3111 *
3112 * This convenience API allows consumers to forcibly disable multiple regulator
3113 * clients in a single API call.
3114 * NOTE: This should be used for situations when device damage will
3115 * likely occur if the regulators are not disabled (e.g. over temp).
3116 * Although regulator_force_disable function call for some consumers can
3117 * return error numbers, the function is called for all consumers.
3118 */
3119int regulator_bulk_force_disable(int num_consumers,
3120 struct regulator_bulk_data *consumers)
3121{
3122 int i;
3123 int ret;
3124
3125 for (i = 0; i < num_consumers; i++)
3126 consumers[i].ret =
3127 regulator_force_disable(consumers[i].consumer);
3128
3129 for (i = 0; i < num_consumers; i++) {
3130 if (consumers[i].ret != 0) {
3131 ret = consumers[i].ret;
3132 goto out;
3133 }
3134 }
3135
3136 return 0;
3137out:
3138 return ret;
3139}
3140EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3141
3142/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003143 * regulator_bulk_free - free multiple regulator consumers
3144 *
3145 * @num_consumers: Number of consumers
3146 * @consumers: Consumer data; clients are stored here.
3147 *
3148 * This convenience API allows consumers to free multiple regulator
3149 * clients in a single API call.
3150 */
3151void regulator_bulk_free(int num_consumers,
3152 struct regulator_bulk_data *consumers)
3153{
3154 int i;
3155
3156 for (i = 0; i < num_consumers; i++) {
3157 regulator_put(consumers[i].consumer);
3158 consumers[i].consumer = NULL;
3159 }
3160}
3161EXPORT_SYMBOL_GPL(regulator_bulk_free);
3162
3163/**
3164 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003165 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003166 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003167 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003168 *
3169 * Called by regulator drivers to notify clients a regulator event has
3170 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003171 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003172 */
3173int regulator_notifier_call_chain(struct regulator_dev *rdev,
3174 unsigned long event, void *data)
3175{
3176 _notifier_call_chain(rdev, event, data);
3177 return NOTIFY_DONE;
3178
3179}
3180EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3181
Mark Brownbe721972009-08-04 20:09:52 +02003182/**
3183 * regulator_mode_to_status - convert a regulator mode into a status
3184 *
3185 * @mode: Mode to convert
3186 *
3187 * Convert a regulator mode into a status.
3188 */
3189int regulator_mode_to_status(unsigned int mode)
3190{
3191 switch (mode) {
3192 case REGULATOR_MODE_FAST:
3193 return REGULATOR_STATUS_FAST;
3194 case REGULATOR_MODE_NORMAL:
3195 return REGULATOR_STATUS_NORMAL;
3196 case REGULATOR_MODE_IDLE:
3197 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003198 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003199 return REGULATOR_STATUS_STANDBY;
3200 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003201 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003202 }
3203}
3204EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3205
David Brownell7ad68e22008-11-11 17:39:02 -08003206/*
3207 * To avoid cluttering sysfs (and memory) with useless state, only
3208 * create attributes that can be meaningfully displayed.
3209 */
3210static int add_regulator_attributes(struct regulator_dev *rdev)
3211{
3212 struct device *dev = &rdev->dev;
3213 struct regulator_ops *ops = rdev->desc->ops;
3214 int status = 0;
3215
3216 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00003217 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
Mark Brownf2889e62012-08-27 11:37:04 -07003218 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
Laxman Dewangan5a523602013-09-10 15:45:05 +05303219 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3220 (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1))) {
David Brownell7ad68e22008-11-11 17:39:02 -08003221 status = device_create_file(dev, &dev_attr_microvolts);
3222 if (status < 0)
3223 return status;
3224 }
3225 if (ops->get_current_limit) {
3226 status = device_create_file(dev, &dev_attr_microamps);
3227 if (status < 0)
3228 return status;
3229 }
3230 if (ops->get_mode) {
3231 status = device_create_file(dev, &dev_attr_opmode);
3232 if (status < 0)
3233 return status;
3234 }
Kim, Milo7b74d142013-02-18 06:50:55 +00003235 if (rdev->ena_pin || ops->is_enabled) {
David Brownell7ad68e22008-11-11 17:39:02 -08003236 status = device_create_file(dev, &dev_attr_state);
3237 if (status < 0)
3238 return status;
3239 }
David Brownell853116a2009-01-14 23:03:17 -08003240 if (ops->get_status) {
3241 status = device_create_file(dev, &dev_attr_status);
3242 if (status < 0)
3243 return status;
3244 }
Mark Brownf59c8f92012-08-31 10:36:37 -07003245 if (ops->get_bypass) {
3246 status = device_create_file(dev, &dev_attr_bypass);
3247 if (status < 0)
3248 return status;
3249 }
David Brownell7ad68e22008-11-11 17:39:02 -08003250
3251 /* some attributes are type-specific */
3252 if (rdev->desc->type == REGULATOR_CURRENT) {
3253 status = device_create_file(dev, &dev_attr_requested_microamps);
3254 if (status < 0)
3255 return status;
3256 }
3257
3258 /* all the other attributes exist to support constraints;
3259 * don't show them if there are no constraints, or if the
3260 * relevant supporting methods are missing.
3261 */
3262 if (!rdev->constraints)
3263 return status;
3264
3265 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003266 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003267 status = device_create_file(dev, &dev_attr_min_microvolts);
3268 if (status < 0)
3269 return status;
3270 status = device_create_file(dev, &dev_attr_max_microvolts);
3271 if (status < 0)
3272 return status;
3273 }
3274 if (ops->set_current_limit) {
3275 status = device_create_file(dev, &dev_attr_min_microamps);
3276 if (status < 0)
3277 return status;
3278 status = device_create_file(dev, &dev_attr_max_microamps);
3279 if (status < 0)
3280 return status;
3281 }
3282
David Brownell7ad68e22008-11-11 17:39:02 -08003283 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3284 if (status < 0)
3285 return status;
3286 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3287 if (status < 0)
3288 return status;
3289 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3290 if (status < 0)
3291 return status;
3292
3293 if (ops->set_suspend_voltage) {
3294 status = device_create_file(dev,
3295 &dev_attr_suspend_standby_microvolts);
3296 if (status < 0)
3297 return status;
3298 status = device_create_file(dev,
3299 &dev_attr_suspend_mem_microvolts);
3300 if (status < 0)
3301 return status;
3302 status = device_create_file(dev,
3303 &dev_attr_suspend_disk_microvolts);
3304 if (status < 0)
3305 return status;
3306 }
3307
3308 if (ops->set_suspend_mode) {
3309 status = device_create_file(dev,
3310 &dev_attr_suspend_standby_mode);
3311 if (status < 0)
3312 return status;
3313 status = device_create_file(dev,
3314 &dev_attr_suspend_mem_mode);
3315 if (status < 0)
3316 return status;
3317 status = device_create_file(dev,
3318 &dev_attr_suspend_disk_mode);
3319 if (status < 0)
3320 return status;
3321 }
3322
3323 return status;
3324}
3325
Mark Brown1130e5b2010-12-21 23:49:31 +00003326static void rdev_init_debugfs(struct regulator_dev *rdev)
3327{
Mark Brown1130e5b2010-12-21 23:49:31 +00003328 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003329 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003330 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003331 return;
3332 }
3333
3334 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3335 &rdev->use_count);
3336 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3337 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003338 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3339 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003340}
3341
Liam Girdwood414c70c2008-04-30 15:59:04 +01003342/**
3343 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003344 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003345 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003346 *
3347 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003348 * Returns a valid pointer to struct regulator_dev on success
3349 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003350 */
Mark Brown65f26842012-04-03 20:46:53 +01003351struct regulator_dev *
3352regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003353 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003354{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003355 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003356 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003357 static atomic_t regulator_no = ATOMIC_INIT(0);
3358 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003359 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003360 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303361 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003362
Mark Brownc1727082012-04-04 00:50:22 +01003363 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003364 return ERR_PTR(-EINVAL);
3365
Mark Brown32c8fad2012-04-11 10:19:12 +01003366 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003367 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003368
Liam Girdwood414c70c2008-04-30 15:59:04 +01003369 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3370 return ERR_PTR(-EINVAL);
3371
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003372 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3373 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003374 return ERR_PTR(-EINVAL);
3375
Mark Brown476c2d82010-12-10 17:28:07 +00003376 /* Only one of each should be implemented */
3377 WARN_ON(regulator_desc->ops->get_voltage &&
3378 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003379 WARN_ON(regulator_desc->ops->set_voltage &&
3380 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003381
3382 /* If we're using selectors we must implement list_voltage. */
3383 if (regulator_desc->ops->get_voltage_sel &&
3384 !regulator_desc->ops->list_voltage) {
3385 return ERR_PTR(-EINVAL);
3386 }
Mark Browne8eef822010-12-12 14:36:17 +00003387 if (regulator_desc->ops->set_voltage_sel &&
3388 !regulator_desc->ops->list_voltage) {
3389 return ERR_PTR(-EINVAL);
3390 }
Mark Brown476c2d82010-12-10 17:28:07 +00003391
Mark Brownc1727082012-04-04 00:50:22 +01003392 init_data = config->init_data;
3393
Liam Girdwood414c70c2008-04-30 15:59:04 +01003394 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3395 if (rdev == NULL)
3396 return ERR_PTR(-ENOMEM);
3397
3398 mutex_lock(&regulator_list_mutex);
3399
3400 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003401 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003402 rdev->owner = regulator_desc->owner;
3403 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003404 if (config->regmap)
3405 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303406 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003407 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303408 else if (dev->parent)
3409 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003410 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003411 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003412 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003413 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003414
Liam Girdwooda5766f12008-10-10 13:22:20 +01003415 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003416 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003417 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003418 if (ret < 0)
3419 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003420 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003421
Liam Girdwooda5766f12008-10-10 13:22:20 +01003422 /* register with sysfs */
3423 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003424 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003425 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003426 dev_set_name(&rdev->dev, "regulator.%d",
3427 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003428 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003429 if (ret != 0) {
3430 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003431 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003432 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003433
3434 dev_set_drvdata(&rdev->dev, rdev);
3435
Marek Szyprowskib2a1ef42012-08-09 16:33:00 +02003436 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003437 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003438 if (ret != 0) {
3439 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3440 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003441 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003442 }
3443
Mark Brown65f73502012-06-27 14:14:38 +01003444 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3445 rdev->ena_gpio_state = 1;
3446
Kim, Milo7b74d142013-02-18 06:50:55 +00003447 if (config->ena_gpio_invert)
Mark Brown65f73502012-06-27 14:14:38 +01003448 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3449 }
3450
Mike Rapoport74f544c2008-11-25 14:53:53 +02003451 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003452 if (init_data)
3453 constraints = &init_data->constraints;
3454
3455 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003456 if (ret < 0)
3457 goto scrub;
3458
David Brownell7ad68e22008-11-11 17:39:02 -08003459 /* add attributes supported by this regulator */
3460 ret = add_regulator_attributes(rdev);
3461 if (ret < 0)
3462 goto scrub;
3463
Mark Brown9a8f5e02011-11-29 18:11:19 +00003464 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303465 supply = init_data->supply_regulator;
3466 else if (regulator_desc->supply_name)
3467 supply = regulator_desc->supply_name;
3468
3469 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003470 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003471
Mark Brown6d191a52012-03-29 14:19:02 +01003472 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003473
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003474 if (ret == -ENODEV) {
3475 /*
3476 * No supply was specified for this regulator and
3477 * there will never be one.
3478 */
3479 ret = 0;
3480 goto add_dev;
3481 } else if (!r) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05303482 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003483 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003484 goto scrub;
3485 }
3486
3487 ret = set_supply(rdev, r);
3488 if (ret < 0)
3489 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303490
3491 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003492 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303493 ret = regulator_enable(rdev->supply);
3494 if (ret < 0)
3495 goto scrub;
3496 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003497 }
3498
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003499add_dev:
Liam Girdwooda5766f12008-10-10 13:22:20 +01003500 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003501 if (init_data) {
3502 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3503 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003504 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003505 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003506 if (ret < 0) {
3507 dev_err(dev, "Failed to set supply %s\n",
3508 init_data->consumer_supplies[i].supply);
3509 goto unset_supplies;
3510 }
Mark Brown23c2f042011-02-24 17:39:09 +00003511 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003512 }
3513
3514 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003515
3516 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003517out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003518 mutex_unlock(&regulator_list_mutex);
3519 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003520
Jani Nikulad4033b52010-04-29 10:55:11 +03003521unset_supplies:
3522 unset_regulator_supplies(rdev);
3523
David Brownell4fca9542008-11-11 17:38:53 -08003524scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003525 if (rdev->supply)
Charles Keepax23ff2f02012-11-14 09:39:31 +00003526 _regulator_put(rdev->supply);
Kim, Milof19b00d2013-02-18 06:50:39 +00003527 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003528 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003529wash:
David Brownell4fca9542008-11-11 17:38:53 -08003530 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003531 /* device core frees rdev */
3532 rdev = ERR_PTR(ret);
3533 goto out;
3534
David Brownell4fca9542008-11-11 17:38:53 -08003535clean:
3536 kfree(rdev);
3537 rdev = ERR_PTR(ret);
3538 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003539}
3540EXPORT_SYMBOL_GPL(regulator_register);
3541
3542/**
3543 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003544 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003545 *
3546 * Called by regulator drivers to unregister a regulator.
3547 */
3548void regulator_unregister(struct regulator_dev *rdev)
3549{
3550 if (rdev == NULL)
3551 return;
3552
Mark Brown891636e2013-07-08 09:14:45 +01003553 if (rdev->supply) {
3554 while (rdev->use_count--)
3555 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01003556 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01003557 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003558 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003559 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003560 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003561 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003562 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003563 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003564 kfree(rdev->constraints);
Kim, Milof19b00d2013-02-18 06:50:39 +00003565 regulator_ena_gpio_free(rdev);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003566 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003567 mutex_unlock(&regulator_list_mutex);
3568}
3569EXPORT_SYMBOL_GPL(regulator_unregister);
3570
3571/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003572 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003573 * @state: system suspend state
3574 *
3575 * Configure each regulator with it's suspend operating parameters for state.
3576 * This will usually be called by machine suspend code prior to supending.
3577 */
3578int regulator_suspend_prepare(suspend_state_t state)
3579{
3580 struct regulator_dev *rdev;
3581 int ret = 0;
3582
3583 /* ON is handled by regulator active state */
3584 if (state == PM_SUSPEND_ON)
3585 return -EINVAL;
3586
3587 mutex_lock(&regulator_list_mutex);
3588 list_for_each_entry(rdev, &regulator_list, list) {
3589
3590 mutex_lock(&rdev->mutex);
3591 ret = suspend_prepare(rdev, state);
3592 mutex_unlock(&rdev->mutex);
3593
3594 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003595 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003596 goto out;
3597 }
3598 }
3599out:
3600 mutex_unlock(&regulator_list_mutex);
3601 return ret;
3602}
3603EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3604
3605/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003606 * regulator_suspend_finish - resume regulators from system wide suspend
3607 *
3608 * Turn on regulators that might be turned off by regulator_suspend_prepare
3609 * and that should be turned on according to the regulators properties.
3610 */
3611int regulator_suspend_finish(void)
3612{
3613 struct regulator_dev *rdev;
3614 int ret = 0, error;
3615
3616 mutex_lock(&regulator_list_mutex);
3617 list_for_each_entry(rdev, &regulator_list, list) {
3618 struct regulator_ops *ops = rdev->desc->ops;
3619
3620 mutex_lock(&rdev->mutex);
3621 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3622 ops->enable) {
3623 error = ops->enable(rdev);
3624 if (error)
3625 ret = error;
3626 } else {
3627 if (!has_full_constraints)
3628 goto unlock;
3629 if (!ops->disable)
3630 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003631 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003632 goto unlock;
3633
3634 error = ops->disable(rdev);
3635 if (error)
3636 ret = error;
3637 }
3638unlock:
3639 mutex_unlock(&rdev->mutex);
3640 }
3641 mutex_unlock(&regulator_list_mutex);
3642 return ret;
3643}
3644EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3645
3646/**
Mark Brownca725562009-03-16 19:36:34 +00003647 * regulator_has_full_constraints - the system has fully specified constraints
3648 *
3649 * Calling this function will cause the regulator API to disable all
3650 * regulators which have a zero use count and don't have an always_on
3651 * constraint in a late_initcall.
3652 *
3653 * The intention is that this will become the default behaviour in a
3654 * future kernel release so users are encouraged to use this facility
3655 * now.
3656 */
3657void regulator_has_full_constraints(void)
3658{
3659 has_full_constraints = 1;
3660}
3661EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3662
3663/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003664 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003665 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003666 *
3667 * Get rdev regulator driver private data. This call can be used in the
3668 * regulator driver context.
3669 */
3670void *rdev_get_drvdata(struct regulator_dev *rdev)
3671{
3672 return rdev->reg_data;
3673}
3674EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3675
3676/**
3677 * regulator_get_drvdata - get regulator driver data
3678 * @regulator: regulator
3679 *
3680 * Get regulator driver private data. This call can be used in the consumer
3681 * driver context when non API regulator specific functions need to be called.
3682 */
3683void *regulator_get_drvdata(struct regulator *regulator)
3684{
3685 return regulator->rdev->reg_data;
3686}
3687EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3688
3689/**
3690 * regulator_set_drvdata - set regulator driver data
3691 * @regulator: regulator
3692 * @data: data
3693 */
3694void regulator_set_drvdata(struct regulator *regulator, void *data)
3695{
3696 regulator->rdev->reg_data = data;
3697}
3698EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3699
3700/**
3701 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003702 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003703 */
3704int rdev_get_id(struct regulator_dev *rdev)
3705{
3706 return rdev->desc->id;
3707}
3708EXPORT_SYMBOL_GPL(rdev_get_id);
3709
Liam Girdwooda5766f12008-10-10 13:22:20 +01003710struct device *rdev_get_dev(struct regulator_dev *rdev)
3711{
3712 return &rdev->dev;
3713}
3714EXPORT_SYMBOL_GPL(rdev_get_dev);
3715
3716void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3717{
3718 return reg_init_data->driver_data;
3719}
3720EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3721
Mark Brownba55a972011-08-23 17:39:10 +01003722#ifdef CONFIG_DEBUG_FS
3723static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3724 size_t count, loff_t *ppos)
3725{
3726 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3727 ssize_t len, ret = 0;
3728 struct regulator_map *map;
3729
3730 if (!buf)
3731 return -ENOMEM;
3732
3733 list_for_each_entry(map, &regulator_map_list, list) {
3734 len = snprintf(buf + ret, PAGE_SIZE - ret,
3735 "%s -> %s.%s\n",
3736 rdev_get_name(map->regulator), map->dev_name,
3737 map->supply);
3738 if (len >= 0)
3739 ret += len;
3740 if (ret > PAGE_SIZE) {
3741 ret = PAGE_SIZE;
3742 break;
3743 }
3744 }
3745
3746 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3747
3748 kfree(buf);
3749
3750 return ret;
3751}
Stephen Boyd24751432012-02-20 22:50:42 -08003752#endif
Mark Brownba55a972011-08-23 17:39:10 +01003753
3754static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003755#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003756 .read = supply_map_read_file,
3757 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003758#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003759};
Mark Brownba55a972011-08-23 17:39:10 +01003760
Liam Girdwood414c70c2008-04-30 15:59:04 +01003761static int __init regulator_init(void)
3762{
Mark Brown34abbd62010-02-12 10:18:08 +00003763 int ret;
3764
Mark Brown34abbd62010-02-12 10:18:08 +00003765 ret = class_register(&regulator_class);
3766
Mark Brown1130e5b2010-12-21 23:49:31 +00003767 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003768 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003769 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003770
Mark Brownf4d562c2012-02-20 21:01:04 +00003771 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3772 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003773
Mark Brown34abbd62010-02-12 10:18:08 +00003774 regulator_dummy_init();
3775
3776 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003777}
3778
3779/* init early to allow our consumers to complete system booting */
3780core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003781
3782static int __init regulator_init_complete(void)
3783{
3784 struct regulator_dev *rdev;
3785 struct regulator_ops *ops;
3786 struct regulation_constraints *c;
3787 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003788
Mark Brown86f5fcf2012-07-06 18:19:13 +01003789 /*
3790 * Since DT doesn't provide an idiomatic mechanism for
3791 * enabling full constraints and since it's much more natural
3792 * with DT to provide them just assume that a DT enabled
3793 * system has full constraints.
3794 */
3795 if (of_have_populated_dt())
3796 has_full_constraints = true;
3797
Mark Brownca725562009-03-16 19:36:34 +00003798 mutex_lock(&regulator_list_mutex);
3799
3800 /* If we have a full configuration then disable any regulators
3801 * which are not in use or always_on. This will become the
3802 * default behaviour in the future.
3803 */
3804 list_for_each_entry(rdev, &regulator_list, list) {
3805 ops = rdev->desc->ops;
3806 c = rdev->constraints;
3807
Mark Brownf25e0b42009-08-03 18:49:55 +01003808 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003809 continue;
3810
3811 mutex_lock(&rdev->mutex);
3812
3813 if (rdev->use_count)
3814 goto unlock;
3815
3816 /* If we can't read the status assume it's on. */
3817 if (ops->is_enabled)
3818 enabled = ops->is_enabled(rdev);
3819 else
3820 enabled = 1;
3821
3822 if (!enabled)
3823 goto unlock;
3824
3825 if (has_full_constraints) {
3826 /* We log since this may kill the system if it
3827 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003828 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003829 ret = ops->disable(rdev);
3830 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003831 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003832 }
3833 } else {
3834 /* The intention is that in future we will
3835 * assume that full constraints are provided
3836 * so warn even if we aren't going to do
3837 * anything here.
3838 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003839 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003840 }
3841
3842unlock:
3843 mutex_unlock(&rdev->mutex);
3844 }
3845
3846 mutex_unlock(&regulator_list_mutex);
3847
3848 return 0;
3849}
3850late_initcall(regulator_init_complete);