blob: 0e0d8297dc8e607b8d70cea4f77300c4faec4a4f [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>
Russell King778b28b2014-06-29 17:55:54 +010027#include <linux/gpio/consumer.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053028#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010029#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053030#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010031#include <linux/regulator/consumer.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040034#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010035
Mark Brown02fa3ec2010-11-10 14:38:30 +000036#define CREATE_TRACE_POINTS
37#include <trace/events/regulator.h>
38
Mark Brown34abbd62010-02-12 10:18:08 +000039#include "dummy.h"
Mark Brown0cdfcc02013-09-11 13:15:40 +010040#include "internal.h"
Mark Brown34abbd62010-02-12 10:18:08 +000041
Mark Brown7d51a0d2011-06-09 16:06:37 +010042#define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080044#define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50#define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52
Liam Girdwood414c70c2008-04-30 15:59:04 +010053static DEFINE_MUTEX(regulator_list_mutex);
54static LIST_HEAD(regulator_list);
55static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000056static LIST_HEAD(regulator_ena_gpio_list);
Charles Keepaxa06ccd92013-10-15 20:14:20 +010057static LIST_HEAD(regulator_supply_alias_list);
Mark Brown21cf8912010-12-21 23:30:07 +000058static bool has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010059
Mark Brown1130e5b2010-12-21 23:49:31 +000060static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000061
Mark Brown8dc53902008-12-31 12:52:40 +000062/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010063 * struct regulator_map
64 *
65 * Used to provide symbolic supply names to devices.
66 */
67struct regulator_map {
68 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010069 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010070 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010071 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010072};
73
Liam Girdwood414c70c2008-04-30 15:59:04 +010074/*
Kim, Milof19b00d2013-02-18 06:50:39 +000075 * struct regulator_enable_gpio
76 *
77 * Management for shared enable GPIO pin
78 */
79struct regulator_enable_gpio {
80 struct list_head list;
Russell King778b28b2014-06-29 17:55:54 +010081 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +000082 u32 enable_count; /* a number of enabled shared GPIO */
83 u32 request_count; /* a number of requested shared GPIO */
84 unsigned int ena_gpio_invert:1;
85};
86
Charles Keepaxa06ccd92013-10-15 20:14:20 +010087/*
88 * struct regulator_supply_alias
89 *
90 * Used to map lookups for a supply onto an alternative device.
91 */
92struct regulator_supply_alias {
93 struct list_head list;
94 struct device *src_dev;
95 const char *src_supply;
96 struct device *alias_dev;
97 const char *alias_supply;
98};
99
Liam Girdwood414c70c2008-04-30 15:59:04 +0100100static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100101static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100102static int _regulator_get_voltage(struct regulator_dev *rdev);
103static int _regulator_get_current_limit(struct regulator_dev *rdev);
104static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
Heiko Stübner71795692014-08-28 12:36:04 -0700105static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100106 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000107static int _regulator_do_set_voltage(struct regulator_dev *rdev,
108 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100109static struct regulator *create_regulator(struct regulator_dev *rdev,
110 struct device *dev,
111 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100112
Mark Brown1083c392009-10-22 16:31:32 +0100113static const char *rdev_get_name(struct regulator_dev *rdev)
114{
115 if (rdev->constraints && rdev->constraints->name)
116 return rdev->constraints->name;
117 else if (rdev->desc->name)
118 return rdev->desc->name;
119 else
120 return "";
121}
122
Mark Brown87b28412013-11-27 16:13:10 +0000123static bool have_full_constraints(void)
124{
Mark Brown75bc9642013-11-27 16:22:53 +0000125 return has_full_constraints || of_have_populated_dt();
Mark Brown87b28412013-11-27 16:13:10 +0000126}
127
Rajendra Nayak69511a42011-11-18 16:47:20 +0530128/**
129 * of_get_regulator - get a regulator device node based on supply name
130 * @dev: Device pointer for the consumer (of regulator) device
131 * @supply: regulator supply name
132 *
133 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100134 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530135 * returns NULL.
136 */
137static struct device_node *of_get_regulator(struct device *dev, const char *supply)
138{
139 struct device_node *regnode = NULL;
140 char prop_name[32]; /* 32 is max size of property name */
141
142 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
143
144 snprintf(prop_name, 32, "%s-supply", supply);
145 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
146
147 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530148 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530149 prop_name, dev->of_node->full_name);
150 return NULL;
151 }
152 return regnode;
153}
154
Mark Brown6492bc12012-04-19 13:19:07 +0100155static int _regulator_can_change_status(struct regulator_dev *rdev)
156{
157 if (!rdev->constraints)
158 return 0;
159
160 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
161 return 1;
162 else
163 return 0;
164}
165
Liam Girdwood414c70c2008-04-30 15:59:04 +0100166/* Platform voltage constraint check */
167static int regulator_check_voltage(struct regulator_dev *rdev,
168 int *min_uV, int *max_uV)
169{
170 BUG_ON(*min_uV > *max_uV);
171
172 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800173 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100174 return -ENODEV;
175 }
176 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800177 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100178 return -EPERM;
179 }
180
181 if (*max_uV > rdev->constraints->max_uV)
182 *max_uV = rdev->constraints->max_uV;
183 if (*min_uV < rdev->constraints->min_uV)
184 *min_uV = rdev->constraints->min_uV;
185
Mark Brown89f425e2011-07-12 11:20:37 +0900186 if (*min_uV > *max_uV) {
187 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100188 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100189 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900190 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100191
192 return 0;
193}
194
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100195/* Make sure we select a voltage that suits the needs of all
196 * regulator consumers
197 */
198static int regulator_check_consumers(struct regulator_dev *rdev,
199 int *min_uV, int *max_uV)
200{
201 struct regulator *regulator;
202
203 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700204 /*
205 * Assume consumers that didn't say anything are OK
206 * with anything in the constraint range.
207 */
208 if (!regulator->min_uV && !regulator->max_uV)
209 continue;
210
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100211 if (*max_uV > regulator->max_uV)
212 *max_uV = regulator->max_uV;
213 if (*min_uV < regulator->min_uV)
214 *min_uV = regulator->min_uV;
215 }
216
Mark Browndd8004a2012-11-28 17:09:27 +0000217 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800218 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
219 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100220 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000221 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100222
223 return 0;
224}
225
Liam Girdwood414c70c2008-04-30 15:59:04 +0100226/* current constraint check */
227static int regulator_check_current_limit(struct regulator_dev *rdev,
228 int *min_uA, int *max_uA)
229{
230 BUG_ON(*min_uA > *max_uA);
231
232 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800233 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100234 return -ENODEV;
235 }
236 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800237 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238 return -EPERM;
239 }
240
241 if (*max_uA > rdev->constraints->max_uA)
242 *max_uA = rdev->constraints->max_uA;
243 if (*min_uA < rdev->constraints->min_uA)
244 *min_uA = rdev->constraints->min_uA;
245
Mark Brown89f425e2011-07-12 11:20:37 +0900246 if (*min_uA > *max_uA) {
247 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100248 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100249 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900250 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100251
252 return 0;
253}
254
255/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900256static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100257{
Mark Brown2c608232011-03-30 06:29:12 +0900258 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800259 case REGULATOR_MODE_FAST:
260 case REGULATOR_MODE_NORMAL:
261 case REGULATOR_MODE_IDLE:
262 case REGULATOR_MODE_STANDBY:
263 break;
264 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900265 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800266 return -EINVAL;
267 }
268
Liam Girdwood414c70c2008-04-30 15:59:04 +0100269 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800270 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100271 return -ENODEV;
272 }
273 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800274 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100275 return -EPERM;
276 }
Mark Brown2c608232011-03-30 06:29:12 +0900277
278 /* The modes are bitmasks, the most power hungry modes having
279 * the lowest values. If the requested mode isn't supported
280 * try higher modes. */
281 while (*mode) {
282 if (rdev->constraints->valid_modes_mask & *mode)
283 return 0;
284 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100285 }
Mark Brown2c608232011-03-30 06:29:12 +0900286
287 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100288}
289
290/* dynamic regulator mode switching constraint check */
291static int regulator_check_drms(struct regulator_dev *rdev)
292{
293 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800294 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100295 return -ENODEV;
296 }
297 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800298 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100299 return -EPERM;
300 }
301 return 0;
302}
303
Liam Girdwood414c70c2008-04-30 15:59:04 +0100304static ssize_t regulator_uV_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
306{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100307 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100308 ssize_t ret;
309
310 mutex_lock(&rdev->mutex);
311 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
312 mutex_unlock(&rdev->mutex);
313
314 return ret;
315}
David Brownell7ad68e22008-11-11 17:39:02 -0800316static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100317
318static ssize_t regulator_uA_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
320{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100321 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100322
323 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
324}
David Brownell7ad68e22008-11-11 17:39:02 -0800325static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100326
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700327static ssize_t name_show(struct device *dev, struct device_attribute *attr,
328 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100329{
330 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100331
Mark Brown1083c392009-10-22 16:31:32 +0100332 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100333}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700334static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100335
David Brownell4fca9542008-11-11 17:38:53 -0800336static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100337{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100338 switch (mode) {
339 case REGULATOR_MODE_FAST:
340 return sprintf(buf, "fast\n");
341 case REGULATOR_MODE_NORMAL:
342 return sprintf(buf, "normal\n");
343 case REGULATOR_MODE_IDLE:
344 return sprintf(buf, "idle\n");
345 case REGULATOR_MODE_STANDBY:
346 return sprintf(buf, "standby\n");
347 }
348 return sprintf(buf, "unknown\n");
349}
350
David Brownell4fca9542008-11-11 17:38:53 -0800351static ssize_t regulator_opmode_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100353{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100354 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100355
David Brownell4fca9542008-11-11 17:38:53 -0800356 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
357}
David Brownell7ad68e22008-11-11 17:39:02 -0800358static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800359
360static ssize_t regulator_print_state(char *buf, int state)
361{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100362 if (state > 0)
363 return sprintf(buf, "enabled\n");
364 else if (state == 0)
365 return sprintf(buf, "disabled\n");
366 else
367 return sprintf(buf, "unknown\n");
368}
369
David Brownell4fca9542008-11-11 17:38:53 -0800370static ssize_t regulator_state_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
372{
373 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100374 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800375
Mark Brown93325462009-08-03 18:49:56 +0100376 mutex_lock(&rdev->mutex);
377 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
378 mutex_unlock(&rdev->mutex);
379
380 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800381}
David Brownell7ad68e22008-11-11 17:39:02 -0800382static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800383
David Brownell853116a2009-01-14 23:03:17 -0800384static ssize_t regulator_status_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
386{
387 struct regulator_dev *rdev = dev_get_drvdata(dev);
388 int status;
389 char *label;
390
391 status = rdev->desc->ops->get_status(rdev);
392 if (status < 0)
393 return status;
394
395 switch (status) {
396 case REGULATOR_STATUS_OFF:
397 label = "off";
398 break;
399 case REGULATOR_STATUS_ON:
400 label = "on";
401 break;
402 case REGULATOR_STATUS_ERROR:
403 label = "error";
404 break;
405 case REGULATOR_STATUS_FAST:
406 label = "fast";
407 break;
408 case REGULATOR_STATUS_NORMAL:
409 label = "normal";
410 break;
411 case REGULATOR_STATUS_IDLE:
412 label = "idle";
413 break;
414 case REGULATOR_STATUS_STANDBY:
415 label = "standby";
416 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700417 case REGULATOR_STATUS_BYPASS:
418 label = "bypass";
419 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100420 case REGULATOR_STATUS_UNDEFINED:
421 label = "undefined";
422 break;
David Brownell853116a2009-01-14 23:03:17 -0800423 default:
424 return -ERANGE;
425 }
426
427 return sprintf(buf, "%s\n", label);
428}
429static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
430
Liam Girdwood414c70c2008-04-30 15:59:04 +0100431static ssize_t regulator_min_uA_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
433{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100434 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435
436 if (!rdev->constraints)
437 return sprintf(buf, "constraint not defined\n");
438
439 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
440}
David Brownell7ad68e22008-11-11 17:39:02 -0800441static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
443static ssize_t regulator_max_uA_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100446 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447
448 if (!rdev->constraints)
449 return sprintf(buf, "constraint not defined\n");
450
451 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
452}
David Brownell7ad68e22008-11-11 17:39:02 -0800453static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454
455static ssize_t regulator_min_uV_show(struct device *dev,
456 struct device_attribute *attr, char *buf)
457{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100458 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459
460 if (!rdev->constraints)
461 return sprintf(buf, "constraint not defined\n");
462
463 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
464}
David Brownell7ad68e22008-11-11 17:39:02 -0800465static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466
467static ssize_t regulator_max_uV_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
469{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100470 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100471
472 if (!rdev->constraints)
473 return sprintf(buf, "constraint not defined\n");
474
475 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
476}
David Brownell7ad68e22008-11-11 17:39:02 -0800477static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100478
479static ssize_t regulator_total_uA_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
481{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100482 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483 struct regulator *regulator;
484 int uA = 0;
485
486 mutex_lock(&rdev->mutex);
487 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100488 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489 mutex_unlock(&rdev->mutex);
490 return sprintf(buf, "%d\n", uA);
491}
David Brownell7ad68e22008-11-11 17:39:02 -0800492static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700494static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
495 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100496{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100497 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498 return sprintf(buf, "%d\n", rdev->use_count);
499}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700500static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100501
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700502static ssize_t type_show(struct device *dev, struct device_attribute *attr,
503 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100505 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100506
507 switch (rdev->desc->type) {
508 case REGULATOR_VOLTAGE:
509 return sprintf(buf, "voltage\n");
510 case REGULATOR_CURRENT:
511 return sprintf(buf, "current\n");
512 }
513 return sprintf(buf, "unknown\n");
514}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700515static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100516
517static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
518 struct device_attribute *attr, char *buf)
519{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100520 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521
Liam Girdwood414c70c2008-04-30 15:59:04 +0100522 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
523}
David Brownell7ad68e22008-11-11 17:39:02 -0800524static DEVICE_ATTR(suspend_mem_microvolts, 0444,
525 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526
527static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
528 struct device_attribute *attr, char *buf)
529{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100530 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100531
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
533}
David Brownell7ad68e22008-11-11 17:39:02 -0800534static DEVICE_ATTR(suspend_disk_microvolts, 0444,
535 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536
537static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
539{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100540 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100541
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
543}
David Brownell7ad68e22008-11-11 17:39:02 -0800544static DEVICE_ATTR(suspend_standby_microvolts, 0444,
545 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
548 struct device_attribute *attr, char *buf)
549{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100550 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551
David Brownell4fca9542008-11-11 17:38:53 -0800552 return regulator_print_opmode(buf,
553 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100554}
David Brownell7ad68e22008-11-11 17:39:02 -0800555static DEVICE_ATTR(suspend_mem_mode, 0444,
556 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557
558static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
560{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100561 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562
David Brownell4fca9542008-11-11 17:38:53 -0800563 return regulator_print_opmode(buf,
564 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100565}
David Brownell7ad68e22008-11-11 17:39:02 -0800566static DEVICE_ATTR(suspend_disk_mode, 0444,
567 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568
569static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
571{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100572 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100573
David Brownell4fca9542008-11-11 17:38:53 -0800574 return regulator_print_opmode(buf,
575 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100576}
David Brownell7ad68e22008-11-11 17:39:02 -0800577static DEVICE_ATTR(suspend_standby_mode, 0444,
578 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100579
580static ssize_t regulator_suspend_mem_state_show(struct device *dev,
581 struct device_attribute *attr, char *buf)
582{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100583 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100584
David Brownell4fca9542008-11-11 17:38:53 -0800585 return regulator_print_state(buf,
586 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100587}
David Brownell7ad68e22008-11-11 17:39:02 -0800588static DEVICE_ATTR(suspend_mem_state, 0444,
589 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590
591static ssize_t regulator_suspend_disk_state_show(struct device *dev,
592 struct device_attribute *attr, char *buf)
593{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100594 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100595
David Brownell4fca9542008-11-11 17:38:53 -0800596 return regulator_print_state(buf,
597 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100598}
David Brownell7ad68e22008-11-11 17:39:02 -0800599static DEVICE_ATTR(suspend_disk_state, 0444,
600 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100601
602static ssize_t regulator_suspend_standby_state_show(struct device *dev,
603 struct device_attribute *attr, char *buf)
604{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100605 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100606
David Brownell4fca9542008-11-11 17:38:53 -0800607 return regulator_print_state(buf,
608 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100609}
David Brownell7ad68e22008-11-11 17:39:02 -0800610static DEVICE_ATTR(suspend_standby_state, 0444,
611 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100612
Mark Brownf59c8f92012-08-31 10:36:37 -0700613static ssize_t regulator_bypass_show(struct device *dev,
614 struct device_attribute *attr, char *buf)
615{
616 struct regulator_dev *rdev = dev_get_drvdata(dev);
617 const char *report;
618 bool bypass;
619 int ret;
620
621 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
622
623 if (ret != 0)
624 report = "unknown";
625 else if (bypass)
626 report = "enabled";
627 else
628 report = "disabled";
629
630 return sprintf(buf, "%s\n", report);
631}
632static DEVICE_ATTR(bypass, 0444,
633 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800634
635/*
636 * These are the only attributes are present for all regulators.
637 * Other attributes are a function of regulator functionality.
638 */
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700639static struct attribute *regulator_dev_attrs[] = {
640 &dev_attr_name.attr,
641 &dev_attr_num_users.attr,
642 &dev_attr_type.attr,
643 NULL,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100644};
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700645ATTRIBUTE_GROUPS(regulator_dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100646
647static void regulator_dev_release(struct device *dev)
648{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100649 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100650 kfree(rdev);
651}
652
653static struct class regulator_class = {
654 .name = "regulator",
655 .dev_release = regulator_dev_release,
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700656 .dev_groups = regulator_dev_groups,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100657};
658
659/* Calculate the new optimum regulator operating mode based on the new total
660 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800661static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100662{
663 struct regulator *sibling;
664 int current_uA = 0, output_uV, input_uV, err;
665 unsigned int mode;
666
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800667 /*
668 * first check to see if we can set modes at all, otherwise just
669 * tell the consumer everything is OK.
670 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100671 err = regulator_check_drms(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800672 if (err < 0)
673 return 0;
674
675 if (!rdev->desc->ops->get_optimum_mode)
676 return 0;
677
678 if (!rdev->desc->ops->set_mode)
679 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100680
681 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000682 output_uV = _regulator_get_voltage(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800683 if (output_uV <= 0) {
684 rdev_err(rdev, "invalid output voltage found\n");
685 return -EINVAL;
686 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100687
688 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000689 input_uV = 0;
690 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800691 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000692 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100693 input_uV = rdev->constraints->input_uV;
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800694 if (input_uV <= 0) {
695 rdev_err(rdev, "invalid input voltage found\n");
696 return -EINVAL;
697 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100698
699 /* calc total requested load */
700 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100701 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100702
703 /* now get the optimum mode for our new total regulator load */
704 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
705 output_uV, current_uA);
706
707 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900708 err = regulator_mode_constrain(rdev, &mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800709 if (err < 0) {
710 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
711 current_uA, input_uV, output_uV);
712 return err;
713 }
714
715 err = rdev->desc->ops->set_mode(rdev, mode);
716 if (err < 0)
717 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
718
719 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100720}
721
722static int suspend_set_state(struct regulator_dev *rdev,
723 struct regulator_state *rstate)
724{
725 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100726
727 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800728 * only warn if the driver implements set_suspend_voltage or
729 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100730 */
731 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800732 if (rdev->desc->ops->set_suspend_voltage ||
733 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800734 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100735 return 0;
736 }
737
738 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800739 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100740 return -EINVAL;
741 }
742
Axel Lin8ac0e952012-04-14 09:14:34 +0800743 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100744 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800745 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100746 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800747 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
748 ret = 0;
749
Liam Girdwood414c70c2008-04-30 15:59:04 +0100750 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800751 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100752 return ret;
753 }
754
755 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
756 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
757 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800758 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100759 return ret;
760 }
761 }
762
763 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
764 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
765 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800766 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100767 return ret;
768 }
769 }
770 return ret;
771}
772
773/* locks held by caller */
774static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
775{
776 if (!rdev->constraints)
777 return -EINVAL;
778
779 switch (state) {
780 case PM_SUSPEND_STANDBY:
781 return suspend_set_state(rdev,
782 &rdev->constraints->state_standby);
783 case PM_SUSPEND_MEM:
784 return suspend_set_state(rdev,
785 &rdev->constraints->state_mem);
786 case PM_SUSPEND_MAX:
787 return suspend_set_state(rdev,
788 &rdev->constraints->state_disk);
789 default:
790 return -EINVAL;
791 }
792}
793
794static void print_constraints(struct regulator_dev *rdev)
795{
796 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000797 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100798 int count = 0;
799 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100800
Mark Brown8f031b42009-10-22 16:31:31 +0100801 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100802 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100803 count += sprintf(buf + count, "%d mV ",
804 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100805 else
Mark Brown8f031b42009-10-22 16:31:31 +0100806 count += sprintf(buf + count, "%d <--> %d mV ",
807 constraints->min_uV / 1000,
808 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100809 }
Mark Brown8f031b42009-10-22 16:31:31 +0100810
811 if (!constraints->min_uV ||
812 constraints->min_uV != constraints->max_uV) {
813 ret = _regulator_get_voltage(rdev);
814 if (ret > 0)
815 count += sprintf(buf + count, "at %d mV ", ret / 1000);
816 }
817
Mark Brownbf5892a2011-05-08 22:13:37 +0100818 if (constraints->uV_offset)
819 count += sprintf(buf, "%dmV offset ",
820 constraints->uV_offset / 1000);
821
Mark Brown8f031b42009-10-22 16:31:31 +0100822 if (constraints->min_uA && constraints->max_uA) {
823 if (constraints->min_uA == constraints->max_uA)
824 count += sprintf(buf + count, "%d mA ",
825 constraints->min_uA / 1000);
826 else
827 count += sprintf(buf + count, "%d <--> %d mA ",
828 constraints->min_uA / 1000,
829 constraints->max_uA / 1000);
830 }
831
832 if (!constraints->min_uA ||
833 constraints->min_uA != constraints->max_uA) {
834 ret = _regulator_get_current_limit(rdev);
835 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400836 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100837 }
838
Liam Girdwood414c70c2008-04-30 15:59:04 +0100839 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
840 count += sprintf(buf + count, "fast ");
841 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
842 count += sprintf(buf + count, "normal ");
843 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
844 count += sprintf(buf + count, "idle ");
845 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
846 count += sprintf(buf + count, "standby");
847
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200848 if (!count)
849 sprintf(buf, "no parameters");
850
Mark Brown194dbae2014-10-31 19:11:59 +0000851 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000852
853 if ((constraints->min_uV != constraints->max_uV) &&
854 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
855 rdev_warn(rdev,
856 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100857}
858
Mark Browne79055d2009-10-19 15:53:50 +0100859static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100860 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100861{
Guodong Xu272e2312014-08-13 19:33:38 +0800862 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100863 int ret;
864
865 /* do we need to apply the constraint voltage */
866 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000867 rdev->constraints->min_uV == rdev->constraints->max_uV) {
Alban Bedel064d5cd2014-05-20 12:12:16 +0200868 int current_uV = _regulator_get_voltage(rdev);
869 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500870 rdev_err(rdev,
871 "failed to get the current voltage(%d)\n",
872 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200873 return current_uV;
874 }
875 if (current_uV < rdev->constraints->min_uV ||
876 current_uV > rdev->constraints->max_uV) {
877 ret = _regulator_do_set_voltage(
878 rdev, rdev->constraints->min_uV,
879 rdev->constraints->max_uV);
880 if (ret < 0) {
881 rdev_err(rdev,
Nishanth Menon69d58832014-06-04 14:53:25 -0500882 "failed to apply %duV constraint(%d)\n",
883 rdev->constraints->min_uV, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200884 return ret;
885 }
Mark Brown75790252010-12-12 14:25:50 +0000886 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100887 }
Mark Browne79055d2009-10-19 15:53:50 +0100888
889 /* constrain machine-level voltage specs to fit
890 * the actual range supported by this regulator.
891 */
892 if (ops->list_voltage && rdev->desc->n_voltages) {
893 int count = rdev->desc->n_voltages;
894 int i;
895 int min_uV = INT_MAX;
896 int max_uV = INT_MIN;
897 int cmin = constraints->min_uV;
898 int cmax = constraints->max_uV;
899
900 /* it's safe to autoconfigure fixed-voltage supplies
901 and the constraints are used by list_voltage. */
902 if (count == 1 && !cmin) {
903 cmin = 1;
904 cmax = INT_MAX;
905 constraints->min_uV = cmin;
906 constraints->max_uV = cmax;
907 }
908
909 /* voltage constraints are optional */
910 if ((cmin == 0) && (cmax == 0))
911 return 0;
912
913 /* else require explicit machine-level constraints */
914 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800915 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100916 return -EINVAL;
917 }
918
919 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
920 for (i = 0; i < count; i++) {
921 int value;
922
923 value = ops->list_voltage(rdev, i);
924 if (value <= 0)
925 continue;
926
927 /* maybe adjust [min_uV..max_uV] */
928 if (value >= cmin && value < min_uV)
929 min_uV = value;
930 if (value <= cmax && value > max_uV)
931 max_uV = value;
932 }
933
934 /* final: [min_uV..max_uV] valid iff constraints valid */
935 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000936 rdev_err(rdev,
937 "unsupportable voltage constraints %u-%uuV\n",
938 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100939 return -EINVAL;
940 }
941
942 /* use regulator's subset of machine constraints */
943 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800944 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
945 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100946 constraints->min_uV = min_uV;
947 }
948 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800949 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
950 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100951 constraints->max_uV = max_uV;
952 }
953 }
954
955 return 0;
956}
957
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530958static int machine_constraints_current(struct regulator_dev *rdev,
959 struct regulation_constraints *constraints)
960{
Guodong Xu272e2312014-08-13 19:33:38 +0800961 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530962 int ret;
963
964 if (!constraints->min_uA && !constraints->max_uA)
965 return 0;
966
967 if (constraints->min_uA > constraints->max_uA) {
968 rdev_err(rdev, "Invalid current constraints\n");
969 return -EINVAL;
970 }
971
972 if (!ops->set_current_limit || !ops->get_current_limit) {
973 rdev_warn(rdev, "Operation of current configuration missing\n");
974 return 0;
975 }
976
977 /* Set regulator current in constraints range */
978 ret = ops->set_current_limit(rdev, constraints->min_uA,
979 constraints->max_uA);
980 if (ret < 0) {
981 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
982 return ret;
983 }
984
985 return 0;
986}
987
Markus Pargmann30c21972014-02-20 17:36:03 +0100988static int _regulator_do_enable(struct regulator_dev *rdev);
989
Liam Girdwooda5766f12008-10-10 13:22:20 +0100990/**
991 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000992 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000993 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100994 *
995 * Allows platform initialisation code to define and constrain
996 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
997 * Constraints *must* be set by platform code in order for some
998 * regulator operations to proceed i.e. set_voltage, set_current_limit,
999 * set_mode.
1000 */
1001static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +00001002 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001003{
1004 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +08001005 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +01001006
Mark Brown9a8f5e02011-11-29 18:11:19 +00001007 if (constraints)
1008 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1009 GFP_KERNEL);
1010 else
1011 rdev->constraints = kzalloc(sizeof(*constraints),
1012 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +00001013 if (!rdev->constraints)
1014 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +01001015
Mark Brownf8c12fe2010-11-29 15:55:17 +00001016 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +01001017 if (ret != 0)
1018 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -08001019
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301020 ret = machine_constraints_current(rdev, rdev->constraints);
1021 if (ret != 0)
1022 goto out;
1023
Liam Girdwooda5766f12008-10-10 13:22:20 +01001024 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001025 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +00001026 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001027 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001028 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +01001029 goto out;
1030 }
1031 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001032
Mark Brown9a8f5e02011-11-29 18:11:19 +00001033 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001034 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001035 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +00001036 ret = -EINVAL;
1037 goto out;
1038 }
1039
Mark Brownf8c12fe2010-11-29 15:55:17 +00001040 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001041 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001042 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +00001043 goto out;
1044 }
1045 }
1046
Mark Browncacf90f2009-03-02 16:32:46 +00001047 /* If the constraints say the regulator should be on at this point
1048 * and we have control then make sure it is enabled.
1049 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001050 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1051 ret = _regulator_do_enable(rdev);
1052 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001053 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +01001054 goto out;
1055 }
1056 }
1057
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301058 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1059 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301060 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1061 if (ret < 0) {
1062 rdev_err(rdev, "failed to set ramp_delay\n");
1063 goto out;
1064 }
1065 }
1066
Liam Girdwooda5766f12008-10-10 13:22:20 +01001067 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001068 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001069out:
Axel Lin1a6958e72011-07-15 10:50:43 +08001070 kfree(rdev->constraints);
1071 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001072 return ret;
1073}
1074
1075/**
1076 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001077 * @rdev: regulator name
1078 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001079 *
1080 * Called by platform initialisation code to set the supply regulator for this
1081 * regulator. This ensures that a regulators supply will also be enabled by the
1082 * core if it's child is enabled.
1083 */
1084static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001085 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001086{
1087 int err;
1088
Mark Brown3801b862011-06-09 16:22:22 +01001089 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1090
1091 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001092 if (rdev->supply == NULL) {
1093 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001094 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001095 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301096 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001097
1098 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001099}
1100
1101/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001102 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001103 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001104 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001105 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001106 *
1107 * Allows platform initialisation code to map physical regulator
1108 * sources to symbolic names for supplies for use by devices. Devices
1109 * should use these symbolic names to request regulators, avoiding the
1110 * need to provide board-specific regulator names as platform data.
1111 */
1112static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001113 const char *consumer_dev_name,
1114 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001115{
1116 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001117 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001118
1119 if (supply == NULL)
1120 return -EINVAL;
1121
Mark Brown9ed20992009-07-21 16:00:26 +01001122 if (consumer_dev_name != NULL)
1123 has_dev = 1;
1124 else
1125 has_dev = 0;
1126
David Brownell6001e132008-12-31 12:54:19 +00001127 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001128 if (node->dev_name && consumer_dev_name) {
1129 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1130 continue;
1131 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001132 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001133 }
1134
David Brownell6001e132008-12-31 12:54:19 +00001135 if (strcmp(node->supply, supply) != 0)
1136 continue;
1137
Mark Brown737f3602012-02-02 00:10:51 +00001138 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1139 consumer_dev_name,
1140 dev_name(&node->regulator->dev),
1141 node->regulator->desc->name,
1142 supply,
1143 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001144 return -EBUSY;
1145 }
1146
Mark Brown9ed20992009-07-21 16:00:26 +01001147 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001148 if (node == NULL)
1149 return -ENOMEM;
1150
1151 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001152 node->supply = supply;
1153
Mark Brown9ed20992009-07-21 16:00:26 +01001154 if (has_dev) {
1155 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1156 if (node->dev_name == NULL) {
1157 kfree(node);
1158 return -ENOMEM;
1159 }
Mark Brown40f92442009-06-17 17:56:39 +01001160 }
1161
Liam Girdwooda5766f12008-10-10 13:22:20 +01001162 list_add(&node->list, &regulator_map_list);
1163 return 0;
1164}
1165
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001166static void unset_regulator_supplies(struct regulator_dev *rdev)
1167{
1168 struct regulator_map *node, *n;
1169
1170 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1171 if (rdev == node->regulator) {
1172 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001173 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001174 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001175 }
1176 }
1177}
1178
Mark Brownf5726ae2011-06-09 16:22:20 +01001179#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001180
1181static struct regulator *create_regulator(struct regulator_dev *rdev,
1182 struct device *dev,
1183 const char *supply_name)
1184{
1185 struct regulator *regulator;
1186 char buf[REG_STR_SIZE];
1187 int err, size;
1188
1189 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1190 if (regulator == NULL)
1191 return NULL;
1192
1193 mutex_lock(&rdev->mutex);
1194 regulator->rdev = rdev;
1195 list_add(&regulator->list, &rdev->consumer_list);
1196
1197 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001198 regulator->dev = dev;
1199
Mark Brown222cc7b2012-06-22 11:39:16 +01001200 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001201 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1202 dev->kobj.name, supply_name);
1203 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001204 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001205
1206 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1207 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001208 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001209
1210 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1211 buf);
1212 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001213 rdev_warn(rdev, "could not add device link %s err %d\n",
1214 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001215 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001216 }
Mark Brown5de70512011-06-19 13:33:16 +01001217 } else {
1218 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1219 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001220 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001221 }
Mark Brown5de70512011-06-19 13:33:16 +01001222
Mark Brown5de70512011-06-19 13:33:16 +01001223 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1224 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001225 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001226 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001227 } else {
1228 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1229 &regulator->uA_load);
1230 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1231 &regulator->min_uV);
1232 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1233 &regulator->max_uV);
1234 }
Mark Brown5de70512011-06-19 13:33:16 +01001235
Mark Brown6492bc12012-04-19 13:19:07 +01001236 /*
1237 * Check now if the regulator is an always on regulator - if
1238 * it is then we don't need to do nearly so much work for
1239 * enable/disable calls.
1240 */
1241 if (!_regulator_can_change_status(rdev) &&
1242 _regulator_is_enabled(rdev))
1243 regulator->always_on = true;
1244
Liam Girdwood414c70c2008-04-30 15:59:04 +01001245 mutex_unlock(&rdev->mutex);
1246 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001247overflow_err:
1248 list_del(&regulator->list);
1249 kfree(regulator);
1250 mutex_unlock(&rdev->mutex);
1251 return NULL;
1252}
1253
Mark Brown31aae2b2009-12-21 12:21:52 +00001254static int _regulator_get_enable_time(struct regulator_dev *rdev)
1255{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301256 if (rdev->constraints && rdev->constraints->enable_time)
1257 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001258 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001259 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001260 return rdev->desc->ops->enable_time(rdev);
1261}
1262
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001263static struct regulator_supply_alias *regulator_find_supply_alias(
1264 struct device *dev, const char *supply)
1265{
1266 struct regulator_supply_alias *map;
1267
1268 list_for_each_entry(map, &regulator_supply_alias_list, list)
1269 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1270 return map;
1271
1272 return NULL;
1273}
1274
1275static void regulator_supply_alias(struct device **dev, const char **supply)
1276{
1277 struct regulator_supply_alias *map;
1278
1279 map = regulator_find_supply_alias(*dev, *supply);
1280 if (map) {
1281 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1282 *supply, map->alias_supply,
1283 dev_name(map->alias_dev));
1284 *dev = map->alias_dev;
1285 *supply = map->alias_supply;
1286 }
1287}
1288
Rajendra Nayak69511a42011-11-18 16:47:20 +05301289static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001290 const char *supply,
1291 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301292{
1293 struct regulator_dev *r;
1294 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001295 struct regulator_map *map;
1296 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301297
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001298 regulator_supply_alias(&dev, &supply);
1299
Rajendra Nayak69511a42011-11-18 16:47:20 +05301300 /* first do a dt based lookup */
1301 if (dev && dev->of_node) {
1302 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001303 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301304 list_for_each_entry(r, &regulator_list, list)
1305 if (r->dev.parent &&
1306 node == r->dev.of_node)
1307 return r;
Mark Brown317b5682014-01-27 17:34:07 +00001308 *ret = -EPROBE_DEFER;
1309 return NULL;
Mark Brown6d191a52012-03-29 14:19:02 +01001310 } else {
1311 /*
1312 * If we couldn't even get the node then it's
1313 * not just that the device didn't register
1314 * yet, there's no node and we'll never
1315 * succeed.
1316 */
1317 *ret = -ENODEV;
1318 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301319 }
1320
1321 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001322 if (dev)
1323 devname = dev_name(dev);
1324
Rajendra Nayak69511a42011-11-18 16:47:20 +05301325 list_for_each_entry(r, &regulator_list, list)
1326 if (strcmp(rdev_get_name(r), supply) == 0)
1327 return r;
1328
Mark Brown576ca4362012-03-30 12:24:37 +01001329 list_for_each_entry(map, &regulator_map_list, list) {
1330 /* If the mapping has a device set up it must match */
1331 if (map->dev_name &&
1332 (!devname || strcmp(map->dev_name, devname)))
1333 continue;
1334
1335 if (strcmp(map->supply, supply) == 0)
1336 return map->regulator;
1337 }
1338
1339
Rajendra Nayak69511a42011-11-18 16:47:20 +05301340 return NULL;
1341}
1342
Mark Brown5ffbd132009-07-21 16:00:23 +01001343/* Internal regulator request function */
1344static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown4ddfebd2013-09-13 19:50:37 +01001345 bool exclusive, bool allow_dummy)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001346{
1347 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001348 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001349 const char *devname = NULL;
Mark Brown317b5682014-01-27 17:34:07 +00001350 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001351
1352 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001353 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001354 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001355 }
1356
Mark Brown40f92442009-06-17 17:56:39 +01001357 if (dev)
1358 devname = dev_name(dev);
1359
Mark Brown317b5682014-01-27 17:34:07 +00001360 if (have_full_constraints())
1361 ret = -ENODEV;
1362 else
1363 ret = -EPROBE_DEFER;
1364
Liam Girdwood414c70c2008-04-30 15:59:04 +01001365 mutex_lock(&regulator_list_mutex);
1366
Mark Brown6d191a52012-03-29 14:19:02 +01001367 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301368 if (rdev)
1369 goto found;
1370
Mark Brownef60abb2013-09-23 16:12:52 +01001371 regulator = ERR_PTR(ret);
1372
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001373 /*
1374 * If we have return value from dev_lookup fail, we do not expect to
1375 * succeed, so, quit with appropriate error value
1376 */
Jingoo Han0d25d092014-01-08 10:02:16 +09001377 if (ret && ret != -ENODEV)
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001378 goto out;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001379
Mark Brown34abbd62010-02-12 10:18:08 +00001380 if (!devname)
1381 devname = "deviceless";
1382
Mark Brown4ddfebd2013-09-13 19:50:37 +01001383 /*
1384 * Assume that a regulator is physically present and enabled
1385 * even if it isn't hooked up and just provide a dummy.
Mark Brown34abbd62010-02-12 10:18:08 +00001386 */
Mark Brown87b28412013-11-27 16:13:10 +00001387 if (have_full_constraints() && allow_dummy) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001388 pr_warn("%s supply %s not found, using dummy regulator\n",
1389 devname, id);
Mark Brown4ddfebd2013-09-13 19:50:37 +01001390
Mark Brown34abbd62010-02-12 10:18:08 +00001391 rdev = dummy_regulator_rdev;
1392 goto found;
Hans de Goede07817192013-12-17 16:24:57 +01001393 /* Don't log an error when called from regulator_get_optional() */
1394 } else if (!have_full_constraints() || exclusive) {
Shuah Khanacc3d5c2014-02-20 12:58:15 -07001395 dev_warn(dev, "dummy supplies not allowed\n");
Mark Brown34abbd62010-02-12 10:18:08 +00001396 }
Mark Brown34abbd62010-02-12 10:18:08 +00001397
Liam Girdwood414c70c2008-04-30 15:59:04 +01001398 mutex_unlock(&regulator_list_mutex);
1399 return regulator;
1400
1401found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001402 if (rdev->exclusive) {
1403 regulator = ERR_PTR(-EPERM);
1404 goto out;
1405 }
1406
1407 if (exclusive && rdev->open_count) {
1408 regulator = ERR_PTR(-EBUSY);
1409 goto out;
1410 }
1411
Liam Girdwooda5766f12008-10-10 13:22:20 +01001412 if (!try_module_get(rdev->owner))
1413 goto out;
1414
Liam Girdwood414c70c2008-04-30 15:59:04 +01001415 regulator = create_regulator(rdev, dev, id);
1416 if (regulator == NULL) {
1417 regulator = ERR_PTR(-ENOMEM);
1418 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001419 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001420 }
1421
Mark Brown5ffbd132009-07-21 16:00:23 +01001422 rdev->open_count++;
1423 if (exclusive) {
1424 rdev->exclusive = 1;
1425
1426 ret = _regulator_is_enabled(rdev);
1427 if (ret > 0)
1428 rdev->use_count = 1;
1429 else
1430 rdev->use_count = 0;
1431 }
1432
Liam Girdwooda5766f12008-10-10 13:22:20 +01001433out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001434 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001435
Liam Girdwood414c70c2008-04-30 15:59:04 +01001436 return regulator;
1437}
Mark Brown5ffbd132009-07-21 16:00:23 +01001438
1439/**
1440 * regulator_get - lookup and obtain a reference to a regulator.
1441 * @dev: device for regulator "consumer"
1442 * @id: Supply name or regulator ID.
1443 *
1444 * Returns a struct regulator corresponding to the regulator producer,
1445 * or IS_ERR() condition containing errno.
1446 *
1447 * Use of supply names configured via regulator_set_device_supply() is
1448 * strongly encouraged. It is recommended that the supply name used
1449 * should match the name used for the supply and/or the relevant
1450 * device pins in the datasheet.
1451 */
1452struct regulator *regulator_get(struct device *dev, const char *id)
1453{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001454 return _regulator_get(dev, id, false, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001455}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001456EXPORT_SYMBOL_GPL(regulator_get);
1457
Stephen Boyd070b9072012-01-16 19:39:58 -08001458/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001459 * regulator_get_exclusive - obtain exclusive access to a regulator.
1460 * @dev: device for regulator "consumer"
1461 * @id: Supply name or regulator ID.
1462 *
1463 * Returns a struct regulator corresponding to the regulator producer,
1464 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001465 * unable to obtain this regulator while this reference is held and the
1466 * use count for the regulator will be initialised to reflect the current
1467 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001468 *
1469 * This is intended for use by consumers which cannot tolerate shared
1470 * use of the regulator such as those which need to force the
1471 * regulator off for correct operation of the hardware they are
1472 * controlling.
1473 *
1474 * Use of supply names configured via regulator_set_device_supply() is
1475 * strongly encouraged. It is recommended that the supply name used
1476 * should match the name used for the supply and/or the relevant
1477 * device pins in the datasheet.
1478 */
1479struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1480{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001481 return _regulator_get(dev, id, true, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001482}
1483EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1484
Mark Brownde1dd9f2013-07-29 21:42:42 +01001485/**
1486 * regulator_get_optional - obtain optional access to a regulator.
1487 * @dev: device for regulator "consumer"
1488 * @id: Supply name or regulator ID.
1489 *
1490 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001491 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001492 *
1493 * This is intended for use by consumers for devices which can have
1494 * some supplies unconnected in normal use, such as some MMC devices.
1495 * It can allow the regulator core to provide stub supplies for other
1496 * supplies requested using normal regulator_get() calls without
1497 * disrupting the operation of drivers that can handle absent
1498 * supplies.
1499 *
1500 * Use of supply names configured via regulator_set_device_supply() is
1501 * strongly encouraged. It is recommended that the supply name used
1502 * should match the name used for the supply and/or the relevant
1503 * device pins in the datasheet.
1504 */
1505struct regulator *regulator_get_optional(struct device *dev, const char *id)
1506{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001507 return _regulator_get(dev, id, false, false);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001508}
1509EXPORT_SYMBOL_GPL(regulator_get_optional);
1510
Charles Keepax23ff2f02012-11-14 09:39:31 +00001511/* Locks held by regulator_put() */
1512static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001513{
1514 struct regulator_dev *rdev;
1515
1516 if (regulator == NULL || IS_ERR(regulator))
1517 return;
1518
Liam Girdwood414c70c2008-04-30 15:59:04 +01001519 rdev = regulator->rdev;
1520
Mark Brown5de70512011-06-19 13:33:16 +01001521 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001522
Liam Girdwood414c70c2008-04-30 15:59:04 +01001523 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001524 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001525 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Mark Brown5de70512011-06-19 13:33:16 +01001526 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001527 list_del(&regulator->list);
1528 kfree(regulator);
1529
Mark Brown5ffbd132009-07-21 16:00:23 +01001530 rdev->open_count--;
1531 rdev->exclusive = 0;
1532
Liam Girdwood414c70c2008-04-30 15:59:04 +01001533 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001534}
1535
1536/**
1537 * regulator_put - "free" the regulator source
1538 * @regulator: regulator source
1539 *
1540 * Note: drivers must ensure that all regulator_enable calls made on this
1541 * regulator source are balanced by regulator_disable calls prior to calling
1542 * this function.
1543 */
1544void regulator_put(struct regulator *regulator)
1545{
1546 mutex_lock(&regulator_list_mutex);
1547 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001548 mutex_unlock(&regulator_list_mutex);
1549}
1550EXPORT_SYMBOL_GPL(regulator_put);
1551
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001552/**
1553 * regulator_register_supply_alias - Provide device alias for supply lookup
1554 *
1555 * @dev: device that will be given as the regulator "consumer"
1556 * @id: Supply name or regulator ID
1557 * @alias_dev: device that should be used to lookup the supply
1558 * @alias_id: Supply name or regulator ID that should be used to lookup the
1559 * supply
1560 *
1561 * All lookups for id on dev will instead be conducted for alias_id on
1562 * alias_dev.
1563 */
1564int regulator_register_supply_alias(struct device *dev, const char *id,
1565 struct device *alias_dev,
1566 const char *alias_id)
1567{
1568 struct regulator_supply_alias *map;
1569
1570 map = regulator_find_supply_alias(dev, id);
1571 if (map)
1572 return -EEXIST;
1573
1574 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1575 if (!map)
1576 return -ENOMEM;
1577
1578 map->src_dev = dev;
1579 map->src_supply = id;
1580 map->alias_dev = alias_dev;
1581 map->alias_supply = alias_id;
1582
1583 list_add(&map->list, &regulator_supply_alias_list);
1584
1585 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1586 id, dev_name(dev), alias_id, dev_name(alias_dev));
1587
1588 return 0;
1589}
1590EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1591
1592/**
1593 * regulator_unregister_supply_alias - Remove device alias
1594 *
1595 * @dev: device that will be given as the regulator "consumer"
1596 * @id: Supply name or regulator ID
1597 *
1598 * Remove a lookup alias if one exists for id on dev.
1599 */
1600void regulator_unregister_supply_alias(struct device *dev, const char *id)
1601{
1602 struct regulator_supply_alias *map;
1603
1604 map = regulator_find_supply_alias(dev, id);
1605 if (map) {
1606 list_del(&map->list);
1607 kfree(map);
1608 }
1609}
1610EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1611
1612/**
1613 * regulator_bulk_register_supply_alias - register multiple aliases
1614 *
1615 * @dev: device that will be given as the regulator "consumer"
1616 * @id: List of supply names or regulator IDs
1617 * @alias_dev: device that should be used to lookup the supply
1618 * @alias_id: List of supply names or regulator IDs that should be used to
1619 * lookup the supply
1620 * @num_id: Number of aliases to register
1621 *
1622 * @return 0 on success, an errno on failure.
1623 *
1624 * This helper function allows drivers to register several supply
1625 * aliases in one operation. If any of the aliases cannot be
1626 * registered any aliases that were registered will be removed
1627 * before returning to the caller.
1628 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001629int regulator_bulk_register_supply_alias(struct device *dev,
1630 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001631 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001632 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001633 int num_id)
1634{
1635 int i;
1636 int ret;
1637
1638 for (i = 0; i < num_id; ++i) {
1639 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1640 alias_id[i]);
1641 if (ret < 0)
1642 goto err;
1643 }
1644
1645 return 0;
1646
1647err:
1648 dev_err(dev,
1649 "Failed to create supply alias %s,%s -> %s,%s\n",
1650 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1651
1652 while (--i >= 0)
1653 regulator_unregister_supply_alias(dev, id[i]);
1654
1655 return ret;
1656}
1657EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1658
1659/**
1660 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1661 *
1662 * @dev: device that will be given as the regulator "consumer"
1663 * @id: List of supply names or regulator IDs
1664 * @num_id: Number of aliases to unregister
1665 *
1666 * This helper function allows drivers to unregister several supply
1667 * aliases in one operation.
1668 */
1669void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001670 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001671 int num_id)
1672{
1673 int i;
1674
1675 for (i = 0; i < num_id; ++i)
1676 regulator_unregister_supply_alias(dev, id[i]);
1677}
1678EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1679
1680
Kim, Milof19b00d2013-02-18 06:50:39 +00001681/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1682static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1683 const struct regulator_config *config)
1684{
1685 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001686 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001687 int ret;
1688
Russell King778b28b2014-06-29 17:55:54 +01001689 gpiod = gpio_to_desc(config->ena_gpio);
1690
Kim, Milof19b00d2013-02-18 06:50:39 +00001691 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001692 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001693 rdev_dbg(rdev, "GPIO %d is already used\n",
1694 config->ena_gpio);
1695 goto update_ena_gpio_to_rdev;
1696 }
1697 }
1698
1699 ret = gpio_request_one(config->ena_gpio,
1700 GPIOF_DIR_OUT | config->ena_gpio_flags,
1701 rdev_get_name(rdev));
1702 if (ret)
1703 return ret;
1704
1705 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1706 if (pin == NULL) {
1707 gpio_free(config->ena_gpio);
1708 return -ENOMEM;
1709 }
1710
Russell King778b28b2014-06-29 17:55:54 +01001711 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001712 pin->ena_gpio_invert = config->ena_gpio_invert;
1713 list_add(&pin->list, &regulator_ena_gpio_list);
1714
1715update_ena_gpio_to_rdev:
1716 pin->request_count++;
1717 rdev->ena_pin = pin;
1718 return 0;
1719}
1720
1721static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1722{
1723 struct regulator_enable_gpio *pin, *n;
1724
1725 if (!rdev->ena_pin)
1726 return;
1727
1728 /* Free the GPIO only in case of no use */
1729 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001730 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001731 if (pin->request_count <= 1) {
1732 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01001733 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00001734 list_del(&pin->list);
1735 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09001736 rdev->ena_pin = NULL;
1737 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00001738 } else {
1739 pin->request_count--;
1740 }
1741 }
1742 }
1743}
1744
Kim, Milo967cfb12013-02-18 06:50:48 +00001745/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001746 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1747 * @rdev: regulator_dev structure
1748 * @enable: enable GPIO at initial use?
1749 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001750 * GPIO is enabled in case of initial use. (enable_count is 0)
1751 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1752 */
1753static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1754{
1755 struct regulator_enable_gpio *pin = rdev->ena_pin;
1756
1757 if (!pin)
1758 return -EINVAL;
1759
1760 if (enable) {
1761 /* Enable GPIO at initial use */
1762 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01001763 gpiod_set_value_cansleep(pin->gpiod,
1764 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001765
1766 pin->enable_count++;
1767 } else {
1768 if (pin->enable_count > 1) {
1769 pin->enable_count--;
1770 return 0;
1771 }
1772
1773 /* Disable GPIO if not used */
1774 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01001775 gpiod_set_value_cansleep(pin->gpiod,
1776 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001777 pin->enable_count = 0;
1778 }
1779 }
1780
1781 return 0;
1782}
1783
Guodong Xu79fd1142014-08-13 19:33:39 +08001784/**
1785 * _regulator_enable_delay - a delay helper function
1786 * @delay: time to delay in microseconds
1787 *
1788 * Delay for the requested amount of time as per the guidelines in:
1789 *
1790 * Documentation/timers/timers-howto.txt
1791 *
1792 * The assumption here is that regulators will never be enabled in
1793 * atomic context and therefore sleeping functions can be used.
1794 */
1795static void _regulator_enable_delay(unsigned int delay)
1796{
1797 unsigned int ms = delay / 1000;
1798 unsigned int us = delay % 1000;
1799
1800 if (ms > 0) {
1801 /*
1802 * For small enough values, handle super-millisecond
1803 * delays in the usleep_range() call below.
1804 */
1805 if (ms < 20)
1806 us += ms * 1000;
1807 else
1808 msleep(ms);
1809 }
1810
1811 /*
1812 * Give the scheduler some room to coalesce with any other
1813 * wakeup sources. For delays shorter than 10 us, don't even
1814 * bother setting up high-resolution timers and just busy-
1815 * loop.
1816 */
1817 if (us >= 10)
1818 usleep_range(us, us + 100);
1819 else
1820 udelay(us);
1821}
1822
Mark Brown5c5659d2012-06-27 13:21:26 +01001823static int _regulator_do_enable(struct regulator_dev *rdev)
1824{
1825 int ret, delay;
1826
1827 /* Query before enabling in case configuration dependent. */
1828 ret = _regulator_get_enable_time(rdev);
1829 if (ret >= 0) {
1830 delay = ret;
1831 } else {
1832 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1833 delay = 0;
1834 }
1835
1836 trace_regulator_enable(rdev_get_name(rdev));
1837
Guodong Xu871f5652014-08-13 19:33:40 +08001838 if (rdev->desc->off_on_delay) {
1839 /* if needed, keep a distance of off_on_delay from last time
1840 * this regulator was disabled.
1841 */
1842 unsigned long start_jiffy = jiffies;
1843 unsigned long intended, max_delay, remaining;
1844
1845 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
1846 intended = rdev->last_off_jiffy + max_delay;
1847
1848 if (time_before(start_jiffy, intended)) {
1849 /* calc remaining jiffies to deal with one-time
1850 * timer wrapping.
1851 * in case of multiple timer wrapping, either it can be
1852 * detected by out-of-range remaining, or it cannot be
1853 * detected and we gets a panelty of
1854 * _regulator_enable_delay().
1855 */
1856 remaining = intended - start_jiffy;
1857 if (remaining <= max_delay)
1858 _regulator_enable_delay(
1859 jiffies_to_usecs(remaining));
1860 }
1861 }
1862
Kim, Milo967cfb12013-02-18 06:50:48 +00001863 if (rdev->ena_pin) {
1864 ret = regulator_ena_gpio_ctrl(rdev, true);
1865 if (ret < 0)
1866 return ret;
Mark Brown65f73502012-06-27 14:14:38 +01001867 rdev->ena_gpio_state = 1;
1868 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001869 ret = rdev->desc->ops->enable(rdev);
1870 if (ret < 0)
1871 return ret;
1872 } else {
1873 return -EINVAL;
1874 }
1875
1876 /* Allow the regulator to ramp; it would be useful to extend
1877 * this for bulk operations so that the regulators can ramp
1878 * together. */
1879 trace_regulator_enable_delay(rdev_get_name(rdev));
1880
Guodong Xu79fd1142014-08-13 19:33:39 +08001881 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01001882
1883 trace_regulator_enable_complete(rdev_get_name(rdev));
1884
1885 return 0;
1886}
1887
Liam Girdwood414c70c2008-04-30 15:59:04 +01001888/* locks held by regulator_enable() */
1889static int _regulator_enable(struct regulator_dev *rdev)
1890{
Mark Brown5c5659d2012-06-27 13:21:26 +01001891 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001892
Liam Girdwood414c70c2008-04-30 15:59:04 +01001893 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001894 if (rdev->constraints &&
1895 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1896 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001897
Mark Brown9a2372f2009-08-03 18:49:57 +01001898 if (rdev->use_count == 0) {
1899 /* The regulator may on if it's not switchable or left on */
1900 ret = _regulator_is_enabled(rdev);
1901 if (ret == -EINVAL || ret == 0) {
1902 if (!_regulator_can_change_status(rdev))
1903 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001904
Mark Brown5c5659d2012-06-27 13:21:26 +01001905 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001906 if (ret < 0)
1907 return ret;
1908
Linus Walleija7433cf2009-08-26 12:54:04 +02001909 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001910 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001911 return ret;
1912 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001913 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001914 }
1915
Mark Brown9a2372f2009-08-03 18:49:57 +01001916 rdev->use_count++;
1917
1918 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001919}
1920
1921/**
1922 * regulator_enable - enable regulator output
1923 * @regulator: regulator source
1924 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001925 * Request that the regulator be enabled with the regulator output at
1926 * the predefined voltage or current value. Calls to regulator_enable()
1927 * must be balanced with calls to regulator_disable().
1928 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001929 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001930 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001931 */
1932int regulator_enable(struct regulator *regulator)
1933{
David Brownell412aec62008-11-16 11:44:46 -08001934 struct regulator_dev *rdev = regulator->rdev;
1935 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001936
Mark Brown6492bc12012-04-19 13:19:07 +01001937 if (regulator->always_on)
1938 return 0;
1939
Mark Brown3801b862011-06-09 16:22:22 +01001940 if (rdev->supply) {
1941 ret = regulator_enable(rdev->supply);
1942 if (ret != 0)
1943 return ret;
1944 }
1945
David Brownell412aec62008-11-16 11:44:46 -08001946 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001947 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001948 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001949
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001950 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001951 regulator_disable(rdev->supply);
1952
Liam Girdwood414c70c2008-04-30 15:59:04 +01001953 return ret;
1954}
1955EXPORT_SYMBOL_GPL(regulator_enable);
1956
Mark Brown5c5659d2012-06-27 13:21:26 +01001957static int _regulator_do_disable(struct regulator_dev *rdev)
1958{
1959 int ret;
1960
1961 trace_regulator_disable(rdev_get_name(rdev));
1962
Kim, Milo967cfb12013-02-18 06:50:48 +00001963 if (rdev->ena_pin) {
1964 ret = regulator_ena_gpio_ctrl(rdev, false);
1965 if (ret < 0)
1966 return ret;
Mark Brown5c5659d2012-06-27 13:21:26 +01001967 rdev->ena_gpio_state = 0;
1968
1969 } else if (rdev->desc->ops->disable) {
1970 ret = rdev->desc->ops->disable(rdev);
1971 if (ret != 0)
1972 return ret;
1973 }
1974
Guodong Xu871f5652014-08-13 19:33:40 +08001975 /* cares about last_off_jiffy only if off_on_delay is required by
1976 * device.
1977 */
1978 if (rdev->desc->off_on_delay)
1979 rdev->last_off_jiffy = jiffies;
1980
Mark Brown5c5659d2012-06-27 13:21:26 +01001981 trace_regulator_disable_complete(rdev_get_name(rdev));
1982
Mark Brown5c5659d2012-06-27 13:21:26 +01001983 return 0;
1984}
1985
Liam Girdwood414c70c2008-04-30 15:59:04 +01001986/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001987static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001988{
1989 int ret = 0;
1990
David Brownellcd94b502009-03-11 16:43:34 -08001991 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001992 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001993 return -EIO;
1994
Liam Girdwood414c70c2008-04-30 15:59:04 +01001995 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001996 if (rdev->use_count == 1 &&
1997 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001998
1999 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01002000 if (_regulator_can_change_status(rdev)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002001 ret = _notifier_call_chain(rdev,
2002 REGULATOR_EVENT_PRE_DISABLE,
2003 NULL);
2004 if (ret & NOTIFY_STOP_MASK)
2005 return -EINVAL;
2006
Mark Brown5c5659d2012-06-27 13:21:26 +01002007 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002008 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002009 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002010 _notifier_call_chain(rdev,
2011 REGULATOR_EVENT_ABORT_DISABLE,
2012 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002013 return ret;
2014 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002015 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2016 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002017 }
2018
Liam Girdwood414c70c2008-04-30 15:59:04 +01002019 rdev->use_count = 0;
2020 } else if (rdev->use_count > 1) {
2021
2022 if (rdev->constraints &&
2023 (rdev->constraints->valid_ops_mask &
2024 REGULATOR_CHANGE_DRMS))
2025 drms_uA_update(rdev);
2026
2027 rdev->use_count--;
2028 }
Mark Brown3801b862011-06-09 16:22:22 +01002029
Liam Girdwood414c70c2008-04-30 15:59:04 +01002030 return ret;
2031}
2032
2033/**
2034 * regulator_disable - disable regulator output
2035 * @regulator: regulator source
2036 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002037 * Disable the regulator output voltage or current. Calls to
2038 * regulator_enable() must be balanced with calls to
2039 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002040 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002041 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002042 * devices have it enabled, the regulator device supports disabling and
2043 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002044 */
2045int regulator_disable(struct regulator *regulator)
2046{
David Brownell412aec62008-11-16 11:44:46 -08002047 struct regulator_dev *rdev = regulator->rdev;
2048 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002049
Mark Brown6492bc12012-04-19 13:19:07 +01002050 if (regulator->always_on)
2051 return 0;
2052
David Brownell412aec62008-11-16 11:44:46 -08002053 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002054 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002055 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002056
Mark Brown3801b862011-06-09 16:22:22 +01002057 if (ret == 0 && rdev->supply)
2058 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002059
Liam Girdwood414c70c2008-04-30 15:59:04 +01002060 return ret;
2061}
2062EXPORT_SYMBOL_GPL(regulator_disable);
2063
2064/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002065static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002066{
2067 int ret = 0;
2068
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002069 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2070 REGULATOR_EVENT_PRE_DISABLE, NULL);
2071 if (ret & NOTIFY_STOP_MASK)
2072 return -EINVAL;
2073
Markus Pargmann66fda752014-02-20 17:36:04 +01002074 ret = _regulator_do_disable(rdev);
2075 if (ret < 0) {
2076 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002077 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2078 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002079 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002080 }
2081
Markus Pargmann66fda752014-02-20 17:36:04 +01002082 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2083 REGULATOR_EVENT_DISABLE, NULL);
2084
2085 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002086}
2087
2088/**
2089 * regulator_force_disable - force disable regulator output
2090 * @regulator: regulator source
2091 *
2092 * Forcibly disable the regulator output voltage or current.
2093 * NOTE: this *will* disable the regulator output even if other consumer
2094 * devices have it enabled. This should be used for situations when device
2095 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2096 */
2097int regulator_force_disable(struct regulator *regulator)
2098{
Mark Brown82d15832011-05-09 11:41:02 +02002099 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002100 int ret;
2101
Mark Brown82d15832011-05-09 11:41:02 +02002102 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002103 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002104 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002105 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002106
Mark Brown3801b862011-06-09 16:22:22 +01002107 if (rdev->supply)
2108 while (rdev->open_count--)
2109 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002110
Liam Girdwood414c70c2008-04-30 15:59:04 +01002111 return ret;
2112}
2113EXPORT_SYMBOL_GPL(regulator_force_disable);
2114
Mark Brownda07ecd2011-09-11 09:53:50 +01002115static void regulator_disable_work(struct work_struct *work)
2116{
2117 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2118 disable_work.work);
2119 int count, i, ret;
2120
2121 mutex_lock(&rdev->mutex);
2122
2123 BUG_ON(!rdev->deferred_disables);
2124
2125 count = rdev->deferred_disables;
2126 rdev->deferred_disables = 0;
2127
2128 for (i = 0; i < count; i++) {
2129 ret = _regulator_disable(rdev);
2130 if (ret != 0)
2131 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2132 }
2133
2134 mutex_unlock(&rdev->mutex);
2135
2136 if (rdev->supply) {
2137 for (i = 0; i < count; i++) {
2138 ret = regulator_disable(rdev->supply);
2139 if (ret != 0) {
2140 rdev_err(rdev,
2141 "Supply disable failed: %d\n", ret);
2142 }
2143 }
2144 }
2145}
2146
2147/**
2148 * regulator_disable_deferred - disable regulator output with delay
2149 * @regulator: regulator source
2150 * @ms: miliseconds until the regulator is disabled
2151 *
2152 * Execute regulator_disable() on the regulator after a delay. This
2153 * is intended for use with devices that require some time to quiesce.
2154 *
2155 * NOTE: this will only disable the regulator output if no other consumer
2156 * devices have it enabled, the regulator device supports disabling and
2157 * machine constraints permit this operation.
2158 */
2159int regulator_disable_deferred(struct regulator *regulator, int ms)
2160{
2161 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01002162 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01002163
Mark Brown6492bc12012-04-19 13:19:07 +01002164 if (regulator->always_on)
2165 return 0;
2166
Mark Brown2b5a24a2012-09-07 11:00:53 +08002167 if (!ms)
2168 return regulator_disable(regulator);
2169
Mark Brownda07ecd2011-09-11 09:53:50 +01002170 mutex_lock(&rdev->mutex);
2171 rdev->deferred_disables++;
2172 mutex_unlock(&rdev->mutex);
2173
Mark Brown070260f2013-07-18 11:52:04 +01002174 ret = queue_delayed_work(system_power_efficient_wq,
2175 &rdev->disable_work,
2176 msecs_to_jiffies(ms));
Mark Brownaa598022011-10-03 22:42:43 +01002177 if (ret < 0)
2178 return ret;
2179 else
2180 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002181}
2182EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2183
Liam Girdwood414c70c2008-04-30 15:59:04 +01002184static int _regulator_is_enabled(struct regulator_dev *rdev)
2185{
Mark Brown65f73502012-06-27 14:14:38 +01002186 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002187 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002188 return rdev->ena_gpio_state;
2189
Mark Brown9a7f6a42010-02-11 17:22:45 +00002190 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002191 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002192 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002193
Mark Brown93325462009-08-03 18:49:56 +01002194 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002195}
2196
2197/**
2198 * regulator_is_enabled - is the regulator output enabled
2199 * @regulator: regulator source
2200 *
David Brownell412aec62008-11-16 11:44:46 -08002201 * Returns positive if the regulator driver backing the source/client
2202 * has requested that the device be enabled, zero if it hasn't, else a
2203 * negative errno code.
2204 *
2205 * Note that the device backing this regulator handle can have multiple
2206 * users, so it might be enabled even if regulator_enable() was never
2207 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002208 */
2209int regulator_is_enabled(struct regulator *regulator)
2210{
Mark Brown93325462009-08-03 18:49:56 +01002211 int ret;
2212
Mark Brown6492bc12012-04-19 13:19:07 +01002213 if (regulator->always_on)
2214 return 1;
2215
Mark Brown93325462009-08-03 18:49:56 +01002216 mutex_lock(&regulator->rdev->mutex);
2217 ret = _regulator_is_enabled(regulator->rdev);
2218 mutex_unlock(&regulator->rdev->mutex);
2219
2220 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002221}
2222EXPORT_SYMBOL_GPL(regulator_is_enabled);
2223
2224/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002225 * regulator_can_change_voltage - check if regulator can change voltage
2226 * @regulator: regulator source
2227 *
2228 * Returns positive if the regulator driver backing the source/client
Masanari Iidae2278672014-02-18 22:54:36 +09002229 * can change its voltage, false otherwise. Useful for detecting fixed
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002230 * or dummy regulators and disabling voltage change logic in the client
2231 * driver.
2232 */
2233int regulator_can_change_voltage(struct regulator *regulator)
2234{
2235 struct regulator_dev *rdev = regulator->rdev;
2236
2237 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002238 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2239 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2240 return 1;
2241
2242 if (rdev->desc->continuous_voltage_range &&
2243 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2244 rdev->constraints->min_uV != rdev->constraints->max_uV)
2245 return 1;
2246 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002247
2248 return 0;
2249}
2250EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2251
2252/**
David Brownell4367cfd2009-02-26 11:48:36 -08002253 * regulator_count_voltages - count regulator_list_voltage() selectors
2254 * @regulator: regulator source
2255 *
2256 * Returns number of selectors, or negative errno. Selectors are
2257 * numbered starting at zero, and typically correspond to bitfields
2258 * in hardware registers.
2259 */
2260int regulator_count_voltages(struct regulator *regulator)
2261{
2262 struct regulator_dev *rdev = regulator->rdev;
2263
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002264 if (rdev->desc->n_voltages)
2265 return rdev->desc->n_voltages;
2266
2267 if (!rdev->supply)
2268 return -EINVAL;
2269
2270 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002271}
2272EXPORT_SYMBOL_GPL(regulator_count_voltages);
2273
2274/**
2275 * regulator_list_voltage - enumerate supported voltages
2276 * @regulator: regulator source
2277 * @selector: identify voltage to list
2278 * Context: can sleep
2279 *
2280 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002281 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002282 * negative errno.
2283 */
2284int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2285{
Guodong Xu272e2312014-08-13 19:33:38 +08002286 struct regulator_dev *rdev = regulator->rdev;
2287 const struct regulator_ops *ops = rdev->desc->ops;
2288 int ret;
David Brownell4367cfd2009-02-26 11:48:36 -08002289
Guennadi Liakhovetskif4460432013-11-13 12:33:00 +01002290 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2291 return rdev->desc->fixed_uV;
2292
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002293 if (ops->list_voltage) {
2294 if (selector >= rdev->desc->n_voltages)
2295 return -EINVAL;
2296 mutex_lock(&rdev->mutex);
2297 ret = ops->list_voltage(rdev, selector);
2298 mutex_unlock(&rdev->mutex);
2299 } else if (rdev->supply) {
2300 ret = regulator_list_voltage(rdev->supply, selector);
2301 } else {
David Brownell4367cfd2009-02-26 11:48:36 -08002302 return -EINVAL;
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002303 }
David Brownell4367cfd2009-02-26 11:48:36 -08002304
2305 if (ret > 0) {
2306 if (ret < rdev->constraints->min_uV)
2307 ret = 0;
2308 else if (ret > rdev->constraints->max_uV)
2309 ret = 0;
2310 }
2311
2312 return ret;
2313}
2314EXPORT_SYMBOL_GPL(regulator_list_voltage);
2315
2316/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002317 * regulator_get_regmap - get the regulator's register map
2318 * @regulator: regulator source
2319 *
2320 * Returns the register map for the given regulator, or an ERR_PTR value
2321 * if the regulator doesn't use regmap.
2322 */
2323struct regmap *regulator_get_regmap(struct regulator *regulator)
2324{
2325 struct regmap *map = regulator->rdev->regmap;
2326
2327 return map ? map : ERR_PTR(-EOPNOTSUPP);
2328}
2329
2330/**
2331 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2332 * @regulator: regulator source
2333 * @vsel_reg: voltage selector register, output parameter
2334 * @vsel_mask: mask for voltage selector bitfield, output parameter
2335 *
2336 * Returns the hardware register offset and bitmask used for setting the
2337 * regulator voltage. This might be useful when configuring voltage-scaling
2338 * hardware or firmware that can make I2C requests behind the kernel's back,
2339 * for example.
2340 *
2341 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2342 * and 0 is returned, otherwise a negative errno is returned.
2343 */
2344int regulator_get_hardware_vsel_register(struct regulator *regulator,
2345 unsigned *vsel_reg,
2346 unsigned *vsel_mask)
2347{
Guodong Xu39f54602014-08-19 18:07:41 +08002348 struct regulator_dev *rdev = regulator->rdev;
2349 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002350
2351 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2352 return -EOPNOTSUPP;
2353
2354 *vsel_reg = rdev->desc->vsel_reg;
2355 *vsel_mask = rdev->desc->vsel_mask;
2356
2357 return 0;
2358}
2359EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2360
2361/**
2362 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2363 * @regulator: regulator source
2364 * @selector: identify voltage to list
2365 *
2366 * Converts the selector to a hardware-specific voltage selector that can be
2367 * directly written to the regulator registers. The address of the voltage
2368 * register can be determined by calling @regulator_get_hardware_vsel_register.
2369 *
2370 * On error a negative errno is returned.
2371 */
2372int regulator_list_hardware_vsel(struct regulator *regulator,
2373 unsigned selector)
2374{
Guodong Xu39f54602014-08-19 18:07:41 +08002375 struct regulator_dev *rdev = regulator->rdev;
2376 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002377
2378 if (selector >= rdev->desc->n_voltages)
2379 return -EINVAL;
2380 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2381 return -EOPNOTSUPP;
2382
2383 return selector;
2384}
2385EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2386
2387/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002388 * regulator_get_linear_step - return the voltage step size between VSEL values
2389 * @regulator: regulator source
2390 *
2391 * Returns the voltage step size between VSEL values for linear
2392 * regulators, or return 0 if the regulator isn't a linear regulator.
2393 */
2394unsigned int regulator_get_linear_step(struct regulator *regulator)
2395{
2396 struct regulator_dev *rdev = regulator->rdev;
2397
2398 return rdev->desc->uV_step;
2399}
2400EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2401
2402/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002403 * regulator_is_supported_voltage - check if a voltage range can be supported
2404 *
2405 * @regulator: Regulator to check.
2406 * @min_uV: Minimum required voltage in uV.
2407 * @max_uV: Maximum required voltage in uV.
2408 *
2409 * Returns a boolean or a negative error code.
2410 */
2411int regulator_is_supported_voltage(struct regulator *regulator,
2412 int min_uV, int max_uV)
2413{
Mark Brownc5f39392012-07-02 15:00:18 +01002414 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002415 int i, voltages, ret;
2416
Mark Brownc5f39392012-07-02 15:00:18 +01002417 /* If we can't change voltage check the current voltage */
2418 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2419 ret = regulator_get_voltage(regulator);
2420 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002421 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002422 else
2423 return ret;
2424 }
2425
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002426 /* Any voltage within constrains range is fine? */
2427 if (rdev->desc->continuous_voltage_range)
2428 return min_uV >= rdev->constraints->min_uV &&
2429 max_uV <= rdev->constraints->max_uV;
2430
Mark Browna7a1ad92009-07-21 16:00:24 +01002431 ret = regulator_count_voltages(regulator);
2432 if (ret < 0)
2433 return ret;
2434 voltages = ret;
2435
2436 for (i = 0; i < voltages; i++) {
2437 ret = regulator_list_voltage(regulator, i);
2438
2439 if (ret >= min_uV && ret <= max_uV)
2440 return 1;
2441 }
2442
2443 return 0;
2444}
Mark Browna398eaa2011-12-28 12:48:45 +00002445EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002446
Heiko Stübner71795692014-08-28 12:36:04 -07002447static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2448 int min_uV, int max_uV,
2449 unsigned *selector)
2450{
2451 struct pre_voltage_change_data data;
2452 int ret;
2453
2454 data.old_uV = _regulator_get_voltage(rdev);
2455 data.min_uV = min_uV;
2456 data.max_uV = max_uV;
2457 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2458 &data);
2459 if (ret & NOTIFY_STOP_MASK)
2460 return -EINVAL;
2461
2462 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2463 if (ret >= 0)
2464 return ret;
2465
2466 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2467 (void *)data.old_uV);
2468
2469 return ret;
2470}
2471
2472static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2473 int uV, unsigned selector)
2474{
2475 struct pre_voltage_change_data data;
2476 int ret;
2477
2478 data.old_uV = _regulator_get_voltage(rdev);
2479 data.min_uV = uV;
2480 data.max_uV = uV;
2481 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2482 &data);
2483 if (ret & NOTIFY_STOP_MASK)
2484 return -EINVAL;
2485
2486 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2487 if (ret >= 0)
2488 return ret;
2489
2490 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2491 (void *)data.old_uV);
2492
2493 return ret;
2494}
2495
Mark Brown75790252010-12-12 14:25:50 +00002496static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2497 int min_uV, int max_uV)
2498{
2499 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002500 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002501 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002502 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002503 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002504
2505 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2506
Mark Brownbf5892a2011-05-08 22:13:37 +01002507 min_uV += rdev->constraints->uV_offset;
2508 max_uV += rdev->constraints->uV_offset;
2509
Axel Lineba41a52012-04-04 10:32:10 +08002510 /*
2511 * If we can't obtain the old selector there is not enough
2512 * info to call set_voltage_time_sel().
2513 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002514 if (_regulator_is_enabled(rdev) &&
2515 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002516 rdev->desc->ops->get_voltage_sel) {
2517 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2518 if (old_selector < 0)
2519 return old_selector;
2520 }
2521
Mark Brown75790252010-12-12 14:25:50 +00002522 if (rdev->desc->ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002523 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2524 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002525
2526 if (ret >= 0) {
2527 if (rdev->desc->ops->list_voltage)
2528 best_val = rdev->desc->ops->list_voltage(rdev,
2529 selector);
2530 else
2531 best_val = _regulator_get_voltage(rdev);
2532 }
2533
Mark Browne8eef822010-12-12 14:36:17 +00002534 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002535 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002536 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2537 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002538 } else {
2539 if (rdev->desc->ops->list_voltage ==
2540 regulator_list_voltage_linear)
2541 ret = regulator_map_voltage_linear(rdev,
2542 min_uV, max_uV);
Axel Lin36698622014-05-24 11:10:43 +08002543 else if (rdev->desc->ops->list_voltage ==
2544 regulator_list_voltage_linear_range)
2545 ret = regulator_map_voltage_linear_range(rdev,
2546 min_uV, max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002547 else
2548 ret = regulator_map_voltage_iterate(rdev,
2549 min_uV, max_uV);
2550 }
Mark Browne8eef822010-12-12 14:36:17 +00002551
Mark Browne843fc42012-05-09 21:16:06 +01002552 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002553 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2554 if (min_uV <= best_val && max_uV >= best_val) {
2555 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002556 if (old_selector == selector)
2557 ret = 0;
2558 else
Heiko Stübner71795692014-08-28 12:36:04 -07002559 ret = _regulator_call_set_voltage_sel(
2560 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002561 } else {
2562 ret = -EINVAL;
2563 }
Mark Browne8eef822010-12-12 14:36:17 +00002564 }
Mark Brown75790252010-12-12 14:25:50 +00002565 } else {
2566 ret = -EINVAL;
2567 }
2568
Axel Lineba41a52012-04-04 10:32:10 +08002569 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302570 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2571 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002572
2573 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2574 old_selector, selector);
2575 if (delay < 0) {
2576 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2577 delay);
2578 delay = 0;
2579 }
Axel Lineba41a52012-04-04 10:32:10 +08002580
Philip Rakity8b96de32012-06-14 15:07:56 -07002581 /* Insert any necessary delays */
2582 if (delay >= 1000) {
2583 mdelay(delay / 1000);
2584 udelay(delay % 1000);
2585 } else if (delay) {
2586 udelay(delay);
2587 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002588 }
2589
Axel Lin2f6c7972012-08-06 23:44:19 +08002590 if (ret == 0 && best_val >= 0) {
2591 unsigned long data = best_val;
2592
Mark Brownded06a52010-12-16 13:59:10 +00002593 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002594 (void *)data);
2595 }
Mark Brownded06a52010-12-16 13:59:10 +00002596
Axel Lineba41a52012-04-04 10:32:10 +08002597 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002598
2599 return ret;
2600}
2601
Mark Browna7a1ad92009-07-21 16:00:24 +01002602/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002603 * regulator_set_voltage - set regulator output voltage
2604 * @regulator: regulator source
2605 * @min_uV: Minimum required voltage in uV
2606 * @max_uV: Maximum acceptable voltage in uV
2607 *
2608 * Sets a voltage regulator to the desired output voltage. This can be set
2609 * during any regulator state. IOW, regulator can be disabled or enabled.
2610 *
2611 * If the regulator is enabled then the voltage will change to the new value
2612 * immediately otherwise if the regulator is disabled the regulator will
2613 * output at the new voltage when enabled.
2614 *
2615 * NOTE: If the regulator is shared between several devices then the lowest
2616 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002617 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002618 * calling this function otherwise this call will fail.
2619 */
2620int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2621{
2622 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002623 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002624 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002625 int current_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002626
2627 mutex_lock(&rdev->mutex);
2628
Mark Brown95a3c232010-12-16 15:49:37 +00002629 /* If we're setting the same range as last time the change
2630 * should be a noop (some cpufreq implementations use the same
2631 * voltage for multiple frequencies, for example).
2632 */
2633 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2634 goto out;
2635
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002636 /* If we're trying to set a range that overlaps the current voltage,
2637 * return succesfully even though the regulator does not support
2638 * changing the voltage.
2639 */
2640 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2641 current_uV = _regulator_get_voltage(rdev);
2642 if (min_uV <= current_uV && current_uV <= max_uV) {
2643 regulator->min_uV = min_uV;
2644 regulator->max_uV = max_uV;
2645 goto out;
2646 }
2647 }
2648
Liam Girdwood414c70c2008-04-30 15:59:04 +01002649 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002650 if (!rdev->desc->ops->set_voltage &&
2651 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002652 ret = -EINVAL;
2653 goto out;
2654 }
2655
2656 /* constraints check */
2657 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2658 if (ret < 0)
2659 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002660
Paolo Pisati92d7a552012-12-13 10:13:00 +01002661 /* restore original values in case of error */
2662 old_min_uV = regulator->min_uV;
2663 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002664 regulator->min_uV = min_uV;
2665 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002666
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002667 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2668 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002669 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002670
Mark Brown75790252010-12-12 14:25:50 +00002671 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002672 if (ret < 0)
2673 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09002674
Liam Girdwood414c70c2008-04-30 15:59:04 +01002675out:
2676 mutex_unlock(&rdev->mutex);
2677 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002678out2:
2679 regulator->min_uV = old_min_uV;
2680 regulator->max_uV = old_max_uV;
2681 mutex_unlock(&rdev->mutex);
2682 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002683}
2684EXPORT_SYMBOL_GPL(regulator_set_voltage);
2685
Mark Brown606a2562010-12-16 15:49:36 +00002686/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002687 * regulator_set_voltage_time - get raise/fall time
2688 * @regulator: regulator source
2689 * @old_uV: starting voltage in microvolts
2690 * @new_uV: target voltage in microvolts
2691 *
2692 * Provided with the starting and ending voltage, this function attempts to
2693 * calculate the time in microseconds required to rise or fall to this new
2694 * voltage.
2695 */
2696int regulator_set_voltage_time(struct regulator *regulator,
2697 int old_uV, int new_uV)
2698{
Guodong Xu272e2312014-08-13 19:33:38 +08002699 struct regulator_dev *rdev = regulator->rdev;
2700 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01002701 int old_sel = -1;
2702 int new_sel = -1;
2703 int voltage;
2704 int i;
2705
2706 /* Currently requires operations to do this */
2707 if (!ops->list_voltage || !ops->set_voltage_time_sel
2708 || !rdev->desc->n_voltages)
2709 return -EINVAL;
2710
2711 for (i = 0; i < rdev->desc->n_voltages; i++) {
2712 /* We only look for exact voltage matches here */
2713 voltage = regulator_list_voltage(regulator, i);
2714 if (voltage < 0)
2715 return -EINVAL;
2716 if (voltage == 0)
2717 continue;
2718 if (voltage == old_uV)
2719 old_sel = i;
2720 if (voltage == new_uV)
2721 new_sel = i;
2722 }
2723
2724 if (old_sel < 0 || new_sel < 0)
2725 return -EINVAL;
2726
2727 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2728}
2729EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2730
2731/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002732 * regulator_set_voltage_time_sel - get raise/fall time
2733 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302734 * @old_selector: selector for starting voltage
2735 * @new_selector: selector for target voltage
2736 *
2737 * Provided with the starting and target voltage selectors, this function
2738 * returns time in microseconds required to rise or fall to this new voltage
2739 *
Axel Linf11d08c2012-06-19 09:38:45 +08002740 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002741 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302742 */
2743int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2744 unsigned int old_selector,
2745 unsigned int new_selector)
2746{
Axel Lin398715a2012-06-18 10:11:28 +08002747 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002748 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002749
2750 if (rdev->constraints->ramp_delay)
2751 ramp_delay = rdev->constraints->ramp_delay;
2752 else if (rdev->desc->ramp_delay)
2753 ramp_delay = rdev->desc->ramp_delay;
2754
2755 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302756 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002757 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302758 }
Axel Lin398715a2012-06-18 10:11:28 +08002759
Axel Linf11d08c2012-06-19 09:38:45 +08002760 /* sanity check */
2761 if (!rdev->desc->ops->list_voltage)
2762 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002763
Axel Linf11d08c2012-06-19 09:38:45 +08002764 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2765 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2766
2767 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302768}
Mark Brownb19dbf72012-06-23 11:34:20 +01002769EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302770
2771/**
Mark Brown606a2562010-12-16 15:49:36 +00002772 * regulator_sync_voltage - re-apply last regulator output voltage
2773 * @regulator: regulator source
2774 *
2775 * Re-apply the last configured voltage. This is intended to be used
2776 * where some external control source the consumer is cooperating with
2777 * has caused the configured voltage to change.
2778 */
2779int regulator_sync_voltage(struct regulator *regulator)
2780{
2781 struct regulator_dev *rdev = regulator->rdev;
2782 int ret, min_uV, max_uV;
2783
2784 mutex_lock(&rdev->mutex);
2785
2786 if (!rdev->desc->ops->set_voltage &&
2787 !rdev->desc->ops->set_voltage_sel) {
2788 ret = -EINVAL;
2789 goto out;
2790 }
2791
2792 /* This is only going to work if we've had a voltage configured. */
2793 if (!regulator->min_uV && !regulator->max_uV) {
2794 ret = -EINVAL;
2795 goto out;
2796 }
2797
2798 min_uV = regulator->min_uV;
2799 max_uV = regulator->max_uV;
2800
2801 /* This should be a paranoia check... */
2802 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2803 if (ret < 0)
2804 goto out;
2805
2806 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2807 if (ret < 0)
2808 goto out;
2809
2810 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2811
2812out:
2813 mutex_unlock(&rdev->mutex);
2814 return ret;
2815}
2816EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2817
Liam Girdwood414c70c2008-04-30 15:59:04 +01002818static int _regulator_get_voltage(struct regulator_dev *rdev)
2819{
Mark Brownbf5892a2011-05-08 22:13:37 +01002820 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002821
2822 if (rdev->desc->ops->get_voltage_sel) {
2823 sel = rdev->desc->ops->get_voltage_sel(rdev);
2824 if (sel < 0)
2825 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002826 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002827 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002828 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002829 } else if (rdev->desc->ops->list_voltage) {
2830 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05302831 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2832 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02002833 } else if (rdev->supply) {
2834 ret = regulator_get_voltage(rdev->supply);
Axel Lincb220d12011-05-23 20:08:10 +08002835 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002836 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002837 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002838
Axel Lincb220d12011-05-23 20:08:10 +08002839 if (ret < 0)
2840 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002841 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002842}
2843
2844/**
2845 * regulator_get_voltage - get regulator output voltage
2846 * @regulator: regulator source
2847 *
2848 * This returns the current regulator voltage in uV.
2849 *
2850 * NOTE: If the regulator is disabled it will return the voltage value. This
2851 * function should not be used to determine regulator state.
2852 */
2853int regulator_get_voltage(struct regulator *regulator)
2854{
2855 int ret;
2856
2857 mutex_lock(&regulator->rdev->mutex);
2858
2859 ret = _regulator_get_voltage(regulator->rdev);
2860
2861 mutex_unlock(&regulator->rdev->mutex);
2862
2863 return ret;
2864}
2865EXPORT_SYMBOL_GPL(regulator_get_voltage);
2866
2867/**
2868 * regulator_set_current_limit - set regulator output current limit
2869 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01002870 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01002871 * @max_uA: Maximum supported current in uA
2872 *
2873 * Sets current sink to the desired output current. This can be set during
2874 * any regulator state. IOW, regulator can be disabled or enabled.
2875 *
2876 * If the regulator is enabled then the current will change to the new value
2877 * immediately otherwise if the regulator is disabled the regulator will
2878 * output at the new current when enabled.
2879 *
2880 * NOTE: Regulator system constraints must be set for this regulator before
2881 * calling this function otherwise this call will fail.
2882 */
2883int regulator_set_current_limit(struct regulator *regulator,
2884 int min_uA, int max_uA)
2885{
2886 struct regulator_dev *rdev = regulator->rdev;
2887 int ret;
2888
2889 mutex_lock(&rdev->mutex);
2890
2891 /* sanity check */
2892 if (!rdev->desc->ops->set_current_limit) {
2893 ret = -EINVAL;
2894 goto out;
2895 }
2896
2897 /* constraints check */
2898 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2899 if (ret < 0)
2900 goto out;
2901
2902 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2903out:
2904 mutex_unlock(&rdev->mutex);
2905 return ret;
2906}
2907EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2908
2909static int _regulator_get_current_limit(struct regulator_dev *rdev)
2910{
2911 int ret;
2912
2913 mutex_lock(&rdev->mutex);
2914
2915 /* sanity check */
2916 if (!rdev->desc->ops->get_current_limit) {
2917 ret = -EINVAL;
2918 goto out;
2919 }
2920
2921 ret = rdev->desc->ops->get_current_limit(rdev);
2922out:
2923 mutex_unlock(&rdev->mutex);
2924 return ret;
2925}
2926
2927/**
2928 * regulator_get_current_limit - get regulator output current
2929 * @regulator: regulator source
2930 *
2931 * This returns the current supplied by the specified current sink in uA.
2932 *
2933 * NOTE: If the regulator is disabled it will return the current value. This
2934 * function should not be used to determine regulator state.
2935 */
2936int regulator_get_current_limit(struct regulator *regulator)
2937{
2938 return _regulator_get_current_limit(regulator->rdev);
2939}
2940EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2941
2942/**
2943 * regulator_set_mode - set regulator operating mode
2944 * @regulator: regulator source
2945 * @mode: operating mode - one of the REGULATOR_MODE constants
2946 *
2947 * Set regulator operating mode to increase regulator efficiency or improve
2948 * regulation performance.
2949 *
2950 * NOTE: Regulator system constraints must be set for this regulator before
2951 * calling this function otherwise this call will fail.
2952 */
2953int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2954{
2955 struct regulator_dev *rdev = regulator->rdev;
2956 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302957 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002958
2959 mutex_lock(&rdev->mutex);
2960
2961 /* sanity check */
2962 if (!rdev->desc->ops->set_mode) {
2963 ret = -EINVAL;
2964 goto out;
2965 }
2966
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302967 /* return if the same mode is requested */
2968 if (rdev->desc->ops->get_mode) {
2969 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2970 if (regulator_curr_mode == mode) {
2971 ret = 0;
2972 goto out;
2973 }
2974 }
2975
Liam Girdwood414c70c2008-04-30 15:59:04 +01002976 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002977 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002978 if (ret < 0)
2979 goto out;
2980
2981 ret = rdev->desc->ops->set_mode(rdev, mode);
2982out:
2983 mutex_unlock(&rdev->mutex);
2984 return ret;
2985}
2986EXPORT_SYMBOL_GPL(regulator_set_mode);
2987
2988static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2989{
2990 int ret;
2991
2992 mutex_lock(&rdev->mutex);
2993
2994 /* sanity check */
2995 if (!rdev->desc->ops->get_mode) {
2996 ret = -EINVAL;
2997 goto out;
2998 }
2999
3000 ret = rdev->desc->ops->get_mode(rdev);
3001out:
3002 mutex_unlock(&rdev->mutex);
3003 return ret;
3004}
3005
3006/**
3007 * regulator_get_mode - get regulator operating mode
3008 * @regulator: regulator source
3009 *
3010 * Get the current regulator operating mode.
3011 */
3012unsigned int regulator_get_mode(struct regulator *regulator)
3013{
3014 return _regulator_get_mode(regulator->rdev);
3015}
3016EXPORT_SYMBOL_GPL(regulator_get_mode);
3017
3018/**
3019 * regulator_set_optimum_mode - set regulator optimum operating mode
3020 * @regulator: regulator source
3021 * @uA_load: load current
3022 *
3023 * Notifies the regulator core of a new device load. This is then used by
3024 * DRMS (if enabled by constraints) to set the most efficient regulator
3025 * operating mode for the new regulator loading.
3026 *
3027 * Consumer devices notify their supply regulator of the maximum power
3028 * they will require (can be taken from device datasheet in the power
3029 * consumption tables) when they change operational status and hence power
3030 * state. Examples of operational state changes that can affect power
3031 * consumption are :-
3032 *
3033 * o Device is opened / closed.
3034 * o Device I/O is about to begin or has just finished.
3035 * o Device is idling in between work.
3036 *
3037 * This information is also exported via sysfs to userspace.
3038 *
3039 * DRMS will sum the total requested load on the regulator and change
3040 * to the most efficient operating mode if platform constraints allow.
3041 *
3042 * Returns the new regulator mode or error.
3043 */
3044int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
3045{
3046 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003047 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003048
Liam Girdwood414c70c2008-04-30 15:59:04 +01003049 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003050 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003051 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003052 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003053
Liam Girdwood414c70c2008-04-30 15:59:04 +01003054 return ret;
3055}
3056EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
3057
3058/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003059 * regulator_allow_bypass - allow the regulator to go into bypass mode
3060 *
3061 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003062 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003063 *
3064 * Allow the regulator to go into bypass mode if all other consumers
3065 * for the regulator also enable bypass mode and the machine
3066 * constraints allow this. Bypass mode means that the regulator is
3067 * simply passing the input directly to the output with no regulation.
3068 */
3069int regulator_allow_bypass(struct regulator *regulator, bool enable)
3070{
3071 struct regulator_dev *rdev = regulator->rdev;
3072 int ret = 0;
3073
3074 if (!rdev->desc->ops->set_bypass)
3075 return 0;
3076
3077 if (rdev->constraints &&
3078 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3079 return 0;
3080
3081 mutex_lock(&rdev->mutex);
3082
3083 if (enable && !regulator->bypass) {
3084 rdev->bypass_count++;
3085
3086 if (rdev->bypass_count == rdev->open_count) {
3087 ret = rdev->desc->ops->set_bypass(rdev, enable);
3088 if (ret != 0)
3089 rdev->bypass_count--;
3090 }
3091
3092 } else if (!enable && regulator->bypass) {
3093 rdev->bypass_count--;
3094
3095 if (rdev->bypass_count != rdev->open_count) {
3096 ret = rdev->desc->ops->set_bypass(rdev, enable);
3097 if (ret != 0)
3098 rdev->bypass_count++;
3099 }
3100 }
3101
3102 if (ret == 0)
3103 regulator->bypass = enable;
3104
3105 mutex_unlock(&rdev->mutex);
3106
3107 return ret;
3108}
3109EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3110
3111/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003112 * regulator_register_notifier - register regulator event notifier
3113 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003114 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003115 *
3116 * Register notifier block to receive regulator events.
3117 */
3118int regulator_register_notifier(struct regulator *regulator,
3119 struct notifier_block *nb)
3120{
3121 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3122 nb);
3123}
3124EXPORT_SYMBOL_GPL(regulator_register_notifier);
3125
3126/**
3127 * regulator_unregister_notifier - unregister regulator event notifier
3128 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003129 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003130 *
3131 * Unregister regulator event notifier block.
3132 */
3133int regulator_unregister_notifier(struct regulator *regulator,
3134 struct notifier_block *nb)
3135{
3136 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3137 nb);
3138}
3139EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3140
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003141/* notify regulator consumers and downstream regulator consumers.
3142 * Note mutex must be held by caller.
3143 */
Heiko Stübner71795692014-08-28 12:36:04 -07003144static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003145 unsigned long event, void *data)
3146{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003147 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003148 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003149}
3150
3151/**
3152 * regulator_bulk_get - get multiple regulator consumers
3153 *
3154 * @dev: Device to supply
3155 * @num_consumers: Number of consumers to register
3156 * @consumers: Configuration of consumers; clients are stored here.
3157 *
3158 * @return 0 on success, an errno on failure.
3159 *
3160 * This helper function allows drivers to get several regulator
3161 * consumers in one operation. If any of the regulators cannot be
3162 * acquired then any regulators that were allocated will be freed
3163 * before returning to the caller.
3164 */
3165int regulator_bulk_get(struct device *dev, int num_consumers,
3166 struct regulator_bulk_data *consumers)
3167{
3168 int i;
3169 int ret;
3170
3171 for (i = 0; i < num_consumers; i++)
3172 consumers[i].consumer = NULL;
3173
3174 for (i = 0; i < num_consumers; i++) {
3175 consumers[i].consumer = regulator_get(dev,
3176 consumers[i].supply);
3177 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003178 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003179 dev_err(dev, "Failed to get supply '%s': %d\n",
3180 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003181 consumers[i].consumer = NULL;
3182 goto err;
3183 }
3184 }
3185
3186 return 0;
3187
3188err:
Axel Linb29c7692012-02-20 10:32:16 +08003189 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003190 regulator_put(consumers[i].consumer);
3191
3192 return ret;
3193}
3194EXPORT_SYMBOL_GPL(regulator_bulk_get);
3195
Mark Brownf21e0e82011-05-24 08:12:40 +08003196static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3197{
3198 struct regulator_bulk_data *bulk = data;
3199
3200 bulk->ret = regulator_enable(bulk->consumer);
3201}
3202
Liam Girdwood414c70c2008-04-30 15:59:04 +01003203/**
3204 * regulator_bulk_enable - enable multiple regulator consumers
3205 *
3206 * @num_consumers: Number of consumers
3207 * @consumers: Consumer data; clients are stored here.
3208 * @return 0 on success, an errno on failure
3209 *
3210 * This convenience API allows consumers to enable multiple regulator
3211 * clients in a single API call. If any consumers cannot be enabled
3212 * then any others that were enabled will be disabled again prior to
3213 * return.
3214 */
3215int regulator_bulk_enable(int num_consumers,
3216 struct regulator_bulk_data *consumers)
3217{
Dan Williams2955b472012-07-09 19:33:25 -07003218 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003219 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003220 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003221
Mark Brown6492bc12012-04-19 13:19:07 +01003222 for (i = 0; i < num_consumers; i++) {
3223 if (consumers[i].consumer->always_on)
3224 consumers[i].ret = 0;
3225 else
3226 async_schedule_domain(regulator_bulk_enable_async,
3227 &consumers[i], &async_domain);
3228 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003229
3230 async_synchronize_full_domain(&async_domain);
3231
3232 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003233 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003234 if (consumers[i].ret != 0) {
3235 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003236 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003237 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003238 }
3239
3240 return 0;
3241
3242err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003243 for (i = 0; i < num_consumers; i++) {
3244 if (consumers[i].ret < 0)
3245 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3246 consumers[i].ret);
3247 else
3248 regulator_disable(consumers[i].consumer);
3249 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003250
3251 return ret;
3252}
3253EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3254
3255/**
3256 * regulator_bulk_disable - disable multiple regulator consumers
3257 *
3258 * @num_consumers: Number of consumers
3259 * @consumers: Consumer data; clients are stored here.
3260 * @return 0 on success, an errno on failure
3261 *
3262 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003263 * clients in a single API call. If any consumers cannot be disabled
3264 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003265 * return.
3266 */
3267int regulator_bulk_disable(int num_consumers,
3268 struct regulator_bulk_data *consumers)
3269{
3270 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003271 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003272
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003273 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003274 ret = regulator_disable(consumers[i].consumer);
3275 if (ret != 0)
3276 goto err;
3277 }
3278
3279 return 0;
3280
3281err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003282 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003283 for (++i; i < num_consumers; ++i) {
3284 r = regulator_enable(consumers[i].consumer);
3285 if (r != 0)
3286 pr_err("Failed to reename %s: %d\n",
3287 consumers[i].supply, r);
3288 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003289
3290 return ret;
3291}
3292EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3293
3294/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003295 * regulator_bulk_force_disable - force disable multiple regulator consumers
3296 *
3297 * @num_consumers: Number of consumers
3298 * @consumers: Consumer data; clients are stored here.
3299 * @return 0 on success, an errno on failure
3300 *
3301 * This convenience API allows consumers to forcibly disable multiple regulator
3302 * clients in a single API call.
3303 * NOTE: This should be used for situations when device damage will
3304 * likely occur if the regulators are not disabled (e.g. over temp).
3305 * Although regulator_force_disable function call for some consumers can
3306 * return error numbers, the function is called for all consumers.
3307 */
3308int regulator_bulk_force_disable(int num_consumers,
3309 struct regulator_bulk_data *consumers)
3310{
3311 int i;
3312 int ret;
3313
3314 for (i = 0; i < num_consumers; i++)
3315 consumers[i].ret =
3316 regulator_force_disable(consumers[i].consumer);
3317
3318 for (i = 0; i < num_consumers; i++) {
3319 if (consumers[i].ret != 0) {
3320 ret = consumers[i].ret;
3321 goto out;
3322 }
3323 }
3324
3325 return 0;
3326out:
3327 return ret;
3328}
3329EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3330
3331/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003332 * regulator_bulk_free - free multiple regulator consumers
3333 *
3334 * @num_consumers: Number of consumers
3335 * @consumers: Consumer data; clients are stored here.
3336 *
3337 * This convenience API allows consumers to free multiple regulator
3338 * clients in a single API call.
3339 */
3340void regulator_bulk_free(int num_consumers,
3341 struct regulator_bulk_data *consumers)
3342{
3343 int i;
3344
3345 for (i = 0; i < num_consumers; i++) {
3346 regulator_put(consumers[i].consumer);
3347 consumers[i].consumer = NULL;
3348 }
3349}
3350EXPORT_SYMBOL_GPL(regulator_bulk_free);
3351
3352/**
3353 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003354 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003355 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003356 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003357 *
3358 * Called by regulator drivers to notify clients a regulator event has
3359 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003360 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003361 */
3362int regulator_notifier_call_chain(struct regulator_dev *rdev,
3363 unsigned long event, void *data)
3364{
3365 _notifier_call_chain(rdev, event, data);
3366 return NOTIFY_DONE;
3367
3368}
3369EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3370
Mark Brownbe721972009-08-04 20:09:52 +02003371/**
3372 * regulator_mode_to_status - convert a regulator mode into a status
3373 *
3374 * @mode: Mode to convert
3375 *
3376 * Convert a regulator mode into a status.
3377 */
3378int regulator_mode_to_status(unsigned int mode)
3379{
3380 switch (mode) {
3381 case REGULATOR_MODE_FAST:
3382 return REGULATOR_STATUS_FAST;
3383 case REGULATOR_MODE_NORMAL:
3384 return REGULATOR_STATUS_NORMAL;
3385 case REGULATOR_MODE_IDLE:
3386 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003387 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003388 return REGULATOR_STATUS_STANDBY;
3389 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003390 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003391 }
3392}
3393EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3394
David Brownell7ad68e22008-11-11 17:39:02 -08003395/*
3396 * To avoid cluttering sysfs (and memory) with useless state, only
3397 * create attributes that can be meaningfully displayed.
3398 */
3399static int add_regulator_attributes(struct regulator_dev *rdev)
3400{
Guodong Xu272e2312014-08-13 19:33:38 +08003401 struct device *dev = &rdev->dev;
3402 const struct regulator_ops *ops = rdev->desc->ops;
3403 int status = 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003404
3405 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00003406 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
Mark Brownf2889e62012-08-27 11:37:04 -07003407 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
Laxman Dewangan5a523602013-09-10 15:45:05 +05303408 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3409 (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1))) {
David Brownell7ad68e22008-11-11 17:39:02 -08003410 status = device_create_file(dev, &dev_attr_microvolts);
3411 if (status < 0)
3412 return status;
3413 }
3414 if (ops->get_current_limit) {
3415 status = device_create_file(dev, &dev_attr_microamps);
3416 if (status < 0)
3417 return status;
3418 }
3419 if (ops->get_mode) {
3420 status = device_create_file(dev, &dev_attr_opmode);
3421 if (status < 0)
3422 return status;
3423 }
Kim, Milo7b74d142013-02-18 06:50:55 +00003424 if (rdev->ena_pin || ops->is_enabled) {
David Brownell7ad68e22008-11-11 17:39:02 -08003425 status = device_create_file(dev, &dev_attr_state);
3426 if (status < 0)
3427 return status;
3428 }
David Brownell853116a2009-01-14 23:03:17 -08003429 if (ops->get_status) {
3430 status = device_create_file(dev, &dev_attr_status);
3431 if (status < 0)
3432 return status;
3433 }
Mark Brownf59c8f92012-08-31 10:36:37 -07003434 if (ops->get_bypass) {
3435 status = device_create_file(dev, &dev_attr_bypass);
3436 if (status < 0)
3437 return status;
3438 }
David Brownell7ad68e22008-11-11 17:39:02 -08003439
3440 /* some attributes are type-specific */
3441 if (rdev->desc->type == REGULATOR_CURRENT) {
3442 status = device_create_file(dev, &dev_attr_requested_microamps);
3443 if (status < 0)
3444 return status;
3445 }
3446
3447 /* all the other attributes exist to support constraints;
3448 * don't show them if there are no constraints, or if the
3449 * relevant supporting methods are missing.
3450 */
3451 if (!rdev->constraints)
3452 return status;
3453
3454 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003455 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003456 status = device_create_file(dev, &dev_attr_min_microvolts);
3457 if (status < 0)
3458 return status;
3459 status = device_create_file(dev, &dev_attr_max_microvolts);
3460 if (status < 0)
3461 return status;
3462 }
3463 if (ops->set_current_limit) {
3464 status = device_create_file(dev, &dev_attr_min_microamps);
3465 if (status < 0)
3466 return status;
3467 status = device_create_file(dev, &dev_attr_max_microamps);
3468 if (status < 0)
3469 return status;
3470 }
3471
David Brownell7ad68e22008-11-11 17:39:02 -08003472 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3473 if (status < 0)
3474 return status;
3475 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3476 if (status < 0)
3477 return status;
3478 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3479 if (status < 0)
3480 return status;
3481
3482 if (ops->set_suspend_voltage) {
3483 status = device_create_file(dev,
3484 &dev_attr_suspend_standby_microvolts);
3485 if (status < 0)
3486 return status;
3487 status = device_create_file(dev,
3488 &dev_attr_suspend_mem_microvolts);
3489 if (status < 0)
3490 return status;
3491 status = device_create_file(dev,
3492 &dev_attr_suspend_disk_microvolts);
3493 if (status < 0)
3494 return status;
3495 }
3496
3497 if (ops->set_suspend_mode) {
3498 status = device_create_file(dev,
3499 &dev_attr_suspend_standby_mode);
3500 if (status < 0)
3501 return status;
3502 status = device_create_file(dev,
3503 &dev_attr_suspend_mem_mode);
3504 if (status < 0)
3505 return status;
3506 status = device_create_file(dev,
3507 &dev_attr_suspend_disk_mode);
3508 if (status < 0)
3509 return status;
3510 }
3511
3512 return status;
3513}
3514
Mark Brown1130e5b2010-12-21 23:49:31 +00003515static void rdev_init_debugfs(struct regulator_dev *rdev)
3516{
Mark Brown1130e5b2010-12-21 23:49:31 +00003517 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003518 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003519 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003520 return;
3521 }
3522
3523 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3524 &rdev->use_count);
3525 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3526 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003527 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3528 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003529}
3530
Liam Girdwood414c70c2008-04-30 15:59:04 +01003531/**
3532 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003533 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003534 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003535 *
3536 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003537 * Returns a valid pointer to struct regulator_dev on success
3538 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003539 */
Mark Brown65f26842012-04-03 20:46:53 +01003540struct regulator_dev *
3541regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003542 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003543{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003544 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003545 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003546 static atomic_t regulator_no = ATOMIC_INIT(0);
3547 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003548 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003549 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303550 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003551
Mark Brownc1727082012-04-04 00:50:22 +01003552 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003553 return ERR_PTR(-EINVAL);
3554
Mark Brown32c8fad2012-04-11 10:19:12 +01003555 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003556 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003557
Liam Girdwood414c70c2008-04-30 15:59:04 +01003558 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3559 return ERR_PTR(-EINVAL);
3560
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003561 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3562 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003563 return ERR_PTR(-EINVAL);
3564
Mark Brown476c2d82010-12-10 17:28:07 +00003565 /* Only one of each should be implemented */
3566 WARN_ON(regulator_desc->ops->get_voltage &&
3567 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003568 WARN_ON(regulator_desc->ops->set_voltage &&
3569 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003570
3571 /* If we're using selectors we must implement list_voltage. */
3572 if (regulator_desc->ops->get_voltage_sel &&
3573 !regulator_desc->ops->list_voltage) {
3574 return ERR_PTR(-EINVAL);
3575 }
Mark Browne8eef822010-12-12 14:36:17 +00003576 if (regulator_desc->ops->set_voltage_sel &&
3577 !regulator_desc->ops->list_voltage) {
3578 return ERR_PTR(-EINVAL);
3579 }
Mark Brown476c2d82010-12-10 17:28:07 +00003580
Liam Girdwood414c70c2008-04-30 15:59:04 +01003581 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3582 if (rdev == NULL)
3583 return ERR_PTR(-ENOMEM);
3584
Mark Browna0c7b162014-09-09 23:13:57 +01003585 init_data = regulator_of_get_init_data(dev, regulator_desc,
3586 &rdev->dev.of_node);
3587 if (!init_data) {
3588 init_data = config->init_data;
3589 rdev->dev.of_node = of_node_get(config->of_node);
3590 }
3591
Liam Girdwood414c70c2008-04-30 15:59:04 +01003592 mutex_lock(&regulator_list_mutex);
3593
3594 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003595 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003596 rdev->owner = regulator_desc->owner;
3597 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003598 if (config->regmap)
3599 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303600 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003601 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303602 else if (dev->parent)
3603 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003604 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003605 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003606 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003607 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003608
Liam Girdwooda5766f12008-10-10 13:22:20 +01003609 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003610 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003611 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003612 if (ret < 0)
3613 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003614 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003615
Liam Girdwooda5766f12008-10-10 13:22:20 +01003616 /* register with sysfs */
3617 rdev->dev.class = &regulator_class;
3618 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003619 dev_set_name(&rdev->dev, "regulator.%d",
3620 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003621 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003622 if (ret != 0) {
3623 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003624 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003625 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003626
3627 dev_set_drvdata(&rdev->dev, rdev);
3628
Markus Pargmann76f439d2014-10-08 15:47:05 +02003629 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3630 gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003631 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003632 if (ret != 0) {
3633 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3634 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003635 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003636 }
3637
Mark Brown65f73502012-06-27 14:14:38 +01003638 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3639 rdev->ena_gpio_state = 1;
3640
Kim, Milo7b74d142013-02-18 06:50:55 +00003641 if (config->ena_gpio_invert)
Mark Brown65f73502012-06-27 14:14:38 +01003642 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3643 }
3644
Mike Rapoport74f544c2008-11-25 14:53:53 +02003645 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003646 if (init_data)
3647 constraints = &init_data->constraints;
3648
3649 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003650 if (ret < 0)
3651 goto scrub;
3652
David Brownell7ad68e22008-11-11 17:39:02 -08003653 /* add attributes supported by this regulator */
3654 ret = add_regulator_attributes(rdev);
3655 if (ret < 0)
3656 goto scrub;
3657
Mark Brown9a8f5e02011-11-29 18:11:19 +00003658 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303659 supply = init_data->supply_regulator;
3660 else if (regulator_desc->supply_name)
3661 supply = regulator_desc->supply_name;
3662
3663 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003664 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003665
Mark Brown6d191a52012-03-29 14:19:02 +01003666 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003667
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003668 if (ret == -ENODEV) {
3669 /*
3670 * No supply was specified for this regulator and
3671 * there will never be one.
3672 */
3673 ret = 0;
3674 goto add_dev;
3675 } else if (!r) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05303676 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003677 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003678 goto scrub;
3679 }
3680
3681 ret = set_supply(rdev, r);
3682 if (ret < 0)
3683 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303684
3685 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003686 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303687 ret = regulator_enable(rdev->supply);
3688 if (ret < 0)
3689 goto scrub;
3690 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003691 }
3692
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003693add_dev:
Liam Girdwooda5766f12008-10-10 13:22:20 +01003694 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003695 if (init_data) {
3696 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3697 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003698 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003699 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003700 if (ret < 0) {
3701 dev_err(dev, "Failed to set supply %s\n",
3702 init_data->consumer_supplies[i].supply);
3703 goto unset_supplies;
3704 }
Mark Brown23c2f042011-02-24 17:39:09 +00003705 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003706 }
3707
3708 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003709
3710 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003711out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003712 mutex_unlock(&regulator_list_mutex);
3713 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003714
Jani Nikulad4033b52010-04-29 10:55:11 +03003715unset_supplies:
3716 unset_regulator_supplies(rdev);
3717
David Brownell4fca9542008-11-11 17:38:53 -08003718scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003719 if (rdev->supply)
Charles Keepax23ff2f02012-11-14 09:39:31 +00003720 _regulator_put(rdev->supply);
Kim, Milof19b00d2013-02-18 06:50:39 +00003721 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003722 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003723wash:
David Brownell4fca9542008-11-11 17:38:53 -08003724 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003725 /* device core frees rdev */
3726 rdev = ERR_PTR(ret);
3727 goto out;
3728
David Brownell4fca9542008-11-11 17:38:53 -08003729clean:
3730 kfree(rdev);
3731 rdev = ERR_PTR(ret);
3732 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003733}
3734EXPORT_SYMBOL_GPL(regulator_register);
3735
3736/**
3737 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003738 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003739 *
3740 * Called by regulator drivers to unregister a regulator.
3741 */
3742void regulator_unregister(struct regulator_dev *rdev)
3743{
3744 if (rdev == NULL)
3745 return;
3746
Mark Brown891636e2013-07-08 09:14:45 +01003747 if (rdev->supply) {
3748 while (rdev->use_count--)
3749 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01003750 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01003751 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003752 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003753 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003754 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003755 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003756 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003757 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003758 kfree(rdev->constraints);
Kim, Milof19b00d2013-02-18 06:50:39 +00003759 regulator_ena_gpio_free(rdev);
Charles Keepax63c7c9e2014-04-03 15:32:17 +01003760 of_node_put(rdev->dev.of_node);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003761 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003762 mutex_unlock(&regulator_list_mutex);
3763}
3764EXPORT_SYMBOL_GPL(regulator_unregister);
3765
3766/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003767 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003768 * @state: system suspend state
3769 *
3770 * Configure each regulator with it's suspend operating parameters for state.
3771 * This will usually be called by machine suspend code prior to supending.
3772 */
3773int regulator_suspend_prepare(suspend_state_t state)
3774{
3775 struct regulator_dev *rdev;
3776 int ret = 0;
3777
3778 /* ON is handled by regulator active state */
3779 if (state == PM_SUSPEND_ON)
3780 return -EINVAL;
3781
3782 mutex_lock(&regulator_list_mutex);
3783 list_for_each_entry(rdev, &regulator_list, list) {
3784
3785 mutex_lock(&rdev->mutex);
3786 ret = suspend_prepare(rdev, state);
3787 mutex_unlock(&rdev->mutex);
3788
3789 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003790 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003791 goto out;
3792 }
3793 }
3794out:
3795 mutex_unlock(&regulator_list_mutex);
3796 return ret;
3797}
3798EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3799
3800/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003801 * regulator_suspend_finish - resume regulators from system wide suspend
3802 *
3803 * Turn on regulators that might be turned off by regulator_suspend_prepare
3804 * and that should be turned on according to the regulators properties.
3805 */
3806int regulator_suspend_finish(void)
3807{
3808 struct regulator_dev *rdev;
3809 int ret = 0, error;
3810
3811 mutex_lock(&regulator_list_mutex);
3812 list_for_each_entry(rdev, &regulator_list, list) {
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003813 mutex_lock(&rdev->mutex);
Markus Pargmann30c21972014-02-20 17:36:03 +01003814 if (rdev->use_count > 0 || rdev->constraints->always_on) {
3815 error = _regulator_do_enable(rdev);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003816 if (error)
3817 ret = error;
3818 } else {
Mark Brown87b28412013-11-27 16:13:10 +00003819 if (!have_full_constraints())
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003820 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003821 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003822 goto unlock;
3823
Markus Pargmann66fda752014-02-20 17:36:04 +01003824 error = _regulator_do_disable(rdev);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003825 if (error)
3826 ret = error;
3827 }
3828unlock:
3829 mutex_unlock(&rdev->mutex);
3830 }
3831 mutex_unlock(&regulator_list_mutex);
3832 return ret;
3833}
3834EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3835
3836/**
Mark Brownca725562009-03-16 19:36:34 +00003837 * regulator_has_full_constraints - the system has fully specified constraints
3838 *
3839 * Calling this function will cause the regulator API to disable all
3840 * regulators which have a zero use count and don't have an always_on
3841 * constraint in a late_initcall.
3842 *
3843 * The intention is that this will become the default behaviour in a
3844 * future kernel release so users are encouraged to use this facility
3845 * now.
3846 */
3847void regulator_has_full_constraints(void)
3848{
3849 has_full_constraints = 1;
3850}
3851EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3852
3853/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003854 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003855 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003856 *
3857 * Get rdev regulator driver private data. This call can be used in the
3858 * regulator driver context.
3859 */
3860void *rdev_get_drvdata(struct regulator_dev *rdev)
3861{
3862 return rdev->reg_data;
3863}
3864EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3865
3866/**
3867 * regulator_get_drvdata - get regulator driver data
3868 * @regulator: regulator
3869 *
3870 * Get regulator driver private data. This call can be used in the consumer
3871 * driver context when non API regulator specific functions need to be called.
3872 */
3873void *regulator_get_drvdata(struct regulator *regulator)
3874{
3875 return regulator->rdev->reg_data;
3876}
3877EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3878
3879/**
3880 * regulator_set_drvdata - set regulator driver data
3881 * @regulator: regulator
3882 * @data: data
3883 */
3884void regulator_set_drvdata(struct regulator *regulator, void *data)
3885{
3886 regulator->rdev->reg_data = data;
3887}
3888EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3889
3890/**
3891 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003892 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003893 */
3894int rdev_get_id(struct regulator_dev *rdev)
3895{
3896 return rdev->desc->id;
3897}
3898EXPORT_SYMBOL_GPL(rdev_get_id);
3899
Liam Girdwooda5766f12008-10-10 13:22:20 +01003900struct device *rdev_get_dev(struct regulator_dev *rdev)
3901{
3902 return &rdev->dev;
3903}
3904EXPORT_SYMBOL_GPL(rdev_get_dev);
3905
3906void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3907{
3908 return reg_init_data->driver_data;
3909}
3910EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3911
Mark Brownba55a972011-08-23 17:39:10 +01003912#ifdef CONFIG_DEBUG_FS
3913static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3914 size_t count, loff_t *ppos)
3915{
3916 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3917 ssize_t len, ret = 0;
3918 struct regulator_map *map;
3919
3920 if (!buf)
3921 return -ENOMEM;
3922
3923 list_for_each_entry(map, &regulator_map_list, list) {
3924 len = snprintf(buf + ret, PAGE_SIZE - ret,
3925 "%s -> %s.%s\n",
3926 rdev_get_name(map->regulator), map->dev_name,
3927 map->supply);
3928 if (len >= 0)
3929 ret += len;
3930 if (ret > PAGE_SIZE) {
3931 ret = PAGE_SIZE;
3932 break;
3933 }
3934 }
3935
3936 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3937
3938 kfree(buf);
3939
3940 return ret;
3941}
Stephen Boyd24751432012-02-20 22:50:42 -08003942#endif
Mark Brownba55a972011-08-23 17:39:10 +01003943
3944static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003945#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003946 .read = supply_map_read_file,
3947 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003948#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003949};
Mark Brownba55a972011-08-23 17:39:10 +01003950
Liam Girdwood414c70c2008-04-30 15:59:04 +01003951static int __init regulator_init(void)
3952{
Mark Brown34abbd62010-02-12 10:18:08 +00003953 int ret;
3954
Mark Brown34abbd62010-02-12 10:18:08 +00003955 ret = class_register(&regulator_class);
3956
Mark Brown1130e5b2010-12-21 23:49:31 +00003957 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003958 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003959 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003960
Mark Brownf4d562c2012-02-20 21:01:04 +00003961 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3962 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003963
Mark Brown34abbd62010-02-12 10:18:08 +00003964 regulator_dummy_init();
3965
3966 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003967}
3968
3969/* init early to allow our consumers to complete system booting */
3970core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003971
3972static int __init regulator_init_complete(void)
3973{
3974 struct regulator_dev *rdev;
Guodong Xu272e2312014-08-13 19:33:38 +08003975 const struct regulator_ops *ops;
Mark Brownca725562009-03-16 19:36:34 +00003976 struct regulation_constraints *c;
3977 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003978
Mark Brown86f5fcf2012-07-06 18:19:13 +01003979 /*
3980 * Since DT doesn't provide an idiomatic mechanism for
3981 * enabling full constraints and since it's much more natural
3982 * with DT to provide them just assume that a DT enabled
3983 * system has full constraints.
3984 */
3985 if (of_have_populated_dt())
3986 has_full_constraints = true;
3987
Mark Brownca725562009-03-16 19:36:34 +00003988 mutex_lock(&regulator_list_mutex);
3989
3990 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01003991 * we have permission to change the status for and which are
3992 * not in use or always_on. This is effectively the default
3993 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00003994 */
3995 list_for_each_entry(rdev, &regulator_list, list) {
3996 ops = rdev->desc->ops;
3997 c = rdev->constraints;
3998
Markus Pargmann66fda752014-02-20 17:36:04 +01003999 if (c && c->always_on)
Mark Brownca725562009-03-16 19:36:34 +00004000 continue;
4001
Mark Browne9535832014-06-01 19:15:16 +01004002 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4003 continue;
4004
Mark Brownca725562009-03-16 19:36:34 +00004005 mutex_lock(&rdev->mutex);
4006
4007 if (rdev->use_count)
4008 goto unlock;
4009
4010 /* If we can't read the status assume it's on. */
4011 if (ops->is_enabled)
4012 enabled = ops->is_enabled(rdev);
4013 else
4014 enabled = 1;
4015
4016 if (!enabled)
4017 goto unlock;
4018
Mark Brown87b28412013-11-27 16:13:10 +00004019 if (have_full_constraints()) {
Mark Brownca725562009-03-16 19:36:34 +00004020 /* We log since this may kill the system if it
4021 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08004022 rdev_info(rdev, "disabling\n");
Markus Pargmann66fda752014-02-20 17:36:04 +01004023 ret = _regulator_do_disable(rdev);
Jingoo Han0d25d092014-01-08 10:02:16 +09004024 if (ret != 0)
Joe Perches5da84fd2010-11-30 05:53:48 -08004025 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00004026 } else {
4027 /* The intention is that in future we will
4028 * assume that full constraints are provided
4029 * so warn even if we aren't going to do
4030 * anything here.
4031 */
Joe Perches5da84fd2010-11-30 05:53:48 -08004032 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00004033 }
4034
4035unlock:
4036 mutex_unlock(&rdev->mutex);
4037 }
4038
4039 mutex_unlock(&regulator_list_mutex);
4040
4041 return 0;
4042}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004043late_initcall_sync(regulator_init_complete);