blob: a291a6b9cd44815daeb77f90836d524396c50a61 [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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100635/* Calculate the new optimum regulator operating mode based on the new total
636 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800637static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100638{
639 struct regulator *sibling;
640 int current_uA = 0, output_uV, input_uV, err;
641 unsigned int mode;
642
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800643 /*
644 * first check to see if we can set modes at all, otherwise just
645 * tell the consumer everything is OK.
646 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100647 err = regulator_check_drms(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800648 if (err < 0)
649 return 0;
650
651 if (!rdev->desc->ops->get_optimum_mode)
652 return 0;
653
654 if (!rdev->desc->ops->set_mode)
655 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100656
657 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000658 output_uV = _regulator_get_voltage(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800659 if (output_uV <= 0) {
660 rdev_err(rdev, "invalid output voltage found\n");
661 return -EINVAL;
662 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100663
664 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000665 input_uV = 0;
666 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800667 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000668 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100669 input_uV = rdev->constraints->input_uV;
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800670 if (input_uV <= 0) {
671 rdev_err(rdev, "invalid input voltage found\n");
672 return -EINVAL;
673 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100674
675 /* calc total requested load */
676 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100677 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100678
679 /* now get the optimum mode for our new total regulator load */
680 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
681 output_uV, current_uA);
682
683 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900684 err = regulator_mode_constrain(rdev, &mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800685 if (err < 0) {
686 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
687 current_uA, input_uV, output_uV);
688 return err;
689 }
690
691 err = rdev->desc->ops->set_mode(rdev, mode);
692 if (err < 0)
693 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
694
695 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100696}
697
698static int suspend_set_state(struct regulator_dev *rdev,
699 struct regulator_state *rstate)
700{
701 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100702
703 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800704 * only warn if the driver implements set_suspend_voltage or
705 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100706 */
707 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800708 if (rdev->desc->ops->set_suspend_voltage ||
709 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800710 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100711 return 0;
712 }
713
714 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800715 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100716 return -EINVAL;
717 }
718
Axel Lin8ac0e952012-04-14 09:14:34 +0800719 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100720 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800721 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100722 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800723 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
724 ret = 0;
725
Liam Girdwood414c70c2008-04-30 15:59:04 +0100726 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800727 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728 return ret;
729 }
730
731 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
732 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
733 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800734 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100735 return ret;
736 }
737 }
738
739 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
740 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
741 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800742 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100743 return ret;
744 }
745 }
746 return ret;
747}
748
749/* locks held by caller */
750static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
751{
752 if (!rdev->constraints)
753 return -EINVAL;
754
755 switch (state) {
756 case PM_SUSPEND_STANDBY:
757 return suspend_set_state(rdev,
758 &rdev->constraints->state_standby);
759 case PM_SUSPEND_MEM:
760 return suspend_set_state(rdev,
761 &rdev->constraints->state_mem);
762 case PM_SUSPEND_MAX:
763 return suspend_set_state(rdev,
764 &rdev->constraints->state_disk);
765 default:
766 return -EINVAL;
767 }
768}
769
770static void print_constraints(struct regulator_dev *rdev)
771{
772 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000773 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100774 int count = 0;
775 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100776
Mark Brown8f031b42009-10-22 16:31:31 +0100777 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100778 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100779 count += sprintf(buf + count, "%d mV ",
780 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100781 else
Mark Brown8f031b42009-10-22 16:31:31 +0100782 count += sprintf(buf + count, "%d <--> %d mV ",
783 constraints->min_uV / 1000,
784 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100785 }
Mark Brown8f031b42009-10-22 16:31:31 +0100786
787 if (!constraints->min_uV ||
788 constraints->min_uV != constraints->max_uV) {
789 ret = _regulator_get_voltage(rdev);
790 if (ret > 0)
791 count += sprintf(buf + count, "at %d mV ", ret / 1000);
792 }
793
Mark Brownbf5892a2011-05-08 22:13:37 +0100794 if (constraints->uV_offset)
795 count += sprintf(buf, "%dmV offset ",
796 constraints->uV_offset / 1000);
797
Mark Brown8f031b42009-10-22 16:31:31 +0100798 if (constraints->min_uA && constraints->max_uA) {
799 if (constraints->min_uA == constraints->max_uA)
800 count += sprintf(buf + count, "%d mA ",
801 constraints->min_uA / 1000);
802 else
803 count += sprintf(buf + count, "%d <--> %d mA ",
804 constraints->min_uA / 1000,
805 constraints->max_uA / 1000);
806 }
807
808 if (!constraints->min_uA ||
809 constraints->min_uA != constraints->max_uA) {
810 ret = _regulator_get_current_limit(rdev);
811 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400812 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100813 }
814
Liam Girdwood414c70c2008-04-30 15:59:04 +0100815 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
816 count += sprintf(buf + count, "fast ");
817 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
818 count += sprintf(buf + count, "normal ");
819 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
820 count += sprintf(buf + count, "idle ");
821 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
822 count += sprintf(buf + count, "standby");
823
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200824 if (!count)
825 sprintf(buf, "no parameters");
826
Mark Brown194dbae2014-10-31 19:11:59 +0000827 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000828
829 if ((constraints->min_uV != constraints->max_uV) &&
830 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
831 rdev_warn(rdev,
832 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100833}
834
Mark Browne79055d2009-10-19 15:53:50 +0100835static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100836 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100837{
Guodong Xu272e2312014-08-13 19:33:38 +0800838 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100839 int ret;
840
841 /* do we need to apply the constraint voltage */
842 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000843 rdev->constraints->min_uV == rdev->constraints->max_uV) {
Alban Bedel064d5cd2014-05-20 12:12:16 +0200844 int current_uV = _regulator_get_voltage(rdev);
845 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500846 rdev_err(rdev,
847 "failed to get the current voltage(%d)\n",
848 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200849 return current_uV;
850 }
851 if (current_uV < rdev->constraints->min_uV ||
852 current_uV > rdev->constraints->max_uV) {
853 ret = _regulator_do_set_voltage(
854 rdev, rdev->constraints->min_uV,
855 rdev->constraints->max_uV);
856 if (ret < 0) {
857 rdev_err(rdev,
Nishanth Menon69d58832014-06-04 14:53:25 -0500858 "failed to apply %duV constraint(%d)\n",
859 rdev->constraints->min_uV, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200860 return ret;
861 }
Mark Brown75790252010-12-12 14:25:50 +0000862 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100863 }
Mark Browne79055d2009-10-19 15:53:50 +0100864
865 /* constrain machine-level voltage specs to fit
866 * the actual range supported by this regulator.
867 */
868 if (ops->list_voltage && rdev->desc->n_voltages) {
869 int count = rdev->desc->n_voltages;
870 int i;
871 int min_uV = INT_MAX;
872 int max_uV = INT_MIN;
873 int cmin = constraints->min_uV;
874 int cmax = constraints->max_uV;
875
876 /* it's safe to autoconfigure fixed-voltage supplies
877 and the constraints are used by list_voltage. */
878 if (count == 1 && !cmin) {
879 cmin = 1;
880 cmax = INT_MAX;
881 constraints->min_uV = cmin;
882 constraints->max_uV = cmax;
883 }
884
885 /* voltage constraints are optional */
886 if ((cmin == 0) && (cmax == 0))
887 return 0;
888
889 /* else require explicit machine-level constraints */
890 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800891 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100892 return -EINVAL;
893 }
894
895 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
896 for (i = 0; i < count; i++) {
897 int value;
898
899 value = ops->list_voltage(rdev, i);
900 if (value <= 0)
901 continue;
902
903 /* maybe adjust [min_uV..max_uV] */
904 if (value >= cmin && value < min_uV)
905 min_uV = value;
906 if (value <= cmax && value > max_uV)
907 max_uV = value;
908 }
909
910 /* final: [min_uV..max_uV] valid iff constraints valid */
911 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000912 rdev_err(rdev,
913 "unsupportable voltage constraints %u-%uuV\n",
914 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100915 return -EINVAL;
916 }
917
918 /* use regulator's subset of machine constraints */
919 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800920 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
921 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100922 constraints->min_uV = min_uV;
923 }
924 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800925 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
926 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100927 constraints->max_uV = max_uV;
928 }
929 }
930
931 return 0;
932}
933
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530934static int machine_constraints_current(struct regulator_dev *rdev,
935 struct regulation_constraints *constraints)
936{
Guodong Xu272e2312014-08-13 19:33:38 +0800937 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530938 int ret;
939
940 if (!constraints->min_uA && !constraints->max_uA)
941 return 0;
942
943 if (constraints->min_uA > constraints->max_uA) {
944 rdev_err(rdev, "Invalid current constraints\n");
945 return -EINVAL;
946 }
947
948 if (!ops->set_current_limit || !ops->get_current_limit) {
949 rdev_warn(rdev, "Operation of current configuration missing\n");
950 return 0;
951 }
952
953 /* Set regulator current in constraints range */
954 ret = ops->set_current_limit(rdev, constraints->min_uA,
955 constraints->max_uA);
956 if (ret < 0) {
957 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
958 return ret;
959 }
960
961 return 0;
962}
963
Markus Pargmann30c21972014-02-20 17:36:03 +0100964static int _regulator_do_enable(struct regulator_dev *rdev);
965
Liam Girdwooda5766f12008-10-10 13:22:20 +0100966/**
967 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000968 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000969 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100970 *
971 * Allows platform initialisation code to define and constrain
972 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
973 * Constraints *must* be set by platform code in order for some
974 * regulator operations to proceed i.e. set_voltage, set_current_limit,
975 * set_mode.
976 */
977static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000978 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100979{
980 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +0800981 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100982
Mark Brown9a8f5e02011-11-29 18:11:19 +0000983 if (constraints)
984 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
985 GFP_KERNEL);
986 else
987 rdev->constraints = kzalloc(sizeof(*constraints),
988 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000989 if (!rdev->constraints)
990 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100991
Mark Brownf8c12fe2010-11-29 15:55:17 +0000992 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100993 if (ret != 0)
994 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800995
Laxman Dewanganf8c17002013-09-20 13:13:02 +0530996 ret = machine_constraints_current(rdev, rdev->constraints);
997 if (ret != 0)
998 goto out;
999
Liam Girdwooda5766f12008-10-10 13:22:20 +01001000 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001001 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +00001002 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001003 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001004 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +01001005 goto out;
1006 }
1007 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001008
Mark Brown9a8f5e02011-11-29 18:11:19 +00001009 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001010 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001011 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +00001012 ret = -EINVAL;
1013 goto out;
1014 }
1015
Mark Brownf8c12fe2010-11-29 15:55:17 +00001016 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001017 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001018 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +00001019 goto out;
1020 }
1021 }
1022
Mark Browncacf90f2009-03-02 16:32:46 +00001023 /* If the constraints say the regulator should be on at this point
1024 * and we have control then make sure it is enabled.
1025 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001026 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1027 ret = _regulator_do_enable(rdev);
1028 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001029 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +01001030 goto out;
1031 }
1032 }
1033
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301034 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1035 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301036 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1037 if (ret < 0) {
1038 rdev_err(rdev, "failed to set ramp_delay\n");
1039 goto out;
1040 }
1041 }
1042
Liam Girdwooda5766f12008-10-10 13:22:20 +01001043 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08001044 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001045out:
Axel Lin1a6958e72011-07-15 10:50:43 +08001046 kfree(rdev->constraints);
1047 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001048 return ret;
1049}
1050
1051/**
1052 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001053 * @rdev: regulator name
1054 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001055 *
1056 * Called by platform initialisation code to set the supply regulator for this
1057 * regulator. This ensures that a regulators supply will also be enabled by the
1058 * core if it's child is enabled.
1059 */
1060static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001061 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001062{
1063 int err;
1064
Mark Brown3801b862011-06-09 16:22:22 +01001065 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1066
1067 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001068 if (rdev->supply == NULL) {
1069 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001070 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001071 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301072 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001073
1074 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001075}
1076
1077/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001078 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001079 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001080 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001081 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001082 *
1083 * Allows platform initialisation code to map physical regulator
1084 * sources to symbolic names for supplies for use by devices. Devices
1085 * should use these symbolic names to request regulators, avoiding the
1086 * need to provide board-specific regulator names as platform data.
1087 */
1088static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001089 const char *consumer_dev_name,
1090 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001091{
1092 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001093 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001094
1095 if (supply == NULL)
1096 return -EINVAL;
1097
Mark Brown9ed20992009-07-21 16:00:26 +01001098 if (consumer_dev_name != NULL)
1099 has_dev = 1;
1100 else
1101 has_dev = 0;
1102
David Brownell6001e132008-12-31 12:54:19 +00001103 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001104 if (node->dev_name && consumer_dev_name) {
1105 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1106 continue;
1107 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001108 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001109 }
1110
David Brownell6001e132008-12-31 12:54:19 +00001111 if (strcmp(node->supply, supply) != 0)
1112 continue;
1113
Mark Brown737f3602012-02-02 00:10:51 +00001114 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1115 consumer_dev_name,
1116 dev_name(&node->regulator->dev),
1117 node->regulator->desc->name,
1118 supply,
1119 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001120 return -EBUSY;
1121 }
1122
Mark Brown9ed20992009-07-21 16:00:26 +01001123 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001124 if (node == NULL)
1125 return -ENOMEM;
1126
1127 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001128 node->supply = supply;
1129
Mark Brown9ed20992009-07-21 16:00:26 +01001130 if (has_dev) {
1131 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1132 if (node->dev_name == NULL) {
1133 kfree(node);
1134 return -ENOMEM;
1135 }
Mark Brown40f92442009-06-17 17:56:39 +01001136 }
1137
Liam Girdwooda5766f12008-10-10 13:22:20 +01001138 list_add(&node->list, &regulator_map_list);
1139 return 0;
1140}
1141
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001142static void unset_regulator_supplies(struct regulator_dev *rdev)
1143{
1144 struct regulator_map *node, *n;
1145
1146 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1147 if (rdev == node->regulator) {
1148 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001149 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001150 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001151 }
1152 }
1153}
1154
Mark Brownf5726ae2011-06-09 16:22:20 +01001155#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001156
1157static struct regulator *create_regulator(struct regulator_dev *rdev,
1158 struct device *dev,
1159 const char *supply_name)
1160{
1161 struct regulator *regulator;
1162 char buf[REG_STR_SIZE];
1163 int err, size;
1164
1165 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1166 if (regulator == NULL)
1167 return NULL;
1168
1169 mutex_lock(&rdev->mutex);
1170 regulator->rdev = rdev;
1171 list_add(&regulator->list, &rdev->consumer_list);
1172
1173 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001174 regulator->dev = dev;
1175
Mark Brown222cc7b2012-06-22 11:39:16 +01001176 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001177 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1178 dev->kobj.name, supply_name);
1179 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001180 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001181
1182 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1183 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001184 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001185
1186 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1187 buf);
1188 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001189 rdev_warn(rdev, "could not add device link %s err %d\n",
1190 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001191 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001192 }
Mark Brown5de70512011-06-19 13:33:16 +01001193 } else {
1194 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1195 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001196 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001197 }
Mark Brown5de70512011-06-19 13:33:16 +01001198
Mark Brown5de70512011-06-19 13:33:16 +01001199 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1200 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001201 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001202 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001203 } else {
1204 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1205 &regulator->uA_load);
1206 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1207 &regulator->min_uV);
1208 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1209 &regulator->max_uV);
1210 }
Mark Brown5de70512011-06-19 13:33:16 +01001211
Mark Brown6492bc12012-04-19 13:19:07 +01001212 /*
1213 * Check now if the regulator is an always on regulator - if
1214 * it is then we don't need to do nearly so much work for
1215 * enable/disable calls.
1216 */
1217 if (!_regulator_can_change_status(rdev) &&
1218 _regulator_is_enabled(rdev))
1219 regulator->always_on = true;
1220
Liam Girdwood414c70c2008-04-30 15:59:04 +01001221 mutex_unlock(&rdev->mutex);
1222 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001223overflow_err:
1224 list_del(&regulator->list);
1225 kfree(regulator);
1226 mutex_unlock(&rdev->mutex);
1227 return NULL;
1228}
1229
Mark Brown31aae2b2009-12-21 12:21:52 +00001230static int _regulator_get_enable_time(struct regulator_dev *rdev)
1231{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301232 if (rdev->constraints && rdev->constraints->enable_time)
1233 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001234 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001235 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001236 return rdev->desc->ops->enable_time(rdev);
1237}
1238
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001239static struct regulator_supply_alias *regulator_find_supply_alias(
1240 struct device *dev, const char *supply)
1241{
1242 struct regulator_supply_alias *map;
1243
1244 list_for_each_entry(map, &regulator_supply_alias_list, list)
1245 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1246 return map;
1247
1248 return NULL;
1249}
1250
1251static void regulator_supply_alias(struct device **dev, const char **supply)
1252{
1253 struct regulator_supply_alias *map;
1254
1255 map = regulator_find_supply_alias(*dev, *supply);
1256 if (map) {
1257 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1258 *supply, map->alias_supply,
1259 dev_name(map->alias_dev));
1260 *dev = map->alias_dev;
1261 *supply = map->alias_supply;
1262 }
1263}
1264
Rajendra Nayak69511a42011-11-18 16:47:20 +05301265static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001266 const char *supply,
1267 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301268{
1269 struct regulator_dev *r;
1270 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001271 struct regulator_map *map;
1272 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301273
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001274 regulator_supply_alias(&dev, &supply);
1275
Rajendra Nayak69511a42011-11-18 16:47:20 +05301276 /* first do a dt based lookup */
1277 if (dev && dev->of_node) {
1278 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001279 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301280 list_for_each_entry(r, &regulator_list, list)
1281 if (r->dev.parent &&
1282 node == r->dev.of_node)
1283 return r;
Mark Brown317b5682014-01-27 17:34:07 +00001284 *ret = -EPROBE_DEFER;
1285 return NULL;
Mark Brown6d191a52012-03-29 14:19:02 +01001286 } else {
1287 /*
1288 * If we couldn't even get the node then it's
1289 * not just that the device didn't register
1290 * yet, there's no node and we'll never
1291 * succeed.
1292 */
1293 *ret = -ENODEV;
1294 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301295 }
1296
1297 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001298 if (dev)
1299 devname = dev_name(dev);
1300
Rajendra Nayak69511a42011-11-18 16:47:20 +05301301 list_for_each_entry(r, &regulator_list, list)
1302 if (strcmp(rdev_get_name(r), supply) == 0)
1303 return r;
1304
Mark Brown576ca4362012-03-30 12:24:37 +01001305 list_for_each_entry(map, &regulator_map_list, list) {
1306 /* If the mapping has a device set up it must match */
1307 if (map->dev_name &&
1308 (!devname || strcmp(map->dev_name, devname)))
1309 continue;
1310
1311 if (strcmp(map->supply, supply) == 0)
1312 return map->regulator;
1313 }
1314
1315
Rajendra Nayak69511a42011-11-18 16:47:20 +05301316 return NULL;
1317}
1318
Bjorn Andersson6261b062015-03-24 18:56:05 -07001319static int regulator_resolve_supply(struct regulator_dev *rdev)
1320{
1321 struct regulator_dev *r;
1322 struct device *dev = rdev->dev.parent;
1323 int ret;
1324
1325 /* No supply to resovle? */
1326 if (!rdev->supply_name)
1327 return 0;
1328
1329 /* Supply already resolved? */
1330 if (rdev->supply)
1331 return 0;
1332
1333 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1334 if (ret == -ENODEV) {
1335 /*
1336 * No supply was specified for this regulator and
1337 * there will never be one.
1338 */
1339 return 0;
1340 }
1341
1342 if (!r) {
1343 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1344 rdev->supply_name, rdev->desc->name);
1345 return -EPROBE_DEFER;
1346 }
1347
1348 /* Recursively resolve the supply of the supply */
1349 ret = regulator_resolve_supply(r);
1350 if (ret < 0)
1351 return ret;
1352
1353 ret = set_supply(rdev, r);
1354 if (ret < 0)
1355 return ret;
1356
1357 /* Cascade always-on state to supply */
1358 if (_regulator_is_enabled(rdev)) {
1359 ret = regulator_enable(rdev->supply);
1360 if (ret < 0)
1361 return ret;
1362 }
1363
1364 return 0;
1365}
1366
Mark Brown5ffbd132009-07-21 16:00:23 +01001367/* Internal regulator request function */
1368static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown4ddfebd2013-09-13 19:50:37 +01001369 bool exclusive, bool allow_dummy)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001370{
1371 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001372 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001373 const char *devname = NULL;
Mark Brown317b5682014-01-27 17:34:07 +00001374 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001375
1376 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001377 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001378 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001379 }
1380
Mark Brown40f92442009-06-17 17:56:39 +01001381 if (dev)
1382 devname = dev_name(dev);
1383
Mark Brown317b5682014-01-27 17:34:07 +00001384 if (have_full_constraints())
1385 ret = -ENODEV;
1386 else
1387 ret = -EPROBE_DEFER;
1388
Liam Girdwood414c70c2008-04-30 15:59:04 +01001389 mutex_lock(&regulator_list_mutex);
1390
Mark Brown6d191a52012-03-29 14:19:02 +01001391 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301392 if (rdev)
1393 goto found;
1394
Mark Brownef60abb2013-09-23 16:12:52 +01001395 regulator = ERR_PTR(ret);
1396
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001397 /*
1398 * If we have return value from dev_lookup fail, we do not expect to
1399 * succeed, so, quit with appropriate error value
1400 */
Jingoo Han0d25d092014-01-08 10:02:16 +09001401 if (ret && ret != -ENODEV)
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001402 goto out;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001403
Mark Brown34abbd62010-02-12 10:18:08 +00001404 if (!devname)
1405 devname = "deviceless";
1406
Mark Brown4ddfebd2013-09-13 19:50:37 +01001407 /*
1408 * Assume that a regulator is physically present and enabled
1409 * even if it isn't hooked up and just provide a dummy.
Mark Brown34abbd62010-02-12 10:18:08 +00001410 */
Mark Brown87b28412013-11-27 16:13:10 +00001411 if (have_full_constraints() && allow_dummy) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001412 pr_warn("%s supply %s not found, using dummy regulator\n",
1413 devname, id);
Mark Brown4ddfebd2013-09-13 19:50:37 +01001414
Mark Brown34abbd62010-02-12 10:18:08 +00001415 rdev = dummy_regulator_rdev;
1416 goto found;
Hans de Goede07817192013-12-17 16:24:57 +01001417 /* Don't log an error when called from regulator_get_optional() */
1418 } else if (!have_full_constraints() || exclusive) {
Shuah Khanacc3d5c2014-02-20 12:58:15 -07001419 dev_warn(dev, "dummy supplies not allowed\n");
Mark Brown34abbd62010-02-12 10:18:08 +00001420 }
Mark Brown34abbd62010-02-12 10:18:08 +00001421
Liam Girdwood414c70c2008-04-30 15:59:04 +01001422 mutex_unlock(&regulator_list_mutex);
1423 return regulator;
1424
1425found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001426 if (rdev->exclusive) {
1427 regulator = ERR_PTR(-EPERM);
1428 goto out;
1429 }
1430
1431 if (exclusive && rdev->open_count) {
1432 regulator = ERR_PTR(-EBUSY);
1433 goto out;
1434 }
1435
Bjorn Andersson6261b062015-03-24 18:56:05 -07001436 ret = regulator_resolve_supply(rdev);
1437 if (ret < 0) {
1438 regulator = ERR_PTR(ret);
1439 goto out;
1440 }
1441
Liam Girdwooda5766f12008-10-10 13:22:20 +01001442 if (!try_module_get(rdev->owner))
1443 goto out;
1444
Liam Girdwood414c70c2008-04-30 15:59:04 +01001445 regulator = create_regulator(rdev, dev, id);
1446 if (regulator == NULL) {
1447 regulator = ERR_PTR(-ENOMEM);
1448 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001449 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001450 }
1451
Mark Brown5ffbd132009-07-21 16:00:23 +01001452 rdev->open_count++;
1453 if (exclusive) {
1454 rdev->exclusive = 1;
1455
1456 ret = _regulator_is_enabled(rdev);
1457 if (ret > 0)
1458 rdev->use_count = 1;
1459 else
1460 rdev->use_count = 0;
1461 }
1462
Liam Girdwooda5766f12008-10-10 13:22:20 +01001463out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001464 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001465
Liam Girdwood414c70c2008-04-30 15:59:04 +01001466 return regulator;
1467}
Mark Brown5ffbd132009-07-21 16:00:23 +01001468
1469/**
1470 * regulator_get - lookup and obtain a reference to a regulator.
1471 * @dev: device for regulator "consumer"
1472 * @id: Supply name or regulator ID.
1473 *
1474 * Returns a struct regulator corresponding to the regulator producer,
1475 * or IS_ERR() condition containing errno.
1476 *
1477 * Use of supply names configured via regulator_set_device_supply() is
1478 * strongly encouraged. It is recommended that the supply name used
1479 * should match the name used for the supply and/or the relevant
1480 * device pins in the datasheet.
1481 */
1482struct regulator *regulator_get(struct device *dev, const char *id)
1483{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001484 return _regulator_get(dev, id, false, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001485}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001486EXPORT_SYMBOL_GPL(regulator_get);
1487
Stephen Boyd070b9072012-01-16 19:39:58 -08001488/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001489 * regulator_get_exclusive - obtain exclusive access to a regulator.
1490 * @dev: device for regulator "consumer"
1491 * @id: Supply name or regulator ID.
1492 *
1493 * Returns a struct regulator corresponding to the regulator producer,
1494 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001495 * unable to obtain this regulator while this reference is held and the
1496 * use count for the regulator will be initialised to reflect the current
1497 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001498 *
1499 * This is intended for use by consumers which cannot tolerate shared
1500 * use of the regulator such as those which need to force the
1501 * regulator off for correct operation of the hardware they are
1502 * controlling.
1503 *
1504 * Use of supply names configured via regulator_set_device_supply() is
1505 * strongly encouraged. It is recommended that the supply name used
1506 * should match the name used for the supply and/or the relevant
1507 * device pins in the datasheet.
1508 */
1509struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1510{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001511 return _regulator_get(dev, id, true, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001512}
1513EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1514
Mark Brownde1dd9f2013-07-29 21:42:42 +01001515/**
1516 * regulator_get_optional - obtain optional access to a regulator.
1517 * @dev: device for regulator "consumer"
1518 * @id: Supply name or regulator ID.
1519 *
1520 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001521 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001522 *
1523 * This is intended for use by consumers for devices which can have
1524 * some supplies unconnected in normal use, such as some MMC devices.
1525 * It can allow the regulator core to provide stub supplies for other
1526 * supplies requested using normal regulator_get() calls without
1527 * disrupting the operation of drivers that can handle absent
1528 * supplies.
1529 *
1530 * Use of supply names configured via regulator_set_device_supply() is
1531 * strongly encouraged. It is recommended that the supply name used
1532 * should match the name used for the supply and/or the relevant
1533 * device pins in the datasheet.
1534 */
1535struct regulator *regulator_get_optional(struct device *dev, const char *id)
1536{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001537 return _regulator_get(dev, id, false, false);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001538}
1539EXPORT_SYMBOL_GPL(regulator_get_optional);
1540
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301541/* regulator_list_mutex lock held by regulator_put() */
Charles Keepax23ff2f02012-11-14 09:39:31 +00001542static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001543{
1544 struct regulator_dev *rdev;
1545
1546 if (regulator == NULL || IS_ERR(regulator))
1547 return;
1548
Liam Girdwood414c70c2008-04-30 15:59:04 +01001549 rdev = regulator->rdev;
1550
Mark Brown5de70512011-06-19 13:33:16 +01001551 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001552
Liam Girdwood414c70c2008-04-30 15:59:04 +01001553 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001554 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001555 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301556 mutex_lock(&rdev->mutex);
Mark Brown5de70512011-06-19 13:33:16 +01001557 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001558 list_del(&regulator->list);
1559 kfree(regulator);
1560
Mark Brown5ffbd132009-07-21 16:00:23 +01001561 rdev->open_count--;
1562 rdev->exclusive = 0;
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301563 mutex_unlock(&rdev->mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001564
Liam Girdwood414c70c2008-04-30 15:59:04 +01001565 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001566}
1567
1568/**
1569 * regulator_put - "free" the regulator source
1570 * @regulator: regulator source
1571 *
1572 * Note: drivers must ensure that all regulator_enable calls made on this
1573 * regulator source are balanced by regulator_disable calls prior to calling
1574 * this function.
1575 */
1576void regulator_put(struct regulator *regulator)
1577{
1578 mutex_lock(&regulator_list_mutex);
1579 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001580 mutex_unlock(&regulator_list_mutex);
1581}
1582EXPORT_SYMBOL_GPL(regulator_put);
1583
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001584/**
1585 * regulator_register_supply_alias - Provide device alias for supply lookup
1586 *
1587 * @dev: device that will be given as the regulator "consumer"
1588 * @id: Supply name or regulator ID
1589 * @alias_dev: device that should be used to lookup the supply
1590 * @alias_id: Supply name or regulator ID that should be used to lookup the
1591 * supply
1592 *
1593 * All lookups for id on dev will instead be conducted for alias_id on
1594 * alias_dev.
1595 */
1596int regulator_register_supply_alias(struct device *dev, const char *id,
1597 struct device *alias_dev,
1598 const char *alias_id)
1599{
1600 struct regulator_supply_alias *map;
1601
1602 map = regulator_find_supply_alias(dev, id);
1603 if (map)
1604 return -EEXIST;
1605
1606 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1607 if (!map)
1608 return -ENOMEM;
1609
1610 map->src_dev = dev;
1611 map->src_supply = id;
1612 map->alias_dev = alias_dev;
1613 map->alias_supply = alias_id;
1614
1615 list_add(&map->list, &regulator_supply_alias_list);
1616
1617 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1618 id, dev_name(dev), alias_id, dev_name(alias_dev));
1619
1620 return 0;
1621}
1622EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1623
1624/**
1625 * regulator_unregister_supply_alias - Remove device alias
1626 *
1627 * @dev: device that will be given as the regulator "consumer"
1628 * @id: Supply name or regulator ID
1629 *
1630 * Remove a lookup alias if one exists for id on dev.
1631 */
1632void regulator_unregister_supply_alias(struct device *dev, const char *id)
1633{
1634 struct regulator_supply_alias *map;
1635
1636 map = regulator_find_supply_alias(dev, id);
1637 if (map) {
1638 list_del(&map->list);
1639 kfree(map);
1640 }
1641}
1642EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1643
1644/**
1645 * regulator_bulk_register_supply_alias - register multiple aliases
1646 *
1647 * @dev: device that will be given as the regulator "consumer"
1648 * @id: List of supply names or regulator IDs
1649 * @alias_dev: device that should be used to lookup the supply
1650 * @alias_id: List of supply names or regulator IDs that should be used to
1651 * lookup the supply
1652 * @num_id: Number of aliases to register
1653 *
1654 * @return 0 on success, an errno on failure.
1655 *
1656 * This helper function allows drivers to register several supply
1657 * aliases in one operation. If any of the aliases cannot be
1658 * registered any aliases that were registered will be removed
1659 * before returning to the caller.
1660 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001661int regulator_bulk_register_supply_alias(struct device *dev,
1662 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001663 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001664 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001665 int num_id)
1666{
1667 int i;
1668 int ret;
1669
1670 for (i = 0; i < num_id; ++i) {
1671 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1672 alias_id[i]);
1673 if (ret < 0)
1674 goto err;
1675 }
1676
1677 return 0;
1678
1679err:
1680 dev_err(dev,
1681 "Failed to create supply alias %s,%s -> %s,%s\n",
1682 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1683
1684 while (--i >= 0)
1685 regulator_unregister_supply_alias(dev, id[i]);
1686
1687 return ret;
1688}
1689EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1690
1691/**
1692 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1693 *
1694 * @dev: device that will be given as the regulator "consumer"
1695 * @id: List of supply names or regulator IDs
1696 * @num_id: Number of aliases to unregister
1697 *
1698 * This helper function allows drivers to unregister several supply
1699 * aliases in one operation.
1700 */
1701void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001702 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001703 int num_id)
1704{
1705 int i;
1706
1707 for (i = 0; i < num_id; ++i)
1708 regulator_unregister_supply_alias(dev, id[i]);
1709}
1710EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1711
1712
Kim, Milof19b00d2013-02-18 06:50:39 +00001713/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1714static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1715 const struct regulator_config *config)
1716{
1717 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001718 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001719 int ret;
1720
Russell King778b28b2014-06-29 17:55:54 +01001721 gpiod = gpio_to_desc(config->ena_gpio);
1722
Kim, Milof19b00d2013-02-18 06:50:39 +00001723 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001724 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001725 rdev_dbg(rdev, "GPIO %d is already used\n",
1726 config->ena_gpio);
1727 goto update_ena_gpio_to_rdev;
1728 }
1729 }
1730
1731 ret = gpio_request_one(config->ena_gpio,
1732 GPIOF_DIR_OUT | config->ena_gpio_flags,
1733 rdev_get_name(rdev));
1734 if (ret)
1735 return ret;
1736
1737 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1738 if (pin == NULL) {
1739 gpio_free(config->ena_gpio);
1740 return -ENOMEM;
1741 }
1742
Russell King778b28b2014-06-29 17:55:54 +01001743 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001744 pin->ena_gpio_invert = config->ena_gpio_invert;
1745 list_add(&pin->list, &regulator_ena_gpio_list);
1746
1747update_ena_gpio_to_rdev:
1748 pin->request_count++;
1749 rdev->ena_pin = pin;
1750 return 0;
1751}
1752
1753static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1754{
1755 struct regulator_enable_gpio *pin, *n;
1756
1757 if (!rdev->ena_pin)
1758 return;
1759
1760 /* Free the GPIO only in case of no use */
1761 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001762 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001763 if (pin->request_count <= 1) {
1764 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01001765 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00001766 list_del(&pin->list);
1767 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09001768 rdev->ena_pin = NULL;
1769 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00001770 } else {
1771 pin->request_count--;
1772 }
1773 }
1774 }
1775}
1776
Kim, Milo967cfb12013-02-18 06:50:48 +00001777/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001778 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1779 * @rdev: regulator_dev structure
1780 * @enable: enable GPIO at initial use?
1781 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001782 * GPIO is enabled in case of initial use. (enable_count is 0)
1783 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1784 */
1785static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1786{
1787 struct regulator_enable_gpio *pin = rdev->ena_pin;
1788
1789 if (!pin)
1790 return -EINVAL;
1791
1792 if (enable) {
1793 /* Enable GPIO at initial use */
1794 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01001795 gpiod_set_value_cansleep(pin->gpiod,
1796 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001797
1798 pin->enable_count++;
1799 } else {
1800 if (pin->enable_count > 1) {
1801 pin->enable_count--;
1802 return 0;
1803 }
1804
1805 /* Disable GPIO if not used */
1806 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01001807 gpiod_set_value_cansleep(pin->gpiod,
1808 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001809 pin->enable_count = 0;
1810 }
1811 }
1812
1813 return 0;
1814}
1815
Guodong Xu79fd1142014-08-13 19:33:39 +08001816/**
1817 * _regulator_enable_delay - a delay helper function
1818 * @delay: time to delay in microseconds
1819 *
1820 * Delay for the requested amount of time as per the guidelines in:
1821 *
1822 * Documentation/timers/timers-howto.txt
1823 *
1824 * The assumption here is that regulators will never be enabled in
1825 * atomic context and therefore sleeping functions can be used.
1826 */
1827static void _regulator_enable_delay(unsigned int delay)
1828{
1829 unsigned int ms = delay / 1000;
1830 unsigned int us = delay % 1000;
1831
1832 if (ms > 0) {
1833 /*
1834 * For small enough values, handle super-millisecond
1835 * delays in the usleep_range() call below.
1836 */
1837 if (ms < 20)
1838 us += ms * 1000;
1839 else
1840 msleep(ms);
1841 }
1842
1843 /*
1844 * Give the scheduler some room to coalesce with any other
1845 * wakeup sources. For delays shorter than 10 us, don't even
1846 * bother setting up high-resolution timers and just busy-
1847 * loop.
1848 */
1849 if (us >= 10)
1850 usleep_range(us, us + 100);
1851 else
1852 udelay(us);
1853}
1854
Mark Brown5c5659d2012-06-27 13:21:26 +01001855static int _regulator_do_enable(struct regulator_dev *rdev)
1856{
1857 int ret, delay;
1858
1859 /* Query before enabling in case configuration dependent. */
1860 ret = _regulator_get_enable_time(rdev);
1861 if (ret >= 0) {
1862 delay = ret;
1863 } else {
1864 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1865 delay = 0;
1866 }
1867
1868 trace_regulator_enable(rdev_get_name(rdev));
1869
Guodong Xu871f5652014-08-13 19:33:40 +08001870 if (rdev->desc->off_on_delay) {
1871 /* if needed, keep a distance of off_on_delay from last time
1872 * this regulator was disabled.
1873 */
1874 unsigned long start_jiffy = jiffies;
1875 unsigned long intended, max_delay, remaining;
1876
1877 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
1878 intended = rdev->last_off_jiffy + max_delay;
1879
1880 if (time_before(start_jiffy, intended)) {
1881 /* calc remaining jiffies to deal with one-time
1882 * timer wrapping.
1883 * in case of multiple timer wrapping, either it can be
1884 * detected by out-of-range remaining, or it cannot be
1885 * detected and we gets a panelty of
1886 * _regulator_enable_delay().
1887 */
1888 remaining = intended - start_jiffy;
1889 if (remaining <= max_delay)
1890 _regulator_enable_delay(
1891 jiffies_to_usecs(remaining));
1892 }
1893 }
1894
Kim, Milo967cfb12013-02-18 06:50:48 +00001895 if (rdev->ena_pin) {
1896 ret = regulator_ena_gpio_ctrl(rdev, true);
1897 if (ret < 0)
1898 return ret;
Mark Brown65f73502012-06-27 14:14:38 +01001899 rdev->ena_gpio_state = 1;
1900 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001901 ret = rdev->desc->ops->enable(rdev);
1902 if (ret < 0)
1903 return ret;
1904 } else {
1905 return -EINVAL;
1906 }
1907
1908 /* Allow the regulator to ramp; it would be useful to extend
1909 * this for bulk operations so that the regulators can ramp
1910 * together. */
1911 trace_regulator_enable_delay(rdev_get_name(rdev));
1912
Guodong Xu79fd1142014-08-13 19:33:39 +08001913 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01001914
1915 trace_regulator_enable_complete(rdev_get_name(rdev));
1916
1917 return 0;
1918}
1919
Liam Girdwood414c70c2008-04-30 15:59:04 +01001920/* locks held by regulator_enable() */
1921static int _regulator_enable(struct regulator_dev *rdev)
1922{
Mark Brown5c5659d2012-06-27 13:21:26 +01001923 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001924
Liam Girdwood414c70c2008-04-30 15:59:04 +01001925 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001926 if (rdev->constraints &&
1927 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1928 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001929
Mark Brown9a2372f2009-08-03 18:49:57 +01001930 if (rdev->use_count == 0) {
1931 /* The regulator may on if it's not switchable or left on */
1932 ret = _regulator_is_enabled(rdev);
1933 if (ret == -EINVAL || ret == 0) {
1934 if (!_regulator_can_change_status(rdev))
1935 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001936
Mark Brown5c5659d2012-06-27 13:21:26 +01001937 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001938 if (ret < 0)
1939 return ret;
1940
Linus Walleija7433cf2009-08-26 12:54:04 +02001941 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001942 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001943 return ret;
1944 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001945 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001946 }
1947
Mark Brown9a2372f2009-08-03 18:49:57 +01001948 rdev->use_count++;
1949
1950 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001951}
1952
1953/**
1954 * regulator_enable - enable regulator output
1955 * @regulator: regulator source
1956 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001957 * Request that the regulator be enabled with the regulator output at
1958 * the predefined voltage or current value. Calls to regulator_enable()
1959 * must be balanced with calls to regulator_disable().
1960 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001961 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001962 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001963 */
1964int regulator_enable(struct regulator *regulator)
1965{
David Brownell412aec62008-11-16 11:44:46 -08001966 struct regulator_dev *rdev = regulator->rdev;
1967 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001968
Mark Brown6492bc12012-04-19 13:19:07 +01001969 if (regulator->always_on)
1970 return 0;
1971
Mark Brown3801b862011-06-09 16:22:22 +01001972 if (rdev->supply) {
1973 ret = regulator_enable(rdev->supply);
1974 if (ret != 0)
1975 return ret;
1976 }
1977
David Brownell412aec62008-11-16 11:44:46 -08001978 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001979 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001980 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001981
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001982 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001983 regulator_disable(rdev->supply);
1984
Liam Girdwood414c70c2008-04-30 15:59:04 +01001985 return ret;
1986}
1987EXPORT_SYMBOL_GPL(regulator_enable);
1988
Mark Brown5c5659d2012-06-27 13:21:26 +01001989static int _regulator_do_disable(struct regulator_dev *rdev)
1990{
1991 int ret;
1992
1993 trace_regulator_disable(rdev_get_name(rdev));
1994
Kim, Milo967cfb12013-02-18 06:50:48 +00001995 if (rdev->ena_pin) {
1996 ret = regulator_ena_gpio_ctrl(rdev, false);
1997 if (ret < 0)
1998 return ret;
Mark Brown5c5659d2012-06-27 13:21:26 +01001999 rdev->ena_gpio_state = 0;
2000
2001 } else if (rdev->desc->ops->disable) {
2002 ret = rdev->desc->ops->disable(rdev);
2003 if (ret != 0)
2004 return ret;
2005 }
2006
Guodong Xu871f5652014-08-13 19:33:40 +08002007 /* cares about last_off_jiffy only if off_on_delay is required by
2008 * device.
2009 */
2010 if (rdev->desc->off_on_delay)
2011 rdev->last_off_jiffy = jiffies;
2012
Mark Brown5c5659d2012-06-27 13:21:26 +01002013 trace_regulator_disable_complete(rdev_get_name(rdev));
2014
Mark Brown5c5659d2012-06-27 13:21:26 +01002015 return 0;
2016}
2017
Liam Girdwood414c70c2008-04-30 15:59:04 +01002018/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002019static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002020{
2021 int ret = 0;
2022
David Brownellcd94b502009-03-11 16:43:34 -08002023 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08002024 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08002025 return -EIO;
2026
Liam Girdwood414c70c2008-04-30 15:59:04 +01002027 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01002028 if (rdev->use_count == 1 &&
2029 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002030
2031 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01002032 if (_regulator_can_change_status(rdev)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002033 ret = _notifier_call_chain(rdev,
2034 REGULATOR_EVENT_PRE_DISABLE,
2035 NULL);
2036 if (ret & NOTIFY_STOP_MASK)
2037 return -EINVAL;
2038
Mark Brown5c5659d2012-06-27 13:21:26 +01002039 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002040 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002041 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002042 _notifier_call_chain(rdev,
2043 REGULATOR_EVENT_ABORT_DISABLE,
2044 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002045 return ret;
2046 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002047 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2048 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002049 }
2050
Liam Girdwood414c70c2008-04-30 15:59:04 +01002051 rdev->use_count = 0;
2052 } else if (rdev->use_count > 1) {
2053
2054 if (rdev->constraints &&
2055 (rdev->constraints->valid_ops_mask &
2056 REGULATOR_CHANGE_DRMS))
2057 drms_uA_update(rdev);
2058
2059 rdev->use_count--;
2060 }
Mark Brown3801b862011-06-09 16:22:22 +01002061
Liam Girdwood414c70c2008-04-30 15:59:04 +01002062 return ret;
2063}
2064
2065/**
2066 * regulator_disable - disable regulator output
2067 * @regulator: regulator source
2068 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002069 * Disable the regulator output voltage or current. Calls to
2070 * regulator_enable() must be balanced with calls to
2071 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002072 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002073 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002074 * devices have it enabled, the regulator device supports disabling and
2075 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002076 */
2077int regulator_disable(struct regulator *regulator)
2078{
David Brownell412aec62008-11-16 11:44:46 -08002079 struct regulator_dev *rdev = regulator->rdev;
2080 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002081
Mark Brown6492bc12012-04-19 13:19:07 +01002082 if (regulator->always_on)
2083 return 0;
2084
David Brownell412aec62008-11-16 11:44:46 -08002085 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002086 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002087 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002088
Mark Brown3801b862011-06-09 16:22:22 +01002089 if (ret == 0 && rdev->supply)
2090 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002091
Liam Girdwood414c70c2008-04-30 15:59:04 +01002092 return ret;
2093}
2094EXPORT_SYMBOL_GPL(regulator_disable);
2095
2096/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002097static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002098{
2099 int ret = 0;
2100
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002101 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2102 REGULATOR_EVENT_PRE_DISABLE, NULL);
2103 if (ret & NOTIFY_STOP_MASK)
2104 return -EINVAL;
2105
Markus Pargmann66fda752014-02-20 17:36:04 +01002106 ret = _regulator_do_disable(rdev);
2107 if (ret < 0) {
2108 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002109 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2110 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002111 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002112 }
2113
Markus Pargmann66fda752014-02-20 17:36:04 +01002114 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2115 REGULATOR_EVENT_DISABLE, NULL);
2116
2117 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002118}
2119
2120/**
2121 * regulator_force_disable - force disable regulator output
2122 * @regulator: regulator source
2123 *
2124 * Forcibly disable the regulator output voltage or current.
2125 * NOTE: this *will* disable the regulator output even if other consumer
2126 * devices have it enabled. This should be used for situations when device
2127 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2128 */
2129int regulator_force_disable(struct regulator *regulator)
2130{
Mark Brown82d15832011-05-09 11:41:02 +02002131 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002132 int ret;
2133
Mark Brown82d15832011-05-09 11:41:02 +02002134 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002135 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002136 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002137 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002138
Mark Brown3801b862011-06-09 16:22:22 +01002139 if (rdev->supply)
2140 while (rdev->open_count--)
2141 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002142
Liam Girdwood414c70c2008-04-30 15:59:04 +01002143 return ret;
2144}
2145EXPORT_SYMBOL_GPL(regulator_force_disable);
2146
Mark Brownda07ecd2011-09-11 09:53:50 +01002147static void regulator_disable_work(struct work_struct *work)
2148{
2149 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2150 disable_work.work);
2151 int count, i, ret;
2152
2153 mutex_lock(&rdev->mutex);
2154
2155 BUG_ON(!rdev->deferred_disables);
2156
2157 count = rdev->deferred_disables;
2158 rdev->deferred_disables = 0;
2159
2160 for (i = 0; i < count; i++) {
2161 ret = _regulator_disable(rdev);
2162 if (ret != 0)
2163 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2164 }
2165
2166 mutex_unlock(&rdev->mutex);
2167
2168 if (rdev->supply) {
2169 for (i = 0; i < count; i++) {
2170 ret = regulator_disable(rdev->supply);
2171 if (ret != 0) {
2172 rdev_err(rdev,
2173 "Supply disable failed: %d\n", ret);
2174 }
2175 }
2176 }
2177}
2178
2179/**
2180 * regulator_disable_deferred - disable regulator output with delay
2181 * @regulator: regulator source
2182 * @ms: miliseconds until the regulator is disabled
2183 *
2184 * Execute regulator_disable() on the regulator after a delay. This
2185 * is intended for use with devices that require some time to quiesce.
2186 *
2187 * NOTE: this will only disable the regulator output if no other consumer
2188 * devices have it enabled, the regulator device supports disabling and
2189 * machine constraints permit this operation.
2190 */
2191int regulator_disable_deferred(struct regulator *regulator, int ms)
2192{
2193 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01002194 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01002195
Mark Brown6492bc12012-04-19 13:19:07 +01002196 if (regulator->always_on)
2197 return 0;
2198
Mark Brown2b5a24a2012-09-07 11:00:53 +08002199 if (!ms)
2200 return regulator_disable(regulator);
2201
Mark Brownda07ecd2011-09-11 09:53:50 +01002202 mutex_lock(&rdev->mutex);
2203 rdev->deferred_disables++;
2204 mutex_unlock(&rdev->mutex);
2205
Mark Brown070260f2013-07-18 11:52:04 +01002206 ret = queue_delayed_work(system_power_efficient_wq,
2207 &rdev->disable_work,
2208 msecs_to_jiffies(ms));
Mark Brownaa598022011-10-03 22:42:43 +01002209 if (ret < 0)
2210 return ret;
2211 else
2212 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002213}
2214EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2215
Liam Girdwood414c70c2008-04-30 15:59:04 +01002216static int _regulator_is_enabled(struct regulator_dev *rdev)
2217{
Mark Brown65f73502012-06-27 14:14:38 +01002218 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002219 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002220 return rdev->ena_gpio_state;
2221
Mark Brown9a7f6a42010-02-11 17:22:45 +00002222 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002223 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002224 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002225
Mark Brown93325462009-08-03 18:49:56 +01002226 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002227}
2228
2229/**
2230 * regulator_is_enabled - is the regulator output enabled
2231 * @regulator: regulator source
2232 *
David Brownell412aec62008-11-16 11:44:46 -08002233 * Returns positive if the regulator driver backing the source/client
2234 * has requested that the device be enabled, zero if it hasn't, else a
2235 * negative errno code.
2236 *
2237 * Note that the device backing this regulator handle can have multiple
2238 * users, so it might be enabled even if regulator_enable() was never
2239 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002240 */
2241int regulator_is_enabled(struct regulator *regulator)
2242{
Mark Brown93325462009-08-03 18:49:56 +01002243 int ret;
2244
Mark Brown6492bc12012-04-19 13:19:07 +01002245 if (regulator->always_on)
2246 return 1;
2247
Mark Brown93325462009-08-03 18:49:56 +01002248 mutex_lock(&regulator->rdev->mutex);
2249 ret = _regulator_is_enabled(regulator->rdev);
2250 mutex_unlock(&regulator->rdev->mutex);
2251
2252 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002253}
2254EXPORT_SYMBOL_GPL(regulator_is_enabled);
2255
2256/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002257 * regulator_can_change_voltage - check if regulator can change voltage
2258 * @regulator: regulator source
2259 *
2260 * Returns positive if the regulator driver backing the source/client
Masanari Iidae2278672014-02-18 22:54:36 +09002261 * can change its voltage, false otherwise. Useful for detecting fixed
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002262 * or dummy regulators and disabling voltage change logic in the client
2263 * driver.
2264 */
2265int regulator_can_change_voltage(struct regulator *regulator)
2266{
2267 struct regulator_dev *rdev = regulator->rdev;
2268
2269 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002270 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2271 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2272 return 1;
2273
2274 if (rdev->desc->continuous_voltage_range &&
2275 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2276 rdev->constraints->min_uV != rdev->constraints->max_uV)
2277 return 1;
2278 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002279
2280 return 0;
2281}
2282EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2283
2284/**
David Brownell4367cfd2009-02-26 11:48:36 -08002285 * regulator_count_voltages - count regulator_list_voltage() selectors
2286 * @regulator: regulator source
2287 *
2288 * Returns number of selectors, or negative errno. Selectors are
2289 * numbered starting at zero, and typically correspond to bitfields
2290 * in hardware registers.
2291 */
2292int regulator_count_voltages(struct regulator *regulator)
2293{
2294 struct regulator_dev *rdev = regulator->rdev;
2295
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002296 if (rdev->desc->n_voltages)
2297 return rdev->desc->n_voltages;
2298
2299 if (!rdev->supply)
2300 return -EINVAL;
2301
2302 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002303}
2304EXPORT_SYMBOL_GPL(regulator_count_voltages);
2305
2306/**
2307 * regulator_list_voltage - enumerate supported voltages
2308 * @regulator: regulator source
2309 * @selector: identify voltage to list
2310 * Context: can sleep
2311 *
2312 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002313 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002314 * negative errno.
2315 */
2316int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2317{
Guodong Xu272e2312014-08-13 19:33:38 +08002318 struct regulator_dev *rdev = regulator->rdev;
2319 const struct regulator_ops *ops = rdev->desc->ops;
2320 int ret;
David Brownell4367cfd2009-02-26 11:48:36 -08002321
Guennadi Liakhovetskif4460432013-11-13 12:33:00 +01002322 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2323 return rdev->desc->fixed_uV;
2324
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002325 if (ops->list_voltage) {
2326 if (selector >= rdev->desc->n_voltages)
2327 return -EINVAL;
2328 mutex_lock(&rdev->mutex);
2329 ret = ops->list_voltage(rdev, selector);
2330 mutex_unlock(&rdev->mutex);
2331 } else if (rdev->supply) {
2332 ret = regulator_list_voltage(rdev->supply, selector);
2333 } else {
David Brownell4367cfd2009-02-26 11:48:36 -08002334 return -EINVAL;
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002335 }
David Brownell4367cfd2009-02-26 11:48:36 -08002336
2337 if (ret > 0) {
2338 if (ret < rdev->constraints->min_uV)
2339 ret = 0;
2340 else if (ret > rdev->constraints->max_uV)
2341 ret = 0;
2342 }
2343
2344 return ret;
2345}
2346EXPORT_SYMBOL_GPL(regulator_list_voltage);
2347
2348/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002349 * regulator_get_regmap - get the regulator's register map
2350 * @regulator: regulator source
2351 *
2352 * Returns the register map for the given regulator, or an ERR_PTR value
2353 * if the regulator doesn't use regmap.
2354 */
2355struct regmap *regulator_get_regmap(struct regulator *regulator)
2356{
2357 struct regmap *map = regulator->rdev->regmap;
2358
2359 return map ? map : ERR_PTR(-EOPNOTSUPP);
2360}
2361
2362/**
2363 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2364 * @regulator: regulator source
2365 * @vsel_reg: voltage selector register, output parameter
2366 * @vsel_mask: mask for voltage selector bitfield, output parameter
2367 *
2368 * Returns the hardware register offset and bitmask used for setting the
2369 * regulator voltage. This might be useful when configuring voltage-scaling
2370 * hardware or firmware that can make I2C requests behind the kernel's back,
2371 * for example.
2372 *
2373 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2374 * and 0 is returned, otherwise a negative errno is returned.
2375 */
2376int regulator_get_hardware_vsel_register(struct regulator *regulator,
2377 unsigned *vsel_reg,
2378 unsigned *vsel_mask)
2379{
Guodong Xu39f54602014-08-19 18:07:41 +08002380 struct regulator_dev *rdev = regulator->rdev;
2381 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002382
2383 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2384 return -EOPNOTSUPP;
2385
2386 *vsel_reg = rdev->desc->vsel_reg;
2387 *vsel_mask = rdev->desc->vsel_mask;
2388
2389 return 0;
2390}
2391EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2392
2393/**
2394 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2395 * @regulator: regulator source
2396 * @selector: identify voltage to list
2397 *
2398 * Converts the selector to a hardware-specific voltage selector that can be
2399 * directly written to the regulator registers. The address of the voltage
2400 * register can be determined by calling @regulator_get_hardware_vsel_register.
2401 *
2402 * On error a negative errno is returned.
2403 */
2404int regulator_list_hardware_vsel(struct regulator *regulator,
2405 unsigned selector)
2406{
Guodong Xu39f54602014-08-19 18:07:41 +08002407 struct regulator_dev *rdev = regulator->rdev;
2408 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002409
2410 if (selector >= rdev->desc->n_voltages)
2411 return -EINVAL;
2412 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2413 return -EOPNOTSUPP;
2414
2415 return selector;
2416}
2417EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2418
2419/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002420 * regulator_get_linear_step - return the voltage step size between VSEL values
2421 * @regulator: regulator source
2422 *
2423 * Returns the voltage step size between VSEL values for linear
2424 * regulators, or return 0 if the regulator isn't a linear regulator.
2425 */
2426unsigned int regulator_get_linear_step(struct regulator *regulator)
2427{
2428 struct regulator_dev *rdev = regulator->rdev;
2429
2430 return rdev->desc->uV_step;
2431}
2432EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2433
2434/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002435 * regulator_is_supported_voltage - check if a voltage range can be supported
2436 *
2437 * @regulator: Regulator to check.
2438 * @min_uV: Minimum required voltage in uV.
2439 * @max_uV: Maximum required voltage in uV.
2440 *
2441 * Returns a boolean or a negative error code.
2442 */
2443int regulator_is_supported_voltage(struct regulator *regulator,
2444 int min_uV, int max_uV)
2445{
Mark Brownc5f39392012-07-02 15:00:18 +01002446 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002447 int i, voltages, ret;
2448
Mark Brownc5f39392012-07-02 15:00:18 +01002449 /* If we can't change voltage check the current voltage */
2450 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2451 ret = regulator_get_voltage(regulator);
2452 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002453 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002454 else
2455 return ret;
2456 }
2457
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002458 /* Any voltage within constrains range is fine? */
2459 if (rdev->desc->continuous_voltage_range)
2460 return min_uV >= rdev->constraints->min_uV &&
2461 max_uV <= rdev->constraints->max_uV;
2462
Mark Browna7a1ad92009-07-21 16:00:24 +01002463 ret = regulator_count_voltages(regulator);
2464 if (ret < 0)
2465 return ret;
2466 voltages = ret;
2467
2468 for (i = 0; i < voltages; i++) {
2469 ret = regulator_list_voltage(regulator, i);
2470
2471 if (ret >= min_uV && ret <= max_uV)
2472 return 1;
2473 }
2474
2475 return 0;
2476}
Mark Browna398eaa2011-12-28 12:48:45 +00002477EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002478
Heiko Stübner71795692014-08-28 12:36:04 -07002479static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2480 int min_uV, int max_uV,
2481 unsigned *selector)
2482{
2483 struct pre_voltage_change_data data;
2484 int ret;
2485
2486 data.old_uV = _regulator_get_voltage(rdev);
2487 data.min_uV = min_uV;
2488 data.max_uV = max_uV;
2489 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2490 &data);
2491 if (ret & NOTIFY_STOP_MASK)
2492 return -EINVAL;
2493
2494 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2495 if (ret >= 0)
2496 return ret;
2497
2498 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2499 (void *)data.old_uV);
2500
2501 return ret;
2502}
2503
2504static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2505 int uV, unsigned selector)
2506{
2507 struct pre_voltage_change_data data;
2508 int ret;
2509
2510 data.old_uV = _regulator_get_voltage(rdev);
2511 data.min_uV = uV;
2512 data.max_uV = uV;
2513 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2514 &data);
2515 if (ret & NOTIFY_STOP_MASK)
2516 return -EINVAL;
2517
2518 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2519 if (ret >= 0)
2520 return ret;
2521
2522 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2523 (void *)data.old_uV);
2524
2525 return ret;
2526}
2527
Mark Brown75790252010-12-12 14:25:50 +00002528static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2529 int min_uV, int max_uV)
2530{
2531 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002532 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002533 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002534 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002535 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002536
2537 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2538
Mark Brownbf5892a2011-05-08 22:13:37 +01002539 min_uV += rdev->constraints->uV_offset;
2540 max_uV += rdev->constraints->uV_offset;
2541
Axel Lineba41a52012-04-04 10:32:10 +08002542 /*
2543 * If we can't obtain the old selector there is not enough
2544 * info to call set_voltage_time_sel().
2545 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002546 if (_regulator_is_enabled(rdev) &&
2547 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002548 rdev->desc->ops->get_voltage_sel) {
2549 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2550 if (old_selector < 0)
2551 return old_selector;
2552 }
2553
Mark Brown75790252010-12-12 14:25:50 +00002554 if (rdev->desc->ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002555 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2556 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002557
2558 if (ret >= 0) {
2559 if (rdev->desc->ops->list_voltage)
2560 best_val = rdev->desc->ops->list_voltage(rdev,
2561 selector);
2562 else
2563 best_val = _regulator_get_voltage(rdev);
2564 }
2565
Mark Browne8eef822010-12-12 14:36:17 +00002566 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002567 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002568 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2569 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002570 } else {
2571 if (rdev->desc->ops->list_voltage ==
2572 regulator_list_voltage_linear)
2573 ret = regulator_map_voltage_linear(rdev,
2574 min_uV, max_uV);
Axel Lin36698622014-05-24 11:10:43 +08002575 else if (rdev->desc->ops->list_voltage ==
2576 regulator_list_voltage_linear_range)
2577 ret = regulator_map_voltage_linear_range(rdev,
2578 min_uV, max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002579 else
2580 ret = regulator_map_voltage_iterate(rdev,
2581 min_uV, max_uV);
2582 }
Mark Browne8eef822010-12-12 14:36:17 +00002583
Mark Browne843fc42012-05-09 21:16:06 +01002584 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002585 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2586 if (min_uV <= best_val && max_uV >= best_val) {
2587 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002588 if (old_selector == selector)
2589 ret = 0;
2590 else
Heiko Stübner71795692014-08-28 12:36:04 -07002591 ret = _regulator_call_set_voltage_sel(
2592 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002593 } else {
2594 ret = -EINVAL;
2595 }
Mark Browne8eef822010-12-12 14:36:17 +00002596 }
Mark Brown75790252010-12-12 14:25:50 +00002597 } else {
2598 ret = -EINVAL;
2599 }
2600
Axel Lineba41a52012-04-04 10:32:10 +08002601 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302602 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2603 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002604
2605 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2606 old_selector, selector);
2607 if (delay < 0) {
2608 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2609 delay);
2610 delay = 0;
2611 }
Axel Lineba41a52012-04-04 10:32:10 +08002612
Philip Rakity8b96de32012-06-14 15:07:56 -07002613 /* Insert any necessary delays */
2614 if (delay >= 1000) {
2615 mdelay(delay / 1000);
2616 udelay(delay % 1000);
2617 } else if (delay) {
2618 udelay(delay);
2619 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002620 }
2621
Axel Lin2f6c7972012-08-06 23:44:19 +08002622 if (ret == 0 && best_val >= 0) {
2623 unsigned long data = best_val;
2624
Mark Brownded06a52010-12-16 13:59:10 +00002625 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002626 (void *)data);
2627 }
Mark Brownded06a52010-12-16 13:59:10 +00002628
Axel Lineba41a52012-04-04 10:32:10 +08002629 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002630
2631 return ret;
2632}
2633
Mark Browna7a1ad92009-07-21 16:00:24 +01002634/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002635 * regulator_set_voltage - set regulator output voltage
2636 * @regulator: regulator source
2637 * @min_uV: Minimum required voltage in uV
2638 * @max_uV: Maximum acceptable voltage in uV
2639 *
2640 * Sets a voltage regulator to the desired output voltage. This can be set
2641 * during any regulator state. IOW, regulator can be disabled or enabled.
2642 *
2643 * If the regulator is enabled then the voltage will change to the new value
2644 * immediately otherwise if the regulator is disabled the regulator will
2645 * output at the new voltage when enabled.
2646 *
2647 * NOTE: If the regulator is shared between several devices then the lowest
2648 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002649 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002650 * calling this function otherwise this call will fail.
2651 */
2652int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2653{
2654 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002655 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002656 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002657 int current_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002658
2659 mutex_lock(&rdev->mutex);
2660
Mark Brown95a3c232010-12-16 15:49:37 +00002661 /* If we're setting the same range as last time the change
2662 * should be a noop (some cpufreq implementations use the same
2663 * voltage for multiple frequencies, for example).
2664 */
2665 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2666 goto out;
2667
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002668 /* If we're trying to set a range that overlaps the current voltage,
2669 * return succesfully even though the regulator does not support
2670 * changing the voltage.
2671 */
2672 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2673 current_uV = _regulator_get_voltage(rdev);
2674 if (min_uV <= current_uV && current_uV <= max_uV) {
2675 regulator->min_uV = min_uV;
2676 regulator->max_uV = max_uV;
2677 goto out;
2678 }
2679 }
2680
Liam Girdwood414c70c2008-04-30 15:59:04 +01002681 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002682 if (!rdev->desc->ops->set_voltage &&
2683 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002684 ret = -EINVAL;
2685 goto out;
2686 }
2687
2688 /* constraints check */
2689 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2690 if (ret < 0)
2691 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002692
Paolo Pisati92d7a552012-12-13 10:13:00 +01002693 /* restore original values in case of error */
2694 old_min_uV = regulator->min_uV;
2695 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002696 regulator->min_uV = min_uV;
2697 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002698
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002699 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2700 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002701 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002702
Mark Brown75790252010-12-12 14:25:50 +00002703 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002704 if (ret < 0)
2705 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09002706
Liam Girdwood414c70c2008-04-30 15:59:04 +01002707out:
2708 mutex_unlock(&rdev->mutex);
2709 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002710out2:
2711 regulator->min_uV = old_min_uV;
2712 regulator->max_uV = old_max_uV;
2713 mutex_unlock(&rdev->mutex);
2714 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002715}
2716EXPORT_SYMBOL_GPL(regulator_set_voltage);
2717
Mark Brown606a2562010-12-16 15:49:36 +00002718/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002719 * regulator_set_voltage_time - get raise/fall time
2720 * @regulator: regulator source
2721 * @old_uV: starting voltage in microvolts
2722 * @new_uV: target voltage in microvolts
2723 *
2724 * Provided with the starting and ending voltage, this function attempts to
2725 * calculate the time in microseconds required to rise or fall to this new
2726 * voltage.
2727 */
2728int regulator_set_voltage_time(struct regulator *regulator,
2729 int old_uV, int new_uV)
2730{
Guodong Xu272e2312014-08-13 19:33:38 +08002731 struct regulator_dev *rdev = regulator->rdev;
2732 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01002733 int old_sel = -1;
2734 int new_sel = -1;
2735 int voltage;
2736 int i;
2737
2738 /* Currently requires operations to do this */
2739 if (!ops->list_voltage || !ops->set_voltage_time_sel
2740 || !rdev->desc->n_voltages)
2741 return -EINVAL;
2742
2743 for (i = 0; i < rdev->desc->n_voltages; i++) {
2744 /* We only look for exact voltage matches here */
2745 voltage = regulator_list_voltage(regulator, i);
2746 if (voltage < 0)
2747 return -EINVAL;
2748 if (voltage == 0)
2749 continue;
2750 if (voltage == old_uV)
2751 old_sel = i;
2752 if (voltage == new_uV)
2753 new_sel = i;
2754 }
2755
2756 if (old_sel < 0 || new_sel < 0)
2757 return -EINVAL;
2758
2759 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2760}
2761EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2762
2763/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002764 * regulator_set_voltage_time_sel - get raise/fall time
2765 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302766 * @old_selector: selector for starting voltage
2767 * @new_selector: selector for target voltage
2768 *
2769 * Provided with the starting and target voltage selectors, this function
2770 * returns time in microseconds required to rise or fall to this new voltage
2771 *
Axel Linf11d08c2012-06-19 09:38:45 +08002772 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002773 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302774 */
2775int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2776 unsigned int old_selector,
2777 unsigned int new_selector)
2778{
Axel Lin398715a2012-06-18 10:11:28 +08002779 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002780 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002781
2782 if (rdev->constraints->ramp_delay)
2783 ramp_delay = rdev->constraints->ramp_delay;
2784 else if (rdev->desc->ramp_delay)
2785 ramp_delay = rdev->desc->ramp_delay;
2786
2787 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302788 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002789 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302790 }
Axel Lin398715a2012-06-18 10:11:28 +08002791
Axel Linf11d08c2012-06-19 09:38:45 +08002792 /* sanity check */
2793 if (!rdev->desc->ops->list_voltage)
2794 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002795
Axel Linf11d08c2012-06-19 09:38:45 +08002796 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2797 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2798
2799 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302800}
Mark Brownb19dbf72012-06-23 11:34:20 +01002801EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302802
2803/**
Mark Brown606a2562010-12-16 15:49:36 +00002804 * regulator_sync_voltage - re-apply last regulator output voltage
2805 * @regulator: regulator source
2806 *
2807 * Re-apply the last configured voltage. This is intended to be used
2808 * where some external control source the consumer is cooperating with
2809 * has caused the configured voltage to change.
2810 */
2811int regulator_sync_voltage(struct regulator *regulator)
2812{
2813 struct regulator_dev *rdev = regulator->rdev;
2814 int ret, min_uV, max_uV;
2815
2816 mutex_lock(&rdev->mutex);
2817
2818 if (!rdev->desc->ops->set_voltage &&
2819 !rdev->desc->ops->set_voltage_sel) {
2820 ret = -EINVAL;
2821 goto out;
2822 }
2823
2824 /* This is only going to work if we've had a voltage configured. */
2825 if (!regulator->min_uV && !regulator->max_uV) {
2826 ret = -EINVAL;
2827 goto out;
2828 }
2829
2830 min_uV = regulator->min_uV;
2831 max_uV = regulator->max_uV;
2832
2833 /* This should be a paranoia check... */
2834 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2835 if (ret < 0)
2836 goto out;
2837
2838 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2839 if (ret < 0)
2840 goto out;
2841
2842 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2843
2844out:
2845 mutex_unlock(&rdev->mutex);
2846 return ret;
2847}
2848EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2849
Liam Girdwood414c70c2008-04-30 15:59:04 +01002850static int _regulator_get_voltage(struct regulator_dev *rdev)
2851{
Mark Brownbf5892a2011-05-08 22:13:37 +01002852 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002853
2854 if (rdev->desc->ops->get_voltage_sel) {
2855 sel = rdev->desc->ops->get_voltage_sel(rdev);
2856 if (sel < 0)
2857 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002858 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002859 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002860 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002861 } else if (rdev->desc->ops->list_voltage) {
2862 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05302863 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2864 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02002865 } else if (rdev->supply) {
2866 ret = regulator_get_voltage(rdev->supply);
Axel Lincb220d12011-05-23 20:08:10 +08002867 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002868 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002869 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002870
Axel Lincb220d12011-05-23 20:08:10 +08002871 if (ret < 0)
2872 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002873 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002874}
2875
2876/**
2877 * regulator_get_voltage - get regulator output voltage
2878 * @regulator: regulator source
2879 *
2880 * This returns the current regulator voltage in uV.
2881 *
2882 * NOTE: If the regulator is disabled it will return the voltage value. This
2883 * function should not be used to determine regulator state.
2884 */
2885int regulator_get_voltage(struct regulator *regulator)
2886{
2887 int ret;
2888
2889 mutex_lock(&regulator->rdev->mutex);
2890
2891 ret = _regulator_get_voltage(regulator->rdev);
2892
2893 mutex_unlock(&regulator->rdev->mutex);
2894
2895 return ret;
2896}
2897EXPORT_SYMBOL_GPL(regulator_get_voltage);
2898
2899/**
2900 * regulator_set_current_limit - set regulator output current limit
2901 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01002902 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01002903 * @max_uA: Maximum supported current in uA
2904 *
2905 * Sets current sink to the desired output current. This can be set during
2906 * any regulator state. IOW, regulator can be disabled or enabled.
2907 *
2908 * If the regulator is enabled then the current will change to the new value
2909 * immediately otherwise if the regulator is disabled the regulator will
2910 * output at the new current when enabled.
2911 *
2912 * NOTE: Regulator system constraints must be set for this regulator before
2913 * calling this function otherwise this call will fail.
2914 */
2915int regulator_set_current_limit(struct regulator *regulator,
2916 int min_uA, int max_uA)
2917{
2918 struct regulator_dev *rdev = regulator->rdev;
2919 int ret;
2920
2921 mutex_lock(&rdev->mutex);
2922
2923 /* sanity check */
2924 if (!rdev->desc->ops->set_current_limit) {
2925 ret = -EINVAL;
2926 goto out;
2927 }
2928
2929 /* constraints check */
2930 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2931 if (ret < 0)
2932 goto out;
2933
2934 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2935out:
2936 mutex_unlock(&rdev->mutex);
2937 return ret;
2938}
2939EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2940
2941static int _regulator_get_current_limit(struct regulator_dev *rdev)
2942{
2943 int ret;
2944
2945 mutex_lock(&rdev->mutex);
2946
2947 /* sanity check */
2948 if (!rdev->desc->ops->get_current_limit) {
2949 ret = -EINVAL;
2950 goto out;
2951 }
2952
2953 ret = rdev->desc->ops->get_current_limit(rdev);
2954out:
2955 mutex_unlock(&rdev->mutex);
2956 return ret;
2957}
2958
2959/**
2960 * regulator_get_current_limit - get regulator output current
2961 * @regulator: regulator source
2962 *
2963 * This returns the current supplied by the specified current sink in uA.
2964 *
2965 * NOTE: If the regulator is disabled it will return the current value. This
2966 * function should not be used to determine regulator state.
2967 */
2968int regulator_get_current_limit(struct regulator *regulator)
2969{
2970 return _regulator_get_current_limit(regulator->rdev);
2971}
2972EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2973
2974/**
2975 * regulator_set_mode - set regulator operating mode
2976 * @regulator: regulator source
2977 * @mode: operating mode - one of the REGULATOR_MODE constants
2978 *
2979 * Set regulator operating mode to increase regulator efficiency or improve
2980 * regulation performance.
2981 *
2982 * NOTE: Regulator system constraints must be set for this regulator before
2983 * calling this function otherwise this call will fail.
2984 */
2985int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2986{
2987 struct regulator_dev *rdev = regulator->rdev;
2988 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302989 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002990
2991 mutex_lock(&rdev->mutex);
2992
2993 /* sanity check */
2994 if (!rdev->desc->ops->set_mode) {
2995 ret = -EINVAL;
2996 goto out;
2997 }
2998
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302999 /* return if the same mode is requested */
3000 if (rdev->desc->ops->get_mode) {
3001 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3002 if (regulator_curr_mode == mode) {
3003 ret = 0;
3004 goto out;
3005 }
3006 }
3007
Liam Girdwood414c70c2008-04-30 15:59:04 +01003008 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003009 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003010 if (ret < 0)
3011 goto out;
3012
3013 ret = rdev->desc->ops->set_mode(rdev, mode);
3014out:
3015 mutex_unlock(&rdev->mutex);
3016 return ret;
3017}
3018EXPORT_SYMBOL_GPL(regulator_set_mode);
3019
3020static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3021{
3022 int ret;
3023
3024 mutex_lock(&rdev->mutex);
3025
3026 /* sanity check */
3027 if (!rdev->desc->ops->get_mode) {
3028 ret = -EINVAL;
3029 goto out;
3030 }
3031
3032 ret = rdev->desc->ops->get_mode(rdev);
3033out:
3034 mutex_unlock(&rdev->mutex);
3035 return ret;
3036}
3037
3038/**
3039 * regulator_get_mode - get regulator operating mode
3040 * @regulator: regulator source
3041 *
3042 * Get the current regulator operating mode.
3043 */
3044unsigned int regulator_get_mode(struct regulator *regulator)
3045{
3046 return _regulator_get_mode(regulator->rdev);
3047}
3048EXPORT_SYMBOL_GPL(regulator_get_mode);
3049
3050/**
3051 * regulator_set_optimum_mode - set regulator optimum operating mode
3052 * @regulator: regulator source
3053 * @uA_load: load current
3054 *
3055 * Notifies the regulator core of a new device load. This is then used by
3056 * DRMS (if enabled by constraints) to set the most efficient regulator
3057 * operating mode for the new regulator loading.
3058 *
3059 * Consumer devices notify their supply regulator of the maximum power
3060 * they will require (can be taken from device datasheet in the power
3061 * consumption tables) when they change operational status and hence power
3062 * state. Examples of operational state changes that can affect power
3063 * consumption are :-
3064 *
3065 * o Device is opened / closed.
3066 * o Device I/O is about to begin or has just finished.
3067 * o Device is idling in between work.
3068 *
3069 * This information is also exported via sysfs to userspace.
3070 *
3071 * DRMS will sum the total requested load on the regulator and change
3072 * to the most efficient operating mode if platform constraints allow.
3073 *
3074 * Returns the new regulator mode or error.
3075 */
3076int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
3077{
3078 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003079 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003080
Liam Girdwood414c70c2008-04-30 15:59:04 +01003081 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003082 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003083 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003084 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003085
Liam Girdwood414c70c2008-04-30 15:59:04 +01003086 return ret;
3087}
3088EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
3089
3090/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003091 * regulator_allow_bypass - allow the regulator to go into bypass mode
3092 *
3093 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003094 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003095 *
3096 * Allow the regulator to go into bypass mode if all other consumers
3097 * for the regulator also enable bypass mode and the machine
3098 * constraints allow this. Bypass mode means that the regulator is
3099 * simply passing the input directly to the output with no regulation.
3100 */
3101int regulator_allow_bypass(struct regulator *regulator, bool enable)
3102{
3103 struct regulator_dev *rdev = regulator->rdev;
3104 int ret = 0;
3105
3106 if (!rdev->desc->ops->set_bypass)
3107 return 0;
3108
3109 if (rdev->constraints &&
3110 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3111 return 0;
3112
3113 mutex_lock(&rdev->mutex);
3114
3115 if (enable && !regulator->bypass) {
3116 rdev->bypass_count++;
3117
3118 if (rdev->bypass_count == rdev->open_count) {
3119 ret = rdev->desc->ops->set_bypass(rdev, enable);
3120 if (ret != 0)
3121 rdev->bypass_count--;
3122 }
3123
3124 } else if (!enable && regulator->bypass) {
3125 rdev->bypass_count--;
3126
3127 if (rdev->bypass_count != rdev->open_count) {
3128 ret = rdev->desc->ops->set_bypass(rdev, enable);
3129 if (ret != 0)
3130 rdev->bypass_count++;
3131 }
3132 }
3133
3134 if (ret == 0)
3135 regulator->bypass = enable;
3136
3137 mutex_unlock(&rdev->mutex);
3138
3139 return ret;
3140}
3141EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3142
3143/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003144 * regulator_register_notifier - register regulator event notifier
3145 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003146 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003147 *
3148 * Register notifier block to receive regulator events.
3149 */
3150int regulator_register_notifier(struct regulator *regulator,
3151 struct notifier_block *nb)
3152{
3153 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3154 nb);
3155}
3156EXPORT_SYMBOL_GPL(regulator_register_notifier);
3157
3158/**
3159 * regulator_unregister_notifier - unregister regulator event notifier
3160 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003161 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003162 *
3163 * Unregister regulator event notifier block.
3164 */
3165int regulator_unregister_notifier(struct regulator *regulator,
3166 struct notifier_block *nb)
3167{
3168 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3169 nb);
3170}
3171EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3172
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003173/* notify regulator consumers and downstream regulator consumers.
3174 * Note mutex must be held by caller.
3175 */
Heiko Stübner71795692014-08-28 12:36:04 -07003176static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003177 unsigned long event, void *data)
3178{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003179 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003180 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003181}
3182
3183/**
3184 * regulator_bulk_get - get multiple regulator consumers
3185 *
3186 * @dev: Device to supply
3187 * @num_consumers: Number of consumers to register
3188 * @consumers: Configuration of consumers; clients are stored here.
3189 *
3190 * @return 0 on success, an errno on failure.
3191 *
3192 * This helper function allows drivers to get several regulator
3193 * consumers in one operation. If any of the regulators cannot be
3194 * acquired then any regulators that were allocated will be freed
3195 * before returning to the caller.
3196 */
3197int regulator_bulk_get(struct device *dev, int num_consumers,
3198 struct regulator_bulk_data *consumers)
3199{
3200 int i;
3201 int ret;
3202
3203 for (i = 0; i < num_consumers; i++)
3204 consumers[i].consumer = NULL;
3205
3206 for (i = 0; i < num_consumers; i++) {
3207 consumers[i].consumer = regulator_get(dev,
3208 consumers[i].supply);
3209 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003210 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003211 dev_err(dev, "Failed to get supply '%s': %d\n",
3212 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003213 consumers[i].consumer = NULL;
3214 goto err;
3215 }
3216 }
3217
3218 return 0;
3219
3220err:
Axel Linb29c7692012-02-20 10:32:16 +08003221 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003222 regulator_put(consumers[i].consumer);
3223
3224 return ret;
3225}
3226EXPORT_SYMBOL_GPL(regulator_bulk_get);
3227
Mark Brownf21e0e82011-05-24 08:12:40 +08003228static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3229{
3230 struct regulator_bulk_data *bulk = data;
3231
3232 bulk->ret = regulator_enable(bulk->consumer);
3233}
3234
Liam Girdwood414c70c2008-04-30 15:59:04 +01003235/**
3236 * regulator_bulk_enable - enable multiple regulator consumers
3237 *
3238 * @num_consumers: Number of consumers
3239 * @consumers: Consumer data; clients are stored here.
3240 * @return 0 on success, an errno on failure
3241 *
3242 * This convenience API allows consumers to enable multiple regulator
3243 * clients in a single API call. If any consumers cannot be enabled
3244 * then any others that were enabled will be disabled again prior to
3245 * return.
3246 */
3247int regulator_bulk_enable(int num_consumers,
3248 struct regulator_bulk_data *consumers)
3249{
Dan Williams2955b472012-07-09 19:33:25 -07003250 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003251 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003252 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003253
Mark Brown6492bc12012-04-19 13:19:07 +01003254 for (i = 0; i < num_consumers; i++) {
3255 if (consumers[i].consumer->always_on)
3256 consumers[i].ret = 0;
3257 else
3258 async_schedule_domain(regulator_bulk_enable_async,
3259 &consumers[i], &async_domain);
3260 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003261
3262 async_synchronize_full_domain(&async_domain);
3263
3264 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003265 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003266 if (consumers[i].ret != 0) {
3267 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003268 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003269 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003270 }
3271
3272 return 0;
3273
3274err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003275 for (i = 0; i < num_consumers; i++) {
3276 if (consumers[i].ret < 0)
3277 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3278 consumers[i].ret);
3279 else
3280 regulator_disable(consumers[i].consumer);
3281 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003282
3283 return ret;
3284}
3285EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3286
3287/**
3288 * regulator_bulk_disable - disable multiple regulator consumers
3289 *
3290 * @num_consumers: Number of consumers
3291 * @consumers: Consumer data; clients are stored here.
3292 * @return 0 on success, an errno on failure
3293 *
3294 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003295 * clients in a single API call. If any consumers cannot be disabled
3296 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003297 * return.
3298 */
3299int regulator_bulk_disable(int num_consumers,
3300 struct regulator_bulk_data *consumers)
3301{
3302 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003303 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003304
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003305 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003306 ret = regulator_disable(consumers[i].consumer);
3307 if (ret != 0)
3308 goto err;
3309 }
3310
3311 return 0;
3312
3313err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003314 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003315 for (++i; i < num_consumers; ++i) {
3316 r = regulator_enable(consumers[i].consumer);
3317 if (r != 0)
3318 pr_err("Failed to reename %s: %d\n",
3319 consumers[i].supply, r);
3320 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003321
3322 return ret;
3323}
3324EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3325
3326/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003327 * regulator_bulk_force_disable - force disable multiple regulator consumers
3328 *
3329 * @num_consumers: Number of consumers
3330 * @consumers: Consumer data; clients are stored here.
3331 * @return 0 on success, an errno on failure
3332 *
3333 * This convenience API allows consumers to forcibly disable multiple regulator
3334 * clients in a single API call.
3335 * NOTE: This should be used for situations when device damage will
3336 * likely occur if the regulators are not disabled (e.g. over temp).
3337 * Although regulator_force_disable function call for some consumers can
3338 * return error numbers, the function is called for all consumers.
3339 */
3340int regulator_bulk_force_disable(int num_consumers,
3341 struct regulator_bulk_data *consumers)
3342{
3343 int i;
3344 int ret;
3345
3346 for (i = 0; i < num_consumers; i++)
3347 consumers[i].ret =
3348 regulator_force_disable(consumers[i].consumer);
3349
3350 for (i = 0; i < num_consumers; i++) {
3351 if (consumers[i].ret != 0) {
3352 ret = consumers[i].ret;
3353 goto out;
3354 }
3355 }
3356
3357 return 0;
3358out:
3359 return ret;
3360}
3361EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3362
3363/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003364 * regulator_bulk_free - free multiple regulator consumers
3365 *
3366 * @num_consumers: Number of consumers
3367 * @consumers: Consumer data; clients are stored here.
3368 *
3369 * This convenience API allows consumers to free multiple regulator
3370 * clients in a single API call.
3371 */
3372void regulator_bulk_free(int num_consumers,
3373 struct regulator_bulk_data *consumers)
3374{
3375 int i;
3376
3377 for (i = 0; i < num_consumers; i++) {
3378 regulator_put(consumers[i].consumer);
3379 consumers[i].consumer = NULL;
3380 }
3381}
3382EXPORT_SYMBOL_GPL(regulator_bulk_free);
3383
3384/**
3385 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003386 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003387 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003388 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003389 *
3390 * Called by regulator drivers to notify clients a regulator event has
3391 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003392 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003393 */
3394int regulator_notifier_call_chain(struct regulator_dev *rdev,
3395 unsigned long event, void *data)
3396{
3397 _notifier_call_chain(rdev, event, data);
3398 return NOTIFY_DONE;
3399
3400}
3401EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3402
Mark Brownbe721972009-08-04 20:09:52 +02003403/**
3404 * regulator_mode_to_status - convert a regulator mode into a status
3405 *
3406 * @mode: Mode to convert
3407 *
3408 * Convert a regulator mode into a status.
3409 */
3410int regulator_mode_to_status(unsigned int mode)
3411{
3412 switch (mode) {
3413 case REGULATOR_MODE_FAST:
3414 return REGULATOR_STATUS_FAST;
3415 case REGULATOR_MODE_NORMAL:
3416 return REGULATOR_STATUS_NORMAL;
3417 case REGULATOR_MODE_IDLE:
3418 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003419 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003420 return REGULATOR_STATUS_STANDBY;
3421 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003422 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003423 }
3424}
3425EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3426
Takashi Iwai39f802d2015-01-30 20:29:31 +01003427static struct attribute *regulator_dev_attrs[] = {
3428 &dev_attr_name.attr,
3429 &dev_attr_num_users.attr,
3430 &dev_attr_type.attr,
3431 &dev_attr_microvolts.attr,
3432 &dev_attr_microamps.attr,
3433 &dev_attr_opmode.attr,
3434 &dev_attr_state.attr,
3435 &dev_attr_status.attr,
3436 &dev_attr_bypass.attr,
3437 &dev_attr_requested_microamps.attr,
3438 &dev_attr_min_microvolts.attr,
3439 &dev_attr_max_microvolts.attr,
3440 &dev_attr_min_microamps.attr,
3441 &dev_attr_max_microamps.attr,
3442 &dev_attr_suspend_standby_state.attr,
3443 &dev_attr_suspend_mem_state.attr,
3444 &dev_attr_suspend_disk_state.attr,
3445 &dev_attr_suspend_standby_microvolts.attr,
3446 &dev_attr_suspend_mem_microvolts.attr,
3447 &dev_attr_suspend_disk_microvolts.attr,
3448 &dev_attr_suspend_standby_mode.attr,
3449 &dev_attr_suspend_mem_mode.attr,
3450 &dev_attr_suspend_disk_mode.attr,
3451 NULL
3452};
3453
David Brownell7ad68e22008-11-11 17:39:02 -08003454/*
3455 * To avoid cluttering sysfs (and memory) with useless state, only
3456 * create attributes that can be meaningfully displayed.
3457 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003458static umode_t regulator_attr_is_visible(struct kobject *kobj,
3459 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003460{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003461 struct device *dev = kobj_to_dev(kobj);
3462 struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003463 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003464 umode_t mode = attr->mode;
3465
3466 /* these three are always present */
3467 if (attr == &dev_attr_name.attr ||
3468 attr == &dev_attr_num_users.attr ||
3469 attr == &dev_attr_type.attr)
3470 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003471
3472 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003473 if (attr == &dev_attr_microvolts.attr) {
3474 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3475 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3476 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3477 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3478 return mode;
3479 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003480 }
David Brownell7ad68e22008-11-11 17:39:02 -08003481
Takashi Iwai39f802d2015-01-30 20:29:31 +01003482 if (attr == &dev_attr_microamps.attr)
3483 return ops->get_current_limit ? mode : 0;
3484
3485 if (attr == &dev_attr_opmode.attr)
3486 return ops->get_mode ? mode : 0;
3487
3488 if (attr == &dev_attr_state.attr)
3489 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3490
3491 if (attr == &dev_attr_status.attr)
3492 return ops->get_status ? mode : 0;
3493
3494 if (attr == &dev_attr_bypass.attr)
3495 return ops->get_bypass ? mode : 0;
3496
David Brownell7ad68e22008-11-11 17:39:02 -08003497 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003498 if (attr == &dev_attr_requested_microamps.attr)
3499 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003500
3501 /* all the other attributes exist to support constraints;
3502 * don't show them if there are no constraints, or if the
3503 * relevant supporting methods are missing.
3504 */
3505 if (!rdev->constraints)
Takashi Iwai39f802d2015-01-30 20:29:31 +01003506 return 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003507
3508 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003509 if (attr == &dev_attr_min_microvolts.attr ||
3510 attr == &dev_attr_max_microvolts.attr)
3511 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003512
Takashi Iwai39f802d2015-01-30 20:29:31 +01003513 if (attr == &dev_attr_min_microamps.attr ||
3514 attr == &dev_attr_max_microamps.attr)
3515 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003516
Takashi Iwai39f802d2015-01-30 20:29:31 +01003517 if (attr == &dev_attr_suspend_standby_state.attr ||
3518 attr == &dev_attr_suspend_mem_state.attr ||
3519 attr == &dev_attr_suspend_disk_state.attr)
3520 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003521
Takashi Iwai39f802d2015-01-30 20:29:31 +01003522 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3523 attr == &dev_attr_suspend_mem_microvolts.attr ||
3524 attr == &dev_attr_suspend_disk_microvolts.attr)
3525 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003526
Takashi Iwai39f802d2015-01-30 20:29:31 +01003527 if (attr == &dev_attr_suspend_standby_mode.attr ||
3528 attr == &dev_attr_suspend_mem_mode.attr ||
3529 attr == &dev_attr_suspend_disk_mode.attr)
3530 return ops->set_suspend_mode ? mode : 0;
3531
3532 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003533}
3534
Takashi Iwai39f802d2015-01-30 20:29:31 +01003535static const struct attribute_group regulator_dev_group = {
3536 .attrs = regulator_dev_attrs,
3537 .is_visible = regulator_attr_is_visible,
3538};
3539
3540static const struct attribute_group *regulator_dev_groups[] = {
3541 &regulator_dev_group,
3542 NULL
3543};
3544
3545static void regulator_dev_release(struct device *dev)
3546{
3547 struct regulator_dev *rdev = dev_get_drvdata(dev);
3548 kfree(rdev);
3549}
3550
3551static struct class regulator_class = {
3552 .name = "regulator",
3553 .dev_release = regulator_dev_release,
3554 .dev_groups = regulator_dev_groups,
3555};
3556
Mark Brown1130e5b2010-12-21 23:49:31 +00003557static void rdev_init_debugfs(struct regulator_dev *rdev)
3558{
Mark Brown1130e5b2010-12-21 23:49:31 +00003559 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003560 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003561 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003562 return;
3563 }
3564
3565 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3566 &rdev->use_count);
3567 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3568 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003569 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3570 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003571}
3572
Liam Girdwood414c70c2008-04-30 15:59:04 +01003573/**
3574 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003575 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01003576 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003577 *
3578 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003579 * Returns a valid pointer to struct regulator_dev on success
3580 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003581 */
Mark Brown65f26842012-04-03 20:46:53 +01003582struct regulator_dev *
3583regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003584 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003585{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003586 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003587 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003588 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303589 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003590 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003591 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003592 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003593
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003594 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003595 return ERR_PTR(-EINVAL);
3596
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003597 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003598 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003599
Liam Girdwood414c70c2008-04-30 15:59:04 +01003600 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3601 return ERR_PTR(-EINVAL);
3602
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003603 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3604 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003605 return ERR_PTR(-EINVAL);
3606
Mark Brown476c2d82010-12-10 17:28:07 +00003607 /* Only one of each should be implemented */
3608 WARN_ON(regulator_desc->ops->get_voltage &&
3609 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003610 WARN_ON(regulator_desc->ops->set_voltage &&
3611 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003612
3613 /* If we're using selectors we must implement list_voltage. */
3614 if (regulator_desc->ops->get_voltage_sel &&
3615 !regulator_desc->ops->list_voltage) {
3616 return ERR_PTR(-EINVAL);
3617 }
Mark Browne8eef822010-12-12 14:36:17 +00003618 if (regulator_desc->ops->set_voltage_sel &&
3619 !regulator_desc->ops->list_voltage) {
3620 return ERR_PTR(-EINVAL);
3621 }
Mark Brown476c2d82010-12-10 17:28:07 +00003622
Liam Girdwood414c70c2008-04-30 15:59:04 +01003623 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3624 if (rdev == NULL)
3625 return ERR_PTR(-ENOMEM);
3626
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003627 /*
3628 * Duplicate the config so the driver could override it after
3629 * parsing init data.
3630 */
3631 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3632 if (config == NULL) {
3633 kfree(rdev);
3634 return ERR_PTR(-ENOMEM);
3635 }
3636
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01003637 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01003638 &rdev->dev.of_node);
3639 if (!init_data) {
3640 init_data = config->init_data;
3641 rdev->dev.of_node = of_node_get(config->of_node);
3642 }
3643
Liam Girdwood414c70c2008-04-30 15:59:04 +01003644 mutex_lock(&regulator_list_mutex);
3645
3646 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003647 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003648 rdev->owner = regulator_desc->owner;
3649 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003650 if (config->regmap)
3651 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303652 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003653 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303654 else if (dev->parent)
3655 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003656 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003657 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003658 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003659 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003660
Liam Girdwooda5766f12008-10-10 13:22:20 +01003661 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003662 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003663 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003664 if (ret < 0)
3665 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003666 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003667
Liam Girdwooda5766f12008-10-10 13:22:20 +01003668 /* register with sysfs */
3669 rdev->dev.class = &regulator_class;
3670 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303671 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05303672 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01003673 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003674 if (ret != 0) {
3675 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003676 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003677 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003678
3679 dev_set_drvdata(&rdev->dev, rdev);
3680
Markus Pargmann76f439d2014-10-08 15:47:05 +02003681 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3682 gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003683 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003684 if (ret != 0) {
3685 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3686 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003687 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003688 }
3689
Mark Brown65f73502012-06-27 14:14:38 +01003690 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3691 rdev->ena_gpio_state = 1;
3692
Kim, Milo7b74d142013-02-18 06:50:55 +00003693 if (config->ena_gpio_invert)
Mark Brown65f73502012-06-27 14:14:38 +01003694 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3695 }
3696
Mike Rapoport74f544c2008-11-25 14:53:53 +02003697 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003698 if (init_data)
3699 constraints = &init_data->constraints;
3700
3701 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003702 if (ret < 0)
3703 goto scrub;
3704
Mark Brown9a8f5e02011-11-29 18:11:19 +00003705 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003706 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303707 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003708 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303709
Liam Girdwooda5766f12008-10-10 13:22:20 +01003710 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003711 if (init_data) {
3712 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3713 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003714 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003715 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003716 if (ret < 0) {
3717 dev_err(dev, "Failed to set supply %s\n",
3718 init_data->consumer_supplies[i].supply);
3719 goto unset_supplies;
3720 }
Mark Brown23c2f042011-02-24 17:39:09 +00003721 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003722 }
3723
3724 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003725
3726 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003727out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003728 mutex_unlock(&regulator_list_mutex);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003729 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003730 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003731
Jani Nikulad4033b52010-04-29 10:55:11 +03003732unset_supplies:
3733 unset_regulator_supplies(rdev);
3734
David Brownell4fca9542008-11-11 17:38:53 -08003735scrub:
Kim, Milof19b00d2013-02-18 06:50:39 +00003736 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003737 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003738wash:
David Brownell4fca9542008-11-11 17:38:53 -08003739 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003740 /* device core frees rdev */
3741 rdev = ERR_PTR(ret);
3742 goto out;
3743
David Brownell4fca9542008-11-11 17:38:53 -08003744clean:
3745 kfree(rdev);
3746 rdev = ERR_PTR(ret);
3747 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003748}
3749EXPORT_SYMBOL_GPL(regulator_register);
3750
3751/**
3752 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003753 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003754 *
3755 * Called by regulator drivers to unregister a regulator.
3756 */
3757void regulator_unregister(struct regulator_dev *rdev)
3758{
3759 if (rdev == NULL)
3760 return;
3761
Mark Brown891636e2013-07-08 09:14:45 +01003762 if (rdev->supply) {
3763 while (rdev->use_count--)
3764 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01003765 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01003766 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003767 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003768 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003769 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003770 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003771 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003772 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003773 kfree(rdev->constraints);
Kim, Milof19b00d2013-02-18 06:50:39 +00003774 regulator_ena_gpio_free(rdev);
Charles Keepax63c7c9e2014-04-03 15:32:17 +01003775 of_node_put(rdev->dev.of_node);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003776 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003777 mutex_unlock(&regulator_list_mutex);
3778}
3779EXPORT_SYMBOL_GPL(regulator_unregister);
3780
3781/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003782 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003783 * @state: system suspend state
3784 *
3785 * Configure each regulator with it's suspend operating parameters for state.
3786 * This will usually be called by machine suspend code prior to supending.
3787 */
3788int regulator_suspend_prepare(suspend_state_t state)
3789{
3790 struct regulator_dev *rdev;
3791 int ret = 0;
3792
3793 /* ON is handled by regulator active state */
3794 if (state == PM_SUSPEND_ON)
3795 return -EINVAL;
3796
3797 mutex_lock(&regulator_list_mutex);
3798 list_for_each_entry(rdev, &regulator_list, list) {
3799
3800 mutex_lock(&rdev->mutex);
3801 ret = suspend_prepare(rdev, state);
3802 mutex_unlock(&rdev->mutex);
3803
3804 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003805 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003806 goto out;
3807 }
3808 }
3809out:
3810 mutex_unlock(&regulator_list_mutex);
3811 return ret;
3812}
3813EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3814
3815/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003816 * regulator_suspend_finish - resume regulators from system wide suspend
3817 *
3818 * Turn on regulators that might be turned off by regulator_suspend_prepare
3819 * and that should be turned on according to the regulators properties.
3820 */
3821int regulator_suspend_finish(void)
3822{
3823 struct regulator_dev *rdev;
3824 int ret = 0, error;
3825
3826 mutex_lock(&regulator_list_mutex);
3827 list_for_each_entry(rdev, &regulator_list, list) {
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003828 mutex_lock(&rdev->mutex);
Markus Pargmann30c21972014-02-20 17:36:03 +01003829 if (rdev->use_count > 0 || rdev->constraints->always_on) {
3830 error = _regulator_do_enable(rdev);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003831 if (error)
3832 ret = error;
3833 } else {
Mark Brown87b28412013-11-27 16:13:10 +00003834 if (!have_full_constraints())
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003835 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003836 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003837 goto unlock;
3838
Markus Pargmann66fda752014-02-20 17:36:04 +01003839 error = _regulator_do_disable(rdev);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003840 if (error)
3841 ret = error;
3842 }
3843unlock:
3844 mutex_unlock(&rdev->mutex);
3845 }
3846 mutex_unlock(&regulator_list_mutex);
3847 return ret;
3848}
3849EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3850
3851/**
Mark Brownca725562009-03-16 19:36:34 +00003852 * regulator_has_full_constraints - the system has fully specified constraints
3853 *
3854 * Calling this function will cause the regulator API to disable all
3855 * regulators which have a zero use count and don't have an always_on
3856 * constraint in a late_initcall.
3857 *
3858 * The intention is that this will become the default behaviour in a
3859 * future kernel release so users are encouraged to use this facility
3860 * now.
3861 */
3862void regulator_has_full_constraints(void)
3863{
3864 has_full_constraints = 1;
3865}
3866EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3867
3868/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003869 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003870 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003871 *
3872 * Get rdev regulator driver private data. This call can be used in the
3873 * regulator driver context.
3874 */
3875void *rdev_get_drvdata(struct regulator_dev *rdev)
3876{
3877 return rdev->reg_data;
3878}
3879EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3880
3881/**
3882 * regulator_get_drvdata - get regulator driver data
3883 * @regulator: regulator
3884 *
3885 * Get regulator driver private data. This call can be used in the consumer
3886 * driver context when non API regulator specific functions need to be called.
3887 */
3888void *regulator_get_drvdata(struct regulator *regulator)
3889{
3890 return regulator->rdev->reg_data;
3891}
3892EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3893
3894/**
3895 * regulator_set_drvdata - set regulator driver data
3896 * @regulator: regulator
3897 * @data: data
3898 */
3899void regulator_set_drvdata(struct regulator *regulator, void *data)
3900{
3901 regulator->rdev->reg_data = data;
3902}
3903EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3904
3905/**
3906 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003907 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003908 */
3909int rdev_get_id(struct regulator_dev *rdev)
3910{
3911 return rdev->desc->id;
3912}
3913EXPORT_SYMBOL_GPL(rdev_get_id);
3914
Liam Girdwooda5766f12008-10-10 13:22:20 +01003915struct device *rdev_get_dev(struct regulator_dev *rdev)
3916{
3917 return &rdev->dev;
3918}
3919EXPORT_SYMBOL_GPL(rdev_get_dev);
3920
3921void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3922{
3923 return reg_init_data->driver_data;
3924}
3925EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3926
Mark Brownba55a972011-08-23 17:39:10 +01003927#ifdef CONFIG_DEBUG_FS
3928static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3929 size_t count, loff_t *ppos)
3930{
3931 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3932 ssize_t len, ret = 0;
3933 struct regulator_map *map;
3934
3935 if (!buf)
3936 return -ENOMEM;
3937
3938 list_for_each_entry(map, &regulator_map_list, list) {
3939 len = snprintf(buf + ret, PAGE_SIZE - ret,
3940 "%s -> %s.%s\n",
3941 rdev_get_name(map->regulator), map->dev_name,
3942 map->supply);
3943 if (len >= 0)
3944 ret += len;
3945 if (ret > PAGE_SIZE) {
3946 ret = PAGE_SIZE;
3947 break;
3948 }
3949 }
3950
3951 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3952
3953 kfree(buf);
3954
3955 return ret;
3956}
Stephen Boyd24751432012-02-20 22:50:42 -08003957#endif
Mark Brownba55a972011-08-23 17:39:10 +01003958
3959static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003960#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003961 .read = supply_map_read_file,
3962 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003963#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003964};
Mark Brownba55a972011-08-23 17:39:10 +01003965
Liam Girdwood414c70c2008-04-30 15:59:04 +01003966static int __init regulator_init(void)
3967{
Mark Brown34abbd62010-02-12 10:18:08 +00003968 int ret;
3969
Mark Brown34abbd62010-02-12 10:18:08 +00003970 ret = class_register(&regulator_class);
3971
Mark Brown1130e5b2010-12-21 23:49:31 +00003972 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003973 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003974 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003975
Mark Brownf4d562c2012-02-20 21:01:04 +00003976 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3977 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003978
Mark Brown34abbd62010-02-12 10:18:08 +00003979 regulator_dummy_init();
3980
3981 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003982}
3983
3984/* init early to allow our consumers to complete system booting */
3985core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003986
3987static int __init regulator_init_complete(void)
3988{
3989 struct regulator_dev *rdev;
Guodong Xu272e2312014-08-13 19:33:38 +08003990 const struct regulator_ops *ops;
Mark Brownca725562009-03-16 19:36:34 +00003991 struct regulation_constraints *c;
3992 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003993
Mark Brown86f5fcf2012-07-06 18:19:13 +01003994 /*
3995 * Since DT doesn't provide an idiomatic mechanism for
3996 * enabling full constraints and since it's much more natural
3997 * with DT to provide them just assume that a DT enabled
3998 * system has full constraints.
3999 */
4000 if (of_have_populated_dt())
4001 has_full_constraints = true;
4002
Mark Brownca725562009-03-16 19:36:34 +00004003 mutex_lock(&regulator_list_mutex);
4004
4005 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004006 * we have permission to change the status for and which are
4007 * not in use or always_on. This is effectively the default
4008 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004009 */
4010 list_for_each_entry(rdev, &regulator_list, list) {
4011 ops = rdev->desc->ops;
4012 c = rdev->constraints;
4013
Markus Pargmann66fda752014-02-20 17:36:04 +01004014 if (c && c->always_on)
Mark Brownca725562009-03-16 19:36:34 +00004015 continue;
4016
Mark Browne9535832014-06-01 19:15:16 +01004017 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4018 continue;
4019
Mark Brownca725562009-03-16 19:36:34 +00004020 mutex_lock(&rdev->mutex);
4021
4022 if (rdev->use_count)
4023 goto unlock;
4024
4025 /* If we can't read the status assume it's on. */
4026 if (ops->is_enabled)
4027 enabled = ops->is_enabled(rdev);
4028 else
4029 enabled = 1;
4030
4031 if (!enabled)
4032 goto unlock;
4033
Mark Brown87b28412013-11-27 16:13:10 +00004034 if (have_full_constraints()) {
Mark Brownca725562009-03-16 19:36:34 +00004035 /* We log since this may kill the system if it
4036 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08004037 rdev_info(rdev, "disabling\n");
Markus Pargmann66fda752014-02-20 17:36:04 +01004038 ret = _regulator_do_disable(rdev);
Jingoo Han0d25d092014-01-08 10:02:16 +09004039 if (ret != 0)
Joe Perches5da84fd2010-11-30 05:53:48 -08004040 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00004041 } else {
4042 /* The intention is that in future we will
4043 * assume that full constraints are provided
4044 * so warn even if we aren't going to do
4045 * anything here.
4046 */
Joe Perches5da84fd2010-11-30 05:53:48 -08004047 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00004048 }
4049
4050unlock:
4051 mutex_unlock(&rdev->mutex);
4052 }
4053
4054 mutex_unlock(&regulator_list_mutex);
4055
4056 return 0;
4057}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004058late_initcall_sync(regulator_init_complete);